chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / sys / fdpass.c
1 /* -*-c-*-
2  *
3  * File descriptor passing
4  *
5  * (c) 2003 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <errno.h>
33
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #include <sys/un.h>
40
41 #include "fdpass.h"
42 #include "macros.h"
43
44 /*----- Main code ---------------------------------------------------------*/
45
46 /* --- @fdpass_send@ --- *
47  *
48  * Arguments:   @int sock@ = socket to send over
49  *              @int fd@ = file descriptor to send
50  *              @const void *p@ = pointer to data to send
51  *              @size_t sz@ = size of buffer to send
52  *
53  * Returns:     On error, @-1@, otherwise number of bytes transferred from
54  *              @p@.
55  *
56  * Use:         Sends a copy of file descriptor @fd@ to the other end of
57  *              @sock@.
58  */
59
60 ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz)
61 {
62   struct iovec iov;
63   struct msghdr msg;
64 #ifndef HAVE_MSG_ACCRIGHTS
65   char buf[CMSG_SPACE(sizeof(fd))];
66   struct cmsghdr *cmsg;
67 #endif
68
69   iov.iov_base = UNCONST(void, p);
70   iov.iov_len = sz;
71   msg.msg_name = 0;
72   msg.msg_namelen = 0;
73   msg.msg_iov = &iov;
74   msg.msg_iovlen = 1;
75 #ifdef HAVE_MSG_ACCRIGHTS
76   msg.msg_accrights = &fd;
77   msg.msg_accrightslen = sizeof(fd);
78 #else
79   msg.msg_flags = 0;
80   msg.msg_control = buf;
81   msg.msg_controllen = sizeof(buf);
82   cmsg = CMSG_FIRSTHDR(&msg);
83   cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
84   cmsg->cmsg_level = SOL_SOCKET;
85   cmsg->cmsg_type = SCM_RIGHTS;
86   memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
87 #endif
88   return (sendmsg(sock, &msg, 0));
89 }
90
91 /* --- @fdpass_recv@ --- *
92  *
93  * Arguments:   @int sock@ = socket to send over
94  *              @int *fd@ = where to put received descriptor
95  *              @void *p@ = pointer to where to put data
96  *              @size_t sz@ = size of buffer
97  *
98  * Returns:     On error, @-1@, otherwise number of bytes transferred.
99  *
100  * Use:         Receives a file descriptor.  If the call succeeds, and there
101  *              was a file descriptor, then @fd@ won't be @-1@ on exit;
102  *              otherwise it will.  At most one descriptor will be collected.
103  */
104
105 /* Qemu 2.8.1 clobbers 12 bytes beyond the end of the control-message
106  * buffer.  This is fixed in 2.12, but I'll bodge it for the sake of Debian
107  * stable.
108  */
109 #define QEMU_SCRATCHSZ 16
110
111 ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz)
112 {
113   struct iovec iov;
114   struct msghdr msg;
115   ssize_t rc;
116 #ifndef HAVE_MSG_ACCRIGHTS
117   char buf[CMSG_SPACE(sizeof(fd)) + QEMU_SCRATCHSZ];
118   struct cmsghdr *cmsg;
119   int fdtmp;
120 #endif
121
122   *fd = -1;
123   iov.iov_base = p;
124   iov.iov_len = sz;
125   msg.msg_name = 0;
126   msg.msg_namelen = 0;
127   msg.msg_iov = &iov;
128   msg.msg_iovlen = 1;
129 #ifdef HAVE_MSG_ACCRIGHTS
130   msg.msg_accrights = fd;
131   msg.msg_accrightslen = sizeof(*fd);
132 #else
133   msg.msg_flags = 0;
134   msg.msg_control = buf;
135   msg.msg_controllen = sizeof(buf) - QEMU_SCRATCHSZ;
136 #endif
137   if ((rc = recvmsg(sock, &msg, 0)) < 0)
138     return (rc);
139 #ifdef HAVE_MSG_ACCRIGHTS
140   if (msg.msg_accrightslen < sizeof(*fd))
141     *fd = -1;
142 #else
143   for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
144     if (cmsg->cmsg_level == SOL_SOCKET &&
145         cmsg->cmsg_type == SCM_RIGHTS &&
146         cmsg->cmsg_len >= CMSG_LEN(sizeof(*fd))) {
147       memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp));
148       if (*fd == -1)
149         *fd = fdtmp;
150       else
151         close(fdtmp);
152     }
153   }
154 #endif
155   return (rc);
156 }
157
158 #undef QEMU_SCRATCHSZ
159
160 /*----- That's all, folks -------------------------------------------------*/