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