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