Mercurial > hg > toybox
annotate lib/xwrap.c @ 956:caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 19 Jul 2013 02:03:02 -0500 |
parents | ce0519f6457c |
children | 252caf3d2b88 |
rev | line source |
---|---|
951 | 1 /* xwrap.c - wrappers around existing library functions. |
2 * | |
3 * Functions with the x prefix are wrappers that either succeed or kill the | |
4 * program with an error message, but never return failure. They usually have | |
5 * the same arguments and return value as the function they wrap. | |
6 * | |
7 * Copyright 2006 Rob Landley <rob@landley.net> | |
8 */ | |
9 | |
10 #include "toys.h" | |
11 | |
12 // Strcpy with size checking: exit if there's not enough space for the string. | |
13 void xstrncpy(char *dest, char *src, size_t size) | |
14 { | |
15 if (strlen(src)+1 > size) error_exit("xstrcpy"); | |
16 strcpy(dest, src); | |
17 } | |
18 | |
19 void xexit(void) | |
20 { | |
21 if (toys.rebound) longjmp(*toys.rebound, 1); | |
22 else exit(toys.exitval); | |
23 } | |
24 | |
25 // Die unless we can allocate memory. | |
26 void *xmalloc(size_t size) | |
27 { | |
28 void *ret = malloc(size); | |
29 if (!ret) error_exit("xmalloc"); | |
30 | |
31 return ret; | |
32 } | |
33 | |
34 // Die unless we can allocate prezeroed memory. | |
35 void *xzalloc(size_t size) | |
36 { | |
37 void *ret = xmalloc(size); | |
38 memset(ret, 0, size); | |
39 return ret; | |
40 } | |
41 | |
42 // Die unless we can change the size of an existing allocation, possibly | |
43 // moving it. (Notice different arguments from libc function.) | |
44 void *xrealloc(void *ptr, size_t size) | |
45 { | |
46 ptr = realloc(ptr, size); | |
47 if (!ptr) error_exit("xrealloc"); | |
48 | |
49 return ptr; | |
50 } | |
51 | |
52 // Die unless we can allocate a copy of this many bytes of string. | |
53 char *xstrndup(char *s, size_t n) | |
54 { | |
55 char *ret = xmalloc(++n); | |
56 strncpy(ret, s, n); | |
57 ret[--n]=0; | |
58 | |
59 return ret; | |
60 } | |
61 | |
62 // Die unless we can allocate a copy of this string. | |
63 char *xstrdup(char *s) | |
64 { | |
65 return xstrndup(s, strlen(s)); | |
66 } | |
67 | |
68 // Die unless we can allocate enough space to sprintf() into. | |
69 char *xmsprintf(char *format, ...) | |
70 { | |
71 va_list va, va2; | |
72 int len; | |
73 char *ret; | |
74 | |
75 va_start(va, format); | |
76 va_copy(va2, va); | |
77 | |
78 // How long is it? | |
79 len = vsnprintf(0, 0, format, va); | |
80 len++; | |
81 va_end(va); | |
82 | |
83 // Allocate and do the sprintf() | |
84 ret = xmalloc(len); | |
85 vsnprintf(ret, len, format, va2); | |
86 va_end(va2); | |
87 | |
88 return ret; | |
89 } | |
90 | |
91 void xprintf(char *format, ...) | |
92 { | |
93 va_list va; | |
94 va_start(va, format); | |
95 | |
96 vprintf(format, va); | |
97 if (ferror(stdout)) perror_exit("write"); | |
98 } | |
99 | |
100 void xputs(char *s) | |
101 { | |
102 if (EOF == puts(s) || fflush(stdout)) perror_exit("write"); | |
103 } | |
104 | |
105 void xputc(char c) | |
106 { | |
107 if (EOF == fputc(c, stdout) || fflush(stdout)) perror_exit("write"); | |
108 } | |
109 | |
110 void xflush(void) | |
111 { | |
112 if (fflush(stdout)) perror_exit("write");; | |
113 } | |
114 | |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
115 // Call xexec with a chunk of optargs, starting at skip. (You can't just |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
116 // call xexec() directly because toy_init() frees optargs.) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
117 void xexec_optargs(int skip) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
118 { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
119 char **s = toys.optargs; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
120 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
121 toys.optargs = 0; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
122 xexec(s+skip); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
123 } |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
124 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
125 |
951 | 126 // Die unless we can exec argv[] (or run builtin command). Note that anything |
127 // with a path isn't a builtin, so /bin/sh won't match the builtin sh. | |
128 void xexec(char **argv) | |
129 { | |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
952
diff
changeset
|
130 if (!CFG_TOYBOX_SINGLE) toy_exec(argv); |
951 | 131 execvp(argv[0], argv); |
132 | |
133 perror_exit("exec %s", argv[0]); | |
134 } | |
135 | |
136 void xaccess(char *path, int flags) | |
137 { | |
138 if (access(path, flags)) perror_exit("Can't access '%s'", path); | |
139 } | |
140 | |
141 // Die unless we can delete a file. (File must exist to be deleted.) | |
142 void xunlink(char *path) | |
143 { | |
144 if (unlink(path)) perror_exit("unlink '%s'", path); | |
145 } | |
146 | |
147 // Die unless we can open/create a file, returning file descriptor. | |
148 int xcreate(char *path, int flags, int mode) | |
149 { | |
150 int fd = open(path, flags, mode); | |
151 if (fd == -1) perror_exit("%s", path); | |
152 return fd; | |
153 } | |
154 | |
155 // Die unless we can open a file, returning file descriptor. | |
156 int xopen(char *path, int flags) | |
157 { | |
158 return xcreate(path, flags, 0); | |
159 } | |
160 | |
161 void xclose(int fd) | |
162 { | |
163 if (close(fd)) perror_exit("xclose"); | |
164 } | |
165 | |
166 int xdup(int fd) | |
167 { | |
168 if (fd != -1) { | |
169 fd = dup(fd); | |
170 if (fd == -1) perror_exit("xdup"); | |
171 } | |
172 return fd; | |
173 } | |
174 | |
175 // Die unless we can open/create a file, returning FILE *. | |
176 FILE *xfopen(char *path, char *mode) | |
177 { | |
178 FILE *f = fopen(path, mode); | |
179 if (!f) perror_exit("No file %s", path); | |
180 return f; | |
181 } | |
182 | |
183 // Die if there's an error other than EOF. | |
184 size_t xread(int fd, void *buf, size_t len) | |
185 { | |
186 ssize_t ret = read(fd, buf, len); | |
187 if (ret < 0) perror_exit("xread"); | |
188 | |
189 return ret; | |
190 } | |
191 | |
192 void xreadall(int fd, void *buf, size_t len) | |
193 { | |
194 if (len != readall(fd, buf, len)) perror_exit("xreadall"); | |
195 } | |
196 | |
197 // There's no xwriteall(), just xwrite(). When we read, there may or may not | |
198 // be more data waiting. When we write, there is data and it had better go | |
199 // somewhere. | |
200 | |
201 void xwrite(int fd, void *buf, size_t len) | |
202 { | |
203 if (len != writeall(fd, buf, len)) perror_exit("xwrite"); | |
204 } | |
205 | |
206 // Die if lseek fails, probably due to being called on a pipe. | |
207 | |
208 off_t xlseek(int fd, off_t offset, int whence) | |
209 { | |
210 offset = lseek(fd, offset, whence); | |
211 if (offset<0) perror_exit("lseek"); | |
212 | |
213 return offset; | |
214 } | |
215 | |
216 char *xgetcwd(void) | |
217 { | |
218 char *buf = getcwd(NULL, 0); | |
219 if (!buf) perror_exit("xgetcwd"); | |
220 | |
221 return buf; | |
222 } | |
223 | |
224 void xstat(char *path, struct stat *st) | |
225 { | |
226 if(stat(path, st)) perror_exit("Can't stat %s", path); | |
227 } | |
228 | |
229 // Cannonicalize path, even to file with one or more missing components at end. | |
230 // if exact, require last path component to exist | |
231 char *xabspath(char *path, int exact) | |
232 { | |
233 struct string_list *todo, *done = 0; | |
234 int try = 9999, dirfd = open("/", 0);; | |
235 char buf[4096], *ret; | |
236 | |
237 // If this isn't an absolute path, start with cwd. | |
238 if (*path != '/') { | |
239 char *temp = xgetcwd(); | |
240 | |
241 splitpath(path, splitpath(temp, &todo)); | |
242 free(temp); | |
243 } else splitpath(path, &todo); | |
244 | |
245 // Iterate through path components | |
246 while (todo) { | |
247 struct string_list *new = llist_pop(&todo), **tail; | |
248 ssize_t len; | |
249 | |
250 if (!try--) { | |
251 errno = ELOOP; | |
252 goto error; | |
253 } | |
254 | |
255 // Removable path componenents. | |
256 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) { | |
257 int x = new->str[1]; | |
258 | |
259 free(new); | |
260 if (x) { | |
261 if (done) free(llist_pop(&done)); | |
262 len = 0; | |
263 } else continue; | |
264 | |
265 // Is this a symlink? | |
266 } else len=readlinkat(dirfd, new->str, buf, 4096); | |
267 | |
268 if (len>4095) goto error; | |
269 if (len<1) { | |
270 int fd; | |
271 char *s = ".."; | |
272 | |
273 // For .. just move dirfd | |
274 if (len) { | |
275 // Not a symlink: add to linked list, move dirfd, fail if error | |
276 if ((exact || todo) && errno != EINVAL) goto error; | |
277 new->next = done; | |
278 done = new; | |
279 if (errno == EINVAL && !todo) break; | |
280 s = new->str; | |
281 } | |
282 fd = openat(dirfd, s, 0); | |
283 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error; | |
284 close(dirfd); | |
285 dirfd = fd; | |
286 continue; | |
287 } | |
288 | |
289 // If this symlink is to an absolute path, discard existing resolved path | |
290 buf[len] = 0; | |
291 if (*buf == '/') { | |
292 llist_traverse(done, free); | |
293 done=0; | |
294 close(dirfd); | |
295 dirfd = open("/", 0); | |
296 } | |
297 free(new); | |
298 | |
299 // prepend components of new path. Note symlink to "/" will leave new NULL | |
300 tail = splitpath(buf, &new); | |
301 | |
302 // symlink to "/" will return null and leave tail alone | |
303 if (new) { | |
304 *tail = todo; | |
305 todo = new; | |
306 } | |
307 } | |
308 close(dirfd); | |
309 | |
310 // At this point done has the path, in reverse order. Reverse list while | |
311 // calculating buffer length. | |
312 | |
313 try = 2; | |
314 while (done) { | |
315 struct string_list *temp = llist_pop(&done);; | |
316 | |
317 if (todo) try++; | |
318 try += strlen(temp->str); | |
319 temp->next = todo; | |
320 todo = temp; | |
321 } | |
322 | |
323 // Assemble return buffer | |
324 | |
325 ret = xmalloc(try); | |
326 *ret = '/'; | |
327 ret [try = 1] = 0; | |
328 while (todo) { | |
329 if (try>1) ret[try++] = '/'; | |
330 try = stpcpy(ret+try, todo->str) - ret; | |
331 free(llist_pop(&todo)); | |
332 } | |
333 | |
334 return ret; | |
335 | |
336 error: | |
337 close(dirfd); | |
338 llist_traverse(todo, free); | |
339 llist_traverse(done, free); | |
340 | |
341 return NULL; | |
342 } | |
343 | |
344 // Resolve all symlinks, returning malloc() memory. | |
345 char *xrealpath(char *path) | |
346 { | |
347 char *new = realpath(path, NULL); | |
348 if (!new) perror_exit("realpath '%s'", path); | |
349 return new; | |
350 } | |
351 | |
352 void xchdir(char *path) | |
353 { | |
354 if (chdir(path)) error_exit("chdir '%s'", path); | |
355 } | |
356 | |
357 // Ensure entire path exists. | |
358 // If mode != -1 set permissions on newly created dirs. | |
359 // Requires that path string be writable (for temporary null terminators). | |
360 void xmkpath(char *path, int mode) | |
361 { | |
362 char *p, old; | |
363 mode_t mask; | |
364 int rc; | |
365 struct stat st; | |
366 | |
367 for (p = path; ; p++) { | |
368 if (!*p || *p == '/') { | |
369 old = *p; | |
370 *p = rc = 0; | |
371 if (stat(path, &st) || !S_ISDIR(st.st_mode)) { | |
372 if (mode != -1) { | |
373 mask=umask(0); | |
374 rc = mkdir(path, mode); | |
375 umask(mask); | |
376 } else rc = mkdir(path, 0777); | |
377 } | |
378 *p = old; | |
379 if(rc) perror_exit("mkpath '%s'", path); | |
380 } | |
381 if (!*p) break; | |
382 } | |
383 } | |
384 | |
385 // setuid() can fail (for example, too many processes belonging to that user), | |
386 // which opens a security hole if the process continues as the original user. | |
387 | |
388 void xsetuid(uid_t uid) | |
389 { | |
390 if (setuid(uid)) perror_exit("xsetuid"); | |
391 } | |
392 | |
393 // This can return null (meaning file not found). It just won't return null | |
394 // for memory allocation reasons. | |
395 char *xreadlink(char *name) | |
396 { | |
397 int len, size = 0; | |
398 char *buf = 0; | |
399 | |
400 // Grow by 64 byte chunks until it's big enough. | |
401 for(;;) { | |
402 size +=64; | |
403 buf = xrealloc(buf, size); | |
404 len = readlink(name, buf, size); | |
405 | |
406 if (len<0) { | |
407 free(buf); | |
408 return 0; | |
409 } | |
410 if (len<size) { | |
411 buf[len]=0; | |
412 return buf; | |
413 } | |
414 } | |
415 } | |
416 | |
417 char *xreadfile(char *name) | |
418 { | |
419 char *buf = readfile(name); | |
420 if (!buf) perror_exit("xreadfile %s", name); | |
421 return buf; | |
422 } | |
423 | |
424 int xioctl(int fd, int request, void *data) | |
425 { | |
426 int rc; | |
427 | |
428 errno = 0; | |
429 rc = ioctl(fd, request, data); | |
430 if (rc == -1 && errno) perror_exit("ioctl %x", request); | |
431 | |
432 return rc; | |
433 } | |
434 | |
435 // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently | |
436 // exists and is this executable. | |
437 void xpidfile(char *name) | |
438 { | |
439 char pidfile[256], spid[32]; | |
440 int i, fd; | |
441 pid_t pid; | |
442 | |
443 sprintf(pidfile, "/var/run/%s.pid", name); | |
444 // Try three times to open the sucker. | |
445 for (i=0; i<3; i++) { | |
446 fd = open(pidfile, O_CREAT|O_EXCL, 0644); | |
447 if (fd != -1) break; | |
448 | |
449 // If it already existed, read it. Loop for race condition. | |
450 fd = open(pidfile, O_RDONLY); | |
451 if (fd == -1) continue; | |
452 | |
453 // Is the old program still there? | |
454 spid[xread(fd, spid, sizeof(spid)-1)] = 0; | |
455 close(fd); | |
456 pid = atoi(spid); | |
457 if (pid < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); | |
458 | |
459 // An else with more sanity checking might be nice here. | |
460 } | |
461 | |
462 if (i == 3) error_exit("xpidfile %s", name); | |
463 | |
464 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); | |
465 close(fd); | |
466 } | |
467 | |
468 // Copy the rest of in to out and close both files. | |
469 | |
470 void xsendfile(int in, int out) | |
471 { | |
472 long len; | |
473 char buf[4096]; | |
474 | |
475 if (in<0) return; | |
476 for (;;) { | |
477 len = xread(in, buf, 4096); | |
478 if (len<1) break; | |
479 xwrite(out, buf, len); | |
480 } | |
481 } | |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
482 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
483 // parse fractional seconds with optional s/m/h/d suffix |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
484 long xparsetime(char *arg, long units, long *fraction) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
485 { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
486 double d; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
487 long l; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
488 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
489 if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
490 else l = strtoul(arg, &arg, 10); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
491 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
492 // Parse suffix |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
493 if (*arg) { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
494 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
495 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
496 if (i == -1) error_exit("Unknown suffix '%c'", *arg); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
497 if (CFG_TOYBOX_FLOAT) d *= ismhd[i]; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
498 else l *= ismhd[i]; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
499 } |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
500 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
501 if (CFG_TOYBOX_FLOAT) { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
502 l = (long)d; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
503 if (fraction) *fraction = units*(d-l); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
504 } else if (fraction) *fraction = 0; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
505 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
506 return l; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
951
diff
changeset
|
507 } |