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