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