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