16 #if (defined SYSTEMD_NOTIFY) && (defined HAVE_SYS_UN_H) && (defined HAVE_SYS_STAT_H) && \
17 (defined HAVE_SYS_TYPES_H)
20 #include <sys/types.h>
24 #define _cleanup_(f) __attribute__((cleanup(f)))
26 static void closep(
int *fd)
35 static int Notify(
const char *message)
37 union sockaddr_union {
39 struct sockaddr_un sun;
41 .sun.sun_family = AF_UNIX,
43 size_t path_length, message_length;
44 _cleanup_(closep)
int fd = -1;
45 const char *socket_path;
47 socket_path = getenv(
"NOTIFY_SOCKET");
54 message_length = strlen(message);
55 if (message_length == 0)
59 if (socket_path[0] !=
'/' && socket_path[0] !=
'@')
62 path_length = strlen(socket_path);
64 if (path_length >=
sizeof(socket_addr.sun.sun_path))
67 memcpy(socket_addr.sun.sun_path, socket_path, path_length);
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;
89 return Notify(
"READY=1");