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