diff options
author | Martin Willi <martin@revosec.ch> | 2014-02-19 17:24:32 +0100 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2014-05-07 15:47:23 +0200 |
commit | 4c56c4621be29dbd8f96f757b8b56ac97d5792f4 (patch) | |
tree | 50a602a306911ad762c05298076a6f70655e8275 /src | |
parent | 954c63a4bc6d12b5a6a9b5e4fa010be9c1727163 (diff) | |
download | strongswan-4c56c4621be29dbd8f96f757b8b56ac97d5792f4.tar.bz2 strongswan-4c56c4621be29dbd8f96f757b8b56ac97d5792f4.tar.xz |
libcharon: Execute scripts defined in strongswan.conf during startup/shutdown
Diffstat (limited to 'src')
-rw-r--r-- | src/libcharon/daemon.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c index 0cecd1d3b..16babf014 100644 --- a/src/libcharon/daemon.c +++ b/src/libcharon/daemon.c @@ -21,6 +21,7 @@ #include <unistd.h> #include <syslog.h> #include <time.h> +#include <errno.h> #include "daemon.h" @@ -476,6 +477,53 @@ static void destroy(private_daemon_t *this) free(this); } +/** + * Run a set of configured scripts + */ +static void run_scripts(private_daemon_t *this, char *verb) +{ + enumerator_t *enumerator; + char *key, *value, *pos, buf[1024]; + FILE *cmd; + + enumerator = lib->settings->create_key_value_enumerator(lib->settings, + "%s.%s-scripts", lib->ns, verb); + while (enumerator->enumerate(enumerator, &key, &value)) + { + DBG1(DBG_DMN, "executing %s script '%s' (%s):", verb, key, value); + cmd = popen(value, "r"); + if (!cmd) + { + DBG1(DBG_DMN, "executing %s script '%s' (%s) failed: %s", + verb, key, value, strerror(errno)); + continue; + } + while (TRUE) + { + if (!fgets(buf, sizeof(buf), cmd)) + { + if (ferror(cmd)) + { + DBG1(DBG_DMN, "reading from %s script '%s' (%s) failed", + verb, key, value); + } + break; + } + else + { + pos = buf + strlen(buf); + if (pos > buf && pos[-1] == '\n') + { + pos[-1] = '\0'; + } + DBG1(DBG_DMN, "%s: %s", key, buf); + } + } + pclose(cmd); + } + enumerator->destroy(enumerator); +} + METHOD(daemon_t, start, void, private_daemon_t *this) { @@ -483,6 +531,8 @@ METHOD(daemon_t, start, void, lib->processor->set_threads(lib->processor, lib->settings->get_int(lib->settings, "%s.threads", DEFAULT_THREADS, lib->ns)); + + run_scripts(this, "start"); } @@ -598,6 +648,8 @@ void libcharon_deinit() return; } + run_scripts(this, "stop"); + destroy(this); charon = NULL; } |