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