3
0

applet_tables.c 5.7 KB

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