Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0-only | |
fac6795d | 6 | * |
fac6795d DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
fac6795d DG |
10 | #include <limits.h> |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
fac6795d DG |
14 | #include <sys/stat.h> |
15 | #include <sys/types.h> | |
fac6795d | 16 | #include <unistd.h> |
2288467f | 17 | #include <inttypes.h> |
fac6795d | 18 | |
90e535ef | 19 | #include <common/common.h> |
edf4b93e | 20 | #include <common/compat/errno.h> |
990570ed | 21 | |
10a8a223 | 22 | #include "sessiond-comm.h" |
fac6795d | 23 | |
6364a07a | 24 | /* For Unix socket */ |
2038dd6c | 25 | #include <common/unix.h> |
6364a07a DG |
26 | /* For Inet socket */ |
27 | #include "inet.h" | |
28 | /* For Inet6 socket */ | |
29 | #include "inet6.h" | |
30 | ||
554831e7 MD |
31 | #define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT" |
32 | ||
32dd26fb | 33 | static struct lttcomm_net_family net_families[] = { |
6364a07a DG |
34 | { LTTCOMM_INET, lttcomm_create_inet_sock }, |
35 | { LTTCOMM_INET6, lttcomm_create_inet6_sock }, | |
36 | }; | |
37 | ||
fac6795d DG |
38 | /* |
39 | * Human readable error message. | |
40 | */ | |
64efa44e SM |
41 | static |
42 | const char *lttcomm_return_code_str(lttcomm_return_code code) { | |
43 | switch (code) { | |
44 | case LTTCOMM_CONSUMERD_SUCCESS: | |
45 | return "consumerd success"; | |
46 | case LTTCOMM_CONSUMERD_COMMAND_SOCK_READY: | |
47 | return "consumerd command socket ready"; | |
48 | case LTTCOMM_CONSUMERD_SUCCESS_RECV_FD: | |
49 | return "consumerd success on receiving fds"; | |
50 | case LTTCOMM_CONSUMERD_ERROR_RECV_FD: | |
51 | return "consumerd error on receiving fds"; | |
52 | case LTTCOMM_CONSUMERD_ERROR_RECV_CMD: | |
53 | return "consumerd error on receiving command"; | |
54 | case LTTCOMM_CONSUMERD_POLL_ERROR: | |
55 | return "consumerd error in polling thread"; | |
56 | case LTTCOMM_CONSUMERD_POLL_NVAL: | |
57 | return "consumerd polling on closed fd"; | |
58 | case LTTCOMM_CONSUMERD_POLL_HUP: | |
59 | return "consumerd all fd hung up"; | |
60 | case LTTCOMM_CONSUMERD_EXIT_SUCCESS: | |
61 | return "consumerd exiting normally"; | |
62 | case LTTCOMM_CONSUMERD_EXIT_FAILURE: | |
63 | return "consumerd exiting on error"; | |
64 | case LTTCOMM_CONSUMERD_OUTFD_ERROR: | |
65 | return "consumerd error opening the tracefile"; | |
66 | case LTTCOMM_CONSUMERD_SPLICE_EBADF: | |
67 | return "consumerd splice EBADF"; | |
68 | case LTTCOMM_CONSUMERD_SPLICE_EINVAL: | |
69 | return "consumerd splice EINVAL"; | |
70 | case LTTCOMM_CONSUMERD_SPLICE_ENOMEM: | |
71 | return "consumerd splice ENOMEM"; | |
72 | case LTTCOMM_CONSUMERD_SPLICE_ESPIPE: | |
73 | return "consumerd splice ESPIPE"; | |
74 | case LTTCOMM_CONSUMERD_ENOMEM: | |
75 | return "consumerd is out of memory"; | |
76 | case LTTCOMM_CONSUMERD_ERROR_METADATA: | |
77 | return "consumerd error with metadata"; | |
78 | case LTTCOMM_CONSUMERD_FATAL: | |
79 | return "consumerd fatal error"; | |
80 | case LTTCOMM_CONSUMERD_RELAYD_FAIL: | |
81 | return "consumerd error on remote relayd"; | |
82 | case LTTCOMM_CONSUMERD_CHANNEL_FAIL: | |
83 | return "consumerd channel creation fail"; | |
84 | case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND: | |
85 | return "consumerd channel not found"; | |
86 | case LTTCOMM_CONSUMERD_ALREADY_SET: | |
87 | return "consumerd resource already set"; | |
88 | case LTTCOMM_CONSUMERD_ROTATION_FAIL: | |
89 | return "consumerd rotation failed"; | |
90 | case LTTCOMM_CONSUMERD_SNAPSHOT_FAILED: | |
91 | return "consumerd snapshot has failed"; | |
92 | case LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED: | |
93 | return "consumerd trace chunk creation failed"; | |
94 | case LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED: | |
95 | return "consumerd trace chunk closing failed"; | |
96 | case LTTCOMM_CONSUMERD_INVALID_PARAMETERS: | |
97 | return "consumerd invalid parameters"; | |
98 | case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL: | |
99 | return "consumerd trace chunk exists on consumer daemon"; | |
100 | case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE: | |
101 | return "consumedd trace chunk exists on relay daemon"; | |
102 | case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK: | |
103 | return "consumerd unknown trace chunk"; | |
104 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
105 | return "consumed relayd does not accept clear command"; | |
106 | case LTTCOMM_CONSUMERD_UNKNOWN_ERROR: | |
107 | return "consumerd unknown error"; | |
108 | case LTTCOMM_NR: | |
109 | abort(); | |
110 | } | |
111 | ||
112 | abort(); | |
fac6795d DG |
113 | }; |
114 | ||
554831e7 MD |
115 | static unsigned long network_timeout; |
116 | ||
fac6795d | 117 | /* |
917216f6 DG |
118 | * Return ptr to string representing a human readable error code from the |
119 | * lttcomm_return_code enum. | |
fac6795d | 120 | * |
917216f6 | 121 | * These code MUST be negative in other to treat that as an error value. |
fac6795d DG |
122 | */ |
123 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
124 | { | |
64efa44e | 125 | code = (lttcomm_return_code) -code; |
fac6795d | 126 | |
64efa44e SM |
127 | if (code != LTTCOMM_CONSUMERD_SUCCESS && |
128 | !(code >= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code < LTTCOMM_NR)) { | |
129 | code = LTTCOMM_CONSUMERD_UNKNOWN_ERROR; | |
fac6795d DG |
130 | } |
131 | ||
64efa44e | 132 | return lttcomm_return_code_str(code); |
fac6795d | 133 | } |
6364a07a DG |
134 | |
135 | /* | |
de5e9086 DG |
136 | * Create socket from an already allocated lttcomm socket structure and init |
137 | * sockaddr in the lttcomm sock. | |
6364a07a | 138 | */ |
de5e9086 | 139 | int lttcomm_create_sock(struct lttcomm_sock *sock) |
6364a07a | 140 | { |
de5e9086 | 141 | int ret, _sock_type, _sock_proto, domain; |
6364a07a | 142 | |
a0377dfe | 143 | LTTNG_ASSERT(sock); |
de5e9086 DG |
144 | |
145 | domain = sock->sockaddr.type; | |
146 | if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) { | |
147 | ERR("Create socket of unknown domain %d", domain); | |
148 | ret = -1; | |
149 | goto error; | |
6364a07a DG |
150 | } |
151 | ||
de5e9086 DG |
152 | switch (sock->proto) { |
153 | case LTTCOMM_SOCK_UDP: | |
154 | _sock_type = SOCK_DGRAM; | |
155 | _sock_proto = IPPROTO_UDP; | |
156 | break; | |
157 | case LTTCOMM_SOCK_TCP: | |
158 | _sock_type = SOCK_STREAM; | |
159 | _sock_proto = IPPROTO_TCP; | |
160 | break; | |
161 | default: | |
162 | ret = -1; | |
163 | goto error; | |
164 | } | |
6364a07a | 165 | |
de5e9086 DG |
166 | ret = net_families[domain].create(sock, _sock_type, _sock_proto); |
167 | if (ret < 0) { | |
168 | goto error; | |
169 | } | |
170 | ||
171 | error: | |
172 | return ret; | |
6364a07a DG |
173 | } |
174 | ||
175 | /* | |
de5e9086 | 176 | * Return allocated lttcomm socket structure. |
6364a07a | 177 | */ |
de5e9086 | 178 | struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) |
6364a07a | 179 | { |
de5e9086 | 180 | struct lttcomm_sock *sock; |
6364a07a | 181 | |
64efa44e | 182 | sock = (lttcomm_sock *) zmalloc(sizeof(lttcomm_sock)); |
de5e9086 DG |
183 | if (sock == NULL) { |
184 | PERROR("zmalloc create sock"); | |
185 | goto end; | |
6364a07a DG |
186 | } |
187 | ||
188 | sock->proto = proto; | |
de5e9086 | 189 | sock->fd = -1; |
6364a07a | 190 | |
de5e9086 DG |
191 | end: |
192 | return sock; | |
6364a07a DG |
193 | } |
194 | ||
195 | /* | |
de5e9086 DG |
196 | * Return an allocated lttcomm socket structure and copy src content into |
197 | * the newly created socket. | |
198 | * | |
199 | * This is mostly useful when lttcomm_sock are passed between process where the | |
200 | * fd and ops have to be changed within the correct address space. | |
6364a07a | 201 | */ |
de5e9086 | 202 | struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src) |
6364a07a | 203 | { |
6364a07a DG |
204 | struct lttcomm_sock *sock; |
205 | ||
de5e9086 | 206 | /* Safety net */ |
a0377dfe | 207 | LTTNG_ASSERT(src); |
de5e9086 DG |
208 | |
209 | sock = lttcomm_alloc_sock(src->proto); | |
6364a07a DG |
210 | if (sock == NULL) { |
211 | goto alloc_error; | |
212 | } | |
213 | ||
de5e9086 | 214 | lttcomm_copy_sock(sock, src); |
6364a07a | 215 | |
de5e9086 | 216 | alloc_error: |
6364a07a | 217 | return sock; |
de5e9086 | 218 | } |
6364a07a | 219 | |
de5e9086 DG |
220 | /* |
221 | * Create and copy socket from an allocated lttcomm socket structure. | |
222 | * | |
223 | * This is mostly useful when lttcomm_sock are passed between process where the | |
224 | * fd and ops have to be changed within the correct address space. | |
225 | */ | |
226 | void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src) | |
227 | { | |
228 | /* Safety net */ | |
a0377dfe FD |
229 | LTTNG_ASSERT(dst); |
230 | LTTNG_ASSERT(src); | |
de5e9086 DG |
231 | |
232 | dst->proto = src->proto; | |
233 | dst->fd = src->fd; | |
234 | dst->ops = src->ops; | |
235 | /* Copy sockaddr information from original socket */ | |
236 | memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr)); | |
6364a07a DG |
237 | } |
238 | ||
239 | /* | |
240 | * Init IPv4 sockaddr structure. | |
241 | */ | |
242 | int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, | |
243 | const char *ip, unsigned int port) | |
244 | { | |
245 | int ret; | |
246 | ||
a0377dfe FD |
247 | LTTNG_ASSERT(sockaddr); |
248 | LTTNG_ASSERT(ip); | |
249 | LTTNG_ASSERT(port > 0 && port <= 65535); | |
6364a07a DG |
250 | |
251 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
252 | ||
253 | sockaddr->type = LTTCOMM_INET; | |
254 | sockaddr->addr.sin.sin_family = AF_INET; | |
255 | sockaddr->addr.sin.sin_port = htons(port); | |
256 | ret = inet_pton(sockaddr->addr.sin.sin_family, ip, | |
257 | &sockaddr->addr.sin.sin_addr); | |
258 | if (ret < 1) { | |
259 | ret = -1; | |
de5e9086 | 260 | ERR("%s with port %d: unrecognized IPv4 address", ip, port); |
6364a07a DG |
261 | goto error; |
262 | } | |
263 | memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero)); | |
264 | ||
265 | error: | |
266 | return ret; | |
267 | } | |
268 | ||
269 | /* | |
270 | * Init IPv6 sockaddr structure. | |
271 | */ | |
272 | int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, | |
273 | const char *ip, unsigned int port) | |
274 | { | |
275 | int ret; | |
276 | ||
a0377dfe FD |
277 | LTTNG_ASSERT(sockaddr); |
278 | LTTNG_ASSERT(ip); | |
279 | LTTNG_ASSERT(port > 0 && port <= 65535); | |
6364a07a DG |
280 | |
281 | memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); | |
282 | ||
283 | sockaddr->type = LTTCOMM_INET6; | |
284 | sockaddr->addr.sin6.sin6_family = AF_INET6; | |
285 | sockaddr->addr.sin6.sin6_port = htons(port); | |
286 | ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip, | |
287 | &sockaddr->addr.sin6.sin6_addr); | |
288 | if (ret < 1) { | |
289 | ret = -1; | |
290 | goto error; | |
291 | } | |
292 | ||
293 | error: | |
294 | return ret; | |
295 | } | |
de5e9086 DG |
296 | |
297 | /* | |
298 | * Return allocated lttcomm socket structure from lttng URI. | |
299 | */ | |
300 | struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) | |
301 | { | |
302 | int ret; | |
303 | int _sock_proto; | |
304 | struct lttcomm_sock *sock = NULL; | |
305 | ||
306 | /* Safety net */ | |
a0377dfe | 307 | LTTNG_ASSERT(uri); |
de5e9086 DG |
308 | |
309 | /* Check URI protocol */ | |
310 | if (uri->proto == LTTNG_TCP) { | |
311 | _sock_proto = LTTCOMM_SOCK_TCP; | |
312 | } else { | |
313 | ERR("Relayd invalid URI proto: %d", uri->proto); | |
314 | goto alloc_error; | |
315 | } | |
316 | ||
64efa44e | 317 | sock = lttcomm_alloc_sock((lttcomm_sock_proto) _sock_proto); |
de5e9086 DG |
318 | if (sock == NULL) { |
319 | goto alloc_error; | |
320 | } | |
321 | ||
322 | /* Check destination type */ | |
323 | if (uri->dtype == LTTNG_DST_IPV4) { | |
324 | ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4, | |
325 | uri->port); | |
326 | if (ret < 0) { | |
327 | goto error; | |
328 | } | |
329 | } else if (uri->dtype == LTTNG_DST_IPV6) { | |
330 | ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6, | |
331 | uri->port); | |
332 | if (ret < 0) { | |
333 | goto error; | |
334 | } | |
335 | } else { | |
336 | /* Command URI is invalid */ | |
337 | ERR("Relayd invalid URI dst type: %d", uri->dtype); | |
338 | goto error; | |
339 | } | |
340 | ||
341 | return sock; | |
342 | ||
343 | error: | |
344 | lttcomm_destroy_sock(sock); | |
345 | alloc_error: | |
346 | return NULL; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Destroy and free lttcomm socket. | |
351 | */ | |
352 | void lttcomm_destroy_sock(struct lttcomm_sock *sock) | |
353 | { | |
0e428499 | 354 | free(sock); |
de5e9086 | 355 | } |
6151a90f JD |
356 | |
357 | /* | |
358 | * Allocate and return a relayd socket object using a given URI to initialize | |
359 | * it and the major/minor version of the supported protocol. | |
360 | * | |
361 | * On error, NULL is returned. | |
362 | */ | |
363 | struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri, | |
364 | uint32_t major, uint32_t minor) | |
365 | { | |
366 | int ret; | |
367 | struct lttcomm_sock *tmp_sock = NULL; | |
368 | struct lttcomm_relayd_sock *rsock = NULL; | |
369 | ||
a0377dfe | 370 | LTTNG_ASSERT(uri); |
6151a90f | 371 | |
64efa44e | 372 | rsock = (lttcomm_relayd_sock *) zmalloc(sizeof(*rsock)); |
6151a90f JD |
373 | if (!rsock) { |
374 | PERROR("zmalloc relayd sock"); | |
375 | goto error; | |
376 | } | |
377 | ||
378 | /* Allocate socket object from URI */ | |
379 | tmp_sock = lttcomm_alloc_sock_from_uri(uri); | |
380 | if (tmp_sock == NULL) { | |
381 | goto error_free; | |
382 | } | |
383 | ||
384 | /* | |
385 | * Create socket object which basically sets the ops according to the | |
386 | * socket protocol. | |
387 | */ | |
388 | lttcomm_copy_sock(&rsock->sock, tmp_sock); | |
389 | /* Temporary socket pointer not needed anymore. */ | |
390 | lttcomm_destroy_sock(tmp_sock); | |
391 | ret = lttcomm_create_sock(&rsock->sock); | |
392 | if (ret < 0) { | |
393 | goto error_free; | |
394 | } | |
395 | ||
396 | rsock->major = major; | |
397 | rsock->minor = minor; | |
398 | ||
399 | return rsock; | |
400 | ||
401 | error_free: | |
402 | free(rsock); | |
403 | error: | |
404 | return NULL; | |
405 | } | |
554831e7 | 406 | |
783a3b9a MD |
407 | /* |
408 | * Set socket receiving timeout. | |
409 | */ | |
783a3b9a MD |
410 | int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec) |
411 | { | |
412 | int ret; | |
413 | struct timeval tv; | |
414 | ||
415 | tv.tv_sec = msec / 1000; | |
416 | tv.tv_usec = (msec % 1000) * 1000; | |
417 | ||
418 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
419 | if (ret < 0) { | |
420 | PERROR("setsockopt SO_RCVTIMEO"); | |
421 | } | |
422 | ||
423 | return ret; | |
424 | } | |
425 | ||
426 | /* | |
427 | * Set socket sending timeout. | |
428 | */ | |
783a3b9a MD |
429 | int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec) |
430 | { | |
431 | int ret; | |
432 | struct timeval tv; | |
433 | ||
434 | tv.tv_sec = msec / 1000; | |
435 | tv.tv_usec = (msec % 1000) * 1000; | |
436 | ||
437 | ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
438 | if (ret < 0) { | |
439 | PERROR("setsockopt SO_SNDTIMEO"); | |
440 | } | |
441 | ||
442 | return ret; | |
443 | } | |
444 | ||
2288467f JG |
445 | int lttcomm_sock_get_port(const struct lttcomm_sock *sock, uint16_t *port) |
446 | { | |
a0377dfe FD |
447 | LTTNG_ASSERT(sock); |
448 | LTTNG_ASSERT(port); | |
449 | LTTNG_ASSERT(sock->sockaddr.type == LTTCOMM_INET || | |
2288467f | 450 | sock->sockaddr.type == LTTCOMM_INET6); |
a0377dfe | 451 | LTTNG_ASSERT(sock->proto == LTTCOMM_SOCK_TCP || |
2288467f JG |
452 | sock->proto == LTTCOMM_SOCK_UDP); |
453 | ||
454 | switch (sock->sockaddr.type) { | |
455 | case LTTCOMM_INET: | |
456 | *port = ntohs(sock->sockaddr.addr.sin.sin_port); | |
457 | break; | |
458 | case LTTCOMM_INET6: | |
459 | *port = ntohs(sock->sockaddr.addr.sin6.sin6_port); | |
460 | break; | |
461 | default: | |
462 | abort(); | |
463 | } | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
2288467f JG |
468 | int lttcomm_sock_set_port(struct lttcomm_sock *sock, uint16_t port) |
469 | { | |
a0377dfe FD |
470 | LTTNG_ASSERT(sock); |
471 | LTTNG_ASSERT(sock->sockaddr.type == LTTCOMM_INET || | |
2288467f | 472 | sock->sockaddr.type == LTTCOMM_INET6); |
a0377dfe | 473 | LTTNG_ASSERT(sock->proto == LTTCOMM_SOCK_TCP || |
2288467f JG |
474 | sock->proto == LTTCOMM_SOCK_UDP); |
475 | ||
476 | switch (sock->sockaddr.type) { | |
477 | case LTTCOMM_INET: | |
478 | sock->sockaddr.addr.sin.sin_port = htons(port); | |
479 | break; | |
480 | case LTTCOMM_INET6: | |
481 | sock->sockaddr.addr.sin6.sin6_port = htons(port); | |
482 | break; | |
483 | default: | |
484 | abort(); | |
485 | } | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
554831e7 MD |
490 | void lttcomm_init(void) |
491 | { | |
492 | const char *env; | |
493 | ||
494 | env = getenv(NETWORK_TIMEOUT_ENV); | |
495 | if (env) { | |
496 | long timeout; | |
497 | ||
498 | errno = 0; | |
499 | timeout = strtol(env, NULL, 0); | |
500 | if (errno != 0 || timeout < -1L) { | |
501 | PERROR("Network timeout"); | |
502 | } else { | |
503 | if (timeout > 0) { | |
504 | network_timeout = timeout; | |
505 | } | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
554831e7 MD |
510 | unsigned long lttcomm_get_network_timeout(void) |
511 | { | |
512 | return network_timeout; | |
513 | } |