-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecshell.c
186 lines (150 loc) · 4.42 KB
/
execshell.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* see LICENSE file for license details */
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <spawn.h>
#include <stdlib.h>
#include <unistd.h>
#include <skalibs/env.h>
#include <skalibs/stralloc.h>
#include <execline/execline.h>
#define UTF8
#include "linenoise/linenoise.h"
#ifdef UTF8
#include "linenoise/encodings/utf8.h"
#endif
#define EXECSHELL_FLAGVAR "EXECSHELL_RUNNING"
#define EXECSHELL_EXIT 112
#define EXECSHELL_BUILTIN "self"
static void *xmalloc(size_t sz) {
void *ret;
ret = malloc(sz);
if (ret == NULL) {
err(1, "could not malloc");
}
return ret;
}
static void signal_setup() {
for (int i = 1; i < NSIG; i++) {
if (i != SIGKILL && i != SIGSTOP && i != SIGSEGV) {
signal(i, (i == SIGCHLD ? SIG_DFL : SIG_IGN));
}
}
}
static void signal_unsetup() {
for (int i = 1; i < NSIG; i++) {
if (i != SIGKILL && i != SIGSTOP && i != SIGSEGV) {
signal(i, SIG_DFL);
}
}
}
int main(int argc, char *argv[]) {
int ret, wstatus, estatus;
pid_t pid, pret;
char *buffer, **pargs, **eargs;
stralloc sa = STRALLOC_ZERO;
(void) argc;
if (getenv(EXECSHELL_FLAGVAR) == NULL) {
/* parent process */
signal_setup();
while ((pid = fork()) > 0) {
if (waitpid(pid, &wstatus, 0) == -1) {
err(1, "parent could not wait on command runner");
}
if (WIFEXITED(wstatus)) {
/* normal exit */
estatus = WEXITSTATUS(wstatus);
if (estatus == 0 || estatus == EXECSHELL_EXIT) {
/* clean exit or internal error */
exit(estatus);
}
} else {
/* it didn't exit normally, so something must be up */
errx(1, "command runner process died with signal");
}
}
if (pid == -1) {
err(1, "parent process could not fork");
}
} else {
if (unsetenv(EXECSHELL_FLAGVAR) == -1) {
warn("could not unset flag environment variable");
}
}
/* child process */
(void) linenoiseHistorySetMaxLen(500);
#ifdef UTF8
/* set up unicode terminal handling bits */
linenoiseSetEncodingFunctions(linenoiseUtf8PrevCharLen,
linenoiseUtf8NextCharLen,
linenoiseUtf8ReadCode);
#endif
while (1) {
errno = 0;
buffer = linenoise("> ");
if (buffer == NULL) {
if (errno == EAGAIN) {
continue;
} else if (errno != 0) {
err(EXECSHELL_EXIT, "could not read input");
} else {
break;
}
}
(void) linenoiseHistoryAdd(buffer);
ret = el_parse_from_string(&sa, buffer);
switch (ret) {
case -4:
warnx("unmatched }");
goto mainloop_out;
case -3:
warnx("unmatched {");
goto mainloop_out;
case -2:
warnx("syntax error");
goto mainloop_out;
case -1:
warnx("unable to parse input");
goto mainloop_out;
case 0:
goto mainloop_out;
}
pargs = (char **) xmalloc(sizeof(char *) * (ret+2));
if (!env_make((const char **) pargs, ret, sa.s, sa.len)) {
err(1, "could not env_make");
}
pargs[ret] = 0;
if (strcmp(pargs[0], EXECSHELL_BUILTIN) == 0) {
pargs[ret] = argv[0];
pargs[ret+1] = 0;
eargs = pargs+1;
pid = 0;
if (setenv(EXECSHELL_FLAGVAR, "1", 1) == -1) {
err(1, "could not set flag environment variable");
}
} else {
eargs = pargs;
signal_setup();
pid = fork();
}
if (pid == -1) {
err(1, "could not fork");
} else if (pid == 0) {
signal_unsetup();
(void) execvp(eargs[0], eargs);
err(1, "could not execvp: %s", eargs[0]);
}
pret = waitpid(pid, NULL, 0);
if (pret == -1) {
warn("could not waitpid");
}
signal_unsetup();
free(pargs);
mainloop_out:
stralloc_free(&sa);
free(buffer);
}
return 0;
}