3
0

xfuncs_printf.c 15 KB

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