klishd.c 10 KB

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