Fix: clear the fixme in high_throughput_limits
[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/defaults.h>
31 #include <common/error.h>
32
33 #include "unix.h"
34
35 /*
36 * Connect to unix socket using the path name.
37 */
38 __attribute__((visibility("hidden")))
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 */
80 __attribute__((visibility("hidden")))
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
96 /*
97 * Creates a AF_UNIX local socket using pathname bind the socket upon creation
98 * and return the fd.
99 */
100 __attribute__((visibility("hidden")))
101 int lttcomm_create_unix_sock(const char *pathname)
102 {
103 struct sockaddr_un sun;
104 int fd;
105 int ret = -1;
106
107 /* Create server socket */
108 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
109 PERROR("socket");
110 goto error;
111 }
112
113 memset(&sun, 0, sizeof(sun));
114 sun.sun_family = AF_UNIX;
115 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
116 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
117
118 /* Unlink the old file if present */
119 (void) unlink(pathname);
120 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
121 if (ret < 0) {
122 PERROR("bind");
123 goto error;
124 }
125
126 return fd;
127
128 error:
129 return ret;
130 }
131
132 /*
133 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
134 */
135 __attribute__((visibility("hidden")))
136 int lttcomm_listen_unix_sock(int sock)
137 {
138 int ret;
139
140 ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN);
141 if (ret < 0) {
142 PERROR("listen");
143 }
144
145 return ret;
146 }
147
148 /*
149 * Receive data of size len in put that data into the buf param. Using recvmsg
150 * API.
151 *
152 * Return the size of received data.
153 */
154 __attribute__((visibility("hidden")))
155 ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
156 {
157 struct msghdr msg;
158 struct iovec iov[1];
159 ssize_t ret = -1;
160 size_t len_last;
161
162 memset(&msg, 0, sizeof(msg));
163
164 iov[0].iov_base = buf;
165 iov[0].iov_len = len;
166 msg.msg_iov = iov;
167 msg.msg_iovlen = 1;
168
169 do {
170 len_last = iov[0].iov_len;
171 ret = recvmsg(sock, &msg, 0);
172 if (ret > 0) {
173 iov[0].iov_base += ret;
174 iov[0].iov_len -= ret;
175 assert(ret <= len_last);
176 }
177 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
178 if (ret < 0) {
179 PERROR("recvmsg");
180 } else if (ret > 0) {
181 ret = len;
182 }
183 /* Else ret = 0 meaning an orderly shutdown. */
184
185 return ret;
186 }
187
188 /*
189 * Send buf data of size len. Using sendmsg API.
190 *
191 * Return the size of sent data.
192 */
193 __attribute__((visibility("hidden")))
194 ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
195 {
196 struct msghdr msg;
197 struct iovec iov[1];
198 ssize_t ret = -1;
199
200 memset(&msg, 0, sizeof(msg));
201
202 iov[0].iov_base = buf;
203 iov[0].iov_len = len;
204 msg.msg_iov = iov;
205 msg.msg_iovlen = 1;
206
207 ret = sendmsg(sock, &msg, 0);
208 if (ret < 0) {
209 /*
210 * Only warn about EPIPE when quiet mode is deactivated.
211 * We consider EPIPE as expected.
212 */
213 if (errno != EPIPE || !lttng_opt_quiet) {
214 PERROR("sendmsg");
215 }
216 }
217
218 return ret;
219 }
220
221 /*
222 * Shutdown cleanly a unix socket.
223 */
224 __attribute__((visibility("hidden")))
225 int lttcomm_close_unix_sock(int sock)
226 {
227 int ret, closeret;
228
229 /* Shutdown receptions and transmissions */
230 ret = shutdown(sock, SHUT_RDWR);
231 if (ret < 0) {
232 PERROR("shutdown");
233 }
234
235 closeret = close(sock);
236 if (closeret) {
237 PERROR("close");
238 }
239
240 return ret;
241 }
242
243 /*
244 * Send a message accompanied by fd(s) over a unix socket.
245 *
246 * Returns the size of data sent, or negative error value.
247 */
248 __attribute__((visibility("hidden")))
249 ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
250 {
251 struct msghdr msg;
252 struct cmsghdr *cmptr;
253 struct iovec iov[1];
254 ssize_t ret = -1;
255 unsigned int sizeof_fds = nb_fd * sizeof(int);
256 char tmp[CMSG_SPACE(sizeof_fds)];
257 char dummy = 0;
258
259 memset(&msg, 0, sizeof(msg));
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 __attribute__((visibility("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 __attribute__((visibility("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
384 iov[0].iov_base = buf;
385 iov[0].iov_len = len;
386 msg.msg_iov = iov;
387 msg.msg_iovlen = 1;
388
389 #ifdef __linux__
390 msg.msg_control = (caddr_t) anc_buf;
391 msg.msg_controllen = CMSG_LEN(sizeof_cred);
392
393 cmptr = CMSG_FIRSTHDR(&msg);
394 cmptr->cmsg_level = SOL_SOCKET;
395 cmptr->cmsg_type = LTTNG_SOCK_CREDS;
396 cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
397
398 creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
399
400 LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
401 LTTNG_SOCK_SET_GID_CRED(creds, getegid());
402 LTTNG_SOCK_SET_PID_CRED(creds, getpid());
403 #endif /* __linux__ */
404
405 do {
406 ret = sendmsg(sock, &msg, 0);
407 } while (ret < 0 && errno == EINTR);
408 if (ret < 0) {
409 /*
410 * Only warn about EPIPE when quiet mode is deactivated.
411 * We consider EPIPE as expected.
412 */
413 if (errno != EPIPE || !lttng_opt_quiet) {
414 PERROR("sendmsg");
415 }
416 }
417 return ret;
418 }
419
420 /*
421 * Recv a message accompanied with credentials from a unix socket.
422 *
423 * Returns the size of received data, or negative error value.
424 */
425 __attribute__((visibility("hidden")))
426 ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
427 lttng_sock_cred *creds)
428 {
429 struct msghdr msg;
430 struct iovec iov[1];
431 ssize_t ret;
432 #ifdef __linux__
433 struct cmsghdr *cmptr;
434 size_t sizeof_cred = sizeof(lttng_sock_cred);
435 char anc_buf[CMSG_SPACE(sizeof_cred)];
436 #endif /* __linux__ */
437
438 memset(&msg, 0, sizeof(msg));
439
440 /* Not allowed */
441 if (creds == NULL) {
442 ret = -1;
443 goto end;
444 }
445
446 /* Prepare to receive the structures */
447 iov[0].iov_base = buf;
448 iov[0].iov_len = len;
449 msg.msg_iov = iov;
450 msg.msg_iovlen = 1;
451
452 #ifdef __linux__
453 msg.msg_control = anc_buf;
454 msg.msg_controllen = sizeof(anc_buf);
455 #endif /* __linux__ */
456
457 do {
458 ret = recvmsg(sock, &msg, 0);
459 } while (ret < 0 && errno == EINTR);
460 if (ret < 0) {
461 PERROR("recvmsg fds");
462 goto end;
463 }
464
465 #ifdef __linux__
466 if (msg.msg_flags & MSG_CTRUNC) {
467 fprintf(stderr, "Error: Control message truncated.\n");
468 ret = -1;
469 goto end;
470 }
471
472 cmptr = CMSG_FIRSTHDR(&msg);
473 if (cmptr == NULL) {
474 fprintf(stderr, "Error: Invalid control message header\n");
475 ret = -1;
476 goto end;
477 }
478
479 if (cmptr->cmsg_level != SOL_SOCKET ||
480 cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
481 fprintf(stderr, "Didn't received any credentials\n");
482 ret = -1;
483 goto end;
484 }
485
486 if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
487 fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
488 (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
489 ret = -1;
490 goto end;
491 }
492
493 memcpy(creds, CMSG_DATA(cmptr), sizeof_cred);
494 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
495 {
496 int peer_ret;
497
498 peer_ret = getpeereid(sock, &creds->uid, &creds->gid);
499 if (peer_ret != 0) {
500 return peer_ret;
501 }
502 }
503 #else
504 #error "Please implement credential support for your OS."
505 #endif /* __linux__ */
506
507 end:
508 return ret;
509 }
510
511 /*
512 * Set socket option to use credentials passing.
513 */
514 #ifdef __linux__
515 __attribute__((visibility("hidden")))
516 int lttcomm_setsockopt_creds_unix_sock(int sock)
517 {
518 int ret, on = 1;
519
520 /* Set socket for credentials retrieval */
521 ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
522 if (ret < 0) {
523 PERROR("setsockopt creds unix sock");
524 }
525 return ret;
526 }
527 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
528 __attribute__((visibility("hidden")))
529 int lttcomm_setsockopt_creds_unix_sock(int sock)
530 {
531 return 0;
532 }
533 #else
534 #error "Please implement credential support for your OS."
535 #endif /* __linux__ */
536
537 /*
538 * Set socket reciving timeout.
539 */
540 __attribute__((visibility("hidden")))
541 int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int sec)
542 {
543 int ret;
544 struct timeval tv;
545
546 tv.tv_sec = sec;
547 tv.tv_usec = 0;
548
549 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
550 if (ret < 0) {
551 PERROR("setsockopt SO_RCVTIMEO");
552 ret = -errno;
553 }
554
555 return ret;
556 }
557
558 /*
559 * Set socket sending timeout.
560 */
561 __attribute__((visibility("hidden")))
562 int lttcomm_setsockopt_snd_timeout(int sock, unsigned int sec)
563 {
564 int ret;
565 struct timeval tv;
566
567 tv.tv_sec = sec;
568 tv.tv_usec = 0;
569
570 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
571 if (ret < 0) {
572 PERROR("setsockopt SO_SNDTIMEO");
573 ret = -errno;
574 }
575
576 return ret;
577 }
This page took 0.040827 seconds and 4 git commands to generate.