Extend API and remove lttng_uri from lttng.h
[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
53a80697 37#include "filter-ast.h"
95b9bd90 38#include "filter-parser.h"
53a80697
MD
39#include "filter-bytecode.h"
40#include "memstream.h"
41
42#ifdef DEBUG
43const int print_xml = 1;
44#define dbg_printf(fmt, args...) \
45 printf("[debug liblttng-ctl] " fmt, ## args)
46#else
47const int print_xml = 0;
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) {
e065084a
DG
256 ret = -ENOTCONN;
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));
e065084a
DG
264
265end:
266 return ret;
267}
268
53a80697
MD
269/*
270 * Send var len data to the session daemon.
271 *
272 * On success, returns the number of bytes sent (>=0)
273 * On error, returns -1
274 */
275static int send_session_varlen(void *data, size_t len)
276{
277 int ret;
278
279 if (!connected) {
280 ret = -ENOTCONN;
281 goto end;
282 }
a4b92340 283
53a80697
MD
284 if (!data || !len) {
285 ret = 0;
286 goto end;
287 }
288
289 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
290
291end:
292 return ret;
293}
294
e065084a 295/*
cd80958d 296 * Receive data from the sessiond socket.
e065084a 297 *
1c8d13c8
TD
298 * On success, returns the number of bytes received (>=0)
299 * On error, returns -1 (recvmsg() error) or -ENOTCONN
e065084a 300 */
ca95a216 301static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
302{
303 int ret;
304
305 if (!connected) {
306 ret = -ENOTCONN;
307 goto end;
fac6795d
DG
308 }
309
ca95a216 310 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 311
e065084a 312end:
fac6795d
DG
313 return ret;
314}
315
316/*
1c8d13c8 317 * Check if we are in the specified group.
65beb5ff 318 *
2269e89e 319 * If yes return 1, else return -1.
947308c4
DG
320 */
321static int check_tracing_group(const char *grp_name)
322{
323 struct group *grp_tracing; /* no free(). See getgrnam(3) */
324 gid_t *grp_list;
325 int grp_list_size, grp_id, i;
326 int ret = -1;
327
328 /* Get GID of group 'tracing' */
329 grp_tracing = getgrnam(grp_name);
b4d8603b
MD
330 if (!grp_tracing) {
331 /* If grp_tracing is NULL, the group does not exist. */
947308c4
DG
332 goto end;
333 }
334
335 /* Get number of supplementary group IDs */
336 grp_list_size = getgroups(0, NULL);
337 if (grp_list_size < 0) {
338 perror("getgroups");
339 goto end;
340 }
341
342 /* Alloc group list of the right size */
343 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392 344 if (!grp_list) {
1c8d13c8 345 perror("malloc");
00795392
MD
346 goto end;
347 }
947308c4 348 grp_id = getgroups(grp_list_size, grp_list);
1c8d13c8 349 if (grp_id < 0) {
947308c4
DG
350 perror("getgroups");
351 goto free_list;
352 }
353
354 for (i = 0; i < grp_list_size; i++) {
355 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 356 ret = 1;
947308c4
DG
357 break;
358 }
359 }
360
361free_list:
362 free(grp_list);
363
364end:
365 return ret;
366}
367
368/*
2269e89e
DG
369 * Try connect to session daemon with sock_path.
370 *
371 * Return 0 on success, else -1
372 */
373static int try_connect_sessiond(const char *sock_path)
374{
375 int ret;
376
377 /* If socket exist, we check if the daemon listens for connect. */
378 ret = access(sock_path, F_OK);
379 if (ret < 0) {
380 /* Not alive */
381 return -1;
382 }
383
384 ret = lttcomm_connect_unix_sock(sock_path);
385 if (ret < 0) {
386 /* Not alive */
387 return -1;
388 }
389
390 ret = lttcomm_close_unix_sock(ret);
391 if (ret < 0) {
392 perror("lttcomm_close_unix_sock");
393 }
394
395 return 0;
396}
397
398/*
1c8d13c8
TD
399 * Set sessiond socket path by putting it in the global
400 * sessiond_sock_path variable.
401 * Returns 0 on success,
402 * -ENOMEM on failure (the sessiond socket path is somehow too long)
947308c4
DG
403 */
404static int set_session_daemon_path(void)
405{
406 int ret;
2269e89e
DG
407 int in_tgroup = 0; /* In tracing group */
408 uid_t uid;
409
410 uid = getuid();
947308c4 411
2269e89e
DG
412 if (uid != 0) {
413 /* Are we in the tracing group ? */
414 in_tgroup = check_tracing_group(tracing_group);
415 }
416
08a9c49f
TD
417 if ((uid == 0) || in_tgroup) {
418 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
2269e89e 419 sizeof(sessiond_sock_path));
08a9c49f 420 }
2269e89e 421
08a9c49f
TD
422 if (uid != 0) {
423 if (in_tgroup) {
424 /* Tracing group */
425 ret = try_connect_sessiond(sessiond_sock_path);
426 if (ret >= 0) {
427 goto end;
2269e89e 428 }
08a9c49f 429 /* Global session daemon not available... */
2269e89e 430 }
08a9c49f
TD
431 /* ...or not in tracing group (and not root), default */
432
433 /*
434 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
435 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
436 */
437 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
439 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
947308c4
DG
440 return -ENOMEM;
441 }
947308c4 442 }
08a9c49f 443end:
947308c4
DG
444 return 0;
445}
446
65beb5ff
DG
447/*
448 * Connect to the LTTng session daemon.
449 *
450 * On success, return 0. On error, return -1.
451 */
452static int connect_sessiond(void)
453{
454 int ret;
455
456 ret = set_session_daemon_path();
457 if (ret < 0) {
1c8d13c8 458 return -1; /* set_session_daemon_path() returns -ENOMEM */
65beb5ff
DG
459 }
460
461 /* Connect to the sesssion daemon */
462 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
463 if (ret < 0) {
464 return ret;
465 }
466
467 sessiond_socket = ret;
468 connected = 1;
469
470 return 0;
471}
472
473/*
1c8d13c8
TD
474 * Clean disconnect from the session daemon.
475 * On success, return 0. On error, return -1.
65beb5ff
DG
476 */
477static int disconnect_sessiond(void)
478{
479 int ret = 0;
480
481 if (connected) {
482 ret = lttcomm_close_unix_sock(sessiond_socket);
483 sessiond_socket = 0;
484 connected = 0;
485 }
486
487 return ret;
488}
489
35a6fdb7 490/*
cd80958d 491 * Ask the session daemon a specific command and put the data into buf.
53a80697 492 * Takes extra var. len. data as input to send to the session daemon.
65beb5ff 493 *
af87c45a 494 * Return size of data (only payload, not header) or a negative error code.
65beb5ff 495 */
53a80697 496static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
a4b92340 497 void *vardata, size_t varlen, void **buf)
65beb5ff
DG
498{
499 int ret;
500 size_t size;
501 void *data = NULL;
cd80958d 502 struct lttcomm_lttng_msg llm;
65beb5ff
DG
503
504 ret = connect_sessiond();
505 if (ret < 0) {
506 goto end;
507 }
508
65beb5ff 509 /* Send command to session daemon */
cd80958d 510 ret = send_session_msg(lsm);
65beb5ff
DG
511 if (ret < 0) {
512 goto end;
513 }
53a80697
MD
514 /* Send var len data */
515 ret = send_session_varlen(vardata, varlen);
516 if (ret < 0) {
517 goto end;
518 }
65beb5ff
DG
519
520 /* Get header from data transmission */
521 ret = recv_data_sessiond(&llm, sizeof(llm));
522 if (ret < 0) {
523 goto end;
524 }
525
526 /* Check error code if OK */
527 if (llm.ret_code != LTTCOMM_OK) {
528 ret = -llm.ret_code;
529 goto end;
530 }
531
532 size = llm.data_size;
533 if (size == 0) {
874d3f84 534 /* If client free with size 0 */
a45d5536
DG
535 if (buf != NULL) {
536 *buf = NULL;
537 }
7d29a247 538 ret = 0;
65beb5ff
DG
539 goto end;
540 }
541
542 data = (void*) malloc(size);
543
544 /* Get payload data */
545 ret = recv_data_sessiond(data, size);
546 if (ret < 0) {
547 free(data);
548 goto end;
549 }
550
83009e5e
DG
551 /*
552 * Extra protection not to dereference a NULL pointer. If buf is NULL at
553 * this point, an error is returned and data is freed.
554 */
555 if (buf == NULL) {
556 ret = -1;
557 free(data);
558 goto end;
559 }
560
65beb5ff
DG
561 *buf = data;
562 ret = size;
563
564end:
565 disconnect_sessiond();
566 return ret;
567}
568
53a80697
MD
569/*
570 * Ask the session daemon a specific command and put the data into buf.
571 *
572 * Return size of data (only payload, not header) or a negative error code.
573 */
574static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
575{
576 return ask_sessiond_varlen(lsm, NULL, 0, buf);
577}
578
9f19cc17 579/*
cd80958d 580 * Create lttng handle and return pointer.
1c8d13c8 581 * The returned pointer will be NULL in case of malloc() error.
9f19cc17 582 */
cd80958d
DG
583struct lttng_handle *lttng_create_handle(const char *session_name,
584 struct lttng_domain *domain)
9f19cc17 585{
cd80958d
DG
586 struct lttng_handle *handle;
587
588 handle = malloc(sizeof(struct lttng_handle));
589 if (handle == NULL) {
590 perror("malloc handle");
591 goto end;
592 }
593
594 /* Copy session name */
595 copy_string(handle->session_name, session_name,
596 sizeof(handle->session_name));
597
598 /* Copy lttng domain */
599 copy_lttng_domain(&handle->domain, domain);
600
601end:
602 return handle;
603}
604
605/*
606 * Destroy handle by free(3) the pointer.
607 */
608void lttng_destroy_handle(struct lttng_handle *handle)
609{
610 if (handle) {
611 free(handle);
eb354453
DG
612 }
613}
614
d9800920
DG
615/*
616 * Register an outside consumer.
1c8d13c8 617 * Returns size of returned session payload data or a negative error code.
d9800920
DG
618 */
619int lttng_register_consumer(struct lttng_handle *handle,
620 const char *socket_path)
621{
622 struct lttcomm_session_msg lsm;
623
624 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
625 copy_string(lsm.session.name, handle->session_name,
626 sizeof(lsm.session.name));
627 copy_lttng_domain(&lsm.domain, &handle->domain);
628
629 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
630
631 return ask_sessiond(&lsm, NULL);
632}
633
1df4dedd 634/*
1c8d13c8
TD
635 * Start tracing for all traces of the session.
636 * Returns size of returned session payload data or a negative error code.
1df4dedd 637 */
6a4f824d 638int lttng_start_tracing(const char *session_name)
f3ed775e 639{
cd80958d
DG
640 struct lttcomm_session_msg lsm;
641
6a4f824d 642 if (session_name == NULL) {
cd80958d
DG
643 return -1;
644 }
645
646 lsm.cmd_type = LTTNG_START_TRACE;
6a4f824d
DG
647
648 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
649
650 return ask_sessiond(&lsm, NULL);
f3ed775e 651}
1df4dedd
DG
652
653/*
1c8d13c8
TD
654 * Stop tracing for all traces of the session.
655 * Returns size of returned session payload data or a negative error code.
f3ed775e 656 */
6a4f824d 657int lttng_stop_tracing(const char *session_name)
f3ed775e 658{
cd80958d
DG
659 struct lttcomm_session_msg lsm;
660
6a4f824d
DG
661 if (session_name == NULL) {
662 return -1;
663 }
664
cd80958d 665 lsm.cmd_type = LTTNG_STOP_TRACE;
6a4f824d
DG
666
667 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
668
669 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
670}
671
672/*
1e46a50f
TD
673 * Add context to event and/or channel.
674 * If event_name is NULL, the context is applied to all events of the channel.
675 * If channel_name is NULL, a lookup of the event's channel is done.
676 * If both are NULL, the context is applied to all events of all channels.
af87c45a
DG
677 *
678 * Returns the size of the returned payload data or a negative error code.
1df4dedd 679 */
cd80958d 680int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
681 struct lttng_event_context *ctx, const char *event_name,
682 const char *channel_name)
d65106b1 683{
cd80958d
DG
684 struct lttcomm_session_msg lsm;
685
9d697d3d
DG
686 /* Safety check. Both are mandatory */
687 if (handle == NULL || ctx == NULL) {
cd80958d
DG
688 return -1;
689 }
690
441c16a7
MD
691 memset(&lsm, 0, sizeof(lsm));
692
cd80958d
DG
693 lsm.cmd_type = LTTNG_ADD_CONTEXT;
694
695 /* Copy channel name */
696 copy_string(lsm.u.context.channel_name, channel_name,
697 sizeof(lsm.u.context.channel_name));
698 /* Copy event name */
699 copy_string(lsm.u.context.event_name, event_name,
700 sizeof(lsm.u.context.event_name));
701
702 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 703
9d697d3d 704 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1 705
cd80958d
DG
706 copy_string(lsm.session.name, handle->session_name,
707 sizeof(lsm.session.name));
708
709 return ask_sessiond(&lsm, NULL);
d65106b1
DG
710}
711
f3ed775e 712/*
1c8d13c8
TD
713 * Enable event(s) for a channel.
714 * If no event name is specified, all events are enabled.
715 * If no channel name is specified, the default 'channel0' is used.
716 * Returns size of returned session payload data or a negative error code.
f3ed775e 717 */
cd80958d 718int lttng_enable_event(struct lttng_handle *handle,
38057ed1 719 struct lttng_event *ev, const char *channel_name)
1df4dedd 720{
cd80958d
DG
721 struct lttcomm_session_msg lsm;
722
5117eeec 723 if (handle == NULL || ev == NULL) {
cd80958d
DG
724 return -1;
725 }
33a2b854 726
441c16a7
MD
727 memset(&lsm, 0, sizeof(lsm));
728
5117eeec 729 /* If no channel name, we put the default name */
94cf3c47 730 if (channel_name == NULL) {
cd80958d
DG
731 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
732 sizeof(lsm.u.enable.channel_name));
33a2b854 733 } else {
cd80958d
DG
734 copy_string(lsm.u.enable.channel_name, channel_name,
735 sizeof(lsm.u.enable.channel_name));
eb354453
DG
736 }
737
cd80958d 738 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 739
8c9ae521 740 if (ev->name[0] != '\0') {
cd80958d 741 lsm.cmd_type = LTTNG_ENABLE_EVENT;
0d0c377a 742 } else {
cd80958d 743 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e 744 }
8c9ae521 745 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
f3ed775e 746
cd80958d
DG
747 copy_string(lsm.session.name, handle->session_name,
748 sizeof(lsm.session.name));
749
750 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
751}
752
53a80697
MD
753/*
754 * set filter for an event
755 * Return negative error value on error.
756 * Return size of returned session payload data if OK.
757 */
758
759int lttng_set_event_filter(struct lttng_handle *handle,
760 const char *event_name, const char *channel_name,
761 const char *filter_expression)
762{
763 struct lttcomm_session_msg lsm;
764 struct filter_parser_ctx *ctx;
765 FILE *fmem;
766 int ret = 0;
767
768 /* Safety check. */
769 if (handle == NULL) {
770 return -1;
771 }
772
773 if (!filter_expression) {
774 return 0;
775 }
776
777 /*
778 * casting const to non-const, as the underlying function will
779 * use it in read-only mode.
780 */
781 fmem = lttng_fmemopen((void *) filter_expression,
782 strlen(filter_expression), "r");
783 if (!fmem) {
784 fprintf(stderr, "Error opening memory as stream\n");
785 return -ENOMEM;
786 }
787 ctx = filter_parser_ctx_alloc(fmem);
788 if (!ctx) {
789 fprintf(stderr, "Error allocating parser\n");
790 ret = -ENOMEM;
791 goto alloc_error;
792 }
793 ret = filter_parser_ctx_append_ast(ctx);
794 if (ret) {
795 fprintf(stderr, "Parse error\n");
796 ret = -EINVAL;
797 goto parse_error;
798 }
799 ret = filter_visitor_set_parent(ctx);
800 if (ret) {
801 fprintf(stderr, "Set parent error\n");
802 ret = -EINVAL;
803 goto parse_error;
804 }
805 if (print_xml) {
806 ret = filter_visitor_print_xml(ctx, stdout, 0);
807 if (ret) {
808 fflush(stdout);
809 fprintf(stderr, "XML print error\n");
810 ret = -EINVAL;
811 goto parse_error;
812 }
813 }
814
815 dbg_printf("Generating IR... ");
816 fflush(stdout);
817 ret = filter_visitor_ir_generate(ctx);
818 if (ret) {
819 fprintf(stderr, "Generate IR error\n");
820 ret = -EINVAL;
821 goto parse_error;
822 }
823 dbg_printf("done\n");
824
825 dbg_printf("Validating IR... ");
826 fflush(stdout);
827 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
828 if (ret) {
829 ret = -EINVAL;
830 goto parse_error;
831 }
832 dbg_printf("done\n");
833
834 dbg_printf("Generating bytecode... ");
835 fflush(stdout);
836 ret = filter_visitor_bytecode_generate(ctx);
837 if (ret) {
838 fprintf(stderr, "Generate bytecode error\n");
839 ret = -EINVAL;
840 goto parse_error;
841 }
842 dbg_printf("done\n");
843 dbg_printf("Size of bytecode generated: %u bytes.\n",
844 bytecode_get_len(&ctx->bytecode->b));
845
846 memset(&lsm, 0, sizeof(lsm));
847
848 lsm.cmd_type = LTTNG_SET_FILTER;
849
850 /* Copy channel name */
851 copy_string(lsm.u.filter.channel_name, channel_name,
852 sizeof(lsm.u.filter.channel_name));
853 /* Copy event name */
854 copy_string(lsm.u.filter.event_name, event_name,
855 sizeof(lsm.u.filter.event_name));
856 lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b)
857 + bytecode_get_len(&ctx->bytecode->b);
858
859 copy_lttng_domain(&lsm.domain, &handle->domain);
860
861 copy_string(lsm.session.name, handle->session_name,
862 sizeof(lsm.session.name));
863
864 ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
865 lsm.u.filter.bytecode_len, NULL);
866
867 filter_bytecode_free(ctx);
868 filter_ir_free(ctx);
869 filter_parser_ctx_free(ctx);
870 if (fclose(fmem) != 0) {
871 perror("fclose");
872 }
873 return ret;
874
875parse_error:
876 filter_bytecode_free(ctx);
877 filter_ir_free(ctx);
878 filter_parser_ctx_free(ctx);
879alloc_error:
880 if (fclose(fmem) != 0) {
881 perror("fclose");
882 }
883 return ret;
884}
885
1df4dedd 886/*
1c8d13c8
TD
887 * Disable event(s) of a channel and domain.
888 * If no event name is specified, all events are disabled.
889 * If no channel name is specified, the default 'channel0' is used.
890 * Returns size of returned session payload data or a negative error code.
1df4dedd 891 */
cd80958d 892int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 893 const char *channel_name)
1df4dedd 894{
cd80958d 895 struct lttcomm_session_msg lsm;
1df4dedd 896
9d697d3d 897 if (handle == NULL) {
cd80958d
DG
898 return -1;
899 }
900
441c16a7
MD
901 memset(&lsm, 0, sizeof(lsm));
902
cd80958d
DG
903 if (channel_name) {
904 copy_string(lsm.u.disable.channel_name, channel_name,
905 sizeof(lsm.u.disable.channel_name));
f3ed775e 906 } else {
cd80958d
DG
907 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
908 sizeof(lsm.u.disable.channel_name));
eb354453
DG
909 }
910
cd80958d 911 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38 912
f84efadf 913 if (name != NULL) {
cd80958d
DG
914 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
915 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 916 } else {
cd80958d 917 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
918 }
919
cd80958d
DG
920 copy_string(lsm.session.name, handle->session_name,
921 sizeof(lsm.session.name));
922
923 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
924}
925
926/*
1c8d13c8
TD
927 * Enable channel per domain
928 * Returns size of returned session payload data or a negative error code.
a5c5a2bd 929 */
cd80958d 930int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 931 struct lttng_channel *chan)
a5c5a2bd 932{
cd80958d
DG
933 struct lttcomm_session_msg lsm;
934
5117eeec
DG
935 /*
936 * NULL arguments are forbidden. No default values.
937 */
938 if (handle == NULL || chan == NULL) {
cd80958d
DG
939 return -1;
940 }
941
441c16a7
MD
942 memset(&lsm, 0, sizeof(lsm));
943
5117eeec 944 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
7d29a247 945
cd80958d
DG
946 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
947
948 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 949
cd80958d
DG
950 copy_string(lsm.session.name, handle->session_name,
951 sizeof(lsm.session.name));
952
953 return ask_sessiond(&lsm, NULL);
8c0faa1d 954}
1df4dedd 955
2ef84c95 956/*
1c8d13c8
TD
957 * All tracing will be stopped for registered events of the channel.
958 * Returns size of returned session payload data or a negative error code.
2ef84c95 959 */
cd80958d 960int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 961{
cd80958d
DG
962 struct lttcomm_session_msg lsm;
963
9d697d3d
DG
964 /* Safety check. Both are mandatory */
965 if (handle == NULL || name == NULL) {
cd80958d
DG
966 return -1;
967 }
968
441c16a7
MD
969 memset(&lsm, 0, sizeof(lsm));
970
cd80958d 971 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 972
9d697d3d
DG
973 copy_string(lsm.u.disable.channel_name, name,
974 sizeof(lsm.u.disable.channel_name));
975
cd80958d
DG
976 copy_lttng_domain(&lsm.domain, &handle->domain);
977
978 copy_string(lsm.session.name, handle->session_name,
979 sizeof(lsm.session.name));
980
981 return ask_sessiond(&lsm, NULL);
ca95a216
DG
982}
983
fac6795d 984/*
1c8d13c8
TD
985 * Lists all available tracepoints of domain.
986 * Sets the contents of the events array.
987 * Returns the number of lttng_event entries in events;
988 * on error, returns a negative value.
fac6795d 989 */
cd80958d 990int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 991 struct lttng_event **events)
fac6795d 992{
052da939 993 int ret;
cd80958d
DG
994 struct lttcomm_session_msg lsm;
995
9d697d3d 996 if (handle == NULL) {
cd80958d
DG
997 return -1;
998 }
fac6795d 999
cd80958d
DG
1000 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1001 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 1002
cd80958d 1003 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
1004 if (ret < 0) {
1005 return ret;
eb354453 1006 }
fac6795d 1007
9f19cc17 1008 return ret / sizeof(struct lttng_event);
fac6795d
DG
1009}
1010
f37d259d
MD
1011/*
1012 * Lists all available tracepoint fields of domain.
1013 * Sets the contents of the event field array.
1014 * Returns the number of lttng_event_field entries in events;
1015 * on error, returns a negative value.
1016 */
1017int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1018 struct lttng_event_field **fields)
1019{
1020 int ret;
1021 struct lttcomm_session_msg lsm;
1022
1023 if (handle == NULL) {
1024 return -1;
1025 }
1026
1027 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1028 copy_lttng_domain(&lsm.domain, &handle->domain);
1029
1030 ret = ask_sessiond(&lsm, (void **) fields);
1031 if (ret < 0) {
1032 return ret;
1033 }
1034
1035 return ret / sizeof(struct lttng_event_field);
1036}
1037
1657e9bb 1038/*
1c8d13c8
TD
1039 * Returns a human readable string describing
1040 * the error code (a negative value).
1657e9bb 1041 */
9a745bc7 1042const char *lttng_strerror(int code)
1657e9bb 1043{
1c8d13c8 1044 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
7d29a247
DG
1045 if (code > -LTTCOMM_OK) {
1046 return "Ended with errors";
1657e9bb
DG
1047 }
1048
7d29a247 1049 return lttcomm_get_readable_code(code);
1657e9bb
DG
1050}
1051
aaf97519 1052/*
a4b92340
DG
1053 * Create a brand new session using name and url for destination.
1054 *
1055 * Returns LTTCOMM_OK on success or a negative error code.
00e2e675 1056 */
a4b92340 1057int lttng_create_session(const char *name, const char *url)
00e2e675 1058{
a4b92340 1059 ssize_t size;
00e2e675 1060 struct lttcomm_session_msg lsm;
a4b92340 1061 struct lttng_uri *uris = NULL;
00e2e675 1062
a4b92340 1063 if (name == NULL) {
00e2e675
DG
1064 return -1;
1065 }
1066
a4b92340 1067 memset(&lsm, 0, sizeof(lsm));
00e2e675 1068
a4b92340 1069 lsm.cmd_type = LTTNG_CREATE_SESSION;
00e2e675 1070 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
a4b92340
DG
1071
1072 /* There should never be a data URL */
1073 size = parse_str_urls_to_uri(url, NULL, &uris);
1074 if (size < 0) {
1075 return LTTCOMM_INVALID;
00e2e675
DG
1076 }
1077
a4b92340
DG
1078 lsm.u.uri.size = size;
1079
1080 return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1081 NULL);
00e2e675
DG
1082}
1083
8028d920 1084/*
8028d920 1085 * Destroy session using name.
1c8d13c8 1086 * Returns size of returned session payload data or a negative error code.
8028d920 1087 */
843f5df9 1088int lttng_destroy_session(const char *session_name)
8028d920 1089{
cd80958d
DG
1090 struct lttcomm_session_msg lsm;
1091
843f5df9 1092 if (session_name == NULL) {
cd80958d
DG
1093 return -1;
1094 }
1095
1096 lsm.cmd_type = LTTNG_DESTROY_SESSION;
843f5df9
DG
1097
1098 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
1099
1100 return ask_sessiond(&lsm, NULL);
aaf97519
DG
1101}
1102
57167058 1103/*
57167058 1104 * Ask the session daemon for all available sessions.
1c8d13c8
TD
1105 * Sets the contents of the sessions array.
1106 * Returns the number of lttng_session entries in sessions;
1107 * on error, returns a negative value.
57167058 1108 */
ca95a216 1109int lttng_list_sessions(struct lttng_session **sessions)
57167058 1110{
ca95a216 1111 int ret;
cd80958d 1112 struct lttcomm_session_msg lsm;
57167058 1113
cd80958d
DG
1114 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1115 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 1116 if (ret < 0) {
ca95a216 1117 return ret;
57167058
DG
1118 }
1119
ca95a216 1120 return ret / sizeof(struct lttng_session);
57167058
DG
1121}
1122
9f19cc17 1123/*
1c8d13c8
TD
1124 * Ask the session daemon for all available domains of a session.
1125 * Sets the contents of the domains array.
1126 * Returns the number of lttng_domain entries in domains;
1127 * on error, returns a negative value.
9f19cc17 1128 */
330be774 1129int lttng_list_domains(const char *session_name,
cd80958d 1130 struct lttng_domain **domains)
9f19cc17
DG
1131{
1132 int ret;
cd80958d
DG
1133 struct lttcomm_session_msg lsm;
1134
330be774 1135 if (session_name == NULL) {
cd80958d
DG
1136 return -1;
1137 }
9f19cc17 1138
cd80958d
DG
1139 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1140
330be774 1141 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
cd80958d
DG
1142
1143 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
1144 if (ret < 0) {
1145 return ret;
1146 }
1147
1148 return ret / sizeof(struct lttng_domain);
1149}
1150
1151/*
1c8d13c8
TD
1152 * Ask the session daemon for all available channels of a session.
1153 * Sets the contents of the channels array.
1154 * Returns the number of lttng_channel entries in channels;
1155 * on error, returns a negative value.
9f19cc17 1156 */
cd80958d
DG
1157int lttng_list_channels(struct lttng_handle *handle,
1158 struct lttng_channel **channels)
9f19cc17
DG
1159{
1160 int ret;
cd80958d
DG
1161 struct lttcomm_session_msg lsm;
1162
9d697d3d 1163 if (handle == NULL) {
cd80958d
DG
1164 return -1;
1165 }
1166
1167 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1168 copy_string(lsm.session.name, handle->session_name,
1169 sizeof(lsm.session.name));
9f19cc17 1170
cd80958d 1171 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1172
cd80958d 1173 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
1174 if (ret < 0) {
1175 return ret;
1176 }
1177
1178 return ret / sizeof(struct lttng_channel);
1179}
1180
1181/*
1c8d13c8
TD
1182 * Ask the session daemon for all available events of a session channel.
1183 * Sets the contents of the events array.
1184 * Returns the number of lttng_event entries in events;
1185 * on error, returns a negative value.
9f19cc17 1186 */
cd80958d
DG
1187int lttng_list_events(struct lttng_handle *handle,
1188 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
1189{
1190 int ret;
cd80958d 1191 struct lttcomm_session_msg lsm;
9f19cc17 1192
9d697d3d
DG
1193 /* Safety check. An handle and channel name are mandatory */
1194 if (handle == NULL || channel_name == NULL) {
cd80958d
DG
1195 return -1;
1196 }
1197
1198 lsm.cmd_type = LTTNG_LIST_EVENTS;
1199 copy_string(lsm.session.name, handle->session_name,
1200 sizeof(lsm.session.name));
1201 copy_string(lsm.u.list.channel_name, channel_name,
1202 sizeof(lsm.u.list.channel_name));
1203
1204 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 1205
cd80958d 1206 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
1207 if (ret < 0) {
1208 return ret;
1209 }
1210
1211 return ret / sizeof(struct lttng_event);
1212}
1213
fac6795d 1214/*
1c8d13c8
TD
1215 * Sets the tracing_group variable with name.
1216 * This function allocates memory pointed to by tracing_group.
1217 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
fac6795d
DG
1218 */
1219int lttng_set_tracing_group(const char *name)
1220{
9d697d3d
DG
1221 if (name == NULL) {
1222 return -1;
1223 }
1224
fac6795d
DG
1225 if (asprintf(&tracing_group, "%s", name) < 0) {
1226 return -ENOMEM;
1227 }
1228
1229 return 0;
1230}
1231
d0254c7c 1232/*
af87c45a 1233 * Returns size of returned session payload data or a negative error code.
d0254c7c 1234 */
cd80958d 1235int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
1236 struct lttng_calibrate *calibrate)
1237{
cd80958d 1238 struct lttcomm_session_msg lsm;
d0254c7c 1239
9d697d3d
DG
1240 /* Safety check. NULL pointer are forbidden */
1241 if (handle == NULL || calibrate == NULL) {
cd80958d
DG
1242 return -1;
1243 }
d0254c7c 1244
cd80958d
DG
1245 lsm.cmd_type = LTTNG_CALIBRATE;
1246 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 1247
cd80958d
DG
1248 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1249
1250 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
1251}
1252
5edd7e09
DG
1253/*
1254 * Set default channel attributes.
441c16a7 1255 * If either or both of the arguments are null, attr content is zeroe'd.
5edd7e09
DG
1256 */
1257void lttng_channel_set_default_attr(struct lttng_domain *domain,
1258 struct lttng_channel_attr *attr)
1259{
1260 /* Safety check */
1261 if (attr == NULL || domain == NULL) {
1262 return;
1263 }
1264
eacaa7a6
DS
1265 memset(attr, 0, sizeof(struct lttng_channel_attr));
1266
5edd7e09
DG
1267 switch (domain->type) {
1268 case LTTNG_DOMAIN_KERNEL:
1269 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1270 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1271 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1272
1273 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1274 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1275 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1276 break;
1277 case LTTNG_DOMAIN_UST:
d78d6610 1278#if 0
5edd7e09
DG
1279 case LTTNG_DOMAIN_UST_EXEC_NAME:
1280 case LTTNG_DOMAIN_UST_PID:
1281 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 1282#endif
5edd7e09
DG
1283 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1284 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1285 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1286
1287 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
1288 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
1289 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
1290 break;
1291 default:
441c16a7 1292 /* Default behavior: leave set to 0. */
5edd7e09
DG
1293 break;
1294 }
1295}
1296
fac6795d 1297/*
2269e89e 1298 * Check if session daemon is alive.
fac6795d 1299 *
2269e89e 1300 * Return 1 if alive or 0 if not.
1c8d13c8 1301 * On error returns a negative value.
fac6795d 1302 */
947308c4 1303int lttng_session_daemon_alive(void)
fac6795d
DG
1304{
1305 int ret;
1306
1307 ret = set_session_daemon_path();
1308 if (ret < 0) {
947308c4 1309 /* Error */
fac6795d
DG
1310 return ret;
1311 }
1312
2269e89e
DG
1313 if (strlen(sessiond_sock_path) == 0) {
1314 /* No socket path set. Weird error */
1315 return -1;
fac6795d
DG
1316 }
1317
2269e89e 1318 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
1319 if (ret < 0) {
1320 /* Not alive */
1321 return 0;
1322 }
7d8234d9 1323
947308c4
DG
1324 /* Is alive */
1325 return 1;
fac6795d
DG
1326}
1327
00e2e675 1328/*
a4b92340 1329 * Set URL for a consumer for a session and domain.
00e2e675
DG
1330 *
1331 * Return 0 on success, else a negative value.
1332 */
a4b92340
DG
1333int lttng_set_consumer_url(struct lttng_handle *handle,
1334 const char *control_url, const char *data_url)
00e2e675 1335{
a4b92340 1336 ssize_t size;
00e2e675 1337 struct lttcomm_session_msg lsm;
a4b92340 1338 struct lttng_uri *uris = NULL;
00e2e675 1339
a4b92340 1340 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
00e2e675
DG
1341 return -1;
1342 }
1343
a4b92340
DG
1344 memset(&lsm, 0, sizeof(lsm));
1345
00e2e675
DG
1346 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1347
1348 copy_string(lsm.session.name, handle->session_name,
1349 sizeof(lsm.session.name));
1350 copy_lttng_domain(&lsm.domain, &handle->domain);
1351
a4b92340
DG
1352 size = parse_str_urls_to_uri(control_url, data_url, &uris);
1353 if (size < 0) {
1354 return LTTCOMM_INVALID;
1355 }
1356
1357 lsm.u.uri.size = size;
00e2e675 1358
a4b92340
DG
1359 return ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1360 NULL);
00e2e675
DG
1361}
1362
1363/*
1364 * Enable consumer for a session and domain.
1365 *
1366 * Return 0 on success, else a negative value.
1367 */
1368int lttng_enable_consumer(struct lttng_handle *handle)
1369{
1370 struct lttcomm_session_msg lsm;
1371
1372 if (handle == NULL) {
1373 return -1;
1374 }
1375
1376 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1377
1378 copy_string(lsm.session.name, handle->session_name,
1379 sizeof(lsm.session.name));
1380 copy_lttng_domain(&lsm.domain, &handle->domain);
1381
1382 return ask_sessiond(&lsm, NULL);
1383}
1384
1385/*
1386 * Disable consumer for a session and domain.
1387 *
1388 * Return 0 on success, else a negative value.
1389 */
1390int lttng_disable_consumer(struct lttng_handle *handle)
1391{
1392 struct lttcomm_session_msg lsm;
1393
1394 if (handle == NULL) {
1395 return -1;
1396 }
1397
1398 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1399
1400 copy_string(lsm.session.name, handle->session_name,
1401 sizeof(lsm.session.name));
1402 copy_lttng_domain(&lsm.domain, &handle->domain);
1403
1404 return ask_sessiond(&lsm, NULL);
1405}
1406
44a5e5eb
DG
1407/*
1408 * Set health socket path by putting it in the global health_sock_path
1409 * variable.
1410 *
1411 * Returns 0 on success or assert(0) on ENOMEM.
1412 */
1413static int set_health_socket_path(void)
1414{
1415 int ret;
1416 int in_tgroup = 0; /* In tracing group */
1417 uid_t uid;
1418 const char *home;
1419
1420 uid = getuid();
1421
1422 if (uid != 0) {
1423 /* Are we in the tracing group ? */
1424 in_tgroup = check_tracing_group(tracing_group);
1425 }
1426
1427 if ((uid == 0) || in_tgroup) {
1428 copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK,
1429 sizeof(health_sock_path));
1430 }
1431
1432 if (uid != 0) {
1433 /*
1434 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1435 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1436 */
1437 home = getenv("HOME");
1438 if (home == NULL) {
1439 /* Fallback in /tmp .. */
1440 home = "/tmp";
1441 }
1442
1443 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1444 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1445 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1446 /* ENOMEM at this point... just kill the control lib. */
1447 assert(0);
1448 }
1449 }
1450
1451 return 0;
1452}
1453
1454/*
1455 * Check session daemon health for a specific health component.
1456 *
1457 * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate
1458 * that the control library was not able to connect to the session daemon
1459 * health socket.
1460 *
1461 * Any other positive value is an lttcomm error which can be translate with
1462 * lttng_strerror().
1463 */
1464int lttng_health_check(enum lttng_health_component c)
1465{
1466 int sock, ret;
1467 struct lttcomm_health_msg msg;
1468 struct lttcomm_health_data reply;
1469
1470 /* Connect to the sesssion daemon */
1471 sock = lttcomm_connect_unix_sock(health_sock_path);
1472 if (sock < 0) {
1473 ret = -1;
1474 goto error;
1475 }
1476
1477 msg.cmd = LTTNG_HEALTH_CHECK;
1478 msg.component = c;
1479
1480 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1481 if (ret < 0) {
1482 goto close_error;
1483 }
1484
1485 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1486 if (ret < 0) {
1487 goto close_error;
1488 }
1489
1490 ret = reply.ret_code;
1491
1492close_error:
1493 close(sock);
1494
1495error:
1496 return ret;
1497}
1498
fac6795d
DG
1499/*
1500 * lib constructor
1501 */
1502static void __attribute__((constructor)) init()
1503{
1504 /* Set default session group */
bbccc3d2 1505 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
44a5e5eb
DG
1506 /* Set socket for health check */
1507 (void) set_health_socket_path();
fac6795d 1508}
This page took 0.106573 seconds and 5 git commands to generate.