shell.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 *shell_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. // Execute shell script
  108. int shell_shell(kcontext_t *context)
  109. {
  110. const char *script = NULL;
  111. pid_t cpid = -1;
  112. int res = -1;
  113. char *fifo_name = NULL;
  114. FILE *wpipe = NULL;
  115. char *command = NULL;
  116. script = kcontext_script(context);
  117. if (faux_str_is_empty(script))
  118. return 0;
  119. // Create FIFO
  120. if (!(fifo_name = shell_mkfifo())) {
  121. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  122. "Error: The ACTION will be not executed.\n");
  123. return -1;
  124. }
  125. // Create process to write to FIFO
  126. cpid = fork();
  127. if (cpid == -1) {
  128. fprintf(stderr, "Error: Can't fork the write process.\n"
  129. "Error: The ACTION will be not executed.\n");
  130. unlink(fifo_name);
  131. faux_str_free(fifo_name);
  132. return -1;
  133. }
  134. // Child: write to FIFO
  135. if (cpid == 0) {
  136. wpipe = fopen(fifo_name, "w");
  137. if (!wpipe)
  138. _exit(-1);
  139. fwrite(script, strlen(script), 1, wpipe);
  140. fflush(wpipe);
  141. fclose(wpipe);
  142. _exit(0);
  143. }
  144. // Parent
  145. // Populate environment. Put command parameters to env vars.
  146. populate_env(context);
  147. // Prepare command
  148. command = faux_str_sprintf("/bin/sh %s", fifo_name);
  149. res = system(command);
  150. // Wait for the writing process
  151. kill(cpid, SIGTERM);
  152. while (waitpid(cpid, NULL, 0) != cpid);
  153. // Clean up
  154. faux_str_free(command);
  155. unlink(fifo_name);
  156. faux_str_free(fifo_name);
  157. return WEXITSTATUS(res);
  158. }