X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=include%2Flibbb.h;h=f22e58e27e8b238369ebf06943b4ea02f4f183c3;hb=d3a42391b12b5428a246166e76175edbafbba069;hp=7c1db3f4082a7f16c83c0686119b4736839a3902;hpb=e4dcba1c103dc28e927e004791e331aaf604383d;p=oweals%2Fbusybox.git diff --git a/include/libbb.h b/include/libbb.h index 7c1db3f40..f22e58e27 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -20,12 +20,23 @@ #include #include #include +#if defined __UCLIBC__ /* TODO: and glibc? */ +/* use inlined versions of these: */ +# define sigfillset(s) __sigfillset(s) +# define sigemptyset(s) __sigemptyset(s) +# define sigisemptyset(s) __sigisemptyset(s) +#endif #include #include #include #include #include #include +/* There are two incompatible basename's, let not use them! */ +/* See the dirname/basename man page for details */ +#include /* dirname,basename */ +#undef basename +#define basename dont_use_basename #include #include #include @@ -33,23 +44,48 @@ #include #include #include +#ifndef major +# include +#endif #include #include #include -#include #include +#include +#include +#if ENABLE_FEATURE_SHADOWPASSWDS +# if !ENABLE_USE_BB_SHADOW +/* If using busybox's shadow implementation, do not include the shadow.h + * header as the toolchain may not provide it at all. + */ +# include +# endif +#endif +#if defined(ANDROID) || defined(__ANDROID__) +# define endpwent() ((void)0) +# define endgrent() ((void)0) +#endif #ifdef HAVE_MNTENT_H # include #endif #ifdef HAVE_SYS_STATFS_H # include #endif +/* Don't do this here: + * #include + * Some linux/ includes pull in conflicting definition + * of struct sysinfo (only in some toolchanins), which breaks build. + * Include sys/sysinfo.h only in those files which need it. + */ #if ENABLE_SELINUX # include # include # include # include #endif +#if ENABLE_FEATURE_UTMP +# include +#endif #if ENABLE_LOCALE_SUPPORT # include #else @@ -58,15 +94,18 @@ #ifdef DMALLOC # include #endif -#include -#include -#if ENABLE_FEATURE_SHADOWPASSWDS -# if !ENABLE_USE_BB_SHADOW -/* If using busybox's shadow implementation, do not include the shadow.h - * header as the toolchain may not provide it at all. - */ -# include -# endif +/* Just in case libc doesn't define some of these... */ +#ifndef _PATH_PASSWD +#define _PATH_PASSWD "/etc/passwd" +#endif +#ifndef _PATH_GROUP +#define _PATH_GROUP "/etc/group" +#endif +#ifndef _PATH_SHADOW +#define _PATH_SHADOW "/etc/shadow" +#endif +#ifndef _PATH_GSHADOW +#define _PATH_GSHADOW "/etc/gshadow" #endif #if defined __FreeBSD__ || defined __OpenBSD__ # include @@ -84,6 +123,15 @@ typedef unsigned socklen_t; # endif #endif +#ifndef HAVE_CLEARENV +# define clearenv() do { if (environ) environ[0] = NULL; } while (0) +#endif +#ifndef HAVE_FDATASYNC +# define fdatasync fsync +#endif +#ifndef HAVE_XTABS +# define XTABS TAB3 +#endif /* Some libc's forget to declare these, do it ourself */ @@ -94,31 +142,6 @@ int vdprintf(int d, const char *format, va_list ap); #endif /* klogctl is in libc's klog.h, but we cheat and not #include that */ int klogctl(int type, char *b, int len); -/* This is declared here rather than #including in order to avoid - * confusing the two versions of basename. See the dirname/basename man page - * for details. */ -#if !defined __FreeBSD__ -char *dirname(char *path); -#endif -/* Include our own copy of struct sysinfo to avoid binary compatibility - * problems with Linux 2.4, which changed things. Grumble, grumble. */ -struct sysinfo { - long uptime; /* Seconds since boot */ - unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ - unsigned long totalram; /* Total usable main memory size */ - unsigned long freeram; /* Available memory size */ - unsigned long sharedram; /* Amount of shared memory */ - unsigned long bufferram; /* Memory used by buffers */ - unsigned long totalswap; /* Total swap space size */ - unsigned long freeswap; /* swap space still available */ - unsigned short procs; /* Number of current processes */ - unsigned short pad; /* Padding needed for m68k */ - unsigned long totalhigh; /* Total high memory size */ - unsigned long freehigh; /* Available high memory size */ - unsigned int mem_unit; /* Memory unit size in bytes */ - char _f[20 - 2 * sizeof(long) - sizeof(int)]; /* Padding: libc5 uses this.. */ -}; -int sysinfo(struct sysinfo* info); #ifndef PATH_MAX # define PATH_MAX 256 #endif @@ -127,6 +150,30 @@ int sysinfo(struct sysinfo* info); #endif +/* Busybox does not use threads, we can speed up stdio. */ +#ifdef HAVE_UNLOCKED_STDIO +# undef getc +# define getc(stream) getc_unlocked(stream) +# undef getchar +# define getchar() getchar_unlocked() +# undef putc +# define putc(c, stream) putc_unlocked(c, stream) +# undef putchar +# define putchar(c) putchar_unlocked(c) +# undef fgetc +# define fgetc(stream) getc_unlocked(stream) +# undef fputc +# define fputc(c, stream) putc_unlocked(c, stream) +#endif +/* Above functions are required by POSIX.1-2008, below ones are extensions */ +#ifdef HAVE_UNLOCKED_LINE_OPS +# undef fgets +# define fgets(s, n, stream) fgets_unlocked(s, n, stream) +# undef fputs +# define fputs(s, stream) fputs_unlocked(s, stream) +#endif + + /* Make all declarations hidden (-fvisibility flag only affects definitions) */ /* (don't include system headers after this until corresponding pop!) */ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN @@ -168,7 +215,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN # if ULONG_MAX > 0xffffffff /* "long" is long enough on this system */ typedef unsigned long uoff_t; -# define XATOOFF(a) xatoul_range(a, 0, LONG_MAX) +# define XATOOFF(a) xatoul_range((a), 0, LONG_MAX) /* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */ # define BB_STRTOOFF bb_strtoul # define STRTOOFF strtoul @@ -177,7 +224,7 @@ typedef unsigned long uoff_t; # else /* "long" is too short, need "long long" */ typedef unsigned long long uoff_t; -# define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX) +# define XATOOFF(a) xatoull_range((a), 0, LLONG_MAX) # define BB_STRTOOFF bb_strtoull # define STRTOOFF strtoull # define OFF_FMT "ll" @@ -194,7 +241,7 @@ typedef unsigned long uoff_t; # define OFF_FMT "l" # else typedef unsigned long uoff_t; -# define XATOOFF(a) xatoul_range(a, 0, LONG_MAX) +# define XATOOFF(a) xatoul_range((a), 0, LONG_MAX) # define BB_STRTOOFF bb_strtoul # define STRTOOFF strtol # define OFF_FMT "l" @@ -202,6 +249,12 @@ typedef unsigned long uoff_t; #endif /* scary. better ideas? (but do *test* them first!) */ #define OFF_T_MAX ((off_t)~((off_t)1 << (sizeof(off_t)*8-1))) +/* Users report bionic to use 32-bit off_t even if LARGEFILE support is requested. + * We misdetected that. Don't let it build: + */ +struct BUG_off_t_size_is_misdetected { + char BUG_off_t_size_is_misdetected[sizeof(off_t) == sizeof(uoff_t) ? 1 : -1]; +}; /* Some useful definitions */ #undef FALSE @@ -211,13 +264,6 @@ typedef unsigned long uoff_t; #undef SKIP #define SKIP ((int) 2) -/* for mtab.c */ -#define MTAB_GETMOUNTPT '1' -#define MTAB_GETDEVICE '2' - -#define BUF_SIZE 8192 -#define EXPAND_ALLOC 1024 - /* Macros for min/max. */ #ifndef MIN #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -272,7 +318,7 @@ extern char *strrstr(const char *haystack, const char *needle) FAST_FUNC; //TODO: supply a pointer to char[11] buffer (avoid statics)? extern const char *bb_mode_string(mode_t mode) FAST_FUNC; -extern int is_directory(const char *name, int followLinks, struct stat *statBuf) FAST_FUNC; +extern int is_directory(const char *name, int followLinks) FAST_FUNC; enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */ FILEUTILS_PRESERVE_STATUS = 1 << 0, /* -p */ FILEUTILS_DEREFERENCE = 1 << 1, /* !-d */ @@ -287,6 +333,7 @@ enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */ FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 9, /* -c */ FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10, #endif + FILEUTILS_IGNORE_CHMOD_ERR = 1 << 11, }; #define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c") extern int remove_file(const char *path, int flags) FAST_FUNC; @@ -323,6 +370,7 @@ extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size) FAST_FUNC; /* "short" copy can be detected by return value < size */ /* this helper yells "short read!" if param is not -1 */ extern void complain_copyfd_and_die(off_t sz) NORETURN FAST_FUNC; + extern char bb_process_escape_sequence(const char **ptr) FAST_FUNC; char* strcpy_and_process_escape_sequences(char *dst, const char *src) FAST_FUNC; /* xxxx_strip version can modify its parameter: @@ -331,13 +379,17 @@ char* strcpy_and_process_escape_sequences(char *dst, const char *src) FAST_FUNC; * "abc/def" -> "def" * "abc/def/" -> "def" !! */ -extern char *bb_get_last_path_component_strip(char *path) FAST_FUNC; +char *bb_get_last_path_component_strip(char *path) FAST_FUNC; /* "abc/def/" -> "" and it never modifies 'path' */ -extern char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC; +char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC; +/* Simpler version: does not special case "/" string */ +const char *bb_basename(const char *name) FAST_FUNC; +/* NB: can violate const-ness (similarly to strchr) */ +char *last_char_is(const char *s, int c) FAST_FUNC; -int ndelay_on(int fd) FAST_FUNC; -int ndelay_off(int fd) FAST_FUNC; -int close_on_exec_on(int fd) FAST_FUNC; +void ndelay_on(int fd) FAST_FUNC; +void ndelay_off(int fd) FAST_FUNC; +void close_on_exec_on(int fd) FAST_FUNC; void xdup2(int, int) FAST_FUNC; void xmove_fd(int, int) FAST_FUNC; @@ -517,12 +569,7 @@ enum { * and if kernel doesn't support it, fall back to IPv4. * This is useful if you plan to bind to resulting local lsa. */ -#if ENABLE_FEATURE_IPV6 int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type) FAST_FUNC; -#else -int xsocket_type(len_and_sockaddr **lsap, int sock_type) FAST_FUNC; -#define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type)) -#endif int xsocket_stream(len_and_sockaddr **lsap) FAST_FUNC; /* Create server socket bound to bindaddr:port. bindaddr can be NULL, * numeric IP ("N.N.N.N") or numeric IPv6 address, @@ -562,7 +609,7 @@ len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t /* Assign sin[6]_port member if the socket is an AF_INET[6] one, * otherwise no-op. Useful for ftp. * NB: does NOT do htons() internally, just direct assignment. */ -void set_nport(len_and_sockaddr *lsa, unsigned port) FAST_FUNC; +void set_nport(struct sockaddr *sa, unsigned port) FAST_FUNC; /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */ int get_nport(const struct sockaddr *sa) FAST_FUNC; /* Reverse DNS. Returns NULL on failure. */ @@ -591,6 +638,7 @@ ssize_t recv_from_to(int fd, void *buf, size_t len, int flags, struct sockaddr *to, socklen_t sa_size) FAST_FUNC; +uint16_t inet_cksum(uint16_t *addr, int len) FAST_FUNC; char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC; char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC; @@ -635,17 +683,20 @@ void *malloc_or_warn(size_t size) FAST_FUNC RETURNS_MALLOC; void *xmalloc(size_t size) FAST_FUNC RETURNS_MALLOC; void *xzalloc(size_t size) FAST_FUNC RETURNS_MALLOC; void *xrealloc(void *old, size_t size) FAST_FUNC; -/* After xrealloc_vector(v, 4, idx) it's ok to use +/* After v = xrealloc_vector(v, SHIFT, idx) it's ok to use * at least v[idx] and v[idx+1], for all idx values. - * shift specifies how many new elements are added (1: 2, 2: 4... 8: 256...) - * when all elements are used up. New elements are zeroed out. */ + * SHIFT specifies how many new elements are added (1:2, 2:4, ..., 8:256...) + * when all elements are used up. New elements are zeroed out. + * xrealloc_vector(v, SHIFT, idx) *MUST* be called with consecutive IDXs - + * skipping an index is a bad bug - it may miss a realloc! + */ #define xrealloc_vector(vector, shift, idx) \ xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx)) void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC; extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC; -extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC; +extern ssize_t nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR) FAST_FUNC; // NB: will return short read on error, not -1, // if some data was read before error occurred extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC; @@ -656,25 +707,31 @@ extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FA // Reads one line a-la fgets (but doesn't save terminating '\n'). // Reads byte-by-byte. Useful when it is important to not read ahead. // Bytes are appended to pfx (which must be malloced, or NULL). -extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; +extern char *xmalloc_reads(int fd, size_t *maxsz_p) FAST_FUNC; /* Reads block up to *maxsz_p (default: INT_MAX - 4095) */ extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; /* Returns NULL if file can't be opened (default max size: INT_MAX - 4095) */ extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; -/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */ -#if ENABLE_FEATURE_SEAMLESS_LZMA \ +/* Never returns NULL */ +extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; + +#define SEAMLESS_COMPRESSION (0 \ + || ENABLE_FEATURE_SEAMLESS_XZ \ + || ENABLE_FEATURE_SEAMLESS_LZMA \ || ENABLE_FEATURE_SEAMLESS_BZ2 \ || ENABLE_FEATURE_SEAMLESS_GZ \ - /* || ENABLE_FEATURE_SEAMLESS_Z */ -extern void setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) FAST_FUNC; -#else -# define setup_unzip_on_fd(...) ((void)0) -#endif + || ENABLE_FEATURE_SEAMLESS_Z) + +#if SEAMLESS_COMPRESSION +/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */ +extern int setup_unzip_on_fd(int fd, int fail_if_not_detected) FAST_FUNC; /* Autodetects .gz etc */ extern int open_zipped(const char *fname) FAST_FUNC; +#else +# define setup_unzip_on_fd(...) (0) +# define open_zipped(fname) open((fname), O_RDONLY); +#endif extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; -/* Never returns NULL */ -extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; extern ssize_t safe_write(int fd, const void *buf, size_t count) FAST_FUNC; // NB: will return short write on error, not -1, @@ -692,8 +749,12 @@ extern void xclose(int fd) FAST_FUNC; /* Reads and prints to stdout till eof, then closes FILE. Exits on error: */ extern void xprint_and_close_file(FILE *file) FAST_FUNC; +/* Reads a line from a text file, up to a newline or NUL byte, inclusive. + * Returns malloc'ed char*. If end is NULL '\n' isn't considered + * end of line. If end isn't NULL, length of the chunk is stored in it. + * Returns NULL if EOF/error. + */ extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC; -extern char *bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) FAST_FUNC; /* Reads up to (and including) TERMINATING_STRING: */ extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC RETURNS_MALLOC; /* Same, with limited max size, and returns the length (excluding NUL): */ @@ -735,7 +796,6 @@ void qsort_string_vector(char **sv, unsigned count) FAST_FUNC; int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms) FAST_FUNC; char *safe_gethostname(void) FAST_FUNC; -char *safe_getdomainname(void) FAST_FUNC; /* Convert each alpha char in str to lower-case */ char* str_tolower(char *str) FAST_FUNC; @@ -758,9 +818,9 @@ void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_F const char *make_human_readable_str(unsigned long long size, unsigned long block_size, unsigned long display_unit) FAST_FUNC; /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */ -char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC; +char *bin2hex(char *dst, const char *src, int count) FAST_FUNC; /* Reverse */ -char* hex2bin(char *dst, const char *str, int count) FAST_FUNC; +char* hex2bin(char *dst, const char *src, int count) FAST_FUNC; /* Generate a UUID */ void generate_uuid(uint8_t *buf) FAST_FUNC; @@ -812,14 +872,14 @@ char* xuid2uname(uid_t uid) FAST_FUNC; char* xgid2group(gid_t gid) FAST_FUNC; char* uid2uname(uid_t uid) FAST_FUNC; char* gid2group(gid_t gid) FAST_FUNC; -char* uid2uname_utoa(long uid) FAST_FUNC; -char* gid2group_utoa(long gid) FAST_FUNC; +char* uid2uname_utoa(uid_t uid) FAST_FUNC; +char* gid2group_utoa(gid_t gid) FAST_FUNC; /* versions which cache results (useful for ps, ls etc) */ const char* get_cached_username(uid_t uid) FAST_FUNC; const char* get_cached_groupname(gid_t gid) FAST_FUNC; void clear_username_cache(void) FAST_FUNC; /* internally usernames are saved in fixed-sized char[] buffers */ -enum { USERNAME_MAX_SIZE = 16 - sizeof(int) }; +enum { USERNAME_MAX_SIZE = 32 - sizeof(uid_t) }; #if ENABLE_FEATURE_CHECK_NAMES void die_if_bad_username(const char* name) FAST_FUNC; #else @@ -834,6 +894,7 @@ void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const # define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0) #endif + int execable_file(const char *name) FAST_FUNC; char *find_execable(const char *filename, char **PATHp) FAST_FUNC; int exists_execable(const char *filename) FAST_FUNC; @@ -842,14 +903,16 @@ int exists_execable(const char *filename) FAST_FUNC; * but it may exec busybox and call applet instead of searching PATH. */ #if ENABLE_FEATURE_PREFER_APPLETS -int bb_execvp(const char *file, char *const argv[]) FAST_FUNC; -#define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd) +int BB_EXECVP(const char *file, char *const argv[]) FAST_FUNC; #define BB_EXECLP(prog,cmd,...) \ - execlp((find_applet_by_name(prog) >= 0) ? CONFIG_BUSYBOX_EXEC_PATH : prog, \ - cmd, __VA_ARGS__) + do { \ + if (find_applet_by_name(prog) >= 0) \ + execlp(bb_busybox_exec_path, cmd, __VA_ARGS__); \ + execlp(prog, cmd, __VA_ARGS__); \ + } while (0) #else #define BB_EXECVP(prog,cmd) execvp(prog,cmd) -#define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__) +#define BB_EXECLP(prog,cmd,...) execlp(prog,cmd,__VA_ARGS__) #endif int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC; @@ -883,19 +946,8 @@ pid_t wait_any_nohang(int *wstat) FAST_FUNC; int wait4pid(pid_t pid) FAST_FUNC; /* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */ int spawn_and_wait(char **argv) FAST_FUNC; -struct nofork_save_area { - jmp_buf die_jmp; - const char *applet_name; - uint32_t option_mask32; - int die_sleep; - uint8_t xfunc_error_retval; - smallint saved; -}; -void save_nofork_data(struct nofork_save_area *save) FAST_FUNC; -void restore_nofork_data(struct nofork_save_area *save) FAST_FUNC; /* Does NOT check that applet is NOFORK, just blindly runs it */ int run_nofork_applet(int applet_no, char **argv) FAST_FUNC; -int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv) FAST_FUNC; /* Helpers for daemonization. * @@ -925,6 +977,7 @@ enum { DAEMON_DEVNULL_STDIO = 2, DAEMON_CLOSE_EXTRA_FDS = 4, DAEMON_ONLY_SANITIZE = 8, /* internal use */ + DAEMON_DOUBLE_FORK = 16, /* double fork to avoid controlling tty */ }; #if BB_MMU enum { re_execed = 0 }; @@ -933,6 +986,9 @@ enum { # define bb_daemonize(flags) bb_daemonize_or_rexec(flags, bogus) #else extern bool re_execed; + /* Note: re_exec() and fork_or_rexec() do argv[0][0] |= 0x80 on NOMMU! + * _Parent_ needs to undo it if it doesn't want to have argv[0] mangled. + */ void re_exec(char **argv) NORETURN FAST_FUNC; pid_t fork_or_rexec(char **argv) FAST_FUNC; int BUG_fork_is_unavailable_on_nommu(void) FAST_FUNC; @@ -962,9 +1018,13 @@ extern uint32_t option_mask32; extern uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC; +/* Having next pointer as a first member allows easy creation + * of "llist-compatible" structs, and using llist_FOO functions + * on them. + */ typedef struct llist_t { - char *data; struct llist_t *link; + char *data; } llist_t; void llist_add_to(llist_t **old_head, void *data) FAST_FUNC; void llist_add_to_end(llist_t **list_head, void *data) FAST_FUNC; @@ -1115,7 +1175,7 @@ extern int del_loop(const char *device) FAST_FUNC; /* If *devname is not NULL, use that name, otherwise try to find free one, * malloc and return it in *devname. * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */ -extern int set_loop(char **devname, const char *file, unsigned long long offset) FAST_FUNC; +extern int set_loop(char **devname, const char *file, unsigned long long offset, int ro) FAST_FUNC; /* Like bb_ask below, but asks on stdin with no timeout. */ char *bb_ask_stdin(const char * prompt) FAST_FUNC; @@ -1136,18 +1196,20 @@ enum { PARSE_MIN_DIE = 0x00100000, // die if < min tokens found // keep a copy of current line PARSE_KEEP_COPY = 0x00200000 * ENABLE_FEATURE_CROND_D, -// PARSE_ESCAPE = 0x00400000, // process escape sequences in tokens + PARSE_EOL_COMMENTS = 0x00400000, // comments are recognized even if they aren't the first char // NORMAL is: // * remove leading and trailing delimiters and collapse // multiple delimiters into one // * warn and continue if less than mintokens delimiters found // * grab everything into last token - PARSE_NORMAL = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY, + // * comments are recognized even if they aren't the first char + PARSE_NORMAL = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY | PARSE_EOL_COMMENTS, }; typedef struct parser_t { FILE *fp; - char *line; char *data; + char *line, *nline; + size_t line_alloc, nline_alloc; int lineno; } parser_t; parser_t* config_open(const char *filename) FAST_FUNC; @@ -1163,10 +1225,8 @@ void config_close(parser_t *parser) FAST_FUNC; * If path is NULL, it is assumed to be "/". * filename should not be NULL. */ char *concat_path_file(const char *path, const char *filename) FAST_FUNC; +/* Returns NULL on . and .. */ char *concat_subpath_file(const char *path, const char *filename) FAST_FUNC; -const char *bb_basename(const char *name) FAST_FUNC; -/* NB: can violate const-ness (similarly to strchr) */ -char *last_char_is(const char *s, int c) FAST_FUNC; int bb_make_directory(char *path, long mode, int flags) FAST_FUNC; @@ -1179,10 +1239,17 @@ char *bb_simplify_path(const char *path) FAST_FUNC; /* Returns ptr to NUL */ char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC; -#define FAIL_DELAY 3 +#define LOGIN_FAIL_DELAY 3 extern void bb_do_delay(int seconds) FAST_FUNC; extern void change_identity(const struct passwd *pw) FAST_FUNC; extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC; + +/* Returns $SHELL, getpwuid(getuid())->pw_shell, or DEFAULT_SHELL. + * Note that getpwuid result might need xstrdup'ing + * if there is a possibility of intervening getpwxxx() calls. + */ +const char *get_shell_name(void); + #if ENABLE_SELINUX extern void renew_current_security_context(void) FAST_FUNC; extern void set_current_security_context(security_context_t sid) FAST_FUNC; @@ -1195,6 +1262,12 @@ extern void selinux_preserve_fcontext(int fdesc) FAST_FUNC; #endif extern void selinux_or_die(void) FAST_FUNC; + +/* systemd support */ +#define SD_LISTEN_FDS_START 3 +int sd_listen_fds(void); + + /* setup_environment: * if chdir pw->pw_dir: ok: else if to_tmp == 1: goto /tmp else: goto / or die * if clear_env = 1: cd(pw->pw_dir), clear environment, then set @@ -1213,6 +1286,7 @@ extern void selinux_or_die(void) FAST_FUNC; #define SETUP_ENV_CHANGEENV (1 << 0) #define SETUP_ENV_CLEARENV (1 << 1) #define SETUP_ENV_TO_TMP (1 << 2) +#define SETUP_ENV_NO_CHDIR (1 << 4) extern void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC; extern int correct_password(const struct passwd *pw) FAST_FUNC; /* Returns a malloced string */ @@ -1221,14 +1295,19 @@ extern int correct_password(const struct passwd *pw) FAST_FUNC; #endif extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_FUNC; extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC; -/* rnd is additional random input. New one is returned. +/* + * rnd is additional random input. New one is returned. * Useful if you call crypt_make_salt many times in a row: * rnd = crypt_make_salt(buf1, 4, 0); * rnd = crypt_make_salt(buf2, 4, rnd); * rnd = crypt_make_salt(buf3, 4, rnd); * (otherwise we risk having same salt generated) */ -extern int crypt_make_salt(char *p, int cnt, int rnd) FAST_FUNC; +extern int crypt_make_salt(char *p, int cnt /*, int rnd*/) FAST_FUNC; +/* "$N$" + sha_salt_16_bytes + NUL */ +#define MAX_PW_SALT_LEN (3 + 16 + 1) +extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC; + /* Returns number of lines changed, or -1 on error */ #if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP) @@ -1275,7 +1354,7 @@ void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) FAST void reset_ino_dev_hashtable(void) FAST_FUNC; #ifdef __GLIBC__ /* At least glibc has horrendously large inline for this, so wrap it */ -unsigned long long bb_makedev(unsigned int major, unsigned int minor) FAST_FUNC; +unsigned long long bb_makedev(unsigned major, unsigned minor) FAST_FUNC; #undef makedev #define makedev(a,b) bb_makedev(a,b) #endif @@ -1296,25 +1375,37 @@ enum { KEYCODE_DELETE = -9, KEYCODE_PAGEUP = -10, KEYCODE_PAGEDOWN = -11, - - KEYCODE_CTRL_UP = KEYCODE_UP & ~0x40, - KEYCODE_CTRL_DOWN = KEYCODE_DOWN & ~0x40, - KEYCODE_CTRL_RIGHT = KEYCODE_RIGHT & ~0x40, - KEYCODE_CTRL_LEFT = KEYCODE_LEFT & ~0x40, + // -12 is reserved for Alt/Ctrl/Shift-TAB #if 0 - KEYCODE_FUN1 = -12, - KEYCODE_FUN2 = -13, - KEYCODE_FUN3 = -14, - KEYCODE_FUN4 = -15, - KEYCODE_FUN5 = -16, - KEYCODE_FUN6 = -17, - KEYCODE_FUN7 = -18, - KEYCODE_FUN8 = -19, - KEYCODE_FUN9 = -20, - KEYCODE_FUN10 = -21, - KEYCODE_FUN11 = -22, - KEYCODE_FUN12 = -23, + KEYCODE_FUN1 = -13, + KEYCODE_FUN2 = -14, + KEYCODE_FUN3 = -15, + KEYCODE_FUN4 = -16, + KEYCODE_FUN5 = -17, + KEYCODE_FUN6 = -18, + KEYCODE_FUN7 = -19, + KEYCODE_FUN8 = -20, + KEYCODE_FUN9 = -21, + KEYCODE_FUN10 = -22, + KEYCODE_FUN11 = -23, + KEYCODE_FUN12 = -24, #endif + /* Be sure that last defined value is small enough + * to not interfere with Alt/Ctrl/Shift bits. + * So far we do not exceed -31 (0xfff..fffe1), + * which gives us three upper bits in LSB to play with. + */ + //KEYCODE_SHIFT_TAB = (-12) & ~0x80, + //KEYCODE_SHIFT_... = KEYCODE_... & ~0x80, + //KEYCODE_CTRL_UP = KEYCODE_UP & ~0x40, + //KEYCODE_CTRL_DOWN = KEYCODE_DOWN & ~0x40, + KEYCODE_CTRL_RIGHT = KEYCODE_RIGHT & ~0x40, + KEYCODE_CTRL_LEFT = KEYCODE_LEFT & ~0x40, + //KEYCODE_ALT_UP = KEYCODE_UP & ~0x20, + //KEYCODE_ALT_DOWN = KEYCODE_DOWN & ~0x20, + KEYCODE_ALT_RIGHT = KEYCODE_RIGHT & ~0x20, + KEYCODE_ALT_LEFT = KEYCODE_LEFT & ~0x20, + KEYCODE_CURSOR_POS = -0x100, /* 0xfff..fff00 */ /* How long is the longest ESC sequence we know? * We want it big enough to be able to contain @@ -1341,8 +1432,9 @@ void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; #if ENABLE_FEATURE_EDITING /* It's NOT just ENABLEd or disabled. It's a number: */ -# ifdef CONFIG_FEATURE_EDITING_HISTORY +# if defined CONFIG_FEATURE_EDITING_HISTORY && CONFIG_FEATURE_EDITING_HISTORY > 0 # define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0) +unsigned size_from_HISTFILESIZE(const char *hp); # else # define MAX_HISTORY 0 # endif @@ -1352,7 +1444,14 @@ typedef struct line_input_t { # if MAX_HISTORY int cnt_history; int cur_history; + int max_history; /* must never be <= 0 */ # if ENABLE_FEATURE_EDITING_SAVEHISTORY + /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT: + * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are + * in on-disk history" + * if FEATURE_EDITING_SAVE_ON_EXIT: "how many in-memory lines are + * also in on-disk history (and thus need to be skipped on save)" + */ unsigned cnt_history_in_file; const char *hist_file; # endif @@ -1360,27 +1459,30 @@ typedef struct line_input_t { # endif } line_input_t; enum { - DO_HISTORY = 1 * (MAX_HISTORY > 0), - SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY, - TAB_COMPLETION = 4 * ENABLE_FEATURE_TAB_COMPLETION, - USERNAME_COMPLETION = 8 * ENABLE_FEATURE_USERNAME_COMPLETION, - VI_MODE = 0x10 * ENABLE_FEATURE_EDITING_VI, - WITH_PATH_LOOKUP = 0x20, - FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION, + DO_HISTORY = 1 * (MAX_HISTORY > 0), + TAB_COMPLETION = 2 * ENABLE_FEATURE_TAB_COMPLETION, + USERNAME_COMPLETION = 4 * ENABLE_FEATURE_USERNAME_COMPLETION, + VI_MODE = 8 * ENABLE_FEATURE_EDITING_VI, + WITH_PATH_LOOKUP = 0x10, + FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION, }; line_input_t *new_line_input_t(int flags) FAST_FUNC; /* So far static: void free_line_input_t(line_input_t *n) FAST_FUNC; */ -/* maxsize must be >= 2. +/* + * maxsize must be >= 2. * Returns: * -1 on read errors or EOF, or on bare Ctrl-D, * 0 on ctrl-C (the line entered is still returned in 'command'), * >0 length of input string, including terminating '\n' */ -int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state) FAST_FUNC; +int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) FAST_FUNC; +# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT +void save_history(line_input_t *st); +# endif #else #define MAX_HISTORY 0 int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC; -#define read_line_input(prompt, command, maxsize, state) \ +#define read_line_input(state, prompt, command, maxsize, timeout) \ read_line_input(prompt, command, maxsize) #endif @@ -1426,6 +1528,7 @@ typedef struct procps_status_t { char *argv0; char *exe; IF_SELINUX(char *context;) + IF_FEATURE_SHOW_THREADS(unsigned main_thread_pid;) /* Everything below must contain no ptrs to malloc'ed data: * it is memset(0) for each process in procps_scan() */ unsigned long vsz, rss; /* we round it to kbytes */ @@ -1487,13 +1590,6 @@ enum { PSSCAN_NICE = (1 << 20) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS, PSSCAN_RUIDGID = (1 << 21) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS, PSSCAN_TASKS = (1 << 22) * ENABLE_FEATURE_SHOW_THREADS, - /* These are all retrieved from proc/NN/stat in one go: */ - PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID - /**/ | PSSCAN_COMM | PSSCAN_STATE - /**/ | PSSCAN_VSZ | PSSCAN_RSS - /**/ | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME - /**/ | PSSCAN_TTY | PSSCAN_NICE - /**/ | PSSCAN_CPU }; //procps_status_t* alloc_procps_scan(void) FAST_FUNC; void free_procps_scan(procps_status_t* sp) FAST_FUNC; @@ -1507,15 +1603,25 @@ int starts_with_cpu(const char *str) FAST_FUNC; unsigned get_cpu_count(void) FAST_FUNC; -extern const char bb_uuenc_tbl_base64[]; -extern const char bb_uuenc_tbl_std[]; +/* Use strict=1 if you process input from untrusted source: + * it will return NULL on invalid %xx (bad hex chars) + * and str + 1 if decoded char is / or NUL. + * In non-strict mode, it always succeeds (returns str), + * and also it additionally decoded '+' to space. + */ +char *percent_decode_in_place(char *str, int strict) FAST_FUNC; + + +extern const char bb_uuenc_tbl_base64[] ALIGN1; +extern const char bb_uuenc_tbl_std[] ALIGN1; void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC; enum { BASE64_FLAG_UU_STOP = 0x100, /* Sign-extends to a value which never matches fgetc result: */ BASE64_FLAG_NO_STOP_CHAR = 0x80, }; -void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags); +const char *decode_base64(char **pp_dst, const char *src) FAST_FUNC; +void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; typedef struct md5_ctx_t { uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */ @@ -1557,16 +1663,24 @@ int print_flags_separated(const int *masks, const char *labels, int print_flags(const masks_labels_t *ml, int flags) FAST_FUNC; typedef struct bb_progress_t { - off_t lastsize; - unsigned lastupdate_sec; + unsigned last_size; + unsigned last_update_sec; + unsigned last_change_sec; unsigned start_sec; - smallint inited; + const char *curfile; } bb_progress_t; -void bb_progress_init(bb_progress_t *p) FAST_FUNC; -void bb_progress_update(bb_progress_t *p, const char *curfile, - off_t beg_range, off_t transferred, - off_t totalsize) FAST_FUNC; +#define is_bb_progress_inited(p) ((p)->curfile != NULL) +#define bb_progress_free(p) do { \ + if (ENABLE_UNICODE_SUPPORT) free((char*)((p)->curfile)); \ + (p)->curfile = NULL; \ +} while (0) +void bb_progress_init(bb_progress_t *p, const char *curfile) FAST_FUNC; +void bb_progress_update(bb_progress_t *p, + uoff_t beg_range, + uoff_t transferred, + uoff_t totalsize) FAST_FUNC; + extern const char *applet_name; @@ -1580,41 +1694,41 @@ extern const char *applet_name; * Therefore now we use #defines. */ /* "BusyBox vN.N.N (timestamp or extra_version)" */ -extern const char bb_banner[]; -extern const char bb_msg_memory_exhausted[]; -extern const char bb_msg_invalid_date[]; +extern const char bb_banner[] ALIGN1; +extern const char bb_msg_memory_exhausted[] ALIGN1; +extern const char bb_msg_invalid_date[] ALIGN1; #define bb_msg_read_error "read error" #define bb_msg_write_error "write error" -extern const char bb_msg_unknown[]; -extern const char bb_msg_can_not_create_raw_socket[]; -extern const char bb_msg_perm_denied_are_you_root[]; -extern const char bb_msg_you_must_be_root[]; -extern const char bb_msg_requires_arg[]; -extern const char bb_msg_invalid_arg[]; -extern const char bb_msg_standard_input[]; -extern const char bb_msg_standard_output[]; +extern const char bb_msg_unknown[] ALIGN1; +extern const char bb_msg_can_not_create_raw_socket[] ALIGN1; +extern const char bb_msg_perm_denied_are_you_root[] ALIGN1; +extern const char bb_msg_you_must_be_root[] ALIGN1; +extern const char bb_msg_requires_arg[] ALIGN1; +extern const char bb_msg_invalid_arg[] ALIGN1; +extern const char bb_msg_standard_input[] ALIGN1; +extern const char bb_msg_standard_output[] ALIGN1; /* NB: (bb_hexdigits_upcase[i] | 0x20) -> lowercase hex digit */ -extern const char bb_hexdigits_upcase[]; +extern const char bb_hexdigits_upcase[] ALIGN1; -extern const char bb_path_wtmp_file[]; +extern const char bb_path_wtmp_file[] ALIGN1; /* Busybox mount uses either /proc/mounts or /etc/mtab to * get the list of currently mounted filesystems */ #define bb_path_mtab_file IF_FEATURE_MTAB_SUPPORT("/etc/mtab")IF_NOT_FEATURE_MTAB_SUPPORT("/proc/mounts") -#define bb_path_passwd_file "/etc/passwd" -#define bb_path_shadow_file "/etc/shadow" -#define bb_path_gshadow_file "/etc/gshadow" -#define bb_path_group_file "/etc/group" +#define bb_path_passwd_file _PATH_PASSWD +#define bb_path_group_file _PATH_GROUP +#define bb_path_shadow_file _PATH_SHADOW +#define bb_path_gshadow_file _PATH_GSHADOW #define bb_path_motd_file "/etc/motd" #define bb_dev_null "/dev/null" -extern const char bb_busybox_exec_path[]; +extern const char bb_busybox_exec_path[] ALIGN1; /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin, * but I want to save a few bytes here */ -extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */ +extern const char bb_PATH_root_path[] ALIGN1; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */ #define bb_default_root_path (bb_PATH_root_path + sizeof("PATH")) #define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin")) @@ -1644,7 +1758,7 @@ extern struct globals *const ptr_to_globals; * If you change LIBBB_DEFAULT_LOGIN_SHELL, * don't forget to change increment constant. */ #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh" -extern const char bb_default_login_shell[]; +extern const char bb_default_login_shell[] ALIGN1; /* "/bin/sh" */ #define DEFAULT_SHELL (bb_default_login_shell+1) /* "sh" */