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