Relayd data available command support
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
CommitLineData
826d496d 1/*
82a3637f
DG
2 * liblttngctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
826d496d 6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d 7 *
d14d33bf
AM
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
82a3637f
DG
11 *
12 * This library is distributed in the hope that it will be useful,
fac6795d 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
d14d33bf
AM
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
20 */
21
22#define _GNU_SOURCE
44a5e5eb 23#include <assert.h>
fac6795d 24#include <grp.h>
1e307fab 25#include <errno.h>
fac6795d
DG
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
990570ed
DG
31#include <common/common.h>
32#include <common/defaults.h>
db758600 33#include <common/sessiond-comm/sessiond-comm.h>
a4b92340 34#include <common/uri.h>
1e307fab 35#include <lttng/lttng.h>
fac6795d 36
d00c599e
DG
37#include "filter/filter-ast.h"
38#include "filter/filter-parser.h"
39#include "filter/filter-bytecode.h"
40#include "filter/memstream.h"
53a80697
MD
41
42#ifdef DEBUG
d00c599e 43static const int print_xml = 1;
53a80697
MD
44#define dbg_printf(fmt, args...) \
45 printf("[debug liblttng-ctl] " fmt, ## args)
46#else
d00c599e 47static const int print_xml = 0;
53a80697
MD
48#define dbg_printf(fmt, args...) \
49do { \
50 /* do nothing but check printf format */ \
51 if (0) \
52 printf("[debug liblttnctl] " fmt, ## args); \
53} while (0)
54#endif
55
56
fac6795d
DG
57/* Socket to session daemon for communication */
58static int sessiond_socket;
59static char sessiond_sock_path[PATH_MAX];
44a5e5eb 60static char health_sock_path[PATH_MAX];
fac6795d 61
fac6795d
DG
62/* Variables */
63static char *tracing_group;
64static int connected;
65
97e19046
DG
66/* Global */
67
68/*
69 * Those two variables are used by error.h to silent or control the verbosity of
70 * error message. They are global to the library so application linking with it
71 * are able to compile correctly and also control verbosity of the library.
72 *
73 * Note that it is *not* possible to silent ERR() and PERROR() macros.
74 */
75int lttng_opt_quiet;
76int lttng_opt_verbose;
77
a4b92340
DG
78static void set_default_url_attr(struct lttng_uri *uri,
79 enum lttng_stream_type stype)
80{
81 uri->stype = stype;
82 if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) {
83 uri->port = (stype == LTTNG_STREAM_CONTROL) ?
84 DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT;
85 }
86}
87
88/*
89 * Parse a string URL and creates URI(s) returning the size of the populated
90 * array.
91 */
92static ssize_t parse_str_urls_to_uri(const char *ctrl_url, const char *data_url,
93 struct lttng_uri **uris)
94{
95 int ret;
96 unsigned int equal = 1, idx = 0;
97 /* Add the "file://" size to the URL maximum size */
98 char url[PATH_MAX + 7];
99 ssize_t size_ctrl = 0, size_data = 0, size;
100 struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL;
101 struct lttng_uri *tmp_uris = NULL;
102
103 /* No URL(s) is allowed. This means that the consumer will be disabled. */
104 if (ctrl_url == NULL && data_url == NULL) {
105 return 0;
106 }
107
108 /* Check if URLs are equal and if so, only use the control URL */
109 if (ctrl_url && data_url) {
110 equal = !strcmp(ctrl_url, data_url);
111 }
112
113 /*
114 * Since we allow the str_url to be a full local filesystem path, we are
115 * going to create a valid file:// URL if it's the case.
116 *
117 * Check if first character is a '/' or else reject the URL.
118 */
119 if (ctrl_url && ctrl_url[0] == '/') {
120 ret = snprintf(url, sizeof(url), "file://%s", ctrl_url);
121 if (ret < 0) {
122 PERROR("snprintf file url");
123 goto parse_error;
124 }
125 ctrl_url = url;
126 }
127
128 /* Parse the control URL if there is one */
129 if (ctrl_url) {
130 size_ctrl = uri_parse(ctrl_url, &ctrl_uris);
131 if (size_ctrl < 1) {
132 ERR("Unable to parse the URL %s", ctrl_url);
133 goto parse_error;
134 }
135
136 /* At this point, we know there is at least one URI in the array */
137 set_default_url_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL);
138
139 if (ctrl_uris[0].dtype == LTTNG_DST_PATH && data_url) {
140 ERR("Can not have a data URL when destination is file://");
141 goto error;
142 }
143
144 /* URL are not equal but the control URL uses a net:// protocol */
145 if (size_ctrl == 2) {
146 if (!equal) {
147 ERR("Control URL uses the net:// protocol and the data URL is "
148 "different. Not allowed.");
149 goto error;
150 } else {
151 set_default_url_attr(&ctrl_uris[1], LTTNG_STREAM_DATA);
152 /*
153 * The data_url and ctrl_url are equal and the ctrl_url
154 * contains a net:// protocol so we just skip the data part.
155 */
156 data_url = NULL;
157 }
158 }
159 }
160
161 if (data_url) {
162 /* We have to parse the data URL in this case */
163 size_data = uri_parse(data_url, &data_uris);
164 if (size_data < 1) {
165 ERR("Unable to parse the URL %s", data_url);
166 goto error;
167 } else if (size_data == 2) {
168 ERR("Data URL can not be set with the net[4|6]:// protocol");
169 goto error;
170 }
171
172 set_default_url_attr(&data_uris[0], LTTNG_STREAM_DATA);
173 }
174
175 /* Compute total size */
176 size = size_ctrl + size_data;
177
178 tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
179 if (tmp_uris == NULL) {
180 PERROR("zmalloc uris");
181 goto error;
182 }
183
184 if (ctrl_uris) {
185 /* It's possible the control URIs array contains more than one URI */
186 memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * size_ctrl);
187 ++idx;
188 }
189
190 if (data_uris) {
191 memcpy(&tmp_uris[idx], data_uris, sizeof(struct lttng_uri));
192 }
193
194 *uris = tmp_uris;
195
196 return size;
197
198error:
199 free(ctrl_uris);
200 free(data_uris);
201 free(tmp_uris);
202parse_error:
203 return -1;
204}
205
99497cd0
MD
206/*
207 * Copy string from src to dst and enforce null terminated byte.
208 */
209static void copy_string(char *dst, const char *src, size_t len)
210{
e7d6716d 211 if (src && dst) {
99497cd0
MD
212 strncpy(dst, src, len);
213 /* Enforce the NULL terminated byte */
214 dst[len - 1] = '\0';
cd80958d
DG
215 } else if (dst) {
216 dst[0] = '\0';
99497cd0
MD
217 }
218}
219
fac6795d 220/*
cd80958d 221 * Copy domain to lttcomm_session_msg domain.
fac6795d 222 *
cd80958d
DG
223 * If domain is unknown, default domain will be the kernel.
224 */
225static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
226{
227 if (src && dst) {
228 switch (src->type) {
00e2e675
DG
229 case LTTNG_DOMAIN_KERNEL:
230 case LTTNG_DOMAIN_UST:
231 /*
232 case LTTNG_DOMAIN_UST_EXEC_NAME:
233 case LTTNG_DOMAIN_UST_PID:
234 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
235 */
236 memcpy(dst, src, sizeof(struct lttng_domain));
237 break;
238 default:
239 memset(dst, 0, sizeof(struct lttng_domain));
00e2e675 240 break;
cd80958d
DG
241 }
242 }
243}
244
245/*
246 * Send lttcomm_session_msg to the session daemon.
fac6795d 247 *
1c8d13c8
TD
248 * On success, returns the number of bytes sent (>=0)
249 * On error, returns -1
fac6795d 250 */
cd80958d 251static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
252{
253 int ret;
254
255 if (!connected) {
2f70b271 256 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 257 goto end;
fac6795d
DG
258 }
259
a4b92340
DG
260 DBG("LSM cmd type : %d", lsm->cmd_type);
261
be040666 262 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
cd80958d 263 sizeof(struct lttcomm_session_msg));
2f70b271
DG
264 if (ret < 0) {
265 ret = -LTTNG_ERR_FATAL;
266 }
e065084a
DG
267
268end:
269 return ret;
270}
271
53a80697
MD
272/*
273 * Send var len data to the session daemon.
274 *
275 * On success, returns the number of bytes sent (>=0)
276 * On error, returns -1
277 */
278static int send_session_varlen(void *data, size_t len)
279{
280 int ret;
281
282 if (!connected) {
2f70b271 283 ret = -LTTNG_ERR_NO_SESSIOND;
53a80697
MD
284 goto end;
285 }
a4b92340 286
53a80697
MD
287 if (!data || !len) {
288 ret = 0;
289 goto end;
290 }
291
292 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
2f70b271
DG
293 if (ret < 0) {
294 ret = -LTTNG_ERR_FATAL;
295 }
53a80697
MD
296
297end:
298 return ret;
299}
300
e065084a 301/*
cd80958d 302 * Receive data from the sessiond socket.
e065084a 303 *
1c8d13c8
TD
304 * On success, returns the number of bytes received (>=0)
305 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 306 */
ca95a216 307static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
308{
309 int ret;
310
311 if (!connected) {
2f70b271 312 ret = -LTTNG_ERR_NO_SESSIOND;
e065084a 313 goto end;
fac6795d
DG
314 }
315
ca95a216 316 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
2f70b271
DG
317 if (ret < 0) {
318 ret = -LTTNG_ERR_FATAL;
319 }
fac6795d 320
e065084a 321end:
fac6795d
DG
322 return ret;
323}
324
325/*
1c8d13c8 326 * Check if we are in the specified group.
65beb5ff 327 *
2269e89e 328 * If yes return 1, else return -1.
947308c4
DG
329 */
330static int check_tracing_group(const char *grp_name)
331{
332 struct group *grp_tracing; /* no free(). See getgrnam(3) */
333 gid_t *grp_list;
334 int grp_list_size, grp_id, i;
335 int ret = -1;
336
337 /* Get GID of group 'tracing' */
338 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
339 if (!grp_tracing) {
340 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
341 goto end;
342 }
343
344 /* Get number of supplementary group IDs */
345 grp_list_size = getgroups(0, NULL);
346 if (grp_list_size < 0) {
347 perror("getgroups");
348 goto end;
349 }
350
351 /* Alloc group list of the right size */
352 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 353 if (!grp_list) {
1c8d13c8 354 perror("malloc");
00795392
MD
355 goto end;
356 }
947308c4 357 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 358 if (grp_id < 0) {
947308c4
DG
359 perror("getgroups");
360 goto free_list;
361 }
362
363 for (i = 0; i < grp_list_size; i++) {
364 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 365 ret = 1;
947308c4
DG
366 break;
367 }
368 }
369
370free_list:
371 free(grp_list);
372
373end:
374 return ret;
375}
376
377/*
2269e89e
DG
378 * Try connect to session daemon with sock_path.
379 *
380 * Return 0 on success, else -1
381 */
382static int try_connect_sessiond(const char *sock_path)
383{
384 int ret;
385
386 /* If socket exist, we check if the daemon listens for connect. */
387 ret = access(sock_path, F_OK);
388 if (ret < 0) {
389 /* Not alive */
2f70b271 390 goto error;
2269e89e
DG
391 }
392
393 ret = lttcomm_connect_unix_sock(sock_path);
394 if (ret < 0) {
395 /* Not alive */
2f70b271 396 goto error;
2269e89e
DG
397 }
398
399 ret = lttcomm_close_unix_sock(ret);
400 if (ret < 0) {
401 perror("lttcomm_close_unix_sock");
402 }
403
404 return 0;
2f70b271
DG
405
406error:
407 return -1;
2269e89e
DG
408}
409
410/*
2f70b271
DG
411 * Set sessiond socket path by putting it in the global sessiond_sock_path
412 * variable.
413 *
414 * Returns 0 on success, negative value on failure (the sessiond socket path
415 * is somehow too long or ENOMEM).
947308c4
DG
416 */
417static int set_session_daemon_path(void)
418{
419 int ret;
2269e89e
DG
420 int in_tgroup = 0; /* In tracing group */
421 uid_t uid;
422
423 uid = getuid();
947308c4 424
2269e89e
DG
425 if (uid != 0) {
426 /* Are we in the tracing group ? */
427 in_tgroup = check_tracing_group(tracing_group);
428 }
429
08a9c49f
TD
430 if ((uid == 0) || in_tgroup) {
431 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 432 sizeof(sessiond_sock_path));
08a9c49f 433 }
2269e89e 434
08a9c49f
TD
435 if (uid != 0) {
436 if (in_tgroup) {
437 /* Tracing group */
438 ret = try_connect_sessiond(sessiond_sock_path);
439 if (ret >= 0) {
440 goto end;
2269e89e 441 }
08a9c49f 442 /* Global session daemon not available... */
2269e89e 443 }
08a9c49f
TD
444 /* ...or not in tracing group (and not root), default */
445
446 /*
447 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
448 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
449 */
450 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
451 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
452 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
2f70b271 453 goto error;
947308c4 454 }
947308c4 455 }
08a9c49f 456end:
947308c4 457 return 0;
2f70b271
DG
458
459error:
460 return -1;
947308c4
DG
461}
462
65beb5ff
DG
463/*
464 * Connect to the LTTng session daemon.
465 *
466 * On success, return 0. On error, return -1.
467 */
468static int connect_sessiond(void)
469{
470 int ret;
471
472 ret = set_session_daemon_path();
473 if (ret < 0) {
2f70b271 474 goto error;
65beb5ff
DG
475 }
476
477 /* Connect to the sesssion daemon */
478 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
479 if (ret < 0) {
2f70b271 480 goto error;
65beb5ff
DG
481 }
482
483 sessiond_socket = ret;
484 connected = 1;
485
486 return 0;
2f70b271
DG
487
488error:
489 return -1;
65beb5ff
DG
490}
491
492/*
1c8d13c8
TD
493 * Clean disconnect from the session daemon.
494 * On success, return 0. On error, return -1.
65beb5ff
DG
495 */
496static int disconnect_sessiond(void)
497{
498 int ret = 0;
499
500 if (connected) {
501 ret = lttcomm_close_unix_sock(sessiond_socket);
502 sessiond_socket = 0;
503 connected = 0;
504 }
505
506 return ret;
507}
508
35a6fdb7 509/*
cd80958d 510 * Ask the session daemon a specific command and put the data into buf.
53a80697 511 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 512 *
af87c45a 513 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 514 */
53a80697 515static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 516 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
517{
518 int ret;
519 size_t size;
520 void *data = NULL;
cd80958d 521 struct lttcomm_lttng_msg llm;
65beb5ff
DG
522
523 ret = connect_sessiond();
524 if (ret < 0) {
2f70b271 525 ret = -LTTNG_ERR_NO_SESSIOND;
65beb5ff
DG
526 goto end;
527 }
528
65beb5ff 529 /* Send command to session daemon */
cd80958d 530 ret = send_session_msg(lsm);
65beb5ff 531 if (ret < 0) {
2f70b271 532 /* Ret value is a valid lttng error code. */
65beb5ff
DG
533 goto end;
534 }
53a80697
MD
535 /* Send var len data */
536 ret = send_session_varlen(vardata, varlen);
537 if (ret < 0) {
2f70b271 538 /* Ret value is a valid lttng error code. */
53a80697
MD
539 goto end;
540 }
65beb5ff
DG
541
542 /* Get header from data transmission */
543 ret = recv_data_sessiond(&llm, sizeof(llm));
544 if (ret < 0) {
2f70b271 545 /* Ret value is a valid lttng error code. */
65beb5ff
DG
546 goto end;
547 }
548
549 /* Check error code if OK */
f73fabfd 550 if (llm.ret_code != LTTNG_OK) {
65beb5ff
DG
551 ret = -llm.ret_code;
552 goto end;
553 }
554
555 size = llm.data_size;
556 if (size == 0) {
874d3f84 557 /* If client free with size 0 */
a45d5536
DG
558 if (buf != NULL) {
559 *buf = NULL;
560 }
7d29a247 561 ret = 0;
65beb5ff
DG
562 goto end;
563 }
564
565 data = (void*) malloc(size);
566
567 /* Get payload data */
568 ret = recv_data_sessiond(data, size);
569 if (ret < 0) {
570 free(data);
571 goto end;
572 }
573
83009e5e
DG
574 /*
575 * Extra protection not to dereference a NULL pointer. If buf is NULL at
576 * this point, an error is returned and data is freed.
577 */
578 if (buf == NULL) {
2f70b271 579 ret = -LTTNG_ERR_INVALID;
83009e5e
DG
580 free(data);
581 goto end;
582 }
583
65beb5ff
DG
584 *buf = data;
585 ret = size;
586
587end:
588 disconnect_sessiond();
589 return ret;
590}
591
53a80697
MD
592/*
593 * Ask the session daemon a specific command and put the data into buf.
594 *
595 * Return size of data (only payload, not header) or a negative error code.
596 */
597static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
598{
599 return ask_sessiond_varlen(lsm, NULL, 0, buf);
600}
601
9f19cc17 602/*
cd80958d 603 * Create lttng handle and return pointer.
1c8d13c8 604 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 605 */
cd80958d
DG
606struct lttng_handle *lttng_create_handle(const char *session_name,
607 struct lttng_domain *domain)
9f19cc17 608{
2f70b271
DG
609 struct lttng_handle *handle = NULL;
610
611 if (domain == NULL) {
612 goto end;
613 }
cd80958d
DG
614
615 handle = malloc(sizeof(struct lttng_handle));
616 if (handle == NULL) {
2f70b271 617 PERROR("malloc handle");
cd80958d
DG
618 goto end;
619 }
620
621 /* Copy session name */
622 copy_string(handle->session_name, session_name,
623 sizeof(handle->session_name));
624
625 /* Copy lttng domain */
626 copy_lttng_domain(&handle->domain, domain);
627
628end:
629 return handle;
630}
631
632/*
633 * Destroy handle by free(3) the pointer.
634 */
635void lttng_destroy_handle(struct lttng_handle *handle)
636{
637 if (handle) {
638 free(handle);
eb354453
DG
639 }
640}
641
d9800920
DG
642/*
643 * Register an outside consumer.
1c8d13c8 644 * Returns size of returned session payload data or a negative error code.
d9800920
DG
645 */
646int lttng_register_consumer(struct lttng_handle *handle,
647 const char *socket_path)
648{
649 struct lttcomm_session_msg lsm;
650
2f70b271
DG
651 if (handle == NULL || socket_path == NULL) {
652 return -LTTNG_ERR_INVALID;
653 }
654
d9800920
DG
655 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
656 copy_string(lsm.session.name, handle->session_name,
657 sizeof(lsm.session.name));
658 copy_lttng_domain(&lsm.domain, &handle->domain);
659
660 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
661
662 return ask_sessiond(&lsm, NULL);
663}
664
1df4dedd 665/*
1c8d13c8
TD
666 * Start tracing for all traces of the session.
667 * Returns size of returned session payload data or a negative error code.
1df4dedd 668 */
6a4f824d 669int lttng_start_tracing(const char *session_name)
f3ed775e 670{
cd80958d
DG
671 struct lttcomm_session_msg lsm;
672
6a4f824d 673 if (session_name == NULL) {
2f70b271 674 return -LTTNG_ERR_INVALID;
cd80958d
DG
675 }
676
677 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
678
679 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
680
681 return ask_sessiond(&lsm, NULL);
f3ed775e 682}
1df4dedd
DG
683
684/*
1c8d13c8
TD
685 * Stop tracing for all traces of the session.
686 * Returns size of returned session payload data or a negative error code.
f3ed775e 687 */
6a4f824d 688int lttng_stop_tracing(const char *session_name)
f3ed775e 689{
cd80958d
DG
690 struct lttcomm_session_msg lsm;
691
6a4f824d 692 if (session_name == NULL) {
2f70b271 693 return -LTTNG_ERR_INVALID;
6a4f824d
DG
694 }
695
cd80958d 696 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
697
698 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
699
700 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
701}
702
703/*
1e46a50f
TD
704 * Add context to event and/or channel.
705 * If event_name is NULL, the context is applied to all events of the channel.
706 * If channel_name is NULL, a lookup of the event's channel is done.
707 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
708 *
709 * Returns the size of the returned payload data or a negative error code.
1df4dedd 710 */
cd80958d 711int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
712 struct lttng_event_context *ctx, const char *event_name,
713 const char *channel_name)
d65106b1 714{
cd80958d
DG
715 struct lttcomm_session_msg lsm;
716
9d697d3d
DG
717 /* Safety check. Both are mandatory */
718 if (handle == NULL || ctx == NULL) {
2f70b271 719 return -LTTNG_ERR_INVALID;
cd80958d
DG
720 }
721
441c16a7
MD
722 memset(&lsm, 0, sizeof(lsm));
723
cd80958d
DG
724 lsm.cmd_type = LTTNG_ADD_CONTEXT;
725
726 /* Copy channel name */
727 copy_string(lsm.u.context.channel_name, channel_name,
728 sizeof(lsm.u.context.channel_name));
729 /* Copy event name */
730 copy_string(lsm.u.context.event_name, event_name,
731 sizeof(lsm.u.context.event_name));
732
733 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 734
9d697d3d 735 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 736
cd80958d
DG
737 copy_string(lsm.session.name, handle->session_name,
738 sizeof(lsm.session.name));
739
740 return ask_sessiond(&lsm, NULL);
d65106b1
DG
741}
742
f3ed775e 743/*
1c8d13c8
TD
744 * Enable event(s) for a channel.
745 * If no event name is specified, all events are enabled.
746 * If no channel name is specified, the default 'channel0' is used.
747 * Returns size of returned session payload data or a negative error code.
f3ed775e 748 */
cd80958d 749int lttng_enable_event(struct lttng_handle *handle,
38057ed1 750 struct lttng_event *ev, const char *channel_name)
1df4dedd 751{
cd80958d
DG
752 struct lttcomm_session_msg lsm;
753
5117eeec 754 if (handle == NULL || ev == NULL) {
2f70b271 755 return -LTTNG_ERR_INVALID;
cd80958d 756 }
33a2b854 757
441c16a7
MD
758 memset(&lsm, 0, sizeof(lsm));
759
5117eeec 760 /* If no channel name, we put the default name */
94cf3c47 761 if (channel_name == NULL) {
cd80958d
DG
762 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
763 sizeof(lsm.u.enable.channel_name));
33a2b854 764 } else {
cd80958d
DG
765 copy_string(lsm.u.enable.channel_name, channel_name,
766 sizeof(lsm.u.enable.channel_name));
eb354453
DG
767 }
768
cd80958d 769 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 770
8c9ae521 771 if (ev->name[0] != '\0') {
cd80958d 772 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 773 } else {
cd80958d 774 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 775 }
8c9ae521 776 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 777
cd80958d
DG
778 copy_string(lsm.session.name, handle->session_name,
779 sizeof(lsm.session.name));
780
781 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
782}
783
53a80697
MD
784/*
785 * set filter for an event
786 * Return negative error value on error.
787 * Return size of returned session payload data if OK.
788 */
789
790int lttng_set_event_filter(struct lttng_handle *handle,
791 const char *event_name, const char *channel_name,
792 const char *filter_expression)
793{
794 struct lttcomm_session_msg lsm;
795 struct filter_parser_ctx *ctx;
796 FILE *fmem;
797 int ret = 0;
798
799 /* Safety check. */
800 if (handle == NULL) {
2f70b271 801 return -LTTNG_ERR_INVALID;
53a80697
MD
802 }
803
804 if (!filter_expression) {
805 return 0;
806 }
807
808 /*
809 * casting const to non-const, as the underlying function will
810 * use it in read-only mode.
811 */
812 fmem = lttng_fmemopen((void *) filter_expression,
813 strlen(filter_expression), "r");
814 if (!fmem) {
815 fprintf(stderr, "Error opening memory as stream\n");
ff7ef7b8 816 return -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
817 }
818 ctx = filter_parser_ctx_alloc(fmem);
819 if (!ctx) {
820 fprintf(stderr, "Error allocating parser\n");
ff7ef7b8 821 ret = -LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
822 goto alloc_error;
823 }
824 ret = filter_parser_ctx_append_ast(ctx);
825 if (ret) {
826 fprintf(stderr, "Parse error\n");
ff7ef7b8 827 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
828 goto parse_error;
829 }
830 ret = filter_visitor_set_parent(ctx);
831 if (ret) {
832 fprintf(stderr, "Set parent error\n");
ff7ef7b8 833 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
834 goto parse_error;
835 }
836 if (print_xml) {
837 ret = filter_visitor_print_xml(ctx, stdout, 0);
838 if (ret) {
839 fflush(stdout);
840 fprintf(stderr, "XML print error\n");
ff7ef7b8 841 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
842 goto parse_error;
843 }
844 }
845
846 dbg_printf("Generating IR... ");
847 fflush(stdout);
848 ret = filter_visitor_ir_generate(ctx);
849 if (ret) {
850 fprintf(stderr, "Generate IR error\n");
ff7ef7b8 851 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
852 goto parse_error;
853 }
854 dbg_printf("done\n");
855
856 dbg_printf("Validating IR... ");
857 fflush(stdout);
858 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
859 if (ret) {
ff7ef7b8 860 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
861 goto parse_error;
862 }
863 dbg_printf("done\n");
864
865 dbg_printf("Generating bytecode... ");
866 fflush(stdout);
867 ret = filter_visitor_bytecode_generate(ctx);
868 if (ret) {
869 fprintf(stderr, "Generate bytecode error\n");
ff7ef7b8 870 ret = -LTTNG_ERR_FILTER_INVAL;
53a80697
MD
871 goto parse_error;
872 }
873 dbg_printf("done\n");
874 dbg_printf("Size of bytecode generated: %u bytes.\n",
875 bytecode_get_len(&ctx->bytecode->b));
876
877 memset(&lsm, 0, sizeof(lsm));
878
879 lsm.cmd_type = LTTNG_SET_FILTER;
880
881 /* Copy channel name */
882 copy_string(lsm.u.filter.channel_name, channel_name,
883 sizeof(lsm.u.filter.channel_name));
884 /* Copy event name */
885 copy_string(lsm.u.filter.event_name, event_name,
886 sizeof(lsm.u.filter.event_name));
887 lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b)
888 + bytecode_get_len(&ctx->bytecode->b);
889
890 copy_lttng_domain(&lsm.domain, &handle->domain);
891
892 copy_string(lsm.session.name, handle->session_name,
893 sizeof(lsm.session.name));
894
895 ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
896 lsm.u.filter.bytecode_len, NULL);
897
898 filter_bytecode_free(ctx);
899 filter_ir_free(ctx);
900 filter_parser_ctx_free(ctx);
901 if (fclose(fmem) != 0) {
902 perror("fclose");
903 }
904 return ret;
905
906parse_error:
907 filter_bytecode_free(ctx);
908 filter_ir_free(ctx);
909 filter_parser_ctx_free(ctx);
910alloc_error:
911 if (fclose(fmem) != 0) {
912 perror("fclose");
913 }
914 return ret;
915}
916
1df4dedd 917/*
1c8d13c8
TD
918 * Disable event(s) of a channel and domain.
919 * If no event name is specified, all events are disabled.
920 * If no channel name is specified, the default 'channel0' is used.
921 * Returns size of returned session payload data or a negative error code.
1df4dedd 922 */
cd80958d 923int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 924 const char *channel_name)
1df4dedd 925{
cd80958d 926 struct lttcomm_session_msg lsm;
1df4dedd 927
9d697d3d 928 if (handle == NULL) {
2f70b271 929 return -LTTNG_ERR_INVALID;
cd80958d
DG
930 }
931
441c16a7
MD
932 memset(&lsm, 0, sizeof(lsm));
933
cd80958d
DG
934 if (channel_name) {
935 copy_string(lsm.u.disable.channel_name, channel_name,
936 sizeof(lsm.u.disable.channel_name));
f3ed775e 937 } else {
cd80958d
DG
938 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
939 sizeof(lsm.u.disable.channel_name));
eb354453
DG
940 }
941
cd80958d 942 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 943
f84efadf 944 if (name != NULL) {
cd80958d
DG
945 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
946 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 947 } else {
cd80958d 948 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
949 }
950
cd80958d
DG
951 copy_string(lsm.session.name, handle->session_name,
952 sizeof(lsm.session.name));
953
954 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
955}
956
957/*
1c8d13c8
TD
958 * Enable channel per domain
959 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 960 */
cd80958d 961int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 962 struct lttng_channel *chan)
a5c5a2bd 963{
cd80958d
DG
964 struct lttcomm_session_msg lsm;
965
5117eeec
DG
966 /*
967 * NULL arguments are forbidden. No default values.
968 */
969 if (handle == NULL || chan == NULL) {
2f70b271 970 return -LTTNG_ERR_INVALID;
cd80958d
DG
971 }
972
441c16a7
MD
973 memset(&lsm, 0, sizeof(lsm));
974
5117eeec 975 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 976
cd80958d
DG
977 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
978
979 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 980
cd80958d
DG
981 copy_string(lsm.session.name, handle->session_name,
982 sizeof(lsm.session.name));
983
984 return ask_sessiond(&lsm, NULL);
8c0faa1d 985}
1df4dedd 986
2ef84c95 987/*
1c8d13c8
TD
988 * All tracing will be stopped for registered events of the channel.
989 * Returns size of returned session payload data or a negative error code.
2ef84c95 990 */
cd80958d 991int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 992{
cd80958d
DG
993 struct lttcomm_session_msg lsm;
994
9d697d3d
DG
995 /* Safety check. Both are mandatory */
996 if (handle == NULL || name == NULL) {
2f70b271 997 return -LTTNG_ERR_INVALID;
cd80958d
DG
998 }
999
441c16a7
MD
1000 memset(&lsm, 0, sizeof(lsm));
1001
cd80958d 1002 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 1003
9d697d3d
DG
1004 copy_string(lsm.u.disable.channel_name, name,
1005 sizeof(lsm.u.disable.channel_name));
1006
cd80958d
DG
1007 copy_lttng_domain(&lsm.domain, &handle->domain);
1008
1009 copy_string(lsm.session.name, handle->session_name,
1010 sizeof(lsm.session.name));
1011
1012 return ask_sessiond(&lsm, NULL);
ca95a216
DG
1013}
1014
fac6795d 1015/*
1c8d13c8
TD
1016 * Lists all available tracepoints of domain.
1017 * Sets the contents of the events array.
1018 * Returns the number of lttng_event entries in events;
1019 * on error, returns a negative value.
fac6795d 1020 */
cd80958d 1021int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 1022 struct lttng_event **events)
fac6795d 1023{
052da939 1024 int ret;
cd80958d
DG
1025 struct lttcomm_session_msg lsm;
1026
9d697d3d 1027 if (handle == NULL) {
2f70b271 1028 return -LTTNG_ERR_INVALID;
cd80958d 1029 }
fac6795d 1030
cd80958d
DG
1031 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1032 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1033
cd80958d 1034 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
1035 if (ret < 0) {
1036 return ret;
eb354453 1037 }
fac6795d 1038
9f19cc17 1039 return ret / sizeof(struct lttng_event);
fac6795d
DG
1040}
1041
f37d259d
MD
1042/*
1043 * Lists all available tracepoint fields of domain.
1044 * Sets the contents of the event field array.
1045 * Returns the number of lttng_event_field entries in events;
1046 * on error, returns a negative value.
1047 */
1048int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1049 struct lttng_event_field **fields)
1050{
1051 int ret;
1052 struct lttcomm_session_msg lsm;
1053
1054 if (handle == NULL) {
2f70b271 1055 return -LTTNG_ERR_INVALID;
f37d259d
MD
1056 }
1057
1058 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1059 copy_lttng_domain(&lsm.domain, &handle->domain);
1060
1061 ret = ask_sessiond(&lsm, (void **) fields);
1062 if (ret < 0) {
1063 return ret;
1064 }
1065
1066 return ret / sizeof(struct lttng_event_field);
1067}
1068
1657e9bb 1069/*
1c8d13c8
TD
1070 * Returns a human readable string describing
1071 * the error code (a negative value).
1657e9bb 1072 */
9a745bc7 1073const char *lttng_strerror(int code)
1657e9bb 1074{
f73fabfd 1075 return error_get_str(code);
1657e9bb
DG
1076}
1077
aaf97519 1078/*
a4b92340
DG
1079 * Create a brand new session using name and url for destination.
1080 *
f73fabfd 1081 * Returns LTTNG_OK on success or a negative error code.
00e2e675 1082 */
a4b92340 1083int lttng_create_session(const char *name, const char *url)
00e2e675 1084{
a4b92340 1085 ssize_t size;
00e2e675 1086 struct lttcomm_session_msg lsm;
a4b92340 1087 struct lttng_uri *uris = NULL;
00e2e675 1088
a4b92340 1089 if (name == NULL) {
2f70b271 1090 return -LTTNG_ERR_INVALID;
00e2e675
DG
1091 }
1092
a4b92340 1093 memset(&lsm, 0, sizeof(lsm));
00e2e675 1094
a4b92340 1095 lsm.cmd_type = LTTNG_CREATE_SESSION;
00e2e675 1096 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1097
1098 /* There should never be a data URL */
1099 size = parse_str_urls_to_uri(url, NULL, &uris);
1100 if (size < 0) {
2f70b271 1101 return -LTTNG_ERR_INVALID;
00e2e675
DG
1102 }
1103
a4b92340
DG
1104 lsm.u.uri.size = size;
1105
1106 return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1107 NULL);
00e2e675
DG
1108}
1109
8028d920 1110/*
8028d920 1111 * Destroy session using name.
1c8d13c8 1112 * Returns size of returned session payload data or a negative error code.
8028d920 1113 */
843f5df9 1114int lttng_destroy_session(const char *session_name)
8028d920 1115{
cd80958d
DG
1116 struct lttcomm_session_msg lsm;
1117
843f5df9 1118 if (session_name == NULL) {
2f70b271 1119 return -LTTNG_ERR_INVALID;
cd80958d
DG
1120 }
1121
1122 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
1123
1124 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
1125
1126 return ask_sessiond(&lsm, NULL);
aaf97519
DG
1127}
1128
57167058 1129/*
57167058 1130 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1131 * Sets the contents of the sessions array.
1132 * Returns the number of lttng_session entries in sessions;
1133 * on error, returns a negative value.
57167058 1134 */
ca95a216 1135int lttng_list_sessions(struct lttng_session **sessions)
57167058 1136{
ca95a216 1137 int ret;
cd80958d 1138 struct lttcomm_session_msg lsm;
57167058 1139
cd80958d
DG
1140 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1141 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 1142 if (ret < 0) {
ca95a216 1143 return ret;
57167058
DG
1144 }
1145
ca95a216 1146 return ret / sizeof(struct lttng_session);
57167058
DG
1147}
1148
9f19cc17 1149/*
1c8d13c8
TD
1150 * Ask the session daemon for all available domains of a session.
1151 * Sets the contents of the domains array.
1152 * Returns the number of lttng_domain entries in domains;
1153 * on error, returns a negative value.
9f19cc17 1154 */
330be774 1155int lttng_list_domains(const char *session_name,
cd80958d 1156 struct lttng_domain **domains)
9f19cc17
DG
1157{
1158 int ret;
cd80958d
DG
1159 struct lttcomm_session_msg lsm;
1160
330be774 1161 if (session_name == NULL) {
2f70b271 1162 return -LTTNG_ERR_INVALID;
cd80958d 1163 }
9f19cc17 1164
cd80958d
DG
1165 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1166
330be774 1167 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
1168
1169 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1170 if (ret < 0) {
1171 return ret;
1172 }
1173
1174 return ret / sizeof(struct lttng_domain);
1175}
1176
1177/*
1c8d13c8
TD
1178 * Ask the session daemon for all available channels of a session.
1179 * Sets the contents of the channels array.
1180 * Returns the number of lttng_channel entries in channels;
1181 * on error, returns a negative value.
9f19cc17 1182 */
cd80958d
DG
1183int lttng_list_channels(struct lttng_handle *handle,
1184 struct lttng_channel **channels)
9f19cc17
DG
1185{
1186 int ret;
cd80958d
DG
1187 struct lttcomm_session_msg lsm;
1188
9d697d3d 1189 if (handle == NULL) {
2f70b271 1190 return -LTTNG_ERR_INVALID;
cd80958d
DG
1191 }
1192
1193 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1194 copy_string(lsm.session.name, handle->session_name,
1195 sizeof(lsm.session.name));
9f19cc17 1196
cd80958d 1197 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1198
cd80958d 1199 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1200 if (ret < 0) {
1201 return ret;
1202 }
1203
1204 return ret / sizeof(struct lttng_channel);
1205}
1206
1207/*
1c8d13c8
TD
1208 * Ask the session daemon for all available events of a session channel.
1209 * Sets the contents of the events array.
1210 * Returns the number of lttng_event entries in events;
1211 * on error, returns a negative value.
9f19cc17 1212 */
cd80958d
DG
1213int lttng_list_events(struct lttng_handle *handle,
1214 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1215{
1216 int ret;
cd80958d 1217 struct lttcomm_session_msg lsm;
9f19cc17 1218
9d697d3d
DG
1219 /* Safety check. An handle and channel name are mandatory */
1220 if (handle == NULL || channel_name == NULL) {
2f70b271 1221 return -LTTNG_ERR_INVALID;
cd80958d
DG
1222 }
1223
1224 lsm.cmd_type = LTTNG_LIST_EVENTS;
1225 copy_string(lsm.session.name, handle->session_name,
1226 sizeof(lsm.session.name));
1227 copy_string(lsm.u.list.channel_name, channel_name,
1228 sizeof(lsm.u.list.channel_name));
1229
1230 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1231
cd80958d 1232 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1233 if (ret < 0) {
1234 return ret;
1235 }
1236
1237 return ret / sizeof(struct lttng_event);
1238}
1239
fac6795d 1240/*
1c8d13c8
TD
1241 * Sets the tracing_group variable with name.
1242 * This function allocates memory pointed to by tracing_group.
1243 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1244 */
1245int lttng_set_tracing_group(const char *name)
1246{
9d697d3d 1247 if (name == NULL) {
2f70b271 1248 return -LTTNG_ERR_INVALID;
9d697d3d
DG
1249 }
1250
fac6795d 1251 if (asprintf(&tracing_group, "%s", name) < 0) {
2f70b271 1252 return -LTTNG_ERR_FATAL;
fac6795d
DG
1253 }
1254
1255 return 0;
1256}
1257
d0254c7c 1258/*
af87c45a 1259 * Returns size of returned session payload data or a negative error code.
d0254c7c 1260 */
cd80958d 1261int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1262 struct lttng_calibrate *calibrate)
1263{
cd80958d 1264 struct lttcomm_session_msg lsm;
d0254c7c 1265
9d697d3d
DG
1266 /* Safety check. NULL pointer are forbidden */
1267 if (handle == NULL || calibrate == NULL) {
2f70b271 1268 return -LTTNG_ERR_INVALID;
cd80958d 1269 }
d0254c7c 1270
cd80958d
DG
1271 lsm.cmd_type = LTTNG_CALIBRATE;
1272 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1273
cd80958d
DG
1274 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1275
1276 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
1277}
1278
5edd7e09
DG
1279/*
1280 * Set default channel attributes.
441c16a7 1281 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1282 */
1283void lttng_channel_set_default_attr(struct lttng_domain *domain,
1284 struct lttng_channel_attr *attr)
1285{
1286 /* Safety check */
1287 if (attr == NULL || domain == NULL) {
1288 return;
1289 }
1290
eacaa7a6
DS
1291 memset(attr, 0, sizeof(struct lttng_channel_attr));
1292
5edd7e09
DG
1293 switch (domain->type) {
1294 case LTTNG_DOMAIN_KERNEL:
1295 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1296 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1297 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1298
1299 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1300 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1301 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1302 break;
1303 case LTTNG_DOMAIN_UST:
d78d6610 1304#if 0
5edd7e09
DG
1305 case LTTNG_DOMAIN_UST_EXEC_NAME:
1306 case LTTNG_DOMAIN_UST_PID:
1307 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 1308#endif
5edd7e09
DG
1309 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1310 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1311 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1312
1313 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
1314 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
1315 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
1316 break;
1317 default:
441c16a7 1318 /* Default behavior: leave set to 0. */
5edd7e09
DG
1319 break;
1320 }
1321}
1322
fac6795d 1323/*
2269e89e 1324 * Check if session daemon is alive.
fac6795d 1325 *
2269e89e 1326 * Return 1 if alive or 0 if not.
1c8d13c8 1327 * On error returns a negative value.
fac6795d 1328 */
947308c4 1329int lttng_session_daemon_alive(void)
fac6795d
DG
1330{
1331 int ret;
1332
1333 ret = set_session_daemon_path();
1334 if (ret < 0) {
947308c4 1335 /* Error */
fac6795d
DG
1336 return ret;
1337 }
1338
2269e89e 1339 if (strlen(sessiond_sock_path) == 0) {
2f70b271
DG
1340 /*
1341 * No socket path set. Weird error which means the constructor was not
1342 * called.
1343 */
1344 assert(0);
fac6795d
DG
1345 }
1346
2269e89e 1347 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1348 if (ret < 0) {
1349 /* Not alive */
1350 return 0;
1351 }
7d8234d9 1352
947308c4
DG
1353 /* Is alive */
1354 return 1;
fac6795d
DG
1355}
1356
00e2e675 1357/*
a4b92340 1358 * Set URL for a consumer for a session and domain.
00e2e675
DG
1359 *
1360 * Return 0 on success, else a negative value.
1361 */
a4b92340
DG
1362int lttng_set_consumer_url(struct lttng_handle *handle,
1363 const char *control_url, const char *data_url)
00e2e675 1364{
a4b92340 1365 ssize_t size;
00e2e675 1366 struct lttcomm_session_msg lsm;
a4b92340 1367 struct lttng_uri *uris = NULL;
00e2e675 1368
a4b92340 1369 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2f70b271 1370 return -LTTNG_ERR_INVALID;
00e2e675
DG
1371 }
1372
a4b92340
DG
1373 memset(&lsm, 0, sizeof(lsm));
1374
00e2e675
DG
1375 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1376
1377 copy_string(lsm.session.name, handle->session_name,
1378 sizeof(lsm.session.name));
1379 copy_lttng_domain(&lsm.domain, &handle->domain);
1380
a4b92340
DG
1381 size = parse_str_urls_to_uri(control_url, data_url, &uris);
1382 if (size < 0) {
2f70b271 1383 return -LTTNG_ERR_INVALID;
a4b92340
DG
1384 }
1385
1386 lsm.u.uri.size = size;
00e2e675 1387
a4b92340
DG
1388 return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1389 NULL);
00e2e675
DG
1390}
1391
1392/*
1393 * Enable consumer for a session and domain.
1394 *
1395 * Return 0 on success, else a negative value.
1396 */
1397int lttng_enable_consumer(struct lttng_handle *handle)
1398{
1399 struct lttcomm_session_msg lsm;
1400
1401 if (handle == NULL) {
2f70b271 1402 return -LTTNG_ERR_INVALID;
00e2e675
DG
1403 }
1404
1405 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1406
1407 copy_string(lsm.session.name, handle->session_name,
1408 sizeof(lsm.session.name));
1409 copy_lttng_domain(&lsm.domain, &handle->domain);
1410
1411 return ask_sessiond(&lsm, NULL);
1412}
1413
1414/*
1415 * Disable consumer for a session and domain.
1416 *
1417 * Return 0 on success, else a negative value.
1418 */
1419int lttng_disable_consumer(struct lttng_handle *handle)
1420{
1421 struct lttcomm_session_msg lsm;
1422
1423 if (handle == NULL) {
2f70b271 1424 return -LTTNG_ERR_INVALID;
00e2e675
DG
1425 }
1426
1427 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1428
1429 copy_string(lsm.session.name, handle->session_name,
1430 sizeof(lsm.session.name));
1431 copy_lttng_domain(&lsm.domain, &handle->domain);
1432
1433 return ask_sessiond(&lsm, NULL);
1434}
1435
44a5e5eb
DG
1436/*
1437 * Set health socket path by putting it in the global health_sock_path
1438 * variable.
1439 *
1440 * Returns 0 on success or assert(0) on ENOMEM.
1441 */
1442static int set_health_socket_path(void)
1443{
1444 int ret;
1445 int in_tgroup = 0; /* In tracing group */
1446 uid_t uid;
1447 const char *home;
1448
1449 uid = getuid();
1450
1451 if (uid != 0) {
1452 /* Are we in the tracing group ? */
1453 in_tgroup = check_tracing_group(tracing_group);
1454 }
1455
1456 if ((uid == 0) || in_tgroup) {
1457 copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK,
1458 sizeof(health_sock_path));
1459 }
1460
1461 if (uid != 0) {
1462 /*
1463 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1464 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1465 */
1466 home = getenv("HOME");
1467 if (home == NULL) {
1468 /* Fallback in /tmp .. */
1469 home = "/tmp";
1470 }
1471
1472 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1473 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1474 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1475 /* ENOMEM at this point... just kill the control lib. */
1476 assert(0);
1477 }
1478 }
1479
1480 return 0;
1481}
1482
1483/*
1484 * Check session daemon health for a specific health component.
1485 *
2f70b271 1486 * Return 0 if health is OK or else 1 if BAD.
44a5e5eb 1487 *
2f70b271 1488 * Any other negative value is a lttng error code which can be translated with
44a5e5eb
DG
1489 * lttng_strerror().
1490 */
1491int lttng_health_check(enum lttng_health_component c)
1492{
1493 int sock, ret;
1494 struct lttcomm_health_msg msg;
1495 struct lttcomm_health_data reply;
1496
1497 /* Connect to the sesssion daemon */
1498 sock = lttcomm_connect_unix_sock(health_sock_path);
1499 if (sock < 0) {
2f70b271 1500 ret = -LTTNG_ERR_NO_SESSIOND;
44a5e5eb
DG
1501 goto error;
1502 }
1503
1504 msg.cmd = LTTNG_HEALTH_CHECK;
1505 msg.component = c;
1506
1507 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1508 if (ret < 0) {
2f70b271 1509 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1510 goto close_error;
1511 }
1512
1513 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1514 if (ret < 0) {
2f70b271 1515 ret = -LTTNG_ERR_FATAL;
44a5e5eb
DG
1516 goto close_error;
1517 }
1518
1519 ret = reply.ret_code;
1520
1521close_error:
1522 close(sock);
1523
1524error:
1525 return ret;
1526}
1527
07424f16
DG
1528/*
1529 * This is an extension of create session that is ONLY and SHOULD only be used
1530 * by the lttng command line program. It exists to avoid using URI parsing in
1531 * the lttng client.
1532 *
1533 * We need the date and time for the trace path subdirectory for the case where
1534 * the user does NOT define one using either -o or -U. Using the normal
1535 * lttng_create_session API call, we have no clue on the session daemon side if
1536 * the URL was generated automatically by the client or define by the user.
1537 *
1538 * So this function "wrapper" is hidden from the public API, takes the datetime
1539 * string and appends it if necessary to the URI subdirectory before sending it
1540 * to the session daemon.
1541 *
1542 * With this extra function, the lttng_create_session call behavior is not
1543 * changed and the timestamp is appended to the URI on the session daemon side
1544 * if necessary.
1545 */
1546int _lttng_create_session_ext(const char *name, const char *url,
1547 const char *datetime)
1548{
1549 int ret;
1550 ssize_t size;
1551 struct lttcomm_session_msg lsm;
1552 struct lttng_uri *uris = NULL;
1553
1554 if (name == NULL || datetime == NULL) {
2f70b271 1555 return -LTTNG_ERR_INVALID;
07424f16
DG
1556 }
1557
1558 memset(&lsm, 0, sizeof(lsm));
1559
1560 lsm.cmd_type = LTTNG_CREATE_SESSION;
1561 if (!strncmp(name, DEFAULT_SESSION_NAME, strlen(DEFAULT_SESSION_NAME))) {
1562 ret = snprintf(lsm.session.name, sizeof(lsm.session.name), "%s-%s",
1563 name, datetime);
1564 if (ret < 0) {
1565 PERROR("snprintf session name datetime");
2f70b271 1566 return -LTTNG_ERR_FATAL;
07424f16
DG
1567 }
1568 } else {
1569 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1570 }
1571
1572 /* There should never be a data URL */
1573 size = parse_str_urls_to_uri(url, NULL, &uris);
1574 if (size < 0) {
2f70b271 1575 return -LTTNG_ERR_INVALID;
07424f16
DG
1576 }
1577
1578 lsm.u.uri.size = size;
1579
1580 if (uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
ad20f474 1581 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", name,
07424f16
DG
1582 datetime);
1583 if (ret < 0) {
1584 PERROR("snprintf uri subdir");
2f70b271 1585 return -LTTNG_ERR_FATAL;
07424f16
DG
1586 }
1587 }
1588
1589 return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1590 NULL);
1591}
1592
806e2684
DG
1593/*
1594 * For a given session name, this call checks if the data is ready to be read
1595 * or is still being extracted by the consumer(s) hence not ready to be used by
1596 * any readers.
1597 */
1598int lttng_data_available(const char *session_name)
1599{
1600 int ret;
1601 struct lttcomm_session_msg lsm;
1602
1603 if (session_name == NULL) {
1604 return -LTTNG_ERR_INVALID;
1605 }
1606
1607 lsm.cmd_type = LTTNG_DATA_AVAILABLE;
1608
1609 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
1610
1611 ret = ask_sessiond(&lsm, NULL);
1612
1613 /*
1614 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1615 * getting -1 means that the reply ret_code was 1 thus meaning that the
1616 * data is available. Yes it is hackish but for now this is the only way.
1617 */
1618 if (ret == -1) {
1619 ret = 1;
1620 }
1621
1622 return ret;
1623}
1624
fac6795d
DG
1625/*
1626 * lib constructor
1627 */
1628static void __attribute__((constructor)) init()
1629{
1630 /* Set default session group */
bbccc3d2 1631 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
44a5e5eb
DG
1632 /* Set socket for health check */
1633 (void) set_health_socket_path();
fac6795d 1634}
This page took 0.185863 seconds and 4 git commands to generate.