klishd.c 11 KB

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