applet_tables.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <limits.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <ctype.h>
  19. #undef ARRAY_SIZE
  20. #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  21. #ifndef PATH_MAX
  22. #define PATH_MAX 1024
  23. #endif
  24. #include "../include/autoconf.h"
  25. #include "../include/applet_metadata.h"
  26. struct 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. /* Define struct bb_applet applets[] */
  40. #include "../include/applets.h"
  41. enum { NUM_APPLETS = ARRAY_SIZE(applets) };
  42. static int cmp_name(const void *a, const void *b)
  43. {
  44. const struct bb_applet *aa = a;
  45. const struct bb_applet *bb = b;
  46. return strcmp(aa->name, bb->name);
  47. }
  48. static int str_isalnum_(const char *s)
  49. {
  50. while (*s) {
  51. if (!isalnum((unsigned char)*s) && *s != '_')
  52. return 0;
  53. s++;
  54. }
  55. return 1;
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. int i, j;
  60. char tmp1[PATH_MAX], tmp2[PATH_MAX];
  61. // In find_applet_by_name(), before linear search, narrow it down
  62. // by looking at N "equidistant" names. With ~350 applets:
  63. // KNOWN_APPNAME_OFFSETS cycles
  64. // 0 9057
  65. // 2 4604 + ~100 bytes of code
  66. // 4 2407 + 4 bytes
  67. // 8 1342 + 8 bytes
  68. // 16 908 + 16 bytes
  69. // 32 884 + 32 bytes
  70. // With 8, int16_t applet_nameofs[] table has 7 elements.
  71. int KNOWN_APPNAME_OFFSETS = 8;
  72. // With 128 applets we do two linear searches, with 1..7 strcmp's in the first one
  73. // and 1..16 strcmp's in the second. With 256 apps, second search does 1..32 strcmp's.
  74. if (NUM_APPLETS < 128)
  75. KNOWN_APPNAME_OFFSETS = 4;
  76. if (NUM_APPLETS < 32)
  77. KNOWN_APPNAME_OFFSETS = 0;
  78. qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
  79. for (i = j = 0; i < NUM_APPLETS-1; ++i) {
  80. if (cmp_name(applets+i, applets+i+1) == 0) {
  81. fprintf(stderr, "%s: duplicate applet name '%s'\n", argv[0],
  82. applets[i].name);
  83. j = 1;
  84. }
  85. }
  86. if (j != 0 || !argv[1])
  87. return 1;
  88. snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid());
  89. i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  90. if (i < 0)
  91. return 1;
  92. dup2(i, 1);
  93. /* Keep in sync with include/busybox.h! */
  94. printf("/* This is a generated file, don't edit */\n\n");
  95. printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
  96. if (NUM_APPLETS == 1) {
  97. printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
  98. printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main);
  99. }
  100. printf("#define KNOWN_APPNAME_OFFSETS %u\n\n", KNOWN_APPNAME_OFFSETS);
  101. if (KNOWN_APPNAME_OFFSETS > 0) {
  102. int ofs, offset[KNOWN_APPNAME_OFFSETS], index[KNOWN_APPNAME_OFFSETS];
  103. for (i = 0; i < KNOWN_APPNAME_OFFSETS; i++)
  104. index[i] = i * NUM_APPLETS / KNOWN_APPNAME_OFFSETS;
  105. ofs = 0;
  106. for (i = 0; i < NUM_APPLETS; i++) {
  107. for (j = 0; j < KNOWN_APPNAME_OFFSETS; j++)
  108. if (i == index[j])
  109. offset[j] = ofs;
  110. ofs += strlen(applets[i].name) + 1;
  111. }
  112. /* If the list of names is too long refuse to proceed */
  113. if (ofs > 0xffff)
  114. return 1;
  115. printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
  116. for (i = 1; i < KNOWN_APPNAME_OFFSETS; i++)
  117. printf("%d,\n", offset[i]);
  118. printf("};\n\n");
  119. }
  120. //printf("#ifndef SKIP_definitions\n");
  121. printf("const char applet_names[] ALIGN1 = \"\"\n");
  122. for (i = 0; i < NUM_APPLETS; i++) {
  123. printf("\"%s\" \"\\0\"\n", applets[i].name);
  124. // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
  125. // MAX_APPLET_NAME_LEN = strlen(applets[i].name);
  126. }
  127. printf(";\n\n");
  128. for (i = 0; i < NUM_APPLETS; i++) {
  129. if (str_isalnum_(applets[i].name))
  130. printf("#define APPLET_NO_%s %d\n", applets[i].name, i);
  131. }
  132. printf("\n");
  133. printf("#ifndef SKIP_applet_main\n");
  134. printf("int (*const applet_main[])(int argc, char **argv) = {\n");
  135. for (i = 0; i < NUM_APPLETS; i++) {
  136. printf("%s_main,\n", applets[i].main);
  137. }
  138. printf("};\n");
  139. printf("#endif\n\n");
  140. #if ENABLE_FEATURE_PREFER_APPLETS \
  141. || ENABLE_FEATURE_SH_STANDALONE \
  142. || ENABLE_FEATURE_SH_NOFORK
  143. printf("const uint8_t applet_flags[] ALIGN1 = {\n");
  144. i = 0;
  145. while (i < NUM_APPLETS) {
  146. int v = applets[i].nofork + (applets[i].noexec << 1);
  147. if (++i < NUM_APPLETS)
  148. v |= (applets[i].nofork + (applets[i].noexec << 1)) << 2;
  149. if (++i < NUM_APPLETS)
  150. v |= (applets[i].nofork + (applets[i].noexec << 1)) << 4;
  151. if (++i < NUM_APPLETS)
  152. v |= (applets[i].nofork + (applets[i].noexec << 1)) << 6;
  153. printf("0x%02x,\n", v);
  154. i++;
  155. }
  156. printf("};\n\n");
  157. #endif
  158. #if ENABLE_FEATURE_SUID
  159. printf("const uint8_t applet_suid[] ALIGN1 = {\n");
  160. i = 0;
  161. while (i < NUM_APPLETS) {
  162. int v = applets[i].need_suid; /* 2 bits */
  163. if (++i < NUM_APPLETS)
  164. v |= applets[i].need_suid << 2;
  165. if (++i < NUM_APPLETS)
  166. v |= applets[i].need_suid << 4;
  167. if (++i < NUM_APPLETS)
  168. v |= applets[i].need_suid << 6;
  169. printf("0x%02x,\n", v);
  170. i++;
  171. }
  172. printf("};\n\n");
  173. #endif
  174. #if ENABLE_FEATURE_INSTALLER
  175. printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
  176. i = 0;
  177. while (i < NUM_APPLETS) {
  178. int v = applets[i].install_loc; /* 3 bits */
  179. if (++i < NUM_APPLETS)
  180. v |= applets[i].install_loc << 4; /* 3 bits */
  181. printf("0x%02x,\n", v);
  182. i++;
  183. }
  184. printf("};\n");
  185. #endif
  186. //printf("#endif /* SKIP_definitions */\n");
  187. // printf("\n");
  188. // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
  189. if (argv[2]) {
  190. FILE *fp;
  191. char line_new[80];
  192. // char line_old[80];
  193. sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
  194. // line_old[0] = 0;
  195. // fp = fopen(argv[2], "r");
  196. // if (fp) {
  197. // fgets(line_old, sizeof(line_old), fp);
  198. // fclose(fp);
  199. // }
  200. // if (strcmp(line_old, line_new) != 0) {
  201. snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid());
  202. fp = fopen(tmp2, "w");
  203. if (!fp)
  204. return 1;
  205. fputs(line_new, fp);
  206. if (fclose(fp))
  207. return 1;
  208. // }
  209. }
  210. if (fclose(stdout))
  211. return 1;
  212. if (rename(tmp1, argv[1]))
  213. return 1;
  214. if (rename(tmp2, argv[2]))
  215. return 1;
  216. return 0;
  217. }