xfuncs_printf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 *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 *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 *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 *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 * 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 *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 *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 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 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 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 open_or_warn(const char *pathname, int flags)
  133. {
  134. return open3_or_warn(pathname, flags, 0666);
  135. }
  136. void xunlink(const char *pathname)
  137. {
  138. if (unlink(pathname))
  139. bb_perror_msg_and_die("can't remove file '%s'", pathname);
  140. }
  141. void 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 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 xpipe(int filedes[2])
  154. {
  155. if (pipe(filedes))
  156. bb_perror_msg_and_die("can't create pipe");
  157. }
  158. void 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 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 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. // Die with an error message if we can't lseek to the right spot.
  181. off_t xlseek(int fd, off_t offset, int whence)
  182. {
  183. off_t off = lseek(fd, offset, whence);
  184. if (off == (off_t)-1) {
  185. if (whence == SEEK_SET)
  186. bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
  187. bb_perror_msg_and_die("lseek");
  188. }
  189. return off;
  190. }
  191. // Die with supplied filename if this FILE * has ferror set.
  192. void die_if_ferror(FILE *fp, const char *fn)
  193. {
  194. if (ferror(fp)) {
  195. /* ferror doesn't set useful errno */
  196. bb_error_msg_and_die("%s: I/O error", fn);
  197. }
  198. }
  199. // Die with an error message if stdout has ferror set.
  200. void die_if_ferror_stdout(void)
  201. {
  202. die_if_ferror(stdout, bb_msg_standard_output);
  203. }
  204. // Die with an error message if we have trouble flushing stdout.
  205. void xfflush_stdout(void)
  206. {
  207. if (fflush(stdout)) {
  208. bb_perror_msg_and_die(bb_msg_standard_output);
  209. }
  210. }
  211. int bb_putchar(int ch)
  212. {
  213. /* time.c needs putc(ch, stdout), not putchar(ch).
  214. * it does "stdout = stderr;", but then glibc's putchar()
  215. * doesn't work as expected. bad glibc, bad */
  216. return putc(ch, stdout);
  217. }
  218. /* Die with an error message if we can't copy an entire FILE * to stdout,
  219. * then close that file. */
  220. void xprint_and_close_file(FILE *file)
  221. {
  222. fflush(stdout);
  223. // copyfd outputs error messages for us.
  224. if (bb_copyfd_eof(fileno(file), 1) == -1)
  225. xfunc_die();
  226. fclose(file);
  227. }
  228. // Die with an error message if we can't malloc() enough space and do an
  229. // sprintf() into that space.
  230. char *xasprintf(const char *format, ...)
  231. {
  232. va_list p;
  233. int r;
  234. char *string_ptr;
  235. #if 1
  236. // GNU extension
  237. va_start(p, format);
  238. r = vasprintf(&string_ptr, format, p);
  239. va_end(p);
  240. #else
  241. // Bloat for systems that haven't got the GNU extension.
  242. va_start(p, format);
  243. r = vsnprintf(NULL, 0, format, p);
  244. va_end(p);
  245. string_ptr = xmalloc(r+1);
  246. va_start(p, format);
  247. r = vsnprintf(string_ptr, r+1, format, p);
  248. va_end(p);
  249. #endif
  250. if (r < 0)
  251. bb_error_msg_and_die(bb_msg_memory_exhausted);
  252. return string_ptr;
  253. }
  254. #if 0 /* If we will ever meet a libc which hasn't [f]dprintf... */
  255. int fdprintf(int fd, const char *format, ...)
  256. {
  257. va_list p;
  258. int r;
  259. char *string_ptr;
  260. #if 1
  261. // GNU extension
  262. va_start(p, format);
  263. r = vasprintf(&string_ptr, format, p);
  264. va_end(p);
  265. #else
  266. // Bloat for systems that haven't got the GNU extension.
  267. va_start(p, format);
  268. r = vsnprintf(NULL, 0, format, p) + 1;
  269. va_end(p);
  270. string_ptr = malloc(r);
  271. if (string_ptr) {
  272. va_start(p, format);
  273. r = vsnprintf(string_ptr, r, format, p);
  274. va_end(p);
  275. }
  276. #endif
  277. if (r >= 0) {
  278. full_write(fd, string_ptr, r);
  279. free(string_ptr);
  280. }
  281. return r;
  282. }
  283. #endif
  284. void 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. // 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 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 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 xchdir(const char *path)
  303. {
  304. if (chdir(path))
  305. bb_perror_msg_and_die("chdir(%s)", path);
  306. }
  307. void 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 *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 *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 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. if (domain == AF_PACKET) s = "PACKET";
  339. if (domain == AF_NETLINK) s = "NETLINK";
  340. USE_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
  341. bb_perror_msg_and_die("socket(AF_%s)", s);
  342. #else
  343. bb_perror_msg_and_die("socket");
  344. #endif
  345. }
  346. return r;
  347. }
  348. // Die with an error message if we can't bind a socket to an address.
  349. void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
  350. {
  351. if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
  352. }
  353. // Die with an error message if we can't listen for connections on a socket.
  354. void xlisten(int s, int backlog)
  355. {
  356. if (listen(s, backlog)) bb_perror_msg_and_die("listen");
  357. }
  358. /* Die with an error message if sendto failed.
  359. * Return bytes sent otherwise */
  360. ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  361. socklen_t tolen)
  362. {
  363. ssize_t ret = sendto(s, buf, len, 0, to, tolen);
  364. if (ret < 0) {
  365. if (ENABLE_FEATURE_CLEAN_UP)
  366. close(s);
  367. bb_perror_msg_and_die("sendto");
  368. }
  369. return ret;
  370. }
  371. // xstat() - a stat() which dies on failure with meaningful error message
  372. void xstat(const char *name, struct stat *stat_buf)
  373. {
  374. if (stat(name, stat_buf))
  375. bb_perror_msg_and_die("can't stat '%s'", name);
  376. }
  377. // selinux_or_die() - die if SELinux is disabled.
  378. void selinux_or_die(void)
  379. {
  380. #if ENABLE_SELINUX
  381. int rc = is_selinux_enabled();
  382. if (rc == 0) {
  383. bb_error_msg_and_die("SELinux is disabled");
  384. } else if (rc < 0) {
  385. bb_error_msg_and_die("is_selinux_enabled() failed");
  386. }
  387. #else
  388. bb_error_msg_and_die("SELinux support is disabled");
  389. #endif
  390. }
  391. int ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
  392. {
  393. int ret;
  394. va_list p;
  395. ret = ioctl(fd, request, argp);
  396. if (ret < 0) {
  397. va_start(p, fmt);
  398. bb_verror_msg(fmt, p, strerror(errno));
  399. /* xfunc_die can actually longjmp, so be nice */
  400. va_end(p);
  401. xfunc_die();
  402. }
  403. return ret;
  404. }
  405. int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
  406. {
  407. va_list p;
  408. int ret = ioctl(fd, request, argp);
  409. if (ret < 0) {
  410. va_start(p, fmt);
  411. bb_verror_msg(fmt, p, strerror(errno));
  412. va_end(p);
  413. }
  414. return ret;
  415. }
  416. #if ENABLE_IOCTL_HEX2STR_ERROR
  417. int bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
  418. {
  419. int ret;
  420. ret = ioctl(fd, request, argp);
  421. if (ret < 0)
  422. bb_simple_perror_msg(ioctl_name);
  423. return ret;
  424. }
  425. int bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
  426. {
  427. int ret;
  428. ret = ioctl(fd, request, argp);
  429. if (ret < 0)
  430. bb_simple_perror_msg_and_die(ioctl_name);
  431. return ret;
  432. }
  433. #else
  434. int bb_ioctl_or_warn(int fd, unsigned request, void *argp)
  435. {
  436. int ret;
  437. ret = ioctl(fd, request, argp);
  438. if (ret < 0)
  439. bb_perror_msg("ioctl %#x failed", request);
  440. return ret;
  441. }
  442. int bb_xioctl(int fd, unsigned request, void *argp)
  443. {
  444. int ret;
  445. ret = ioctl(fd, request, argp);
  446. if (ret < 0)
  447. bb_perror_msg_and_die("ioctl %#x failed", request);
  448. return ret;
  449. }
  450. #endif