Commit | Line | Data |
---|---|---|
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 */ | |
38 | static int sessiond_socket; | |
39 | static char sessiond_sock_path[PATH_MAX]; | |
40 | ||
fac6795d DG |
41 | /* Variables */ |
42 | static char *tracing_group; | |
43 | static int connected; | |
44 | ||
99497cd0 MD |
45 | /* |
46 | * Copy string from src to dst and enforce null terminated byte. | |
47 | */ | |
48 | static void copy_string(char *dst, const char *src, size_t len) | |
49 | { | |
cd80958d | 50 | if (src) { |
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 | */ | |
64 | static 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 | 88 | static 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 | |
100 | end: | |
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 | 110 | static 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 | 121 | end: |
fac6795d DG |
122 | return ret; |
123 | } | |
124 | ||
125 | /* | |
947308c4 | 126 | * Check if the specified group name exist. |
65beb5ff DG |
127 | * |
128 | * If yes return 0, else return -1. | |
947308c4 DG |
129 | */ |
130 | static 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)); | |
156 | grp_id = getgroups(grp_list_size, grp_list); | |
157 | if (grp_id < -1) { | |
158 | perror("getgroups"); | |
159 | goto free_list; | |
160 | } | |
161 | ||
162 | for (i = 0; i < grp_list_size; i++) { | |
163 | if (grp_list[i] == grp_tracing->gr_gid) { | |
164 | ret = 0; | |
165 | break; | |
166 | } | |
167 | } | |
168 | ||
169 | free_list: | |
170 | free(grp_list); | |
171 | ||
172 | end: | |
173 | return ret; | |
174 | } | |
175 | ||
176 | /* | |
65beb5ff DG |
177 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
178 | * variable. | |
947308c4 DG |
179 | */ |
180 | static int set_session_daemon_path(void) | |
181 | { | |
182 | int ret; | |
183 | ||
184 | /* Are we in the tracing group ? */ | |
185 | ret = check_tracing_group(tracing_group); | |
186 | if (ret < 0 && getuid() != 0) { | |
99497cd0 | 187 | if (snprintf(sessiond_sock_path, PATH_MAX, |
cd80958d DG |
188 | DEFAULT_HOME_CLIENT_UNIX_SOCK, |
189 | getenv("HOME")) < 0) { | |
947308c4 DG |
190 | return -ENOMEM; |
191 | } | |
192 | } else { | |
cd80958d DG |
193 | copy_string(sessiond_sock_path, |
194 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
195 | PATH_MAX); | |
947308c4 DG |
196 | } |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
65beb5ff DG |
201 | /* |
202 | * Connect to the LTTng session daemon. | |
203 | * | |
204 | * On success, return 0. On error, return -1. | |
205 | */ | |
206 | static int connect_sessiond(void) | |
207 | { | |
208 | int ret; | |
209 | ||
210 | ret = set_session_daemon_path(); | |
211 | if (ret < 0) { | |
212 | return ret; | |
213 | } | |
214 | ||
215 | /* Connect to the sesssion daemon */ | |
216 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
217 | if (ret < 0) { | |
218 | return ret; | |
219 | } | |
220 | ||
221 | sessiond_socket = ret; | |
222 | connected = 1; | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Clean disconnect the session daemon. | |
229 | */ | |
230 | static int disconnect_sessiond(void) | |
231 | { | |
232 | int ret = 0; | |
233 | ||
234 | if (connected) { | |
235 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
236 | sessiond_socket = 0; | |
237 | connected = 0; | |
238 | } | |
239 | ||
240 | return ret; | |
241 | } | |
242 | ||
35a6fdb7 | 243 | /* |
cd80958d | 244 | * Ask the session daemon a specific command and put the data into buf. |
65beb5ff | 245 | * |
cd80958d | 246 | * Return size of data (only payload, not header). |
65beb5ff | 247 | */ |
cd80958d | 248 | static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) |
65beb5ff DG |
249 | { |
250 | int ret; | |
251 | size_t size; | |
252 | void *data = NULL; | |
cd80958d | 253 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
254 | |
255 | ret = connect_sessiond(); | |
256 | if (ret < 0) { | |
257 | goto end; | |
258 | } | |
259 | ||
65beb5ff | 260 | /* Send command to session daemon */ |
cd80958d | 261 | ret = send_session_msg(lsm); |
65beb5ff DG |
262 | if (ret < 0) { |
263 | goto end; | |
264 | } | |
265 | ||
266 | /* Get header from data transmission */ | |
267 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
268 | if (ret < 0) { | |
269 | goto end; | |
270 | } | |
271 | ||
272 | /* Check error code if OK */ | |
273 | if (llm.ret_code != LTTCOMM_OK) { | |
274 | ret = -llm.ret_code; | |
275 | goto end; | |
276 | } | |
277 | ||
278 | size = llm.data_size; | |
279 | if (size == 0) { | |
7d29a247 | 280 | ret = 0; |
65beb5ff DG |
281 | goto end; |
282 | } | |
283 | ||
284 | data = (void*) malloc(size); | |
285 | ||
286 | /* Get payload data */ | |
287 | ret = recv_data_sessiond(data, size); | |
288 | if (ret < 0) { | |
289 | free(data); | |
290 | goto end; | |
291 | } | |
292 | ||
293 | *buf = data; | |
294 | ret = size; | |
295 | ||
296 | end: | |
297 | disconnect_sessiond(); | |
298 | return ret; | |
299 | } | |
300 | ||
9f19cc17 | 301 | /* |
cd80958d | 302 | * Create lttng handle and return pointer. |
9f19cc17 | 303 | */ |
cd80958d DG |
304 | struct lttng_handle *lttng_create_handle(const char *session_name, |
305 | struct lttng_domain *domain) | |
9f19cc17 | 306 | { |
cd80958d DG |
307 | struct lttng_handle *handle; |
308 | ||
309 | handle = malloc(sizeof(struct lttng_handle)); | |
310 | if (handle == NULL) { | |
311 | perror("malloc handle"); | |
312 | goto end; | |
313 | } | |
314 | ||
315 | /* Copy session name */ | |
316 | copy_string(handle->session_name, session_name, | |
317 | sizeof(handle->session_name)); | |
318 | ||
319 | /* Copy lttng domain */ | |
320 | copy_lttng_domain(&handle->domain, domain); | |
321 | ||
322 | end: | |
323 | return handle; | |
324 | } | |
325 | ||
326 | /* | |
327 | * Destroy handle by free(3) the pointer. | |
328 | */ | |
329 | void lttng_destroy_handle(struct lttng_handle *handle) | |
330 | { | |
331 | if (handle) { | |
332 | free(handle); | |
eb354453 DG |
333 | } |
334 | } | |
335 | ||
1df4dedd | 336 | /* |
f3ed775e | 337 | * Start tracing for all trace of the session. |
1df4dedd | 338 | */ |
cd80958d | 339 | int lttng_start_tracing(struct lttng_handle *handle) |
f3ed775e | 340 | { |
cd80958d DG |
341 | struct lttcomm_session_msg lsm; |
342 | ||
343 | if (!handle) { | |
344 | return -1; | |
345 | } | |
346 | ||
347 | lsm.cmd_type = LTTNG_START_TRACE; | |
348 | copy_string(lsm.session.name, handle->session_name, | |
349 | sizeof(lsm.session.name)); | |
350 | ||
351 | return ask_sessiond(&lsm, NULL); | |
f3ed775e | 352 | } |
1df4dedd DG |
353 | |
354 | /* | |
f3ed775e DG |
355 | * Stop tracing for all trace of the session. |
356 | */ | |
cd80958d | 357 | int lttng_stop_tracing(struct lttng_handle *handle) |
f3ed775e | 358 | { |
cd80958d DG |
359 | struct lttcomm_session_msg lsm; |
360 | ||
361 | lsm.cmd_type = LTTNG_STOP_TRACE; | |
362 | copy_string(lsm.session.name, handle->session_name, | |
363 | sizeof(lsm.session.name)); | |
364 | ||
365 | return ask_sessiond(&lsm, NULL); | |
f3ed775e DG |
366 | } |
367 | ||
368 | /* | |
cd80958d | 369 | * Add context to event or/and channel. |
1df4dedd | 370 | */ |
cd80958d | 371 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
372 | struct lttng_event_context *ctx, const char *event_name, |
373 | const char *channel_name) | |
d65106b1 | 374 | { |
cd80958d DG |
375 | struct lttcomm_session_msg lsm; |
376 | ||
377 | if (!handle) { | |
378 | return -1; | |
379 | } | |
380 | ||
381 | lsm.cmd_type = LTTNG_ADD_CONTEXT; | |
382 | ||
383 | /* Copy channel name */ | |
384 | copy_string(lsm.u.context.channel_name, channel_name, | |
385 | sizeof(lsm.u.context.channel_name)); | |
386 | /* Copy event name */ | |
387 | copy_string(lsm.u.context.event_name, event_name, | |
388 | sizeof(lsm.u.context.event_name)); | |
389 | ||
390 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d65106b1 | 391 | |
eb354453 DG |
392 | if (ctx) { |
393 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); | |
d65106b1 DG |
394 | } |
395 | ||
cd80958d DG |
396 | copy_string(lsm.session.name, handle->session_name, |
397 | sizeof(lsm.session.name)); | |
398 | ||
399 | return ask_sessiond(&lsm, NULL); | |
d65106b1 DG |
400 | } |
401 | ||
f3ed775e | 402 | /* |
cd80958d | 403 | * Enable event |
f3ed775e | 404 | */ |
cd80958d | 405 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 406 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 407 | { |
cd80958d DG |
408 | struct lttcomm_session_msg lsm; |
409 | ||
410 | if (!handle) { | |
411 | return -1; | |
412 | } | |
33a2b854 | 413 | |
94cf3c47 | 414 | if (channel_name == NULL) { |
cd80958d DG |
415 | copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
416 | sizeof(lsm.u.enable.channel_name)); | |
33a2b854 | 417 | } else { |
cd80958d DG |
418 | copy_string(lsm.u.enable.channel_name, channel_name, |
419 | sizeof(lsm.u.enable.channel_name)); | |
eb354453 DG |
420 | } |
421 | ||
cd80958d | 422 | copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 423 | |
cd80958d DG |
424 | if (ev) { |
425 | lsm.cmd_type = LTTNG_ENABLE_EVENT; | |
426 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); | |
0d0c377a | 427 | } else { |
cd80958d | 428 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e DG |
429 | } |
430 | ||
cd80958d DG |
431 | copy_string(lsm.session.name, handle->session_name, |
432 | sizeof(lsm.session.name)); | |
433 | ||
434 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
435 | } |
436 | ||
437 | /* | |
f5177a38 | 438 | * Disable event of a channel and domain. |
1df4dedd | 439 | */ |
cd80958d | 440 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 441 | const char *channel_name) |
1df4dedd | 442 | { |
cd80958d | 443 | struct lttcomm_session_msg lsm; |
1df4dedd | 444 | |
cd80958d DG |
445 | if (!handle) { |
446 | return -1; | |
447 | } | |
448 | ||
449 | if (channel_name) { | |
450 | copy_string(lsm.u.disable.channel_name, channel_name, | |
451 | sizeof(lsm.u.disable.channel_name)); | |
f3ed775e | 452 | } else { |
cd80958d DG |
453 | copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
454 | sizeof(lsm.u.disable.channel_name)); | |
eb354453 DG |
455 | } |
456 | ||
cd80958d | 457 | copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 DG |
458 | |
459 | if (name == NULL) { | |
cd80958d DG |
460 | copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); |
461 | lsm.cmd_type = LTTNG_DISABLE_EVENT; | |
f5177a38 | 462 | } else { |
cd80958d | 463 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
464 | } |
465 | ||
cd80958d DG |
466 | copy_string(lsm.session.name, handle->session_name, |
467 | sizeof(lsm.session.name)); | |
468 | ||
469 | return ask_sessiond(&lsm, NULL); | |
1df4dedd DG |
470 | } |
471 | ||
472 | /* | |
0d0c377a | 473 | * Enable channel per domain |
a5c5a2bd | 474 | */ |
cd80958d | 475 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 476 | struct lttng_channel *chan) |
a5c5a2bd | 477 | { |
cd80958d DG |
478 | struct lttcomm_session_msg lsm; |
479 | ||
480 | if (!handle) { | |
481 | return -1; | |
482 | } | |
483 | ||
eb354453 | 484 | if (chan) { |
cd80958d | 485 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
eb354453 | 486 | } |
7d29a247 | 487 | |
cd80958d DG |
488 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
489 | ||
490 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
7d29a247 | 491 | |
cd80958d DG |
492 | copy_string(lsm.session.name, handle->session_name, |
493 | sizeof(lsm.session.name)); | |
494 | ||
495 | return ask_sessiond(&lsm, NULL); | |
8c0faa1d | 496 | } |
1df4dedd | 497 | |
2ef84c95 | 498 | /* |
0b97ec54 | 499 | * All tracing will be stopped for registered events of the channel. |
2ef84c95 | 500 | */ |
cd80958d | 501 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 502 | { |
cd80958d DG |
503 | struct lttcomm_session_msg lsm; |
504 | ||
505 | if (!handle) { | |
506 | return -1; | |
507 | } | |
508 | ||
509 | if (name) { | |
510 | copy_string(lsm.u.disable.channel_name, name, | |
511 | sizeof(lsm.u.disable.channel_name)); | |
512 | } | |
513 | ||
514 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; | |
1df4dedd | 515 | |
cd80958d DG |
516 | copy_lttng_domain(&lsm.domain, &handle->domain); |
517 | ||
518 | copy_string(lsm.session.name, handle->session_name, | |
519 | sizeof(lsm.session.name)); | |
520 | ||
521 | return ask_sessiond(&lsm, NULL); | |
ca95a216 DG |
522 | } |
523 | ||
fac6795d | 524 | /* |
2a71efd5 | 525 | * List all available tracepoints of domain. |
fac6795d | 526 | * |
2a71efd5 | 527 | * Return the size (bytes) of the list and set the events array. |
7d29a247 | 528 | * On error, return negative value. |
fac6795d | 529 | */ |
cd80958d | 530 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 531 | struct lttng_event **events) |
fac6795d | 532 | { |
052da939 | 533 | int ret; |
cd80958d DG |
534 | struct lttcomm_session_msg lsm; |
535 | ||
536 | if (!handle) { | |
537 | return -1; | |
538 | } | |
fac6795d | 539 | |
cd80958d DG |
540 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
541 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
2a71efd5 | 542 | |
cd80958d | 543 | ret = ask_sessiond(&lsm, (void **) events); |
052da939 DG |
544 | if (ret < 0) { |
545 | return ret; | |
eb354453 | 546 | } |
fac6795d | 547 | |
9f19cc17 | 548 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
549 | } |
550 | ||
1657e9bb | 551 | /* |
7d29a247 | 552 | * Return a human readable string of code |
1657e9bb | 553 | */ |
7d29a247 | 554 | const char *lttng_get_readable_code(int code) |
1657e9bb | 555 | { |
7d29a247 DG |
556 | if (code > -LTTCOMM_OK) { |
557 | return "Ended with errors"; | |
1657e9bb DG |
558 | } |
559 | ||
7d29a247 | 560 | return lttcomm_get_readable_code(code); |
1657e9bb DG |
561 | } |
562 | ||
aaf97519 | 563 | /* |
894be886 | 564 | * Create a brand new session using name. |
aaf97519 | 565 | */ |
38057ed1 | 566 | int lttng_create_session(const char *name, const char *path) |
aaf97519 | 567 | { |
cd80958d DG |
568 | struct lttcomm_session_msg lsm; |
569 | ||
570 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
571 | copy_string(lsm.session.name, name, sizeof(lsm.session.name)); | |
572 | copy_string(lsm.session.path, path, sizeof(lsm.session.path)); | |
573 | ||
574 | return ask_sessiond(&lsm, NULL); | |
8028d920 DG |
575 | } |
576 | ||
577 | /* | |
8028d920 DG |
578 | * Destroy session using name. |
579 | */ | |
cd80958d | 580 | int lttng_destroy_session(struct lttng_handle *handle) |
8028d920 | 581 | { |
cd80958d DG |
582 | struct lttcomm_session_msg lsm; |
583 | ||
584 | if (!handle) { | |
585 | return -1; | |
586 | } | |
587 | ||
588 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
589 | copy_string(lsm.session.name, handle->session_name, | |
590 | sizeof(lsm.session.name)); | |
591 | ||
592 | return ask_sessiond(&lsm, NULL); | |
aaf97519 DG |
593 | } |
594 | ||
57167058 | 595 | /* |
57167058 DG |
596 | * Ask the session daemon for all available sessions. |
597 | * | |
ca95a216 DG |
598 | * Return number of session. |
599 | * On error, return negative value. | |
57167058 | 600 | */ |
ca95a216 | 601 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 602 | { |
ca95a216 | 603 | int ret; |
cd80958d | 604 | struct lttcomm_session_msg lsm; |
57167058 | 605 | |
cd80958d DG |
606 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
607 | ret = ask_sessiond(&lsm, (void**) sessions); | |
57167058 | 608 | if (ret < 0) { |
ca95a216 | 609 | return ret; |
57167058 DG |
610 | } |
611 | ||
ca95a216 | 612 | return ret / sizeof(struct lttng_session); |
57167058 DG |
613 | } |
614 | ||
9f19cc17 DG |
615 | /* |
616 | * List domain of a session. | |
617 | */ | |
cd80958d DG |
618 | int lttng_list_domains(struct lttng_handle *handle, |
619 | struct lttng_domain **domains) | |
9f19cc17 DG |
620 | { |
621 | int ret; | |
cd80958d DG |
622 | struct lttcomm_session_msg lsm; |
623 | ||
624 | if (!handle) { | |
625 | return -1; | |
626 | } | |
9f19cc17 | 627 | |
cd80958d DG |
628 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
629 | ||
630 | copy_string(lsm.session.name, handle->session_name, | |
631 | sizeof(lsm.session.name)); | |
632 | ||
633 | ret = ask_sessiond(&lsm, (void**) domains); | |
9f19cc17 DG |
634 | if (ret < 0) { |
635 | return ret; | |
636 | } | |
637 | ||
638 | return ret / sizeof(struct lttng_domain); | |
639 | } | |
640 | ||
641 | /* | |
642 | * List channels of a session | |
643 | */ | |
cd80958d DG |
644 | int lttng_list_channels(struct lttng_handle *handle, |
645 | struct lttng_channel **channels) | |
9f19cc17 DG |
646 | { |
647 | int ret; | |
cd80958d DG |
648 | struct lttcomm_session_msg lsm; |
649 | ||
650 | if (!handle) { | |
651 | return -1; | |
652 | } | |
653 | ||
654 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
655 | copy_string(lsm.session.name, handle->session_name, | |
656 | sizeof(lsm.session.name)); | |
9f19cc17 | 657 | |
cd80958d | 658 | copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 659 | |
cd80958d | 660 | ret = ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
661 | if (ret < 0) { |
662 | return ret; | |
663 | } | |
664 | ||
665 | return ret / sizeof(struct lttng_channel); | |
666 | } | |
667 | ||
668 | /* | |
669 | * List events of a session channel. | |
670 | */ | |
cd80958d DG |
671 | int lttng_list_events(struct lttng_handle *handle, |
672 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
673 | { |
674 | int ret; | |
cd80958d | 675 | struct lttcomm_session_msg lsm; |
9f19cc17 | 676 | |
cd80958d DG |
677 | if (!handle) { |
678 | return -1; | |
679 | } | |
680 | ||
681 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
682 | copy_string(lsm.session.name, handle->session_name, | |
683 | sizeof(lsm.session.name)); | |
684 | copy_string(lsm.u.list.channel_name, channel_name, | |
685 | sizeof(lsm.u.list.channel_name)); | |
686 | ||
687 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
9f19cc17 | 688 | |
cd80958d | 689 | ret = ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
690 | if (ret < 0) { |
691 | return ret; | |
692 | } | |
693 | ||
694 | return ret / sizeof(struct lttng_event); | |
695 | } | |
696 | ||
fac6795d DG |
697 | /* |
698 | * lttng_set_tracing_group | |
699 | * | |
700 | * Set tracing group variable with name. This function | |
701 | * allocate memory pointed by tracing_group. | |
702 | */ | |
703 | int lttng_set_tracing_group(const char *name) | |
704 | { | |
705 | if (asprintf(&tracing_group, "%s", name) < 0) { | |
706 | return -ENOMEM; | |
707 | } | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
d0254c7c MD |
712 | /* |
713 | * lttng_calibrate | |
714 | */ | |
cd80958d | 715 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
716 | struct lttng_calibrate *calibrate) |
717 | { | |
cd80958d | 718 | struct lttcomm_session_msg lsm; |
d0254c7c | 719 | |
cd80958d DG |
720 | if (!handle) { |
721 | return -1; | |
722 | } | |
d0254c7c | 723 | |
cd80958d DG |
724 | lsm.cmd_type = LTTNG_CALIBRATE; |
725 | copy_lttng_domain(&lsm.domain, &handle->domain); | |
d0254c7c | 726 | |
cd80958d DG |
727 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
728 | ||
729 | return ask_sessiond(&lsm, NULL); | |
d0254c7c MD |
730 | } |
731 | ||
fac6795d DG |
732 | /* |
733 | * lttng_check_session_daemon | |
734 | * | |
947308c4 DG |
735 | * Yes, return 1 |
736 | * No, return 0 | |
737 | * Error, return negative value | |
fac6795d | 738 | */ |
947308c4 | 739 | int lttng_session_daemon_alive(void) |
fac6795d DG |
740 | { |
741 | int ret; | |
742 | ||
743 | ret = set_session_daemon_path(); | |
744 | if (ret < 0) { | |
947308c4 | 745 | /* Error */ |
fac6795d DG |
746 | return ret; |
747 | } | |
748 | ||
7d8234d9 | 749 | /* If socket exist, we check if the daemon listens to connect. */ |
fac6795d DG |
750 | ret = access(sessiond_sock_path, F_OK); |
751 | if (ret < 0) { | |
947308c4 DG |
752 | /* Not alive */ |
753 | return 0; | |
fac6795d DG |
754 | } |
755 | ||
7d8234d9 MD |
756 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
757 | if (ret < 0) { | |
758 | /* Not alive */ | |
759 | return 0; | |
760 | } | |
761 | ret = lttcomm_close_unix_sock(ret); | |
762 | if (ret < 0) | |
763 | perror("lttcomm_close_unix_sock"); | |
764 | ||
947308c4 DG |
765 | /* Is alive */ |
766 | return 1; | |
fac6795d DG |
767 | } |
768 | ||
769 | /* | |
770 | * lib constructor | |
771 | */ | |
772 | static void __attribute__((constructor)) init() | |
773 | { | |
774 | /* Set default session group */ | |
64a23ac4 | 775 | lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP); |
fac6795d | 776 | } |