113bab2f09e9189c2b1e83ae37a1134108f9479clandley/* Toybox infrastructure.
213bab2f09e9189c2b1e83ae37a1134108f9479clandley *
313bab2f09e9189c2b1e83ae37a1134108f9479clandley * Copyright 2006 Rob Landley <rob@landley.net>
413bab2f09e9189c2b1e83ae37a1134108f9479clandley */
513bab2f09e9189c2b1e83ae37a1134108f9479clandley
6c56215062c961402515daeef8330ed75cd94af29landley#include "toys.h"
7c56215062c961402515daeef8330ed75cd94af29landley
890afbad4c1f115d5363fe99eb797d2458cd298a0Rob Landley#ifndef TOYBOX_VERSION
9ad602aa127e44eade76fbb05fd27ee8f5825282aRob Landley#define TOYBOX_VERSION "0.5.2"
1090afbad4c1f115d5363fe99eb797d2458cd298a0Rob Landley#endif
11ad602aa127e44eade76fbb05fd27ee8f5825282aRob Landley
12f2311a42a0751e7c039981857fcf60b40f36b475Rob Landley// Populate toy_list[].
13c56215062c961402515daeef8330ed75cd94af29landley
14a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley#undef NEWTOY
15a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley#undef OLDTOY
16a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley#define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
17f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley#define OLDTOY(name, oldname, flags) \
18f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley  {#name, oldname##_main, OPTSTR_##oldname, flags},
19a6d696b74adb08d83b21f8299b5443f31a5f43ecRob Landley
20c56215062c961402515daeef8330ed75cd94af29landleystruct toy_list toy_list[] = {
2155928b1e0a08d84a5cbc50020f0a8c1024f5b6ceRob Landley#include "generated/newtoys.h"
22c56215062c961402515daeef8330ed75cd94af29landley};
23c56215062c961402515daeef8330ed75cd94af29landley
24933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley// global context for this command.
25c56215062c961402515daeef8330ed75cd94af29landley
26c56215062c961402515daeef8330ed75cd94af29landleystruct toy_context toys;
27b1aaba1fc8176ac0b7c202a664d2554aa0967116Rob Landleyunion global_union this;
288fdcfdb4479dff7a993a25a63253f0e749fd99feRob Landleychar toybuf[4096], libbuf[4096];
29c56215062c961402515daeef8330ed75cd94af29landley
304f344e356d2c36c4b1df46917eaef25f82ca79a9landleystruct toy_list *toy_find(char *name)
3113bab2f09e9189c2b1e83ae37a1134108f9479clandley{
327aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  int top, bottom, middle;
3313bab2f09e9189c2b1e83ae37a1134108f9479clandley
34dc1af185e73410c5cad997c600678c212262a13cRob Landley  if (!CFG_TOYBOX) return 0;
35dc1af185e73410c5cad997c600678c212262a13cRob Landley
367aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // If the name starts with "toybox" accept that as a match.  Otherwise
377aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // skip the first entry, which is out of order.
3813bab2f09e9189c2b1e83ae37a1134108f9479clandley
397aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (!strncmp(name,"toybox",6)) return toy_list;
407aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  bottom = 1;
4113bab2f09e9189c2b1e83ae37a1134108f9479clandley
42933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley  // Binary search to find this command.
4313bab2f09e9189c2b1e83ae37a1134108f9479clandley
447aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  top = ARRAY_LEN(toy_list)-1;
457aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  for (;;) {
467aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    int result;
472c226859cfc911c4a1eea009897050a16714aeecRob Landley
487aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    middle = (top+bottom)/2;
497aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (middle<bottom || middle>top) return NULL;
507aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    result = strcmp(name,toy_list[middle].name);
517aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (!result) return toy_list+middle;
527aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (result<0) top=--middle;
537aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    else bottom = ++middle;
547aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
5513bab2f09e9189c2b1e83ae37a1134108f9479clandley}
5613bab2f09e9189c2b1e83ae37a1134108f9479clandley
57fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley// Figure out whether or not anything is using the option parsing logic,
58fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley// because the compiler can't figure out whether or not to optimize it away
598f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// on its' own.  NEED_OPTIONS becomes a constant allowing if() to optimize
608f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// stuff out via dead code elimination.
61fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley
62fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#undef NEWTOY
63fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#undef OLDTOY
64fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley#define NEWTOY(name, opts, flags) opts ||
65f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
664307a7b07cec4ad8cbab47a29ba941f8cb041812Rob Landleystatic const int NEED_OPTIONS =
6755928b1e0a08d84a5cbc50020f0a8c1024f5b6ceRob Landley#include "generated/newtoys.h"
68fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley0;  // Ends the opts || opts || opts...
69fc2224b26c73198d00eba714caf47bd2e3a5e63bRob Landley
70bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley// Subset of init needed by singlemain
71bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landleystatic void toy_singleinit(struct toy_list *which, char *argv[])
72bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley{
73bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.which = which;
74bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.argv = argv;
75bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
769b14cb6b12e6f1b07a1ad401e5cb6e091df4ac2fRob Landley  if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE));
77fc49761a9c992a120a27cca016d6a3ed31b1f3d3Rob Landley
78bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) {
79dc1af185e73410c5cad997c600678c212262a13cRob Landley    if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
805f80533836c60a167f69b71624fad5c494057ee2Rob Landley      if (!(toys.which = toy_find(toys.argv[2]))) return;
81bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    show_help();
82bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    xexit();
83bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
84bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
85bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  if (NEED_OPTIONS && which->options) get_optflags();
86bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  else {
87bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toys.optargs = argv+1;
88bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    for (toys.optc=0; toys.optargs[toys.optc]; toys.optc++);
89bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
90bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toys.old_umask = umask(0);
91bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
921bc522424f6bb21842366ccd11953136436b684bRob Landley  toys.signalfd--;
9390b200cac621ee925476297a2b70ddf3101cbb45Rob Landley  toys.toycount = ARRAY_LEN(toy_list);
94bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley}
95bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
968f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// Setup toybox global state for this command.
978f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley
98cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landleyvoid toy_init(struct toy_list *which, char *argv[])
99cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley{
1007aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  // Drop permissions for non-suid commands.
101e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
1027aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (CFG_TOYBOX_SUID) {
1037aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    uid_t uid = getuid(), euid = geteuid();
104e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
1057aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (!(which->flags & TOYFLAG_STAYROOT)) {
1065c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley      if (uid != euid) {
1075c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley        if (!setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
1085c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley        else euid = uid;
1095c87c1426b5c99dd1f2a3ac7493996c342efef4eRob Landley      }
110bf1e70f3554c2f591e15df7abca03138861e5c6cRob Landley    } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
111bf1e70f3554c2f591e15df7abca03138861e5c6cRob Landley      error_msg("Not installed suid root");
112e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
1133bc5d3d880627b1a9c38ed4b92ff894c7752f124Rob Landley    if ((which->flags & TOYFLAG_NEEDROOT) && euid) {
1143bc5d3d880627b1a9c38ed4b92ff894c7752f124Rob Landley      toys.exithelp++;
1153bc5d3d880627b1a9c38ed4b92ff894c7752f124Rob Landley      error_exit("Not root");
1163bc5d3d880627b1a9c38ed4b92ff894c7752f124Rob Landley    }
1177aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
118e0377fb294821a68112d4da09f836ac42e3d5956Rob Landley
119caf39c26827f355c4e107f55c5c51f67c484bfd7Rob Landley  // Free old toys contents (to be reentrant), but leave rebound if any
120db037ef6e0d7e84e5fd9ba831b55550a9b487257Rob Landley
1217aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (toys.optargs != toys.argv+1) free(toys.optargs);
122caf39c26827f355c4e107f55c5c51f67c484bfd7Rob Landley  memset(&toys, 0, offsetof(struct toy_context, rebound));
1231e2399b91cfd9895939876c4fb47af11c13ddf89Rob Landley  if (toys.recursion > 1) memset(&this, 0, sizeof(this));
124cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley
125bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  // Subset of init needed by singlemain.
126bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  toy_singleinit(which, argv);
127cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley}
128cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4landley
1298f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// Like exec() but runs an internal toybox command instead of another file.
130c6705af4231b8071830721f98021d0e79223da12Rob Landley// Only returns if it can't run command internally, otherwise exit() when done.
1314f344e356d2c36c4b1df46917eaef25f82ca79a9landleyvoid toy_exec(char *argv[])
13213bab2f09e9189c2b1e83ae37a1134108f9479clandley{
1337aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  struct toy_list *which;
134b841cd2f8c1b378c1e8d73f5915662b8e79aac80Rob Landley
135c6705af4231b8071830721f98021d0e79223da12Rob Landley  // Return if we can't find it, or need to re-exec to acquire root,
136c6705af4231b8071830721f98021d0e79223da12Rob Landley  // or if stack depth is getting silly.
13709ee264817b4331a02279a9a5c9af6f2e846c37fRob Landley  if (!(which = toy_find(argv[0]))) return;
138c6705af4231b8071830721f98021d0e79223da12Rob Landley  if (toys.recursion && (which->flags & TOYFLAG_ROOTONLY) && getuid()) return;
139c6705af4231b8071830721f98021d0e79223da12Rob Landley  if (toys.recursion++ > 5) return;
140c6705af4231b8071830721f98021d0e79223da12Rob Landley
1417c5ed1cedaa881fff52da019da96f9bf6735dc5dRob Landley  // don't blank old optargs if our new argc lives in the old optargs.
1427c5ed1cedaa881fff52da019da96f9bf6735dc5dRob Landley  if (argv>=toys.optargs && argv<=toys.optargs+toys.optc) toys.optargs = 0;
1437c5ed1cedaa881fff52da019da96f9bf6735dc5dRob Landley
144c6705af4231b8071830721f98021d0e79223da12Rob Landley  // Run command
1457aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  toy_init(which, argv);
1465f80533836c60a167f69b71624fad5c494057ee2Rob Landley  if (toys.which) toys.which->toy_main();
147953722e3826c779f261a4c8a3649e36b1333a86fRob Landley  xexit();
1484f344e356d2c36c4b1df46917eaef25f82ca79a9landley}
14913bab2f09e9189c2b1e83ae37a1134108f9479clandley
1508f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// Multiplexer command, first argument is command to run, rest are args to that.
1518f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley// If first argument starts with - output list of command install paths.
1528f8c504e585b6850abf628cabdb9ef231bef1a35Rob Landley
153efda21ca931766eed6cfc49d1b2122c53827d9fcRob Landleyvoid toybox_main(void)
1544f344e356d2c36c4b1df46917eaef25f82ca79a9landley{
1557aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  static char *toy_paths[]={"usr/","bin/","sbin/",0};
1567aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  int i, len = 0;
1577aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
1586c62448f72ed9fc2b6a59ee09735283c07060cdbRob Landley  toys.which = toy_list;
1597aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  if (toys.argv[1]) {
160dc11ed8d3bc9a3d5b3610db813b63f214d4932ddRob Landley    toys.optc = toys.recursion = 0;
1615f80533836c60a167f69b71624fad5c494057ee2Rob Landley    toy_exec(toys.argv+1);
162e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley    if (!strcmp("--version", toys.argv[1])) {
163e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley      xputs(TOYBOX_VERSION);
164e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley      xexit();
165e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley    }
166ad602aa127e44eade76fbb05fd27ee8f5825282aRob Landley    if (toys.argv[1][0] != '-') error_exit("Unknown command %s", toys.argv[1]);
1677aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
1687aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
169933919cd8094e870b3e7a554605fd49b20ddb1e0Rob Landley  // Output list of command.
170e7acb4749dc655fee22d08f2723ea783f0d75fb6Rob Landley  for (i=1; i<ARRAY_LEN(toy_list); i++) {
1717aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    int fl = toy_list[i].flags;
1727aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    if (fl & TOYMASK_LOCATION) {
1737aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley      if (toys.argv[1]) {
1747aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley        int j;
1757aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley        for (j=0; toy_paths[j]; j++)
1767aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley          if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
1777aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley      }
178d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      len += printf("%s",toy_list[i].name);
179d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      if (++len > 65) len = 0;
180d3f335d2cf64eb6c556ee5ba6e4a9315766e3c6eRob Landley      xputc(len ? ' ' : '\n');
1817aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley    }
1827aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  }
1837aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley  xputc('\n');
1844f344e356d2c36c4b1df46917eaef25f82ca79a9landley}
1854f344e356d2c36c4b1df46917eaef25f82ca79a9landley
1864f344e356d2c36c4b1df46917eaef25f82ca79a9landleyint main(int argc, char *argv[])
1874f344e356d2c36c4b1df46917eaef25f82ca79a9landley{
188483cfdabaf6ab282987a0a21d6177c3daa95dc12Rob Landley  // We check our own stdout errors, disable sigpipe killer
189483cfdabaf6ab282987a0a21d6177c3daa95dc12Rob Landley  signal(SIGPIPE, SIG_IGN);
190e7c09548b9c6188857260064c8ceba03d850c4b7Rob Landley
191d04dc1feb92a279e27e4487c502944f454d43837Rob Landley  if (CFG_TOYBOX) {
192bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // Trim path off of command name
193bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    *argv = basename(*argv);
194bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley
195bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // Call the multiplexer, adjusting this argv[] to be its' argv[1].
196bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // (It will adjust it back before calling toy_exec().)
197bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toys.argv = argv-1;
198bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toybox_main();
199bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  } else {
200bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    // a single toybox command built standalone with no multiplexer
201bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toy_singleinit(toy_list, argv);
202bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley    toy_list->toy_main();
203bb504f382dc6c596e6b2b9ce04931e7fbdd15bbeRob Landley  }
2047aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley
205aad492fd87d689c443e87561c23abc2e12b785a9Rob Landley  xexit();
20613bab2f09e9189c2b1e83ae37a1134108f9479clandley}
207