blob: e3d10c83f1b946ded744b5a0c537d3fa58d749db [file] [log] [blame]
Denis Vlasenko41f5add2007-11-28 06:49:42 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko41f5add2007-11-28 06:49:42 +00009 */
Denys Vlasenko6ec15102011-03-28 01:23:38 +020010#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
Michael Olbrich43dd0062018-04-12 10:36:54 +020013#include <limits.h>
Denis Vlasenko41f5add2007-11-28 06:49:42 +000014#include <stdlib.h>
15#include <string.h>
16#include <stdio.h>
Denys Vlasenko6ec15102011-03-28 01:23:38 +020017#include <unistd.h>
Denys Vlasenkocd7a38a2014-09-18 00:47:05 +020018#include <ctype.h>
Denys Vlasenko6ec15102011-03-28 01:23:38 +020019
20#undef ARRAY_SIZE
21#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
Denis Vlasenko41f5add2007-11-28 06:49:42 +000022
23#include "../include/autoconf.h"
Denys Vlasenko6ec15102011-03-28 01:23:38 +020024#include "../include/applet_metadata.h"
Denis Vlasenko41f5add2007-11-28 06:49:42 +000025
26struct bb_applet {
27 const char *name;
28 const char *main;
29 enum bb_install_loc_t install_loc;
30 enum bb_suid_t need_suid;
31 /* true if instead of fork(); exec("applet"); waitpid();
32 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
33 unsigned char noexec;
34 /* Even nicer */
35 /* true if instead of fork(); exec("applet"); waitpid();
36 * one can simply call applet_main(argc,argv); */
37 unsigned char nofork;
38};
39
40/* Define struct bb_applet applets[] */
41#include "../include/applets.h"
42
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000043enum { NUM_APPLETS = ARRAY_SIZE(applets) };
Denis Vlasenko41f5add2007-11-28 06:49:42 +000044
Denis Vlasenko41f5add2007-11-28 06:49:42 +000045static 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
Denys Vlasenkocd7a38a2014-09-18 00:47:05 +020052static int str_isalnum_(const char *s)
53{
54 while (*s) {
55 if (!isalnum(*s) && *s != '_')
56 return 0;
57 s++;
58 }
59 return 1;
60}
61
Denis Vlasenko41f5add2007-11-28 06:49:42 +000062int main(int argc, char **argv)
63{
Ron Yorston610c4c32016-03-30 00:42:05 +020064 int i, j;
Michael Olbrich43dd0062018-04-12 10:36:54 +020065 char tmp1[PATH_MAX], tmp2[PATH_MAX];
Denys Vlasenkodd02a052016-04-02 15:18:26 +020066
67 // In find_applet_by_name(), before linear search, narrow it down
68 // by looking at N "equidistant" names. With ~350 applets:
69 // KNOWN_APPNAME_OFFSETS cycles
70 // 0 9057
71 // 2 4604 + ~100 bytes of code
72 // 4 2407 + 4 bytes
73 // 8 1342 + 8 bytes
74 // 16 908 + 16 bytes
75 // 32 884 + 32 bytes
76 // With 8, int16_t applet_nameofs[] table has 7 elements.
77 int KNOWN_APPNAME_OFFSETS = 8;
78 // With 128 applets we do two linear searches, with 1..7 strcmp's in the first one
79 // and 1..16 strcmp's in the second. With 256 apps, second search does 1..32 strcmp's.
80 if (NUM_APPLETS < 128)
81 KNOWN_APPNAME_OFFSETS = 4;
82 if (NUM_APPLETS < 32)
83 KNOWN_APPNAME_OFFSETS = 0;
Denis Vlasenko41f5add2007-11-28 06:49:42 +000084
85 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
86
Denis Vlasenko745cd172007-11-29 03:31:20 +000087 if (!argv[1])
88 return 1;
Michael Olbrich43dd0062018-04-12 10:36:54 +020089 snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid());
90 i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666);
Denis Vlasenko745cd172007-11-29 03:31:20 +000091 if (i < 0)
92 return 1;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000093 dup2(i, 1);
Denis Vlasenko745cd172007-11-29 03:31:20 +000094
95 /* Keep in sync with include/busybox.h! */
96
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +020097 printf("/* This is a generated file, don't edit */\n\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +000098
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000099 printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000100 if (NUM_APPLETS == 1) {
101 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
Denys Vlasenko78a3b672012-03-19 04:38:00 +0100102 printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000103 }
Ron Yorston610c4c32016-03-30 00:42:05 +0200104
Denys Vlasenkodd02a052016-04-02 15:18:26 +0200105 printf("#define KNOWN_APPNAME_OFFSETS %u\n\n", KNOWN_APPNAME_OFFSETS);
106 if (KNOWN_APPNAME_OFFSETS > 0) {
107 int ofs, offset[KNOWN_APPNAME_OFFSETS], index[KNOWN_APPNAME_OFFSETS];
108 for (i = 0; i < KNOWN_APPNAME_OFFSETS; i++)
109 index[i] = i * NUM_APPLETS / KNOWN_APPNAME_OFFSETS;
110 ofs = 0;
111 for (i = 0; i < NUM_APPLETS; i++) {
112 for (j = 0; j < KNOWN_APPNAME_OFFSETS; j++)
113 if (i == index[j])
114 offset[j] = ofs;
115 ofs += strlen(applets[i].name) + 1;
116 }
117 /* If the list of names is too long refuse to proceed */
118 if (ofs > 0xffff)
119 return 1;
Ron Yorston610c4c32016-03-30 00:42:05 +0200120 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
121 for (i = 1; i < KNOWN_APPNAME_OFFSETS; i++)
122 printf("%d,\n", offset[i]);
123 printf("};\n\n");
124 }
Denis Vlasenko468aea22008-04-01 14:47:57 +0000125
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200126 //printf("#ifndef SKIP_definitions\n");
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200127 printf("const char applet_names[] ALIGN1 = \"\"\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +0000128 for (i = 0; i < NUM_APPLETS; i++) {
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000129 printf("\"%s\" \"\\0\"\n", applets[i].name);
Denys Vlasenko924a1792013-11-26 12:03:24 +0100130// if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
131// MAX_APPLET_NAME_LEN = strlen(applets[i].name);
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000132 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200133 printf(";\n\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000134
Denys Vlasenkocd7a38a2014-09-18 00:47:05 +0200135 for (i = 0; i < NUM_APPLETS; i++) {
136 if (str_isalnum_(applets[i].name))
137 printf("#define APPLET_NO_%s %d\n", applets[i].name, i);
138 }
139 printf("\n");
140
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200141 printf("#ifndef SKIP_applet_main\n");
142 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000143 for (i = 0; i < NUM_APPLETS; i++) {
144 printf("%s_main,\n", applets[i].main);
145 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200146 printf("};\n");
147 printf("#endif\n\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000148
Denys Vlasenko0fb00452016-07-22 18:48:38 +0200149#if ENABLE_FEATURE_PREFER_APPLETS \
150 || ENABLE_FEATURE_SH_STANDALONE \
151 || ENABLE_FEATURE_SH_NOFORK
Ron Yorston610c4c32016-03-30 00:42:05 +0200152 printf("const uint8_t applet_flags[] ALIGN1 = {\n");
153 i = 0;
154 while (i < NUM_APPLETS) {
155 int v = applets[i].nofork + (applets[i].noexec << 1);
156 if (++i < NUM_APPLETS)
157 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 2;
158 if (++i < NUM_APPLETS)
159 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 4;
160 if (++i < NUM_APPLETS)
161 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 6;
162 printf("0x%02x,\n", v);
163 i++;
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000164 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200165 printf("};\n\n");
Ron Yorston610c4c32016-03-30 00:42:05 +0200166#endif
167
168#if ENABLE_FEATURE_SUID
169 printf("const uint8_t applet_suid[] ALIGN1 = {\n");
170 i = 0;
171 while (i < NUM_APPLETS) {
172 int v = applets[i].need_suid; /* 2 bits */
173 if (++i < NUM_APPLETS)
174 v |= applets[i].need_suid << 2;
175 if (++i < NUM_APPLETS)
176 v |= applets[i].need_suid << 4;
177 if (++i < NUM_APPLETS)
178 v |= applets[i].need_suid << 6;
179 printf("0x%02x,\n", v);
180 i++;
181 }
182 printf("};\n\n");
183#endif
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000184
Denis Vlasenko745cd172007-11-29 03:31:20 +0000185#if ENABLE_FEATURE_INSTALLER
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200186 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +0000187 i = 0;
188 while (i < NUM_APPLETS) {
189 int v = applets[i].install_loc; /* 3 bits */
190 if (++i < NUM_APPLETS)
191 v |= applets[i].install_loc << 4; /* 3 bits */
192 printf("0x%02x,\n", v);
193 i++;
194 }
Denys Vlasenko14974842010-03-23 01:08:26 +0100195 printf("};\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +0000196#endif
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200197 //printf("#endif /* SKIP_definitions */\n");
Denys Vlasenko0dddbc12016-08-23 20:21:36 +0200198
Denys Vlasenko924a1792013-11-26 12:03:24 +0100199// printf("\n");
200// printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000201
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200202 if (argv[2]) {
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200203 FILE *fp;
Denys Vlasenko0dddbc12016-08-23 20:21:36 +0200204 char line_new[80];
205// char line_old[80];
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200206
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200207 sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
Denys Vlasenko0dddbc12016-08-23 20:21:36 +0200208// line_old[0] = 0;
209// fp = fopen(argv[2], "r");
210// if (fp) {
211// fgets(line_old, sizeof(line_old), fp);
212// fclose(fp);
213// }
214// if (strcmp(line_old, line_new) != 0) {
Michael Olbrich43dd0062018-04-12 10:36:54 +0200215 snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid());
216 fp = fopen(tmp2, "w");
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200217 if (!fp)
218 return 1;
219 fputs(line_new, fp);
Michael Olbrich43dd0062018-04-12 10:36:54 +0200220 if (fclose(fp))
221 return 1;
Denys Vlasenko0dddbc12016-08-23 20:21:36 +0200222// }
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200223 }
224
Michael Olbrich43dd0062018-04-12 10:36:54 +0200225 if (fclose(stdout))
226 return 1;
227 if (rename(tmp1, argv[1]))
228 return 1;
229 if (rename(tmp2, argv[2]))
230 return 1;
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000231 return 0;
232}