klishd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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/argv.h>
  23. #include <faux/ini.h>
  24. #include <faux/log.h>
  25. #include <faux/sched.h>
  26. #include <faux/sysdb.h>
  27. #include <faux/net.h>
  28. #include <faux/list.h>
  29. #include <faux/conv.h>
  30. #include <faux/file.h>
  31. #include <faux/eloop.h>
  32. #include <faux/error.h>
  33. #include <klish/ktp.h>
  34. #include <klish/ktp_session.h>
  35. #include <klish/kscheme.h>
  36. #include <klish/ischeme.h>
  37. #include <klish/kcontext.h>
  38. #include <klish/ksession.h>
  39. #include <klish/kdb.h>
  40. #include <klish/kpargv.h>
  41. #include "private.h"
  42. // Local static functions
  43. static int create_listen_unix_sock(const char *path);
  44. static kscheme_t *load_all_dbs(const char *dbs,
  45. faux_ini_t *global_config, faux_error_t *error);
  46. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error);
  47. // Main loop events
  48. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  49. void *associated_data, void *user_data);
  50. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  51. void *associated_data, void *user_data);
  52. //static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  53. // void *associated_data, void *user_data);
  54. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  55. void *associated_data, void *user_data);
  56. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  57. void *associated_data, void *user_data);
  58. //static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  59. // void *associated_data, void *user_data);
  60. //static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  61. // void *associated_data, void *user_data);
  62. /** @brief Main function
  63. */
  64. int main(int argc, char **argv)
  65. {
  66. int retval = -1;
  67. struct options *opts = NULL;
  68. int pidfd = -1;
  69. int logoptions = 0;
  70. faux_eloop_t *eloop = NULL;
  71. int listen_unix_sock = -1;
  72. ktpd_session_t *ktpd_session = NULL;
  73. kscheme_t *scheme = NULL;
  74. ksession_t *session = NULL;
  75. faux_error_t *error = faux_error_new();
  76. faux_ini_t *config = NULL;
  77. int client_fd = -1;
  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 = 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. if (!(scheme = load_all_dbs(opts->dbs, config, error))) {
  132. fprintf(stderr, "Scheme errors:\n");
  133. goto err;
  134. }
  135. // Parsing
  136. {
  137. // const char *line = "cmd o4 m7 o2 e1";
  138. const char *line = "cmd o4 o4 o4 m3 o2";
  139. kpargv_t *pargv = NULL;
  140. kpargv_pargs_node_t *p_iter = NULL;
  141. session = ksession_new(scheme, "/lowview");
  142. kpath_push(ksession_path(session), klevel_new(kscheme_find_entry_by_path(scheme, "/main")));
  143. pargv = ksession_parse_line(session, line, KPURPOSE_COMPLETION);
  144. if (pargv) {
  145. printf("Level: %lu, Command: %s, Line '%s': %s\n",
  146. kpargv_level(pargv),
  147. kpargv_command(pargv) ? kentry_name(kpargv_command(pargv)) : "<none>",
  148. line,
  149. kpargv_status_str(pargv));
  150. kparg_t *parg = NULL;
  151. p_iter = kpargv_pargs_iter(pargv);
  152. if (kpargv_pargs_len(pargv) > 0) {
  153. while ((parg = kpargv_pargs_each(&p_iter))) {
  154. printf("%s(%s) ", kparg_value(parg), kentry_name(kparg_entry(parg)));
  155. }
  156. printf("\n");
  157. }
  158. // Completions
  159. if (!kpargv_completions_is_empty(pargv)) {
  160. kentry_t *completion = NULL;
  161. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  162. printf("Completions (%s):\n", kpargv_last_arg(pargv));
  163. while ((completion = kpargv_completions_each(&citer)))
  164. printf("* %s\n", kentry_name(completion));
  165. }
  166. }
  167. kpargv_free(pargv);
  168. ksession_free(session);
  169. }
  170. // Listen socket
  171. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  172. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  173. if (listen_unix_sock < 0)
  174. goto err;
  175. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  176. // Event loop
  177. eloop = faux_eloop_new(NULL);
  178. // Signals
  179. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  180. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  181. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  182. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  183. faux_eloop_add_signal(eloop, SIGHUP, wait_for_child_ev, NULL);
  184. // Listen socket. Waiting for new connections
  185. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN,
  186. listen_socket_ev, &client_fd);
  187. // Scheduled events
  188. // faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  189. // faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  190. // Main loop
  191. faux_eloop_loop(eloop);
  192. faux_eloop_free(eloop);
  193. retval = 0;
  194. err:
  195. // Print errors
  196. if (faux_error_len(error) > 0)
  197. faux_error_show(error);
  198. faux_error_free(error);
  199. // Close listen socket
  200. if (listen_unix_sock >= 0)
  201. close(listen_unix_sock);
  202. // Finish listen daemon if it's not forked service process.
  203. if (client_fd < 0) {
  204. // Free scheme
  205. clear_scheme(scheme, error);
  206. // Free command line options
  207. opts_free(opts);
  208. faux_ini_free(config);
  209. // Remove pidfile
  210. if (pidfd >= 0) {
  211. if (unlink(opts->pidfile) < 0) {
  212. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  213. opts->pidfile, strerror(errno));
  214. }
  215. }
  216. syslog(LOG_INFO, "Stop daemon.\n");
  217. return retval;
  218. }
  219. // ATTENTION: It's a forked service process
  220. retval = -1; // Pessimism for service process
  221. ktpd_session = ktpd_session_new(client_fd, scheme, NULL);
  222. assert(ktpd_session);
  223. if (!ktpd_session) {
  224. syslog(LOG_ERR, "Can't create KTPd session\n");
  225. close(client_fd);
  226. goto err_client;
  227. }
  228. // ktpd_session_set_stall_cb(ktpd_session, fd_stall_cb, eloop);
  229. // faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  230. syslog(LOG_DEBUG, "New connection %d\n", client_fd);
  231. retval = 0;
  232. err_client:
  233. ktpd_session_free(ktpd_session);
  234. // Free scheme
  235. clear_scheme(scheme, error);
  236. // Free command line options
  237. opts_free(opts);
  238. faux_ini_free(config);
  239. return retval;
  240. }
  241. static bool_t load_db(kscheme_t *scheme, const char *db_name,
  242. faux_ini_t *config, faux_error_t *error)
  243. {
  244. kdb_t *db = NULL;
  245. const char *sofile = NULL;
  246. assert(scheme);
  247. if (!scheme)
  248. return BOOL_FALSE;
  249. assert(db_name);
  250. if (!db_name)
  251. return BOOL_FALSE;
  252. // DB.libxml2.so = <so filename>
  253. if (config)
  254. sofile = faux_ini_find(config, "so");
  255. db = kdb_new(db_name, sofile);
  256. assert(db);
  257. if (!db)
  258. return BOOL_FALSE;
  259. kdb_set_ini(db, config);
  260. kdb_set_error(db, error);
  261. // Load DB plugin
  262. if (!kdb_load_plugin(db)) {
  263. faux_error_sprintf(error,
  264. "DB \"%s\": Can't load DB plugin", db_name);
  265. kdb_free(db);
  266. return BOOL_FALSE;
  267. }
  268. // Check plugin API version
  269. if ((kdb_major(db) != KDB_MAJOR) ||
  270. (kdb_minor(db) != KDB_MINOR)) {
  271. faux_error_sprintf(error,
  272. "DB \"%s\": Plugin's API version is %u.%u, need %u.%u",
  273. db_name,
  274. kdb_major(db), kdb_minor(db),
  275. KDB_MAJOR, KDB_MINOR);
  276. kdb_free(db);
  277. return BOOL_FALSE;
  278. }
  279. // Init plugin
  280. if (kdb_has_init_fn(db) && !kdb_init(db)) {
  281. faux_error_sprintf(error,
  282. "DB \"%s\": Can't init DB plugin", db_name);
  283. kdb_free(db);
  284. return BOOL_FALSE;
  285. }
  286. // Load scheme
  287. if (!kdb_has_load_fn(db) || !kdb_load_scheme(db, scheme)) {
  288. faux_error_sprintf(error,
  289. "DB \"%s\": Can't load scheme from DB plugin", db_name);
  290. kdb_fini(db);
  291. kdb_free(db);
  292. return BOOL_FALSE;
  293. }
  294. // Fini plugin
  295. if (kdb_has_fini_fn(db) && !kdb_fini(db)) {
  296. faux_error_sprintf(error,
  297. "DB \"%s\": Can't fini DB plugin", db_name);
  298. kdb_free(db);
  299. return BOOL_FALSE;
  300. }
  301. kdb_free(db);
  302. return BOOL_TRUE;
  303. }
  304. static kscheme_t *load_all_dbs(const char *dbs,
  305. faux_ini_t *global_config, faux_error_t *error)
  306. {
  307. kscheme_t *scheme = NULL;
  308. faux_argv_t *dbs_argv = NULL;
  309. faux_argv_node_t *iter = NULL;
  310. const char *db_name = NULL;
  311. bool_t retcode = BOOL_TRUE;
  312. kcontext_t *context = NULL;
  313. assert(dbs);
  314. if (!dbs)
  315. return NULL;
  316. scheme = kscheme_new();
  317. assert(scheme);
  318. if (!scheme)
  319. return NULL;
  320. dbs_argv = faux_argv_new();
  321. assert(dbs_argv);
  322. if (!dbs_argv) {
  323. kscheme_free(scheme);
  324. return NULL;
  325. }
  326. if (faux_argv_parse(dbs_argv, dbs) <= 0) {
  327. kscheme_free(scheme);
  328. faux_argv_free(dbs_argv);
  329. return NULL;
  330. }
  331. // For each DB
  332. iter = faux_argv_iter(dbs_argv);
  333. while ((db_name = faux_argv_each(&iter))) {
  334. faux_ini_t *config = NULL; // Sub-config for current DB
  335. char *prefix = NULL;
  336. prefix = faux_str_mcat(&prefix, "DB.", db_name, ".", NULL);
  337. if (config)
  338. config = faux_ini_extract_subini(global_config, prefix);
  339. if (!load_db(scheme, db_name, config, error))
  340. retcode = BOOL_FALSE;
  341. faux_ini_free(config);
  342. faux_str_free(prefix);
  343. }
  344. faux_argv_free(dbs_argv);
  345. // Something went wrong while loading DBs
  346. if (!retcode) {
  347. kscheme_free(scheme);
  348. return NULL;
  349. }
  350. // Prepare scheme
  351. context = kcontext_new(KCONTEXT_PLUGIN_INIT);
  352. retcode = kscheme_prepare(scheme, context, error);
  353. kcontext_free(context);
  354. if (!retcode) {
  355. kscheme_free(scheme);
  356. faux_error_sprintf(error, "Scheme preparing errors.\n");
  357. return NULL;
  358. }
  359. /*
  360. // Debug
  361. {
  362. kdb_t *deploy_db = NULL;
  363. // Deploy (for testing purposes)
  364. deploy_db = kdb_new("ischeme", NULL);
  365. kdb_load_plugin(deploy_db);
  366. kdb_init(deploy_db);
  367. kdb_deploy_scheme(deploy_db, scheme);
  368. kdb_fini(deploy_db);
  369. kdb_free(deploy_db);
  370. }
  371. */
  372. return scheme;
  373. }
  374. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error)
  375. {
  376. kcontext_t *context = NULL;
  377. if (!scheme)
  378. return BOOL_TRUE; // It's not an error
  379. context = kcontext_new(KCONTEXT_PLUGIN_FINI);
  380. kscheme_fini(scheme, context, error);
  381. kcontext_free(context);
  382. kscheme_free(scheme);
  383. return BOOL_TRUE;
  384. }
  385. /** @brief Create listen socket
  386. *
  387. * Previously removes old socket's file from filesystem. Note daemon must check
  388. * for already working daemon to don't duplicate.
  389. *
  390. * @param [in] path Socket path within filesystem.
  391. * @return Socket descriptor of < 0 on error.
  392. */
  393. static int create_listen_unix_sock(const char *path)
  394. {
  395. int sock = -1;
  396. int opt = 1;
  397. struct sockaddr_un laddr = {};
  398. assert(path);
  399. if (!path)
  400. return -1;
  401. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  402. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  403. goto err;
  404. }
  405. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  406. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  407. goto err;
  408. }
  409. // Remove old (lost) socket's file
  410. unlink(path);
  411. laddr.sun_family = AF_UNIX;
  412. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  413. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  414. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  415. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  416. goto err;
  417. }
  418. if (listen(sock, 128)) {
  419. unlink(path);
  420. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  421. goto err;
  422. }
  423. return sock;
  424. err:
  425. if (sock >= 0)
  426. close(sock);
  427. return -1;
  428. }
  429. /** @brief Stop main event loop.
  430. */
  431. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  432. void *associated_data, void *user_data)
  433. {
  434. // Happy compiler
  435. eloop = eloop;
  436. type = type;
  437. associated_data = associated_data;
  438. user_data = user_data;
  439. return BOOL_FALSE; // Stop Event Loop
  440. }
  441. /** @brief Wait for child processes (service processes).
  442. */
  443. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  444. void *associated_data, void *user_data)
  445. {
  446. int wstatus = 0;
  447. pid_t child_pid = -1;
  448. // Wait for any child process. Doesn't block.
  449. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  450. if (WIFSIGNALED(wstatus)) {
  451. syslog(LOG_ERR, "Service process %d was terminated "
  452. "by signal: %d",
  453. child_pid, WTERMSIG(wstatus));
  454. } else {
  455. syslog(LOG_ERR, "Service process %d was terminated: %d",
  456. child_pid, WIFEXITED(wstatus));
  457. }
  458. }
  459. // Happy compiler
  460. eloop = eloop;
  461. type = type;
  462. associated_data = associated_data;
  463. user_data = user_data;
  464. return BOOL_TRUE;
  465. }
  466. /** @brief Re-read config file.
  467. *
  468. * This function can refresh klishd options but plugins (dbs for example) are
  469. * already inited and there is no way to re-init them on-the-fly.
  470. */
  471. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  472. void *associated_data, void *user_data)
  473. {
  474. struct options *opts = (struct options *)user_data;
  475. faux_ini_t *ini = NULL;
  476. if (access(opts->cfgfile, R_OK) == 0) {
  477. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  478. if (!(ini = config_parse(opts->cfgfile, opts)))
  479. syslog(LOG_ERR, "Error while config file parsing.\n");
  480. } else if (opts->cfgfile_userdefined) {
  481. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  482. }
  483. faux_ini_free(ini); // No way to use it later
  484. // Happy compiler
  485. eloop = eloop;
  486. type = type;
  487. associated_data = associated_data;
  488. return BOOL_TRUE;
  489. }
  490. bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  491. {
  492. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  493. assert(session);
  494. assert(eloop);
  495. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  496. return BOOL_TRUE;
  497. }
  498. /** @brief Event on listen socket. New remote client.
  499. */
  500. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  501. void *associated_data, void *user_data)
  502. {
  503. int new_conn = -1;
  504. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  505. pid_t child_pid = -1;
  506. assert(user_data);
  507. new_conn = accept(info->fd, NULL, NULL);
  508. if (new_conn < 0) {
  509. syslog(LOG_ERR, "Can't accept() new connection");
  510. return BOOL_TRUE;
  511. }
  512. // Fork new instance for newly connected client
  513. child_pid = fork();
  514. if (child_pid < 0) {
  515. close(new_conn);
  516. syslog(LOG_ERR, "Can't fork service process for client");
  517. return BOOL_TRUE;
  518. }
  519. // Parent
  520. if (child_pid > 0) {
  521. close(new_conn); // It's needed by child but not for parent
  522. syslog(LOG_ERR, "Service process for client was forked: %d",
  523. child_pid);
  524. return BOOL_TRUE;
  525. }
  526. // Child (forked service process)
  527. // Pass new ktpd_session to main programm
  528. *((int *)user_data) = new_conn;
  529. type = type; // Happy compiler
  530. eloop = eloop;
  531. // Return BOOL_FALSE to break listen parent loop. Child will create its
  532. // own loop then.
  533. return BOOL_FALSE;
  534. }
  535. /*
  536. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  537. void *associated_data, void *user_data)
  538. {
  539. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  540. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  541. ktpd_session_t *session = NULL;
  542. assert(clients);
  543. // Find out session
  544. session = ktpd_clients_find(clients, info->fd);
  545. if (!session) { // Some strange case
  546. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  547. faux_eloop_del_fd(eloop, info->fd);
  548. close(info->fd);
  549. return BOOL_TRUE;
  550. }
  551. // Write data
  552. if (info->revents & POLLOUT) {
  553. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  554. if (!ktpd_session_async_out(session)) {
  555. // Someting went wrong
  556. faux_eloop_del_fd(eloop, info->fd);
  557. ktpd_clients_del(clients, info->fd);
  558. syslog(LOG_ERR, "Problem with async input");
  559. }
  560. }
  561. // Read data
  562. if (info->revents & POLLIN) {
  563. if (!ktpd_session_async_in(session)) {
  564. // Someting went wrong
  565. faux_eloop_del_fd(eloop, info->fd);
  566. ktpd_clients_del(clients, info->fd);
  567. syslog(LOG_ERR, "Problem with async input");
  568. }
  569. }
  570. // EOF
  571. if (info->revents & POLLHUP) {
  572. faux_eloop_del_fd(eloop, info->fd);
  573. ktpd_clients_del(clients, info->fd);
  574. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  575. }
  576. type = type; // Happy compiler
  577. user_data = user_data; // Happy compiler
  578. return BOOL_TRUE;
  579. }
  580. */
  581. /*
  582. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  583. void *associated_data, void *user_data)
  584. {
  585. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  586. printf("Once %d\n", info->ev_id);
  587. // Happy compiler
  588. eloop = eloop;
  589. type = type;
  590. associated_data = associated_data;
  591. user_data = user_data;
  592. return BOOL_TRUE;
  593. }
  594. */
  595. /*
  596. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  597. void *associated_data, void *user_data)
  598. {
  599. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  600. printf("Periodic %d\n", info->ev_id);
  601. // Happy compiler
  602. eloop = eloop;
  603. type = type;
  604. associated_data = associated_data;
  605. user_data = user_data;
  606. return BOOL_TRUE;
  607. }
  608. */