Change lttng.h API char to const char
[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 */
38057ed1 271int lttng_start_tracing(const char *session_name)
f3ed775e
DG
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 */
38057ed1 280int lttng_stop_tracing(const char *session_name)
f3ed775e
DG
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 289int lttng_add_context(struct lttng_domain *domain,
38057ed1
DG
290 struct lttng_event_context *ctx, const char *event_name,
291 const char *channel_name)
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 323int lttng_enable_event(struct lttng_domain *domain,
38057ed1 324 struct lttng_event *ev, const 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 */
38057ed1
DG
357int lttng_disable_event(struct lttng_domain *domain, const char *name,
358 const 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 */
38057ed1
DG
391int lttng_enable_channel(struct lttng_domain *domain,
392 struct lttng_channel *chan)
a5c5a2bd 393{
7d29a247 394 int ret;
8c0faa1d 395
f3ed775e 396 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
7d29a247
DG
397
398 switch (domain->type) {
399 case LTTNG_DOMAIN_KERNEL:
400 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
401 break;
402 case LTTNG_DOMAIN_UST:
403 ret = LTTCOMM_NOT_IMPLEMENTED;
404 break;
405 default:
406 ret = LTTCOMM_UNKNOWN_DOMAIN;
407 break;
408 };
409
410 return ret;
8c0faa1d 411}
1df4dedd 412
2ef84c95 413/*
7d29a247 414 * Disable recording for the channel for the kernel tracer.
2ef84c95 415 */
38057ed1 416int lttng_disable_channel(struct lttng_domain *domain, const char *name)
2ef84c95 417{
7d29a247 418 int ret;
2ef84c95 419
7d29a247 420 strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
1df4dedd 421
7d29a247
DG
422 switch (domain->type) {
423 case LTTNG_DOMAIN_KERNEL:
424 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
425 break;
426 case LTTNG_DOMAIN_UST:
427 ret = LTTCOMM_NOT_IMPLEMENTED;
428 break;
429 default:
430 ret = LTTCOMM_UNKNOWN_DOMAIN;
431 break;
432 };
ca95a216 433
7d29a247 434 return ret;
ca95a216
DG
435}
436
fac6795d 437/*
7d29a247 438 * List all available events in the kernel.
fac6795d 439 *
7d29a247
DG
440 * Return the size (bytes) of the list and set the event_list array.
441 * On error, return negative value.
fac6795d 442 */
7d29a247 443int lttng_list_events(struct lttng_domain *domain, char **event_list)
fac6795d 444{
ca95a216 445 int ret;
fac6795d 446
7d29a247
DG
447 switch (domain->type) {
448 case LTTNG_DOMAIN_KERNEL:
449 ret = ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
450 break;
451 case LTTNG_DOMAIN_UST:
452 ret = LTTCOMM_NOT_IMPLEMENTED;
453 break;
454 default:
455 ret = LTTCOMM_UNKNOWN_DOMAIN;
456 break;
457 };
fac6795d 458
7d29a247 459 return ret;
fac6795d
DG
460}
461
1657e9bb 462/*
7d29a247 463 * Return a human readable string of code
1657e9bb 464 */
7d29a247 465const char *lttng_get_readable_code(int code)
1657e9bb 466{
7d29a247
DG
467 if (code > -LTTCOMM_OK) {
468 return "Ended with errors";
1657e9bb
DG
469 }
470
7d29a247 471 return lttcomm_get_readable_code(code);
1657e9bb
DG
472}
473
aaf97519 474/*
894be886 475 * Create a brand new session using name.
aaf97519 476 */
38057ed1 477int lttng_create_session(const char *name, const char *path)
aaf97519 478{
947308c4 479 strncpy(lsm.session_name, name, NAME_MAX);
f3ed775e 480 strncpy(lsm.path, path, PATH_MAX);
947308c4 481 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
482}
483
484/*
8028d920
DG
485 * Destroy session using name.
486 */
38057ed1 487int lttng_destroy_session(const char *name)
8028d920 488{
f3ed775e 489 strncpy(lsm.session_name, name, NAME_MAX);
947308c4 490 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
491}
492
57167058 493/*
57167058
DG
494 * Ask the session daemon for all available sessions.
495 *
ca95a216
DG
496 * Return number of session.
497 * On error, return negative value.
57167058 498 */
ca95a216 499int lttng_list_sessions(struct lttng_session **sessions)
57167058 500{
ca95a216 501 int ret;
57167058 502
ca95a216 503 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 504 if (ret < 0) {
ca95a216 505 return ret;
57167058
DG
506 }
507
ca95a216 508 return ret / sizeof(struct lttng_session);
57167058
DG
509}
510
7d29a247
DG
511/*
512 * Set session name for the current lsm.
513 */
38057ed1 514void lttng_set_session_name(const char *name)
e8be5f4f 515{
f3ed775e 516 strncpy(lsm.session_name, name, NAME_MAX);
e8be5f4f
DG
517}
518
fac6795d
DG
519/*
520 * lttng_set_tracing_group
521 *
522 * Set tracing group variable with name. This function
523 * allocate memory pointed by tracing_group.
524 */
525int lttng_set_tracing_group(const char *name)
526{
527 if (asprintf(&tracing_group, "%s", name) < 0) {
528 return -ENOMEM;
529 }
530
531 return 0;
532}
533
534/*
535 * lttng_check_session_daemon
536 *
947308c4
DG
537 * Yes, return 1
538 * No, return 0
539 * Error, return negative value
fac6795d 540 */
947308c4 541int lttng_session_daemon_alive(void)
fac6795d
DG
542{
543 int ret;
544
545 ret = set_session_daemon_path();
546 if (ret < 0) {
947308c4 547 /* Error */
fac6795d
DG
548 return ret;
549 }
550
551 /* If socket exist, we consider the daemon started */
552 ret = access(sessiond_sock_path, F_OK);
553 if (ret < 0) {
947308c4
DG
554 /* Not alive */
555 return 0;
fac6795d
DG
556 }
557
947308c4
DG
558 /* Is alive */
559 return 1;
fac6795d
DG
560}
561
562/*
563 * lib constructor
564 */
565static void __attribute__((constructor)) init()
566{
567 /* Set default session group */
64a23ac4 568 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 569}
This page took 0.050097 seconds and 5 git commands to generate.