landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 1 | /* vi: set ts=4 :*/ |
| 2 | /* Toybox infrastructure. |
| 3 | * |
| 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | * |
| 6 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 9 | #include "generated/config.h" |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 10 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 11 | #include "lib/portability.h" |
| 12 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 13 | #include <ctype.h> |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 14 | #include <dirent.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | #include <fcntl.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 | ae2e4b7 | 2008-11-15 05:17:23 -0600 | [diff] [blame] | 20 | #include <pty.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 21 | #include <pwd.h> |
Rob Landley | 6973a1d | 2006-11-25 16:50:00 -0500 | [diff] [blame] | 22 | #include <setjmp.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 23 | #include <stdarg.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 24 | #include <stdint.h> |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 25 | #include <stdio.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Rob Landley | c92fde0 | 2007-04-23 15:45:55 -0400 | [diff] [blame] | 29 | #include <sys/mman.h> |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 30 | #include <sys/mount.h> |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 31 | #include <sys/stat.h> |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 32 | #include <sys/statvfs.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 35 | #include <unistd.h> |
Rob Landley | 07c78d3 | 2007-12-28 03:29:33 -0600 | [diff] [blame] | 36 | #include <utime.h> |
| 37 | |
Rob Landley | 6f45849 | 2009-08-06 20:39:28 -0500 | [diff] [blame^] | 38 | #undef _XOPEN_SOURCE |
Rob Landley | 07c78d3 | 2007-12-28 03:29:33 -0600 | [diff] [blame] | 39 | #define _XOPEN_SOURCE 600 |
| 40 | #include <time.h> |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 41 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 42 | #include "lib/lib.h" |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 43 | #include "toys/e2fs.h" |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 44 | |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 45 | // Get list of function prototypes for all enabled command_main() functions. |
| 46 | |
| 47 | #define NEWTOY(name, opts, flags) void name##_main(void); |
| 48 | #define OLDTOY(name, oldname, opts, flags) |
| 49 | #include "generated/newtoys.h" |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 50 | #include "generated/globals.h" |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 51 | |
Rob Landley | f2311a4 | 2006-11-04 17:45:18 -0500 | [diff] [blame] | 52 | // These live in main.c |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 53 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 54 | struct toy_list *toy_find(char *name); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 55 | void toy_init(struct toy_list *which, char *argv[]); |
| 56 | void toy_exec(char *argv[]); |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 57 | |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 58 | // Flags describing applet behavior. |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 59 | |
| 60 | #define TOYFLAG_USR (1<<0) |
| 61 | #define TOYFLAG_BIN (1<<1) |
| 62 | #define TOYFLAG_SBIN (1<<2) |
| 63 | #define TOYMASK_LOCATION ((1<<4)-1) |
| 64 | |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 65 | // 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] | 66 | #define TOYFLAG_NOFORK (1<<4) |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 67 | |
| 68 | // Start applet with a umask of 0 (saves old umask in this.old_umask) |
Rob Landley | 0f8c4c5 | 2008-02-12 19:05:44 -0600 | [diff] [blame] | 69 | #define TOYFLAG_UMASK (1<<5) |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 70 | |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 71 | // Array of available applets |
| 72 | |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 73 | extern struct toy_list { |
| 74 | char *name; |
| 75 | void (*toy_main)(void); |
| 76 | char *options; |
| 77 | int flags; |
| 78 | } toy_list[]; |
| 79 | |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 80 | // Global context shared by all applets. |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 81 | |
| 82 | extern struct toy_context { |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 83 | struct toy_list *which; // Which entry in toy_list is this one? |
| 84 | int exitval; // Value error_exit feeds to exit() |
Rob Landley | f2f98fa | 2007-05-17 02:38:27 -0400 | [diff] [blame] | 85 | char **argv; // Original command line arguments |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 86 | unsigned optflags; // Command line option flags from get_optflags() |
| 87 | char **optargs; // Arguments left over from get_optflags() |
Rob Landley | 26bf9e6 | 2008-02-12 17:36:13 -0600 | [diff] [blame] | 88 | int optc; // Count of optargs |
Rob Landley | 53dda1a | 2009-01-25 16:59:14 -0600 | [diff] [blame] | 89 | int exithelp; // Should error_exit print a usage message first? |
| 90 | int old_umask; // Old umask preserved by TOYFLAG_UMASK |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 91 | } toys; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 92 | |
| 93 | // One big temporary buffer, for use by applets (not library functions). |
| 94 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 95 | extern char toybuf[4096]; |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 96 | |
| 97 | #define DEFINE_GLOBALS(...) |