title: Unix domain socket error, sendmsg: No buffer space available
body: <p>I've got a very simple code snippet, try to know how unix domain socket works, I've written the sender function, not yet having receiver function, it 's like below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;unistd.h&gt;
  #define ERR_EXIT(m) { \
          perror(m); \
          exit(EXIT_FAILURE); \
      }
  
  void send_fd(int sock_fd, int number){
      iovec vec;
      vec.iov_base = &amp;number;
      vec.iov_len = sizeof(number);
  
      msghdr msg;
      msg.msg_name = NULL;
      msg.msg_namelen = 0;
      msg.msg_iov = &amp;vec;
      msg.msg_iovlen = 1;
      msg.msg_flags = 0;
  
      int ret = sendmsg(sock_fd, &amp;msg, 0);
      if (ret != 1)
          ERR_EXIT("sendmsg");
  }
  
  int main(void){
      int sockfds[2];
      if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockfds) &lt; 0)
          ERR_EXIT("socketpair");
      send_fd(sockfds[1], 20);
      return 0;
  }
  </code></pre>
  
  <p>Compile and run it on linux, it prints:</p>
  
  <pre><code>sendmsg: No buffer space available
  </code></pre>
  
  <p>Well I didn't print this message myself, guess it's printed by sendmsg itself. Where does my program get wrong? I've googled for sometime and check this site, didn't find good clue.</p>
  
  <h2>How to fix it? Thanks.</h2>
  
  <p>Yes, should add 
  msghrd msg={0}; to initialize, problem solved!</p>