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