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 | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 6 | #include "generated/config.h" |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 7 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 8 | #include "lib/portability.h" |
9 | |||||
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 10 | #include <ctype.h> |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 11 | #include <dirent.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 12 | #include <errno.h> |
13 | #include <fcntl.h> | ||||
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 14 | #include <grp.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 15 | #include <inttypes.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 16 | #include <limits.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 17 | #include <libgen.h> |
18 | #include <math.h> | ||||
Rob Landley | ae2e4b7 | 2008-11-15 05:17:23 -0600 | [diff] [blame] | 19 | #include <pty.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 20 | #include <pwd.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 21 | #include <sched.h> |
Rob Landley | 6973a1d | 2006-11-25 16:50:00 -0500 | [diff] [blame] | 22 | #include <setjmp.h> |
Elie De Brauwer | 756f794 | 2012-07-15 13:28:51 +0200 | [diff] [blame] | 23 | #include <sched.h> |
Elie De Brauwer | 0b11a16 | 2012-04-24 23:09:27 +0200 | [diff] [blame] | 24 | #include <shadow.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 25 | #include <stdarg.h> |
Rob Landley | caf39c2 | 2012-11-16 00:35:46 -0600 | [diff] [blame] | 26 | #include <stddef.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 27 | #include <stdint.h> |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 28 | #include <stdio.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 29 | #include <stdlib.h> |
30 | #include <string.h> | ||||
Rob Landley | e3b171e | 2012-04-28 01:22:50 -0500 | [diff] [blame] | 31 | #include <strings.h> |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 32 | #include <sys/ioctl.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 33 | #include <sys/mman.h> |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 34 | #include <sys/mount.h> |
Rob Landley | 6da3be9 | 2012-03-12 00:26:23 -0500 | [diff] [blame] | 35 | #include <sys/resource.h> |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 36 | #include <sys/stat.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 37 | #include <sys/statvfs.h> |
Rob Landley | 9494ffc | 2012-02-17 12:05:26 -0600 | [diff] [blame] | 38 | #include <sys/sysinfo.h> |
Elie De Brauwer | 2c16281 | 2012-02-18 15:33:27 +0100 | [diff] [blame] | 39 | #include <sys/swap.h> |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 40 | #include <sys/times.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 41 | #include <sys/types.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 42 | #include <sys/utsname.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 43 | #include <sys/wait.h> |
Elie De Brauwer | 0b11a16 | 2012-04-24 23:09:27 +0200 | [diff] [blame] | 44 | #include <syslog.h> |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 45 | #include <time.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 46 | #include <unistd.h> |
Rob Landley | 07c78d3 | 2007-12-28 03:29:33 -0600 | [diff] [blame] | 47 | #include <utime.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 48 | #include <utmpx.h> |
Felix Janda | 250e005 | 2012-11-21 20:38:29 +0100 | [diff] [blame] | 49 | |
Rob Landley | 6cf0a11 | 2012-11-26 14:14:29 -0600 | [diff] [blame^] | 50 | // Internationalization support |
51 | |||||
Felix Janda | 250e005 | 2012-11-21 20:38:29 +0100 | [diff] [blame] | 52 | #include <locale.h> |
Felix Janda | abb8ca2 | 2012-11-08 11:19:07 -0600 | [diff] [blame] | 53 | #include <wchar.h> |
54 | #include <wctype.h> | ||||
Rob Landley | 07c78d3 | 2007-12-28 03:29:33 -0600 | [diff] [blame] | 55 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 56 | #include "lib/lib.h" |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 57 | #include "toys/e2fs.h" |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 58 | |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 59 | // Get list of function prototypes for all enabled command_main() functions. |
60 | |||||
61 | #define NEWTOY(name, opts, flags) void name##_main(void); | ||||
62 | #define OLDTOY(name, oldname, opts, flags) | ||||
63 | #include "generated/newtoys.h" | ||||
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 64 | #include "generated/globals.h" |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 65 | |
Rob Landley | f2311a4 | 2006-11-04 17:45:18 -0500 | [diff] [blame] | 66 | // These live in main.c |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 67 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 68 | struct toy_list *toy_find(char *name); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 69 | void toy_init(struct toy_list *which, char *argv[]); |
70 | void toy_exec(char *argv[]); | ||||
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 71 | |
Rob Landley | 43e9d33 | 2012-04-14 21:43:24 -0500 | [diff] [blame] | 72 | // Flags describing command behavior. |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 73 | |
74 | #define TOYFLAG_USR (1<<0) | ||||
75 | #define TOYFLAG_BIN (1<<1) | ||||
76 | #define TOYFLAG_SBIN (1<<2) | ||||
77 | #define TOYMASK_LOCATION ((1<<4)-1) | ||||
78 | |||||
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 79 | // This is a shell built-in function, running in the same process context. |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 80 | #define TOYFLAG_NOFORK (1<<4) |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 81 | |
Rob Landley | 43e9d33 | 2012-04-14 21:43:24 -0500 | [diff] [blame] | 82 | // Start command with a umask of 0 (saves old umask in this.old_umask) |
Rob Landley | 0f8c4c5 | 2008-02-12 19:05:44 -0600 | [diff] [blame] | 83 | #define TOYFLAG_UMASK (1<<5) |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 84 | |
Rob Landley | 43e9d33 | 2012-04-14 21:43:24 -0500 | [diff] [blame] | 85 | // This command runs as root. |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 86 | #define TOYFLAG_STAYROOT (1<<6) |
87 | #define TOYFLAG_NEEDROOT (1<<7) | ||||
88 | #define TOYFLAG_ROOTONLY (TOYFLAG_STAYROOT|TOYFLAG_NEEDROOT) | ||||
89 | |||||
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 90 | // Array of available applets |
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? |
103 | int exitval; // Value error_exit feeds to exit() | ||||
104 | char **argv; // Original command line arguments | ||||
105 | unsigned optflags; // Command line option flags from get_optflags() | ||||
106 | char **optargs; // Arguments left over from get_optflags() | ||||
107 | int optc; // Count of optargs | ||||
108 | int exithelp; // Should error_exit print a usage message first? | ||||
109 | int old_umask; // Old umask preserved by TOYFLAG_UMASK | ||||
Rob Landley | caf39c2 | 2012-11-16 00:35:46 -0600 | [diff] [blame] | 110 | jmp_buf *rebound; // longjmp here instead of exit when do_rebound set |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 111 | } toys; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 112 | |
Rob Landley | 43e9d33 | 2012-04-14 21:43:24 -0500 | [diff] [blame] | 113 | // One big temporary buffer, for use by commands (not library functions). |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 114 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 115 | extern char toybuf[4096]; |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 116 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 117 | #define GLOBALS(...) |
Rob Landley | 9c1c5ec | 2012-08-14 01:42:06 -0500 | [diff] [blame] | 118 | |
119 | #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) |