chiark / gitweb /
@@@ random mess
[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 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42 #include <sys/un.h>
43
44 #include "fdpass.h"
45 #include "macros.h"
46
47 /*----- Main code ---------------------------------------------------------*/
48
49 /* --- @fdpass_send@ --- *
50  *
51  * Arguments:   @int sock@ = socket to send over
52  *              @int fd@ = file descriptor to send
53  *              @const void *p@ = pointer to data to send
54  *              @size_t sz@ = size of buffer to send
55  *
56  * Returns:     On error, @-1@, otherwise number of bytes transferred from
57  *              @p@.
58  *
59  * Use:         Sends a copy of file descriptor @fd@ to the other end of
60  *              @sock@.
61  */
62
63 ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz)
64 {
65   struct iovec iov;
66   struct msghdr msg;
67 #ifndef HAVE_MSG_ACCRIGHTS
68   char buf[CMSG_SPACE(sizeof(fd))];
69   struct cmsghdr *cmsg;
70 #endif
71
72   iov.iov_base = UNCONST(void, p);
73   iov.iov_len = sz;
74   msg.msg_name = 0;
75   msg.msg_namelen = 0;
76   msg.msg_iov = &iov;
77   msg.msg_iovlen = 1;
78 #ifdef HAVE_MSG_ACCRIGHTS
79   msg.msg_accrights = &fd;
80   msg.msg_accrightslen = sizeof(fd);
81 #else
82   msg.msg_flags = 0;
83   msg.msg_control = buf;
84   msg.msg_controllen = sizeof(buf);
85   cmsg = CMSG_FIRSTHDR(&msg);
86   cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
87   cmsg->cmsg_level = SOL_SOCKET;
88   cmsg->cmsg_type = SCM_RIGHTS;
89   memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
90 #endif
91   return (sendmsg(sock, &msg, 0));
92 }
93
94 /* --- @fdpass_recv@ --- *
95  *
96  * Arguments:   @int sock@ = socket to send over
97  *              @int *fd@ = where to put received descriptor
98  *              @void *p@ = pointer to where to put data
99  *              @size_t sz@ = size of buffer
100  *
101  * Returns:     On error, @-1@, otherwise number of bytes transferred.
102  *
103  * Use:         Receives a file descriptor.  If the call succeeds, and there
104  *              was a file descriptor, then @fd@ won't be @-1@ on exit;
105  *              otherwise it will.  At most one descriptor will be collected.
106  */
107
108 /* Qemu 2.8.1 clobbers 12 bytes beyond the end of the control-message
109  * buffer.  This is fixed in 2.12, but I'll bodge it for the sake of Debian
110  * stable.
111  */
112 #define QEMU_SCRATCHSZ 16
113
114 ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz)
115 {
116   struct iovec iov;
117   struct msghdr msg;
118   ssize_t rc;
119 #ifndef HAVE_MSG_ACCRIGHTS
120   char buf[CMSG_SPACE(sizeof(fd)) + QEMU_SCRATCHSZ];
121   struct cmsghdr *cmsg;
122   int fdtmp;
123 #endif
124
125   *fd = -1;
126   iov.iov_base = p;
127   iov.iov_len = sz;
128   msg.msg_name = 0;
129   msg.msg_namelen = 0;
130   msg.msg_iov = &iov;
131   msg.msg_iovlen = 1;
132 #ifdef HAVE_MSG_ACCRIGHTS
133   msg.msg_accrights = fd;
134   msg.msg_accrightslen = sizeof(*fd);
135 #else
136   msg.msg_flags = 0;
137   msg.msg_control = buf;
138   msg.msg_controllen = sizeof(buf) - QEMU_SCRATCHSZ;
139 #endif
140   if ((rc = recvmsg(sock, &msg, 0)) < 0)
141     return (rc);
142 #ifdef HAVE_MSG_ACCRIGHTS
143   if (msg.msg_accrightslen < sizeof(*fd))
144     *fd = -1;
145 #else
146   for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
147     if (cmsg->cmsg_level == SOL_SOCKET &&
148         cmsg->cmsg_type == SCM_RIGHTS &&
149         cmsg->cmsg_len >= CMSG_LEN(sizeof(*fd))) {
150       memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp));
151       if (*fd == -1)
152         *fd = fdtmp;
153       else
154         close(fdtmp);
155     }
156   }
157 #endif
158   return (rc);
159 }
160
161 #undef QEMU_SCRATCHSZ
162
163 /*----- That's all, folks -------------------------------------------------*/