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