3
0

libbb.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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 <mntent.h>
  19. #include <netdb.h>
  20. #include <setjmp.h>
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. /* #include <strings.h> - said to be obsolete */
  28. #include <sys/ioctl.h>
  29. #include <sys/mman.h>
  30. #include <sys/socket.h>
  31. #include <sys/stat.h>
  32. #include <sys/statfs.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <termios.h>
  37. #include <time.h>
  38. #include <unistd.h>
  39. #include <utime.h>
  40. #if ENABLE_SELINUX
  41. #include <selinux/selinux.h>
  42. #include <selinux/context.h>
  43. #endif
  44. #if ENABLE_LOCALE_SUPPORT
  45. #include <locale.h>
  46. #else
  47. #define setlocale(x,y) ((void)0)
  48. #endif
  49. #include "pwd_.h"
  50. #include "grp_.h"
  51. /* ifdef it out, because it may include <shadow.h> */
  52. /* and we may not even _have_ <shadow.h>! */
  53. #if ENABLE_FEATURE_SHADOWPASSWDS
  54. #include "shadow_.h"
  55. #endif
  56. /* Try to pull in PATH_MAX */
  57. #include <limits.h>
  58. #include <sys/param.h>
  59. #ifndef PATH_MAX
  60. #define PATH_MAX 256
  61. #endif
  62. /* Tested to work correctly (IIRC :]) */
  63. #define MAXINT(T) (T)( \
  64. ((T)-1) > 0 \
  65. ? (T)-1 \
  66. : (T)~((T)1 << (sizeof(T)*8-1)) \
  67. )
  68. #define MININT(T) (T)( \
  69. ((T)-1) > 0 \
  70. ? (T)0 \
  71. : ((T)1 << (sizeof(T)*8-1)) \
  72. )
  73. /* Large file support */
  74. /* Note that CONFIG_LFS forces bbox to be built with all common ops
  75. * (stat, lseek etc) mapped to "largefile" variants by libc.
  76. * Practically it means that open() automatically has O_LARGEFILE added
  77. * and all filesize/file_offset parameters and struct members are "large"
  78. * (in today's world - signed 64bit). For full support of large files,
  79. * we need a few helper #defines (below) and careful use of off_t
  80. * instead of int/ssize_t. No lseek64(), O_LARGEFILE etc necessary */
  81. #if ENABLE_LFS
  82. /* CONFIG_LFS is on */
  83. # if ULONG_MAX > 0xffffffff
  84. /* "long" is long enough on this system */
  85. # define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  86. /* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
  87. # define BB_STRTOOFF bb_strtoul
  88. # define STRTOOFF strtoul
  89. /* usage: printf("size: %"OFF_FMT"d (%"OFF_FMT"x)\n", sz, sz); */
  90. # define OFF_FMT "l"
  91. # else
  92. /* "long" is too short, need "long long" */
  93. # define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX)
  94. # define BB_STRTOOFF bb_strtoull
  95. # define STRTOOFF strtoull
  96. # define OFF_FMT "ll"
  97. # endif
  98. #else
  99. /* CONFIG_LFS is off */
  100. # if UINT_MAX == 0xffffffff
  101. /* While sizeof(off_t) == sizeof(int), off_t is typedef'ed to long anyway.
  102. * gcc will throw warnings on printf("%d", off_t). Crap... */
  103. # define XATOOFF(a) xatoi_u(a)
  104. # define BB_STRTOOFF bb_strtou
  105. # define STRTOOFF strtol
  106. # define OFF_FMT "l"
  107. # else
  108. # define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
  109. # define BB_STRTOOFF bb_strtoul
  110. # define STRTOOFF strtol
  111. # define OFF_FMT "l"
  112. # endif
  113. #endif
  114. /* scary. better ideas? (but do *test* them first!) */
  115. #define OFF_T_MAX ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
  116. /* Some useful definitions */
  117. #undef FALSE
  118. #define FALSE ((int) 0)
  119. #undef TRUE
  120. #define TRUE ((int) 1)
  121. #undef SKIP
  122. #define SKIP ((int) 2)
  123. /* for mtab.c */
  124. #define MTAB_GETMOUNTPT '1'
  125. #define MTAB_GETDEVICE '2'
  126. #define BUF_SIZE 8192
  127. #define EXPAND_ALLOC 1024
  128. /* Macros for min/max. */
  129. #ifndef MIN
  130. #define MIN(a,b) (((a)<(b))?(a):(b))
  131. #endif
  132. #ifndef MAX
  133. #define MAX(a,b) (((a)>(b))?(a):(b))
  134. #endif
  135. /* buffer allocation schemes */
  136. #if ENABLE_FEATURE_BUFFERS_GO_ON_STACK
  137. #define RESERVE_CONFIG_BUFFER(buffer,len) char buffer[len]
  138. #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
  139. #define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  140. #else
  141. #if ENABLE_FEATURE_BUFFERS_GO_IN_BSS
  142. #define RESERVE_CONFIG_BUFFER(buffer,len) static char buffer[len]
  143. #define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
  144. #define RELEASE_CONFIG_BUFFER(buffer) ((void)0)
  145. #else
  146. #define RESERVE_CONFIG_BUFFER(buffer,len) char *buffer = xmalloc(len)
  147. #define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer = xmalloc(len)
  148. #define RELEASE_CONFIG_BUFFER(buffer) free(buffer)
  149. #endif
  150. #endif
  151. #if defined(__GLIBC__)
  152. /* glibc uses __errno_location() to get a ptr to errno */
  153. /* We can just memorize it once - no multithreading in busybox :) */
  154. extern int *const bb_errno;
  155. #undef errno
  156. #define errno (*bb_errno)
  157. #endif
  158. #if defined(__GLIBC__) && __GLIBC__ < 2
  159. int vdprintf(int d, const char *format, va_list ap);
  160. #endif
  161. // This is declared here rather than #including <libgen.h> in order to avoid
  162. // confusing the two versions of basename. See the dirname/basename man page
  163. // for details.
  164. char *dirname(char *path);
  165. /* Include our own copy of struct sysinfo to avoid binary compatibility
  166. * problems with Linux 2.4, which changed things. Grumble, grumble. */
  167. struct sysinfo {
  168. long uptime; /* Seconds since boot */
  169. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  170. unsigned long totalram; /* Total usable main memory size */
  171. unsigned long freeram; /* Available memory size */
  172. unsigned long sharedram; /* Amount of shared memory */
  173. unsigned long bufferram; /* Memory used by buffers */
  174. unsigned long totalswap; /* Total swap space size */
  175. unsigned long freeswap; /* swap space still available */
  176. unsigned short procs; /* Number of current processes */
  177. unsigned short pad; /* Padding needed for m68k */
  178. unsigned long totalhigh; /* Total high memory size */
  179. unsigned long freehigh; /* Available high memory size */
  180. unsigned int mem_unit; /* Memory unit size in bytes */
  181. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  182. };
  183. int sysinfo(struct sysinfo* info);
  184. unsigned long long monotonic_us(void);
  185. unsigned monotonic_sec(void);
  186. extern void chomp(char *s);
  187. extern void trim(char *s);
  188. extern char *skip_whitespace(const char *);
  189. extern char *skip_non_whitespace(const char *);
  190. //TODO: supply a pointer to char[11] buffer (avoid statics)?
  191. extern const char *bb_mode_string(mode_t mode);
  192. extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
  193. extern int remove_file(const char *path, int flags);
  194. extern int copy_file(const char *source, const char *dest, int flags);
  195. enum {
  196. ACTION_RECURSE = (1 << 0),
  197. ACTION_FOLLOWLINKS = (1 << 1),
  198. ACTION_FOLLOWLINKS_L0 = (1 << 2),
  199. ACTION_DEPTHFIRST = (1 << 3),
  200. /*ACTION_REVERSE = (1 << 4), - unused */
  201. };
  202. extern int recursive_action(const char *fileName, unsigned flags,
  203. int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
  204. int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
  205. void* userData, unsigned depth);
  206. extern int device_open(const char *device, int mode);
  207. extern int get_console_fd(void);
  208. extern char *find_block_device(const char *path);
  209. /* bb_copyfd_XX print read/write errors and return -1 if they occur */
  210. extern off_t bb_copyfd_eof(int fd1, int fd2);
  211. extern off_t bb_copyfd_size(int fd1, int fd2, off_t size);
  212. extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size);
  213. /* "short" copy can be detected by return value < size */
  214. /* this helper yells "short read!" if param is not -1 */
  215. extern void complain_copyfd_and_die(off_t sz) ATTRIBUTE_NORETURN;
  216. extern char bb_process_escape_sequence(const char **ptr);
  217. /* TODO: sometimes modifies its parameter, which
  218. * makes it rather inconvenient at times: */
  219. extern char *bb_get_last_path_component(char *path);
  220. int ndelay_on(int fd);
  221. int ndelay_off(int fd);
  222. void xdup2(int, int);
  223. void xmove_fd(int, int);
  224. DIR *xopendir(const char *path);
  225. DIR *warn_opendir(const char *path);
  226. /* UNUSED: char *xmalloc_realpath(const char *path); */
  227. char *xmalloc_readlink(const char *path);
  228. char *xmalloc_readlink_or_warn(const char *path);
  229. char *xrealloc_getcwd_or_warn(char *cwd);
  230. //TODO: signal(sid, f) is the same? then why?
  231. extern void sig_catch(int,void (*)(int));
  232. //#define sig_ignore(s) (sig_catch((s), SIG_IGN))
  233. //#define sig_uncatch(s) (sig_catch((s), SIG_DFL))
  234. extern void sig_block(int);
  235. extern void sig_unblock(int);
  236. /* UNUSED: extern void sig_blocknone(void); */
  237. extern void sig_pause(void);
  238. void xsetgid(gid_t gid);
  239. void xsetuid(uid_t uid);
  240. void xchdir(const char *path);
  241. void xsetenv(const char *key, const char *value);
  242. void xunlink(const char *pathname);
  243. void xstat(const char *pathname, struct stat *buf);
  244. int xopen(const char *pathname, int flags);
  245. int xopen3(const char *pathname, int flags, int mode);
  246. int open_or_warn(const char *pathname, int flags);
  247. int open3_or_warn(const char *pathname, int flags, int mode);
  248. void xpipe(int filedes[2]);
  249. off_t xlseek(int fd, off_t offset, int whence);
  250. off_t fdlength(int fd);
  251. int xsocket(int domain, int type, int protocol);
  252. void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
  253. void xlisten(int s, int backlog);
  254. void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen);
  255. ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  256. socklen_t tolen);
  257. /* SO_REUSEADDR allows a server to rebind to an address that is already
  258. * "in use" by old connections to e.g. previous server instance which is
  259. * killed or crashed. Without it bind will fail until all such connections
  260. * time out. Linux does not allow multiple live binds on same ip:port
  261. * regardless of SO_REUSEADDR (unlike some other flavors of Unix).
  262. * Turn it on before you call bind(). */
  263. void setsockopt_reuseaddr(int fd); /* On Linux this never fails. */
  264. int setsockopt_broadcast(int fd);
  265. /* NB: returns port in host byte order */
  266. unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port);
  267. typedef struct len_and_sockaddr {
  268. socklen_t len;
  269. union {
  270. struct sockaddr sa;
  271. struct sockaddr_in sin;
  272. #if ENABLE_FEATURE_IPV6
  273. struct sockaddr_in6 sin6;
  274. #endif
  275. };
  276. } len_and_sockaddr;
  277. enum {
  278. LSA_SIZEOF_SA = sizeof(
  279. union {
  280. struct sockaddr sa;
  281. struct sockaddr_in sin;
  282. #if ENABLE_FEATURE_IPV6
  283. struct sockaddr_in6 sin6;
  284. #endif
  285. }
  286. )
  287. };
  288. /* Create stream socket, and allocate suitable lsa.
  289. * (lsa of correct size and lsa->sa.sa_family (AF_INET/AF_INET6))
  290. * af == AF_UNSPEC will result in trying to create IPv6 socket,
  291. * and if kernel doesn't support it, IPv4.
  292. */
  293. int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int af,) int sock_type);
  294. int xsocket_stream(len_and_sockaddr **lsap);
  295. /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  296. * numeric IP ("N.N.N.N") or numeric IPv6 address,
  297. * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  298. * Only if there is no suffix, port argument is used */
  299. /* NB: these set SO_REUSEADDR before bind */
  300. int create_and_bind_stream_or_die(const char *bindaddr, int port);
  301. int create_and_bind_dgram_or_die(const char *bindaddr, int port);
  302. /* Create client TCP socket connected to peer:port. Peer cannot be NULL.
  303. * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname,
  304. * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
  305. * If there is no suffix, port argument is used */
  306. int create_and_connect_stream_or_die(const char *peer, int port);
  307. /* Connect to peer identified by lsa */
  308. int xconnect_stream(const len_and_sockaddr *lsa);
  309. /* Return malloc'ed len_and_sockaddr with socket address of host:port
  310. * Currently will return IPv4 or IPv6 sockaddrs only
  311. * (depending on host), but in theory nothing prevents e.g.
  312. * UNIX socket address being returned, IPX sockaddr etc...
  313. * On error does bb_error_msg and returns NULL */
  314. len_and_sockaddr* host2sockaddr(const char *host, int port);
  315. /* Version which dies on error */
  316. len_and_sockaddr* xhost2sockaddr(const char *host, int port);
  317. len_and_sockaddr* xdotted2sockaddr(const char *host, int port);
  318. #if ENABLE_FEATURE_IPV6
  319. /* Same, useful if you want to force family (e.g. IPv6) */
  320. len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af);
  321. len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af);
  322. #else
  323. /* [we evaluate af: think about "host_and_af2sockaddr(..., af++)"] */
  324. #define host_and_af2sockaddr(host, port, af) ((void)(af), host2sockaddr((host), (port)))
  325. #define xhost_and_af2sockaddr(host, port, af) ((void)(af), xhost2sockaddr((host), (port)))
  326. #endif
  327. /* Assign sin[6]_port member if the socket is an AF_INET[6] one,
  328. * otherwise no-op. Useful for ftp.
  329. * NB: does NOT do htons() internally, just direct assignment. */
  330. void set_nport(len_and_sockaddr *lsa, unsigned port);
  331. /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */
  332. int get_nport(const struct sockaddr *sa);
  333. /* Reverse DNS. Returns NULL on failure. */
  334. char* xmalloc_sockaddr2host(const struct sockaddr *sa);
  335. /* This one doesn't append :PORTNUM */
  336. char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa);
  337. /* This one also doesn't fall back to dotted IP (returns NULL) */
  338. char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa);
  339. /* inet_[ap]ton on steroids */
  340. char* xmalloc_sockaddr2dotted(const struct sockaddr *sa);
  341. char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa);
  342. // "old" (ipv4 only) API
  343. // users: traceroute.c hostname.c - use _list_ of all IPs
  344. struct hostent *xgethostbyname(const char *name);
  345. // Also mount.c and inetd.c are using gethostbyname(),
  346. // + inet_common.c has additional IPv4-only stuff
  347. void socket_want_pktinfo(int fd);
  348. ssize_t send_to_from(int fd, void *buf, size_t len, int flags,
  349. const struct sockaddr *from, const struct sockaddr *to,
  350. socklen_t tolen);
  351. ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
  352. struct sockaddr *from, struct sockaddr *to,
  353. socklen_t sa_size);
  354. extern char *xstrdup(const char *s);
  355. extern char *xstrndup(const char *s, int n);
  356. extern char *safe_strncpy(char *dst, const char *src, size_t size);
  357. extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
  358. // gcc-4.1.1 still isn't good enough at optimizing it
  359. // (+200 bytes compared to macro)
  360. //static ALWAYS_INLINE
  361. //int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; }
  362. //static ALWAYS_INLINE
  363. //int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; }
  364. #define LONE_DASH(s) ((s)[0] == '-' && !(s)[1])
  365. #define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
  366. #define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
  367. #define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
  368. #define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
  369. /* dmalloc will redefine these to it's own implementation. It is safe
  370. * to have the prototypes here unconditionally. */
  371. extern void *malloc_or_warn(size_t size);
  372. extern void *xmalloc(size_t size);
  373. extern void *xzalloc(size_t size);
  374. extern void *xrealloc(void *old, size_t size);
  375. extern ssize_t safe_read(int fd, void *buf, size_t count);
  376. extern ssize_t full_read(int fd, void *buf, size_t count);
  377. extern void xread(int fd, void *buf, size_t count);
  378. extern unsigned char xread_char(int fd);
  379. // Read one line a-la fgets. Uses one read(), works only on seekable streams
  380. extern char *reads(int fd, char *buf, size_t count);
  381. // Read one line a-la fgets. Reads byte-by-byte.
  382. // Useful when it is important to not read ahead.
  383. extern char *xmalloc_reads(int fd, char *pfx);
  384. extern ssize_t read_close(int fd, void *buf, size_t count);
  385. extern ssize_t open_read_close(const char *filename, void *buf, size_t count);
  386. extern void *xmalloc_open_read_close(const char *filename, size_t *sizep);
  387. extern ssize_t safe_write(int fd, const void *buf, size_t count);
  388. extern ssize_t full_write(int fd, const void *buf, size_t count);
  389. extern void xwrite(int fd, const void *buf, size_t count);
  390. /* Reads and prints to stdout till eof, then closes FILE. Exits on error: */
  391. extern void xprint_and_close_file(FILE *file);
  392. extern char *xmalloc_fgets(FILE *file);
  393. /* Read up to (and including) TERMINATING_STRING: */
  394. extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string);
  395. /* Chops off '\n' from the end, unlike fgets: */
  396. extern char *xmalloc_getline(FILE *file);
  397. extern char *bb_get_chunk_from_file(FILE *file, int *end);
  398. extern void die_if_ferror(FILE *file, const char *msg);
  399. extern void die_if_ferror_stdout(void);
  400. extern void xfflush_stdout(void);
  401. extern void fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
  402. extern int fclose_if_not_stdin(FILE *file);
  403. extern FILE *xfopen(const char *filename, const char *mode);
  404. /* Prints warning to stderr and returns NULL on failure: */
  405. extern FILE *fopen_or_warn(const char *filename, const char *mode);
  406. /* "Opens" stdin if filename is special, else just opens file: */
  407. extern FILE *fopen_or_warn_stdin(const char *filename);
  408. /* Convert each alpha char in str to lower-case */
  409. extern char* str_tolower(char *str);
  410. char *utoa(unsigned n);
  411. char *itoa(int n);
  412. /* Returns a pointer past the formatted number, does NOT null-terminate */
  413. char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
  414. char *itoa_to_buf(int n, char *buf, unsigned buflen);
  415. void smart_ulltoa5(unsigned long long ul, char buf[5]);
  416. //TODO: provide pointer to buf (avoid statics)?
  417. const char *make_human_readable_str(unsigned long long size,
  418. unsigned long block_size, unsigned long display_unit);
  419. /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
  420. char *bin2hex(char *buf, const char *cp, int count);
  421. /* Last element is marked by mult == 0 */
  422. struct suffix_mult {
  423. char suffix[4];
  424. unsigned mult;
  425. };
  426. #include "xatonum.h"
  427. /* Specialized: */
  428. /* Using xatoi() instead of naive atoi() is not always convenient -
  429. * in many places people want *non-negative* values, but store them
  430. * in signed int. Therefore we need this one:
  431. * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */
  432. int xatoi_u(const char *numstr);
  433. /* Useful for reading port numbers */
  434. uint16_t xatou16(const char *numstr);
  435. /* These parse entries in /etc/passwd and /etc/group. This is desirable
  436. * for BusyBox since we want to avoid using the glibc NSS stuff, which
  437. * increases target size and is often not needed on embedded systems. */
  438. long xuname2uid(const char *name);
  439. long xgroup2gid(const char *name);
  440. /* wrapper: allows string to contain numeric uid or gid */
  441. unsigned long get_ug_id(const char *s, long (*xname2id)(const char *));
  442. /* from chpst. Does not die, returns 0 on failure */
  443. struct bb_uidgid_t {
  444. uid_t uid;
  445. gid_t gid;
  446. };
  447. /* always sets uid and gid */
  448. int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok);
  449. /* chown-like handling of "user[:[group]" */
  450. void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group);
  451. /* bb_getpwuid, bb_getgrgid:
  452. * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
  453. * as a string to buf, return user/group name or NULL
  454. * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
  455. * bb_getXXXid(NULL, -1, id) - return user/group name or exit
  456. */
  457. char *bb_getpwuid(char *name, int bufsize, long uid);
  458. char *bb_getgrgid(char *group, int bufsize, long gid);
  459. /* versions which cache results (useful for ps, ls etc) */
  460. const char* get_cached_username(uid_t uid);
  461. const char* get_cached_groupname(gid_t gid);
  462. void clear_username_cache(void);
  463. /* internally usernames are saved in fixed-sized char[] buffers */
  464. enum { USERNAME_MAX_SIZE = 16 - sizeof(int) };
  465. struct bb_applet;
  466. int execable_file(const char *name);
  467. char *find_execable(const char *filename);
  468. int exists_execable(const char *filename);
  469. #if ENABLE_FEATURE_PREFER_APPLETS
  470. int bb_execvp(const char *file, char *const argv[]);
  471. #define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
  472. #define BB_EXECLP(prog,cmd,...) \
  473. execlp((find_applet_by_name(prog)) ? CONFIG_BUSYBOX_EXEC_PATH : prog, \
  474. cmd, __VA_ARGS__)
  475. #else
  476. #define BB_EXECVP(prog,cmd) execvp(prog,cmd)
  477. #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__)
  478. #endif
  479. /* NOMMU friendy fork+exec */
  480. pid_t spawn(char **argv);
  481. pid_t xspawn(char **argv);
  482. /* Unlike waitpid, waits ONLY for one process,
  483. * It's safe to pass negative 'pids' from failed [v]fork -
  484. * wait4pid will return -1 (and will not clobber [v]fork's errno).
  485. * IOW: rc = wait4pid(spawn(argv));
  486. * if (rc < 0) bb_perror_msg("%s", argv[0]);
  487. * if (rc > 0) bb_error_msg("exit code: %d", rc);
  488. */
  489. int wait4pid(int pid);
  490. int wait_pid(int *wstat, int pid);
  491. int wait_nohang(int *wstat);
  492. #define wait_crashed(w) ((w) & 127)
  493. #define wait_exitcode(w) ((w) >> 8)
  494. #define wait_stopsig(w) ((w) >> 8)
  495. #define wait_stopped(w) (((w) & 127) == 127)
  496. /* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */
  497. int spawn_and_wait(char **argv);
  498. struct nofork_save_area {
  499. jmp_buf die_jmp;
  500. const struct bb_applet *current_applet;
  501. int xfunc_error_retval;
  502. uint32_t option_mask32;
  503. int die_sleep;
  504. smallint saved;
  505. };
  506. void save_nofork_data(struct nofork_save_area *save);
  507. void restore_nofork_data(struct nofork_save_area *save);
  508. /* Does NOT check that applet is NOFORK, just blindly runs it */
  509. int run_nofork_applet(const struct bb_applet *a, char **argv);
  510. int run_nofork_applet_prime(struct nofork_save_area *old, const struct bb_applet *a, char **argv);
  511. /* Helpers for daemonization.
  512. *
  513. * bb_daemonize(flags) = daemonize, does not compile on NOMMU
  514. *
  515. * bb_daemonize_or_rexec(flags, argv) = daemonizes on MMU (and ignores argv),
  516. * rexec's itself on NOMMU with argv passed as command line.
  517. * Thus bb_daemonize_or_rexec may cause your <applet>_main() to be re-executed
  518. * from the start. (It will detect it and not reexec again second time).
  519. * You have to audit carefully that you don't do something twice as a result
  520. * (opening files/sockets, parsing config files etc...)!
  521. *
  522. * Both of the above will redirect fd 0,1,2 to /dev/null and drop ctty
  523. * (will do setsid()).
  524. *
  525. * forkexit_or_rexec(argv) = bare-bones "fork + parent exits" on MMU,
  526. * "vfork + re-exec ourself" on NOMMU. No fd redirection, no setsid().
  527. * Currently used for openvt. On MMU ignores argv.
  528. *
  529. * Helper for network daemons in foreground mode:
  530. *
  531. * bb_sanitize_stdio() = make sure that fd 0,1,2 are opened by opening them
  532. * to /dev/null if they are not.
  533. */
  534. enum {
  535. DAEMON_CHDIR_ROOT = 1,
  536. DAEMON_DEVNULL_STDIO = 2,
  537. DAEMON_CLOSE_EXTRA_FDS = 4,
  538. DAEMON_ONLY_SANITIZE = 8, /* internal use */
  539. };
  540. #if BB_MMU
  541. void forkexit_or_rexec(void);
  542. enum { re_execed = 0 };
  543. # define forkexit_or_rexec(argv) forkexit_or_rexec()
  544. # define bb_daemonize_or_rexec(flags, argv) bb_daemonize_or_rexec(flags)
  545. # define bb_daemonize(flags) bb_daemonize_or_rexec(flags, bogus)
  546. #else
  547. void re_exec(char **argv) ATTRIBUTE_NORETURN;
  548. void forkexit_or_rexec(char **argv);
  549. extern bool re_execed;
  550. # define fork() BUG_fork_is_unavailable_on_nommu()
  551. # define daemon(a,b) BUG_daemon_is_unavailable_on_nommu()
  552. # define bb_daemonize(a) BUG_bb_daemonize_is_unavailable_on_nommu()
  553. #endif
  554. void bb_daemonize_or_rexec(int flags, char **argv);
  555. void bb_sanitize_stdio(void);
  556. extern const char *opt_complementary;
  557. #if ENABLE_GETOPT_LONG
  558. #define No_argument "\0"
  559. #define Required_argument "\001"
  560. #define Optional_argument "\002"
  561. extern const char *applet_long_options;
  562. #endif
  563. extern uint32_t option_mask32;
  564. extern uint32_t getopt32(char **argv, const char *applet_opts, ...);
  565. typedef struct llist_t {
  566. char *data;
  567. struct llist_t *link;
  568. } llist_t;
  569. void llist_add_to(llist_t **old_head, void *data);
  570. void llist_add_to_end(llist_t **list_head, void *data);
  571. void *llist_pop(llist_t **elm);
  572. void llist_unlink(llist_t **head, llist_t *elm);
  573. void llist_free(llist_t *elm, void (*freeit)(void *data));
  574. llist_t *llist_rev(llist_t *list);
  575. /* BTW, surprisingly, changing API to
  576. * llist_t *llist_add_to(llist_t *old_head, void *data)
  577. * etc does not result in smaller code... */
  578. /* start_stop_daemon and udhcpc are special - they want
  579. * to create pidfiles regardless of FEATURE_PIDFILE */
  580. #if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
  581. /* True only if we created pidfile which is *file*, not /dev/null etc */
  582. extern smallint wrote_pidfile;
  583. void write_pidfile(const char *path);
  584. #define remove_pidfile(path) do { if (wrote_pidfile) unlink(path); } while (0)
  585. #else
  586. enum { wrote_pidfile = 0 };
  587. #define write_pidfile(path) ((void)0)
  588. #define remove_pidfile(path) ((void)0)
  589. #endif
  590. enum {
  591. LOGMODE_NONE = 0,
  592. LOGMODE_STDIO = (1 << 0),
  593. LOGMODE_SYSLOG = (1 << 1) * ENABLE_FEATURE_SYSLOG,
  594. LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
  595. };
  596. extern const char *msg_eol;
  597. extern smallint logmode;
  598. extern int die_sleep;
  599. extern int xfunc_error_retval;
  600. extern jmp_buf die_jmp;
  601. extern void xfunc_die(void) ATTRIBUTE_NORETURN;
  602. extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
  603. extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  604. extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  605. extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  606. extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  607. extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  608. extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  609. extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
  610. extern void bb_perror_nomsg(void);
  611. extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  612. extern void bb_verror_msg(const char *s, va_list p, const char *strerr);
  613. /* applets which are useful from another applets */
  614. int bb_cat(char** argv);
  615. int bb_echo(char** argv);
  616. int test_main(int argc, char** argv);
  617. int kill_main(int argc, char **argv);
  618. #if ENABLE_ROUTE
  619. void bb_displayroutes(int noresolve, int netstatfmt);
  620. #endif
  621. int chown_main(int argc, char **argv);
  622. #if ENABLE_GUNZIP
  623. int gunzip_main(int argc, char **argv);
  624. #endif
  625. int bbunpack(char **argv,
  626. char* (*make_new_name)(char *filename),
  627. USE_DESKTOP(long long) int (*unpacker)(void)
  628. );
  629. /* Networking */
  630. int create_icmp_socket(void);
  631. int create_icmp6_socket(void);
  632. /* interface.c */
  633. /* This structure defines protocol families and their handlers. */
  634. struct aftype {
  635. const char *name;
  636. const char *title;
  637. int af;
  638. int alen;
  639. char *(*print) (unsigned char *);
  640. const char *(*sprint) (struct sockaddr *, int numeric);
  641. int (*input) (/*int type,*/ const char *bufp, struct sockaddr *);
  642. void (*herror) (char *text);
  643. int (*rprint) (int options);
  644. int (*rinput) (int typ, int ext, char **argv);
  645. /* may modify src */
  646. int (*getmask) (char *src, struct sockaddr * mask, char *name);
  647. };
  648. /* This structure defines hardware protocols and their handlers. */
  649. struct hwtype {
  650. const char *name;
  651. const char *title;
  652. int type;
  653. int alen;
  654. char *(*print) (unsigned char *);
  655. int (*input) (const char *, struct sockaddr *);
  656. int (*activate) (int fd);
  657. int suppress_null_addr;
  658. };
  659. extern smallint interface_opt_a;
  660. int display_interfaces(char *ifname);
  661. const struct aftype *get_aftype(const char *name);
  662. const struct hwtype *get_hwtype(const char *name);
  663. const struct hwtype *get_hwntype(int type);
  664. #ifndef BUILD_INDIVIDUAL
  665. extern const struct bb_applet *find_applet_by_name(const char *name);
  666. /* Returns only if applet is not found. */
  667. extern void run_applet_and_exit(const char *name, char **argv);
  668. extern void run_current_applet_and_exit(char **argv) ATTRIBUTE_NORETURN;
  669. #endif
  670. extern int match_fstype(const struct mntent *mt, const char *fstypes);
  671. extern struct mntent *find_mount_point(const char *name, const char *table);
  672. extern void erase_mtab(const char * name);
  673. extern unsigned int tty_baud_to_value(speed_t speed);
  674. extern speed_t tty_value_to_baud(unsigned int value);
  675. extern void bb_warn_ignoring_args(int n);
  676. extern int get_linux_version_code(void);
  677. extern char *query_loop(const char *device);
  678. extern int del_loop(const char *device);
  679. /* If *devname is not NULL, use that name, otherwise try to find free one,
  680. * malloc and return it in *devname.
  681. * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */
  682. extern int set_loop(char **devname, const char *file, unsigned long long offset);
  683. //TODO: pass buf pointer or return allocated buf (avoid statics)?
  684. char *bb_askpass(int timeout, const char * prompt);
  685. int bb_ask_confirmation(void);
  686. int klogctl(int type, char * b, int len);
  687. extern int bb_parse_mode(const char* s, mode_t* theMode);
  688. char *concat_path_file(const char *path, const char *filename);
  689. char *concat_subpath_file(const char *path, const char *filename);
  690. const char *bb_basename(const char *name);
  691. /* NB: can violate const-ness (similarly to strchr) */
  692. char *last_char_is(const char *s, int c);
  693. USE_DESKTOP(long long) int uncompress(int fd_in, int fd_out);
  694. int inflate(int in, int out);
  695. int bb_make_directory(char *path, long mode, int flags);
  696. int get_signum(const char *name);
  697. const char *get_signame(int number);
  698. char *bb_simplify_path(const char *path);
  699. #define FAIL_DELAY 3
  700. extern void bb_do_delay(int seconds);
  701. extern void change_identity(const struct passwd *pw);
  702. extern const char *change_identity_e2str(const struct passwd *pw);
  703. extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) ATTRIBUTE_NORETURN;
  704. #if ENABLE_SELINUX
  705. extern void renew_current_security_context(void);
  706. extern void set_current_security_context(security_context_t sid);
  707. extern context_t set_security_context_component(security_context_t cur_context,
  708. char *user, char *role, char *type, char *range);
  709. extern void setfscreatecon_or_die(security_context_t scontext);
  710. #endif
  711. extern void selinux_or_die(void);
  712. extern int restricted_shell(const char *shell);
  713. extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw);
  714. extern int correct_password(const struct passwd *pw);
  715. /* Returns a ptr to static storage */
  716. extern char *pw_encrypt(const char *clear, const char *salt);
  717. extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
  718. extern int index_in_str_array(const char *const string_array[], const char *key);
  719. extern int index_in_strings(const char *strings, const char *key);
  720. extern int index_in_substr_array(const char *const string_array[], const char *key);
  721. extern int index_in_substrings(const char *strings, const char *key);
  722. extern void print_login_issue(const char *issue_file, const char *tty);
  723. extern void print_login_prompt(void);
  724. /* rnd is additional random input. New one is returned.
  725. * Useful if you call crypt_make_salt many times in a row:
  726. * rnd = crypt_make_salt(buf1, 4, 0);
  727. * rnd = crypt_make_salt(buf2, 4, rnd);
  728. * rnd = crypt_make_salt(buf3, 4, rnd);
  729. * (otherwise we risk having same salt generated)
  730. */
  731. extern int crypt_make_salt(char *p, int cnt, int rnd);
  732. /* Returns number of lines changed, or -1 on error */
  733. extern int update_passwd(const char *filename, const char *username,
  734. const char *new_pw);
  735. /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
  736. int get_terminal_width_height(int fd, int *width, int *height);
  737. int ioctl_or_perror(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
  738. void ioctl_or_perror_and_die(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
  739. #if ENABLE_IOCTL_HEX2STR_ERROR
  740. int bb_ioctl_or_warn(int fd, int request, void *argp, const char *ioctl_name);
  741. void bb_xioctl(int fd, int request, void *argp, const char *ioctl_name);
  742. #define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp,#request)
  743. #define xioctl(fd,request,argp) bb_xioctl(fd,request,argp,#request)
  744. #else
  745. int bb_ioctl_or_warn(int fd, int request, void *argp);
  746. void bb_xioctl(int fd, int request, void *argp);
  747. #define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp)
  748. #define xioctl(fd,request,argp) bb_xioctl(fd,request,argp)
  749. #endif
  750. char *is_in_ino_dev_hashtable(const struct stat *statbuf);
  751. void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
  752. void reset_ino_dev_hashtable(void);
  753. #ifdef __GLIBC__
  754. /* At least glibc has horrendously large inline for this, so wrap it */
  755. unsigned long long bb_makedev(unsigned int major, unsigned int minor);
  756. #undef makedev
  757. #define makedev(a,b) bb_makedev(a,b)
  758. #endif
  759. #if ENABLE_FEATURE_EDITING
  760. /* It's NOT just ENABLEd or disabled. It's a number: */
  761. #ifdef CONFIG_FEATURE_EDITING_HISTORY
  762. #define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0)
  763. #else
  764. #define MAX_HISTORY 0
  765. #endif
  766. typedef struct line_input_t {
  767. int flags;
  768. const char *path_lookup;
  769. #if MAX_HISTORY
  770. int cnt_history;
  771. int cur_history;
  772. USE_FEATURE_EDITING_SAVEHISTORY(const char *hist_file;)
  773. char *history[MAX_HISTORY + 1];
  774. #endif
  775. } line_input_t;
  776. enum {
  777. DO_HISTORY = 1 * (MAX_HISTORY > 0),
  778. SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
  779. TAB_COMPLETION = 4 * ENABLE_FEATURE_TAB_COMPLETION,
  780. USERNAME_COMPLETION = 8 * ENABLE_FEATURE_USERNAME_COMPLETION,
  781. VI_MODE = 0x10 * ENABLE_FEATURE_EDITING_VI,
  782. WITH_PATH_LOOKUP = 0x20,
  783. FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
  784. };
  785. line_input_t *new_line_input_t(int flags);
  786. int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state);
  787. #else
  788. int read_line_input(const char* prompt, char* command, int maxsize);
  789. #define read_line_input(prompt, command, maxsize, state) \
  790. read_line_input(prompt, command, maxsize)
  791. #endif
  792. #ifndef COMM_LEN
  793. #ifdef TASK_COMM_LEN
  794. enum { COMM_LEN = TASK_COMM_LEN };
  795. #else
  796. /* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */
  797. enum { COMM_LEN = 16 };
  798. #endif
  799. #endif
  800. typedef struct {
  801. DIR *dir;
  802. /* Fields are set to 0/NULL if failed to determine (or not requested) */
  803. /*char *cmd;*/
  804. char *argv0;
  805. /*char *exe;*/
  806. USE_SELINUX(char *context;)
  807. /* Everything below must contain no ptrs to malloc'ed data:
  808. * it is memset(0) for each process in procps_scan() */
  809. unsigned vsz, rss; /* we round it to kbytes */
  810. unsigned long stime, utime;
  811. unsigned pid;
  812. unsigned ppid;
  813. unsigned pgid;
  814. unsigned sid;
  815. unsigned uid;
  816. unsigned gid;
  817. unsigned tty_major,tty_minor;
  818. char state[4];
  819. /* basename of executable in exec(2), read from /proc/N/stat
  820. * (if executable is symlink or script, it is NOT replaced
  821. * by link target or interpreter name) */
  822. char comm[COMM_LEN];
  823. /* user/group? - use passwd/group parsing functions */
  824. } procps_status_t;
  825. enum {
  826. PSSCAN_PID = 1 << 0,
  827. PSSCAN_PPID = 1 << 1,
  828. PSSCAN_PGID = 1 << 2,
  829. PSSCAN_SID = 1 << 3,
  830. PSSCAN_UIDGID = 1 << 4,
  831. PSSCAN_COMM = 1 << 5,
  832. /* PSSCAN_CMD = 1 << 6, - use read_cmdline instead */
  833. PSSCAN_ARGV0 = 1 << 7,
  834. /* PSSCAN_EXE = 1 << 8, - not implemented */
  835. PSSCAN_STATE = 1 << 9,
  836. PSSCAN_VSZ = 1 << 10,
  837. PSSCAN_RSS = 1 << 11,
  838. PSSCAN_STIME = 1 << 12,
  839. PSSCAN_UTIME = 1 << 13,
  840. PSSCAN_TTY = 1 << 14,
  841. USE_SELINUX(PSSCAN_CONTEXT = 1 << 15,)
  842. /* These are all retrieved from proc/NN/stat in one go: */
  843. PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
  844. | PSSCAN_COMM | PSSCAN_STATE
  845. | PSSCAN_VSZ | PSSCAN_RSS
  846. | PSSCAN_STIME | PSSCAN_UTIME
  847. | PSSCAN_TTY,
  848. };
  849. procps_status_t* alloc_procps_scan(int flags);
  850. void free_procps_scan(procps_status_t* sp);
  851. procps_status_t* procps_scan(procps_status_t* sp, int flags);
  852. /* Format cmdline (up to col chars) into char buf[col+1] */
  853. /* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
  854. void read_cmdline(char *buf, int col, unsigned pid, const char *comm);
  855. pid_t *find_pid_by_name(const char* procName);
  856. pid_t *pidlist_reverse(pid_t *pidList);
  857. extern const char bb_uuenc_tbl_base64[];
  858. extern const char bb_uuenc_tbl_std[];
  859. void bb_uuencode(char *store, const void *s, int length, const char *tbl);
  860. typedef struct sha1_ctx_t {
  861. uint32_t count[2];
  862. uint32_t hash[5];
  863. uint32_t wbuf[16];
  864. } sha1_ctx_t;
  865. void sha1_begin(sha1_ctx_t *ctx);
  866. void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx);
  867. void *sha1_end(void *resbuf, sha1_ctx_t *ctx);
  868. typedef struct md5_ctx_t {
  869. uint32_t A;
  870. uint32_t B;
  871. uint32_t C;
  872. uint32_t D;
  873. uint64_t total;
  874. uint32_t buflen;
  875. char buffer[128];
  876. } md5_ctx_t;
  877. void md5_begin(md5_ctx_t *ctx);
  878. void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
  879. void *md5_end(void *resbuf, md5_ctx_t *ctx);
  880. uint32_t *crc32_filltable(uint32_t *tbl256, int endian);
  881. enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */
  882. FILEUTILS_PRESERVE_STATUS = 1,
  883. FILEUTILS_DEREFERENCE = 2,
  884. FILEUTILS_RECUR = 4,
  885. FILEUTILS_FORCE = 8,
  886. FILEUTILS_INTERACTIVE = 0x10,
  887. FILEUTILS_MAKE_HARDLINK = 0x20,
  888. FILEUTILS_MAKE_SOFTLINK = 0x40,
  889. #if ENABLE_SELINUX
  890. FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x80,
  891. FILEUTILS_SET_SECURITY_CONTEXT = 0x100
  892. #endif
  893. };
  894. #define FILEUTILS_CP_OPTSTR "pdRfils" USE_SELINUX("c")
  895. extern const struct bb_applet *current_applet;
  896. extern const char *applet_name;
  897. /* "BusyBox vN.N.N (timestamp or extra_vestion)" */
  898. extern const char bb_banner[];
  899. extern const char bb_msg_memory_exhausted[];
  900. extern const char bb_msg_invalid_date[];
  901. extern const char bb_msg_read_error[];
  902. extern const char bb_msg_write_error[];
  903. extern const char bb_msg_unknown[];
  904. extern const char bb_msg_can_not_create_raw_socket[];
  905. extern const char bb_msg_perm_denied_are_you_root[];
  906. extern const char bb_msg_requires_arg[];
  907. extern const char bb_msg_invalid_arg[];
  908. extern const char bb_msg_standard_input[];
  909. extern const char bb_msg_standard_output[];
  910. extern const char bb_str_default[];
  911. /* NB: (bb_hexdigits_upcase[i] | 0x20) -> lowercase hex digit */
  912. extern const char bb_hexdigits_upcase[];
  913. extern const char bb_path_mtab_file[];
  914. extern const char bb_path_passwd_file[];
  915. extern const char bb_path_shadow_file[];
  916. extern const char bb_path_gshadow_file[];
  917. extern const char bb_path_group_file[];
  918. extern const char bb_path_motd_file[];
  919. extern const char bb_path_wtmp_file[];
  920. extern const char bb_dev_null[];
  921. extern const char bb_busybox_exec_path[];
  922. /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
  923. * but I want to save a few bytes here */
  924. extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
  925. #define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
  926. #define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin"))
  927. extern const int const_int_0;
  928. extern const int const_int_1;
  929. #ifndef BUFSIZ
  930. #define BUFSIZ 4096
  931. #endif
  932. /* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
  933. enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
  934. extern char bb_common_bufsiz1[COMMON_BUFSIZE];
  935. /* This struct is deliberately not defined. */
  936. /* See docs/keep_data_small.txt */
  937. struct globals;
  938. /* '*const' ptr makes gcc optimize code much better.
  939. * Magic prevents ptr_to_globals from going into rodata.
  940. * If you want to assign a value, use PTR_TO_GLOBALS = xxx */
  941. extern struct globals *const ptr_to_globals;
  942. #define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals)
  943. /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  944. * use bb_default_login_shell and following defines.
  945. * If you change LIBBB_DEFAULT_LOGIN_SHELL,
  946. * don't forget to change increment constant. */
  947. #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
  948. extern const char bb_default_login_shell[];
  949. /* "/bin/sh" */
  950. #define DEFAULT_SHELL (bb_default_login_shell+1)
  951. /* "sh" */
  952. #define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
  953. #if ENABLE_FEATURE_DEVFS
  954. # define CURRENT_VC "/dev/vc/0"
  955. # define VC_1 "/dev/vc/1"
  956. # define VC_2 "/dev/vc/2"
  957. # define VC_3 "/dev/vc/3"
  958. # define VC_4 "/dev/vc/4"
  959. # define VC_5 "/dev/vc/5"
  960. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  961. /* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
  962. respective serial ports .. as such, we can't use the common device paths for
  963. these. -- PFM */
  964. # define SC_0 "/dev/ttsc/0"
  965. # define SC_1 "/dev/ttsc/1"
  966. # define SC_FORMAT "/dev/ttsc/%d"
  967. #else
  968. # define SC_0 "/dev/tts/0"
  969. # define SC_1 "/dev/tts/1"
  970. # define SC_FORMAT "/dev/tts/%d"
  971. #endif
  972. # define VC_FORMAT "/dev/vc/%d"
  973. # define LOOP_FORMAT "/dev/loop/%d"
  974. # define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)
  975. # define LOOP_NAME "/dev/loop/"
  976. # define FB_0 "/dev/fb/0"
  977. #else
  978. # define CURRENT_VC "/dev/tty0"
  979. # define VC_1 "/dev/tty1"
  980. # define VC_2 "/dev/tty2"
  981. # define VC_3 "/dev/tty3"
  982. # define VC_4 "/dev/tty4"
  983. # define VC_5 "/dev/tty5"
  984. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  985. # define SC_0 "/dev/ttySC0"
  986. # define SC_1 "/dev/ttySC1"
  987. # define SC_FORMAT "/dev/ttySC%d"
  988. #else
  989. # define SC_0 "/dev/ttyS0"
  990. # define SC_1 "/dev/ttyS1"
  991. # define SC_FORMAT "/dev/ttyS%d"
  992. #endif
  993. # define VC_FORMAT "/dev/tty%d"
  994. # define LOOP_FORMAT "/dev/loop%d"
  995. # define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
  996. # define LOOP_NAME "/dev/loop"
  997. # define FB_0 "/dev/fb0"
  998. #endif
  999. /* The following devices are the same on devfs and non-devfs systems. */
  1000. #define CURRENT_TTY "/dev/tty"
  1001. #define DEV_CONSOLE "/dev/console"
  1002. #ifndef RB_POWER_OFF
  1003. /* Stop system and switch power off if possible. */
  1004. #define RB_POWER_OFF 0x4321fedc
  1005. #endif
  1006. /* Make sure we call functions instead of macros. */
  1007. #undef isalnum
  1008. #undef isalpha
  1009. #undef isascii
  1010. #undef isblank
  1011. #undef iscntrl
  1012. #undef isgraph
  1013. #undef islower
  1014. #undef isprint
  1015. #undef ispunct
  1016. #undef isspace
  1017. #undef isupper
  1018. #undef isxdigit
  1019. /* This one is more efficient - we save ~400 bytes */
  1020. #undef isdigit
  1021. #define isdigit(a) ((unsigned)((a) - '0') <= 9)
  1022. #ifdef DMALLOC
  1023. #include <dmalloc.h>
  1024. #endif
  1025. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  1026. #endif /* __LIBBUSYBOX_H__ */