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 struct arg_list *al = llist_pop(&toys.xexit);
50
51
52
53 ((void (*)(int))(al->arg))(0);
54
55 free(al);
56 }
57 if (fflush(NULL) || ferror(stdout))
58 if (!toys.exitval) perror_msg("write");
59 _xexit();
60}
61
62void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off)
63{
64 void *ret = mmap(addr, length, prot, flags, fd, off);
65 if (ret == MAP_FAILED) perror_exit("mmap");
66 return ret;
67}
68
69
70void *xmalloc(size_t size)
71{
72 void *ret = malloc(size);
73 if (!ret) error_exit("xmalloc(%ld)", (long)size);
74
75 return ret;
76}
77
78
79void *xzalloc(size_t size)
80{
81 void *ret = xmalloc(size);
82 memset(ret, 0, size);
83 return ret;
84}
85
86
87
88void *xrealloc(void *ptr, size_t size)
89{
90 ptr = realloc(ptr, size);
91 if (!ptr) error_exit("xrealloc");
92
93 return ptr;
94}
95
96
97char *xstrndup(char *s, size_t n)
98{
99 char *ret = strndup(s, ++n);
100
101 if (!ret) error_exit("xstrndup");
102 ret[--n] = 0;
103
104 return ret;
105}
106
107
108char *xstrdup(char *s)
109{
110 return xstrndup(s, strlen(s));
111}
112
113void *xmemdup(void *s, long len)
114{
115 void *ret = xmalloc(len);
116 memcpy(ret, s, len);
117
118 return ret;
119}
120
121
122char *xmprintf(char *format, ...)
123{
124 va_list va, va2;
125 int len;
126 char *ret;
127
128 va_start(va, format);
129 va_copy(va2, va);
130
131
132 len = vsnprintf(0, 0, format, va);
133 len++;
134 va_end(va);
135
136
137 ret = xmalloc(len);
138 vsnprintf(ret, len, format, va2);
139 va_end(va2);
140
141 return ret;
142}
143
144void xflush(void)
145{
146 if (fflush(0) || ferror(stdout)) perror_exit("write");
147}
148
149void xprintf(char *format, ...)
150{
151 va_list va;
152 va_start(va, format);
153
154 vprintf(format, va);
155 va_end(va);
156 xflush();
157}
158
159
160void xputsl(char *s, int len)
161{
162 int out;
163
164 while (len != (out = fwrite(s, 1, len, stdout))) {
165 if (out<1) perror_exit("write");
166 len -= out;
167 s += out;
168 }
169 xflush();
170}
171
172
173void xputsn(char *s)
174{
175 xputsl(s, strlen(s));
176}
177
178
179void xputs(char *s)
180{
181 puts(s);
182 xflush();
183}
184
185void xputc(char c)
186{
187 if (EOF == fputc(c, stdout)) perror_exit("write");
188 xflush();
189}
190
191
192
193
194
195pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid)
196{
197 if (pid == -1) perror_exit("vfork");
198
199
200 toys.stacktop = 0;
201
202 return pid;
203}
204
205
206
207void xexec(char **argv)
208{
209
210 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE && toys.stacktop && **argv != '/')
211 toy_exec(argv);
212 execvp(argv[0], argv);
213
214 perror_msg("exec %s", argv[0]);
215 toys.exitval = 127;
216 if (!toys.stacktop) _exit(toys.exitval);
217 xexit();
218}
219
220
221
222
223
224
225
226pid_t xpopen_both(char **argv, int *pipes)
227{
228 int cestnepasun[4], pid;
229
230
231
232 if (pipes) {
233 for (pid = 0; pid < 2; pid++) {
234 if (pipes[pid] != 0) continue;
235 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
236 pipes[pid] = cestnepasun[pid+1];
237 }
238 }
239
240
241 if (!(pid = CFG_TOYBOX_FORK ? xfork() : XVFORK())) {
242
243 if (pipes) {
244
245
246 if (pipes[1] != -1) close(cestnepasun[2]);
247 if (pipes[0] != -1) {
248 close(cestnepasun[1]);
249 if (cestnepasun[0]) {
250 dup2(cestnepasun[0], 0);
251 close(cestnepasun[0]);
252 }
253 }
254 if (pipes[1] != -1) {
255 dup2(cestnepasun[3], 1);
256 dup2(cestnepasun[3], 2);
257 if (cestnepasun[3] > 2 || !cestnepasun[3]) close(cestnepasun[3]);
258 }
259 }
260 if (argv) xexec(argv);
261
262
263 if (CFG_TOYBOX_FORK) {
264 toy_init(toys.which, toys.argv);
265 toys.stacktop = 0;
266 toys.which->toy_main();
267 xexit();
268
269
270 } else {
271 char *s = "/proc/self/exe";
272
273
274
275 **toys.argv |= 0x80;
276 execv(s, toys.argv);
277 perror_msg_raw(s);
278
279 _exit(127);
280 }
281 }
282
283
284 if (!CFG_TOYBOX_FORK) **toys.argv &= 0x7f;
285 if (pipes) {
286 if (pipes[0] != -1) close(cestnepasun[0]);
287 if (pipes[1] != -1) close(cestnepasun[3]);
288 }
289
290 return pid;
291}
292
293
294int xwaitpid(pid_t pid)
295{
296 int status;
297
298 while (-1 == waitpid(pid, &status, 0) && errno == EINTR);
299
300 return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
301}
302
303int xpclose_both(pid_t pid, int *pipes)
304{
305 if (pipes) {
306 close(pipes[0]);
307 close(pipes[1]);
308 }
309
310 return xwaitpid(pid);
311}
312
313
314pid_t xpopen(char **argv, int *pipe, int isstdout)
315{
316 int pipes[2], pid;
317
318 pipes[!isstdout] = -1;
319 pipes[!!isstdout] = 0;
320 pid = xpopen_both(argv, pipes);
321 *pipe = pid ? pipes[!!isstdout] : -1;
322
323 return pid;
324}
325
326int xpclose(pid_t pid, int pipe)
327{
328 close(pipe);
329
330 return xpclose_both(pid, 0);
331}
332
333
334int xrun(char **argv)
335{
336 return xpclose_both(xpopen_both(argv, 0), 0);
337}
338
339void xaccess(char *path, int flags)
340{
341 if (access(path, flags)) perror_exit("Can't access '%s'", path);
342}
343
344
345void xunlink(char *path)
346{
347 if (unlink(path)) perror_exit("unlink '%s'", path);
348}
349
350
351
352
353int xcreate_stdio(char *path, int flags, int mode)
354{
355 int fd = open(path, (flags^O_CLOEXEC)&~WARN_ONLY, mode);
356
357 if (fd == -1) ((mode&WARN_ONLY) ? perror_msg_raw : perror_exit_raw)(path);
358 return fd;
359}
360
361
362int xopen_stdio(char *path, int flags)
363{
364 return xcreate_stdio(path, flags, 0);
365}
366
367void xpipe(int *pp)
368{
369 if (pipe(pp)) perror_exit("xpipe");
370}
371
372void xclose(int fd)
373{
374 if (close(fd)) perror_exit("xclose");
375}
376
377int xdup(int fd)
378{
379 if (fd != -1) {
380 fd = dup(fd);
381 if (fd == -1) perror_exit("xdup");
382 }
383 return fd;
384}
385
386
387
388int notstdio(int fd)
389{
390 if (fd<0) return fd;
391
392 while (fd<3) {
393 int fd2 = xdup(fd);
394
395 close(fd);
396 xopen_stdio("/dev/null", O_RDWR);
397 fd = fd2;
398 }
399
400 return fd;
401}
402
403void xrename(char *from, char *to)
404{
405 if (rename(from, to)) perror_exit("rename %s -> %s", from, to);
406}
407
408int xtempfile(char *name, char **tempname)
409{
410 int fd;
411
412 *tempname = xmprintf("%s%s", name, "XXXXXX");
413 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
414
415 return fd;
416}
417
418
419int xcreate(char *path, int flags, int mode)
420{
421 return notstdio(xcreate_stdio(path, flags, mode));
422}
423
424
425int xopen(char *path, int flags)
426{
427 return notstdio(xopen_stdio(path, flags));
428}
429
430
431int openro(char *path, int flags)
432{
433 if (!strcmp(path, "-")) return 0;
434
435 return xopen(path, flags^WARN_ONLY);
436}
437
438
439int xopenro(char *path)
440{
441 return openro(path, O_RDONLY|WARN_ONLY);
442}
443
444FILE *xfdopen(int fd, char *mode)
445{
446 FILE *f = fdopen(fd, mode);
447
448 if (!f) perror_exit("xfdopen");
449
450 return f;
451}
452
453
454FILE *xfopen(char *path, char *mode)
455{
456 FILE *f = fopen(path, mode);
457 if (!f) perror_exit("No file %s", path);
458 return f;
459}
460
461
462size_t xread(int fd, void *buf, size_t len)
463{
464 ssize_t ret = read(fd, buf, len);
465 if (ret < 0) perror_exit("xread");
466
467 return ret;
468}
469
470void xreadall(int fd, void *buf, size_t len)
471{
472 if (len != readall(fd, buf, len)) perror_exit("xreadall");
473}
474
475
476
477
478
479void xwrite(int fd, void *buf, size_t len)
480{
481 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
482}
483
484
485
486off_t xlseek(int fd, off_t offset, int whence)
487{
488 offset = lseek(fd, offset, whence);
489 if (offset<0) perror_exit("lseek");
490
491 return offset;
492}
493
494char *xgetcwd(void)
495{
496 char *buf = getcwd(NULL, 0);
497 if (!buf) perror_exit("xgetcwd");
498
499 return buf;
500}
501
502void xstat(char *path, struct stat *st)
503{
504 if(stat(path, st)) perror_exit("Can't stat %s", path);
505}
506
507
508
509char *xabspath(char *path, int exact)
510{
511 struct string_list *todo, *done = 0;
512 int try = 9999, dirfd = open("/", 0), missing = 0;
513 char *ret;
514
515
516 if (*path != '/') {
517 char *temp = xgetcwd();
518
519 splitpath(path, splitpath(temp, &todo));
520 free(temp);
521 } else splitpath(path, &todo);
522
523
524 while (todo) {
525 struct string_list *new = llist_pop(&todo), **tail;
526 ssize_t len;
527
528
529 if (!try--) {
530 errno = ELOOP;
531 goto error;
532 }
533
534
535 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) {
536 int x = new->str[1];
537
538 free(new);
539 if (!x) continue;
540 if (done) free(llist_pop(&done));
541 len = 0;
542
543 if (missing) missing--;
544 else {
545 if (-1 == (x = openat(dirfd, "..", 0))) goto error;
546 close(dirfd);
547 dirfd = x;
548 }
549 continue;
550 }
551
552
553 len = readlinkat(dirfd, new->str, libbuf, sizeof(libbuf));
554 if (len>4095) goto error;
555
556
557 if (len<1) {
558 int fd;
559
560 new->next = done;
561 done = new;
562 if (errno == EINVAL && !todo) break;
563 if (errno == ENOENT && exact<0) {
564 missing++;
565 continue;
566 }
567 if (errno != EINVAL && (exact || todo)) goto error;
568
569 fd = openat(dirfd, new->str, 0);
570 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error;
571 close(dirfd);
572 dirfd = fd;
573 continue;
574 }
575
576
577 libbuf[len] = 0;
578 if (*libbuf == '/') {
579 llist_traverse(done, free);
580 done=0;
581 close(dirfd);
582 dirfd = open("/", 0);
583 }
584 free(new);
585
586
587 tail = splitpath(libbuf, &new);
588
589
590 if (new) {
591 *tail = todo;
592 todo = new;
593 }
594 }
595 close(dirfd);
596
597
598
599
600 try = 2;
601 while (done) {
602 struct string_list *temp = llist_pop(&done);;
603
604 if (todo) try++;
605 try += strlen(temp->str);
606 temp->next = todo;
607 todo = temp;
608 }
609
610
611
612 ret = xmalloc(try);
613 *ret = '/';
614 ret [try = 1] = 0;
615 while (todo) {
616 if (try>1) ret[try++] = '/';
617 try = stpcpy(ret+try, todo->str) - ret;
618 free(llist_pop(&todo));
619 }
620
621 return ret;
622
623error:
624 close(dirfd);
625 llist_traverse(todo, free);
626 llist_traverse(done, free);
627
628 return 0;
629}
630
631void xchdir(char *path)
632{
633 if (chdir(path)) perror_exit("chdir '%s'", path);
634}
635
636void xchroot(char *path)
637{
638 if (chroot(path)) error_exit("chroot '%s'", path);
639 xchdir("/");
640}
641
642struct passwd *xgetpwuid(uid_t uid)
643{
644 struct passwd *pwd = getpwuid(uid);
645 if (!pwd) error_exit("bad uid %ld", (long)uid);
646 return pwd;
647}
648
649struct group *xgetgrgid(gid_t gid)
650{
651 struct group *group = getgrgid(gid);
652
653 if (!group) perror_exit("gid %ld", (long)gid);
654 return group;
655}
656
657unsigned xgetuid(char *name)
658{
659 struct passwd *up = getpwnam(name);
660 char *s = 0;
661 long uid;
662
663 if (up) return up->pw_uid;
664
665 uid = estrtol(name, &s, 10);
666 if (!errno && s && !*s && uid>=0 && uid<=UINT_MAX) return uid;
667
668 error_exit("bad user '%s'", name);
669}
670
671unsigned xgetgid(char *name)
672{
673 struct group *gr = getgrnam(name);
674 char *s = 0;
675 long gid;
676
677 if (gr) return gr->gr_gid;
678
679 gid = estrtol(name, &s, 10);
680 if (!errno && s && !*s && gid>=0 && gid<=UINT_MAX) return gid;
681
682 error_exit("bad group '%s'", name);
683}
684
685struct passwd *xgetpwnam(char *name)
686{
687 struct passwd *up = getpwnam(name);
688
689 if (!up) perror_exit("user '%s'", name);
690 return up;
691}
692
693struct group *xgetgrnam(char *name)
694{
695 struct group *gr = getgrnam(name);
696
697 if (!gr) perror_exit("group '%s'", name);
698 return gr;
699}
700
701
702
703
704void xsetuser(struct passwd *pwd)
705{
706 if (initgroups(pwd->pw_name, pwd->pw_gid) || setgid(pwd->pw_uid)
707 || setuid(pwd->pw_uid)) perror_exit("xsetuser '%s'", pwd->pw_name);
708}
709
710
711
712char *xreadlink(char *name)
713{
714 int len, size = 0;
715 char *buf = 0;
716
717
718 for(;;) {
719 size +=64;
720 buf = xrealloc(buf, size);
721 len = readlink(name, buf, size);
722
723 if (len<0) {
724 free(buf);
725 return 0;
726 }
727 if (len<size) {
728 buf[len]=0;
729 return buf;
730 }
731 }
732}
733
734char *xreadfile(char *name, char *buf, off_t len)
735{
736 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
737
738 return buf;
739}
740
741
742
743int xioctl(int fd, int request, void *data)
744{
745 int rc;
746
747 errno = 0;
748 rc = ioctl(fd, request, data);
749 if (rc == -1 && errno) perror_exit("ioctl %x", request);
750
751 return rc;
752}
753
754
755
756void xpidfile(char *name)
757{
758 char pidfile[256], spid[32];
759 int i, fd;
760 pid_t pid;
761
762 sprintf(pidfile, "/var/run/%s.pid", name);
763
764 for (i=0; i<3; i++) {
765 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
766 if (fd != -1) break;
767
768
769 fd = open(pidfile, O_RDONLY);
770 if (fd == -1) continue;
771
772
773 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
774 close(fd);
775 pid = atoi(spid);
776 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
777
778
779 }
780
781 if (i == 3) error_exit("xpidfile %s", name);
782
783 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
784 close(fd);
785}
786
787
788
789long long xsendfile(int in, int out)
790{
791 long long total = 0;
792 long len;
793
794 if (in<0) return 0;
795 for (;;) {
796 len = xread(in, libbuf, sizeof(libbuf));
797 if (len<1) break;
798 xwrite(out, libbuf, len);
799 total += len;
800 }
801
802 return total;
803}
804
805double xstrtod(char *s)
806{
807 char *end;
808 double d;
809
810 errno = 0;
811 d = strtod(s, &end);
812 if (!errno && *end) errno = E2BIG;
813 if (errno) perror_exit("strtod %s", s);
814
815 return d;
816}
817
818
819long xparsetime(char *arg, long zeroes, long *fraction)
820{
821 long l, fr = 0, mask = 1;
822 char *end;
823
824 if (*arg != '.' && !isdigit(*arg)) error_exit("Not a number '%s'", arg);
825 l = strtoul(arg, &end, 10);
826 if (*end == '.') {
827 end++;
828 while (zeroes--) {
829 fr *= 10;
830 mask *= 10;
831 if (isdigit(*end)) fr += *end++-'0';
832 }
833 while (isdigit(*end)) end++;
834 }
835
836
837 if (*end) {
838 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *end);
839
840 if (i == -1 || *(end+1)) error_exit("Unknown suffix '%s'", end);
841 l *= ismhd[i];
842 fr *= ismhd[i];
843 l += fr/mask;
844 fr %= mask;
845 }
846 if (fraction) *fraction = fr;
847
848 return l;
849}
850
851long long xparsemillitime(char *arg)
852{
853 long l, ll;
854
855 l = xparsetime(arg, 3, &ll);
856
857 return (l*1000LL)+ll;
858}
859
860
861
862
863void xregcomp(regex_t *preg, char *regex, int cflags)
864{
865 int rc = regcomp(preg, regex, cflags);
866
867 if (rc) {
868 regerror(rc, preg, libbuf, sizeof(libbuf));
869 error_exit("xregcomp: %s", libbuf);
870 }
871}
872
873char *xtzset(char *new)
874{
875 char *old = getenv("TZ");
876
877 if (old) old = xstrdup(old);
878 if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
879 tzset();
880
881 return old;
882}
883
884
885void xsignal_flags(int signal, void *handler, int flags)
886{
887 struct sigaction *sa = (void *)libbuf;
888
889 memset(sa, 0, sizeof(struct sigaction));
890 sa->sa_handler = handler;
891 sa->sa_flags = flags;
892
893 if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
894}
895
896void xsignal(int signal, void *handler)
897{
898 xsignal_flags(signal, handler, 0);
899}
900