1:255.16-alt1
[systemd_ALT.git] / man / notify-selfcontained-example.c
blob9a7553e3013ae25007dde84ce60038a2fd1d9ba1
1 /* SPDX-License-Identifier: MIT-0 */
3 /* Implement the systemd notify protocol without external dependencies.
4 * Supports both readiness notification on startup and on reloading,
5 * according to the protocol defined at:
6 * https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
7 * This protocol is guaranteed to be stable as per:
8 * https://systemd.io/PORTABILITY_AND_STABILITY/ */
10 #include <errno.h>
11 #include <inttypes.h>
12 #include <signal.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 #include <time.h>
20 #include <unistd.h>
22 #define _cleanup_(f) __attribute__((cleanup(f)))
24 static void closep(int *fd) {
25 if (!fd || *fd < 0)
26 return;
28 close(*fd);
29 *fd = -1;
32 static int notify(const char *message) {
33 union sockaddr_union {
34 struct sockaddr sa;
35 struct sockaddr_un sun;
36 } socket_addr = {
37 .sun.sun_family = AF_UNIX,
39 size_t path_length, message_length;
40 _cleanup_(closep) int fd = -1;
41 const char *socket_path;
43 socket_path = getenv("NOTIFY_SOCKET");
44 if (!socket_path)
45 return 0; /* Not running under systemd? Nothing to do */
47 if (!message)
48 return -EINVAL;
50 message_length = strlen(message);
51 if (message_length == 0)
52 return -EINVAL;
54 /* Only AF_UNIX is supported, with path or abstract sockets */
55 if (socket_path[0] != '/' && socket_path[0] != '@')
56 return -EAFNOSUPPORT;
58 path_length = strlen(socket_path);
59 /* Ensure there is room for NUL byte */
60 if (path_length >= sizeof(socket_addr.sun.sun_path))
61 return -E2BIG;
63 memcpy(socket_addr.sun.sun_path, socket_path, path_length);
65 /* Support for abstract socket */
66 if (socket_addr.sun.sun_path[0] == '@')
67 socket_addr.sun.sun_path[0] = 0;
69 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
70 if (fd < 0)
71 return -errno;
73 if (connect(fd, &socket_addr.sa, offsetof(struct sockaddr_un, sun_path) + path_length) != 0)
74 return -errno;
76 ssize_t written = write(fd, message, message_length);
77 if (written != (ssize_t) message_length)
78 return written < 0 ? -errno : -EPROTO;
80 return 1; /* Notified! */
83 static int notify_ready(void) {
84 return notify("READY=1");
87 static int notify_reloading(void) {
88 /* A buffer with length sufficient to format the maximum UINT64 value. */
89 char reload_message[sizeof("RELOADING=1\nMONOTONIC_USEC=18446744073709551615")];
90 struct timespec ts;
91 uint64_t now;
93 /* Notify systemd that we are reloading, including a CLOCK_MONOTONIC timestamp in usec
94 * so that the program is compatible with a Type=notify-reload service. */
96 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
97 return -errno;
99 if (ts.tv_sec < 0 || ts.tv_nsec < 0 ||
100 (uint64_t) ts.tv_sec > (UINT64_MAX - (ts.tv_nsec / 1000ULL)) / 1000000ULL)
101 return -EINVAL;
103 now = (uint64_t) ts.tv_sec * 1000000ULL + (uint64_t) ts.tv_nsec / 1000ULL;
105 if (snprintf(reload_message, sizeof(reload_message), "RELOADING=1\nMONOTONIC_USEC=%" PRIu64, now) < 0)
106 return -EINVAL;
108 return notify(reload_message);
111 static volatile sig_atomic_t reloading = 0;
112 static volatile sig_atomic_t terminating = 0;
114 static void signal_handler(int sig) {
115 if (sig == SIGHUP)
116 reloading = 1;
117 else if (sig == SIGINT || sig == SIGTERM)
118 terminating = 1;
121 int main(int argc, char **argv) {
122 struct sigaction sa = {
123 .sa_handler = signal_handler,
124 .sa_flags = SA_RESTART,
126 int r;
128 /* Setup signal handlers */
129 sigemptyset(&sa.sa_mask);
130 sigaction(SIGHUP, &sa, NULL);
131 sigaction(SIGINT, &sa, NULL);
132 sigaction(SIGTERM, &sa, NULL);
134 /* Do more service initialization work here … */
136 /* Now that all the preparations steps are done, signal readiness */
138 r = notify_ready();
139 if (r < 0) {
140 fprintf(stderr, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r));
141 return EXIT_FAILURE;
144 while (!terminating) {
145 if (reloading) {
146 reloading = false;
148 /* As a separate but related feature, we can also notify the manager
149 * when reloading configuration. This allows accurate state-tracking,
150 * and also automated hook-in of 'systemctl reload' without having to
151 * specify manually an ExecReload= line in the unit file. */
153 r = notify_reloading();
154 if (r < 0) {
155 fprintf(stderr, "Failed to notify reloading to $NOTIFY_SOCKET: %s\n", strerror(-r));
156 return EXIT_FAILURE;
159 /* Do some reconfiguration work here … */
161 r = notify_ready();
162 if (r < 0) {
163 fprintf(stderr, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r));
164 return EXIT_FAILURE;
168 /* Do some daemon work here … */
169 sleep(5);
172 return EXIT_SUCCESS;