klishd.c 11 KB

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