113bab2f09e9189c2b1e83ae37a1134108f9479clandley/* Toybox infrastructure.
213bab2f09e9189c2b1e83ae37a1134108f9479clandley *
313bab2f09e9189c2b1e83ae37a1134108f9479clandley * Copyright 2006 Rob Landley <rob@landley.net>
413bab2f09e9189c2b1e83ae37a1134108f9479clandley */
513bab2f09e9189c2b1e83ae37a1134108f9479clandley
6c56215062c961402515daeef8330ed75cd94af29landley#include "toys.h"
7c56215062c961402515daeef8330ed75cd94af29landley
890afbad4c1f115d5363fe99eb797d2458cd298a0Rob Landley#ifndef TOYBOX_VERSION
95af26a03df96cdea056440355713182cab4a4242Rob Landley#ifndef TOYBOX_VENDOR
105af26a03df96cdea056440355713182cab4a4242Rob Landley#define TOYBOX_VENDOR ""
115af26a03df96cdea056440355713182cab4a4242Rob Landley#endif
12c0ad4545137107503d64c7ed1d9bc685ec2f8e9cRob Landley#define TOYBOX_VERSION "0.7.6"TOYBOX_VENDOR
1390afbad4c1f115d5363fe99eb797d2458cd298a0Rob Landley#endif
14ad602aa127e44eade76fbb05fd27ee8f5825282aRob Landley
15f2311a42a0751e7c039981857fcf60b40f36b475Rob Landley// Populate toy_list[].
16c56215062c961402515daeef8330ed75cd94af29landley
17a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley#undef NEWTOY
18a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley#undef OLDTOY
19ec0b482e9fd27cb994b30e0f1b8a4d39a85735ccRob Landley#define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags},
20f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley#define OLDTOY(name, oldname, flags) \
21f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley  {#name, oldname##_main, OPTSTR_##oldname, flags},
22a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley
23c56215062c961402515daeef8330ed75cd94af29landleystruct toy_list toy_list[] = {
2455928b1e0a08d84a5cbc50020f0a8c1024f5b6ceRob Landley#include "generated/newtoys.h"
25c56215062c961402515daeef8330ed75cd94af29landley};
26c56215062c961402515daeef8330ed75cd94af29landley
27933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley// global context for this command.
28c56215062c961402515daeef8330ed75cd94af29landley
29c56215062c961402515daeef8330ed75cd94af29landleystruct toy_context toys;
30b1aaba1fc8176ac0b7c202a664d2554aa0967116Rob Landleyunion global_union this;
318fdcfdb4479dff7a993a25a63253f0e749fd99feRob Landleychar toybuf[4096], libbuf[4096];
32c56215062c961402515daeef8330ed75cd94af29landley
334f344e356d2c36c4b1df46917eaef25f82ca79a9landleystruct toy_list *toy_find(char *name)
3413bab2f09e9189c2b1e83ae37a1134108f9479clandley{
357aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  int top, bottom, middle;
3613bab2f09e9189c2b1e83ae37a1134108f9479clandley
37dc1af185e73410c5cad997c600678c212262a13cRob Landley  if (!CFG_TOYBOX) return 0;
38dc1af185e73410c5cad997c600678c212262a13cRob Landley
397aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // If the name starts with "toybox" accept that as a match.  Otherwise
407aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // skip the first entry, which is out of order.
4113bab2f09e9189c2b1e83ae37a1134108f9479clandley
427aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (!strncmp(name,"toybox",6)) return toy_list;
437aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  bottom = 1;
4413bab2f09e9189c2b1e83ae37a1134108f9479clandley
45933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley  // Binary search to find this command.
4613bab2f09e9189c2b1e83ae37a1134108f9479clandley
477aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  top = ARRAY_LEN(toy_list)-1;
487aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  for (;;) {
497aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    int result;
502c226859cfc911c4a1eea009897050a16714aeecRob Landley
517aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    middle = (top+bottom)/2;
527aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (middle<bottom || middle>top) return NULL;
537aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    result = strcmp(name,toy_list[middle].name);
547aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (!result) return toy_list+middle;
5577f9c7700604127f9e9f46d44764ca3db978f706Joyounger    if (result<0) top = --middle;
567aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    else bottom = ++middle;
577aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
5813bab2f09e9189c2b1e83ae37a1134108f9479clandley}
5913bab2f09e9189c2b1e83ae37a1134108f9479clandley
60fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley// Figure out whether or not anything is using the option parsing logic,
61fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley// because the compiler can't figure out whether or not to optimize it away
628f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// on its' own.  NEED_OPTIONS becomes a constant allowing if() to optimize
638f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// stuff out via dead code elimination.
64fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley
65fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#undef NEWTOY
66fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#undef OLDTOY
67fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#define NEWTOY(name, opts, flags) opts ||
68f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
694307a7b07cec4ad8cbab47a29ba941f8cb041812Rob Landleystatic const int NEED_OPTIONS =
7055928b1e0a08d84a5cbc50020f0a8c1024f5b6ceRob Landley#include "generated/newtoys.h"
71fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley0;  // Ends the opts || opts || opts...
72fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley
7329e75d51d447c5c0aae9834f5988b79a945c7accRob Landleystatic void unknown(char *name)
7429e75d51d447c5c0aae9834f5988b79a945c7accRob Landley{
7529e75d51d447c5c0aae9834f5988b79a945c7accRob Landley  toys.exitval = 127;
76b2574799d14e45ad1b43e8dfd1099fd8cf9b5420Rob Landley  toys.which = toy_list;
7729e75d51d447c5c0aae9834f5988b79a945c7accRob Landley  error_exit("Unknown command %s", name);
7829e75d51d447c5c0aae9834f5988b79a945c7accRob Landley}
7929e75d51d447c5c0aae9834f5988b79a945c7accRob Landley
803b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley// Setup toybox global state for this command.
81bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landleystatic void toy_singleinit(struct toy_list *which, char *argv[])
82bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley{
83bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.which = which;
84bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.argv = argv;
85bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
86b5e405ce448fd50988346cc42a9099a5fdbafe8dRob Landley  if (CFG_TOYBOX_I18N) setlocale(LC_CTYPE, "C.UTF-8");
87fc49761a9c992a120a27cca016d6a3ed31b1f3d3Rob Landley
8829e75d51d447c5c0aae9834f5988b79a945c7accRob Landley  // Parse --help and --version for (almost) all commands
8929e75d51d447c5c0aae9834f5988b79a945c7accRob Landley  if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) {
9029e75d51d447c5c0aae9834f5988b79a945c7accRob Landley    if (!strcmp(argv[1], "--help")) {
9129e75d51d447c5c0aae9834f5988b79a945c7accRob Landley      if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
9229e75d51d447c5c0aae9834f5988b79a945c7accRob Landley        if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]);
9329e75d51d447c5c0aae9834f5988b79a945c7accRob Landley      show_help(stdout);
9429e75d51d447c5c0aae9834f5988b79a945c7accRob Landley      xexit();
9529e75d51d447c5c0aae9834f5988b79a945c7accRob Landley    }
9629e75d51d447c5c0aae9834f5988b79a945c7accRob Landley
9729e75d51d447c5c0aae9834f5988b79a945c7accRob Landley    if (!strcmp(argv[1], "--version")) {
9829e75d51d447c5c0aae9834f5988b79a945c7accRob Landley      xputs("toybox "TOYBOX_VERSION);
9929e75d51d447c5c0aae9834f5988b79a945c7accRob Landley      xexit();
10029e75d51d447c5c0aae9834f5988b79a945c7accRob Landley    }
101bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
102bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
103bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  if (NEED_OPTIONS && which->options) get_optflags();
104bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  else {
105bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toys.optargs = argv+1;
1063b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley    for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
107bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
108bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.old_umask = umask(0);
109bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
1101bc522424f6bb21842366ccd11953136436b684bRob Landley  toys.signalfd--;
11190b200cac621ee925476297a2b70ddf3101cbb45Rob Landley  toys.toycount = ARRAY_LEN(toy_list);
112bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley}
113bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
1143b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley// Full init needed by multiplexer or reentrant calls, calls singleinit at end
115cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landleyvoid toy_init(struct toy_list *which, char *argv[])
116cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley{
117ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley  void *oldwhich = toys.which;
118ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley
1197aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // Drop permissions for non-suid commands.
120e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
1217aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (CFG_TOYBOX_SUID) {
122ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley    if (!toys.which) toys.which = toy_list;
123ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley
1247aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    uid_t uid = getuid(), euid = geteuid();
125e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
1267aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (!(which->flags & TOYFLAG_STAYROOT)) {
1275c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley      if (uid != euid) {
12862b53ed9e64e40d2534f1239c4b314d84e79f15fPatrick Ohly        if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
129ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley        euid = uid;
130ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley        toys.wasroot++;
1315c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley      }
132bf1e70f3554c2f591e15df7abca03138861e5c6cRob Landley    } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
133bf1e70f3554c2f591e15df7abca03138861e5c6cRob Landley      error_msg("Not installed suid root");
134e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
135e5354ca12a232b3f97726214254a868771cb70d1Rob Landley    if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
1367aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
137e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
138caf39c26827f355c4e107f55c5c51f67c484bfd7Rob Landley  // Free old toys contents (to be reentrant), but leave rebound if any
1393b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // don't blank old optargs if our new argc lives in the old optargs.
1403b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
141caf39c26827f355c4e107f55c5c51f67c484bfd7Rob Landley  memset(&toys, 0, offsetof(struct toy_context, rebound));
142ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley  if (oldwhich) memset(&this, 0, sizeof(this));
143cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley
1443b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Continue to portion of init needed by standalone commands
145bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toy_singleinit(which, argv);
146cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley}
147cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley
1488f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// Like exec() but runs an internal toybox command instead of another file.
149c6705af4231b8071830721f98021d0e79223da12Rob Landley// Only returns if it can't run command internally, otherwise exit() when done.
1504f344e356d2c36c4b1df46917eaef25f82ca79a9landleyvoid toy_exec(char *argv[])
15113bab2f09e9189c2b1e83ae37a1134108f9479clandley{
1527aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  struct toy_list *which;
153b841cd2f8c1b378c1e8d73f5915662b8e79aac80Rob Landley
1543b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Return if we can't find it (which includes no multiplexer case),
1553b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (!(which = toy_find(*argv))) return;
156c6705af4231b8071830721f98021d0e79223da12Rob Landley
1573b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Return if stack depth getting noticeable (proxy for leaked heap, etc).
158869da8ce3752ce6f5aa63d302eebe60a2b5c8da8Rob Landley
159869da8ce3752ce6f5aa63d302eebe60a2b5c8da8Rob Landley  // Compiler writers have decided subtracting char * is undefined behavior,
160869da8ce3752ce6f5aa63d302eebe60a2b5c8da8Rob Landley  // so convert to integers. (LP64 says sizeof(long)==sizeof(pointer).)
1614e756d7f9fd1b32b545ce8c43120dc733653b52fRob Landley  if (!CFG_TOYBOX_NORECURSE)
1624e756d7f9fd1b32b545ce8c43120dc733653b52fRob Landley    if (toys.stacktop && labs((long)toys.stacktop-(long)&which)>6000) return;
1633b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley
1643b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Return if we need to re-exec to acquire root via suid bit.
165ca311f1a41a579a57076adfeb2cc08b20dbca21aRob Landley  if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
1667c5ed1cedaa881fff52da019da96f9bf6735dc5dRob Landley
167c6705af4231b8071830721f98021d0e79223da12Rob Landley  // Run command
1687aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  toy_init(which, argv);
1695f80533836c60a167f69b71624fad5c494057ee2Rob Landley  if (toys.which) toys.which->toy_main();
170953722e3826c779f261a4c8a3649e36b1333a86fRob Landley  xexit();
1714f344e356d2c36c4b1df46917eaef25f82ca79a9landley}
17213bab2f09e9189c2b1e83ae37a1134108f9479clandley
1738f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// Multiplexer command, first argument is command to run, rest are args to that.
1748f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// If first argument starts with - output list of command install paths.
175efda21ca931766eed6cfc49d1b2122c53827d9fcRob Landleyvoid toybox_main(void)
1764f344e356d2c36c4b1df46917eaef25f82ca79a9landley{
1777aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  static char *toy_paths[]={"usr/","bin/","sbin/",0};
1787aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  int i, len = 0;
1797aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
1803b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // fast path: try to exec immediately.
1813b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // (Leave toys.which null to disable suid return logic.)
1823b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (toys.argv[1]) toy_exec(toys.argv+1);
1833b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley
1843b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // For early error reporting
1856c62448f72ed9fc2b6a59ee09735283c07060cdbRob Landley  toys.which = toy_list;
1863b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley
18729e75d51d447c5c0aae9834f5988b79a945c7accRob Landley  if (toys.argv[1] && toys.argv[1][0] != '-') unknown(toys.argv[1]);
1887aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
189933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley  // Output list of command.
190e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley  for (i=1; i<ARRAY_LEN(toy_list); i++) {
1917aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    int fl = toy_list[i].flags;
1927aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (fl & TOYMASK_LOCATION) {
1937aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley      if (toys.argv[1]) {
1947aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley        int j;
1957aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley        for (j=0; toy_paths[j]; j++)
1967aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley          if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
1977aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley      }
198d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      len += printf("%s",toy_list[i].name);
199d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      if (++len > 65) len = 0;
200d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      xputc(len ? ' ' : '\n');
2017aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    }
2027aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
2037aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  xputc('\n');
2044f344e356d2c36c4b1df46917eaef25f82ca79a9landley}
2054f344e356d2c36c4b1df46917eaef25f82ca79a9landley
2064f344e356d2c36c4b1df46917eaef25f82ca79a9landleyint main(int argc, char *argv[])
2074f344e356d2c36c4b1df46917eaef25f82ca79a9landley{
2083b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (!*argv) return 127;
209e7c09548b9c6188857260064c8ceba03d850c4b7Rob Landley
2103b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Snapshot stack location so we can detect recursion depth later.
2113b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // This is its own block so probe doesn't permanently consume stack.
2123b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  else {
2133b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley    int stack;
2143b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley
2153b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley    toys.stacktop = &stack;
2163b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  }
217cb49c305e3c78179b19d6f174ae73309544292b8Rob Landley  *argv = getbasename(*argv);
2183b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley
21918cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // Up to and including Android M, bionic's dynamic linker added a handler to
22018cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // cause a crash dump on SIGPIPE. That was removed in Android N, but adbd
22118cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // was still setting the SIGPIPE disposition to SIG_IGN, and its children
22218cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // were inheriting that. In Android O, adbd is fixed, but manually asking
22318cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // for the default disposition is harmless, and it'll be a long time before
22418cd02cc3e6529bcd3c8bcdf005e78925a320e35Elliott Hughes  // no one's using anything older than O!
225e95731e93f569ec7266d78f01a43f8a5159d4968Rob Landley  if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
226e95731e93f569ec7266d78f01a43f8a5159d4968Rob Landley
2273b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // If nommu can't fork, special reentry path.
2283b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  // Use !stacktop to signal "vfork happened", both before and after xexec()
2293b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (!CFG_TOYBOX_FORK) {
2303b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley    if (0x80 & **argv) {
2313b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley      **argv &= 0x7f;
2323b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley      toys.stacktop = 0;
2333b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley    }
2343b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  }
235bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
2363b51a07e478d64a84e40b3a7c026b2f8566b194bRob Landley  if (CFG_TOYBOX) {
237bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // Call the multiplexer, adjusting this argv[] to be its' argv[1].
238bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // (It will adjust it back before calling toy_exec().)
239bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toys.argv = argv-1;
240bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toybox_main();
241bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  } else {
242bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // a single toybox command built standalone with no multiplexer
243bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toy_singleinit(toy_list, argv);
244bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toy_list->toy_main();
245bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
2467aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
247aad492fd87d689c443e87561c23abc2e12b785a9Rob Landley  xexit();
24813bab2f09e9189c2b1e83ae37a1134108f9479clandley}
249