xfuncs_printf.c 17 KB

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