1 /* Toybox infrastructure. 2 * 3 * Copyright 2006 Rob Landley <[email protected]> 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 <grp.h> 17 #include <inttypes.h> 18 #include <limits.h> 19 #include <math.h> 20 #include <paths.h> 21 #include <pwd.h> 22 #include <regex.h> 23 #include <sched.h> 24 #include <setjmp.h> 25 #include <signal.h> 26 #include <stdarg.h> 27 #include <stddef.h> 28 #include <stdint.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <strings.h> 33 #include <sys/mman.h> 34 #include <sys/resource.h> 35 #include <sys/stat.h> 36 #include <sys/statvfs.h> 37 #include <sys/time.h> 38 #include <sys/times.h> 39 #include <sys/uio.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 #include <sys/ttydefaults.h> 70 71 #include "lib/lib.h" 72 #include "lib/lsm.h" 73 #include "lib/toyflags.h" 74 75 // Get list of function prototypes for all enabled command_main() functions. 76 77 #define NEWTOY(name, opts, flags) void name##_main(void); 78 #define OLDTOY(name, oldname, flags) void oldname##_main(void); 79 #include "generated/newtoys.h" 80 #include "generated/flags.h" 81 #include "generated/globals.h" 82 #include "generated/tags.h" 83 84 // These live in main.c 85 86 #define HELP_USAGE 1 // usage: line only 87 #define HELP_HEADER 2 // Add Toybox header line to help output 88 #define HELP_SEE 4 // "See COMMAND" instead of dereferencing alias 89 #define HELP_HTML 8 // Output HTML 90 91 struct toy_list *toy_find(char *name); 92 void show_help(FILE *out, int full); 93 void check_help(char **arg); 94 void toy_singleinit(struct toy_list *which, char *argv[]); 95 void toy_init(struct toy_list *which, char *argv[]); 96 void toy_exec_which(struct toy_list *which, char *argv[]); 97 void toy_exec(char *argv[]); 98 99 // Array of available commands 100 101 extern struct toy_list { 102 char *name; 103 void (*toy_main)(void); 104 char *options; 105 unsigned flags; 106 } toy_list[]; 107 108 // Global context shared by all commands. 109 110 extern struct toy_context { 111 struct toy_list *which; // Which entry in toy_list is this one? 112 char **argv; // Original command line arguments 113 char **optargs; // Arguments left over from get_optflags() 114 unsigned long long optflags; // Command line option flags from get_optflags() 115 int optc; // Count of optargs 116 short toycount; // Total number of commands in this build 117 char exitval; // Value error_exit feeds to exit() 118 char wasroot; // dropped setuid 119 120 // toy_init() should not zero past here. 121 sigjmp_buf *rebound; // siglongjmp here instead of exit when do_rebound 122 struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit() 123 void *stacktop; // nested toy_exec() call count, or 0 if vforked 124 int envc; // Count of original environ entries 125 int old_umask; // Old umask preserved by TOYFLAG_UMASK 126 short signal; // generic_signal() records what signal it saw here 127 int signalfd; // and writes signal to this fd, if set 128 } toys; 129 130 // Two big temporary buffers: one for use by commands, one for library functions 131 132 extern char **environ, *toybox_version, toybuf[4096], libbuf[4096]; 133 134 #define FLAG(x) (!!(toys.optflags&FLAG_##x)) // Return 1 if flag set, 0 if not 135 136 #define GLOBALS(...) 137 #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) 138 #define TAGGED_ARRAY(X, ...) {__VA_ARGS__} 139 140 #ifndef TOYBOX_VERSION 141 #ifndef TOYBOX_VENDOR 142 #define TOYBOX_VENDOR "" 143 #endif 144 #define TOYBOX_VERSION "0.8.11"TOYBOX_VENDOR 145 #endif 146