script.c 4.3 KB

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