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