-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserdo.c
More file actions
225 lines (212 loc) · 4.78 KB
/
serdo.c
File metadata and controls
225 lines (212 loc) · 4.78 KB
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
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <libowfat/str.h>
#include <libowfat/byte.h>
#include <libowfat/scan.h>
#include <libowfat/errmsg.h>
#define MAXENV 256
char* envp[MAXENV+2];
int envc;
int continueonerror;
int envset(char* s) {
int i,l;
if (s[l=str_chr(s,'=')]!='=') return -1;
++l;
for (i=0; i<envc; ++i)
if (byte_equal(envp[i],l,s)) {
envp[i]=s;
return 0;
}
if (envc<MAXENV) {
envp[envc]=s;
envp[++envc]=0;
return 0;
}
return -1;
}
int spawn(char** argv, int last) {
int i,ignore;
ignore=(argv[0][0]=='-');
if (ignore) ++argv[0];
if (str_equal(argv[0],"cd")) {
if (chdir(argv[1])==-1) {
carpsys("chdir failed");
failornot:
return ignore?0:-1;
}
return 0;
} else if (str_equal(argv[0],"export")) {
for (i=1; argv[i]; ++i) envset(argv[i]);
return 0;
} else if (str_equal(argv[0],"ulimit")) {
struct rlimit rl;
for (i=1; argv[i] && argv[i+1]; i+=2) {
int id=-1;
if (argv[i][0]!='-') {
ulimitsyntax:
carp("ulimit syntax error: ",argv[i]);
continue;
}
switch(argv[i][1]) {
case 'c': id=RLIMIT_CORE; break;
case 'd': id=RLIMIT_DATA; break;
case 'e': id=RLIMIT_NICE; break;
case 'f': id=RLIMIT_FSIZE; break;
case 'i': id=RLIMIT_SIGPENDING; break;
case 'l': id=RLIMIT_MEMLOCK; break;
case 'm': id=RLIMIT_RSS; break;
case 'n': id=RLIMIT_NOFILE; break;
case 'r': id=RLIMIT_RTPRIO; break;
case 's': id=RLIMIT_STACK; break;
case 't': id=RLIMIT_CPU; break;
case 'u': id=RLIMIT_NPROC; break;
case 'v': id=RLIMIT_AS; break;
case 'x': id=RLIMIT_LOCKS; break;
default: goto ulimitsyntax;
}
if (!strcmp(argv[i+1],"unlimited")) {
rl.rlim_cur=rl.rlim_max=RLIM_INFINITY;
} else {
unsigned long ul;
if (argv[i+1][scan_ulong(argv[i+1],&ul)])
goto ulimitsyntax;
rl.rlim_cur=rl.rlim_max=ul;
}
if (setrlimit(id,&rl)==-1) {
carpsys("ulimit failed");
goto failornot;
}
}
return 0;
}
if (!last) {
if ((i=fork())==-1) diesys(1,"cannot fork");
} else i=0;
if (!i) {
/* child */
environ=envp;
_exit(execvp(argv[0],argv));
}
if (waitpid(i,&i,0)==-1) diesys(1,"waitpid failed");
if (ignore) return 0;
if (!WIFEXITED(i))
return -1;
return WEXITSTATUS(i);
}
int run(char* s,int last) {
int i,spaces;
char** argv,**next;
for (i=spaces=0; s[i]; ++i) if (s[i]==' ') ++spaces;
next=argv=alloca((spaces+2)*sizeof(char*));
while (*s) {
while (*s && isspace(*s)) ++s;
if (*s=='"') {
++s;
*next=s;
while (*s && (*s != '"' || s[-1] == '\\')) ++s;
if (!*s) {
--*next;
break;
}
*s=0;
++s;
} else if (*s=='\'') {
++s;
*next=s;
while (*s && (*s != '\'' || s[-1] == '\\')) ++s;
if (!*s) {
--*next;
break;
}
*s=0;
++s;
} else {
*next=s;
while (*s && *s!=' ' && *s!='\t') ++s;
if (*s) {
*s=0;
++s;
}
}
++next;
}
*next=0;
return spawn(argv,last);
}
int execute(char* s) {
char* start;
int r;
r=0;
while (*s) {
int last;
while (isspace(*s)) ++s;
if (*s == '#') {
while (*s && *s != '\n') ++s;
continue;
}
start=s;
while (*s && *s != '\n') ++s;
/* advance to the end of the line */
if (*s) {
/* not the last line in the file */
char* tmp;
*s=0;
++s;
/* If it's the last command in the file, we do not fork+exec but
* we execve it directly. So we need to find out here if this is
* the last command in the file. For that we need to skip all the
* lines after it that are comments */
for (tmp=s; *tmp; ++tmp)
if (!isspace(*tmp) && *tmp=='#') {
for (++tmp; *tmp && *tmp!='\n'; ++tmp) ;
} else
break;
last=(*tmp==0);
} else
last=1;
r=run(start,last);
if (r!=0 && !continueonerror)
break;
}
return r;
}
int batch(char* s) {
struct stat ss;
int fd=open(s,O_RDONLY);
char* map;
if (fd==-1) diesys(1,"could not open ",s);
if (fstat(fd,&ss)==-1) diesys(1,"could not stat ",s);
if (ss.st_size>32768) die(1,"file ",s," is too large");
map=alloca(ss.st_size+1);
if (read(fd,map,ss.st_size)!=(long)ss.st_size) diesys(1,"read error");
map[ss.st_size]=0;
close(fd);
return execute(map);
}
int main(int argc,char* argv[],char* env[]) {
static char* fakeargv[]={0,"script",0};
int r;
(void)argc;
if (argc<2) {
if (!access("script",O_RDONLY))
argv=fakeargv;
else
die(1,"usage: serdo [-c] filename");
}
errmsg_iam("serdo");
for (envc=0; envc<MAXENV && env[envc]; ++envc) envp[envc]=env[envc];
envp[envc]=0;
if (str_equal(argv[1],"-c")) {
continueonerror=1;
++argv;
}
while (*++argv) {
if ((r=batch(*argv)))
return r;
}
return 0;
}