Make sure the pth_msgport_t exists before trying to lock it's mutex in
[pwmd.git] / assuan / src / assuan-uds.c
blob02f77a525f6d632d6a854da17901d9ac895dea62
1 /* assuan-uds.c - Assuan unix domain socket utilities
2 * Copyright (C) 2006 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #ifndef HAVE_W32_SYSTEM
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #else
33 #include <windows.h>
34 #endif
35 #if HAVE_SYS_UIO_H
36 #include <sys/uio.h>
37 #endif
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <assert.h>
43 #include "assuan-defs.h"
45 #ifdef USE_DESCRIPTOR_PASSING
46 /* Provide replacement for missing CMSG maccros. We assume that
47 size_t matches the alignment requirement. */
48 #define MY_ALIGN(n) ((((n))+ sizeof(size_t)-1) & (size_t)~(sizeof(size_t)-1))
49 #ifndef CMSG_SPACE
50 #define CMSG_SPACE(n) (MY_ALIGN(sizeof(struct cmsghdr)) + MY_ALIGN((n)))
51 #endif
52 #ifndef CMSG_LEN
53 #define CMSG_LEN(n) (MY_ALIGN(sizeof(struct cmsghdr)) + (n))
54 #endif
55 #ifndef CMSG_FIRSTHDR
56 #define CMSG_FIRSTHDR(mhdr) \
57 ((size_t)(mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
58 ? (struct cmsghdr*) (mhdr)->msg_control : (struct cmsghdr*)NULL)
59 #endif
60 #ifndef CMSG_DATA
61 #define CMSG_DATA(cmsg) ((unsigned char*)((struct cmsghdr*)(cmsg)+1))
62 #endif
63 #endif /*USE_DESCRIPTOR_PASSING*/
66 /* Read from a unix domain socket using sendmsg.
68 FIXME: We don't need the buffering. It is a leftover from the time
69 when we used datagrams. */
70 static ssize_t
71 uds_reader (assuan_context_t ctx, void *buf, size_t buflen)
73 #ifndef HAVE_W32_SYSTEM
74 int len = ctx->uds.buffersize;
76 if (!ctx->uds.bufferallocated)
78 ctx->uds.buffer = xtrymalloc (2048);
79 if (!ctx->uds.buffer)
80 return _assuan_error (ASSUAN_Out_Of_Core);
81 ctx->uds.bufferallocated = 2048;
84 while (!len) /* No data is buffered. */
86 struct msghdr msg;
87 struct iovec iovec;
88 #ifdef USE_DESCRIPTOR_PASSING
89 union {
90 struct cmsghdr cm;
91 char control[CMSG_SPACE(sizeof (int))];
92 } control_u;
93 struct cmsghdr *cmptr;
94 #endif /*USE_DESCRIPTOR_PASSING*/
96 memset (&msg, 0, sizeof (msg));
98 msg.msg_name = NULL;
99 msg.msg_namelen = 0;
100 msg.msg_iov = &iovec;
101 msg.msg_iovlen = 1;
102 iovec.iov_base = ctx->uds.buffer;
103 iovec.iov_len = ctx->uds.bufferallocated;
104 #ifdef USE_DESCRIPTOR_PASSING
105 msg.msg_control = control_u.control;
106 msg.msg_controllen = sizeof (control_u.control);
107 #endif
109 len = _assuan_simple_recvmsg (ctx, &msg);
110 if (len < 0)
111 return -1;
112 if (len == 0)
113 return 0;
115 ctx->uds.buffersize = len;
116 ctx->uds.bufferoffset = 0;
118 #ifdef USE_DESCRIPTOR_PASSING
119 cmptr = CMSG_FIRSTHDR (&msg);
120 if (cmptr && cmptr->cmsg_len == CMSG_LEN (sizeof(int)))
122 if (cmptr->cmsg_level != SOL_SOCKET
123 || cmptr->cmsg_type != SCM_RIGHTS)
124 _assuan_log_printf ("unexpected ancillary data received\n");
125 else
127 int fd = *((int*)CMSG_DATA (cmptr));
129 if (ctx->uds.pendingfdscount >= DIM (ctx->uds.pendingfds))
131 _assuan_log_printf ("too many descriptors pending - "
132 "closing received descriptor %d\n", fd);
133 _assuan_close (fd);
135 else
136 ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = fd;
139 #endif /*USE_DESCRIPTOR_PASSING*/
142 /* Return some data to the user. */
144 if (len > buflen) /* We have more than the user requested. */
145 len = buflen;
147 memcpy (buf, (char*)ctx->uds.buffer + ctx->uds.bufferoffset, len);
148 ctx->uds.buffersize -= len;
149 assert (ctx->uds.buffersize >= 0);
150 ctx->uds.bufferoffset += len;
151 assert (ctx->uds.bufferoffset <= ctx->uds.bufferallocated);
153 return len;
154 #else /*HAVE_W32_SYSTEM*/
155 int res = recvfrom (HANDLE2SOCKET(ctx->inbound.fd), buf, buflen, 0, NULL, NULL);
156 if (res < 0)
157 errno = _assuan_sock_wsa2errno (WSAGetLastError ());
158 return res;
159 #endif /*HAVE_W32_SYSTEM*/
163 /* Write to the domain server. */
164 static ssize_t
165 uds_writer (assuan_context_t ctx, const void *buf, size_t buflen)
167 #ifndef HAVE_W32_SYSTEM
168 struct msghdr msg;
169 struct iovec iovec;
170 ssize_t len;
172 memset (&msg, 0, sizeof (msg));
174 msg.msg_name = NULL;
175 msg.msg_namelen = 0;
176 msg.msg_iovlen = 1;
177 msg.msg_iov = &iovec;
178 iovec.iov_base = (void*)buf;
179 iovec.iov_len = buflen;
181 len = _assuan_simple_sendmsg (ctx, &msg);
183 return len;
184 #else /*HAVE_W32_SYSTEM*/
185 int res = sendto (HANDLE2SOCKET(ctx->outbound.fd), buf, buflen, 0,
186 (struct sockaddr *)&ctx->serveraddr,
187 sizeof (struct sockaddr_in));
188 if (res < 0)
189 errno = _assuan_sock_wsa2errno (WSAGetLastError ());
190 return res;
191 #endif /*HAVE_W32_SYSTEM*/
195 static assuan_error_t
196 uds_sendfd (assuan_context_t ctx, assuan_fd_t fd)
198 #ifdef USE_DESCRIPTOR_PASSING
199 struct msghdr msg;
200 struct iovec iovec;
201 union {
202 struct cmsghdr cm;
203 char control[CMSG_SPACE(sizeof (int))];
204 } control_u;
205 struct cmsghdr *cmptr;
206 int len;
207 char buffer[80];
209 /* We need to send some real data so that a read won't return 0
210 which will be taken as an EOF. It also helps with debugging. */
211 snprintf (buffer, sizeof(buffer)-1, "# descriptor %d is in flight\n", fd);
212 buffer[sizeof(buffer)-1] = 0;
214 memset (&msg, 0, sizeof (msg));
216 msg.msg_name = NULL;
217 msg.msg_namelen = 0;
218 msg.msg_iovlen = 1;
219 msg.msg_iov = &iovec;
220 iovec.iov_base = buffer;
221 iovec.iov_len = strlen (buffer);
223 msg.msg_control = control_u.control;
224 msg.msg_controllen = sizeof (control_u.control);
225 cmptr = CMSG_FIRSTHDR (&msg);
226 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
227 cmptr->cmsg_level = SOL_SOCKET;
228 cmptr->cmsg_type = SCM_RIGHTS;
229 *((int*)CMSG_DATA (cmptr)) = fd;
231 len = _assuan_simple_sendmsg (ctx, &msg);
232 if (len < 0)
234 _assuan_log_printf ("uds_sendfd: %s\n", strerror (errno));
235 return _assuan_error (ASSUAN_Write_Error);
237 else
238 return 0;
239 #else
240 return _assuan_error (ASSUAN_Not_Implemented);
241 #endif
245 static assuan_error_t
246 uds_receivefd (assuan_context_t ctx, assuan_fd_t *fd)
248 #ifdef USE_DESCRIPTOR_PASSING
249 int i;
251 if (!ctx->uds.pendingfdscount)
253 _assuan_log_printf ("no pending file descriptors!\n");
254 return _assuan_error (ASSUAN_General_Error);
256 assert (ctx->uds.pendingfdscount <= DIM(ctx->uds.pendingfds));
258 *fd = ctx->uds.pendingfds[0];
259 for (i=1; i < ctx->uds.pendingfdscount; i++)
260 ctx->uds.pendingfds[i-1] = ctx->uds.pendingfds[i];
261 ctx->uds.pendingfdscount--;
263 return 0;
264 #else
265 return _assuan_error (ASSUAN_Not_Implemented);
266 #endif
270 /* Close all pending fds. */
271 void
272 _assuan_uds_close_fds (assuan_context_t ctx)
274 int i;
276 for (i = 0; i < ctx->uds.pendingfdscount; i++)
277 _assuan_close (ctx->uds.pendingfds[i]);
278 ctx->uds.pendingfdscount = 0;
281 /* Deinitialize the unix domain socket I/O functions. */
282 void
283 _assuan_uds_deinit (assuan_context_t ctx)
285 /* First call the finish_handler which should close descriptors etc. */
286 ctx->finish_handler (ctx);
288 if (ctx->uds.buffer)
290 assert (ctx->uds.bufferallocated);
291 ctx->uds.bufferallocated = 0;
292 xfree (ctx->uds.buffer);
295 _assuan_uds_close_fds (ctx);
299 /* Helper function to initialize a context for domain I/O. */
300 void
301 _assuan_init_uds_io (assuan_context_t ctx)
303 static struct assuan_io io = { uds_reader, uds_writer,
304 uds_sendfd, uds_receivefd };
306 ctx->io = &io;
307 ctx->uds.buffer = 0;
308 ctx->uds.bufferoffset = 0;
309 ctx->uds.buffersize = 0;
310 ctx->uds.bufferallocated = 0;
311 ctx->uds.pendingfdscount = 0;