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