Prepare for '-Wignored-qualifiers'
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.cpp
CommitLineData
826d496d 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
fac6795d 6 *
fac6795d
DG
7 */
8
4222116f 9#include <sys/socket.h>
6c1c0768 10#define _LGPL_SOURCE
fac6795d
DG
11#include <limits.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
fac6795d
DG
15#include <sys/stat.h>
16#include <sys/types.h>
fac6795d 17#include <unistd.h>
2288467f 18#include <inttypes.h>
fac6795d 19
90e535ef 20#include <common/common.h>
edf4b93e 21#include <common/compat/errno.h>
990570ed 22
10a8a223 23#include "sessiond-comm.h"
fac6795d 24
6364a07a 25/* For Unix socket */
2038dd6c 26#include <common/unix.h>
6364a07a
DG
27/* For Inet socket */
28#include "inet.h"
29/* For Inet6 socket */
30#include "inet6.h"
31
554831e7
MD
32#define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT"
33
32dd26fb 34static struct lttcomm_net_family net_families[] = {
6364a07a
DG
35 { LTTCOMM_INET, lttcomm_create_inet_sock },
36 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
37};
38
fac6795d
DG
39/*
40 * Human readable error message.
41 */
64efa44e
SM
42static
43const char *lttcomm_return_code_str(lttcomm_return_code code) {
44 switch (code) {
45 case LTTCOMM_CONSUMERD_SUCCESS:
46 return "consumerd success";
47 case LTTCOMM_CONSUMERD_COMMAND_SOCK_READY:
48 return "consumerd command socket ready";
49 case LTTCOMM_CONSUMERD_SUCCESS_RECV_FD:
50 return "consumerd success on receiving fds";
51 case LTTCOMM_CONSUMERD_ERROR_RECV_FD:
52 return "consumerd error on receiving fds";
53 case LTTCOMM_CONSUMERD_ERROR_RECV_CMD:
54 return "consumerd error on receiving command";
55 case LTTCOMM_CONSUMERD_POLL_ERROR:
56 return "consumerd error in polling thread";
57 case LTTCOMM_CONSUMERD_POLL_NVAL:
58 return "consumerd polling on closed fd";
59 case LTTCOMM_CONSUMERD_POLL_HUP:
60 return "consumerd all fd hung up";
61 case LTTCOMM_CONSUMERD_EXIT_SUCCESS:
62 return "consumerd exiting normally";
63 case LTTCOMM_CONSUMERD_EXIT_FAILURE:
64 return "consumerd exiting on error";
65 case LTTCOMM_CONSUMERD_OUTFD_ERROR:
66 return "consumerd error opening the tracefile";
67 case LTTCOMM_CONSUMERD_SPLICE_EBADF:
68 return "consumerd splice EBADF";
69 case LTTCOMM_CONSUMERD_SPLICE_EINVAL:
70 return "consumerd splice EINVAL";
71 case LTTCOMM_CONSUMERD_SPLICE_ENOMEM:
72 return "consumerd splice ENOMEM";
73 case LTTCOMM_CONSUMERD_SPLICE_ESPIPE:
74 return "consumerd splice ESPIPE";
75 case LTTCOMM_CONSUMERD_ENOMEM:
76 return "consumerd is out of memory";
77 case LTTCOMM_CONSUMERD_ERROR_METADATA:
78 return "consumerd error with metadata";
79 case LTTCOMM_CONSUMERD_FATAL:
80 return "consumerd fatal error";
81 case LTTCOMM_CONSUMERD_RELAYD_FAIL:
82 return "consumerd error on remote relayd";
83 case LTTCOMM_CONSUMERD_CHANNEL_FAIL:
84 return "consumerd channel creation fail";
85 case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND:
86 return "consumerd channel not found";
87 case LTTCOMM_CONSUMERD_ALREADY_SET:
88 return "consumerd resource already set";
89 case LTTCOMM_CONSUMERD_ROTATION_FAIL:
90 return "consumerd rotation failed";
91 case LTTCOMM_CONSUMERD_SNAPSHOT_FAILED:
92 return "consumerd snapshot has failed";
93 case LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED:
94 return "consumerd trace chunk creation failed";
95 case LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED:
96 return "consumerd trace chunk closing failed";
97 case LTTCOMM_CONSUMERD_INVALID_PARAMETERS:
98 return "consumerd invalid parameters";
99 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL:
100 return "consumerd trace chunk exists on consumer daemon";
101 case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE:
102 return "consumedd trace chunk exists on relay daemon";
103 case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK:
104 return "consumerd unknown trace chunk";
105 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
106 return "consumed relayd does not accept clear command";
107 case LTTCOMM_CONSUMERD_UNKNOWN_ERROR:
108 return "consumerd unknown error";
109 case LTTCOMM_NR:
110 abort();
111 }
112
113 abort();
fac6795d
DG
114};
115
554831e7
MD
116static unsigned long network_timeout;
117
fac6795d 118/*
917216f6
DG
119 * Return ptr to string representing a human readable error code from the
120 * lttcomm_return_code enum.
fac6795d 121 *
917216f6 122 * These code MUST be negative in other to treat that as an error value.
fac6795d
DG
123 */
124const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
125{
64efa44e 126 code = (lttcomm_return_code) -code;
fac6795d 127
64efa44e
SM
128 if (code != LTTCOMM_CONSUMERD_SUCCESS &&
129 !(code >= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code < LTTCOMM_NR)) {
130 code = LTTCOMM_CONSUMERD_UNKNOWN_ERROR;
fac6795d
DG
131 }
132
64efa44e 133 return lttcomm_return_code_str(code);
fac6795d 134}
6364a07a
DG
135
136/*
de5e9086
DG
137 * Create socket from an already allocated lttcomm socket structure and init
138 * sockaddr in the lttcomm sock.
6364a07a 139 */
de5e9086 140int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 141{
de5e9086 142 int ret, _sock_type, _sock_proto, domain;
6364a07a 143
a0377dfe 144 LTTNG_ASSERT(sock);
de5e9086
DG
145
146 domain = sock->sockaddr.type;
147 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
148 ERR("Create socket of unknown domain %d", domain);
149 ret = -1;
150 goto error;
6364a07a
DG
151 }
152
de5e9086
DG
153 switch (sock->proto) {
154 case LTTCOMM_SOCK_UDP:
155 _sock_type = SOCK_DGRAM;
156 _sock_proto = IPPROTO_UDP;
157 break;
158 case LTTCOMM_SOCK_TCP:
159 _sock_type = SOCK_STREAM;
160 _sock_proto = IPPROTO_TCP;
161 break;
162 default:
163 ret = -1;
164 goto error;
165 }
6364a07a 166
de5e9086
DG
167 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
168 if (ret < 0) {
169 goto error;
170 }
171
172error:
173 return ret;
6364a07a
DG
174}
175
176/*
de5e9086 177 * Return allocated lttcomm socket structure.
6364a07a 178 */
de5e9086 179struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 180{
de5e9086 181 struct lttcomm_sock *sock;
6364a07a 182
64efa44e 183 sock = (lttcomm_sock *) zmalloc(sizeof(lttcomm_sock));
de5e9086
DG
184 if (sock == NULL) {
185 PERROR("zmalloc create sock");
186 goto end;
6364a07a
DG
187 }
188
189 sock->proto = proto;
de5e9086 190 sock->fd = -1;
6364a07a 191
de5e9086
DG
192end:
193 return sock;
6364a07a
DG
194}
195
196/*
de5e9086
DG
197 * Return an allocated lttcomm socket structure and copy src content into
198 * the newly created socket.
199 *
200 * This is mostly useful when lttcomm_sock are passed between process where the
201 * fd and ops have to be changed within the correct address space.
6364a07a 202 */
de5e9086 203struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 204{
6364a07a
DG
205 struct lttcomm_sock *sock;
206
de5e9086 207 /* Safety net */
a0377dfe 208 LTTNG_ASSERT(src);
de5e9086
DG
209
210 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
211 if (sock == NULL) {
212 goto alloc_error;
213 }
214
de5e9086 215 lttcomm_copy_sock(sock, src);
6364a07a 216
de5e9086 217alloc_error:
6364a07a 218 return sock;
de5e9086 219}
6364a07a 220
de5e9086
DG
221/*
222 * Create and copy socket from an allocated lttcomm socket structure.
223 *
224 * This is mostly useful when lttcomm_sock are passed between process where the
225 * fd and ops have to be changed within the correct address space.
226 */
227void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
228{
229 /* Safety net */
a0377dfe
FD
230 LTTNG_ASSERT(dst);
231 LTTNG_ASSERT(src);
de5e9086
DG
232
233 dst->proto = src->proto;
234 dst->fd = src->fd;
235 dst->ops = src->ops;
236 /* Copy sockaddr information from original socket */
237 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
6364a07a
DG
238}
239
240/*
241 * Init IPv4 sockaddr structure.
242 */
243int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
244 const char *ip, unsigned int port)
245{
246 int ret;
247
a0377dfe
FD
248 LTTNG_ASSERT(sockaddr);
249 LTTNG_ASSERT(ip);
250 LTTNG_ASSERT(port > 0 && port <= 65535);
6364a07a
DG
251
252 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
253
254 sockaddr->type = LTTCOMM_INET;
255 sockaddr->addr.sin.sin_family = AF_INET;
256 sockaddr->addr.sin.sin_port = htons(port);
257 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
258 &sockaddr->addr.sin.sin_addr);
259 if (ret < 1) {
260 ret = -1;
de5e9086 261 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
262 goto error;
263 }
264 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
265
266error:
267 return ret;
268}
269
270/*
271 * Init IPv6 sockaddr structure.
272 */
273int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
274 const char *ip, unsigned int port)
275{
276 int ret;
277
a0377dfe
FD
278 LTTNG_ASSERT(sockaddr);
279 LTTNG_ASSERT(ip);
280 LTTNG_ASSERT(port > 0 && port <= 65535);
6364a07a
DG
281
282 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
283
284 sockaddr->type = LTTCOMM_INET6;
285 sockaddr->addr.sin6.sin6_family = AF_INET6;
286 sockaddr->addr.sin6.sin6_port = htons(port);
287 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
288 &sockaddr->addr.sin6.sin6_addr);
289 if (ret < 1) {
290 ret = -1;
291 goto error;
292 }
293
294error:
295 return ret;
296}
de5e9086
DG
297
298/*
299 * Return allocated lttcomm socket structure from lttng URI.
300 */
301struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
302{
303 int ret;
304 int _sock_proto;
305 struct lttcomm_sock *sock = NULL;
306
307 /* Safety net */
a0377dfe 308 LTTNG_ASSERT(uri);
de5e9086
DG
309
310 /* Check URI protocol */
311 if (uri->proto == LTTNG_TCP) {
312 _sock_proto = LTTCOMM_SOCK_TCP;
313 } else {
314 ERR("Relayd invalid URI proto: %d", uri->proto);
315 goto alloc_error;
316 }
317
64efa44e 318 sock = lttcomm_alloc_sock((lttcomm_sock_proto) _sock_proto);
de5e9086
DG
319 if (sock == NULL) {
320 goto alloc_error;
321 }
322
323 /* Check destination type */
324 if (uri->dtype == LTTNG_DST_IPV4) {
325 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
326 uri->port);
327 if (ret < 0) {
328 goto error;
329 }
330 } else if (uri->dtype == LTTNG_DST_IPV6) {
331 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
332 uri->port);
333 if (ret < 0) {
334 goto error;
335 }
336 } else {
337 /* Command URI is invalid */
338 ERR("Relayd invalid URI dst type: %d", uri->dtype);
339 goto error;
340 }
341
342 return sock;
343
344error:
345 lttcomm_destroy_sock(sock);
346alloc_error:
347 return NULL;
348}
349
350/*
351 * Destroy and free lttcomm socket.
352 */
353void lttcomm_destroy_sock(struct lttcomm_sock *sock)
354{
0e428499 355 free(sock);
de5e9086 356}
6151a90f
JD
357
358/*
359 * Allocate and return a relayd socket object using a given URI to initialize
360 * it and the major/minor version of the supported protocol.
361 *
362 * On error, NULL is returned.
363 */
364struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri,
365 uint32_t major, uint32_t minor)
366{
367 int ret;
368 struct lttcomm_sock *tmp_sock = NULL;
369 struct lttcomm_relayd_sock *rsock = NULL;
370
a0377dfe 371 LTTNG_ASSERT(uri);
6151a90f 372
64efa44e 373 rsock = (lttcomm_relayd_sock *) zmalloc(sizeof(*rsock));
6151a90f
JD
374 if (!rsock) {
375 PERROR("zmalloc relayd sock");
376 goto error;
377 }
378
379 /* Allocate socket object from URI */
380 tmp_sock = lttcomm_alloc_sock_from_uri(uri);
381 if (tmp_sock == NULL) {
382 goto error_free;
383 }
384
385 /*
386 * Create socket object which basically sets the ops according to the
387 * socket protocol.
388 */
389 lttcomm_copy_sock(&rsock->sock, tmp_sock);
390 /* Temporary socket pointer not needed anymore. */
391 lttcomm_destroy_sock(tmp_sock);
392 ret = lttcomm_create_sock(&rsock->sock);
393 if (ret < 0) {
394 goto error_free;
395 }
396
397 rsock->major = major;
398 rsock->minor = minor;
399
400 return rsock;
401
402error_free:
403 free(rsock);
404error:
405 return NULL;
406}
554831e7 407
783a3b9a
MD
408/*
409 * Set socket receiving timeout.
410 */
783a3b9a
MD
411int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
412{
413 int ret;
414 struct timeval tv;
415
416 tv.tv_sec = msec / 1000;
417 tv.tv_usec = (msec % 1000) * 1000;
418
419 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
420 if (ret < 0) {
421 PERROR("setsockopt SO_RCVTIMEO");
422 }
423
424 return ret;
425}
426
427/*
428 * Set socket sending timeout.
429 */
783a3b9a
MD
430int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
431{
432 int ret;
433 struct timeval tv;
434
435 tv.tv_sec = msec / 1000;
436 tv.tv_usec = (msec % 1000) * 1000;
437
438 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
439 if (ret < 0) {
440 PERROR("setsockopt SO_SNDTIMEO");
441 }
442
443 return ret;
444}
445
2288467f
JG
446int lttcomm_sock_get_port(const struct lttcomm_sock *sock, uint16_t *port)
447{
a0377dfe
FD
448 LTTNG_ASSERT(sock);
449 LTTNG_ASSERT(port);
450 LTTNG_ASSERT(sock->sockaddr.type == LTTCOMM_INET ||
2288467f 451 sock->sockaddr.type == LTTCOMM_INET6);
a0377dfe 452 LTTNG_ASSERT(sock->proto == LTTCOMM_SOCK_TCP ||
2288467f
JG
453 sock->proto == LTTCOMM_SOCK_UDP);
454
455 switch (sock->sockaddr.type) {
456 case LTTCOMM_INET:
457 *port = ntohs(sock->sockaddr.addr.sin.sin_port);
458 break;
459 case LTTCOMM_INET6:
460 *port = ntohs(sock->sockaddr.addr.sin6.sin6_port);
461 break;
462 default:
463 abort();
464 }
465
466 return 0;
467}
468
2288467f
JG
469int lttcomm_sock_set_port(struct lttcomm_sock *sock, uint16_t port)
470{
a0377dfe
FD
471 LTTNG_ASSERT(sock);
472 LTTNG_ASSERT(sock->sockaddr.type == LTTCOMM_INET ||
2288467f 473 sock->sockaddr.type == LTTCOMM_INET6);
a0377dfe 474 LTTNG_ASSERT(sock->proto == LTTCOMM_SOCK_TCP ||
2288467f
JG
475 sock->proto == LTTCOMM_SOCK_UDP);
476
477 switch (sock->sockaddr.type) {
478 case LTTCOMM_INET:
479 sock->sockaddr.addr.sin.sin_port = htons(port);
480 break;
481 case LTTCOMM_INET6:
482 sock->sockaddr.addr.sin6.sin6_port = htons(port);
483 break;
484 default:
485 abort();
486 }
487
488 return 0;
489}
490
554831e7
MD
491void lttcomm_init(void)
492{
493 const char *env;
494
495 env = getenv(NETWORK_TIMEOUT_ENV);
496 if (env) {
497 long timeout;
498
499 errno = 0;
500 timeout = strtol(env, NULL, 0);
501 if (errno != 0 || timeout < -1L) {
502 PERROR("Network timeout");
503 } else {
504 if (timeout > 0) {
505 network_timeout = timeout;
506 }
507 }
508 }
509}
510
554831e7
MD
511unsigned long lttcomm_get_network_timeout(void)
512{
513 return network_timeout;
514}
4222116f
JR
515
516/*
517 * Only valid for an ipv4 and ipv6 bound socket that is already connected to its
518 * peer.
519 */
520int lttcomm_populate_sock_from_open_socket(
521 struct lttcomm_sock *sock,
522 int fd,
523 enum lttcomm_sock_proto protocol)
524{
525 int ret = 0;
526 socklen_t storage_len;
527 struct sockaddr_storage storage = { 0 };
528
529 assert(sock);
530 assert(fd >= 0);
531
532 sock->proto = protocol;
533
534 storage_len = sizeof(storage);
535 ret = getpeername(fd, (struct sockaddr *) &storage,
536 &storage_len);
537 if (ret) {
538 ERR("Failed to get peer info for socket %d (errno: %d)", fd,
539 errno);
540 ret = -1;
541 goto end;
542 }
543
544 if (storage_len > sizeof(storage)) {
545 ERR("Failed to get peer info for socket %d: storage size is too small", fd);
546 ret = -1;
547 goto end;
548 }
549
550 switch (storage.ss_family) {
551 case AF_INET:
552 sock->sockaddr.type = LTTCOMM_INET;
553 memcpy(&sock->sockaddr.addr, &storage,
554 sizeof(struct sockaddr_in));
555 break;
556 case AF_INET6:
557 sock->sockaddr.type = LTTCOMM_INET6;
558 memcpy(&sock->sockaddr.addr, &storage,
559 sizeof(struct sockaddr_in6));
560 break;
561 default:
562 abort();
563 break;
564 }
565
566 /* Create a valid socket object with a temporary fd. */
567 ret = lttcomm_create_sock(sock);
568 if (ret < 0) {
569 ERR("Failed to create temporary socket object");
570 ret = -1;
571 goto end;
572 }
573
574 /* Substitute the fd. */
575 if (sock->ops->close(sock)) {
576 ret = -1;
577 goto end;
578 }
579 sock->fd = fd;
580
581end:
582 return ret;
583}
This page took 0.099781 seconds and 4 git commands to generate.