klishd.c 11 KB

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