blob: 5334827ca7617b297f43a90639f12985c28aff90 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00002/*
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00003 * BusyBox' main applet dispatcher.
4 *
Rob Landleyeb84a422006-09-20 21:41:13 +00005 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00006 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007#include "busybox.h"
Eric Andersene5dfced2001-04-09 22:48:12 +00008
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00009const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
Denis Vlasenko9067f132007-03-24 12:11:17 +000010smallint re_execed;
Erik Andersen05df2392000-01-13 04:43:48 +000011
Eric Andersenbdfd0d72001-10-24 05:00:29 +000012#ifdef CONFIG_FEATURE_INSTALLER
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013/*
John Beppu8f425db2000-06-27 04:50:02 +000014 * directory table
Eric Andersen3570a342000-09-25 21:45:58 +000015 * this should be consistent w/ the enum, busybox.h::Location,
John Beppueb028332000-06-28 00:55:31 +000016 * or else...
John Beppu8f425db2000-06-27 04:50:02 +000017 */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000018static const char usr_bin [] = "/usr/bin";
19static const char usr_sbin[] = "/usr/sbin";
Eric Andersenfcffa2c2002-04-06 05:17:57 +000020
21static const char* const install_dir[] = {
22 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
23 &usr_bin [4], /* "/bin" */
24 &usr_sbin[4], /* "/sbin" */
25 usr_bin,
26 usr_sbin
John Beppu8f425db2000-06-27 04:50:02 +000027};
28
29/* abstract link() */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000030typedef int (*link_func)(const char *, const char *);
John Beppu8f425db2000-06-27 04:50:02 +000031
32/* create (sym)links for each applet */
Eric Andersenc5949f62000-09-25 20:35:54 +000033static void install_links(const char *busybox, int use_symbolic_links)
John Beppu8f425db2000-06-27 04:50:02 +000034{
Denis Vlasenko0ee39992006-12-24 15:23:28 +000035 link_func lf = link;
Eric Andersene5dfced2001-04-09 22:48:12 +000036 char *fpc;
John Beppueb028332000-06-28 00:55:31 +000037 int i;
Eric Andersenc5949f62000-09-25 20:35:54 +000038 int rc;
John Beppu8f425db2000-06-27 04:50:02 +000039
Eric Andersenc7bda1c2004-03-15 08:29:22 +000040 if (use_symbolic_links)
Denis Vlasenko0ee39992006-12-24 15:23:28 +000041 lf = symlink;
John Beppu8f425db2000-06-27 04:50:02 +000042
John Beppueb028332000-06-28 00:55:31 +000043 for (i = 0; applets[i].name != NULL; i++) {
Eric Andersene5dfced2001-04-09 22:48:12 +000044 fpc = concat_path_file(
Denis Vlasenko0ee39992006-12-24 15:23:28 +000045 install_dir[applets[i].location],
46 applets[i].name);
47 rc = lf(busybox, fpc);
48 if (rc != 0 && errno != EEXIST) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_msg("%s", fpc);
John Beppu8f425db2000-06-27 04:50:02 +000050 }
Eric Andersene5dfced2001-04-09 22:48:12 +000051 free(fpc);
John Beppueb028332000-06-28 00:55:31 +000052 }
John Beppu8f425db2000-06-27 04:50:02 +000053}
54
Rob Landley8a7a6782005-09-05 04:13:33 +000055#else
56#define install_links(x,y)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000057#endif /* CONFIG_FEATURE_INSTALLER */
John Beppu8f425db2000-06-27 04:50:02 +000058
Eric Andersencc8ed391999-10-05 16:24:54 +000059int main(int argc, char **argv)
60{
Matt Kraaif2cc2762001-02-01 19:21:20 +000061 const char *s;
Eric Andersenf5d5e772001-01-24 23:34:48 +000062
Denis Vlasenko9067f132007-03-24 12:11:17 +000063 /* NOMMU re-exec trick sets high-order bit in first byte of name */
64 if (argv[0][0] & 0x80) {
65 re_execed = 1;
66 argv[0][0] &= 0x7f;
67 }
68
Denis Vlasenko0ee39992006-12-24 15:23:28 +000069 applet_name = argv[0];
70 if (*applet_name == '-')
71 applet_name++;
72 while ((s = strchr(applet_name, '/')))
73 applet_name = s + 1;
Matt Kraai449377a2001-08-27 15:02:32 +000074
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +000075 /* Set locale for everybody except 'init' */
76 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
Eric Andersene5dfced2001-04-09 22:48:12 +000077 setlocale(LC_ALL, "");
Eric Andersene5dfced2001-04-09 22:48:12 +000078
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000079 run_applet_by_name(applet_name, argc, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_error_msg_and_die("applet not found");
Eric Andersenf5d5e772001-01-24 23:34:48 +000081}
82
Denis Vlasenko06af2162007-02-03 17:28:39 +000083int busybox_main(int argc, char **argv);
Eric Andersenf5d5e772001-01-24 23:34:48 +000084int busybox_main(int argc, char **argv)
85{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086 /*
87 * This style of argument parsing doesn't scale well
John Beppu27b59242000-06-27 04:56:45 +000088 * in the event that busybox starts wanting more --options.
89 * If someone has a cleaner approach, by all means implement it.
90 */
Rob Landleyb766c392005-09-04 11:10:37 +000091 if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
John Beppu8f425db2000-06-27 04:50:02 +000092 int use_symbolic_links = 0;
John Beppu7cdc76d2000-06-28 00:41:26 +000093 char *busybox;
John Beppu8f425db2000-06-27 04:50:02 +000094
John Beppu27b59242000-06-27 04:56:45 +000095 /* to use symlinks, or not to use symlinks... */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000096 if (argc > 2)
97 if (strcmp(argv[2], "-s") == 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000098 use_symbolic_links = 1;
John Beppu7cdc76d2000-06-28 00:41:26 +000099
100 /* link */
Bernhard Reutner-Fischerb40bdb32006-11-22 18:40:06 +0000101// XXX: FIXME: this is broken. Why not just use argv[0] ?
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000102 busybox = xmalloc_readlink_or_warn("/proc/self/exe");
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000103 if (!busybox)
104 return 1;
105 install_links(busybox, use_symbolic_links);
106 if (ENABLE_FEATURE_CLEAN_UP)
John Beppu7cdc76d2000-06-28 00:41:26 +0000107 free(busybox);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000108 return 0;
John Beppu8f425db2000-06-27 04:50:02 +0000109 }
John Beppu8f425db2000-06-27 04:50:02 +0000110
Rob Landleyb766c392005-09-04 11:10:37 +0000111 /* Deal with --help. (Also print help when called with no arguments) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000112
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000113 if (argc == 1 || !strcmp(argv[1], "--help") ) {
114 if (argc > 2) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000115 applet_name = argv[2];
116 run_applet_by_name(applet_name, 2, argv);
"Vladimir N. Oleynik"10a1fe62005-09-05 11:25:27 +0000117 } else {
Rob Landleyb766c392005-09-04 11:10:37 +0000118 const struct BB_applet *a;
119 int col, output_width;
Eric Andersencc8ed391999-10-05 16:24:54 +0000120
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000121 output_width = 80 - sizeof("start-stop-daemon, ") - 8;
Rob Landleyb766c392005-09-04 11:10:37 +0000122 if (ENABLE_FEATURE_AUTOWIDTH) {
123 /* Obtain the terminal width. */
124 get_terminal_width_height(0, &output_width, NULL);
125 /* leading tab and room to wrap */
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000126 output_width -= sizeof("start-stop-daemon, ") + 8;
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000127 }
Tim Rikerb1ffba02003-11-07 19:37:20 +0000128
Rob Landleyeb84a422006-09-20 21:41:13 +0000129 printf("%s\n"
Denis Vlasenko01c27fc2006-10-05 21:10:53 +0000130 "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
131 "Licensed under GPLv2.  See source distribution for full notice.\n\n"
Rob Landleyb766c392005-09-04 11:10:37 +0000132 "Usage: busybox [function] [arguments]...\n"
133 " or: [function] [arguments]...\n\n"
134 "\tBusyBox is a multi-call binary that combines many common Unix\n"
135 "\tutilities into a single executable. Most people will create a\n"
136 "\tlink to busybox for each function they wish to use and BusyBox\n"
137 "\twill act like whatever it was invoked as!\n"
138 "\nCurrently defined functions:\n", bb_msg_full_version);
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000139 col = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000140 for (a = applets; a->name;) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000141 col += printf("%s%s", (col ? ", " : "\t"), a->name);
142 a++;
Rob Landleyb766c392005-09-04 11:10:37 +0000143 if (col > output_width && a->name) {
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000144 puts(",");
Rob Landleyb766c392005-09-04 11:10:37 +0000145 col = 0;
146 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000148 puts("\n");
149 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 }
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000151 } else run_applet_by_name(argv[1], argc - 1, argv + 1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000152
Rob Landleyb766c392005-09-04 11:10:37 +0000153 bb_error_msg_and_die("applet not found");
Eric Andersencc8ed391999-10-05 16:24:54 +0000154}