82fc68d66fd0fc89919a62d2d6ac2755e90f43fc
[lttng-tools.git] / src / common / unix.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
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>
18 #include <errno.h>
19
20 #include <common/common.h>
21 #include <common/sessiond-comm/sessiond-comm.h>
22
23 #include "unix.h"
24
25 /*
26 * Connect to unix socket using the path name.
27 */
28 LTTNG_HIDDEN
29 int lttcomm_connect_unix_sock(const char *pathname)
30 {
31 struct sockaddr_un s_un;
32 int fd, ret, closeret;
33
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
42 fd = socket(PF_UNIX, SOCK_STREAM, 0);
43 if (fd < 0) {
44 PERROR("socket");
45 ret = fd;
46 goto error;
47 }
48
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';
53
54 ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un));
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 */
78 LTTNG_HIDDEN
79 int lttcomm_accept_unix_sock(int sock)
80 {
81 int new_fd;
82 struct sockaddr_un s_un;
83 socklen_t len = sizeof(s_un);
84
85 /* Blocking call */
86 new_fd = accept(sock, (struct sockaddr *) &s_un, &len);
87 if (new_fd < 0) {
88 PERROR("accept");
89 }
90
91 return new_fd;
92 }
93
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
104 /*
105 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
106 * and return the fd.
107 */
108 LTTNG_HIDDEN
109 int lttcomm_create_unix_sock(const char *pathname)
110 {
111 struct sockaddr_un s_un;
112 int fd = -1;
113 int ret = -1;
114
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
123 /* Create server socket */
124 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
125 PERROR("socket");
126 goto error;
127 }
128
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';
133
134 /* Unlink the old file if present */
135 (void) unlink(pathname);
136 ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un));
137 if (ret < 0) {
138 PERROR("bind");
139 goto error;
140 }
141
142 return fd;
143
144 error:
145 if (fd >= 0) {
146 if (close(fd) < 0) {
147 PERROR("close create unix sock");
148 }
149 }
150 return ret;
151 }
152
153 /*
154 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
155 */
156 LTTNG_HIDDEN
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 */
175 LTTNG_HIDDEN
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;
181 size_t len_last;
182
183 memset(&msg, 0, sizeof(msg));
184
185 iov[0].iov_base = buf;
186 iov[0].iov_len = len;
187 msg.msg_iov = iov;
188 msg.msg_iovlen = 1;
189
190 do {
191 len_last = iov[0].iov_len;
192 ret = lttng_recvmsg_nosigpipe(sock, &msg);
193 if (ret > 0) {
194 iov[0].iov_base += ret;
195 iov[0].iov_len -= ret;
196 assert(ret <= len_last);
197 }
198 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
199 if (ret < 0) {
200 PERROR("recvmsg");
201 } else if (ret > 0) {
202 ret = len;
203 }
204 /* Else ret = 0 meaning an orderly shutdown. */
205
206 return ret;
207 }
208
209 /*
210 * Receive data of size len in put that data into the buf param. Using recvmsg
211 * API. Only use with sockets set in non-blocking mode.
212 *
213 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
214 * poll set. The poll loop will handle the EPIPE original cause.
215 *
216 * Return the size of received data.
217 */
218 LTTNG_HIDDEN
219 ssize_t lttcomm_recv_unix_sock_non_block(int sock, void *buf, size_t len)
220 {
221 struct msghdr msg;
222 struct iovec iov[1];
223 ssize_t ret;
224
225 memset(&msg, 0, sizeof(msg));
226
227 iov[0].iov_base = buf;
228 iov[0].iov_len = len;
229 msg.msg_iov = iov;
230 msg.msg_iovlen = 1;
231
232 retry:
233 ret = lttng_recvmsg_nosigpipe(sock, &msg);
234 if (ret < 0) {
235 if (errno == EINTR) {
236 goto retry;
237 } else {
238 /*
239 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
240 */
241 if (errno == EAGAIN || errno == EWOULDBLOCK ||
242 errno == EPIPE) {
243 /*
244 * Nothing was recv.
245 */
246 ret = 0;
247 goto end;
248 }
249
250 /* Unexpected error */
251 PERROR("recvmsg");
252 ret = -1;
253 goto end;
254 }
255 }
256
257 end:
258 return ret;
259 }
260
261 /*
262 * Send buf data of size len. Using sendmsg API.
263 *
264 * Return the size of sent data.
265 */
266 LTTNG_HIDDEN
267 ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len)
268 {
269 struct msghdr msg;
270 struct iovec iov[1];
271 ssize_t ret;
272
273 memset(&msg, 0, sizeof(msg));
274
275 iov[0].iov_base = (void *) buf;
276 iov[0].iov_len = len;
277 msg.msg_iov = iov;
278 msg.msg_iovlen = 1;
279
280 while (iov[0].iov_len) {
281 ret = sendmsg(sock, &msg, 0);
282 if (ret < 0) {
283 if (errno == EINTR) {
284 continue;
285 } else {
286 /*
287 * Only warn about EPIPE when quiet mode is
288 * deactivated.
289 * We consider EPIPE as expected.
290 */
291 if (errno != EPIPE || !lttng_opt_quiet) {
292 PERROR("sendmsg");
293 }
294 goto end;
295 }
296 }
297 iov[0].iov_len -= ret;
298 iov[0].iov_base += ret;
299 }
300 ret = len;
301 end:
302 return ret;
303 }
304
305 /*
306 * Send buf data of size len. Using sendmsg API.
307 * Only use with non-blocking sockets. The difference with the blocking version
308 * of the function is that this one does not retry to send on partial sends,
309 * except if the interruption was caused by a signal (EINTR).
310 *
311 * NOTE: EPIPE errors are NOT reported. This call expects the socket to be in a
312 * poll set. The poll loop will handle the EPIPE original cause.
313 *
314 * Return the size of sent data.
315 */
316 LTTNG_HIDDEN
317 ssize_t lttcomm_send_unix_sock_non_block(int sock, const void *buf, size_t len)
318 {
319 struct msghdr msg;
320 struct iovec iov[1];
321 ssize_t ret;
322
323 memset(&msg, 0, sizeof(msg));
324
325 iov[0].iov_base = (void *) buf;
326 iov[0].iov_len = len;
327 msg.msg_iov = iov;
328 msg.msg_iovlen = 1;
329
330 retry:
331 ret = sendmsg(sock, &msg, 0);
332 if (ret < 0) {
333 if (errno == EINTR) {
334 goto retry;
335 } else {
336 /*
337 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
338 */
339 if (errno == EAGAIN || errno == EWOULDBLOCK ||
340 errno == EPIPE) {
341 /*
342 * This can happen in non blocking mode.
343 * Nothing was sent.
344 */
345 ret = 0;
346 goto end;
347 }
348
349 /* Unexpected error */
350 PERROR("sendmsg");
351 ret = -1;
352 goto end;
353 }
354 }
355 end:
356 return ret;
357 }
358
359 /*
360 * Shutdown cleanly a unix socket.
361 */
362 LTTNG_HIDDEN
363 int lttcomm_close_unix_sock(int sock)
364 {
365 int ret, closeret;
366
367 /* Shutdown receptions and transmissions */
368 ret = shutdown(sock, SHUT_RDWR);
369 if (ret < 0) {
370 PERROR("shutdown");
371 }
372
373 closeret = close(sock);
374 if (closeret) {
375 PERROR("close");
376 }
377
378 return ret;
379 }
380
381 /*
382 * Send a message accompanied by fd(s) over a unix socket.
383 *
384 * Returns the size of data sent, or negative error value.
385 */
386 LTTNG_HIDDEN
387 ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
388 {
389 struct msghdr msg;
390 struct cmsghdr *cmptr;
391 struct iovec iov[1];
392 ssize_t ret = -1;
393 unsigned int sizeof_fds = nb_fd * sizeof(int);
394 char tmp[CMSG_SPACE(sizeof_fds)];
395 char dummy = 0;
396
397 memset(&msg, 0, sizeof(msg));
398 memset(tmp, 0, sizeof(tmp));
399
400 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
401 return -EINVAL;
402
403 msg.msg_control = (caddr_t)tmp;
404 msg.msg_controllen = CMSG_LEN(sizeof_fds);
405
406 cmptr = CMSG_FIRSTHDR(&msg);
407 if (!cmptr) {
408 return -1;
409 }
410
411 cmptr->cmsg_level = SOL_SOCKET;
412 cmptr->cmsg_type = SCM_RIGHTS;
413 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
414 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
415 /* Sum of the length of all control messages in the buffer: */
416 msg.msg_controllen = cmptr->cmsg_len;
417
418 iov[0].iov_base = &dummy;
419 iov[0].iov_len = 1;
420 msg.msg_iov = iov;
421 msg.msg_iovlen = 1;
422
423 do {
424 ret = sendmsg(sock, &msg, 0);
425 } while (ret < 0 && errno == EINTR);
426 if (ret < 0) {
427 /*
428 * Only warn about EPIPE when quiet mode is deactivated.
429 * We consider EPIPE as expected.
430 */
431 if (errno != EPIPE || !lttng_opt_quiet) {
432 PERROR("sendmsg");
433 }
434 }
435 return ret;
436 }
437
438 /*
439 * Recv a message accompanied by fd(s) from a unix socket.
440 *
441 * Returns the size of received data, or negative error value.
442 *
443 * Expect at most "nb_fd" file descriptors. Returns the number of fd
444 * actually received in nb_fd.
445 */
446 LTTNG_HIDDEN
447 ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
448 {
449 struct iovec iov[1];
450 ssize_t ret = 0;
451 struct cmsghdr *cmsg;
452 size_t sizeof_fds = nb_fd * sizeof(int);
453
454 #ifdef __linux__
455 /* Account for the struct ucred cmsg in the buffer size */
456 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
457 #else
458 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
459 #endif /* __linux__ */
460
461 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
462 struct msghdr msg;
463 char dummy;
464
465 memset(&msg, 0, sizeof(msg));
466
467 /* Prepare to receive the structures */
468 iov[0].iov_base = &dummy;
469 iov[0].iov_len = 1;
470 msg.msg_iov = iov;
471 msg.msg_iovlen = 1;
472
473 cmsg = (struct cmsghdr *) recv_buf;
474 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
475 cmsg->cmsg_level = SOL_SOCKET;
476 cmsg->cmsg_type = SCM_RIGHTS;
477
478 msg.msg_control = cmsg;
479 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
480 msg.msg_flags = 0;
481
482 do {
483 ret = recvmsg(sock, &msg, 0);
484 } while (ret < 0 && errno == EINTR);
485 if (ret < 0) {
486 PERROR("recvmsg fds");
487 goto end;
488 }
489
490 if (ret != 1) {
491 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
492 ret, 1);
493 goto end;
494 }
495
496 if (msg.msg_flags & MSG_CTRUNC) {
497 fprintf(stderr, "Error: Control message truncated.\n");
498 ret = -1;
499 goto end;
500 }
501
502 /*
503 * If the socket was configured with SO_PASSCRED, the kernel will add a
504 * control message (cmsg) to the ancillary data of the unix socket. We
505 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
506 * message.
507 */
508 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
509 if (cmsg->cmsg_level != SOL_SOCKET) {
510 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
511 ret = -1;
512 goto end;
513 }
514 if (cmsg->cmsg_type == SCM_RIGHTS) {
515 /*
516 * We found the controle message for file descriptors,
517 * now copy the fds to the fds ptr and return success.
518 */
519 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
520 fprintf(stderr, "Error: Received %zu bytes of"
521 "ancillary data for FDs, expected %zu\n",
522 (size_t) cmsg->cmsg_len,
523 (size_t) CMSG_LEN(sizeof_fds));
524 ret = -1;
525 goto end;
526 }
527 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
528 ret = sizeof_fds;
529 goto end;
530 }
531 #ifdef __linux__
532 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
533 /*
534 * Expect credentials to be sent when expecting fds even
535 * if no credential were include in the send(). The
536 * kernel adds them...
537 */
538 ret = -1;
539 }
540 #endif /* __linux__ */
541 }
542 end:
543 return ret;
544 }
545
546 /*
547 * Send a message with credentials over a unix socket.
548 *
549 * Returns the size of data sent, or negative error value.
550 */
551 LTTNG_HIDDEN
552 ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
553 {
554 struct msghdr msg;
555 struct iovec iov[1];
556 ssize_t ret = -1;
557 #ifdef __linux__
558 struct cmsghdr *cmptr;
559 size_t sizeof_cred = sizeof(lttng_sock_cred);
560 char anc_buf[CMSG_SPACE(sizeof_cred)];
561 lttng_sock_cred *creds;
562
563 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
564 #endif /* __linux__ */
565
566 memset(&msg, 0, sizeof(msg));
567
568 iov[0].iov_base = buf;
569 iov[0].iov_len = len;
570 msg.msg_iov = iov;
571 msg.msg_iovlen = 1;
572
573 #ifdef __linux__
574 msg.msg_control = (caddr_t) anc_buf;
575 msg.msg_controllen = CMSG_LEN(sizeof_cred);
576
577 cmptr = CMSG_FIRSTHDR(&msg);
578 if (!cmptr) {
579 return -1;
580 }
581 cmptr->cmsg_level = SOL_SOCKET;
582 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
583 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
584
585 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
586
587 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
588 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
589 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
590 #endif /* __linux__ */
591
592 do {
593 ret = sendmsg(sock, &msg, 0);
594 } while (ret < 0 && errno == EINTR);
595 if (ret < 0) {
596 /*
597 * Only warn about EPIPE when quiet mode is deactivated.
598 * We consider EPIPE as expected.
599 */
600 if (errno != EPIPE || !lttng_opt_quiet) {
601 PERROR("sendmsg");
602 }
603 }
604 return ret;
605 }
606
607 /*
608 * Recv a message accompanied with credentials from a unix socket.
609 *
610 * Returns the size of received data, or negative error value.
611 */
612 LTTNG_HIDDEN
613 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
614 lttng_sock_cred *creds)
615 {
616 struct msghdr msg;
617 struct iovec iov[1];
618 ssize_t ret;
619 size_t len_last;
620 #ifdef __linux__
621 struct cmsghdr *cmptr;
622 size_t sizeof_cred = sizeof(lttng_sock_cred);
623 char anc_buf[CMSG_SPACE(sizeof_cred)];
624 #endif /* __linux__ */
625
626 memset(&msg, 0, sizeof(msg));
627
628 /* Not allowed */
629 if (creds == NULL) {
630 ret = -1;
631 goto end;
632 }
633
634 /* Prepare to receive the structures */
635 iov[0].iov_base = buf;
636 iov[0].iov_len = len;
637 msg.msg_iov = iov;
638 msg.msg_iovlen = 1;
639
640 #ifdef __linux__
641 msg.msg_control = anc_buf;
642 msg.msg_controllen = sizeof(anc_buf);
643 #endif /* __linux__ */
644
645 do {
646 len_last = iov[0].iov_len;
647 ret = recvmsg(sock, &msg, 0);
648 if (ret > 0) {
649 iov[0].iov_base += ret;
650 iov[0].iov_len -= ret;
651 assert(ret <= len_last);
652 }
653 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
654 if (ret < 0) {
655 PERROR("recvmsg fds");
656 goto end;
657 } else if (ret > 0) {
658 ret = len;
659 }
660 /* Else ret = 0 meaning an orderly shutdown. */
661
662 #ifdef __linux__
663 if (msg.msg_flags & MSG_CTRUNC) {
664 fprintf(stderr, "Error: Control message truncated.\n");
665 ret = -1;
666 goto end;
667 }
668
669 cmptr = CMSG_FIRSTHDR(&msg);
670 if (cmptr == NULL) {
671 fprintf(stderr, "Error: Invalid control message header\n");
672 ret = -1;
673 goto end;
674 }
675
676 if (cmptr->cmsg_level != SOL_SOCKET ||
677 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
678 fprintf(stderr, "Didn't received any credentials\n");
679 ret = -1;
680 goto end;
681 }
682
683 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
684 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
685 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
686 ret = -1;
687 goto end;
688 }
689
690 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
691 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
692 {
693 int peer_ret;
694
695 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
696 if (peer_ret != 0) {
697 return peer_ret;
698 }
699 }
700 #else
701 #error "Please implement credential support for your OS."
702 #endif /* __linux__ */
703
704 end:
705 return ret;
706 }
707
708 /*
709 * Set socket option to use credentials passing.
710 */
711 #ifdef __linux__
712 LTTNG_HIDDEN
713 int lttcomm_setsockopt_creds_unix_sock(int sock)
714 {
715 int ret, on = 1;
716
717 /* Set socket for credentials retrieval */
718 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
719 if (ret < 0) {
720 PERROR("setsockopt creds unix sock");
721 }
722 return ret;
723 }
724 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
725 LTTNG_HIDDEN
726 int lttcomm_setsockopt_creds_unix_sock(int sock)
727 {
728 return 0;
729 }
730 #else
731 #error "Please implement credential support for your OS."
732 #endif /* __linux__ */
This page took 0.06327 seconds and 4 git commands to generate.