script.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. char *name = NULL;
  22. name = faux_str_sprintf("/tmp/klish.fifo.%u.XXXXXX", getpid());
  23. mktemp(name);
  24. if (mkfifo(name, 0600) < 0) {
  25. faux_str_free(name);
  26. return NULL;
  27. }
  28. return name;
  29. }
  30. const char *kcontext_type_e_str[] = {
  31. "none",
  32. "plugin_init",
  33. "plugin_fini",
  34. "action",
  35. "service_action"
  36. };
  37. #define PREFIX "KLISH_"
  38. #define OVERWRITE 1
  39. static bool_t populate_env_kpargv(const kpargv_t *pargv, const char *prefix)
  40. {
  41. const kentry_t *entry = NULL;
  42. kpargv_pargs_node_t *iter = NULL;
  43. kparg_t *parg = NULL;
  44. const kentry_t *saved_entry = NULL;
  45. size_t num = 0;
  46. if (!pargv)
  47. return BOOL_FALSE;
  48. // Command
  49. entry = kpargv_command(pargv);
  50. if (entry) {
  51. char *var = faux_str_sprintf("%sCOMMAND", prefix);
  52. setenv(var, kentry_name(entry), OVERWRITE);
  53. faux_str_free(var);
  54. }
  55. // Parameters
  56. iter = kpargv_pargs_iter(pargv);
  57. while ((parg = kpargv_pargs_each(&iter))) {
  58. char *var = NULL;
  59. entry = kparg_entry(parg);
  60. if (kentry_max(entry) > 1) { // Multi
  61. if (entry == saved_entry)
  62. num++;
  63. else
  64. num = 0;
  65. var = faux_str_sprintf("%sPARAM_%s_%u",
  66. prefix, kentry_name(entry), num);
  67. saved_entry = entry;
  68. } else { // Single
  69. var = faux_str_sprintf("%sPARAM_%s",
  70. prefix, kentry_name(entry));
  71. saved_entry = NULL;
  72. num = 0;
  73. }
  74. setenv(var, kparg_value(parg), OVERWRITE);
  75. faux_str_free(var);
  76. }
  77. return BOOL_TRUE;
  78. }
  79. static bool_t populate_env(kcontext_t *context)
  80. {
  81. kcontext_type_e type = KCONTEXT_TYPE_NONE;
  82. const kentry_t *entry = NULL;
  83. const char *str = NULL;
  84. assert(context);
  85. // Type
  86. type = kcontext_type(context);
  87. if (type >= KCONTEXT_TYPE_MAX)
  88. type = KCONTEXT_TYPE_NONE;
  89. setenv(PREFIX"TYPE", kcontext_type_e_str[type], OVERWRITE);
  90. // Candidate
  91. entry = kcontext_candidate_entry(context);
  92. if (entry)
  93. setenv(PREFIX"CANDIDATE", kentry_name(entry), OVERWRITE);
  94. // Value
  95. str = kcontext_candidate_value(context);
  96. if (str)
  97. setenv(PREFIX"VALUE", str, OVERWRITE);
  98. // Parameters
  99. populate_env_kpargv(kcontext_pargv(context), PREFIX);
  100. // Parent parameters
  101. populate_env_kpargv(kcontext_parent_pargv(context), PREFIX"PARENT_");
  102. return BOOL_TRUE;
  103. }
  104. static char *find_out_shebang(const char *script)
  105. {
  106. char *default_shebang = "/bin/sh";
  107. char *shebang = NULL;
  108. char *line = NULL;
  109. line = faux_str_getline(script, NULL);
  110. if (
  111. !line ||
  112. (strlen(line) < 2) ||
  113. (line[0] != '#') ||
  114. (line[1] != '!')
  115. ) {
  116. faux_str_free(line);
  117. return faux_str_dup(default_shebang);
  118. }
  119. shebang = faux_str_dup(line + 2);
  120. faux_str_free(line);
  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. faux_str_free(fifo_name);
  155. if (!wpipe)
  156. _exit(-1);
  157. fwrite(script, strlen(script), 1, wpipe);
  158. fflush(wpipe);
  159. fclose(wpipe);
  160. _exit(0);
  161. }
  162. // Parent
  163. // Populate environment. Put command parameters to env vars.
  164. populate_env(context);
  165. // Prepare command
  166. shebang = find_out_shebang(script);
  167. command = faux_str_sprintf("%s %s", shebang, fifo_name);
  168. res = system(command);
  169. // Wait for the writing process
  170. kill(cpid, SIGTERM);
  171. while (waitpid(cpid, NULL, 0) != cpid);
  172. // Clean up
  173. faux_str_free(command);
  174. unlink(fifo_name);
  175. faux_str_free(fifo_name);
  176. faux_str_free(shebang);
  177. return WEXITSTATUS(res);
  178. }