Add kernel context support
[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/*
ca95a216 92 * ask_sessiond
fac6795d 93 *
947308c4 94 * Ask the session daemon a specific command and put the data into buf.
ca95a216
DG
95 *
96 * Return size of data (only payload, not header).
fac6795d 97 */
6abb15de 98static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
fac6795d 99{
ca95a216
DG
100 int ret;
101 size_t size;
102 void *data = NULL;
ca95a216 103
7442b2ba
DG
104 ret = lttng_connect_sessiond();
105 if (ret < 0) {
106 goto end;
107 }
108
ca95a216
DG
109 lsm.cmd_type = lct;
110
111 /* Send command to session daemon */
112 ret = send_data_sessiond();
113 if (ret < 0) {
114 goto end;
fac6795d
DG
115 }
116
ca95a216 117 /* Get header from data transmission */
5461b305 118 ret = recv_data_sessiond(&llm, sizeof(llm));
ca95a216
DG
119 if (ret < 0) {
120 goto end;
121 }
fac6795d 122
ca95a216 123 /* Check error code if OK */
5461b305
DG
124 if (llm.ret_code != LTTCOMM_OK) {
125 ret = -llm.ret_code;
ca95a216
DG
126 goto end;
127 }
fac6795d 128
f3ed775e 129 size = llm.data_size;
ca95a216
DG
130 if (size == 0) {
131 goto end;
fac6795d
DG
132 }
133
ca95a216
DG
134 data = (void*) malloc(size);
135
136 /* Get payload data */
137 ret = recv_data_sessiond(data, size);
fac6795d 138 if (ret < 0) {
947308c4 139 free(data);
fac6795d
DG
140 goto end;
141 }
142
ca95a216
DG
143 *buf = data;
144 ret = size;
fac6795d
DG
145
146end:
7442b2ba 147 lttng_disconnect_sessiond();
fac6795d
DG
148 return ret;
149}
150
947308c4
DG
151/*
152 * check_tracing_group
153 *
154 * Check if the specified group name exist.
155 * If yes, 0, else -1
156 */
157static int check_tracing_group(const char *grp_name)
158{
159 struct group *grp_tracing; /* no free(). See getgrnam(3) */
160 gid_t *grp_list;
161 int grp_list_size, grp_id, i;
162 int ret = -1;
163
164 /* Get GID of group 'tracing' */
165 grp_tracing = getgrnam(grp_name);
166 if (grp_tracing == NULL) {
167 /* NULL means not found also. getgrnam(3) */
168 if (errno != 0) {
169 perror("getgrnam");
170 }
171 goto end;
172 }
173
174 /* Get number of supplementary group IDs */
175 grp_list_size = getgroups(0, NULL);
176 if (grp_list_size < 0) {
177 perror("getgroups");
178 goto end;
179 }
180
181 /* Alloc group list of the right size */
182 grp_list = malloc(grp_list_size * sizeof(gid_t));
183 grp_id = getgroups(grp_list_size, grp_list);
184 if (grp_id < -1) {
185 perror("getgroups");
186 goto free_list;
187 }
188
189 for (i = 0; i < grp_list_size; i++) {
190 if (grp_list[i] == grp_tracing->gr_gid) {
191 ret = 0;
192 break;
193 }
194 }
195
196free_list:
197 free(grp_list);
198
199end:
200 return ret;
201}
202
203/*
204 * set_session_daemon_path
205 *
206 * Set sessiond socket path by putting it in
207 * the global sessiond_sock_path variable.
208 */
209static int set_session_daemon_path(void)
210{
211 int ret;
212
213 /* Are we in the tracing group ? */
214 ret = check_tracing_group(tracing_group);
215 if (ret < 0 && getuid() != 0) {
216 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
217 getenv("HOME")) < 0) {
218 return -ENOMEM;
219 }
220 } else {
221 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
222 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
223 }
224
225 return 0;
226}
227
1df4dedd 228/*
f3ed775e
DG
229 * lttng_start_tracing
230 *
231 * Start tracing for all trace of the session.
1df4dedd 232 */
f3ed775e
DG
233int lttng_start_tracing(char *session_name)
234{
235 strncpy(lsm.session_name, session_name, NAME_MAX);
236 return ask_sessiond(LTTNG_START_TRACE, NULL);
237}
1df4dedd
DG
238
239/*
f3ed775e 240 * lttng_stop_tracing
1df4dedd 241 *
f3ed775e
DG
242 * Stop tracing for all trace of the session.
243 */
244int lttng_stop_tracing(char *session_name)
245{
246 strncpy(lsm.session_name, session_name, NAME_MAX);
247 return ask_sessiond(LTTNG_STOP_TRACE, NULL);
248}
249
250/*
251 * BEGIN Kernel control API
1df4dedd 252 */
f3ed775e 253
d65106b1
DG
254/*
255 * lttng_kernel_add_context
256 */
257int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
258 char *event_name, char *channel_name)
259{
260 if (strlen(channel_name) != 0) {
261 strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
262 }
263
264 if (strlen(event_name) != 0) {
265 strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
266 }
267
268 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_kernel_context));
269 return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
270}
271
f3ed775e
DG
272/*
273 * lttng_kernel_enable_event
274 */
275int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
1df4dedd 276{
33a2b854
DG
277 int ret;
278
f3ed775e
DG
279 if (strlen(channel_name) == 0) {
280 strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
33a2b854 281 } else {
f3ed775e
DG
282 strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
283 }
284
285 if (ev == NULL) {
286 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
287 } else {
288 memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
289 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
33a2b854
DG
290 }
291
292 return ret;
1df4dedd
DG
293}
294
295/*
296 * lttng_kernel_disable_event
297 *
298 * Disable an event in the kernel tracer.
299 */
f3ed775e 300int lttng_kernel_disable_event(char *name, char *channel_name)
1df4dedd 301{
f3ed775e 302 int ret;
1df4dedd 303
f3ed775e
DG
304 if (strlen(channel_name) == 0) {
305 strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
306 } else {
307 strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
308 }
309
310 if (name == NULL) {
311 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
312 } else {
313 strncpy(lsm.u.disable.name, name, NAME_MAX);
314 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
315 }
316
317 return ret;
1df4dedd
DG
318}
319
320/*
f3ed775e 321 * lttng_kernel_enable_channel
1df4dedd 322 *
f3ed775e 323 * Enable recording for a channel for the kernel tracer.
1df4dedd 324 */
f3ed775e 325int lttng_kernel_enable_channel(char *name)
1df4dedd 326{
d36b8583 327 strncpy(lsm.u.enable.channel_name, name, NAME_MAX);
f3ed775e 328 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
1df4dedd 329}
a5c5a2bd
DG
330
331/*
f3ed775e 332 * lttng_kernel_disable_channel
a5c5a2bd 333 *
f3ed775e 334 * Disable recording for the channel for the kernel tracer.
a5c5a2bd 335 */
f3ed775e 336int lttng_kernel_disable_channel(char *name)
a5c5a2bd 337{
26cc6b4e 338 strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
f3ed775e 339 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
a5c5a2bd 340}
8c0faa1d
DG
341
342/*
f3ed775e 343 * lttng_kernel_create_channel
8c0faa1d 344 *
f3ed775e 345 * Create a channel in the kernel tracer.
8c0faa1d 346 */
f3ed775e 347int lttng_kernel_create_channel(struct lttng_channel *chan)
8c0faa1d 348{
f3ed775e
DG
349 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
350 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
8c0faa1d 351}
1df4dedd 352
2ef84c95 353/*
f3ed775e 354 * lttng_list_events
2ef84c95
DG
355 *
356 * List all available events in the kernel.
357 *
358 * Return the size (bytes) of the list and set the event_list array.
359 * On error, return negative value.
360 */
361int lttng_kernel_list_events(char **event_list)
362{
f3ed775e 363 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
2ef84c95
DG
364}
365
1df4dedd 366/*
f3ed775e 367 * END Kernel control API
1df4dedd
DG
368 */
369
ca95a216
DG
370/*
371 * lttng_get_readable_code
372 *
373 * Return a human readable string of code
374 */
375const char *lttng_get_readable_code(int code)
376{
377 if (code > -LTTCOMM_OK) {
378 return "Ended with errors";
379 }
380
381 return lttcomm_get_readable_code(code);
382}
383
fac6795d
DG
384/*
385 * lttng_ust_list_apps
386 *
947308c4 387 * Ask the session daemon for all UST traceable applications.
fac6795d 388 *
ca95a216
DG
389 * Return the number of pids.
390 * On error, return negative value.
fac6795d 391 */
f3ed775e 392int lttng_ust_list_traceable_apps(pid_t **pids)
fac6795d 393{
ca95a216 394 int ret;
fac6795d 395
f3ed775e 396 ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
fac6795d 397 if (ret < 0) {
ca95a216 398 return ret;
fac6795d
DG
399 }
400
ca95a216 401 return ret / sizeof(pid_t);
fac6795d
DG
402}
403
1657e9bb
DG
404/*
405 * lttng_list_traces
406 *
947308c4 407 * Ask the session daemon for all traces (kernel and ust) for the session
f3ed775e 408 * identified by name.
1657e9bb
DG
409 *
410 * Return the number of traces.
947308c4 411 * On error, return negative value.
1657e9bb 412 */
f3ed775e
DG
413/*
414int lttng_list_traces(char *session_name, struct lttng_trace **traces)
1657e9bb
DG
415{
416 int ret;
417
f3ed775e 418 strncpy(lsm.session_name, session_name, NAME_MAX);
1657e9bb
DG
419
420 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
421 if (ret < 0) {
422 return ret;
423 }
424
425 return ret / sizeof(struct lttng_trace);
426}
f3ed775e 427*/
1657e9bb 428
aaf97519
DG
429/*
430 * lttng_create_session
431 *
894be886 432 * Create a brand new session using name.
aaf97519 433 */
f3ed775e 434int lttng_create_session(char *name, char *path)
aaf97519 435{
947308c4 436 strncpy(lsm.session_name, name, NAME_MAX);
f3ed775e 437 strncpy(lsm.path, path, PATH_MAX);
947308c4 438 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
439}
440
441/*
442 * lttng_destroy_session
443 *
444 * Destroy session using name.
445 */
f3ed775e 446int lttng_destroy_session(char *name)
8028d920 447{
f3ed775e 448 strncpy(lsm.session_name, name, NAME_MAX);
947308c4 449 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
450}
451
57167058
DG
452/*
453 * lttng_list_sessions
454 *
455 * Ask the session daemon for all available sessions.
456 *
ca95a216
DG
457 * Return number of session.
458 * On error, return negative value.
57167058 459 */
ca95a216 460int lttng_list_sessions(struct lttng_session **sessions)
57167058 461{
ca95a216 462 int ret;
57167058 463
ca95a216 464 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 465 if (ret < 0) {
ca95a216 466 return ret;
57167058
DG
467 }
468
ca95a216 469 return ret / sizeof(struct lttng_session);
57167058
DG
470}
471
fac6795d
DG
472/*
473 * lttng_connect_sessiond
474 *
475 * Connect to the LTTng session daemon.
476 * On success, return 0
477 * On error, return a negative value
478 */
479int lttng_connect_sessiond(void)
480{
481 int ret;
482
483 ret = set_session_daemon_path();
484 if (ret < 0) {
485 return ret;
486 }
487
488 /* Connect to the sesssion daemon */
489 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
490 if (ret < 0) {
491 return ret;
492 }
493
494 sessiond_socket = ret;
495 connected = 1;
496
497 return 0;
498}
499
87378cf5
DG
500/*
501 * lttng_disconnect_sessiond
502 *
503 * Clean disconnect the session daemon.
504 */
505int lttng_disconnect_sessiond(void)
506{
507 int ret = 0;
508
509 if (connected) {
510 ret = lttcomm_close_unix_sock(sessiond_socket);
511 sessiond_socket = 0;
512 connected = 0;
513 }
514
515 return ret;
516}
517
f3ed775e 518void lttng_set_session_name(char *name)
e8be5f4f 519{
f3ed775e 520 strncpy(lsm.session_name, name, NAME_MAX);
e8be5f4f
DG
521}
522
fac6795d
DG
523/*
524 * lttng_set_tracing_group
525 *
526 * Set tracing group variable with name. This function
527 * allocate memory pointed by tracing_group.
528 */
529int lttng_set_tracing_group(const char *name)
530{
531 if (asprintf(&tracing_group, "%s", name) < 0) {
532 return -ENOMEM;
533 }
534
535 return 0;
536}
537
538/*
539 * lttng_check_session_daemon
540 *
947308c4
DG
541 * Yes, return 1
542 * No, return 0
543 * Error, return negative value
fac6795d 544 */
947308c4 545int lttng_session_daemon_alive(void)
fac6795d
DG
546{
547 int ret;
548
549 ret = set_session_daemon_path();
550 if (ret < 0) {
947308c4 551 /* Error */
fac6795d
DG
552 return ret;
553 }
554
555 /* If socket exist, we consider the daemon started */
556 ret = access(sessiond_sock_path, F_OK);
557 if (ret < 0) {
947308c4
DG
558 /* Not alive */
559 return 0;
fac6795d
DG
560 }
561
947308c4
DG
562 /* Is alive */
563 return 1;
fac6795d
DG
564}
565
566/*
567 * lib constructor
568 */
569static void __attribute__((constructor)) init()
570{
571 /* Set default session group */
64a23ac4 572 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 573}
This page took 0.051818 seconds and 5 git commands to generate.