10f66f451Sopenharmony_ci/* Toybox infrastructure.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2006 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci */
50f66f451Sopenharmony_ci
60f66f451Sopenharmony_ci// Stuff that needs to go before the standard headers
70f66f451Sopenharmony_ci
80f66f451Sopenharmony_ci#include "generated/config.h"
90f66f451Sopenharmony_ci#include "lib/portability.h"
100f66f451Sopenharmony_ci#include "syscall.h"
110f66f451Sopenharmony_ci
120f66f451Sopenharmony_ci// General posix-2008 headers
130f66f451Sopenharmony_ci#include <ctype.h>
140f66f451Sopenharmony_ci#include <dirent.h>
150f66f451Sopenharmony_ci#include <errno.h>
160f66f451Sopenharmony_ci#include <fcntl.h>
170f66f451Sopenharmony_ci#include <fnmatch.h>
180f66f451Sopenharmony_ci#include <grp.h>
190f66f451Sopenharmony_ci#include <inttypes.h>
200f66f451Sopenharmony_ci#include <limits.h>
210f66f451Sopenharmony_ci#include <math.h>
220f66f451Sopenharmony_ci#include <paths.h>
230f66f451Sopenharmony_ci#include <pwd.h>
240f66f451Sopenharmony_ci#include <regex.h>
250f66f451Sopenharmony_ci#include <sched.h>
260f66f451Sopenharmony_ci#include <setjmp.h>
270f66f451Sopenharmony_ci#include <signal.h>
280f66f451Sopenharmony_ci#include <stdarg.h>
290f66f451Sopenharmony_ci#include <stddef.h>
300f66f451Sopenharmony_ci#include <stdint.h>
310f66f451Sopenharmony_ci#include <stdio.h>
320f66f451Sopenharmony_ci#include <stdlib.h>
330f66f451Sopenharmony_ci#include <string.h>
340f66f451Sopenharmony_ci#include <strings.h>
350f66f451Sopenharmony_ci#include <sys/mman.h>
360f66f451Sopenharmony_ci#include <sys/resource.h>
370f66f451Sopenharmony_ci#include <sys/stat.h>
380f66f451Sopenharmony_ci#include <sys/statfs.h>
390f66f451Sopenharmony_ci#include <sys/statvfs.h>
400f66f451Sopenharmony_ci#include <sys/sysinfo.h>
410f66f451Sopenharmony_ci#include <sys/time.h>
420f66f451Sopenharmony_ci#include <sys/times.h>
430f66f451Sopenharmony_ci#include <sys/utsname.h>
440f66f451Sopenharmony_ci#include <sys/wait.h>
450f66f451Sopenharmony_ci#include <syslog.h>
460f66f451Sopenharmony_ci#include <termios.h>
470f66f451Sopenharmony_ci#include <time.h>
480f66f451Sopenharmony_ci#include <unistd.h>
490f66f451Sopenharmony_ci#include <utime.h>
500f66f451Sopenharmony_ci
510f66f451Sopenharmony_ci// Posix networking
520f66f451Sopenharmony_ci
530f66f451Sopenharmony_ci#include <arpa/inet.h>
540f66f451Sopenharmony_ci#include <netdb.h>
550f66f451Sopenharmony_ci#include <net/if.h>
560f66f451Sopenharmony_ci#include <netinet/in.h>
570f66f451Sopenharmony_ci#include <netinet/tcp.h>
580f66f451Sopenharmony_ci#include <poll.h>
590f66f451Sopenharmony_ci#include <sys/socket.h>
600f66f451Sopenharmony_ci#include <sys/un.h>
610f66f451Sopenharmony_ci
620f66f451Sopenharmony_ci// Internationalization support (also in POSIX and LSB)
630f66f451Sopenharmony_ci
640f66f451Sopenharmony_ci#include <locale.h>
650f66f451Sopenharmony_ci#include <wchar.h>
660f66f451Sopenharmony_ci#include <wctype.h>
670f66f451Sopenharmony_ci
680f66f451Sopenharmony_ci// LSB 4.1 headers
690f66f451Sopenharmony_ci#include <sys/ioctl.h>
700f66f451Sopenharmony_ci
710f66f451Sopenharmony_ci#include "lib/lib.h"
720f66f451Sopenharmony_ci#include "lib/lsm.h"
730f66f451Sopenharmony_ci#include "lib/toyflags.h"
740f66f451Sopenharmony_ci
750f66f451Sopenharmony_ci// Get list of function prototypes for all enabled command_main() functions.
760f66f451Sopenharmony_ci
770f66f451Sopenharmony_ci#define NEWTOY(name, opts, flags) void name##_main(void);
780f66f451Sopenharmony_ci#define OLDTOY(name, oldname, flags) void oldname##_main(void);
790f66f451Sopenharmony_ci#include "generated/newtoys.h"
800f66f451Sopenharmony_ci#include "generated/flags.h"
810f66f451Sopenharmony_ci#include "generated/globals.h"
820f66f451Sopenharmony_ci#include "generated/tags.h"
830f66f451Sopenharmony_ci
840f66f451Sopenharmony_ci// These live in main.c
850f66f451Sopenharmony_ci
860f66f451Sopenharmony_ci#define USER_PRIVILEGE_PROCESS_GROUP 1U
870f66f451Sopenharmony_ci#define KERNEL_PROCESS_GROUP         2U
880f66f451Sopenharmony_ci
890f66f451Sopenharmony_cistruct toy_list *toy_find(char *name);
900f66f451Sopenharmony_civoid toy_init(struct toy_list *which, char *argv[]);
910f66f451Sopenharmony_civoid toy_exec(char *argv[]);
920f66f451Sopenharmony_ci
930f66f451Sopenharmony_ci// Array of available commands
940f66f451Sopenharmony_ci
950f66f451Sopenharmony_ciextern struct toy_list {
960f66f451Sopenharmony_ci  char *name;
970f66f451Sopenharmony_ci  void (*toy_main)(void);
980f66f451Sopenharmony_ci  char *options;
990f66f451Sopenharmony_ci  unsigned flags;
1000f66f451Sopenharmony_ci} toy_list[];
1010f66f451Sopenharmony_ci
1020f66f451Sopenharmony_ci// Global context shared by all commands.
1030f66f451Sopenharmony_ci
1040f66f451Sopenharmony_ciextern struct toy_context {
1050f66f451Sopenharmony_ci  struct toy_list *which;  // Which entry in toy_list is this one?
1060f66f451Sopenharmony_ci  char **argv;             // Original command line arguments
1070f66f451Sopenharmony_ci  char **optargs;          // Arguments left over from get_optflags()
1080f66f451Sopenharmony_ci  unsigned long long optflags; // Command line option flags from get_optflags()
1090f66f451Sopenharmony_ci  int optc;                // Count of optargs
1100f66f451Sopenharmony_ci  int envc;                // Count of original environ entries
1110f66f451Sopenharmony_ci  int old_umask;           // Old umask preserved by TOYFLAG_UMASK
1120f66f451Sopenharmony_ci  short toycount;          // Total number of commands in this build
1130f66f451Sopenharmony_ci  short signal;            // generic_signal() records what signal it saw here
1140f66f451Sopenharmony_ci  int signalfd;            // and writes signal to this fd, if set
1150f66f451Sopenharmony_ci  char exitval;            // Value error_exit feeds to exit()
1160f66f451Sopenharmony_ci  char wasroot;            // dropped setuid
1170f66f451Sopenharmony_ci
1180f66f451Sopenharmony_ci  // This is at the end so toy_init() doesn't zero it.
1190f66f451Sopenharmony_ci  sigjmp_buf *rebound;     // siglongjmp here instead of exit when do_rebound
1200f66f451Sopenharmony_ci  struct arg_list *xexit;  // atexit() functions for xexit(), set by sigatexit()
1210f66f451Sopenharmony_ci  void *stacktop;          // nested toy_exec() call count, or 0 if vforked
1220f66f451Sopenharmony_ci} toys;
1230f66f451Sopenharmony_ci
1240f66f451Sopenharmony_ci// Two big temporary buffers: one for use by commands, one for library functions
1250f66f451Sopenharmony_ci
1260f66f451Sopenharmony_ciextern char toybuf[4096], libbuf[4096];
1270f66f451Sopenharmony_ci
1280f66f451Sopenharmony_ciextern char **environ;
1290f66f451Sopenharmony_ci
1300f66f451Sopenharmony_ci#define FLAG(x) (toys.optflags&FLAG_##x)
1310f66f451Sopenharmony_ci
1320f66f451Sopenharmony_ci#define GLOBALS(...)
1330f66f451Sopenharmony_ci#define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
1340f66f451Sopenharmony_ci#define TAGGED_ARRAY(X, ...) {__VA_ARGS__}
1350f66f451Sopenharmony_ci
1360f66f451Sopenharmony_ci#ifndef TOYBOX_VERSION
1370f66f451Sopenharmony_ci#ifndef TOYBOX_VENDOR
1380f66f451Sopenharmony_ci#define TOYBOX_VENDOR ""
1390f66f451Sopenharmony_ci#endif
1400f66f451Sopenharmony_ci#define TOYBOX_VERSION "0.8.10"TOYBOX_VENDOR
1410f66f451Sopenharmony_ci#endif
142