1
0

script.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. *
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include <faux/str.h>
  16. #include <faux/list.h>
  17. #include <klish/kcontext.h>
  18. #include <klish/ksession.h>
  19. static char *script_mkfifo(void)
  20. {
  21. int res = 0;
  22. char *name = NULL;
  23. name = faux_str_sprintf("/tmp/klish.fifo.%u.XXXXXX", getpid());
  24. mktemp(name);
  25. if (mkfifo(name, 0600) < 0) {
  26. faux_str_free(name);
  27. return NULL;
  28. }
  29. return name;
  30. }
  31. const char *kcontext_type_e_str[] = {
  32. "none",
  33. "plugin_init",
  34. "plugin_fini",
  35. "action",
  36. "service_action"
  37. };
  38. #define PREFIX "KLISH_"
  39. #define OVERWRITE 1
  40. static bool_t populate_env_kpargv(const kpargv_t *pargv, const char *prefix)
  41. {
  42. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  43. const kentry_t *entry = NULL;
  44. kpargv_pargs_node_t *iter = NULL;
  45. kparg_t *parg = NULL;
  46. const kentry_t *saved_entry = NULL;
  47. size_t num = 0;
  48. if (!pargv)
  49. return BOOL_FALSE;
  50. // Command
  51. entry = kpargv_command(pargv);
  52. if (entry) {
  53. char *var = faux_str_sprintf("%sCOMMAND", prefix);
  54. setenv(var, kentry_name(entry), OVERWRITE);
  55. faux_str_free(var);
  56. }
  57. // Parameters
  58. iter = kpargv_pargs_iter(pargv);
  59. while ((parg = kpargv_pargs_each(&iter))) {
  60. const char *str = NULL;
  61. char *var = NULL;
  62. entry = kparg_entry(parg);
  63. if (kentry_max(entry) > 1) { // Multi
  64. if (entry == saved_entry)
  65. num++;
  66. else
  67. num = 0;
  68. var = faux_str_sprintf("%sPARAM_%s_%u",
  69. prefix, kentry_name(entry), num);
  70. saved_entry = entry;
  71. } else { // Single
  72. var = faux_str_sprintf("%sPARAM_%s",
  73. prefix, kentry_name(entry));
  74. saved_entry = NULL;
  75. num = 0;
  76. }
  77. setenv(var, kparg_value(parg), OVERWRITE);
  78. faux_str_free(var);
  79. }
  80. return BOOL_TRUE;
  81. }
  82. static bool_t populate_env(kcontext_t *context)
  83. {
  84. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  85. const kentry_t *entry = NULL;
  86. const char *str = NULL;
  87. assert(context);
  88. // Type
  89. type = kcontext_type(context);
  90. if (type >= KCONTEXT_TYPE_MAX)
  91. type = KCONTEXT_TYPE_NONE;
  92. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  93. // Candidate
  94. entry = kcontext_candidate_entry(context);
  95. if (entry)
  96. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  97. // Value
  98. str = kcontext_candidate_value(context);
  99. if (str)
  100. setenv(PREFIX"VALUE", str, OVERWRITE);
  101. // Parameters
  102. populate_env_kpargv(kcontext_pargv(context), PREFIX);
  103. // Parent parameters
  104. populate_env_kpargv(kcontext_parent_pargv(context), PREFIX"PARENT_");
  105. return BOOL_TRUE;
  106. }
  107. static char *find_out_shebang(const char *script)
  108. {
  109. char *default_shebang = "/bin/sh";
  110. char *shebang = NULL;
  111. char *line = NULL;
  112. line = faux_str_getline(script, NULL);
  113. if (
  114. !line ||
  115. (strlen(line) < 2) ||
  116. (line[0] != '#') ||
  117. (line[1] != '!')
  118. )
  119. return faux_str_dup(default_shebang);
  120. shebang = faux_str_dup(line + 2);
  121. return shebang;
  122. }
  123. // Execute script
  124. int script_script(kcontext_t *context)
  125. {
  126. const char *script = NULL;
  127. pid_t cpid = -1;
  128. int res = -1;
  129. char *fifo_name = NULL;
  130. FILE *wpipe = NULL;
  131. char *command = NULL;
  132. char *shebang = NULL;
  133. script = kcontext_script(context);
  134. if (faux_str_is_empty(script))
  135. return 0;
  136. // Create FIFO
  137. if (!(fifo_name = script_mkfifo())) {
  138. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  139. "Error: The ACTION will be not executed.\n");
  140. return -1;
  141. }
  142. // Create process to write to FIFO
  143. cpid = fork();
  144. if (cpid == -1) {
  145. fprintf(stderr, "Error: Can't fork the write process.\n"
  146. "Error: The ACTION will be not executed.\n");
  147. unlink(fifo_name);
  148. faux_str_free(fifo_name);
  149. return -1;
  150. }
  151. // Child: write to FIFO
  152. if (cpid == 0) {
  153. wpipe = fopen(fifo_name, "w");
  154. if (!wpipe)
  155. _exit(-1);
  156. fwrite(script, strlen(script), 1, wpipe);
  157. fflush(wpipe);
  158. fclose(wpipe);
  159. _exit(0);
  160. }
  161. // Parent
  162. // Populate environment. Put command parameters to env vars.
  163. populate_env(context);
  164. // Prepare command
  165. shebang = find_out_shebang(script);
  166. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  167. res = system(command);
  168. // Wait for the writing process
  169. kill(cpid, SIGTERM);
  170. while (waitpid(cpid, NULL, 0) != cpid);
  171. // Clean up
  172. faux_str_free(command);
  173. unlink(fifo_name);
  174. faux_str_free(fifo_name);
  175. faux_str_free(shebang);
  176. return WEXITSTATUS(res);
  177. }