xfuncs_printf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 GPL version 2, see file LICENSE in this tarball for details.
  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 an existing file and return a fd.
  117. int FAST_FUNC xopen(const char *pathname, int flags)
  118. {
  119. return xopen3(pathname, flags, 0666);
  120. }
  121. // Warn if we can't open a file and return a fd.
  122. int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
  123. {
  124. int ret;
  125. ret = open(pathname, flags, mode);
  126. if (ret < 0) {
  127. bb_perror_msg("can't open '%s'", pathname);
  128. }
  129. return ret;
  130. }
  131. // Warn if we can't open a file and return a fd.
  132. int FAST_FUNC open_or_warn(const char *pathname, int flags)
  133. {
  134. return open3_or_warn(pathname, flags, 0666);
  135. }
  136. void FAST_FUNC xunlink(const char *pathname)
  137. {
  138. if (unlink(pathname))
  139. bb_perror_msg_and_die("can't remove file '%s'", pathname);
  140. }
  141. void FAST_FUNC xrename(const char *oldpath, const char *newpath)
  142. {
  143. if (rename(oldpath, newpath))
  144. bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath);
  145. }
  146. int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath)
  147. {
  148. int n = rename(oldpath, newpath);
  149. if (n)
  150. bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath);
  151. return n;
  152. }
  153. void FAST_FUNC xpipe(int filedes[2])
  154. {
  155. if (pipe(filedes))
  156. bb_perror_msg_and_die("can't create pipe");
  157. }
  158. void FAST_FUNC xdup2(int from, int to)
  159. {
  160. if (dup2(from, to) != to)
  161. bb_perror_msg_and_die("can't duplicate file descriptor");
  162. }
  163. // "Renumber" opened fd
  164. void FAST_FUNC xmove_fd(int from, int to)
  165. {
  166. if (from == to)
  167. return;
  168. xdup2(from, to);
  169. close(from);
  170. }
  171. // Die with an error message if we can't write the entire buffer.
  172. void FAST_FUNC xwrite(int fd, const void *buf, size_t count)
  173. {
  174. if (count) {
  175. ssize_t size = full_write(fd, buf, count);
  176. if ((size_t)size != count)
  177. bb_error_msg_and_die("short write");
  178. }
  179. }
  180. void FAST_FUNC xwrite_str(int fd, const char *str)
  181. {
  182. xwrite(fd, str, strlen(str));
  183. }
  184. // Die with an error message if we can't lseek to the right spot.
  185. off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
  186. {
  187. off_t off = lseek(fd, offset, whence);
  188. if (off == (off_t)-1) {
  189. if (whence == SEEK_SET)
  190. bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
  191. bb_perror_msg_and_die("lseek");
  192. }
  193. return off;
  194. }
  195. // Die with supplied filename if this FILE* has ferror set.
  196. void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
  197. {
  198. if (ferror(fp)) {
  199. /* ferror doesn't set useful errno */
  200. bb_error_msg_and_die("%s: I/O error", fn);
  201. }
  202. }
  203. // Die with an error message if stdout has ferror set.
  204. void FAST_FUNC die_if_ferror_stdout(void)
  205. {
  206. die_if_ferror(stdout, bb_msg_standard_output);
  207. }
  208. // Die with an error message if we have trouble flushing stdout.
  209. void FAST_FUNC xfflush_stdout(void)
  210. {
  211. if (fflush(stdout)) {
  212. bb_perror_msg_and_die(bb_msg_standard_output);
  213. }
  214. }
  215. int FAST_FUNC bb_putchar(int ch)
  216. {
  217. /* time.c needs putc(ch, stdout), not putchar(ch).
  218. * it does "stdout = stderr;", but then glibc's putchar()
  219. * doesn't work as expected. bad glibc, bad */
  220. return putc(ch, stdout);
  221. }
  222. /* Die with an error message if we can't copy an entire FILE* to stdout,
  223. * then close that file. */
  224. void FAST_FUNC xprint_and_close_file(FILE *file)
  225. {
  226. fflush(stdout);
  227. // copyfd outputs error messages for us.
  228. if (bb_copyfd_eof(fileno(file), 1) == -1)
  229. xfunc_die();
  230. fclose(file);
  231. }
  232. // Die with an error message if we can't malloc() enough space and do an
  233. // sprintf() into that space.
  234. char* FAST_FUNC xasprintf(const char *format, ...)
  235. {
  236. va_list p;
  237. int r;
  238. char *string_ptr;
  239. #if 1
  240. // GNU extension
  241. va_start(p, format);
  242. r = vasprintf(&string_ptr, format, p);
  243. va_end(p);
  244. #else
  245. // Bloat for systems that haven't got the GNU extension.
  246. va_start(p, format);
  247. r = vsnprintf(NULL, 0, format, p);
  248. va_end(p);
  249. string_ptr = xmalloc(r+1);
  250. va_start(p, format);
  251. r = vsnprintf(string_ptr, r+1, format, p);
  252. va_end(p);
  253. #endif
  254. if (r < 0)
  255. bb_error_msg_and_die(bb_msg_memory_exhausted);
  256. return string_ptr;
  257. }
  258. #if 0 /* If we will ever meet a libc which hasn't [f]dprintf... */
  259. int FAST_FUNC fdprintf(int fd, const char *format, ...)
  260. {
  261. va_list p;
  262. int r;
  263. char *string_ptr;
  264. #if 1
  265. // GNU extension
  266. va_start(p, format);
  267. r = vasprintf(&string_ptr, format, p);
  268. va_end(p);
  269. #else
  270. // Bloat for systems that haven't got the GNU extension.
  271. va_start(p, format);
  272. r = vsnprintf(NULL, 0, format, p) + 1;
  273. va_end(p);
  274. string_ptr = malloc(r);
  275. if (string_ptr) {
  276. va_start(p, format);
  277. r = vsnprintf(string_ptr, r, format, p);
  278. va_end(p);
  279. }
  280. #endif
  281. if (r >= 0) {
  282. full_write(fd, string_ptr, r);
  283. free(string_ptr);
  284. }
  285. return r;
  286. }
  287. #endif
  288. void FAST_FUNC xsetenv(const char *key, const char *value)
  289. {
  290. if (setenv(key, value, 1))
  291. bb_error_msg_and_die(bb_msg_memory_exhausted);
  292. }
  293. /* Handles "VAR=VAL" strings, even those which are part of environ
  294. * _right now_
  295. */
  296. void FAST_FUNC bb_unsetenv(const char *var)
  297. {
  298. char *tp = strchr(var, '=');
  299. if (!tp) {
  300. unsetenv(var);
  301. return;
  302. }
  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. tp = xstrndup(var, tp - var);
  309. unsetenv(tp);
  310. free(tp);
  311. }
  312. // Die with an error message if we can't set gid. (Because resource limits may
  313. // limit this user to a given number of processes, and if that fills up the
  314. // setgid() will fail and we'll _still_be_root_, which is bad.)
  315. void FAST_FUNC xsetgid(gid_t gid)
  316. {
  317. if (setgid(gid)) bb_perror_msg_and_die("setgid");
  318. }
  319. // Die with an error message if we can't set uid. (See xsetgid() for why.)
  320. void FAST_FUNC xsetuid(uid_t uid)
  321. {
  322. if (setuid(uid)) bb_perror_msg_and_die("setuid");
  323. }
  324. // Die if we can't chdir to a new path.
  325. void FAST_FUNC xchdir(const char *path)
  326. {
  327. if (chdir(path))
  328. bb_perror_msg_and_die("chdir(%s)", path);
  329. }
  330. void FAST_FUNC xchroot(const char *path)
  331. {
  332. if (chroot(path))
  333. bb_perror_msg_and_die("can't change root directory to %s", path);
  334. }
  335. // Print a warning message if opendir() fails, but don't die.
  336. DIR* FAST_FUNC warn_opendir(const char *path)
  337. {
  338. DIR *dp;
  339. dp = opendir(path);
  340. if (!dp)
  341. bb_perror_msg("can't open '%s'", path);
  342. return dp;
  343. }
  344. // Die with an error message if opendir() fails.
  345. DIR* FAST_FUNC xopendir(const char *path)
  346. {
  347. DIR *dp;
  348. dp = opendir(path);
  349. if (!dp)
  350. bb_perror_msg_and_die("can't open '%s'", path);
  351. return dp;
  352. }
  353. // Die with an error message if we can't open a new socket.
  354. int FAST_FUNC xsocket(int domain, int type, int protocol)
  355. {
  356. int r = socket(domain, type, protocol);
  357. if (r < 0) {
  358. /* Hijack vaguely related config option */
  359. #if ENABLE_VERBOSE_RESOLUTION_ERRORS
  360. const char *s = "INET";
  361. if (domain == AF_PACKET) s = "PACKET";
  362. if (domain == AF_NETLINK) s = "NETLINK";
  363. IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
  364. bb_perror_msg_and_die("socket(AF_%s)", s);
  365. #else
  366. bb_perror_msg_and_die("socket");
  367. #endif
  368. }
  369. return r;
  370. }
  371. // Die with an error message if we can't bind a socket to an address.
  372. void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
  373. {
  374. if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
  375. }
  376. // Die with an error message if we can't listen for connections on a socket.
  377. void FAST_FUNC xlisten(int s, int backlog)
  378. {
  379. if (listen(s, backlog)) bb_perror_msg_and_die("listen");
  380. }
  381. /* Die with an error message if sendto failed.
  382. * Return bytes sent otherwise */
  383. ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  384. socklen_t tolen)
  385. {
  386. ssize_t ret = sendto(s, buf, len, 0, to, tolen);
  387. if (ret < 0) {
  388. if (ENABLE_FEATURE_CLEAN_UP)
  389. close(s);
  390. bb_perror_msg_and_die("sendto");
  391. }
  392. return ret;
  393. }
  394. // xstat() - a stat() which dies on failure with meaningful error message
  395. void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
  396. {
  397. if (stat(name, stat_buf))
  398. bb_perror_msg_and_die("can't stat '%s'", name);
  399. }
  400. // selinux_or_die() - die if SELinux is disabled.
  401. void FAST_FUNC selinux_or_die(void)
  402. {
  403. #if ENABLE_SELINUX
  404. int rc = is_selinux_enabled();
  405. if (rc == 0) {
  406. bb_error_msg_and_die("SELinux is disabled");
  407. } else if (rc < 0) {
  408. bb_error_msg_and_die("is_selinux_enabled() failed");
  409. }
  410. #else
  411. bb_error_msg_and_die("SELinux support is disabled");
  412. #endif
  413. }
  414. int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
  415. {
  416. int ret;
  417. va_list p;
  418. ret = ioctl(fd, request, argp);
  419. if (ret < 0) {
  420. va_start(p, fmt);
  421. bb_verror_msg(fmt, p, strerror(errno));
  422. /* xfunc_die can actually longjmp, so be nice */
  423. va_end(p);
  424. xfunc_die();
  425. }
  426. return ret;
  427. }
  428. int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
  429. {
  430. va_list p;
  431. int ret = ioctl(fd, request, argp);
  432. if (ret < 0) {
  433. va_start(p, fmt);
  434. bb_verror_msg(fmt, p, strerror(errno));
  435. va_end(p);
  436. }
  437. return ret;
  438. }
  439. #if ENABLE_IOCTL_HEX2STR_ERROR
  440. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
  441. {
  442. int ret;
  443. ret = ioctl(fd, request, argp);
  444. if (ret < 0)
  445. bb_simple_perror_msg(ioctl_name);
  446. return ret;
  447. }
  448. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
  449. {
  450. int ret;
  451. ret = ioctl(fd, request, argp);
  452. if (ret < 0)
  453. bb_simple_perror_msg_and_die(ioctl_name);
  454. return ret;
  455. }
  456. #else
  457. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
  458. {
  459. int ret;
  460. ret = ioctl(fd, request, argp);
  461. if (ret < 0)
  462. bb_perror_msg("ioctl %#x failed", request);
  463. return ret;
  464. }
  465. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
  466. {
  467. int ret;
  468. ret = ioctl(fd, request, argp);
  469. if (ret < 0)
  470. bb_perror_msg_and_die("ioctl %#x failed", request);
  471. return ret;
  472. }
  473. #endif