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>
30 #include <common/common.h>
32 #include "sessiond-comm.h"
38 /* For Inet6 socket */
41 #define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT"
43 static struct lttcomm_net_family net_families
[] = {
44 { LTTCOMM_INET
, lttcomm_create_inet_sock
},
45 { LTTCOMM_INET6
, lttcomm_create_inet6_sock
},
49 * Human readable error message.
51 static const char *lttcomm_readable_code
[] = {
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) ] = "consumerd command socket ready",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD
) ] = "consumerd success on receiving fds",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD
) ] = "consumerd error on receiving fds",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD
) ] = "consumerd error on receiving command",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR
) ] = "consumerd error in polling thread",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL
) ] = "consumerd polling on closed fd",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP
) ] = "consumerd all fd hung up",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS
) ] = "consumerd exiting normally",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE
) ] = "consumerd exiting on error",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR
) ] = "consumerd error opening the tracefile",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF
) ] = "consumerd splice EBADF",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL
) ] = "consumerd splice EINVAL",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM
) ] = "consumerd splice ENOMEM",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE
) ] = "consumerd splice ESPIPE",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM
) ] = "Consumer is out of memory",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA
) ] = "Error with metadata",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL
) ] = "Fatal error",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL
) ] = "Error on remote relayd",
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR
) ] = "Unknown error code"
75 static unsigned long network_timeout
;
78 * Return ptr to string representing a human readable error code from the
79 * lttcomm_return_code enum.
81 * These code MUST be negative in other to treat that as an error value.
84 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
88 if (code
< LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
|| code
> LTTCOMM_NR
) {
92 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(code
)];
96 * Create socket from an already allocated lttcomm socket structure and init
97 * sockaddr in the lttcomm sock.
100 int lttcomm_create_sock(struct lttcomm_sock
*sock
)
102 int ret
, _sock_type
, _sock_proto
, domain
;
106 domain
= sock
->sockaddr
.type
;
107 if (domain
!= LTTCOMM_INET
&& domain
!= LTTCOMM_INET6
) {
108 ERR("Create socket of unknown domain %d", domain
);
113 switch (sock
->proto
) {
114 case LTTCOMM_SOCK_UDP
:
115 _sock_type
= SOCK_DGRAM
;
116 _sock_proto
= IPPROTO_UDP
;
118 case LTTCOMM_SOCK_TCP
:
119 _sock_type
= SOCK_STREAM
;
120 _sock_proto
= IPPROTO_TCP
;
127 ret
= net_families
[domain
].create(sock
, _sock_type
, _sock_proto
);
137 * Return allocated lttcomm socket structure.
140 struct lttcomm_sock
*lttcomm_alloc_sock(enum lttcomm_sock_proto proto
)
142 struct lttcomm_sock
*sock
;
144 sock
= zmalloc(sizeof(struct lttcomm_sock
));
146 PERROR("zmalloc create sock");
158 * Return an allocated lttcomm socket structure and copy src content into
159 * the newly created socket.
161 * This is mostly useful when lttcomm_sock are passed between process where the
162 * fd and ops have to be changed within the correct address space.
165 struct lttcomm_sock
*lttcomm_alloc_copy_sock(struct lttcomm_sock
*src
)
167 struct lttcomm_sock
*sock
;
172 sock
= lttcomm_alloc_sock(src
->proto
);
177 lttcomm_copy_sock(sock
, src
);
184 * Create and copy socket from an allocated lttcomm socket structure.
186 * This is mostly useful when lttcomm_sock are passed between process where the
187 * fd and ops have to be changed within the correct address space.
190 void lttcomm_copy_sock(struct lttcomm_sock
*dst
, struct lttcomm_sock
*src
)
196 dst
->proto
= src
->proto
;
199 /* Copy sockaddr information from original socket */
200 memcpy(&dst
->sockaddr
, &src
->sockaddr
, sizeof(dst
->sockaddr
));
204 * Init IPv4 sockaddr structure.
207 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
208 const char *ip
, unsigned int port
)
214 assert(port
> 0 && port
<= 65535);
216 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
218 sockaddr
->type
= LTTCOMM_INET
;
219 sockaddr
->addr
.sin
.sin_family
= AF_INET
;
220 sockaddr
->addr
.sin
.sin_port
= htons(port
);
221 ret
= inet_pton(sockaddr
->addr
.sin
.sin_family
, ip
,
222 &sockaddr
->addr
.sin
.sin_addr
);
225 ERR("%s with port %d: unrecognized IPv4 address", ip
, port
);
228 memset(sockaddr
->addr
.sin
.sin_zero
, 0, sizeof(sockaddr
->addr
.sin
.sin_zero
));
235 * Init IPv6 sockaddr structure.
238 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
239 const char *ip
, unsigned int port
)
245 assert(port
> 0 && port
<= 65535);
247 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
249 sockaddr
->type
= LTTCOMM_INET6
;
250 sockaddr
->addr
.sin6
.sin6_family
= AF_INET6
;
251 sockaddr
->addr
.sin6
.sin6_port
= htons(port
);
252 ret
= inet_pton(sockaddr
->addr
.sin6
.sin6_family
, ip
,
253 &sockaddr
->addr
.sin6
.sin6_addr
);
264 * Return allocated lttcomm socket structure from lttng URI.
267 struct lttcomm_sock
*lttcomm_alloc_sock_from_uri(struct lttng_uri
*uri
)
271 struct lttcomm_sock
*sock
= NULL
;
276 /* Check URI protocol */
277 if (uri
->proto
== LTTNG_TCP
) {
278 _sock_proto
= LTTCOMM_SOCK_TCP
;
280 ERR("Relayd invalid URI proto: %d", uri
->proto
);
284 sock
= lttcomm_alloc_sock(_sock_proto
);
289 /* Check destination type */
290 if (uri
->dtype
== LTTNG_DST_IPV4
) {
291 ret
= lttcomm_init_inet_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv4
,
296 } else if (uri
->dtype
== LTTNG_DST_IPV6
) {
297 ret
= lttcomm_init_inet6_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv6
,
303 /* Command URI is invalid */
304 ERR("Relayd invalid URI dst type: %d", uri
->dtype
);
311 lttcomm_destroy_sock(sock
);
317 * Destroy and free lttcomm socket.
320 void lttcomm_destroy_sock(struct lttcomm_sock
*sock
)
326 * Allocate and return a relayd socket object using a given URI to initialize
327 * it and the major/minor version of the supported protocol.
329 * On error, NULL is returned.
332 struct lttcomm_relayd_sock
*lttcomm_alloc_relayd_sock(struct lttng_uri
*uri
,
333 uint32_t major
, uint32_t minor
)
336 struct lttcomm_sock
*tmp_sock
= NULL
;
337 struct lttcomm_relayd_sock
*rsock
= NULL
;
341 rsock
= zmalloc(sizeof(*rsock
));
343 PERROR("zmalloc relayd sock");
347 /* Allocate socket object from URI */
348 tmp_sock
= lttcomm_alloc_sock_from_uri(uri
);
349 if (tmp_sock
== NULL
) {
354 * Create socket object which basically sets the ops according to the
357 lttcomm_copy_sock(&rsock
->sock
, tmp_sock
);
358 /* Temporary socket pointer not needed anymore. */
359 lttcomm_destroy_sock(tmp_sock
);
360 ret
= lttcomm_create_sock(&rsock
->sock
);
365 rsock
->major
= major
;
366 rsock
->minor
= minor
;
377 * Set socket receiving timeout.
380 int lttcomm_setsockopt_rcv_timeout(int sock
, unsigned int msec
)
385 tv
.tv_sec
= msec
/ 1000;
386 tv
.tv_usec
= (msec
% 1000) * 1000;
388 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
390 PERROR("setsockopt SO_RCVTIMEO");
397 * Set socket sending timeout.
400 int lttcomm_setsockopt_snd_timeout(int sock
, unsigned int msec
)
405 tv
.tv_sec
= msec
/ 1000;
406 tv
.tv_usec
= (msec
% 1000) * 1000;
408 ret
= setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
410 PERROR("setsockopt SO_SNDTIMEO");
417 void lttcomm_init(void)
421 env
= getenv(NETWORK_TIMEOUT_ENV
);
426 timeout
= strtol(env
, NULL
, 0);
427 if (errno
!= 0 || timeout
< -1L) {
428 PERROR("Network timeout");
431 network_timeout
= timeout
;
438 unsigned long lttcomm_get_network_timeout(void)
440 return network_timeout
;