3
0

xfuncs_printf.c 17 KB

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