Implement support for lttng-modules syscall filtering
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.c
CommitLineData
826d496d 1/*
917216f6
DG
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fac6795d 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
fac6795d 8 *
917216f6
DG
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
fac6795d 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
1e307fab 20#include <assert.h>
fac6795d
DG
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
fac6795d
DG
25#include <sys/stat.h>
26#include <sys/types.h>
fac6795d 27#include <unistd.h>
3bd1e081 28#include <errno.h>
fac6795d 29
90e535ef 30#include <common/common.h>
990570ed 31
10a8a223 32#include "sessiond-comm.h"
fac6795d 33
6364a07a
DG
34/* For Unix socket */
35#include "unix.h"
36/* For Inet socket */
37#include "inet.h"
38/* For Inet6 socket */
39#include "inet6.h"
40
554831e7
MD
41#define NETWORK_TIMEOUT_ENV "LTTNG_NETWORK_SOCKET_TIMEOUT"
42
32dd26fb 43static struct lttcomm_net_family net_families[] = {
6364a07a
DG
44 { LTTCOMM_INET, lttcomm_create_inet_sock },
45 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
46};
47
fac6795d
DG
48/*
49 * Human readable error message.
50 */
51static const char *lttcomm_readable_code[] = {
f73fabfd
DG
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
40727660
MD
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM) ] = "Consumer is out of memory",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA) ] = "Error with metadata",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL) ] = "Fatal error",
618a6a28 69 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL) ] = "Error on remote relayd",
80e327fa 70
f73fabfd
DG
71 /* Last element */
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code"
fac6795d
DG
73};
74
554831e7
MD
75static unsigned long network_timeout;
76
fac6795d 77/*
917216f6
DG
78 * Return ptr to string representing a human readable error code from the
79 * lttcomm_return_code enum.
fac6795d 80 *
917216f6 81 * These code MUST be negative in other to treat that as an error value.
fac6795d 82 */
90e535ef 83LTTNG_HIDDEN
fac6795d
DG
84const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
85{
f73fabfd 86 code = -code;
fac6795d 87
c617c0c6 88 if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) {
f73fabfd 89 code = LTTCOMM_NR;
fac6795d
DG
90 }
91
f73fabfd 92 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)];
fac6795d 93}
6364a07a
DG
94
95/*
de5e9086
DG
96 * Create socket from an already allocated lttcomm socket structure and init
97 * sockaddr in the lttcomm sock.
6364a07a 98 */
90e535ef 99LTTNG_HIDDEN
de5e9086 100int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 101{
de5e9086 102 int ret, _sock_type, _sock_proto, domain;
6364a07a 103
de5e9086
DG
104 assert(sock);
105
106 domain = sock->sockaddr.type;
107 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
108 ERR("Create socket of unknown domain %d", domain);
109 ret = -1;
110 goto error;
6364a07a
DG
111 }
112
de5e9086
DG
113 switch (sock->proto) {
114 case LTTCOMM_SOCK_UDP:
115 _sock_type = SOCK_DGRAM;
116 _sock_proto = IPPROTO_UDP;
117 break;
118 case LTTCOMM_SOCK_TCP:
119 _sock_type = SOCK_STREAM;
120 _sock_proto = IPPROTO_TCP;
121 break;
122 default:
123 ret = -1;
124 goto error;
125 }
6364a07a 126
de5e9086
DG
127 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
128 if (ret < 0) {
129 goto error;
130 }
131
132error:
133 return ret;
6364a07a
DG
134}
135
136/*
de5e9086 137 * Return allocated lttcomm socket structure.
6364a07a 138 */
90e535ef 139LTTNG_HIDDEN
de5e9086 140struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 141{
de5e9086 142 struct lttcomm_sock *sock;
6364a07a 143
de5e9086
DG
144 sock = zmalloc(sizeof(struct lttcomm_sock));
145 if (sock == NULL) {
146 PERROR("zmalloc create sock");
147 goto end;
6364a07a
DG
148 }
149
150 sock->proto = proto;
de5e9086 151 sock->fd = -1;
6364a07a 152
de5e9086
DG
153end:
154 return sock;
6364a07a
DG
155}
156
157/*
de5e9086
DG
158 * Return an allocated lttcomm socket structure and copy src content into
159 * the newly created socket.
160 *
161 * This is mostly useful when lttcomm_sock are passed between process where the
162 * fd and ops have to be changed within the correct address space.
6364a07a 163 */
90e535ef 164LTTNG_HIDDEN
de5e9086 165struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 166{
6364a07a
DG
167 struct lttcomm_sock *sock;
168
de5e9086
DG
169 /* Safety net */
170 assert(src);
171
172 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
173 if (sock == NULL) {
174 goto alloc_error;
175 }
176
de5e9086 177 lttcomm_copy_sock(sock, src);
6364a07a 178
de5e9086 179alloc_error:
6364a07a 180 return sock;
de5e9086 181}
6364a07a 182
de5e9086
DG
183/*
184 * Create and copy socket from an allocated lttcomm socket structure.
185 *
186 * This is mostly useful when lttcomm_sock are passed between process where the
187 * fd and ops have to be changed within the correct address space.
188 */
90e535ef 189LTTNG_HIDDEN
de5e9086
DG
190void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
191{
192 /* Safety net */
193 assert(dst);
194 assert(src);
195
196 dst->proto = src->proto;
197 dst->fd = src->fd;
198 dst->ops = src->ops;
199 /* Copy sockaddr information from original socket */
200 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
6364a07a
DG
201}
202
203/*
204 * Init IPv4 sockaddr structure.
205 */
90e535ef 206LTTNG_HIDDEN
6364a07a
DG
207int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
208 const char *ip, unsigned int port)
209{
210 int ret;
211
212 assert(sockaddr);
213 assert(ip);
214 assert(port > 0 && port <= 65535);
215
216 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
217
218 sockaddr->type = LTTCOMM_INET;
219 sockaddr->addr.sin.sin_family = AF_INET;
220 sockaddr->addr.sin.sin_port = htons(port);
221 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
222 &sockaddr->addr.sin.sin_addr);
223 if (ret < 1) {
224 ret = -1;
de5e9086 225 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
226 goto error;
227 }
228 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
229
230error:
231 return ret;
232}
233
234/*
235 * Init IPv6 sockaddr structure.
236 */
90e535ef 237LTTNG_HIDDEN
6364a07a
DG
238int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
239 const char *ip, unsigned int port)
240{
241 int ret;
242
243 assert(sockaddr);
244 assert(ip);
245 assert(port > 0 && port <= 65535);
246
247 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
248
249 sockaddr->type = LTTCOMM_INET6;
250 sockaddr->addr.sin6.sin6_family = AF_INET6;
251 sockaddr->addr.sin6.sin6_port = htons(port);
252 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
253 &sockaddr->addr.sin6.sin6_addr);
254 if (ret < 1) {
255 ret = -1;
256 goto error;
257 }
258
259error:
260 return ret;
261}
de5e9086
DG
262
263/*
264 * Return allocated lttcomm socket structure from lttng URI.
265 */
90e535ef 266LTTNG_HIDDEN
de5e9086
DG
267struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
268{
269 int ret;
270 int _sock_proto;
271 struct lttcomm_sock *sock = NULL;
272
273 /* Safety net */
274 assert(uri);
275
276 /* Check URI protocol */
277 if (uri->proto == LTTNG_TCP) {
278 _sock_proto = LTTCOMM_SOCK_TCP;
279 } else {
280 ERR("Relayd invalid URI proto: %d", uri->proto);
281 goto alloc_error;
282 }
283
284 sock = lttcomm_alloc_sock(_sock_proto);
285 if (sock == NULL) {
286 goto alloc_error;
287 }
288
289 /* Check destination type */
290 if (uri->dtype == LTTNG_DST_IPV4) {
291 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
292 uri->port);
293 if (ret < 0) {
294 goto error;
295 }
296 } else if (uri->dtype == LTTNG_DST_IPV6) {
297 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
298 uri->port);
299 if (ret < 0) {
300 goto error;
301 }
302 } else {
303 /* Command URI is invalid */
304 ERR("Relayd invalid URI dst type: %d", uri->dtype);
305 goto error;
306 }
307
308 return sock;
309
310error:
311 lttcomm_destroy_sock(sock);
312alloc_error:
313 return NULL;
314}
315
316/*
317 * Destroy and free lttcomm socket.
318 */
90e535ef 319LTTNG_HIDDEN
de5e9086
DG
320void lttcomm_destroy_sock(struct lttcomm_sock *sock)
321{
0e428499 322 free(sock);
de5e9086 323}
6151a90f
JD
324
325/*
326 * Allocate and return a relayd socket object using a given URI to initialize
327 * it and the major/minor version of the supported protocol.
328 *
329 * On error, NULL is returned.
330 */
bc182241 331LTTNG_HIDDEN
6151a90f
JD
332struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri,
333 uint32_t major, uint32_t minor)
334{
335 int ret;
336 struct lttcomm_sock *tmp_sock = NULL;
337 struct lttcomm_relayd_sock *rsock = NULL;
338
339 assert(uri);
340
341 rsock = zmalloc(sizeof(*rsock));
342 if (!rsock) {
343 PERROR("zmalloc relayd sock");
344 goto error;
345 }
346
347 /* Allocate socket object from URI */
348 tmp_sock = lttcomm_alloc_sock_from_uri(uri);
349 if (tmp_sock == NULL) {
350 goto error_free;
351 }
352
353 /*
354 * Create socket object which basically sets the ops according to the
355 * socket protocol.
356 */
357 lttcomm_copy_sock(&rsock->sock, tmp_sock);
358 /* Temporary socket pointer not needed anymore. */
359 lttcomm_destroy_sock(tmp_sock);
360 ret = lttcomm_create_sock(&rsock->sock);
361 if (ret < 0) {
362 goto error_free;
363 }
364
365 rsock->major = major;
366 rsock->minor = minor;
367
368 return rsock;
369
370error_free:
371 free(rsock);
372error:
373 return NULL;
374}
554831e7 375
783a3b9a
MD
376/*
377 * Set socket receiving timeout.
378 */
379LTTNG_HIDDEN
380int lttcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
381{
382 int ret;
383 struct timeval tv;
384
385 tv.tv_sec = msec / 1000;
386 tv.tv_usec = (msec % 1000) * 1000;
387
388 ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
389 if (ret < 0) {
390 PERROR("setsockopt SO_RCVTIMEO");
391 }
392
393 return ret;
394}
395
396/*
397 * Set socket sending timeout.
398 */
399LTTNG_HIDDEN
400int lttcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
401{
402 int ret;
403 struct timeval tv;
404
405 tv.tv_sec = msec / 1000;
406 tv.tv_usec = (msec % 1000) * 1000;
407
408 ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
409 if (ret < 0) {
410 PERROR("setsockopt SO_SNDTIMEO");
411 }
412
413 return ret;
414}
415
554831e7
MD
416LTTNG_HIDDEN
417void lttcomm_init(void)
418{
419 const char *env;
420
421 env = getenv(NETWORK_TIMEOUT_ENV);
422 if (env) {
423 long timeout;
424
425 errno = 0;
426 timeout = strtol(env, NULL, 0);
427 if (errno != 0 || timeout < -1L) {
428 PERROR("Network timeout");
429 } else {
430 if (timeout > 0) {
431 network_timeout = timeout;
432 }
433 }
434 }
435}
436
437LTTNG_HIDDEN
438unsigned long lttcomm_get_network_timeout(void)
439{
440 return network_timeout;
441}
This page took 0.076362 seconds and 5 git commands to generate.