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