| 44 | } |
| 45 | |
| 46 | inline bool SupervisedSystemd() { |
| 47 | const char *notify_socket = getenv("NOTIFY_SOCKET"); |
| 48 | if (!notify_socket) { |
| 49 | WARN("systemd supervision requested, but NOTIFY_SOCKET not found"); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | auto fd = UniqueFD(socket(AF_UNIX, SOCK_DGRAM, 0)); |
| 54 | if (!fd) { |
| 55 | WARN("Cannot connect to systemd socket {}", notify_socket); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | sockaddr_un su = {}; |
| 60 | su.sun_family = AF_UNIX; |
| 61 | strncpy(su.sun_path, notify_socket, sizeof(su.sun_path) - 1); |
| 62 | su.sun_path[sizeof(su.sun_path) - 1] = '\0'; |
| 63 | if (notify_socket[0] == '@') su.sun_path[0] = '\0'; |
| 64 | |
| 65 | iovec iov = {}; |
| 66 | std::string ready = "READY=1"; |
| 67 | iov.iov_base = ready.data(); |
| 68 | iov.iov_len = ready.size(); |
| 69 | |
| 70 | msghdr hdr = {}; |
| 71 | hdr.msg_name = &su; |
| 72 | hdr.msg_namelen = offsetof(sockaddr_un, sun_path) + strlen(su.sun_path); |
| 73 | hdr.msg_iov = &iov; |
| 74 | hdr.msg_iovlen = 1; |
| 75 | |
| 76 | int sendto_flags = 0; |
| 77 | unsetenv("NOTIFY_SOCKET"); |
| 78 | #ifdef HAVE_MSG_NOSIGNAL |
| 79 | sendto_flags |= MSG_NOSIGNAL; |
| 80 | #endif |
| 81 | if (sendmsg(*fd, &hdr, sendto_flags) < 0) { |
| 82 | WARN("Cannot send notification to systemd"); |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | inline bool IsSupervisedMode(SupervisedMode mode) { |
| 89 | if (mode == kSupervisedAutoDetect) { |
no test coverage detected