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