klishd.c 11 KB

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