shell_new.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, clish_plugin_free);
  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->machine_interface = BOOL_FALSE; /* Human oriented protocol by default. */
  59. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  60. this->log = BOOL_FALSE; /* Disable logging by default */
  61. this->log_facility = LOG_LOCAL0; /* LOCAL0 for compatibility */
  62. this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
  63. this->user = lub_db_getpwuid(getuid()); /* Get user information */
  64. this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
  65. this->canon_out = BOOL_FALSE; /* A canonical output is needed in special cases only */
  66. /* Create template (string) for FIFO name generation */
  67. snprintf(template, sizeof(template),
  68. "%s/klish.fifo.%u.XXXXXX", "/tmp", getpid());
  69. template[sizeof(template) - 1] = '\0';
  70. this->fifo_temp = lub_string_dup(template);
  71. /* Create internal ptypes and params */
  72. /* Args */
  73. tmp_ptype = clish_shell_find_create_ptype(this,
  74. "__ptype_ARGS",
  75. "Arguments", "[^\\\\]+",
  76. CLISH_PTYPE_METHOD_REGEXP,
  77. CLISH_PTYPE_PRE_NONE);
  78. assert(tmp_ptype);
  79. /* Push non-NULL istream */
  80. if (istream)
  81. clish_shell_push_fd(this, istream, stop_on_error);
  82. }
  83. /*--------------------------------------------------------- */
  84. static void clish_shell_fini(clish_shell_t *this)
  85. {
  86. clish_var_t *var;
  87. unsigned int i;
  88. /* Free all loaded plugins */
  89. lub_list_free_all(this->plugins);
  90. /* Delete each VIEW */
  91. lub_list_free_all(this->view_tree);
  92. /* Delete each PTYPE */
  93. lub_list_free_all(this->ptype_tree);
  94. /* delete each VAR held */
  95. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  96. lub_bintree_remove(&this->var_tree, var);
  97. clish_var_delete(var);
  98. }
  99. /* Free empty hooks */
  100. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  101. if (clish_sym__get_name(this->hooks[i]))
  102. continue;
  103. clish_sym_free(this->hooks[i]);
  104. }
  105. /* Free symbol list */
  106. lub_list_free_all(this->syms);
  107. /* Free user data storage */
  108. lub_list_free_all(this->udata);
  109. /* free the textual details */
  110. lub_string_free(this->overview);
  111. /* Remove the startup command */
  112. if (this->startup)
  113. clish_command_delete(this->startup);
  114. /* Remove the watchdog command */
  115. if (this->wdog)
  116. clish_command_delete(this->wdog);
  117. /* clean up the file stack */
  118. while (!clish_shell_pop_file(this));
  119. /* delete the tinyrl object */
  120. clish_shell_tinyrl_delete(this->tinyrl);
  121. /* finalize each of the pwd strings */
  122. for (i = 0; i < this->pwdc; i++) {
  123. clish_shell__fini_pwd(this->pwdv[i]);
  124. free(this->pwdv[i]);
  125. }
  126. /* free the pwd vector */
  127. free(this->pwdv);
  128. konf_client_free(this->client);
  129. lub_string_free(this->lockfile);
  130. lub_string_free(this->default_shebang);
  131. free(this->user);
  132. if (this->fifo_temp)
  133. lub_string_free(this->fifo_temp);
  134. }
  135. /*-------------------------------------------------------- */
  136. clish_shell_t *clish_shell_new(
  137. FILE * istream,
  138. FILE * ostream,
  139. bool_t stop_on_error)
  140. {
  141. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  142. if (this)
  143. clish_shell_init(this, istream, ostream, stop_on_error);
  144. return this;
  145. }
  146. /*--------------------------------------------------------- */
  147. void clish_shell_delete(clish_shell_t *this)
  148. {
  149. clish_shell_fini(this);
  150. free(this);
  151. }
  152. CLISH_GET(shell, struct passwd *, user);