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 | ||
0e904f3e 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; |
b51ae080 | 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); | |
284 | cmptr->cmsg_level = SOL_SOCKET; | |
285 | cmptr->cmsg_type = SCM_RIGHTS; | |
286 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); | |
287 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); | |
288 | /* Sum of the length of all control messages in the buffer: */ | |
289 | msg.msg_controllen = cmptr->cmsg_len; | |
290 | ||
291 | iov[0].iov_base = &dummy; | |
292 | iov[0].iov_len = 1; | |
293 | msg.msg_iov = iov; | |
294 | msg.msg_iovlen = 1; | |
295 | ||
296 | do { | |
297 | ret = sendmsg(sock, &msg, 0); | |
298 | } while (ret < 0 && errno == EINTR); | |
299 | if (ret < 0) { | |
300 | /* | |
301 | * Only warn about EPIPE when quiet mode is deactivated. | |
302 | * We consider EPIPE as expected. | |
303 | */ | |
304 | if (errno != EPIPE || !lttng_opt_quiet) { | |
305 | PERROR("sendmsg"); | |
306 | } | |
307 | } | |
308 | return ret; | |
309 | } | |
310 | ||
311 | /* | |
312 | * Recv a message accompanied by fd(s) from a unix socket. | |
313 | * | |
314 | * Returns the size of received data, or negative error value. | |
315 | * | |
316 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
317 | * actually received in nb_fd. | |
318 | */ | |
90e535ef | 319 | LTTNG_HIDDEN |
0d37f2bc DG |
320 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
321 | { | |
322 | struct iovec iov[1]; | |
323 | ssize_t ret = 0; | |
324 | struct cmsghdr *cmsg; | |
325 | size_t sizeof_fds = nb_fd * sizeof(int); | |
326 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
327 | struct msghdr msg; | |
328 | char dummy; | |
329 | ||
330 | memset(&msg, 0, sizeof(msg)); | |
331 | ||
332 | /* Prepare to receive the structures */ | |
333 | iov[0].iov_base = &dummy; | |
334 | iov[0].iov_len = 1; | |
335 | msg.msg_iov = iov; | |
336 | msg.msg_iovlen = 1; | |
337 | msg.msg_control = recv_fd; | |
338 | msg.msg_controllen = sizeof(recv_fd); | |
339 | ||
340 | do { | |
341 | ret = recvmsg(sock, &msg, 0); | |
342 | } while (ret < 0 && errno == EINTR); | |
343 | if (ret < 0) { | |
344 | PERROR("recvmsg fds"); | |
345 | goto end; | |
346 | } | |
347 | if (ret != 1) { | |
348 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
349 | ret, 1); | |
350 | goto end; | |
351 | } | |
352 | if (msg.msg_flags & MSG_CTRUNC) { | |
353 | fprintf(stderr, "Error: Control message truncated.\n"); | |
354 | ret = -1; | |
355 | goto end; | |
356 | } | |
357 | cmsg = CMSG_FIRSTHDR(&msg); | |
358 | if (!cmsg) { | |
359 | fprintf(stderr, "Error: Invalid control message header\n"); | |
360 | ret = -1; | |
361 | goto end; | |
362 | } | |
363 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | |
364 | fprintf(stderr, "Didn't received any fd\n"); | |
365 | ret = -1; | |
366 | goto end; | |
367 | } | |
368 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { | |
369 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
370 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); | |
371 | ret = -1; | |
372 | goto end; | |
373 | } | |
374 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); | |
375 | ret = sizeof_fds; | |
376 | end: | |
377 | return ret; | |
378 | } | |
379 | ||
380 | /* | |
381 | * Send a message with credentials over a unix socket. | |
382 | * | |
383 | * Returns the size of data sent, or negative error value. | |
384 | */ | |
90e535ef | 385 | LTTNG_HIDDEN |
0d37f2bc DG |
386 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) |
387 | { | |
388 | struct msghdr msg; | |
389 | struct iovec iov[1]; | |
390 | ssize_t ret = -1; | |
391 | #ifdef __linux__ | |
392 | struct cmsghdr *cmptr; | |
393 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
394 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
395 | lttng_sock_cred *creds; | |
396 | #endif /* __linux__ */ | |
397 | ||
398 | memset(&msg, 0, sizeof(msg)); | |
c617c0c6 | 399 | memset(anc_buf, 0, CMSG_SPACE(sizeof_cred) * sizeof(char)); |
0d37f2bc DG |
400 | |
401 | iov[0].iov_base = buf; | |
402 | iov[0].iov_len = len; | |
403 | msg.msg_iov = iov; | |
404 | msg.msg_iovlen = 1; | |
405 | ||
406 | #ifdef __linux__ | |
407 | msg.msg_control = (caddr_t) anc_buf; | |
408 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
409 | ||
410 | cmptr = CMSG_FIRSTHDR(&msg); | |
411 | cmptr->cmsg_level = SOL_SOCKET; | |
412 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; | |
413 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
414 | ||
415 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); | |
416 | ||
417 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); | |
418 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); | |
419 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); | |
420 | #endif /* __linux__ */ | |
421 | ||
422 | do { | |
423 | ret = sendmsg(sock, &msg, 0); | |
424 | } while (ret < 0 && errno == EINTR); | |
425 | if (ret < 0) { | |
426 | /* | |
427 | * Only warn about EPIPE when quiet mode is deactivated. | |
428 | * We consider EPIPE as expected. | |
429 | */ | |
430 | if (errno != EPIPE || !lttng_opt_quiet) { | |
431 | PERROR("sendmsg"); | |
432 | } | |
433 | } | |
434 | return ret; | |
435 | } | |
436 | ||
437 | /* | |
438 | * Recv a message accompanied with credentials from a unix socket. | |
439 | * | |
440 | * Returns the size of received data, or negative error value. | |
441 | */ | |
90e535ef | 442 | LTTNG_HIDDEN |
0d37f2bc DG |
443 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, |
444 | lttng_sock_cred *creds) | |
445 | { | |
446 | struct msghdr msg; | |
447 | struct iovec iov[1]; | |
448 | ssize_t ret; | |
4100e49a | 449 | size_t len_last; |
0d37f2bc DG |
450 | #ifdef __linux__ |
451 | struct cmsghdr *cmptr; | |
452 | size_t sizeof_cred = sizeof(lttng_sock_cred); | |
453 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
454 | #endif /* __linux__ */ | |
455 | ||
456 | memset(&msg, 0, sizeof(msg)); | |
457 | ||
458 | /* Not allowed */ | |
459 | if (creds == NULL) { | |
460 | ret = -1; | |
461 | goto end; | |
462 | } | |
463 | ||
464 | /* Prepare to receive the structures */ | |
465 | iov[0].iov_base = buf; | |
466 | iov[0].iov_len = len; | |
467 | msg.msg_iov = iov; | |
468 | msg.msg_iovlen = 1; | |
469 | ||
470 | #ifdef __linux__ | |
471 | msg.msg_control = anc_buf; | |
472 | msg.msg_controllen = sizeof(anc_buf); | |
473 | #endif /* __linux__ */ | |
474 | ||
475 | do { | |
4100e49a | 476 | len_last = iov[0].iov_len; |
0d37f2bc | 477 | ret = recvmsg(sock, &msg, 0); |
4100e49a DG |
478 | if (ret > 0) { |
479 | iov[0].iov_base += ret; | |
480 | iov[0].iov_len -= ret; | |
481 | assert(ret <= len_last); | |
482 | } | |
483 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
0d37f2bc DG |
484 | if (ret < 0) { |
485 | PERROR("recvmsg fds"); | |
486 | goto end; | |
4100e49a DG |
487 | } else if (ret > 0) { |
488 | ret = len; | |
0d37f2bc | 489 | } |
4100e49a | 490 | /* Else ret = 0 meaning an orderly shutdown. */ |
0d37f2bc DG |
491 | |
492 | #ifdef __linux__ | |
493 | if (msg.msg_flags & MSG_CTRUNC) { | |
494 | fprintf(stderr, "Error: Control message truncated.\n"); | |
495 | ret = -1; | |
496 | goto end; | |
497 | } | |
498 | ||
499 | cmptr = CMSG_FIRSTHDR(&msg); | |
500 | if (cmptr == NULL) { | |
501 | fprintf(stderr, "Error: Invalid control message header\n"); | |
502 | ret = -1; | |
503 | goto end; | |
504 | } | |
505 | ||
506 | if (cmptr->cmsg_level != SOL_SOCKET || | |
507 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { | |
508 | fprintf(stderr, "Didn't received any credentials\n"); | |
509 | ret = -1; | |
510 | goto end; | |
511 | } | |
512 | ||
513 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
514 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
515 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); | |
516 | ret = -1; | |
517 | goto end; | |
518 | } | |
519 | ||
520 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
521 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) | |
522 | { | |
523 | int peer_ret; | |
524 | ||
525 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); | |
526 | if (peer_ret != 0) { | |
527 | return peer_ret; | |
528 | } | |
529 | } | |
530 | #else | |
531 | #error "Please implement credential support for your OS." | |
532 | #endif /* __linux__ */ | |
533 | ||
534 | end: | |
535 | return ret; | |
536 | } | |
537 | ||
538 | /* | |
539 | * Set socket option to use credentials passing. | |
540 | */ | |
541 | #ifdef __linux__ | |
90e535ef | 542 | LTTNG_HIDDEN |
0d37f2bc DG |
543 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
544 | { | |
545 | int ret, on = 1; | |
546 | ||
547 | /* Set socket for credentials retrieval */ | |
548 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
549 | if (ret < 0) { | |
550 | PERROR("setsockopt creds unix sock"); | |
551 | } | |
552 | return ret; | |
553 | } | |
554 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) | |
90e535ef | 555 | LTTNG_HIDDEN |
0d37f2bc DG |
556 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
557 | { | |
558 | return 0; | |
559 | } | |
560 | #else | |
561 | #error "Please implement credential support for your OS." | |
562 | #endif /* __linux__ */ |