xfuncs_printf.c 16 KB

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