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> |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <stdio.h> |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | |
| 18 | #undef ARRAY_SIZE |
| 19 | #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 20 | |
| 21 | #include "../include/autoconf.h" |
Denys Vlasenko | 6ec1510 | 2011-03-28 01:23:38 +0200 | [diff] [blame] | 22 | #include "../include/applet_metadata.h" |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 23 | |
| 24 | struct bb_applet { |
| 25 | const char *name; |
| 26 | const char *main; |
| 27 | enum bb_install_loc_t install_loc; |
| 28 | enum bb_suid_t need_suid; |
| 29 | /* true if instead of fork(); exec("applet"); waitpid(); |
| 30 | * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */ |
| 31 | unsigned char noexec; |
| 32 | /* Even nicer */ |
| 33 | /* true if instead of fork(); exec("applet"); waitpid(); |
| 34 | * one can simply call applet_main(argc,argv); */ |
| 35 | unsigned char nofork; |
| 36 | }; |
| 37 | |
| 38 | /* Define struct bb_applet applets[] */ |
| 39 | #include "../include/applets.h" |
| 40 | |
Denis Vlasenko | 32b2a9f | 2008-02-22 22:43:22 +0000 | [diff] [blame] | 41 | enum { NUM_APPLETS = ARRAY_SIZE(applets) }; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 42 | |
| 43 | static int offset[NUM_APPLETS]; |
| 44 | |
| 45 | static int cmp_name(const void *a, const void *b) |
| 46 | { |
| 47 | const struct bb_applet *aa = a; |
| 48 | const struct bb_applet *bb = b; |
| 49 | return strcmp(aa->name, bb->name); |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) |
| 53 | { |
| 54 | int i; |
| 55 | int ofs; |
Denys Vlasenko | 924a179 | 2013-11-26 12:03:24 +0100 | [diff] [blame] | 56 | // unsigned MAX_APPLET_NAME_LEN = 1; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 57 | |
| 58 | qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name); |
| 59 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 60 | ofs = 0; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 61 | for (i = 0; i < NUM_APPLETS; i++) { |
| 62 | offset[i] = ofs; |
| 63 | ofs += strlen(applets[i].name) + 1; |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 64 | } |
| 65 | /* We reuse 4 high-order bits of offset array for other purposes, |
| 66 | * so if they are indeed needed, refuse to proceed */ |
| 67 | if (ofs > 0xfff) |
| 68 | return 1; |
| 69 | if (!argv[1]) |
| 70 | return 1; |
| 71 | |
| 72 | i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666); |
| 73 | if (i < 0) |
| 74 | return 1; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 75 | dup2(i, 1); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 76 | |
| 77 | /* Keep in sync with include/busybox.h! */ |
| 78 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 79 | printf("/* This is a generated file, don't edit */\n\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 81 | printf("#define NUM_APPLETS %u\n", NUM_APPLETS); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 82 | if (NUM_APPLETS == 1) { |
| 83 | printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name); |
Denys Vlasenko | 78a3b67 | 2012-03-19 04:38:00 +0100 | [diff] [blame] | 84 | printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 85 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 86 | printf("\n"); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 87 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 88 | //printf("#ifndef SKIP_definitions\n"); |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 89 | printf("const char applet_names[] ALIGN1 = \"\"\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 90 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 91 | printf("\"%s\" \"\\0\"\n", applets[i].name); |
Denys Vlasenko | 924a179 | 2013-11-26 12:03:24 +0100 | [diff] [blame] | 92 | // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name)) |
| 93 | // MAX_APPLET_NAME_LEN = strlen(applets[i].name); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 94 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 95 | printf(";\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 96 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 97 | printf("#ifndef SKIP_applet_main\n"); |
| 98 | printf("int (*const applet_main[])(int argc, char **argv) = {\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 99 | for (i = 0; i < NUM_APPLETS; i++) { |
| 100 | printf("%s_main,\n", applets[i].main); |
| 101 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 102 | printf("};\n"); |
| 103 | printf("#endif\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 104 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 105 | printf("const uint16_t applet_nameofs[] ALIGN2 = {\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 106 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 107 | printf("0x%04x,\n", |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 108 | offset[i] |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 109 | #if ENABLE_FEATURE_PREFER_APPLETS |
| 110 | + (applets[i].nofork << 12) |
| 111 | + (applets[i].noexec << 13) |
| 112 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 113 | #if ENABLE_FEATURE_SUID |
Denis Vlasenko | 32b2a9f | 2008-02-22 22:43:22 +0000 | [diff] [blame] | 114 | + (applets[i].need_suid << 14) /* 2 bits */ |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 115 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 116 | ); |
| 117 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 118 | printf("};\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 120 | #if ENABLE_FEATURE_INSTALLER |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 121 | printf("const uint8_t applet_install_loc[] ALIGN1 = {\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 122 | i = 0; |
| 123 | while (i < NUM_APPLETS) { |
| 124 | int v = applets[i].install_loc; /* 3 bits */ |
| 125 | if (++i < NUM_APPLETS) |
| 126 | v |= applets[i].install_loc << 4; /* 3 bits */ |
| 127 | printf("0x%02x,\n", v); |
| 128 | i++; |
| 129 | } |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 130 | printf("};\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 131 | #endif |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 132 | //printf("#endif /* SKIP_definitions */\n"); |
Denys Vlasenko | 924a179 | 2013-11-26 12:03:24 +0100 | [diff] [blame] | 133 | // printf("\n"); |
| 134 | // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN); |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 135 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 136 | if (argv[2]) { |
| 137 | char line_old[80]; |
| 138 | char line_new[80]; |
| 139 | FILE *fp; |
| 140 | |
| 141 | line_old[0] = 0; |
| 142 | fp = fopen(argv[2], "r"); |
| 143 | if (fp) { |
| 144 | fgets(line_old, sizeof(line_old), fp); |
| 145 | fclose(fp); |
| 146 | } |
| 147 | sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS); |
| 148 | if (strcmp(line_old, line_new) != 0) { |
| 149 | fp = fopen(argv[2], "w"); |
| 150 | if (!fp) |
| 151 | return 1; |
| 152 | fputs(line_new, fp); |
| 153 | } |
| 154 | } |
| 155 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |