klishd.c 11 KB

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