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