Commit | Line | Data |
---|---|---|
6364a07a | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
6364a07a | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
6364a07a | 5 | * |
6364a07a DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
6364a07a DG |
9 | #include <limits.h> |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/types.h> | |
15 | #include <unistd.h> | |
a655f4cf | 16 | #include <fcntl.h> |
389fbf04 | 17 | #include <common/compat/time.h> |
a655f4cf | 18 | #include <poll.h> |
6364a07a | 19 | |
90e535ef | 20 | #include <common/common.h> |
395d6b02 | 21 | #include <common/time.h> |
edf4b93e | 22 | #include <common/compat/errno.h> |
6364a07a DG |
23 | |
24 | #include "inet.h" | |
25 | ||
a655f4cf MD |
26 | #define RECONNECT_DELAY 200 /* ms */ |
27 | ||
6364a07a DG |
28 | /* |
29 | * INET protocol operations. | |
30 | */ | |
31 | static const struct lttcomm_proto_ops inet_ops = { | |
32 | .bind = lttcomm_bind_inet_sock, | |
33 | .close = lttcomm_close_inet_sock, | |
34 | .connect = lttcomm_connect_inet_sock, | |
35 | .accept = lttcomm_accept_inet_sock, | |
36 | .listen = lttcomm_listen_inet_sock, | |
37 | .recvmsg = lttcomm_recvmsg_inet_sock, | |
38 | .sendmsg = lttcomm_sendmsg_inet_sock, | |
39 | }; | |
40 | ||
d831c249 DG |
41 | unsigned long lttcomm_inet_tcp_timeout; |
42 | ||
6364a07a DG |
43 | /* |
44 | * Creates an PF_INET socket. | |
45 | */ | |
46 | int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto) | |
47 | { | |
de5e9086 | 48 | int val = 1, ret; |
783a3b9a | 49 | unsigned long timeout; |
6364a07a DG |
50 | |
51 | /* Create server socket */ | |
52 | if ((sock->fd = socket(PF_INET, type, proto)) < 0) { | |
53 | PERROR("socket inet"); | |
54 | goto error; | |
55 | } | |
56 | ||
57 | sock->ops = &inet_ops; | |
58 | ||
59 | /* | |
60 | * Set socket option to reuse the address. | |
61 | */ | |
62 | ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)); | |
63 | if (ret < 0) { | |
64 | PERROR("setsockopt inet"); | |
65 | goto error; | |
66 | } | |
783a3b9a MD |
67 | timeout = lttcomm_get_network_timeout(); |
68 | if (timeout) { | |
69 | ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout); | |
70 | if (ret) { | |
71 | goto error; | |
72 | } | |
73 | ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout); | |
74 | if (ret) { | |
75 | goto error; | |
76 | } | |
77 | } | |
6364a07a DG |
78 | |
79 | return 0; | |
80 | ||
81 | error: | |
82 | return -1; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Bind socket and return. | |
87 | */ | |
88 | int lttcomm_bind_inet_sock(struct lttcomm_sock *sock) | |
89 | { | |
2288467f | 90 | return bind(sock->fd, |
1a65bbb8 JG |
91 | (const struct sockaddr *) ALIGNED_CONST_PTR( |
92 | sock->sockaddr.addr.sin), | |
6364a07a | 93 | sizeof(sock->sockaddr.addr.sin)); |
6364a07a DG |
94 | } |
95 | ||
a655f4cf MD |
96 | static |
97 | int connect_no_timeout(struct lttcomm_sock *sock) | |
98 | { | |
1a65bbb8 JG |
99 | return connect(sock->fd, |
100 | (const struct sockaddr *) ALIGNED_CONST_PTR( | |
101 | sock->sockaddr.addr.sin), | |
a655f4cf MD |
102 | sizeof(sock->sockaddr.addr.sin)); |
103 | } | |
104 | ||
a655f4cf MD |
105 | static |
106 | int connect_with_timeout(struct lttcomm_sock *sock) | |
107 | { | |
108 | unsigned long timeout = lttcomm_get_network_timeout(); | |
109 | int ret, flags, connect_ret; | |
110 | struct timespec orig_time, cur_time; | |
2daf6502 | 111 | unsigned long diff_ms; |
a655f4cf MD |
112 | |
113 | ret = fcntl(sock->fd, F_GETFL, 0); | |
114 | if (ret == -1) { | |
115 | PERROR("fcntl"); | |
116 | return -1; | |
117 | } | |
118 | flags = ret; | |
119 | ||
120 | /* Set socket to nonblock */ | |
121 | ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK); | |
122 | if (ret == -1) { | |
123 | PERROR("fcntl"); | |
124 | return -1; | |
125 | } | |
126 | ||
389fbf04 | 127 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); |
a655f4cf MD |
128 | if (ret == -1) { |
129 | PERROR("clock_gettime"); | |
130 | return -1; | |
131 | } | |
132 | ||
133 | connect_ret = connect(sock->fd, | |
1a65bbb8 JG |
134 | (const struct sockaddr *) ALIGNED_CONST_PTR( |
135 | sock->sockaddr.addr.sin), | |
136 | sizeof(sock->sockaddr.addr.sin)); | |
137 | if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK && | |
138 | errno != EINPROGRESS) { | |
a655f4cf MD |
139 | goto error; |
140 | } else if (!connect_ret) { | |
141 | /* Connect succeeded */ | |
142 | goto success; | |
143 | } | |
144 | ||
48ac9596 JR |
145 | DBG("Asynchronous connect for sock %d, performing polling with" |
146 | " timeout: %lums", sock->fd, timeout); | |
a655f4cf MD |
147 | /* |
148 | * Perform poll loop following EINPROGRESS recommendation from | |
149 | * connect(2) man page. | |
150 | */ | |
151 | do { | |
152 | struct pollfd fds; | |
153 | ||
154 | fds.fd = sock->fd; | |
155 | fds.events = POLLOUT; | |
156 | fds.revents = 0; | |
157 | ret = poll(&fds, 1, RECONNECT_DELAY); | |
158 | if (ret < 0) { | |
159 | goto error; | |
160 | } else if (ret > 0) { | |
161 | int optval; | |
162 | socklen_t optval_len = sizeof(optval); | |
163 | ||
164 | if (!(fds.revents & POLLOUT)) { | |
165 | /* Either hup or error */ | |
166 | errno = EPIPE; | |
167 | goto error; | |
168 | } | |
169 | /* got something */ | |
170 | ret = getsockopt(sock->fd, SOL_SOCKET, | |
171 | SO_ERROR, &optval, &optval_len); | |
172 | if (ret) { | |
48ac9596 | 173 | PERROR("getsockopt"); |
a655f4cf MD |
174 | goto error; |
175 | } | |
176 | if (!optval) { | |
177 | connect_ret = 0; | |
178 | goto success; | |
179 | } else { | |
48ac9596 JR |
180 | /* Get actual connect() errno from opt_val */ |
181 | errno = optval; | |
a655f4cf MD |
182 | goto error; |
183 | } | |
184 | } | |
185 | /* ret == 0: timeout */ | |
389fbf04 | 186 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); |
a655f4cf MD |
187 | if (ret == -1) { |
188 | PERROR("clock_gettime"); | |
189 | connect_ret = ret; | |
190 | goto error; | |
191 | } | |
2daf6502 MD |
192 | if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) { |
193 | ERR("timespec_to_ms input overflows milliseconds output"); | |
194 | connect_ret = -1; | |
195 | goto error; | |
196 | } | |
197 | } while (diff_ms < timeout); | |
a655f4cf MD |
198 | |
199 | /* Timeout */ | |
200 | errno = ETIMEDOUT; | |
201 | connect_ret = -1; | |
202 | ||
203 | success: | |
204 | /* Restore initial flags */ | |
205 | ret = fcntl(sock->fd, F_SETFL, flags); | |
206 | if (ret == -1) { | |
207 | PERROR("fcntl"); | |
208 | /* Continue anyway */ | |
209 | } | |
210 | error: | |
211 | return connect_ret; | |
212 | } | |
213 | ||
6364a07a DG |
214 | /* |
215 | * Connect PF_INET socket. | |
216 | */ | |
217 | int lttcomm_connect_inet_sock(struct lttcomm_sock *sock) | |
218 | { | |
219 | int ret, closeret; | |
220 | ||
a655f4cf MD |
221 | if (lttcomm_get_network_timeout()) { |
222 | ret = connect_with_timeout(sock); | |
223 | } else { | |
224 | ret = connect_no_timeout(sock); | |
225 | } | |
6364a07a | 226 | if (ret < 0) { |
82c05d47 | 227 | PERROR("connect"); |
6364a07a DG |
228 | goto error_connect; |
229 | } | |
230 | ||
231 | return ret; | |
232 | ||
233 | error_connect: | |
234 | closeret = close(sock->fd); | |
235 | if (closeret) { | |
236 | PERROR("close inet"); | |
237 | } | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
242 | /* | |
243 | * Do an accept(2) on the sock and return the new lttcomm socket. The socket | |
244 | * MUST be bind(2) before. | |
245 | */ | |
246 | struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock) | |
247 | { | |
248 | int new_fd; | |
88a5db70 | 249 | socklen_t len; |
6364a07a | 250 | struct lttcomm_sock *new_sock; |
71c648d8 | 251 | unsigned long timeout; |
1a65bbb8 | 252 | struct sockaddr_in new_addr = {}; |
6364a07a DG |
253 | |
254 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
255 | /* | |
256 | * accept(2) does not exist for UDP so simply return the passed socket. | |
257 | */ | |
258 | new_sock = sock; | |
259 | goto end; | |
260 | } | |
261 | ||
de5e9086 | 262 | new_sock = lttcomm_alloc_sock(sock->proto); |
6364a07a DG |
263 | if (new_sock == NULL) { |
264 | goto error; | |
265 | } | |
266 | ||
1a65bbb8 | 267 | len = sizeof(new_addr); |
88a5db70 | 268 | |
6364a07a | 269 | /* Blocking call */ |
1a65bbb8 | 270 | new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len); |
6364a07a DG |
271 | if (new_fd < 0) { |
272 | PERROR("accept inet"); | |
273 | goto error; | |
274 | } | |
1a65bbb8 | 275 | new_sock->sockaddr.addr.sin = new_addr; |
71c648d8 JG |
276 | timeout = lttcomm_get_network_timeout(); |
277 | if (timeout) { | |
278 | int ret; | |
279 | ||
280 | ret = lttcomm_setsockopt_rcv_timeout(new_fd, timeout); | |
281 | if (ret) { | |
475cd9fa | 282 | goto error_close; |
71c648d8 JG |
283 | } |
284 | ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout); | |
285 | if (ret) { | |
475cd9fa | 286 | goto error_close; |
71c648d8 JG |
287 | } |
288 | } | |
6364a07a DG |
289 | |
290 | new_sock->fd = new_fd; | |
de5e9086 | 291 | new_sock->ops = &inet_ops; |
6364a07a DG |
292 | |
293 | end: | |
294 | return new_sock; | |
295 | ||
475cd9fa DG |
296 | error_close: |
297 | if (close(new_fd) < 0) { | |
298 | PERROR("accept inet close fd"); | |
299 | } | |
300 | ||
6364a07a DG |
301 | error: |
302 | free(new_sock); | |
303 | return NULL; | |
304 | } | |
305 | ||
306 | /* | |
307 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
308 | */ | |
309 | int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog) | |
310 | { | |
311 | int ret; | |
312 | ||
313 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
314 | /* listen(2) does not exist for UDP so simply return success. */ | |
315 | ret = 0; | |
316 | goto end; | |
317 | } | |
318 | ||
319 | /* Default listen backlog */ | |
320 | if (backlog <= 0) { | |
321 | backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN; | |
322 | } | |
323 | ||
324 | ret = listen(sock->fd, backlog); | |
325 | if (ret < 0) { | |
326 | PERROR("listen inet"); | |
327 | } | |
328 | ||
329 | end: | |
330 | return ret; | |
331 | } | |
332 | ||
333 | /* | |
334 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
335 | * API. | |
336 | * | |
337 | * Return the size of received data. | |
338 | */ | |
339 | ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf, | |
340 | size_t len, int flags) | |
341 | { | |
342 | struct msghdr msg; | |
343 | struct iovec iov[1]; | |
344 | ssize_t ret = -1; | |
7c5aef62 | 345 | size_t len_last; |
1a65bbb8 | 346 | struct sockaddr_in addr = sock->sockaddr.addr.sin; |
6364a07a DG |
347 | |
348 | memset(&msg, 0, sizeof(msg)); | |
349 | ||
350 | iov[0].iov_base = buf; | |
351 | iov[0].iov_len = len; | |
352 | msg.msg_iov = iov; | |
353 | msg.msg_iovlen = 1; | |
354 | ||
1a65bbb8 | 355 | msg.msg_name = (struct sockaddr *) &addr; |
6364a07a DG |
356 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
357 | ||
6364a07a | 358 | do { |
7c5aef62 | 359 | len_last = iov[0].iov_len; |
6364a07a | 360 | ret = recvmsg(sock->fd, &msg, flags); |
7c5aef62 | 361 | if (ret > 0) { |
5312a3ed JG |
362 | if (flags & MSG_DONTWAIT) { |
363 | goto end; | |
364 | } | |
7c5aef62 DG |
365 | iov[0].iov_base += ret; |
366 | iov[0].iov_len -= ret; | |
a0377dfe | 367 | LTTNG_ASSERT(ret <= len_last); |
7c5aef62 DG |
368 | } |
369 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
5312a3ed | 370 | |
6364a07a | 371 | if (ret < 0) { |
c2e8c366 JG |
372 | if (errno == EAGAIN && flags & MSG_DONTWAIT) { |
373 | /* | |
374 | * EAGAIN is expected in non-blocking mode and should | |
375 | * not be reported as an error. Moreover, if no data | |
376 | * was read, 0 must not be returned as it would be | |
377 | * interpreted as an orderly shutdown of the socket. | |
378 | */ | |
379 | goto end; | |
380 | } | |
6364a07a | 381 | PERROR("recvmsg inet"); |
7c5aef62 DG |
382 | } else if (ret > 0) { |
383 | ret = len; | |
6364a07a | 384 | } |
7c5aef62 | 385 | /* Else ret = 0 meaning an orderly shutdown. */ |
5312a3ed | 386 | end: |
6364a07a DG |
387 | return ret; |
388 | } | |
389 | ||
390 | /* | |
391 | * Send buf data of size len. Using sendmsg API. | |
392 | * | |
393 | * Return the size of sent data. | |
394 | */ | |
c2d69327 | 395 | ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf, |
6364a07a DG |
396 | size_t len, int flags) |
397 | { | |
398 | struct msghdr msg; | |
399 | struct iovec iov[1]; | |
400 | ssize_t ret = -1; | |
401 | ||
402 | memset(&msg, 0, sizeof(msg)); | |
403 | ||
c2d69327 | 404 | iov[0].iov_base = (void *) buf; |
6364a07a DG |
405 | iov[0].iov_len = len; |
406 | msg.msg_iov = iov; | |
407 | msg.msg_iovlen = 1; | |
408 | ||
409 | switch (sock->proto) { | |
410 | case LTTCOMM_SOCK_UDP: | |
1a65bbb8 JG |
411 | { |
412 | struct sockaddr_in addr = sock->sockaddr.addr.sin; | |
413 | ||
414 | msg.msg_name = (struct sockaddr *) &addr; | |
6364a07a DG |
415 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin); |
416 | break; | |
1a65bbb8 | 417 | } |
6364a07a DG |
418 | default: |
419 | break; | |
420 | } | |
421 | ||
422 | do { | |
423 | ret = sendmsg(sock->fd, &msg, flags); | |
424 | } while (ret < 0 && errno == EINTR); | |
425 | if (ret < 0) { | |
426 | /* | |
427 | * Only warn about EPIPE when quiet mode is deactivated. | |
428 | * We consider EPIPE as expected. | |
429 | */ | |
430 | if (errno != EPIPE || !lttng_opt_quiet) { | |
431 | PERROR("sendmsg inet"); | |
432 | } | |
433 | } | |
434 | ||
435 | return ret; | |
436 | } | |
437 | ||
438 | /* | |
439 | * Shutdown cleanly and close. | |
440 | */ | |
441 | int lttcomm_close_inet_sock(struct lttcomm_sock *sock) | |
442 | { | |
6e742359 | 443 | int ret; |
6364a07a | 444 | |
de5e9086 | 445 | /* Don't try to close an invalid marked socket */ |
6364a07a DG |
446 | if (sock->fd == -1) { |
447 | return 0; | |
448 | } | |
449 | ||
6e742359 DG |
450 | ret = close(sock->fd); |
451 | if (ret) { | |
6364a07a DG |
452 | PERROR("close inet"); |
453 | } | |
454 | ||
455 | /* Mark socket */ | |
456 | sock->fd = -1; | |
457 | ||
458 | return ret; | |
459 | } | |
d831c249 DG |
460 | |
461 | /* | |
462 | * Return value read from /proc or else 0 if value is not found. | |
463 | */ | |
464 | static unsigned long read_proc_value(const char *path) | |
465 | { | |
466 | int ret, fd; | |
6cd525e8 | 467 | ssize_t size_ret; |
d831c249 DG |
468 | long r_val; |
469 | unsigned long val = 0; | |
470 | char buf[64]; | |
471 | ||
472 | fd = open(path, O_RDONLY); | |
473 | if (fd < 0) { | |
474 | goto error; | |
475 | } | |
476 | ||
6cd525e8 MD |
477 | size_ret = lttng_read(fd, buf, sizeof(buf)); |
478 | /* | |
479 | * Allow reading a file smaller than buf, but keep space for | |
480 | * final \0. | |
481 | */ | |
482 | if (size_ret < 0 || size_ret >= sizeof(buf)) { | |
d831c249 DG |
483 | PERROR("read proc failed"); |
484 | goto error_close; | |
485 | } | |
6cd525e8 | 486 | buf[size_ret] = '\0'; |
d831c249 DG |
487 | |
488 | errno = 0; | |
489 | r_val = strtol(buf, NULL, 10); | |
490 | if (errno != 0 || r_val < -1L) { | |
491 | val = 0; | |
492 | goto error_close; | |
493 | } else { | |
494 | if (r_val > 0) { | |
495 | val = r_val; | |
496 | } | |
497 | } | |
498 | ||
499 | error_close: | |
500 | ret = close(fd); | |
501 | if (ret) { | |
502 | PERROR("close /proc value"); | |
503 | } | |
504 | error: | |
505 | return val; | |
506 | } | |
507 | ||
d831c249 DG |
508 | void lttcomm_inet_init(void) |
509 | { | |
18eed43f DG |
510 | unsigned long syn_retries, fin_timeout, syn_timeout, env; |
511 | ||
512 | env = lttcomm_get_network_timeout(); | |
513 | if (env) { | |
514 | lttcomm_inet_tcp_timeout = env; | |
515 | goto end; | |
516 | } | |
d831c249 DG |
517 | |
518 | /* Assign default value and see if we can change it. */ | |
519 | lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT; | |
520 | ||
521 | syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH); | |
522 | fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH); | |
523 | ||
524 | syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR; | |
525 | ||
526 | /* | |
527 | * Get the maximum between the two possible timeout value and use that to | |
528 | * get the maximum with the default timeout. | |
529 | */ | |
530 | lttcomm_inet_tcp_timeout = max_t(unsigned long, | |
531 | max_t(unsigned long, syn_timeout, fin_timeout), | |
532 | lttcomm_inet_tcp_timeout); | |
533 | ||
18eed43f | 534 | end: |
d831c249 DG |
535 | DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout); |
536 | } |