blob: 81c495529cf8dcd32909d85a571293301f7fe4a7 [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 Landleyd1f51492019-03-11 17:58:26 -050021#include <paths.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040022#include <pwd.h>
Rob Landley5b405822014-03-29 18:11:00 -050023#include <regex.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060024#include <sched.h>
Rob Landley6973a1d2006-11-25 16:50:00 -050025#include <setjmp.h>
Rob Landley1bc52242014-05-21 07:24:16 -050026#include <signal.h>
landley4f344e32006-10-05 16:18:03 -040027#include <stdarg.h>
Rob Landleycaf39c22012-11-16 00:35:46 -060028#include <stddef.h>
landley09ea7ac2006-10-30 01:38:00 -050029#include <stdint.h>
landleyc5621502006-09-28 17:18:51 -040030#include <stdio.h>
landley4f344e32006-10-05 16:18:03 -040031#include <stdlib.h>
32#include <string.h>
Rob Landleye3b171e2012-04-28 01:22:50 -050033#include <strings.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040034#include <sys/mman.h>
Rob Landley6da3be92012-03-12 00:26:23 -050035#include <sys/resource.h>
landley00f87f12006-10-25 18:38:37 -040036#include <sys/stat.h>
landley09ea7ac2006-10-30 01:38:00 -050037#include <sys/statvfs.h>
Rob Landley503c8b82012-12-01 18:12:06 -060038#include <sys/time.h>
Rob Landley628eb9b2012-06-16 14:19:56 -050039#include <sys/times.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060040#include <sys/utsname.h>
landley09ea7ac2006-10-30 01:38:00 -050041#include <sys/wait.h>
Elie De Brauwer0b11a162012-04-24 23:09:27 +020042#include <syslog.h>
Isaac Dunham46ddf0e2014-11-19 16:38:46 -060043#include <termios.h>
Rob Landley628eb9b2012-06-16 14:19:56 -050044#include <time.h>
landley4f344e32006-10-05 16:18:03 -040045#include <unistd.h>
Rob Landley07c78d32007-12-28 03:29:33 -060046#include <utime.h>
Felix Janda250e0052012-11-21 20:38:29 +010047
Rob Landley5b405822014-03-29 18:11:00 -050048// Posix networking
Rob Landley98c322e2013-11-02 14:24:33 -050049
50#include <arpa/inet.h>
51#include <netdb.h>
52#include <net/if.h>
53#include <netinet/in.h>
54#include <netinet/tcp.h>
55#include <poll.h>
56#include <sys/socket.h>
57#include <sys/un.h>
58
Rob Landley61cb6ab2021-12-29 13:28:45 -060059// Internationalization support (also in POSIX)
Rob Landley5b405822014-03-29 18:11:00 -050060
Rob Landley75b89012020-12-06 00:02:46 -060061#include <langinfo.h>
Rob Landley5b405822014-03-29 18:11:00 -050062#include <locale.h>
63#include <wchar.h>
64#include <wctype.h>
65
Rob Landley61cb6ab2021-12-29 13:28:45 -060066// Non-posix headers
Rob Landley5b405822014-03-29 18:11:00 -050067#include <sys/ioctl.h>
Rob Landley61cb6ab2021-12-29 13:28:45 -060068#include <sys/syscall.h>
Rob Landley5b405822014-03-29 18:11:00 -050069
Rob Landley7d6af772015-09-29 05:09:46 -050070#include "lib/lib.h"
71#include "lib/lsm.h"
Rob Landleydf07fb72016-02-10 23:27:55 -060072#include "lib/toyflags.h"
Rob Landley7d6af772015-09-29 05:09:46 -050073
74// Get list of function prototypes for all enabled command_main() functions.
75
76#define NEWTOY(name, opts, flags) void name##_main(void);
77#define OLDTOY(name, oldname, flags) void oldname##_main(void);
78#include "generated/newtoys.h"
79#include "generated/flags.h"
80#include "generated/globals.h"
Rob Landleyf96bb3d2015-12-13 16:52:26 -060081#include "generated/tags.h"
Rob Landley7d6af772015-09-29 05:09:46 -050082
Rob Landleyf2311a42006-11-04 17:45:18 -050083// These live in main.c
landleyc5621502006-09-28 17:18:51 -040084
landley4f344e32006-10-05 16:18:03 -040085struct toy_list *toy_find(char *name);
Rob Landleyfc0fbe62022-02-12 02:16:56 -060086void show_help(FILE *out, int full);
Rob Landley29e7ed92021-06-18 08:57:07 -050087void check_help(char **arg);
Rob Landley114541b2020-03-11 22:09:15 -050088void toy_singleinit(struct toy_list *which, char *argv[]);
Rob Landleyfc0fbe62022-02-12 02:16:56 -060089void toy_init(struct toy_list *which, char *argv[]);
landleycd9dfc32006-10-18 18:38:16 -040090void toy_exec(char *argv[]);
landleyc5621502006-09-28 17:18:51 -040091
Rob Landley933919c2013-04-21 12:15:59 -050092// Array of available commands
Rob Landley53dda1a2009-01-25 16:59:14 -060093
Rob Landleyb1aaba12008-01-20 17:25:44 -060094extern struct toy_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060095 char *name;
96 void (*toy_main)(void);
97 char *options;
Rob Landley677cd8c2019-03-10 23:05:24 -050098 unsigned flags;
Rob Landleyb1aaba12008-01-20 17:25:44 -060099} toy_list[];
100
Rob Landley43e9d332012-04-14 21:43:24 -0500101// Global context shared by all commands.
landleyc5621502006-09-28 17:18:51 -0400102
103extern struct toy_context {
Rob Landley7aa651a2012-11-13 17:14:08 -0600104 struct toy_list *which; // Which entry in toy_list is this one?
Rob Landley7aa651a2012-11-13 17:14:08 -0600105 char **argv; // Original command line arguments
Rob Landley7aa651a2012-11-13 17:14:08 -0600106 char **optargs; // Arguments left over from get_optflags()
Rob Landleyaaecbba2015-12-10 15:57:08 -0600107 unsigned long long optflags; // Command line option flags from get_optflags()
Rob Landley7aa651a2012-11-13 17:14:08 -0600108 int optc; // Count of optargs
Rob Landleyca311f12016-01-30 16:28:13 -0600109 short toycount; // Total number of commands in this build
Rob Landley19ebdb72017-10-21 09:53:14 -0500110 char exitval; // Value error_exit feeds to exit()
111 char wasroot; // dropped setuid
Rob Landley4c2bd622014-05-05 21:20:11 -0500112
Rob Landley114541b2020-03-11 22:09:15 -0500113 // toy_init() should not zero past here.
Elliott Hughesbbadc5e2019-01-22 12:52:55 -0800114 sigjmp_buf *rebound; // siglongjmp here instead of exit when do_rebound
Rob Landleyeb24df92016-03-13 20:23:41 -0500115 struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit()
Rob Landleyca311f12016-01-30 16:28:13 -0600116 void *stacktop; // nested toy_exec() call count, or 0 if vforked
Rob Landley114541b2020-03-11 22:09:15 -0500117 int envc; // Count of original environ entries
118 int old_umask; // Old umask preserved by TOYFLAG_UMASK
119 short signal; // generic_signal() records what signal it saw here
120 int signalfd; // and writes signal to this fd, if set
landleyc5621502006-09-28 17:18:51 -0400121} toys;
Rob Landley8324b892006-11-19 02:49:22 -0500122
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500123// Two big temporary buffers: one for use by commands, one for library functions
Rob Landley8324b892006-11-19 02:49:22 -0500124
Rob Landleyfc0fbe62022-02-12 02:16:56 -0600125extern char **environ, *toybox_version, toybuf[4096], libbuf[4096];
Strake59bf7ce2013-08-17 02:54:58 -0500126
Rob Landley747e2962018-12-04 21:29:51 -0600127#define FLAG(x) (toys.optflags&FLAG_##x)
128
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500129#define GLOBALS(...)
Rob Landley9c1c5ec2012-08-14 01:42:06 -0500130#define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
Rob Landleyfc7543b2015-12-12 21:18:40 -0600131#define TAGGED_ARRAY(X, ...) {__VA_ARGS__}
Rob Landley2b729ee2020-01-30 22:04:43 -0600132
133#ifndef TOYBOX_VERSION
134#ifndef TOYBOX_VENDOR
135#define TOYBOX_VENDOR ""
136#endif
Rob Landleya5584352021-11-30 06:13:23 -0600137#define TOYBOX_VERSION "0.8.6"TOYBOX_VENDOR
Rob Landley2b729ee2020-01-30 22:04:43 -0600138#endif