libbb.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Busybox main internal header file
  4. *
  5. * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
  6. * Permission has been granted to redistribute this code under the GPL.
  7. *
  8. * Licensed under the GPL version 2, see the file LICENSE in this tarball.
  9. */
  10. #ifndef __LIBBUSYBOX_H__
  11. #define __LIBBUSYBOX_H__ 1
  12. #include "platform.h"
  13. #include <ctype.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <inttypes.h>
  18. #include <netdb.h>
  19. #include <setjmp.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <sys/poll.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/mman.h>
  29. #include <sys/socket.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <termios.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <utime.h>
  38. /* Try to pull in PATH_MAX */
  39. #include <limits.h>
  40. #include <sys/param.h>
  41. #ifndef PATH_MAX
  42. #define PATH_MAX 256
  43. #endif
  44. #ifdef HAVE_MNTENT_H
  45. #include <mntent.h>
  46. #endif
  47. #ifdef HAVE_SYS_STATFS_H
  48. #include <sys/statfs.h>
  49. #endif
  50. #if ENABLE_SELINUX
  51. #include <selinux/selinux.h>
  52. #include <selinux/context.h>
  53. #include <selinux/flask.h>
  54. #include <selinux/av_permissions.h>
  55. #endif
  56. #if ENABLE_LOCALE_SUPPORT
  57. #include <locale.h>
  58. #else
  59. #define setlocale(x,y) ((void)0)
  60. #endif
  61. #ifdef DMALLOC
  62. #include <dmalloc.h>
  63. #endif
  64. #include <pwd.h>
  65. #include <grp.h>
  66. #if ENABLE_FEATURE_SHADOWPASSWDS
  67. # include <shadow.h>
  68. #endif
  69. /* Some libc's forget to declare these, do it ourself */
  70. extern char **environ;
  71. #if defined(__GLIBC__) && __GLIBC__ < 2
  72. int vdprintf(int d, const char *format, va_list ap);
  73. #endif
  74. /* klogctl is in libc's klog.h, but we cheat and not #include that */
  75. int klogctl(int type, char *b, int len);
  76. /* This is declared here rather than #including <libgen.h> in order to avoid
  77. * confusing the two versions of basename. See the dirname/basename man page
  78. * for details. */
  79. char *dirname(char *path);
  80. /* Include our own copy of struct sysinfo to avoid binary compatibility
  81. * problems with Linux 2.4, which changed things. Grumble, grumble. */
  82. struct sysinfo {
  83. long uptime; /* Seconds since boot */
  84. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  85. unsigned long totalram; /* Total usable main memory size */
  86. unsigned long freeram; /* Available memory size */
  87. unsigned long sharedram; /* Amount of shared memory */
  88. unsigned long bufferram; /* Memory used by buffers */
  89. unsigned long totalswap; /* Total swap space size */
  90. unsigned long freeswap; /* swap space still available */
  91. unsigned short procs; /* Number of current processes */
  92. unsigned short pad; /* Padding needed for m68k */
  93. unsigned long totalhigh; /* Total high memory size */
  94. unsigned long freehigh; /* Available high memory size */
  95. unsigned int mem_unit; /* Memory unit size in bytes */
  96. char _f[20 - 2 * sizeof(long) - sizeof(int)]; /* Padding: libc5 uses this.. */
  97. };
  98. int sysinfo(struct sysinfo* info);
  99. /* Make all declarations hidden (-fvisibility flag only affects definitions) */
  100. /* (don't include system headers after this until corresponding pop!) */
  101. #if __GNUC_PREREQ(4,1)
  102. # pragma GCC visibility push(hidden)
  103. #endif
  104. #if ENABLE_USE_BB_PWD_GRP
  105. # include "pwd_.h"
  106. # include "grp_.h"
  107. #endif
  108. #if ENABLE_FEATURE_SHADOWPASSWDS
  109. # if ENABLE_USE_BB_SHADOW
  110. # include "shadow_.h"
  111. # endif
  112. #endif
  113. /* Tested to work correctly with all int types (IIRC :]) */
  114. #define MAXINT(T) (T)( \
  115. ((T)-1) > 0 \
  116. ? (T)-1 \
  117. : (T)~((T)1 << (sizeof(T)*8-1)) \
  118. )
  119. #define MININT(T) (T)( \
  120. ((T)-1) > 0 \
  121. ? (T)0 \
  122. : ((T)1 << (sizeof(T)*8-1)) \
  123. )
  124. /* Large file support */
  125. /* Note that CONFIG_LFS=y forces bbox to be built with all common ops
  126. * (stat, lseek etc) mapped to "largefile" variants by libc.
  127. * Practically it means that open() automatically has O_LARGEFILE added
  128. * and all filesize/file_offset parameters and struct members are "large"
  129. * (in today's world - signed 64bit). For full support of large files,
  130. * we need a few helper #defines (below) and careful use of off_t
  131. * instead of int/ssize_t. No lseek64(), O_LARGEFILE etc necessary */
  132. #if ENABLE_LFS
  133. /* CONFIG_LFS is on */
  134. # if ULONG_MAX > 0xffffffff
  135. /* "long" is long enough on this system */
  136. # define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  137. /* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
  138. # define BB_STRTOOFF bb_strtoul
  139. # define STRTOOFF strtoul
  140. /* usage: printf("size: %"OFF_FMT"d (%"OFF_FMT"x)\n", sz, sz); */
  141. # define OFF_FMT "l"
  142. # else
  143. /* "long" is too short, need "long long" */
  144. # define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX)
  145. # define BB_STRTOOFF bb_strtoull
  146. # define STRTOOFF strtoull
  147. # define OFF_FMT "ll"
  148. # endif
  149. #else
  150. /* CONFIG_LFS is off */
  151. # if UINT_MAX == 0xffffffff
  152. /* While sizeof(off_t) == sizeof(int), off_t is typedef'ed to long anyway.
  153. * gcc will throw warnings on printf("%d", off_t). Crap... */
  154. # define XATOOFF(a) xatoi_u(a)
  155. # define BB_STRTOOFF bb_strtou
  156. # define STRTOOFF strtol
  157. # define OFF_FMT "l"
  158. # else
  159. # define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  160. # define BB_STRTOOFF bb_strtoul
  161. # define STRTOOFF strtol
  162. # define OFF_FMT "l"
  163. # endif
  164. #endif
  165. /* scary. better ideas? (but do *test* them first!) */
  166. #define OFF_T_MAX ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
  167. /* Some useful definitions */
  168. #undef FALSE
  169. #define FALSE ((int) 0)
  170. #undef TRUE
  171. #define TRUE ((int) 1)
  172. #undef SKIP
  173. #define SKIP ((int) 2)
  174. /* for mtab.c */
  175. #define MTAB_GETMOUNTPT '1'
  176. #define MTAB_GETDEVICE '2'
  177. #define BUF_SIZE 8192
  178. #define EXPAND_ALLOC 1024
  179. /* Macros for min/max. */
  180. #ifndef MIN
  181. #define MIN(a,b) (((a)<(b))?(a):(b))
  182. #endif
  183. #ifndef MAX
  184. #define MAX(a,b) (((a)>(b))?(a):(b))
  185. #endif
  186. /* buffer allocation schemes */
  187. #if ENABLE_FEATURE_BUFFERS_GO_ON_STACK
  188. #define RESERVE_CONFIG_BUFFER(buffer,len) char buffer[len]
  189. #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
  190. #define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  191. #else
  192. #if ENABLE_FEATURE_BUFFERS_GO_IN_BSS
  193. #define RESERVE_CONFIG_BUFFER(buffer,len) static char buffer[len]
  194. #define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
  195. #define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  196. #else
  197. #define RESERVE_CONFIG_BUFFER(buffer,len) char *buffer = xmalloc(len)
  198. #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer = xmalloc(len)
  199. #define RELEASE_CONFIG_BUFFER(buffer) free(buffer)
  200. #endif
  201. #endif
  202. #if defined(__GLIBC__)
  203. /* glibc uses __errno_location() to get a ptr to errno */
  204. /* We can just memorize it once - no multithreading in busybox :) */
  205. extern int *const bb_errno;
  206. #undef errno
  207. #define errno (*bb_errno)
  208. #endif
  209. unsigned long long monotonic_ns(void) FAST_FUNC;
  210. unsigned long long monotonic_us(void) FAST_FUNC;
  211. unsigned monotonic_sec(void) FAST_FUNC;
  212. extern void chomp(char *s) FAST_FUNC;
  213. extern void trim(char *s) FAST_FUNC;
  214. extern char *skip_whitespace(const char *) FAST_FUNC;
  215. extern char *skip_non_whitespace(const char *) FAST_FUNC;
  216. extern char *strrstr(const char *haystack, const char *needle) FAST_FUNC;
  217. //TODO: supply a pointer to char[11] buffer (avoid statics)?
  218. extern const char *bb_mode_string(mode_t mode) FAST_FUNC;
  219. extern int is_directory(const char *name, int followLinks, struct stat *statBuf) FAST_FUNC;
  220. enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */
  221. FILEUTILS_PRESERVE_STATUS = 1,
  222. FILEUTILS_DEREFERENCE = 2,
  223. FILEUTILS_RECUR = 4,
  224. FILEUTILS_FORCE = 8,
  225. FILEUTILS_INTERACTIVE = 0x10,
  226. FILEUTILS_MAKE_HARDLINK = 0x20,
  227. FILEUTILS_MAKE_SOFTLINK = 0x40,
  228. FILEUTILS_DEREF_SOFTLINK = 0x80,
  229. #if ENABLE_SELINUX
  230. FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x100,
  231. FILEUTILS_SET_SECURITY_CONTEXT = 0x200
  232. #endif
  233. };
  234. #define FILEUTILS_CP_OPTSTR "pdRfilsL" USE_SELINUX("c")
  235. extern int remove_file(const char *path, int flags) FAST_FUNC;
  236. /* NB: without FILEUTILS_RECUR in flags, it will basically "cat"
  237. * the source, not copy (unless "source" is a directory).
  238. * This makes "cp /dev/null file" and "install /dev/null file" (!!!)
  239. * work coreutils-compatibly. */
  240. extern int copy_file(const char *source, const char *dest, int flags) FAST_FUNC;
  241. enum {
  242. ACTION_RECURSE = (1 << 0),
  243. ACTION_FOLLOWLINKS = (1 << 1),
  244. ACTION_FOLLOWLINKS_L0 = (1 << 2),
  245. ACTION_DEPTHFIRST = (1 << 3),
  246. /*ACTION_REVERSE = (1 << 4), - unused */
  247. ACTION_QUIET = (1 << 5),
  248. };
  249. extern int recursive_action(const char *fileName, unsigned flags,
  250. int FAST_FUNC (*fileAction)(const char *fileName, struct stat* statbuf, void* userData, int depth),
  251. int FAST_FUNC (*dirAction)(const char *fileName, struct stat* statbuf, void* userData, int depth),
  252. void* userData, unsigned depth) FAST_FUNC;
  253. extern int device_open(const char *device, int mode) FAST_FUNC;
  254. enum { GETPTY_BUFSIZE = 16 }; /* more than enough for "/dev/ttyXXX" */
  255. extern int xgetpty(char *line) FAST_FUNC;
  256. extern int get_console_fd_or_die(void) FAST_FUNC;
  257. extern void console_make_active(int fd, const int vt_num) FAST_FUNC;
  258. extern char *find_block_device(const char *path) FAST_FUNC;
  259. /* bb_copyfd_XX print read/write errors and return -1 if they occur */
  260. extern off_t bb_copyfd_eof(int fd1, int fd2) FAST_FUNC;
  261. extern off_t bb_copyfd_size(int fd1, int fd2, off_t size) FAST_FUNC;
  262. extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size) FAST_FUNC;
  263. /* "short" copy can be detected by return value < size */
  264. /* this helper yells "short read!" if param is not -1 */
  265. extern void complain_copyfd_and_die(off_t sz) NORETURN FAST_FUNC;
  266. extern char bb_process_escape_sequence(const char **ptr) FAST_FUNC;
  267. /* xxxx_strip version can modify its parameter:
  268. * "/" -> "/"
  269. * "abc" -> "abc"
  270. * "abc/def" -> "def"
  271. * "abc/def/" -> "def" !!
  272. */
  273. extern char *bb_get_last_path_component_strip(char *path) FAST_FUNC;
  274. /* "abc/def/" -> "" and it never modifies 'path' */
  275. extern char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC;
  276. int ndelay_on(int fd) FAST_FUNC;
  277. int ndelay_off(int fd) FAST_FUNC;
  278. int close_on_exec_on(int fd) FAST_FUNC;
  279. void xdup2(int, int) FAST_FUNC;
  280. void xmove_fd(int, int) FAST_FUNC;
  281. DIR *xopendir(const char *path) FAST_FUNC;
  282. DIR *warn_opendir(const char *path) FAST_FUNC;
  283. /* UNUSED: char *xmalloc_realpath(const char *path) FAST_FUNC; */
  284. char *xmalloc_readlink(const char *path) FAST_FUNC;
  285. char *xmalloc_readlink_or_warn(const char *path) FAST_FUNC;
  286. char *xrealloc_getcwd_or_warn(char *cwd) FAST_FUNC;
  287. char *xmalloc_follow_symlinks(const char *path) FAST_FUNC;
  288. enum {
  289. /* bb_signals(BB_FATAL_SIGS, handler) catches all signals which
  290. * otherwise would kill us, except for those resulting from bugs:
  291. * SIGSEGV, SIGILL, SIGFPE.
  292. * Other fatal signals not included (TODO?):
  293. * SIGBUS Bus error (bad memory access)
  294. * SIGPOLL Pollable event. Synonym of SIGIO
  295. * SIGPROF Profiling timer expired
  296. * SIGSYS Bad argument to routine
  297. * SIGTRAP Trace/breakpoint trap
  298. *
  299. * The only known arch with some of these sigs not fitting
  300. * into 32 bits is parisc (SIGXCPU=33, SIGXFSZ=34, SIGSTKFLT=36).
  301. * Dance around with long long to guard against that...
  302. */
  303. BB_FATAL_SIGS = (int)(0
  304. + (1LL << SIGHUP)
  305. + (1LL << SIGINT)
  306. + (1LL << SIGTERM)
  307. + (1LL << SIGPIPE) // Write to pipe with no readers
  308. + (1LL << SIGQUIT) // Quit from keyboard
  309. + (1LL << SIGABRT) // Abort signal from abort(3)
  310. + (1LL << SIGALRM) // Timer signal from alarm(2)
  311. + (1LL << SIGVTALRM) // Virtual alarm clock
  312. + (1LL << SIGXCPU) // CPU time limit exceeded
  313. + (1LL << SIGXFSZ) // File size limit exceeded
  314. + (1LL << SIGUSR1) // Yes kids, these are also fatal!
  315. + (1LL << SIGUSR2)
  316. + 0),
  317. };
  318. void bb_signals(int sigs, void (*f)(int)) FAST_FUNC;
  319. /* Unlike signal() and bb_signals, sets handler with sigaction()
  320. * and in a way that while signal handler is run, no other signals
  321. * will be blocked: */
  322. void bb_signals_recursive(int sigs, void (*f)(int)) FAST_FUNC;
  323. /* syscalls like read() will be interrupted with EINTR: */
  324. void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int)) FAST_FUNC;
  325. /* syscalls like read() won't be interrupted (though select/poll will be): */
  326. void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int)) FAST_FUNC;
  327. void wait_for_any_sig(void) FAST_FUNC;
  328. void kill_myself_with_sig(int sig) NORETURN FAST_FUNC;
  329. void sig_block(int sig) FAST_FUNC;
  330. void sig_unblock(int sig) FAST_FUNC;
  331. /* Will do sigaction(signum, act, NULL): */
  332. int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
  333. /* SIG_BLOCK/SIG_UNBLOCK all signals: */
  334. int sigprocmask_allsigs(int how) FAST_FUNC;
  335. /* Standard handler which just records signo */
  336. extern smallint bb_got_signal;
  337. void record_signo(int signo); /* not FAST_FUNC! */
  338. void xsetgid(gid_t gid) FAST_FUNC;
  339. void xsetuid(uid_t uid) FAST_FUNC;
  340. void xchdir(const char *path) FAST_FUNC;
  341. void xchroot(const char *path) FAST_FUNC;
  342. void xsetenv(const char *key, const char *value) FAST_FUNC;
  343. void xunlink(const char *pathname) FAST_FUNC;
  344. void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
  345. int xopen(const char *pathname, int flags) FAST_FUNC FAST_FUNC;
  346. int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
  347. int open_or_warn(const char *pathname, int flags) FAST_FUNC;
  348. int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
  349. int open_or_warn_stdin(const char *pathname) FAST_FUNC;
  350. void xrename(const char *oldpath, const char *newpath) FAST_FUNC;
  351. int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC;
  352. off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC;
  353. off_t fdlength(int fd) FAST_FUNC;
  354. void xpipe(int filedes[2]) FAST_FUNC;
  355. /* In this form code with pipes is much more readable */
  356. struct fd_pair { int rd; int wr; };
  357. #define piped_pair(pair) pipe(&((pair).rd))
  358. #define xpiped_pair(pair) xpipe(&((pair).rd))
  359. /* Useful for having small structure members/global variables */
  360. typedef int8_t socktype_t;
  361. typedef int8_t family_t;
  362. struct BUG_too_small {
  363. char BUG_socktype_t_too_small[(0
  364. | SOCK_STREAM
  365. | SOCK_DGRAM
  366. | SOCK_RDM
  367. | SOCK_SEQPACKET
  368. | SOCK_RAW
  369. ) <= 127 ? 1 : -1];
  370. char BUG_family_t_too_small[(0
  371. | AF_UNSPEC
  372. | AF_INET
  373. | AF_INET6
  374. | AF_UNIX
  375. #ifdef AF_PACKET
  376. | AF_PACKET
  377. #endif
  378. #ifdef AF_NETLINK
  379. | AF_NETLINK
  380. #endif
  381. /* | AF_DECnet */
  382. /* | AF_IPX */
  383. ) <= 127 ? 1 : -1];
  384. };
  385. int xsocket(int domain, int type, int protocol) FAST_FUNC;
  386. void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
  387. void xlisten(int s, int backlog) FAST_FUNC;
  388. void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC;
  389. ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  390. socklen_t tolen) FAST_FUNC;
  391. /* SO_REUSEADDR allows a server to rebind to an address that is already
  392. * "in use" by old connections to e.g. previous server instance which is
  393. * killed or crashed. Without it bind will fail until all such connections
  394. * time out. Linux does not allow multiple live binds on same ip:port
  395. * regardless of SO_REUSEADDR (unlike some other flavors of Unix).
  396. * Turn it on before you call bind(). */
  397. void setsockopt_reuseaddr(int fd) FAST_FUNC; /* On Linux this never fails. */
  398. int setsockopt_broadcast(int fd) FAST_FUNC;
  399. int setsockopt_bindtodevice(int fd, const char *iface) FAST_FUNC;
  400. /* NB: returns port in host byte order */
  401. unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) FAST_FUNC;
  402. typedef struct len_and_sockaddr {
  403. socklen_t len;
  404. union {
  405. struct sockaddr sa;
  406. struct sockaddr_in sin;
  407. #if ENABLE_FEATURE_IPV6
  408. struct sockaddr_in6 sin6;
  409. #endif
  410. } u;
  411. } len_and_sockaddr;
  412. enum {
  413. LSA_LEN_SIZE = offsetof(len_and_sockaddr, u),
  414. LSA_SIZEOF_SA = sizeof(
  415. union {
  416. struct sockaddr sa;
  417. struct sockaddr_in sin;
  418. #if ENABLE_FEATURE_IPV6
  419. struct sockaddr_in6 sin6;
  420. #endif
  421. }
  422. )
  423. };
  424. /* Create stream socket, and allocate suitable lsa.
  425. * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6))
  426. * af == AF_UNSPEC will result in trying to create IPv6 socket,
  427. * and if kernel doesn't support it, IPv4.
  428. */
  429. #if ENABLE_FEATURE_IPV6
  430. int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type) FAST_FUNC;
  431. #else
  432. int xsocket_type(len_and_sockaddr **lsap, int sock_type) FAST_FUNC;
  433. #define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type))
  434. #endif
  435. int xsocket_stream(len_and_sockaddr **lsap) FAST_FUNC;
  436. /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  437. * numeric IP ("N.N.N.N") or numeric IPv6 address,
  438. * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  439. * Only if there is no suffix, port argument is used */
  440. /* NB: these set SO_REUSEADDR before bind */
  441. int create_and_bind_stream_or_die(const char *bindaddr, int port) FAST_FUNC;
  442. int create_and_bind_dgram_or_die(const char *bindaddr, int port) FAST_FUNC;
  443. /* Create client TCP socket connected to peer:port. Peer cannot be NULL.
  444. * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname,
  445. * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  446. * If there is no suffix, port argument is used */
  447. int create_and_connect_stream_or_die(const char *peer, int port) FAST_FUNC;
  448. /* Connect to peer identified by lsa */
  449. int xconnect_stream(const len_and_sockaddr *lsa) FAST_FUNC;
  450. /* Return malloc'ed len_and_sockaddr with socket address of host:port
  451. * Currently will return IPv4 or IPv6 sockaddrs only
  452. * (depending on host), but in theory nothing prevents e.g.
  453. * UNIX socket address being returned, IPX sockaddr etc...
  454. * On error does bb_error_msg and returns NULL */
  455. len_and_sockaddr* host2sockaddr(const char *host, int port) FAST_FUNC;
  456. /* Version which dies on error */
  457. len_and_sockaddr* xhost2sockaddr(const char *host, int port) FAST_FUNC;
  458. len_and_sockaddr* xdotted2sockaddr(const char *host, int port) FAST_FUNC;
  459. /* Same, useful if you want to force family (e.g. IPv6) */
  460. #if !ENABLE_FEATURE_IPV6
  461. #define host_and_af2sockaddr(host, port, af) host2sockaddr((host), (port))
  462. #define xhost_and_af2sockaddr(host, port, af) xhost2sockaddr((host), (port))
  463. #else
  464. len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af) FAST_FUNC;
  465. len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af) FAST_FUNC;
  466. #endif
  467. /* Assign sin[6]_port member if the socket is an AF_INET[6] one,
  468. * otherwise no-op. Useful for ftp.
  469. * NB: does NOT do htons() internally, just direct assignment. */
  470. void set_nport(len_and_sockaddr *lsa, unsigned port) FAST_FUNC;
  471. /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */
  472. int get_nport(const struct sockaddr *sa) FAST_FUNC;
  473. /* Reverse DNS. Returns NULL on failure. */
  474. char* xmalloc_sockaddr2host(const struct sockaddr *sa) FAST_FUNC;
  475. /* This one doesn't append :PORTNUM */
  476. char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa) FAST_FUNC;
  477. /* This one also doesn't fall back to dotted IP (returns NULL) */
  478. char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) FAST_FUNC;
  479. /* inet_[ap]ton on steroids */
  480. char* xmalloc_sockaddr2dotted(const struct sockaddr *sa) FAST_FUNC;
  481. char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) FAST_FUNC;
  482. // "old" (ipv4 only) API
  483. // users: traceroute.c hostname.c - use _list_ of all IPs
  484. struct hostent *xgethostbyname(const char *name) FAST_FUNC;
  485. // Also mount.c and inetd.c are using gethostbyname(),
  486. // + inet_common.c has additional IPv4-only stuff
  487. void socket_want_pktinfo(int fd) FAST_FUNC;
  488. ssize_t send_to_from(int fd, void *buf, size_t len, int flags,
  489. const struct sockaddr *to,
  490. const struct sockaddr *from,
  491. socklen_t tolen) FAST_FUNC;
  492. ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
  493. struct sockaddr *from,
  494. struct sockaddr *to,
  495. socklen_t sa_size) FAST_FUNC;
  496. char *xstrdup(const char *s) FAST_FUNC;
  497. char *xstrndup(const char *s, int n) FAST_FUNC;
  498. void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
  499. char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
  500. /* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
  501. * But potentially slow, don't use in one-billion-times loops */
  502. int bb_putchar(int ch) FAST_FUNC;
  503. char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  504. /* Prints unprintable chars ch as ^C or M-c to file
  505. * (M-c is used only if ch is ORed with PRINTABLE_META),
  506. * else it is printed as-is (except for ch = 0x9b) */
  507. enum { PRINTABLE_META = 0x100 };
  508. void fputc_printable(int ch, FILE *file) FAST_FUNC;
  509. // gcc-4.1.1 still isn't good enough at optimizing it
  510. // (+200 bytes compared to macro)
  511. //static ALWAYS_INLINE
  512. //int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; }
  513. //static ALWAYS_INLINE
  514. //int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; }
  515. #define LONE_DASH(s) ((s)[0] == '-' && !(s)[1])
  516. #define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
  517. #define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
  518. #define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
  519. #define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
  520. /* dmalloc will redefine these to it's own implementation. It is safe
  521. * to have the prototypes here unconditionally. */
  522. void *malloc_or_warn(size_t size) FAST_FUNC;
  523. void *xmalloc(size_t size) FAST_FUNC;
  524. void *xzalloc(size_t size) FAST_FUNC;
  525. void *xrealloc(void *old, size_t size) FAST_FUNC;
  526. /* After xrealloc_vector(v, 4, idx) it's ok to use
  527. * at least v[idx] and v[idx+1], for all idx values.
  528. * shift specifies how many new elements are added (1: 2, 2: 4... 8: 256...)
  529. * when all elements are used up. New elements are zeroed out. */
  530. #define xrealloc_vector(vector, shift, idx) \
  531. xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx))
  532. void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC;
  533. extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC;
  534. extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
  535. // NB: will return short read on error, not -1,
  536. // if some data was read before error occurred
  537. extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
  538. extern void xread(int fd, void *buf, size_t count) FAST_FUNC;
  539. extern unsigned char xread_char(int fd) FAST_FUNC;
  540. extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
  541. extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
  542. // Reads one line a-la fgets (but doesn't save terminating '\n').
  543. // Reads byte-by-byte. Useful when it is important to not read ahead.
  544. // Bytes are appended to pfx (which must be malloced, or NULL).
  545. extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
  546. /* Reads block up to *maxsz_p (default: MAX_INT(ssize_t)) */
  547. extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC;
  548. /* Returns NULL if file can't be opened */
  549. extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
  550. /* Autodetects .gz etc */
  551. extern int open_zipped(const char *fname) FAST_FUNC;
  552. extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC;
  553. /* Never returns NULL */
  554. extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
  555. extern ssize_t safe_write(int fd, const void *buf, size_t count) FAST_FUNC;
  556. // NB: will return short write on error, not -1,
  557. // if some data was written before error occurred
  558. extern ssize_t full_write(int fd, const void *buf, size_t count) FAST_FUNC;
  559. extern void xwrite(int fd, const void *buf, size_t count) FAST_FUNC;
  560. extern void xopen_xwrite_close(const char* file, const char *str) FAST_FUNC;
  561. /* Reads and prints to stdout till eof, then closes FILE. Exits on error: */
  562. extern void xprint_and_close_file(FILE *file) FAST_FUNC;
  563. extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC;
  564. extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC;
  565. /* Reads up to (and including) TERMINATING_STRING: */
  566. extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC;
  567. /* Chops off TERMINATING_STRING from the end: */
  568. extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string) FAST_FUNC;
  569. /* Reads up to (and including) "\n" or NUL byte: */
  570. extern char *xmalloc_fgets(FILE *file) FAST_FUNC;
  571. /* Chops off '\n' from the end, unlike fgets: */
  572. extern char *xmalloc_fgetline(FILE *file) FAST_FUNC;
  573. /* Same, but doesn't try to conserve space (may have some slack after the end) */
  574. /* extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC; */
  575. extern void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
  576. extern void die_if_ferror_stdout(void) FAST_FUNC;
  577. extern void xfflush_stdout(void) FAST_FUNC;
  578. extern void fflush_stdout_and_exit(int retval) NORETURN FAST_FUNC;
  579. extern int fclose_if_not_stdin(FILE *file) FAST_FUNC;
  580. extern FILE *xfopen(const char *filename, const char *mode) FAST_FUNC;
  581. /* Prints warning to stderr and returns NULL on failure: */
  582. extern FILE *fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
  583. /* "Opens" stdin if filename is special, else just opens file: */
  584. extern FILE *xfopen_stdin(const char *filename) FAST_FUNC;
  585. extern FILE *fopen_or_warn_stdin(const char *filename) FAST_FUNC;
  586. extern FILE* fopen_for_read(const char *path) FAST_FUNC;
  587. extern FILE* xfopen_for_read(const char *path) FAST_FUNC;
  588. extern FILE* fopen_for_write(const char *path) FAST_FUNC;
  589. extern FILE* xfopen_for_write(const char *path) FAST_FUNC;
  590. int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
  591. void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
  592. /* Wrapper which restarts poll on EINTR or ENOMEM.
  593. * On other errors complains [perror("poll")] and returns.
  594. * Warning! May take (much) longer than timeout_ms to return!
  595. * If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */
  596. int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms) FAST_FUNC;
  597. char *safe_gethostname(void) FAST_FUNC;
  598. char *safe_getdomainname(void) FAST_FUNC;
  599. /* Convert each alpha char in str to lower-case */
  600. char* str_tolower(char *str) FAST_FUNC;
  601. char *utoa(unsigned n) FAST_FUNC;
  602. char *itoa(int n) FAST_FUNC;
  603. /* Returns a pointer past the formatted number, does NOT null-terminate */
  604. char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) FAST_FUNC;
  605. char *itoa_to_buf(int n, char *buf, unsigned buflen) FAST_FUNC;
  606. /* Intelligent formatters of bignums */
  607. void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
  608. void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
  609. //TODO: provide pointer to buf (avoid statics)?
  610. const char *make_human_readable_str(unsigned long long size,
  611. unsigned long block_size, unsigned long display_unit) FAST_FUNC;
  612. /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
  613. char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC;
  614. /* Last element is marked by mult == 0 */
  615. struct suffix_mult {
  616. char suffix[4];
  617. unsigned mult;
  618. };
  619. #include "xatonum.h"
  620. /* Specialized: */
  621. /* Using xatoi() instead of naive atoi() is not always convenient -
  622. * in many places people want *non-negative* values, but store them
  623. * in signed int. Therefore we need this one:
  624. * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
  625. int xatoi_u(const char *numstr) FAST_FUNC;
  626. /* Useful for reading port numbers */
  627. uint16_t xatou16(const char *numstr) FAST_FUNC;
  628. /* These parse entries in /etc/passwd and /etc/group. This is desirable
  629. * for BusyBox since we want to avoid using the glibc NSS stuff, which
  630. * increases target size and is often not needed on embedded systems. */
  631. long xuname2uid(const char *name) FAST_FUNC;
  632. long xgroup2gid(const char *name) FAST_FUNC;
  633. /* wrapper: allows string to contain numeric uid or gid */
  634. unsigned long get_ug_id(const char *s, long FAST_FUNC (*xname2id)(const char *)) FAST_FUNC;
  635. /* from chpst. Does not die, returns 0 on failure */
  636. struct bb_uidgid_t {
  637. uid_t uid;
  638. gid_t gid;
  639. };
  640. /* always sets uid and gid */
  641. int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC;
  642. /* always sets uid and gid, allows numeric; exits on failure */
  643. void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC;
  644. /* chown-like handling of "user[:[group]" */
  645. void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC;
  646. /* bb_getpwuid, bb_getgrgid:
  647. * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
  648. * as a string to buf, return user/group name or NULL
  649. * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
  650. * bb_getXXXid(NULL, -1, id) - return user/group name or exit
  651. */
  652. char *bb_getpwuid(char *name, int bufsize, long uid) FAST_FUNC;
  653. char *bb_getgrgid(char *group, int bufsize, long gid) FAST_FUNC;
  654. /* versions which cache results (useful for ps, ls etc) */
  655. const char* get_cached_username(uid_t uid) FAST_FUNC;
  656. const char* get_cached_groupname(gid_t gid) FAST_FUNC;
  657. void clear_username_cache(void) FAST_FUNC;
  658. /* internally usernames are saved in fixed-sized char[] buffers */
  659. enum { USERNAME_MAX_SIZE = 16 - sizeof(int) };
  660. #if ENABLE_FEATURE_CHECK_NAMES
  661. void die_if_bad_username(const char* name) FAST_FUNC;
  662. #else
  663. #define die_if_bad_username(name) ((void)(name))
  664. #endif
  665. int execable_file(const char *name) FAST_FUNC;
  666. char *find_execable(const char *filename, char **PATHp) FAST_FUNC;
  667. int exists_execable(const char *filename) FAST_FUNC;
  668. /* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),
  669. * but it may exec busybox and call applet instead of searching PATH.
  670. */
  671. #if ENABLE_FEATURE_PREFER_APPLETS
  672. int bb_execvp(const char *file, char *const argv[]) FAST_FUNC;
  673. #define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
  674. #define BB_EXECLP(prog,cmd,...) \
  675. execlp((find_applet_by_name(prog) >= 0) ? CONFIG_BUSYBOX_EXEC_PATH : prog, \
  676. cmd, __VA_ARGS__)
  677. #else
  678. #define BB_EXECVP(prog,cmd) execvp(prog,cmd)
  679. #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__)
  680. #endif
  681. /* NOMMU friendy fork+exec */
  682. pid_t spawn(char **argv) FAST_FUNC;
  683. pid_t xspawn(char **argv) FAST_FUNC;
  684. pid_t safe_waitpid(pid_t pid, int *wstat, int options) FAST_FUNC;
  685. /* Unlike waitpid, waits ONLY for one process.
  686. * It's safe to pass negative 'pids' from failed [v]fork -
  687. * wait4pid will return -1 (and will not clobber [v]fork's errno).
  688. * IOW: rc = wait4pid(spawn(argv));
  689. * if (rc < 0) bb_perror_msg("%s", argv[0]);
  690. * if (rc > 0) bb_error_msg("exit code: %d", rc);
  691. */
  692. int wait4pid(pid_t pid) FAST_FUNC;
  693. pid_t wait_any_nohang(int *wstat) FAST_FUNC;
  694. #define wait_crashed(w) ((w) & 127)
  695. #define wait_exitcode(w) ((w) >> 8)
  696. #define wait_stopsig(w) ((w) >> 8)
  697. #define wait_stopped(w) (((w) & 127) == 127)
  698. /* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */
  699. pid_t spawn_and_wait(char **argv) FAST_FUNC;
  700. struct nofork_save_area {
  701. jmp_buf die_jmp;
  702. const char *applet_name;
  703. int xfunc_error_retval;
  704. uint32_t option_mask32;
  705. int die_sleep;
  706. smallint saved;
  707. };
  708. void save_nofork_data(struct nofork_save_area *save) FAST_FUNC;
  709. void restore_nofork_data(struct nofork_save_area *save) FAST_FUNC;
  710. /* Does NOT check that applet is NOFORK, just blindly runs it */
  711. int run_nofork_applet(int applet_no, char **argv) FAST_FUNC;
  712. int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv) FAST_FUNC;
  713. /* Helpers for daemonization.
  714. *
  715. * bb_daemonize(flags) = daemonize, does not compile on NOMMU
  716. *
  717. * bb_daemonize_or_rexec(flags, argv) = daemonizes on MMU (and ignores argv),
  718. * rexec's itself on NOMMU with argv passed as command line.
  719. * Thus bb_daemonize_or_rexec may cause your <applet>_main() to be re-executed
  720. * from the start. (It will detect it and not reexec again second time).
  721. * You have to audit carefully that you don't do something twice as a result
  722. * (opening files/sockets, parsing config files etc...)!
  723. *
  724. * Both of the above will redirect fd 0,1,2 to /dev/null and drop ctty
  725. * (will do setsid()).
  726. *
  727. * forkexit_or_rexec(argv) = bare-bones "fork + parent exits" on MMU,
  728. * "vfork + re-exec ourself" on NOMMU. No fd redirection, no setsid().
  729. * Currently used for setsid only. On MMU ignores argv.
  730. *
  731. * Helper for network daemons in foreground mode:
  732. *
  733. * bb_sanitize_stdio() = make sure that fd 0,1,2 are opened by opening them
  734. * to /dev/null if they are not.
  735. */
  736. enum {
  737. DAEMON_CHDIR_ROOT = 1,
  738. DAEMON_DEVNULL_STDIO = 2,
  739. DAEMON_CLOSE_EXTRA_FDS = 4,
  740. DAEMON_ONLY_SANITIZE = 8, /* internal use */
  741. };
  742. #if BB_MMU
  743. void forkexit_or_rexec(void) FAST_FUNC;
  744. enum { re_execed = 0 };
  745. # define forkexit_or_rexec(argv) forkexit_or_rexec()
  746. # define bb_daemonize_or_rexec(flags, argv) bb_daemonize_or_rexec(flags)
  747. # define bb_daemonize(flags) bb_daemonize_or_rexec(flags, bogus)
  748. #else
  749. void re_exec(char **argv) NORETURN FAST_FUNC;
  750. void forkexit_or_rexec(char **argv) FAST_FUNC;
  751. extern bool re_execed;
  752. int BUG_fork_is_unavailable_on_nommu(void) FAST_FUNC;
  753. int BUG_daemon_is_unavailable_on_nommu(void) FAST_FUNC;
  754. void BUG_bb_daemonize_is_unavailable_on_nommu(void) FAST_FUNC;
  755. # define fork() BUG_fork_is_unavailable_on_nommu()
  756. # define daemon(a,b) BUG_daemon_is_unavailable_on_nommu()
  757. # define bb_daemonize(a) BUG_bb_daemonize_is_unavailable_on_nommu()
  758. #endif
  759. void bb_daemonize_or_rexec(int flags, char **argv) FAST_FUNC;
  760. void bb_sanitize_stdio(void) FAST_FUNC;
  761. /* Clear dangerous stuff, set PATH. Return 1 if was run by different user. */
  762. int sanitize_env_if_suid(void) FAST_FUNC;
  763. extern const char *const bb_argv_dash[]; /* "-", NULL */
  764. extern const char *opt_complementary;
  765. #if ENABLE_GETOPT_LONG
  766. #define No_argument "\0"
  767. #define Required_argument "\001"
  768. #define Optional_argument "\002"
  769. extern const char *applet_long_options;
  770. #endif
  771. extern uint32_t option_mask32;
  772. extern uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC;
  773. typedef struct llist_t {
  774. char *data;
  775. struct llist_t *link;
  776. } llist_t;
  777. void llist_add_to(llist_t **old_head, void *data) FAST_FUNC;
  778. void llist_add_to_end(llist_t **list_head, void *data) FAST_FUNC;
  779. void *llist_pop(llist_t **elm) FAST_FUNC;
  780. void llist_unlink(llist_t **head, llist_t *elm) FAST_FUNC;
  781. void llist_free(llist_t *elm, void (*freeit)(void *data)) FAST_FUNC;
  782. llist_t *llist_rev(llist_t *list) FAST_FUNC;
  783. /* BTW, surprisingly, changing API to
  784. * llist_t *llist_add_to(llist_t *old_head, void *data)
  785. * etc does not result in smaller code... */
  786. /* start_stop_daemon and udhcpc are special - they want
  787. * to create pidfiles regardless of FEATURE_PIDFILE */
  788. #if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
  789. /* True only if we created pidfile which is *file*, not /dev/null etc */
  790. extern smallint wrote_pidfile;
  791. void write_pidfile(const char *path) FAST_FUNC;
  792. #define remove_pidfile(path) do { if (wrote_pidfile) unlink(path); } while (0)
  793. #else
  794. enum { wrote_pidfile = 0 };
  795. #define write_pidfile(path) ((void)0)
  796. #define remove_pidfile(path) ((void)0)
  797. #endif
  798. enum {
  799. LOGMODE_NONE = 0,
  800. LOGMODE_STDIO = (1 << 0),
  801. LOGMODE_SYSLOG = (1 << 1) * ENABLE_FEATURE_SYSLOG,
  802. LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
  803. };
  804. extern const char *msg_eol;
  805. extern smallint logmode;
  806. extern int die_sleep;
  807. extern int xfunc_error_retval;
  808. extern jmp_buf die_jmp;
  809. extern void xfunc_die(void) NORETURN FAST_FUNC;
  810. extern void bb_show_usage(void) NORETURN FAST_FUNC;
  811. extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  812. extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  813. extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  814. extern void bb_simple_perror_msg(const char *s) FAST_FUNC;
  815. extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  816. extern void bb_simple_perror_msg_and_die(const char *s) __attribute__ ((noreturn)) FAST_FUNC;
  817. extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  818. extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
  819. extern void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC;
  820. extern void bb_perror_nomsg(void) FAST_FUNC;
  821. extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
  822. extern void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC;
  823. /* We need to export XXX_main from libbusybox
  824. * only if we build "individual" binaries
  825. */
  826. #if ENABLE_FEATURE_INDIVIDUAL
  827. #define MAIN_EXTERNALLY_VISIBLE EXTERNALLY_VISIBLE
  828. #else
  829. #define MAIN_EXTERNALLY_VISIBLE
  830. #endif
  831. /* Applets which are useful from another applets */
  832. int bb_cat(char** argv);
  833. /* If shell needs them, they exist even if not enabled as applets */
  834. int echo_main(int argc, char** argv) USE_ECHO(MAIN_EXTERNALLY_VISIBLE);
  835. int printf_main(int argc, char **argv) USE_PRINTF(MAIN_EXTERNALLY_VISIBLE);
  836. int test_main(int argc, char **argv) USE_TEST(MAIN_EXTERNALLY_VISIBLE);
  837. int kill_main(int argc, char **argv) USE_KILL(MAIN_EXTERNALLY_VISIBLE);
  838. /* Similar, but used by chgrp, not shell */
  839. int chown_main(int argc, char **argv) USE_CHOWN(MAIN_EXTERNALLY_VISIBLE);
  840. /* Don't need USE_xxx() guard for these */
  841. int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  842. int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  843. #if ENABLE_ROUTE
  844. void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
  845. #endif
  846. /* "Keycodes" that report an escape sequence.
  847. * We use something which fits into signed char,
  848. * yet doesn't represent any valid Unicode characher.
  849. * Also, -1 is reserved for error indication and we don't use it. */
  850. enum {
  851. KEYCODE_UP = -2,
  852. KEYCODE_DOWN = -3,
  853. KEYCODE_RIGHT = -4,
  854. KEYCODE_LEFT = -5,
  855. KEYCODE_HOME = -6,
  856. KEYCODE_END = -7,
  857. KEYCODE_INSERT = -8,
  858. KEYCODE_DELETE = -9,
  859. KEYCODE_PAGEUP = -10,
  860. KEYCODE_PAGEDOWN = -11,
  861. #if 0
  862. KEYCODE_FUN1 = -12,
  863. KEYCODE_FUN2 = -13,
  864. KEYCODE_FUN3 = -14,
  865. KEYCODE_FUN4 = -15,
  866. KEYCODE_FUN5 = -16,
  867. KEYCODE_FUN6 = -17,
  868. KEYCODE_FUN7 = -18,
  869. KEYCODE_FUN8 = -19,
  870. KEYCODE_FUN9 = -20,
  871. KEYCODE_FUN10 = -21,
  872. KEYCODE_FUN11 = -22,
  873. KEYCODE_FUN12 = -23,
  874. #endif
  875. /* How long the longest ESC sequence we know? */
  876. KEYCODE_BUFFER_SIZE = 4
  877. };
  878. int read_key(int fd, smalluint *nbuffered, char *buffer) FAST_FUNC;
  879. /* Networking */
  880. int create_icmp_socket(void) FAST_FUNC;
  881. int create_icmp6_socket(void) FAST_FUNC;
  882. /* interface.c */
  883. /* This structure defines protocol families and their handlers. */
  884. struct aftype {
  885. const char *name;
  886. const char *title;
  887. int af;
  888. int alen;
  889. char* FAST_FUNC (*print)(unsigned char *);
  890. const char* FAST_FUNC (*sprint)(struct sockaddr *, int numeric);
  891. int FAST_FUNC (*input)(/*int type,*/ const char *bufp, struct sockaddr *);
  892. void FAST_FUNC (*herror)(char *text);
  893. int FAST_FUNC (*rprint)(int options);
  894. int FAST_FUNC (*rinput)(int typ, int ext, char **argv);
  895. /* may modify src */
  896. int FAST_FUNC (*getmask)(char *src, struct sockaddr *mask, char *name);
  897. };
  898. /* This structure defines hardware protocols and their handlers. */
  899. struct hwtype {
  900. const char *name;
  901. const char *title;
  902. int type;
  903. int alen;
  904. char* FAST_FUNC (*print)(unsigned char *);
  905. int FAST_FUNC (*input)(const char *, struct sockaddr *);
  906. int FAST_FUNC (*activate)(int fd);
  907. int suppress_null_addr;
  908. };
  909. extern smallint interface_opt_a;
  910. int display_interfaces(char *ifname) FAST_FUNC;
  911. #if ENABLE_FEATURE_HWIB
  912. int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC;
  913. #else
  914. #define in_ib(a, b) 1 /* fail */
  915. #endif
  916. const struct aftype *get_aftype(const char *name) FAST_FUNC;
  917. const struct hwtype *get_hwtype(const char *name) FAST_FUNC;
  918. const struct hwtype *get_hwntype(int type) FAST_FUNC;
  919. #ifndef BUILD_INDIVIDUAL
  920. extern int find_applet_by_name(const char *name) FAST_FUNC;
  921. /* Returns only if applet is not found. */
  922. extern void run_applet_and_exit(const char *name, char **argv) FAST_FUNC;
  923. extern void run_applet_no_and_exit(int a, char **argv) NORETURN FAST_FUNC;
  924. #endif
  925. #ifdef HAVE_MNTENT_H
  926. extern int match_fstype(const struct mntent *mt, const char *fstypes) FAST_FUNC;
  927. extern struct mntent *find_mount_point(const char *name, const char *table) FAST_FUNC;
  928. #endif
  929. extern void erase_mtab(const char * name) FAST_FUNC;
  930. extern unsigned int tty_baud_to_value(speed_t speed) FAST_FUNC;
  931. extern speed_t tty_value_to_baud(unsigned int value) FAST_FUNC;
  932. extern void bb_warn_ignoring_args(int n) FAST_FUNC;
  933. extern int get_linux_version_code(void) FAST_FUNC;
  934. extern char *query_loop(const char *device) FAST_FUNC;
  935. extern int del_loop(const char *device) FAST_FUNC;
  936. /* If *devname is not NULL, use that name, otherwise try to find free one,
  937. * malloc and return it in *devname.
  938. * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */
  939. extern int set_loop(char **devname, const char *file, unsigned long long offset) FAST_FUNC;
  940. //TODO: pass buf pointer or return allocated buf (avoid statics)?
  941. char *bb_askpass(int timeout, const char * prompt) FAST_FUNC;
  942. int bb_ask_confirmation(void) FAST_FUNC;
  943. int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC;
  944. /*
  945. * Config file parser
  946. */
  947. enum {
  948. PARSE_COLLAPSE = 0x00010000, // treat consecutive delimiters as one
  949. PARSE_TRIM = 0x00020000, // trim leading and trailing delimiters
  950. // TODO: COLLAPSE and TRIM seem to always go in pair
  951. PARSE_GREEDY = 0x00040000, // last token takes entire remainder of the line
  952. PARSE_MIN_DIE = 0x00100000, // die if < min tokens found
  953. // keep a copy of current line
  954. PARSE_KEEP_COPY = 0x00200000 * ENABLE_FEATURE_CROND_D,
  955. // PARSE_ESCAPE = 0x00400000, // process escape sequences in tokens
  956. // NORMAL is:
  957. // * remove leading and trailing delimiters and collapse
  958. // multiple delimiters into one
  959. // * warn and continue if less than mintokens delimiters found
  960. // * grab everything into last token
  961. PARSE_NORMAL = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY,
  962. };
  963. typedef struct parser_t {
  964. FILE *fp;
  965. char *line;
  966. char *data;
  967. int lineno;
  968. } parser_t;
  969. parser_t* config_open(const char *filename) FAST_FUNC;
  970. parser_t* config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) FAST_FUNC;
  971. int config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) FAST_FUNC;
  972. #define config_read(parser, tokens, max, min, str, flags) \
  973. config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str)
  974. void config_close(parser_t *parser) FAST_FUNC;
  975. /* Concatenate path and filename to new allocated buffer.
  976. * Add "/" only as needed (no duplicate "//" are produced).
  977. * If path is NULL, it is assumed to be "/".
  978. * filename should not be NULL. */
  979. char *concat_path_file(const char *path, const char *filename) FAST_FUNC;
  980. char *concat_subpath_file(const char *path, const char *filename) FAST_FUNC;
  981. const char *bb_basename(const char *name) FAST_FUNC;
  982. /* NB: can violate const-ness (similarly to strchr) */
  983. char *last_char_is(const char *s, int c) FAST_FUNC;
  984. int bb_make_directory(char *path, long mode, int flags) FAST_FUNC;
  985. int get_signum(const char *name) FAST_FUNC;
  986. const char *get_signame(int number) FAST_FUNC;
  987. void print_signames(void) FAST_FUNC;
  988. char *bb_simplify_path(const char *path) FAST_FUNC;
  989. #define FAIL_DELAY 3
  990. extern void bb_do_delay(int seconds) FAST_FUNC;
  991. extern void change_identity(const struct passwd *pw) FAST_FUNC;
  992. extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC;
  993. extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) FAST_FUNC;
  994. #if ENABLE_SELINUX
  995. extern void renew_current_security_context(void) FAST_FUNC;
  996. extern void set_current_security_context(security_context_t sid) FAST_FUNC;
  997. extern context_t set_security_context_component(security_context_t cur_context,
  998. char *user, char *role, char *type, char *range) FAST_FUNC;
  999. extern void setfscreatecon_or_die(security_context_t scontext) FAST_FUNC;
  1000. extern void selinux_preserve_fcontext(int fdesc) FAST_FUNC;
  1001. #else
  1002. #define selinux_preserve_fcontext(fdesc) ((void)0)
  1003. #endif
  1004. extern void selinux_or_die(void) FAST_FUNC;
  1005. extern int restricted_shell(const char *shell) FAST_FUNC;
  1006. /* setup_environment:
  1007. * if clear_env = 1: cd(pw->pw_dir), clear environment, then set
  1008. * TERM=(old value)
  1009. * USER=pw->pw_name, LOGNAME=pw->pw_name
  1010. * PATH=bb_default_[root_]path
  1011. * HOME=pw->pw_dir
  1012. * SHELL=shell
  1013. * else if change_env = 1:
  1014. * if not root (if pw->pw_uid != 0):
  1015. * USER=pw->pw_name, LOGNAME=pw->pw_name
  1016. * HOME=pw->pw_dir
  1017. * SHELL=shell
  1018. * else does nothing
  1019. */
  1020. extern void setup_environment(const char *shell, int clear_env, int change_env, const struct passwd *pw) FAST_FUNC;
  1021. extern int correct_password(const struct passwd *pw) FAST_FUNC;
  1022. /* Returns a malloced string */
  1023. #if !ENABLE_USE_BB_CRYPT
  1024. #define pw_encrypt(clear, salt, cleanup) pw_encrypt(clear, salt)
  1025. #endif
  1026. extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_FUNC;
  1027. extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
  1028. /* rnd is additional random input. New one is returned.
  1029. * Useful if you call crypt_make_salt many times in a row:
  1030. * rnd = crypt_make_salt(buf1, 4, 0);
  1031. * rnd = crypt_make_salt(buf2, 4, rnd);
  1032. * rnd = crypt_make_salt(buf3, 4, rnd);
  1033. * (otherwise we risk having same salt generated)
  1034. */
  1035. extern int crypt_make_salt(char *p, int cnt, int rnd) FAST_FUNC;
  1036. /* Returns number of lines changed, or -1 on error */
  1037. extern int update_passwd(const char *filename, const char *username,
  1038. const char *new_pw) FAST_FUNC;
  1039. int index_in_str_array(const char *const string_array[], const char *key) FAST_FUNC;
  1040. int index_in_strings(const char *strings, const char *key) FAST_FUNC;
  1041. int index_in_substr_array(const char *const string_array[], const char *key) FAST_FUNC;
  1042. int index_in_substrings(const char *strings, const char *key) FAST_FUNC;
  1043. const char *nth_string(const char *strings, int n) FAST_FUNC;
  1044. extern void print_login_issue(const char *issue_file, const char *tty) FAST_FUNC;
  1045. extern void print_login_prompt(void) FAST_FUNC;
  1046. /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
  1047. int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;
  1048. int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
  1049. /* NB: "unsigned request" is crucial! "int request" will break some arches! */
  1050. int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC;
  1051. int ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC;
  1052. #if ENABLE_IOCTL_HEX2STR_ERROR
  1053. int bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
  1054. int bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
  1055. #define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp,#request)
  1056. #define xioctl(fd,request,argp) bb_xioctl(fd,request,argp,#request)
  1057. #else
  1058. int bb_ioctl_or_warn(int fd, unsigned request, void *argp) FAST_FUNC;
  1059. int bb_xioctl(int fd, unsigned request, void *argp) FAST_FUNC;
  1060. #define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp)
  1061. #define xioctl(fd,request,argp) bb_xioctl(fd,request,argp)
  1062. #endif
  1063. char *is_in_ino_dev_hashtable(const struct stat *statbuf) FAST_FUNC;
  1064. void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) FAST_FUNC;
  1065. void reset_ino_dev_hashtable(void) FAST_FUNC;
  1066. #ifdef __GLIBC__
  1067. /* At least glibc has horrendously large inline for this, so wrap it */
  1068. unsigned long long bb_makedev(unsigned int major, unsigned int minor) FAST_FUNC;
  1069. #undef makedev
  1070. #define makedev(a,b) bb_makedev(a,b)
  1071. #endif
  1072. #if ENABLE_FEATURE_EDITING
  1073. /* It's NOT just ENABLEd or disabled. It's a number: */
  1074. #ifdef CONFIG_FEATURE_EDITING_HISTORY
  1075. #define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0)
  1076. #else
  1077. #define MAX_HISTORY 0
  1078. #endif
  1079. typedef struct line_input_t {
  1080. int flags;
  1081. const char *path_lookup;
  1082. #if MAX_HISTORY
  1083. int cnt_history;
  1084. int cur_history;
  1085. USE_FEATURE_EDITING_SAVEHISTORY(const char *hist_file;)
  1086. char *history[MAX_HISTORY + 1];
  1087. #endif
  1088. } line_input_t;
  1089. enum {
  1090. DO_HISTORY = 1 * (MAX_HISTORY > 0),
  1091. SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
  1092. TAB_COMPLETION = 4 * ENABLE_FEATURE_TAB_COMPLETION,
  1093. USERNAME_COMPLETION = 8 * ENABLE_FEATURE_USERNAME_COMPLETION,
  1094. VI_MODE = 0x10 * ENABLE_FEATURE_EDITING_VI,
  1095. WITH_PATH_LOOKUP = 0x20,
  1096. FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
  1097. };
  1098. line_input_t *new_line_input_t(int flags) FAST_FUNC;
  1099. /* Returns:
  1100. * -1 on read errors or EOF, or on bare Ctrl-D,
  1101. * 0 on ctrl-C (the line entered is still returned in 'command'),
  1102. * >0 length of input string, including terminating '\n'
  1103. */
  1104. int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state) FAST_FUNC;
  1105. #else
  1106. int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
  1107. #define read_line_input(prompt, command, maxsize, state) \
  1108. read_line_input(prompt, command, maxsize)
  1109. #endif
  1110. #ifndef COMM_LEN
  1111. #ifdef TASK_COMM_LEN
  1112. enum { COMM_LEN = TASK_COMM_LEN };
  1113. #else
  1114. /* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
  1115. enum { COMM_LEN = 16 };
  1116. #endif
  1117. #endif
  1118. typedef struct procps_status_t {
  1119. DIR *dir;
  1120. uint8_t shift_pages_to_bytes;
  1121. uint8_t shift_pages_to_kb;
  1122. /* Fields are set to 0/NULL if failed to determine (or not requested) */
  1123. uint16_t argv_len;
  1124. char *argv0;
  1125. USE_SELINUX(char *context;)
  1126. /* Everything below must contain no ptrs to malloc'ed data:
  1127. * it is memset(0) for each process in procps_scan() */
  1128. unsigned long vsz, rss; /* we round it to kbytes */
  1129. unsigned long stime, utime;
  1130. unsigned long start_time;
  1131. unsigned pid;
  1132. unsigned ppid;
  1133. unsigned pgid;
  1134. unsigned sid;
  1135. unsigned uid;
  1136. unsigned gid;
  1137. unsigned tty_major,tty_minor;
  1138. #if ENABLE_FEATURE_TOPMEM
  1139. unsigned long mapped_rw;
  1140. unsigned long mapped_ro;
  1141. unsigned long shared_clean;
  1142. unsigned long shared_dirty;
  1143. unsigned long private_clean;
  1144. unsigned long private_dirty;
  1145. unsigned long stack;
  1146. #endif
  1147. char state[4];
  1148. /* basename of executable in exec(2), read from /proc/N/stat
  1149. * (if executable is symlink or script, it is NOT replaced
  1150. * by link target or interpreter name) */
  1151. char comm[COMM_LEN];
  1152. /* user/group? - use passwd/group parsing functions */
  1153. #if ENABLE_FEATURE_TOP_SMP_PROCESS
  1154. int last_seen_on_cpu;
  1155. #endif
  1156. } procps_status_t;
  1157. enum {
  1158. PSSCAN_PID = 1 << 0,
  1159. PSSCAN_PPID = 1 << 1,
  1160. PSSCAN_PGID = 1 << 2,
  1161. PSSCAN_SID = 1 << 3,
  1162. PSSCAN_UIDGID = 1 << 4,
  1163. PSSCAN_COMM = 1 << 5,
  1164. /* PSSCAN_CMD = 1 << 6, - use read_cmdline instead */
  1165. PSSCAN_ARGV0 = 1 << 7,
  1166. /* PSSCAN_EXE = 1 << 8, - not implemented */
  1167. PSSCAN_STATE = 1 << 9,
  1168. PSSCAN_VSZ = 1 << 10,
  1169. PSSCAN_RSS = 1 << 11,
  1170. PSSCAN_STIME = 1 << 12,
  1171. PSSCAN_UTIME = 1 << 13,
  1172. PSSCAN_TTY = 1 << 14,
  1173. PSSCAN_SMAPS = (1 << 15) * ENABLE_FEATURE_TOPMEM,
  1174. /* NB: used by find_pid_by_name(). Any applet using it
  1175. * needs to be mentioned here. */
  1176. PSSCAN_ARGVN = (1 << 16) * (ENABLE_KILLALL
  1177. || ENABLE_PGREP || ENABLE_PKILL
  1178. || ENABLE_PIDOF
  1179. || ENABLE_SESTATUS
  1180. ),
  1181. USE_SELINUX(PSSCAN_CONTEXT = 1 << 17,)
  1182. PSSCAN_START_TIME = 1 << 18,
  1183. PSSCAN_CPU = 1 << 19,
  1184. /* These are all retrieved from proc/NN/stat in one go: */
  1185. PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
  1186. /**/ | PSSCAN_COMM | PSSCAN_STATE
  1187. /**/ | PSSCAN_VSZ | PSSCAN_RSS
  1188. /**/ | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
  1189. /**/ | PSSCAN_TTY
  1190. #if ENABLE_FEATURE_TOP_SMP_PROCESS
  1191. /**/ | PSSCAN_CPU
  1192. #endif
  1193. };
  1194. //procps_status_t* alloc_procps_scan(void) FAST_FUNC;
  1195. void free_procps_scan(procps_status_t* sp) FAST_FUNC;
  1196. procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
  1197. /* Format cmdline (up to col chars) into char buf[col+1] */
  1198. /* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
  1199. void read_cmdline(char *buf, int col, unsigned pid, const char *comm) FAST_FUNC;
  1200. pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
  1201. pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
  1202. extern const char bb_uuenc_tbl_base64[];
  1203. extern const char bb_uuenc_tbl_std[];
  1204. void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC;
  1205. typedef struct sha1_ctx_t {
  1206. uint32_t count[2];
  1207. uint32_t hash[5];
  1208. uint32_t wbuf[16];
  1209. } sha1_ctx_t;
  1210. void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC;
  1211. void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx) FAST_FUNC;
  1212. void *sha1_end(void *resbuf, sha1_ctx_t *ctx) FAST_FUNC;
  1213. typedef struct md5_ctx_t {
  1214. uint32_t A;
  1215. uint32_t B;
  1216. uint32_t C;
  1217. uint32_t D;
  1218. uint64_t total;
  1219. uint32_t buflen;
  1220. char buffer[128];
  1221. } md5_ctx_t;
  1222. void md5_begin(md5_ctx_t *ctx) FAST_FUNC;
  1223. void md5_hash(const void *data, size_t length, md5_ctx_t *ctx) FAST_FUNC;
  1224. void *md5_end(void *resbuf, md5_ctx_t *ctx) FAST_FUNC;
  1225. uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
  1226. typedef struct masks_labels_t {
  1227. const char *labels;
  1228. const int masks[];
  1229. } masks_labels_t;
  1230. int print_flags_separated(const int *masks, const char *labels,
  1231. int flags, const char *separator) FAST_FUNC;
  1232. int print_flags(const masks_labels_t *ml, int flags) FAST_FUNC;
  1233. extern const char *applet_name;
  1234. /* "BusyBox vN.N.N (timestamp or extra_version)" */
  1235. extern const char bb_banner[];
  1236. extern const char bb_msg_memory_exhausted[];
  1237. extern const char bb_msg_invalid_date[];
  1238. extern const char bb_msg_read_error[];
  1239. extern const char bb_msg_write_error[];
  1240. extern const char bb_msg_unknown[];
  1241. extern const char bb_msg_can_not_create_raw_socket[];
  1242. extern const char bb_msg_perm_denied_are_you_root[];
  1243. extern const char bb_msg_requires_arg[];
  1244. extern const char bb_msg_invalid_arg[];
  1245. extern const char bb_msg_standard_input[];
  1246. extern const char bb_msg_standard_output[];
  1247. extern const char bb_str_default[];
  1248. /* NB: (bb_hexdigits_upcase[i] | 0x20) -> lowercase hex digit */
  1249. extern const char bb_hexdigits_upcase[];
  1250. extern const char bb_path_mtab_file[];
  1251. extern const char bb_path_passwd_file[];
  1252. extern const char bb_path_shadow_file[];
  1253. extern const char bb_path_gshadow_file[];
  1254. extern const char bb_path_group_file[];
  1255. extern const char bb_path_motd_file[];
  1256. extern const char bb_path_wtmp_file[];
  1257. extern const char bb_dev_null[];
  1258. extern const char bb_busybox_exec_path[];
  1259. /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
  1260. * but I want to save a few bytes here */
  1261. extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
  1262. #define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
  1263. #define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin"))
  1264. extern const int const_int_0;
  1265. extern const int const_int_1;
  1266. #ifndef BUFSIZ
  1267. #define BUFSIZ 4096
  1268. #endif
  1269. /* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
  1270. enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
  1271. extern char bb_common_bufsiz1[COMMON_BUFSIZE];
  1272. /* This struct is deliberately not defined. */
  1273. /* See docs/keep_data_small.txt */
  1274. struct globals;
  1275. /* '*const' ptr makes gcc optimize code much better.
  1276. * Magic prevents ptr_to_globals from going into rodata.
  1277. * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
  1278. extern struct globals *const ptr_to_globals;
  1279. /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
  1280. #define barrier() __asm__ __volatile__("":::"memory")
  1281. #define SET_PTR_TO_GLOBALS(x) do { \
  1282. (*(struct globals**)&ptr_to_globals) = (x); \
  1283. barrier(); \
  1284. } while (0)
  1285. /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  1286. * use bb_default_login_shell and following defines.
  1287. * If you change LIBBB_DEFAULT_LOGIN_SHELL,
  1288. * don't forget to change increment constant. */
  1289. #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
  1290. extern const char bb_default_login_shell[];
  1291. /* "/bin/sh" */
  1292. #define DEFAULT_SHELL (bb_default_login_shell+1)
  1293. /* "sh" */
  1294. #define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
  1295. #if ENABLE_FEATURE_DEVFS
  1296. # define CURRENT_VC "/dev/vc/0"
  1297. # define VC_1 "/dev/vc/1"
  1298. # define VC_2 "/dev/vc/2"
  1299. # define VC_3 "/dev/vc/3"
  1300. # define VC_4 "/dev/vc/4"
  1301. # define VC_5 "/dev/vc/5"
  1302. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  1303. /* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
  1304. respective serial ports .. as such, we can't use the common device paths for
  1305. these. -- PFM */
  1306. # define SC_0 "/dev/ttsc/0"
  1307. # define SC_1 "/dev/ttsc/1"
  1308. # define SC_FORMAT "/dev/ttsc/%d"
  1309. #else
  1310. # define SC_0 "/dev/tts/0"
  1311. # define SC_1 "/dev/tts/1"
  1312. # define SC_FORMAT "/dev/tts/%d"
  1313. #endif
  1314. # define VC_FORMAT "/dev/vc/%d"
  1315. # define LOOP_FORMAT "/dev/loop/%d"
  1316. # define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)
  1317. # define LOOP_NAME "/dev/loop/"
  1318. # define FB_0 "/dev/fb/0"
  1319. #else
  1320. # define CURRENT_VC "/dev/tty0"
  1321. # define VC_1 "/dev/tty1"
  1322. # define VC_2 "/dev/tty2"
  1323. # define VC_3 "/dev/tty3"
  1324. # define VC_4 "/dev/tty4"
  1325. # define VC_5 "/dev/tty5"
  1326. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  1327. # define SC_0 "/dev/ttySC0"
  1328. # define SC_1 "/dev/ttySC1"
  1329. # define SC_FORMAT "/dev/ttySC%d"
  1330. #else
  1331. # define SC_0 "/dev/ttyS0"
  1332. # define SC_1 "/dev/ttyS1"
  1333. # define SC_FORMAT "/dev/ttyS%d"
  1334. #endif
  1335. # define VC_FORMAT "/dev/tty%d"
  1336. # define LOOP_FORMAT "/dev/loop%d"
  1337. # define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
  1338. # define LOOP_NAME "/dev/loop"
  1339. # define FB_0 "/dev/fb0"
  1340. #endif
  1341. /* The following devices are the same on devfs and non-devfs systems. */
  1342. #define CURRENT_TTY "/dev/tty"
  1343. #define DEV_CONSOLE "/dev/console"
  1344. #ifndef RB_POWER_OFF
  1345. /* Stop system and switch power off if possible. */
  1346. #define RB_POWER_OFF 0x4321fedc
  1347. #endif
  1348. /* Make sure we call functions instead of macros. */
  1349. #undef isalnum
  1350. #undef isalpha
  1351. #undef isascii
  1352. #undef isblank
  1353. #undef iscntrl
  1354. #undef isgraph
  1355. #undef islower
  1356. #undef isprint
  1357. #undef ispunct
  1358. #undef isspace
  1359. #undef isupper
  1360. #undef isxdigit
  1361. /* This one is more efficient - we save ~400 bytes */
  1362. #undef isdigit
  1363. #define isdigit(a) ((unsigned)((a) - '0') <= 9)
  1364. #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  1365. #if __GNUC_PREREQ(4,1)
  1366. # pragma GCC visibility pop
  1367. #endif
  1368. #endif /* __LIBBUSYBOX_H__ */