Fix: tests: fix unused-but-set warning in test_fd_tracker.c
[lttng-tools.git] / src / common / sessiond-comm / inet6.c
CommitLineData
6364a07a 1/*
ab5be9fa 2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6364a07a 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
6364a07a 5 *
6364a07a
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
6364a07a
DG
9#include <limits.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
a655f4cf 16#include <fcntl.h>
389fbf04 17#include <common/compat/time.h>
a655f4cf 18#include <poll.h>
6364a07a 19
90e535ef 20#include <common/common.h>
395d6b02 21#include <common/time.h>
edf4b93e 22#include <common/compat/errno.h>
6364a07a
DG
23
24#include "inet6.h"
25
a655f4cf
MD
26#define RECONNECT_DELAY 200 /* ms */
27
6364a07a
DG
28/*
29 * INET protocol operations.
30 */
31static const struct lttcomm_proto_ops inet6_ops = {
32 .bind = lttcomm_bind_inet6_sock,
33 .close = lttcomm_close_inet6_sock,
34 .connect = lttcomm_connect_inet6_sock,
35 .accept = lttcomm_accept_inet6_sock,
36 .listen = lttcomm_listen_inet6_sock,
37 .recvmsg = lttcomm_recvmsg_inet6_sock,
38 .sendmsg = lttcomm_sendmsg_inet6_sock,
39};
40
41/*
42 * Creates an PF_INET socket.
43 */
44int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto)
45{
de5e9086 46 int val = 1, ret;
783a3b9a 47 unsigned long timeout;
6364a07a
DG
48
49 /* Create server socket */
7b43086d 50 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
6364a07a
DG
51 PERROR("socket inet6");
52 goto error;
53 }
54
55 sock->ops = &inet6_ops;
56
57 /*
58 * Set socket option to reuse the address.
59 */
60 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
61 if (ret < 0) {
62 PERROR("setsockopt inet6");
63 goto error;
64 }
783a3b9a
MD
65 timeout = lttcomm_get_network_timeout();
66 if (timeout) {
67 ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout);
68 if (ret) {
69 goto error;
70 }
71 ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout);
72 if (ret) {
73 goto error;
74 }
75 }
6364a07a
DG
76
77 return 0;
78
79error:
80 return -1;
81}
82
83/*
84 * Bind socket and return.
85 */
86int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock)
87{
2288467f 88 return bind(sock->fd,
1a65bbb8
JG
89 (const struct sockaddr *) ALIGNED_CONST_PTR(
90 sock->sockaddr.addr.sin6),
6364a07a 91 sizeof(sock->sockaddr.addr.sin6));
6364a07a
DG
92}
93
a655f4cf
MD
94static
95int connect_no_timeout(struct lttcomm_sock *sock)
96{
1a65bbb8
JG
97 return connect(sock->fd,
98 (const struct sockaddr *) ALIGNED_CONST_PTR(
99 sock->sockaddr.addr.sin6),
a655f4cf
MD
100 sizeof(sock->sockaddr.addr.sin6));
101}
102
a655f4cf
MD
103static
104int connect_with_timeout(struct lttcomm_sock *sock)
105{
106 unsigned long timeout = lttcomm_get_network_timeout();
107 int ret, flags, connect_ret;
108 struct timespec orig_time, cur_time;
2daf6502 109 unsigned long diff_ms;
a655f4cf
MD
110
111 ret = fcntl(sock->fd, F_GETFL, 0);
112 if (ret == -1) {
113 PERROR("fcntl");
114 return -1;
115 }
116 flags = ret;
117
118 /* Set socket to nonblock */
119 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
120 if (ret == -1) {
121 PERROR("fcntl");
122 return -1;
123 }
124
389fbf04 125 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
a655f4cf
MD
126 if (ret == -1) {
127 PERROR("clock_gettime");
128 return -1;
129 }
130
131 connect_ret = connect(sock->fd,
1a65bbb8
JG
132 (const struct sockaddr *) ALIGNED_CONST_PTR(
133 sock->sockaddr.addr.sin6),
134 sizeof(sock->sockaddr.addr.sin6));
135 if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK &&
136 errno != EINPROGRESS) {
a655f4cf
MD
137 goto error;
138 } else if (!connect_ret) {
139 /* Connect succeeded */
140 goto success;
141 }
142
48ac9596
JR
143 DBG("Asynchronous connect for sock %d, performing polling with"
144 " timeout: %lums", sock->fd, timeout);
145
a655f4cf
MD
146 /*
147 * Perform poll loop following EINPROGRESS recommendation from
148 * connect(2) man page.
149 */
150 do {
151 struct pollfd fds;
152
153 fds.fd = sock->fd;
154 fds.events = POLLOUT;
155 fds.revents = 0;
156 ret = poll(&fds, 1, RECONNECT_DELAY);
157 if (ret < 0) {
158 goto error;
159 } else if (ret > 0) {
160 int optval;
161 socklen_t optval_len = sizeof(optval);
162
163 if (!(fds.revents & POLLOUT)) {
164 /* Either hup or error */
165 errno = EPIPE;
166 goto error;
167 }
168 /* got something */
169 ret = getsockopt(sock->fd, SOL_SOCKET,
170 SO_ERROR, &optval, &optval_len);
171 if (ret) {
48ac9596 172 PERROR("getsockopt");
a655f4cf
MD
173 goto error;
174 }
175 if (!optval) {
176 connect_ret = 0;
177 goto success;
178 } else {
48ac9596
JR
179 /* Get actual connect() errno from opt_val */
180 errno = optval;
a655f4cf
MD
181 goto error;
182 }
183 }
184 /* ret == 0: timeout */
389fbf04 185 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
a655f4cf
MD
186 if (ret == -1) {
187 PERROR("clock_gettime");
188 connect_ret = ret;
189 goto error;
190 }
2daf6502
MD
191 if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) {
192 ERR("timespec_to_ms input overflows milliseconds output");
193 connect_ret = -1;
194 goto error;
195 }
196 } while (diff_ms < timeout);
a655f4cf
MD
197
198 /* Timeout */
199 errno = ETIMEDOUT;
200 connect_ret = -1;
201
202success:
203 /* Restore initial flags */
204 ret = fcntl(sock->fd, F_SETFL, flags);
205 if (ret == -1) {
206 PERROR("fcntl");
207 /* Continue anyway */
208 }
209error:
210 return connect_ret;
211}
212
6364a07a
DG
213/*
214 * Connect PF_INET socket.
215 */
216int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
217{
218 int ret, closeret;
219
a655f4cf
MD
220 if (lttcomm_get_network_timeout()) {
221 ret = connect_with_timeout(sock);
222 } else {
223 ret = connect_no_timeout(sock);
224 }
6364a07a 225 if (ret < 0) {
82c05d47 226 PERROR("connect inet6");
6364a07a
DG
227 goto error_connect;
228 }
229
230 return ret;
231
232error_connect:
233 closeret = close(sock->fd);
234 if (closeret) {
235 PERROR("close inet6");
236 }
237
238 return ret;
239}
240
241/*
242 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
243 * MUST be bind(2) before.
244 */
245struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
246{
247 int new_fd;
88a5db70 248 socklen_t len;
6364a07a 249 struct lttcomm_sock *new_sock;
1a65bbb8 250 struct sockaddr_in6 new_addr = {};
6364a07a
DG
251
252 if (sock->proto == LTTCOMM_SOCK_UDP) {
253 /*
254 * accept(2) does not exist for UDP so simply return the passed socket.
255 */
256 new_sock = sock;
257 goto end;
258 }
259
de5e9086 260 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
261 if (new_sock == NULL) {
262 goto error;
263 }
264
1a65bbb8 265 len = sizeof(new_addr);
88a5db70 266
6364a07a 267 /* Blocking call */
1a65bbb8 268 new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len);
6364a07a
DG
269 if (new_fd < 0) {
270 PERROR("accept inet6");
271 goto error;
272 }
1a65bbb8 273 new_sock->sockaddr.addr.sin6 = new_addr;
6364a07a 274 new_sock->fd = new_fd;
de5e9086 275 new_sock->ops = &inet6_ops;
6364a07a
DG
276
277end:
278 return new_sock;
279
280error:
281 free(new_sock);
282 return NULL;
283}
284
285/*
286 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
287 */
288int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog)
289{
290 int ret;
291
292 if (sock->proto == LTTCOMM_SOCK_UDP) {
293 /* listen(2) does not exist for UDP so simply return success. */
294 ret = 0;
295 goto end;
296 }
297
298 /* Default listen backlog */
299 if (backlog <= 0) {
300 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
301 }
302
303 ret = listen(sock->fd, backlog);
304 if (ret < 0) {
305 PERROR("listen inet6");
306 }
307
308end:
309 return ret;
310}
311
312/*
313 * Receive data of size len in put that data into the buf param. Using recvmsg
314 * API.
315 *
316 * Return the size of received data.
317 */
318ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
319 size_t len, int flags)
320{
321 struct msghdr msg;
322 struct iovec iov[1];
323 ssize_t ret = -1;
7c5aef62 324 size_t len_last;
1a65bbb8 325 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
6364a07a
DG
326
327 memset(&msg, 0, sizeof(msg));
328
329 iov[0].iov_base = buf;
330 iov[0].iov_len = len;
331 msg.msg_iov = iov;
332 msg.msg_iovlen = 1;
333
1a65bbb8 334 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
335 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
336
6364a07a 337 do {
7c5aef62 338 len_last = iov[0].iov_len;
6364a07a 339 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62 340 if (ret > 0) {
5312a3ed
JG
341 if (flags & MSG_DONTWAIT) {
342 goto end;
343 }
7c5aef62
DG
344 iov[0].iov_base += ret;
345 iov[0].iov_len -= ret;
a0377dfe 346 LTTNG_ASSERT(ret <= len_last);
7c5aef62
DG
347 }
348 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a 349 if (ret < 0) {
7c5aef62
DG
350 PERROR("recvmsg inet");
351 } else if (ret > 0) {
352 ret = len;
6364a07a 353 }
7c5aef62 354 /* Else ret = 0 meaning an orderly shutdown. */
5312a3ed 355end:
6364a07a
DG
356 return ret;
357}
358
359/*
360 * Send buf data of size len. Using sendmsg API.
361 *
362 * Return the size of sent data.
363 */
c2d69327 364ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
365 size_t len, int flags)
366{
367 struct msghdr msg;
368 struct iovec iov[1];
369 ssize_t ret = -1;
370
371 memset(&msg, 0, sizeof(msg));
372
c2d69327 373 iov[0].iov_base = (void *) buf;
6364a07a
DG
374 iov[0].iov_len = len;
375 msg.msg_iov = iov;
376 msg.msg_iovlen = 1;
377
378 switch (sock->proto) {
379 case LTTCOMM_SOCK_UDP:
1a65bbb8
JG
380 {
381 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
382
383 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
384 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
385 break;
1a65bbb8 386 }
6364a07a
DG
387 default:
388 break;
389 }
390
391 do {
392 ret = sendmsg(sock->fd, &msg, flags);
393 } while (ret < 0 && errno == EINTR);
394 if (ret < 0) {
395 /*
396 * Only warn about EPIPE when quiet mode is deactivated.
397 * We consider EPIPE as expected.
398 */
399 if (errno != EPIPE || !lttng_opt_quiet) {
400 PERROR("sendmsg inet6");
401 }
402 }
403
404 return ret;
405}
406
407/*
408 * Shutdown cleanly and close.
409 */
410int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
411{
6e742359 412 int ret;
6364a07a 413
de5e9086 414 /* Don't try to close an invalid marked socket */
6364a07a
DG
415 if (sock->fd == -1) {
416 return 0;
417 }
418
6e742359
DG
419 ret = close(sock->fd);
420 if (ret) {
6364a07a
DG
421 PERROR("close inet6");
422 }
423
424 /* Mark socket */
425 sock->fd = -1;
426
427 return ret;
428}
This page took 0.072201 seconds and 4 git commands to generate.