ed7f5dc165e1b3961a2f53ef3107a0f54db85472
[lttng-tools.git] / src / common / sessiond-comm / inet.c
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 _LGPL_SOURCE
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>
28 #include <fcntl.h>
29 #include <common/compat/time.h>
30 #include <poll.h>
31
32 #include <common/common.h>
33 #include <common/time.h>
34
35 #include "inet.h"
36
37 #define RECONNECT_DELAY 200 /* ms */
38
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
52 unsigned long lttcomm_inet_tcp_timeout;
53
54 /*
55 * Creates an PF_INET socket.
56 */
57 LTTNG_HIDDEN
58 int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
59 {
60 int val = 1, ret;
61 unsigned long timeout;
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 }
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 }
90
91 return 0;
92
93 error:
94 return -1;
95 }
96
97 /*
98 * Bind socket and return.
99 */
100 LTTNG_HIDDEN
101 int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
102 {
103 int ret;
104
105 ret = bind(sock->fd, (const struct sockaddr *) &sock->sockaddr.addr.sin,
106 sizeof(sock->sockaddr.addr.sin));
107 if (ret < 0) {
108 PERROR("bind inet");
109 }
110
111 return ret;
112 }
113
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
161 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
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
179 DBG("Asynchronous connect for sock %d, performing polling with"
180 " timeout: %lums", sock->fd, timeout);
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 PERROR("getsockopt");
208 goto error;
209 }
210 if (!optval) {
211 connect_ret = 0;
212 goto success;
213 } else {
214 /* Get actual connect() errno from opt_val */
215 errno = optval;
216 goto error;
217 }
218 }
219 /* ret == 0: timeout */
220 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
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
243 /*
244 * Connect PF_INET socket.
245 */
246 LTTNG_HIDDEN
247 int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
248 {
249 int ret, closeret;
250
251 if (lttcomm_get_network_timeout()) {
252 ret = connect_with_timeout(sock);
253 } else {
254 ret = connect_no_timeout(sock);
255 }
256 if (ret < 0) {
257 PERROR("connect");
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 */
276 LTTNG_HIDDEN
277 struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
278 {
279 int new_fd;
280 socklen_t len;
281 struct lttcomm_sock *new_sock;
282 unsigned long timeout;
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
292 new_sock = lttcomm_alloc_sock(sock->proto);
293 if (new_sock == NULL) {
294 goto error;
295 }
296
297 len = sizeof(new_sock->sockaddr.addr.sin);
298
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 }
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) {
312 goto error_close;
313 }
314 ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout);
315 if (ret) {
316 goto error_close;
317 }
318 }
319
320 new_sock->fd = new_fd;
321 new_sock->ops = &inet_ops;
322
323 end:
324 return new_sock;
325
326 error_close:
327 if (close(new_fd) < 0) {
328 PERROR("accept inet close fd");
329 }
330
331 error:
332 free(new_sock);
333 return NULL;
334 }
335
336 /*
337 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
338 */
339 LTTNG_HIDDEN
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 */
370 LTTNG_HIDDEN
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;
377 size_t len_last;
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
389 do {
390 len_last = iov[0].iov_len;
391 ret = recvmsg(sock->fd, &msg, flags);
392 if (ret > 0) {
393 iov[0].iov_base += ret;
394 iov[0].iov_len -= ret;
395 assert(ret <= len_last);
396 }
397 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
398 if (ret < 0) {
399 PERROR("recvmsg inet");
400 } else if (ret > 0) {
401 ret = len;
402 }
403 /* Else ret = 0 meaning an orderly shutdown. */
404
405 return ret;
406 }
407
408 /*
409 * Send buf data of size len. Using sendmsg API.
410 *
411 * Return the size of sent data.
412 */
413 LTTNG_HIDDEN
414 ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
415 size_t len, int flags)
416 {
417 struct msghdr msg;
418 struct iovec iov[1];
419 ssize_t ret = -1;
420
421 memset(&msg, 0, sizeof(msg));
422
423 iov[0].iov_base = (void *) buf;
424 iov[0].iov_len = len;
425 msg.msg_iov = iov;
426 msg.msg_iovlen = 1;
427
428 switch (sock->proto) {
429 case LTTCOMM_SOCK_UDP:
430 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin;
431 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
432 break;
433 default:
434 break;
435 }
436
437 do {
438 ret = sendmsg(sock->fd, &msg, flags);
439 } while (ret < 0 && errno == EINTR);
440 if (ret < 0) {
441 /*
442 * Only warn about EPIPE when quiet mode is deactivated.
443 * We consider EPIPE as expected.
444 */
445 if (errno != EPIPE || !lttng_opt_quiet) {
446 PERROR("sendmsg inet");
447 }
448 }
449
450 return ret;
451 }
452
453 /*
454 * Shutdown cleanly and close.
455 */
456 LTTNG_HIDDEN
457 int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
458 {
459 int ret;
460
461 /* Don't try to close an invalid marked socket */
462 if (sock->fd == -1) {
463 return 0;
464 }
465
466 ret = close(sock->fd);
467 if (ret) {
468 PERROR("close inet");
469 }
470
471 /* Mark socket */
472 sock->fd = -1;
473
474 return ret;
475 }
476
477 /*
478 * Return value read from /proc or else 0 if value is not found.
479 */
480 static unsigned long read_proc_value(const char *path)
481 {
482 int ret, fd;
483 ssize_t size_ret;
484 long r_val;
485 unsigned long val = 0;
486 char buf[64];
487
488 fd = open(path, O_RDONLY);
489 if (fd < 0) {
490 goto error;
491 }
492
493 size_ret = lttng_read(fd, buf, sizeof(buf));
494 /*
495 * Allow reading a file smaller than buf, but keep space for
496 * final \0.
497 */
498 if (size_ret < 0 || size_ret >= sizeof(buf)) {
499 PERROR("read proc failed");
500 goto error_close;
501 }
502 buf[size_ret] = '\0';
503
504 errno = 0;
505 r_val = strtol(buf, NULL, 10);
506 if (errno != 0 || r_val < -1L) {
507 val = 0;
508 goto error_close;
509 } else {
510 if (r_val > 0) {
511 val = r_val;
512 }
513 }
514
515 error_close:
516 ret = close(fd);
517 if (ret) {
518 PERROR("close /proc value");
519 }
520 error:
521 return val;
522 }
523
524 LTTNG_HIDDEN
525 void lttcomm_inet_init(void)
526 {
527 unsigned long syn_retries, fin_timeout, syn_timeout, env;
528
529 env = lttcomm_get_network_timeout();
530 if (env) {
531 lttcomm_inet_tcp_timeout = env;
532 goto end;
533 }
534
535 /* Assign default value and see if we can change it. */
536 lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT;
537
538 syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH);
539 fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH);
540
541 syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR;
542
543 /*
544 * Get the maximum between the two possible timeout value and use that to
545 * get the maximum with the default timeout.
546 */
547 lttcomm_inet_tcp_timeout = max_t(unsigned long,
548 max_t(unsigned long, syn_timeout, fin_timeout),
549 lttcomm_inet_tcp_timeout);
550
551 end:
552 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
553 }
This page took 0.038891 seconds and 3 git commands to generate.