15c31746638e8b1c811c9f00c56e106cbc12024f
[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 * Return the size of received data.
224 */
225 LTTNG_HIDDEN
226 ssize_t lttcomm_recv_unix_sock_non_block(int sock, void *buf, size_t len)
227 {
228 struct msghdr msg;
229 struct iovec iov[1];
230 ssize_t ret;
231
232 memset(&msg, 0, sizeof(msg));
233
234 iov[0].iov_base = buf;
235 iov[0].iov_len = len;
236 msg.msg_iov = iov;
237 msg.msg_iovlen = 1;
238
239 retry:
240 ret = lttng_recvmsg_nosigpipe(sock, &msg);
241 if (ret < 0) {
242 if (errno == EINTR) {
243 goto retry;
244 } else {
245 /* We consider EPIPE and EAGAIN as expected. */
246 if (!lttng_opt_quiet &&
247 (errno != EPIPE && errno != EAGAIN)) {
248 PERROR("recvmsg");
249 }
250 goto end;
251 }
252 }
253 ret = len;
254 end:
255 return ret;
256 }
257
258 /*
259 * Send buf data of size len. Using sendmsg API.
260 *
261 * Return the size of sent data.
262 */
263 LTTNG_HIDDEN
264 ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len)
265 {
266 struct msghdr msg;
267 struct iovec iov[1];
268 ssize_t ret;
269
270 memset(&msg, 0, sizeof(msg));
271
272 iov[0].iov_base = (void *) buf;
273 iov[0].iov_len = len;
274 msg.msg_iov = iov;
275 msg.msg_iovlen = 1;
276
277 while (iov[0].iov_len) {
278 ret = sendmsg(sock, &msg, 0);
279 if (ret < 0) {
280 if (errno == EINTR) {
281 continue;
282 } else {
283 /*
284 * Only warn about EPIPE when quiet mode is
285 * deactivated.
286 * We consider EPIPE as expected.
287 */
288 if (errno != EPIPE || !lttng_opt_quiet) {
289 PERROR("sendmsg");
290 }
291 goto end;
292 }
293 }
294 iov[0].iov_len -= ret;
295 iov[0].iov_base += ret;
296 }
297 ret = len;
298 end:
299 return ret;
300 }
301
302 /*
303 * Send buf data of size len. Using sendmsg API.
304 * Only use with non-blocking sockets. The difference with the blocking version
305 * of the function is that this one does not retry to send on partial sends,
306 * except if the interruption was caused by a signal (EINTR).
307 *
308 * Return the size of sent data.
309 */
310 LTTNG_HIDDEN
311 ssize_t lttcomm_send_unix_sock_non_block(int sock, const void *buf, size_t len)
312 {
313 struct msghdr msg;
314 struct iovec iov[1];
315 ssize_t ret;
316
317 memset(&msg, 0, sizeof(msg));
318
319 iov[0].iov_base = (void *) buf;
320 iov[0].iov_len = len;
321 msg.msg_iov = iov;
322 msg.msg_iovlen = 1;
323
324 retry:
325 ret = sendmsg(sock, &msg, 0);
326 if (ret < 0) {
327 if (errno == EINTR) {
328 goto retry;
329 } else {
330 /* We consider EPIPE and EAGAIN as expected. */
331 if (!lttng_opt_quiet &&
332 (errno != EPIPE && errno != EAGAIN)) {
333 PERROR("sendmsg");
334 }
335 goto end;
336 }
337 }
338 ret = len;
339 end:
340 return ret;
341 }
342
343 /*
344 * Shutdown cleanly a unix socket.
345 */
346 LTTNG_HIDDEN
347 int lttcomm_close_unix_sock(int sock)
348 {
349 int ret, closeret;
350
351 /* Shutdown receptions and transmissions */
352 ret = shutdown(sock, SHUT_RDWR);
353 if (ret < 0) {
354 PERROR("shutdown");
355 }
356
357 closeret = close(sock);
358 if (closeret) {
359 PERROR("close");
360 }
361
362 return ret;
363 }
364
365 /*
366 * Send a message accompanied by fd(s) over a unix socket.
367 *
368 * Returns the size of data sent, or negative error value.
369 */
370 LTTNG_HIDDEN
371 ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
372 {
373 struct msghdr msg;
374 struct cmsghdr *cmptr;
375 struct iovec iov[1];
376 ssize_t ret = -1;
377 unsigned int sizeof_fds = nb_fd * sizeof(int);
378 char tmp[CMSG_SPACE(sizeof_fds)];
379 char dummy = 0;
380
381 memset(&msg, 0, sizeof(msg));
382 memset(tmp, 0, sizeof(tmp));
383
384 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
385 return -EINVAL;
386
387 msg.msg_control = (caddr_t)tmp;
388 msg.msg_controllen = CMSG_LEN(sizeof_fds);
389
390 cmptr = CMSG_FIRSTHDR(&msg);
391 if (!cmptr) {
392 return -1;
393 }
394
395 cmptr->cmsg_level = SOL_SOCKET;
396 cmptr->cmsg_type = SCM_RIGHTS;
397 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
398 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
399 /* Sum of the length of all control messages in the buffer: */
400 msg.msg_controllen = cmptr->cmsg_len;
401
402 iov[0].iov_base = &dummy;
403 iov[0].iov_len = 1;
404 msg.msg_iov = iov;
405 msg.msg_iovlen = 1;
406
407 do {
408 ret = sendmsg(sock, &msg, 0);
409 } while (ret < 0 && errno == EINTR);
410 if (ret < 0) {
411 /*
412 * Only warn about EPIPE when quiet mode is deactivated.
413 * We consider EPIPE as expected.
414 */
415 if (errno != EPIPE || !lttng_opt_quiet) {
416 PERROR("sendmsg");
417 }
418 }
419 return ret;
420 }
421
422 /*
423 * Recv a message accompanied by fd(s) from a unix socket.
424 *
425 * Returns the size of received data, or negative error value.
426 *
427 * Expect at most "nb_fd" file descriptors. Returns the number of fd
428 * actually received in nb_fd.
429 */
430 LTTNG_HIDDEN
431 ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
432 {
433 struct iovec iov[1];
434 ssize_t ret = 0;
435 struct cmsghdr *cmsg;
436 size_t sizeof_fds = nb_fd * sizeof(int);
437
438 #ifdef __linux__
439 /* Account for the struct ucred cmsg in the buffer size */
440 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
441 #else
442 #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
443 #endif /* __linux__ */
444
445 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
446 struct msghdr msg;
447 char dummy;
448
449 memset(&msg, 0, sizeof(msg));
450
451 /* Prepare to receive the structures */
452 iov[0].iov_base = &dummy;
453 iov[0].iov_len = 1;
454 msg.msg_iov = iov;
455 msg.msg_iovlen = 1;
456
457 cmsg = (struct cmsghdr *) recv_buf;
458 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
459 cmsg->cmsg_level = SOL_SOCKET;
460 cmsg->cmsg_type = SCM_RIGHTS;
461
462 msg.msg_control = cmsg;
463 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
464 msg.msg_flags = 0;
465
466 do {
467 ret = recvmsg(sock, &msg, 0);
468 } while (ret < 0 && errno == EINTR);
469 if (ret < 0) {
470 PERROR("recvmsg fds");
471 goto end;
472 }
473
474 if (ret != 1) {
475 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
476 ret, 1);
477 goto end;
478 }
479
480 if (msg.msg_flags & MSG_CTRUNC) {
481 fprintf(stderr, "Error: Control message truncated.\n");
482 ret = -1;
483 goto end;
484 }
485
486 /*
487 * If the socket was configured with SO_PASSCRED, the kernel will add a
488 * control message (cmsg) to the ancillary data of the unix socket. We
489 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
490 * message.
491 */
492 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
493 if (cmsg->cmsg_level != SOL_SOCKET) {
494 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
495 ret = -1;
496 goto end;
497 }
498 if (cmsg->cmsg_type == SCM_RIGHTS) {
499 /*
500 * We found the controle message for file descriptors,
501 * now copy the fds to the fds ptr and return success.
502 */
503 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
504 fprintf(stderr, "Error: Received %zu bytes of"
505 "ancillary data for FDs, expected %zu\n",
506 (size_t) cmsg->cmsg_len,
507 (size_t) CMSG_LEN(sizeof_fds));
508 ret = -1;
509 goto end;
510 }
511 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
512 ret = sizeof_fds;
513 goto end;
514 }
515 #ifdef __linux__
516 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
517 /*
518 * Expect credentials to be sent when expecting fds even
519 * if no credential were include in the send(). The
520 * kernel adds them...
521 */
522 ret = -1;
523 }
524 #endif /* __linux__ */
525 }
526 end:
527 return ret;
528 }
529
530 /*
531 * Send a message with credentials over a unix socket.
532 *
533 * Returns the size of data sent, or negative error value.
534 */
535 LTTNG_HIDDEN
536 ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
537 {
538 struct msghdr msg;
539 struct iovec iov[1];
540 ssize_t ret = -1;
541 #ifdef __linux__
542 struct cmsghdr *cmptr;
543 size_t sizeof_cred = sizeof(lttng_sock_cred);
544 char anc_buf[CMSG_SPACE(sizeof_cred)];
545 lttng_sock_cred *creds;
546
547 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
548 #endif /* __linux__ */
549
550 memset(&msg, 0, sizeof(msg));
551
552 iov[0].iov_base = buf;
553 iov[0].iov_len = len;
554 msg.msg_iov = iov;
555 msg.msg_iovlen = 1;
556
557 #ifdef __linux__
558 msg.msg_control = (caddr_t) anc_buf;
559 msg.msg_controllen = CMSG_LEN(sizeof_cred);
560
561 cmptr = CMSG_FIRSTHDR(&msg);
562 if (!cmptr) {
563 return -1;
564 }
565 cmptr->cmsg_level = SOL_SOCKET;
566 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
567 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
568
569 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
570
571 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
572 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
573 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
574 #endif /* __linux__ */
575
576 do {
577 ret = sendmsg(sock, &msg, 0);
578 } while (ret < 0 && errno == EINTR);
579 if (ret < 0) {
580 /*
581 * Only warn about EPIPE when quiet mode is deactivated.
582 * We consider EPIPE as expected.
583 */
584 if (errno != EPIPE || !lttng_opt_quiet) {
585 PERROR("sendmsg");
586 }
587 }
588 return ret;
589 }
590
591 /*
592 * Recv a message accompanied with credentials from a unix socket.
593 *
594 * Returns the size of received data, or negative error value.
595 */
596 LTTNG_HIDDEN
597 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
598 lttng_sock_cred *creds)
599 {
600 struct msghdr msg;
601 struct iovec iov[1];
602 ssize_t ret;
603 size_t len_last;
604 #ifdef __linux__
605 struct cmsghdr *cmptr;
606 size_t sizeof_cred = sizeof(lttng_sock_cred);
607 char anc_buf[CMSG_SPACE(sizeof_cred)];
608 #endif /* __linux__ */
609
610 memset(&msg, 0, sizeof(msg));
611
612 /* Not allowed */
613 if (creds == NULL) {
614 ret = -1;
615 goto end;
616 }
617
618 /* Prepare to receive the structures */
619 iov[0].iov_base = buf;
620 iov[0].iov_len = len;
621 msg.msg_iov = iov;
622 msg.msg_iovlen = 1;
623
624 #ifdef __linux__
625 msg.msg_control = anc_buf;
626 msg.msg_controllen = sizeof(anc_buf);
627 #endif /* __linux__ */
628
629 do {
630 len_last = iov[0].iov_len;
631 ret = recvmsg(sock, &msg, 0);
632 if (ret > 0) {
633 iov[0].iov_base += ret;
634 iov[0].iov_len -= ret;
635 assert(ret <= len_last);
636 }
637 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
638 if (ret < 0) {
639 PERROR("recvmsg fds");
640 goto end;
641 } else if (ret > 0) {
642 ret = len;
643 }
644 /* Else ret = 0 meaning an orderly shutdown. */
645
646 #ifdef __linux__
647 if (msg.msg_flags & MSG_CTRUNC) {
648 fprintf(stderr, "Error: Control message truncated.\n");
649 ret = -1;
650 goto end;
651 }
652
653 cmptr = CMSG_FIRSTHDR(&msg);
654 if (cmptr == NULL) {
655 fprintf(stderr, "Error: Invalid control message header\n");
656 ret = -1;
657 goto end;
658 }
659
660 if (cmptr->cmsg_level != SOL_SOCKET ||
661 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
662 fprintf(stderr, "Didn't received any credentials\n");
663 ret = -1;
664 goto end;
665 }
666
667 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
668 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
669 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
670 ret = -1;
671 goto end;
672 }
673
674 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
675 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
676 {
677 int peer_ret;
678
679 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
680 if (peer_ret != 0) {
681 return peer_ret;
682 }
683 }
684 #else
685 #error "Please implement credential support for your OS."
686 #endif /* __linux__ */
687
688 end:
689 return ret;
690 }
691
692 /*
693 * Set socket option to use credentials passing.
694 */
695 #ifdef __linux__
696 LTTNG_HIDDEN
697 int lttcomm_setsockopt_creds_unix_sock(int sock)
698 {
699 int ret, on = 1;
700
701 /* Set socket for credentials retrieval */
702 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
703 if (ret < 0) {
704 PERROR("setsockopt creds unix sock");
705 }
706 return ret;
707 }
708 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
709 LTTNG_HIDDEN
710 int lttcomm_setsockopt_creds_unix_sock(int sock)
711 {
712 return 0;
713 }
714 #else
715 #error "Please implement credential support for your OS."
716 #endif /* __linux__ */
This page took 0.042705 seconds and 3 git commands to generate.