shell_new.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <syslog.h>
  10. #include <limits.h>
  11. #include "lub/string.h"
  12. #include "lub/db.h"
  13. #include "lub/list.h"
  14. #include "clish/plugin.h"
  15. /*-------------------------------------------------------- */
  16. static void clish_shell_init(clish_shell_t * this,
  17. FILE * istream, FILE * ostream, bool_t stop_on_error)
  18. {
  19. clish_ptype_t *tmp_ptype = NULL;
  20. int i;
  21. char template[PATH_MAX];
  22. /* initialise the tree of views */
  23. lub_bintree_init(&this->view_tree,
  24. clish_view_bt_offset(),
  25. clish_view_bt_compare, clish_view_bt_getkey);
  26. /* Init PTYPE list */
  27. this->ptype_tree = lub_list_new(clish_ptype_compare);
  28. /* initialise the tree of vars */
  29. lub_bintree_init(&this->var_tree,
  30. clish_var_bt_offset(),
  31. clish_var_bt_compare, clish_var_bt_getkey);
  32. /* Initialize plugin list */
  33. this->plugins = lub_list_new(NULL);
  34. /* Initialise the list of unresolved (yet) symbols */
  35. this->syms = lub_list_new(clish_sym_compare);
  36. /* Create userdata storage */
  37. this->udata = lub_list_new(clish_udata_compare);
  38. assert(this->udata);
  39. /* Hooks */
  40. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  41. this->hooks[i] = clish_sym_new(NULL, NULL, i);
  42. this->hooks_use[i] = BOOL_FALSE;
  43. }
  44. /* Set up defaults */
  45. this->global = NULL;
  46. this->startup = NULL;
  47. this->idle_timeout = 0; /* No idle timeout by default */
  48. this->wdog = NULL;
  49. this->wdog_timeout = 0; /* No watchdog timeout by default */
  50. this->wdog_active = BOOL_FALSE;
  51. this->state = SHELL_STATE_INITIALISING;
  52. this->overview = NULL;
  53. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  54. this->current_file = NULL;
  55. this->pwdv = NULL;
  56. this->pwdc = 0;
  57. this->depth = -1; /* Current depth is undefined */
  58. this->client = NULL;
  59. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  60. this->default_shebang = lub_string_dup("/bin/sh");
  61. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  62. this->log = BOOL_FALSE; /* Disable logging by default */
  63. this->log_facility = LOG_LOCAL0; /* LOCAL0 for compatibility */
  64. this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
  65. this->user = lub_db_getpwuid(getuid()); /* Get user information */
  66. this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
  67. this->canon_out = BOOL_FALSE; /* A canonical output is needed in special cases only */
  68. /* Create template (string) for FIFO name generation */
  69. snprintf(template, sizeof(template),
  70. "%s/klish.fifo.%u.XXXXXX", "/tmp", getpid());
  71. template[sizeof(template) - 1] = '\0';
  72. this->fifo_temp = lub_string_dup(template);
  73. /* Create internal ptypes and params */
  74. /* Args */
  75. tmp_ptype = clish_shell_find_create_ptype(this,
  76. "__ptype_ARGS",
  77. "Arguments", "[^\\\\]+",
  78. CLISH_PTYPE_METHOD_REGEXP,
  79. CLISH_PTYPE_PRE_NONE);
  80. assert(tmp_ptype);
  81. /* Push non-NULL istream */
  82. if (istream)
  83. clish_shell_push_fd(this, istream, stop_on_error);
  84. }
  85. /*--------------------------------------------------------- */
  86. static void clish_shell_fini(clish_shell_t *this)
  87. {
  88. clish_view_t *view;
  89. clish_var_t *var;
  90. unsigned i;
  91. lub_list_node_t *iter;
  92. /* Free all loaded plugins */
  93. while ((iter = lub_list__get_head(this->plugins))) {
  94. /* Remove the symbol from the list */
  95. lub_list_del(this->plugins, iter);
  96. /* Free the instance */
  97. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter),
  98. (void *)this);
  99. lub_list_node_free(iter);
  100. }
  101. lub_list_free(this->plugins);
  102. /* delete each VIEW held */
  103. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  104. lub_bintree_remove(&this->view_tree, view);
  105. clish_view_delete(view);
  106. }
  107. /* Delete each PTYPE */
  108. while ((iter = lub_list__get_head(this->ptype_tree))) {
  109. lub_list_del(this->ptype_tree, iter);
  110. clish_ptype_free((clish_ptype_t *)lub_list_node__get_data(iter));
  111. lub_list_node_free(iter);
  112. }
  113. lub_list_free(this->ptype_tree);
  114. /* delete each VAR held */
  115. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  116. lub_bintree_remove(&this->var_tree, var);
  117. clish_var_delete(var);
  118. }
  119. /* Free empty hooks */
  120. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  121. if (clish_sym__get_name(this->hooks[i]))
  122. continue;
  123. clish_sym_free(this->hooks[i]);
  124. }
  125. /* Free symbol list */
  126. while ((iter = lub_list__get_head(this->syms))) {
  127. /* Remove the symbol from the list */
  128. lub_list_del(this->syms, iter);
  129. /* Free the instance */
  130. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  131. lub_list_node_free(iter);
  132. }
  133. lub_list_free(this->syms);
  134. /* Free user data storage */
  135. while ((iter = lub_list__get_head(this->udata))) {
  136. /* Remove the symbol from the list */
  137. lub_list_del(this->udata, iter);
  138. /* Free the instance */
  139. clish_udata_free((clish_udata_t *)lub_list_node__get_data(iter));
  140. lub_list_node_free(iter);
  141. }
  142. lub_list_free(this->udata);
  143. /* free the textual details */
  144. lub_string_free(this->overview);
  145. /* Remove the startup command */
  146. if (this->startup)
  147. clish_command_delete(this->startup);
  148. /* Remove the watchdog command */
  149. if (this->wdog)
  150. clish_command_delete(this->wdog);
  151. /* clean up the file stack */
  152. while (!clish_shell_pop_file(this));
  153. /* delete the tinyrl object */
  154. clish_shell_tinyrl_delete(this->tinyrl);
  155. /* finalize each of the pwd strings */
  156. for (i = 0; i < this->pwdc; i++) {
  157. clish_shell__fini_pwd(this->pwdv[i]);
  158. free(this->pwdv[i]);
  159. }
  160. /* free the pwd vector */
  161. free(this->pwdv);
  162. konf_client_free(this->client);
  163. lub_string_free(this->lockfile);
  164. lub_string_free(this->default_shebang);
  165. free(this->user);
  166. if (this->fifo_temp)
  167. lub_string_free(this->fifo_temp);
  168. }
  169. /*-------------------------------------------------------- */
  170. clish_shell_t *clish_shell_new(
  171. FILE * istream,
  172. FILE * ostream,
  173. bool_t stop_on_error)
  174. {
  175. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  176. if (this)
  177. clish_shell_init(this, istream, ostream, stop_on_error);
  178. return this;
  179. }
  180. /*--------------------------------------------------------- */
  181. void clish_shell_delete(clish_shell_t *this)
  182. {
  183. clish_shell_fini(this);
  184. free(this);
  185. }
  186. CLISH_GET(shell, struct passwd *, user);