ksession.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. };
  22. // Scheme
  23. KGET(session, kscheme_t *, scheme);
  24. // Path
  25. KGET(session, kpath_t *, path);
  26. // Done
  27. KGET_BOOL(session, done);
  28. KSET_BOOL(session, done);
  29. // Width of pseudo terminal
  30. KGET(session, size_t, term_width);
  31. KSET(session, size_t, term_width);
  32. // Height of pseudo terminal
  33. KGET(session, size_t, term_height);
  34. KSET(session, size_t, term_height);
  35. // PID of client (Unix socket peer)
  36. KGET(session, pid_t, pid);
  37. KSET(session, pid_t, pid);
  38. // UID of client (Unix socket peer)
  39. KGET(session, uid_t, uid);
  40. KSET(session, uid_t, uid);
  41. // Client user name (Unix socket peer)
  42. KSET_STR(session, user);
  43. KGET_STR(session, user);
  44. ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
  45. {
  46. ksession_t *session = NULL;
  47. const kentry_t *entry = NULL;
  48. const char *entry_to_search = NULL;
  49. klevel_t *level = NULL;
  50. assert(scheme);
  51. if (!scheme)
  52. return NULL;
  53. // Before real session allocation we will try to find starting entry.
  54. // Starting entry can be get from function argument, from STARTUP tag or
  55. // default name 'main' can be used. Don't create session if we can't get
  56. // starting entry at all. Priorities are (from higher) argument, STARTUP,
  57. // default name.
  58. if (start_entry)
  59. entry_to_search = start_entry;
  60. // STARTUP is not implemented yet
  61. else
  62. entry_to_search = KSESSION_STARTING_ENTRY;
  63. entry = kscheme_find_entry_by_path(scheme, entry_to_search);
  64. if (!entry)
  65. return NULL; // Can't find starting entry
  66. session = faux_zmalloc(sizeof(*session));
  67. assert(session);
  68. if (!session)
  69. return NULL;
  70. // Initialization
  71. session->scheme = scheme;
  72. // Create kpath_t stack
  73. session->path = kpath_new();
  74. assert(session->path);
  75. level = klevel_new(entry);
  76. assert(level);
  77. kpath_push(session->path, level);
  78. session->done = BOOL_FALSE;
  79. session->term_width = 0;
  80. session->term_height = 0;
  81. // Peer data
  82. session->pid = -1;
  83. session->uid = -1;
  84. session->user = NULL;
  85. return session;
  86. }
  87. void ksession_free(ksession_t *session)
  88. {
  89. if (!session)
  90. return;
  91. kpath_free(session->path);
  92. faux_str_free(session->user);
  93. free(session);
  94. }