2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
20 #include <common/common.h>
21 #include <common/compat/errno.h>
23 #include "sessiond-comm.h"
26 #include <common/unix.h>
29 /* For Inet6 socket */
32 #define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT"
34 static struct lttcomm_net_family net_families
[] = {
35 { LTTCOMM_INET
, lttcomm_create_inet_sock
},
36 { LTTCOMM_INET6
, lttcomm_create_inet6_sock
},
40 * Human readable error message.
42 static const char *lttcomm_readable_code
[] = {
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) ] = "consumerd command socket ready",
44 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD
) ] = "consumerd success on receiving fds",
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD
) ] = "consumerd error on receiving fds",
46 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD
) ] = "consumerd error on receiving command",
47 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR
) ] = "consumerd error in polling thread",
48 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL
) ] = "consumerd polling on closed fd",
49 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP
) ] = "consumerd all fd hung up",
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS
) ] = "consumerd exiting normally",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE
) ] = "consumerd exiting on error",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR
) ] = "consumerd error opening the tracefile",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF
) ] = "consumerd splice EBADF",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL
) ] = "consumerd splice EINVAL",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM
) ] = "consumerd splice ENOMEM",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE
) ] = "consumerd splice ESPIPE",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM
) ] = "Consumer is out of memory",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA
) ] = "Error with metadata",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL
) ] = "Fatal error",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL
) ] = "Error on remote relayd",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR
) ] = "Unknown error code"
66 static unsigned long network_timeout
;
69 * Return ptr to string representing a human readable error code from the
70 * lttcomm_return_code enum.
72 * These code MUST be negative in other to treat that as an error value.
75 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
79 if (code
< LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
|| code
> LTTCOMM_NR
) {
83 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(code
)];
87 * Create socket from an already allocated lttcomm socket structure and init
88 * sockaddr in the lttcomm sock.
91 int lttcomm_create_sock(struct lttcomm_sock
*sock
)
93 int ret
, _sock_type
, _sock_proto
, domain
;
97 domain
= sock
->sockaddr
.type
;
98 if (domain
!= LTTCOMM_INET
&& domain
!= LTTCOMM_INET6
) {
99 ERR("Create socket of unknown domain %d", domain
);
104 switch (sock
->proto
) {
105 case LTTCOMM_SOCK_UDP
:
106 _sock_type
= SOCK_DGRAM
;
107 _sock_proto
= IPPROTO_UDP
;
109 case LTTCOMM_SOCK_TCP
:
110 _sock_type
= SOCK_STREAM
;
111 _sock_proto
= IPPROTO_TCP
;
118 ret
= net_families
[domain
].create(sock
, _sock_type
, _sock_proto
);
128 * Return allocated lttcomm socket structure.
131 struct lttcomm_sock
*lttcomm_alloc_sock(enum lttcomm_sock_proto proto
)
133 struct lttcomm_sock
*sock
;
135 sock
= zmalloc(sizeof(struct lttcomm_sock
));
137 PERROR("zmalloc create sock");
149 * Return an allocated lttcomm socket structure and copy src content into
150 * the newly created socket.
152 * This is mostly useful when lttcomm_sock are passed between process where the
153 * fd and ops have to be changed within the correct address space.
156 struct lttcomm_sock
*lttcomm_alloc_copy_sock(struct lttcomm_sock
*src
)
158 struct lttcomm_sock
*sock
;
163 sock
= lttcomm_alloc_sock(src
->proto
);
168 lttcomm_copy_sock(sock
, src
);
175 * Create and copy socket from an allocated lttcomm socket structure.
177 * This is mostly useful when lttcomm_sock are passed between process where the
178 * fd and ops have to be changed within the correct address space.
181 void lttcomm_copy_sock(struct lttcomm_sock
*dst
, struct lttcomm_sock
*src
)
187 dst
->proto
= src
->proto
;
190 /* Copy sockaddr information from original socket */
191 memcpy(&dst
->sockaddr
, &src
->sockaddr
, sizeof(dst
->sockaddr
));
195 * Init IPv4 sockaddr structure.
198 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
199 const char *ip
, unsigned int port
)
205 assert(port
> 0 && port
<= 65535);
207 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
209 sockaddr
->type
= LTTCOMM_INET
;
210 sockaddr
->addr
.sin
.sin_family
= AF_INET
;
211 sockaddr
->addr
.sin
.sin_port
= htons(port
);
212 ret
= inet_pton(sockaddr
->addr
.sin
.sin_family
, ip
,
213 &sockaddr
->addr
.sin
.sin_addr
);
216 ERR("%s with port %d: unrecognized IPv4 address", ip
, port
);
219 memset(sockaddr
->addr
.sin
.sin_zero
, 0, sizeof(sockaddr
->addr
.sin
.sin_zero
));
226 * Init IPv6 sockaddr structure.
229 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
230 const char *ip
, unsigned int port
)
236 assert(port
> 0 && port
<= 65535);
238 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
240 sockaddr
->type
= LTTCOMM_INET6
;
241 sockaddr
->addr
.sin6
.sin6_family
= AF_INET6
;
242 sockaddr
->addr
.sin6
.sin6_port
= htons(port
);
243 ret
= inet_pton(sockaddr
->addr
.sin6
.sin6_family
, ip
,
244 &sockaddr
->addr
.sin6
.sin6_addr
);
255 * Return allocated lttcomm socket structure from lttng URI.
258 struct lttcomm_sock
*lttcomm_alloc_sock_from_uri(struct lttng_uri
*uri
)
262 struct lttcomm_sock
*sock
= NULL
;
267 /* Check URI protocol */
268 if (uri
->proto
== LTTNG_TCP
) {
269 _sock_proto
= LTTCOMM_SOCK_TCP
;
271 ERR("Relayd invalid URI proto: %d", uri
->proto
);
275 sock
= lttcomm_alloc_sock(_sock_proto
);
280 /* Check destination type */
281 if (uri
->dtype
== LTTNG_DST_IPV4
) {
282 ret
= lttcomm_init_inet_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv4
,
287 } else if (uri
->dtype
== LTTNG_DST_IPV6
) {
288 ret
= lttcomm_init_inet6_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv6
,
294 /* Command URI is invalid */
295 ERR("Relayd invalid URI dst type: %d", uri
->dtype
);
302 lttcomm_destroy_sock(sock
);
308 * Destroy and free lttcomm socket.
311 void lttcomm_destroy_sock(struct lttcomm_sock
*sock
)
317 * Allocate and return a relayd socket object using a given URI to initialize
318 * it and the major/minor version of the supported protocol.
320 * On error, NULL is returned.
323 struct lttcomm_relayd_sock
*lttcomm_alloc_relayd_sock(struct lttng_uri
*uri
,
324 uint32_t major
, uint32_t minor
)
327 struct lttcomm_sock
*tmp_sock
= NULL
;
328 struct lttcomm_relayd_sock
*rsock
= NULL
;
332 rsock
= zmalloc(sizeof(*rsock
));
334 PERROR("zmalloc relayd sock");
338 /* Allocate socket object from URI */
339 tmp_sock
= lttcomm_alloc_sock_from_uri(uri
);
340 if (tmp_sock
== NULL
) {
345 * Create socket object which basically sets the ops according to the
348 lttcomm_copy_sock(&rsock
->sock
, tmp_sock
);
349 /* Temporary socket pointer not needed anymore. */
350 lttcomm_destroy_sock(tmp_sock
);
351 ret
= lttcomm_create_sock(&rsock
->sock
);
356 rsock
->major
= major
;
357 rsock
->minor
= minor
;
368 * Set socket receiving timeout.
371 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
376 tv
.tv_sec
= msec
/ 1000;
377 tv
.tv_usec
= (msec
% 1000) * 1000;
379 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
381 PERROR("setsockopt SO_RCVTIMEO");
388 * Set socket sending timeout.
391 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
396 tv
.tv_sec
= msec
/ 1000;
397 tv
.tv_usec
= (msec
% 1000) * 1000;
399 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
401 PERROR("setsockopt SO_SNDTIMEO");
408 int lttcomm_sock_get_port(const struct lttcomm_sock
*sock
, uint16_t *port
)
412 assert(sock
->sockaddr
.type
== LTTCOMM_INET
||
413 sock
->sockaddr
.type
== LTTCOMM_INET6
);
414 assert(sock
->proto
== LTTCOMM_SOCK_TCP
||
415 sock
->proto
== LTTCOMM_SOCK_UDP
);
417 switch (sock
->sockaddr
.type
) {
419 *port
= ntohs(sock
->sockaddr
.addr
.sin
.sin_port
);
422 *port
= ntohs(sock
->sockaddr
.addr
.sin6
.sin6_port
);
432 int lttcomm_sock_set_port(struct lttcomm_sock
*sock
, uint16_t port
)
435 assert(sock
->sockaddr
.type
== LTTCOMM_INET
||
436 sock
->sockaddr
.type
== LTTCOMM_INET6
);
437 assert(sock
->proto
== LTTCOMM_SOCK_TCP
||
438 sock
->proto
== LTTCOMM_SOCK_UDP
);
440 switch (sock
->sockaddr
.type
) {
442 sock
->sockaddr
.addr
.sin
.sin_port
= htons(port
);
445 sock
->sockaddr
.addr
.sin6
.sin6_port
= htons(port
);
455 void lttcomm_init(void)
459 env
= getenv(NETWORK_TIMEOUT_ENV
);
464 timeout
= strtol(env
, NULL
, 0);
465 if (errno
!= 0 || timeout
< -1L) {
466 PERROR("Network timeout");
469 network_timeout
= timeout
;
476 unsigned long lttcomm_get_network_timeout(void)
478 return network_timeout
;