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