Implement filter bytecode support in lttng-session, and parse filter string
[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
990570ed 30#include <common/defaults.h>
4c462e79 31#include <common/error.h>
990570ed 32
10a8a223 33#include "sessiond-comm.h"
fac6795d 34
6364a07a
DG
35/* For Unix socket */
36#include "unix.h"
37/* For Inet socket */
38#include "inet.h"
39/* For Inet6 socket */
40#include "inet6.h"
41
42struct lttcomm_net_family net_families[] = {
43 { LTTCOMM_INET, lttcomm_create_inet_sock },
44 { LTTCOMM_INET6, lttcomm_create_inet6_sock },
45};
46
fac6795d
DG
47/*
48 * Human readable error message.
49 */
50static const char *lttcomm_readable_code[] = {
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
7d29a247
DG
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
fac6795d
DG
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
e065084a 58 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
f3ed775e 59 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
ce3d728c 60 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
ca95a216 61 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
ae856491 62 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_DIR_FAIL) ] = "Create directory failed",
379473d2 63 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
520ff687 64 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
379473d2
DG
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
27673bb6 67 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
d9800920 68 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
2b0bf864 69 [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found",
8e0af1b4 70 [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied",
20fe2104 71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
394016d1 72 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION) ] = "Kernel tracer version is not compatible",
7d29a247 73 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
20fe2104 74 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
42224349 75 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_EXIST) ] = "Kernel channel already exists",
20fe2104 76 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
f3ed775e 77 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
26cc6b4e 78 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
d36b8583 79 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
d65106b1 80 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
f34daff7
DG
81 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
82 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
aaf26714 83 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
8c0faa1d
DG
84 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed",
85 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed",
86 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed",
87 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed",
88 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed",
89 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist",
84291629 90 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
2ef84c95 91 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
4466912f 92 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL) ] = "UST calibration failed",
e0c7ec2b 93 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible",
0177d773 94 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed",
2b0bf864 95 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed",
f6a9efaa 96 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist",
2bdd86d4
MD
97 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found",
98 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed",
99 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed",
100 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed",
101 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed",
102 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed",
103 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed",
104 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed",
105 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed",
7753dea8
MD
106 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed",
107 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed",
2bdd86d4
MD
108 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed",
109 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed",
110 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist",
111 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found",
112 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed",
3b25d04d 113 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist",
be4f0921 114 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found",
ae357384 115 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist",
ae856491 116 [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context",
30905cf4 117 [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership",
80e327fa
DG
118 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started",
119 [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped",
bd29c13d 120 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_ENOSYS) ] = "Kernel event type not supported",
80e327fa 121
3bd1e081
MD
122 [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
123 [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
124 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
f2fc6720 125 [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command",
3bd1e081
MD
126 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread",
127 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd",
128 [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up",
129 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally",
130 [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error",
131 [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile",
132 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF",
133 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL",
134 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
135 [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
d65106b1 136 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
b024d072 137 [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter",
5c827ce0
DG
138 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_USTCONSUMERD) ] = "No UST consumer detected",
139 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_KERNCONSUMERD) ] = "No kernel consumer detected",
a027b2d2 140 [ LTTCOMM_ERR_INDEX(LTTCOMM_EVENT_EXIST_LOGLEVEL) ] = "Event already enabled with different loglevel",
de5e9086
DG
141 [ LTTCOMM_ERR_INDEX(LTTCOMM_URI_DATA_MISS) ] = "Missing data path URI",
142 [ LTTCOMM_ERR_INDEX(LTTCOMM_URI_CTRL_MISS) ] = "Missing control data path URI",
143 [ LTTCOMM_ERR_INDEX(LTTCOMM_ENABLE_CONSUMER_FAIL) ] = "Enabling consumer failed",
144 [ LTTCOMM_ERR_INDEX(LTTCOMM_RELAYD_SESSION_FAIL) ] = "Unable to create session on lttng-relayd",
145 [ LTTCOMM_ERR_INDEX(LTTCOMM_RELAYD_VERSION_FAIL) ] = "Relay daemon not compatible",
53a80697
MD
146 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_INVAL) ] = "Invalid filter bytecode",
147 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_NOMEM) ] = "Not enough memory for filter bytecode",
148 [ LTTCOMM_ERR_INDEX(LTTCOMM_FILTER_EXIST) ] = "Filter already exist",
fac6795d
DG
149};
150
151/*
917216f6
DG
152 * Return ptr to string representing a human readable error code from the
153 * lttcomm_return_code enum.
fac6795d 154 *
917216f6 155 * These code MUST be negative in other to treat that as an error value.
fac6795d
DG
156 */
157const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
158{
159 int tmp_code = -code;
160
161 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
162 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
163 }
164
165 return "Unknown error code";
166}
6364a07a
DG
167
168/*
de5e9086
DG
169 * Create socket from an already allocated lttcomm socket structure and init
170 * sockaddr in the lttcomm sock.
6364a07a 171 */
de5e9086 172int lttcomm_create_sock(struct lttcomm_sock *sock)
6364a07a 173{
de5e9086 174 int ret, _sock_type, _sock_proto, domain;
6364a07a 175
de5e9086
DG
176 assert(sock);
177
178 domain = sock->sockaddr.type;
179 if (domain != LTTCOMM_INET && domain != LTTCOMM_INET6) {
180 ERR("Create socket of unknown domain %d", domain);
181 ret = -1;
182 goto error;
6364a07a
DG
183 }
184
de5e9086
DG
185 switch (sock->proto) {
186 case LTTCOMM_SOCK_UDP:
187 _sock_type = SOCK_DGRAM;
188 _sock_proto = IPPROTO_UDP;
189 break;
190 case LTTCOMM_SOCK_TCP:
191 _sock_type = SOCK_STREAM;
192 _sock_proto = IPPROTO_TCP;
193 break;
194 default:
195 ret = -1;
196 goto error;
197 }
6364a07a 198
de5e9086
DG
199 ret = net_families[domain].create(sock, _sock_type, _sock_proto);
200 if (ret < 0) {
201 goto error;
202 }
203
204error:
205 return ret;
6364a07a
DG
206}
207
208/*
de5e9086 209 * Return allocated lttcomm socket structure.
6364a07a 210 */
de5e9086 211struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto)
6364a07a 212{
de5e9086 213 struct lttcomm_sock *sock;
6364a07a 214
de5e9086
DG
215 sock = zmalloc(sizeof(struct lttcomm_sock));
216 if (sock == NULL) {
217 PERROR("zmalloc create sock");
218 goto end;
6364a07a
DG
219 }
220
221 sock->proto = proto;
de5e9086 222 sock->fd = -1;
6364a07a 223
de5e9086
DG
224end:
225 return sock;
6364a07a
DG
226}
227
228/*
de5e9086
DG
229 * Return an allocated lttcomm socket structure and copy src content into
230 * the newly created socket.
231 *
232 * This is mostly useful when lttcomm_sock are passed between process where the
233 * fd and ops have to be changed within the correct address space.
6364a07a 234 */
de5e9086 235struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src)
6364a07a 236{
6364a07a
DG
237 struct lttcomm_sock *sock;
238
de5e9086
DG
239 /* Safety net */
240 assert(src);
241
242 sock = lttcomm_alloc_sock(src->proto);
6364a07a
DG
243 if (sock == NULL) {
244 goto alloc_error;
245 }
246
de5e9086 247 lttcomm_copy_sock(sock, src);
6364a07a 248
de5e9086 249alloc_error:
6364a07a 250 return sock;
de5e9086 251}
6364a07a 252
de5e9086
DG
253/*
254 * Create and copy socket from an allocated lttcomm socket structure.
255 *
256 * This is mostly useful when lttcomm_sock are passed between process where the
257 * fd and ops have to be changed within the correct address space.
258 */
259void lttcomm_copy_sock(struct lttcomm_sock *dst, struct lttcomm_sock *src)
260{
261 /* Safety net */
262 assert(dst);
263 assert(src);
264
265 dst->proto = src->proto;
266 dst->fd = src->fd;
267 dst->ops = src->ops;
268 /* Copy sockaddr information from original socket */
269 memcpy(&dst->sockaddr, &src->sockaddr, sizeof(dst->sockaddr));
6364a07a
DG
270}
271
272/*
273 * Init IPv4 sockaddr structure.
274 */
275int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
276 const char *ip, unsigned int port)
277{
278 int ret;
279
280 assert(sockaddr);
281 assert(ip);
282 assert(port > 0 && port <= 65535);
283
284 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
285
286 sockaddr->type = LTTCOMM_INET;
287 sockaddr->addr.sin.sin_family = AF_INET;
288 sockaddr->addr.sin.sin_port = htons(port);
289 ret = inet_pton(sockaddr->addr.sin.sin_family, ip,
290 &sockaddr->addr.sin.sin_addr);
291 if (ret < 1) {
292 ret = -1;
de5e9086 293 ERR("%s with port %d: unrecognized IPv4 address", ip, port);
6364a07a
DG
294 goto error;
295 }
296 memset(sockaddr->addr.sin.sin_zero, 0, sizeof(sockaddr->addr.sin.sin_zero));
297
298error:
299 return ret;
300}
301
302/*
303 * Init IPv6 sockaddr structure.
304 */
305int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
306 const char *ip, unsigned int port)
307{
308 int ret;
309
310 assert(sockaddr);
311 assert(ip);
312 assert(port > 0 && port <= 65535);
313
314 memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr));
315
316 sockaddr->type = LTTCOMM_INET6;
317 sockaddr->addr.sin6.sin6_family = AF_INET6;
318 sockaddr->addr.sin6.sin6_port = htons(port);
319 ret = inet_pton(sockaddr->addr.sin6.sin6_family, ip,
320 &sockaddr->addr.sin6.sin6_addr);
321 if (ret < 1) {
322 ret = -1;
323 goto error;
324 }
325
326error:
327 return ret;
328}
de5e9086
DG
329
330/*
331 * Return allocated lttcomm socket structure from lttng URI.
332 */
333struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
334{
335 int ret;
336 int _sock_proto;
337 struct lttcomm_sock *sock = NULL;
338
339 /* Safety net */
340 assert(uri);
341
342 /* Check URI protocol */
343 if (uri->proto == LTTNG_TCP) {
344 _sock_proto = LTTCOMM_SOCK_TCP;
345 } else {
346 ERR("Relayd invalid URI proto: %d", uri->proto);
347 goto alloc_error;
348 }
349
350 sock = lttcomm_alloc_sock(_sock_proto);
351 if (sock == NULL) {
352 goto alloc_error;
353 }
354
355 /* Check destination type */
356 if (uri->dtype == LTTNG_DST_IPV4) {
357 ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
358 uri->port);
359 if (ret < 0) {
360 goto error;
361 }
362 } else if (uri->dtype == LTTNG_DST_IPV6) {
363 ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
364 uri->port);
365 if (ret < 0) {
366 goto error;
367 }
368 } else {
369 /* Command URI is invalid */
370 ERR("Relayd invalid URI dst type: %d", uri->dtype);
371 goto error;
372 }
373
374 return sock;
375
376error:
377 lttcomm_destroy_sock(sock);
378alloc_error:
379 return NULL;
380}
381
382/*
383 * Destroy and free lttcomm socket.
384 */
385void lttcomm_destroy_sock(struct lttcomm_sock *sock)
386{
387 if (sock != NULL) {
388 free(sock);
389 }
390}
This page took 0.077683 seconds and 4 git commands to generate.