Commit | Line | Data |
---|---|---|
0d37f2bc DG |
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 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
0d37f2bc DG |
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 | ||
90e535ef | 30 | #include <common/common.h> |
2038dd6c | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
0d37f2bc DG |
32 | |
33 | #include "unix.h" | |
34 | ||
35 | /* | |
36 | * Connect to unix socket using the path name. | |
37 | */ | |
90e535ef | 38 | LTTNG_HIDDEN |
0d37f2bc DG |
39 | int lttcomm_connect_unix_sock(const char *pathname) |
40 | { | |
665886a6 | 41 | struct sockaddr_un s_un; |
0d37f2bc DG |
42 | int fd, ret, closeret; |
43 | ||
7f8bf467 JG |
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 | ||
0d37f2bc DG |
52 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
53 | if (fd < 0) { | |
54 | PERROR("socket"); | |
55 | ret = fd; | |
56 | goto error; | |
57 | } | |
58 | ||
665886a6 MJ |
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'; | |
0d37f2bc | 63 | |
665886a6 | 64 | ret = connect(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
0d37f2bc DG |
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 | */ | |
90e535ef | 88 | LTTNG_HIDDEN |
0d37f2bc DG |
89 | int lttcomm_accept_unix_sock(int sock) |
90 | { | |
91 | int new_fd; | |
665886a6 | 92 | struct sockaddr_un s_un; |
50786a72 | 93 | socklen_t len = sizeof(s_un); |
0d37f2bc DG |
94 | |
95 | /* Blocking call */ | |
665886a6 | 96 | new_fd = accept(sock, (struct sockaddr *) &s_un, &len); |
0d37f2bc DG |
97 | if (new_fd < 0) { |
98 | PERROR("accept"); | |
99 | } | |
100 | ||
101 | return new_fd; | |
102 | } | |
103 | ||
7567352f MD |
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 | ||
0d37f2bc DG |
114 | /* |
115 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation | |
116 | * and return the fd. | |
117 | */ | |
90e535ef | 118 | LTTNG_HIDDEN |
0d37f2bc DG |
119 | int lttcomm_create_unix_sock(const char *pathname) |
120 | { | |
665886a6 | 121 | struct sockaddr_un s_un; |
7f8bf467 | 122 | int fd = -1; |
0d37f2bc DG |
123 | int ret = -1; |
124 | ||
7f8bf467 JG |
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 | ||
0d37f2bc DG |
133 | /* Create server socket */ |
134 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
135 | PERROR("socket"); | |
136 | goto error; | |
137 | } | |
138 | ||
665886a6 MJ |
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'; | |
0d37f2bc DG |
143 | |
144 | /* Unlink the old file if present */ | |
145 | (void) unlink(pathname); | |
665886a6 | 146 | ret = bind(fd, (struct sockaddr *) &s_un, sizeof(s_un)); |
0d37f2bc DG |
147 | if (ret < 0) { |
148 | PERROR("bind"); | |
149 | goto error; | |
150 | } | |
151 | ||
152 | return fd; | |
153 | ||
154 | error: | |
17e75273 DG |
155 | if (fd >= 0) { |
156 | if (close(fd) < 0) { | |
157 | PERROR("close create unix sock"); | |
158 | } | |
159 | } | |
0d37f2bc DG |
160 | return ret; |
161 | } | |
162 | ||
163 | /* | |
164 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
165 | */ | |
90e535ef | 166 | LTTNG_HIDDEN |
0d37f2bc DG |
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 | */ | |
90e535ef | 185 | LTTNG_HIDDEN |
0d37f2bc DG |
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; | |
7c5aef62 | 191 | size_t len_last; |
0d37f2bc DG |
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 { | |
7c5aef62 | 201 | len_last = iov[0].iov_len; |
fbb1fd3a | 202 | ret = lttng_recvmsg_nosigpipe(sock, &msg); |
7c5aef62 DG |
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)); | |
0d37f2bc DG |
209 | if (ret < 0) { |
210 | PERROR("recvmsg"); | |
7c5aef62 DG |
211 | } else if (ret > 0) { |
212 | ret = len; | |
0d37f2bc | 213 | } |
7c5aef62 | 214 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
215 | |
216 | return ret; | |
217 | } | |
218 | ||
c72435ad JG |
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 | /* | |
246 | * Only warn about EPIPE when quiet mode is | |
247 | * deactivated. | |
248 | * We consider EPIPE as expected. | |
249 | */ | |
250 | if (errno != EPIPE || !lttng_opt_quiet) { | |
251 | PERROR("recvmsg"); | |
252 | } | |
253 | goto end; | |
254 | } | |
255 | } | |
256 | ret = len; | |
257 | end: | |
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 | 266 | LTTNG_HIDDEN |
c2d69327 | 267 | ssize_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; | |
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 | * Return the size of sent data. | |
312 | */ | |
313 | LTTNG_HIDDEN | |
314 | ssize_t lttcomm_send_unix_sock_non_block(int sock, const void *buf, size_t len) | |
315 | { | |
316 | struct msghdr msg; | |
317 | struct iovec iov[1]; | |
318 | ssize_t ret; | |
319 | ||
320 | memset(&msg, 0, sizeof(msg)); | |
321 | ||
322 | iov[0].iov_base = (void *) buf; | |
323 | iov[0].iov_len = len; | |
324 | msg.msg_iov = iov; | |
325 | msg.msg_iovlen = 1; | |
326 | ||
327 | retry: | |
0d37f2bc DG |
328 | ret = sendmsg(sock, &msg, 0); |
329 | if (ret < 0) { | |
c72435ad JG |
330 | if (errno == EINTR) { |
331 | goto retry; | |
332 | } else { | |
333 | /* | |
334 | * Only warn about EPIPE when quiet mode is | |
335 | * deactivated. | |
336 | * We consider EPIPE as expected. | |
337 | */ | |
338 | if (errno != EPIPE || !lttng_opt_quiet) { | |
339 | PERROR("sendmsg"); | |
340 | } | |
341 | goto end; | |
0d37f2bc DG |
342 | } |
343 | } | |
c72435ad JG |
344 | ret = len; |
345 | end: | |
0d37f2bc DG |
346 | return ret; |
347 | } | |
348 | ||
349 | /* | |
350 | * Shutdown cleanly a unix socket. | |
351 | */ | |
90e535ef | 352 | LTTNG_HIDDEN |
0d37f2bc DG |
353 | int lttcomm_close_unix_sock(int sock) |
354 | { | |
355 | int ret, closeret; | |
356 | ||
357 | /* Shutdown receptions and transmissions */ | |
358 | ret = shutdown(sock, SHUT_RDWR); | |
359 | if (ret < 0) { | |
360 | PERROR("shutdown"); | |
361 | } | |
362 | ||
363 | closeret = close(sock); | |
364 | if (closeret) { | |
365 | PERROR("close"); | |
366 | } | |
367 | ||
368 | return ret; | |
369 | } | |
370 | ||
371 | /* | |
372 | * Send a message accompanied by fd(s) over a unix socket. | |
373 | * | |
374 | * Returns the size of data sent, or negative error value. | |
375 | */ | |
90e535ef | 376 | LTTNG_HIDDEN |
ac2f30af | 377 | ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd) |
0d37f2bc DG |
378 | { |
379 | struct msghdr msg; | |
380 | struct cmsghdr *cmptr; | |
381 | struct iovec iov[1]; | |
382 | ssize_t ret = -1; | |
383 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
384 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
385 | char dummy = 0; | |
386 | ||
387 | memset(&msg, 0, sizeof(msg)); | |
b3f35e02 | 388 | memset(tmp, 0, sizeof(tmp)); |
0d37f2bc DG |
389 | |
390 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) | |
391 | return -EINVAL; | |
392 | ||
393 | msg.msg_control = (caddr_t)tmp; | |
394 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
395 | ||
396 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
397 | if (!cmptr) { |
398 | return -1; | |
399 | } | |
b3f35e02 | 400 | |
0d37f2bc DG |
401 | cmptr->cmsg_level = SOL_SOCKET; |
402 | cmptr->cmsg_type = SCM_RIGHTS; | |
403 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
404 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
405 | /* Sum of the length of all control messages in the buffer: */ | |
406 | msg.msg_controllen = cmptr->cmsg_len; | |
407 | ||
408 | iov[0].iov_base = &dummy; | |
409 | iov[0].iov_len = 1; | |
410 | msg.msg_iov = iov; | |
411 | msg.msg_iovlen = 1; | |
412 | ||
413 | do { | |
414 | ret = sendmsg(sock, &msg, 0); | |
415 | } while (ret < 0 && errno == EINTR); | |
416 | if (ret < 0) { | |
417 | /* | |
418 | * Only warn about EPIPE when quiet mode is deactivated. | |
419 | * We consider EPIPE as expected. | |
420 | */ | |
421 | if (errno != EPIPE || !lttng_opt_quiet) { | |
422 | PERROR("sendmsg"); | |
423 | } | |
424 | } | |
425 | return ret; | |
426 | } | |
427 | ||
428 | /* | |
429 | * Recv a message accompanied by fd(s) from a unix socket. | |
430 | * | |
431 | * Returns the size of received data, or negative error value. | |
432 | * | |
433 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
434 | * actually received in nb_fd. | |
435 | */ | |
90e535ef | 436 | LTTNG_HIDDEN |
0d37f2bc DG |
437 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
438 | { | |
439 | struct iovec iov[1]; | |
440 | ssize_t ret = 0; | |
441 | struct cmsghdr *cmsg; | |
442 | size_t sizeof_fds = nb_fd * sizeof(int); | |
b3f35e02 | 443 | |
6380c001 MJ |
444 | #ifdef __linux__ |
445 | /* Account for the struct ucred cmsg in the buffer size */ | |
446 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) + CMSG_SPACE(sizeof(struct ucred)) | |
447 | #else | |
448 | #define LTTNG_SOCK_RECV_FDS_BUF_SIZE CMSG_SPACE(sizeof_fds) | |
449 | #endif /* __linux__ */ | |
450 | ||
451 | char recv_buf[LTTNG_SOCK_RECV_FDS_BUF_SIZE]; | |
0d37f2bc DG |
452 | struct msghdr msg; |
453 | char dummy; | |
454 | ||
455 | memset(&msg, 0, sizeof(msg)); | |
456 | ||
457 | /* Prepare to receive the structures */ | |
458 | iov[0].iov_base = &dummy; | |
459 | iov[0].iov_len = 1; | |
460 | msg.msg_iov = iov; | |
461 | msg.msg_iovlen = 1; | |
b3f35e02 FD |
462 | |
463 | cmsg = (struct cmsghdr *) recv_buf; | |
464 | cmsg->cmsg_len = CMSG_LEN(sizeof_fds); | |
465 | cmsg->cmsg_level = SOL_SOCKET; | |
466 | cmsg->cmsg_type = SCM_RIGHTS; | |
467 | ||
468 | msg.msg_control = cmsg; | |
469 | msg.msg_controllen = CMSG_LEN(sizeof(recv_buf)); | |
470 | msg.msg_flags = 0; | |
0d37f2bc DG |
471 | |
472 | do { | |
473 | ret = recvmsg(sock, &msg, 0); | |
474 | } while (ret < 0 && errno == EINTR); | |
475 | if (ret < 0) { | |
476 | PERROR("recvmsg fds"); | |
477 | goto end; | |
478 | } | |
b3f35e02 | 479 | |
0d37f2bc DG |
480 | if (ret != 1) { |
481 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
482 | ret, 1); | |
483 | goto end; | |
484 | } | |
b3f35e02 | 485 | |
0d37f2bc DG |
486 | if (msg.msg_flags & MSG_CTRUNC) { |
487 | fprintf(stderr, "Error: Control message truncated.\n"); | |
488 | ret = -1; | |
489 | goto end; | |
490 | } | |
b3f35e02 FD |
491 | |
492 | /* | |
493 | * If the socket was configured with SO_PASSCRED, the kernel will add a | |
494 | * control message (cmsg) to the ancillary data of the unix socket. We | |
495 | * need to expect a cmsg of the SCM_CREDENTIALS as the first control | |
496 | * message. | |
497 | */ | |
498 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
b3f35e02 FD |
499 | if (cmsg->cmsg_level != SOL_SOCKET) { |
500 | fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n"); | |
501 | ret = -1; | |
502 | goto end; | |
503 | } | |
504 | if (cmsg->cmsg_type == SCM_RIGHTS) { | |
505 | /* | |
506 | * We found the controle message for file descriptors, | |
507 | * now copy the fds to the fds ptr and return success. | |
508 | */ | |
509 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
510 | fprintf(stderr, "Error: Received %zu bytes of" | |
511 | "ancillary data for FDs, expected %zu\n", | |
512 | (size_t) cmsg->cmsg_len, | |
513 | (size_t) CMSG_LEN(sizeof_fds)); | |
514 | ret = -1; | |
515 | goto end; | |
516 | } | |
517 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
518 | ret = sizeof_fds; | |
519 | goto end; | |
520 | } | |
6380c001 | 521 | #ifdef __linux__ |
b3f35e02 FD |
522 | if (cmsg->cmsg_type == SCM_CREDENTIALS) { |
523 | /* | |
524 | * Expect credentials to be sent when expecting fds even | |
525 | * if no credential were include in the send(). The | |
526 | * kernel adds them... | |
527 | */ | |
b3f35e02 FD |
528 | ret = -1; |
529 | } | |
6380c001 | 530 | #endif /* __linux__ */ |
0d37f2bc | 531 | } |
0d37f2bc DG |
532 | end: |
533 | return ret; | |
534 | } | |
535 | ||
536 | /* | |
537 | * Send a message with credentials over a unix socket. | |
538 | * | |
539 | * Returns the size of data sent, or negative error value. | |
540 | */ | |
90e535ef | 541 | LTTNG_HIDDEN |
0d37f2bc DG |
542 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
543 | { | |
544 | struct msghdr msg; | |
545 | struct iovec iov[1]; | |
546 | ssize_t ret = -1; | |
547 | #ifdef __linux__ | |
548 | struct cmsghdr *cmptr; | |
549 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
550 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
551 | lttng_sock_cred *creds; | |
8bbffd54 MJ |
552 | |
553 | memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char)); | |
0d37f2bc DG |
554 | #endif /* __linux__ */ |
555 | ||
556 | memset(&msg, 0, sizeof(msg)); | |
557 | ||
558 | iov[0].iov_base = buf; | |
559 | iov[0].iov_len = len; | |
560 | msg.msg_iov = iov; | |
561 | msg.msg_iovlen = 1; | |
562 | ||
563 | #ifdef __linux__ | |
564 | msg.msg_control = (caddr_t) anc_buf; | |
565 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
566 | ||
567 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
568 | if (!cmptr) { |
569 | return -1; | |
570 | } | |
0d37f2bc DG |
571 | cmptr->cmsg_level = SOL_SOCKET; |
572 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; | |
573 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
574 | ||
575 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); | |
576 | ||
577 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); | |
578 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); | |
579 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); | |
580 | #endif /* __linux__ */ | |
581 | ||
582 | do { | |
583 | ret = sendmsg(sock, &msg, 0); | |
584 | } while (ret < 0 && errno == EINTR); | |
585 | if (ret < 0) { | |
586 | /* | |
587 | * Only warn about EPIPE when quiet mode is deactivated. | |
588 | * We consider EPIPE as expected. | |
589 | */ | |
590 | if (errno != EPIPE || !lttng_opt_quiet) { | |
591 | PERROR("sendmsg"); | |
592 | } | |
593 | } | |
594 | return ret; | |
595 | } | |
596 | ||
597 | /* | |
598 | * Recv a message accompanied with credentials from a unix socket. | |
599 | * | |
600 | * Returns the size of received data, or negative error value. | |
601 | */ | |
90e535ef | 602 | LTTNG_HIDDEN |
0d37f2bc DG |
603 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
604 | lttng_sock_cred *creds) | |
605 | { | |
606 | struct msghdr msg; | |
607 | struct iovec iov[1]; | |
608 | ssize_t ret; | |
4100e49a | 609 | size_t len_last; |
0d37f2bc DG |
610 | #ifdef __linux__ |
611 | struct cmsghdr *cmptr; | |
612 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
613 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
614 | #endif /* __linux__ */ | |
615 | ||
616 | memset(&msg, 0, sizeof(msg)); | |
617 | ||
618 | /* Not allowed */ | |
619 | if (creds == NULL) { | |
620 | ret = -1; | |
621 | goto end; | |
622 | } | |
623 | ||
624 | /* Prepare to receive the structures */ | |
625 | iov[0].iov_base = buf; | |
626 | iov[0].iov_len = len; | |
627 | msg.msg_iov = iov; | |
628 | msg.msg_iovlen = 1; | |
629 | ||
630 | #ifdef __linux__ | |
631 | msg.msg_control = anc_buf; | |
632 | msg.msg_controllen = sizeof(anc_buf); | |
633 | #endif /* __linux__ */ | |
634 | ||
635 | do { | |
4100e49a | 636 | len_last = iov[0].iov_len; |
0d37f2bc | 637 | ret = recvmsg(sock, &msg, 0); |
4100e49a DG |
638 | if (ret > 0) { |
639 | iov[0].iov_base += ret; | |
640 | iov[0].iov_len -= ret; | |
641 | assert(ret <= len_last); | |
642 | } | |
643 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
644 | if (ret < 0) { |
645 | PERROR("recvmsg fds"); | |
646 | goto end; | |
4100e49a DG |
647 | } else if (ret > 0) { |
648 | ret = len; | |
0d37f2bc | 649 | } |
4100e49a | 650 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
651 | |
652 | #ifdef __linux__ | |
653 | if (msg.msg_flags & MSG_CTRUNC) { | |
654 | fprintf(stderr, "Error: Control message truncated.\n"); | |
655 | ret = -1; | |
656 | goto end; | |
657 | } | |
658 | ||
659 | cmptr = CMSG_FIRSTHDR(&msg); | |
660 | if (cmptr == NULL) { | |
661 | fprintf(stderr, "Error: Invalid control message header\n"); | |
662 | ret = -1; | |
663 | goto end; | |
664 | } | |
665 | ||
666 | if (cmptr->cmsg_level != SOL_SOCKET || | |
667 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { | |
668 | fprintf(stderr, "Didn't received any credentials\n"); | |
669 | ret = -1; | |
670 | goto end; | |
671 | } | |
672 | ||
673 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
674 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
675 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); | |
676 | ret = -1; | |
677 | goto end; | |
678 | } | |
679 | ||
680 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
bfa419e4 | 681 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
0d37f2bc DG |
682 | { |
683 | int peer_ret; | |
684 | ||
685 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); | |
686 | if (peer_ret != 0) { | |
687 | return peer_ret; | |
688 | } | |
689 | } | |
690 | #else | |
691 | #error "Please implement credential support for your OS." | |
692 | #endif /* __linux__ */ | |
693 | ||
694 | end: | |
695 | return ret; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Set socket option to use credentials passing. | |
700 | */ | |
701 | #ifdef __linux__ | |
90e535ef | 702 | LTTNG_HIDDEN |
0d37f2bc DG |
703 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
704 | { | |
705 | int ret, on = 1; | |
706 | ||
707 | /* Set socket for credentials retrieval */ | |
708 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
709 | if (ret < 0) { | |
710 | PERROR("setsockopt creds unix sock"); | |
711 | } | |
712 | return ret; | |
713 | } | |
bfa419e4 | 714 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
90e535ef | 715 | LTTNG_HIDDEN |
0d37f2bc DG |
716 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
717 | { | |
718 | return 0; | |
719 | } | |
720 | #else | |
721 | #error "Please implement credential support for your OS." | |
722 | #endif /* __linux__ */ |