xfuncs_printf.c 16 KB

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