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 | ||
6c1c0768 | 19 | #define _LGPL_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> |
2288467f | 29 | #include <inttypes.h> |
fac6795d | 30 | |
90e535ef | 31 | #include <common/common.h> |
990570ed | 32 | |
10a8a223 | 33 | #include "sessiond-comm.h" |
fac6795d | 34 | |
6364a07a | 35 | /* For Unix socket */ |
2038dd6c | 36 | #include <common/unix.h> |
6364a07a DG |
37 | /* For Inet socket */ |
38 | #include "inet.h" | |
39 | /* For Inet6 socket */ | |
40 | #include "inet6.h" | |
41 | ||
554831e7 MD |
42 | #define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT" |
43 | ||
32dd26fb | 44 | static struct lttcomm_net_family net_families[] = { |
6364a07a DG |
45 | { LTTCOMM_INET, lttcomm_create_inet_sock }, |
46 | { LTTCOMM_INET6, lttcomm_create_inet6_sock }, | |
47 | }; | |
48 | ||
fac6795d DG |
49 | /* |
50 | * Human readable error message. | |
51 | */ | |
52 | static const char *lttcomm_readable_code[] = { | |
f73fabfd DG |
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", | |
40727660 MD |
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", | |
618a6a28 | 70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL) ] = "Error on remote relayd", |
80e327fa | 71 | |
f73fabfd DG |
72 | /* Last element */ |
73 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code" | |
fac6795d DG |
74 | }; |
75 | ||
554831e7 MD |
76 | static unsigned long network_timeout; |
77 | ||
fac6795d | 78 | /* |
917216f6 DG |
79 | * Return ptr to string representing a human readable error code from the |
80 | * lttcomm_return_code enum. | |
fac6795d | 81 | * |
917216f6 | 82 | * These code MUST be negative in other to treat that as an error value. |
fac6795d | 83 | */ |
90e535ef | 84 | LTTNG_HIDDEN |
fac6795d DG |
85 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
86 | { | |
f73fabfd | 87 | code = -code; |
fac6795d | 88 | |
c617c0c6 | 89 | if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) { |
f73fabfd | 90 | code = LTTCOMM_NR; |
fac6795d DG |
91 | } |
92 | ||
f73fabfd | 93 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)]; |
fac6795d | 94 | } |
6364a07a DG |
95 | |
96 | /* | |
de5e9086 DG |
97 | * Create socket from an already allocated lttcomm socket structure and init |
98 | * sockaddr in the lttcomm sock. | |
6364a07a | 99 | */ |
90e535ef | 100 | LTTNG_HIDDEN |
de5e9086 | 101 | int lttcomm_create_sock(struct lttcomm_sock *sock) |
6364a07a | 102 | { |
de5e9086 | 103 | int ret, _sock_type, _sock_proto, domain; |
6364a07a | 104 | |
de5e9086 DG |
105 | assert(sock); |
106 | ||
107 | domain = sock->sockaddr.type; | |
108 | if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) { | |
109 | ERR("Create socket of unknown domain %d", domain); | |
110 | ret = -1; | |
111 | goto error; | |
6364a07a DG |
112 | } |
113 | ||
de5e9086 DG |
114 | switch (sock->proto) { |
115 | case LTTCOMM_SOCK_UDP: | |
116 | _sock_type = SOCK_DGRAM; | |
117 | _sock_proto = IPPROTO_UDP; | |
118 | break; | |
119 | case LTTCOMM_SOCK_TCP: | |
120 | _sock_type = SOCK_STREAM; | |
121 | _sock_proto = IPPROTO_TCP; | |
122 | break; | |
123 | default: | |
124 | ret = -1; | |
125 | goto error; | |
126 | } | |
6364a07a | 127 | |
de5e9086 DG |
128 | ret = net_families[domain].create(sock, _sock_type, _sock_proto); |
129 | if (ret < 0) { | |
130 | goto error; | |
131 | } | |
132 | ||
133 | error: | |
134 | return ret; | |
6364a07a DG |
135 | } |
136 | ||
137 | /* | |
de5e9086 | 138 | * Return allocated lttcomm socket structure. |
6364a07a | 139 | */ |
90e535ef | 140 | LTTNG_HIDDEN |
de5e9086 | 141 | struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) |
6364a07a | 142 | { |
de5e9086 | 143 | struct lttcomm_sock *sock; |
6364a07a | 144 | |
de5e9086 DG |
145 | sock = zmalloc(sizeof(struct lttcomm_sock)); |
146 | if (sock == NULL) { | |
147 | PERROR("zmalloc create sock"); | |
148 | goto end; | |
6364a07a DG |
149 | } |
150 | ||
151 | sock->proto = proto; | |
de5e9086 | 152 | sock->fd = -1; |
6364a07a | 153 | |
de5e9086 DG |
154 | end: |
155 | return sock; | |
6364a07a DG |
156 | } |
157 | ||
158 | /* | |
de5e9086 DG |
159 | * Return an allocated lttcomm socket structure and copy src content into |
160 | * the newly created socket. | |
161 | * | |
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. | |
6364a07a | 164 | */ |
90e535ef | 165 | LTTNG_HIDDEN |
de5e9086 | 166 | struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src) |
6364a07a | 167 | { |
6364a07a DG |
168 | struct lttcomm_sock *sock; |
169 | ||
de5e9086 DG |
170 | /* Safety net */ |
171 | assert(src); | |
172 | ||
173 | sock = lttcomm_alloc_sock(src->proto); | |
6364a07a DG |
174 | if (sock == NULL) { |
175 | goto alloc_error; | |
176 | } | |
177 | ||
de5e9086 | 178 | lttcomm_copy_sock(sock, src); |
6364a07a | 179 | |
de5e9086 | 180 | alloc_error: |
6364a07a | 181 | return sock; |
de5e9086 | 182 | } |
6364a07a | 183 | |
de5e9086 DG |
184 | /* |
185 | * Create and copy socket from an allocated lttcomm socket structure. | |
186 | * | |
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. | |
189 | */ | |
90e535ef | 190 | LTTNG_HIDDEN |
de5e9086 DG |
191 | void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src) |
192 | { | |
193 | /* Safety net */ | |
194 | assert(dst); | |
195 | assert(src); | |
196 | ||
197 | dst->proto = src->proto; | |
198 | dst->fd = src->fd; | |
199 | dst->ops = src->ops; | |
200 | /* Copy sockaddr information from original socket */ | |
201 | memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr)); | |
6364a07a DG |
202 | } |
203 | ||
204 | /* | |
205 | * Init IPv4 sockaddr structure. | |
206 | */ | |
90e535ef | 207 | LTTNG_HIDDEN |
6364a07a DG |
208 | int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, |
209 | const char *ip, unsigned int port) | |
210 | { | |
211 | int ret; | |
212 | ||
213 | assert(sockaddr); | |
214 | assert(ip); | |
215 | assert(port > 0 && port <= 65535); | |
216 | ||
217 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
218 | ||
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); | |
224 | if (ret < 1) { | |
225 | ret = -1; | |
de5e9086 | 226 | ERR("%s with port %d: unrecognized IPv4 address", ip, port); |
6364a07a DG |
227 | goto error; |
228 | } | |
229 | memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero)); | |
230 | ||
231 | error: | |
232 | return ret; | |
233 | } | |
234 | ||
235 | /* | |
236 | * Init IPv6 sockaddr structure. | |
237 | */ | |
90e535ef | 238 | LTTNG_HIDDEN |
6364a07a DG |
239 | int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, |
240 | const char *ip, unsigned int port) | |
241 | { | |
242 | int ret; | |
243 | ||
244 | assert(sockaddr); | |
245 | assert(ip); | |
246 | assert(port > 0 && port <= 65535); | |
247 | ||
248 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
249 | ||
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); | |
255 | if (ret < 1) { | |
256 | ret = -1; | |
257 | goto error; | |
258 | } | |
259 | ||
260 | error: | |
261 | return ret; | |
262 | } | |
de5e9086 DG |
263 | |
264 | /* | |
265 | * Return allocated lttcomm socket structure from lttng URI. | |
266 | */ | |
90e535ef | 267 | LTTNG_HIDDEN |
de5e9086 DG |
268 | struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) |
269 | { | |
270 | int ret; | |
271 | int _sock_proto; | |
272 | struct lttcomm_sock *sock = NULL; | |
273 | ||
274 | /* Safety net */ | |
275 | assert(uri); | |
276 | ||
277 | /* Check URI protocol */ | |
278 | if (uri->proto == LTTNG_TCP) { | |
279 | _sock_proto = LTTCOMM_SOCK_TCP; | |
280 | } else { | |
281 | ERR("Relayd invalid URI proto: %d", uri->proto); | |
282 | goto alloc_error; | |
283 | } | |
284 | ||
285 | sock = lttcomm_alloc_sock(_sock_proto); | |
286 | if (sock == NULL) { | |
287 | goto alloc_error; | |
288 | } | |
289 | ||
290 | /* Check destination type */ | |
291 | if (uri->dtype == LTTNG_DST_IPV4) { | |
292 | ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4, | |
293 | uri->port); | |
294 | if (ret < 0) { | |
295 | goto error; | |
296 | } | |
297 | } else if (uri->dtype == LTTNG_DST_IPV6) { | |
298 | ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6, | |
299 | uri->port); | |
300 | if (ret < 0) { | |
301 | goto error; | |
302 | } | |
303 | } else { | |
304 | /* Command URI is invalid */ | |
305 | ERR("Relayd invalid URI dst type: %d", uri->dtype); | |
306 | goto error; | |
307 | } | |
308 | ||
309 | return sock; | |
310 | ||
311 | error: | |
312 | lttcomm_destroy_sock(sock); | |
313 | alloc_error: | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Destroy and free lttcomm socket. | |
319 | */ | |
90e535ef | 320 | LTTNG_HIDDEN |
de5e9086 DG |
321 | void lttcomm_destroy_sock(struct lttcomm_sock *sock) |
322 | { | |
0e428499 | 323 | free(sock); |
de5e9086 | 324 | } |
6151a90f JD |
325 | |
326 | /* | |
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. | |
329 | * | |
330 | * On error, NULL is returned. | |
331 | */ | |
bc182241 | 332 | LTTNG_HIDDEN |
6151a90f JD |
333 | struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri, |
334 | uint32_t major, uint32_t minor) | |
335 | { | |
336 | int ret; | |
337 | struct lttcomm_sock *tmp_sock = NULL; | |
338 | struct lttcomm_relayd_sock *rsock = NULL; | |
339 | ||
340 | assert(uri); | |
341 | ||
342 | rsock = zmalloc(sizeof(*rsock)); | |
343 | if (!rsock) { | |
344 | PERROR("zmalloc relayd sock"); | |
345 | goto error; | |
346 | } | |
347 | ||
348 | /* Allocate socket object from URI */ | |
349 | tmp_sock = lttcomm_alloc_sock_from_uri(uri); | |
350 | if (tmp_sock == NULL) { | |
351 | goto error_free; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Create socket object which basically sets the ops according to the | |
356 | * socket protocol. | |
357 | */ | |
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); | |
362 | if (ret < 0) { | |
363 | goto error_free; | |
364 | } | |
365 | ||
366 | rsock->major = major; | |
367 | rsock->minor = minor; | |
368 | ||
369 | return rsock; | |
370 | ||
371 | error_free: | |
372 | free(rsock); | |
373 | error: | |
374 | return NULL; | |
375 | } | |
554831e7 | 376 | |
783a3b9a MD |
377 | /* |
378 | * Set socket receiving timeout. | |
379 | */ | |
380 | LTTNG_HIDDEN | |
381 | int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) | |
382 | { | |
383 | int ret; | |
384 | struct timeval tv; | |
385 | ||
386 | tv.tv_sec = msec / 1000; | |
387 | tv.tv_usec = (msec % 1000) * 1000; | |
388 | ||
389 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
390 | if (ret < 0) { | |
391 | PERROR("setsockopt SO_RCVTIMEO"); | |
392 | } | |
393 | ||
394 | return ret; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Set socket sending timeout. | |
399 | */ | |
400 | LTTNG_HIDDEN | |
401 | int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec) | |
402 | { | |
403 | int ret; | |
404 | struct timeval tv; | |
405 | ||
406 | tv.tv_sec = msec / 1000; | |
407 | tv.tv_usec = (msec % 1000) * 1000; | |
408 | ||
409 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
410 | if (ret < 0) { | |
411 | PERROR("setsockopt SO_SNDTIMEO"); | |
412 | } | |
413 | ||
414 | return ret; | |
415 | } | |
416 | ||
2288467f JG |
417 | LTTNG_HIDDEN |
418 | int lttcomm_sock_get_port(const struct lttcomm_sock *sock, uint16_t *port) | |
419 | { | |
420 | assert(sock); | |
421 | assert(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); | |
426 | ||
427 | switch (sock->sockaddr.type) { | |
428 | case LTTCOMM_INET: | |
429 | *port = ntohs(sock->sockaddr.addr.sin.sin_port); | |
430 | break; | |
431 | case LTTCOMM_INET6: | |
432 | *port = ntohs(sock->sockaddr.addr.sin6.sin6_port); | |
433 | break; | |
434 | default: | |
435 | abort(); | |
436 | } | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | LTTNG_HIDDEN | |
442 | int lttcomm_sock_set_port(struct lttcomm_sock *sock, uint16_t port) | |
443 | { | |
444 | assert(sock); | |
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); | |
449 | ||
450 | switch (sock->sockaddr.type) { | |
451 | case LTTCOMM_INET: | |
452 | sock->sockaddr.addr.sin.sin_port = htons(port); | |
453 | break; | |
454 | case LTTCOMM_INET6: | |
455 | sock->sockaddr.addr.sin6.sin6_port = htons(port); | |
456 | break; | |
457 | default: | |
458 | abort(); | |
459 | } | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
554831e7 MD |
464 | LTTNG_HIDDEN |
465 | void lttcomm_init(void) | |
466 | { | |
467 | const char *env; | |
468 | ||
469 | env = getenv(NETWORK_TIMEOUT_ENV); | |
470 | if (env) { | |
471 | long timeout; | |
472 | ||
473 | errno = 0; | |
474 | timeout = strtol(env, NULL, 0); | |
475 | if (errno != 0 || timeout < -1L) { | |
476 | PERROR("Network timeout"); | |
477 | } else { | |
478 | if (timeout > 0) { | |
479 | network_timeout = timeout; | |
480 | } | |
481 | } | |
482 | } | |
483 | } | |
484 | ||
485 | LTTNG_HIDDEN | |
486 | unsigned long lttcomm_get_network_timeout(void) | |
487 | { | |
488 | return network_timeout; | |
489 | } |