Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Applet table generator. |
| 4 | * Runs on host and produces include/applet_tables.h |
| 5 | * |
| 6 | * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <fcntl.h> |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 13 | #include <limits.h> |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <stdio.h> |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 17 | #include <unistd.h> |
Denys Vlasenko | cd7a38a | 2014-09-18 00:47:05 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 19 | |
| 20 | #undef ARRAY_SIZE |
| 21 | #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 22 | |
Denys Vlasenko | 0723131 | 2020-12-17 11:22:44 +0100 | [diff] [blame] | 23 | #ifndef PATH_MAX |
| 24 | #define PATH_MAX 1024 |
| 25 | #endif |
| 26 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 27 | #include "../include/autoconf.h" |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 28 | #include "../include/applet_metadata.h" |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 29 | |
| 30 | struct bb_applet { |
| 31 | const char *name; |
| 32 | const char *main; |
| 33 | enum bb_install_loc_t install_loc; |
| 34 | enum bb_suid_t need_suid; |
| 35 | /* true if instead of fork(); exec("applet"); waitpid(); |
| 36 | * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */ |
| 37 | unsigned char noexec; |
| 38 | /* Even nicer */ |
| 39 | /* true if instead of fork(); exec("applet"); waitpid(); |
| 40 | * one can simply call applet_main(argc,argv); */ |
| 41 | unsigned char nofork; |
| 42 | }; |
| 43 | |
| 44 | /* Define struct bb_applet applets[] */ |
| 45 | #include "../include/applets.h" |
| 46 | |
Denis Vlasenko | 32b2a9f | 2008-02-22 22:43:22 +0000 | [diff] [blame] | 47 | enum { NUM_APPLETS = ARRAY_SIZE(applets) }; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 49 | static int cmp_name(const void *a, const void *b) |
| 50 | { |
| 51 | const struct bb_applet *aa = a; |
| 52 | const struct bb_applet *bb = b; |
| 53 | return strcmp(aa->name, bb->name); |
| 54 | } |
| 55 | |
Denys Vlasenko | cd7a38a | 2014-09-18 00:47:05 +0200 | [diff] [blame] | 56 | static int str_isalnum_(const char *s) |
| 57 | { |
| 58 | while (*s) { |
Denys Vlasenko | 1a45b2c | 2021-04-14 19:12:43 +0200 | [diff] [blame] | 59 | if (!isalnum((unsigned char)*s) && *s != '_') |
Denys Vlasenko | cd7a38a | 2014-09-18 00:47:05 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | s++; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 66 | int main(int argc, char **argv) |
| 67 | { |
Ron Yorston | 610c4c3 | 2016-03-30 00:42:05 +0200 | [diff] [blame] | 68 | int i, j; |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 69 | char tmp1[PATH_MAX], tmp2[PATH_MAX]; |
Denys Vlasenko | dd02a05 | 2016-04-02 15:18:26 +0200 | [diff] [blame] | 70 | |
| 71 | // In find_applet_by_name(), before linear search, narrow it down |
| 72 | // by looking at N "equidistant" names. With ~350 applets: |
| 73 | // KNOWN_APPNAME_OFFSETS cycles |
| 74 | // 0 9057 |
| 75 | // 2 4604 + ~100 bytes of code |
| 76 | // 4 2407 + 4 bytes |
| 77 | // 8 1342 + 8 bytes |
| 78 | // 16 908 + 16 bytes |
| 79 | // 32 884 + 32 bytes |
| 80 | // With 8, int16_t applet_nameofs[] table has 7 elements. |
| 81 | int KNOWN_APPNAME_OFFSETS = 8; |
| 82 | // With 128 applets we do two linear searches, with 1..7 strcmp's in the first one |
| 83 | // and 1..16 strcmp's in the second. With 256 apps, second search does 1..32 strcmp's. |
| 84 | if (NUM_APPLETS < 128) |
| 85 | KNOWN_APPNAME_OFFSETS = 4; |
| 86 | if (NUM_APPLETS < 32) |
| 87 | KNOWN_APPNAME_OFFSETS = 0; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 88 | |
| 89 | qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name); |
| 90 | |
Ron Yorston | 2d21779 | 2018-11-25 11:46:39 +0000 | [diff] [blame] | 91 | for (i = j = 0; i < NUM_APPLETS-1; ++i) { |
| 92 | if (cmp_name(applets+i, applets+i+1) == 0) { |
| 93 | fprintf(stderr, "%s: duplicate applet name '%s'\n", argv[0], |
| 94 | applets[i].name); |
| 95 | j = 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (j != 0 || !argv[1]) |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 100 | return 1; |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 101 | snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid()); |
| 102 | i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 103 | if (i < 0) |
| 104 | return 1; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 105 | dup2(i, 1); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 106 | |
| 107 | /* Keep in sync with include/busybox.h! */ |
| 108 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 109 | printf("/* This is a generated file, don't edit */\n\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 111 | printf("#define NUM_APPLETS %u\n", NUM_APPLETS); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 112 | if (NUM_APPLETS == 1) { |
| 113 | printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name); |
Denys Vlasenko | 78a3b67 | 2012-03-19 04:38:00 +0100 | [diff] [blame] | 114 | printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 115 | } |
Ron Yorston | 610c4c3 | 2016-03-30 00:42:05 +0200 | [diff] [blame] | 116 | |
Denys Vlasenko | dd02a05 | 2016-04-02 15:18:26 +0200 | [diff] [blame] | 117 | printf("#define KNOWN_APPNAME_OFFSETS %u\n\n", KNOWN_APPNAME_OFFSETS); |
| 118 | if (KNOWN_APPNAME_OFFSETS > 0) { |
| 119 | int ofs, offset[KNOWN_APPNAME_OFFSETS], index[KNOWN_APPNAME_OFFSETS]; |
| 120 | for (i = 0; i < KNOWN_APPNAME_OFFSETS; i++) |
| 121 | index[i] = i * NUM_APPLETS / KNOWN_APPNAME_OFFSETS; |
| 122 | ofs = 0; |
| 123 | for (i = 0; i < NUM_APPLETS; i++) { |
| 124 | for (j = 0; j < KNOWN_APPNAME_OFFSETS; j++) |
| 125 | if (i == index[j]) |
| 126 | offset[j] = ofs; |
| 127 | ofs += strlen(applets[i].name) + 1; |
| 128 | } |
| 129 | /* If the list of names is too long refuse to proceed */ |
| 130 | if (ofs > 0xffff) |
| 131 | return 1; |
Ron Yorston | 610c4c3 | 2016-03-30 00:42:05 +0200 | [diff] [blame] | 132 | printf("const uint16_t applet_nameofs[] ALIGN2 = {\n"); |
| 133 | for (i = 1; i < KNOWN_APPNAME_OFFSETS; i++) |
| 134 | printf("%d,\n", offset[i]); |
| 135 | printf("};\n\n"); |
| 136 | } |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 137 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 138 | //printf("#ifndef SKIP_definitions\n"); |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 139 | printf("const char applet_names[] ALIGN1 = \"\"\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 140 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 141 | printf("\"%s\" \"\\0\"\n", applets[i].name); |
Denys Vlasenko | 924a179 | 2013-11-26 12:03:24 +0100 | [diff] [blame] | 142 | // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name)) |
| 143 | // MAX_APPLET_NAME_LEN = strlen(applets[i].name); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 144 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 145 | printf(";\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 146 | |
Denys Vlasenko | cd7a38a | 2014-09-18 00:47:05 +0200 | [diff] [blame] | 147 | for (i = 0; i < NUM_APPLETS; i++) { |
| 148 | if (str_isalnum_(applets[i].name)) |
| 149 | printf("#define APPLET_NO_%s %d\n", applets[i].name, i); |
| 150 | } |
| 151 | printf("\n"); |
| 152 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 153 | printf("#ifndef SKIP_applet_main\n"); |
| 154 | printf("int (*const applet_main[])(int argc, char **argv) = {\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 155 | for (i = 0; i < NUM_APPLETS; i++) { |
| 156 | printf("%s_main,\n", applets[i].main); |
| 157 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 158 | printf("};\n"); |
| 159 | printf("#endif\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 160 | |
Denys Vlasenko | 0fb0045 | 2016-07-22 18:48:38 +0200 | [diff] [blame] | 161 | #if ENABLE_FEATURE_PREFER_APPLETS \ |
| 162 | || ENABLE_FEATURE_SH_STANDALONE \ |
| 163 | || ENABLE_FEATURE_SH_NOFORK |
Ron Yorston | 610c4c3 | 2016-03-30 00:42:05 +0200 | [diff] [blame] | 164 | printf("const uint8_t applet_flags[] ALIGN1 = {\n"); |
| 165 | i = 0; |
| 166 | while (i < NUM_APPLETS) { |
| 167 | int v = applets[i].nofork + (applets[i].noexec << 1); |
| 168 | if (++i < NUM_APPLETS) |
| 169 | v |= (applets[i].nofork + (applets[i].noexec << 1)) << 2; |
| 170 | if (++i < NUM_APPLETS) |
| 171 | v |= (applets[i].nofork + (applets[i].noexec << 1)) << 4; |
| 172 | if (++i < NUM_APPLETS) |
| 173 | v |= (applets[i].nofork + (applets[i].noexec << 1)) << 6; |
| 174 | printf("0x%02x,\n", v); |
| 175 | i++; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 176 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 177 | printf("};\n\n"); |
Ron Yorston | 610c4c3 | 2016-03-30 00:42:05 +0200 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | #if ENABLE_FEATURE_SUID |
| 181 | printf("const uint8_t applet_suid[] ALIGN1 = {\n"); |
| 182 | i = 0; |
| 183 | while (i < NUM_APPLETS) { |
| 184 | int v = applets[i].need_suid; /* 2 bits */ |
| 185 | if (++i < NUM_APPLETS) |
| 186 | v |= applets[i].need_suid << 2; |
| 187 | if (++i < NUM_APPLETS) |
| 188 | v |= applets[i].need_suid << 4; |
| 189 | if (++i < NUM_APPLETS) |
| 190 | v |= applets[i].need_suid << 6; |
| 191 | printf("0x%02x,\n", v); |
| 192 | i++; |
| 193 | } |
| 194 | printf("};\n\n"); |
| 195 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 197 | #if ENABLE_FEATURE_INSTALLER |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 198 | printf("const uint8_t applet_install_loc[] ALIGN1 = {\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 199 | i = 0; |
| 200 | while (i < NUM_APPLETS) { |
| 201 | int v = applets[i].install_loc; /* 3 bits */ |
| 202 | if (++i < NUM_APPLETS) |
| 203 | v |= applets[i].install_loc << 4; /* 3 bits */ |
| 204 | printf("0x%02x,\n", v); |
| 205 | i++; |
| 206 | } |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 207 | printf("};\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 208 | #endif |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 209 | //printf("#endif /* SKIP_definitions */\n"); |
Denys Vlasenko | 0dddbc1 | 2016-08-23 20:21:36 +0200 | [diff] [blame] | 210 | |
Denys Vlasenko | 924a179 | 2013-11-26 12:03:24 +0100 | [diff] [blame] | 211 | // printf("\n"); |
| 212 | // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN); |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 213 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 214 | if (argv[2]) { |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 215 | FILE *fp; |
Denys Vlasenko | 0dddbc1 | 2016-08-23 20:21:36 +0200 | [diff] [blame] | 216 | char line_new[80]; |
| 217 | // char line_old[80]; |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 218 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 219 | sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS); |
Denys Vlasenko | 0dddbc1 | 2016-08-23 20:21:36 +0200 | [diff] [blame] | 220 | // line_old[0] = 0; |
| 221 | // fp = fopen(argv[2], "r"); |
| 222 | // if (fp) { |
| 223 | // fgets(line_old, sizeof(line_old), fp); |
| 224 | // fclose(fp); |
| 225 | // } |
| 226 | // if (strcmp(line_old, line_new) != 0) { |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 227 | snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid()); |
| 228 | fp = fopen(tmp2, "w"); |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 229 | if (!fp) |
| 230 | return 1; |
| 231 | fputs(line_new, fp); |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 232 | if (fclose(fp)) |
| 233 | return 1; |
Denys Vlasenko | 0dddbc1 | 2016-08-23 20:21:36 +0200 | [diff] [blame] | 234 | // } |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Michael Olbrich | 43dd006 | 2018-04-12 10:36:54 +0200 | [diff] [blame] | 237 | if (fclose(stdout)) |
| 238 | return 1; |
| 239 | if (rename(tmp1, argv[1])) |
| 240 | return 1; |
| 241 | if (rename(tmp2, argv[2])) |
| 242 | return 1; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | } |