applet_tables.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 offset[NUM_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;
  57. int ofs;
  58. // unsigned MAX_APPLET_NAME_LEN = 1;
  59. qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
  60. ofs = 0;
  61. for (i = 0; i < NUM_APPLETS; i++) {
  62. offset[i] = ofs;
  63. ofs += strlen(applets[i].name) + 1;
  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. i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
  72. if (i < 0)
  73. return 1;
  74. dup2(i, 1);
  75. /* Keep in sync with include/busybox.h! */
  76. printf("/* This is a generated file, don't edit */\n\n");
  77. printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
  78. if (NUM_APPLETS == 1) {
  79. printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
  80. printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main);
  81. }
  82. printf("\n");
  83. //printf("#ifndef SKIP_definitions\n");
  84. printf("const char applet_names[] ALIGN1 = \"\"\n");
  85. for (i = 0; i < NUM_APPLETS; i++) {
  86. printf("\"%s\" \"\\0\"\n", applets[i].name);
  87. // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
  88. // MAX_APPLET_NAME_LEN = strlen(applets[i].name);
  89. }
  90. printf(";\n\n");
  91. for (i = 0; i < NUM_APPLETS; i++) {
  92. if (str_isalnum_(applets[i].name))
  93. printf("#define APPLET_NO_%s %d\n", applets[i].name, i);
  94. }
  95. printf("\n");
  96. printf("#ifndef SKIP_applet_main\n");
  97. printf("int (*const applet_main[])(int argc, char **argv) = {\n");
  98. for (i = 0; i < NUM_APPLETS; i++) {
  99. printf("%s_main,\n", applets[i].main);
  100. }
  101. printf("};\n");
  102. printf("#endif\n\n");
  103. printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
  104. for (i = 0; i < NUM_APPLETS; i++) {
  105. printf("0x%04x,\n",
  106. offset[i]
  107. #if ENABLE_FEATURE_PREFER_APPLETS
  108. + (applets[i].nofork << 12)
  109. + (applets[i].noexec << 13)
  110. #endif
  111. #if ENABLE_FEATURE_SUID
  112. + (applets[i].need_suid << 14) /* 2 bits */
  113. #endif
  114. );
  115. }
  116. printf("};\n\n");
  117. #if ENABLE_FEATURE_INSTALLER
  118. printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
  119. i = 0;
  120. while (i < NUM_APPLETS) {
  121. int v = applets[i].install_loc; /* 3 bits */
  122. if (++i < NUM_APPLETS)
  123. v |= applets[i].install_loc << 4; /* 3 bits */
  124. printf("0x%02x,\n", v);
  125. i++;
  126. }
  127. printf("};\n");
  128. #endif
  129. //printf("#endif /* SKIP_definitions */\n");
  130. // printf("\n");
  131. // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
  132. if (argv[2]) {
  133. char line_old[80];
  134. char line_new[80];
  135. FILE *fp;
  136. line_old[0] = 0;
  137. fp = fopen(argv[2], "r");
  138. if (fp) {
  139. fgets(line_old, sizeof(line_old), fp);
  140. fclose(fp);
  141. }
  142. sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
  143. if (strcmp(line_old, line_new) != 0) {
  144. fp = fopen(argv[2], "w");
  145. if (!fp)
  146. return 1;
  147. fputs(line_new, fp);
  148. }
  149. }
  150. return 0;
  151. }