shell_new.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /* Create template (string) for FIFO name generation */
  70. snprintf(template, sizeof(template),
  71. "/tmp/klish.fifo.%u.XXXXXX", getpid());
  72. template[sizeof(template) - 1] = '\0';
  73. this->fifo_temp = lub_string_dup(template);
  74. /* Create internal ptypes and params */
  75. /* Args */
  76. tmp_ptype = clish_shell_find_create_ptype(this,
  77. "__ptype_ARGS",
  78. "Arguments", "[^\\\\]+",
  79. CLISH_PTYPE_REGEXP,
  80. CLISH_PTYPE_NONE);
  81. assert(tmp_ptype);
  82. /* Push non-NULL istream */
  83. if (istream)
  84. clish_shell_push_fd(this, istream, stop_on_error);
  85. }
  86. /*--------------------------------------------------------- */
  87. static void clish_shell_fini(clish_shell_t *this)
  88. {
  89. clish_view_t *view;
  90. clish_ptype_t *ptype;
  91. clish_var_t *var;
  92. unsigned i;
  93. lub_list_node_t *iter;
  94. /* Free all loaded plugins */
  95. while ((iter = lub_list__get_head(this->plugins))) {
  96. /* Remove the symbol from the list */
  97. lub_list_del(this->plugins, iter);
  98. /* Free the instance */
  99. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter),
  100. (void *)this);
  101. lub_list_node_free(iter);
  102. }
  103. lub_list_free(this->plugins);
  104. /* delete each VIEW held */
  105. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  106. lub_bintree_remove(&this->view_tree, view);
  107. clish_view_delete(view);
  108. }
  109. /* delete each PTYPE held */
  110. while ((ptype = lub_bintree_findfirst(&this->ptype_tree))) {
  111. lub_bintree_remove(&this->ptype_tree, ptype);
  112. clish_ptype_delete(ptype);
  113. }
  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. /*--------------------------------------------------------- */
  187. struct passwd *clish_shell__get_user(clish_shell_t * this)
  188. {
  189. return this->user;
  190. }
  191. /*-------------------------------------------------------- */