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