inet: fix: possible unaligned access in packed structure (inet/inet6)
[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 iov[0].iov_base += ret;
380 iov[0].iov_len -= ret;
381 assert(ret <= len_last);
382 }
383 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
384 if (ret < 0) {
385 PERROR("recvmsg inet");
386 } else if (ret > 0) {
387 ret = len;
388 }
389 /* Else ret = 0 meaning an orderly shutdown. */
390
391 return ret;
392 }
393
394 /*
395 * Send buf data of size len. Using sendmsg API.
396 *
397 * Return the size of sent data.
398 */
399 LTTNG_HIDDEN
400 ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
401 size_t len, int flags)
402 {
403 struct msghdr msg;
404 struct iovec iov[1];
405 ssize_t ret = -1;
406
407 memset(&msg, 0, sizeof(msg));
408
409 iov[0].iov_base = (void *) buf;
410 iov[0].iov_len = len;
411 msg.msg_iov = iov;
412 msg.msg_iovlen = 1;
413
414 switch (sock->proto) {
415 case LTTCOMM_SOCK_UDP:
416 {
417 struct sockaddr_in addr = sock->sockaddr.addr.sin;
418
419 msg.msg_name = (struct sockaddr *) &addr;
420 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
421 break;
422 }
423 default:
424 break;
425 }
426
427 do {
428 ret = sendmsg(sock->fd, &msg, flags);
429 } while (ret < 0 && errno == EINTR);
430 if (ret < 0) {
431 /*
432 * Only warn about EPIPE when quiet mode is deactivated.
433 * We consider EPIPE as expected.
434 */
435 if (errno != EPIPE || !lttng_opt_quiet) {
436 PERROR("sendmsg inet");
437 }
438 }
439
440 return ret;
441 }
442
443 /*
444 * Shutdown cleanly and close.
445 */
446 LTTNG_HIDDEN
447 int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
448 {
449 int ret;
450
451 /* Don't try to close an invalid marked socket */
452 if (sock->fd == -1) {
453 return 0;
454 }
455
456 ret = close(sock->fd);
457 if (ret) {
458 PERROR("close inet");
459 }
460
461 /* Mark socket */
462 sock->fd = -1;
463
464 return ret;
465 }
466
467 /*
468 * Return value read from /proc or else 0 if value is not found.
469 */
470 static unsigned long read_proc_value(const char *path)
471 {
472 int ret, fd;
473 ssize_t size_ret;
474 long r_val;
475 unsigned long val = 0;
476 char buf[64];
477
478 fd = open(path, O_RDONLY);
479 if (fd < 0) {
480 goto error;
481 }
482
483 size_ret = lttng_read(fd, buf, sizeof(buf));
484 /*
485 * Allow reading a file smaller than buf, but keep space for
486 * final \0.
487 */
488 if (size_ret < 0 || size_ret >= sizeof(buf)) {
489 PERROR("read proc failed");
490 goto error_close;
491 }
492 buf[size_ret] = '\0';
493
494 errno = 0;
495 r_val = strtol(buf, NULL, 10);
496 if (errno != 0 || r_val < -1L) {
497 val = 0;
498 goto error_close;
499 } else {
500 if (r_val > 0) {
501 val = r_val;
502 }
503 }
504
505 error_close:
506 ret = close(fd);
507 if (ret) {
508 PERROR("close /proc value");
509 }
510 error:
511 return val;
512 }
513
514 LTTNG_HIDDEN
515 void lttcomm_inet_init(void)
516 {
517 unsigned long syn_retries, fin_timeout, syn_timeout, env;
518
519 env = lttcomm_get_network_timeout();
520 if (env) {
521 lttcomm_inet_tcp_timeout = env;
522 goto end;
523 }
524
525 /* Assign default value and see if we can change it. */
526 lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT;
527
528 syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH);
529 fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH);
530
531 syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR;
532
533 /*
534 * Get the maximum between the two possible timeout value and use that to
535 * get the maximum with the default timeout.
536 */
537 lttcomm_inet_tcp_timeout = max_t(unsigned long,
538 max_t(unsigned long, syn_timeout, fin_timeout),
539 lttcomm_inet_tcp_timeout);
540
541 end:
542 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
543 }
This page took 0.039695 seconds and 4 git commands to generate.