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