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