Skip to content

Commit

Permalink
Replace sendmsg with sendto shortcut
Browse files Browse the repository at this point in the history
There is a problem with calculating .msg_namelen field
of msghdr struct. Instead of

    .msg_name   = &sa,
    .msg_namelen = sizeof(sa.sun_family) + strlen(sd_unix_path),

it must set as

    .msg_namelen = sizeof(sa) // larger value than current invalid one

It works on linux but when I tried to enable this feature for macOS
it didn't (maybe because of different order of fields in the struct).

Instead of fixing calculation, I've replaced original sendmsg call
with sendto, because it's a convenient shortcut which
simplifies code and can prevent such mistakes.
  • Loading branch information
mmelentiev-mail committed Aug 20, 2019
1 parent 1314b95 commit bc30b10
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/systemd.c
Expand Up @@ -100,23 +100,14 @@ int systemd_notify(const char *message) {
struct sockaddr_un sa = {
.sun_family = AF_UNIX,
};
struct iovec vec = {
.iov_base = (char *)message,
.iov_len = (size_t )strlen(message)
};
struct msghdr msg = {
.msg_iov = &vec,
.msg_iovlen = 1,
.msg_name = &sa,
};

msg.msg_namelen = sizeof(sa.sun_family) + strlen(sd_unix_path);
strncpy(sa.sun_path, sd_unix_path, sizeof(sa.sun_path));
if (sa.sun_path[0] == '@')
sa.sun_path[0] = '\0';

say_debug("systemd: sending message '%s'", message);
ssize_t sent = sendmsg(systemd_fd, &msg, MSG_NOSIGNAL);
ssize_t sent = sendto(systemd_fd, message, (size_t)strlen(message),
MSG_NOSIGNAL, (struct sockaddr*)&sa, sizeof(sa));
if (sent == -1) {
say_syserror("systemd: failed to send message");
return -1;
Expand Down

0 comments on commit bc30b10

Please sign in to comment.