fix trivial compile errors found by randomconfig run
[oweals/busybox.git] / include / libbb.h
index 6956aaf7b75000ba6f2008b009fa60f4bbaedeaf..32bb3113d910d8f9e2215eb7134d7150d3d5615c 100644 (file)
 /* scary. better ideas? (but do *test* them first!) */
 #define OFF_T_MAX  ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
 
-/* This structure defines protocol families and their handlers. */
-struct aftype {
-       const char *name;
-       const char *title;
-       int af;
-       int alen;
-       char *(*print) (unsigned char *);
-       const char *(*sprint) (struct sockaddr *, int numeric);
-       int (*input) (/*int type,*/ const char *bufp, struct sockaddr *);
-       void (*herror) (char *text);
-       int (*rprint) (int options);
-       int (*rinput) (int typ, int ext, char **argv);
-
-       /* may modify src */
-       int (*getmask) (char *src, struct sockaddr * mask, char *name);
-};
-
-/* This structure defines hardware protocols and their handlers. */
-struct hwtype {
-       const char *name;
-       const char *title;
-       int type;
-       int alen;
-       char *(*print) (unsigned char *);
-       int (*input) (const char *, struct sockaddr *);
-       int (*activate) (int fd);
-       int suppress_null_addr;
-};
-
 /* Some useful definitions */
 #undef FALSE
 #define FALSE   ((int) 0)
@@ -269,31 +240,16 @@ char *xrealloc_getcwd_or_warn(char *cwd);
 char *xmalloc_readlink_or_warn(const char *path);
 char *xmalloc_realpath(const char *path);
 
-/* Unlike waitpid, waits ONLY for one process,
- * It's safe to pass negative 'pids' from failed [v]fork -
- * wait4pid will return -1 and ECHILD in errno.
- * IOW: rc = wait4pid(spawn(argv));
- *      if (rc < 0) bb_perror_msg("%s", argv[0]);
- *      if (rc > 0) bb_error_msg("exit code: %d", rc);
- */
-extern int wait4pid(int pid);
-extern int wait_pid(int *wstat, int pid);
-extern int wait_nohang(int *wstat);
+
 //TODO: signal(sid, f) is the same? then why?
 extern void sig_catch(int,void (*)(int));
 //#define sig_ignore(s) (sig_catch((s), SIG_IGN))
 //#define sig_uncatch(s) (sig_catch((s), SIG_DFL))
 extern void sig_block(int);
 extern void sig_unblock(int);
-/* UNUSED: extern void sig_blocknone(void);*/
+/* UNUSED: extern void sig_blocknone(void); */
 extern void sig_pause(void);
 
-#define wait_crashed(w) ((w) & 127)
-#define wait_exitcode(w) ((w) >> 8)
-#define wait_stopsig(w) ((w) >> 8)
-#define wait_stopped(w) (((w) & 127) == 127)
-
-
 
 void xsetgid(gid_t gid);
 void xsetuid(uid_t uid);
@@ -303,6 +259,8 @@ void xunlink(const char *pathname);
 void xstat(const char *pathname, struct stat *buf);
 int xopen(const char *pathname, int flags);
 int xopen3(const char *pathname, int flags, int mode);
+int open_or_warn(const char *pathname, int flags);
+int open3_or_warn(const char *pathname, int flags, int mode);
 off_t xlseek(int fd, off_t offset, int whence);
 off_t fdlength(int fd);
 
@@ -311,6 +269,8 @@ int xsocket(int domain, int type, int protocol);
 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
 void xlisten(int s, int backlog);
 void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen);
+ssize_t xsendto(int s, const  void *buf, size_t len, const struct sockaddr *to,
+                               socklen_t tolen);
 int setsockopt_reuseaddr(int fd);
 int setsockopt_broadcast(int fd);
 /* NB: returns port in host byte order */
@@ -343,7 +303,7 @@ int xsocket_stream(len_and_sockaddr **lsap);
 /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  * numeric IP ("N.N.N.N") or numeric IPv6 address,
  * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
- * If there is no suffix, port argument is used */
+ * Only if there is no suffix, port argument is used */
 int create_and_bind_stream_or_die(const char *bindaddr, int port);
 int create_and_bind_dgram_or_die(const char *bindaddr, int port);
 /* Create client TCP socket connected to peer:port. Peer cannot be NULL.
@@ -412,16 +372,18 @@ extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf,
 //int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; }
 //static ATTRIBUTE_ALWAYS_INLINE
 //int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; }
-#define LONE_DASH(s) ((s)[0] == '-' && !(s)[1])
+#define LONE_DASH(s)     ((s)[0] == '-' && !(s)[1])
 #define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
-#define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
+#define LONE_CHAR(s,c)     ((s)[0] == (c) && !(s)[1])
 #define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
+#define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
 
 /* dmalloc will redefine these to it's own implementation. It is safe
  * to have the prototypes here unconditionally.  */
+extern void *malloc_or_warn(size_t size);
 extern void *xmalloc(size_t size);
-extern void *xrealloc(void *old, size_t size);
 extern void *xzalloc(size_t size);
+extern void *xrealloc(void *old, size_t size);
 
 extern ssize_t safe_read(int fd, void *buf, size_t count);
 extern ssize_t full_read(int fd, void *buf, size_t count);
@@ -455,15 +417,20 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode);
 /* "Opens" stdin if filename is special, else just opens file: */
 extern FILE *fopen_or_warn_stdin(const char *filename);
 
+/* Convert each alpha char in str to lower-case */
+extern char* str_tolower(char *str);
 
-extern char *utoa(unsigned n);
-extern char *itoa(int n);
+char *utoa(unsigned n);
+char *itoa(int n);
 /* Returns a pointer past the formatted number, does NOT null-terminate */
-extern char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
-extern char *itoa_to_buf(int n, char *buf, unsigned buflen);
-extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
-/* Put a string of hex bytes (ala "1b"), return advanced pointer */
-extern char *bin2hex(char *buf, const char *cp, int count);
+char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
+char *itoa_to_buf(int n, char *buf, unsigned buflen);
+void smart_ulltoa5(unsigned long long ul, char buf[5]);
+//TODO: provide pointer to buf (avoid statics)?
+const char *make_human_readable_str(unsigned long long size,
+               unsigned long block_size, unsigned long display_unit);
+/* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
+char *bin2hex(char *buf, const char *cp, int count);
 
 struct suffix_mult {
        const char *suffix;
@@ -508,11 +475,12 @@ void clear_username_cache(void);
 enum { USERNAME_MAX_SIZE = 16 - sizeof(int) };
 
 
+struct bb_applet;
 int execable_file(const char *name);
 char *find_execable(const char *filename);
 int exists_execable(const char *filename);
 
-#if ENABLE_FEATURE_EXEC_PREFER_APPLETS
+#if ENABLE_FEATURE_PREFER_APPLETS
 int bb_execvp(const char *file, char *const argv[]);
 #define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
 #define BB_EXECLP(prog,cmd,...) \
@@ -526,6 +494,36 @@ int bb_execvp(const char *file, char *const argv[]);
 /* NOMMU friendy fork+exec */
 pid_t spawn(char **argv);
 pid_t xspawn(char **argv);
+
+/* Unlike waitpid, waits ONLY for one process,
+ * It's safe to pass negative 'pids' from failed [v]fork -
+ * wait4pid will return -1 (and will not clobber [v]fork's errno).
+ * IOW: rc = wait4pid(spawn(argv));
+ *      if (rc < 0) bb_perror_msg("%s", argv[0]);
+ *      if (rc > 0) bb_error_msg("exit code: %d", rc);
+ */
+int wait4pid(int pid);
+int wait_pid(int *wstat, int pid);
+int wait_nohang(int *wstat);
+#define wait_crashed(w) ((w) & 127)
+#define wait_exitcode(w) ((w) >> 8)
+#define wait_stopsig(w) ((w) >> 8)
+#define wait_stopped(w) (((w) & 127) == 127)
+/* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */
+int spawn_and_wait(char **argv);
+struct nofork_save_area {
+       const struct bb_applet *current_applet;
+       int xfunc_error_retval;
+       uint32_t option_mask32;
+       int die_sleep;
+       smallint saved;
+};
+void save_nofork_data(struct nofork_save_area *save);
+void restore_nofork_data(struct nofork_save_area *save);
+/* Does NOT check that applet is NOFORK, just blindly runs it */
+int run_nofork_applet(const struct bb_applet *a, char **argv);
+int run_nofork_applet_prime(struct nofork_save_area *old, const struct bb_applet *a, char **argv);
+
 /* Helpers for daemonization.
  *
  * bb_daemonize(flags) = daemonize, does not compile on NOMMU
@@ -555,14 +553,14 @@ enum {
        DAEMON_CLOSE_EXTRA_FDS = 4,
        DAEMON_ONLY_SANITIZE = 8, /* internal use */
 };
-#ifndef BB_NOMMU
+#if BB_MMU
   void forkexit_or_rexec(void);
 # define forkexit_or_rexec(argv)            forkexit_or_rexec()
 # define bb_daemonize_or_rexec(flags, argv) bb_daemonize_or_rexec(flags)
 # define bb_daemonize(flags)                bb_daemonize_or_rexec(flags, bogus)
 #else
   void forkexit_or_rexec(char **argv);
-  extern smallint re_execed;
+  extern bool re_execed;
 # define fork()          BUG_fork_is_unavailable_on_nommu()
 # define daemon(a,b)     BUG_daemon_is_unavailable_on_nommu()
 # define bb_daemonize(a) BUG_bb_daemonize_is_unavailable_on_nommu()
@@ -571,6 +569,7 @@ void bb_daemonize_or_rexec(int flags, char **argv);
 void bb_sanitize_stdio(void);
 
 
+// TODO: always error out?
 enum { BB_GETOPT_ERROR = 0x80000000 };
 extern const char *opt_complementary;
 #if ENABLE_GETOPT_LONG
@@ -580,22 +579,27 @@ extern uint32_t option_mask32;
 extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
 
 
-typedef struct llist_s {
+typedef struct llist_t {
        char *data;
-       struct llist_s *link;
+       struct llist_t *link;
 } llist_t;
-extern void llist_add_to(llist_t **old_head, void *data);
-extern void llist_add_to_end(llist_t **list_head, void *data);
-extern void *llist_pop(llist_t **elm);
-extern void llist_unlink(llist_t **head, llist_t *elm);
-extern void llist_free(llist_t *elm, void (*freeit)(void *data));
-extern llist_t* llist_rev(llist_t *list);
+void llist_add_to(llist_t **old_head, void *data);
+void llist_add_to_end(llist_t **list_head, void *data);
+void *llist_pop(llist_t **elm);
+void llist_unlink(llist_t **head, llist_t *elm);
+void llist_free(llist_t *elm, void (*freeit)(void *data));
+llist_t *llist_rev(llist_t *list);
+/* BTW, surprisingly, changing API to 
+ *   llist_t *llist_add_to(llist_t *old_head, void *data)
+ * etc does not result in smaller code... */
+
 
 #if ENABLE_FEATURE_PIDFILE
 int write_pidfile(const char *path);
 #define remove_pidfile(f) ((void)unlink(f))
 #else
-#define write_pidfile(f)  1
+/* Why? #defining it to 1 gives "warning: statement with no effect"... */
+static ATTRIBUTE_ALWAYS_INLINE int write_pidfile(const char *path) { return 1; }
 #define remove_pidfile(f) ((void)0)
 #endif
 
@@ -609,7 +613,8 @@ extern const char *msg_eol;
 extern smallint logmode;
 extern int die_sleep;
 extern int xfunc_error_retval;
-extern void sleep_and_die(void) ATTRIBUTE_NORETURN;
+extern jmp_buf die_jmp;
+extern void xfunc_die(void) ATTRIBUTE_NORETURN;
 extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
 extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
@@ -631,6 +636,7 @@ extern void bb_vinfo_msg(const char *s, va_list p);
 int bb_cat(char** argv);
 int bb_echo(char** argv);
 int bb_test(int argc, char** argv);
+int kill_main(int argc, char **argv);
 #if ENABLE_ROUTE
 void bb_displayroutes(int noresolve, int netstatfmt);
 #endif
@@ -648,6 +654,33 @@ int bbunpack(char **argv,
 int create_icmp_socket(void);
 int create_icmp6_socket(void);
 /* interface.c */
+/* This structure defines protocol families and their handlers. */
+struct aftype {
+       const char *name;
+       const char *title;
+       int af;
+       int alen;
+       char *(*print) (unsigned char *);
+       const char *(*sprint) (struct sockaddr *, int numeric);
+       int (*input) (/*int type,*/ const char *bufp, struct sockaddr *);
+       void (*herror) (char *text);
+       int (*rprint) (int options);
+       int (*rinput) (int typ, int ext, char **argv);
+
+       /* may modify src */
+       int (*getmask) (char *src, struct sockaddr * mask, char *name);
+};
+/* This structure defines hardware protocols and their handlers. */
+struct hwtype {
+       const char *name;
+       const char *title;
+       int type;
+       int alen;
+       char *(*print) (unsigned char *);
+       int (*input) (const char *, struct sockaddr *);
+       int (*activate) (int fd);
+       int suppress_null_addr;
+};
 extern int interface_opt_a;
 int display_interfaces(char *ifname);
 const struct aftype *get_aftype(const char *name);
@@ -656,8 +689,10 @@ const struct hwtype *get_hwntype(int type);
 
 
 #ifndef BUILD_INDIVIDUAL
-extern struct BB_applet *find_applet_by_name(const char *name);
-extern void run_applet_by_name(const char *name, int argc, char **argv);
+extern const struct bb_applet *find_applet_by_name(const char *name);
+/* Returns only if applet is not found. */
+extern void run_applet_and_exit(const char *name, char **argv);
+extern void run_current_applet_and_exit(char **argv) ATTRIBUTE_NORETURN;
 #endif
 
 extern int match_fstype(const struct mntent *mt, const char *fstypes);
@@ -674,10 +709,6 @@ extern int del_loop(const char *device);
 extern int set_loop(char **device, const char *file, unsigned long long offset);
 
 
-//TODO: provide pointer to buf (avoid statics)?
-const char *make_human_readable_str(unsigned long long size,
-               unsigned long block_size, unsigned long display_unit);
-
 //TODO: pass buf pointer or return allocated buf (avoid statics)?
 char *bb_askpass(int timeout, const char * prompt);
 int bb_ask_confirmation(void);
@@ -687,6 +718,7 @@ extern int bb_parse_mode(const char* s, mode_t* theMode);
 
 char *concat_path_file(const char *path, const char *filename);
 char *concat_subpath_file(const char *path, const char *filename);
+/* NB: can violate const-ness (similarly to strchr) */
 char *last_char_is(const char *s, int c);
 
 
@@ -724,15 +756,16 @@ extern int index_in_substr_array(const char * const string_array[], const char *
 extern void print_login_issue(const char *issue_file, const char *tty);
 extern void print_login_prompt(void);
 
+extern void crypt_make_salt(char *p, int cnt);
 
-extern int get_terminal_width_height(const int fd, int *width, int *height);
+int get_terminal_width_height(const int fd, int *width, int *height);
 
 char *is_in_ino_dev_hashtable(const struct stat *statbuf);
 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
 void reset_ino_dev_hashtable(void);
 #ifdef __GLIBC__
 /* At least glibc has horrendously large inline for this, so wrap it */
-extern unsigned long long bb_makedev(unsigned int major, unsigned int minor);
+unsigned long long bb_makedev(unsigned int major, unsigned int minor);
 #undef makedev
 #define makedev(a,b) bb_makedev(a,b)
 #endif
@@ -745,7 +778,7 @@ extern unsigned long long bb_makedev(unsigned int major, unsigned int minor);
 #else
 #define MAX_HISTORY 0
 #endif
-struct line_input_t {
+typedef struct line_input_t {
        int flags;
        const char *path_lookup;
 #if MAX_HISTORY
@@ -754,7 +787,7 @@ struct line_input_t {
        USE_FEATURE_EDITING_SAVEHISTORY(const char *hist_file;)
        char *history[MAX_HISTORY + 1];
 #endif
-};
+} line_input_t;
 enum {
        DO_HISTORY = 1 * (MAX_HISTORY > 0),
        SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
@@ -764,7 +797,6 @@ enum {
        WITH_PATH_LOOKUP = 0x20,
        FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
 };
-typedef struct line_input_t line_input_t;
 line_input_t *new_line_input_t(int flags);
 int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state);
 #else
@@ -786,7 +818,7 @@ typedef struct {
        DIR *dir;
 /* Fields are set to 0/NULL if failed to determine (or not requested) */
        char *cmd;
-       unsigned long vsz;
+       unsigned vsz, rss; /* we round it to kbytes */
        unsigned long stime, utime;
        unsigned pid;
        unsigned ppid;
@@ -794,11 +826,12 @@ typedef struct {
        unsigned sid;
        unsigned uid;
        unsigned gid;
-       /* basename of executable file in call to exec(2), size from */
-       /* sizeof(task_struct.comm) in /usr/include/linux/sched.h */
        char state[4];
+       char tty_str[8]; /* "maj,min" or "?" */
+       /* basename of executable in exec(2), read from /proc/N/stat, */
+       /* size from sizeof(task_struct.comm) in /usr/include/linux/sched.h */
        char comm[COMM_LEN];
-//     user/group? - use passwd/group parsing functions
+       /* user/group? - use passwd/group parsing functions */
 } procps_status_t;
 enum {
        PSSCAN_PID      = 1 << 0,
@@ -810,12 +843,16 @@ enum {
        PSSCAN_CMD      = 1 << 6,
        PSSCAN_STATE    = 1 << 7,
        PSSCAN_VSZ      = 1 << 8,
-       PSSCAN_STIME    = 1 << 9,
-       PSSCAN_UTIME    = 1 << 10,
+       PSSCAN_RSS      = 1 << 9,
+       PSSCAN_STIME    = 1 << 10,
+       PSSCAN_UTIME    = 1 << 11,
+       PSSCAN_TTY      = 1 << 12,
        /* 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_STIME | PSSCAN_UTIME,
+                       | PSSCAN_VSZ | PSSCAN_RSS
+                       | PSSCAN_STIME | PSSCAN_UTIME
+                       | PSSCAN_TTY,
 };
 procps_status_t* alloc_procps_scan(int flags);
 void free_procps_scan(procps_status_t* sp);
@@ -850,7 +887,7 @@ void md5_begin(md5_ctx_t *ctx);
 void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
 void *md5_end(void *resbuf, md5_ctx_t *ctx);
 
-uint32_t *crc32_filltable(int endian);
+uint32_t *crc32_filltable(uint32_t *tbl256, int endian);
 
 
 enum { /* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depend on them. */
@@ -868,6 +905,7 @@ enum {      /* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depend on them. */
 };
 
 #define FILEUTILS_CP_OPTSTR "pdRfils" USE_SELINUX("c")
+extern const struct bb_applet *current_applet;
 extern const char *applet_name;
 extern const char BB_BANNER[];
 
@@ -902,18 +940,22 @@ extern const char bb_dev_null[];
 extern const int const_int_0;
 extern const int const_int_1;
 
+
 #ifndef BUFSIZ
 #define BUFSIZ 4096
 #endif
-extern char bb_common_bufsiz1[BUFSIZ+1];
+/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
+extern char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1];
 /* This struct is deliberately not defined. */
 /* See docs/keep_data_small.txt */
 struct globals;
-/* Magic prevents this from going into rodata */
-/* If you want to assign a value, use PTR_TO_GLOBALS = xxx */
+/* '*const' ptr makes gcc optimize code much better.
+ * Magic prevents ptr_to_globals from going into rodata
+ * If you want to assign a value, use PTR_TO_GLOBALS = xxx */
 extern struct globals *const ptr_to_globals;
 #define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals)
 
+
 /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  * use bb_default_login_shell and following defines.
  * If you change LIBBB_DEFAULT_LOGIN_SHELL,