applet_tables.c 6.3 KB

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