annotate lib/xwrap.c @ 1528:ec07449e8e4a draft

Add TOYBOX_NORECURSE so xexec() won't make internal function calls.
author Rob Landley <rob@landley.net>
date Mon, 20 Oct 2014 19:52:29 -0500
parents bff076394df5
children e127aa575ff2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* xwrap.c - wrappers around existing library functions.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Functions with the x prefix are wrappers that either succeed or kill the
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * program with an error message, but never return failure. They usually have
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * the same arguments and return value as the function they wrap.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Copyright 2006 Rob Landley <rob@landley.net>
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
8 */
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
9
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
10 #include "toys.h"
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
11
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
12 // Strcpy with size checking: exit if there's not enough space for the string.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
13 void xstrncpy(char *dest, char *src, size_t size)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
14 {
1105
1bca28705a87 Give xstrncpy() a more informative error message.
Rob Landley <rob@landley.net>
parents: 1053
diff changeset
15 if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size);
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
16 strcpy(dest, src);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
17 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
18
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
19 void xexit(void)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
20 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
21 if (toys.rebound) longjmp(*toys.rebound, 1);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
22 else exit(toys.exitval);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
23 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
24
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
25 // Die unless we can allocate memory.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
26 void *xmalloc(size_t size)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
27 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
28 void *ret = malloc(size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
29 if (!ret) error_exit("xmalloc");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
30
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
31 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
32 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
33
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
34 // Die unless we can allocate prezeroed memory.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
35 void *xzalloc(size_t size)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
37 void *ret = xmalloc(size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
38 memset(ret, 0, size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
39 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
40 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
41
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
42 // Die unless we can change the size of an existing allocation, possibly
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
43 // moving it. (Notice different arguments from libc function.)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
44 void *xrealloc(void *ptr, size_t size)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
45 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
46 ptr = realloc(ptr, size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (!ptr) error_exit("xrealloc");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
48
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
49 return ptr;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
50 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
51
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
52 // Die unless we can allocate a copy of this many bytes of string.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
53 char *xstrndup(char *s, size_t n)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
54 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
55 char *ret = xmalloc(++n);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
56 strncpy(ret, s, n);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
57 ret[--n]=0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
58
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
59 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
60 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
61
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
62 // Die unless we can allocate a copy of this string.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
63 char *xstrdup(char *s)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
64 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
65 return xstrndup(s, strlen(s));
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
67
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
68 // Die unless we can allocate enough space to sprintf() into.
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1170
diff changeset
69 char *xmprintf(char *format, ...)
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
70 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
71 va_list va, va2;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
72 int len;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
73 char *ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
74
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
75 va_start(va, format);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
76 va_copy(va2, va);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
77
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
78 // How long is it?
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
79 len = vsnprintf(0, 0, format, va);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
80 len++;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
81 va_end(va);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
82
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
83 // Allocate and do the sprintf()
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
84 ret = xmalloc(len);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
85 vsnprintf(ret, len, format, va2);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
86 va_end(va2);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
87
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
88 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
89 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
90
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
91 void xprintf(char *format, ...)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
92 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
93 va_list va;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
94 va_start(va, format);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
95
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
96 vprintf(format, va);
1318
955169e818d0 Isaac Dunham suggested xprintf() should call fflush() instead of ferror(), and posix-2008 doesn't say if fflush() covers ferror() (or can return success when the stream's error state is set), so call both.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
97 if (fflush(stdout) || ferror(stdout)) perror_exit("write");
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
98 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
99
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
100 void xputs(char *s)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
101 {
1318
955169e818d0 Isaac Dunham suggested xprintf() should call fflush() instead of ferror(), and posix-2008 doesn't say if fflush() covers ferror() (or can return success when the stream's error state is set), so call both.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
102 if (EOF == puts(s) || fflush(stdout) || ferror(stdout)) perror_exit("write");
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
103 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
104
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
105 void xputc(char c)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
106 {
1318
955169e818d0 Isaac Dunham suggested xprintf() should call fflush() instead of ferror(), and posix-2008 doesn't say if fflush() covers ferror() (or can return success when the stream's error state is set), so call both.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
107 if (EOF == fputc(c, stdout) || fflush(stdout) || ferror(stdout))
955169e818d0 Isaac Dunham suggested xprintf() should call fflush() instead of ferror(), and posix-2008 doesn't say if fflush() covers ferror() (or can return success when the stream's error state is set), so call both.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
108 perror_exit("write");
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
109 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
110
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
111 void xflush(void)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
112 {
1318
955169e818d0 Isaac Dunham suggested xprintf() should call fflush() instead of ferror(), and posix-2008 doesn't say if fflush() covers ferror() (or can return success when the stream's error state is set), so call both.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
113 if (fflush(stdout) || ferror(stdout)) perror_exit("write");;
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
114 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
115
1327
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
116 pid_t xfork(void)
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
117 {
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
118 pid_t pid = fork();
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
119
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
120 if (pid < 0) perror_exit("fork");
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
121
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
122 return pid;
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
123 }
85f297591693 Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents: 1318
diff changeset
124
952
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
125 // 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
126 // 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
127 void xexec_optargs(int skip)
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
128 {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
129 char **s = toys.optargs;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
130
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
131 toys.optargs = 0;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
132 xexec(s+skip);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
133 }
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
134
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
135
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
136 // Die unless we can exec argv[] (or run builtin command). Note that anything
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
137 // with a path isn't a builtin, so /bin/sh won't match the builtin sh.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
138 void xexec(char **argv)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
139 {
1528
ec07449e8e4a Add TOYBOX_NORECURSE so xexec() won't make internal function calls.
Rob Landley <rob@landley.net>
parents: 1473
diff changeset
140 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE) toy_exec(argv);
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
141 execvp(argv[0], argv);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
142
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
143 perror_exit("exec %s", argv[0]);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
144 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
145
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
146 // Spawn child process, capturing stdin/stdout.
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
147 // argv[]: command to exec. If null, child returns to original program.
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
148 // pipes[2]: stdin, stdout of new process. If -1 will not have pipe allocated.
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
149 // return: pid of child process
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
150 pid_t xpopen_both(char **argv, int *pipes)
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
151 {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
152 int cestnepasun[4], pid;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
153
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
154 // Make the pipes? Not this won't set either pipe to 0 because if fds are
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
155 // allocated in order and if fd0 was free it would go to cestnepasun[0]
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
156 if (pipes) {
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
157 for (pid = 0; pid < 2; pid++) {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
158 if (pipes[pid] == -1) continue;
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
159 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
160 pipes[pid] = cestnepasun[pid+1];
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
161 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
162 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
163
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
164 // Child process
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
165 if (!(pid = xfork())) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
166 // Dance of the stdin/stdout redirection.
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
167 if (pipes) {
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
168 // if we had no stdin/out, pipe handles could overlap, so test for it
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
169 // and free up potentially overlapping pipe handles before reuse
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
170 if (pipes[1] != -1) close(cestnepasun[2]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
171 if (pipes[0] != -1) {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
172 close(cestnepasun[1]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
173 if (cestnepasun[0]) {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
174 dup2(cestnepasun[0], 0);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
175 close(cestnepasun[0]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
176 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
177 }
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
178 if (pipes[1] != -1) {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
179 dup2(cestnepasun[3], 1);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
180 dup2(cestnepasun[3], 2);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
181 if (cestnepasun[3] > 2 || !cestnepasun[3]) close(cestnepasun[3]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
182 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
183 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
184 if (argv) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
185 if (CFG_TOYBOX) toy_exec(argv);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
186 execvp(argv[0], argv);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
187 _exit(127);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
188 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
189 return 0;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
190
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
191 }
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
192
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
193 // Parent process
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
194 if (pipes) {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
195 if (pipes[0] != -1) close(cestnepasun[0]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
196 if (pipes[1] != -1) close(cestnepasun[3]);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
197 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
198
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
199 return pid;
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
200 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
201
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
202 int xpclose_both(pid_t pid, int *pipes)
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
203 {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
204 int rc = 127;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
205
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
206 if (pipes) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
207 close(pipes[0]);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
208 close(pipes[1]);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
209 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
210 waitpid(pid, &rc, 0);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
211
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
212 return WIFEXITED(rc) ? WEXITSTATUS(rc) : WTERMSIG(rc) + 127;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
213 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
214
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
215 // Wrapper to xpopen with a pipe for just one of stdin/stdout
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
216 pid_t xpopen(char **argv, int *pipe, int stdout)
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
217 {
1473
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
218 int pipes[2], pid;
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
219
1473
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
220 pipes[!stdout] = -1;
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
221 pipes[!!stdout] = 0;
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
222 pid = xpopen_both(argv, pipes);
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
223 *pipe = pid ? pipes[!!stdout] : -1;
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
224
1473
bff076394df5 Second attempt at one way xpopen().
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
225 return pid;
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
226 }
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
227
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
228 int xpclose(pid_t pid, int pipe)
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
229 {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
230 close(pipe);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
231
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
232 return xpclose_both(pid, 0);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
233 }
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
234
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
235 // Call xpopen and wait for it to finish, keeping existing stdin/stdout.
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
236 int xrun(char **argv)
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
237 {
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
238 return xpclose_both(xpopen_both(argv, 0), 0);
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
239 }
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1420
diff changeset
240
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
241 void xaccess(char *path, int flags)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
242 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
243 if (access(path, flags)) perror_exit("Can't access '%s'", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
244 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
245
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
246 // Die unless we can delete a file. (File must exist to be deleted.)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
247 void xunlink(char *path)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
248 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
249 if (unlink(path)) perror_exit("unlink '%s'", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
250 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
251
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
252 // Die unless we can open/create a file, returning file descriptor.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
253 int xcreate(char *path, int flags, int mode)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
254 {
1403
411cf82cdf77 Default xcreate/xopen to O_CLOEXEC. (Pass O_CLOEXEC in the flags to switch it back off.)
Rob Landley <rob@landley.net>
parents: 1400
diff changeset
255 int fd = open(path, flags^O_CLOEXEC, mode);
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
256 if (fd == -1) perror_exit("%s", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
257 return fd;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
258 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
259
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
260 // Die unless we can open a file, returning file descriptor.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
261 int xopen(char *path, int flags)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
262 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
263 return xcreate(path, flags, 0);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
264 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
265
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
266 void xclose(int fd)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
267 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
268 if (close(fd)) perror_exit("xclose");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
269 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
270
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
271 int xdup(int fd)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
272 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
273 if (fd != -1) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
274 fd = dup(fd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
275 if (fd == -1) perror_exit("xdup");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
276 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
277 return fd;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
278 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
279
991
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
280 FILE *xfdopen(int fd, char *mode)
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
281 {
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
282 FILE *f = fdopen(fd, mode);
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
283
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
284 if (!f) perror_exit("xfdopen");
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
285
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
286 return f;
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
287 }
252caf3d2b88 Forgot to check in xfdopen(). My bad.
Rob Landley <rob@landley.net>
parents: 956
diff changeset
288
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
289 // Die unless we can open/create a file, returning FILE *.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
290 FILE *xfopen(char *path, char *mode)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
291 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
292 FILE *f = fopen(path, mode);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
293 if (!f) perror_exit("No file %s", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
294 return f;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
295 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
296
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
297 // Die if there's an error other than EOF.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
298 size_t xread(int fd, void *buf, size_t len)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
299 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
300 ssize_t ret = read(fd, buf, len);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
301 if (ret < 0) perror_exit("xread");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
302
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
303 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
304 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
305
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
306 void xreadall(int fd, void *buf, size_t len)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
307 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
308 if (len != readall(fd, buf, len)) perror_exit("xreadall");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
309 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
310
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
311 // There's no xwriteall(), just xwrite(). When we read, there may or may not
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
312 // be more data waiting. When we write, there is data and it had better go
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
313 // somewhere.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
314
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
315 void xwrite(int fd, void *buf, size_t len)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
316 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
317 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
318 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
319
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
320 // Die if lseek fails, probably due to being called on a pipe.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
321
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
322 off_t xlseek(int fd, off_t offset, int whence)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
323 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
324 offset = lseek(fd, offset, whence);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
325 if (offset<0) perror_exit("lseek");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
326
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
327 return offset;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
328 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
329
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
330 char *xgetcwd(void)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
331 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
332 char *buf = getcwd(NULL, 0);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
333 if (!buf) perror_exit("xgetcwd");
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
334
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
335 return buf;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
336 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
337
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
338 void xstat(char *path, struct stat *st)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
339 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
340 if(stat(path, st)) perror_exit("Can't stat %s", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
341 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
342
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
343 // Cannonicalize path, even to file with one or more missing components at end.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
344 // if exact, require last path component to exist
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
345 char *xabspath(char *path, int exact)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
346 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
347 struct string_list *todo, *done = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
348 int try = 9999, dirfd = open("/", 0);;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
349 char buf[4096], *ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
350
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
351 // If this isn't an absolute path, start with cwd.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
352 if (*path != '/') {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
353 char *temp = xgetcwd();
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
354
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
355 splitpath(path, splitpath(temp, &todo));
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
356 free(temp);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
357 } else splitpath(path, &todo);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
358
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
359 // Iterate through path components
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
360 while (todo) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
361 struct string_list *new = llist_pop(&todo), **tail;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
362 ssize_t len;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
363
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
364 if (!try--) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
365 errno = ELOOP;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
366 goto error;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
367 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
368
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
369 // Removable path componenents.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
370 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
371 int x = new->str[1];
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
372
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
373 free(new);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
374 if (x) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
375 if (done) free(llist_pop(&done));
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
376 len = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
377 } else continue;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
378
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
379 // Is this a symlink?
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
380 } else len=readlinkat(dirfd, new->str, buf, 4096);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
381
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
382 if (len>4095) goto error;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
383 if (len<1) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
384 int fd;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
385 char *s = "..";
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
386
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
387 // For .. just move dirfd
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
388 if (len) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
389 // Not a symlink: add to linked list, move dirfd, fail if error
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
390 if ((exact || todo) && errno != EINVAL) goto error;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
391 new->next = done;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
392 done = new;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
393 if (errno == EINVAL && !todo) break;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
394 s = new->str;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
395 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
396 fd = openat(dirfd, s, 0);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
397 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
398 close(dirfd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
399 dirfd = fd;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
400 continue;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
401 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
402
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
403 // If this symlink is to an absolute path, discard existing resolved path
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
404 buf[len] = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
405 if (*buf == '/') {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
406 llist_traverse(done, free);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
407 done=0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
408 close(dirfd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
409 dirfd = open("/", 0);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
410 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
411 free(new);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
412
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
413 // prepend components of new path. Note symlink to "/" will leave new NULL
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
414 tail = splitpath(buf, &new);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
415
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
416 // symlink to "/" will return null and leave tail alone
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
417 if (new) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
418 *tail = todo;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
419 todo = new;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
420 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
421 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
422 close(dirfd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
423
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
424 // At this point done has the path, in reverse order. Reverse list while
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
425 // calculating buffer length.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
426
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
427 try = 2;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
428 while (done) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
429 struct string_list *temp = llist_pop(&done);;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
430
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
431 if (todo) try++;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
432 try += strlen(temp->str);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
433 temp->next = todo;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
434 todo = temp;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
435 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
436
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
437 // Assemble return buffer
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
438
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
439 ret = xmalloc(try);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
440 *ret = '/';
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
441 ret [try = 1] = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
442 while (todo) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
443 if (try>1) ret[try++] = '/';
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
444 try = stpcpy(ret+try, todo->str) - ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
445 free(llist_pop(&todo));
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
446 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
447
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
448 return ret;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
449
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
450 error:
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
451 close(dirfd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
452 llist_traverse(todo, free);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
453 llist_traverse(done, free);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
454
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
455 return NULL;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
456 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
457
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
458 void xchdir(char *path)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
459 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
460 if (chdir(path)) error_exit("chdir '%s'", path);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
461 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
462
1156
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
463 void xchroot(char *path)
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
464 {
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
465 if (chroot(path)) error_exit("chroot '%s'", path);
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
466 xchdir("/");
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
467 }
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
468
1129
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
469 struct passwd *xgetpwuid(uid_t uid)
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
470 {
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
471 struct passwd *pwd = getpwuid(uid);
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
472 if (!pwd) error_exit("bad uid %ld", (long)uid);
1129
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
473 return pwd;
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
474 }
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
475
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
476 struct group *xgetgrgid(gid_t gid)
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
477 {
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
478 struct group *group = getgrgid(gid);
1400
31cb9ba1815c Improve gid/uid error messages.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
479
31cb9ba1815c Improve gid/uid error messages.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
480 if (!group) perror_exit("gid %ld", (long)gid);
1129
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
481 return group;
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
482 }
c644f85444d0 Move xgetpwuid() and xgetgrgid() into xwrap.c
Rob Landley <rob@landley.net>
parents: 1105
diff changeset
483
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
484 struct passwd *xgetpwnam(char *name)
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
485 {
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
486 struct passwd *up = getpwnam(name);
1400
31cb9ba1815c Improve gid/uid error messages.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
487
31cb9ba1815c Improve gid/uid error messages.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
488 if (!up) perror_exit("user '%s'", name);
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
489 return up;
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
490 }
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
491
1420
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
492 struct group *xgetgrnam(char *name)
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
493 {
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
494 struct group *gr = getgrnam(name);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
495
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
496 if (!gr) perror_exit("group '%s'", name);
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
497 return gr;
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
498 }
9d760c092c41 Implement exec -user, -group, and -newer. Enable find in defconfig.
Rob Landley <rob@landley.net>
parents: 1408
diff changeset
499
1156
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
500 // setuid() can fail (for example, too many processes belonging to that user),
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
501 // which opens a security hole if the process continues as the original user.
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
502
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
503 void xsetuser(struct passwd *pwd)
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
504 {
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
505 if (initgroups(pwd->pw_name, pwd->pw_gid) || setgid(pwd->pw_uid)
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
506 || setuid(pwd->pw_uid)) perror_exit("xsetuser '%s'", pwd->pw_name);
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
507 }
faf7117c4489 Fix some issues raised (albeit indirectly) by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1130
diff changeset
508
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
509 // This can return null (meaning file not found). It just won't return null
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
510 // for memory allocation reasons.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
511 char *xreadlink(char *name)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
512 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
513 int len, size = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
514 char *buf = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
515
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
516 // Grow by 64 byte chunks until it's big enough.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
517 for(;;) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
518 size +=64;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
519 buf = xrealloc(buf, size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
520 len = readlink(name, buf, size);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
521
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
522 if (len<0) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
523 free(buf);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
524 return 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
525 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
526 if (len<size) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
527 buf[len]=0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
528 return buf;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
529 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
530 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
531 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
532
1170
8afe1fde9314 Pass through all the readfile() arguments from xreadfile().
Rob Landley <rob@landley.net>
parents: 1156
diff changeset
533 char *xreadfile(char *name, char *buf, off_t len)
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
534 {
1170
8afe1fde9314 Pass through all the readfile() arguments from xreadfile().
Rob Landley <rob@landley.net>
parents: 1156
diff changeset
535 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
8afe1fde9314 Pass through all the readfile() arguments from xreadfile().
Rob Landley <rob@landley.net>
parents: 1156
diff changeset
536
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
537 return buf;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
538 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
539
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
540 int xioctl(int fd, int request, void *data)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
541 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
542 int rc;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
543
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
544 errno = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
545 rc = ioctl(fd, request, data);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
546 if (rc == -1 && errno) perror_exit("ioctl %x", request);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
547
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
548 return rc;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
549 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
550
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
551 // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
552 // exists and is this executable.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
553 void xpidfile(char *name)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
554 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
555 char pidfile[256], spid[32];
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
556 int i, fd;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
557 pid_t pid;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
558
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
559 sprintf(pidfile, "/var/run/%s.pid", name);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
560 // Try three times to open the sucker.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
561 for (i=0; i<3; i++) {
1028
58bfd974216d syslogd: cleanup
Felix Janda <felix.janda at posteo.de>
parents: 991
diff changeset
562 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
563 if (fd != -1) break;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
564
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
565 // If it already existed, read it. Loop for race condition.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
566 fd = open(pidfile, O_RDONLY);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
567 if (fd == -1) continue;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
568
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
569 // Is the old program still there?
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
570 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
571 close(fd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
572 pid = atoi(spid);
1053
501fd74c028e Fix for xpidfile spotted by Felix Janda.
Rob Landley <rob@landley.net>
parents: 1043
diff changeset
573 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
951
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
574
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
575 // An else with more sanity checking might be nice here.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
576 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
577
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
578 if (i == 3) error_exit("xpidfile %s", name);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
579
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
580 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
581 close(fd);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
582 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
583
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
584 // Copy the rest of in to out and close both files.
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
585
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
586 void xsendfile(int in, int out)
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
587 {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
588 long len;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
589 char buf[4096];
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
590
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
591 if (in<0) return;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
592 for (;;) {
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
593 len = xread(in, buf, 4096);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
594 if (len<1) break;
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
595 xwrite(out, buf, len);
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
596 }
62d59b8aea34 Split lib/xwrap.c from lib/lib.c
Rob Landley <rob@landley.net>
parents:
diff changeset
597 }
952
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
598
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
599 // 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
600 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
601 {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
602 double d;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
603 long l;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
604
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
605 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
606 else l = strtoul(arg, &arg, 10);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
607
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
608 // Parse suffix
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
609 if (*arg) {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
610 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
611
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
612 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
613 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
614 else l *= ismhd[i];
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
615 }
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
616
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
617 if (CFG_TOYBOX_FLOAT) {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
618 l = (long)d;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
619 if (fraction) *fraction = units*(d-l);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
620 } else if (fraction) *fraction = 0;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
621
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
622 return l;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents: 951
diff changeset
623 }
1235
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
624
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
625 // Compile a regular expression into a regex_t
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
626 void xregcomp(regex_t *preg, char *regex, int cflags)
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
627 {
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
628 int rc = regcomp(preg, regex, cflags);
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
629
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
630 if (rc) {
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
631 regerror(rc, preg, libbuf, sizeof(libbuf));
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
632 error_exit("xregcomp: %s", libbuf);
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
633 }
63db77909fc8 Group headers by standard (POSIX or LSB) or function (internationalization, networking). Move headers standards ignore (but which have been there >15 years) to lib/portability.h. Fold xregcomp into lib since it's posix.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
634 }