3
0

libbb.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Busybox main internal header file
  4. *
  5. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  6. *
  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.
  9. *
  10. */
  11. #ifndef __LIBBUSYBOX_H__
  12. #define __LIBBUSYBOX_H__ 1
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <termios.h>
  19. #include <stdint.h>
  20. #include <netdb.h>
  21. #include <features.h>
  22. #include "platform.h"
  23. #include "bb_config.h"
  24. #ifdef CONFIG_SELINUX
  25. #include <selinux/selinux.h>
  26. #endif
  27. #include "pwd_.h"
  28. #include "grp_.h"
  29. #ifdef CONFIG_FEATURE_SHADOWPASSWDS
  30. #include "shadow_.h"
  31. #endif
  32. #ifdef CONFIG_FEATURE_SHA1_PASSWORDS
  33. # include "sha1.h"
  34. #endif
  35. /* Some useful definitions */
  36. #define FALSE ((int) 0)
  37. #define TRUE ((int) 1)
  38. #define SKIP ((int) 2)
  39. /* for mtab.c */
  40. #define MTAB_GETMOUNTPT '1'
  41. #define MTAB_GETDEVICE '2'
  42. #define BUF_SIZE 8192
  43. #define EXPAND_ALLOC 1024
  44. /* Macros for min/max. */
  45. #ifndef MIN
  46. #define MIN(a,b) (((a)<(b))?(a):(b))
  47. #endif
  48. #ifndef MAX
  49. #define MAX(a,b) (((a)>(b))?(a):(b))
  50. #endif
  51. extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
  52. extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  53. extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  54. extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  55. extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  56. extern void bb_vherror_msg(const char *s, va_list p);
  57. extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
  58. extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  59. extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
  60. extern void bb_perror_nomsg(void);
  61. /* These two are used internally -- you shouldn't need to use them */
  62. extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
  63. extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
  64. extern int bb_echo(int argc, char** argv);
  65. extern const char *bb_mode_string(int mode);
  66. extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
  67. extern int remove_file(const char *path, int flags);
  68. extern int copy_file(const char *source, const char *dest, int flags);
  69. extern ssize_t safe_read(int fd, void *buf, size_t count);
  70. extern ssize_t bb_full_read(int fd, void *buf, size_t len);
  71. extern ssize_t safe_write(int fd, const void *buf, size_t count);
  72. extern ssize_t bb_full_write(int fd, const void *buf, size_t len);
  73. extern int recursive_action(const char *fileName, int recurse,
  74. int followLinks, int depthFirst,
  75. int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
  76. int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
  77. void* userData);
  78. extern int bb_parse_mode( const char* s, mode_t* theMode);
  79. extern long bb_xgetlarg(const char *arg, int base, long lower, long upper);
  80. extern unsigned long bb_baud_to_value(speed_t speed);
  81. extern speed_t bb_value_to_baud(unsigned long value);
  82. extern int get_kernel_revision(void);
  83. extern int get_console_fd(void);
  84. extern struct mntent *find_mount_point(const char *name, const char *table);
  85. extern void erase_mtab(const char * name);
  86. extern long *find_pid_by_name( const char* pidName);
  87. extern long *pidlist_reverse(long *pidList);
  88. extern char *find_block_device(char *path);
  89. extern char *bb_get_line_from_file(FILE *file);
  90. extern char *bb_get_chomped_line_from_file(FILE *file);
  91. extern char *bb_get_chunk_from_file(FILE *file, int *end);
  92. extern int bb_copyfd_size(int fd1, int fd2, const off_t size);
  93. extern int bb_copyfd_eof(int fd1, int fd2);
  94. extern void bb_xprint_and_close_file(FILE *file);
  95. extern int bb_xprint_file_by_name(const char *filename);
  96. extern char bb_process_escape_sequence(const char **ptr);
  97. extern char *bb_get_last_path_component(char *path);
  98. extern FILE *bb_wfopen(const char *path, const char *mode);
  99. extern FILE *bb_wfopen_input(const char *filename);
  100. extern FILE *bb_xfopen(const char *path, const char *mode);
  101. extern int bb_fclose_nonstdin(FILE *f);
  102. extern void bb_fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
  103. extern void xstat(const char *filename, struct stat *buf);
  104. #define BB_GETOPT_ERROR 0x80000000UL
  105. extern const char *bb_opt_complementally;
  106. extern const struct option *bb_applet_long_options;
  107. extern unsigned long bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...);
  108. extern int bb_vfprintf(FILE * __restrict stream, const char * __restrict format,
  109. va_list arg) __attribute__ ((format (printf, 2, 0)));
  110. extern int bb_vprintf(const char * __restrict format, va_list arg)
  111. __attribute__ ((format (printf, 1, 0)));
  112. extern int bb_fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  113. __attribute__ ((format (printf, 2, 3)));
  114. extern int bb_printf(const char * __restrict format, ...)
  115. __attribute__ ((format (printf, 1, 2)));
  116. //#warning rename to xferror_filename?
  117. extern void bb_xferror(FILE *fp, const char *fn);
  118. extern void bb_xferror_stdout(void);
  119. extern void bb_xfflush_stdout(void);
  120. extern void bb_warn_ignoring_args(int n);
  121. extern void chomp(char *s);
  122. extern void trim(char *s);
  123. extern const char *bb_skip_whitespace(const char *);
  124. extern struct BB_applet *find_applet_by_name(const char *name);
  125. void run_applet_by_name(const char *name, int argc, char **argv);
  126. /* dmalloc will redefine these to it's own implementation. It is safe
  127. * to have the prototypes here unconditionally. */
  128. extern void *xmalloc(size_t size);
  129. extern void *xrealloc(void *old, size_t size);
  130. extern void *xcalloc(size_t nmemb, size_t size);
  131. extern char *bb_xstrdup (const char *s);
  132. extern char *bb_xstrndup (const char *s, int n);
  133. extern char *safe_strncpy(char *dst, const char *src, size_t size);
  134. extern int safe_strtoi(char *arg, int* value);
  135. extern int safe_strtod(char *arg, double* value);
  136. extern int safe_strtol(char *arg, long* value);
  137. extern int safe_strtoul(char *arg, unsigned long* value);
  138. struct suffix_mult {
  139. const char *suffix;
  140. unsigned int mult;
  141. };
  142. extern unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
  143. unsigned long lower,
  144. unsigned long upper,
  145. const struct suffix_mult *suffixes);
  146. extern unsigned long bb_xgetularg_bnd(const char *arg, int base,
  147. unsigned long lower,
  148. unsigned long upper);
  149. extern unsigned long bb_xgetularg10_bnd(const char *arg,
  150. unsigned long lower,
  151. unsigned long upper);
  152. extern unsigned long bb_xgetularg10(const char *arg);
  153. extern long bb_xgetlarg_bnd_sfx(const char *arg, int base,
  154. long lower,
  155. long upper,
  156. const struct suffix_mult *suffixes);
  157. extern long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes);
  158. //#warning pitchable now?
  159. extern unsigned long bb_xparse_number(const char *numstr,
  160. const struct suffix_mult *suffixes);
  161. /* These parse entries in /etc/passwd and /etc/group. This is desirable
  162. * for BusyBox since we want to avoid using the glibc NSS stuff, which
  163. * increases target size and is often not needed on embedded systems. */
  164. extern long bb_xgetpwnam(const char *name);
  165. extern long bb_xgetgrnam(const char *name);
  166. extern char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);
  167. extern char * bb_getpwuid(char *name, long uid, int bufsize);
  168. extern char * bb_getgrgid(char *group, long gid, int bufsize);
  169. extern char *bb_askpass(int timeout, const char * prompt);
  170. extern int device_open(const char *device, int mode);
  171. extern char *query_loop(const char *device);
  172. extern int del_loop(const char *device);
  173. extern int set_loop(char **device, const char *file, int offset);
  174. #if (__GLIBC__ < 2)
  175. extern int vdprintf(int d, const char *format, va_list ap);
  176. #endif
  177. int nfsmount(const char *spec, const char *node, int *flags,
  178. char **mount_opts, int running_bg);
  179. /* Include our own copy of struct sysinfo to avoid binary compatability
  180. * problems with Linux 2.4, which changed things. Grumble, grumble. */
  181. struct sysinfo {
  182. long uptime; /* Seconds since boot */
  183. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  184. unsigned long totalram; /* Total usable main memory size */
  185. unsigned long freeram; /* Available memory size */
  186. unsigned long sharedram; /* Amount of shared memory */
  187. unsigned long bufferram; /* Memory used by buffers */
  188. unsigned long totalswap; /* Total swap space size */
  189. unsigned long freeswap; /* swap space still available */
  190. unsigned short procs; /* Number of current processes */
  191. unsigned short pad; /* Padding needed for m68k */
  192. unsigned long totalhigh; /* Total high memory size */
  193. unsigned long freehigh; /* Available high memory size */
  194. unsigned int mem_unit; /* Memory unit size in bytes */
  195. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  196. };
  197. extern int sysinfo (struct sysinfo* info);
  198. enum {
  199. KILOBYTE = 1024,
  200. MEGABYTE = (KILOBYTE*1024),
  201. GIGABYTE = (MEGABYTE*1024)
  202. };
  203. const char *make_human_readable_str(unsigned long long size,
  204. unsigned long block_size, unsigned long display_unit);
  205. int bb_ask_confirmation(void);
  206. int klogctl(int type, char * b, int len);
  207. char *xgetcwd(char *cwd);
  208. char *xreadlink(const char *path);
  209. char *concat_path_file(const char *path, const char *filename);
  210. char *concat_subpath_file(const char *path, const char *filename);
  211. char *last_char_is(const char *s, int c);
  212. int read_package_field(const char *package_buffer, char **field_name, char **field_value);
  213. //#warning yuk!
  214. char *fgets_str(FILE *file, const char *terminating_string);
  215. extern int uncompress(int fd_in, int fd_out);
  216. extern int inflate(int in, int out);
  217. extern struct hostent *xgethostbyname(const char *name);
  218. extern struct hostent *xgethostbyname2(const char *name, int af);
  219. extern int create_icmp_socket(void);
  220. extern int create_icmp6_socket(void);
  221. extern int xconnect(struct sockaddr_in *s_addr);
  222. extern unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
  223. extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
  224. //#warning wrap this?
  225. char *dirname (char *path);
  226. int bb_make_directory (char *path, long mode, int flags);
  227. const char *u_signal_names(const char *str_sig, int *signo, int startnum);
  228. char *bb_simplify_path(const char *path);
  229. enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */
  230. FILEUTILS_PRESERVE_STATUS = 1,
  231. FILEUTILS_DEREFERENCE = 2,
  232. FILEUTILS_RECUR = 4,
  233. FILEUTILS_FORCE = 8,
  234. FILEUTILS_INTERACTIVE = 16
  235. };
  236. extern const char *bb_applet_name;
  237. extern const char * const bb_msg_full_version;
  238. extern const char * const bb_msg_memory_exhausted;
  239. extern const char * const bb_msg_invalid_date;
  240. extern const char * const bb_msg_io_error;
  241. extern const char * const bb_msg_read_error;
  242. extern const char * const bb_msg_write_error;
  243. extern const char * const bb_msg_name_longer_than_foo;
  244. extern const char * const bb_msg_unknown;
  245. extern const char * const bb_msg_can_not_create_raw_socket;
  246. extern const char * const bb_msg_perm_denied_are_you_root;
  247. extern const char * const bb_msg_standard_input;
  248. extern const char * const bb_msg_standard_output;
  249. extern const char * const bb_path_nologin_file;
  250. extern const char * const bb_path_passwd_file;
  251. extern const char * const bb_path_shadow_file;
  252. extern const char * const bb_path_gshadow_file;
  253. extern const char * const bb_path_group_file;
  254. extern const char * const bb_path_securetty_file;
  255. extern const char * const bb_path_motd_file;
  256. extern const char * const bb_dev_null;
  257. #ifndef BUFSIZ
  258. #define BUFSIZ 4096
  259. #endif
  260. extern char bb_common_bufsiz1[BUFSIZ+1];
  261. /*
  262. * You can change LIBBB_DEFAULT_LOGIN_SHELL, but don`t use,
  263. * use bb_default_login_shell and next defines,
  264. * if you LIBBB_DEFAULT_LOGIN_SHELL change,
  265. * don`t lose change increment constant!
  266. */
  267. #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
  268. extern const char * const bb_default_login_shell;
  269. /* "/bin/sh" */
  270. #define DEFAULT_SHELL (bb_default_login_shell+1)
  271. /* "sh" */
  272. #define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
  273. extern const char bb_path_mtab_file[];
  274. extern int bb_default_error_retval;
  275. #ifdef CONFIG_FEATURE_DEVFS
  276. # define CURRENT_VC "/dev/vc/0"
  277. # define VC_1 "/dev/vc/1"
  278. # define VC_2 "/dev/vc/2"
  279. # define VC_3 "/dev/vc/3"
  280. # define VC_4 "/dev/vc/4"
  281. # define VC_5 "/dev/vc/5"
  282. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  283. /* Yes, this sucks, but both SH (including sh64) and H8 have a SCI(F) for their
  284. respective serial ports .. as such, we can't use the common device paths for
  285. these. -- PFM */
  286. # define SC_0 "/dev/ttsc/0"
  287. # define SC_1 "/dev/ttsc/1"
  288. # define SC_FORMAT "/dev/ttsc/%d"
  289. #else
  290. # define SC_0 "/dev/tts/0"
  291. # define SC_1 "/dev/tts/1"
  292. # define SC_FORMAT "/dev/tts/%d"
  293. #endif
  294. # define VC_FORMAT "/dev/vc/%d"
  295. # define LOOP_FORMAT "/dev/loop/%d"
  296. # define FB_0 "/dev/fb/0"
  297. #else
  298. # define CURRENT_VC "/dev/tty0"
  299. # define VC_1 "/dev/tty1"
  300. # define VC_2 "/dev/tty2"
  301. # define VC_3 "/dev/tty3"
  302. # define VC_4 "/dev/tty4"
  303. # define VC_5 "/dev/tty5"
  304. #if defined(__sh__) || defined(__H8300H__) || defined(__H8300S__)
  305. # define SC_0 "/dev/ttySC0"
  306. # define SC_1 "/dev/ttySC1"
  307. # define SC_FORMAT "/dev/ttySC%d"
  308. #else
  309. # define SC_0 "/dev/ttyS0"
  310. # define SC_1 "/dev/ttyS1"
  311. # define SC_FORMAT "/dev/ttyS%d"
  312. #endif
  313. # define VC_FORMAT "/dev/tty%d"
  314. # define LOOP_FORMAT "/dev/loop%d"
  315. # define FB_0 "/dev/fb0"
  316. #endif
  317. //#warning put these in .o files
  318. /* The following devices are the same on devfs and non-devfs systems. */
  319. #define CURRENT_TTY "/dev/tty"
  320. #define CONSOLE_DEV "/dev/console"
  321. int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
  322. void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
  323. void reset_ino_dev_hashtable(void);
  324. char *bb_xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
  325. #define FAIL_DELAY 3
  326. extern void bb_do_delay(int seconds);
  327. extern void change_identity ( const struct passwd *pw );
  328. extern const char *change_identity_e2str ( const struct passwd *pw );
  329. extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args);
  330. #ifdef CONFIG_SELINUX
  331. extern void renew_current_security_context(void);
  332. extern void set_current_security_context(security_context_t sid);
  333. #endif
  334. extern int run_parts(char **args, const unsigned char test_mode, char **env);
  335. extern int restricted_shell ( const char *shell );
  336. extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
  337. extern int correct_password ( const struct passwd *pw );
  338. extern char *pw_encrypt(const char *clear, const char *salt);
  339. extern struct spwd *pwd_to_spwd(const struct passwd *pw);
  340. extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
  341. extern int bb_xopen(const char *pathname, int flags);
  342. extern ssize_t bb_xread(int fd, void *buf, size_t count);
  343. extern void bb_xread_all(int fd, void *buf, size_t count);
  344. extern unsigned char bb_xread_char(int fd);
  345. #ifndef COMM_LEN
  346. /*#include <sched.h> *//* Task command name length */
  347. #ifdef TASK_COMM_LEN
  348. #define COMM_LEN TASK_COMM_LEN
  349. #else
  350. #define COMM_LEN 16 /* synchronize with size of comm in struct task_struct
  351. in /usr/include/linux/sched.h */
  352. #endif
  353. #endif
  354. typedef struct {
  355. int pid;
  356. char user[9];
  357. char state[4];
  358. unsigned long rss;
  359. int ppid;
  360. #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
  361. unsigned pcpu;
  362. unsigned pscpu;
  363. unsigned long stime, utime;
  364. #endif
  365. char *cmd;
  366. /* basename of executable file in call to exec(2),
  367. size from kernel headers */
  368. char short_cmd[COMM_LEN];
  369. } procps_status_t;
  370. extern procps_status_t * procps_scan(int save_user_arg0);
  371. extern int compare_string_array(const char * const string_array[], const char *key);
  372. extern int my_query_module(const char *name, int which, void **buf, size_t *bufsize, size_t *ret);
  373. typedef struct llist_s {
  374. char *data;
  375. struct llist_s *link;
  376. } llist_t;
  377. extern llist_t *llist_add_to(llist_t *old_head, char *new_item);
  378. extern llist_t *llist_add_to_end(llist_t *list_head, char *data);
  379. extern llist_t *llist_free_one(llist_t *elm);
  380. extern void llist_free(llist_t *elm);
  381. extern void print_login_issue(const char *issue_file, const char *tty);
  382. extern void print_login_prompt(void);
  383. extern void vfork_daemon_rexec(int nochdir, int noclose,
  384. int argc, char **argv, char *foreground_opt);
  385. extern int get_terminal_width_height(int fd, int *width, int *height);
  386. extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
  387. typedef struct _sha1_ctx_t_ {
  388. uint32_t count[2];
  389. uint32_t hash[5];
  390. uint32_t wbuf[16];
  391. } sha1_ctx_t;
  392. void sha1_begin(sha1_ctx_t *ctx);
  393. void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx);
  394. void *sha1_end(void *resbuf, sha1_ctx_t *ctx);
  395. typedef struct _md5_ctx_t_ {
  396. uint32_t A;
  397. uint32_t B;
  398. uint32_t C;
  399. uint32_t D;
  400. uint32_t total[2];
  401. uint32_t buflen;
  402. char buffer[128];
  403. } md5_ctx_t;
  404. void md5_begin(md5_ctx_t *ctx);
  405. void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
  406. void *md5_end(void *resbuf, md5_ctx_t *ctx);
  407. /* busybox.h will include dmalloc later for us, else include it here. */
  408. #if !defined _BB_INTERNAL_H_ && defined DMALLOC
  409. #include <dmalloc.h>
  410. #endif
  411. #endif /* __LIBBUSYBOX_H__ */