2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
26 #include <sys/types.h>
31 #include <common/common.h>
33 #include "sessiond-comm.h"
36 #include <common/unix.h>
39 /* For Inet6 socket */
42 #define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT"
44 static struct lttcomm_net_family net_families
[] = {
45 { LTTCOMM_INET
, lttcomm_create_inet_sock
},
46 { LTTCOMM_INET6
, lttcomm_create_inet6_sock
},
50 * Human readable error message.
52 static const char *lttcomm_readable_code
[] = {
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) ] = "consumerd command socket ready",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD
) ] = "consumerd success on receiving fds",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD
) ] = "consumerd error on receiving fds",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD
) ] = "consumerd error on receiving command",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR
) ] = "consumerd error in polling thread",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL
) ] = "consumerd polling on closed fd",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP
) ] = "consumerd all fd hung up",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS
) ] = "consumerd exiting normally",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE
) ] = "consumerd exiting on error",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR
) ] = "consumerd error opening the tracefile",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF
) ] = "consumerd splice EBADF",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL
) ] = "consumerd splice EINVAL",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM
) ] = "consumerd splice ENOMEM",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE
) ] = "consumerd splice ESPIPE",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM
) ] = "Consumer is out of memory",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA
) ] = "Error with metadata",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL
) ] = "Fatal error",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL
) ] = "Error on remote relayd",
73 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR
) ] = "Unknown error code"
76 static unsigned long network_timeout
;
79 * Return ptr to string representing a human readable error code from the
80 * lttcomm_return_code enum.
82 * These code MUST be negative in other to treat that as an error value.
85 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
89 if (code
< LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
|| code
> LTTCOMM_NR
) {
93 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(code
)];
97 * Create socket from an already allocated lttcomm socket structure and init
98 * sockaddr in the lttcomm sock.
101 int lttcomm_create_sock(struct lttcomm_sock
*sock
)
103 int ret
, _sock_type
, _sock_proto
, domain
;
107 domain
= sock
->sockaddr
.type
;
108 if (domain
!= LTTCOMM_INET
&& domain
!= LTTCOMM_INET6
) {
109 ERR("Create socket of unknown domain %d", domain
);
114 switch (sock
->proto
) {
115 case LTTCOMM_SOCK_UDP
:
116 _sock_type
= SOCK_DGRAM
;
117 _sock_proto
= IPPROTO_UDP
;
119 case LTTCOMM_SOCK_TCP
:
120 _sock_type
= SOCK_STREAM
;
121 _sock_proto
= IPPROTO_TCP
;
128 ret
= net_families
[domain
].create(sock
, _sock_type
, _sock_proto
);
138 * Return allocated lttcomm socket structure.
141 struct lttcomm_sock
*lttcomm_alloc_sock(enum lttcomm_sock_proto proto
)
143 struct lttcomm_sock
*sock
;
145 sock
= zmalloc(sizeof(struct lttcomm_sock
));
147 PERROR("zmalloc create sock");
159 * Return an allocated lttcomm socket structure and copy src content into
160 * the newly created socket.
162 * This is mostly useful when lttcomm_sock are passed between process where the
163 * fd and ops have to be changed within the correct address space.
166 struct lttcomm_sock
*lttcomm_alloc_copy_sock(struct lttcomm_sock
*src
)
168 struct lttcomm_sock
*sock
;
173 sock
= lttcomm_alloc_sock(src
->proto
);
178 lttcomm_copy_sock(sock
, src
);
185 * Create and copy socket from an allocated lttcomm socket structure.
187 * This is mostly useful when lttcomm_sock are passed between process where the
188 * fd and ops have to be changed within the correct address space.
191 void lttcomm_copy_sock(struct lttcomm_sock
*dst
, struct lttcomm_sock
*src
)
197 dst
->proto
= src
->proto
;
200 /* Copy sockaddr information from original socket */
201 memcpy(&dst
->sockaddr
, &src
->sockaddr
, sizeof(dst
->sockaddr
));
205 * Init IPv4 sockaddr structure.
208 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
209 const char *ip
, unsigned int port
)
215 assert(port
> 0 && port
<= 65535);
217 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
219 sockaddr
->type
= LTTCOMM_INET
;
220 sockaddr
->addr
.sin
.sin_family
= AF_INET
;
221 sockaddr
->addr
.sin
.sin_port
= htons(port
);
222 ret
= inet_pton(sockaddr
->addr
.sin
.sin_family
, ip
,
223 &sockaddr
->addr
.sin
.sin_addr
);
226 ERR("%s with port %d: unrecognized IPv4 address", ip
, port
);
229 memset(sockaddr
->addr
.sin
.sin_zero
, 0, sizeof(sockaddr
->addr
.sin
.sin_zero
));
236 * Init IPv6 sockaddr structure.
239 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
240 const char *ip
, unsigned int port
)
246 assert(port
> 0 && port
<= 65535);
248 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
250 sockaddr
->type
= LTTCOMM_INET6
;
251 sockaddr
->addr
.sin6
.sin6_family
= AF_INET6
;
252 sockaddr
->addr
.sin6
.sin6_port
= htons(port
);
253 ret
= inet_pton(sockaddr
->addr
.sin6
.sin6_family
, ip
,
254 &sockaddr
->addr
.sin6
.sin6_addr
);
265 * Return allocated lttcomm socket structure from lttng URI.
268 struct lttcomm_sock
*lttcomm_alloc_sock_from_uri(struct lttng_uri
*uri
)
272 struct lttcomm_sock
*sock
= NULL
;
277 /* Check URI protocol */
278 if (uri
->proto
== LTTNG_TCP
) {
279 _sock_proto
= LTTCOMM_SOCK_TCP
;
281 ERR("Relayd invalid URI proto: %d", uri
->proto
);
285 sock
= lttcomm_alloc_sock(_sock_proto
);
290 /* Check destination type */
291 if (uri
->dtype
== LTTNG_DST_IPV4
) {
292 ret
= lttcomm_init_inet_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv4
,
297 } else if (uri
->dtype
== LTTNG_DST_IPV6
) {
298 ret
= lttcomm_init_inet6_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv6
,
304 /* Command URI is invalid */
305 ERR("Relayd invalid URI dst type: %d", uri
->dtype
);
312 lttcomm_destroy_sock(sock
);
318 * Destroy and free lttcomm socket.
321 void lttcomm_destroy_sock(struct lttcomm_sock
*sock
)
327 * Allocate and return a relayd socket object using a given URI to initialize
328 * it and the major/minor version of the supported protocol.
330 * On error, NULL is returned.
333 struct lttcomm_relayd_sock
*lttcomm_alloc_relayd_sock(struct lttng_uri
*uri
,
334 uint32_t major
, uint32_t minor
)
337 struct lttcomm_sock
*tmp_sock
= NULL
;
338 struct lttcomm_relayd_sock
*rsock
= NULL
;
342 rsock
= zmalloc(sizeof(*rsock
));
344 PERROR("zmalloc relayd sock");
348 /* Allocate socket object from URI */
349 tmp_sock
= lttcomm_alloc_sock_from_uri(uri
);
350 if (tmp_sock
== NULL
) {
355 * Create socket object which basically sets the ops according to the
358 lttcomm_copy_sock(&rsock
->sock
, tmp_sock
);
359 /* Temporary socket pointer not needed anymore. */
360 lttcomm_destroy_sock(tmp_sock
);
361 ret
= lttcomm_create_sock(&rsock
->sock
);
366 rsock
->major
= major
;
367 rsock
->minor
= minor
;
378 * Set socket receiving timeout.
381 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
386 tv
.tv_sec
= msec
/ 1000;
387 tv
.tv_usec
= (msec
% 1000) * 1000;
389 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
391 PERROR("setsockopt SO_RCVTIMEO");
398 * Set socket sending timeout.
401 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
406 tv
.tv_sec
= msec
/ 1000;
407 tv
.tv_usec
= (msec
% 1000) * 1000;
409 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
411 PERROR("setsockopt SO_SNDTIMEO");
418 int lttcomm_sock_get_port(const struct lttcomm_sock
*sock
, uint16_t *port
)
422 assert(sock
->sockaddr
.type
== LTTCOMM_INET
||
423 sock
->sockaddr
.type
== LTTCOMM_INET6
);
424 assert(sock
->proto
== LTTCOMM_SOCK_TCP
||
425 sock
->proto
== LTTCOMM_SOCK_UDP
);
427 switch (sock
->sockaddr
.type
) {
429 *port
= ntohs(sock
->sockaddr
.addr
.sin
.sin_port
);
432 *port
= ntohs(sock
->sockaddr
.addr
.sin6
.sin6_port
);
442 int lttcomm_sock_set_port(struct lttcomm_sock
*sock
, uint16_t port
)
445 assert(sock
->sockaddr
.type
== LTTCOMM_INET
||
446 sock
->sockaddr
.type
== LTTCOMM_INET6
);
447 assert(sock
->proto
== LTTCOMM_SOCK_TCP
||
448 sock
->proto
== LTTCOMM_SOCK_UDP
);
450 switch (sock
->sockaddr
.type
) {
452 sock
->sockaddr
.addr
.sin
.sin_port
= htons(port
);
455 sock
->sockaddr
.addr
.sin6
.sin6_port
= htons(port
);
465 void lttcomm_init(void)
469 env
= getenv(NETWORK_TIMEOUT_ENV
);
474 timeout
= strtol(env
, NULL
, 0);
475 if (errno
!= 0 || timeout
< -1L) {
476 PERROR("Network timeout");
479 network_timeout
= timeout
;
486 unsigned long lttcomm_get_network_timeout(void)
488 return network_timeout
;