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