• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "syscall.h"
11 
12 // General posix-2008 headers
13 #include <ctype.h>
14 #include <dirent.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <fnmatch.h>
18 #include <grp.h>
19 #include <inttypes.h>
20 #include <limits.h>
21 #include <math.h>
22 #include <paths.h>
23 #include <pwd.h>
24 #include <regex.h>
25 #include <sched.h>
26 #include <setjmp.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/stat.h>
38 #include <sys/statfs.h>
39 #include <sys/statvfs.h>
40 #include <sys/sysinfo.h>
41 #include <sys/time.h>
42 #include <sys/times.h>
43 #include <sys/utsname.h>
44 #include <sys/wait.h>
45 #include <syslog.h>
46 #include <termios.h>
47 #include <time.h>
48 #include <unistd.h>
49 #include <utime.h>
50 
51 // Posix networking
52 
53 #include <arpa/inet.h>
54 #include <netdb.h>
55 #include <net/if.h>
56 #include <netinet/in.h>
57 #include <netinet/tcp.h>
58 #include <poll.h>
59 #include <sys/socket.h>
60 #include <sys/un.h>
61 
62 // Internationalization support (also in POSIX and LSB)
63 
64 #include <locale.h>
65 #include <wchar.h>
66 #include <wctype.h>
67 
68 // LSB 4.1 headers
69 #include <sys/ioctl.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 USER_PRIVILEGE_PROCESS_GROUP 1U
87 #define KERNEL_PROCESS_GROUP         2U
88 
89 struct toy_list *toy_find(char *name);
90 void toy_init(struct toy_list *which, char *argv[]);
91 void toy_exec(char *argv[]);
92 
93 // Array of available commands
94 
95 extern struct toy_list {
96   char *name;
97   void (*toy_main)(void);
98   char *options;
99   unsigned flags;
100 } toy_list[];
101 
102 // Global context shared by all commands.
103 
104 extern struct toy_context {
105   struct toy_list *which;  // Which entry in toy_list is this one?
106   char **argv;             // Original command line arguments
107   char **optargs;          // Arguments left over from get_optflags()
108   unsigned long long optflags; // Command line option flags from get_optflags()
109   int optc;                // Count of optargs
110   int envc;                // Count of original environ entries
111   int old_umask;           // Old umask preserved by TOYFLAG_UMASK
112   short toycount;          // Total number of commands in this build
113   short signal;            // generic_signal() records what signal it saw here
114   int signalfd;            // and writes signal to this fd, if set
115   char exitval;            // Value error_exit feeds to exit()
116   char wasroot;            // dropped setuid
117 
118   // This is at the end so toy_init() doesn't zero it.
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 } toys;
123 
124 // Two big temporary buffers: one for use by commands, one for library functions
125 
126 extern char toybuf[4096], libbuf[4096];
127 
128 extern char **environ;
129 
130 #define FLAG(x) (toys.optflags&FLAG_##x)
131 
132 #define GLOBALS(...)
133 #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
134 #define TAGGED_ARRAY(X, ...) {__VA_ARGS__}
135