Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
917216f6 DG |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
fac6795d | 4 | * |
d14d33bf AM |
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. | |
fac6795d | 8 | * |
917216f6 DG |
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 | |
12 | * more details. | |
fac6795d | 13 | * |
d14d33bf AM |
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. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
1e307fab | 20 | #include <assert.h> |
fac6795d DG |
21 | #include <limits.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
fac6795d DG |
25 | #include <sys/stat.h> |
26 | #include <sys/types.h> | |
fac6795d | 27 | #include <unistd.h> |
3bd1e081 | 28 | #include <errno.h> |
fac6795d | 29 | |
90e535ef | 30 | #include <common/common.h> |
990570ed | 31 | |
10a8a223 | 32 | #include "sessiond-comm.h" |
fac6795d | 33 | |
6364a07a DG |
34 | /* For Unix socket */ |
35 | #include "unix.h" | |
36 | /* For Inet socket */ | |
37 | #include "inet.h" | |
38 | /* For Inet6 socket */ | |
39 | #include "inet6.h" | |
40 | ||
32dd26fb | 41 | static struct lttcomm_net_family net_families[] = { |
6364a07a DG |
42 | { LTTCOMM_INET, lttcomm_create_inet_sock }, |
43 | { LTTCOMM_INET6, lttcomm_create_inet6_sock }, | |
44 | }; | |
45 | ||
fac6795d DG |
46 | /* |
47 | * Human readable error message. | |
48 | */ | |
49 | static const char *lttcomm_readable_code[] = { | |
f73fabfd DG |
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", | |
40727660 MD |
64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM) ] = "Consumer is out of memory", |
65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA) ] = "Error with metadata", | |
66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL) ] = "Fatal error", | |
618a6a28 | 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL) ] = "Error on remote relayd", |
80e327fa | 68 | |
f73fabfd DG |
69 | /* Last element */ |
70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code" | |
fac6795d DG |
71 | }; |
72 | ||
73 | /* | |
917216f6 DG |
74 | * Return ptr to string representing a human readable error code from the |
75 | * lttcomm_return_code enum. | |
fac6795d | 76 | * |
917216f6 | 77 | * These code MUST be negative in other to treat that as an error value. |
fac6795d | 78 | */ |
90e535ef | 79 | LTTNG_HIDDEN |
fac6795d DG |
80 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
81 | { | |
f73fabfd | 82 | code = -code; |
fac6795d | 83 | |
c617c0c6 | 84 | if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) { |
f73fabfd | 85 | code = LTTCOMM_NR; |
fac6795d DG |
86 | } |
87 | ||
f73fabfd | 88 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)]; |
fac6795d | 89 | } |
6364a07a DG |
90 | |
91 | /* | |
de5e9086 DG |
92 | * Create socket from an already allocated lttcomm socket structure and init |
93 | * sockaddr in the lttcomm sock. | |
6364a07a | 94 | */ |
90e535ef | 95 | LTTNG_HIDDEN |
de5e9086 | 96 | int lttcomm_create_sock(struct lttcomm_sock *sock) |
6364a07a | 97 | { |
de5e9086 | 98 | int ret, _sock_type, _sock_proto, domain; |
6364a07a | 99 | |
de5e9086 DG |
100 | assert(sock); |
101 | ||
102 | domain = sock->sockaddr.type; | |
103 | if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) { | |
104 | ERR("Create socket of unknown domain %d", domain); | |
105 | ret = -1; | |
106 | goto error; | |
6364a07a DG |
107 | } |
108 | ||
de5e9086 DG |
109 | switch (sock->proto) { |
110 | case LTTCOMM_SOCK_UDP: | |
111 | _sock_type = SOCK_DGRAM; | |
112 | _sock_proto = IPPROTO_UDP; | |
113 | break; | |
114 | case LTTCOMM_SOCK_TCP: | |
115 | _sock_type = SOCK_STREAM; | |
116 | _sock_proto = IPPROTO_TCP; | |
117 | break; | |
118 | default: | |
119 | ret = -1; | |
120 | goto error; | |
121 | } | |
6364a07a | 122 | |
de5e9086 DG |
123 | ret = net_families[domain].create(sock, _sock_type, _sock_proto); |
124 | if (ret < 0) { | |
125 | goto error; | |
126 | } | |
127 | ||
128 | error: | |
129 | return ret; | |
6364a07a DG |
130 | } |
131 | ||
132 | /* | |
de5e9086 | 133 | * Return allocated lttcomm socket structure. |
6364a07a | 134 | */ |
90e535ef | 135 | LTTNG_HIDDEN |
de5e9086 | 136 | struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) |
6364a07a | 137 | { |
de5e9086 | 138 | struct lttcomm_sock *sock; |
6364a07a | 139 | |
de5e9086 DG |
140 | sock = zmalloc(sizeof(struct lttcomm_sock)); |
141 | if (sock == NULL) { | |
142 | PERROR("zmalloc create sock"); | |
143 | goto end; | |
6364a07a DG |
144 | } |
145 | ||
146 | sock->proto = proto; | |
de5e9086 | 147 | sock->fd = -1; |
6364a07a | 148 | |
de5e9086 DG |
149 | end: |
150 | return sock; | |
6364a07a DG |
151 | } |
152 | ||
153 | /* | |
de5e9086 DG |
154 | * Return an allocated lttcomm socket structure and copy src content into |
155 | * the newly created socket. | |
156 | * | |
157 | * This is mostly useful when lttcomm_sock are passed between process where the | |
158 | * fd and ops have to be changed within the correct address space. | |
6364a07a | 159 | */ |
90e535ef | 160 | LTTNG_HIDDEN |
de5e9086 | 161 | struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src) |
6364a07a | 162 | { |
6364a07a DG |
163 | struct lttcomm_sock *sock; |
164 | ||
de5e9086 DG |
165 | /* Safety net */ |
166 | assert(src); | |
167 | ||
168 | sock = lttcomm_alloc_sock(src->proto); | |
6364a07a DG |
169 | if (sock == NULL) { |
170 | goto alloc_error; | |
171 | } | |
172 | ||
de5e9086 | 173 | lttcomm_copy_sock(sock, src); |
6364a07a | 174 | |
de5e9086 | 175 | alloc_error: |
6364a07a | 176 | return sock; |
de5e9086 | 177 | } |
6364a07a | 178 | |
de5e9086 DG |
179 | /* |
180 | * Create and copy socket from an allocated lttcomm socket structure. | |
181 | * | |
182 | * This is mostly useful when lttcomm_sock are passed between process where the | |
183 | * fd and ops have to be changed within the correct address space. | |
184 | */ | |
90e535ef | 185 | LTTNG_HIDDEN |
de5e9086 DG |
186 | void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src) |
187 | { | |
188 | /* Safety net */ | |
189 | assert(dst); | |
190 | assert(src); | |
191 | ||
192 | dst->proto = src->proto; | |
193 | dst->fd = src->fd; | |
194 | dst->ops = src->ops; | |
195 | /* Copy sockaddr information from original socket */ | |
196 | memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr)); | |
6364a07a DG |
197 | } |
198 | ||
199 | /* | |
200 | * Init IPv4 sockaddr structure. | |
201 | */ | |
90e535ef | 202 | LTTNG_HIDDEN |
6364a07a DG |
203 | int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, |
204 | const char *ip, unsigned int port) | |
205 | { | |
206 | int ret; | |
207 | ||
208 | assert(sockaddr); | |
209 | assert(ip); | |
210 | assert(port > 0 && port <= 65535); | |
211 | ||
212 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
213 | ||
214 | sockaddr->type = LTTCOMM_INET; | |
215 | sockaddr->addr.sin.sin_family = AF_INET; | |
216 | sockaddr->addr.sin.sin_port = htons(port); | |
217 | ret = inet_pton(sockaddr->addr.sin.sin_family, ip, | |
218 | &sockaddr->addr.sin.sin_addr); | |
219 | if (ret < 1) { | |
220 | ret = -1; | |
de5e9086 | 221 | ERR("%s with port %d: unrecognized IPv4 address", ip, port); |
6364a07a DG |
222 | goto error; |
223 | } | |
224 | memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero)); | |
225 | ||
226 | error: | |
227 | return ret; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Init IPv6 sockaddr structure. | |
232 | */ | |
90e535ef | 233 | LTTNG_HIDDEN |
6364a07a DG |
234 | int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, |
235 | const char *ip, unsigned int port) | |
236 | { | |
237 | int ret; | |
238 | ||
239 | assert(sockaddr); | |
240 | assert(ip); | |
241 | assert(port > 0 && port <= 65535); | |
242 | ||
243 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
244 | ||
245 | sockaddr->type = LTTCOMM_INET6; | |
246 | sockaddr->addr.sin6.sin6_family = AF_INET6; | |
247 | sockaddr->addr.sin6.sin6_port = htons(port); | |
248 | ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip, | |
249 | &sockaddr->addr.sin6.sin6_addr); | |
250 | if (ret < 1) { | |
251 | ret = -1; | |
252 | goto error; | |
253 | } | |
254 | ||
255 | error: | |
256 | return ret; | |
257 | } | |
de5e9086 DG |
258 | |
259 | /* | |
260 | * Return allocated lttcomm socket structure from lttng URI. | |
261 | */ | |
90e535ef | 262 | LTTNG_HIDDEN |
de5e9086 DG |
263 | struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) |
264 | { | |
265 | int ret; | |
266 | int _sock_proto; | |
267 | struct lttcomm_sock *sock = NULL; | |
268 | ||
269 | /* Safety net */ | |
270 | assert(uri); | |
271 | ||
272 | /* Check URI protocol */ | |
273 | if (uri->proto == LTTNG_TCP) { | |
274 | _sock_proto = LTTCOMM_SOCK_TCP; | |
275 | } else { | |
276 | ERR("Relayd invalid URI proto: %d", uri->proto); | |
277 | goto alloc_error; | |
278 | } | |
279 | ||
280 | sock = lttcomm_alloc_sock(_sock_proto); | |
281 | if (sock == NULL) { | |
282 | goto alloc_error; | |
283 | } | |
284 | ||
285 | /* Check destination type */ | |
286 | if (uri->dtype == LTTNG_DST_IPV4) { | |
287 | ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4, | |
288 | uri->port); | |
289 | if (ret < 0) { | |
290 | goto error; | |
291 | } | |
292 | } else if (uri->dtype == LTTNG_DST_IPV6) { | |
293 | ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6, | |
294 | uri->port); | |
295 | if (ret < 0) { | |
296 | goto error; | |
297 | } | |
298 | } else { | |
299 | /* Command URI is invalid */ | |
300 | ERR("Relayd invalid URI dst type: %d", uri->dtype); | |
301 | goto error; | |
302 | } | |
303 | ||
304 | return sock; | |
305 | ||
306 | error: | |
307 | lttcomm_destroy_sock(sock); | |
308 | alloc_error: | |
309 | return NULL; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Destroy and free lttcomm socket. | |
314 | */ | |
90e535ef | 315 | LTTNG_HIDDEN |
de5e9086 DG |
316 | void lttcomm_destroy_sock(struct lttcomm_sock *sock) |
317 | { | |
0e428499 | 318 | free(sock); |
de5e9086 | 319 | } |
6151a90f JD |
320 | |
321 | /* | |
322 | * Allocate and return a relayd socket object using a given URI to initialize | |
323 | * it and the major/minor version of the supported protocol. | |
324 | * | |
325 | * On error, NULL is returned. | |
326 | */ | |
bc182241 | 327 | LTTNG_HIDDEN |
6151a90f JD |
328 | struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri, |
329 | uint32_t major, uint32_t minor) | |
330 | { | |
331 | int ret; | |
332 | struct lttcomm_sock *tmp_sock = NULL; | |
333 | struct lttcomm_relayd_sock *rsock = NULL; | |
334 | ||
335 | assert(uri); | |
336 | ||
337 | rsock = zmalloc(sizeof(*rsock)); | |
338 | if (!rsock) { | |
339 | PERROR("zmalloc relayd sock"); | |
340 | goto error; | |
341 | } | |
342 | ||
343 | /* Allocate socket object from URI */ | |
344 | tmp_sock = lttcomm_alloc_sock_from_uri(uri); | |
345 | if (tmp_sock == NULL) { | |
346 | goto error_free; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Create socket object which basically sets the ops according to the | |
351 | * socket protocol. | |
352 | */ | |
353 | lttcomm_copy_sock(&rsock->sock, tmp_sock); | |
354 | /* Temporary socket pointer not needed anymore. */ | |
355 | lttcomm_destroy_sock(tmp_sock); | |
356 | ret = lttcomm_create_sock(&rsock->sock); | |
357 | if (ret < 0) { | |
358 | goto error_free; | |
359 | } | |
360 | ||
361 | rsock->major = major; | |
362 | rsock->minor = minor; | |
363 | ||
364 | return rsock; | |
365 | ||
366 | error_free: | |
367 | free(rsock); | |
368 | error: | |
369 | return NULL; | |
370 | } |