Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * Copyright (C) 2006 Rob Landley |
| 7 | * Copyright (C) 2006 Denys Vlasenko |
| 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 10 | */ |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 11 | /* We need to have separate xfuncs.c and xfuncs_printf.c because |
| 12 | * with current linkers, even with section garbage collection, |
| 13 | * if *.o module references any of XXXprintf functions, you pull in |
| 14 | * entire printf machinery. Even if you do not use the function |
| 15 | * which uses XXXprintf. |
| 16 | * |
| 17 | * xfuncs.c contains functions (not necessarily xfuncs) |
| 18 | * which do not pull in printf, directly or indirectly. |
| 19 | * xfunc_printf.c contains those which do. |
| 20 | */ |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
| 22 | |
| 23 | |
| 24 | /* All the functions starting with "x" call bb_error_msg_and_die() if they |
| 25 | * fail, so callers never need to check for errors. If it returned, it |
| 26 | * succeeded. */ |
| 27 | |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 28 | void FAST_FUNC bb_die_memory_exhausted(void) |
| 29 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 30 | bb_simple_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 33 | #ifndef DMALLOC |
| 34 | /* dmalloc provides variants of these that do abort() on failure. |
| 35 | * Since dmalloc's prototypes overwrite the impls here as they are |
| 36 | * included after these prototypes in libbb.h, all is well. |
| 37 | */ |
| 38 | // Warn if we can't allocate size bytes of memory. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 39 | void* FAST_FUNC malloc_or_warn(size_t size) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 40 | { |
| 41 | void *ptr = malloc(size); |
| 42 | if (ptr == NULL && size != 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 43 | bb_simple_error_msg(bb_msg_memory_exhausted); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 44 | return ptr; |
| 45 | } |
| 46 | |
| 47 | // Die if we can't allocate size bytes of memory. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 48 | void* FAST_FUNC xmalloc(size_t size) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 49 | { |
| 50 | void *ptr = malloc(size); |
| 51 | if (ptr == NULL && size != 0) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 52 | bb_die_memory_exhausted(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 53 | return ptr; |
| 54 | } |
| 55 | |
| 56 | // Die if we can't resize previously allocated memory. (This returns a pointer |
| 57 | // to the new memory, which may or may not be the same as the old memory. |
| 58 | // It'll copy the contents to a new chunk and free the old one if necessary.) |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 59 | void* FAST_FUNC xrealloc(void *ptr, size_t size) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 60 | { |
| 61 | ptr = realloc(ptr, size); |
| 62 | if (ptr == NULL && size != 0) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 63 | bb_die_memory_exhausted(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 64 | return ptr; |
| 65 | } |
| 66 | #endif /* DMALLOC */ |
| 67 | |
| 68 | // Die if we can't allocate and zero size bytes of memory. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 69 | void* FAST_FUNC xzalloc(size_t size) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 70 | { |
| 71 | void *ptr = xmalloc(size); |
| 72 | memset(ptr, 0, size); |
| 73 | return ptr; |
| 74 | } |
| 75 | |
| 76 | // Die if we can't copy a string to freshly allocated memory. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 77 | char* FAST_FUNC xstrdup(const char *s) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 78 | { |
| 79 | char *t; |
| 80 | |
| 81 | if (s == NULL) |
| 82 | return NULL; |
| 83 | |
| 84 | t = strdup(s); |
| 85 | |
| 86 | if (t == NULL) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 87 | bb_die_memory_exhausted(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 88 | |
| 89 | return t; |
| 90 | } |
| 91 | |
| 92 | // Die if we can't allocate n+1 bytes (space for the null terminator) and copy |
| 93 | // the (possibly truncated to length n) string into it. |
Denys Vlasenko | 6062c0d | 2022-01-05 23:02:13 +0100 | [diff] [blame] | 94 | char* FAST_FUNC xstrndup(const char *s, size_t n) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 95 | { |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 96 | char *t; |
| 97 | |
Martin Lewis | 9b4a9d9 | 2020-03-08 13:35:20 -0500 | [diff] [blame] | 98 | t = strndup(s, n); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 99 | |
Martin Lewis | 9b4a9d9 | 2020-03-08 13:35:20 -0500 | [diff] [blame] | 100 | if (t == NULL) |
| 101 | bb_die_memory_exhausted(); |
| 102 | |
| 103 | return t; |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Denys Vlasenko | 6062c0d | 2022-01-05 23:02:13 +0100 | [diff] [blame] | 106 | void* FAST_FUNC xmemdup(const void *s, size_t n) |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 107 | { |
| 108 | return memcpy(xmalloc(n), s, n); |
| 109 | } |
| 110 | |
Denys Vlasenko | fd3c512 | 2020-12-14 18:25:28 +0100 | [diff] [blame] | 111 | void* FAST_FUNC mmap_read(int fd, size_t size) |
| 112 | { |
| 113 | return mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 114 | } |
| 115 | |
| 116 | void* FAST_FUNC mmap_anon(size_t size) |
| 117 | { |
| 118 | return mmap(NULL, size, |
| 119 | PROT_READ | PROT_WRITE, |
| 120 | MAP_PRIVATE | MAP_ANONYMOUS, |
| 121 | /* ignored: */ -1, 0); |
| 122 | } |
| 123 | |
| 124 | void* FAST_FUNC xmmap_anon(size_t size) |
| 125 | { |
| 126 | void *p = mmap_anon(size); |
| 127 | if (p == MAP_FAILED) |
| 128 | bb_die_memory_exhausted(); |
| 129 | return p; |
| 130 | } |
| 131 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 132 | // Die if we can't open a file and return a FILE* to it. |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 133 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 134 | FILE* FAST_FUNC xfopen(const char *path, const char *mode) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 135 | { |
| 136 | FILE *fp = fopen(path, mode); |
| 137 | if (fp == NULL) |
| 138 | bb_perror_msg_and_die("can't open '%s'", path); |
| 139 | return fp; |
| 140 | } |
| 141 | |
| 142 | // Die if we can't open a file and return a fd. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 143 | int FAST_FUNC xopen3(const char *pathname, int flags, int mode) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 144 | { |
| 145 | int ret; |
| 146 | |
| 147 | ret = open(pathname, flags, mode); |
| 148 | if (ret < 0) { |
| 149 | bb_perror_msg_and_die("can't open '%s'", pathname); |
| 150 | } |
| 151 | return ret; |
| 152 | } |
| 153 | |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 154 | // Die if we can't open a file and return a fd. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 155 | int FAST_FUNC xopen(const char *pathname, int flags) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 156 | { |
| 157 | return xopen3(pathname, flags, 0666); |
| 158 | } |
| 159 | |
| 160 | // Warn if we can't open a file and return a fd. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 161 | int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | ret = open(pathname, flags, mode); |
| 166 | if (ret < 0) { |
| 167 | bb_perror_msg("can't open '%s'", pathname); |
| 168 | } |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | // Warn if we can't open a file and return a fd. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 173 | int FAST_FUNC open_or_warn(const char *pathname, int flags) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 174 | { |
| 175 | return open3_or_warn(pathname, flags, 0666); |
| 176 | } |
| 177 | |
Ryan Mallon | 5906a5c | 2013-10-08 14:52:49 +0200 | [diff] [blame] | 178 | /* Die if we can't open an existing file readonly with O_NONBLOCK |
| 179 | * and return the fd. |
| 180 | * Note that for ioctl O_RDONLY is sufficient. |
| 181 | */ |
| 182 | int FAST_FUNC xopen_nonblocking(const char *pathname) |
| 183 | { |
| 184 | return xopen(pathname, O_RDONLY | O_NONBLOCK); |
| 185 | } |
| 186 | |
| 187 | int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g) |
| 188 | { |
| 189 | int fd; |
| 190 | uid_t old_euid = geteuid(); |
| 191 | gid_t old_egid = getegid(); |
| 192 | |
| 193 | xsetegid(g); |
| 194 | xseteuid(u); |
| 195 | |
| 196 | fd = xopen(pathname, flags); |
| 197 | |
| 198 | xseteuid(old_euid); |
| 199 | xsetegid(old_egid); |
| 200 | |
| 201 | return fd; |
| 202 | } |
| 203 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 204 | void FAST_FUNC xunlink(const char *pathname) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 205 | { |
| 206 | if (unlink(pathname)) |
| 207 | bb_perror_msg_and_die("can't remove file '%s'", pathname); |
| 208 | } |
| 209 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 210 | void FAST_FUNC xrename(const char *oldpath, const char *newpath) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 211 | { |
| 212 | if (rename(oldpath, newpath)) |
| 213 | bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath); |
| 214 | } |
| 215 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 216 | int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 217 | { |
| 218 | int n = rename(oldpath, newpath); |
| 219 | if (n) |
| 220 | bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath); |
| 221 | return n; |
| 222 | } |
| 223 | |
Denys Vlasenko | fe2d806 | 2021-04-14 17:52:18 +0200 | [diff] [blame] | 224 | void FAST_FUNC xpipe(int *filedes) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 225 | { |
| 226 | if (pipe(filedes)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 227 | bb_simple_perror_msg_and_die("can't create pipe"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 230 | void FAST_FUNC xdup2(int from, int to) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 231 | { |
| 232 | if (dup2(from, to) != to) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 233 | bb_simple_perror_msg_and_die("can't duplicate file descriptor"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 234 | // " %d to %d", from, to); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // "Renumber" opened fd |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 238 | void FAST_FUNC xmove_fd(int from, int to) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 239 | { |
| 240 | if (from == to) |
| 241 | return; |
| 242 | xdup2(from, to); |
| 243 | close(from); |
| 244 | } |
| 245 | |
| 246 | // Die with an error message if we can't write the entire buffer. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 247 | void FAST_FUNC xwrite(int fd, const void *buf, size_t count) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 248 | { |
| 249 | if (count) { |
| 250 | ssize_t size = full_write(fd, buf, count); |
Denys Vlasenko | 9fd61be | 2016-09-05 15:20:10 +0200 | [diff] [blame] | 251 | if ((size_t)size != count) { |
| 252 | /* |
| 253 | * Two cases: write error immediately; |
| 254 | * or some writes succeeded, then we hit an error. |
| 255 | * In either case, errno is set. |
| 256 | */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 257 | bb_simple_perror_msg_and_die( |
Denys Vlasenko | 9fd61be | 2016-09-05 15:20:10 +0200 | [diff] [blame] | 258 | size >= 0 ? "short write" : "write error" |
| 259 | ); |
| 260 | } |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 263 | void FAST_FUNC xwrite_str(int fd, const char *str) |
| 264 | { |
| 265 | xwrite(fd, str, strlen(str)); |
| 266 | } |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 267 | |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 268 | void FAST_FUNC xclose(int fd) |
| 269 | { |
| 270 | if (close(fd)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 271 | bb_simple_perror_msg_and_die("close failed"); |
Denys Vlasenko | dcd27ab | 2009-10-05 03:03:07 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 274 | // Die with an error message if we can't lseek to the right spot. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 275 | off_t FAST_FUNC xlseek(int fd, off_t offset, int whence) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 276 | { |
| 277 | off_t off = lseek(fd, offset, whence); |
| 278 | if (off == (off_t)-1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 279 | bb_perror_msg_and_die("lseek(%"OFF_FMT"u, %d)", offset, whence); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 280 | } |
| 281 | return off; |
| 282 | } |
| 283 | |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 284 | int FAST_FUNC xmkstemp(char *template) |
| 285 | { |
| 286 | int fd = mkstemp(template); |
| 287 | if (fd < 0) |
| 288 | bb_perror_msg_and_die("can't create temp file '%s'", template); |
| 289 | return fd; |
| 290 | } |
| 291 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 292 | // Die with supplied filename if this FILE* has ferror set. |
| 293 | void FAST_FUNC die_if_ferror(FILE *fp, const char *fn) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 294 | { |
| 295 | if (ferror(fp)) { |
| 296 | /* ferror doesn't set useful errno */ |
| 297 | bb_error_msg_and_die("%s: I/O error", fn); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Die with an error message if stdout has ferror set. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 302 | void FAST_FUNC die_if_ferror_stdout(void) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 303 | { |
| 304 | die_if_ferror(stdout, bb_msg_standard_output); |
| 305 | } |
| 306 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 307 | int FAST_FUNC fflush_all(void) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 308 | { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 309 | return fflush(NULL); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 313 | int FAST_FUNC bb_putchar(int ch) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 314 | { |
Denys Vlasenko | c066472 | 2010-01-02 18:49:22 +0100 | [diff] [blame] | 315 | return putchar(ch); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 318 | int FAST_FUNC fputs_stdout(const char *s) |
| 319 | { |
| 320 | return fputs(s, stdout); |
| 321 | } |
| 322 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 323 | /* Die with an error message if we can't copy an entire FILE* to stdout, |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 324 | * then close that file. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 325 | void FAST_FUNC xprint_and_close_file(FILE *file) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 326 | { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 327 | fflush_all(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 328 | // copyfd outputs error messages for us. |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 329 | if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 330 | xfunc_die(); |
| 331 | |
| 332 | fclose(file); |
| 333 | } |
| 334 | |
| 335 | // Die with an error message if we can't malloc() enough space and do an |
| 336 | // sprintf() into that space. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 337 | char* FAST_FUNC xasprintf(const char *format, ...) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 338 | { |
| 339 | va_list p; |
| 340 | int r; |
| 341 | char *string_ptr; |
| 342 | |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 343 | va_start(p, format); |
| 344 | r = vasprintf(&string_ptr, format, p); |
| 345 | va_end(p); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 346 | |
| 347 | if (r < 0) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 348 | bb_die_memory_exhausted(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 349 | return string_ptr; |
| 350 | } |
| 351 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 352 | void FAST_FUNC xsetenv(const char *key, const char *value) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 353 | { |
| 354 | if (setenv(key, value, 1)) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 355 | bb_die_memory_exhausted(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 358 | /* Handles "VAR=VAL" strings, even those which are part of environ |
| 359 | * _right now_ |
| 360 | */ |
| 361 | void FAST_FUNC bb_unsetenv(const char *var) |
| 362 | { |
Denys Vlasenko | ef0366e | 2017-07-22 02:15:17 +0200 | [diff] [blame] | 363 | char onstack[128 - 16]; /* smaller stack setup code on x86 */ |
| 364 | char *tp; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 365 | |
Denys Vlasenko | ef0366e | 2017-07-22 02:15:17 +0200 | [diff] [blame] | 366 | tp = strchr(var, '='); |
| 367 | if (tp) { |
| 368 | /* In case var was putenv'ed, we can't replace '=' |
| 369 | * with NUL and unsetenv(var) - it won't work, |
| 370 | * env is modified by the replacement, unsetenv |
| 371 | * sees "VAR" instead of "VAR=VAL" and does not remove it! |
| 372 | * Horror :( |
| 373 | */ |
| 374 | unsigned sz = tp - var; |
| 375 | if (sz < sizeof(onstack)) { |
| 376 | ((char*)mempcpy(onstack, var, sz))[0] = '\0'; |
| 377 | tp = NULL; |
| 378 | var = onstack; |
| 379 | } else { |
| 380 | /* unlikely: very long var name */ |
| 381 | var = tp = xstrndup(var, sz); |
| 382 | } |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 383 | } |
Denys Vlasenko | ef0366e | 2017-07-22 02:15:17 +0200 | [diff] [blame] | 384 | unsetenv(var); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 385 | free(tp); |
| 386 | } |
| 387 | |
Denys Vlasenko | dd8adde | 2010-06-24 05:00:50 +0200 | [diff] [blame] | 388 | void FAST_FUNC bb_unsetenv_and_free(char *var) |
| 389 | { |
| 390 | bb_unsetenv(var); |
| 391 | free(var); |
| 392 | } |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 393 | |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 394 | // Die with an error message if we can't set gid. (Because resource limits may |
| 395 | // limit this user to a given number of processes, and if that fills up the |
| 396 | // setgid() will fail and we'll _still_be_root_, which is bad.) |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 397 | void FAST_FUNC xsetgid(gid_t gid) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 398 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 399 | if (setgid(gid)) bb_simple_perror_msg_and_die("setgid"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Die with an error message if we can't set uid. (See xsetgid() for why.) |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 403 | void FAST_FUNC xsetuid(uid_t uid) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 404 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 405 | if (setuid(uid)) bb_simple_perror_msg_and_die("setuid"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Ryan Mallon | 5906a5c | 2013-10-08 14:52:49 +0200 | [diff] [blame] | 408 | void FAST_FUNC xsetegid(gid_t egid) |
| 409 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 410 | if (setegid(egid)) bb_simple_perror_msg_and_die("setegid"); |
Ryan Mallon | 5906a5c | 2013-10-08 14:52:49 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void FAST_FUNC xseteuid(uid_t euid) |
| 414 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 415 | if (seteuid(euid)) bb_simple_perror_msg_and_die("seteuid"); |
Ryan Mallon | 5906a5c | 2013-10-08 14:52:49 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Denys Vlasenko | c2788f8 | 2022-01-13 12:56:10 +0100 | [diff] [blame] | 418 | int FAST_FUNC chdir_or_warn(const char *path) |
| 419 | { |
| 420 | int r = chdir(path); |
| 421 | if (r != 0) |
| 422 | bb_perror_msg("can't change directory to '%s'", path); |
| 423 | return r; |
| 424 | } |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 425 | // Die if we can't chdir to a new path. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 426 | void FAST_FUNC xchdir(const char *path) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 427 | { |
Denys Vlasenko | c2788f8 | 2022-01-13 12:56:10 +0100 | [diff] [blame] | 428 | if (chdir_or_warn(path) != 0) |
| 429 | xfunc_die(); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Denys Vlasenko | c4199f2 | 2016-04-01 22:12:44 +0200 | [diff] [blame] | 432 | void FAST_FUNC xfchdir(int fd) |
| 433 | { |
| 434 | if (fchdir(fd)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 435 | bb_simple_perror_msg_and_die("fchdir"); |
Denys Vlasenko | c4199f2 | 2016-04-01 22:12:44 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 438 | void FAST_FUNC xchroot(const char *path) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 439 | { |
| 440 | if (chroot(path)) |
Pascal Bellard | 70fc8c1 | 2012-06-12 13:21:02 +0200 | [diff] [blame] | 441 | bb_perror_msg_and_die("can't change root directory to '%s'", path); |
Denys Vlasenko | 0687a5b | 2012-03-08 00:28:24 +0100 | [diff] [blame] | 442 | xchdir("/"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Print a warning message if opendir() fails, but don't die. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 446 | DIR* FAST_FUNC warn_opendir(const char *path) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 447 | { |
| 448 | DIR *dp; |
| 449 | |
| 450 | dp = opendir(path); |
| 451 | if (!dp) |
| 452 | bb_perror_msg("can't open '%s'", path); |
| 453 | return dp; |
| 454 | } |
| 455 | |
| 456 | // Die with an error message if opendir() fails. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 457 | DIR* FAST_FUNC xopendir(const char *path) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 458 | { |
| 459 | DIR *dp; |
| 460 | |
| 461 | dp = opendir(path); |
| 462 | if (!dp) |
| 463 | bb_perror_msg_and_die("can't open '%s'", path); |
| 464 | return dp; |
| 465 | } |
| 466 | |
| 467 | // Die with an error message if we can't open a new socket. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 468 | int FAST_FUNC xsocket(int domain, int type, int protocol) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 469 | { |
| 470 | int r = socket(domain, type, protocol); |
| 471 | |
| 472 | if (r < 0) { |
| 473 | /* Hijack vaguely related config option */ |
| 474 | #if ENABLE_VERBOSE_RESOLUTION_ERRORS |
| 475 | const char *s = "INET"; |
Jeremie Koenig | 2988511 | 2010-05-27 15:39:24 +0200 | [diff] [blame] | 476 | # ifdef AF_PACKET |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 477 | if (domain == AF_PACKET) s = "PACKET"; |
Jeremie Koenig | 2988511 | 2010-05-27 15:39:24 +0200 | [diff] [blame] | 478 | # endif |
| 479 | # ifdef AF_NETLINK |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 480 | if (domain == AF_NETLINK) s = "NETLINK"; |
Jeremie Koenig | 2988511 | 2010-05-27 15:39:24 +0200 | [diff] [blame] | 481 | # endif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 482 | IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";) |
Denys Vlasenko | b2e5fc3 | 2009-11-25 14:52:47 +0100 | [diff] [blame] | 483 | bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 484 | #else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 485 | bb_simple_perror_msg_and_die("socket"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 486 | #endif |
| 487 | } |
| 488 | |
| 489 | return r; |
| 490 | } |
| 491 | |
| 492 | // Die with an error message if we can't bind a socket to an address. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 493 | void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 494 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 495 | if (bind(sockfd, my_addr, addrlen)) bb_simple_perror_msg_and_die("bind"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Die with an error message if we can't listen for connections on a socket. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 499 | void FAST_FUNC xlisten(int s, int backlog) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 500 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 501 | if (listen(s, backlog)) bb_simple_perror_msg_and_die("listen"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Die with an error message if sendto failed. |
| 505 | * Return bytes sent otherwise */ |
Denis Vlasenko | fa65a3d | 2009-01-24 20:11:36 +0000 | [diff] [blame] | 506 | ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to, |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 507 | socklen_t tolen) |
| 508 | { |
| 509 | ssize_t ret = sendto(s, buf, len, 0, to, tolen); |
| 510 | if (ret < 0) { |
| 511 | if (ENABLE_FEATURE_CLEAN_UP) |
| 512 | close(s); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 513 | bb_simple_perror_msg_and_die("sendto"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 514 | } |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | // xstat() - a stat() which dies on failure with meaningful error message |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 519 | void FAST_FUNC xstat(const char *name, struct stat *stat_buf) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 520 | { |
| 521 | if (stat(name, stat_buf)) |
| 522 | bb_perror_msg_and_die("can't stat '%s'", name); |
| 523 | } |
| 524 | |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 525 | void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg) |
| 526 | { |
| 527 | /* errmsg is usually a file name, but not always: |
| 528 | * xfstat may be called in a spot where file name is no longer |
| 529 | * available, and caller may give e.g. "can't stat input file" string. |
| 530 | */ |
| 531 | if (fstat(fd, stat_buf)) |
| 532 | bb_simple_perror_msg_and_die(errmsg); |
| 533 | } |
| 534 | |
Denys Vlasenko | bb15969 | 2020-12-16 23:00:51 +0100 | [diff] [blame] | 535 | #if ENABLE_SELINUX |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 536 | // selinux_or_die() - die if SELinux is disabled. |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 537 | void FAST_FUNC selinux_or_die(void) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 538 | { |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 539 | int rc = is_selinux_enabled(); |
| 540 | if (rc == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 541 | bb_simple_error_msg_and_die("SELinux is disabled"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 542 | } else if (rc < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 543 | bb_simple_error_msg_and_die("is_selinux_enabled() failed"); |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 544 | } |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 545 | } |
Denys Vlasenko | bb15969 | 2020-12-16 23:00:51 +0100 | [diff] [blame] | 546 | #else |
| 547 | /* not defined, other code must have no calls to it */ |
| 548 | #endif |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 549 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 550 | int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 551 | { |
| 552 | int ret; |
| 553 | va_list p; |
| 554 | |
| 555 | ret = ioctl(fd, request, argp); |
| 556 | if (ret < 0) { |
| 557 | va_start(p, fmt); |
| 558 | bb_verror_msg(fmt, p, strerror(errno)); |
| 559 | /* xfunc_die can actually longjmp, so be nice */ |
| 560 | va_end(p); |
| 561 | xfunc_die(); |
| 562 | } |
| 563 | return ret; |
| 564 | } |
| 565 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 566 | int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 567 | { |
| 568 | va_list p; |
| 569 | int ret = ioctl(fd, request, argp); |
| 570 | |
| 571 | if (ret < 0) { |
| 572 | va_start(p, fmt); |
| 573 | bb_verror_msg(fmt, p, strerror(errno)); |
| 574 | va_end(p); |
| 575 | } |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | #if ENABLE_IOCTL_HEX2STR_ERROR |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 580 | int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 581 | { |
| 582 | int ret; |
| 583 | |
| 584 | ret = ioctl(fd, request, argp); |
| 585 | if (ret < 0) |
| 586 | bb_simple_perror_msg(ioctl_name); |
| 587 | return ret; |
| 588 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 589 | int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 590 | { |
| 591 | int ret; |
| 592 | |
| 593 | ret = ioctl(fd, request, argp); |
| 594 | if (ret < 0) |
| 595 | bb_simple_perror_msg_and_die(ioctl_name); |
| 596 | return ret; |
| 597 | } |
| 598 | #else |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 599 | int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 600 | { |
| 601 | int ret; |
| 602 | |
| 603 | ret = ioctl(fd, request, argp); |
| 604 | if (ret < 0) |
| 605 | bb_perror_msg("ioctl %#x failed", request); |
| 606 | return ret; |
| 607 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 608 | int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp) |
Denis Vlasenko | 858ebf1 | 2008-04-09 00:33:53 +0000 | [diff] [blame] | 609 | { |
| 610 | int ret; |
| 611 | |
| 612 | ret = ioctl(fd, request, argp); |
| 613 | if (ret < 0) |
| 614 | bb_perror_msg_and_die("ioctl %#x failed", request); |
| 615 | return ret; |
| 616 | } |
| 617 | #endif |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 618 | |
| 619 | char* FAST_FUNC xmalloc_ttyname(int fd) |
| 620 | { |
Denys Vlasenko | 543efd7 | 2013-08-06 00:41:06 +0200 | [diff] [blame] | 621 | char buf[128]; |
| 622 | int r = ttyname_r(fd, buf, sizeof(buf) - 1); |
| 623 | if (r) |
| 624 | return NULL; |
| 625 | return xstrdup(buf); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void FAST_FUNC generate_uuid(uint8_t *buf) |
| 629 | { |
| 630 | /* http://www.ietf.org/rfc/rfc4122.txt |
| 631 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 632 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 633 | * | time_low | |
| 634 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 635 | * | time_mid | time_hi_and_version | |
| 636 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 637 | * |clk_seq_and_variant | node (0-1) | |
| 638 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 639 | * | node (2-5) | |
| 640 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 641 | * IOW, uuid has this layout: |
| 642 | * uint32_t time_low (big endian) |
| 643 | * uint16_t time_mid (big endian) |
| 644 | * uint16_t time_hi_and_version (big endian) |
| 645 | * version is a 4-bit field: |
| 646 | * 1 Time-based |
| 647 | * 2 DCE Security, with embedded POSIX UIDs |
| 648 | * 3 Name-based (MD5) |
| 649 | * 4 Randomly generated |
| 650 | * 5 Name-based (SHA-1) |
| 651 | * uint16_t clk_seq_and_variant (big endian) |
| 652 | * variant is a 3-bit field: |
| 653 | * 0xx Reserved, NCS backward compatibility |
| 654 | * 10x The variant specified in rfc4122 |
| 655 | * 110 Reserved, Microsoft backward compatibility |
| 656 | * 111 Reserved for future definition |
| 657 | * uint8_t node[6] |
| 658 | * |
| 659 | * For version 4, these bits are set/cleared: |
| 660 | * time_hi_and_version & 0x0fff | 0x4000 |
| 661 | * clk_seq_and_variant & 0x3fff | 0x8000 |
| 662 | */ |
| 663 | pid_t pid; |
| 664 | int i; |
| 665 | |
Denys Vlasenko | eb77305 | 2020-11-29 14:24:14 +0100 | [diff] [blame] | 666 | open_read_close("/dev/urandom", buf, 16); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 667 | /* Paranoia. /dev/urandom may be missing. |
| 668 | * rand() is guaranteed to generate at least [0, 2^15) range, |
Denys Vlasenko | eb77305 | 2020-11-29 14:24:14 +0100 | [diff] [blame] | 669 | * but lowest bits in some libc are not so "random". |
| 670 | */ |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 671 | srand(monotonic_us()); /* pulls in printf */ |
| 672 | pid = getpid(); |
| 673 | while (1) { |
| 674 | for (i = 0; i < 16; i++) |
| 675 | buf[i] ^= rand() >> 5; |
| 676 | if (pid == 0) |
| 677 | break; |
| 678 | srand(pid); |
| 679 | pid = 0; |
| 680 | } |
| 681 | |
| 682 | /* version = 4 */ |
| 683 | buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; |
| 684 | /* variant = 10x */ |
| 685 | buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; |
| 686 | } |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 687 | |
| 688 | #if BB_MMU |
| 689 | pid_t FAST_FUNC xfork(void) |
| 690 | { |
| 691 | pid_t pid; |
| 692 | pid = fork(); |
| 693 | if (pid < 0) /* wtf? */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 694 | bb_simple_perror_msg_and_die("vfork"+1); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 695 | return pid; |
| 696 | } |
| 697 | #endif |
Denys Vlasenko | 8220399 | 2016-04-02 18:06:24 +0200 | [diff] [blame] | 698 | |
| 699 | void FAST_FUNC xvfork_parent_waits_and_exits(void) |
| 700 | { |
| 701 | pid_t pid; |
| 702 | |
| 703 | fflush_all(); |
| 704 | pid = xvfork(); |
| 705 | if (pid > 0) { |
| 706 | /* Parent */ |
| 707 | int exit_status = wait_for_exitstatus(pid); |
| 708 | if (WIFSIGNALED(exit_status)) |
| 709 | kill_myself_with_sig(WTERMSIG(exit_status)); |
| 710 | _exit(WEXITSTATUS(exit_status)); |
| 711 | } |
| 712 | /* Child continues */ |
| 713 | } |
Denys Vlasenko | eb0c2e2 | 2020-12-16 21:36:36 +0100 | [diff] [blame] | 714 | |
| 715 | // Useful when we do know that pid is valid, and we just want to wait |
| 716 | // for it to exit. Not existing pid is fatal. waitpid() status is not returned. |
| 717 | int FAST_FUNC wait_for_exitstatus(pid_t pid) |
| 718 | { |
| 719 | int exit_status, n; |
| 720 | |
| 721 | n = safe_waitpid(pid, &exit_status, 0); |
| 722 | if (n < 0) |
| 723 | bb_simple_perror_msg_and_die("waitpid"); |
| 724 | return exit_status; |
| 725 | } |
| 726 | |
| 727 | void FAST_FUNC xsettimeofday(const struct timeval *tv) |
| 728 | { |
| 729 | if (settimeofday(tv, NULL)) |
| 730 | bb_simple_perror_msg_and_die("settimeofday"); |
| 731 | } |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 732 | |
| 733 | void FAST_FUNC xgettimeofday(struct timeval *tv) |
| 734 | { |
| 735 | #if 0 |
| 736 | if (gettimeofday(tv, NULL)) |
| 737 | bb_simple_perror_msg_and_die("gettimeofday"); |
| 738 | #else |
| 739 | /* Never fails on Linux */ |
| 740 | gettimeofday(tv, NULL); |
| 741 | #endif |
| 742 | } |