blob: 93907fb6af515599397eb44d9fe2cd69e6ed0222 [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>
5 *
6 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7 */
8
Rob Landley28964802008-01-19 17:08:39 -06009#include "generated/config.h"
Rob Landleyfd1c5ba2007-02-03 14:10:00 -050010
Rob Landley90163772007-01-18 21:54:08 -050011#include "lib/portability.h"
12
landley09ea7ac2006-10-30 01:38:00 -050013#include <ctype.h>
Rob Landleyfd1c5ba2007-02-03 14:10:00 -050014#include <dirent.h>
landley09ea7ac2006-10-30 01:38:00 -050015#include <errno.h>
16#include <fcntl.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 Landleyae2e4b72008-11-15 05:17:23 -060020#include <pty.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040021#include <pwd.h>
Rob Landley6973a1d2006-11-25 16:50:00 -050022#include <setjmp.h>
landley4f344e32006-10-05 16:18:03 -040023#include <stdarg.h>
landley09ea7ac2006-10-30 01:38:00 -050024#include <stdint.h>
landleyc5621502006-09-28 17:18:51 -040025#include <stdio.h>
landley4f344e32006-10-05 16:18:03 -040026#include <stdlib.h>
27#include <string.h>
Rob Landley055cfcb2007-01-14 20:20:06 -050028#include <sys/ioctl.h>
Rob Landleyc92fde02007-04-23 15:45:55 -040029#include <sys/mman.h>
Rob Landleye2580db2007-01-23 13:20:38 -050030#include <sys/mount.h>
landley00f87f12006-10-25 18:38:37 -040031#include <sys/stat.h>
landley09ea7ac2006-10-30 01:38:00 -050032#include <sys/statvfs.h>
33#include <sys/types.h>
34#include <sys/wait.h>
landley4f344e32006-10-05 16:18:03 -040035#include <unistd.h>
Rob Landley07c78d32007-12-28 03:29:33 -060036#include <utime.h>
37
Rob Landley6f458492009-08-06 20:39:28 -050038#undef _XOPEN_SOURCE
Rob Landley07c78d32007-12-28 03:29:33 -060039#define _XOPEN_SOURCE 600
40#include <time.h>
landleyc5621502006-09-28 17:18:51 -040041
landley4f344e32006-10-05 16:18:03 -040042#include "lib/lib.h"
Rob Landleye2580db2007-01-23 13:20:38 -050043#include "toys/e2fs.h"
landleyc5621502006-09-28 17:18:51 -040044
Rob Landley55928b12008-01-19 17:43:27 -060045// 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 Landleyb1aaba12008-01-20 17:25:44 -060050#include "generated/globals.h"
Rob Landley55928b12008-01-19 17:43:27 -060051
Rob Landleyf2311a42006-11-04 17:45:18 -050052// These live in main.c
landleyc5621502006-09-28 17:18:51 -040053
landley4f344e32006-10-05 16:18:03 -040054struct toy_list *toy_find(char *name);
landleycd9dfc32006-10-18 18:38:16 -040055void toy_init(struct toy_list *which, char *argv[]);
56void toy_exec(char *argv[]);
landleyc5621502006-09-28 17:18:51 -040057
Rob Landley53dda1a2009-01-25 16:59:14 -060058// Flags describing applet behavior.
Rob Landleyb1aaba12008-01-20 17:25:44 -060059
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 Landley53dda1a2009-01-25 16:59:14 -060065// This is a shell built-in function, running in the same process context.
Rob Landleyb1aaba12008-01-20 17:25:44 -060066#define TOYFLAG_NOFORK (1<<4)
Rob Landley53dda1a2009-01-25 16:59:14 -060067
68// Start applet with a umask of 0 (saves old umask in this.old_umask)
Rob Landley0f8c4c52008-02-12 19:05:44 -060069#define TOYFLAG_UMASK (1<<5)
Rob Landleyb1aaba12008-01-20 17:25:44 -060070
Rob Landley53dda1a2009-01-25 16:59:14 -060071// Array of available applets
72
Rob Landleyb1aaba12008-01-20 17:25:44 -060073extern struct toy_list {
74 char *name;
75 void (*toy_main)(void);
76 char *options;
77 int flags;
78} toy_list[];
79
Rob Landley53dda1a2009-01-25 16:59:14 -060080// Global context shared by all applets.
landleyc5621502006-09-28 17:18:51 -040081
82extern struct toy_context {
landley4f344e32006-10-05 16:18:03 -040083 struct toy_list *which; // Which entry in toy_list is this one?
84 int exitval; // Value error_exit feeds to exit()
Rob Landleyf2f98fa2007-05-17 02:38:27 -040085 char **argv; // Original command line arguments
Rob Landley8324b892006-11-19 02:49:22 -050086 unsigned optflags; // Command line option flags from get_optflags()
87 char **optargs; // Arguments left over from get_optflags()
Rob Landley26bf9e62008-02-12 17:36:13 -060088 int optc; // Count of optargs
Rob Landley53dda1a2009-01-25 16:59:14 -060089 int exithelp; // Should error_exit print a usage message first?
90 int old_umask; // Old umask preserved by TOYFLAG_UMASK
landleyc5621502006-09-28 17:18:51 -040091} toys;
Rob Landley8324b892006-11-19 02:49:22 -050092
93// One big temporary buffer, for use by applets (not library functions).
94
Rob Landley055cfcb2007-01-14 20:20:06 -050095extern char toybuf[4096];
Rob Landleyb1aaba12008-01-20 17:25:44 -060096
97#define DEFINE_GLOBALS(...)