ksession.c 3.1 KB

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