1
2
3
4
5
6
7
8
9
10#include "toys.h"
11
12
13
14
15
16void xstrncpy(char *dest, char *src, size_t size)
17{
18 if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size);
19 strcpy(dest, src);
20}
21
22void xstrncat(char *dest, char *src, size_t size)
23{
24 long len = strlen(dest);
25
26 if (len+strlen(src)+1 > size)
27 error_exit("'%s%s' > %ld bytes", dest, src, (long)size);
28 strcpy(dest+len, src);
29}
30
31
32
33
34
35
36
37
38void _xexit(void)
39{
40 if (toys.rebound) siglongjmp(*toys.rebound, 1);
41
42 _exit(toys.exitval);
43}
44
45void xexit(void)
46{
47
48 while (toys.xexit) {
49
50
51
52 ((void (*)(int))(toys.xexit->arg))(0);
53
54 free(llist_pop(&toys.xexit));
55 }
56 if (fflush(NULL) || ferror(stdout))
57 if (!toys.exitval) perror_msg("write");
58 _xexit();
59}
60
61void *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
68
69void *xmalloc(size_t size)
70{
71 void *ret = malloc(size);
72 if (!ret) error_exit("xmalloc(%ld)", (long)size);
73
74 return ret;
75}
76
77
78void *xzalloc(size_t size)
79{
80 void *ret = xmalloc(size);
81 memset(ret, 0, size);
82 return ret;
83}
84
85
86
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
96char *xstrndup(char *s, size_t n)
97{
98 char *ret = strndup(s, ++n);
99
100 if (!ret) error_exit("xstrndup");
101 ret[--n] = 0;
102
103 return ret;
104}
105
106
107char *xstrdup(char *s)
108{
109 return xstrndup(s, strlen(s));
110}
111
112void *xmemdup(void *s, long len)
113{
114 void *ret = xmalloc(len);
115 memcpy(ret, s, len);
116
117 return ret;
118}
119
120
121char *xmprintf(char *format, ...)
122{
123 va_list va, va2;
124 int len;
125 char *ret;
126
127 va_start(va, format);
128 va_copy(va2, va);
129
130
131 len = vsnprintf(0, 0, format, va);
132 len++;
133 va_end(va);
134
135
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);
149 va_end(va);
150 if (fflush(stdout) || ferror(stdout)) perror_exit("write");
151}
152
153void xputs(char *s)
154{
155 if (EOF == puts(s) || fflush(stdout) || ferror(stdout)) perror_exit("write");
156}
157
158void xputc(char c)
159{
160 if (EOF == fputc(c, stdout) || fflush(stdout) || ferror(stdout))
161 perror_exit("write");
162}
163
164void xflush(void)
165{
166 if (fflush(stdout) || ferror(stdout)) perror_exit("write");;
167}
168
169
170
171
172
173pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid)
174{
175 if (pid == -1) perror_exit("vfork");
176
177
178 toys.stacktop = 0;
179
180 return pid;
181}
182
183
184
185void xexec(char **argv)
186{
187
188 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE && toys.stacktop) toy_exec(argv);
189 execvp(argv[0], argv);
190
191 perror_msg("exec %s", argv[0]);
192 toys.exitval = 127;
193 if (!CFG_TOYBOX_FORK) _exit(toys.exitval);
194 xexit();
195}
196
197
198
199
200
201
202
203pid_t xpopen_both(char **argv, int *pipes)
204{
205 int cestnepasun[4], pid;
206
207
208
209 if (pipes) {
210 for (pid = 0; pid < 2; pid++) {
211 if (pipes[pid] != 0) continue;
212 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
213 pipes[pid] = cestnepasun[pid+1];
214 }
215 }
216
217
218 if (!(pid = CFG_TOYBOX_FORK ? xfork() : XVFORK())) {
219
220 if (pipes) {
221
222
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 }
230 }
231 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 }
236 }
237 if (argv) xexec(argv);
238
239
240 if (CFG_TOYBOX_FORK) {
241 toy_init(toys.which, toys.argv);
242 toys.stacktop = 0;
243 toys.which->toy_main();
244 xexit();
245
246
247 } else {
248 char *s = "/proc/self/exe";
249
250
251
252 **toys.argv |= 0x80;
253 execv(s, toys.argv);
254 perror_msg_raw(s);
255
256 _exit(127);
257 }
258 }
259
260
261 if (!CFG_TOYBOX_FORK) **toys.argv &= 0x7f;
262 if (pipes) {
263 if (pipes[0] != -1) close(cestnepasun[0]);
264 if (pipes[1] != -1) close(cestnepasun[3]);
265 }
266
267 return pid;
268}
269
270
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
280int xpclose_both(pid_t pid, int *pipes)
281{
282 if (pipes) {
283 close(pipes[0]);
284 close(pipes[1]);
285 }
286
287 return xwaitpid(pid);
288}
289
290
291pid_t xpopen(char **argv, int *pipe, int isstdout)
292{
293 int pipes[2], pid;
294
295 pipes[!isstdout] = -1;
296 pipes[!!isstdout] = 0;
297 pid = xpopen_both(argv, pipes);
298 *pipe = pid ? pipes[!!isstdout] : -1;
299
300 return pid;
301}
302
303int xpclose(pid_t pid, int pipe)
304{
305 close(pipe);
306
307 return xpclose_both(pid, 0);
308}
309
310
311int xrun(char **argv)
312{
313 return xpclose_both(xpopen_both(argv, 0), 0);
314}
315
316void xaccess(char *path, int flags)
317{
318 if (access(path, flags)) perror_exit("Can't access '%s'", path);
319}
320
321
322void xunlink(char *path)
323{
324 if (unlink(path)) perror_exit("unlink '%s'", path);
325}
326
327
328
329
330int xcreate_stdio(char *path, int flags, int mode)
331{
332 int fd = open(path, (flags^O_CLOEXEC)&~WARN_ONLY, mode);
333
334 if (fd == -1) ((mode&WARN_ONLY) ? perror_msg_raw : perror_exit_raw)(path);
335 return fd;
336}
337
338
339int xopen_stdio(char *path, int flags)
340{
341 return xcreate_stdio(path, flags, 0);
342}
343
344void xpipe(int *pp)
345{
346 if (pipe(pp)) perror_exit("xpipe");
347}
348
349void 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
363
364
365int notstdio(int fd)
366{
367 if (fd<0) return fd;
368
369 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
381int xcreate(char *path, int flags, int mode)
382{
383 return notstdio(xcreate_stdio(path, flags, mode));
384}
385
386
387int xopen(char *path, int flags)
388{
389 return notstdio(xopen_stdio(path, flags));
390}
391
392
393int openro(char *path, int flags)
394{
395 if (!strcmp(path, "-")) return 0;
396
397 return xopen(path, flags^WARN_ONLY);
398}
399
400
401int xopenro(char *path)
402{
403 return openro(path, O_RDONLY|WARN_ONLY);
404}
405
406FILE *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
415
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
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
438
439
440
441void xwrite(int fd, void *buf, size_t len)
442{
443 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
444}
445
446
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
470
471char *xabspath(char *path, int exact)
472{
473 struct string_list *todo, *done = 0;
474 int try = 9999, dirfd = open("/", 0);;
475 char *ret;
476
477
478 if (*path != '/') {
479 char *temp = xgetcwd();
480
481 splitpath(path, splitpath(temp, &todo));
482 free(temp);
483 } else splitpath(path, &todo);
484
485
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
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
506 } else len = readlinkat(dirfd, new->str, libbuf, sizeof(libbuf));
507
508 if (len>4095) goto error;
509 if (len<1) {
510 int fd;
511 char *s = "..";
512
513
514 if (len) {
515
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
530 libbuf[len] = 0;
531 if (*libbuf == '/') {
532 llist_traverse(done, free);
533 done=0;
534 close(dirfd);
535 dirfd = open("/", 0);
536 }
537 free(new);
538
539
540 tail = splitpath(libbuf, &new);
541
542
543 if (new) {
544 *tail = todo;
545 todo = new;
546 }
547 }
548 close(dirfd);
549
550
551
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
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
584void xchdir(char *path)
585{
586 if (chdir(path)) error_exit("chdir '%s'", path);
587}
588
589void xchroot(char *path)
590{
591 if (chroot(path)) error_exit("chroot '%s'", path);
592 xchdir("/");
593}
594
595struct passwd *xgetpwuid(uid_t uid)
596{
597 struct passwd *pwd = getpwuid(uid);
598 if (!pwd) error_exit("bad uid %ld", (long)uid);
599 return pwd;
600}
601
602struct group *xgetgrgid(gid_t gid)
603{
604 struct group *group = getgrgid(gid);
605
606 if (!group) perror_exit("gid %ld", (long)gid);
607 return group;
608}
609
610unsigned xgetuid(char *name)
611{
612 struct passwd *up = getpwnam(name);
613 char *s = 0;
614 long uid;
615
616 if (up) return up->pw_uid;
617
618 uid = estrtol(name, &s, 10);
619 if (!errno && s && !*s && uid>=0 && uid<=UINT_MAX) return uid;
620
621 error_exit("bad user '%s'", name);
622}
623
624unsigned xgetgid(char *name)
625{
626 struct group *gr = getgrnam(name);
627 char *s = 0;
628 long gid;
629
630 if (gr) return gr->gr_gid;
631
632 gid = estrtol(name, &s, 10);
633 if (!errno && s && !*s && gid>=0 && gid<=UINT_MAX) return gid;
634
635 error_exit("bad group '%s'", name);
636}
637
638struct passwd *xgetpwnam(char *name)
639{
640 struct passwd *up = getpwnam(name);
641
642 if (!up) perror_exit("user '%s'", name);
643 return up;
644}
645
646struct 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
654
655
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
663
664
665char *xreadlink(char *name)
666{
667 int len, size = 0;
668 char *buf = 0;
669
670
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
687char *xreadfile(char *name, char *buf, off_t len)
688{
689 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
690
691 return buf;
692}
693
694
695
696int 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
708
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
717 for (i=0; i<3; i++) {
718 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
719 if (fd != -1) break;
720
721
722 fd = open(pidfile, O_RDONLY);
723 if (fd == -1) continue;
724
725
726 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
727 close(fd);
728 pid = atoi(spid);
729 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
730
731
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
741
742long long xsendfile(int in, int out)
743{
744 long long total = 0;
745 long len;
746
747 if (in<0) return 0;
748 for (;;) {
749 len = xread(in, libbuf, sizeof(libbuf));
750 if (len<1) break;
751 xwrite(out, libbuf, len);
752 total += len;
753 }
754
755 return total;
756}
757
758double xstrtod(char *s)
759{
760 char *end;
761 double d;
762
763 errno = 0;
764 d = strtod(s, &end);
765 if (!errno && *end) errno = E2BIG;
766 if (errno) perror_exit("strtod %s", s);
767
768 return d;
769}
770
771
772long xparsetime(char *arg, long units, long *fraction)
773{
774 double d;
775 long l;
776
777 if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg);
778 else l = strtoul(arg, &arg, 10);
779
780
781 if (*arg) {
782 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg);
783
784 if (i == -1) error_exit("Unknown suffix '%c'", *arg);
785 if (CFG_TOYBOX_FLOAT) d *= ismhd[i];
786 else l *= ismhd[i];
787 }
788
789 if (CFG_TOYBOX_FLOAT) {
790 l = (long)d;
791 if (fraction) *fraction = units*(d-l);
792 } else if (fraction) *fraction = 0;
793
794 return l;
795}
796
797
798void xregcomp(regex_t *preg, char *regex, int cflags)
799{
800 int rc = regcomp(preg, regex, cflags);
801
802 if (rc) {
803 regerror(rc, preg, libbuf, sizeof(libbuf));
804 error_exit("xregcomp: %s", libbuf);
805 }
806}
807
808char *xtzset(char *new)
809{
810 char *old = getenv("TZ");
811
812 if (old) old = xstrdup(old);
813 if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
814 tzset();
815
816 return old;
817}
818
819
820void xsignal(int signal, void *handler)
821{
822 struct sigaction *sa = (void *)libbuf;
823
824 memset(sa, 0, sizeof(struct sigaction));
825 sa->sa_handler = handler;
826
827 if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
828}
829