13

I'm using a UNIX domain socket to transfer a file descriptor to another process. This works fine, but when I first try to see if socket is writeable using select(), the sendmsg() call fails with a Bad File Descriptor error.

The sendmsg() function does work fine in combination with select() if I don't add the file descriptor info to the msghdr struct, so the conflict seems to be between select() and transferring file descriptors.

I couldn't find any info on this in the man pages for select(), recvmsg(), or any other. Since this needs to become a server which hands out file descriptors to multiple processes, I'd still like to be able to use select().

Is there anything I can do to make this work, or does anyone know of alternative solutions?

Platform is Ubuntu 10.4.

This is the code that initializes the structures:



struct cmsghdr_fd : public cmsghdr
{
  int fd;
};

int sendfd(int sock, int fd)
{
  struct msghdr hdr;
  struct iovec data;
  struct cmsghdr_fd msgdata;

  char dummy = '*';
  data.iov_base = &dummy;
  data.iov_len = sizeof(dummy);

  hdr.msg_name = NULL;
  hdr.msg_namelen = 0;
  hdr.msg_iov = &data;
  hdr.msg_iovlen = 1;
  hdr.msg_flags = 0;

  hdr.msg_control = &msgdata;
  hdr.msg_controllen = sizeof(msgdata);

  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
  cmsg->cmsg_len   = hdr.msg_controllen;
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type  = SCM_RIGHTS;

  *(int*)CMSG_DATA(cmsg) = fd;

  int n = sendmsg(sock, &hdr, 0);

  if(n == -1)
    printf("sendmsg() failed: %s (socket fd = %d)\n", strerror(errno), sock);

  return n;
}

Again, this works, as long as I don't call select() first to check whether the socket is ready for writing.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
svdree
  • 13,298
  • 4
  • 28
  • 21
  • Can you include your code that populates `msghdr` and `cmsghdr`? – Nikolai Fetissov Dec 20 '10 at 13:52
  • I'd verify that your surrounding code isn't corrupting the msghdr's length (or the data itself), since from a very short read of the kernel's handling, I think that would be the likely issue – Hasturkun Dec 20 '10 at 13:55
  • I added the code that populates the structures to the question. – svdree Dec 20 '10 at 14:10
  • You should really be setting `cmsg_len` using the `CMSG_LEN` macro, but I don't think this is really the problem here... – Hasturkun Dec 20 '10 at 14:16
  • 1
    There's some differences between this code and other typical sendfd()'s I've seen - do any of these make a difference: http://code.swtch.com/plan9port/src/0e6ae8ed3276/src/lib9/sendfd.c or http://www.koders.com/c/fidAA01FB46DDB5D83E39B335DA2639B226F39CF21E.aspx – nos Dec 20 '10 at 14:18
  • We'll probably need to see the `select()` usage too. – Nikolai Fetissov Dec 20 '10 at 14:46
  • @nos: I tried the code from the first link you supplied, and my problem disappeared, thanks a lot! – svdree Dec 20 '10 at 15:27
  • Never use `select`. Use the `poll` system call or the `epoll` interface instead. `select` has a fixed length set of bits. If your process ever opens more than 1024 descriptors you will overflow this set with bizarre and not-useful results. Use `poll` if you care about less than 5 or 10 file descriptors. Use `epoll` if you care about more. `poll` (and `select`) have scaling issues because of the linear scan of the results that's required each time you use it. – Omnifarious Dec 05 '17 at 01:36

1 Answers1

11

I tried the sendfd code at this page, which was kindly provided by nos, and even though it's only slightly different, it works even when I use it in combination with select(). This is what the code looks like now:



    int sendfd(int sock, int fd)
    {
      struct msghdr hdr;
      struct iovec data;

      char cmsgbuf[CMSG_SPACE(sizeof(int))];

      char dummy = '*';
      data.iov_base = &dummy;
      data.iov_len = sizeof(dummy);

      memset(&hdr, 0, sizeof(hdr));
      hdr.msg_name = NULL;
      hdr.msg_namelen = 0;
      hdr.msg_iov = &data;
      hdr.msg_iovlen = 1;
      hdr.msg_flags = 0;

      hdr.msg_control = cmsgbuf;
      hdr.msg_controllen = CMSG_LEN(sizeof(int));

      struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
      cmsg->cmsg_len   = CMSG_LEN(sizeof(int));
      cmsg->cmsg_level = SOL_SOCKET;
      cmsg->cmsg_type  = SCM_RIGHTS;

      *(int*)CMSG_DATA(cmsg) = fd;

      int n = sendmsg(sock, &hdr, 0);

      if(n == -1)
        printf("sendmsg() failed: %s (socket fd = %d)\n", strerror(errno), sock);

      return n;
        }

Community
  • 1
  • 1
svdree
  • 13,298
  • 4
  • 28
  • 21
  • Never use `select` anymore. I gave reasons in my comment on the OP. Use the `poll` system call (which is portable to BSD) or the `epoll` system. – Omnifarious Dec 05 '17 at 01:37