Skip to content

Commit

Permalink
Remove dependency on libsystemd
Browse files Browse the repository at this point in the history
We only use sd_notify and it's trivial to open code.
  • Loading branch information
cdown committed Apr 1, 2024
1 parent fc5b864 commit 1a6b1d9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: awalsh128/cache-apt-pkgs-action@v1
with:
packages: gcc-10 libnotify-dev libsystemd-dev afl clang clang-tidy
packages: gcc-10 libnotify-dev afl clang clang-tidy
version: 1.0

- run: gcc --version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install Packages
run: |
sudo apt-get update
sudo apt-get install --yes libnotify-dev libsystemd-dev
sudo apt-get install --yes libnotify-dev
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
13 changes: 0 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ datarootdir:=$(prefix)/share
mandir:=$(datarootdir)/man


WANT_SD_NOTIFY=1
HAS_LIBSYSTEMD=$(shell pkg-config libsystemd && echo 1 || echo 0)

ifeq ($(HAS_LIBSYSTEMD),0)
$(warning libsystemd not found, setting WANT_SD_NOTIFY=0)
WANT_SD_NOTIFY=0
endif

ifeq ($(WANT_SD_NOTIFY),1)
CFLAGS+=-DWANT_SD_NOTIFY $(shell pkg-config --cflags libsystemd)
LDFLAGS+=$(shell pkg-config --libs libsystemd)
endif

SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,3 @@ Thresholds are specified with fields in the following format:

Issues and pull requests are welcome! Please feel free to file them [on
GitHub](https://github.com/cdown/psi-notify).

[sd_notify]: https://www.freedesktop.org/software/systemd/man/sd_notify.html
53 changes: 34 additions & 19 deletions psi-notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

#include "psi-notify.h"

#ifdef WANT_SD_NOTIFY
#include <systemd/sd-daemon.h>
#else /* !WANT_SD_NOTIFY */
#define sd_notifyf(reset_env, fmt, ...) \
do { \
} while (0)
#define sd_notify(reset_env, state) sd_notifyf(reset_env, "%s", state)
#endif /* WANT_SD_NOTIFY */

static volatile sig_atomic_t config_reload_pending = 0; /* SIGHUP */
static volatile sig_atomic_t run = 1; /* SIGTERM, SIGINT */

Expand All @@ -40,6 +33,22 @@ static Alert active_notif[] = {
[RT_IO] = DEFAULT_ALERT_STATE,
};

#define NOTIFY_MAX 256
static int sd_notify(const char *message) {
const char *notify_path = getenv("NOTIFY_SOCKET");
if (!notify_path) {
return 0;
}
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
expect(fd >= 0);
struct sockaddr_un addr = {.sun_family = AF_UNIX};
snprintf_check(addr.sun_path, sizeof(addr.sun_path), "%s", notify_path);
expect(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0);
expect(write(fd, message, strlen(message)) == (ssize_t)strlen(message));
close(fd);
return 0;
}

static void request_reload_config(int sig) {
(void)sig;
config_reload_pending = 1;
Expand Down Expand Up @@ -262,10 +271,14 @@ static void config_reset_user_facing(void) {
#define WATCHDOG_GRACE_PERIOD_SEC 5
#define SEC_TO_USEC 1000000
static void watchdog_update_usec(void) {
sd_notifyf(0,
"WATCHDOG_USEC=%lld",
((long long)cfg.update_interval + WATCHDOG_GRACE_PERIOD_SEC) *
SEC_TO_USEC);
char message[NOTIFY_MAX];
snprintf_check(
message,
sizeof(message),
"WATCHDOG_USEC=%lld",
((long long)cfg.update_interval + WATCHDOG_GRACE_PERIOD_SEC) *
SEC_TO_USEC);
sd_notify(message);
}

static void config_get_path(char *out) {
Expand Down Expand Up @@ -854,27 +867,29 @@ int main(int argc, char *argv[]) {

expect(clock_gettime(CLOCK_MONOTONIC, &in) == 0);

sd_notify(0,
"READY=1\nWATCHDOG=1\n"
sd_notify("READY=1\nWATCHDOG=1\n"
"STATUS=Checking current pressures...");

for_each_arr(i, all_res) { pressure_check_notify_if_new(all_res[i]); }

unblock_all_signals();

if (config_reload_pending) {
sd_notify(0, "RELOADING=1\nSTATUS=Reloading config...");
sd_notify("RELOADING=1\nSTATUS=Reloading config...");
if (config_update_from_file(NULL) == 0) {
print_config();
}
config_reload_pending = 0;
} else if (run) {
sd_notifyf(
0,
char message[NOTIFY_MAX];
snprintf_check(
message,
sizeof(message),
"STATUS=Waiting. Current alerts: CPU: %s, memory: %s, I/O: %s",
active_inactive(&active_notif[RT_CPU]),
active_inactive(&active_notif[RT_MEMORY]),
active_inactive(&active_notif[RT_IO]));
sd_notify(message);

suspend_for_remaining_interval(&in);
}
Expand All @@ -887,7 +902,7 @@ int main(int argc, char *argv[]) {
unblock_all_signals();

info("Terminating after %lu intervals elapsed.\n", num_iters);
sd_notify(0, "STOPPING=1\nSTATUS=Tearing down...");
sd_notify("STOPPING=1\nSTATUS=Tearing down...");

free(cfg.cpu.filename);
free(cfg.memory.filename);
Expand Down

0 comments on commit 1a6b1d9

Please sign in to comment.