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 static struct lttcomm_net_family net_families
[] = {
42 { LTTCOMM_INET
, lttcomm_create_inet_sock
},
43 { LTTCOMM_INET6
, lttcomm_create_inet6_sock
},
47 * Human readable error message.
49 static const char *lttcomm_readable_code
[] = {
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) ] = "consumerd command socket ready",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD
) ] = "consumerd success on receiving fds",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD
) ] = "consumerd error on receiving fds",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD
) ] = "consumerd error on receiving command",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR
) ] = "consumerd error in polling thread",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL
) ] = "consumerd polling on closed fd",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP
) ] = "consumerd all fd hung up",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS
) ] = "consumerd exiting normally",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE
) ] = "consumerd exiting on error",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR
) ] = "consumerd error opening the tracefile",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF
) ] = "consumerd splice EBADF",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL
) ] = "consumerd splice EINVAL",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM
) ] = "consumerd splice ENOMEM",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE
) ] = "consumerd splice ESPIPE",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR
) ] = "Unknown error code"
70 * Return ptr to string representing a human readable error code from the
71 * lttcomm_return_code enum.
73 * These code MUST be negative in other to treat that as an error value.
76 const char *lttcomm_get_readable_code(enum lttcomm_return_code code
)
80 if (code
< LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
|| code
> LTTCOMM_NR
) {
84 return lttcomm_readable_code
[LTTCOMM_ERR_INDEX(code
)];
88 * Create socket from an already allocated lttcomm socket structure and init
89 * sockaddr in the lttcomm sock.
92 int lttcomm_create_sock(struct lttcomm_sock
*sock
)
94 int ret
, _sock_type
, _sock_proto
, domain
;
98 domain
= sock
->sockaddr
.type
;
99 if (domain
!= LTTCOMM_INET
&& domain
!= LTTCOMM_INET6
) {
100 ERR("Create socket of unknown domain %d", domain
);
105 switch (sock
->proto
) {
106 case LTTCOMM_SOCK_UDP
:
107 _sock_type
= SOCK_DGRAM
;
108 _sock_proto
= IPPROTO_UDP
;
110 case LTTCOMM_SOCK_TCP
:
111 _sock_type
= SOCK_STREAM
;
112 _sock_proto
= IPPROTO_TCP
;
119 ret
= net_families
[domain
].create(sock
, _sock_type
, _sock_proto
);
129 * Return allocated lttcomm socket structure.
132 struct lttcomm_sock
*lttcomm_alloc_sock(enum lttcomm_sock_proto proto
)
134 struct lttcomm_sock
*sock
;
136 sock
= zmalloc(sizeof(struct lttcomm_sock
));
138 PERROR("zmalloc create sock");
150 * Return an allocated lttcomm socket structure and copy src content into
151 * the newly created socket.
153 * This is mostly useful when lttcomm_sock are passed between process where the
154 * fd and ops have to be changed within the correct address space.
157 struct lttcomm_sock
*lttcomm_alloc_copy_sock(struct lttcomm_sock
*src
)
159 struct lttcomm_sock
*sock
;
164 sock
= lttcomm_alloc_sock(src
->proto
);
169 lttcomm_copy_sock(sock
, src
);
176 * Create and copy socket from an allocated lttcomm socket structure.
178 * This is mostly useful when lttcomm_sock are passed between process where the
179 * fd and ops have to be changed within the correct address space.
182 void lttcomm_copy_sock(struct lttcomm_sock
*dst
, struct lttcomm_sock
*src
)
188 dst
->proto
= src
->proto
;
191 /* Copy sockaddr information from original socket */
192 memcpy(&dst
->sockaddr
, &src
->sockaddr
, sizeof(dst
->sockaddr
));
196 * Init IPv4 sockaddr structure.
199 int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
200 const char *ip
, unsigned int port
)
206 assert(port
> 0 && port
<= 65535);
208 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
210 sockaddr
->type
= LTTCOMM_INET
;
211 sockaddr
->addr
.sin
.sin_family
= AF_INET
;
212 sockaddr
->addr
.sin
.sin_port
= htons(port
);
213 ret
= inet_pton(sockaddr
->addr
.sin
.sin_family
, ip
,
214 &sockaddr
->addr
.sin
.sin_addr
);
217 ERR("%s with port %d: unrecognized IPv4 address", ip
, port
);
220 memset(sockaddr
->addr
.sin
.sin_zero
, 0, sizeof(sockaddr
->addr
.sin
.sin_zero
));
227 * Init IPv6 sockaddr structure.
230 int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr
*sockaddr
,
231 const char *ip
, unsigned int port
)
237 assert(port
> 0 && port
<= 65535);
239 memset(sockaddr
, 0, sizeof(struct lttcomm_sockaddr
));
241 sockaddr
->type
= LTTCOMM_INET6
;
242 sockaddr
->addr
.sin6
.sin6_family
= AF_INET6
;
243 sockaddr
->addr
.sin6
.sin6_port
= htons(port
);
244 ret
= inet_pton(sockaddr
->addr
.sin6
.sin6_family
, ip
,
245 &sockaddr
->addr
.sin6
.sin6_addr
);
256 * Return allocated lttcomm socket structure from lttng URI.
259 struct lttcomm_sock
*lttcomm_alloc_sock_from_uri(struct lttng_uri
*uri
)
263 struct lttcomm_sock
*sock
= NULL
;
268 /* Check URI protocol */
269 if (uri
->proto
== LTTNG_TCP
) {
270 _sock_proto
= LTTCOMM_SOCK_TCP
;
272 ERR("Relayd invalid URI proto: %d", uri
->proto
);
276 sock
= lttcomm_alloc_sock(_sock_proto
);
281 /* Check destination type */
282 if (uri
->dtype
== LTTNG_DST_IPV4
) {
283 ret
= lttcomm_init_inet_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv4
,
288 } else if (uri
->dtype
== LTTNG_DST_IPV6
) {
289 ret
= lttcomm_init_inet6_sockaddr(&sock
->sockaddr
, uri
->dst
.ipv6
,
295 /* Command URI is invalid */
296 ERR("Relayd invalid URI dst type: %d", uri
->dtype
);
303 lttcomm_destroy_sock(sock
);
309 * Destroy and free lttcomm socket.
312 void lttcomm_destroy_sock(struct lttcomm_sock
*sock
)
318 * Allocate and return a relayd socket object using a given URI to initialize
319 * it and the major/minor version of the supported protocol.
321 * On error, NULL is returned.
324 struct lttcomm_relayd_sock
*lttcomm_alloc_relayd_sock(struct lttng_uri
*uri
,
325 uint32_t major
, uint32_t minor
)
328 struct lttcomm_sock
*tmp_sock
= NULL
;
329 struct lttcomm_relayd_sock
*rsock
= NULL
;
333 rsock
= zmalloc(sizeof(*rsock
));
335 PERROR("zmalloc relayd sock");
339 /* Allocate socket object from URI */
340 tmp_sock
= lttcomm_alloc_sock_from_uri(uri
);
341 if (tmp_sock
== NULL
) {
346 * Create socket object which basically sets the ops according to the
349 lttcomm_copy_sock(&rsock
->sock
, tmp_sock
);
350 /* Temporary socket pointer not needed anymore. */
351 lttcomm_destroy_sock(tmp_sock
);
352 ret
= lttcomm_create_sock(&rsock
->sock
);
357 rsock
->major
= major
;
358 rsock
->minor
= minor
;