libbb.h 56 KB

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