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