xfuncs_printf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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[2])
  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. /* Die with an error message if we can't copy an entire FILE* to stdout,
  272. * then close that file. */
  273. void FAST_FUNC xprint_and_close_file(FILE *file)
  274. {
  275. fflush_all();
  276. // copyfd outputs error messages for us.
  277. if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1)
  278. xfunc_die();
  279. fclose(file);
  280. }
  281. // Die with an error message if we can't malloc() enough space and do an
  282. // sprintf() into that space.
  283. char* FAST_FUNC xasprintf(const char *format, ...)
  284. {
  285. va_list p;
  286. int r;
  287. char *string_ptr;
  288. va_start(p, format);
  289. r = vasprintf(&string_ptr, format, p);
  290. va_end(p);
  291. if (r < 0)
  292. bb_die_memory_exhausted();
  293. return string_ptr;
  294. }
  295. void FAST_FUNC xsetenv(const char *key, const char *value)
  296. {
  297. if (setenv(key, value, 1))
  298. bb_die_memory_exhausted();
  299. }
  300. /* Handles "VAR=VAL" strings, even those which are part of environ
  301. * _right now_
  302. */
  303. void FAST_FUNC bb_unsetenv(const char *var)
  304. {
  305. char onstack[128 - 16]; /* smaller stack setup code on x86 */
  306. char *tp;
  307. tp = strchr(var, '=');
  308. if (tp) {
  309. /* In case var was putenv'ed, we can't replace '='
  310. * with NUL and unsetenv(var) - it won't work,
  311. * env is modified by the replacement, unsetenv
  312. * sees "VAR" instead of "VAR=VAL" and does not remove it!
  313. * Horror :(
  314. */
  315. unsigned sz = tp - var;
  316. if (sz < sizeof(onstack)) {
  317. ((char*)mempcpy(onstack, var, sz))[0] = '\0';
  318. tp = NULL;
  319. var = onstack;
  320. } else {
  321. /* unlikely: very long var name */
  322. var = tp = xstrndup(var, sz);
  323. }
  324. }
  325. unsetenv(var);
  326. free(tp);
  327. }
  328. void FAST_FUNC bb_unsetenv_and_free(char *var)
  329. {
  330. bb_unsetenv(var);
  331. free(var);
  332. }
  333. // Die with an error message if we can't set gid. (Because resource limits may
  334. // limit this user to a given number of processes, and if that fills up the
  335. // setgid() will fail and we'll _still_be_root_, which is bad.)
  336. void FAST_FUNC xsetgid(gid_t gid)
  337. {
  338. if (setgid(gid)) bb_simple_perror_msg_and_die("setgid");
  339. }
  340. // Die with an error message if we can't set uid. (See xsetgid() for why.)
  341. void FAST_FUNC xsetuid(uid_t uid)
  342. {
  343. if (setuid(uid)) bb_simple_perror_msg_and_die("setuid");
  344. }
  345. void FAST_FUNC xsetegid(gid_t egid)
  346. {
  347. if (setegid(egid)) bb_simple_perror_msg_and_die("setegid");
  348. }
  349. void FAST_FUNC xseteuid(uid_t euid)
  350. {
  351. if (seteuid(euid)) bb_simple_perror_msg_and_die("seteuid");
  352. }
  353. // Die if we can't chdir to a new path.
  354. void FAST_FUNC xchdir(const char *path)
  355. {
  356. if (chdir(path))
  357. bb_perror_msg_and_die("can't change directory to '%s'", path);
  358. }
  359. void FAST_FUNC xfchdir(int fd)
  360. {
  361. if (fchdir(fd))
  362. bb_simple_perror_msg_and_die("fchdir");
  363. }
  364. void FAST_FUNC xchroot(const char *path)
  365. {
  366. if (chroot(path))
  367. bb_perror_msg_and_die("can't change root directory to '%s'", path);
  368. xchdir("/");
  369. }
  370. // Print a warning message if opendir() fails, but don't die.
  371. DIR* FAST_FUNC warn_opendir(const char *path)
  372. {
  373. DIR *dp;
  374. dp = opendir(path);
  375. if (!dp)
  376. bb_perror_msg("can't open '%s'", path);
  377. return dp;
  378. }
  379. // Die with an error message if opendir() fails.
  380. DIR* FAST_FUNC xopendir(const char *path)
  381. {
  382. DIR *dp;
  383. dp = opendir(path);
  384. if (!dp)
  385. bb_perror_msg_and_die("can't open '%s'", path);
  386. return dp;
  387. }
  388. // Die with an error message if we can't open a new socket.
  389. int FAST_FUNC xsocket(int domain, int type, int protocol)
  390. {
  391. int r = socket(domain, type, protocol);
  392. if (r < 0) {
  393. /* Hijack vaguely related config option */
  394. #if ENABLE_VERBOSE_RESOLUTION_ERRORS
  395. const char *s = "INET";
  396. # ifdef AF_PACKET
  397. if (domain == AF_PACKET) s = "PACKET";
  398. # endif
  399. # ifdef AF_NETLINK
  400. if (domain == AF_NETLINK) s = "NETLINK";
  401. # endif
  402. IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
  403. bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
  404. #else
  405. bb_simple_perror_msg_and_die("socket");
  406. #endif
  407. }
  408. return r;
  409. }
  410. // Die with an error message if we can't bind a socket to an address.
  411. void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
  412. {
  413. if (bind(sockfd, my_addr, addrlen)) bb_simple_perror_msg_and_die("bind");
  414. }
  415. // Die with an error message if we can't listen for connections on a socket.
  416. void FAST_FUNC xlisten(int s, int backlog)
  417. {
  418. if (listen(s, backlog)) bb_simple_perror_msg_and_die("listen");
  419. }
  420. /* Die with an error message if sendto failed.
  421. * Return bytes sent otherwise */
  422. ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  423. socklen_t tolen)
  424. {
  425. ssize_t ret = sendto(s, buf, len, 0, to, tolen);
  426. if (ret < 0) {
  427. if (ENABLE_FEATURE_CLEAN_UP)
  428. close(s);
  429. bb_simple_perror_msg_and_die("sendto");
  430. }
  431. return ret;
  432. }
  433. // xstat() - a stat() which dies on failure with meaningful error message
  434. void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
  435. {
  436. if (stat(name, stat_buf))
  437. bb_perror_msg_and_die("can't stat '%s'", name);
  438. }
  439. void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
  440. {
  441. /* errmsg is usually a file name, but not always:
  442. * xfstat may be called in a spot where file name is no longer
  443. * available, and caller may give e.g. "can't stat input file" string.
  444. */
  445. if (fstat(fd, stat_buf))
  446. bb_simple_perror_msg_and_die(errmsg);
  447. }
  448. #if ENABLE_SELINUX
  449. // selinux_or_die() - die if SELinux is disabled.
  450. void FAST_FUNC selinux_or_die(void)
  451. {
  452. int rc = is_selinux_enabled();
  453. if (rc == 0) {
  454. bb_simple_error_msg_and_die("SELinux is disabled");
  455. } else if (rc < 0) {
  456. bb_simple_error_msg_and_die("is_selinux_enabled() failed");
  457. }
  458. }
  459. #else
  460. /* not defined, other code must have no calls to it */
  461. #endif
  462. int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
  463. {
  464. int ret;
  465. va_list p;
  466. ret = ioctl(fd, request, argp);
  467. if (ret < 0) {
  468. va_start(p, fmt);
  469. bb_verror_msg(fmt, p, strerror(errno));
  470. /* xfunc_die can actually longjmp, so be nice */
  471. va_end(p);
  472. xfunc_die();
  473. }
  474. return ret;
  475. }
  476. int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
  477. {
  478. va_list p;
  479. int ret = ioctl(fd, request, argp);
  480. if (ret < 0) {
  481. va_start(p, fmt);
  482. bb_verror_msg(fmt, p, strerror(errno));
  483. va_end(p);
  484. }
  485. return ret;
  486. }
  487. #if ENABLE_IOCTL_HEX2STR_ERROR
  488. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
  489. {
  490. int ret;
  491. ret = ioctl(fd, request, argp);
  492. if (ret < 0)
  493. bb_simple_perror_msg(ioctl_name);
  494. return ret;
  495. }
  496. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
  497. {
  498. int ret;
  499. ret = ioctl(fd, request, argp);
  500. if (ret < 0)
  501. bb_simple_perror_msg_and_die(ioctl_name);
  502. return ret;
  503. }
  504. #else
  505. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
  506. {
  507. int ret;
  508. ret = ioctl(fd, request, argp);
  509. if (ret < 0)
  510. bb_perror_msg("ioctl %#x failed", request);
  511. return ret;
  512. }
  513. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
  514. {
  515. int ret;
  516. ret = ioctl(fd, request, argp);
  517. if (ret < 0)
  518. bb_perror_msg_and_die("ioctl %#x failed", request);
  519. return ret;
  520. }
  521. #endif
  522. char* FAST_FUNC xmalloc_ttyname(int fd)
  523. {
  524. char buf[128];
  525. int r = ttyname_r(fd, buf, sizeof(buf) - 1);
  526. if (r)
  527. return NULL;
  528. return xstrdup(buf);
  529. }
  530. void FAST_FUNC generate_uuid(uint8_t *buf)
  531. {
  532. /* http://www.ietf.org/rfc/rfc4122.txt
  533. * 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
  534. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  535. * | time_low |
  536. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  537. * | time_mid | time_hi_and_version |
  538. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  539. * |clk_seq_and_variant | node (0-1) |
  540. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  541. * | node (2-5) |
  542. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  543. * IOW, uuid has this layout:
  544. * uint32_t time_low (big endian)
  545. * uint16_t time_mid (big endian)
  546. * uint16_t time_hi_and_version (big endian)
  547. * version is a 4-bit field:
  548. * 1 Time-based
  549. * 2 DCE Security, with embedded POSIX UIDs
  550. * 3 Name-based (MD5)
  551. * 4 Randomly generated
  552. * 5 Name-based (SHA-1)
  553. * uint16_t clk_seq_and_variant (big endian)
  554. * variant is a 3-bit field:
  555. * 0xx Reserved, NCS backward compatibility
  556. * 10x The variant specified in rfc4122
  557. * 110 Reserved, Microsoft backward compatibility
  558. * 111 Reserved for future definition
  559. * uint8_t node[6]
  560. *
  561. * For version 4, these bits are set/cleared:
  562. * time_hi_and_version & 0x0fff | 0x4000
  563. * clk_seq_and_variant & 0x3fff | 0x8000
  564. */
  565. pid_t pid;
  566. int i;
  567. open_read_close("/dev/urandom", buf, 16);
  568. /* Paranoia. /dev/urandom may be missing.
  569. * rand() is guaranteed to generate at least [0, 2^15) range,
  570. * but lowest bits in some libc are not so "random".
  571. */
  572. srand(monotonic_us()); /* pulls in printf */
  573. pid = getpid();
  574. while (1) {
  575. for (i = 0; i < 16; i++)
  576. buf[i] ^= rand() >> 5;
  577. if (pid == 0)
  578. break;
  579. srand(pid);
  580. pid = 0;
  581. }
  582. /* version = 4 */
  583. buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
  584. /* variant = 10x */
  585. buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
  586. }
  587. #if BB_MMU
  588. pid_t FAST_FUNC xfork(void)
  589. {
  590. pid_t pid;
  591. pid = fork();
  592. if (pid < 0) /* wtf? */
  593. bb_simple_perror_msg_and_die("vfork"+1);
  594. return pid;
  595. }
  596. #endif
  597. void FAST_FUNC xvfork_parent_waits_and_exits(void)
  598. {
  599. pid_t pid;
  600. fflush_all();
  601. pid = xvfork();
  602. if (pid > 0) {
  603. /* Parent */
  604. int exit_status = wait_for_exitstatus(pid);
  605. if (WIFSIGNALED(exit_status))
  606. kill_myself_with_sig(WTERMSIG(exit_status));
  607. _exit(WEXITSTATUS(exit_status));
  608. }
  609. /* Child continues */
  610. }
  611. // Useful when we do know that pid is valid, and we just want to wait
  612. // for it to exit. Not existing pid is fatal. waitpid() status is not returned.
  613. int FAST_FUNC wait_for_exitstatus(pid_t pid)
  614. {
  615. int exit_status, n;
  616. n = safe_waitpid(pid, &exit_status, 0);
  617. if (n < 0)
  618. bb_simple_perror_msg_and_die("waitpid");
  619. return exit_status;
  620. }
  621. void FAST_FUNC xsettimeofday(const struct timeval *tv)
  622. {
  623. if (settimeofday(tv, NULL))
  624. bb_simple_perror_msg_and_die("settimeofday");
  625. }