ksession.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /** @file ksession.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kscheme.h>
  10. #include <klish/kpath.h>
  11. #include <klish/ksession.h>
  12. struct ksession_s {
  13. kscheme_t *scheme;
  14. kpath_t *path;
  15. bool_t done; // Indicates that session is over and must be closed
  16. size_t term_width;
  17. size_t term_height;
  18. pid_t pid;
  19. uid_t uid;
  20. char *user;
  21. bool_t isatty_stdin;
  22. bool_t isatty_stdout;
  23. bool_t isatty_stderr;
  24. };
  25. // Scheme
  26. KGET(session, kscheme_t *, scheme);
  27. // Path
  28. KGET(session, kpath_t *, path);
  29. // Done
  30. KGET_BOOL(session, done);
  31. KSET_BOOL(session, done);
  32. // Width of pseudo terminal
  33. KGET(session, size_t, term_width);
  34. KSET(session, size_t, term_width);
  35. // Height of pseudo terminal
  36. KGET(session, size_t, term_height);
  37. KSET(session, size_t, term_height);
  38. // PID of client (Unix socket peer)
  39. KGET(session, pid_t, pid);
  40. KSET(session, pid_t, pid);
  41. // UID of client (Unix socket peer)
  42. KGET(session, uid_t, uid);
  43. KSET(session, uid_t, uid);
  44. // Client user name (Unix socket peer)
  45. KSET_STR(session, user);
  46. KGET_STR(session, user);
  47. // isatty
  48. KGET_BOOL(session, isatty_stdin);
  49. KSET_BOOL(session, isatty_stdin);
  50. KGET_BOOL(session, isatty_stdout);
  51. KSET_BOOL(session, isatty_stdout);
  52. KGET_BOOL(session, isatty_stderr);
  53. KSET_BOOL(session, isatty_stderr);
  54. ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
  55. {
  56. ksession_t *session = NULL;
  57. const kentry_t *entry = NULL;
  58. const char *entry_to_search = NULL;
  59. klevel_t *level = NULL;
  60. assert(scheme);
  61. if (!scheme)
  62. return NULL;
  63. // Before real session allocation we will try to find starting entry.
  64. // Starting entry can be get from function argument, from STARTUP tag or
  65. // default name 'main' can be used. Don't create session if we can't get
  66. // starting entry at all. Priorities are (from higher) argument, STARTUP,
  67. // default name.
  68. if (start_entry)
  69. entry_to_search = start_entry;
  70. // STARTUP is not implemented yet
  71. else
  72. entry_to_search = KSESSION_STARTING_ENTRY;
  73. entry = kscheme_find_entry_by_path(scheme, entry_to_search);
  74. if (!entry)
  75. return NULL; // Can't find starting entry
  76. session = faux_zmalloc(sizeof(*session));
  77. assert(session);
  78. if (!session)
  79. return NULL;
  80. // Initialization
  81. session->scheme = scheme;
  82. // Create kpath_t stack
  83. session->path = kpath_new();
  84. assert(session->path);
  85. level = klevel_new(entry);
  86. assert(level);
  87. kpath_push(session->path, level);
  88. session->done = BOOL_FALSE;
  89. session->term_width = 0;
  90. session->term_height = 0;
  91. // Peer data
  92. session->pid = -1;
  93. session->uid = -1;
  94. session->user = NULL;
  95. session->isatty_stdin = BOOL_FALSE;
  96. session->isatty_stdout = BOOL_FALSE;
  97. session->isatty_stderr = BOOL_FALSE;
  98. return session;
  99. }
  100. void ksession_free(ksession_t *session)
  101. {
  102. if (!session)
  103. return;
  104. kpath_free(session->path);
  105. faux_str_free(session->user);
  106. free(session);
  107. }