diff --git a/libsystemd/sd-daemon.c b/libsystemd/sd-daemon.c index 67bea2d5..c58ab7fd 100644 --- a/libsystemd/sd-daemon.c +++ b/libsystemd/sd-daemon.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,8 @@ #include #include +#include "sd-daemon.h" + /* * NOTE: As per spec., this function does not return POSIX OK(0) but * rather "a positive value", i.e., 1. @@ -82,3 +85,133 @@ int sd_notify(int unset_environment, const char *state) return 1; } + +/* + * Printf-style notification function + */ +int sd_notifyf(int unset_environment, const char *format, ...) +{ + va_list ap; + char *state; + int ret; + + if (!format) + return -EINVAL; + + va_start(ap, format); + ret = vasprintf(&state, format, ap); + va_end(ap); + + if (ret < 0) + return -ENOMEM; + + ret = sd_notify(unset_environment, state); + free(state); + + return ret; +} + +/* + * PID-specific notification function + */ +int sd_pid_notify(pid_t pid, int unset_environment, const char *state) +{ + char path[256], *env; + size_t len; + int ret; + + if (!state) + return -EINVAL; + + len = strlen(state); + if (len == 0) + return -EINVAL; + + if (pid == 0) + pid = getpid(); + + env = getenv("NOTIFY_SOCKET"); + if (env) { + env = strdup(env); + if (!env) + return -errno; + } + + snprintf(path, sizeof(path), "@run/finit/notify/%d", pid); + setenv("NOTIFY_SOCKET", path, 1); + + ret = sd_notify(unset_environment, state); + + if (env) { + setenv("NOTIFY_SOCKET", env, 1); + free(env); + } + + return ret; +} + +/* + * PID-specific printf-style notification function + */ +int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) +{ + va_list ap; + char *state; + int ret; + + if (!format) + return -EINVAL; + + va_start(ap, format); + ret = vasprintf(&state, format, ap); + va_end(ap); + + if (ret < 0) + return -ENOMEM; + + ret = sd_pid_notify(pid, unset_environment, state); + free(state); + + return ret; +} + +/* + * Socket activation stub - Finit doesn't support socket activation yet + * Returns 0 (no file descriptors passed) + */ +int sd_listen_fds(int unset_environment) +{ + if (unset_environment) { + unsetenv("LISTEN_PID"); + unsetenv("LISTEN_FDS"); + unsetenv("LISTEN_FDNAMES"); + } + + return 0; +} + +/* + * Watchdog stub - Finit doesn't support watchdog yet + * Returns 0 (no watchdog enabled) + */ +int sd_watchdog_enabled(int unset_environment, uint64_t *usec) +{ + if (unset_environment) { + unsetenv("WATCHDOG_PID"); + unsetenv("WATCHDOG_USEC"); + } + + if (usec) + *usec = 0; + + return 0; +} + +/* + * System detection function + * Returns 1 to indicate systemd compatibility (trick applications) + */ +int sd_booted(void) +{ + return 1; +} diff --git a/libsystemd/sd-daemon.h b/libsystemd/sd-daemon.h index b9ed4a0d..672968f0 100644 --- a/libsystemd/sd-daemon.h +++ b/libsystemd/sd-daemon.h @@ -1,6 +1,28 @@ #ifndef SD_DAEMON_H_ #define SD_DAEMON_H_ +#include +#include + +#define SD_LISTEN_FDS_START 3 + +/* Basic notification function */ int sd_notify(int unset_environment, const char *state); +/* Printf-style notification functions */ +int sd_notifyf(int unset_environment, const char *format, ...); + +/* PID-specific notification functions */ +int sd_pid_notify(pid_t pid, int unset_environment, const char *state); +int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...); + +/* Socket activation stubs */ +int sd_listen_fds(int unset_environment); + +/* Watchdog stub */ +int sd_watchdog_enabled(int unset_environment, uint64_t *usec); + +/* System detection */ +int sd_booted(void); + #endif /* SD_DAEMON_H_ */