1 /* Toybox infrastructure. 2 * 3 * Copyright 2006 Rob Landley <rob@landley.net> 4 */ 5 6 // Stuff that needs to go before the standard headers 7 8 #include "generated/config.h" 9 #include "lib/portability.h" 10 11 // General posix-2008 headers 12 #include <ctype.h> 13 #include <dirent.h> 14 #include <errno.h> 15 #include <fcntl.h> 16 #include <fnmatch.h> 17 #include <grp.h> 18 #include <inttypes.h> 19 #include <limits.h> 20 #include <math.h> 21 #include <paths.h> 22 #include <pwd.h> 23 #include <regex.h> 24 #include <sched.h> 25 #include <setjmp.h> 26 #include <signal.h> 27 #include <stdarg.h> 28 #include <stddef.h> 29 #include <stdint.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <strings.h> 34 #include <sys/mman.h> 35 #include <sys/resource.h> 36 #include <sys/stat.h> 37 #include <sys/statvfs.h> 38 #include <sys/time.h> 39 #include <sys/times.h> 40 #include <sys/utsname.h> 41 #include <sys/wait.h> 42 #include <syslog.h> 43 #include <termios.h> 44 #include <time.h> 45 #include <unistd.h> 46 #include <utime.h> 47 48 // Posix networking 49 50 #include <arpa/inet.h> 51 #include <netdb.h> 52 #include <net/if.h> 53 #include <netinet/in.h> 54 #include <netinet/tcp.h> 55 #include <poll.h> 56 #include <sys/socket.h> 57 #include <sys/un.h> 58 59 // Internationalization support (also in POSIX) 60 61 #include <langinfo.h> 62 #include <locale.h> 63 #include <wchar.h> 64 #include <wctype.h> 65 66 // Non-posix headers 67 #include <sys/ioctl.h> 68 #include <sys/syscall.h> 69 70 #include "lib/lib.h" 71 #include "lib/lsm.h" 72 #include "lib/toyflags.h" 73 74 // Get list of function prototypes for all enabled command_main() functions. 75 76 #define NEWTOY(name, opts, flags) void name##_main(void); 77 #define OLDTOY(name, oldname, flags) void oldname##_main(void); 78 #include "generated/newtoys.h" 79 #include "generated/flags.h" 80 #include "generated/globals.h" 81 #include "generated/tags.h" 82 83 // These live in main.c 84 85 #define HELP_USAGE 1 // usage: line only 86 #define HELP_HEADER 2 // Add Toybox header line to help output 87 #define HELP_SEE 4 // "See COMMAND" instead of dereferencing alias 88 #define HELP_HTML 8 // Output HTML 89 90 struct toy_list *toy_find(char *name); 91 void show_help(FILE *out, int full); 92 void check_help(char **arg); 93 void toy_singleinit(struct toy_list *which, char *argv[]); 94 void toy_init(struct toy_list *which, char *argv[]); 95 void toy_exec(char *argv[]); 96 97 // Array of available commands 98 99 extern struct toy_list { 100 char *name; 101 void (*toy_main)(void); 102 char *options; 103 unsigned flags; 104 } toy_list[]; 105 106 // Global context shared by all commands. 107 108 extern struct toy_context { 109 struct toy_list *which; // Which entry in toy_list is this one? 110 char **argv; // Original command line arguments 111 char **optargs; // Arguments left over from get_optflags() 112 unsigned long long optflags; // Command line option flags from get_optflags() 113 int optc; // Count of optargs 114 short toycount; // Total number of commands in this build 115 char exitval; // Value error_exit feeds to exit() 116 char wasroot; // dropped setuid 117 118 // toy_init() should not zero past here. 119 sigjmp_buf *rebound; // siglongjmp here instead of exit when do_rebound 120 struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit() 121 void *stacktop; // nested toy_exec() call count, or 0 if vforked 122 int envc; // Count of original environ entries 123 int old_umask; // Old umask preserved by TOYFLAG_UMASK 124 short signal; // generic_signal() records what signal it saw here 125 int signalfd; // and writes signal to this fd, if set 126 } toys; 127 128 // Two big temporary buffers: one for use by commands, one for library functions 129 130 extern char **environ, *toybox_version, toybuf[4096], libbuf[4096]; 131 132 #define FLAG(x) (!!(toys.optflags&FLAG_##x)) // Return 1 if flag set, 0 if not 133 134 #define GLOBALS(...) 135 #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) 136 #define TAGGED_ARRAY(X, ...) {__VA_ARGS__} 137 138 #ifndef TOYBOX_VERSION 139 #ifndef TOYBOX_VENDOR 140 #define TOYBOX_VENDOR "" 141 #endif 142 #define TOYBOX_VERSION "0.8.9"TOYBOX_VENDOR 143 #endif 144