1 /* vi: set sw=4 ts=4: */
3 * Busybox main internal header file
5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
8 * Permission has been granted to redistribute this code under the GPL.
11 #ifndef __LIBBUSYBOX_H__
12 #define __LIBBUSYBOX_H__ 1
17 #include <sys/types.h>
27 #include "bb_config.h"
29 #include <selinux/selinux.h>
34 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
37 #ifdef CONFIG_FEATURE_SHA1_PASSWORDS
41 /* Some useful definitions */
42 #define FALSE ((int) 0)
43 #define TRUE ((int) 1)
44 #define SKIP ((int) 2)
47 #define MTAB_GETMOUNTPT '1'
48 #define MTAB_GETDEVICE '2'
51 #define EXPAND_ALLOC 1024
53 /* Macros for min/max. */
55 #define MIN(a,b) (((a)<(b))?(a):(b))
59 #define MAX(a,b) (((a)>(b))?(a):(b))
62 extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
63 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
64 extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
65 extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
66 extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
67 extern void bb_vherror_msg(const char *s, va_list p);
68 extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
69 extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
71 extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
72 extern void bb_perror_nomsg(void);
74 /* These two are used internally -- you shouldn't need to use them */
75 extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
76 extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
78 extern int bb_echo(int argc, char** argv);
80 extern const char *bb_mode_string(int mode);
81 extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
83 extern int remove_file(const char *path, int flags);
84 extern int copy_file(const char *source, const char *dest, int flags);
85 extern ssize_t safe_read(int fd, void *buf, size_t count);
86 extern ssize_t bb_full_read(int fd, void *buf, size_t len);
87 extern ssize_t safe_write(int fd, const void *buf, size_t count);
88 extern ssize_t bb_full_write(int fd, const void *buf, size_t len);
89 extern int recursive_action(const char *fileName, int recurse,
90 int followLinks, int depthFirst,
91 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
92 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
95 extern int bb_parse_mode( const char* s, mode_t* theMode);
96 extern long bb_xgetlarg(const char *arg, int base, long lower, long upper);
98 extern unsigned long bb_baud_to_value(speed_t speed);
99 extern speed_t bb_value_to_baud(unsigned long value);
101 extern int get_kernel_revision(void);
103 extern int get_console_fd(void);
104 extern struct mntent *find_mount_point(const char *name, const char *table);
105 extern void erase_mtab(const char * name);
106 extern long *find_pid_by_name( const char* pidName);
107 extern long *pidlist_reverse(long *pidList);
108 extern char *find_block_device(char *path);
109 extern char *bb_get_line_from_file(FILE *file);
110 extern char *bb_get_chomped_line_from_file(FILE *file);
111 extern char *bb_get_chunk_from_file(FILE *file);
112 extern int bb_copyfd_size(int fd1, int fd2, const off_t size);
113 extern int bb_copyfd_eof(int fd1, int fd2);
114 extern void bb_xprint_and_close_file(FILE *file);
115 extern int bb_xprint_file_by_name(const char *filename);
116 extern char bb_process_escape_sequence(const char **ptr);
117 extern char *bb_get_last_path_component(char *path);
118 extern FILE *bb_wfopen(const char *path, const char *mode);
119 extern FILE *bb_wfopen_input(const char *filename);
120 extern FILE *bb_xfopen(const char *path, const char *mode);
122 extern int bb_fclose_nonstdin(FILE *f);
123 extern void bb_fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
125 #define BB_GETOPT_ERROR 0x80000000UL
126 extern const char *bb_opt_complementally;
127 extern const struct option *bb_applet_long_options;
128 extern unsigned long bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...);
130 extern int bb_vfprintf(FILE * __restrict stream, const char * __restrict format,
131 va_list arg) __attribute__ ((format (printf, 2, 0)));
132 extern int bb_vprintf(const char * __restrict format, va_list arg)
133 __attribute__ ((format (printf, 1, 0)));
134 extern int bb_fprintf(FILE * __restrict stream, const char * __restrict format, ...)
135 __attribute__ ((format (printf, 2, 3)));
136 extern int bb_printf(const char * __restrict format, ...)
137 __attribute__ ((format (printf, 1, 2)));
139 //#warning rename to xferror_filename?
140 extern void bb_xferror(FILE *fp, const char *fn);
141 extern void bb_xferror_stdout(void);
142 extern void bb_xfflush_stdout(void);
144 extern void bb_warn_ignoring_args(int n);
146 extern void chomp(char *s);
147 extern void trim(char *s);
148 extern const char *bb_skip_whitespace(const char *);
150 extern struct BB_applet *find_applet_by_name(const char *name);
151 void run_applet_by_name(const char *name, int argc, char **argv);
153 /* dmalloc will redefine these to it's own implementation. It is safe
154 * to have the prototypes here unconditionally. */
155 extern void *xmalloc(size_t size);
156 extern void *xrealloc(void *old, size_t size);
157 extern void *xcalloc(size_t nmemb, size_t size);
159 extern char *bb_xstrdup (const char *s);
160 extern char *bb_xstrndup (const char *s, int n);
161 extern char *safe_strncpy(char *dst, const char *src, size_t size);
162 extern int safe_strtoi(char *arg, int* value);
163 extern int safe_strtod(char *arg, double* value);
164 extern int safe_strtol(char *arg, long* value);
165 extern int safe_strtoul(char *arg, unsigned long* value);
172 extern unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
175 const struct suffix_mult *suffixes);
176 extern unsigned long bb_xgetularg_bnd(const char *arg, int base,
178 unsigned long upper);
179 extern unsigned long bb_xgetularg10_bnd(const char *arg,
181 unsigned long upper);
182 extern unsigned long bb_xgetularg10(const char *arg);
184 extern long bb_xgetlarg_bnd_sfx(const char *arg, int base,
187 const struct suffix_mult *suffixes);
188 extern long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes);
191 //#warning pitchable now?
192 extern unsigned long bb_xparse_number(const char *numstr,
193 const struct suffix_mult *suffixes);
196 /* These parse entries in /etc/passwd and /etc/group. This is desirable
197 * for BusyBox since we want to avoid using the glibc NSS stuff, which
198 * increases target size and is often not needed on embedded systems. */
199 extern long bb_xgetpwnam(const char *name);
200 extern long bb_xgetgrnam(const char *name);
201 extern char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);
202 extern char * bb_getpwuid(char *name, long uid, int bufsize);
203 extern char * bb_getgrgid(char *group, long gid, int bufsize);
204 extern char *bb_askpass(int timeout, const char * prompt);
206 extern int device_open(const char *device, int mode);
208 extern char *query_loop(const char *device);
209 extern int del_loop(const char *device);
210 extern int set_loop(char **device, const char *file, int offset);
213 extern int vdprintf(int d, const char *format, va_list ap);
216 int nfsmount(const char *spec, const char *node, int *flags,
217 char **mount_opts, int running_bg);
219 /* Include our own copy of struct sysinfo to avoid binary compatability
220 * problems with Linux 2.4, which changed things. Grumble, grumble. */
222 long uptime; /* Seconds since boot */
223 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
224 unsigned long totalram; /* Total usable main memory size */
225 unsigned long freeram; /* Available memory size */
226 unsigned long sharedram; /* Amount of shared memory */
227 unsigned long bufferram; /* Memory used by buffers */
228 unsigned long totalswap; /* Total swap space size */
229 unsigned long freeswap; /* swap space still available */
230 unsigned short procs; /* Number of current processes */
231 unsigned short pad; /* Padding needed for m68k */
232 unsigned long totalhigh; /* Total high memory size */
233 unsigned long freehigh; /* Available high memory size */
234 unsigned int mem_unit; /* Memory unit size in bytes */
235 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
237 extern int sysinfo (struct sysinfo* info);
241 MEGABYTE = (KILOBYTE*1024),
242 GIGABYTE = (MEGABYTE*1024)
244 const char *make_human_readable_str(unsigned long long size,
245 unsigned long block_size, unsigned long display_unit);
247 int bb_ask_confirmation(void);
248 int klogctl(int type, char * b, int len);
250 char *xgetcwd(char *cwd);
251 char *xreadlink(const char *path);
252 char *concat_path_file(const char *path, const char *filename);
253 char *concat_subpath_file(const char *path, const char *filename);
254 char *last_char_is(const char *s, int c);
256 int read_package_field(const char *package_buffer, char **field_name, char **field_value);
258 char *fgets_str(FILE *file, const char *terminating_string);
260 extern int uncompress(int fd_in, int fd_out);
261 extern int inflate(int in, int out);
263 extern struct hostent *xgethostbyname(const char *name);
264 extern struct hostent *xgethostbyname2(const char *name, int af);
265 extern int create_icmp_socket(void);
266 extern int create_icmp6_socket(void);
267 extern int xconnect(struct sockaddr_in *s_addr);
268 extern unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
269 extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
271 //#warning wrap this?
272 char *dirname (char *path);
274 int bb_make_directory (char *path, long mode, int flags);
276 const char *u_signal_names(const char *str_sig, int *signo, int startnum);
277 char *bb_simplify_path(const char *path);
279 enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */
280 FILEUTILS_PRESERVE_STATUS = 1,
281 FILEUTILS_DEREFERENCE = 2,
284 FILEUTILS_INTERACTIVE = 16
287 extern const char *bb_applet_name;
289 extern const char * const bb_msg_full_version;
290 extern const char * const bb_msg_memory_exhausted;
291 extern const char * const bb_msg_invalid_date;
292 extern const char * const bb_msg_io_error;
293 extern const char * const bb_msg_read_error;
294 extern const char * const bb_msg_write_error;
295 extern const char * const bb_msg_name_longer_than_foo;
296 extern const char * const bb_msg_unknown;
297 extern const char * const bb_msg_can_not_create_raw_socket;
298 extern const char * const bb_msg_perm_denied_are_you_root;
299 extern const char * const bb_msg_standard_input;
300 extern const char * const bb_msg_standard_output;
302 extern const char * const bb_path_nologin_file;
303 extern const char * const bb_path_passwd_file;
304 extern const char * const bb_path_shadow_file;
305 extern const char * const bb_path_gshadow_file;
306 extern const char * const bb_path_group_file;
307 extern const char * const bb_path_securetty_file;
308 extern const char * const bb_path_motd_file;
309 extern const char * const bb_dev_null;
314 extern char bb_common_bufsiz1[BUFSIZ+1];
317 * You can change LIBBB_DEFAULT_LOGIN_SHELL, but don`t use,
318 * use bb_default_login_shell and next defines,
319 * if you LIBBB_DEFAULT_LOGIN_SHELL change,
320 * don`t lose change increment constant!
322 #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
324 extern const char * const bb_default_login_shell;
326 #define DEFAULT_SHELL (bb_default_login_shell+1)
328 #define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
331 extern const char bb_path_mtab_file[];
333 extern int bb_default_error_retval;
335 #ifdef CONFIG_FEATURE_DEVFS
336 # define CURRENT_VC "/dev/vc/0"
337 # define VC_1 "/dev/vc/1"
338 # define VC_2 "/dev/vc/2"
339 # define VC_3 "/dev/vc/3"
340 # define VC_4 "/dev/vc/4"
341 # define VC_5 "/dev/vc/5"
342 #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
343 /* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
344 respective serial ports .. as such, we can't use the common device paths for
346 # define SC_0 "/dev/ttsc/0"
347 # define SC_1 "/dev/ttsc/1"
348 # define SC_FORMAT "/dev/ttsc/%d"
350 # define SC_0 "/dev/tts/0"
351 # define SC_1 "/dev/tts/1"
352 # define SC_FORMAT "/dev/tts/%d"
354 # define VC_FORMAT "/dev/vc/%d"
355 # define LOOP_FORMAT "/dev/loop/%d"
356 # define FB_0 "/dev/fb/0"
358 # define CURRENT_VC "/dev/tty0"
359 # define VC_1 "/dev/tty1"
360 # define VC_2 "/dev/tty2"
361 # define VC_3 "/dev/tty3"
362 # define VC_4 "/dev/tty4"
363 # define VC_5 "/dev/tty5"
364 #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
365 # define SC_0 "/dev/ttySC0"
366 # define SC_1 "/dev/ttySC1"
367 # define SC_FORMAT "/dev/ttySC%d"
369 # define SC_0 "/dev/ttyS0"
370 # define SC_1 "/dev/ttyS1"
371 # define SC_FORMAT "/dev/ttyS%d"
373 # define VC_FORMAT "/dev/tty%d"
374 # define LOOP_FORMAT "/dev/loop%d"
375 # define FB_0 "/dev/fb0"
378 //#warning put these in .o files
380 /* The following devices are the same on devfs and non-devfs systems. */
381 #define CURRENT_TTY "/dev/tty"
382 #define CONSOLE_DEV "/dev/console"
384 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
385 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
386 void reset_ino_dev_hashtable(void);
388 /* Stupid gcc always includes its own builtin strlen()... */
389 extern size_t bb_strlen(const char *string);
390 #define strlen(x) bb_strlen(x)
392 char *bb_xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
395 extern void bb_do_delay(int seconds);
396 extern void change_identity ( const struct passwd *pw );
397 extern const char *change_identity_e2str ( const struct passwd *pw );
398 extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args);
399 #ifdef CONFIG_SELINUX
400 extern void renew_current_security_context(void);
401 extern void set_current_security_context(security_context_t sid);
403 extern int run_parts(char **args, const unsigned char test_mode, char **env);
404 extern int restricted_shell ( const char *shell );
405 extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
406 extern int correct_password ( const struct passwd *pw );
407 extern char *pw_encrypt(const char *clear, const char *salt);
408 extern struct spwd *pwd_to_spwd(const struct passwd *pw);
409 extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
411 extern int bb_xopen(const char *pathname, int flags);
412 extern ssize_t bb_xread(int fd, void *buf, size_t count);
413 extern void bb_xread_all(int fd, void *buf, size_t count);
414 extern unsigned char bb_xread_char(int fd);
417 /*#include <sched.h> *//* Task command name length */
419 #define COMM_LEN TASK_COMM_LEN
421 #define COMM_LEN 16 /* synchronize with size of comm in struct task_struct
422 in /usr/include/linux/sched.h */
431 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
434 unsigned long stime, utime;
438 /* basename of executable file in call to exec(2),
439 size from kernel headers */
440 char short_cmd[COMM_LEN];
443 extern procps_status_t * procps_scan(int save_user_arg0);
444 extern int compare_string_array(const char * const string_array[], const char *key);
446 extern int my_query_module(const char *name, int which, void **buf, size_t *bufsize, size_t *ret);
448 typedef struct llist_s {
450 struct llist_s *link;
452 extern llist_t *llist_add_to(llist_t *old_head, char *new_item);
453 extern llist_t *llist_add_to_end(llist_t *list_head, char *data);
454 extern llist_t *llist_free_one(llist_t *elm);
455 extern void llist_free(llist_t *elm);
457 extern void print_login_issue(const char *issue_file, const char *tty);
458 extern void print_login_prompt(void);
460 extern void vfork_daemon_rexec(int nochdir, int noclose,
461 int argc, char **argv, char *foreground_opt);
462 extern int get_terminal_width_height(int fd, int *width, int *height);
463 extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
467 extern int hash_fd(int fd, const size_t size, const uint8_t hash_algo, uint8_t *hashval);
469 /* busybox.h will include dmalloc later for us, else include it here. */
470 #if !defined _BB_INTERNAL_H_ && defined DMALLOC
474 #endif /* __LIBBUSYBOX_H__ */