sigwaitinfo() may fail with EINTR if interrupted by an unblocked signal not in the set
[strongswan.git] / src / charon / charon.c
index be4a954..4c2a9a4 100644 (file)
  */
 
 #include <stdio.h>
-#define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
 #include <signal.h>
-#undef _POSIX_PTHREAD_SEMANTICS
 #include <pthread.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
-#include <syslog.h>
-#include <errno.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <fcntl.h>
+#include <errno.h>
 
 #include <hydra.h>
 #include <daemon.h>
 #include <private/android_filesystem_config.h> /* for AID_VPN */
 #endif
 
-#ifndef LOG_AUTHPRIV /* not defined on OpenSolaris */
-#define LOG_AUTHPRIV LOG_AUTH
-#endif
-
 /**
  * PID file, in which charon stores its process id
  */
 #define PID_FILE IPSEC_PIDDIR "/charon.pid"
 
 /**
+ * Default user and group
+ */
+#ifndef IPSEC_USER
+#define IPSEC_USER NULL
+#endif
+
+#ifndef IPSEC_GROUP
+#define IPSEC_GROUP NULL
+#endif
+
+/**
  * Global reference to PID file (required to truncate, if undeletable)
  */
 static FILE *pidfile = NULL;
 
 /**
+ * Log levels as defined via command line arguments
+ */
+static level_t levels[DBG_MAX];
+
+/**
+ * Whether to only use syslog when logging
+ */
+static bool use_syslog = FALSE;
+
+/**
  * hook in library for debugging messages
  */
 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
@@ -83,7 +98,7 @@ static void run()
 {
        sigset_t set;
 
-       /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
+       /* handle SIGINT, SIGHUP and SIGTERM in this handler */
        sigemptyset(&set);
        sigaddset(&set, SIGINT);
        sigaddset(&set, SIGHUP);
@@ -93,12 +108,15 @@ static void run()
        while (TRUE)
        {
                int sig;
-               int error;
 
-               error = sigwait(&set, &sig);
-               if (error)
+               sig = sigwaitinfo(&set, NULL);
+               if (sig == -1)
                {
-                       DBG1(DBG_DMN, "error %d while waiting for a signal", error);
+                       if (errno == EINTR)
+                       {       /* ignore signals we didn't wait for */
+                               continue;
+                       }
+                       DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(errno));
                        return;
                }
                switch (sig)
@@ -107,8 +125,9 @@ static void run()
                        {
                                DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
                                         "configuration");
-                               if (lib->settings->load_files(lib->settings, NULL, FALSE))
+                               if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
                                {
+                                       charon->load_loggers(charon, levels, !use_syslog);
                                        lib->plugins->reload(lib->plugins, NULL);
                                }
                                else
@@ -129,11 +148,6 @@ static void run()
                                charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
                                return;
                        }
-                       default:
-                       {
-                               DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
-                               break;
-                       }
                }
        }
 }
@@ -143,20 +157,20 @@ static void run()
  */
 static bool lookup_uid_gid()
 {
-#ifdef IPSEC_USER
-       if (!charon->caps->resolve_uid(charon->caps, IPSEC_USER))
+       char *name;
+
+       name = lib->settings->get_str(lib->settings, "charon.user", IPSEC_USER);
+       if (name && !lib->caps->resolve_uid(lib->caps, name))
        {
                return FALSE;
        }
-#endif
-#ifdef IPSEC_GROUP
-       if (!charon->caps->resolve_gid(charon->caps, IPSEC_GROUP))
+       name = lib->settings->get_str(lib->settings, "charon.group", IPSEC_GROUP);
+       if (name && !lib->caps->resolve_gid(lib->caps, name))
        {
                return FALSE;
        }
-#endif
 #ifdef ANDROID
-       charon->caps->set_uid(charon->caps, AID_VPN);
+       lib->caps->set_uid(lib->caps, AID_VPN);
 #endif
        return TRUE;
 }
@@ -170,6 +184,7 @@ static void segv_handler(int signal)
 
        DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
        backtrace = backtrace_create(2);
+       backtrace->log(backtrace, NULL, TRUE);
        backtrace->log(backtrace, stderr, TRUE);
        backtrace->destroy(backtrace);
 
@@ -212,9 +227,17 @@ static bool check_pidfile()
        pidfile = fopen(PID_FILE, "w");
        if (pidfile)
        {
+               int fd;
+
+               fd = fileno(pidfile);
+               if (fd == -1 || fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+               {
+                       DBG1(DBG_LIB, "setting FD_CLOEXEC for '"PID_FILE"' failed: %s",
+                                strerror(errno));
+               }
                ignore_result(fchown(fileno(pidfile),
-                                                        charon->caps->get_uid(charon->caps),
-                                                        charon->caps->get_gid(charon->caps)));
+                                                        lib->caps->get_uid(lib->caps),
+                                                        lib->caps->get_gid(lib->caps)));
                fprintf(pidfile, "%d\n", getpid());
                fflush(pidfile);
        }
@@ -239,141 +262,6 @@ static void unlink_pidfile()
 }
 
 /**
- * Initialize logging
- */
-static void initialize_loggers(bool use_stderr, level_t levels[])
-{
-       sys_logger_t *sys_logger;
-       file_logger_t *file_logger;
-       enumerator_t *enumerator;
-       char *identifier, *facility, *filename;
-       int loggers_defined = 0;
-       debug_t group;
-       level_t  def;
-       bool append, ike_name;
-       FILE *file;
-
-       /* setup sysloggers */
-       identifier = lib->settings->get_str(lib->settings,
-                                                                               "charon.syslog.identifier", NULL);
-       if (identifier)
-       {       /* set identifier, which is prepended to each log line */
-               openlog(identifier, 0, 0);
-       }
-       enumerator = lib->settings->create_section_enumerator(lib->settings,
-                                                                                                                 "charon.syslog");
-       while (enumerator->enumerate(enumerator, &facility))
-       {
-               loggers_defined++;
-
-               ike_name = lib->settings->get_bool(lib->settings,
-                                                               "charon.syslog.%s.ike_name", FALSE, facility);
-               if (streq(facility, "daemon"))
-               {
-                       sys_logger = sys_logger_create(LOG_DAEMON, ike_name);
-               }
-               else if (streq(facility, "auth"))
-               {
-                       sys_logger = sys_logger_create(LOG_AUTHPRIV, ike_name);
-               }
-               else
-               {
-                       continue;
-               }
-               def = lib->settings->get_int(lib->settings,
-                                                                        "charon.syslog.%s.default", 1, facility);
-               for (group = 0; group < DBG_MAX; group++)
-               {
-                       sys_logger->set_level(sys_logger, group,
-                               lib->settings->get_int(lib->settings,
-                                                                          "charon.syslog.%s.%N", def,
-                                                                          facility, debug_lower_names, group));
-               }
-               charon->sys_loggers->insert_last(charon->sys_loggers, sys_logger);
-               charon->bus->add_logger(charon->bus, &sys_logger->logger);
-       }
-       enumerator->destroy(enumerator);
-
-       /* and file loggers */
-       enumerator = lib->settings->create_section_enumerator(lib->settings,
-                                                                                                                 "charon.filelog");
-       while (enumerator->enumerate(enumerator, &filename))
-       {
-               loggers_defined++;
-               if (streq(filename, "stderr"))
-               {
-                       file = stderr;
-               }
-               else if (streq(filename, "stdout"))
-               {
-                       file = stdout;
-               }
-               else
-               {
-                       append = lib->settings->get_bool(lib->settings,
-                                                                       "charon.filelog.%s.append", TRUE, filename);
-                       file = fopen(filename, append ? "a" : "w");
-                       if (file == NULL)
-                       {
-                               DBG1(DBG_DMN, "opening file %s for logging failed: %s",
-                                        filename, strerror(errno));
-                               continue;
-                       }
-                       if (lib->settings->get_bool(lib->settings,
-                                                       "charon.filelog.%s.flush_line", FALSE, filename))
-                       {
-                               setlinebuf(file);
-                       }
-               }
-               file_logger = file_logger_create(file,
-                                               lib->settings->get_str(lib->settings,
-                                                       "charon.filelog.%s.time_format", NULL, filename),
-                                               lib->settings->get_bool(lib->settings,
-                                                       "charon.filelog.%s.ike_name", FALSE, filename));
-               def = lib->settings->get_int(lib->settings,
-                                                                        "charon.filelog.%s.default", 1, filename);
-               for (group = 0; group < DBG_MAX; group++)
-               {
-                       file_logger->set_level(file_logger, group,
-                               lib->settings->get_int(lib->settings,
-                                                                          "charon.filelog.%s.%N", def,
-                                                                          filename, debug_lower_names, group));
-               }
-               charon->file_loggers->insert_last(charon->file_loggers, file_logger);
-               charon->bus->add_logger(charon->bus, &file_logger->logger);
-
-       }
-       enumerator->destroy(enumerator);
-
-       /* set up legacy style default loggers provided via command-line */
-       if (!loggers_defined)
-       {
-               /* set up default stdout file_logger */
-               file_logger = file_logger_create(stdout, NULL, FALSE);
-               charon->file_loggers->insert_last(charon->file_loggers, file_logger);
-               /* set up default daemon sys_logger */
-               sys_logger = sys_logger_create(LOG_DAEMON, FALSE);
-               charon->sys_loggers->insert_last(charon->sys_loggers, sys_logger);
-               for (group = 0; group < DBG_MAX; group++)
-               {
-                       sys_logger->set_level(sys_logger, group, levels[group]);
-                       if (use_stderr)
-                       {
-                               file_logger->set_level(file_logger, group, levels[group]);
-                       }
-               }
-               charon->bus->add_logger(charon->bus, &file_logger->logger);
-               charon->bus->add_logger(charon->bus, &sys_logger->logger);
-
-               /* set up default auth sys_logger */
-               sys_logger = sys_logger_create(LOG_AUTHPRIV, FALSE);
-               sys_logger->set_level(sys_logger, DBG_ANY, LEVEL_AUDIT);
-               charon->sys_loggers->insert_last(charon->sys_loggers, sys_logger);
-               charon->bus->add_logger(charon->bus, &sys_logger->logger);
-       }
-}
-
-/**
  * print command line usage and exit
  */
 static void usage(const char *msg)
@@ -387,7 +275,7 @@ static void usage(const char *msg)
                                        "         [--version]\n"
                                        "         [--use-syslog]\n"
                                        "         [--debug-<type> <level>]\n"
-                                       "           <type>:  log context type (dmn|mgr|ike|chd|job|cfg|knl|net|asn|enc|tnc|imc|imv|pts|tls|lib)\n"
+                                       "           <type>:  log context type (dmn|mgr|ike|chd|job|cfg|knl|net|asn|enc|tnc|imc|imv|pts|tls|esp|lib)\n"
                                        "           <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
                                        "                                    2 = controlmore, 3 = raw, 4 = private)\n"
                                        "\n"
@@ -400,8 +288,6 @@ static void usage(const char *msg)
 int main(int argc, char *argv[])
 {
        struct sigaction action;
-       bool use_syslog = FALSE;
-       level_t levels[DBG_MAX];
        int group, status = SS_RC_INITIALIZATION_FAILED;
        struct utsname utsname;
 
@@ -409,7 +295,7 @@ int main(int argc, char *argv[])
        dbg = dbg_stderr;
 
        /* initialize library */
-       if (!library_init(NULL))
+       if (!library_init(NULL, "charon"))
        {
                library_deinit();
                exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
@@ -423,7 +309,7 @@ int main(int argc, char *argv[])
                exit(SS_RC_DAEMON_INTEGRITY);
        }
 
-       if (!libhydra_init("charon"))
+       if (!libhydra_init())
        {
                dbg_stderr(DBG_DMN, 1, "initialization failed - aborting charon");
                libhydra_deinit();
@@ -431,7 +317,7 @@ int main(int argc, char *argv[])
                exit(SS_RC_INITIALIZATION_FAILED);
        }
 
-       if (!libcharon_init("charon"))
+       if (!libcharon_init())
        {
                dbg_stderr(DBG_DMN, 1, "initialization failed - aborting charon");
                goto deinit;
@@ -466,6 +352,7 @@ int main(int argc, char *argv[])
                        { "debug-imv", required_argument, &group, DBG_IMV },
                        { "debug-pts", required_argument, &group, DBG_PTS },
                        { "debug-tls", required_argument, &group, DBG_TLS },
+                       { "debug-esp", required_argument, &group, DBG_ESP },
                        { "debug-lib", required_argument, &group, DBG_LIB },
                        { 0,0,0,0 }
                };
@@ -504,7 +391,7 @@ int main(int argc, char *argv[])
                goto deinit;
        }
 
-       initialize_loggers(!use_syslog, levels);
+       charon->load_loggers(charon, levels, !use_syslog);
 
        if (uname(&utsname) != 0)
        {
@@ -528,22 +415,22 @@ int main(int argc, char *argv[])
                DBG1(DBG_DMN, "initialization failed - aborting charon");
                goto deinit;
        }
+       lib->plugins->status(lib->plugins, LEVEL_CTRL);
 
        if (check_pidfile())
        {
                DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
-               status = -1;
                goto deinit;
        }
 
-       if (!charon->caps->drop(charon->caps))
+       if (!lib->caps->drop(lib->caps))
        {
                DBG1(DBG_DMN, "capability dropping failed - aborting charon");
                goto deinit;
        }
 
        /* add handler for SEGV and ILL,
-        * INT, TERM and HUP are handled by sigwait() in run() */
+        * INT, TERM and HUP are handled by sigwaitinfo() in run() */
        action.sa_handler = segv_handler;
        action.sa_flags = 0;
        sigemptyset(&action.sa_mask);
@@ -574,4 +461,3 @@ deinit:
        library_deinit();
        return status;
 }
-