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 | ||
19 | #define _GNU_SOURCE | |
6c1c0768 | 20 | #define _LGPL_SOURCE |
0d37f2bc DG |
21 | #include <assert.h> |
22 | #include <limits.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <unistd.h> | |
29 | #include <errno.h> | |
30 | ||
90e535ef | 31 | #include <common/common.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 | { | |
41 | struct sockaddr_un sun; | |
42 | int fd, ret, closeret; | |
43 | ||
44 | fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
45 | if (fd < 0) { | |
46 | PERROR("socket"); | |
47 | ret = fd; | |
48 | goto error; | |
49 | } | |
50 | ||
51 | memset(&sun, 0, sizeof(sun)); | |
52 | sun.sun_family = AF_UNIX; | |
53 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
54 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
55 | ||
56 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
57 | if (ret < 0) { | |
58 | /* | |
59 | * Don't print message on connect error, because connect is used in | |
60 | * normal execution to detect if sessiond is alive. | |
61 | */ | |
62 | goto error_connect; | |
63 | } | |
64 | ||
65 | return fd; | |
66 | ||
67 | error_connect: | |
68 | closeret = close(fd); | |
69 | if (closeret) { | |
70 | PERROR("close"); | |
71 | } | |
72 | error: | |
73 | return ret; | |
74 | } | |
75 | ||
76 | /* | |
77 | * Do an accept(2) on the sock and return the new file descriptor. The socket | |
78 | * MUST be bind(2) before. | |
79 | */ | |
90e535ef | 80 | LTTNG_HIDDEN |
0d37f2bc DG |
81 | int lttcomm_accept_unix_sock(int sock) |
82 | { | |
83 | int new_fd; | |
84 | struct sockaddr_un sun; | |
85 | socklen_t len = 0; | |
86 | ||
87 | /* Blocking call */ | |
88 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
89 | if (new_fd < 0) { | |
90 | PERROR("accept"); | |
91 | } | |
92 | ||
93 | return new_fd; | |
94 | } | |
95 | ||
7567352f MD |
96 | LTTNG_HIDDEN |
97 | int lttcomm_create_anon_unix_socketpair(int *fds) | |
98 | { | |
99 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) < 0) { | |
100 | PERROR("socketpair"); | |
101 | return -1; | |
102 | } | |
103 | return 0; | |
104 | } | |
105 | ||
0d37f2bc DG |
106 | /* |
107 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation | |
108 | * and return the fd. | |
109 | */ | |
90e535ef | 110 | LTTNG_HIDDEN |
0d37f2bc DG |
111 | int lttcomm_create_unix_sock(const char *pathname) |
112 | { | |
113 | struct sockaddr_un sun; | |
114 | int fd; | |
115 | int ret = -1; | |
116 | ||
117 | /* Create server socket */ | |
118 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
119 | PERROR("socket"); | |
120 | goto error; | |
121 | } | |
122 | ||
123 | memset(&sun, 0, sizeof(sun)); | |
124 | sun.sun_family = AF_UNIX; | |
125 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
126 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
127 | ||
128 | /* Unlink the old file if present */ | |
129 | (void) unlink(pathname); | |
130 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); | |
131 | if (ret < 0) { | |
132 | PERROR("bind"); | |
133 | goto error; | |
134 | } | |
135 | ||
136 | return fd; | |
137 | ||
138 | error: | |
17e75273 DG |
139 | if (fd >= 0) { |
140 | if (close(fd) < 0) { | |
141 | PERROR("close create unix sock"); | |
142 | } | |
143 | } | |
0d37f2bc DG |
144 | return ret; |
145 | } | |
146 | ||
147 | /* | |
148 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
149 | */ | |
90e535ef | 150 | LTTNG_HIDDEN |
0d37f2bc DG |
151 | int lttcomm_listen_unix_sock(int sock) |
152 | { | |
153 | int ret; | |
154 | ||
155 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); | |
156 | if (ret < 0) { | |
157 | PERROR("listen"); | |
158 | } | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | /* | |
164 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
165 | * API. | |
166 | * | |
167 | * Return the size of received data. | |
168 | */ | |
90e535ef | 169 | LTTNG_HIDDEN |
0d37f2bc DG |
170 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
171 | { | |
172 | struct msghdr msg; | |
173 | struct iovec iov[1]; | |
174 | ssize_t ret = -1; | |
7c5aef62 | 175 | size_t len_last; |
0d37f2bc DG |
176 | |
177 | memset(&msg, 0, sizeof(msg)); | |
178 | ||
179 | iov[0].iov_base = buf; | |
180 | iov[0].iov_len = len; | |
181 | msg.msg_iov = iov; | |
182 | msg.msg_iovlen = 1; | |
183 | ||
184 | do { | |
7c5aef62 | 185 | len_last = iov[0].iov_len; |
fbe49b3b | 186 | ret = recvmsg(sock, &msg, MSG_NOSIGNAL); |
7c5aef62 DG |
187 | if (ret > 0) { |
188 | iov[0].iov_base += ret; | |
189 | iov[0].iov_len -= ret; | |
190 | assert(ret <= len_last); | |
191 | } | |
192 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
193 | if (ret < 0) { |
194 | PERROR("recvmsg"); | |
7c5aef62 DG |
195 | } else if (ret > 0) { |
196 | ret = len; | |
0d37f2bc | 197 | } |
7c5aef62 | 198 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
199 | |
200 | return ret; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Send buf data of size len. Using sendmsg API. | |
205 | * | |
206 | * Return the size of sent data. | |
207 | */ | |
90e535ef | 208 | LTTNG_HIDDEN |
0d37f2bc DG |
209 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) |
210 | { | |
211 | struct msghdr msg; | |
212 | struct iovec iov[1]; | |
213 | ssize_t ret = -1; | |
214 | ||
215 | memset(&msg, 0, sizeof(msg)); | |
216 | ||
217 | iov[0].iov_base = buf; | |
218 | iov[0].iov_len = len; | |
219 | msg.msg_iov = iov; | |
220 | msg.msg_iovlen = 1; | |
221 | ||
222 | ret = sendmsg(sock, &msg, 0); | |
223 | if (ret < 0) { | |
224 | /* | |
225 | * Only warn about EPIPE when quiet mode is deactivated. | |
226 | * We consider EPIPE as expected. | |
227 | */ | |
228 | if (errno != EPIPE || !lttng_opt_quiet) { | |
229 | PERROR("sendmsg"); | |
230 | } | |
231 | } | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | /* | |
237 | * Shutdown cleanly a unix socket. | |
238 | */ | |
90e535ef | 239 | LTTNG_HIDDEN |
0d37f2bc DG |
240 | int lttcomm_close_unix_sock(int sock) |
241 | { | |
242 | int ret, closeret; | |
243 | ||
244 | /* Shutdown receptions and transmissions */ | |
245 | ret = shutdown(sock, SHUT_RDWR); | |
246 | if (ret < 0) { | |
247 | PERROR("shutdown"); | |
248 | } | |
249 | ||
250 | closeret = close(sock); | |
251 | if (closeret) { | |
252 | PERROR("close"); | |
253 | } | |
254 | ||
255 | return ret; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Send a message accompanied by fd(s) over a unix socket. | |
260 | * | |
261 | * Returns the size of data sent, or negative error value. | |
262 | */ | |
90e535ef | 263 | LTTNG_HIDDEN |
0d37f2bc DG |
264 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
265 | { | |
266 | struct msghdr msg; | |
267 | struct cmsghdr *cmptr; | |
268 | struct iovec iov[1]; | |
269 | ssize_t ret = -1; | |
270 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
271 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
272 | char dummy = 0; | |
273 | ||
274 | memset(&msg, 0, sizeof(msg)); | |
c617c0c6 | 275 | memset(tmp, 0, CMSG_SPACE(sizeof_fds) * sizeof(char)); |
0d37f2bc DG |
276 | |
277 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) | |
278 | return -EINVAL; | |
279 | ||
280 | msg.msg_control = (caddr_t)tmp; | |
281 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
282 | ||
283 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
284 | if (!cmptr) { |
285 | return -1; | |
286 | } | |
0d37f2bc DG |
287 | cmptr->cmsg_level = SOL_SOCKET; |
288 | cmptr->cmsg_type = SCM_RIGHTS; | |
289 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
290 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
291 | /* Sum of the length of all control messages in the buffer: */ | |
292 | msg.msg_controllen = cmptr->cmsg_len; | |
293 | ||
294 | iov[0].iov_base = &dummy; | |
295 | iov[0].iov_len = 1; | |
296 | msg.msg_iov = iov; | |
297 | msg.msg_iovlen = 1; | |
298 | ||
299 | do { | |
300 | ret = sendmsg(sock, &msg, 0); | |
301 | } while (ret < 0 && errno == EINTR); | |
302 | if (ret < 0) { | |
303 | /* | |
304 | * Only warn about EPIPE when quiet mode is deactivated. | |
305 | * We consider EPIPE as expected. | |
306 | */ | |
307 | if (errno != EPIPE || !lttng_opt_quiet) { | |
308 | PERROR("sendmsg"); | |
309 | } | |
310 | } | |
311 | return ret; | |
312 | } | |
313 | ||
314 | /* | |
315 | * Recv a message accompanied by fd(s) from a unix socket. | |
316 | * | |
317 | * Returns the size of received data, or negative error value. | |
318 | * | |
319 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
320 | * actually received in nb_fd. | |
321 | */ | |
90e535ef | 322 | LTTNG_HIDDEN |
0d37f2bc DG |
323 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
324 | { | |
325 | struct iovec iov[1]; | |
326 | ssize_t ret = 0; | |
327 | struct cmsghdr *cmsg; | |
328 | size_t sizeof_fds = nb_fd * sizeof(int); | |
329 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
330 | struct msghdr msg; | |
331 | char dummy; | |
332 | ||
333 | memset(&msg, 0, sizeof(msg)); | |
334 | ||
335 | /* Prepare to receive the structures */ | |
336 | iov[0].iov_base = &dummy; | |
337 | iov[0].iov_len = 1; | |
338 | msg.msg_iov = iov; | |
339 | msg.msg_iovlen = 1; | |
340 | msg.msg_control = recv_fd; | |
341 | msg.msg_controllen = sizeof(recv_fd); | |
342 | ||
343 | do { | |
344 | ret = recvmsg(sock, &msg, 0); | |
345 | } while (ret < 0 && errno == EINTR); | |
346 | if (ret < 0) { | |
347 | PERROR("recvmsg fds"); | |
348 | goto end; | |
349 | } | |
350 | if (ret != 1) { | |
351 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
352 | ret, 1); | |
353 | goto end; | |
354 | } | |
355 | if (msg.msg_flags & MSG_CTRUNC) { | |
356 | fprintf(stderr, "Error: Control message truncated.\n"); | |
357 | ret = -1; | |
358 | goto end; | |
359 | } | |
360 | cmsg = CMSG_FIRSTHDR(&msg); | |
361 | if (!cmsg) { | |
362 | fprintf(stderr, "Error: Invalid control message header\n"); | |
363 | ret = -1; | |
364 | goto end; | |
365 | } | |
366 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | |
367 | fprintf(stderr, "Didn't received any fd\n"); | |
368 | ret = -1; | |
369 | goto end; | |
370 | } | |
371 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
372 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
373 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); | |
374 | ret = -1; | |
375 | goto end; | |
376 | } | |
377 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
378 | ret = sizeof_fds; | |
379 | end: | |
380 | return ret; | |
381 | } | |
382 | ||
383 | /* | |
384 | * Send a message with credentials over a unix socket. | |
385 | * | |
386 | * Returns the size of data sent, or negative error value. | |
387 | */ | |
90e535ef | 388 | LTTNG_HIDDEN |
0d37f2bc DG |
389 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
390 | { | |
391 | struct msghdr msg; | |
392 | struct iovec iov[1]; | |
393 | ssize_t ret = -1; | |
394 | #ifdef __linux__ | |
395 | struct cmsghdr *cmptr; | |
396 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
397 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
398 | lttng_sock_cred *creds; | |
399 | #endif /* __linux__ */ | |
400 | ||
401 | memset(&msg, 0, sizeof(msg)); | |
c617c0c6 | 402 | memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char)); |
0d37f2bc DG |
403 | |
404 | iov[0].iov_base = buf; | |
405 | iov[0].iov_len = len; | |
406 | msg.msg_iov = iov; | |
407 | msg.msg_iovlen = 1; | |
408 | ||
409 | #ifdef __linux__ | |
410 | msg.msg_control = (caddr_t) anc_buf; | |
411 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
412 | ||
413 | cmptr = CMSG_FIRSTHDR(&msg); | |
1d8d0328 MJ |
414 | if (!cmptr) { |
415 | return -1; | |
416 | } | |
0d37f2bc DG |
417 | cmptr->cmsg_level = SOL_SOCKET; |
418 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; | |
419 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
420 | ||
421 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); | |
422 | ||
423 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); | |
424 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); | |
425 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); | |
426 | #endif /* __linux__ */ | |
427 | ||
428 | do { | |
429 | ret = sendmsg(sock, &msg, 0); | |
430 | } while (ret < 0 && errno == EINTR); | |
431 | if (ret < 0) { | |
432 | /* | |
433 | * Only warn about EPIPE when quiet mode is deactivated. | |
434 | * We consider EPIPE as expected. | |
435 | */ | |
436 | if (errno != EPIPE || !lttng_opt_quiet) { | |
437 | PERROR("sendmsg"); | |
438 | } | |
439 | } | |
440 | return ret; | |
441 | } | |
442 | ||
443 | /* | |
444 | * Recv a message accompanied with credentials from a unix socket. | |
445 | * | |
446 | * Returns the size of received data, or negative error value. | |
447 | */ | |
90e535ef | 448 | LTTNG_HIDDEN |
0d37f2bc DG |
449 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
450 | lttng_sock_cred *creds) | |
451 | { | |
452 | struct msghdr msg; | |
453 | struct iovec iov[1]; | |
454 | ssize_t ret; | |
4100e49a | 455 | size_t len_last; |
0d37f2bc DG |
456 | #ifdef __linux__ |
457 | struct cmsghdr *cmptr; | |
458 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
459 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
460 | #endif /* __linux__ */ | |
461 | ||
462 | memset(&msg, 0, sizeof(msg)); | |
463 | ||
464 | /* Not allowed */ | |
465 | if (creds == NULL) { | |
466 | ret = -1; | |
467 | goto end; | |
468 | } | |
469 | ||
470 | /* Prepare to receive the structures */ | |
471 | iov[0].iov_base = buf; | |
472 | iov[0].iov_len = len; | |
473 | msg.msg_iov = iov; | |
474 | msg.msg_iovlen = 1; | |
475 | ||
476 | #ifdef __linux__ | |
477 | msg.msg_control = anc_buf; | |
478 | msg.msg_controllen = sizeof(anc_buf); | |
479 | #endif /* __linux__ */ | |
480 | ||
481 | do { | |
4100e49a | 482 | len_last = iov[0].iov_len; |
0d37f2bc | 483 | ret = recvmsg(sock, &msg, 0); |
4100e49a DG |
484 | if (ret > 0) { |
485 | iov[0].iov_base += ret; | |
486 | iov[0].iov_len -= ret; | |
487 | assert(ret <= len_last); | |
488 | } | |
489 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
490 | if (ret < 0) { |
491 | PERROR("recvmsg fds"); | |
492 | goto end; | |
4100e49a DG |
493 | } else if (ret > 0) { |
494 | ret = len; | |
0d37f2bc | 495 | } |
4100e49a | 496 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
497 | |
498 | #ifdef __linux__ | |
499 | if (msg.msg_flags & MSG_CTRUNC) { | |
500 | fprintf(stderr, "Error: Control message truncated.\n"); | |
501 | ret = -1; | |
502 | goto end; | |
503 | } | |
504 | ||
505 | cmptr = CMSG_FIRSTHDR(&msg); | |
506 | if (cmptr == NULL) { | |
507 | fprintf(stderr, "Error: Invalid control message header\n"); | |
508 | ret = -1; | |
509 | goto end; | |
510 | } | |
511 | ||
512 | if (cmptr->cmsg_level != SOL_SOCKET || | |
513 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { | |
514 | fprintf(stderr, "Didn't received any credentials\n"); | |
515 | ret = -1; | |
516 | goto end; | |
517 | } | |
518 | ||
519 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
520 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
521 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); | |
522 | ret = -1; | |
523 | goto end; | |
524 | } | |
525 | ||
526 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
527 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) | |
528 | { | |
529 | int peer_ret; | |
530 | ||
531 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); | |
532 | if (peer_ret != 0) { | |
533 | return peer_ret; | |
534 | } | |
535 | } | |
536 | #else | |
537 | #error "Please implement credential support for your OS." | |
538 | #endif /* __linux__ */ | |
539 | ||
540 | end: | |
541 | return ret; | |
542 | } | |
543 | ||
544 | /* | |
545 | * Set socket option to use credentials passing. | |
546 | */ | |
547 | #ifdef __linux__ | |
90e535ef | 548 | LTTNG_HIDDEN |
0d37f2bc DG |
549 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
550 | { | |
551 | int ret, on = 1; | |
552 | ||
553 | /* Set socket for credentials retrieval */ | |
554 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
555 | if (ret < 0) { | |
556 | PERROR("setsockopt creds unix sock"); | |
557 | } | |
558 | return ret; | |
559 | } | |
560 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) | |
90e535ef | 561 | LTTNG_HIDDEN |
0d37f2bc DG |
562 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
563 | { | |
564 | return 0; | |
565 | } | |
566 | #else | |
567 | #error "Please implement credential support for your OS." | |
568 | #endif /* __linux__ */ |