Commit | Line | Data |
---|---|---|
0d37f2bc | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
0d37f2bc | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
0d37f2bc | 6 | * |
0d37f2bc DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
0d37f2bc DG |
10 | #include <limits.h> |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/types.h> | |
16 | #include <unistd.h> | |
0d37f2bc | 17 | |
90e535ef | 18 | #include <common/common.h> |
edf4b93e | 19 | #include <common/compat/errno.h> |
2038dd6c | 20 | #include <common/sessiond-comm/sessiond-comm.h> |
fe489250 | 21 | #include <common/fd-handle.h> |
0d37f2bc DG |
22 | |
23 | #include "unix.h" | |
24 | ||
25 | /* | |
26 | * Connect to unix socket using the path name. | |
27 | */ | |
90e535ef | 28 | LTTNG_HIDDEN |
0d37f2bc DG |
29 | int lttcomm_connect_unix_sock(const char *pathname) |
30 | { | |
665886a6 | 31 | struct sockaddr_un s_un; |
0d37f2bc DG |
32 | int fd, ret, closeret; |
33 | ||
7f8bf467 JG |
34 | if (strlen(pathname) >= sizeof(s_un.sun_path)) { |
35 | ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).", | |
36 | pathname, strlen(pathname) + 1, | |
37 | sizeof(s_un.sun_path)); | |
38 | ret = -ENAMETOOLONG; | |
39 | goto error; | |
40 | } | |
41 | ||
0d37f2bc DG |
42 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
43 | if (fd < 0) { | |
44 | PERROR("socket"); | |
45 | ret = fd; | |
46 | goto error; | |
47 | } | |
48 | ||
665886a6 MJ |
49 | memset(&s_un, 0, sizeof(s_un)); |
50 | s_un.sun_family = AF_UNIX; | |
51 | strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path)); | |
52 | s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; | |
0d37f2bc | 53 | |
665886a6 | 54 | ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
0d37f2bc DG |
55 | if (ret < 0) { |
56 | /* | |
57 | * Don't print message on connect error, because connect is used in | |
58 | * normal execution to detect if sessiond is alive. | |
59 | */ | |
60 | goto error_connect; | |
61 | } | |
62 | ||
63 | return fd; | |
64 | ||
65 | error_connect: | |
66 | closeret = close(fd); | |
67 | if (closeret) { | |
68 | PERROR("close"); | |
69 | } | |
70 | error: | |
71 | return ret; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Do an accept(2) on the sock and return the new file descriptor. The socket | |
76 | * MUST be bind(2) before. | |
77 | */ | |
90e535ef | 78 | LTTNG_HIDDEN |
0d37f2bc DG |
79 | int lttcomm_accept_unix_sock(int sock) |
80 | { | |
81 | int new_fd; | |
665886a6 | 82 | struct sockaddr_un s_un; |
50786a72 | 83 | socklen_t len = sizeof(s_un); |
0d37f2bc DG |
84 | |
85 | /* Blocking call */ | |
665886a6 | 86 | new_fd = accept(sock, (struct sockaddr *) &s_un, &len); |
0d37f2bc DG |
87 | if (new_fd < 0) { |
88 | PERROR("accept"); | |
89 | } | |
90 | ||
91 | return new_fd; | |
92 | } | |
93 | ||
7567352f MD |
94 | LTTNG_HIDDEN |
95 | int lttcomm_create_anon_unix_socketpair(int *fds) | |
96 | { | |
97 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) < 0) { | |
98 | PERROR("socketpair"); | |
99 | return -1; | |
100 | } | |
101 | return 0; | |
102 | } | |
103 | ||
0d37f2bc DG |
104 | /* |
105 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation | |
106 | * and return the fd. | |
107 | */ | |
90e535ef | 108 | LTTNG_HIDDEN |
0d37f2bc DG |
109 | int lttcomm_create_unix_sock(const char *pathname) |
110 | { | |
665886a6 | 111 | struct sockaddr_un s_un; |
7f8bf467 | 112 | int fd = -1; |
0d37f2bc DG |
113 | int ret = -1; |
114 | ||
7f8bf467 JG |
115 | if (strlen(pathname) >= sizeof(s_un.sun_path)) { |
116 | ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).", | |
117 | pathname, strlen(pathname) + 1, | |
118 | sizeof(s_un.sun_path)); | |
119 | ret = -ENAMETOOLONG; | |
120 | goto error; | |
121 | } | |
122 | ||
0d37f2bc DG |
123 | /* Create server socket */ |
124 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
125 | PERROR("socket"); | |
126 | goto error; | |
127 | } | |
128 | ||
665886a6 MJ |
129 | memset(&s_un, 0, sizeof(s_un)); |
130 | s_un.sun_family = AF_UNIX; | |
131 | strncpy(s_un.sun_path, pathname, sizeof(s_un.sun_path)); | |
132 | s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; | |
0d37f2bc DG |
133 | |
134 | /* Unlink the old file if present */ | |
135 | (void) unlink(pathname); | |
665886a6 | 136 | ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
0d37f2bc DG |
137 | if (ret < 0) { |
138 | PERROR("bind"); | |
139 | goto error; | |
140 | } | |
141 | ||
142 | return fd; | |
143 | ||
144 | error: | |
17e75273 DG |
145 | if (fd >= 0) { |
146 | if (close(fd) < 0) { | |
147 | PERROR("close create unix sock"); | |
148 | } | |
149 | } | |
0d37f2bc DG |
150 | return ret; |
151 | } | |
152 | ||
153 | /* | |
154 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
155 | */ | |
90e535ef | 156 | LTTNG_HIDDEN |
0d37f2bc DG |
157 | int lttcomm_listen_unix_sock(int sock) |
158 | { | |
159 | int ret; | |
160 | ||
161 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); | |
162 | if (ret < 0) { | |
163 | PERROR("listen"); | |
164 | } | |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
171 | * API. | |
172 | * | |
173 | * Return the size of received data. | |
174 | */ | |
90e535ef | 175 | LTTNG_HIDDEN |
0d37f2bc DG |
176 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
177 | { | |
178 | struct msghdr msg; | |
179 | struct iovec iov[1]; | |
180 | ssize_t ret = -1; | |
7c5aef62 | 181 | size_t len_last; |
0d37f2bc | 182 | |
a0377dfe FD |
183 | LTTNG_ASSERT(sock); |
184 | LTTNG_ASSERT(buf); | |
185 | LTTNG_ASSERT(len > 0); | |
982583bd | 186 | |
0d37f2bc DG |
187 | memset(&msg, 0, sizeof(msg)); |
188 | ||
189 | iov[0].iov_base = buf; | |
190 | iov[0].iov_len = len; | |
191 | msg.msg_iov = iov; | |
192 | msg.msg_iovlen = 1; | |
193 | ||
194 | do { | |
7c5aef62 | 195 | len_last = iov[0].iov_len; |
fbb1fd3a | 196 | ret = lttng_recvmsg_nosigpipe(sock, &msg); |
7c5aef62 DG |
197 | if (ret > 0) { |
198 | iov[0].iov_base += ret; | |
199 | iov[0].iov_len -= ret; | |
a0377dfe | 200 | LTTNG_ASSERT(ret <= len_last); |
7c5aef62 DG |
201 | } |
202 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
203 | if (ret < 0) { |
204 | PERROR("recvmsg"); | |
7c5aef62 DG |
205 | } else if (ret > 0) { |
206 | ret = len; | |
0d37f2bc | 207 | } |
7c5aef62 | 208 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
209 | |
210 | return ret; | |
211 | } | |
212 | ||
c72435ad JG |
213 | /* |
214 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
215 | * API. Only use with sockets set in non-blocking mode. | |
216 | * | |
44c180ca JR |
217 | * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a |
218 | * poll set. The poll loop will handle the EPIPE original cause. | |
219 | * | |
c72435ad JG |
220 | * Return the size of received data. |
221 | */ | |
222 | LTTNG_HIDDEN | |
223 | ssize_t lttcomm_recv_unix_sock_non_block(int sock, void *buf, size_t len) | |
224 | { | |
225 | struct msghdr msg; | |
226 | struct iovec iov[1]; | |
227 | ssize_t ret; | |
228 | ||
a0377dfe FD |
229 | LTTNG_ASSERT(sock); |
230 | LTTNG_ASSERT(buf); | |
231 | LTTNG_ASSERT(len > 0); | |
982583bd | 232 | |
c72435ad JG |
233 | memset(&msg, 0, sizeof(msg)); |
234 | ||
235 | iov[0].iov_base = buf; | |
236 | iov[0].iov_len = len; | |
237 | msg.msg_iov = iov; | |
238 | msg.msg_iovlen = 1; | |
239 | ||
240 | retry: | |
241 | ret = lttng_recvmsg_nosigpipe(sock, &msg); | |
242 | if (ret < 0) { | |
243 | if (errno == EINTR) { | |
244 | goto retry; | |
245 | } else { | |
44c180ca JR |
246 | /* |
247 | * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected. | |
248 | */ | |
249 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
250 | errno == EPIPE) { | |
251 | /* | |
252 | * Nothing was recv. | |
253 | */ | |
254 | ret = 0; | |
255 | goto end; | |
c72435ad | 256 | } |
44c180ca JR |
257 | |
258 | /* Unexpected error */ | |
259 | PERROR("recvmsg"); | |
260 | ret = -1; | |
c72435ad JG |
261 | goto end; |
262 | } | |
263 | } | |
5b86bd5e | 264 | |
c72435ad JG |
265 | end: |
266 | return ret; | |
267 | } | |
268 | ||
0d37f2bc DG |
269 | /* |
270 | * Send buf data of size len. Using sendmsg API. | |
271 | * | |
272 | * Return the size of sent data. | |
273 | */ | |
90e535ef | 274 | LTTNG_HIDDEN |
c2d69327 | 275 | ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len) |
0d37f2bc DG |
276 | { |
277 | struct msghdr msg; | |
278 | struct iovec iov[1]; | |
c72435ad | 279 | ssize_t ret; |
0d37f2bc | 280 | |
a0377dfe FD |
281 | LTTNG_ASSERT(sock); |
282 | LTTNG_ASSERT(buf); | |
283 | LTTNG_ASSERT(len > 0); | |
982583bd | 284 | |
0d37f2bc DG |
285 | memset(&msg, 0, sizeof(msg)); |
286 | ||
c2d69327 | 287 | iov[0].iov_base = (void *) buf; |
0d37f2bc DG |
288 | iov[0].iov_len = len; |
289 | msg.msg_iov = iov; | |
290 | msg.msg_iovlen = 1; | |
291 | ||
c72435ad JG |
292 | while (iov[0].iov_len) { |
293 | ret = sendmsg(sock, &msg, 0); | |
294 | if (ret < 0) { | |
295 | if (errno == EINTR) { | |
296 | continue; | |
297 | } else { | |
298 | /* | |
299 | * Only warn about EPIPE when quiet mode is | |
300 | * deactivated. | |
301 | * We consider EPIPE as expected. | |
302 | */ | |
303 | if (errno != EPIPE || !lttng_opt_quiet) { | |
304 | PERROR("sendmsg"); | |
305 | } | |
306 | goto end; | |
307 | } | |
308 | } | |
309 | iov[0].iov_len -= ret; | |
310 | iov[0].iov_base += ret; | |
311 | } | |
312 | ret = len; | |
313 | end: | |
314 | return ret; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Send buf data of size len. Using sendmsg API. | |
319 | * Only use with non-blocking sockets. The difference with the blocking version | |
320 | * of the function is that this one does not retry to send on partial sends, | |
321 | * except if the interruption was caused by a signal (EINTR). | |
322 | * | |
44c180ca JR |
323 | * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a |
324 | * poll set. The poll loop will handle the EPIPE original cause. | |
325 | * | |
c72435ad JG |
326 | * Return the size of sent data. |
327 | */ | |
328 | LTTNG_HIDDEN | |
329 | ssize_t lttcomm_send_unix_sock_non_block(int sock, const void *buf, size_t len) | |
330 | { | |
331 | struct msghdr msg; | |
332 | struct iovec iov[1]; | |
333 | ssize_t ret; | |
334 | ||
a0377dfe FD |
335 | LTTNG_ASSERT(sock); |
336 | LTTNG_ASSERT(buf); | |
337 | LTTNG_ASSERT(len > 0); | |
982583bd | 338 | |
c72435ad JG |
339 | memset(&msg, 0, sizeof(msg)); |
340 | ||
341 | iov[0].iov_base = (void *) buf; | |
342 | iov[0].iov_len = len; | |
343 | msg.msg_iov = iov; | |
344 | msg.msg_iovlen = 1; | |
345 | ||
346 | retry: | |
0d37f2bc DG |
347 | ret = sendmsg(sock, &msg, 0); |
348 | if (ret < 0) { | |
c72435ad JG |
349 | if (errno == EINTR) { |
350 | goto retry; | |
351 | } else { | |
44c180ca JR |
352 | /* |
353 | * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected. | |
354 | */ | |
355 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
356 | errno == EPIPE) { | |
357 | /* | |
358 | * This can happen in non blocking mode. | |
359 | * Nothing was sent. | |
360 | */ | |
361 | ret = 0; | |
362 | goto end; | |
c72435ad | 363 | } |
44c180ca JR |
364 | |
365 | /* Unexpected error */ | |
366 | PERROR("sendmsg"); | |
367 | ret = -1; | |
c72435ad | 368 | goto end; |
0d37f2bc DG |
369 | } |
370 | } | |
c72435ad | 371 | end: |
0d37f2bc DG |
372 | return ret; |
373 | } | |
374 | ||
375 | /* | |
376 | * Shutdown cleanly a unix socket. | |
377 | */ | |
90e535ef | 378 | LTTNG_HIDDEN |
0d37f2bc DG |
379 | int lttcomm_close_unix_sock(int sock) |
380 | { | |
381 | int ret, closeret; | |
382 | ||
383 | /* Shutdown receptions and transmissions */ | |
384 | ret = shutdown(sock, SHUT_RDWR); | |
385 | if (ret < 0) { | |
386 | PERROR("shutdown"); | |
387 | } | |
388 | ||
389 | closeret = close(sock); | |
390 | if (closeret) { | |
391 | PERROR("close"); | |
392 | } | |
393 | ||
394 | return ret; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Send a message accompanied by fd(s) over a unix socket. | |
399 | * | |
400 | * Returns the size of data sent, or negative error value. | |
401 | */ | |
90e535ef | 402 | LTTNG_HIDDEN |
ac2f30af | 403 | ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd) |
0d37f2bc DG |
404 | { |
405 | struct msghdr msg; | |
406 | struct cmsghdr *cmptr; | |
407 | struct iovec iov[1]; | |
408 | ssize_t ret = -1; | |
409 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
410 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
411 | char dummy = 0; | |
412 | ||
a0377dfe FD |
413 | LTTNG_ASSERT(sock); |
414 | LTTNG_ASSERT(fds); | |
415 | LTTNG_ASSERT(nb_fd > 0); | |
982583bd | 416 | |
0d37f2bc | 417 | memset(&msg, 0, sizeof(msg)); |
b3f35e02 | 418 | memset(tmp, 0, sizeof(tmp)); |
0d37f2bc DG |
419 | |
420 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) | |
421 | return -EINVAL; | |
422 | ||
423 | msg.msg_control = (caddr_t)tmp; | |
424 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
425 | ||
426 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
427 | if (!cmptr) { |
428 | return -1; | |
429 | } | |
b3f35e02 | 430 | |
0d37f2bc DG |
431 | cmptr->cmsg_level = SOL_SOCKET; |
432 | cmptr->cmsg_type = SCM_RIGHTS; | |
433 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
434 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
435 | /* Sum of the length of all control messages in the buffer: */ | |
436 | msg.msg_controllen = cmptr->cmsg_len; | |
437 | ||
438 | iov[0].iov_base = &dummy; | |
439 | iov[0].iov_len = 1; | |
440 | msg.msg_iov = iov; | |
441 | msg.msg_iovlen = 1; | |
442 | ||
443 | do { | |
444 | ret = sendmsg(sock, &msg, 0); | |
445 | } while (ret < 0 && errno == EINTR); | |
446 | if (ret < 0) { | |
447 | /* | |
448 | * Only warn about EPIPE when quiet mode is deactivated. | |
449 | * We consider EPIPE as expected. | |
450 | */ | |
451 | if (errno != EPIPE || !lttng_opt_quiet) { | |
452 | PERROR("sendmsg"); | |
453 | } | |
454 | } | |
455 | return ret; | |
456 | } | |
457 | ||
fe489250 JG |
458 | /* |
459 | * Send the fd(s) of a payload view over a unix socket. | |
460 | * | |
461 | * Returns the size of data sent, or negative error value. | |
462 | */ | |
463 | static | |
464 | ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock, | |
465 | struct lttng_payload_view *view, | |
466 | bool blocking) | |
467 | { | |
468 | int i; | |
469 | ssize_t ret; | |
470 | struct lttng_dynamic_array raw_fds; | |
471 | const int fd_count = lttng_payload_view_get_fd_handle_count(view); | |
472 | ||
473 | lttng_dynamic_array_init(&raw_fds, sizeof(int), NULL); | |
474 | ||
3545ccee JR |
475 | if (fd_count < 0) { |
476 | ret = -LTTNG_ERR_INVALID; | |
477 | goto end; | |
478 | } | |
479 | ||
fe489250 JG |
480 | /* |
481 | * Prepare a contiguous array of file descriptors to send them. | |
482 | * | |
483 | * Note that the reference to each fd is released during the iteration; | |
484 | * we're just getting the numerical value of the fds to conform to the | |
485 | * syscall's interface. We rely on the fact that "view" must remain | |
486 | * valid for the duration of the call and that the underlying payload | |
487 | * owns a reference to the fd_handles. | |
488 | */ | |
489 | for (i = 0; i < fd_count; i++) { | |
490 | struct fd_handle *handle = | |
491 | lttng_payload_view_pop_fd_handle(view); | |
492 | const int raw_fd = fd_handle_get_fd(handle); | |
493 | const int add_ret = lttng_dynamic_array_add_element( | |
494 | &raw_fds, &raw_fd); | |
495 | ||
496 | fd_handle_put(handle); | |
497 | if (add_ret) { | |
498 | ret = -LTTNG_ERR_NOMEM; | |
499 | goto end; | |
500 | } | |
501 | } | |
502 | ||
503 | if (blocking) { | |
504 | ret = lttcomm_send_fds_unix_sock(sock, | |
505 | (const int *) raw_fds.buffer.data, fd_count); | |
506 | } else { | |
507 | ret = lttcomm_send_fds_unix_sock_non_block(sock, | |
508 | (const int *) raw_fds.buffer.data, fd_count); | |
509 | } | |
510 | ||
511 | end: | |
512 | lttng_dynamic_array_reset(&raw_fds); | |
513 | return ret; | |
514 | } | |
515 | ||
516 | LTTNG_HIDDEN | |
517 | ssize_t lttcomm_send_payload_view_fds_unix_sock(int sock, | |
518 | struct lttng_payload_view *view) | |
519 | { | |
520 | return _lttcomm_send_payload_view_fds_unix_sock(sock, view, true); | |
521 | } | |
522 | ||
523 | LTTNG_HIDDEN | |
524 | ssize_t lttcomm_send_payload_view_fds_unix_sock_non_block(int sock, | |
525 | struct lttng_payload_view *view) | |
526 | { | |
527 | return _lttcomm_send_payload_view_fds_unix_sock(sock, view, false); | |
528 | } | |
529 | ||
b72ce630 JR |
530 | /* |
531 | * Send a message accompanied by fd(s) over a unix socket. | |
532 | * Only use for non blocking socket. | |
533 | * | |
534 | * Returns the size of data sent, or negative error value. | |
535 | */ | |
536 | LTTNG_HIDDEN | |
537 | ssize_t lttcomm_send_fds_unix_sock_non_block(int sock, const int *fds, size_t nb_fd) | |
538 | { | |
539 | struct msghdr msg; | |
540 | struct cmsghdr *cmptr; | |
541 | struct iovec iov[1]; | |
542 | ssize_t ret = -1; | |
543 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
544 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
545 | char dummy = 0; | |
546 | ||
a0377dfe FD |
547 | LTTNG_ASSERT(sock); |
548 | LTTNG_ASSERT(fds); | |
549 | LTTNG_ASSERT(nb_fd > 0); | |
982583bd | 550 | |
b72ce630 JR |
551 | memset(&msg, 0, sizeof(msg)); |
552 | memset(tmp, 0, sizeof(tmp)); | |
553 | ||
554 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) | |
555 | return -EINVAL; | |
556 | ||
557 | msg.msg_control = (caddr_t)tmp; | |
558 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
559 | ||
560 | cmptr = CMSG_FIRSTHDR(&msg); | |
561 | if (!cmptr) { | |
562 | return -1; | |
563 | } | |
564 | ||
565 | cmptr->cmsg_level = SOL_SOCKET; | |
566 | cmptr->cmsg_type = SCM_RIGHTS; | |
567 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
568 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
569 | /* Sum of the length of all control messages in the buffer: */ | |
570 | msg.msg_controllen = cmptr->cmsg_len; | |
571 | ||
572 | iov[0].iov_base = &dummy; | |
573 | iov[0].iov_len = 1; | |
574 | msg.msg_iov = iov; | |
575 | msg.msg_iovlen = 1; | |
576 | ||
577 | retry: | |
578 | ret = sendmsg(sock, &msg, 0); | |
579 | if (ret < 0) { | |
580 | if (errno == EINTR) { | |
581 | goto retry; | |
582 | } else { | |
583 | /* | |
584 | * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected. | |
585 | */ | |
586 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
587 | /* | |
588 | * This can happen in non blocking mode. | |
589 | * Nothing was sent. | |
590 | */ | |
591 | ret = 0; | |
592 | goto end; | |
593 | } | |
594 | ||
595 | if (errno == EPIPE) { | |
596 | /* Expected error, pass error to caller */ | |
597 | DBG3("EPIPE on sendmsg"); | |
598 | ret = -1; | |
599 | goto end; | |
600 | } | |
601 | ||
602 | /* Unexpected error */ | |
603 | PERROR("sendmsg"); | |
604 | ret = -1; | |
605 | goto end; | |
606 | } | |
607 | } | |
608 | ||
609 | end: | |
610 | return ret; | |
611 | } | |
612 | ||
0d37f2bc DG |
613 | /* |
614 | * Recv a message accompanied by fd(s) from a unix socket. | |
615 | * | |
616 | * Returns the size of received data, or negative error value. | |
617 | * | |
618 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
619 | * actually received in nb_fd. | |
620 | */ | |
90e535ef | 621 | LTTNG_HIDDEN |
0d37f2bc DG |
622 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
623 | { | |
624 | struct iovec iov[1]; | |
625 | ssize_t ret = 0; | |
626 | struct cmsghdr *cmsg; | |
627 | size_t sizeof_fds = nb_fd * sizeof(int); | |
b3f35e02 | 628 | |
ba49ae8c MJ |
629 | #ifdef __linux__ |
630 | /* Account for the struct ucred cmsg in the buffer size */ | |
631 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred)) | |
632 | #else | |
633 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) | |
634 | #endif /* __linux__ */ | |
635 | ||
636 | char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE]; | |
0d37f2bc DG |
637 | struct msghdr msg; |
638 | char dummy; | |
639 | ||
a0377dfe FD |
640 | LTTNG_ASSERT(sock); |
641 | LTTNG_ASSERT(fds); | |
642 | LTTNG_ASSERT(nb_fd > 0); | |
982583bd | 643 | |
0d37f2bc DG |
644 | memset(&msg, 0, sizeof(msg)); |
645 | ||
646 | /* Prepare to receive the structures */ | |
647 | iov[0].iov_base = &dummy; | |
648 | iov[0].iov_len = 1; | |
649 | msg.msg_iov = iov; | |
650 | msg.msg_iovlen = 1; | |
b3f35e02 FD |
651 | |
652 | cmsg = (struct cmsghdr *) recv_buf; | |
653 | cmsg->cmsg_len = CMSG_LEN(sizeof_fds); | |
654 | cmsg->cmsg_level = SOL_SOCKET; | |
655 | cmsg->cmsg_type = SCM_RIGHTS; | |
656 | ||
657 | msg.msg_control = cmsg; | |
658 | msg.msg_controllen = CMSG_LEN(sizeof(recv_buf)); | |
659 | msg.msg_flags = 0; | |
0d37f2bc | 660 | |
b72ce630 JR |
661 | retry: |
662 | ret = lttng_recvmsg_nosigpipe(sock, &msg); | |
0d37f2bc | 663 | if (ret < 0) { |
b72ce630 JR |
664 | if (errno == EINTR) { |
665 | goto retry; | |
666 | } else { | |
667 | /* We consider EPIPE and EAGAIN as expected. */ | |
668 | if (!lttng_opt_quiet && | |
669 | (errno != EPIPE && errno != EAGAIN)) { | |
670 | PERROR("recvmsg"); | |
671 | } | |
672 | goto end; | |
673 | } | |
674 | } | |
675 | ||
676 | if (ret != 1) { | |
677 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
678 | ret, 1); | |
0d37f2bc DG |
679 | goto end; |
680 | } | |
b3f35e02 | 681 | |
b72ce630 JR |
682 | if (msg.msg_flags & MSG_CTRUNC) { |
683 | fprintf(stderr, "Error: Control message truncated.\n"); | |
684 | ret = -1; | |
685 | goto end; | |
686 | } | |
687 | ||
688 | /* | |
689 | * If the socket was configured with SO_PASSCRED, the kernel will add a | |
690 | * control message (cmsg) to the ancillary data of the unix socket. We | |
691 | * need to expect a cmsg of the SCM_CREDENTIALS as the first control | |
692 | * message. | |
693 | */ | |
694 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
695 | if (cmsg->cmsg_level != SOL_SOCKET) { | |
696 | fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n"); | |
697 | ret = -1; | |
698 | goto end; | |
699 | } | |
700 | if (cmsg->cmsg_type == SCM_RIGHTS) { | |
701 | /* | |
702 | * We found the controle message for file descriptors, | |
703 | * now copy the fds to the fds ptr and return success. | |
704 | */ | |
705 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
706 | fprintf(stderr, "Error: Received %zu bytes of" | |
707 | "ancillary data for FDs, expected %zu\n", | |
708 | (size_t) cmsg->cmsg_len, | |
709 | (size_t) CMSG_LEN(sizeof_fds)); | |
710 | ret = -1; | |
711 | goto end; | |
712 | } | |
713 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
714 | ret = sizeof_fds; | |
715 | goto end; | |
716 | } | |
717 | #ifdef __linux__ | |
718 | if (cmsg->cmsg_type == SCM_CREDENTIALS) { | |
719 | /* | |
720 | * Expect credentials to be sent when expecting fds even | |
721 | * if no credential were include in the send(). The | |
722 | * kernel adds them... | |
723 | */ | |
724 | ret = -1; | |
725 | } | |
726 | #endif /* __linux__ */ | |
727 | } | |
728 | end: | |
729 | return ret; | |
730 | } | |
731 | ||
fe489250 JG |
732 | static |
733 | void close_raw_fd(void *ptr) | |
734 | { | |
735 | const int raw_fd = *((const int *) ptr); | |
736 | ||
737 | if (raw_fd >= 0) { | |
738 | const int ret = close(raw_fd); | |
739 | ||
740 | if (ret) { | |
741 | PERROR("Failed to close file descriptor %d", raw_fd); | |
742 | } | |
743 | } | |
744 | } | |
745 | ||
746 | static | |
747 | enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds, | |
748 | struct lttng_payload *payload) | |
749 | { | |
750 | int i; | |
751 | enum lttng_error_code ret_code = LTTNG_OK; | |
752 | const int fd_count = lttng_dynamic_array_get_count(raw_fds); | |
753 | ||
754 | for (i = 0; i < fd_count; i++) { | |
755 | int ret; | |
756 | struct fd_handle *handle; | |
757 | int *raw_fd = (int *) lttng_dynamic_array_get_element( | |
758 | raw_fds, i); | |
759 | ||
a0377dfe | 760 | LTTNG_ASSERT(*raw_fd != -1); |
5dbd9742 | 761 | |
fe489250 JG |
762 | handle = fd_handle_create(*raw_fd); |
763 | if (!handle) { | |
764 | ret_code = LTTNG_ERR_NOMEM; | |
765 | goto end; | |
766 | } | |
767 | ||
768 | /* FD ownership transferred to the handle. */ | |
769 | *raw_fd = -1; | |
770 | ||
771 | ret = lttng_payload_push_fd_handle(payload, handle); | |
772 | fd_handle_put(handle); | |
773 | if (ret) { | |
774 | ret_code = LTTNG_ERR_NOMEM; | |
775 | goto end; | |
776 | } | |
777 | } | |
778 | ||
779 | end: | |
780 | return ret_code; | |
781 | } | |
782 | ||
783 | static | |
784 | ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd, | |
785 | struct lttng_payload *payload, bool blocking) | |
786 | { | |
5dbd9742 | 787 | int i = 0; |
fe489250 JG |
788 | enum lttng_error_code add_ret; |
789 | ssize_t ret; | |
5dbd9742 | 790 | int default_value = -1; |
fe489250 JG |
791 | struct lttng_dynamic_array raw_fds; |
792 | ||
a0377dfe FD |
793 | LTTNG_ASSERT(sock); |
794 | LTTNG_ASSERT(payload); | |
795 | LTTNG_ASSERT(nb_fd > 0); | |
982583bd | 796 | |
fe489250 | 797 | lttng_dynamic_array_init(&raw_fds, sizeof(int), close_raw_fd); |
5dbd9742 JR |
798 | |
799 | for (i = 0; i < nb_fd; i++) { | |
800 | if (lttng_dynamic_array_add_element(&raw_fds, &default_value)) { | |
801 | ret = -LTTNG_ERR_NOMEM; | |
802 | goto end; | |
803 | } | |
fe489250 JG |
804 | } |
805 | ||
806 | if (blocking) { | |
807 | ret = lttcomm_recv_fds_unix_sock( | |
808 | sock, (int *) raw_fds.buffer.data, nb_fd); | |
809 | } else { | |
810 | ret = lttcomm_recv_fds_unix_sock_non_block( | |
811 | sock, (int *) raw_fds.buffer.data, nb_fd); | |
812 | } | |
813 | ||
b3ba184e | 814 | if (ret <= 0) { |
fe489250 JG |
815 | goto end; |
816 | } | |
817 | ||
818 | add_ret = add_fds_to_payload(&raw_fds, payload); | |
819 | if (add_ret != LTTNG_OK) { | |
820 | ret = - (int) add_ret; | |
821 | goto end; | |
822 | } | |
823 | ||
824 | end: | |
825 | lttng_dynamic_array_reset(&raw_fds); | |
826 | return ret; | |
827 | } | |
828 | ||
829 | LTTNG_HIDDEN | |
830 | ssize_t lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd, | |
831 | struct lttng_payload *payload) | |
832 | { | |
833 | return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, true); | |
834 | } | |
835 | ||
836 | LTTNG_HIDDEN | |
837 | ssize_t lttcomm_recv_payload_fds_unix_sock_non_block(int sock, size_t nb_fd, | |
838 | struct lttng_payload *payload) | |
839 | { | |
840 | return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, false); | |
841 | } | |
842 | ||
b72ce630 JR |
843 | /* |
844 | * Recv a message accompanied by fd(s) from a non-blocking unix socket. | |
845 | * Only use with non-blocking sockets. | |
846 | * | |
847 | * Returns the size of received data, or negative error value. | |
848 | * | |
849 | * Expect at most "nb_fd" file descriptors. | |
850 | * | |
851 | * Note that based on our comprehension, partial reception of fds is not | |
852 | * possible since the FDs are actually in the control message. It is all or | |
853 | * nothing, still the sender side can send the wrong number of fds. | |
854 | */ | |
855 | LTTNG_HIDDEN | |
856 | ssize_t lttcomm_recv_fds_unix_sock_non_block(int sock, int *fds, size_t nb_fd) | |
857 | { | |
858 | struct iovec iov[1]; | |
859 | ssize_t ret = 0; | |
860 | struct cmsghdr *cmsg; | |
861 | size_t sizeof_fds = nb_fd * sizeof(int); | |
862 | ||
a0377dfe FD |
863 | LTTNG_ASSERT(sock); |
864 | LTTNG_ASSERT(fds); | |
865 | LTTNG_ASSERT(nb_fd > 0); | |
982583bd | 866 | |
b72ce630 JR |
867 | #ifdef __linux__ |
868 | /* Account for the struct ucred cmsg in the buffer size */ | |
869 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred)) | |
870 | #else | |
871 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) | |
872 | #endif /* __linux__ */ | |
873 | ||
874 | char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE]; | |
875 | struct msghdr msg; | |
876 | char dummy; | |
877 | ||
878 | memset(&msg, 0, sizeof(msg)); | |
879 | ||
880 | /* Prepare to receive the structures */ | |
881 | iov[0].iov_base = &dummy; | |
882 | iov[0].iov_len = 1; | |
883 | msg.msg_iov = iov; | |
884 | msg.msg_iovlen = 1; | |
885 | ||
886 | cmsg = (struct cmsghdr *) recv_buf; | |
887 | cmsg->cmsg_len = CMSG_LEN(sizeof_fds); | |
888 | cmsg->cmsg_level = SOL_SOCKET; | |
889 | cmsg->cmsg_type = SCM_RIGHTS; | |
890 | ||
891 | msg.msg_control = cmsg; | |
892 | msg.msg_controllen = CMSG_LEN(sizeof(recv_buf)); | |
893 | msg.msg_flags = 0; | |
894 | ||
895 | retry: | |
896 | ret = lttng_recvmsg_nosigpipe(sock, &msg); | |
897 | if (ret < 0) { | |
898 | if (errno == EINTR) { | |
899 | goto retry; | |
900 | } else { | |
901 | /* | |
902 | * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected. | |
903 | */ | |
904 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
905 | /* | |
906 | * This can happen in non blocking mode. | |
907 | * Nothing was recv. | |
908 | */ | |
909 | ret = 0; | |
910 | goto end; | |
911 | } | |
912 | ||
913 | if (errno == EPIPE) { | |
914 | /* Expected error, pass error to caller */ | |
915 | DBG3("EPIPE on recvmsg"); | |
916 | ret = -1; | |
917 | goto end; | |
918 | } | |
919 | ||
920 | /* Unexpected error */ | |
921 | PERROR("recvmsg"); | |
922 | ret = -1; | |
923 | goto end; | |
924 | } | |
925 | } | |
926 | ||
0d37f2bc DG |
927 | if (ret != 1) { |
928 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
929 | ret, 1); | |
930 | goto end; | |
931 | } | |
b3f35e02 | 932 | |
0d37f2bc DG |
933 | if (msg.msg_flags & MSG_CTRUNC) { |
934 | fprintf(stderr, "Error: Control message truncated.\n"); | |
935 | ret = -1; | |
936 | goto end; | |
937 | } | |
b3f35e02 FD |
938 | |
939 | /* | |
940 | * If the socket was configured with SO_PASSCRED, the kernel will add a | |
941 | * control message (cmsg) to the ancillary data of the unix socket. We | |
942 | * need to expect a cmsg of the SCM_CREDENTIALS as the first control | |
943 | * message. | |
944 | */ | |
945 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
b3f35e02 FD |
946 | if (cmsg->cmsg_level != SOL_SOCKET) { |
947 | fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n"); | |
948 | ret = -1; | |
949 | goto end; | |
950 | } | |
951 | if (cmsg->cmsg_type == SCM_RIGHTS) { | |
952 | /* | |
953 | * We found the controle message for file descriptors, | |
954 | * now copy the fds to the fds ptr and return success. | |
955 | */ | |
956 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
957 | fprintf(stderr, "Error: Received %zu bytes of" | |
958 | "ancillary data for FDs, expected %zu\n", | |
959 | (size_t) cmsg->cmsg_len, | |
960 | (size_t) CMSG_LEN(sizeof_fds)); | |
961 | ret = -1; | |
962 | goto end; | |
963 | } | |
964 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
965 | ret = sizeof_fds; | |
966 | goto end; | |
967 | } | |
ba49ae8c | 968 | #ifdef __linux__ |
b3f35e02 FD |
969 | if (cmsg->cmsg_type == SCM_CREDENTIALS) { |
970 | /* | |
971 | * Expect credentials to be sent when expecting fds even | |
972 | * if no credential were include in the send(). The | |
973 | * kernel adds them... | |
974 | */ | |
b3f35e02 FD |
975 | ret = -1; |
976 | } | |
ba49ae8c | 977 | #endif /* __linux__ */ |
0d37f2bc | 978 | } |
0d37f2bc DG |
979 | end: |
980 | return ret; | |
981 | } | |
982 | ||
983 | /* | |
984 | * Send a message with credentials over a unix socket. | |
985 | * | |
986 | * Returns the size of data sent, or negative error value. | |
987 | */ | |
90e535ef | 988 | LTTNG_HIDDEN |
e368fb43 | 989 | ssize_t lttcomm_send_creds_unix_sock(int sock, const void *buf, size_t len) |
0d37f2bc DG |
990 | { |
991 | struct msghdr msg; | |
992 | struct iovec iov[1]; | |
993 | ssize_t ret = -1; | |
9bc1a4b4 | 994 | #if defined(__linux__) || defined(__CYGWIN__) |
0d37f2bc DG |
995 | struct cmsghdr *cmptr; |
996 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
997 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
998 | lttng_sock_cred *creds; | |
8bbffd54 MJ |
999 | |
1000 | memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char)); | |
9bc1a4b4 | 1001 | #endif /* __linux__, __CYGWIN__ */ |
0d37f2bc DG |
1002 | |
1003 | memset(&msg, 0, sizeof(msg)); | |
1004 | ||
a0377dfe FD |
1005 | LTTNG_ASSERT(sock); |
1006 | LTTNG_ASSERT(buf); | |
1007 | LTTNG_ASSERT(len > 0); | |
982583bd | 1008 | |
e368fb43 | 1009 | iov[0].iov_base = (void *) buf; |
0d37f2bc DG |
1010 | iov[0].iov_len = len; |
1011 | msg.msg_iov = iov; | |
1012 | msg.msg_iovlen = 1; | |
1013 | ||
9bc1a4b4 | 1014 | #if defined(__linux__) || defined(__CYGWIN__) |
0d37f2bc DG |
1015 | msg.msg_control = (caddr_t) anc_buf; |
1016 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
1017 | ||
1018 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
1019 | if (!cmptr) { |
1020 | return -1; | |
1021 | } | |
0d37f2bc DG |
1022 | cmptr->cmsg_level = SOL_SOCKET; |
1023 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; | |
1024 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
1025 | ||
1026 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); | |
1027 | ||
1028 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); | |
1029 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); | |
1030 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); | |
9bc1a4b4 | 1031 | #endif /* __linux__, __CYGWIN__ */ |
0d37f2bc DG |
1032 | |
1033 | do { | |
1034 | ret = sendmsg(sock, &msg, 0); | |
1035 | } while (ret < 0 && errno == EINTR); | |
1036 | if (ret < 0) { | |
1037 | /* | |
1038 | * Only warn about EPIPE when quiet mode is deactivated. | |
1039 | * We consider EPIPE as expected. | |
1040 | */ | |
1041 | if (errno != EPIPE || !lttng_opt_quiet) { | |
1042 | PERROR("sendmsg"); | |
1043 | } | |
1044 | } | |
1045 | return ret; | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * Recv a message accompanied with credentials from a unix socket. | |
1050 | * | |
1051 | * Returns the size of received data, or negative error value. | |
1052 | */ | |
90e535ef | 1053 | LTTNG_HIDDEN |
0d37f2bc DG |
1054 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
1055 | lttng_sock_cred *creds) | |
1056 | { | |
1057 | struct msghdr msg; | |
1058 | struct iovec iov[1]; | |
1059 | ssize_t ret; | |
4100e49a | 1060 | size_t len_last; |
9bc1a4b4 | 1061 | #if defined(__linux__) || defined(__CYGWIN__) |
0d37f2bc DG |
1062 | struct cmsghdr *cmptr; |
1063 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
1064 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
9bc1a4b4 | 1065 | #endif /* __linux__, __CYGWIN__ */ |
0d37f2bc | 1066 | |
a0377dfe FD |
1067 | LTTNG_ASSERT(sock); |
1068 | LTTNG_ASSERT(buf); | |
1069 | LTTNG_ASSERT(len > 0); | |
1070 | LTTNG_ASSERT(creds); | |
0d37f2bc | 1071 | |
982583bd | 1072 | memset(&msg, 0, sizeof(msg)); |
0d37f2bc DG |
1073 | |
1074 | /* Prepare to receive the structures */ | |
1075 | iov[0].iov_base = buf; | |
1076 | iov[0].iov_len = len; | |
1077 | msg.msg_iov = iov; | |
1078 | msg.msg_iovlen = 1; | |
1079 | ||
9bc1a4b4 | 1080 | #if defined(__linux__) || defined(__CYGWIN__) |
0d37f2bc DG |
1081 | msg.msg_control = anc_buf; |
1082 | msg.msg_controllen = sizeof(anc_buf); | |
9bc1a4b4 | 1083 | #endif /* __linux__, __CYGWIN__ */ |
0d37f2bc DG |
1084 | |
1085 | do { | |
4100e49a | 1086 | len_last = iov[0].iov_len; |
0d37f2bc | 1087 | ret = recvmsg(sock, &msg, 0); |
4100e49a DG |
1088 | if (ret > 0) { |
1089 | iov[0].iov_base += ret; | |
1090 | iov[0].iov_len -= ret; | |
a0377dfe | 1091 | LTTNG_ASSERT(ret <= len_last); |
4100e49a DG |
1092 | } |
1093 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
1094 | if (ret < 0) { |
1095 | PERROR("recvmsg fds"); | |
1096 | goto end; | |
4100e49a DG |
1097 | } else if (ret > 0) { |
1098 | ret = len; | |
0d37f2bc | 1099 | } |
4100e49a | 1100 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc | 1101 | |
9bc1a4b4 | 1102 | #if defined(__linux__) || defined(__CYGWIN__) |
0d37f2bc DG |
1103 | if (msg.msg_flags & MSG_CTRUNC) { |
1104 | fprintf(stderr, "Error: Control message truncated.\n"); | |
1105 | ret = -1; | |
1106 | goto end; | |
1107 | } | |
1108 | ||
1109 | cmptr = CMSG_FIRSTHDR(&msg); | |
1110 | if (cmptr == NULL) { | |
1111 | fprintf(stderr, "Error: Invalid control message header\n"); | |
1112 | ret = -1; | |
1113 | goto end; | |
1114 | } | |
1115 | ||
1116 | if (cmptr->cmsg_level != SOL_SOCKET || | |
1117 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { | |
1118 | fprintf(stderr, "Didn't received any credentials\n"); | |
1119 | ret = -1; | |
1120 | goto end; | |
1121 | } | |
1122 | ||
1123 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
1124 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
1125 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); | |
1126 | ret = -1; | |
1127 | goto end; | |
1128 | } | |
1129 | ||
1130 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
9bc1a4b4 JG |
1131 | #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__)) |
1132 | if (lttng_get_unix_socket_peer_creds(sock, creds)) { | |
1133 | fprintf(stderr, "ARG\n"); | |
1134 | ret = -1; | |
1135 | goto end; | |
0d37f2bc DG |
1136 | } |
1137 | #else | |
1138 | #error "Please implement credential support for your OS." | |
9bc1a4b4 | 1139 | #endif /* __linux__, __CYGWIN__ */ |
0d37f2bc DG |
1140 | |
1141 | end: | |
1142 | return ret; | |
1143 | } | |
1144 | ||
1145 | /* | |
1146 | * Set socket option to use credentials passing. | |
1147 | */ | |
9bc1a4b4 | 1148 | #if defined(__linux__) || defined(__CYGWIN__) |
90e535ef | 1149 | LTTNG_HIDDEN |
0d37f2bc DG |
1150 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
1151 | { | |
1152 | int ret, on = 1; | |
1153 | ||
1154 | /* Set socket for credentials retrieval */ | |
1155 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
1156 | if (ret < 0) { | |
1157 | PERROR("setsockopt creds unix sock"); | |
1158 | } | |
1159 | return ret; | |
1160 | } | |
9bc1a4b4 | 1161 | #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__)) |
90e535ef | 1162 | LTTNG_HIDDEN |
0d37f2bc DG |
1163 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
1164 | { | |
1165 | return 0; | |
1166 | } | |
1167 | #else | |
1168 | #error "Please implement credential support for your OS." | |
1169 | #endif /* __linux__ */ |