Fix: uprobe: missing error code on allocation failure
[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>
18#include <errno.h>
19
90e535ef 20#include <common/common.h>
2038dd6c 21#include <common/sessiond-comm/sessiond-comm.h>
0d37f2bc
DG
22
23#include "unix.h"
24
25/*
26 * Connect to unix socket using the path name.
27 */
90e535ef 28LTTNG_HIDDEN
0d37f2bc
DG
29int lttcomm_connect_unix_sock(const char *pathname)
30{
665886a6 31 struct sockaddr_un s_un;
0d37f2bc
DG
32 int fd, ret, closeret;
33
7f8bf467
JG
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
0d37f2bc
DG
42 fd = socket(PF_UNIX, SOCK_STREAM, 0);
43 if (fd < 0) {
44 PERROR("socket");
45 ret = fd;
46 goto error;
47 }
48
665886a6
MJ
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';
0d37f2bc 53
665886a6 54 ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un));
0d37f2bc
DG
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
65error_connect:
66 closeret = close(fd);
67 if (closeret) {
68 PERROR("close");
69 }
70error:
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 */
90e535ef 78LTTNG_HIDDEN
0d37f2bc
DG
79int lttcomm_accept_unix_sock(int sock)
80{
81 int new_fd;
665886a6 82 struct sockaddr_un s_un;
50786a72 83 socklen_t len = sizeof(s_un);
0d37f2bc
DG
84
85 /* Blocking call */
665886a6 86 new_fd = accept(sock, (struct sockaddr *) &s_un, &len);
0d37f2bc
DG
87 if (new_fd < 0) {
88 PERROR("accept");
89 }
90
91 return new_fd;
92}
93
7567352f
MD
94LTTNG_HIDDEN
95int 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
0d37f2bc
DG
104/*
105 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
106 * and return the fd.
107 */
90e535ef 108LTTNG_HIDDEN
0d37f2bc
DG
109int lttcomm_create_unix_sock(const char *pathname)
110{
665886a6 111 struct sockaddr_un s_un;
7f8bf467 112 int fd = -1;
0d37f2bc
DG
113 int ret = -1;
114
7f8bf467
JG
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
0d37f2bc
DG
123 /* Create server socket */
124 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
125 PERROR("socket");
126 goto error;
127 }
128
665886a6
MJ
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';
0d37f2bc
DG
133
134 /* Unlink the old file if present */
135 (void) unlink(pathname);
665886a6 136 ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un));
0d37f2bc
DG
137 if (ret < 0) {
138 PERROR("bind");
139 goto error;
140 }
141
142 return fd;
143
144error:
17e75273
DG
145 if (fd >= 0) {
146 if (close(fd) < 0) {
147 PERROR("close create unix sock");
148 }
149 }
0d37f2bc
DG
150 return ret;
151}
152
153/*
154 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
155 */
90e535ef 156LTTNG_HIDDEN
0d37f2bc
DG
157int 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 */
90e535ef 175LTTNG_HIDDEN
0d37f2bc
DG
176ssize_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;
7c5aef62 181 size_t len_last;
0d37f2bc
DG
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 {
7c5aef62 191 len_last = iov[0].iov_len;
fbb1fd3a 192 ret = lttng_recvmsg_nosigpipe(sock, &msg);
7c5aef62
DG
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));
0d37f2bc
DG
199 if (ret < 0) {
200 PERROR("recvmsg");
7c5aef62
DG
201 } else if (ret > 0) {
202 ret = len;
0d37f2bc 203 }
7c5aef62 204 /* Else ret = 0 meaning an orderly shutdown. */
0d37f2bc
DG
205
206 return ret;
207}
208
c72435ad
JG
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 *
44c180ca
JR
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 *
c72435ad
JG
216 * Return the size of received data.
217 */
218LTTNG_HIDDEN
219ssize_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
232retry:
233 ret = lttng_recvmsg_nosigpipe(sock, &msg);
234 if (ret < 0) {
235 if (errno == EINTR) {
236 goto retry;
237 } else {
44c180ca
JR
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;
c72435ad 248 }
44c180ca
JR
249
250 /* Unexpected error */
251 PERROR("recvmsg");
252 ret = -1;
c72435ad
JG
253 goto end;
254 }
255 }
5b86bd5e 256
c72435ad
JG
257end:
258 return ret;
259}
260
0d37f2bc
DG
261/*
262 * Send buf data of size len. Using sendmsg API.
263 *
264 * Return the size of sent data.
265 */
90e535ef 266LTTNG_HIDDEN
c2d69327 267ssize_t lttcomm_send_unix_sock(int sock, const void *buf, size_t len)
0d37f2bc
DG
268{
269 struct msghdr msg;
270 struct iovec iov[1];
c72435ad 271 ssize_t ret;
0d37f2bc
DG
272
273 memset(&msg, 0, sizeof(msg));
274
c2d69327 275 iov[0].iov_base = (void *) buf;
0d37f2bc
DG
276 iov[0].iov_len = len;
277 msg.msg_iov = iov;
278 msg.msg_iovlen = 1;
279
c72435ad
JG
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;
301end:
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 *
44c180ca
JR
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 *
c72435ad
JG
314 * Return the size of sent data.
315 */
316LTTNG_HIDDEN
317ssize_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
330retry:
0d37f2bc
DG
331 ret = sendmsg(sock, &msg, 0);
332 if (ret < 0) {
c72435ad
JG
333 if (errno == EINTR) {
334 goto retry;
335 } else {
44c180ca
JR
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;
c72435ad 347 }
44c180ca
JR
348
349 /* Unexpected error */
350 PERROR("sendmsg");
351 ret = -1;
c72435ad 352 goto end;
0d37f2bc
DG
353 }
354 }
c72435ad 355end:
0d37f2bc
DG
356 return ret;
357}
358
359/*
360 * Shutdown cleanly a unix socket.
361 */
90e535ef 362LTTNG_HIDDEN
0d37f2bc
DG
363int 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 */
90e535ef 386LTTNG_HIDDEN
ac2f30af 387ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
0d37f2bc
DG
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));
b3f35e02 398 memset(tmp, 0, sizeof(tmp));
0d37f2bc
DG
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);
1d8d0328
MJ
407 if (!cmptr) {
408 return -1;
409 }
b3f35e02 410
0d37f2bc
DG
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
b72ce630
JR
438/*
439 * Send a message accompanied by fd(s) over a unix socket.
440 * Only use for non blocking socket.
441 *
442 * Returns the size of data sent, or negative error value.
443 */
444LTTNG_HIDDEN
445ssize_t lttcomm_send_fds_unix_sock_non_block(int sock, const int *fds, size_t nb_fd)
446{
447 struct msghdr msg;
448 struct cmsghdr *cmptr;
449 struct iovec iov[1];
450 ssize_t ret = -1;
451 unsigned int sizeof_fds = nb_fd * sizeof(int);
452 char tmp[CMSG_SPACE(sizeof_fds)];
453 char dummy = 0;
454
455 memset(&msg, 0, sizeof(msg));
456 memset(tmp, 0, sizeof(tmp));
457
458 if (nb_fd > LTTCOMM_MAX_SEND_FDS)
459 return -EINVAL;
460
461 msg.msg_control = (caddr_t)tmp;
462 msg.msg_controllen = CMSG_LEN(sizeof_fds);
463
464 cmptr = CMSG_FIRSTHDR(&msg);
465 if (!cmptr) {
466 return -1;
467 }
468
469 cmptr->cmsg_level = SOL_SOCKET;
470 cmptr->cmsg_type = SCM_RIGHTS;
471 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
472 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
473 /* Sum of the length of all control messages in the buffer: */
474 msg.msg_controllen = cmptr->cmsg_len;
475
476 iov[0].iov_base = &dummy;
477 iov[0].iov_len = 1;
478 msg.msg_iov = iov;
479 msg.msg_iovlen = 1;
480
481retry:
482 ret = sendmsg(sock, &msg, 0);
483 if (ret < 0) {
484 if (errno == EINTR) {
485 goto retry;
486 } else {
487 /*
488 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
489 */
490 if (errno == EAGAIN || errno == EWOULDBLOCK) {
491 /*
492 * This can happen in non blocking mode.
493 * Nothing was sent.
494 */
495 ret = 0;
496 goto end;
497 }
498
499 if (errno == EPIPE) {
500 /* Expected error, pass error to caller */
501 DBG3("EPIPE on sendmsg");
502 ret = -1;
503 goto end;
504 }
505
506 /* Unexpected error */
507 PERROR("sendmsg");
508 ret = -1;
509 goto end;
510 }
511 }
512
513end:
514 return ret;
515}
516
0d37f2bc
DG
517/*
518 * Recv a message accompanied by fd(s) from a unix socket.
519 *
520 * Returns the size of received data, or negative error value.
521 *
522 * Expect at most "nb_fd" file descriptors. Returns the number of fd
523 * actually received in nb_fd.
524 */
90e535ef 525LTTNG_HIDDEN
0d37f2bc
DG
526ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
527{
528 struct iovec iov[1];
529 ssize_t ret = 0;
530 struct cmsghdr *cmsg;
531 size_t sizeof_fds = nb_fd * sizeof(int);
b3f35e02 532
ba49ae8c
MJ
533#ifdef __linux__
534/* Account for the struct ucred cmsg in the buffer size */
535#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
536#else
537#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
538#endif /* __linux__ */
539
540 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
0d37f2bc
DG
541 struct msghdr msg;
542 char dummy;
543
544 memset(&msg, 0, sizeof(msg));
545
546 /* Prepare to receive the structures */
547 iov[0].iov_base = &dummy;
548 iov[0].iov_len = 1;
549 msg.msg_iov = iov;
550 msg.msg_iovlen = 1;
b3f35e02
FD
551
552 cmsg = (struct cmsghdr *) recv_buf;
553 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
554 cmsg->cmsg_level = SOL_SOCKET;
555 cmsg->cmsg_type = SCM_RIGHTS;
556
557 msg.msg_control = cmsg;
558 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
559 msg.msg_flags = 0;
0d37f2bc 560
b72ce630
JR
561retry:
562 ret = lttng_recvmsg_nosigpipe(sock, &msg);
0d37f2bc 563 if (ret < 0) {
b72ce630
JR
564 if (errno == EINTR) {
565 goto retry;
566 } else {
567 /* We consider EPIPE and EAGAIN as expected. */
568 if (!lttng_opt_quiet &&
569 (errno != EPIPE && errno != EAGAIN)) {
570 PERROR("recvmsg");
571 }
572 goto end;
573 }
574 }
575
576 if (ret != 1) {
577 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
578 ret, 1);
0d37f2bc
DG
579 goto end;
580 }
b3f35e02 581
b72ce630
JR
582 if (msg.msg_flags & MSG_CTRUNC) {
583 fprintf(stderr, "Error: Control message truncated.\n");
584 ret = -1;
585 goto end;
586 }
587
588 /*
589 * If the socket was configured with SO_PASSCRED, the kernel will add a
590 * control message (cmsg) to the ancillary data of the unix socket. We
591 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
592 * message.
593 */
594 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
595 if (cmsg->cmsg_level != SOL_SOCKET) {
596 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
597 ret = -1;
598 goto end;
599 }
600 if (cmsg->cmsg_type == SCM_RIGHTS) {
601 /*
602 * We found the controle message for file descriptors,
603 * now copy the fds to the fds ptr and return success.
604 */
605 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
606 fprintf(stderr, "Error: Received %zu bytes of"
607 "ancillary data for FDs, expected %zu\n",
608 (size_t) cmsg->cmsg_len,
609 (size_t) CMSG_LEN(sizeof_fds));
610 ret = -1;
611 goto end;
612 }
613 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
614 ret = sizeof_fds;
615 goto end;
616 }
617#ifdef __linux__
618 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
619 /*
620 * Expect credentials to be sent when expecting fds even
621 * if no credential were include in the send(). The
622 * kernel adds them...
623 */
624 ret = -1;
625 }
626#endif /* __linux__ */
627 }
628end:
629 return ret;
630}
631
632/*
633 * Recv a message accompanied by fd(s) from a non-blocking unix socket.
634 * Only use with non-blocking sockets.
635 *
636 * Returns the size of received data, or negative error value.
637 *
638 * Expect at most "nb_fd" file descriptors.
639 *
640 * Note that based on our comprehension, partial reception of fds is not
641 * possible since the FDs are actually in the control message. It is all or
642 * nothing, still the sender side can send the wrong number of fds.
643 */
644LTTNG_HIDDEN
645ssize_t lttcomm_recv_fds_unix_sock_non_block(int sock, int *fds, size_t nb_fd)
646{
647 struct iovec iov[1];
648 ssize_t ret = 0;
649 struct cmsghdr *cmsg;
650 size_t sizeof_fds = nb_fd * sizeof(int);
651
652#ifdef __linux__
653/* Account for the struct ucred cmsg in the buffer size */
654#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred))
655#else
656#define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds)
657#endif /* __linux__ */
658
659 char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE];
660 struct msghdr msg;
661 char dummy;
662
663 memset(&msg, 0, sizeof(msg));
664
665 /* Prepare to receive the structures */
666 iov[0].iov_base = &dummy;
667 iov[0].iov_len = 1;
668 msg.msg_iov = iov;
669 msg.msg_iovlen = 1;
670
671 cmsg = (struct cmsghdr *) recv_buf;
672 cmsg->cmsg_len = CMSG_LEN(sizeof_fds);
673 cmsg->cmsg_level = SOL_SOCKET;
674 cmsg->cmsg_type = SCM_RIGHTS;
675
676 msg.msg_control = cmsg;
677 msg.msg_controllen = CMSG_LEN(sizeof(recv_buf));
678 msg.msg_flags = 0;
679
680retry:
681 ret = lttng_recvmsg_nosigpipe(sock, &msg);
682 if (ret < 0) {
683 if (errno == EINTR) {
684 goto retry;
685 } else {
686 /*
687 * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
688 */
689 if (errno == EAGAIN || errno == EWOULDBLOCK) {
690 /*
691 * This can happen in non blocking mode.
692 * Nothing was recv.
693 */
694 ret = 0;
695 goto end;
696 }
697
698 if (errno == EPIPE) {
699 /* Expected error, pass error to caller */
700 DBG3("EPIPE on recvmsg");
701 ret = -1;
702 goto end;
703 }
704
705 /* Unexpected error */
706 PERROR("recvmsg");
707 ret = -1;
708 goto end;
709 }
710 }
711
0d37f2bc
DG
712 if (ret != 1) {
713 fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
714 ret, 1);
715 goto end;
716 }
b3f35e02 717
0d37f2bc
DG
718 if (msg.msg_flags & MSG_CTRUNC) {
719 fprintf(stderr, "Error: Control message truncated.\n");
720 ret = -1;
721 goto end;
722 }
b3f35e02
FD
723
724 /*
725 * If the socket was configured with SO_PASSCRED, the kernel will add a
726 * control message (cmsg) to the ancillary data of the unix socket. We
727 * need to expect a cmsg of the SCM_CREDENTIALS as the first control
728 * message.
729 */
730 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
b3f35e02
FD
731 if (cmsg->cmsg_level != SOL_SOCKET) {
732 fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
733 ret = -1;
734 goto end;
735 }
736 if (cmsg->cmsg_type == SCM_RIGHTS) {
737 /*
738 * We found the controle message for file descriptors,
739 * now copy the fds to the fds ptr and return success.
740 */
741 if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
742 fprintf(stderr, "Error: Received %zu bytes of"
743 "ancillary data for FDs, expected %zu\n",
744 (size_t) cmsg->cmsg_len,
745 (size_t) CMSG_LEN(sizeof_fds));
746 ret = -1;
747 goto end;
748 }
749 memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
750 ret = sizeof_fds;
751 goto end;
752 }
ba49ae8c 753#ifdef __linux__
b3f35e02
FD
754 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
755 /*
756 * Expect credentials to be sent when expecting fds even
757 * if no credential were include in the send(). The
758 * kernel adds them...
759 */
b3f35e02
FD
760 ret = -1;
761 }
ba49ae8c 762#endif /* __linux__ */
0d37f2bc 763 }
0d37f2bc
DG
764end:
765 return ret;
766}
767
768/*
769 * Send a message with credentials over a unix socket.
770 *
771 * Returns the size of data sent, or negative error value.
772 */
90e535ef 773LTTNG_HIDDEN
0d37f2bc
DG
774ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len)
775{
776 struct msghdr msg;
777 struct iovec iov[1];
778 ssize_t ret = -1;
779#ifdef __linux__
780 struct cmsghdr *cmptr;
781 size_t sizeof_cred = sizeof(lttng_sock_cred);
782 char anc_buf[CMSG_SPACE(sizeof_cred)];
783 lttng_sock_cred *creds;
8bbffd54
MJ
784
785 memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char));
0d37f2bc
DG
786#endif /* __linux__ */
787
788 memset(&msg, 0, sizeof(msg));
789
790 iov[0].iov_base = buf;
791 iov[0].iov_len = len;
792 msg.msg_iov = iov;
793 msg.msg_iovlen = 1;
794
795#ifdef __linux__
796 msg.msg_control = (caddr_t) anc_buf;
797 msg.msg_controllen = CMSG_LEN(sizeof_cred);
798
799 cmptr = CMSG_FIRSTHDR(&msg);
1d8d0328
MJ
800 if (!cmptr) {
801 return -1;
802 }
0d37f2bc
DG
803 cmptr->cmsg_level = SOL_SOCKET;
804 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
805 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
806
807 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
808
809 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
810 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
811 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
812#endif /* __linux__ */
813
814 do {
815 ret = sendmsg(sock, &msg, 0);
816 } while (ret < 0 && errno == EINTR);
817 if (ret < 0) {
818 /*
819 * Only warn about EPIPE when quiet mode is deactivated.
820 * We consider EPIPE as expected.
821 */
822 if (errno != EPIPE || !lttng_opt_quiet) {
823 PERROR("sendmsg");
824 }
825 }
826 return ret;
827}
828
829/*
830 * Recv a message accompanied with credentials from a unix socket.
831 *
832 * Returns the size of received data, or negative error value.
833 */
90e535ef 834LTTNG_HIDDEN
0d37f2bc
DG
835ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
836 lttng_sock_cred *creds)
837{
838 struct msghdr msg;
839 struct iovec iov[1];
840 ssize_t ret;
4100e49a 841 size_t len_last;
0d37f2bc
DG
842#ifdef __linux__
843 struct cmsghdr *cmptr;
844 size_t sizeof_cred = sizeof(lttng_sock_cred);
845 char anc_buf[CMSG_SPACE(sizeof_cred)];
846#endif /* __linux__ */
847
848 memset(&msg, 0, sizeof(msg));
849
850 /* Not allowed */
851 if (creds == NULL) {
852 ret = -1;
853 goto end;
854 }
855
856 /* Prepare to receive the structures */
857 iov[0].iov_base = buf;
858 iov[0].iov_len = len;
859 msg.msg_iov = iov;
860 msg.msg_iovlen = 1;
861
862#ifdef __linux__
863 msg.msg_control = anc_buf;
864 msg.msg_controllen = sizeof(anc_buf);
865#endif /* __linux__ */
866
867 do {
4100e49a 868 len_last = iov[0].iov_len;
0d37f2bc 869 ret = recvmsg(sock, &msg, 0);
4100e49a
DG
870 if (ret > 0) {
871 iov[0].iov_base += ret;
872 iov[0].iov_len -= ret;
873 assert(ret <= len_last);
874 }
875 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
0d37f2bc
DG
876 if (ret < 0) {
877 PERROR("recvmsg fds");
878 goto end;
4100e49a
DG
879 } else if (ret > 0) {
880 ret = len;
0d37f2bc 881 }
4100e49a 882 /* Else ret = 0 meaning an orderly shutdown. */
0d37f2bc
DG
883
884#ifdef __linux__
885 if (msg.msg_flags & MSG_CTRUNC) {
886 fprintf(stderr, "Error: Control message truncated.\n");
887 ret = -1;
888 goto end;
889 }
890
891 cmptr = CMSG_FIRSTHDR(&msg);
892 if (cmptr == NULL) {
893 fprintf(stderr, "Error: Invalid control message header\n");
894 ret = -1;
895 goto end;
896 }
897
898 if (cmptr->cmsg_level != SOL_SOCKET ||
899 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
900 fprintf(stderr, "Didn't received any credentials\n");
901 ret = -1;
902 goto end;
903 }
904
905 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
906 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
907 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
908 ret = -1;
909 goto end;
910 }
911
912 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
bfa419e4 913#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
0d37f2bc
DG
914 {
915 int peer_ret;
916
917 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
918 if (peer_ret != 0) {
919 return peer_ret;
920 }
921 }
922#else
923#error "Please implement credential support for your OS."
924#endif /* __linux__ */
925
926end:
927 return ret;
928}
929
930/*
931 * Set socket option to use credentials passing.
932 */
933#ifdef __linux__
90e535ef 934LTTNG_HIDDEN
0d37f2bc
DG
935int lttcomm_setsockopt_creds_unix_sock(int sock)
936{
937 int ret, on = 1;
938
939 /* Set socket for credentials retrieval */
940 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
941 if (ret < 0) {
942 PERROR("setsockopt creds unix sock");
943 }
944 return ret;
945}
bfa419e4 946#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
90e535ef 947LTTNG_HIDDEN
0d37f2bc
DG
948int lttcomm_setsockopt_creds_unix_sock(int sock)
949{
950 return 0;
951}
952#else
953#error "Please implement credential support for your OS."
954#endif /* __linux__ */
This page took 0.110595 seconds and 5 git commands to generate.