relayd: Implement get_configuration relayd protocol command
[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 return bind(sock->fd,
104 (const struct sockaddr *) ALIGNED_CONST_PTR(
105 sock->sockaddr.addr.sin),
106 sizeof(sock->sockaddr.addr.sin));
107 }
108
109 static
110 int connect_no_timeout(struct lttcomm_sock *sock)
111 {
112 return connect(sock->fd,
113 (const struct sockaddr *) ALIGNED_CONST_PTR(
114 sock->sockaddr.addr.sin),
115 sizeof(sock->sockaddr.addr.sin));
116 }
117
118 static
119 int 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;
124 unsigned long diff_ms;
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
140 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
141 if (ret == -1) {
142 PERROR("clock_gettime");
143 return -1;
144 }
145
146 connect_ret = connect(sock->fd,
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) {
152 goto error;
153 } else if (!connect_ret) {
154 /* Connect succeeded */
155 goto success;
156 }
157
158 DBG("Asynchronous connect for sock %d, performing polling with"
159 " timeout: %lums", sock->fd, timeout);
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) {
186 PERROR("getsockopt");
187 goto error;
188 }
189 if (!optval) {
190 connect_ret = 0;
191 goto success;
192 } else {
193 /* Get actual connect() errno from opt_val */
194 errno = optval;
195 goto error;
196 }
197 }
198 /* ret == 0: timeout */
199 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
200 if (ret == -1) {
201 PERROR("clock_gettime");
202 connect_ret = ret;
203 goto error;
204 }
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);
211
212 /* Timeout */
213 errno = ETIMEDOUT;
214 connect_ret = -1;
215
216 success:
217 /* Restore initial flags */
218 ret = fcntl(sock->fd, F_SETFL, flags);
219 if (ret == -1) {
220 PERROR("fcntl");
221 /* Continue anyway */
222 }
223 error:
224 return connect_ret;
225 }
226
227 /*
228 * Connect PF_INET socket.
229 */
230 LTTNG_HIDDEN
231 int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
232 {
233 int ret, closeret;
234
235 if (lttcomm_get_network_timeout()) {
236 ret = connect_with_timeout(sock);
237 } else {
238 ret = connect_no_timeout(sock);
239 }
240 if (ret < 0) {
241 PERROR("connect");
242 goto error_connect;
243 }
244
245 return ret;
246
247 error_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 */
260 LTTNG_HIDDEN
261 struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
262 {
263 int new_fd;
264 socklen_t len;
265 struct lttcomm_sock *new_sock;
266 unsigned long timeout;
267 struct sockaddr_in new_addr = {};
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
277 new_sock = lttcomm_alloc_sock(sock->proto);
278 if (new_sock == NULL) {
279 goto error;
280 }
281
282 len = sizeof(new_addr);
283
284 /* Blocking call */
285 new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len);
286 if (new_fd < 0) {
287 PERROR("accept inet");
288 goto error;
289 }
290 new_sock->sockaddr.addr.sin = new_addr;
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) {
297 goto error_close;
298 }
299 ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout);
300 if (ret) {
301 goto error_close;
302 }
303 }
304
305 new_sock->fd = new_fd;
306 new_sock->ops = &inet_ops;
307
308 end:
309 return new_sock;
310
311 error_close:
312 if (close(new_fd) < 0) {
313 PERROR("accept inet close fd");
314 }
315
316 error:
317 free(new_sock);
318 return NULL;
319 }
320
321 /*
322 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
323 */
324 LTTNG_HIDDEN
325 int 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
345 end:
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 */
355 LTTNG_HIDDEN
356 ssize_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;
362 size_t len_last;
363 struct sockaddr_in addr = sock->sockaddr.addr.sin;
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
372 msg.msg_name = (struct sockaddr *) &addr;
373 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
374
375 do {
376 len_last = iov[0].iov_len;
377 ret = recvmsg(sock->fd, &msg, flags);
378 if (ret > 0) {
379 if (flags & MSG_DONTWAIT) {
380 goto end;
381 }
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));
387
388 if (ret < 0) {
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 }
398 PERROR("recvmsg inet");
399 } else if (ret > 0) {
400 ret = len;
401 }
402 /* Else ret = 0 meaning an orderly shutdown. */
403 end:
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 */
412 LTTNG_HIDDEN
413 ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
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
422 iov[0].iov_base = (void *) buf;
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:
429 {
430 struct sockaddr_in addr = sock->sockaddr.addr.sin;
431
432 msg.msg_name = (struct sockaddr *) &addr;
433 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
434 break;
435 }
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 */
459 LTTNG_HIDDEN
460 int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
461 {
462 int ret;
463
464 /* Don't try to close an invalid marked socket */
465 if (sock->fd == -1) {
466 return 0;
467 }
468
469 ret = close(sock->fd);
470 if (ret) {
471 PERROR("close inet");
472 }
473
474 /* Mark socket */
475 sock->fd = -1;
476
477 return ret;
478 }
479
480 /*
481 * Return value read from /proc or else 0 if value is not found.
482 */
483 static unsigned long read_proc_value(const char *path)
484 {
485 int ret, fd;
486 ssize_t size_ret;
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
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)) {
502 PERROR("read proc failed");
503 goto error_close;
504 }
505 buf[size_ret] = '\0';
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
518 error_close:
519 ret = close(fd);
520 if (ret) {
521 PERROR("close /proc value");
522 }
523 error:
524 return val;
525 }
526
527 LTTNG_HIDDEN
528 void lttcomm_inet_init(void)
529 {
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 }
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
554 end:
555 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
556 }
This page took 0.040412 seconds and 4 git commands to generate.