blob: 2bc01ad10c6954a3077c99a33244ed42d300f1a1 [file] [log] [blame]
Denis Vlasenko858ebf12008-04-09 00:33:53 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko858ebf12008-04-09 00:33:53 +000010 */
Denis Vlasenko858ebf12008-04-09 00:33:53 +000011/* 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 Vlasenko858ebf12008-04-09 00:33:53 +000021#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
28#ifndef DMALLOC
29/* dmalloc provides variants of these that do abort() on failure.
30 * Since dmalloc's prototypes overwrite the impls here as they are
31 * included after these prototypes in libbb.h, all is well.
32 */
33// Warn if we can't allocate size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000034void* FAST_FUNC malloc_or_warn(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000035{
36 void *ptr = malloc(size);
37 if (ptr == NULL && size != 0)
38 bb_error_msg(bb_msg_memory_exhausted);
39 return ptr;
40}
41
42// Die if we can't allocate size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000043void* FAST_FUNC xmalloc(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000044{
45 void *ptr = malloc(size);
46 if (ptr == NULL && size != 0)
47 bb_error_msg_and_die(bb_msg_memory_exhausted);
48 return ptr;
49}
50
51// Die if we can't resize previously allocated memory. (This returns a pointer
52// to the new memory, which may or may not be the same as the old memory.
53// It'll copy the contents to a new chunk and free the old one if necessary.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000054void* FAST_FUNC xrealloc(void *ptr, size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000055{
56 ptr = realloc(ptr, size);
57 if (ptr == NULL && size != 0)
58 bb_error_msg_and_die(bb_msg_memory_exhausted);
59 return ptr;
60}
61#endif /* DMALLOC */
62
63// Die if we can't allocate and zero size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000064void* FAST_FUNC xzalloc(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000065{
66 void *ptr = xmalloc(size);
67 memset(ptr, 0, size);
68 return ptr;
69}
70
71// Die if we can't copy a string to freshly allocated memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000072char* FAST_FUNC xstrdup(const char *s)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000073{
74 char *t;
75
76 if (s == NULL)
77 return NULL;
78
79 t = strdup(s);
80
81 if (t == NULL)
82 bb_error_msg_and_die(bb_msg_memory_exhausted);
83
84 return t;
85}
86
87// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
88// the (possibly truncated to length n) string into it.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000089char* FAST_FUNC xstrndup(const char *s, int n)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000090{
91 int m;
92 char *t;
93
94 if (ENABLE_DEBUG && s == NULL)
95 bb_error_msg_and_die("xstrndup bug");
96
97 /* We can just xmalloc(n+1) and strncpy into it, */
98 /* but think about xstrndup("abc", 10000) wastage! */
99 m = n;
100 t = (char*) s;
101 while (m) {
102 if (!*t) break;
103 m--;
104 t++;
105 }
106 n -= m;
107 t = xmalloc(n + 1);
108 t[n] = '\0';
109
110 return memcpy(t, s, n);
111}
112
Ron Yorstond840c5d2015-07-19 23:05:20 +0200113void* FAST_FUNC xmemdup(const void *s, int n)
114{
115 return memcpy(xmalloc(n), s, n);
116}
117
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000118// Die if we can't open a file and return a FILE* to it.
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000119// Notice we haven't got xfread(), This is for use with fscanf() and friends.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000120FILE* FAST_FUNC xfopen(const char *path, const char *mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000121{
122 FILE *fp = fopen(path, mode);
123 if (fp == NULL)
124 bb_perror_msg_and_die("can't open '%s'", path);
125 return fp;
126}
127
128// Die if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000129int FAST_FUNC xopen3(const char *pathname, int flags, int mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000130{
131 int ret;
132
133 ret = open(pathname, flags, mode);
134 if (ret < 0) {
135 bb_perror_msg_and_die("can't open '%s'", pathname);
136 }
137 return ret;
138}
139
Denys Vlasenkoc05387d2010-10-18 02:38:27 +0200140// Die if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000141int FAST_FUNC xopen(const char *pathname, int flags)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000142{
143 return xopen3(pathname, flags, 0666);
144}
145
146// Warn if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000147int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000148{
149 int ret;
150
151 ret = open(pathname, flags, mode);
152 if (ret < 0) {
153 bb_perror_msg("can't open '%s'", pathname);
154 }
155 return ret;
156}
157
158// Warn if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000159int FAST_FUNC open_or_warn(const char *pathname, int flags)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000160{
161 return open3_or_warn(pathname, flags, 0666);
162}
163
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200164/* Die if we can't open an existing file readonly with O_NONBLOCK
165 * and return the fd.
166 * Note that for ioctl O_RDONLY is sufficient.
167 */
168int FAST_FUNC xopen_nonblocking(const char *pathname)
169{
170 return xopen(pathname, O_RDONLY | O_NONBLOCK);
171}
172
173int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g)
174{
175 int fd;
176 uid_t old_euid = geteuid();
177 gid_t old_egid = getegid();
178
179 xsetegid(g);
180 xseteuid(u);
181
182 fd = xopen(pathname, flags);
183
184 xseteuid(old_euid);
185 xsetegid(old_egid);
186
187 return fd;
188}
189
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000190void FAST_FUNC xunlink(const char *pathname)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000191{
192 if (unlink(pathname))
193 bb_perror_msg_and_die("can't remove file '%s'", pathname);
194}
195
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000196void FAST_FUNC xrename(const char *oldpath, const char *newpath)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000197{
198 if (rename(oldpath, newpath))
199 bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath);
200}
201
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000202int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000203{
204 int n = rename(oldpath, newpath);
205 if (n)
206 bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath);
207 return n;
208}
209
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000210void FAST_FUNC xpipe(int filedes[2])
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000211{
212 if (pipe(filedes))
213 bb_perror_msg_and_die("can't create pipe");
214}
215
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000216void FAST_FUNC xdup2(int from, int to)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000217{
218 if (dup2(from, to) != to)
219 bb_perror_msg_and_die("can't duplicate file descriptor");
220}
221
222// "Renumber" opened fd
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000223void FAST_FUNC xmove_fd(int from, int to)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000224{
225 if (from == to)
226 return;
227 xdup2(from, to);
228 close(from);
229}
230
231// Die with an error message if we can't write the entire buffer.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000232void FAST_FUNC xwrite(int fd, const void *buf, size_t count)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000233{
234 if (count) {
235 ssize_t size = full_write(fd, buf, count);
Denys Vlasenko9fd61be2016-09-05 15:20:10 +0200236 if ((size_t)size != count) {
237 /*
238 * Two cases: write error immediately;
239 * or some writes succeeded, then we hit an error.
240 * In either case, errno is set.
241 */
242 bb_perror_msg_and_die(
243 size >= 0 ? "short write" : "write error"
244 );
245 }
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000246 }
247}
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000248void FAST_FUNC xwrite_str(int fd, const char *str)
249{
250 xwrite(fd, str, strlen(str));
251}
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000252
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200253void FAST_FUNC xclose(int fd)
254{
255 if (close(fd))
256 bb_perror_msg_and_die("close failed");
257}
258
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000259// Die with an error message if we can't lseek to the right spot.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000260off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000261{
262 off_t off = lseek(fd, offset, whence);
263 if (off == (off_t)-1) {
264 if (whence == SEEK_SET)
265 bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
266 bb_perror_msg_and_die("lseek");
267 }
268 return off;
269}
270
Alexander Shishkin67227372010-10-22 13:27:16 +0200271int FAST_FUNC xmkstemp(char *template)
272{
273 int fd = mkstemp(template);
274 if (fd < 0)
275 bb_perror_msg_and_die("can't create temp file '%s'", template);
276 return fd;
277}
278
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000279// Die with supplied filename if this FILE* has ferror set.
280void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000281{
282 if (ferror(fp)) {
283 /* ferror doesn't set useful errno */
284 bb_error_msg_and_die("%s: I/O error", fn);
285 }
286}
287
288// Die with an error message if stdout has ferror set.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000289void FAST_FUNC die_if_ferror_stdout(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000290{
291 die_if_ferror(stdout, bb_msg_standard_output);
292}
293
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100294int FAST_FUNC fflush_all(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000295{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100296 return fflush(NULL);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000297}
298
299
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000300int FAST_FUNC bb_putchar(int ch)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000301{
Denys Vlasenkoc0664722010-01-02 18:49:22 +0100302 return putchar(ch);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000303}
304
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000305/* Die with an error message if we can't copy an entire FILE* to stdout,
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000306 * then close that file. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000307void FAST_FUNC xprint_and_close_file(FILE *file)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000308{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100309 fflush_all();
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000310 // copyfd outputs error messages for us.
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100311 if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000312 xfunc_die();
313
314 fclose(file);
315}
316
317// Die with an error message if we can't malloc() enough space and do an
318// sprintf() into that space.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000319char* FAST_FUNC xasprintf(const char *format, ...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000320{
321 va_list p;
322 int r;
323 char *string_ptr;
324
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000325 va_start(p, format);
326 r = vasprintf(&string_ptr, format, p);
327 va_end(p);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000328
329 if (r < 0)
330 bb_error_msg_and_die(bb_msg_memory_exhausted);
331 return string_ptr;
332}
333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000334void FAST_FUNC xsetenv(const char *key, const char *value)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000335{
336 if (setenv(key, value, 1))
337 bb_error_msg_and_die(bb_msg_memory_exhausted);
338}
339
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000340/* Handles "VAR=VAL" strings, even those which are part of environ
341 * _right now_
342 */
343void FAST_FUNC bb_unsetenv(const char *var)
344{
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200345 char onstack[128 - 16]; /* smaller stack setup code on x86 */
346 char *tp;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000347
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200348 tp = strchr(var, '=');
349 if (tp) {
350 /* In case var was putenv'ed, we can't replace '='
351 * with NUL and unsetenv(var) - it won't work,
352 * env is modified by the replacement, unsetenv
353 * sees "VAR" instead of "VAR=VAL" and does not remove it!
354 * Horror :(
355 */
356 unsigned sz = tp - var;
357 if (sz < sizeof(onstack)) {
358 ((char*)mempcpy(onstack, var, sz))[0] = '\0';
359 tp = NULL;
360 var = onstack;
361 } else {
362 /* unlikely: very long var name */
363 var = tp = xstrndup(var, sz);
364 }
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000365 }
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200366 unsetenv(var);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000367 free(tp);
368}
369
Denys Vlasenkodd8adde2010-06-24 05:00:50 +0200370void FAST_FUNC bb_unsetenv_and_free(char *var)
371{
372 bb_unsetenv(var);
373 free(var);
374}
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000375
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000376// Die with an error message if we can't set gid. (Because resource limits may
377// limit this user to a given number of processes, and if that fills up the
378// setgid() will fail and we'll _still_be_root_, which is bad.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000379void FAST_FUNC xsetgid(gid_t gid)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000380{
381 if (setgid(gid)) bb_perror_msg_and_die("setgid");
382}
383
384// Die with an error message if we can't set uid. (See xsetgid() for why.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000385void FAST_FUNC xsetuid(uid_t uid)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000386{
387 if (setuid(uid)) bb_perror_msg_and_die("setuid");
388}
389
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200390void FAST_FUNC xsetegid(gid_t egid)
391{
392 if (setegid(egid)) bb_perror_msg_and_die("setegid");
393}
394
395void FAST_FUNC xseteuid(uid_t euid)
396{
397 if (seteuid(euid)) bb_perror_msg_and_die("seteuid");
398}
399
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000400// Die if we can't chdir to a new path.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000401void FAST_FUNC xchdir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000402{
403 if (chdir(path))
Pascal Bellard70fc8c12012-06-12 13:21:02 +0200404 bb_perror_msg_and_die("can't change directory to '%s'", path);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000405}
406
Denys Vlasenkoc4199f22016-04-01 22:12:44 +0200407void FAST_FUNC xfchdir(int fd)
408{
409 if (fchdir(fd))
410 bb_perror_msg_and_die("fchdir");
411}
412
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000413void FAST_FUNC xchroot(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000414{
415 if (chroot(path))
Pascal Bellard70fc8c12012-06-12 13:21:02 +0200416 bb_perror_msg_and_die("can't change root directory to '%s'", path);
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100417 xchdir("/");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000418}
419
420// Print a warning message if opendir() fails, but don't die.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000421DIR* FAST_FUNC warn_opendir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000422{
423 DIR *dp;
424
425 dp = opendir(path);
426 if (!dp)
427 bb_perror_msg("can't open '%s'", path);
428 return dp;
429}
430
431// Die with an error message if opendir() fails.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000432DIR* FAST_FUNC xopendir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000433{
434 DIR *dp;
435
436 dp = opendir(path);
437 if (!dp)
438 bb_perror_msg_and_die("can't open '%s'", path);
439 return dp;
440}
441
442// Die with an error message if we can't open a new socket.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000443int FAST_FUNC xsocket(int domain, int type, int protocol)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000444{
445 int r = socket(domain, type, protocol);
446
447 if (r < 0) {
448 /* Hijack vaguely related config option */
449#if ENABLE_VERBOSE_RESOLUTION_ERRORS
450 const char *s = "INET";
Jeremie Koenig29885112010-05-27 15:39:24 +0200451# ifdef AF_PACKET
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000452 if (domain == AF_PACKET) s = "PACKET";
Jeremie Koenig29885112010-05-27 15:39:24 +0200453# endif
454# ifdef AF_NETLINK
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000455 if (domain == AF_NETLINK) s = "NETLINK";
Jeremie Koenig29885112010-05-27 15:39:24 +0200456# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000457IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100458 bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000459#else
460 bb_perror_msg_and_die("socket");
461#endif
462 }
463
464 return r;
465}
466
467// Die with an error message if we can't bind a socket to an address.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000468void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000469{
470 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
471}
472
473// Die with an error message if we can't listen for connections on a socket.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000474void FAST_FUNC xlisten(int s, int backlog)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000475{
476 if (listen(s, backlog)) bb_perror_msg_and_die("listen");
477}
478
479/* Die with an error message if sendto failed.
480 * Return bytes sent otherwise */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000481ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000482 socklen_t tolen)
483{
484 ssize_t ret = sendto(s, buf, len, 0, to, tolen);
485 if (ret < 0) {
486 if (ENABLE_FEATURE_CLEAN_UP)
487 close(s);
488 bb_perror_msg_and_die("sendto");
489 }
490 return ret;
491}
492
493// xstat() - a stat() which dies on failure with meaningful error message
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000494void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000495{
496 if (stat(name, stat_buf))
497 bb_perror_msg_and_die("can't stat '%s'", name);
498}
499
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200500void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
501{
502 /* errmsg is usually a file name, but not always:
503 * xfstat may be called in a spot where file name is no longer
504 * available, and caller may give e.g. "can't stat input file" string.
505 */
506 if (fstat(fd, stat_buf))
507 bb_simple_perror_msg_and_die(errmsg);
508}
509
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000510// selinux_or_die() - die if SELinux is disabled.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000511void FAST_FUNC selinux_or_die(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000512{
513#if ENABLE_SELINUX
514 int rc = is_selinux_enabled();
515 if (rc == 0) {
516 bb_error_msg_and_die("SELinux is disabled");
517 } else if (rc < 0) {
518 bb_error_msg_and_die("is_selinux_enabled() failed");
519 }
520#else
521 bb_error_msg_and_die("SELinux support is disabled");
522#endif
523}
524
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000525int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000526{
527 int ret;
528 va_list p;
529
530 ret = ioctl(fd, request, argp);
531 if (ret < 0) {
532 va_start(p, fmt);
533 bb_verror_msg(fmt, p, strerror(errno));
534 /* xfunc_die can actually longjmp, so be nice */
535 va_end(p);
536 xfunc_die();
537 }
538 return ret;
539}
540
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000541int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000542{
543 va_list p;
544 int ret = ioctl(fd, request, argp);
545
546 if (ret < 0) {
547 va_start(p, fmt);
548 bb_verror_msg(fmt, p, strerror(errno));
549 va_end(p);
550 }
551 return ret;
552}
553
554#if ENABLE_IOCTL_HEX2STR_ERROR
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000555int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000556{
557 int ret;
558
559 ret = ioctl(fd, request, argp);
560 if (ret < 0)
561 bb_simple_perror_msg(ioctl_name);
562 return ret;
563}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000564int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000565{
566 int ret;
567
568 ret = ioctl(fd, request, argp);
569 if (ret < 0)
570 bb_simple_perror_msg_and_die(ioctl_name);
571 return ret;
572}
573#else
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000574int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000575{
576 int ret;
577
578 ret = ioctl(fd, request, argp);
579 if (ret < 0)
580 bb_perror_msg("ioctl %#x failed", request);
581 return ret;
582}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000583int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000584{
585 int ret;
586
587 ret = ioctl(fd, request, argp);
588 if (ret < 0)
589 bb_perror_msg_and_die("ioctl %#x failed", request);
590 return ret;
591}
592#endif
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200593
594char* FAST_FUNC xmalloc_ttyname(int fd)
595{
Denys Vlasenko543efd72013-08-06 00:41:06 +0200596 char buf[128];
597 int r = ttyname_r(fd, buf, sizeof(buf) - 1);
598 if (r)
599 return NULL;
600 return xstrdup(buf);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200601}
602
603void FAST_FUNC generate_uuid(uint8_t *buf)
604{
605 /* http://www.ietf.org/rfc/rfc4122.txt
606 * 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
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | time_low |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * | time_mid | time_hi_and_version |
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * |clk_seq_and_variant | node (0-1) |
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * | node (2-5) |
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * IOW, uuid has this layout:
617 * uint32_t time_low (big endian)
618 * uint16_t time_mid (big endian)
619 * uint16_t time_hi_and_version (big endian)
620 * version is a 4-bit field:
621 * 1 Time-based
622 * 2 DCE Security, with embedded POSIX UIDs
623 * 3 Name-based (MD5)
624 * 4 Randomly generated
625 * 5 Name-based (SHA-1)
626 * uint16_t clk_seq_and_variant (big endian)
627 * variant is a 3-bit field:
628 * 0xx Reserved, NCS backward compatibility
629 * 10x The variant specified in rfc4122
630 * 110 Reserved, Microsoft backward compatibility
631 * 111 Reserved for future definition
632 * uint8_t node[6]
633 *
634 * For version 4, these bits are set/cleared:
635 * time_hi_and_version & 0x0fff | 0x4000
636 * clk_seq_and_variant & 0x3fff | 0x8000
637 */
638 pid_t pid;
639 int i;
640
641 i = open("/dev/urandom", O_RDONLY);
642 if (i >= 0) {
643 read(i, buf, 16);
644 close(i);
645 }
646 /* Paranoia. /dev/urandom may be missing.
647 * rand() is guaranteed to generate at least [0, 2^15) range,
648 * but lowest bits in some libc are not so "random". */
649 srand(monotonic_us()); /* pulls in printf */
650 pid = getpid();
651 while (1) {
652 for (i = 0; i < 16; i++)
653 buf[i] ^= rand() >> 5;
654 if (pid == 0)
655 break;
656 srand(pid);
657 pid = 0;
658 }
659
660 /* version = 4 */
661 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
662 /* variant = 10x */
663 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
664}
Pascal Bellard926031b2010-07-04 15:32:38 +0200665
666#if BB_MMU
667pid_t FAST_FUNC xfork(void)
668{
669 pid_t pid;
670 pid = fork();
671 if (pid < 0) /* wtf? */
672 bb_perror_msg_and_die("vfork"+1);
673 return pid;
674}
675#endif
Denys Vlasenko82203992016-04-02 18:06:24 +0200676
677void FAST_FUNC xvfork_parent_waits_and_exits(void)
678{
679 pid_t pid;
680
681 fflush_all();
682 pid = xvfork();
683 if (pid > 0) {
684 /* Parent */
685 int exit_status = wait_for_exitstatus(pid);
686 if (WIFSIGNALED(exit_status))
687 kill_myself_with_sig(WTERMSIG(exit_status));
688 _exit(WEXITSTATUS(exit_status));
689 }
690 /* Child continues */
691}