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