blob: 8cb72de8b891c52a344de8feeda78f13841ba78c [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* 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 */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000024#ifndef __LIBCONFIG_H__
25#define __LIBCONFIG_H__ 1
Eric Andersenaad1a882001-03-16 22:47:14 +000026
27#include <stdio.h>
28#include <stdarg.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
Matt Kraaic55b8d42001-05-16 15:40:51 +000032#include <netdb.h>
33
Eric Andersenaad1a882001-03-16 22:47:14 +000034#ifdef DMALLOC
Eric Andersenbdfd0d72001-10-24 05:00:29 +000035#include <dmalloc.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000036#endif
37
38#include <features.h>
Eric Andersenb6b519b2001-04-09 23:52:18 +000039
Robert Grieblc9aca452002-06-04 20:06:25 +000040#include "config.h"
41
Eric Andersen887ca792002-07-03 23:19:26 +000042#include "pwd_.h"
43#include "grp_.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000044#ifdef CONFIG_FEATURE_SHADOWPASSWDS
45#include "shadow_.h"
46#endif
47#ifdef CONFIG_FEATURE_SHA1_PASSWORDS
48# include "sha1.h"
49#endif
50
Glenn L McGrath5a66b642002-09-25 06:08:19 +000051/* Compatability with ANSI C */
52#ifndef inline
53# define inline
54#endif
Eric Andersen27f64e12002-06-23 04:24:25 +000055
Eric Andersen8d79ce82001-07-22 23:00:15 +000056#if (__GNU_LIBRARY__ < 5) && (!defined __dietlibc__)
Eric Andersenaad1a882001-03-16 22:47:14 +000057/* libc5 doesn't define socklen_t */
Glenn L McGratha88cb502003-02-08 23:36:16 +000058#ifndef _SOCKLEN_T
59#define _SOCKLEN_T
Eric Andersenaad1a882001-03-16 22:47:14 +000060typedef unsigned int socklen_t;
Glenn L McGratha88cb502003-02-08 23:36:16 +000061#endif
Eric Andersenb2e3e9b2001-04-04 19:25:57 +000062/* libc5 doesn't implement BSD 4.4 daemon() */
63extern int daemon (int nochdir, int noclose);
Eric Andersenbe0c3602001-08-02 10:55:32 +000064/* libc5 doesn't implement strtok_r */
65char *strtok_r(char *s, const char *delim, char **ptrptr);
Eric Andersenaad1a882001-03-16 22:47:14 +000066#endif
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
Eric Andersenaad1a882001-03-16 22:47:14 +000080/* 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
Eric Andersenaad1a882001-03-16 22:47:14 +000089extern void show_usage(void) __attribute__ ((noreturn));
Eric Andersenb183dfa2001-03-19 19:24:06 +000090extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
91extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Glenn L McGrath2e51a142003-01-20 23:50:59 +000092extern void perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
93extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Matt Kraaic55b8d42001-05-16 15:40:51 +000094extern void vherror_msg(const char *s, va_list p);
Glenn L McGrath2e51a142003-01-20 23:50:59 +000095extern void herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
96extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Eric Andersenaad1a882001-03-16 22:47:14 +000097
Eric Andersenb183dfa2001-03-19 19:24:06 +000098/* These two are used internally -- you shouldn't need to use them */
99extern void verror_msg(const char *s, va_list p);
100extern void vperror_msg(const char *s, va_list p);
101
Eric Andersenaad1a882001-03-16 22:47:14 +0000102const char *mode_string(int mode);
103const char *time_string(time_t timeVal);
Eric Andersen8cd16d82001-05-07 23:11:23 +0000104int is_directory(const char *name, int followLinks, struct stat *statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +0000105int isDevice(const char *name);
106
Matt Kraai8810bdb2001-04-24 20:04:18 +0000107int remove_file(const char *path, int flags);
Matt Kraai91b28552001-04-23 18:53:07 +0000108int copy_file(const char *source, const char *dest, int flags);
Glenn L McGrathc9cac5b2001-04-12 13:47:12 +0000109int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize);
Eric Andersenaad1a882001-03-16 22:47:14 +0000110char *buildName(const char *dirName, const char *fileName);
111int makeString(int argc, const char **argv, char *buf, int bufLen);
112char *getChunk(int size);
113char *chunkstrdup(const char *str);
114void freeChunks(void);
115ssize_t safe_read(int fd, void *buf, size_t count);
116int full_write(int fd, const char *buf, int len);
117int full_read(int fd, char *buf, int len);
118int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
119 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
120 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
121 void* userData);
122
Eric Andersenaad1a882001-03-16 22:47:14 +0000123extern int parse_mode( const char* s, mode_t* theMode);
Eric Andersena9c6bc42003-03-07 17:29:12 +0000124extern long bb_xgetlarg(char *arg, int base, long lower, long upper);
Eric Andersenaad1a882001-03-16 22:47:14 +0000125
126extern int get_kernel_revision(void);
127
Eric Andersenc38678d2002-09-16 06:22:25 +0000128extern int get_console_fd(void);
Eric Andersenaad1a882001-03-16 22:47:14 +0000129extern struct mntent *find_mount_point(const char *name, const char *table);
130extern void write_mtab(char* blockDevice, char* directory,
131 char* filesystemType, long flags, char* string_flags);
132extern void erase_mtab(const char * name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000133extern long atoi_w_units (const char *cp);
Eric Andersen44608e92002-10-22 12:21:15 +0000134extern long* find_pid_by_name( const char* pidName);
Eric Andersenc911a432001-05-15 17:42:16 +0000135extern char *find_real_root_device_name(const char* name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000136extern char *get_line_from_file(FILE *file);
137extern void print_file(FILE *file);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000138extern int copyfd(int fd1, int fd2, const off_t chunksize);
Eric Andersenaad1a882001-03-16 22:47:14 +0000139extern int print_file_by_name(char *filename);
Eric Andersene5dfced2001-04-09 22:48:12 +0000140extern char process_escape_sequence(const char **ptr);
Eric Andersenaad1a882001-03-16 22:47:14 +0000141extern char *get_last_path_component(char *path);
142extern FILE *wfopen(const char *path, const char *mode);
143extern FILE *xfopen(const char *path, const char *mode);
144extern void chomp(char *s);
145extern void trim(char *s);
146extern struct BB_applet *find_applet_by_name(const char *name);
147void run_applet_by_name(const char *name, int argc, char **argv);
148
149#ifndef DMALLOC
150extern void *xmalloc (size_t size);
151extern void *xrealloc(void *old, size_t size);
152extern void *xcalloc(size_t nmemb, size_t size);
153extern char *xstrdup (const char *s);
154#endif
155extern char *xstrndup (const char *s, int n);
156extern char * safe_strncpy(char *dst, const char *src, size_t size);
157
158struct suffix_mult {
Eric Andersene5dfced2001-04-09 22:48:12 +0000159 const char *suffix;
Eric Andersenaad1a882001-03-16 22:47:14 +0000160 int mult;
161};
162
163extern unsigned long parse_number(const char *numstr,
164 const struct suffix_mult *suffixes);
165
166
167/* These parse entries in /etc/passwd and /etc/group. This is desirable
168 * for BusyBox since we want to avoid using the glibc NSS stuff, which
169 * increases target size and is often not needed embedded systems. */
170extern long my_getpwnam(const char *name);
171extern long my_getgrnam(const char *name);
Eric Andersen02e6ba92002-09-30 20:39:56 +0000172extern char * my_getpwuid(char *name, long uid);
173extern char * my_getgrgid(char *group, long gid);
Eric Andersenaad1a882001-03-16 22:47:14 +0000174extern long my_getpwnamegid(const char *name);
175
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000176extern int device_open(const char *device, int mode);
Eric Andersenaad1a882001-03-16 22:47:14 +0000177
178extern int del_loop(const char *device);
179extern int set_loop(const char *device, const char *file, int offset, int *loopro);
180extern char *find_unused_loop_device (void);
181
182
183#if (__GLIBC__ < 2)
184extern int vdprintf(int d, const char *format, va_list ap);
185#endif
186
187int nfsmount(const char *spec, const char *node, int *flags,
188 char **extra_opts, char **mount_opts, int running_bg);
189
190void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
191void syslog_msg(int facility, int pri, const char *msg);
192
193/* Include our own copy of struct sysinfo to avoid binary compatability
194 * problems with Linux 2.4, which changed things. Grumble, grumble. */
195struct sysinfo {
196 long uptime; /* Seconds since boot */
197 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
198 unsigned long totalram; /* Total usable main memory size */
199 unsigned long freeram; /* Available memory size */
200 unsigned long sharedram; /* Amount of shared memory */
201 unsigned long bufferram; /* Memory used by buffers */
202 unsigned long totalswap; /* Total swap space size */
203 unsigned long freeswap; /* swap space still available */
204 unsigned short procs; /* Number of current processes */
Matt Kraai52462252001-06-08 13:57:27 +0000205 unsigned short pad; /* Padding needed for m68k */
Eric Andersenaad1a882001-03-16 22:47:14 +0000206 unsigned long totalhigh; /* Total high memory size */
207 unsigned long freehigh; /* Available high memory size */
208 unsigned int mem_unit; /* Memory unit size in bytes */
209 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
210};
211extern int sysinfo (struct sysinfo* info);
212
Eric Andersenaad1a882001-03-16 22:47:14 +0000213enum {
214 KILOBYTE = 1024,
215 MEGABYTE = (KILOBYTE*1024),
216 GIGABYTE = (MEGABYTE*1024)
217};
Eric Andersenf429bac2001-06-13 08:02:45 +0000218const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
Eric Andersenaad1a882001-03-16 22:47:14 +0000219
220int ask_confirmation(void);
Eric Andersene76c3b02001-04-05 03:14:39 +0000221int klogctl(int type, char * b, int len);
Eric Andersenaad1a882001-03-16 22:47:14 +0000222
Eric Andersene5dfced2001-04-09 22:48:12 +0000223char *xgetcwd(char *cwd);
Mark Whitley8a633262001-04-30 18:17:00 +0000224char *xreadlink(const char *path);
Eric Andersene5dfced2001-04-09 22:48:12 +0000225char *concat_path_file(const char *path, const char *filename);
Eric Andersenc911a432001-05-15 17:42:16 +0000226char *last_char_is(const char *s, int c);
Eric Andersene5dfced2001-04-09 22:48:12 +0000227
Eric Andersen34506362001-08-02 05:02:46 +0000228extern long arith (const char *startbuf, int *errcode);
Eric Andersen74bcd162001-07-30 21:41:37 +0000229
Glenn L McGrathc3fbec72001-07-18 15:47:21 +0000230int read_package_field(const char *package_buffer, char **field_name, char **field_value);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000231char *fgets_str(FILE *file, const char *terminating_string);
Glenn L McGrath359c1062001-04-12 10:19:08 +0000232
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000233extern int uncompress(int fd_in, int fd_out);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000234extern int inflate(int in, int out);
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000235
Matt Kraaic55b8d42001-05-16 15:40:51 +0000236extern struct hostent *xgethostbyname(const char *name);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000237extern struct hostent *xgethostbyname2(const char *name, int af);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000238extern int create_icmp_socket(void);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000239extern int create_icmp6_socket(void);
240extern int xconnect(const char *host, const char *port);
Matt Kraaic55b8d42001-05-16 15:40:51 +0000241
Matt Kraai80f6d552001-08-24 20:35:45 +0000242char *dirname (char *path);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000243
Eric Andersen879d6c82001-08-02 09:58:19 +0000244int make_directory (char *path, long mode, int flags);
Matt Kraaiceeff732001-06-21 19:41:37 +0000245
Eric Andersen34506362001-08-02 05:02:46 +0000246const char *u_signal_names(const char *str_sig, int *signo, int startnum);
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000247char *simplify_path(const char *path);
Eric Andersen34506362001-08-02 05:02:46 +0000248
Glenn L McGratha2831572001-04-12 00:49:26 +0000249#define CT_AUTO 0
250#define CT_UNIX2DOS 1
251#define CT_DOS2UNIX 2
Matt Kraai1e04ea32001-04-12 21:38:06 +0000252/* extern int convert(char *fn, int ConvType); */
Glenn L McGratha2831572001-04-12 00:49:26 +0000253
Matt Kraai91b28552001-04-23 18:53:07 +0000254enum {
Matt Kraai01441032001-04-24 01:30:02 +0000255 FILEUTILS_PRESERVE_STATUS = 1,
Matt Kraai4c557bf2001-10-05 01:35:10 +0000256 FILEUTILS_DEREFERENCE = 2,
Matt Kraai01441032001-04-24 01:30:02 +0000257 FILEUTILS_RECUR = 4,
258 FILEUTILS_FORCE = 8,
259 FILEUTILS_INTERACTIVE = 16
Matt Kraai91b28552001-04-23 18:53:07 +0000260};
261
Eric Andersen0e9aef32001-04-30 18:18:02 +0000262extern const char *applet_name;
Eric Andersen6f343942001-04-30 18:08:03 +0000263extern const char * const full_version;
264extern const char * const name_too_long;
265extern const char * const omitting_directory;
266extern const char * const not_a_directory;
267extern const char * const memory_exhausted;
268extern const char * const invalid_date;
269extern const char * const invalid_option;
270extern const char * const io_error;
271extern const char * const dash_dash_help;
272extern const char * const write_error;
273extern const char * const too_few_args;
274extern const char * const name_longer_than_foo;
Eric Andersen5f265b72001-05-11 16:58:46 +0000275extern const char * const unknown;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000276extern const char * const can_not_create_raw_socket;
Eric Andersen27f64e12002-06-23 04:24:25 +0000277extern const char * const nologin_file;
278extern const char * const passwd_file;
279extern const char * const shadow_file;
280extern const char * const gshadow_file;
281extern const char * const group_file;
282extern const char * const securetty_file;
283extern const char * const motd_file;
Eric Andersen27f64e12002-06-23 04:24:25 +0000284extern const char * const _path_login;
Matt Kraai9ff93252001-04-24 01:12:33 +0000285
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000286#ifdef CONFIG_FEATURE_DEVFS
Matt Kraai439e3df2001-07-23 14:52:08 +0000287# define CURRENT_VC "/dev/vc/0"
288# define VC_1 "/dev/vc/1"
289# define VC_2 "/dev/vc/2"
290# define VC_3 "/dev/vc/3"
291# define VC_4 "/dev/vc/4"
292# define VC_5 "/dev/vc/5"
293# define SC_0 "/dev/tts/0"
294# define SC_1 "/dev/tts/1"
295# define VC_FORMAT "/dev/vc/%d"
296# define SC_FORMAT "/dev/tts/%d"
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000297# define LOOP_FORMAT "/dev/loop/%d"
Matt Kraai439e3df2001-07-23 14:52:08 +0000298#else
299# define CURRENT_VC "/dev/tty0"
300# define VC_1 "/dev/tty1"
301# define VC_2 "/dev/tty2"
302# define VC_3 "/dev/tty3"
303# define VC_4 "/dev/tty4"
304# define VC_5 "/dev/tty5"
305# define SC_0 "/dev/ttyS0"
306# define SC_1 "/dev/ttyS1"
307# define VC_FORMAT "/dev/tty%d"
308# define SC_FORMAT "/dev/ttyS%d"
Eric Andersenc7a3fb92002-03-20 15:25:25 +0000309# define LOOP_FORMAT "/dev/loop%d"
Matt Kraai439e3df2001-07-23 14:52:08 +0000310#endif
311
312/* The following devices are the same on devfs and non-devfs systems. */
313#define CURRENT_TTY "/dev/tty"
314#define CONSOLE_DEV "/dev/console"
315
Matt Kraaiace02dc2001-12-17 15:26:36 +0000316int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
317void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
318void reset_ino_dev_hashtable(void);
319
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000320/* Stupid gcc always includes its own builtin strlen()... */
321extern size_t xstrlen(const char *string);
322#define strlen(x) xstrlen(x)
323
Robert Griebld378c312002-07-19 00:05:54 +0000324void bb_asprintf(char **string_ptr, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
325
Eric Andersen27f64e12002-06-23 04:24:25 +0000326
327#define FAIL_DELAY 3
328extern void change_identity ( const struct passwd *pw );
329extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args );
Glenn L McGrath545106f2002-11-11 06:21:00 +0000330extern int run_parts(char **args, const unsigned char test_mode);
Eric Andersen27f64e12002-06-23 04:24:25 +0000331extern int restricted_shell ( const char *shell );
332extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
333extern int correct_password ( const struct passwd *pw );
334extern char *pw_encrypt(const char *clear, const char *salt);
335extern struct spwd *pwd_to_spwd(const struct passwd *pw);
336extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
337
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000338extern int xopen(const char *pathname, int flags);
339extern ssize_t xread(int fd, void *buf, size_t count);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000340extern void xread_all(int fd, void *buf, size_t count);
341extern unsigned char xread_char(int fd);
342
Eric Andersen44608e92002-10-22 12:21:15 +0000343typedef struct {
344 int pid;
345 char user[9];
346 char state[4];
347 unsigned long rss;
348 int ppid;
349#ifdef FEATURE_CPU_USAGE_PERCENTAGE
350 unsigned pcpu;
351 unsigned long stime, utime;
352#endif
353 char *cmd;
354
355 /* basename of executable file in call to exec(2),
356 size from kernel headers */
357 char short_cmd[16];
358} procps_status_t;
359
360extern procps_status_t * procps_scan(int save_user_arg0);
Glenn L McGrathb9638752002-12-02 00:01:36 +0000361extern unsigned short compare_string_array(const char *string_array[], const char *key);
Eric Andersen44608e92002-10-22 12:21:15 +0000362
Tim Rikercf932742002-12-14 01:58:59 +0000363extern int my_query_module(const char *name, int which, void **buf, size_t *bufsize, size_t *ret);
364
Glenn L McGrath66125c82002-12-08 00:54:33 +0000365typedef struct llist_s {
366 char *data;
367 struct llist_s *link;
368} llist_t;
369extern llist_t *llist_add_to(llist_t *old_head, char *new_item);
370
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000371void print_login_issue(const char *issue_file, const char *tty);
372void print_login_prompt(void);
373
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000374#endif /* __LIBCONFIG_H__ */