shell_new.c 5.2 KB

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