blob: 83f54a0a1a3e09687861b381645334ef71f36441 [file] [log] [blame]
Rob Landleyd3904932013-07-16 00:04:56 -05001/* 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
Rob Landley2fb85a32014-12-04 21:41:12 -060012// strcpy and strncat with size checking. Size is the total space in "dest",
13// including null terminator. Exit if there's not enough space for the string
14// (including space for the null terminator), because silently truncating is
15// still broken behavior. (And leaving the string unterminated is INSANE.)
Rob Landleyd3904932013-07-16 00:04:56 -050016void xstrncpy(char *dest, char *src, size_t size)
17{
Rob Landley3704f822013-11-02 14:24:54 -050018 if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size);
Rob Landleyd3904932013-07-16 00:04:56 -050019 strcpy(dest, src);
20}
21
Rob Landley2fb85a32014-12-04 21:41:12 -060022void xstrncat(char *dest, char *src, size_t size)
23{
Rob Landley8f7137e2016-01-28 13:36:12 -060024 long len = strlen(dest);
Rob Landley2fb85a32014-12-04 21:41:12 -060025
Rob Landley8f7137e2016-01-28 13:36:12 -060026 if (len+strlen(src)+1 > size)
Rob Landley70a84a32015-03-01 15:58:40 -060027 error_exit("'%s%s' > %ld bytes", dest, src, (long)size);
Rob Landley2fb85a32014-12-04 21:41:12 -060028 strcpy(dest+len, src);
29}
30
Rob Landleyeb24df92016-03-13 20:23:41 -050031// We replaced exit(), _exit(), and atexit() with xexit(), _xexit(), and
32// sigatexit(). This gives _xexit() the option to siglongjmp(toys.rebound, 1)
33// instead of exiting, lets xexit() report stdout flush failures to stderr
34// and change the exit code to indicate error, lets our toys.exit function
35// change happen for signal exit paths and lets us remove the functions
36// after we've called them.
37
38void _xexit(void)
39{
40 if (toys.rebound) siglongjmp(*toys.rebound, 1);
41
42 _exit(toys.exitval);
43}
44
Rob Landleyd3904932013-07-16 00:04:56 -050045void xexit(void)
46{
Rob Landleyeb24df92016-03-13 20:23:41 -050047 // Call toys.xexit functions in reverse order added.
48 while (toys.xexit) {
49 // This is typecasting xexit->arg to a function pointer,then calling it.
Elliott Hughes072ea412016-04-21 18:18:05 -070050 // Using the invalid signal number 0 lets the signal handlers distinguish
51 // an actual signal from a regular exit.
52 ((void (*)(int))(toys.xexit->arg))(0);
Rob Landleyeb24df92016-03-13 20:23:41 -050053
54 free(llist_pop(&toys.xexit));
55 }
Rob Landleyaad492f2015-01-03 16:25:36 -060056 if (fflush(NULL) || ferror(stdout))
57 if (!toys.exitval) perror_msg("write");
Rob Landleyeb24df92016-03-13 20:23:41 -050058 _xexit();
Rob Landleyd3904932013-07-16 00:04:56 -050059}
60
Elliott Hughes12f07442017-05-23 17:35:49 -070061void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off)
62{
63 void *ret = mmap(addr, length, prot, flags, fd, off);
64 if (ret == MAP_FAILED) perror_exit("mmap");
65 return ret;
66}
67
Rob Landleyd3904932013-07-16 00:04:56 -050068// Die unless we can allocate memory.
69void *xmalloc(size_t size)
70{
71 void *ret = malloc(size);
Rob Landleyadf6f032015-12-31 14:09:54 -060072 if (!ret) error_exit("xmalloc(%ld)", (long)size);
Rob Landleyd3904932013-07-16 00:04:56 -050073
74 return ret;
75}
76
77// Die unless we can allocate prezeroed memory.
78void *xzalloc(size_t size)
79{
80 void *ret = xmalloc(size);
81 memset(ret, 0, size);
82 return ret;
83}
84
85// Die unless we can change the size of an existing allocation, possibly
86// moving it. (Notice different arguments from libc function.)
87void *xrealloc(void *ptr, size_t size)
88{
89 ptr = realloc(ptr, size);
90 if (!ptr) error_exit("xrealloc");
91
92 return ptr;
93}
94
95// Die unless we can allocate a copy of this many bytes of string.
96char *xstrndup(char *s, size_t n)
97{
Rob Landley2fb85a32014-12-04 21:41:12 -060098 char *ret = strndup(s, ++n);
99
100 if (!ret) error_exit("xstrndup");
101 ret[--n] = 0;
Rob Landleyd3904932013-07-16 00:04:56 -0500102
103 return ret;
104}
105
106// Die unless we can allocate a copy of this string.
107char *xstrdup(char *s)
108{
109 return xstrndup(s, strlen(s));
110}
111
Rob Landley8f7137e2016-01-28 13:36:12 -0600112void *xmemdup(void *s, long len)
113{
114 void *ret = xmalloc(len);
115 memcpy(ret, s, len);
116
117 return ret;
118}
119
Rob Landleyd3904932013-07-16 00:04:56 -0500120// Die unless we can allocate enough space to sprintf() into.
Rob Landley59d85e22014-01-16 09:26:50 -0600121char *xmprintf(char *format, ...)
Rob Landleyd3904932013-07-16 00:04:56 -0500122{
123 va_list va, va2;
124 int len;
125 char *ret;
126
127 va_start(va, format);
128 va_copy(va2, va);
129
130 // How long is it?
131 len = vsnprintf(0, 0, format, va);
132 len++;
133 va_end(va);
134
135 // Allocate and do the sprintf()
136 ret = xmalloc(len);
137 vsnprintf(ret, len, format, va2);
138 va_end(va2);
139
140 return ret;
141}
142
143void xprintf(char *format, ...)
144{
145 va_list va;
146 va_start(va, format);
147
148 vprintf(format, va);
Rob Landley21f3c8d2014-10-20 19:56:05 -0500149 va_end(va);
Rob Landleyddbaa712014-05-26 12:25:47 -0500150 if (fflush(stdout) || ferror(stdout)) perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500151}
152
153void xputs(char *s)
154{
Rob Landleyddbaa712014-05-26 12:25:47 -0500155 if (EOF == puts(s) || fflush(stdout) || ferror(stdout)) perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500156}
157
158void xputc(char c)
159{
Rob Landleyddbaa712014-05-26 12:25:47 -0500160 if (EOF == fputc(c, stdout) || fflush(stdout) || ferror(stdout))
161 perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500162}
163
164void xflush(void)
165{
Rob Landleyddbaa712014-05-26 12:25:47 -0500166 if (fflush(stdout) || ferror(stdout)) perror_exit("write");;
Rob Landleyd3904932013-07-16 00:04:56 -0500167}
168
Rob Landley7d6af772015-09-29 05:09:46 -0500169// This is called through the XVFORK macro because parent/child of vfork
170// share a stack, so child returning from a function would stomp the return
171// address parent would need. Solution: make vfork() an argument so processes
172// diverge before function gets called.
Rob Landley938901d2017-02-04 00:34:31 -0600173pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid)
Rob Landley7d6af772015-09-29 05:09:46 -0500174{
175 if (pid == -1) perror_exit("vfork");
176
177 // Signal to xexec() and friends that we vforked so can't recurse
178 toys.stacktop = 0;
179
180 return pid;
181}
182
Rob Landleyd3904932013-07-16 00:04:56 -0500183// Die unless we can exec argv[] (or run builtin command). Note that anything
184// with a path isn't a builtin, so /bin/sh won't match the builtin sh.
185void xexec(char **argv)
186{
Rob Landley3b51a072015-09-27 09:03:41 -0500187 // Only recurse to builtin when we have multiplexer and !vfork context.
188 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE && toys.stacktop) toy_exec(argv);
Rob Landleyd3904932013-07-16 00:04:56 -0500189 execvp(argv[0], argv);
190
Rob Landley9ce93992015-08-06 07:39:23 -0500191 perror_msg("exec %s", argv[0]);
192 toys.exitval = 127;
193 if (!CFG_TOYBOX_FORK) _exit(toys.exitval);
194 xexit();
Rob Landleyd3904932013-07-16 00:04:56 -0500195}
196
Rob Landley44e68a12014-06-03 06:27:24 -0500197// Spawn child process, capturing stdin/stdout.
Rob Landley7d6af772015-09-29 05:09:46 -0500198// argv[]: command to exec. If null, child re-runs original program with
199// toys.stacktop zeroed.
200// pipes[2]: stdin, stdout of new process, only allocated if zero on way in,
201// pass NULL to skip pipe allocation entirely.
Rob Landley44e68a12014-06-03 06:27:24 -0500202// return: pid of child process
Rob Landley360d57f2014-09-14 12:29:44 -0500203pid_t xpopen_both(char **argv, int *pipes)
Rob Landley44e68a12014-06-03 06:27:24 -0500204{
205 int cestnepasun[4], pid;
206
Rob Landley7d6af772015-09-29 05:09:46 -0500207 // Make the pipes? Note this won't set either pipe to 0 because if fds are
Rob Landley360d57f2014-09-14 12:29:44 -0500208 // allocated in order and if fd0 was free it would go to cestnepasun[0]
Rob Landley44e68a12014-06-03 06:27:24 -0500209 if (pipes) {
Rob Landley360d57f2014-09-14 12:29:44 -0500210 for (pid = 0; pid < 2; pid++) {
Rob Landley7d6af772015-09-29 05:09:46 -0500211 if (pipes[pid] != 0) continue;
Rob Landley360d57f2014-09-14 12:29:44 -0500212 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
213 pipes[pid] = cestnepasun[pid+1];
214 }
Rob Landley44e68a12014-06-03 06:27:24 -0500215 }
216
Rob Landley7d6af772015-09-29 05:09:46 -0500217 // Child process.
218 if (!(pid = CFG_TOYBOX_FORK ? xfork() : XVFORK())) {
Rob Landley44e68a12014-06-03 06:27:24 -0500219 // Dance of the stdin/stdout redirection.
220 if (pipes) {
Rob Landley360d57f2014-09-14 12:29:44 -0500221 // if we had no stdin/out, pipe handles could overlap, so test for it
222 // and free up potentially overlapping pipe handles before reuse
223 if (pipes[1] != -1) close(cestnepasun[2]);
224 if (pipes[0] != -1) {
225 close(cestnepasun[1]);
226 if (cestnepasun[0]) {
227 dup2(cestnepasun[0], 0);
228 close(cestnepasun[0]);
229 }
Rob Landley44e68a12014-06-03 06:27:24 -0500230 }
Rob Landley360d57f2014-09-14 12:29:44 -0500231 if (pipes[1] != -1) {
232 dup2(cestnepasun[3], 1);
233 dup2(cestnepasun[3], 2);
234 if (cestnepasun[3] > 2 || !cestnepasun[3]) close(cestnepasun[3]);
235 }
Rob Landley44e68a12014-06-03 06:27:24 -0500236 }
Rob Landley7d6af772015-09-29 05:09:46 -0500237 if (argv) xexec(argv);
238
239 // In fork() case, force recursion because we know it's us.
240 if (CFG_TOYBOX_FORK) {
241 toy_init(toys.which, toys.argv);
242 toys.stacktop = 0;
243 toys.which->toy_main();
244 xexit();
245 // In vfork() case, exec /proc/self/exe with high bit of first letter set
246 // to tell main() we reentered.
247 } else {
248 char *s = "/proc/self/exe";
249
250 // We did a nommu-friendly vfork but must exec to continue.
251 // setting high bit of argv[0][0] to let new process know
252 **toys.argv |= 0x80;
253 execv(s, toys.argv);
Rob Landleyd3a435e2016-01-05 22:26:58 -0600254 perror_msg_raw(s);
Rob Landley7d6af772015-09-29 05:09:46 -0500255
Rob Landley44e68a12014-06-03 06:27:24 -0500256 _exit(127);
257 }
Rob Landley44e68a12014-06-03 06:27:24 -0500258 }
Rob Landley360d57f2014-09-14 12:29:44 -0500259
260 // Parent process
Rob Landley7d6af772015-09-29 05:09:46 -0500261 if (!CFG_TOYBOX_FORK) **toys.argv &= 0x7f;
Rob Landley360d57f2014-09-14 12:29:44 -0500262 if (pipes) {
263 if (pipes[0] != -1) close(cestnepasun[0]);
264 if (pipes[1] != -1) close(cestnepasun[3]);
265 }
266
267 return pid;
Rob Landley44e68a12014-06-03 06:27:24 -0500268}
269
Rob Landley3b51a072015-09-27 09:03:41 -0500270// Wait for child process to exit, then return adjusted exit code.
271int xwaitpid(pid_t pid)
272{
273 int status;
274
275 while (-1 == waitpid(pid, &status, 0) && errno == EINTR);
276
277 return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
278}
279
Rob Landley360d57f2014-09-14 12:29:44 -0500280int xpclose_both(pid_t pid, int *pipes)
Rob Landley44e68a12014-06-03 06:27:24 -0500281{
Rob Landley44e68a12014-06-03 06:27:24 -0500282 if (pipes) {
283 close(pipes[0]);
284 close(pipes[1]);
285 }
Rob Landley44e68a12014-06-03 06:27:24 -0500286
Rob Landley3b51a072015-09-27 09:03:41 -0500287 return xwaitpid(pid);
Rob Landley44e68a12014-06-03 06:27:24 -0500288}
289
Rob Landley360d57f2014-09-14 12:29:44 -0500290// Wrapper to xpopen with a pipe for just one of stdin/stdout
Rob Landleyea9dd8a2017-02-04 14:55:36 -0600291pid_t xpopen(char **argv, int *pipe, int isstdout)
Rob Landley360d57f2014-09-14 12:29:44 -0500292{
Rob Landley8a990712014-09-14 19:54:19 -0500293 int pipes[2], pid;
Rob Landley360d57f2014-09-14 12:29:44 -0500294
Rob Landleyea9dd8a2017-02-04 14:55:36 -0600295 pipes[!isstdout] = -1;
296 pipes[!!isstdout] = 0;
Rob Landley8a990712014-09-14 19:54:19 -0500297 pid = xpopen_both(argv, pipes);
Rob Landleyea9dd8a2017-02-04 14:55:36 -0600298 *pipe = pid ? pipes[!!isstdout] : -1;
Rob Landley360d57f2014-09-14 12:29:44 -0500299
Rob Landley8a990712014-09-14 19:54:19 -0500300 return pid;
Rob Landley360d57f2014-09-14 12:29:44 -0500301}
302
303int xpclose(pid_t pid, int pipe)
304{
305 close(pipe);
306
307 return xpclose_both(pid, 0);
308}
309
310// Call xpopen and wait for it to finish, keeping existing stdin/stdout.
311int xrun(char **argv)
312{
313 return xpclose_both(xpopen_both(argv, 0), 0);
314}
315
Rob Landleyd3904932013-07-16 00:04:56 -0500316void xaccess(char *path, int flags)
317{
318 if (access(path, flags)) perror_exit("Can't access '%s'", path);
319}
320
321// Die unless we can delete a file. (File must exist to be deleted.)
322void xunlink(char *path)
323{
324 if (unlink(path)) perror_exit("unlink '%s'", path);
325}
326
327// Die unless we can open/create a file, returning file descriptor.
Rob Landley299d4382016-09-04 17:26:34 -0500328// The meaning of O_CLOEXEC is reversed (it defaults on, pass it to disable)
329// and WARN_ONLY tells us not to exit.
Rob Landley027a73a2016-08-04 10:16:59 -0500330int xcreate_stdio(char *path, int flags, int mode)
Rob Landleyd3904932013-07-16 00:04:56 -0500331{
Rob Landley299d4382016-09-04 17:26:34 -0500332 int fd = open(path, (flags^O_CLOEXEC)&~WARN_ONLY, mode);
Rob Landley027a73a2016-08-04 10:16:59 -0500333
Rob Landley299d4382016-09-04 17:26:34 -0500334 if (fd == -1) ((mode&WARN_ONLY) ? perror_msg_raw : perror_exit_raw)(path);
Rob Landleyd3904932013-07-16 00:04:56 -0500335 return fd;
336}
337
338// Die unless we can open a file, returning file descriptor.
Rob Landley027a73a2016-08-04 10:16:59 -0500339int xopen_stdio(char *path, int flags)
Rob Landleyd3904932013-07-16 00:04:56 -0500340{
Rob Landley027a73a2016-08-04 10:16:59 -0500341 return xcreate_stdio(path, flags, 0);
Rob Landleyd3904932013-07-16 00:04:56 -0500342}
343
Rob Landleya7a869c2016-02-09 17:06:12 -0600344void xpipe(int *pp)
Rob Landley85f54d82016-02-08 15:24:33 -0600345{
346 if (pipe(pp)) perror_exit("xpipe");
347}
348
Rob Landleyd3904932013-07-16 00:04:56 -0500349void xclose(int fd)
350{
351 if (close(fd)) perror_exit("xclose");
352}
353
354int xdup(int fd)
355{
356 if (fd != -1) {
357 fd = dup(fd);
358 if (fd == -1) perror_exit("xdup");
359 }
360 return fd;
361}
362
Rob Landley027a73a2016-08-04 10:16:59 -0500363// Move file descriptor above stdin/stdout/stderr, using /dev/null to consume
364// old one. (We should never be called with stdin/stdout/stderr closed, but...)
365int notstdio(int fd)
366{
Rob Landley7f7907f2016-09-05 00:52:44 -0500367 if (fd<0) return fd;
368
Rob Landley027a73a2016-08-04 10:16:59 -0500369 while (fd<3) {
370 int fd2 = xdup(fd);
371
372 close(fd);
373 xopen_stdio("/dev/null", O_RDWR);
374 fd = fd2;
375 }
376
377 return fd;
378}
379
380// Create a file but don't return stdin/stdout/stderr
381int xcreate(char *path, int flags, int mode)
382{
383 return notstdio(xcreate_stdio(path, flags, mode));
384}
385
386// Open a file descriptor NOT in stdin/stdout/stderr
387int xopen(char *path, int flags)
388{
389 return notstdio(xopen_stdio(path, flags));
390}
391
Rob Landley299d4382016-09-04 17:26:34 -0500392// Open read only, treating "-" as a synonym for stdin, defaulting to warn only
393int openro(char *path, int flags)
Rob Landley027a73a2016-08-04 10:16:59 -0500394{
395 if (!strcmp(path, "-")) return 0;
396
Rob Landley299d4382016-09-04 17:26:34 -0500397 return xopen(path, flags^WARN_ONLY);
398}
399
400// Open read only, treating "-" as a synonym for stdin.
401int xopenro(char *path)
402{
403 return openro(path, O_RDONLY|WARN_ONLY);
Rob Landley027a73a2016-08-04 10:16:59 -0500404}
405
Rob Landley1aa75112013-08-07 12:19:51 -0500406FILE *xfdopen(int fd, char *mode)
407{
408 FILE *f = fdopen(fd, mode);
409
410 if (!f) perror_exit("xfdopen");
411
412 return f;
413}
414
Rob Landleyd3904932013-07-16 00:04:56 -0500415// Die unless we can open/create a file, returning FILE *.
416FILE *xfopen(char *path, char *mode)
417{
418 FILE *f = fopen(path, mode);
419 if (!f) perror_exit("No file %s", path);
420 return f;
421}
422
423// Die if there's an error other than EOF.
424size_t xread(int fd, void *buf, size_t len)
425{
426 ssize_t ret = read(fd, buf, len);
427 if (ret < 0) perror_exit("xread");
428
429 return ret;
430}
431
432void xreadall(int fd, void *buf, size_t len)
433{
434 if (len != readall(fd, buf, len)) perror_exit("xreadall");
435}
436
437// There's no xwriteall(), just xwrite(). When we read, there may or may not
438// be more data waiting. When we write, there is data and it had better go
439// somewhere.
440
441void xwrite(int fd, void *buf, size_t len)
442{
443 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
444}
445
446// Die if lseek fails, probably due to being called on a pipe.
447
448off_t xlseek(int fd, off_t offset, int whence)
449{
450 offset = lseek(fd, offset, whence);
451 if (offset<0) perror_exit("lseek");
452
453 return offset;
454}
455
456char *xgetcwd(void)
457{
458 char *buf = getcwd(NULL, 0);
459 if (!buf) perror_exit("xgetcwd");
460
461 return buf;
462}
463
464void xstat(char *path, struct stat *st)
465{
466 if(stat(path, st)) perror_exit("Can't stat %s", path);
467}
468
469// Cannonicalize path, even to file with one or more missing components at end.
470// if exact, require last path component to exist
Rob Landley2c1cf4a2015-01-18 14:06:14 -0600471char *xabspath(char *path, int exact)
Rob Landleyd3904932013-07-16 00:04:56 -0500472{
473 struct string_list *todo, *done = 0;
474 int try = 9999, dirfd = open("/", 0);;
Rob Landley480fb072016-06-30 10:37:35 -0500475 char *ret;
Rob Landleyd3904932013-07-16 00:04:56 -0500476
477 // If this isn't an absolute path, start with cwd.
478 if (*path != '/') {
479 char *temp = xgetcwd();
480
481 splitpath(path, splitpath(temp, &todo));
482 free(temp);
483 } else splitpath(path, &todo);
484
485 // Iterate through path components
486 while (todo) {
487 struct string_list *new = llist_pop(&todo), **tail;
488 ssize_t len;
489
490 if (!try--) {
491 errno = ELOOP;
492 goto error;
493 }
494
495 // Removable path componenents.
496 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) {
497 int x = new->str[1];
498
499 free(new);
500 if (x) {
501 if (done) free(llist_pop(&done));
502 len = 0;
503 } else continue;
504
505 // Is this a symlink?
Rob Landley480fb072016-06-30 10:37:35 -0500506 } else len = readlinkat(dirfd, new->str, libbuf, sizeof(libbuf));
Rob Landleyd3904932013-07-16 00:04:56 -0500507
508 if (len>4095) goto error;
509 if (len<1) {
510 int fd;
511 char *s = "..";
512
513 // For .. just move dirfd
514 if (len) {
515 // Not a symlink: add to linked list, move dirfd, fail if error
516 if ((exact || todo) && errno != EINVAL) goto error;
517 new->next = done;
518 done = new;
519 if (errno == EINVAL && !todo) break;
520 s = new->str;
521 }
522 fd = openat(dirfd, s, 0);
523 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error;
524 close(dirfd);
525 dirfd = fd;
526 continue;
527 }
528
529 // If this symlink is to an absolute path, discard existing resolved path
Rob Landley480fb072016-06-30 10:37:35 -0500530 libbuf[len] = 0;
531 if (*libbuf == '/') {
Rob Landleyd3904932013-07-16 00:04:56 -0500532 llist_traverse(done, free);
533 done=0;
534 close(dirfd);
535 dirfd = open("/", 0);
536 }
537 free(new);
538
539 // prepend components of new path. Note symlink to "/" will leave new NULL
Rob Landley480fb072016-06-30 10:37:35 -0500540 tail = splitpath(libbuf, &new);
Rob Landleyd3904932013-07-16 00:04:56 -0500541
542 // symlink to "/" will return null and leave tail alone
543 if (new) {
544 *tail = todo;
545 todo = new;
546 }
547 }
548 close(dirfd);
549
550 // At this point done has the path, in reverse order. Reverse list while
551 // calculating buffer length.
552
553 try = 2;
554 while (done) {
555 struct string_list *temp = llist_pop(&done);;
556
557 if (todo) try++;
558 try += strlen(temp->str);
559 temp->next = todo;
560 todo = temp;
561 }
562
563 // Assemble return buffer
564
565 ret = xmalloc(try);
566 *ret = '/';
567 ret [try = 1] = 0;
568 while (todo) {
569 if (try>1) ret[try++] = '/';
570 try = stpcpy(ret+try, todo->str) - ret;
571 free(llist_pop(&todo));
572 }
573
574 return ret;
575
576error:
577 close(dirfd);
578 llist_traverse(todo, free);
579 llist_traverse(done, free);
580
581 return NULL;
582}
583
Rob Landleyd3904932013-07-16 00:04:56 -0500584void xchdir(char *path)
585{
586 if (chdir(path)) error_exit("chdir '%s'", path);
587}
588
Rob Landleyafba5b82013-12-23 06:49:38 -0600589void xchroot(char *path)
590{
591 if (chroot(path)) error_exit("chroot '%s'", path);
592 xchdir("/");
593}
594
Rob Landley9e44a582013-11-28 20:18:04 -0600595struct passwd *xgetpwuid(uid_t uid)
596{
597 struct passwd *pwd = getpwuid(uid);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600598 if (!pwd) error_exit("bad uid %ld", (long)uid);
Rob Landley9e44a582013-11-28 20:18:04 -0600599 return pwd;
600}
601
602struct group *xgetgrgid(gid_t gid)
603{
604 struct group *group = getgrgid(gid);
Rob Landley4fd07e02014-07-21 19:57:36 -0500605
606 if (!group) perror_exit("gid %ld", (long)gid);
Rob Landley9e44a582013-11-28 20:18:04 -0600607 return group;
608}
609
Rob Landley3d64b0c2016-08-18 21:33:27 -0500610unsigned xgetuid(char *name)
Rob Landleyb8140d12015-03-12 11:11:08 -0500611{
Rob Landley3d64b0c2016-08-18 21:33:27 -0500612 struct passwd *up = getpwnam(name);
613 char *s = 0;
614 long uid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500615
Rob Landley3d64b0c2016-08-18 21:33:27 -0500616 if (up) return up->pw_uid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500617
Rob Landley3d64b0c2016-08-18 21:33:27 -0500618 uid = estrtol(name, &s, 10);
619 if (!errno && s && !*s && uid>=0 && uid<=UINT_MAX) return uid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500620
Rob Landley3d64b0c2016-08-18 21:33:27 -0500621 error_exit("bad user '%s'", name);
Rob Landleyb8140d12015-03-12 11:11:08 -0500622}
623
Rob Landley3d64b0c2016-08-18 21:33:27 -0500624unsigned xgetgid(char *name)
Rob Landleyb8140d12015-03-12 11:11:08 -0500625{
Rob Landley3d64b0c2016-08-18 21:33:27 -0500626 struct group *gr = getgrnam(name);
627 char *s = 0;
628 long gid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500629
Rob Landley3d64b0c2016-08-18 21:33:27 -0500630 if (gr) return gr->gr_gid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500631
Rob Landley3d64b0c2016-08-18 21:33:27 -0500632 gid = estrtol(name, &s, 10);
633 if (!errno && s && !*s && gid>=0 && gid<=UINT_MAX) return gid;
Rob Landleyb8140d12015-03-12 11:11:08 -0500634
Rob Landley3d64b0c2016-08-18 21:33:27 -0500635 error_exit("bad group '%s'", name);
Rob Landleyb8140d12015-03-12 11:11:08 -0500636}
637
Rob Landley5ec4ab32013-11-28 21:06:15 -0600638struct passwd *xgetpwnam(char *name)
639{
640 struct passwd *up = getpwnam(name);
Rob Landley4fd07e02014-07-21 19:57:36 -0500641
642 if (!up) perror_exit("user '%s'", name);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600643 return up;
644}
645
Rob Landley60c35c42014-08-03 15:50:10 -0500646struct group *xgetgrnam(char *name)
647{
648 struct group *gr = getgrnam(name);
649
650 if (!gr) perror_exit("group '%s'", name);
651 return gr;
652}
653
Rob Landleyafba5b82013-12-23 06:49:38 -0600654// setuid() can fail (for example, too many processes belonging to that user),
655// which opens a security hole if the process continues as the original user.
656
657void xsetuser(struct passwd *pwd)
658{
659 if (initgroups(pwd->pw_name, pwd->pw_gid) || setgid(pwd->pw_uid)
660 || setuid(pwd->pw_uid)) perror_exit("xsetuser '%s'", pwd->pw_name);
661}
662
Rob Landleyd3904932013-07-16 00:04:56 -0500663// This can return null (meaning file not found). It just won't return null
664// for memory allocation reasons.
665char *xreadlink(char *name)
666{
667 int len, size = 0;
668 char *buf = 0;
669
670 // Grow by 64 byte chunks until it's big enough.
671 for(;;) {
672 size +=64;
673 buf = xrealloc(buf, size);
674 len = readlink(name, buf, size);
675
676 if (len<0) {
677 free(buf);
678 return 0;
679 }
680 if (len<size) {
681 buf[len]=0;
682 return buf;
683 }
684 }
685}
686
Rob Landleydc373172013-12-27 18:45:01 -0600687char *xreadfile(char *name, char *buf, off_t len)
Rob Landleyd3904932013-07-16 00:04:56 -0500688{
Rob Landleydc373172013-12-27 18:45:01 -0600689 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
690
Rob Landleyd3904932013-07-16 00:04:56 -0500691 return buf;
692}
693
Rob Landley299d4382016-09-04 17:26:34 -0500694// The data argument to ioctl() is actually long, but it's usually used as
695// a pointer. If you need to feed in a number, do (void *)(long) typecast.
Rob Landleyd3904932013-07-16 00:04:56 -0500696int xioctl(int fd, int request, void *data)
697{
698 int rc;
699
700 errno = 0;
701 rc = ioctl(fd, request, data);
702 if (rc == -1 && errno) perror_exit("ioctl %x", request);
703
704 return rc;
705}
706
707// Open a /var/run/NAME.pid file, dying if we can't write it or if it currently
708// exists and is this executable.
709void xpidfile(char *name)
710{
711 char pidfile[256], spid[32];
712 int i, fd;
713 pid_t pid;
714
715 sprintf(pidfile, "/var/run/%s.pid", name);
716 // Try three times to open the sucker.
717 for (i=0; i<3; i++) {
Felix Jandadccfb2a2013-08-26 21:55:33 +0200718 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
Rob Landleyd3904932013-07-16 00:04:56 -0500719 if (fd != -1) break;
720
721 // If it already existed, read it. Loop for race condition.
722 fd = open(pidfile, O_RDONLY);
723 if (fd == -1) continue;
724
725 // Is the old program still there?
726 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
727 close(fd);
728 pid = atoi(spid);
Rob Landley46e8e1d2013-09-06 04:45:36 -0500729 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
Rob Landleyd3904932013-07-16 00:04:56 -0500730
731 // An else with more sanity checking might be nice here.
732 }
733
734 if (i == 3) error_exit("xpidfile %s", name);
735
736 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
737 close(fd);
738}
739
740// Copy the rest of in to out and close both files.
741
Rob Landleya1a559e2017-01-04 01:32:44 -0600742long long xsendfile(int in, int out)
Rob Landleyd3904932013-07-16 00:04:56 -0500743{
Rob Landleya1a559e2017-01-04 01:32:44 -0600744 long long total = 0;
Rob Landleyd3904932013-07-16 00:04:56 -0500745 long len;
Rob Landleyd3904932013-07-16 00:04:56 -0500746
Rob Landleya1a559e2017-01-04 01:32:44 -0600747 if (in<0) return 0;
Rob Landleyd3904932013-07-16 00:04:56 -0500748 for (;;) {
Rob Landley6d283702014-11-28 16:57:45 -0600749 len = xread(in, libbuf, sizeof(libbuf));
Rob Landleyd3904932013-07-16 00:04:56 -0500750 if (len<1) break;
Rob Landley6d283702014-11-28 16:57:45 -0600751 xwrite(out, libbuf, len);
Rob Landleya1a559e2017-01-04 01:32:44 -0600752 total += len;
Rob Landleyd3904932013-07-16 00:04:56 -0500753 }
Rob Landleya1a559e2017-01-04 01:32:44 -0600754
755 return total;
Rob Landleyd3904932013-07-16 00:04:56 -0500756}
Rob Landley72756672013-07-17 17:22:46 -0500757
758// parse fractional seconds with optional s/m/h/d suffix
759long xparsetime(char *arg, long units, long *fraction)
760{
761 double d;
762 long l;
763
764 if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg);
765 else l = strtoul(arg, &arg, 10);
Rob Landley2c1cf4a2015-01-18 14:06:14 -0600766
Rob Landley72756672013-07-17 17:22:46 -0500767 // Parse suffix
768 if (*arg) {
769 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg);
770
771 if (i == -1) error_exit("Unknown suffix '%c'", *arg);
772 if (CFG_TOYBOX_FLOAT) d *= ismhd[i];
773 else l *= ismhd[i];
774 }
775
776 if (CFG_TOYBOX_FLOAT) {
777 l = (long)d;
778 if (fraction) *fraction = units*(d-l);
779 } else if (fraction) *fraction = 0;
780
781 return l;
782}
Rob Landley5b405822014-03-29 18:11:00 -0500783
784// Compile a regular expression into a regex_t
785void xregcomp(regex_t *preg, char *regex, int cflags)
786{
787 int rc = regcomp(preg, regex, cflags);
788
789 if (rc) {
790 regerror(rc, preg, libbuf, sizeof(libbuf));
791 error_exit("xregcomp: %s", libbuf);
792 }
793}
Rob Landleyc277f342015-02-09 16:34:24 -0600794
795char *xtzset(char *new)
796{
Elliott Hughesc55d30d2016-01-09 12:20:50 -0800797 char *old = getenv("TZ");
Rob Landleyc277f342015-02-09 16:34:24 -0600798
Elliott Hughesc55d30d2016-01-09 12:20:50 -0800799 if (old) old = xstrdup(old);
800 if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
Rob Landleyc277f342015-02-09 16:34:24 -0600801 tzset();
802
Elliott Hughesc55d30d2016-01-09 12:20:50 -0800803 return old;
Rob Landleyc277f342015-02-09 16:34:24 -0600804}
Rob Landleye6abb612015-03-09 14:52:32 -0500805
806// Set a signal handler
807void xsignal(int signal, void *handler)
808{
809 struct sigaction *sa = (void *)libbuf;
810
811 memset(sa, 0, sizeof(struct sigaction));
812 sa->sa_handler = handler;
813
814 if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
815}