shell_new.c 5.0 KB

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