Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 2 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 5 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
Eric Andersen | 8c725e6 | 2000-11-30 00:27:06 +0000 | [diff] [blame] | 8 | |
| 9 | #undef APPLET |
| 10 | #undef APPLET_NOUSAGE |
| 11 | #undef PROTOTYPES |
Mark Whitley | 130005c | 2000-10-25 00:28:27 +0000 | [diff] [blame] | 12 | #include "applets.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 13 | |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 14 | #define bb_need_full_version |
| 15 | #define BB_DECLARE_EXTERN |
| 16 | #include "messages.c" |
| 17 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | static int been_there_done_that = 0; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 19 | const char *applet_name; |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 20 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 21 | #ifdef BB_FEATURE_INSTALLER |
| 22 | /* |
| 23 | * directory table |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | * this should be consistent w/ the enum, busybox.h::Location, |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 25 | * or else... |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 26 | */ |
| 27 | static char* install_dir[] = { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 28 | "/", |
| 29 | "/bin", |
| 30 | "/sbin", |
| 31 | "/usr/bin", |
| 32 | "/usr/sbin", |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* abstract link() */ |
| 36 | typedef int (*__link_f)(const char *, const char *); |
| 37 | |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 38 | /* |
| 39 | * Where in the filesystem is this busybox? |
| 40 | * [return] |
| 41 | * malloc'd string w/ full pathname of busybox's location |
| 42 | * NULL on failure |
| 43 | */ |
| 44 | static char *busybox_fullpath() |
| 45 | { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 46 | pid_t pid; |
| 47 | char path[256]; |
| 48 | char proc[256]; |
| 49 | int len; |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 50 | |
| 51 | pid = getpid(); |
| 52 | sprintf(proc, "/proc/%d/exe", pid); |
| 53 | len = readlink(proc, path, 256); |
| 54 | if (len != -1) { |
| 55 | path[len] = 0; |
| 56 | } else { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 57 | perror_msg("%s", proc); |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | return strdup(path); |
| 61 | } |
| 62 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 63 | /* create (sym)links for each applet */ |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 64 | static void install_links(const char *busybox, int use_symbolic_links) |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 65 | { |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 66 | __link_f Link = link; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 67 | |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 68 | char command[256]; |
| 69 | int i; |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 70 | int rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 72 | if (use_symbolic_links) |
| 73 | Link = symlink; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 74 | |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 75 | for (i = 0; applets[i].name != NULL; i++) { |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 76 | sprintf ( command, "%s/%s", |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 77 | install_dir[applets[i].location], applets[i].name); |
Eric Andersen | c5949f6 | 2000-09-25 20:35:54 +0000 | [diff] [blame] | 78 | rc = Link(busybox, command); |
| 79 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 80 | if (rc) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 81 | perror_msg("%s", command); |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 82 | } |
John Beppu | eb02833 | 2000-06-28 00:55:31 +0000 | [diff] [blame] | 83 | } |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 84 | } |
| 85 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 86 | #endif /* BB_FEATURE_INSTALLER */ |
| 87 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | int main(int argc, char **argv) |
| 89 | { |
Matt Kraai | f2cc276 | 2001-02-01 19:21:20 +0000 | [diff] [blame] | 90 | const char *s; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 91 | |
| 92 | for (s = applet_name = argv[0]; *s != '\0';) { |
| 93 | if (*s++ == '/') |
| 94 | applet_name = s; |
| 95 | } |
| 96 | |
| 97 | #ifdef BB_SH |
| 98 | /* Add in a special case hack -- whenever **argv == '-' |
| 99 | * (i.e. '-su' or '-sh') always invoke the shell */ |
| 100 | if (**argv == '-' && *(*argv+1)!= '-') { |
| 101 | exit(((*(shell_main)) (argc, argv))); |
| 102 | } |
| 103 | #endif |
| 104 | |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 105 | run_applet_by_name(applet_name, argc, argv); |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 106 | error_msg_and_die("applet not found"); |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | int busybox_main(int argc, char **argv) |
| 111 | { |
| 112 | int col = 0, len, i; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 113 | |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 114 | #ifdef BB_FEATURE_INSTALLER |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 115 | /* |
| 116 | * This style of argument parsing doesn't scale well |
| 117 | * in the event that busybox starts wanting more --options. |
| 118 | * If someone has a cleaner approach, by all means implement it. |
| 119 | */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 120 | if (argc > 1 && (strcmp(argv[1], "--install") == 0)) { |
| 121 | int use_symbolic_links = 0; |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 122 | int rc = 0; |
| 123 | char *busybox; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 124 | |
John Beppu | 27b5924 | 2000-06-27 04:56:45 +0000 | [diff] [blame] | 125 | /* to use symlinks, or not to use symlinks... */ |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 126 | if (argc > 2) { |
| 127 | if ((strcmp(argv[2], "-s") == 0)) { |
| 128 | use_symbolic_links = 1; |
| 129 | } |
| 130 | } |
John Beppu | 7cdc76d | 2000-06-28 00:41:26 +0000 | [diff] [blame] | 131 | |
| 132 | /* link */ |
| 133 | busybox = busybox_fullpath(); |
| 134 | if (busybox) { |
| 135 | install_links(busybox, use_symbolic_links); |
| 136 | free(busybox); |
| 137 | } else { |
| 138 | rc = 1; |
| 139 | } |
| 140 | return rc; |
John Beppu | 8f425db | 2000-06-27 04:50:02 +0000 | [diff] [blame] | 141 | } |
| 142 | #endif /* BB_FEATURE_INSTALLER */ |
| 143 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 146 | /* If we've already been here once, exit now */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | if (been_there_done_that == 1 || argc < 1) { |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 148 | const struct BB_applet *a = applets; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 150 | fprintf(stderr, "%s\n\n" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 151 | "Usage: busybox [function] [arguments]...\n" |
| 152 | " or: [function] [arguments]...\n\n" |
John Beppu | b4f8606 | 2000-04-13 03:36:01 +0000 | [diff] [blame] | 153 | "\tBusyBox is a multi-call binary that combines many common Unix\n" |
| 154 | "\tutilities into a single executable. Most people will create a\n" |
| 155 | "\tlink to busybox for each function they wish to use, and BusyBox\n" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 156 | "\twill act like whatever it was invoked as.\n" |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 157 | "\nCurrently defined functions:\n", full_version); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | |
| 159 | while (a->name != 0) { |
| 160 | col += |
| 161 | fprintf(stderr, "%s%s", ((col == 0) ? "\t" : ", "), |
| 162 | (a++)->name); |
| 163 | if (col > 60 && a->name != 0) { |
| 164 | fprintf(stderr, ",\n"); |
| 165 | col = 0; |
| 166 | } |
| 167 | } |
| 168 | fprintf(stderr, "\n\n"); |
| 169 | exit(-1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 170 | } |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 171 | |
| 172 | /* Flag that we've been here already */ |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 173 | been_there_done_that = 1; |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 174 | |
Matt Kraai | 8abc78a | 2000-12-15 00:35:22 +0000 | [diff] [blame] | 175 | /* Move the command line down a notch */ |
| 176 | len = argv[argc] + strlen(argv[argc]) - argv[1]; |
| 177 | memmove(argv[0], argv[1], len); |
| 178 | memset(argv[0] + len, 0, argv[1] - argv[0]); |
| 179 | |
| 180 | /* Fix up the argv pointers */ |
| 181 | len = argv[1] - argv[0]; |
Eric Andersen | 90ca284 | 2001-01-27 08:32:57 +0000 | [diff] [blame] | 182 | memmove(argv, argv + 1, sizeof(char *) * (argc + 1)); |
Matt Kraai | 8abc78a | 2000-12-15 00:35:22 +0000 | [diff] [blame] | 183 | for (i = 0; i < argc; i++) |
| 184 | argv[i] -= len; |
Eric Andersen | 5e09b6e | 2000-12-08 19:03:12 +0000 | [diff] [blame] | 185 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 186 | return (main(argc, argv)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 187 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | Local Variables: |
| 191 | c-file-style: "linux" |
| 192 | c-basic-offset: 4 |
| 193 | tab-width: 4 |
| 194 | End: |
| 195 | */ |