Add missing error handling in liblttngctl
[lttng-tools.git] / liblttngctl / lttngctl.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 *
82a3637f
DG
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
fac6795d 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82a3637f
DG
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fac6795d
DG
21 */
22
23#define _GNU_SOURCE
24#include <errno.h>
25#include <grp.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
5b97ec60 31#include <lttng/lttng.h>
fac6795d 32
e88129fc 33#include <lttng-sessiond-comm.h>
fac6795d 34#include "lttngerr.h"
f3ed775e 35#include "lttng-share.h"
fac6795d
DG
36
37/* Socket to session daemon for communication */
38static int sessiond_socket;
39static char sessiond_sock_path[PATH_MAX];
40
fac6795d
DG
41/* Variables */
42static char *tracing_group;
43static int connected;
44
99497cd0
MD
45/*
46 * Copy string from src to dst and enforce null terminated byte.
47 */
48static void copy_string(char *dst, const char *src, size_t len)
49{
e7d6716d 50 if (src && dst) {
99497cd0
MD
51 strncpy(dst, src, len);
52 /* Enforce the NULL terminated byte */
53 dst[len - 1] = '\0';
cd80958d
DG
54 } else if (dst) {
55 dst[0] = '\0';
99497cd0
MD
56 }
57}
58
fac6795d 59/*
cd80958d 60 * Copy domain to lttcomm_session_msg domain.
fac6795d 61 *
cd80958d
DG
62 * If domain is unknown, default domain will be the kernel.
63 */
64static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
65{
66 if (src && dst) {
67 switch (src->type) {
68 case LTTNG_DOMAIN_KERNEL:
69 case LTTNG_DOMAIN_UST:
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
73 memcpy(dst, src, sizeof(struct lttng_domain));
74 break;
75 default:
76 dst->type = LTTNG_DOMAIN_KERNEL;
77 break;
78 }
79 }
80}
81
82/*
83 * Send lttcomm_session_msg to the session daemon.
fac6795d 84 *
cd80958d
DG
85 * On success, return 0
86 * On error, return error code
fac6795d 87 */
cd80958d 88static int send_session_msg(struct lttcomm_session_msg *lsm)
fac6795d
DG
89{
90 int ret;
91
92 if (!connected) {
e065084a
DG
93 ret = -ENOTCONN;
94 goto end;
fac6795d
DG
95 }
96
cd80958d
DG
97 ret = lttcomm_send_unix_sock(sessiond_socket, lsm,
98 sizeof(struct lttcomm_session_msg));
e065084a
DG
99
100end:
101 return ret;
102}
103
104/*
cd80958d 105 * Receive data from the sessiond socket.
e065084a 106 *
cd80958d
DG
107 * On success, return 0
108 * On error, return recv() error code
e065084a 109 */
ca95a216 110static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
111{
112 int ret;
113
114 if (!connected) {
115 ret = -ENOTCONN;
116 goto end;
fac6795d
DG
117 }
118
ca95a216 119 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 120
e065084a 121end:
fac6795d
DG
122 return ret;
123}
124
125/*
947308c4 126 * Check if the specified group name exist.
65beb5ff 127 *
2269e89e 128 * If yes return 1, else return -1.
947308c4
DG
129 */
130static int check_tracing_group(const char *grp_name)
131{
132 struct group *grp_tracing; /* no free(). See getgrnam(3) */
133 gid_t *grp_list;
134 int grp_list_size, grp_id, i;
135 int ret = -1;
136
137 /* Get GID of group 'tracing' */
138 grp_tracing = getgrnam(grp_name);
139 if (grp_tracing == NULL) {
140 /* NULL means not found also. getgrnam(3) */
141 if (errno != 0) {
142 perror("getgrnam");
143 }
144 goto end;
145 }
146
147 /* Get number of supplementary group IDs */
148 grp_list_size = getgroups(0, NULL);
149 if (grp_list_size < 0) {
150 perror("getgroups");
151 goto end;
152 }
153
154 /* Alloc group list of the right size */
155 grp_list = malloc(grp_list_size * sizeof(gid_t));
00795392
MD
156 if (!grp_list) {
157 ret = -1;
158 goto end;
159 }
947308c4
DG
160 grp_id = getgroups(grp_list_size, grp_list);
161 if (grp_id < -1) {
162 perror("getgroups");
163 goto free_list;
164 }
165
166 for (i = 0; i < grp_list_size; i++) {
167 if (grp_list[i] == grp_tracing->gr_gid) {
2269e89e 168 ret = 1;
947308c4
DG
169 break;
170 }
171 }
172
173free_list:
174 free(grp_list);
175
176end:
177 return ret;
178}
179
180/*
2269e89e
DG
181 * Try connect to session daemon with sock_path.
182 *
183 * Return 0 on success, else -1
184 */
185static int try_connect_sessiond(const char *sock_path)
186{
187 int ret;
188
189 /* If socket exist, we check if the daemon listens for connect. */
190 ret = access(sock_path, F_OK);
191 if (ret < 0) {
192 /* Not alive */
193 return -1;
194 }
195
196 ret = lttcomm_connect_unix_sock(sock_path);
197 if (ret < 0) {
198 /* Not alive */
199 return -1;
200 }
201
202 ret = lttcomm_close_unix_sock(ret);
203 if (ret < 0) {
204 perror("lttcomm_close_unix_sock");
205 }
206
207 return 0;
208}
209
210/*
211 * Set sessiond socket path by putting it in the global sessiond_sock_path
212 * variable.
947308c4
DG
213 */
214static int set_session_daemon_path(void)
215{
216 int ret;
2269e89e
DG
217 int in_tgroup = 0; /* In tracing group */
218 uid_t uid;
219
220 uid = getuid();
947308c4 221
2269e89e
DG
222 if (uid != 0) {
223 /* Are we in the tracing group ? */
224 in_tgroup = check_tracing_group(tracing_group);
225 }
226
227 if (uid == 0) {
228 /* Root */
229 copy_string(sessiond_sock_path,
230 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
231 sizeof(sessiond_sock_path));
232 } else if (in_tgroup) {
233 /* Tracing group */
234 copy_string(sessiond_sock_path,
235 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
236 sizeof(sessiond_sock_path));
237
238 ret = try_connect_sessiond(sessiond_sock_path);
239 if (ret < 0) {
240 /* Global session daemon not available */
241 if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
242 DEFAULT_HOME_CLIENT_UNIX_SOCK,
243 getenv("HOME")) < 0) {
244 return -ENOMEM;
245 }
246 }
247 } else {
248 /* Not in tracing group and not root, default */
99497cd0 249 if (snprintf(sessiond_sock_path, PATH_MAX,
cd80958d
DG
250 DEFAULT_HOME_CLIENT_UNIX_SOCK,
251 getenv("HOME")) < 0) {
947308c4
DG
252 return -ENOMEM;
253 }
947308c4
DG
254 }
255
256 return 0;
257}
258
65beb5ff
DG
259/*
260 * Connect to the LTTng session daemon.
261 *
262 * On success, return 0. On error, return -1.
263 */
264static int connect_sessiond(void)
265{
266 int ret;
267
268 ret = set_session_daemon_path();
269 if (ret < 0) {
270 return ret;
271 }
272
273 /* Connect to the sesssion daemon */
274 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
275 if (ret < 0) {
276 return ret;
277 }
278
279 sessiond_socket = ret;
280 connected = 1;
281
282 return 0;
283}
284
285/*
286 * Clean disconnect the session daemon.
287 */
288static int disconnect_sessiond(void)
289{
290 int ret = 0;
291
292 if (connected) {
293 ret = lttcomm_close_unix_sock(sessiond_socket);
294 sessiond_socket = 0;
295 connected = 0;
296 }
297
298 return ret;
299}
300
35a6fdb7 301/*
cd80958d 302 * Ask the session daemon a specific command and put the data into buf.
65beb5ff 303 *
cd80958d 304 * Return size of data (only payload, not header).
65beb5ff 305 */
cd80958d 306static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
65beb5ff
DG
307{
308 int ret;
309 size_t size;
310 void *data = NULL;
cd80958d 311 struct lttcomm_lttng_msg llm;
65beb5ff
DG
312
313 ret = connect_sessiond();
314 if (ret < 0) {
315 goto end;
316 }
317
65beb5ff 318 /* Send command to session daemon */
cd80958d 319 ret = send_session_msg(lsm);
65beb5ff
DG
320 if (ret < 0) {
321 goto end;
322 }
323
324 /* Get header from data transmission */
325 ret = recv_data_sessiond(&llm, sizeof(llm));
326 if (ret < 0) {
327 goto end;
328 }
329
330 /* Check error code if OK */
331 if (llm.ret_code != LTTCOMM_OK) {
332 ret = -llm.ret_code;
333 goto end;
334 }
335
336 size = llm.data_size;
337 if (size == 0) {
7d29a247 338 ret = 0;
65beb5ff
DG
339 goto end;
340 }
341
342 data = (void*) malloc(size);
343
344 /* Get payload data */
345 ret = recv_data_sessiond(data, size);
346 if (ret < 0) {
347 free(data);
348 goto end;
349 }
350
351 *buf = data;
352 ret = size;
353
354end:
355 disconnect_sessiond();
356 return ret;
357}
358
9f19cc17 359/*
cd80958d 360 * Create lttng handle and return pointer.
9f19cc17 361 */
cd80958d
DG
362struct lttng_handle *lttng_create_handle(const char *session_name,
363 struct lttng_domain *domain)
9f19cc17 364{
cd80958d
DG
365 struct lttng_handle *handle;
366
367 handle = malloc(sizeof(struct lttng_handle));
368 if (handle == NULL) {
369 perror("malloc handle");
370 goto end;
371 }
372
373 /* Copy session name */
374 copy_string(handle->session_name, session_name,
375 sizeof(handle->session_name));
376
377 /* Copy lttng domain */
378 copy_lttng_domain(&handle->domain, domain);
379
380end:
381 return handle;
382}
383
384/*
385 * Destroy handle by free(3) the pointer.
386 */
387void lttng_destroy_handle(struct lttng_handle *handle)
388{
389 if (handle) {
390 free(handle);
eb354453
DG
391 }
392}
393
d9800920
DG
394/*
395 * Register an outside consumer.
396 */
397int lttng_register_consumer(struct lttng_handle *handle,
398 const char *socket_path)
399{
400 struct lttcomm_session_msg lsm;
401
402 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
403 copy_string(lsm.session.name, handle->session_name,
404 sizeof(lsm.session.name));
405 copy_lttng_domain(&lsm.domain, &handle->domain);
406
407 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
408
409 return ask_sessiond(&lsm, NULL);
410}
411
1df4dedd 412/*
f3ed775e 413 * Start tracing for all trace of the session.
1df4dedd 414 */
cd80958d 415int lttng_start_tracing(struct lttng_handle *handle)
f3ed775e 416{
cd80958d
DG
417 struct lttcomm_session_msg lsm;
418
419 if (!handle) {
420 return -1;
421 }
422
423 lsm.cmd_type = LTTNG_START_TRACE;
424 copy_string(lsm.session.name, handle->session_name,
425 sizeof(lsm.session.name));
426
427 return ask_sessiond(&lsm, NULL);
f3ed775e 428}
1df4dedd
DG
429
430/*
f3ed775e
DG
431 * Stop tracing for all trace of the session.
432 */
cd80958d 433int lttng_stop_tracing(struct lttng_handle *handle)
f3ed775e 434{
cd80958d
DG
435 struct lttcomm_session_msg lsm;
436
437 lsm.cmd_type = LTTNG_STOP_TRACE;
438 copy_string(lsm.session.name, handle->session_name,
439 sizeof(lsm.session.name));
440
441 return ask_sessiond(&lsm, NULL);
f3ed775e
DG
442}
443
444/*
cd80958d 445 * Add context to event or/and channel.
1df4dedd 446 */
cd80958d 447int lttng_add_context(struct lttng_handle *handle,
38057ed1
DG
448 struct lttng_event_context *ctx, const char *event_name,
449 const char *channel_name)
d65106b1 450{
cd80958d
DG
451 struct lttcomm_session_msg lsm;
452
453 if (!handle) {
454 return -1;
455 }
456
457 lsm.cmd_type = LTTNG_ADD_CONTEXT;
458
459 /* Copy channel name */
460 copy_string(lsm.u.context.channel_name, channel_name,
461 sizeof(lsm.u.context.channel_name));
462 /* Copy event name */
463 copy_string(lsm.u.context.event_name, event_name,
464 sizeof(lsm.u.context.event_name));
465
466 copy_lttng_domain(&lsm.domain, &handle->domain);
d65106b1 467
eb354453
DG
468 if (ctx) {
469 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
d65106b1
DG
470 }
471
cd80958d
DG
472 copy_string(lsm.session.name, handle->session_name,
473 sizeof(lsm.session.name));
474
475 return ask_sessiond(&lsm, NULL);
d65106b1
DG
476}
477
f3ed775e 478/*
cd80958d 479 * Enable event
f3ed775e 480 */
cd80958d 481int lttng_enable_event(struct lttng_handle *handle,
38057ed1 482 struct lttng_event *ev, const char *channel_name)
1df4dedd 483{
cd80958d
DG
484 struct lttcomm_session_msg lsm;
485
486 if (!handle) {
487 return -1;
488 }
33a2b854 489
94cf3c47 490 if (channel_name == NULL) {
cd80958d
DG
491 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
492 sizeof(lsm.u.enable.channel_name));
33a2b854 493 } else {
cd80958d
DG
494 copy_string(lsm.u.enable.channel_name, channel_name,
495 sizeof(lsm.u.enable.channel_name));
eb354453
DG
496 }
497
cd80958d 498 copy_lttng_domain(&lsm.domain, &handle->domain);
0d0c377a 499
cd80958d
DG
500 if (ev) {
501 lsm.cmd_type = LTTNG_ENABLE_EVENT;
502 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
0d0c377a 503 } else {
cd80958d 504 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
f3ed775e
DG
505 }
506
cd80958d
DG
507 copy_string(lsm.session.name, handle->session_name,
508 sizeof(lsm.session.name));
509
510 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
511}
512
513/*
f5177a38 514 * Disable event of a channel and domain.
1df4dedd 515 */
cd80958d 516int lttng_disable_event(struct lttng_handle *handle, const char *name,
38057ed1 517 const char *channel_name)
1df4dedd 518{
cd80958d 519 struct lttcomm_session_msg lsm;
1df4dedd 520
cd80958d
DG
521 if (!handle) {
522 return -1;
523 }
524
525 if (channel_name) {
526 copy_string(lsm.u.disable.channel_name, channel_name,
527 sizeof(lsm.u.disable.channel_name));
f3ed775e 528 } else {
cd80958d
DG
529 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
530 sizeof(lsm.u.disable.channel_name));
eb354453
DG
531 }
532
cd80958d 533 copy_lttng_domain(&lsm.domain, &handle->domain);
f5177a38
DG
534
535 if (name == NULL) {
cd80958d
DG
536 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
537 lsm.cmd_type = LTTNG_DISABLE_EVENT;
f5177a38 538 } else {
cd80958d 539 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
f3ed775e
DG
540 }
541
cd80958d
DG
542 copy_string(lsm.session.name, handle->session_name,
543 sizeof(lsm.session.name));
544
545 return ask_sessiond(&lsm, NULL);
1df4dedd
DG
546}
547
548/*
0d0c377a 549 * Enable channel per domain
a5c5a2bd 550 */
cd80958d 551int lttng_enable_channel(struct lttng_handle *handle,
38057ed1 552 struct lttng_channel *chan)
a5c5a2bd 553{
cd80958d
DG
554 struct lttcomm_session_msg lsm;
555
556 if (!handle) {
557 return -1;
558 }
559
eb354453 560 if (chan) {
cd80958d 561 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
eb354453 562 }
7d29a247 563
cd80958d
DG
564 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
565
566 copy_lttng_domain(&lsm.domain, &handle->domain);
7d29a247 567
cd80958d
DG
568 copy_string(lsm.session.name, handle->session_name,
569 sizeof(lsm.session.name));
570
571 return ask_sessiond(&lsm, NULL);
8c0faa1d 572}
1df4dedd 573
2ef84c95 574/*
0b97ec54 575 * All tracing will be stopped for registered events of the channel.
2ef84c95 576 */
cd80958d 577int lttng_disable_channel(struct lttng_handle *handle, const char *name)
2ef84c95 578{
cd80958d
DG
579 struct lttcomm_session_msg lsm;
580
581 if (!handle) {
582 return -1;
583 }
584
585 if (name) {
586 copy_string(lsm.u.disable.channel_name, name,
587 sizeof(lsm.u.disable.channel_name));
588 }
589
590 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1df4dedd 591
cd80958d
DG
592 copy_lttng_domain(&lsm.domain, &handle->domain);
593
594 copy_string(lsm.session.name, handle->session_name,
595 sizeof(lsm.session.name));
596
597 return ask_sessiond(&lsm, NULL);
ca95a216
DG
598}
599
fac6795d 600/*
2a71efd5 601 * List all available tracepoints of domain.
fac6795d 602 *
2a71efd5 603 * Return the size (bytes) of the list and set the events array.
7d29a247 604 * On error, return negative value.
fac6795d 605 */
cd80958d 606int lttng_list_tracepoints(struct lttng_handle *handle,
2a71efd5 607 struct lttng_event **events)
fac6795d 608{
052da939 609 int ret;
cd80958d
DG
610 struct lttcomm_session_msg lsm;
611
612 if (!handle) {
613 return -1;
614 }
fac6795d 615
cd80958d
DG
616 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
617 copy_lttng_domain(&lsm.domain, &handle->domain);
2a71efd5 618
cd80958d 619 ret = ask_sessiond(&lsm, (void **) events);
052da939
DG
620 if (ret < 0) {
621 return ret;
eb354453 622 }
fac6795d 623
9f19cc17 624 return ret / sizeof(struct lttng_event);
fac6795d
DG
625}
626
1657e9bb 627/*
7d29a247 628 * Return a human readable string of code
1657e9bb 629 */
7d29a247 630const char *lttng_get_readable_code(int code)
1657e9bb 631{
7d29a247
DG
632 if (code > -LTTCOMM_OK) {
633 return "Ended with errors";
1657e9bb
DG
634 }
635
7d29a247 636 return lttcomm_get_readable_code(code);
1657e9bb
DG
637}
638
aaf97519 639/*
894be886 640 * Create a brand new session using name.
aaf97519 641 */
38057ed1 642int lttng_create_session(const char *name, const char *path)
aaf97519 643{
cd80958d
DG
644 struct lttcomm_session_msg lsm;
645
646 lsm.cmd_type = LTTNG_CREATE_SESSION;
647 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
648 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
649
650 return ask_sessiond(&lsm, NULL);
8028d920
DG
651}
652
653/*
8028d920
DG
654 * Destroy session using name.
655 */
cd80958d 656int lttng_destroy_session(struct lttng_handle *handle)
8028d920 657{
cd80958d
DG
658 struct lttcomm_session_msg lsm;
659
660 if (!handle) {
661 return -1;
662 }
663
664 lsm.cmd_type = LTTNG_DESTROY_SESSION;
665 copy_string(lsm.session.name, handle->session_name,
666 sizeof(lsm.session.name));
667
668 return ask_sessiond(&lsm, NULL);
aaf97519
DG
669}
670
57167058 671/*
57167058
DG
672 * Ask the session daemon for all available sessions.
673 *
ca95a216
DG
674 * Return number of session.
675 * On error, return negative value.
57167058 676 */
ca95a216 677int lttng_list_sessions(struct lttng_session **sessions)
57167058 678{
ca95a216 679 int ret;
cd80958d 680 struct lttcomm_session_msg lsm;
57167058 681
cd80958d
DG
682 lsm.cmd_type = LTTNG_LIST_SESSIONS;
683 ret = ask_sessiond(&lsm, (void**) sessions);
57167058 684 if (ret < 0) {
ca95a216 685 return ret;
57167058
DG
686 }
687
ca95a216 688 return ret / sizeof(struct lttng_session);
57167058
DG
689}
690
9f19cc17
DG
691/*
692 * List domain of a session.
693 */
cd80958d
DG
694int lttng_list_domains(struct lttng_handle *handle,
695 struct lttng_domain **domains)
9f19cc17
DG
696{
697 int ret;
cd80958d
DG
698 struct lttcomm_session_msg lsm;
699
700 if (!handle) {
701 return -1;
702 }
9f19cc17 703
cd80958d
DG
704 lsm.cmd_type = LTTNG_LIST_DOMAINS;
705
706 copy_string(lsm.session.name, handle->session_name,
707 sizeof(lsm.session.name));
708
709 ret = ask_sessiond(&lsm, (void**) domains);
9f19cc17
DG
710 if (ret < 0) {
711 return ret;
712 }
713
714 return ret / sizeof(struct lttng_domain);
715}
716
717/*
718 * List channels of a session
719 */
cd80958d
DG
720int lttng_list_channels(struct lttng_handle *handle,
721 struct lttng_channel **channels)
9f19cc17
DG
722{
723 int ret;
cd80958d
DG
724 struct lttcomm_session_msg lsm;
725
726 if (!handle) {
727 return -1;
728 }
729
730 lsm.cmd_type = LTTNG_LIST_CHANNELS;
731 copy_string(lsm.session.name, handle->session_name,
732 sizeof(lsm.session.name));
9f19cc17 733
cd80958d 734 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 735
cd80958d 736 ret = ask_sessiond(&lsm, (void**) channels);
9f19cc17
DG
737 if (ret < 0) {
738 return ret;
739 }
740
741 return ret / sizeof(struct lttng_channel);
742}
743
744/*
745 * List events of a session channel.
746 */
cd80958d
DG
747int lttng_list_events(struct lttng_handle *handle,
748 const char *channel_name, struct lttng_event **events)
9f19cc17
DG
749{
750 int ret;
cd80958d 751 struct lttcomm_session_msg lsm;
9f19cc17 752
cd80958d
DG
753 if (!handle) {
754 return -1;
755 }
756
757 lsm.cmd_type = LTTNG_LIST_EVENTS;
758 copy_string(lsm.session.name, handle->session_name,
759 sizeof(lsm.session.name));
760 copy_string(lsm.u.list.channel_name, channel_name,
761 sizeof(lsm.u.list.channel_name));
762
763 copy_lttng_domain(&lsm.domain, &handle->domain);
9f19cc17 764
cd80958d 765 ret = ask_sessiond(&lsm, (void**) events);
9f19cc17
DG
766 if (ret < 0) {
767 return ret;
768 }
769
770 return ret / sizeof(struct lttng_event);
771}
772
fac6795d
DG
773/*
774 * lttng_set_tracing_group
775 *
776 * Set tracing group variable with name. This function
777 * allocate memory pointed by tracing_group.
778 */
779int lttng_set_tracing_group(const char *name)
780{
781 if (asprintf(&tracing_group, "%s", name) < 0) {
782 return -ENOMEM;
783 }
784
785 return 0;
786}
787
d0254c7c
MD
788/*
789 * lttng_calibrate
790 */
cd80958d 791int lttng_calibrate(struct lttng_handle *handle,
d0254c7c
MD
792 struct lttng_calibrate *calibrate)
793{
cd80958d 794 struct lttcomm_session_msg lsm;
d0254c7c 795
cd80958d
DG
796 if (!handle) {
797 return -1;
798 }
d0254c7c 799
cd80958d
DG
800 lsm.cmd_type = LTTNG_CALIBRATE;
801 copy_lttng_domain(&lsm.domain, &handle->domain);
d0254c7c 802
cd80958d
DG
803 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
804
805 return ask_sessiond(&lsm, NULL);
d0254c7c
MD
806}
807
fac6795d 808/*
2269e89e 809 * Check if session daemon is alive.
fac6795d 810 *
2269e89e
DG
811 * Return 1 if alive or 0 if not.
812 * On error return -1
fac6795d 813 */
947308c4 814int lttng_session_daemon_alive(void)
fac6795d
DG
815{
816 int ret;
817
818 ret = set_session_daemon_path();
819 if (ret < 0) {
947308c4 820 /* Error */
fac6795d
DG
821 return ret;
822 }
823
2269e89e
DG
824 if (strlen(sessiond_sock_path) == 0) {
825 /* No socket path set. Weird error */
826 return -1;
fac6795d
DG
827 }
828
2269e89e 829 ret = try_connect_sessiond(sessiond_sock_path);
7d8234d9
MD
830 if (ret < 0) {
831 /* Not alive */
832 return 0;
833 }
7d8234d9 834
947308c4
DG
835 /* Is alive */
836 return 1;
fac6795d
DG
837}
838
839/*
840 * lib constructor
841 */
842static void __attribute__((constructor)) init()
843{
844 /* Set default session group */
64a23ac4 845 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 846}
This page took 0.063005 seconds and 4 git commands to generate.