Home | History | Annotate | Download | only in toybox
      1 /* Toybox infrastructure.
      2  *
      3  * Copyright 2006 Rob Landley <rob (at) landley.net>
      4  */
      5 
      6 #include "toys.h"
      7 
      8 #ifndef TOYBOX_VERSION
      9 #ifndef TOYBOX_VENDOR
     10 #define TOYBOX_VENDOR ""
     11 #endif
     12 #define TOYBOX_VERSION "0.7.6"TOYBOX_VENDOR
     13 #endif
     14 
     15 // Populate toy_list[].
     16 
     17 #undef NEWTOY
     18 #undef OLDTOY
     19 #define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags},
     20 #define OLDTOY(name, oldname, flags) \
     21   {#name, oldname##_main, OPTSTR_##oldname, flags},
     22 
     23 struct toy_list toy_list[] = {
     24 #include "generated/newtoys.h"
     25 };
     26 
     27 // global context for this command.
     28 
     29 struct toy_context toys;
     30 union global_union this;
     31 char toybuf[4096], libbuf[4096];
     32 
     33 struct toy_list *toy_find(char *name)
     34 {
     35   int top, bottom, middle;
     36 
     37   if (!CFG_TOYBOX) return 0;
     38 
     39   // If the name starts with "toybox" accept that as a match.  Otherwise
     40   // skip the first entry, which is out of order.
     41 
     42   if (!strncmp(name,"toybox",6)) return toy_list;
     43   bottom = 1;
     44 
     45   // Binary search to find this command.
     46 
     47   top = ARRAY_LEN(toy_list)-1;
     48   for (;;) {
     49     int result;
     50 
     51     middle = (top+bottom)/2;
     52     if (middle<bottom || middle>top) return NULL;
     53     result = strcmp(name,toy_list[middle].name);
     54     if (!result) return toy_list+middle;
     55     if (result<0) top = --middle;
     56     else bottom = ++middle;
     57   }
     58 }
     59 
     60 // Figure out whether or not anything is using the option parsing logic,
     61 // because the compiler can't figure out whether or not to optimize it away
     62 // on its' own.  NEED_OPTIONS becomes a constant allowing if() to optimize
     63 // stuff out via dead code elimination.
     64 
     65 #undef NEWTOY
     66 #undef OLDTOY
     67 #define NEWTOY(name, opts, flags) opts ||
     68 #define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
     69 static const int NEED_OPTIONS =
     70 #include "generated/newtoys.h"
     71 0;  // Ends the opts || opts || opts...
     72 
     73 static void unknown(char *name)
     74 {
     75   toys.exitval = 127;
     76   toys.which = toy_list;
     77   error_exit("Unknown command %s", name);
     78 }
     79 
     80 // Setup toybox global state for this command.
     81 static void toy_singleinit(struct toy_list *which, char *argv[])
     82 {
     83   toys.which = which;
     84   toys.argv = argv;
     85 
     86   if (CFG_TOYBOX_I18N) setlocale(LC_CTYPE, "C.UTF-8");
     87 
     88   // Parse --help and --version for (almost) all commands
     89   if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) {
     90     if (!strcmp(argv[1], "--help")) {
     91       if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
     92         if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]);
     93       show_help(stdout);
     94       xexit();
     95     }
     96 
     97     if (!strcmp(argv[1], "--version")) {
     98       xputs("toybox "TOYBOX_VERSION);
     99       xexit();
    100     }
    101   }
    102 
    103   if (NEED_OPTIONS && which->options) get_optflags();
    104   else {
    105     toys.optargs = argv+1;
    106     for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
    107   }
    108   toys.old_umask = umask(0);
    109   if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
    110   toys.signalfd--;
    111   toys.toycount = ARRAY_LEN(toy_list);
    112 }
    113 
    114 // Full init needed by multiplexer or reentrant calls, calls singleinit at end
    115 void toy_init(struct toy_list *which, char *argv[])
    116 {
    117   void *oldwhich = toys.which;
    118 
    119   // Drop permissions for non-suid commands.
    120 
    121   if (CFG_TOYBOX_SUID) {
    122     if (!toys.which) toys.which = toy_list;
    123 
    124     uid_t uid = getuid(), euid = geteuid();
    125 
    126     if (!(which->flags & TOYFLAG_STAYROOT)) {
    127       if (uid != euid) {
    128         if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
    129         euid = uid;
    130         toys.wasroot++;
    131       }
    132     } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
    133       error_msg("Not installed suid root");
    134 
    135     if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
    136   }
    137 
    138   // Free old toys contents (to be reentrant), but leave rebound if any
    139   // don't blank old optargs if our new argc lives in the old optargs.
    140   if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
    141   memset(&toys, 0, offsetof(struct toy_context, rebound));
    142   if (oldwhich) memset(&this, 0, sizeof(this));
    143 
    144   // Continue to portion of init needed by standalone commands
    145   toy_singleinit(which, argv);
    146 }
    147 
    148 // Like exec() but runs an internal toybox command instead of another file.
    149 // Only returns if it can't run command internally, otherwise exit() when done.
    150 void toy_exec(char *argv[])
    151 {
    152   struct toy_list *which;
    153 
    154   // Return if we can't find it (which includes no multiplexer case),
    155   if (!(which = toy_find(*argv))) return;
    156 
    157   // Return if stack depth getting noticeable (proxy for leaked heap, etc).
    158 
    159   // Compiler writers have decided subtracting char * is undefined behavior,
    160   // so convert to integers. (LP64 says sizeof(long)==sizeof(pointer).)
    161   if (!CFG_TOYBOX_NORECURSE)
    162     if (toys.stacktop && labs((long)toys.stacktop-(long)&which)>6000) return;
    163 
    164   // Return if we need to re-exec to acquire root via suid bit.
    165   if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
    166 
    167   // Run command
    168   toy_init(which, argv);
    169   if (toys.which) toys.which->toy_main();
    170   xexit();
    171 }
    172 
    173 // Multiplexer command, first argument is command to run, rest are args to that.
    174 // If first argument starts with - output list of command install paths.
    175 void toybox_main(void)
    176 {
    177   static char *toy_paths[]={"usr/","bin/","sbin/",0};
    178   int i, len = 0;
    179 
    180   // fast path: try to exec immediately.
    181   // (Leave toys.which null to disable suid return logic.)
    182   if (toys.argv[1]) toy_exec(toys.argv+1);
    183 
    184   // For early error reporting
    185   toys.which = toy_list;
    186 
    187   if (toys.argv[1] && toys.argv[1][0] != '-') unknown(toys.argv[1]);
    188 
    189   // Output list of command.
    190   for (i=1; i<ARRAY_LEN(toy_list); i++) {
    191     int fl = toy_list[i].flags;
    192     if (fl & TOYMASK_LOCATION) {
    193       if (toys.argv[1]) {
    194         int j;
    195         for (j=0; toy_paths[j]; j++)
    196           if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
    197       }
    198       len += printf("%s",toy_list[i].name);
    199       if (++len > 65) len = 0;
    200       xputc(len ? ' ' : '\n');
    201     }
    202   }
    203   xputc('\n');
    204 }
    205 
    206 int main(int argc, char *argv[])
    207 {
    208   if (!*argv) return 127;
    209 
    210   // Snapshot stack location so we can detect recursion depth later.
    211   // This is its own block so probe doesn't permanently consume stack.
    212   else {
    213     int stack;
    214 
    215     toys.stacktop = &stack;
    216   }
    217   *argv = getbasename(*argv);
    218 
    219   // Up to and including Android M, bionic's dynamic linker added a handler to
    220   // cause a crash dump on SIGPIPE. That was removed in Android N, but adbd
    221   // was still setting the SIGPIPE disposition to SIG_IGN, and its children
    222   // were inheriting that. In Android O, adbd is fixed, but manually asking
    223   // for the default disposition is harmless, and it'll be a long time before
    224   // no one's using anything older than O!
    225   if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
    226 
    227   // If nommu can't fork, special reentry path.
    228   // Use !stacktop to signal "vfork happened", both before and after xexec()
    229   if (!CFG_TOYBOX_FORK) {
    230     if (0x80 & **argv) {
    231       **argv &= 0x7f;
    232       toys.stacktop = 0;
    233     }
    234   }
    235 
    236   if (CFG_TOYBOX) {
    237     // Call the multiplexer, adjusting this argv[] to be its' argv[1].
    238     // (It will adjust it back before calling toy_exec().)
    239     toys.argv = argv-1;
    240     toybox_main();
    241   } else {
    242     // a single toybox command built standalone with no multiplexer
    243     toy_singleinit(toy_list, argv);
    244     toy_list->toy_main();
    245   }
    246 
    247   xexit();
    248 }
    249