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