shell_new.c 6.1 KB

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