Debian Codegraph @

src/assuan/assuan-uds.c

raw symbols SCIP
1 /* assuan-uds.c - Assuan unix domain socket utilities
2 * Copyright (C) 2006 Free Software Foundation, Inc.
3 *
4 * This file is part of Assuan.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:410:9`!
21 #include <config.h>cxx . . $ `<file>/config.h`/
22 #endif
23
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_Hcxx . . $ `config.h:73:9`!
36 #include <sys/uio.h>
37 #endif
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <assert.h>
42
43 #include "assuan-defs.h"cxx . . $ `<file>/src/assuan/assuan-defs.h`/
44
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*/
64
65
66 /* Read from a unix domain socket using sendmsg.
67
68 FIXME: We don't need the buffering. It is a leftover from the time
69 when we used datagrams. */
70 static ssize_tcxx . . $ ssize_t#
71 uds_readercxx . . $ uds_reader(b497eb30b6de61d8). (assuan_context_tcxx . . $ assuan_context_t# ctx, void *buf, size_tcxx . . $ size_t# buflen)
72 {
73 #ifndef HAVE_W32_SYSTEM
74 int len = ctx->udscxx . . $ assuan_context_s#uds..buffersizecxx . . $ assuan_context_s#$anonymous_type_6#buffersize.;
75
76 if (!ctx->udscxx . . $ assuan_context_s#uds..bufferallocatedcxx . . $ assuan_context_s#$anonymous_type_6#bufferallocated.)
77 {
78 ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer. = xtrymalloccxx . . $ `src/assuan/assuan-defs.h:261:9`! (2048);
79 if (!ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer.)
80 return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Out_Of_Corecxx . . $ `src/assuan/assuan.h:239:10`!);
81 ctx->udscxx . . $ assuan_context_s#uds..bufferallocatedcxx . . $ assuan_context_s#$anonymous_type_6#bufferallocated. = 2048;
82 }
83
84 while (!len) /* No data is buffered. */
85 {
86 struct msghdrcxx . . $ msghdr# msg;
87 struct ioveccxx . . $ 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*/
95
96 memsetcxx . . $ memset(d1de1e4fb215f1c6). (&msg, 0, sizeof (msg));
97
98 msg.msg_namecxx . . $ msghdr#msg_name. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
99 msg.msg_namelencxx . . $ msghdr#msg_namelen. = 0;
100 msg.msg_iovcxx . . $ msghdr#msg_iov. = &iovec;
101 msg.msg_iovlencxx . . $ msghdr#msg_iovlen. = 1;
102 iovec.iov_basecxx . . $ iovec#iov_base. = ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer.;
103 iovec.iov_lencxx . . $ iovec#iov_len. = ctx->udscxx . . $ assuan_context_s#uds..bufferallocatedcxx . . $ assuan_context_s#$anonymous_type_6#bufferallocated.;
104 #ifdef USE_DESCRIPTOR_PASSING
105 msg.msg_control = control_u.control;
106 msg.msg_controllen = sizeof (control_u.control);
107 #endif
108
109 len = _assuan_simple_recvmsgcxx . . $ `src/assuan/assuan.h:194:9`! (ctx, &msg);
110 if (len < 0)
111 return -1;
112 if (len == 0)
113 return 0;
114
115 ctx->udscxx . . $ assuan_context_s#uds..buffersizecxx . . $ assuan_context_s#$anonymous_type_6#buffersize. = len;
116 ctx->udscxx . . $ assuan_context_s#uds..bufferoffsetcxx . . $ assuan_context_s#$anonymous_type_6#bufferoffset. = 0;
117
118 #ifdef USE_DESCRIPTOR_PASSING
119 cmptr = CMSG_FIRSTHDR (&msg);
120 if (cmptr && cmptr->cmsg_len == CMSG_LEN (sizeof(int)))
121 {
122 if (cmptr->cmsg_level != SOL_SOCKET
123 || cmptr->cmsg_type != SCM_RIGHTS)
124 _assuan_log_printf ("unexpected ancillary data received\n");
125 else
126 {
127 int fd = *((int*)CMSG_DATA (cmptr));
128
129 if (ctx->uds.pendingfdscount >= DIM (ctx->uds.pendingfds))
130 {
131 _assuan_log_printf ("too many descriptors pending - "
132 "closing received descriptor %d\n", fd);
133 _assuan_close (fd);
134 }
135 else
136 ctx->uds.pendingfds[ctx->uds.pendingfdscount++] = fd;
137 }
138 }
139 #endif /*USE_DESCRIPTOR_PASSING*/
140 }
141
142 /* Return some data to the user. */
143
144 if (len > buflen) /* We have more than the user requested. */
145 len = buflen;
146
147 memcpycxx . . $ memcpy(cc817cb175f282d). (buf, (char*)ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer. + ctx->udscxx . . $ assuan_context_s#uds..bufferoffsetcxx . . $ assuan_context_s#$anonymous_type_6#bufferoffset., len);
148 ctx->udscxx . . $ assuan_context_s#uds..buffersizecxx . . $ assuan_context_s#$anonymous_type_6#buffersize. -= len;
149 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (ctx->uds.buffersize >= 0);
150 ctx->udscxx . . $ assuan_context_s#uds..bufferoffsetcxx . . $ assuan_context_s#$anonymous_type_6#bufferoffset. += len;
151 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (ctx->uds.bufferoffset <= ctx->uds.bufferallocated);
152
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*/
160 }
161
162
163 /* Write to the domain server. */
164 static ssize_tcxx . . $ ssize_t#
165 uds_writercxx . . $ uds_writer(d04d63104e2e3469). (assuan_context_tcxx . . $ assuan_context_t# ctx, const void *buf, size_tcxx . . $ size_t# buflen)
166 {
167 #ifndef HAVE_W32_SYSTEM
168 struct msghdrcxx . . $ msghdr# msg;
169 struct ioveccxx . . $ iovec# iovec;
170 ssize_tcxx . . $ ssize_t# len;
171
172 memsetcxx . . $ memset(d1de1e4fb215f1c6). (&msg, 0, sizeof (msg));
173
174 msg.msg_namecxx . . $ msghdr#msg_name. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
175 msg.msg_namelencxx . . $ msghdr#msg_namelen. = 0;
176 msg.msg_iovlencxx . . $ msghdr#msg_iovlen. = 1;
177 msg.msg_iovcxx . . $ msghdr#msg_iov. = &iovec;
178 iovec.iov_basecxx . . $ iovec#iov_base. = (void*)buf;
179 iovec.iov_lencxx . . $ iovec#iov_len. = buflen;
180
181 len = _assuan_simple_sendmsgcxx . . $ `src/assuan/assuan.h:195:9`! (ctx, &msg);
182
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*/
192 }
193
194
195 static assuan_error_tcxx . . $ assuan_error_t#
196 uds_sendfdcxx . . $ uds_sendfd(4ec17fed7bee2d1b). (assuan_context_tcxx . . $ assuan_context_t# ctx, assuan_fd_tcxx . . $ assuan_fd_t# fd)
197 {
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];
208
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;
213
214 memset (&msg, 0, sizeof (msg));
215
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);
222
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;
230
231 len = _assuan_simple_sendmsg (ctx, &msg);
232 if (len < 0)
233 {
234 _assuan_log_printf ("uds_sendfd: %s\n", strerror (errno));
235 return _assuan_error (ASSUAN_Write_Error);
236 }
237 else
238 return 0;
239 #else
240 return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Not_Implementedcxx . . $ `src/assuan/assuan.h:259:10`!);
241 #endif
242 }
243
244
245 static assuan_error_tcxx . . $ assuan_error_t#
246 uds_receivefdcxx . . $ uds_receivefd(3a1234f60f87babe). (assuan_context_tcxx . . $ assuan_context_t# ctx, assuan_fd_tcxx . . $ assuan_fd_t# *fd)
247 {
248 #ifdef USE_DESCRIPTOR_PASSING
249 int i;
250
251 if (!ctx->uds.pendingfdscount)
252 {
253 _assuan_log_printf ("no pending file descriptors!\n");
254 return _assuan_error (ASSUAN_General_Error);
255 }
256 assert (ctx->uds.pendingfdscount <= DIM(ctx->uds.pendingfds));
257
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--;
262
263 return 0;
264 #else
265 return _assuan_errorcxx . . $ `src/assuan/assuan.h:189:9`! (ASSUAN_Not_Implementedcxx . . $ `src/assuan/assuan.h:259:10`!);
266 #endif
267 }
268
269
270 /* Close all pending fds. */
271 void
272 _assuan_uds_close_fdscxx . . $ `src/assuan/assuan.h:192:9`! (assuan_context_tcxx . . $ assuan_context_t# ctx)
273 {
274 int i;
275
276 for (i = 0; i < ctx->udscxx . . $ assuan_context_s#uds..pendingfdscountcxx . . $ assuan_context_s#$anonymous_type_6#pendingfdscount.; i++)
277 _assuan_closecxx . . $ _assuan_close(7864480464b09eea). (ctx->udscxx . . $ assuan_context_s#uds..pendingfdscxx . . $ assuan_context_s#$anonymous_type_6#pendingfds.[i]);
278 ctx->udscxx . . $ assuan_context_s#uds..pendingfdscountcxx . . $ assuan_context_s#$anonymous_type_6#pendingfdscount. = 0;
279 }
280
281 /* Deinitialize the unix domain socket I/O functions. */
282 void
283 _assuan_uds_deinitcxx . . $ `src/assuan/assuan.h:193:9`! (assuan_context_tcxx . . $ assuan_context_t# ctx)
284 {
285 /* First call the finish_handler which should close descriptors etc. */
286 ctx->finish_handlercxx . . $ assuan_context_s#finish_handler. (ctx);
287
288 if (ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer.)
289 {
290 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (ctx->uds.bufferallocated);
291 ctx->udscxx . . $ assuan_context_s#uds..bufferallocatedcxx . . $ assuan_context_s#$anonymous_type_6#bufferallocated. = 0;
292 xfreecxx . . $ `src/assuan/assuan-defs.h:264:9`! (ctx->uds.buffer);
293 }
294
295 _assuan_uds_close_fdscxx . . $ `src/assuan/assuan.h:192:9`! (ctx);
296 }
297
298
299 /* Helper function to initialize a context for domain I/O. */
300 void
301 _assuan_init_uds_iocxx . . $ `src/assuan/assuan.h:191:9`! (assuan_context_tcxx . . $ assuan_context_t# ctx)
302 {
303 static struct assuan_iocxx . . $ assuan_io# io = { uds_readercxx . . $ uds_reader(b497eb30b6de61d8)., uds_writercxx . . $ uds_writer(d04d63104e2e3469).,
304 uds_sendfdcxx . . $ uds_sendfd(4ec17fed7bee2d1b)., uds_receivefdcxx . . $ uds_receivefd(3a1234f60f87babe). };
305
306 ctx->iocxx . . $ assuan_context_s#io. = &io;
307 ctx->udscxx . . $ assuan_context_s#uds..buffercxx . . $ assuan_context_s#$anonymous_type_6#buffer. = 0;
308 ctx->udscxx . . $ assuan_context_s#uds..bufferoffsetcxx . . $ assuan_context_s#$anonymous_type_6#bufferoffset. = 0;
309 ctx->udscxx . . $ assuan_context_s#uds..buffersizecxx . . $ assuan_context_s#$anonymous_type_6#buffersize. = 0;
310 ctx->udscxx . . $ assuan_context_s#uds..bufferallocatedcxx . . $ assuan_context_s#$anonymous_type_6#bufferallocated. = 0;
311 ctx->udscxx . . $ assuan_context_s#uds..pendingfdscountcxx . . $ assuan_context_s#$anonymous_type_6#pendingfdscount. = 0;
312 }
313