klishd.c 11 KB

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