klishd.c 11 KB

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