Eric Andersen | f6b7139 | 2000-09-26 01:09:18 +0000 | [diff] [blame^] | 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 _BB_INTERNAL_H_ |
| 25 | #define _BB_INTERNAL_H_ 1 |
| 26 | |
| 27 | #include "Config.h" |
| 28 | |
| 29 | #ifdef DMALLOC |
| 30 | #include "dmalloc.h" |
| 31 | #endif |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <stdarg.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | #include <errno.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/param.h> |
| 40 | #include <mntent.h> |
| 41 | #include <regex.h> |
| 42 | /* for the _syscall() macros */ |
| 43 | #include <sys/syscall.h> |
| 44 | #include <linux/unistd.h> |
| 45 | |
| 46 | |
| 47 | /* Some useful definitions */ |
| 48 | #define FALSE ((int) 1) |
| 49 | #define TRUE ((int) 0) |
| 50 | |
| 51 | /* for mtab.c */ |
| 52 | #define MTAB_GETMOUNTPT '1' |
| 53 | #define MTAB_GETDEVICE '2' |
| 54 | |
| 55 | #define BUF_SIZE 8192 |
| 56 | #define EXPAND_ALLOC 1024 |
| 57 | |
| 58 | |
| 59 | #define isBlank(ch) (((ch) == ' ') || ((ch) == '\t')) |
| 60 | #define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9')) |
| 61 | #define isOctal(ch) (((ch) >= '0') && ((ch) <= '7')) |
| 62 | #define isWildCard(ch) (((ch) == '*') || ((ch) == '?') || ((ch) == '[')) |
| 63 | |
| 64 | /* Macros for min/max. */ |
| 65 | #ifndef MIN |
| 66 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
| 67 | #endif |
| 68 | |
| 69 | #ifndef MAX |
| 70 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
| 71 | #endif |
| 72 | |
| 73 | |
| 74 | /* I don't like nested includes, but the string and io functions are used |
| 75 | * too often |
| 76 | */ |
| 77 | #include <stdio.h> |
| 78 | #if !defined(NO_STRING_H) || defined(STDC_HEADERS) |
| 79 | # include <string.h> |
| 80 | # if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__) |
| 81 | # include <memory.h> |
| 82 | # endif |
| 83 | # define memzero(s, n) memset ((void *)(s), 0, (n)) |
| 84 | #else |
| 85 | # include <strings.h> |
| 86 | # define strchr index |
| 87 | # define strrchr rindex |
| 88 | # define memcpy(d, s, n) bcopy((s), (d), (n)) |
| 89 | # define memcmp(s1, s2, n) bcmp((s1), (s2), (n)) |
| 90 | # define memzero(s, n) bzero((s), (n)) |
| 91 | #endif |
| 92 | |
| 93 | |
| 94 | enum Location { |
| 95 | _BB_DIR_ROOT = 0, |
| 96 | _BB_DIR_BIN, |
| 97 | _BB_DIR_SBIN, |
| 98 | _BB_DIR_USR_BIN, |
| 99 | _BB_DIR_USR_SBIN |
| 100 | }; |
| 101 | |
| 102 | struct BB_applet { |
| 103 | const char* name; |
| 104 | int (*main)(int argc, char** argv); |
| 105 | enum Location location; |
| 106 | const char* usage; |
| 107 | }; |
| 108 | /* From busybox.c */ |
| 109 | extern const struct BB_applet applets[]; |
| 110 | |
| 111 | extern int ar_main(int argc, char **argv); |
| 112 | extern int basename_main(int argc, char **argv); |
| 113 | extern int bogomips_main(int argc, char **argv); |
| 114 | extern int busybox_main(int argc, char** argv); |
| 115 | extern int cat_main(int argc, char** argv); |
| 116 | extern int chmod_chown_chgrp_main(int argc, char** argv); |
| 117 | extern int chroot_main(int argc, char** argv); |
| 118 | extern int chvt_main(int argc, char** argv); |
| 119 | extern int clear_main(int argc, char** argv); |
| 120 | extern int cp_mv_main(int argc, char** argv); |
| 121 | extern int cut_main(int argc, char** argv); |
| 122 | extern int date_main(int argc, char** argv); |
| 123 | extern int dc_main(int argc, char** argv); |
| 124 | extern int dd_main(int argc, char** argv); |
| 125 | extern int dirname_main(int argc, char** argv); |
| 126 | extern int deallocvt_main(int argc, char** argv); |
| 127 | extern int df_main(int argc, char** argv); |
| 128 | extern int dmesg_main(int argc, char** argv); |
| 129 | extern int dos2unix_main(int argc, char** argv); |
| 130 | extern int du_main(int argc, char** argv); |
| 131 | extern int dumpkmap_main(int argc, char** argv); |
| 132 | extern int dutmp_main(int argc, char** argv); |
| 133 | extern int echo_main(int argc, char** argv); |
| 134 | extern int expr_main(int argc, char** argv); |
| 135 | extern int false_main(int argc, char** argv); |
| 136 | extern int fbset_main(int argc, char** argv); |
| 137 | extern int fdisk_main(int argc, char** argv); |
| 138 | extern int fdflush_main(int argc, char **argv); |
| 139 | extern int fsck_minix_main(int argc, char **argv); |
| 140 | extern int find_main(int argc, char** argv); |
| 141 | extern int free_main(int argc, char** argv); |
| 142 | extern int freeramdisk_main(int argc, char** argv); |
| 143 | extern int getopt_main(int argc, char** argv); |
| 144 | extern int grep_main(int argc, char** argv); |
| 145 | extern int gunzip_main (int argc, char** argv); |
| 146 | extern int gzip_main(int argc, char** argv); |
| 147 | extern int halt_main(int argc, char** argv); |
| 148 | extern int head_main(int argc, char** argv); |
| 149 | extern int hostid_main(int argc, char** argv); |
| 150 | extern int hostname_main(int argc, char** argv); |
| 151 | extern int id_main(int argc, char** argv); |
| 152 | extern int init_main(int argc, char** argv); |
| 153 | extern int insmod_main(int argc, char** argv); |
| 154 | extern int kill_main(int argc, char** argv); |
| 155 | extern int length_main(int argc, char** argv); |
| 156 | extern int ln_main(int argc, char** argv); |
| 157 | extern int loadacm_main(int argc, char** argv); |
| 158 | extern int loadfont_main(int argc, char** argv); |
| 159 | extern int loadkmap_main(int argc, char** argv); |
| 160 | extern int losetup_main(int argc, char** argv); |
| 161 | extern int logger_main(int argc, char **argv); |
| 162 | extern int logname_main(int argc, char **argv); |
| 163 | extern int ls_main(int argc, char** argv); |
| 164 | extern int lsmod_main(int argc, char** argv); |
| 165 | extern int makedevs_main(int argc, char** argv); |
| 166 | extern int md5sum_main(int argc, char** argv); |
| 167 | extern int mkdir_main(int argc, char** argv); |
| 168 | extern int mkfifo_main(int argc, char **argv); |
| 169 | extern int mkfs_minix_main(int argc, char **argv); |
| 170 | extern int mknod_main(int argc, char** argv); |
| 171 | extern int mkswap_main(int argc, char** argv); |
| 172 | extern int mktemp_main(int argc, char **argv); |
| 173 | extern int nc_main(int argc, char** argv); |
| 174 | extern int more_main(int argc, char** argv); |
| 175 | extern int mount_main(int argc, char** argv); |
| 176 | extern int mt_main(int argc, char** argv); |
| 177 | extern int nslookup_main(int argc, char **argv); |
| 178 | extern int ping_main(int argc, char **argv); |
| 179 | extern int poweroff_main(int argc, char **argv); |
| 180 | extern int printf_main(int argc, char** argv); |
| 181 | extern int ps_main(int argc, char** argv); |
| 182 | extern int pwd_main(int argc, char** argv); |
| 183 | extern int rdate_main(int argc, char** argv); |
| 184 | extern int reboot_main(int argc, char** argv); |
| 185 | extern int renice_main(int argc, char** argv); |
| 186 | extern int reset_main(int argc, char** argv); |
| 187 | extern int rm_main(int argc, char** argv); |
| 188 | extern int rmdir_main(int argc, char **argv); |
| 189 | extern int rmmod_main(int argc, char** argv); |
| 190 | extern int sed_main(int argc, char** argv); |
| 191 | extern int sfdisk_main(int argc, char** argv); |
| 192 | extern int setkeycodes_main(int argc, char** argv); |
| 193 | extern int shell_main(int argc, char** argv); |
| 194 | extern int sleep_main(int argc, char** argv); |
| 195 | extern int sort_main(int argc, char** argv); |
| 196 | extern int swap_on_off_main(int argc, char** argv); |
| 197 | extern int sync_main(int argc, char** argv); |
| 198 | extern int syslogd_main(int argc, char **argv); |
| 199 | extern int tail_main(int argc, char** argv); |
| 200 | extern int tar_main(int argc, char** argv); |
| 201 | extern int tee_main(int argc, char** argv); |
| 202 | extern int test_main(int argc, char** argv); |
| 203 | extern int telnet_main(int argc, char** argv); |
| 204 | extern int touch_main(int argc, char** argv); |
| 205 | extern int tr_main(int argc, char** argv); |
| 206 | extern int true_main(int argc, char** argv); |
| 207 | extern int tput_main(int argc, char** argv); |
| 208 | extern int tryopen_main(int argc, char** argv); |
| 209 | extern int tty_main(int argc, char** argv); |
| 210 | extern int umount_main(int argc, char** argv); |
| 211 | extern int uname_main(int argc, char** argv); |
| 212 | extern int uniq_main(int argc, char** argv); |
| 213 | extern int unix2dos_main(int argc, char** argv); |
| 214 | extern int unrpm_main(int argc, char** argv); |
| 215 | extern int update_main(int argc, char** argv); |
| 216 | extern int uptime_main(int argc, char** argv); |
| 217 | extern int usleep_main(int argc, char** argv); |
| 218 | extern int uuencode_main(int argc, char** argv); |
| 219 | extern int uudecode_main(int argc, char** argv); |
| 220 | extern int wc_main(int argc, char** argv); |
| 221 | extern int wget_main(int argc, char** argv); |
| 222 | extern int which_main(int argc, char** argv); |
| 223 | extern int whoami_main(int argc, char** argv); |
| 224 | extern int xargs_main(int argc, char** argv); |
| 225 | extern int yes_main(int argc, char** argv); |
| 226 | |
| 227 | extern const char ar_usage[]; |
| 228 | extern const char basename_usage[]; |
| 229 | extern const char cat_usage[]; |
| 230 | extern const char chgrp_usage[]; |
| 231 | extern const char chmod_usage[]; |
| 232 | extern const char chown_usage[]; |
| 233 | extern const char chroot_usage[]; |
| 234 | extern const char chvt_usage[]; |
| 235 | extern const char clear_usage[]; |
| 236 | extern const char cp_usage[]; |
| 237 | extern const char cut_usage[]; |
| 238 | extern const char date_usage[]; |
| 239 | extern const char dc_usage[]; |
| 240 | extern const char dd_usage[]; |
| 241 | extern const char deallocvt_usage[]; |
| 242 | extern const char df_usage[]; |
| 243 | extern const char dirname_usage[]; |
| 244 | extern const char dmesg_usage[]; |
| 245 | extern const char dos2unix_usage[]; |
| 246 | extern const char du_usage[]; |
| 247 | extern const char dumpkmap_usage[]; |
| 248 | extern const char dutmp_usage[]; |
| 249 | extern const char echo_usage[]; |
| 250 | extern const char expr_usage[]; |
| 251 | extern const char false_usage[]; |
| 252 | extern const char fdflush_usage[]; |
| 253 | extern const char find_usage[]; |
| 254 | extern const char free_usage[]; |
| 255 | extern const char freeramdisk_usage[]; |
| 256 | extern const char fsck_minix_usage[]; |
| 257 | extern const char grep_usage[]; |
| 258 | extern const char gunzip_usage[]; |
| 259 | extern const char gzip_usage[]; |
| 260 | extern const char halt_usage[]; |
| 261 | extern const char head_usage[]; |
| 262 | extern const char hostid_usage[]; |
| 263 | extern const char hostname_usage[]; |
| 264 | extern const char id_usage[]; |
| 265 | extern const char insmod_usage[]; |
| 266 | extern const char kill_usage[]; |
| 267 | extern const char killall_usage[]; |
| 268 | extern const char length_usage[]; |
| 269 | extern const char ln_usage[]; |
| 270 | extern const char loadacm_usage[]; |
| 271 | extern const char loadfont_usage[]; |
| 272 | extern const char loadkmap_usage[]; |
| 273 | extern const char logger_usage[]; |
| 274 | extern const char logname_usage[]; |
| 275 | extern const char ls_usage[]; |
| 276 | extern const char lsmod_usage[]; |
| 277 | extern const char makedevs_usage[]; |
| 278 | extern const char md5sum_usage[]; |
| 279 | extern const char mkdir_usage[]; |
| 280 | extern const char mkfifo_usage[]; |
| 281 | extern const char mkfs_minix_usage[]; |
| 282 | extern const char mknod_usage[]; |
| 283 | extern const char mkswap_usage[]; |
| 284 | extern const char mktemp_usage[]; |
| 285 | extern const char more_usage[]; |
| 286 | extern const char mount_usage[]; |
| 287 | extern const char mt_usage[]; |
| 288 | extern const char mv_usage[]; |
| 289 | extern const char nc_usage[]; |
| 290 | extern const char nslookup_usage[]; |
| 291 | extern const char ping_usage[]; |
| 292 | extern const char poweroff_usage[]; |
| 293 | extern const char printf_usage[]; |
| 294 | extern const char ps_usage[]; |
| 295 | extern const char pwd_usage[]; |
| 296 | extern const char rdate_usage[]; |
| 297 | extern const char reboot_usage[]; |
| 298 | extern const char renice_usage[]; |
| 299 | extern const char reset_usage[]; |
| 300 | extern const char rm_usage[]; |
| 301 | extern const char rmdir_usage[]; |
| 302 | extern const char rmmod_usage[]; |
| 303 | extern const char sed_usage[]; |
| 304 | extern const char setkeycodes_usage[]; |
| 305 | extern const char shell_usage[]; |
| 306 | extern const char sleep_usage[]; |
| 307 | extern const char sort_usage[]; |
| 308 | extern const char swapoff_usage[]; |
| 309 | extern const char swapon_usage[]; |
| 310 | extern const char sync_usage[]; |
| 311 | extern const char syslogd_usage[]; |
| 312 | extern const char tail_usage[]; |
| 313 | extern const char tar_usage[]; |
| 314 | extern const char tee_usage[]; |
| 315 | extern const char telnet_usage[]; |
| 316 | extern const char test_usage[]; |
| 317 | extern const char touch_usage[]; |
| 318 | extern const char tr_usage[]; |
| 319 | extern const char true_usage[]; |
| 320 | extern const char tty_usage[]; |
| 321 | extern const char umount_usage[]; |
| 322 | extern const char uname_usage[]; |
| 323 | extern const char uniq_usage[]; |
| 324 | extern const char unix2dos_usage[]; |
| 325 | extern const char unrpm_usage[]; |
| 326 | extern const char update_usage[]; |
| 327 | extern const char uptime_usage[]; |
| 328 | extern const char usleep_usage[]; |
| 329 | extern const char uudecode_usage[]; |
| 330 | extern const char uuencode_usage[]; |
| 331 | extern const char wc_usage[]; |
| 332 | extern const char wget_usage[]; |
| 333 | extern const char which_usage[]; |
| 334 | extern const char whoami_usage[]; |
| 335 | extern const char xargs_usage[]; |
| 336 | extern const char yes_usage[]; |
| 337 | |
| 338 | extern const char *applet_name; |
| 339 | |
| 340 | extern void usage(const char *usage) __attribute__ ((noreturn)); |
| 341 | extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); |
| 342 | extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); |
| 343 | |
| 344 | const char *modeString(int mode); |
| 345 | const char *timeString(time_t timeVal); |
| 346 | int isDirectory(const char *name, const int followLinks, struct stat *statBuf); |
| 347 | int isDevice(const char *name); |
| 348 | |
| 349 | typedef struct ino_dev_hash_bucket_struct { |
| 350 | struct ino_dev_hash_bucket_struct *next; |
| 351 | ino_t ino; |
| 352 | dev_t dev; |
| 353 | char name[1]; |
| 354 | } ino_dev_hashtable_bucket_t; |
| 355 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name); |
| 356 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); |
| 357 | void reset_ino_dev_hashtable(void); |
| 358 | |
| 359 | int copyFile(const char *srcName, const char *destName, |
| 360 | int setModes, int followLinks, int forceFlag); |
| 361 | int copySubFile(int srcFd, int dstFd, size_t remaining); |
| 362 | char *buildName(const char *dirName, const char *fileName); |
| 363 | int makeString(int argc, const char **argv, char *buf, int bufLen); |
| 364 | char *getChunk(int size); |
| 365 | char *chunkstrdup(const char *str); |
| 366 | void freeChunks(void); |
| 367 | int fullWrite(int fd, const char *buf, int len); |
| 368 | int fullRead(int fd, char *buf, int len); |
| 369 | int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst, |
| 370 | int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData), |
| 371 | int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData), |
| 372 | void* userData); |
| 373 | |
| 374 | extern int createPath (const char *name, int mode); |
| 375 | extern int parse_mode( const char* s, mode_t* theMode); |
| 376 | |
| 377 | extern int get_kernel_revision(void); |
| 378 | |
| 379 | extern int get_console_fd(char* tty_name); |
| 380 | extern struct mntent *findMountPoint(const char *name, const char *table); |
| 381 | extern void write_mtab(char* blockDevice, char* directory, |
| 382 | char* filesystemType, long flags, char* string_flags); |
| 383 | extern void erase_mtab(const char * name); |
| 384 | extern void mtab_read(void); |
| 385 | extern char *mtab_first(void **iter); |
| 386 | extern char *mtab_next(void **iter); |
| 387 | extern char *mtab_getinfo(const char *match, const char which); |
| 388 | extern int check_wildcard_match(const char* text, const char* pattern); |
| 389 | extern long getNum (const char *cp); |
| 390 | extern pid_t* findPidByName( char* pidName); |
| 391 | extern int find_real_root_device_name(char* name); |
| 392 | extern char *get_line_from_file(FILE *file); |
| 393 | extern void print_file(FILE *file); |
| 394 | extern int print_file_by_name(char *filename); |
| 395 | extern char process_escape_sequence(char **ptr); |
| 396 | extern char *get_last_path_component(char *path); |
| 397 | extern void xregcomp(regex_t *preg, const char *regex, int cflags); |
| 398 | |
| 399 | #ifndef DMALLOC |
| 400 | extern void *xmalloc (size_t size); |
| 401 | extern void *xrealloc(void *old, size_t size); |
| 402 | extern void *xcalloc(size_t nmemb, size_t size); |
| 403 | extern char *xstrdup (const char *s); |
| 404 | #endif |
| 405 | extern char *xstrndup (const char *s, int n); |
| 406 | |
| 407 | |
| 408 | /* These parse entries in /etc/passwd and /etc/group. This is desirable |
| 409 | * for BusyBox since we want to avoid using the glibc NSS stuff, which |
| 410 | * increases target size and is often not needed embedded systems. */ |
| 411 | extern long my_getpwnam(char *name); |
| 412 | extern long my_getgrnam(char *name); |
| 413 | extern void my_getpwuid(char *name, long uid); |
| 414 | extern void my_getgrgid(char *group, long gid); |
| 415 | extern long my_getpwnamegid(char *name); |
| 416 | |
| 417 | extern int device_open(char *device, int mode); |
| 418 | |
| 419 | #if defined BB_FEATURE_MOUNT_LOOP |
| 420 | extern int del_loop(const char *device); |
| 421 | extern int set_loop(const char *device, const char *file, int offset, int *loopro); |
| 422 | extern char *find_unused_loop_device (void); |
| 423 | #endif |
| 424 | |
| 425 | |
| 426 | #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) |
| 427 | extern int vdprintf(int d, const char *format, va_list ap); |
| 428 | #endif |
| 429 | |
| 430 | #if defined BB_NFSMOUNT |
| 431 | int nfsmount(const char *spec, const char *node, int *flags, |
| 432 | char **extra_opts, char **mount_opts, int running_bg); |
| 433 | #endif |
| 434 | |
| 435 | #ifndef RB_POWER_OFF |
| 436 | /* Stop system and switch power off if possible. */ |
| 437 | #define RB_POWER_OFF 0x4321fedc |
| 438 | #endif |
| 439 | |
| 440 | /* Include our own copy of struct sysinfo to avoid binary compatability |
| 441 | * problems with Linux 2.4, which changed things. Grumble, grumble. */ |
| 442 | struct sysinfo { |
| 443 | long uptime; /* Seconds since boot */ |
| 444 | unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ |
| 445 | unsigned long totalram; /* Total usable main memory size */ |
| 446 | unsigned long freeram; /* Available memory size */ |
| 447 | unsigned long sharedram; /* Amount of shared memory */ |
| 448 | unsigned long bufferram; /* Memory used by buffers */ |
| 449 | unsigned long totalswap; /* Total swap space size */ |
| 450 | unsigned long freeswap; /* swap space still available */ |
| 451 | unsigned short procs; /* Number of current processes */ |
| 452 | unsigned long totalhigh; /* Total high memory size */ |
| 453 | unsigned long freehigh; /* Available high memory size */ |
| 454 | unsigned int mem_unit; /* Memory unit size in bytes */ |
| 455 | char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ |
| 456 | }; |
| 457 | extern int sysinfo (struct sysinfo* info); |
| 458 | |
| 459 | /* Bit map related macros -- libc5 doens't provide these... sigh. */ |
| 460 | #ifndef setbit |
| 461 | #define NBBY CHAR_BIT |
| 462 | #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY)) |
| 463 | #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) |
| 464 | #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY))) |
| 465 | #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) |
| 466 | #endif |
| 467 | |
| 468 | #endif /* _BB_INTERNAL_H_ */ |