starter.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /* strongSwan IPsec starter
  2. * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <sys/select.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <syslog.h>
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. #include <time.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <pthread.h>
  32. #include <library.h>
  33. #include <utils/backtrace.h>
  34. #include <threading/thread.h>
  35. #include <utils/debug.h>
  36. #include "confread.h"
  37. #include "files.h"
  38. #include "starterstroke.h"
  39. #include "invokecharon.h"
  40. #include "cmp.h"
  41. #ifndef LOG_AUTHPRIV
  42. #define LOG_AUTHPRIV LOG_AUTH
  43. #endif
  44. #define CHARON_RESTART_DELAY 5
  45. static const char* cmd_default = IPSEC_DIR "/charon";
  46. static const char* pid_file_default = IPSEC_PIDDIR "/charon.pid";
  47. static const char* starter_pid_file_default = IPSEC_PIDDIR "/starter.pid";
  48. char *daemon_name = NULL;
  49. char *cmd = NULL;
  50. char *pid_file = NULL;
  51. char *starter_pid_file = NULL;
  52. static char *config_file = NULL;
  53. /* logging */
  54. static bool log_to_stderr = TRUE;
  55. static bool log_to_syslog = TRUE;
  56. static level_t current_loglevel = 1;
  57. /**
  58. * logging function for scepclient
  59. */
  60. static void starter_dbg(debug_t group, level_t level, char *fmt, ...)
  61. {
  62. char buffer[8192];
  63. char *current = buffer, *next;
  64. va_list args;
  65. if (level <= current_loglevel)
  66. {
  67. if (log_to_stderr)
  68. {
  69. va_start(args, fmt);
  70. vfprintf(stderr, fmt, args);
  71. va_end(args);
  72. fprintf(stderr, "\n");
  73. }
  74. if (log_to_syslog)
  75. {
  76. /* write in memory buffer first */
  77. va_start(args, fmt);
  78. vsnprintf(buffer, sizeof(buffer), fmt, args);
  79. va_end(args);
  80. /* do a syslog with every line */
  81. while (current)
  82. {
  83. next = strchr(current, '\n');
  84. if (next)
  85. {
  86. *(next++) = '\0';
  87. }
  88. syslog(LOG_INFO, "%s\n", current);
  89. current = next;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Initialize logging to stderr/syslog
  96. */
  97. static void init_log(const char *program)
  98. {
  99. dbg = starter_dbg;
  100. if (log_to_stderr)
  101. {
  102. setbuf(stderr, NULL);
  103. }
  104. if (log_to_syslog)
  105. {
  106. openlog(program, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
  107. }
  108. }
  109. /**
  110. * Deinitialize logging to syslog
  111. */
  112. static void close_log()
  113. {
  114. if (log_to_syslog)
  115. {
  116. closelog();
  117. }
  118. }
  119. /**
  120. * Return codes defined by Linux Standard Base Core Specification 3.1
  121. * in section 20.2. Init Script Actions
  122. */
  123. #define LSB_RC_SUCCESS 0 /* success */
  124. #define LSB_RC_FAILURE 1 /* generic or unspecified error */
  125. #define LSB_RC_INVALID_ARGUMENT 2 /* invalid or excess argument(s) */
  126. #define LSB_RC_NOT_IMPLEMENTED 3 /* unimplemented feature (reload) */
  127. #define LSB_RC_NOT_ALLOWED 4 /* user had insufficient privilege */
  128. #define LSB_RC_NOT_INSTALLED 5 /* program is not installed */
  129. #define LSB_RC_NOT_CONFIGURED 6 /* program is not configured */
  130. #define LSB_RC_NOT_RUNNING 7 /* program is not running */
  131. #define FLAG_ACTION_START_PLUTO 0x01
  132. #define FLAG_ACTION_UPDATE 0x02
  133. #define FLAG_ACTION_RELOAD 0x04
  134. #define FLAG_ACTION_QUIT 0x08
  135. #define FLAG_ACTION_LISTEN 0x10
  136. #define FLAG_ACTION_START_CHARON 0x20
  137. static unsigned int _action_ = 0;
  138. /**
  139. * Handle signals in the main thread
  140. */
  141. static void signal_handler(int signal)
  142. {
  143. switch (signal)
  144. {
  145. case SIGCHLD:
  146. {
  147. int status, exit_status = 0;
  148. pid_t pid;
  149. char *name = NULL;
  150. while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
  151. {
  152. if (pid == starter_charon_pid())
  153. {
  154. if (asprintf(&name, " (%s)", daemon_name) < 0)
  155. {
  156. name = NULL;
  157. }
  158. }
  159. if (WIFSIGNALED(status))
  160. {
  161. DBG2(DBG_APP, "child %d%s has been killed by sig %d\n",
  162. pid, name?name:"", WTERMSIG(status));
  163. }
  164. else if (WIFSTOPPED(status))
  165. {
  166. DBG2(DBG_APP, "child %d%s has been stopped by sig %d\n",
  167. pid, name?name:"", WSTOPSIG(status));
  168. }
  169. else if (WIFEXITED(status))
  170. {
  171. exit_status = WEXITSTATUS(status);
  172. if (exit_status >= SS_RC_FIRST && exit_status <= SS_RC_LAST)
  173. {
  174. _action_ = FLAG_ACTION_QUIT;
  175. }
  176. DBG2(DBG_APP, "child %d%s has quit (exit code %d)\n",
  177. pid, name?name:"", exit_status);
  178. }
  179. else
  180. {
  181. DBG2(DBG_APP, "child %d%s has quit", pid, name?name:"");
  182. }
  183. if (pid == starter_charon_pid())
  184. {
  185. starter_charon_sigchild(pid, exit_status);
  186. }
  187. }
  188. if (name)
  189. {
  190. free(name);
  191. }
  192. }
  193. break;
  194. case SIGALRM:
  195. _action_ |= FLAG_ACTION_START_CHARON;
  196. break;
  197. case SIGHUP:
  198. _action_ |= FLAG_ACTION_UPDATE;
  199. break;
  200. case SIGTERM:
  201. case SIGQUIT:
  202. case SIGINT:
  203. _action_ |= FLAG_ACTION_QUIT;
  204. break;
  205. case SIGUSR1:
  206. _action_ |= FLAG_ACTION_RELOAD;
  207. _action_ |= FLAG_ACTION_UPDATE;
  208. break;
  209. default:
  210. DBG1(DBG_APP, "fsig(): unknown signal %d -- investigate", signal);
  211. break;
  212. }
  213. }
  214. /**
  215. * Handle fatal signals raised by threads
  216. */
  217. static void fatal_signal_handler(int signal)
  218. {
  219. backtrace_t *backtrace;
  220. DBG1(DBG_APP, "thread %u received %d", thread_current_id(), signal);
  221. backtrace = backtrace_create(2);
  222. backtrace->log(backtrace, stderr, TRUE);
  223. backtrace->destroy(backtrace);
  224. DBG1(DBG_APP, "killing ourself, received critical signal");
  225. abort();
  226. }
  227. static bool check_pid(char *pid_file)
  228. {
  229. struct stat stb;
  230. FILE *pidfile;
  231. if (stat(pid_file, &stb) == 0)
  232. {
  233. pidfile = fopen(pid_file, "r");
  234. if (pidfile)
  235. {
  236. char buf[64];
  237. pid_t pid = 0;
  238. memset(buf, 0, sizeof(buf));
  239. if (fread(buf, 1, sizeof(buf), pidfile))
  240. {
  241. buf[sizeof(buf) - 1] = '\0';
  242. pid = atoi(buf);
  243. }
  244. fclose(pidfile);
  245. if (pid && pid != getpid() && kill(pid, 0) == 0)
  246. { /* such a process is running */
  247. return TRUE;
  248. }
  249. }
  250. DBG1(DBG_APP, "removing pidfile '%s', process not running", pid_file);
  251. unlink(pid_file);
  252. }
  253. return FALSE;
  254. }
  255. /* Set daemon name and adjust command and pid filenames accordingly */
  256. static bool set_daemon_name()
  257. {
  258. if (!daemon_name)
  259. {
  260. daemon_name = "charon";
  261. }
  262. if (asprintf(&cmd, IPSEC_DIR"/%s", daemon_name) < 0)
  263. {
  264. cmd = (char*)cmd_default;
  265. }
  266. if (asprintf(&pid_file, IPSEC_PIDDIR"/%s.pid", daemon_name) < 0)
  267. {
  268. pid_file = (char*)pid_file_default;
  269. }
  270. if (asprintf(&starter_pid_file, IPSEC_PIDDIR"/starter.%s.pid",
  271. daemon_name) < 0)
  272. {
  273. starter_pid_file = (char*)starter_pid_file_default;
  274. }
  275. return TRUE;
  276. }
  277. static void cleanup()
  278. {
  279. if (cmd != cmd_default)
  280. {
  281. free(cmd);
  282. }
  283. if (pid_file != pid_file_default)
  284. {
  285. free(pid_file);
  286. }
  287. if (starter_pid_file != starter_pid_file_default)
  288. {
  289. free(starter_pid_file);
  290. }
  291. }
  292. static void usage(char *name)
  293. {
  294. fprintf(stderr, "Usage: starter [--nofork] [--auto-update <sec>]\n"
  295. " [--debug|--debug-more|--debug-all|--nolog]\n"
  296. " [--attach-gdb] [--daemon <name>]\n"
  297. " [--conf <path to ipsec.conf>]\n");
  298. exit(LSB_RC_INVALID_ARGUMENT);
  299. }
  300. int main (int argc, char **argv)
  301. {
  302. starter_config_t *cfg = NULL;
  303. starter_config_t *new_cfg;
  304. starter_conn_t *conn, *conn2;
  305. starter_ca_t *ca, *ca2;
  306. struct sigaction action;
  307. struct stat stb;
  308. int i;
  309. int id = 1;
  310. struct timespec ts;
  311. unsigned long auto_update = 0;
  312. time_t last_reload;
  313. bool no_fork = FALSE;
  314. bool attach_gdb = FALSE;
  315. bool load_warning = FALSE;
  316. bool conftest = FALSE;
  317. library_init(NULL, "starter");
  318. atexit(library_deinit);
  319. /* parse command line */
  320. for (i = 1; i < argc; i++)
  321. {
  322. if (streq(argv[i], "--debug"))
  323. {
  324. current_loglevel = 2;
  325. }
  326. else if (streq(argv[i], "--debug-more"))
  327. {
  328. current_loglevel = 3;
  329. }
  330. else if (streq(argv[i], "--debug-all"))
  331. {
  332. current_loglevel = 4;
  333. }
  334. else if (streq(argv[i], "--nolog"))
  335. {
  336. current_loglevel = 0;
  337. }
  338. else if (streq(argv[i], "--nofork"))
  339. {
  340. no_fork = TRUE;
  341. }
  342. else if (streq(argv[i], "--attach-gdb"))
  343. {
  344. no_fork = TRUE;
  345. attach_gdb = TRUE;
  346. }
  347. else if (streq(argv[i], "--auto-update") && i+1 < argc)
  348. {
  349. auto_update = atoi(argv[++i]);
  350. if (!auto_update)
  351. usage(argv[0]);
  352. }
  353. else if (streq(argv[i], "--daemon") && i+1 < argc)
  354. {
  355. daemon_name = argv[++i];
  356. }
  357. else if (streq(argv[i], "--conf") && i+1 < argc)
  358. {
  359. config_file = argv[++i];
  360. }
  361. else if (streq(argv[i], "--conftest"))
  362. {
  363. conftest = TRUE;
  364. }
  365. else
  366. {
  367. usage(argv[0]);
  368. }
  369. }
  370. if (!set_daemon_name())
  371. {
  372. DBG1(DBG_APP, "unable to set daemon name");
  373. exit(LSB_RC_FAILURE);
  374. }
  375. if (!config_file)
  376. {
  377. config_file = lib->settings->get_str(lib->settings,
  378. "starter.config_file", CONFIG_FILE);
  379. }
  380. init_log("ipsec_starter");
  381. if (conftest)
  382. {
  383. int status = LSB_RC_SUCCESS;
  384. cfg = confread_load(config_file);
  385. if (cfg == NULL || cfg->err > 0)
  386. {
  387. DBG1(DBG_APP, "config invalid!");
  388. status = LSB_RC_INVALID_ARGUMENT;
  389. }
  390. else
  391. {
  392. DBG1(DBG_APP, "config OK");
  393. }
  394. if (cfg)
  395. {
  396. confread_free(cfg);
  397. }
  398. cleanup();
  399. exit(status);
  400. }
  401. if (stat(cmd, &stb) != 0)
  402. {
  403. DBG1(DBG_APP, "IKE daemon '%s' not found", cmd);
  404. cleanup();
  405. exit(LSB_RC_FAILURE);
  406. }
  407. DBG1(DBG_APP, "Starting %sSwan "VERSION" IPsec [starter]...",
  408. lib->settings->get_bool(lib->settings,
  409. "charon.i_dont_care_about_security_and_use_aggressive_mode_psk",
  410. FALSE) ? "weak" : "strong");
  411. #ifdef LOAD_WARNING
  412. load_warning = TRUE;
  413. #endif
  414. if (lib->settings->get_bool(lib->settings, "starter.load_warning", load_warning))
  415. {
  416. if (lib->settings->get_str(lib->settings, "charon.load", NULL))
  417. {
  418. DBG1(DBG_APP, "!! Your strongswan.conf contains manual plugin load options for charon.");
  419. DBG1(DBG_APP, "!! This is recommended for experts only, see");
  420. DBG1(DBG_APP, "!! http://wiki.strongswan.org/projects/strongswan/wiki/PluginLoad");
  421. }
  422. }
  423. #ifndef STARTER_ALLOW_NON_ROOT
  424. /* verify that we can start */
  425. if (getuid() != 0)
  426. {
  427. DBG1(DBG_APP, "permission denied (must be superuser)");
  428. cleanup();
  429. exit(LSB_RC_NOT_ALLOWED);
  430. }
  431. #endif
  432. if (check_pid(pid_file))
  433. {
  434. DBG1(DBG_APP, "%s is already running (%s exists) -- skipping daemon start",
  435. daemon_name, pid_file);
  436. }
  437. else
  438. {
  439. _action_ |= FLAG_ACTION_START_CHARON;
  440. }
  441. if (stat(DEV_RANDOM, &stb) != 0)
  442. {
  443. DBG1(DBG_APP, "unable to start strongSwan IPsec -- no %s!", DEV_RANDOM);
  444. cleanup();
  445. exit(LSB_RC_FAILURE);
  446. }
  447. if (stat(DEV_URANDOM, &stb)!= 0)
  448. {
  449. DBG1(DBG_APP, "unable to start strongSwan IPsec -- no %s!", DEV_URANDOM);
  450. cleanup();
  451. exit(LSB_RC_FAILURE);
  452. }
  453. cfg = confread_load(config_file);
  454. if (cfg == NULL || cfg->err > 0)
  455. {
  456. DBG1(DBG_APP, "unable to start strongSwan -- fatal errors in config");
  457. if (cfg)
  458. {
  459. confread_free(cfg);
  460. }
  461. cleanup();
  462. exit(LSB_RC_INVALID_ARGUMENT);
  463. }
  464. last_reload = time_monotonic(NULL);
  465. if (check_pid(starter_pid_file))
  466. {
  467. DBG1(DBG_APP, "starter is already running (%s exists) -- no fork done",
  468. starter_pid_file);
  469. confread_free(cfg);
  470. cleanup();
  471. exit(LSB_RC_SUCCESS);
  472. }
  473. /* fork if we're not debugging stuff */
  474. if (!no_fork)
  475. {
  476. log_to_stderr = FALSE;
  477. switch (fork())
  478. {
  479. case 0:
  480. {
  481. int fnull;
  482. close_log();
  483. fnull = open("/dev/null", O_RDWR);
  484. if (fnull >= 0)
  485. {
  486. dup2(fnull, STDIN_FILENO);
  487. dup2(fnull, STDOUT_FILENO);
  488. dup2(fnull, STDERR_FILENO);
  489. close(fnull);
  490. }
  491. setsid();
  492. init_log("ipsec_starter");
  493. }
  494. break;
  495. case -1:
  496. DBG1(DBG_APP, "can't fork: %s", strerror(errno));
  497. break;
  498. default:
  499. confread_free(cfg);
  500. cleanup();
  501. exit(LSB_RC_SUCCESS);
  502. }
  503. }
  504. /* save pid file in /var/run/starter[.daemon_name].pid */
  505. {
  506. FILE *fd = fopen(starter_pid_file, "w");
  507. if (fd)
  508. {
  509. fprintf(fd, "%u\n", getpid());
  510. fclose(fd);
  511. }
  512. }
  513. /* we handle these signals only in pselect() */
  514. memset(&action, 0, sizeof(action));
  515. sigemptyset(&action.sa_mask);
  516. sigaddset(&action.sa_mask, SIGHUP);
  517. sigaddset(&action.sa_mask, SIGINT);
  518. sigaddset(&action.sa_mask, SIGTERM);
  519. sigaddset(&action.sa_mask, SIGQUIT);
  520. sigaddset(&action.sa_mask, SIGALRM);
  521. sigaddset(&action.sa_mask, SIGUSR1);
  522. pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
  523. /* install a handler for fatal signals */
  524. action.sa_handler = fatal_signal_handler;
  525. sigaction(SIGSEGV, &action, NULL);
  526. sigaction(SIGILL, &action, NULL);
  527. sigaction(SIGBUS, &action, NULL);
  528. action.sa_handler = SIG_IGN;
  529. sigaction(SIGPIPE, &action, NULL);
  530. /* install main signal handler */
  531. action.sa_handler = signal_handler;
  532. sigaction(SIGHUP, &action, NULL);
  533. sigaction(SIGINT, &action, NULL);
  534. sigaction(SIGTERM, &action, NULL);
  535. sigaction(SIGQUIT, &action, NULL);
  536. sigaction(SIGALRM, &action, NULL);
  537. sigaction(SIGUSR1, &action, NULL);
  538. /* this is not blocked above as we want to receive it asynchronously */
  539. sigaction(SIGCHLD, &action, NULL);
  540. /* empty mask for pselect() call below */
  541. sigemptyset(&action.sa_mask);
  542. for (;;)
  543. {
  544. /*
  545. * Stop charon (if started) and exit
  546. */
  547. if (_action_ & FLAG_ACTION_QUIT)
  548. {
  549. if (starter_charon_pid())
  550. {
  551. starter_stop_charon();
  552. }
  553. confread_free(cfg);
  554. unlink(starter_pid_file);
  555. cleanup();
  556. DBG1(DBG_APP, "ipsec starter stopped");
  557. close_log();
  558. exit(LSB_RC_SUCCESS);
  559. }
  560. /*
  561. * Delete all connections. Will be added below
  562. */
  563. if (_action_ & FLAG_ACTION_RELOAD)
  564. {
  565. _action_ &= ~FLAG_ACTION_RELOAD;
  566. if (starter_charon_pid())
  567. {
  568. for (conn = cfg->conn_first; conn; conn = conn->next)
  569. {
  570. if (conn->state == STATE_ADDED)
  571. {
  572. if (starter_charon_pid())
  573. {
  574. if (conn->startup == STARTUP_ROUTE)
  575. {
  576. starter_stroke_unroute_conn(conn);
  577. }
  578. starter_stroke_del_conn(conn);
  579. }
  580. conn->state = STATE_TO_ADD;
  581. }
  582. }
  583. for (ca = cfg->ca_first; ca; ca = ca->next)
  584. {
  585. if (ca->state == STATE_ADDED)
  586. {
  587. if (starter_charon_pid())
  588. {
  589. starter_stroke_del_ca(ca);
  590. }
  591. ca->state = STATE_TO_ADD;
  592. }
  593. }
  594. }
  595. }
  596. /*
  597. * Update configuration
  598. */
  599. if (_action_ & FLAG_ACTION_UPDATE)
  600. {
  601. _action_ &= ~FLAG_ACTION_UPDATE;
  602. DBG2(DBG_APP, "Reloading config...");
  603. new_cfg = confread_load(config_file);
  604. if (new_cfg && (new_cfg->err == 0))
  605. {
  606. /* Switch to new config. New conn will be loaded below */
  607. /* Look for new connections that are already loaded */
  608. for (conn = cfg->conn_first; conn; conn = conn->next)
  609. {
  610. if (conn->state == STATE_ADDED)
  611. {
  612. for (conn2 = new_cfg->conn_first; conn2; conn2 = conn2->next)
  613. {
  614. if (conn2->state == STATE_TO_ADD && starter_cmp_conn(conn, conn2))
  615. {
  616. conn->state = STATE_REPLACED;
  617. conn2->state = STATE_ADDED;
  618. conn2->id = conn->id;
  619. break;
  620. }
  621. }
  622. }
  623. }
  624. /* Remove conn sections that have become unused */
  625. for (conn = cfg->conn_first; conn; conn = conn->next)
  626. {
  627. if (conn->state == STATE_ADDED)
  628. {
  629. if (starter_charon_pid())
  630. {
  631. if (conn->startup == STARTUP_ROUTE)
  632. {
  633. starter_stroke_unroute_conn(conn);
  634. }
  635. starter_stroke_del_conn(conn);
  636. }
  637. }
  638. }
  639. /* Look for new ca sections that are already loaded */
  640. for (ca = cfg->ca_first; ca; ca = ca->next)
  641. {
  642. if (ca->state == STATE_ADDED)
  643. {
  644. for (ca2 = new_cfg->ca_first; ca2; ca2 = ca2->next)
  645. {
  646. if (ca2->state == STATE_TO_ADD && starter_cmp_ca(ca, ca2))
  647. {
  648. ca->state = STATE_REPLACED;
  649. ca2->state = STATE_ADDED;
  650. break;
  651. }
  652. }
  653. }
  654. }
  655. /* Remove ca sections that have become unused */
  656. for (ca = cfg->ca_first; ca; ca = ca->next)
  657. {
  658. if (ca->state == STATE_ADDED)
  659. {
  660. if (starter_charon_pid())
  661. {
  662. starter_stroke_del_ca(ca);
  663. }
  664. }
  665. }
  666. confread_free(cfg);
  667. cfg = new_cfg;
  668. }
  669. else
  670. {
  671. DBG1(DBG_APP, "can't reload config file due to errors -- keeping old one");
  672. if (new_cfg)
  673. {
  674. confread_free(new_cfg);
  675. }
  676. }
  677. last_reload = time_monotonic(NULL);
  678. }
  679. /*
  680. * Start daemon
  681. */
  682. if (_action_ & FLAG_ACTION_START_CHARON)
  683. {
  684. _action_ &= ~FLAG_ACTION_START_CHARON;
  685. if (!starter_charon_pid())
  686. {
  687. DBG2(DBG_APP, "Attempting to start %s...", daemon_name);
  688. if (starter_start_charon(cfg, no_fork, attach_gdb))
  689. {
  690. /* schedule next try */
  691. alarm(CHARON_RESTART_DELAY);
  692. }
  693. starter_stroke_configure(cfg);
  694. }
  695. for (ca = cfg->ca_first; ca; ca = ca->next)
  696. {
  697. if (ca->state == STATE_ADDED)
  698. {
  699. ca->state = STATE_TO_ADD;
  700. }
  701. }
  702. for (conn = cfg->conn_first; conn; conn = conn->next)
  703. {
  704. if (conn->state == STATE_ADDED)
  705. {
  706. conn->state = STATE_TO_ADD;
  707. }
  708. }
  709. }
  710. /*
  711. * Add stale conn and ca sections
  712. */
  713. if (starter_charon_pid())
  714. {
  715. for (ca = cfg->ca_first; ca; ca = ca->next)
  716. {
  717. if (ca->state == STATE_TO_ADD)
  718. {
  719. if (starter_charon_pid())
  720. {
  721. starter_stroke_add_ca(ca);
  722. }
  723. ca->state = STATE_ADDED;
  724. }
  725. }
  726. for (conn = cfg->conn_first; conn; conn = conn->next)
  727. {
  728. if (conn->state == STATE_TO_ADD)
  729. {
  730. if (conn->id == 0)
  731. {
  732. /* affect new unique id */
  733. conn->id = id++;
  734. }
  735. if (starter_charon_pid())
  736. {
  737. starter_stroke_add_conn(cfg, conn);
  738. }
  739. conn->state = STATE_ADDED;
  740. if (conn->startup == STARTUP_START)
  741. {
  742. if (starter_charon_pid())
  743. {
  744. starter_stroke_initiate_conn(conn);
  745. }
  746. }
  747. else if (conn->startup == STARTUP_ROUTE)
  748. {
  749. if (starter_charon_pid())
  750. {
  751. starter_stroke_route_conn(conn);
  752. }
  753. }
  754. }
  755. }
  756. }
  757. /*
  758. * If auto_update activated, when to stop select
  759. */
  760. if (auto_update)
  761. {
  762. time_t now = time_monotonic(NULL);
  763. ts.tv_sec = (now < last_reload + auto_update) ?
  764. (last_reload + auto_update - now) : 0;
  765. ts.tv_nsec = 0;
  766. }
  767. /*
  768. * Wait for something to happen
  769. */
  770. if (!_action_ &&
  771. pselect(0, NULL, NULL, NULL, auto_update ? &ts : NULL,
  772. &action.sa_mask) == 0)
  773. {
  774. /* timeout -> auto_update */
  775. _action_ |= FLAG_ACTION_UPDATE;
  776. }
  777. }
  778. exit(LSB_RC_SUCCESS);
  779. }