blob: 4b2d3736b56152184b3bc530d39218113381ac0a [file] [log] [blame]
landleyc5621502006-09-28 17:18:51 -04001/* Toybox infrastructure.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
landleyc5621502006-09-28 17:18:51 -04004 */
5
Rob Landley5b405822014-03-29 18:11:00 -05006// Stuff that needs to go before the standard headers
Rob Landleyfd1c5ba2007-02-03 14:10:00 -05007
Rob Landley5b405822014-03-29 18:11:00 -05008#include "generated/config.h"
Rob Landley90163772007-01-18 21:54:08 -05009#include "lib/portability.h"
10
Rob Landley5b405822014-03-29 18:11:00 -050011// General posix-2008 headers
landley09ea7ac2006-10-30 01:38:00 -050012#include <ctype.h>
Rob Landleyfd1c5ba2007-02-03 14:10:00 -050013#include <dirent.h>
landley09ea7ac2006-10-30 01:38:00 -050014#include <errno.h>
15#include <fcntl.h>
Isaac Dunham9953f642014-04-09 17:26:09 -050016#include <fnmatch.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040017#include <grp.h>
landley09ea7ac2006-10-30 01:38:00 -050018#include <inttypes.h>
landley4f344e32006-10-05 16:18:03 -040019#include <limits.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060020#include <math.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040021#include <pwd.h>
Rob Landley5b405822014-03-29 18:11:00 -050022#include <regex.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060023#include <sched.h>
Rob Landley6973a1d2006-11-25 16:50:00 -050024#include <setjmp.h>
Rob Landley1bc52242014-05-21 07:24:16 -050025#include <signal.h>
landley4f344e32006-10-05 16:18:03 -040026#include <stdarg.h>
Rob Landleycaf39c22012-11-16 00:35:46 -060027#include <stddef.h>
landley09ea7ac2006-10-30 01:38:00 -050028#include <stdint.h>
landleyc5621502006-09-28 17:18:51 -040029#include <stdio.h>
landley4f344e32006-10-05 16:18:03 -040030#include <stdlib.h>
31#include <string.h>
Rob Landleye3b171e2012-04-28 01:22:50 -050032#include <strings.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040033#include <sys/mman.h>
Rob Landley6da3be92012-03-12 00:26:23 -050034#include <sys/resource.h>
landley00f87f12006-10-25 18:38:37 -040035#include <sys/stat.h>
landley09ea7ac2006-10-30 01:38:00 -050036#include <sys/statvfs.h>
Rob Landley503c8b82012-12-01 18:12:06 -060037#include <sys/time.h>
Rob Landley628eb9b2012-06-16 14:19:56 -050038#include <sys/times.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060039#include <sys/utsname.h>
landley09ea7ac2006-10-30 01:38:00 -050040#include <sys/wait.h>
Elie De Brauwer0b11a162012-04-24 23:09:27 +020041#include <syslog.h>
Isaac Dunham46ddf0e2014-11-19 16:38:46 -060042#include <termios.h>
Rob Landley628eb9b2012-06-16 14:19:56 -050043#include <time.h>
landley4f344e32006-10-05 16:18:03 -040044#include <unistd.h>
Rob Landley07c78d32007-12-28 03:29:33 -060045#include <utime.h>
Felix Janda250e0052012-11-21 20:38:29 +010046
Rob Landley5b405822014-03-29 18:11:00 -050047// Posix networking
Rob Landley98c322e2013-11-02 14:24:33 -050048
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 Landley5b405822014-03-29 18:11:00 -050058// 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 Landley0a4bd4b2015-01-16 13:55:32 -060065#include <pty.h>
Rob Landley5b405822014-03-29 18:11:00 -050066#include <sys/ioctl.h>
67#include <sys/statfs.h>
68#include <sys/sysinfo.h>
69
landley4f344e32006-10-05 16:18:03 -040070#include "lib/lib.h"
Rob Landleye2580db2007-01-23 13:20:38 -050071#include "toys/e2fs.h"
landleyc5621502006-09-28 17:18:51 -040072
Rob Landley55928b12008-01-19 17:43:27 -060073// Get list of function prototypes for all enabled command_main() functions.
74
75#define NEWTOY(name, opts, flags) void name##_main(void);
Rob Landleyf3e56f42014-12-31 21:30:59 -060076#define OLDTOY(name, oldname, flags) void oldname##_main(void);
Rob Landley55928b12008-01-19 17:43:27 -060077#include "generated/newtoys.h"
Rob Landley207cada2013-10-03 03:18:00 -050078#include "generated/flags.h"
Rob Landleyb1aaba12008-01-20 17:25:44 -060079#include "generated/globals.h"
Rob Landley55928b12008-01-19 17:43:27 -060080
Rob Landleyf2311a42006-11-04 17:45:18 -050081// These live in main.c
landleyc5621502006-09-28 17:18:51 -040082
landley4f344e32006-10-05 16:18:03 -040083struct toy_list *toy_find(char *name);
landleycd9dfc32006-10-18 18:38:16 -040084void toy_init(struct toy_list *which, char *argv[]);
85void toy_exec(char *argv[]);
landleyc5621502006-09-28 17:18:51 -040086
Rob Landley43e9d332012-04-14 21:43:24 -050087// Flags describing command behavior.
Rob Landleyb1aaba12008-01-20 17:25:44 -060088
89#define TOYFLAG_USR (1<<0)
90#define TOYFLAG_BIN (1<<1)
91#define TOYFLAG_SBIN (1<<2)
92#define TOYMASK_LOCATION ((1<<4)-1)
93
Rob Landley53dda1a2009-01-25 16:59:14 -060094// This is a shell built-in function, running in the same process context.
Rob Landleyb1aaba12008-01-20 17:25:44 -060095#define TOYFLAG_NOFORK (1<<4)
Rob Landley53dda1a2009-01-25 16:59:14 -060096
Rob Landley43e9d332012-04-14 21:43:24 -050097// Start command with a umask of 0 (saves old umask in this.old_umask)
Rob Landley0f8c4c52008-02-12 19:05:44 -060098#define TOYFLAG_UMASK (1<<5)
Rob Landleyb1aaba12008-01-20 17:25:44 -060099
Rob Landley43e9d332012-04-14 21:43:24 -0500100// This command runs as root.
Rob Landleye0377fb2010-01-05 12:17:05 -0600101#define TOYFLAG_STAYROOT (1<<6)
102#define TOYFLAG_NEEDROOT (1<<7)
103#define TOYFLAG_ROOTONLY (TOYFLAG_STAYROOT|TOYFLAG_NEEDROOT)
104
Rob Landley89a62bf2014-06-09 05:51:04 -0500105// Call setlocale to listen to environment variables.
106// This invalidates sprintf("%.*s", size, string) as a valid length constraint.
107#define TOYFLAG_LOCALE (1<<8)
108
Rob Landley933919c2013-04-21 12:15:59 -0500109// Array of available commands
Rob Landley53dda1a2009-01-25 16:59:14 -0600110
Rob Landleyb1aaba12008-01-20 17:25:44 -0600111extern struct toy_list {
Rob Landley7aa651a2012-11-13 17:14:08 -0600112 char *name;
113 void (*toy_main)(void);
114 char *options;
115 int flags;
Rob Landleyb1aaba12008-01-20 17:25:44 -0600116} toy_list[];
117
Rob Landley43e9d332012-04-14 21:43:24 -0500118// Global context shared by all commands.
landleyc5621502006-09-28 17:18:51 -0400119
120extern struct toy_context {
Rob Landley7aa651a2012-11-13 17:14:08 -0600121 struct toy_list *which; // Which entry in toy_list is this one?
Rob Landley7aa651a2012-11-13 17:14:08 -0600122 char **argv; // Original command line arguments
Rob Landley7aa651a2012-11-13 17:14:08 -0600123 char **optargs; // Arguments left over from get_optflags()
Rob Landley36aa7d72014-03-28 17:48:02 -0500124 unsigned optflags; // Command line option flags from get_optflags()
125 int exitval; // Value error_exit feeds to exit()
Rob Landley7aa651a2012-11-13 17:14:08 -0600126 int optc; // Count of optargs
127 int exithelp; // Should error_exit print a usage message first?
128 int old_umask; // Old umask preserved by TOYFLAG_UMASK
Rob Landley36aa7d72014-03-28 17:48:02 -0500129 int toycount; // Total number of commands in this build
Rob Landley1bc52242014-05-21 07:24:16 -0500130 int signal; // generic_signal() records what signal it saw here
131 int signalfd; // and writes signal to this fd, if set
Rob Landley4c2bd622014-05-05 21:20:11 -0500132
133 // This is at the end so toy_init() doesn't zero it.
134 jmp_buf *rebound; // longjmp here instead of exit when do_rebound set
Rob Landleyd4bae7d2014-10-26 13:34:33 -0500135 int recursion; // How many nested calls to toy_exec()
landleyc5621502006-09-28 17:18:51 -0400136} toys;
Rob Landley8324b892006-11-19 02:49:22 -0500137
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500138// Two big temporary buffers: one for use by commands, one for library functions
Rob Landley8324b892006-11-19 02:49:22 -0500139
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500140extern char toybuf[4096], libbuf[4096];
Rob Landleyb1aaba12008-01-20 17:25:44 -0600141
Strake59bf7ce2013-08-17 02:54:58 -0500142extern char **environ;
143
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500144#define GLOBALS(...)
Rob Landley9c1c5ec2012-08-14 01:42:06 -0500145
146#define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))