libbb.h 17 KB

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