New public API for lttng control
[lttng-tools.git] / liblttngctl / liblttngctl.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <grp.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
5b97ec60 27#include <lttng/lttng.h>
fac6795d
DG
28
29#include "liblttsessiondcomm.h"
30#include "lttngerr.h"
f3ed775e 31#include "lttng-share.h"
fac6795d
DG
32
33/* Socket to session daemon for communication */
34static int sessiond_socket;
35static char sessiond_sock_path[PATH_MAX];
36
37/* Communication structure to ltt-sessiond */
fac6795d 38static struct lttcomm_session_msg lsm;
5461b305 39static struct lttcomm_lttng_msg llm;
fac6795d 40
fac6795d
DG
41/* Variables */
42static char *tracing_group;
43static int connected;
44
45/*
ca95a216 46 * send_data_sessiond
fac6795d 47 *
e065084a 48 * Send lttcomm_session_msg to the session daemon.
fac6795d
DG
49 *
50 * On success, return 0
51 * On error, return error code
52 */
ca95a216 53static int send_data_sessiond(void)
fac6795d
DG
54{
55 int ret;
56
57 if (!connected) {
e065084a
DG
58 ret = -ENOTCONN;
59 goto end;
fac6795d
DG
60 }
61
62 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
e065084a
DG
63
64end:
65 return ret;
66}
67
68/*
ca95a216 69 * recv_data_sessiond
e065084a
DG
70 *
71 * Receive data from the sessiond socket.
72 *
73 * On success, return 0
74 * On error, return recv() error code
75 */
ca95a216 76static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
77{
78 int ret;
79
80 if (!connected) {
81 ret = -ENOTCONN;
82 goto end;
fac6795d
DG
83 }
84
ca95a216 85 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 86
e065084a 87end:
fac6795d
DG
88 return ret;
89}
90
91/*
947308c4 92 * Check if the specified group name exist.
65beb5ff
DG
93 *
94 * If yes return 0, else return -1.
947308c4
DG
95 */
96static int check_tracing_group(const char *grp_name)
97{
98 struct group *grp_tracing; /* no free(). See getgrnam(3) */
99 gid_t *grp_list;
100 int grp_list_size, grp_id, i;
101 int ret = -1;
102
103 /* Get GID of group 'tracing' */
104 grp_tracing = getgrnam(grp_name);
105 if (grp_tracing == NULL) {
106 /* NULL means not found also. getgrnam(3) */
107 if (errno != 0) {
108 perror("getgrnam");
109 }
110 goto end;
111 }
112
113 /* Get number of supplementary group IDs */
114 grp_list_size = getgroups(0, NULL);
115 if (grp_list_size < 0) {
116 perror("getgroups");
117 goto end;
118 }
119
120 /* Alloc group list of the right size */
121 grp_list = malloc(grp_list_size * sizeof(gid_t));
122 grp_id = getgroups(grp_list_size, grp_list);
123 if (grp_id < -1) {
124 perror("getgroups");
125 goto free_list;
126 }
127
128 for (i = 0; i < grp_list_size; i++) {
129 if (grp_list[i] == grp_tracing->gr_gid) {
130 ret = 0;
131 break;
132 }
133 }
134
135free_list:
136 free(grp_list);
137
138end:
139 return ret;
140}
141
142/*
65beb5ff
DG
143 * Set sessiond socket path by putting it in the global sessiond_sock_path
144 * variable.
947308c4
DG
145 */
146static int set_session_daemon_path(void)
147{
148 int ret;
149
150 /* Are we in the tracing group ? */
151 ret = check_tracing_group(tracing_group);
152 if (ret < 0 && getuid() != 0) {
153 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
154 getenv("HOME")) < 0) {
155 return -ENOMEM;
156 }
157 } else {
158 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
159 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
160 }
161
162 return 0;
163}
164
65beb5ff
DG
165/*
166 * Connect to the LTTng session daemon.
167 *
168 * On success, return 0. On error, return -1.
169 */
170static int connect_sessiond(void)
171{
172 int ret;
173
174 ret = set_session_daemon_path();
175 if (ret < 0) {
176 return ret;
177 }
178
179 /* Connect to the sesssion daemon */
180 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
181 if (ret < 0) {
182 return ret;
183 }
184
185 sessiond_socket = ret;
186 connected = 1;
187
188 return 0;
189}
190
191/*
192 * Clean disconnect the session daemon.
193 */
194static int disconnect_sessiond(void)
195{
196 int ret = 0;
197
198 if (connected) {
199 ret = lttcomm_close_unix_sock(sessiond_socket);
200 sessiond_socket = 0;
201 connected = 0;
202 }
203
204 return ret;
205}
206
207/*
208 * ask_sessiond
209 *
210 * Ask the session daemon a specific command and put the data into buf.
211 *
212 * Return size of data (only payload, not header).
213 */
214static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
215{
216 int ret;
217 size_t size;
218 void *data = NULL;
219
220 ret = connect_sessiond();
221 if (ret < 0) {
222 goto end;
223 }
224
225 lsm.cmd_type = lct;
226
227 /* Send command to session daemon */
228 ret = send_data_sessiond();
229 if (ret < 0) {
230 goto end;
231 }
232
233 /* Get header from data transmission */
234 ret = recv_data_sessiond(&llm, sizeof(llm));
235 if (ret < 0) {
236 goto end;
237 }
238
239 /* Check error code if OK */
240 if (llm.ret_code != LTTCOMM_OK) {
241 ret = -llm.ret_code;
242 goto end;
243 }
244
245 size = llm.data_size;
246 if (size == 0) {
7d29a247 247 ret = 0;
65beb5ff
DG
248 goto end;
249 }
250
251 data = (void*) malloc(size);
252
253 /* Get payload data */
254 ret = recv_data_sessiond(data, size);
255 if (ret < 0) {
256 free(data);
257 goto end;
258 }
259
260 *buf = data;
261 ret = size;
262
263end:
264 disconnect_sessiond();
265 return ret;
266}
267
1df4dedd 268/*
f3ed775e 269 * Start tracing for all trace of the session.
1df4dedd 270 */
f3ed775e
DG
271int lttng_start_tracing(char *session_name)
272{
273 strncpy(lsm.session_name, session_name, NAME_MAX);
274 return ask_sessiond(LTTNG_START_TRACE, NULL);
275}
1df4dedd
DG
276
277/*
f3ed775e
DG
278 * Stop tracing for all trace of the session.
279 */
280int lttng_stop_tracing(char *session_name)
281{
282 strncpy(lsm.session_name, session_name, NAME_MAX);
283 return ask_sessiond(LTTNG_STOP_TRACE, NULL);
284}
285
286/*
7d29a247 287 * lttng_add_context
1df4dedd 288 */
7d29a247
DG
289int lttng_add_context(struct lttng_domain *domain,
290 struct lttng_event_context *ctx, char *event_name, char *channel_name)
f3ed775e 291
d65106b1 292{
7d29a247
DG
293 int ret;
294
94cf3c47 295 if (channel_name != NULL) {
d65106b1
DG
296 strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
297 }
298
94cf3c47 299 if (event_name != NULL) {
d65106b1
DG
300 strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
301 }
302
7d29a247
DG
303 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
304
305 switch (domain->type) {
306 case LTTNG_DOMAIN_KERNEL:
307 ret = ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
308 break;
309 case LTTNG_DOMAIN_UST:
310 ret = LTTCOMM_NOT_IMPLEMENTED;
311 break;
312 default:
313 ret = LTTCOMM_UNKNOWN_DOMAIN;
314 break;
315 };
316
317 return ret;
d65106b1
DG
318}
319
f3ed775e 320/*
7d29a247 321 * lttng_enable_event
f3ed775e 322 */
7d29a247
DG
323int lttng_enable_event(struct lttng_domain *domain,
324 struct lttng_event *ev, char *channel_name)
1df4dedd 325{
33a2b854
DG
326 int ret;
327
94cf3c47 328 if (channel_name == NULL) {
f3ed775e 329 strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
33a2b854 330 } else {
f3ed775e
DG
331 strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
332 }
333
7d29a247
DG
334 switch (domain->type) {
335 case LTTNG_DOMAIN_KERNEL:
336 if (ev == NULL) {
337 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
338 } else {
339 memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
340 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
341 }
342 break;
343 case LTTNG_DOMAIN_UST:
344 ret = LTTCOMM_NOT_IMPLEMENTED;
345 break;
346 default:
347 ret = LTTCOMM_UNKNOWN_DOMAIN;
348 break;
349 };
33a2b854
DG
350
351 return ret;
1df4dedd
DG
352}
353
354/*
7d29a247 355 * Disable an event in the kernel tracer.
1df4dedd 356 */
7d29a247
DG
357int lttng_disable_event(struct lttng_domain *domain, char *name,
358 char *channel_name)
1df4dedd 359{
f3ed775e 360 int ret;
1df4dedd 361
94cf3c47 362 if (channel_name == NULL) {
f3ed775e
DG
363 strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
364 } else {
365 strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
366 }
367
7d29a247
DG
368 switch (domain->type) {
369 case LTTNG_DOMAIN_KERNEL:
370 if (name == NULL) {
371 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
372 } else {
373 strncpy(lsm.u.disable.name, name, NAME_MAX);
374 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
375 }
376 break;
377 case LTTNG_DOMAIN_UST:
378 ret = LTTCOMM_NOT_IMPLEMENTED;
379 break;
380 default:
381 ret = LTTCOMM_UNKNOWN_DOMAIN;
382 break;
383 };
f3ed775e
DG
384
385 return ret;
1df4dedd
DG
386}
387
388/*
7d29a247 389 * Enable recording for a channel for the kernel tracer.
a5c5a2bd 390 */
7d29a247 391int lttng_enable_channel(struct lttng_domain *domain, struct lttng_channel *chan)
a5c5a2bd 392{
7d29a247 393 int ret;
8c0faa1d 394
f3ed775e 395 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
7d29a247
DG
396
397 switch (domain->type) {
398 case LTTNG_DOMAIN_KERNEL:
399 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
400 break;
401 case LTTNG_DOMAIN_UST:
402 ret = LTTCOMM_NOT_IMPLEMENTED;
403 break;
404 default:
405 ret = LTTCOMM_UNKNOWN_DOMAIN;
406 break;
407 };
408
409 return ret;
8c0faa1d 410}
1df4dedd 411
2ef84c95 412/*
7d29a247 413 * Disable recording for the channel for the kernel tracer.
2ef84c95 414 */
7d29a247 415int lttng_disable_channel(struct lttng_domain *domain, char *name)
2ef84c95 416{
7d29a247 417 int ret;
2ef84c95 418
7d29a247 419 strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
1df4dedd 420
7d29a247
DG
421 switch (domain->type) {
422 case LTTNG_DOMAIN_KERNEL:
423 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
424 break;
425 case LTTNG_DOMAIN_UST:
426 ret = LTTCOMM_NOT_IMPLEMENTED;
427 break;
428 default:
429 ret = LTTCOMM_UNKNOWN_DOMAIN;
430 break;
431 };
ca95a216 432
7d29a247 433 return ret;
ca95a216
DG
434}
435
fac6795d 436/*
7d29a247 437 * List all available events in the kernel.
fac6795d 438 *
7d29a247
DG
439 * Return the size (bytes) of the list and set the event_list array.
440 * On error, return negative value.
fac6795d 441 */
7d29a247 442int lttng_list_events(struct lttng_domain *domain, char **event_list)
fac6795d 443{
ca95a216 444 int ret;
fac6795d 445
7d29a247
DG
446 switch (domain->type) {
447 case LTTNG_DOMAIN_KERNEL:
448 ret = ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
449 break;
450 case LTTNG_DOMAIN_UST:
451 ret = LTTCOMM_NOT_IMPLEMENTED;
452 break;
453 default:
454 ret = LTTCOMM_UNKNOWN_DOMAIN;
455 break;
456 };
fac6795d 457
7d29a247 458 return ret;
fac6795d
DG
459}
460
1657e9bb 461/*
7d29a247 462 * Return a human readable string of code
1657e9bb 463 */
7d29a247 464const char *lttng_get_readable_code(int code)
1657e9bb 465{
7d29a247
DG
466 if (code > -LTTCOMM_OK) {
467 return "Ended with errors";
1657e9bb
DG
468 }
469
7d29a247 470 return lttcomm_get_readable_code(code);
1657e9bb
DG
471}
472
aaf97519 473/*
894be886 474 * Create a brand new session using name.
aaf97519 475 */
f3ed775e 476int lttng_create_session(char *name, char *path)
aaf97519 477{
947308c4 478 strncpy(lsm.session_name, name, NAME_MAX);
f3ed775e 479 strncpy(lsm.path, path, PATH_MAX);
947308c4 480 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
481}
482
483/*
8028d920
DG
484 * Destroy session using name.
485 */
f3ed775e 486int lttng_destroy_session(char *name)
8028d920 487{
f3ed775e 488 strncpy(lsm.session_name, name, NAME_MAX);
947308c4 489 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
490}
491
57167058 492/*
57167058
DG
493 * Ask the session daemon for all available sessions.
494 *
ca95a216
DG
495 * Return number of session.
496 * On error, return negative value.
57167058 497 */
ca95a216 498int lttng_list_sessions(struct lttng_session **sessions)
57167058 499{
ca95a216 500 int ret;
57167058 501
ca95a216 502 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 503 if (ret < 0) {
ca95a216 504 return ret;
57167058
DG
505 }
506
ca95a216 507 return ret / sizeof(struct lttng_session);
57167058
DG
508}
509
7d29a247
DG
510/*
511 * Set session name for the current lsm.
512 */
f3ed775e 513void lttng_set_session_name(char *name)
e8be5f4f 514{
f3ed775e 515 strncpy(lsm.session_name, name, NAME_MAX);
e8be5f4f
DG
516}
517
fac6795d
DG
518/*
519 * lttng_set_tracing_group
520 *
521 * Set tracing group variable with name. This function
522 * allocate memory pointed by tracing_group.
523 */
524int lttng_set_tracing_group(const char *name)
525{
526 if (asprintf(&tracing_group, "%s", name) < 0) {
527 return -ENOMEM;
528 }
529
530 return 0;
531}
532
533/*
534 * lttng_check_session_daemon
535 *
947308c4
DG
536 * Yes, return 1
537 * No, return 0
538 * Error, return negative value
fac6795d 539 */
947308c4 540int lttng_session_daemon_alive(void)
fac6795d
DG
541{
542 int ret;
543
544 ret = set_session_daemon_path();
545 if (ret < 0) {
947308c4 546 /* Error */
fac6795d
DG
547 return ret;
548 }
549
550 /* If socket exist, we consider the daemon started */
551 ret = access(sessiond_sock_path, F_OK);
552 if (ret < 0) {
947308c4
DG
553 /* Not alive */
554 return 0;
fac6795d
DG
555 }
556
947308c4
DG
557 /* Is alive */
558 return 1;
fac6795d
DG
559}
560
561/*
562 * lib constructor
563 */
564static void __attribute__((constructor)) init()
565{
566 /* Set default session group */
64a23ac4 567 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 568}
This page took 0.077328 seconds and 4 git commands to generate.