xfuncs_printf.c 16 KB

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