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/ */
17 #include <sys/socket.h>
22 #define _cleanup_(f) __attribute__((cleanup(f)))
24 static void closep(int *fd
) {
32 static int notify(const char *message
) {
33 union sockaddr_union
{
35 struct sockaddr_un sun
;
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");
45 return 0; /* Not running under systemd? Nothing to do */
50 message_length
= strlen(message
);
51 if (message_length
== 0)
54 /* Only AF_UNIX is supported, with path or abstract sockets */
55 if (socket_path
[0] != '/' && socket_path
[0] != '@')
58 path_length
= strlen(socket_path
);
59 /* Ensure there is room for NUL byte */
60 if (path_length
>= sizeof(socket_addr
.sun
.sun_path
))
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);
73 if (connect(fd
, &socket_addr
.sa
, offsetof(struct sockaddr_un
, sun_path
) + path_length
) != 0)
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")];
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)
99 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0 ||
100 (uint64_t) ts
.tv_sec
> (UINT64_MAX
- (ts
.tv_nsec
/ 1000ULL)) / 1000000ULL)
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)
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
) {
117 else if (sig
== SIGINT
|| sig
== SIGTERM
)
121 int main(int argc
, char **argv
) {
122 struct sigaction sa
= {
123 .sa_handler
= signal_handler
,
124 .sa_flags
= SA_RESTART
,
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 */
140 fprintf(stderr
, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r
));
144 while (!terminating
) {
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();
155 fprintf(stderr
, "Failed to notify reloading to $NOTIFY_SOCKET: %s\n", strerror(-r
));
159 /* Do some reconfiguration work here … */
163 fprintf(stderr
, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r
));
168 /* Do some daemon work here … */