klishd.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <signal.h>
  8. #include <syslog.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include <sys/fsuid.h>
  17. #include <sys/wait.h>
  18. #include <poll.h>
  19. #include <time.h>
  20. #include <faux/faux.h>
  21. #include <faux/str.h>
  22. #include <faux/ini.h>
  23. #include <faux/log.h>
  24. #include <faux/sched.h>
  25. #include <faux/sysdb.h>
  26. #include <faux/net.h>
  27. #include <faux/list.h>
  28. #include <faux/conv.h>
  29. #include <faux/file.h>
  30. #include <faux/eloop.h>
  31. #include <klish/ktp.h>
  32. #include <klish/ktp_session.h>
  33. #include "private.h"
  34. // Local static functions
  35. static int create_listen_unix_sock(const char *path);
  36. // Main loop events
  37. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  38. void *associated_data, void *user_data);
  39. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  40. void *associated_data, void *user_data);
  41. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  42. void *associated_data, void *user_data);
  43. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  44. void *associated_data, void *user_data);
  45. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  46. void *associated_data, void *user_data);
  47. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  48. void *associated_data, void *user_data);
  49. /** @brief Main function
  50. */
  51. int main(int argc, char **argv)
  52. {
  53. int retval = -1;
  54. struct options *opts = NULL;
  55. int pidfd = -1;
  56. int logoptions = 0;
  57. faux_eloop_t *eloop = NULL;
  58. int listen_unix_sock = -1;
  59. ktpd_clients_t *clients = NULL;
  60. struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
  61. struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
  62. // Parse command line options
  63. opts = opts_init();
  64. if (opts_parse(argc, argv, opts))
  65. goto err;
  66. // Initialize syslog
  67. logoptions = LOG_CONS;
  68. if (opts->foreground)
  69. logoptions |= LOG_PERROR;
  70. openlog(LOG_NAME, logoptions, opts->log_facility);
  71. if (!opts->verbose)
  72. setlogmask(LOG_UPTO(LOG_INFO));
  73. // Parse config file
  74. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  75. if (!access(opts->cfgfile, R_OK)) {
  76. if (config_parse(opts->cfgfile, opts))
  77. goto err;
  78. } else if (opts->cfgfile_userdefined) {
  79. // User defined config must be found
  80. fprintf(stderr, "Error: Can't find config file %s\n",
  81. opts->cfgfile);
  82. goto err;
  83. }
  84. // DEBUG: Show options
  85. opts_show(opts);
  86. syslog(LOG_INFO, "Start daemon.\n");
  87. // Fork the daemon
  88. if (!opts->foreground) {
  89. // Daemonize
  90. syslog(LOG_DEBUG, "Daemonize\n");
  91. if (daemon(0, 0) < 0) {
  92. syslog(LOG_ERR, "Can't daemonize\n");
  93. goto err;
  94. }
  95. // Write pidfile
  96. syslog(LOG_DEBUG, "Write PID file: %s\n", opts->pidfile);
  97. if ((pidfd = open(opts->pidfile,
  98. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  99. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  100. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  101. opts->pidfile, strerror(errno));
  102. } else {
  103. char str[20];
  104. snprintf(str, sizeof(str), "%u\n", getpid());
  105. str[sizeof(str) - 1] = '\0';
  106. if (write(pidfd, str, strlen(str)) < 0)
  107. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  108. opts->pidfile, strerror(errno));
  109. close(pidfd);
  110. }
  111. }
  112. // Listen socket
  113. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  114. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  115. if (listen_unix_sock < 0)
  116. goto err;
  117. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  118. // Clients sessions DB
  119. clients = ktpd_clients_new();
  120. assert(clients);
  121. if (!clients)
  122. goto err;
  123. // Event loop
  124. eloop = faux_eloop_new(NULL);
  125. // Signals
  126. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  127. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  128. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  129. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  130. // Listen socket. Waiting for new connections
  131. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN, listen_socket_ev, clients);
  132. // Scheduled events
  133. faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  134. faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  135. // Main loop
  136. faux_eloop_loop(eloop);
  137. faux_eloop_free(eloop);
  138. /*
  139. // Non-blocking wait for all children
  140. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
  141. syslog(LOG_DEBUG, "Exit child process %d\n", pid);
  142. }
  143. */
  144. retval = 0;
  145. err:
  146. syslog(LOG_DEBUG, "Cleanup.\n");
  147. ktpd_clients_free(clients);
  148. // Close listen socket
  149. if (listen_unix_sock >= 0)
  150. close(listen_unix_sock);
  151. // Remove pidfile
  152. if (pidfd >= 0) {
  153. if (unlink(opts->pidfile) < 0) {
  154. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  155. opts->pidfile, strerror(errno));
  156. }
  157. }
  158. // Free command line options
  159. opts_free(opts);
  160. syslog(LOG_INFO, "Stop daemon.\n");
  161. return retval;
  162. }
  163. /** @brief Create listen socket
  164. *
  165. * Previously removes old socket's file from filesystem. Note daemon must check
  166. * for already working daemon to don't duplicate.
  167. *
  168. * @param [in] path Socket path within filesystem.
  169. * @return Socket descriptor of < 0 on error.
  170. */
  171. static int create_listen_unix_sock(const char *path)
  172. {
  173. int sock = -1;
  174. int opt = 1;
  175. struct sockaddr_un laddr = {};
  176. assert(path);
  177. if (!path)
  178. return -1;
  179. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  180. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  181. goto err;
  182. }
  183. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  184. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  185. goto err;
  186. }
  187. // Remove old (lost) socket's file
  188. unlink(path);
  189. laddr.sun_family = AF_UNIX;
  190. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  191. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  192. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  193. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  194. goto err;
  195. }
  196. if (listen(sock, 128)) {
  197. unlink(path);
  198. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  199. goto err;
  200. }
  201. return sock;
  202. err:
  203. if (sock >= 0)
  204. close(sock);
  205. return -1;
  206. }
  207. /** @brief Stop main event loop.
  208. */
  209. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  210. void *associated_data, void *user_data)
  211. {
  212. // Happy compiler
  213. eloop = eloop;
  214. type = type;
  215. associated_data = associated_data;
  216. user_data = user_data;
  217. return BOOL_FALSE; // Stop Event Loop
  218. }
  219. /** @brief Re-read config file.
  220. */
  221. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  222. void *associated_data, void *user_data)
  223. {
  224. struct options *opts = (struct options *)user_data;
  225. if (access(opts->cfgfile, R_OK) == 0) {
  226. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  227. if (config_parse(opts->cfgfile, opts) < 0)
  228. syslog(LOG_ERR, "Error while config file parsing.\n");
  229. } else if (opts->cfgfile_userdefined) {
  230. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  231. }
  232. // Happy compiler
  233. eloop = eloop;
  234. type = type;
  235. associated_data = associated_data;
  236. return BOOL_TRUE;
  237. }
  238. /** @brief Event on listen socket. New remote client.
  239. */
  240. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  241. void *associated_data, void *user_data)
  242. {
  243. int new_conn = -1;
  244. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  245. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  246. ktpd_session_t *session = NULL;
  247. assert(clients);
  248. new_conn = accept(info->fd, NULL, NULL);
  249. if (new_conn < 0) {
  250. syslog(LOG_ERR, "Can't accept() new connection");
  251. return BOOL_TRUE;
  252. }
  253. session = ktpd_clients_add(clients, new_conn);
  254. if (!session) {
  255. syslog(LOG_ERR, "Duplicated client fd");
  256. close(new_conn);
  257. return BOOL_TRUE;
  258. }
  259. faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  260. syslog(LOG_DEBUG, "New connection %d", new_conn);
  261. type = type; // Happy compiler
  262. user_data = user_data; // Happy compiler
  263. return BOOL_TRUE;
  264. }
  265. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  266. void *associated_data, void *user_data)
  267. {
  268. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  269. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  270. ktpd_session_t *session = NULL;
  271. assert(clients);
  272. // Find out session
  273. session = ktpd_clients_find(clients, info->fd);
  274. if (!session) { // Some strange case
  275. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  276. faux_eloop_del_fd(eloop, info->fd);
  277. close(info->fd);
  278. return BOOL_TRUE;
  279. }
  280. // Read data
  281. if (info->revents & POLLIN) {
  282. if (!ktpd_session_async_in(session)) {
  283. // Someting went wrong
  284. faux_eloop_del_fd(eloop, info->fd);
  285. ktpd_clients_del(clients, info->fd);
  286. syslog(LOG_ERR, "Problem with async input");
  287. }
  288. }
  289. // EOF
  290. if (info->revents & POLLHUP) {
  291. faux_eloop_del_fd(eloop, info->fd);
  292. ktpd_clients_del(clients, info->fd);
  293. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  294. }
  295. type = type; // Happy compiler
  296. user_data = user_data; // Happy compiler
  297. return BOOL_TRUE;
  298. }
  299. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  300. void *associated_data, void *user_data)
  301. {
  302. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  303. printf("Once %d\n", info->ev_id);
  304. // Happy compiler
  305. eloop = eloop;
  306. type = type;
  307. associated_data = associated_data;
  308. user_data = user_data;
  309. return BOOL_TRUE;
  310. }
  311. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  312. void *associated_data, void *user_data)
  313. {
  314. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  315. printf("Periodic %d\n", info->ev_id);
  316. // Happy compiler
  317. eloop = eloop;
  318. type = type;
  319. associated_data = associated_data;
  320. user_data = user_data;
  321. return BOOL_TRUE;
  322. }