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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
#include "mk.h"
char *shell = "/bin/rc";
char *shellname = "rc";
static Word *encodenulls(char*, int);
void
readenv(void)
{
char *p;
int envf, f;
Dir *e;
char nam[1024];
int i, n, len;
Word *w;
rfork(RFENVG); /* use copy of the current environment variables */
envf = open("/env", OREAD);
if(envf < 0)
return;
while((n = dirread(envf, &e)) > 0){
for(i = 0; i < n; i++){
len = e[i].length;
/* don't import funny names, NULL values,
* or internal mk variables
*/
if(len <= 0 || *shname(e[i].name) != '\0')
continue;
if (symlook(e[i].name, S_INTERNAL, 0))
continue;
sprint(nam, "/env/%s", e[i].name);
f = open(nam, OREAD);
if(f < 0)
continue;
p = Malloc(len+1);
if(read(f, p, len) != len){
perror(nam);
close(f);
continue;
}
close(f);
if (p[len-1] == 0)
len--;
else
p[len] = 0;
w = encodenulls(p, len);
free(p);
p = strdup(e[i].name);
setvar(p, (void *) w);
symlook(p, S_EXPORTED, (void*)"")->value = (void*)"";
}
free(e);
}
close(envf);
}
/* break string of values into words at 01's or nulls*/
static Word *
encodenulls(char *s, int n)
{
Word *w, *head;
char *cp;
head = w = 0;
while (n-- > 0) {
for (cp = s; *cp && *cp != '\0'; cp++)
n--;
*cp = 0;
if (w) {
w->next = newword(s);
w = w->next;
} else
head = w = newword(s);
s = cp+1;
}
if (!head)
head = newword("");
return head;
}
/* as well as 01's, change blanks to nulls, so that rc will
* treat the words as separate arguments
*/
void
exportenv(Envy *e)
{
int f, n, hasvalue, first;
Word *w;
Symtab *sy;
char nam[256];
for(;e->name; e++){
sy = symlook(e->name, S_VAR, 0);
if (e->values == 0 || e->values->s == 0 || e->values->s[0] == 0)
hasvalue = 0;
else
hasvalue = 1;
if(sy == 0 && !hasvalue) /* non-existant null symbol */
continue;
sprint(nam, "/env/%s", e->name);
if (sy != 0 && !hasvalue) { /* Remove from environment */
/* we could remove it from the symbol table
* too, but we're in the child copy, and it
* would still remain in the parent's table.
*/
remove(nam);
delword(e->values);
e->values = 0; /* memory leak */
continue;
}
f = create(nam, OWRITE, 0666L);
if(f < 0) {
fprint(2, "can't create %s, f=%d\n", nam, f);
perror(nam);
continue;
}
first = 1;
for (w = e->values; w; w = w->next) {
n = strlen(w->s);
if (n) {
if(first)
first = 0;
else{
if (write (f, "\0", 1) != 1)
perror(nam);
}
if (write(f, w->s, n) != n)
perror(nam);
}
}
close(f);
}
}
int
waitfor(char *msg)
{
Waitmsg *w;
int pid;
if((w=wait()) == nil)
return -1;
strecpy(msg, msg+ERRMAX, w->msg);
pid = w->pid;
free(w);
return pid;
}
void
expunge(int pid, char *msg)
{
postnote(PNPROC, pid, msg);
}
int
execsh(char *args, char *cmd, Bufblock *buf, Envy *e)
{
char *p;
int tot, n, pid, in[2], out[2];
if(buf && pipe(out) < 0){
perror("pipe");
Exit();
}
pid = rfork(RFPROC|RFFDG|RFENVG);
if(pid < 0){
perror("mk rfork");
Exit();
}
if(pid == 0){
if(buf)
close(out[0]);
if(pipe(in) < 0){
perror("pipe");
Exit();
}
pid = fork();
if(pid < 0){
perror("mk fork");
Exit();
}
if(pid != 0){
dup(in[0], 0);
if(buf){
dup(out[1], 1);
close(out[1]);
}
close(in[0]);
close(in[1]);
if (e)
exportenv(e);
if(shflags)
execl(shell, shellname, shflags, args, nil);
else
execl(shell, shellname, args, nil);
perror(shell);
_exits("exec");
}
close(out[1]);
close(in[0]);
p = cmd+strlen(cmd);
while(cmd < p){
n = write(in[1], cmd, p-cmd);
if(n < 0)
break;
cmd += n;
}
close(in[1]);
_exits(0);
}
if(buf){
close(out[1]);
tot = 0;
for(;;){
if (buf->current >= buf->end)
growbuf(buf);
n = read(out[0], buf->current, buf->end-buf->current);
if(n <= 0)
break;
buf->current += n;
tot += n;
}
if (tot && buf->current[-1] == '\n')
buf->current--;
close(out[0]);
}
return pid;
}
int
pipecmd(char *cmd, Envy *e, int *fd)
{
int pid, pfd[2];
if(DEBUG(D_EXEC))
fprint(1, "pipecmd='%s'\n", cmd);/**/
if(fd && pipe(pfd) < 0){
perror("pipe");
Exit();
}
pid = rfork(RFPROC|RFFDG|RFENVG);
if(pid < 0){
perror("mk fork");
Exit();
}
if(pid == 0){
if(fd){
close(pfd[0]);
dup(pfd[1], 1);
close(pfd[1]);
}
if(e)
exportenv(e);
if(shflags)
execl(shell, shellname, shflags, "-c", cmd, nil);
else
execl(shell, shellname, "-c", cmd, nil);
perror(shell);
_exits("exec");
}
if(fd){
close(pfd[1]);
*fd = pfd[0];
}
return pid;
}
void
Exit(void)
{
while(waitpid() >= 0)
;
exits("error");
}
int
notifyf(void *a, char *msg)
{
static int nnote;
USED(a);
if(++nnote > 100){ /* until andrew fixes his program */
fprint(2, "mk: too many notes\n");
notify(0);
abort();
}
if(strcmp(msg, "interrupt")!=0 && strcmp(msg, "hangup")!=0)
return 0;
killchildren(msg);
return -1;
}
void
catchnotes()
{
atnotify(notifyf, 1);
}
char*
maketmp(void)
{
static char temp[] = "/tmp/mkargXXXXXX";
mktemp(temp);
return temp;
}
int
chgtime(char *name)
{
Dir sbuf;
if(access(name, AEXIST) >= 0) {
nulldir(&sbuf);
sbuf.mtime = time(0);
return dirwstat(name, &sbuf);
}
return close(create(name, OWRITE, 0666));
}
void
rcopy(char **to, Resub *match, int n)
{
int c;
char *p;
*to = match->s.sp; /* stem0 matches complete target */
for(to++, match++; --n > 0; to++, match++){
if(match->s.sp && match->e.ep){
p = match->e.ep;
c = *p;
*p = 0;
*to = strdup(match->s.sp);
*p = c;
}
else
*to = 0;
}
}
ulong
mkmtime(char *name)
{
Dir *buf;
ulong t;
buf = dirstat(name);
if(buf == nil)
return 0;
t = buf->mtime;
free(buf);
return t;
}
char *
membername(char *s, int, char*)
{
return s;
}
|