Fix: unprivilieged sessiond agent port clashes with root sessiond
[lttng-tools.git] / src / common / sessiond-comm / inet6.c
CommitLineData
6364a07a
DG
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
6c1c0768 18#define _LGPL_SOURCE
6364a07a
DG
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>
a655f4cf 28#include <fcntl.h>
389fbf04 29#include <common/compat/time.h>
a655f4cf 30#include <poll.h>
6364a07a 31
90e535ef 32#include <common/common.h>
395d6b02 33#include <common/time.h>
6364a07a
DG
34
35#include "inet6.h"
36
a655f4cf
MD
37#define RECONNECT_DELAY 200 /* ms */
38
6364a07a
DG
39/*
40 * INET protocol operations.
41 */
42static 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 */
90e535ef 55LTTNG_HIDDEN
6364a07a
DG
56int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto)
57{
de5e9086 58 int val = 1, ret;
783a3b9a 59 unsigned long timeout;
6364a07a
DG
60
61 /* Create server socket */
7b43086d 62 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
6364a07a
DG
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 }
783a3b9a
MD
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 }
6364a07a
DG
88
89 return 0;
90
91error:
92 return -1;
93}
94
95/*
96 * Bind socket and return.
97 */
90e535ef 98LTTNG_HIDDEN
6364a07a
DG
99int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock)
100{
2288467f
JG
101 return bind(sock->fd,
102 (const struct sockaddr *) &sock->sockaddr.addr.sin6,
6364a07a 103 sizeof(sock->sockaddr.addr.sin6));
6364a07a
DG
104}
105
a655f4cf
MD
106static
107int 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 */
116static
117unsigned 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
132static
133int 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
389fbf04 153 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
a655f4cf
MD
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
48ac9596
JR
171 DBG("Asynchronous connect for sock %d, performing polling with"
172 " timeout: %lums", sock->fd, timeout);
173
a655f4cf
MD
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) {
48ac9596 200 PERROR("getsockopt");
a655f4cf
MD
201 goto error;
202 }
203 if (!optval) {
204 connect_ret = 0;
205 goto success;
206 } else {
48ac9596
JR
207 /* Get actual connect() errno from opt_val */
208 errno = optval;
a655f4cf
MD
209 goto error;
210 }
211 }
212 /* ret == 0: timeout */
389fbf04 213 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
a655f4cf
MD
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
225success:
226 /* Restore initial flags */
227 ret = fcntl(sock->fd, F_SETFL, flags);
228 if (ret == -1) {
229 PERROR("fcntl");
230 /* Continue anyway */
231 }
232error:
233 return connect_ret;
234}
235
6364a07a
DG
236/*
237 * Connect PF_INET socket.
238 */
90e535ef 239LTTNG_HIDDEN
6364a07a
DG
240int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
241{
242 int ret, closeret;
243
a655f4cf
MD
244 if (lttcomm_get_network_timeout()) {
245 ret = connect_with_timeout(sock);
246 } else {
247 ret = connect_no_timeout(sock);
248 }
6364a07a 249 if (ret < 0) {
82c05d47 250 PERROR("connect inet6");
6364a07a
DG
251 goto error_connect;
252 }
253
254 return ret;
255
256error_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 */
90e535ef 269LTTNG_HIDDEN
6364a07a
DG
270struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
271{
272 int new_fd;
88a5db70 273 socklen_t len;
6364a07a
DG
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
de5e9086 284 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
285 if (new_sock == NULL) {
286 goto error;
287 }
288
88a5db70
DG
289 len = sizeof(new_sock->sockaddr.addr.sin6);
290
6364a07a
DG
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;
de5e9086 300 new_sock->ops = &inet6_ops;
6364a07a
DG
301
302end:
303 return new_sock;
304
305error:
306 free(new_sock);
307 return NULL;
308}
309
310/*
311 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
312 */
90e535ef 313LTTNG_HIDDEN
6364a07a
DG
314int 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
334end:
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 */
90e535ef 344LTTNG_HIDDEN
6364a07a
DG
345ssize_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;
7c5aef62 351 size_t len_last;
6364a07a
DG
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
6364a07a 363 do {
7c5aef62 364 len_last = iov[0].iov_len;
6364a07a 365 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
366 if (ret > 0) {
367 iov[0].iov_base += ret;
368 iov[0].iov_len -= ret;
369 assert(ret <= len_last);
370 }
371 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a 372 if (ret < 0) {
7c5aef62
DG
373 PERROR("recvmsg inet");
374 } else if (ret > 0) {
375 ret = len;
6364a07a 376 }
7c5aef62 377 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
378
379 return ret;
380}
381
382/*
383 * Send buf data of size len. Using sendmsg API.
384 *
385 * Return the size of sent data.
386 */
90e535ef 387LTTNG_HIDDEN
c2d69327 388ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
389 size_t len, int flags)
390{
391 struct msghdr msg;
392 struct iovec iov[1];
393 ssize_t ret = -1;
394
395 memset(&msg, 0, sizeof(msg));
396
c2d69327 397 iov[0].iov_base = (void *) buf;
6364a07a
DG
398 iov[0].iov_len = len;
399 msg.msg_iov = iov;
400 msg.msg_iovlen = 1;
401
402 switch (sock->proto) {
403 case LTTCOMM_SOCK_UDP:
404 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6;
405 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
406 break;
407 default:
408 break;
409 }
410
411 do {
412 ret = sendmsg(sock->fd, &msg, flags);
413 } while (ret < 0 && errno == EINTR);
414 if (ret < 0) {
415 /*
416 * Only warn about EPIPE when quiet mode is deactivated.
417 * We consider EPIPE as expected.
418 */
419 if (errno != EPIPE || !lttng_opt_quiet) {
420 PERROR("sendmsg inet6");
421 }
422 }
423
424 return ret;
425}
426
427/*
428 * Shutdown cleanly and close.
429 */
90e535ef 430LTTNG_HIDDEN
6364a07a
DG
431int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
432{
6e742359 433 int ret;
6364a07a 434
de5e9086 435 /* Don't try to close an invalid marked socket */
6364a07a
DG
436 if (sock->fd == -1) {
437 return 0;
438 }
439
6e742359
DG
440 ret = close(sock->fd);
441 if (ret) {
6364a07a
DG
442 PERROR("close inet6");
443 }
444
445 /* Mark socket */
446 sock->fd = -1;
447
448 return ret;
449}
This page took 0.072646 seconds and 5 git commands to generate.