diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-03-25 06:14:13 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-03-25 06:14:13 +0100 |
commit | b97deaecc5743c40a6ea3301ba4f2f09af912cb8 (patch) | |
tree | 570da462129bec5a5d8865da3863f8302d829869 /src/fastd.c | |
parent | 2a1ad90ae34ad461c80fd52f6ea00bd8cb5c984a (diff) | |
download | fastd-b97deaecc5743c40a6ea3301ba4f2f09af912cb8.tar fastd-b97deaecc5743c40a6ea3301ba4f2f09af912cb8.zip |
Don't depend on libsystemd-daemon for systemd integration, always use systemd integration on Linux
Diffstat (limited to 'src/fastd.c')
-rw-r--r-- | src/fastd.c | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/src/fastd.c b/src/fastd.c index a6b8310..bbc5c61 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -51,8 +51,8 @@ #include <openssl/err.h> #endif -#ifdef ENABLE_SYSTEMD -#include <systemd/sd-daemon.h> +#ifdef USE_SYSTEMD +#include <sys/un.h> #endif @@ -873,11 +873,42 @@ static int daemonize(fastd_context_t *ctx) { return -1; } +#ifdef USE_SYSTEMD +static void notify_systemd(fastd_context_t *ctx, const char *notify_socket) { + int fd; + struct sockaddr_un sa = {}; + + if ((notify_socket[0] != '@' && notify_socket[0] != '/') || notify_socket[1] == 0) + return; + + fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); + if (fd < 0) + return; + + sa.sun_family = AF_UNIX; + + strncpy(sa.sun_path, notify_socket, sizeof(sa.sun_path)); + if (sa.sun_path[0] == '@') + sa.sun_path[0] = 0; + + if (connect(fd, (struct sockaddr*)&sa, offsetof(struct sockaddr_un, sun_path) + strnlen(notify_socket, sizeof(sa.sun_path))) < 0) { + pr_debug_errno(ctx, "unable to connect to notify socket: connect"); + close(fd); + return; + } + + dprintf(fd, "READY=1\nMAINPID=%lu", (unsigned long) getpid()); + pr_debug(ctx, "sent startup notification to systemd"); + + close(fd); +} +#endif + int main(int argc, char *argv[]) { fastd_context_t ctx = {}; int status_fd = -1; -#ifdef ENABLE_SYSTEMD +#ifdef USE_SYSTEMD char *notify_socket = getenv("NOTIFY_SOCKET"); if (notify_socket) { @@ -959,10 +990,9 @@ int main(int argc, char *argv[]) { write_pid(&ctx, getpid()); -#ifdef ENABLE_SYSTEMD +#ifdef USE_SYSTEMD if (notify_socket) { - setenv("NOTIFY_SOCKET", notify_socket, 1); - sd_notifyf(1, "READY=1\nMAINPID=%lu", (unsigned long) getpid()); + notify_systemd(&ctx, notify_socket); free(notify_socket); } #endif |