blob: 55a0ff809cc39dc1ad455758f5e28830bc7b2243 [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
51
Eric Andersen8d79ce82001-07-22 23:00:15 +000052#if (__GNU_LIBRARY__ < 5) && (!defined __dietlibc__)
Eric Andersenaad1a882001-03-16 22:47:14 +000053/* libc5 doesn't define socklen_t */
54typedef unsigned int socklen_t;
Eric Andersenb2e3e9b2001-04-04 19:25:57 +000055/* libc5 doesn't implement BSD 4.4 daemon() */
56extern int daemon (int nochdir, int noclose);
Eric Andersenbe0c3602001-08-02 10:55:32 +000057/* libc5 doesn't implement strtok_r */
58char *strtok_r(char *s, const char *delim, char **ptrptr);
Eric Andersenaad1a882001-03-16 22:47:14 +000059#endif
60
61/* Some useful definitions */
62#define FALSE ((int) 0)
63#define TRUE ((int) 1)
64#define SKIP ((int) 2)
65
66/* for mtab.c */
67#define MTAB_GETMOUNTPT '1'
68#define MTAB_GETDEVICE '2'
69
70#define BUF_SIZE 8192
71#define EXPAND_ALLOC 1024
72
Eric Andersenb5ec61e2001-04-18 20:17:05 +000073static inline int is_decimal(int ch) { return ((ch >= '0') && (ch <= '9')); }
74static inline int is_octal(int ch) { return ((ch >= '0') && (ch <= '7')); }
Eric Andersenaad1a882001-03-16 22:47:14 +000075
76/* Macros for min/max. */
77#ifndef MIN
78#define MIN(a,b) (((a)<(b))?(a):(b))
79#endif
80
81#ifndef MAX
82#define MAX(a,b) (((a)>(b))?(a):(b))
83#endif
84
85
86
87extern void show_usage(void) __attribute__ ((noreturn));
Eric Andersenb183dfa2001-03-19 19:24:06 +000088extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
89extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Eric Andersenaad1a882001-03-16 22:47:14 +000090extern void perror_msg(const char *s, ...);
91extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
Matt Kraaic55b8d42001-05-16 15:40:51 +000092extern void vherror_msg(const char *s, va_list p);
93extern void herror_msg(const char *s, ...);
94extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
Eric Andersenaad1a882001-03-16 22:47:14 +000095
Eric Andersenb183dfa2001-03-19 19:24:06 +000096/* These two are used internally -- you shouldn't need to use them */
97extern void verror_msg(const char *s, va_list p);
98extern void vperror_msg(const char *s, va_list p);
99
Eric Andersenaad1a882001-03-16 22:47:14 +0000100const char *mode_string(int mode);
101const char *time_string(time_t timeVal);
Eric Andersen8cd16d82001-05-07 23:11:23 +0000102int is_directory(const char *name, int followLinks, struct stat *statBuf);
Eric Andersenaad1a882001-03-16 22:47:14 +0000103int isDevice(const char *name);
104
Matt Kraai8810bdb2001-04-24 20:04:18 +0000105int remove_file(const char *path, int flags);
Matt Kraai91b28552001-04-23 18:53:07 +0000106int copy_file(const char *source, const char *dest, int flags);
Glenn L McGrathc9cac5b2001-04-12 13:47:12 +0000107int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize);
Eric Andersenaad1a882001-03-16 22:47:14 +0000108char *buildName(const char *dirName, const char *fileName);
109int makeString(int argc, const char **argv, char *buf, int bufLen);
110char *getChunk(int size);
111char *chunkstrdup(const char *str);
112void freeChunks(void);
113ssize_t safe_read(int fd, void *buf, size_t count);
114int full_write(int fd, const char *buf, int len);
115int full_read(int fd, char *buf, int len);
116int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
117 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
118 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
119 void* userData);
120
Eric Andersenaad1a882001-03-16 22:47:14 +0000121extern int parse_mode( const char* s, mode_t* theMode);
122
123extern int get_kernel_revision(void);
124
125extern int get_console_fd(char* tty_name);
126extern struct mntent *find_mount_point(const char *name, const char *table);
127extern void write_mtab(char* blockDevice, char* directory,
128 char* filesystemType, long flags, char* string_flags);
129extern void erase_mtab(const char * name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000130extern long atoi_w_units (const char *cp);
Eric Andersenb24d6562001-12-06 14:52:32 +0000131extern long* find_pid_by_name( char* pidName);
Eric Andersenc911a432001-05-15 17:42:16 +0000132extern char *find_real_root_device_name(const char* name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000133extern char *get_line_from_file(FILE *file);
134extern void print_file(FILE *file);
Matt Kraaid6ef0742001-05-18 14:14:55 +0000135extern int copyfd(int fd1, int fd2);
Eric Andersenaad1a882001-03-16 22:47:14 +0000136extern int print_file_by_name(char *filename);
Eric Andersene5dfced2001-04-09 22:48:12 +0000137extern char process_escape_sequence(const char **ptr);
Eric Andersenaad1a882001-03-16 22:47:14 +0000138extern char *get_last_path_component(char *path);
139extern FILE *wfopen(const char *path, const char *mode);
140extern FILE *xfopen(const char *path, const char *mode);
141extern void chomp(char *s);
142extern void trim(char *s);
143extern struct BB_applet *find_applet_by_name(const char *name);
144void run_applet_by_name(const char *name, int argc, char **argv);
145
146#ifndef DMALLOC
147extern void *xmalloc (size_t size);
148extern void *xrealloc(void *old, size_t size);
149extern void *xcalloc(size_t nmemb, size_t size);
150extern char *xstrdup (const char *s);
151#endif
152extern char *xstrndup (const char *s, int n);
153extern char * safe_strncpy(char *dst, const char *src, size_t size);
154
155struct suffix_mult {
Eric Andersene5dfced2001-04-09 22:48:12 +0000156 const char *suffix;
Eric Andersenaad1a882001-03-16 22:47:14 +0000157 int mult;
158};
159
160extern unsigned long parse_number(const char *numstr,
161 const struct suffix_mult *suffixes);
162
163
164/* These parse entries in /etc/passwd and /etc/group. This is desirable
165 * for BusyBox since we want to avoid using the glibc NSS stuff, which
166 * increases target size and is often not needed embedded systems. */
167extern long my_getpwnam(const char *name);
168extern long my_getgrnam(const char *name);
169extern void my_getpwuid(char *name, long uid);
170extern void my_getgrgid(char *group, long gid);
171extern long my_getpwnamegid(const char *name);
172
173extern int device_open(char *device, int mode);
174
175extern int del_loop(const char *device);
176extern int set_loop(const char *device, const char *file, int offset, int *loopro);
177extern char *find_unused_loop_device (void);
178
179
180#if (__GLIBC__ < 2)
181extern int vdprintf(int d, const char *format, va_list ap);
182#endif
183
184int nfsmount(const char *spec, const char *node, int *flags,
185 char **extra_opts, char **mount_opts, int running_bg);
186
187void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
188void syslog_msg(int facility, int pri, const char *msg);
189
190/* Include our own copy of struct sysinfo to avoid binary compatability
191 * problems with Linux 2.4, which changed things. Grumble, grumble. */
192struct sysinfo {
193 long uptime; /* Seconds since boot */
194 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
195 unsigned long totalram; /* Total usable main memory size */
196 unsigned long freeram; /* Available memory size */
197 unsigned long sharedram; /* Amount of shared memory */
198 unsigned long bufferram; /* Memory used by buffers */
199 unsigned long totalswap; /* Total swap space size */
200 unsigned long freeswap; /* swap space still available */
201 unsigned short procs; /* Number of current processes */
Matt Kraai52462252001-06-08 13:57:27 +0000202 unsigned short pad; /* Padding needed for m68k */
Eric Andersenaad1a882001-03-16 22:47:14 +0000203 unsigned long totalhigh; /* Total high memory size */
204 unsigned long freehigh; /* Available high memory size */
205 unsigned int mem_unit; /* Memory unit size in bytes */
206 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
207};
208extern int sysinfo (struct sysinfo* info);
209
Eric Andersenaad1a882001-03-16 22:47:14 +0000210enum {
211 KILOBYTE = 1024,
212 MEGABYTE = (KILOBYTE*1024),
213 GIGABYTE = (MEGABYTE*1024)
214};
Eric Andersenf429bac2001-06-13 08:02:45 +0000215const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
Eric Andersenaad1a882001-03-16 22:47:14 +0000216
217int ask_confirmation(void);
Eric Andersene76c3b02001-04-05 03:14:39 +0000218int klogctl(int type, char * b, int len);
Eric Andersenaad1a882001-03-16 22:47:14 +0000219
Eric Andersene5dfced2001-04-09 22:48:12 +0000220char *xgetcwd(char *cwd);
Mark Whitley8a633262001-04-30 18:17:00 +0000221char *xreadlink(const char *path);
Eric Andersene5dfced2001-04-09 22:48:12 +0000222char *concat_path_file(const char *path, const char *filename);
Eric Andersenc911a432001-05-15 17:42:16 +0000223char *last_char_is(const char *s, int c);
Eric Andersene5dfced2001-04-09 22:48:12 +0000224
Eric Andersen34506362001-08-02 05:02:46 +0000225extern long arith (const char *startbuf, int *errcode);
Eric Andersen74bcd162001-07-30 21:41:37 +0000226
Glenn L McGrathc3fbec72001-07-18 15:47:21 +0000227int read_package_field(const char *package_buffer, char **field_name, char **field_value);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000228char *fgets_str(FILE *file, const char *terminating_string);
Glenn L McGrath359c1062001-04-12 10:19:08 +0000229
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000230extern int inflate(FILE *in, FILE *out);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000231extern int unzip(FILE *l_in_file, FILE *l_out_file);
232extern void gz_close(int gunzip_pid);
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000233extern FILE *gz_open(FILE *compressed_file, int *pid);
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000234
Matt Kraaic55b8d42001-05-16 15:40:51 +0000235extern struct hostent *xgethostbyname(const char *name);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000236extern struct hostent *xgethostbyname2(const char *name, int af);
Eric Andersen7467c8d2001-07-12 20:26:32 +0000237extern int create_icmp_socket(void);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000238extern int create_icmp6_socket(void);
239extern int xconnect(const char *host, const char *port);
Matt Kraaic55b8d42001-05-16 15:40:51 +0000240
Matt Kraai80f6d552001-08-24 20:35:45 +0000241char *dirname (char *path);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000242
Eric Andersen879d6c82001-08-02 09:58:19 +0000243int make_directory (char *path, long mode, int flags);
Matt Kraaiceeff732001-06-21 19:41:37 +0000244
Eric Andersen34506362001-08-02 05:02:46 +0000245const char *u_signal_names(const char *str_sig, int *signo, int startnum);
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000246char *simplify_path(const char *path);
Eric Andersen34506362001-08-02 05:02:46 +0000247
Glenn L McGratha2831572001-04-12 00:49:26 +0000248#define CT_AUTO 0
249#define CT_UNIX2DOS 1
250#define CT_DOS2UNIX 2
Matt Kraai1e04ea32001-04-12 21:38:06 +0000251/* extern int convert(char *fn, int ConvType); */
Glenn L McGratha2831572001-04-12 00:49:26 +0000252
Matt Kraai91b28552001-04-23 18:53:07 +0000253enum {
Matt Kraai01441032001-04-24 01:30:02 +0000254 FILEUTILS_PRESERVE_STATUS = 1,
Matt Kraai4c557bf2001-10-05 01:35:10 +0000255 FILEUTILS_DEREFERENCE = 2,
Matt Kraai01441032001-04-24 01:30:02 +0000256 FILEUTILS_RECUR = 4,
257 FILEUTILS_FORCE = 8,
258 FILEUTILS_INTERACTIVE = 16
Matt Kraai91b28552001-04-23 18:53:07 +0000259};
260
Eric Andersen0e9aef32001-04-30 18:18:02 +0000261extern const char *applet_name;
Eric Andersen6f343942001-04-30 18:08:03 +0000262extern const char * const full_version;
263extern const char * const name_too_long;
264extern const char * const omitting_directory;
265extern const char * const not_a_directory;
266extern const char * const memory_exhausted;
267extern const char * const invalid_date;
268extern const char * const invalid_option;
269extern const char * const io_error;
270extern const char * const dash_dash_help;
271extern const char * const write_error;
272extern const char * const too_few_args;
273extern const char * const name_longer_than_foo;
Eric Andersen5f265b72001-05-11 16:58:46 +0000274extern const char * const unknown;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000275extern const char * const can_not_create_raw_socket;
Eric Andersen27f64e12002-06-23 04:24:25 +0000276extern const char * const nologin_file;
277extern const char * const passwd_file;
278extern const char * const shadow_file;
279extern const char * const gshadow_file;
280extern const char * const group_file;
281extern const char * const securetty_file;
282extern const char * const motd_file;
283extern const char * const issue_file;
284extern 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 );
330extern int restricted_shell ( const char *shell );
331extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
332extern int correct_password ( const struct passwd *pw );
333extern char *pw_encrypt(const char *clear, const char *salt);
334extern struct spwd *pwd_to_spwd(const struct passwd *pw);
335extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
336
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000337#endif /* __LIBCONFIG_H__ */