blob: a3339868b851fe2aa4542b1a979dab61d17a0521 [file] [log] [blame]
landleyc5621502006-09-28 17:18:51 -04001/* vi: set ts=4 :*/
2/* Toybox infrastructure.
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
landleyc5621502006-09-28 17:18:51 -04005 */
6
Rob Landley28964802008-01-19 17:08:39 -06007#include "generated/config.h"
Rob Landleyfd1c5ba2007-02-03 14:10:00 -05008
Rob Landley90163772007-01-18 21:54:08 -05009#include "lib/portability.h"
10
landley09ea7ac2006-10-30 01:38:00 -050011#include <ctype.h>
Rob Landleyfd1c5ba2007-02-03 14:10:00 -050012#include <dirent.h>
landley09ea7ac2006-10-30 01:38:00 -050013#include <errno.h>
14#include <fcntl.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040015#include <grp.h>
landley09ea7ac2006-10-30 01:38:00 -050016#include <inttypes.h>
landley4f344e32006-10-05 16:18:03 -040017#include <limits.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060018#include <libgen.h>
19#include <math.h>
Rob Landleyae2e4b72008-11-15 05:17:23 -060020#include <pty.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040021#include <pwd.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060022#include <sched.h>
Rob Landley6973a1d2006-11-25 16:50:00 -050023#include <setjmp.h>
Elie De Brauwer0b11a162012-04-24 23:09:27 +020024#include <shadow.h>
landley4f344e32006-10-05 16:18:03 -040025#include <stdarg.h>
landley09ea7ac2006-10-30 01:38:00 -050026#include <stdint.h>
landleyc5621502006-09-28 17:18:51 -040027#include <stdio.h>
landley4f344e32006-10-05 16:18:03 -040028#include <stdlib.h>
29#include <string.h>
Rob Landleye3b171e2012-04-28 01:22:50 -050030#include <strings.h>
Rob Landley055cfcb2007-01-14 20:20:06 -050031#include <sys/ioctl.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040032#include <sys/mman.h>
Rob Landleye2580db2007-01-23 13:20:38 -050033#include <sys/mount.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 Landley9494ffc2012-02-17 12:05:26 -060037#include <sys/sysinfo.h>
Elie De Brauwer2c162812012-02-18 15:33:27 +010038#include <sys/swap.h>
Rob Landley628eb9b2012-06-16 14:19:56 -050039#include <sys/times.h>
landley09ea7ac2006-10-30 01:38:00 -050040#include <sys/types.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060041#include <sys/utsname.h>
landley09ea7ac2006-10-30 01:38:00 -050042#include <sys/wait.h>
Elie De Brauwer0b11a162012-04-24 23:09:27 +020043#include <syslog.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>
Rob Landleyf05f6602012-03-07 19:04:50 -060047#include <utmpx.h>
Rob Landley07c78d32007-12-28 03:29:33 -060048
landley4f344e32006-10-05 16:18:03 -040049#include "lib/lib.h"
Rob Landleye2580db2007-01-23 13:20:38 -050050#include "toys/e2fs.h"
landleyc5621502006-09-28 17:18:51 -040051
Rob Landley55928b12008-01-19 17:43:27 -060052// Get list of function prototypes for all enabled command_main() functions.
53
54#define NEWTOY(name, opts, flags) void name##_main(void);
55#define OLDTOY(name, oldname, opts, flags)
56#include "generated/newtoys.h"
Rob Landleyb1aaba12008-01-20 17:25:44 -060057#include "generated/globals.h"
Rob Landley55928b12008-01-19 17:43:27 -060058
Rob Landleyf2311a42006-11-04 17:45:18 -050059// These live in main.c
landleyc5621502006-09-28 17:18:51 -040060
landley4f344e32006-10-05 16:18:03 -040061struct toy_list *toy_find(char *name);
landleycd9dfc32006-10-18 18:38:16 -040062void toy_init(struct toy_list *which, char *argv[]);
63void toy_exec(char *argv[]);
landleyc5621502006-09-28 17:18:51 -040064
Rob Landley43e9d332012-04-14 21:43:24 -050065// Flags describing command behavior.
Rob Landleyb1aaba12008-01-20 17:25:44 -060066
67#define TOYFLAG_USR (1<<0)
68#define TOYFLAG_BIN (1<<1)
69#define TOYFLAG_SBIN (1<<2)
70#define TOYMASK_LOCATION ((1<<4)-1)
71
Rob Landley53dda1a2009-01-25 16:59:14 -060072// This is a shell built-in function, running in the same process context.
Rob Landleyb1aaba12008-01-20 17:25:44 -060073#define TOYFLAG_NOFORK (1<<4)
Rob Landley53dda1a2009-01-25 16:59:14 -060074
Rob Landley43e9d332012-04-14 21:43:24 -050075// Start command with a umask of 0 (saves old umask in this.old_umask)
Rob Landley0f8c4c52008-02-12 19:05:44 -060076#define TOYFLAG_UMASK (1<<5)
Rob Landleyb1aaba12008-01-20 17:25:44 -060077
Rob Landley43e9d332012-04-14 21:43:24 -050078// This command runs as root.
Rob Landleye0377fb2010-01-05 12:17:05 -060079#define TOYFLAG_STAYROOT (1<<6)
80#define TOYFLAG_NEEDROOT (1<<7)
81#define TOYFLAG_ROOTONLY (TOYFLAG_STAYROOT|TOYFLAG_NEEDROOT)
82
Rob Landley53dda1a2009-01-25 16:59:14 -060083// Array of available applets
84
Rob Landleyb1aaba12008-01-20 17:25:44 -060085extern struct toy_list {
Rob Landley43e9d332012-04-14 21:43:24 -050086 char *name;
87 void (*toy_main)(void);
88 char *options;
89 int flags;
Rob Landleyb1aaba12008-01-20 17:25:44 -060090} toy_list[];
91
Rob Landley43e9d332012-04-14 21:43:24 -050092// Global context shared by all commands.
landleyc5621502006-09-28 17:18:51 -040093
94extern struct toy_context {
landley4f344e32006-10-05 16:18:03 -040095 struct toy_list *which; // Which entry in toy_list is this one?
96 int exitval; // Value error_exit feeds to exit()
Rob Landleyf2f98fa2007-05-17 02:38:27 -040097 char **argv; // Original command line arguments
Rob Landley8324b892006-11-19 02:49:22 -050098 unsigned optflags; // Command line option flags from get_optflags()
99 char **optargs; // Arguments left over from get_optflags()
Rob Landley26bf9e62008-02-12 17:36:13 -0600100 int optc; // Count of optargs
Rob Landley53dda1a2009-01-25 16:59:14 -0600101 int exithelp; // Should error_exit print a usage message first?
102 int old_umask; // Old umask preserved by TOYFLAG_UMASK
landleyc5621502006-09-28 17:18:51 -0400103} toys;
Rob Landley8324b892006-11-19 02:49:22 -0500104
Rob Landley43e9d332012-04-14 21:43:24 -0500105// One big temporary buffer, for use by commands (not library functions).
Rob Landley8324b892006-11-19 02:49:22 -0500106
Rob Landley055cfcb2007-01-14 20:20:06 -0500107extern char toybuf[4096];
Rob Landleyb1aaba12008-01-20 17:25:44 -0600108
109#define DEFINE_GLOBALS(...)