<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div>Hi All,<br><br></div> After trying a few things out, here are couple doubts that I have.<br><br></div>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<br></div>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 <br></div>service restart.<br><br></div><div> 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?<br><br></div> Can someone point out what should be the correct fix for this to make it work?<br>int my_pid_notify_with_fds(pid_t pid, int unset_environment,<br> char *state, int *fds, unsigned n_fds)<br>{<br> struct sockaddr_un saddr;<br> struct iovec iov;<br> struct msghdr snd_msg;<br> struct cmsghdr *cmsg;<br> const char *e;<br> int myfd, ret;<br><br> saddr.sun_family = AF_UNIX;<br> e = getenv("NOTIFY_SOCKET");<br> strncpy(saddr.sun_path, e,sizeof(saddr.sun_path));<br><br> myfd = socket(AF_UNIX, SOCK_DGRAM, 0);<br> if (connect(myfd, (const struct sockaddr *) &saddr, sizeof(saddr)) != 0) {<br> return -1;<br> }<br><br> iov.iov_base = (char *)state;<br> iov.iov_len = strlen (state);<br><br> snd_msg.msg_iov = &iov;<br> snd_msg.msg_iovlen = 1;<br> snd_msg.msg_name = NULL;<br> snd_msg.msg_namelen = 0;<br><br> snd_msg.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds);<br> snd_msg.msg_control = malloc(snd_msg.msg_controllen);<br> cmsg = CMSG_FIRSTHDR(&snd_msg);<br> cmsg->cmsg_level = SOL_SOCKET;<br> cmsg->cmsg_type = SCM_RIGHTS;<br> cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);<br> memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);<br><br> ret = sendmsg(myfd, &snd_msg, 0);<br> close (myfd);<br> return (ret);<br>}<br><br><br></div>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.<br><br></div> The sequence is, <br></div>1. Start the service, and handle some clients<br></div>2. On a trigger, store the client fds<br></div>3. Do a systemctl restart of the service, and the service gets these fds in the new invocation<br></div>4. it handles the client and the client closes the connection<br></div>5. Now I do another restart of the service, but I still get this fd<br><br></div> So how do I make systemd "forgot" the fd or how is this scenario to be handled?<br><br></div>Thanks<br></div>jana<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 5, 2016 at 1:55 PM, Pathangi Janardhanan <span dir="ltr"><<a href="mailto:path.jana@gmail.com" target="_blank">path.jana@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<br><br><div><div class="gmail_extra"><span class=""><br><div class="gmail_quote">>> This is the output of the strace call on this section of the code in<br>>> the sd_pid_notify_with_fds, when I try to save the fds.<br>>><br>>> socket(PF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 8<br>>><br>>> sendmsg(8, {msg_name(21)={sa_family=AF_LOCAL,<br>>> sun_path="/run/systemd/notify"}, msg_iov(1)=[{"FDSTORE=1\n", 10}],<br>>> msg_controllen=40, {cmsg_len=20, cmsg_level=SOL_SOCKET,<br>>> cmsg_type=SCM_RIGHTS, {7}}, msg_flags=0}, MSG_NOSIGNAL) = -1 EINVAL<br>>> (Invalid argument)<span><br><br>></span>Which version of systemd are you running? Does it contain a5bd3c32?<br><br></div></span><div class="gmail_quote"> Sorry forgot to attach the version, here is the version I am using<br><br></div>jana@ctoserver11:~$ systemd --version<br>systemd 225<br>+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID -ELFUTILS +KMOD -IDN<br><br></div><div class="gmail_extra">And my OS is Ubuntu Wily<br><br>jana@ctoserver11:~$ lsb_release -a<br>No LSB modules are available.<br>Distributor ID: Ubuntu<br>Description: Ubuntu 15.10<br>Release: 15.10<br>Codename: wily<br><br></div><div class="gmail_extra"> let me if this is an issue and I have to move to another version?<br><br></div><div class="gmail_extra">Thanks<br></div><div class="gmail_extra">Jana<br><br></div></div></div>
</blockquote></div><br></div>