blob: a89ac3694777ef488a6b350c54cbdf1fa9185bab [file] [log] [blame]
Eric Andersenf6b71392000-09-26 01:09:18 +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 */
24#ifndef _BB_INTERNAL_H_
25#define _BB_INTERNAL_H_ 1
26
27#include "Config.h"
28
Eric Andersenf6b71392000-09-26 01:09:18 +000029#include <stdlib.h>
30#include <stdarg.h>
31#include <string.h>
32#include <unistd.h>
33#include <errno.h>
34#include <sys/stat.h>
35#include <sys/param.h>
Eric Andersen01bda5d2001-01-02 01:16:38 +000036#include <sys/types.h>
Eric Andersenf6b71392000-09-26 01:09:18 +000037#include <mntent.h>
38#include <regex.h>
39/* for the _syscall() macros */
40#include <sys/syscall.h>
41#include <linux/unistd.h>
42
Eric Andersen01bda5d2001-01-02 01:16:38 +000043#ifdef DMALLOC
44#include "dmalloc.h"
45#endif
Eric Andersenf6b71392000-09-26 01:09:18 +000046
47/* Some useful definitions */
Eric Andersen2187adc2000-12-04 20:31:45 +000048#define FALSE ((int) 0)
49#define TRUE ((int) 1)
Matt Kraaia1f97752000-12-19 06:24:08 +000050#define SKIP ((int) 2)
Eric Andersenf6b71392000-09-26 01:09:18 +000051
52/* for mtab.c */
53#define MTAB_GETMOUNTPT '1'
54#define MTAB_GETDEVICE '2'
55
56#define BUF_SIZE 8192
57#define EXPAND_ALLOC 1024
58
Mark Whitleyf57c9442000-12-07 19:56:48 +000059static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
60static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); }
Eric Andersenf6b71392000-09-26 01:09:18 +000061
62/* Macros for min/max. */
63#ifndef MIN
64#define MIN(a,b) (((a)<(b))?(a):(b))
65#endif
66
67#ifndef MAX
68#define MAX(a,b) (((a)>(b))?(a):(b))
69#endif
70
71
72/* I don't like nested includes, but the string and io functions are used
73 * too often
74 */
75#include <stdio.h>
76#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
77# include <string.h>
78# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
79# include <memory.h>
80# endif
81# define memzero(s, n) memset ((void *)(s), 0, (n))
82#else
83# include <strings.h>
84# define strchr index
85# define strrchr rindex
86# define memcpy(d, s, n) bcopy((s), (d), (n))
87# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
88# define memzero(s, n) bzero((s), (n))
89#endif
90
91
92enum Location {
93 _BB_DIR_ROOT = 0,
94 _BB_DIR_BIN,
95 _BB_DIR_SBIN,
96 _BB_DIR_USR_BIN,
97 _BB_DIR_USR_SBIN
98};
99
100struct BB_applet {
101 const char* name;
102 int (*main)(int argc, char** argv);
103 enum Location location;
104 const char* usage;
105};
106/* From busybox.c */
107extern const struct BB_applet applets[];
108
Eric Andersen8c725e62000-11-30 00:27:06 +0000109/* Automagically pull in all the applet function prototypes and
Eric Andersen87559822000-12-01 19:02:24 +0000110 * applet usage strings. These are all of the form:
Eric Andersen8c725e62000-11-30 00:27:06 +0000111 * extern int foo_main(int argc, char **argv);
112 * extern const char foo_usage[];
113 * These are all autogenerated from the set of currently defined applets.
114 */
115#define PROTOTYPES
116#include "applets.h"
117#undef PROTOTYPES
Eric Andersenf6b71392000-09-26 01:09:18 +0000118
119extern const char *applet_name;
Eric Andersen8c725e62000-11-30 00:27:06 +0000120extern int applet_name_compare(const void *x, const void *y);
Eric Andersenf6b71392000-09-26 01:09:18 +0000121
122extern void usage(const char *usage) __attribute__ ((noreturn));
Mark Whitleyf57c9442000-12-07 19:56:48 +0000123extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
124extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
125extern void perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
126extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Eric Andersenf6b71392000-09-26 01:09:18 +0000127
Mark Whitleyf57c9442000-12-07 19:56:48 +0000128const char *mode_string(int mode);
129const char *time_string(time_t timeVal);
130int is_directory(const char *name, const int followLinks, struct stat *statBuf);
Eric Andersenf6b71392000-09-26 01:09:18 +0000131int isDevice(const char *name);
132
133typedef struct ino_dev_hash_bucket_struct {
134 struct ino_dev_hash_bucket_struct *next;
135 ino_t ino;
136 dev_t dev;
137 char name[1];
138} ino_dev_hashtable_bucket_t;
139int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
140void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
141void reset_ino_dev_hashtable(void);
142
Mark Whitleyf57c9442000-12-07 19:56:48 +0000143int copy_file(const char *srcName, const char *destName,
Eric Andersenf6b71392000-09-26 01:09:18 +0000144 int setModes, int followLinks, int forceFlag);
Mark Whitley47583682000-12-05 20:03:17 +0000145int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
Eric Andersenf6b71392000-09-26 01:09:18 +0000146char *buildName(const char *dirName, const char *fileName);
147int makeString(int argc, const char **argv, char *buf, int bufLen);
148char *getChunk(int size);
149char *chunkstrdup(const char *str);
150void freeChunks(void);
Matt Kraaibfa79672000-12-15 22:34:34 +0000151ssize_t safe_read(int fd, void *buf, size_t count);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000152int full_write(int fd, const char *buf, int len);
153int full_read(int fd, char *buf, int len);
154int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersenf6b71392000-09-26 01:09:18 +0000155 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
156 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
157 void* userData);
158
Mark Whitleyf57c9442000-12-07 19:56:48 +0000159extern int create_path (const char *name, int mode);
Eric Andersenf6b71392000-09-26 01:09:18 +0000160extern int parse_mode( const char* s, mode_t* theMode);
161
162extern int get_kernel_revision(void);
163
164extern int get_console_fd(char* tty_name);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000165extern struct mntent *find_mount_point(const char *name, const char *table);
Eric Andersenf6b71392000-09-26 01:09:18 +0000166extern void write_mtab(char* blockDevice, char* directory,
167 char* filesystemType, long flags, char* string_flags);
168extern void erase_mtab(const char * name);
169extern void mtab_read(void);
170extern char *mtab_first(void **iter);
171extern char *mtab_next(void **iter);
172extern char *mtab_getinfo(const char *match, const char which);
173extern int check_wildcard_match(const char* text, const char* pattern);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000174extern long atoi_w_units (const char *cp);
175extern pid_t* find_pid_by_name( char* pidName);
Eric Andersenf6b71392000-09-26 01:09:18 +0000176extern int find_real_root_device_name(char* name);
177extern char *get_line_from_file(FILE *file);
178extern void print_file(FILE *file);
179extern int print_file_by_name(char *filename);
180extern char process_escape_sequence(char **ptr);
181extern char *get_last_path_component(char *path);
182extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Matt Kraaic0321f92000-09-27 04:09:22 +0000183extern FILE *wfopen(const char *path, const char *mode);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000184extern FILE *xfopen(const char *path, const char *mode);
Eric Andersenf6b71392000-09-26 01:09:18 +0000185
Eric Andersen01bda5d2001-01-02 01:16:38 +0000186#ifndef DMALLOC
Eric Andersenf6b71392000-09-26 01:09:18 +0000187extern void *xmalloc (size_t size);
188extern void *xrealloc(void *old, size_t size);
189extern void *xcalloc(size_t nmemb, size_t size);
190extern char *xstrdup (const char *s);
191#endif
192extern char *xstrndup (const char *s, int n);
193
Matt Kraai24ac0172000-12-18 21:38:57 +0000194struct suffix_mult {
195 char *suffix;
196 int mult;
197};
198
199extern unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes);
200
Eric Andersenf6b71392000-09-26 01:09:18 +0000201
202/* These parse entries in /etc/passwd and /etc/group. This is desirable
203 * for BusyBox since we want to avoid using the glibc NSS stuff, which
204 * increases target size and is often not needed embedded systems. */
205extern long my_getpwnam(char *name);
206extern long my_getgrnam(char *name);
207extern void my_getpwuid(char *name, long uid);
208extern void my_getgrgid(char *group, long gid);
209extern long my_getpwnamegid(char *name);
210
211extern int device_open(char *device, int mode);
212
213#if defined BB_FEATURE_MOUNT_LOOP
214extern int del_loop(const char *device);
215extern int set_loop(const char *device, const char *file, int offset, int *loopro);
216extern char *find_unused_loop_device (void);
217#endif
218
219
220#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
221extern int vdprintf(int d, const char *format, va_list ap);
222#endif
223
224#if defined BB_NFSMOUNT
225int nfsmount(const char *spec, const char *node, int *flags,
226 char **extra_opts, char **mount_opts, int running_bg);
227#endif
228
229#ifndef RB_POWER_OFF
230/* Stop system and switch power off if possible. */
231#define RB_POWER_OFF 0x4321fedc
232#endif
233
234/* Include our own copy of struct sysinfo to avoid binary compatability
235 * problems with Linux 2.4, which changed things. Grumble, grumble. */
236struct sysinfo {
237 long uptime; /* Seconds since boot */
238 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
239 unsigned long totalram; /* Total usable main memory size */
240 unsigned long freeram; /* Available memory size */
241 unsigned long sharedram; /* Amount of shared memory */
242 unsigned long bufferram; /* Memory used by buffers */
243 unsigned long totalswap; /* Total swap space size */
244 unsigned long freeswap; /* swap space still available */
245 unsigned short procs; /* Number of current processes */
246 unsigned long totalhigh; /* Total high memory size */
247 unsigned long freehigh; /* Available high memory size */
248 unsigned int mem_unit; /* Memory unit size in bytes */
249 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
250};
251extern int sysinfo (struct sysinfo* info);
252
253/* Bit map related macros -- libc5 doens't provide these... sigh. */
254#ifndef setbit
255#define NBBY CHAR_BIT
256#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
257#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
258#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
259#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
260#endif
261
262#endif /* _BB_INTERNAL_H_ */