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/ */
19 #include <sys/socket.h>
24 #define _cleanup_(f) __attribute__((cleanup(f)))
26 static void closep(int *fd
) {
34 static int notify(const char *message
) {
35 union sockaddr_union
{
37 struct sockaddr_un sun
;
39 .sun
.sun_family
= AF_UNIX
,
41 size_t path_length
, message_length
;
42 _cleanup_(closep
) int fd
= -1;
43 const char *socket_path
;
45 /* Verify the argument first */
49 message_length
= strlen(message
);
50 if (message_length
== 0)
53 /* If the variable is not set, the protocol is a noop */
54 socket_path
= getenv("NOTIFY_SOCKET");
56 return 0; /* Not set? Nothing to do */
58 /* Only AF_UNIX is supported, with path or abstract sockets */
59 if (socket_path
[0] != '/' && socket_path
[0] != '@')
62 path_length
= strlen(socket_path
);
63 /* Ensure there is room for NUL byte */
64 if (path_length
>= sizeof(socket_addr
.sun
.sun_path
))
67 memcpy(socket_addr
.sun
.sun_path
, socket_path
, path_length
);
69 /* Support for abstract socket */
70 if (socket_addr
.sun
.sun_path
[0] == '@')
71 socket_addr
.sun
.sun_path
[0] = 0;
73 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
77 if (connect(fd
, &socket_addr
.sa
, offsetof(struct sockaddr_un
, sun_path
) + path_length
) != 0)
80 ssize_t written
= write(fd
, message
, message_length
);
81 if (written
!= (ssize_t
) message_length
)
82 return written
< 0 ? -errno
: -EPROTO
;
84 return 1; /* Notified! */
87 static int notify_ready(void) {
88 return notify("READY=1");
91 static int notify_reloading(void) {
92 /* A buffer with length sufficient to format the maximum UINT64 value. */
93 char reload_message
[sizeof("RELOADING=1\nMONOTONIC_USEC=18446744073709551615")];
97 /* Notify systemd that we are reloading, including a CLOCK_MONOTONIC timestamp in usec
98 * so that the program is compatible with a Type=notify-reload service. */
100 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) < 0)
103 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0 ||
104 (uint64_t) ts
.tv_sec
> (UINT64_MAX
- (ts
.tv_nsec
/ 1000ULL)) / 1000000ULL)
107 now
= (uint64_t) ts
.tv_sec
* 1000000ULL + (uint64_t) ts
.tv_nsec
/ 1000ULL;
109 if (snprintf(reload_message
, sizeof(reload_message
), "RELOADING=1\nMONOTONIC_USEC=%" PRIu64
, now
) < 0)
112 return notify(reload_message
);
115 static int notify_stopping(void) {
116 return notify("STOPPING=1");
119 static volatile sig_atomic_t reloading
= 0;
120 static volatile sig_atomic_t terminating
= 0;
122 static void signal_handler(int sig
) {
125 else if (sig
== SIGINT
|| sig
== SIGTERM
)
129 int main(int argc
, char **argv
) {
130 struct sigaction sa
= {
131 .sa_handler
= signal_handler
,
132 .sa_flags
= SA_RESTART
,
136 /* Setup signal handlers */
137 sigemptyset(&sa
.sa_mask
);
138 sigaction(SIGHUP
, &sa
, NULL
);
139 sigaction(SIGINT
, &sa
, NULL
);
140 sigaction(SIGTERM
, &sa
, NULL
);
142 /* Do more service initialization work here … */
144 /* Now that all the preparations steps are done, signal readiness */
148 fprintf(stderr
, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r
));
152 while (!terminating
) {
156 /* As a separate but related feature, we can also notify the manager
157 * when reloading configuration. This allows accurate state-tracking,
158 * and also automated hook-in of 'systemctl reload' without having to
159 * specify manually an ExecReload= line in the unit file. */
161 r
= notify_reloading();
163 fprintf(stderr
, "Failed to notify reloading to $NOTIFY_SOCKET: %s\n", strerror(-r
));
167 /* Do some reconfiguration work here … */
171 fprintf(stderr
, "Failed to notify readiness to $NOTIFY_SOCKET: %s\n", strerror(-r
));
176 /* Do some daemon work here … */
180 r
= notify_stopping();
182 fprintf(stderr
, "Failed to report termination to $NOTIFY_SOCKET: %s\n", strerror(-r
));
186 /* Do some shutdown work here … */