Port: Use LTTNG_HOST_NAME_MAX instead of HOST_NAME_MAX
[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 _GNU_SOURCE
19 #define _LGPL_SOURCE
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>
29 #include <fcntl.h>
30 #include <time.h>
31 #include <poll.h>
32
33 #include <common/common.h>
34
35 #include "inet.h"
36
37 #define MSEC_PER_SEC 1000
38 #define NSEC_PER_MSEC 1000000
39 #define RECONNECT_DELAY 200 /* ms */
40
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
54 unsigned long lttcomm_inet_tcp_timeout;
55
56 /*
57 * Creates an PF_INET socket.
58 */
59 LTTNG_HIDDEN
60 int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
61 {
62 int val = 1, ret;
63 unsigned long timeout;
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 }
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 }
92
93 return 0;
94
95 error:
96 return -1;
97 }
98
99 /*
100 * Bind socket and return.
101 */
102 LTTNG_HIDDEN
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
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
240 /*
241 * Connect PF_INET socket.
242 */
243 LTTNG_HIDDEN
244 int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
245 {
246 int ret, closeret;
247
248 if (lttcomm_get_network_timeout()) {
249 ret = connect_with_timeout(sock);
250 } else {
251 ret = connect_no_timeout(sock);
252 }
253 if (ret < 0) {
254 PERROR("connect");
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 */
273 LTTNG_HIDDEN
274 struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
275 {
276 int new_fd;
277 socklen_t len;
278 struct lttcomm_sock *new_sock;
279 unsigned long timeout;
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
289 new_sock = lttcomm_alloc_sock(sock->proto);
290 if (new_sock == NULL) {
291 goto error;
292 }
293
294 len = sizeof(new_sock->sockaddr.addr.sin);
295
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 }
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) {
309 goto error_close;
310 }
311 ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout);
312 if (ret) {
313 goto error_close;
314 }
315 }
316
317 new_sock->fd = new_fd;
318 new_sock->ops = &inet_ops;
319
320 end:
321 return new_sock;
322
323 error_close:
324 if (close(new_fd) < 0) {
325 PERROR("accept inet close fd");
326 }
327
328 error:
329 free(new_sock);
330 return NULL;
331 }
332
333 /*
334 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
335 */
336 LTTNG_HIDDEN
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 */
367 LTTNG_HIDDEN
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;
374 size_t len_last;
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
386 do {
387 len_last = iov[0].iov_len;
388 ret = recvmsg(sock->fd, &msg, flags);
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));
395 if (ret < 0) {
396 PERROR("recvmsg inet");
397 } else if (ret > 0) {
398 ret = len;
399 }
400 /* Else ret = 0 meaning an orderly shutdown. */
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 */
410 LTTNG_HIDDEN
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 */
453 LTTNG_HIDDEN
454 int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
455 {
456 int ret;
457
458 /* Don't try to close an invalid marked socket */
459 if (sock->fd == -1) {
460 return 0;
461 }
462
463 ret = close(sock->fd);
464 if (ret) {
465 PERROR("close inet");
466 }
467
468 /* Mark socket */
469 sock->fd = -1;
470
471 return ret;
472 }
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;
480 ssize_t size_ret;
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
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)) {
496 PERROR("read proc failed");
497 goto error_close;
498 }
499 buf[size_ret] = '\0';
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 {
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 }
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
548 end:
549 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
550 }
This page took 0.040146 seconds and 4 git commands to generate.