3
0

xfuncs_printf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. /* 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. // Die with supplied filename if this FILE* has ferror set.
  209. void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
  210. {
  211. if (ferror(fp)) {
  212. /* ferror doesn't set useful errno */
  213. bb_error_msg_and_die("%s: I/O error", fn);
  214. }
  215. }
  216. // Die with an error message if stdout has ferror set.
  217. void FAST_FUNC die_if_ferror_stdout(void)
  218. {
  219. die_if_ferror(stdout, bb_msg_standard_output);
  220. }
  221. int FAST_FUNC fflush_all(void)
  222. {
  223. return fflush(NULL);
  224. }
  225. int FAST_FUNC bb_putchar(int ch)
  226. {
  227. return putchar(ch);
  228. }
  229. /* Die with an error message if we can't copy an entire FILE* to stdout,
  230. * then close that file. */
  231. void FAST_FUNC xprint_and_close_file(FILE *file)
  232. {
  233. fflush_all();
  234. // copyfd outputs error messages for us.
  235. if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1)
  236. xfunc_die();
  237. fclose(file);
  238. }
  239. // Die with an error message if we can't malloc() enough space and do an
  240. // sprintf() into that space.
  241. char* FAST_FUNC xasprintf(const char *format, ...)
  242. {
  243. va_list p;
  244. int r;
  245. char *string_ptr;
  246. va_start(p, format);
  247. r = vasprintf(&string_ptr, format, p);
  248. va_end(p);
  249. if (r < 0)
  250. bb_error_msg_and_die(bb_msg_memory_exhausted);
  251. return string_ptr;
  252. }
  253. void FAST_FUNC xsetenv(const char *key, const char *value)
  254. {
  255. if (setenv(key, value, 1))
  256. bb_error_msg_and_die(bb_msg_memory_exhausted);
  257. }
  258. /* Handles "VAR=VAL" strings, even those which are part of environ
  259. * _right now_
  260. */
  261. void FAST_FUNC bb_unsetenv(const char *var)
  262. {
  263. char *tp = strchr(var, '=');
  264. if (!tp) {
  265. unsetenv(var);
  266. return;
  267. }
  268. /* In case var was putenv'ed, we can't replace '='
  269. * with NUL and unsetenv(var) - it won't work,
  270. * env is modified by the replacement, unsetenv
  271. * sees "VAR" instead of "VAR=VAL" and does not remove it!
  272. * horror :( */
  273. tp = xstrndup(var, tp - var);
  274. unsetenv(tp);
  275. free(tp);
  276. }
  277. // Die with an error message if we can't set gid. (Because resource limits may
  278. // limit this user to a given number of processes, and if that fills up the
  279. // setgid() will fail and we'll _still_be_root_, which is bad.)
  280. void FAST_FUNC xsetgid(gid_t gid)
  281. {
  282. if (setgid(gid)) bb_perror_msg_and_die("setgid");
  283. }
  284. // Die with an error message if we can't set uid. (See xsetgid() for why.)
  285. void FAST_FUNC xsetuid(uid_t uid)
  286. {
  287. if (setuid(uid)) bb_perror_msg_and_die("setuid");
  288. }
  289. // Die if we can't chdir to a new path.
  290. void FAST_FUNC xchdir(const char *path)
  291. {
  292. if (chdir(path))
  293. bb_perror_msg_and_die("chdir(%s)", path);
  294. }
  295. void FAST_FUNC xchroot(const char *path)
  296. {
  297. if (chroot(path))
  298. bb_perror_msg_and_die("can't change root directory to %s", path);
  299. }
  300. // Print a warning message if opendir() fails, but don't die.
  301. DIR* FAST_FUNC warn_opendir(const char *path)
  302. {
  303. DIR *dp;
  304. dp = opendir(path);
  305. if (!dp)
  306. bb_perror_msg("can't open '%s'", path);
  307. return dp;
  308. }
  309. // Die with an error message if opendir() fails.
  310. DIR* FAST_FUNC xopendir(const char *path)
  311. {
  312. DIR *dp;
  313. dp = opendir(path);
  314. if (!dp)
  315. bb_perror_msg_and_die("can't open '%s'", path);
  316. return dp;
  317. }
  318. // Die with an error message if we can't open a new socket.
  319. int FAST_FUNC xsocket(int domain, int type, int protocol)
  320. {
  321. int r = socket(domain, type, protocol);
  322. if (r < 0) {
  323. /* Hijack vaguely related config option */
  324. #if ENABLE_VERBOSE_RESOLUTION_ERRORS
  325. const char *s = "INET";
  326. if (domain == AF_PACKET) s = "PACKET";
  327. if (domain == AF_NETLINK) s = "NETLINK";
  328. IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
  329. bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
  330. #else
  331. bb_perror_msg_and_die("socket");
  332. #endif
  333. }
  334. return r;
  335. }
  336. // Die with an error message if we can't bind a socket to an address.
  337. void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
  338. {
  339. if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
  340. }
  341. // Die with an error message if we can't listen for connections on a socket.
  342. void FAST_FUNC xlisten(int s, int backlog)
  343. {
  344. if (listen(s, backlog)) bb_perror_msg_and_die("listen");
  345. }
  346. /* Die with an error message if sendto failed.
  347. * Return bytes sent otherwise */
  348. ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  349. socklen_t tolen)
  350. {
  351. ssize_t ret = sendto(s, buf, len, 0, to, tolen);
  352. if (ret < 0) {
  353. if (ENABLE_FEATURE_CLEAN_UP)
  354. close(s);
  355. bb_perror_msg_and_die("sendto");
  356. }
  357. return ret;
  358. }
  359. // xstat() - a stat() which dies on failure with meaningful error message
  360. void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
  361. {
  362. if (stat(name, stat_buf))
  363. bb_perror_msg_and_die("can't stat '%s'", name);
  364. }
  365. // selinux_or_die() - die if SELinux is disabled.
  366. void FAST_FUNC selinux_or_die(void)
  367. {
  368. #if ENABLE_SELINUX
  369. int rc = is_selinux_enabled();
  370. if (rc == 0) {
  371. bb_error_msg_and_die("SELinux is disabled");
  372. } else if (rc < 0) {
  373. bb_error_msg_and_die("is_selinux_enabled() failed");
  374. }
  375. #else
  376. bb_error_msg_and_die("SELinux support is disabled");
  377. #endif
  378. }
  379. int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
  380. {
  381. int ret;
  382. va_list p;
  383. ret = ioctl(fd, request, argp);
  384. if (ret < 0) {
  385. va_start(p, fmt);
  386. bb_verror_msg(fmt, p, strerror(errno));
  387. /* xfunc_die can actually longjmp, so be nice */
  388. va_end(p);
  389. xfunc_die();
  390. }
  391. return ret;
  392. }
  393. int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
  394. {
  395. va_list p;
  396. int ret = ioctl(fd, request, argp);
  397. if (ret < 0) {
  398. va_start(p, fmt);
  399. bb_verror_msg(fmt, p, strerror(errno));
  400. va_end(p);
  401. }
  402. return ret;
  403. }
  404. #if ENABLE_IOCTL_HEX2STR_ERROR
  405. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
  406. {
  407. int ret;
  408. ret = ioctl(fd, request, argp);
  409. if (ret < 0)
  410. bb_simple_perror_msg(ioctl_name);
  411. return ret;
  412. }
  413. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
  414. {
  415. int ret;
  416. ret = ioctl(fd, request, argp);
  417. if (ret < 0)
  418. bb_simple_perror_msg_and_die(ioctl_name);
  419. return ret;
  420. }
  421. #else
  422. int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
  423. {
  424. int ret;
  425. ret = ioctl(fd, request, argp);
  426. if (ret < 0)
  427. bb_perror_msg("ioctl %#x failed", request);
  428. return ret;
  429. }
  430. int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
  431. {
  432. int ret;
  433. ret = ioctl(fd, request, argp);
  434. if (ret < 0)
  435. bb_perror_msg_and_die("ioctl %#x failed", request);
  436. return ret;
  437. }
  438. #endif