[systemd-devel] Unable to store fds with systemd and reterive it back again

Pathangi Janardhanan path.jana at gmail.com
Tue Jan 5 15:07:49 PST 2016


Hi All,

 After trying a few things out, here are couple doubts that I have.

1. I am able to get this to work, if I modify the way the sendmsg is being
done, here is a temp. function that I wrote in my own service
to mimic the actions of sd_pid_notify_with_pids and with this I am able to
store the fds with systemd and reterive it back again on a
service restart.

 The only real difference is that I do a connect on the socket before doing
the sendmsg call, and searching through some stuff on google seems to
indicate that you need to do connect for unix domain sockets, but not sure
if that is the right thing to do?

 Can someone point out what should be the correct fix for this to make it
work?
int my_pid_notify_with_fds(pid_t pid, int unset_environment,
    char *state, int *fds, unsigned n_fds)
{
    struct sockaddr_un saddr;
    struct iovec iov;
    struct msghdr snd_msg;
    struct cmsghdr *cmsg;
    const char *e;
    int myfd, ret;

    saddr.sun_family = AF_UNIX;
    e = getenv("NOTIFY_SOCKET");
    strncpy(saddr.sun_path, e,sizeof(saddr.sun_path));

    myfd = socket(AF_UNIX, SOCK_DGRAM, 0);
   if (connect(myfd, (const struct sockaddr *) &saddr, sizeof(saddr)) != 0)
{
        return -1;
    }

    iov.iov_base = (char *)state;
    iov.iov_len = strlen (state);

    snd_msg.msg_iov = &iov;
    snd_msg.msg_iovlen = 1;
    snd_msg.msg_name = NULL;
    snd_msg.msg_namelen = 0;

    snd_msg.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds);
    snd_msg.msg_control = malloc(snd_msg.msg_controllen);
    cmsg = CMSG_FIRSTHDR(&snd_msg);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
    memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);

    ret = sendmsg(myfd, &snd_msg, 0);
    close (myfd);
    return (ret);
}


2. The other issue I now have, is that after my service is restarted, I am
able to reterive this and continue to service the client. But I am not able
to delete this from the systemd state.

 The sequence is,
1. Start the service, and handle some clients
2. On a trigger, store the client fds
3. Do a systemctl restart of the service, and the service gets these fds in
the new invocation
4. it handles the client and the client closes the connection
5. Now I do another restart of the service, but I still get this fd

 So how do I make systemd "forgot" the fd or how is this scenario to be
handled?

Thanks
jana


On Tue, Jan 5, 2016 at 1:55 PM, Pathangi Janardhanan <path.jana at gmail.com>
wrote:

> Hi,
>
>
> >>  This is the output of the strace call on this section of the code in
> >> the sd_pid_notify_with_fds, when I try to save the fds.
> >>
> >> socket(PF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 8
> >>
> >> sendmsg(8, {msg_name(21)={sa_family=AF_LOCAL,
> >> sun_path="/run/systemd/notify"}, msg_iov(1)=[{"FDSTORE=1\n", 10}],
> >> msg_controllen=40, {cmsg_len=20, cmsg_level=SOL_SOCKET,
> >> cmsg_type=SCM_RIGHTS, {7}}, msg_flags=0}, MSG_NOSIGNAL) = -1 EINVAL
> >> (Invalid argument)
>
> >Which version of systemd are you running? Does it contain a5bd3c32?
>
>  Sorry forgot to attach the version, here is the version I am using
>
> jana at ctoserver11:~$ systemd --version
> systemd 225
> +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP
> +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID -ELFUTILS +KMOD -IDN
>
> And my OS is Ubuntu Wily
>
> jana at ctoserver11:~$ lsb_release -a
> No LSB modules are available.
> Distributor ID:    Ubuntu
> Description:    Ubuntu 15.10
> Release:    15.10
> Codename:    wily
>
>  let me if this is an issue and I have to move to another version?
>
> Thanks
> Jana
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20160105/b080f335/attachment.html>


More information about the systemd-devel mailing list