landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 1 | /* Toybox infrastructure. |
| 2 | * |
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 6 | // Stuff that needs to go before the standard headers |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 7 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 8 | #include "generated/config.h" |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 9 | #include "lib/portability.h" |
| 10 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 11 | // General posix-2008 headers |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 12 | #include <ctype.h> |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 13 | #include <dirent.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
Isaac Dunham | 9953f64 | 2014-04-09 17:26:09 -0500 | [diff] [blame] | 16 | #include <fnmatch.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 17 | #include <grp.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 18 | #include <inttypes.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 19 | #include <limits.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 20 | #include <math.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 21 | #include <pwd.h> |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 22 | #include <regex.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 23 | #include <sched.h> |
Rob Landley | 6973a1d | 2006-11-25 16:50:00 -0500 | [diff] [blame] | 24 | #include <setjmp.h> |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 25 | #include <signal.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 26 | #include <stdarg.h> |
Rob Landley | caf39c2 | 2012-11-16 00:35:46 -0600 | [diff] [blame] | 27 | #include <stddef.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 28 | #include <stdint.h> |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 29 | #include <stdio.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Rob Landley | e3b171e | 2012-04-28 01:22:50 -0500 | [diff] [blame] | 32 | #include <strings.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 33 | #include <sys/mman.h> |
Rob Landley | 6da3be9 | 2012-03-12 00:26:23 -0500 | [diff] [blame] | 34 | #include <sys/resource.h> |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 35 | #include <sys/stat.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 36 | #include <sys/statvfs.h> |
Rob Landley | 503c8b8 | 2012-12-01 18:12:06 -0600 | [diff] [blame] | 37 | #include <sys/time.h> |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 38 | #include <sys/times.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 39 | #include <sys/utsname.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 40 | #include <sys/wait.h> |
Elie De Brauwer | 0b11a16 | 2012-04-24 23:09:27 +0200 | [diff] [blame] | 41 | #include <syslog.h> |
Isaac Dunham | 46ddf0e | 2014-11-19 16:38:46 -0600 | [diff] [blame] | 42 | #include <termios.h> |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 43 | #include <time.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 44 | #include <unistd.h> |
Rob Landley | 07c78d3 | 2007-12-28 03:29:33 -0600 | [diff] [blame] | 45 | #include <utime.h> |
Felix Janda | 250e005 | 2012-11-21 20:38:29 +0100 | [diff] [blame] | 46 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 47 | // Posix networking |
Rob Landley | 98c322e | 2013-11-02 14:24:33 -0500 | [diff] [blame] | 48 | |
| 49 | #include <arpa/inet.h> |
| 50 | #include <netdb.h> |
| 51 | #include <net/if.h> |
| 52 | #include <netinet/in.h> |
| 53 | #include <netinet/tcp.h> |
| 54 | #include <poll.h> |
| 55 | #include <sys/socket.h> |
| 56 | #include <sys/un.h> |
| 57 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 58 | // Internationalization support (also in POSIX and LSB) |
| 59 | |
| 60 | #include <locale.h> |
| 61 | #include <wchar.h> |
| 62 | #include <wctype.h> |
| 63 | |
| 64 | // LSB 4.1 headers |
Rob Landley | 0a4bd4b | 2015-01-16 13:55:32 -0600 | [diff] [blame] | 65 | #include <pty.h> |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 66 | #include <sys/ioctl.h> |
| 67 | #include <sys/statfs.h> |
| 68 | #include <sys/sysinfo.h> |
| 69 | |
Rob Landley | 7d6af77 | 2015-09-29 05:09:46 -0500 | [diff] [blame] | 70 | #include "lib/lib.h" |
| 71 | #include "lib/lsm.h" |
Rob Landley | df07fb7 | 2016-02-10 23:27:55 -0600 | [diff] [blame] | 72 | #include "lib/toyflags.h" |
Rob Landley | 7d6af77 | 2015-09-29 05:09:46 -0500 | [diff] [blame] | 73 | #include "toys/e2fs.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" |
Rob Landley | f96bb3d | 2015-12-13 16:52:26 -0600 | [diff] [blame] | 82 | #include "generated/tags.h" |
Rob Landley | 7d6af77 | 2015-09-29 05:09:46 -0500 | [diff] [blame] | 83 | |
Rob Landley | f2311a4 | 2006-11-04 17:45:18 -0500 | [diff] [blame] | 84 | // These live in main.c |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 85 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 86 | struct toy_list *toy_find(char *name); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 87 | void toy_init(struct toy_list *which, char *argv[]); |
| 88 | void toy_exec(char *argv[]); |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 89 | |
Rob Landley | 933919c | 2013-04-21 12:15:59 -0500 | [diff] [blame] | 90 | // Array of available commands |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 91 | |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 92 | extern struct toy_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 93 | char *name; |
| 94 | void (*toy_main)(void); |
| 95 | char *options; |
| 96 | int flags; |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 97 | } toy_list[]; |
| 98 | |
Rob Landley | 43e9d33 | 2012-04-14 21:43:24 -0500 | [diff] [blame] | 99 | // Global context shared by all commands. |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 100 | |
| 101 | extern struct toy_context { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 102 | struct toy_list *which; // Which entry in toy_list is this one? |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 103 | char **argv; // Original command line arguments |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 104 | char **optargs; // Arguments left over from get_optflags() |
Rob Landley | aaecbba | 2015-12-10 15:57:08 -0600 | [diff] [blame] | 105 | unsigned long long optflags; // Command line option flags from get_optflags() |
Rob Landley | 36aa7d7 | 2014-03-28 17:48:02 -0500 | [diff] [blame] | 106 | int exitval; // Value error_exit feeds to exit() |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 107 | int optc; // Count of optargs |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 108 | int old_umask; // Old umask preserved by TOYFLAG_UMASK |
Rob Landley | ca311f1 | 2016-01-30 16:28:13 -0600 | [diff] [blame] | 109 | short toycount; // Total number of commands in this build |
| 110 | short signal; // generic_signal() records what signal it saw here |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 111 | int signalfd; // and writes signal to this fd, if set |
Rob Landley | ca311f1 | 2016-01-30 16:28:13 -0600 | [diff] [blame] | 112 | int wasroot; // dropped setuid |
Rob Landley | 4c2bd62 | 2014-05-05 21:20:11 -0500 | [diff] [blame] | 113 | |
| 114 | // This is at the end so toy_init() doesn't zero it. |
| 115 | jmp_buf *rebound; // longjmp here instead of exit when do_rebound set |
Rob Landley | eb24df9 | 2016-03-13 20:23:41 -0500 | [diff] [blame] | 116 | struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit() |
Rob Landley | ca311f1 | 2016-01-30 16:28:13 -0600 | [diff] [blame] | 117 | void *stacktop; // nested toy_exec() call count, or 0 if vforked |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 118 | } toys; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 119 | |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 120 | // Two big temporary buffers: one for use by commands, one for library functions |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 121 | |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 122 | extern char toybuf[4096], libbuf[4096]; |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 123 | |
Strake | 59bf7ce | 2013-08-17 02:54:58 -0500 | [diff] [blame] | 124 | extern char **environ; |
| 125 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 126 | #define GLOBALS(...) |
Rob Landley | 9c1c5ec | 2012-08-14 01:42:06 -0500 | [diff] [blame] | 127 | #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) |
Rob Landley | fc7543b | 2015-12-12 21:18:40 -0600 | [diff] [blame] | 128 | #define TAGGED_ARRAY(X, ...) {__VA_ARGS__} |