forked from dhobsd/castty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
79 lines (61 loc) · 1.2 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define _XOPEN_SOURCE
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include "castty.h"
void
shellproc(const char *shell, const char *exec_cmd, struct winsize *win, int masterfd)
{
char *pt_name;
int slavefd;
slavefd = -1;
if (setsid() == -1) {
perror("setsid");
goto end;
}
if (grantpt(masterfd) == -1) {
perror("grantpt");
goto end;
}
if (unlockpt(masterfd) == -1) {
perror("unlockpt");
goto end;
}
pt_name = ptsname(masterfd);
if (pt_name == NULL) {
perror("ptsname");
goto end;
}
if ((slavefd = open(pt_name, O_RDWR)) < 0) {
perror("open(ptsname)");
goto end;
}
if (ioctl(slavefd, TIOCSCTTY, 0) == -1) {
perror("ioctl(TIOCSCTTY)");
goto end;
}
if (ioctl(slavefd, TIOCSWINSZ, win)) {
perror("ioctl(TIOCSWINSZ)");
goto end;
}
xclose(masterfd);
xdup2(slavefd, 0);
xdup2(slavefd, 1);
xdup2(slavefd, 2);
xclose(slavefd);
if (!exec_cmd) {
execl(shell, strrchr(shell, '/') + 1, "-i", NULL);
} else {
execl(shell, strrchr(shell, '/') + 1, "-c", exec_cmd, NULL);
}
perror("execl");
end:
if (kill(0, SIGTERM) == -1) {
perror("kill");
}
}