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