Remove connect/disconnect sessiond from lttng API
[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) {
247 goto end;
248 }
249
250 data = (void*) malloc(size);
251
252 /* Get payload data */
253 ret = recv_data_sessiond(data, size);
254 if (ret < 0) {
255 free(data);
256 goto end;
257 }
258
259 *buf = data;
260 ret = size;
261
262end:
263 disconnect_sessiond();
264 return ret;
265}
266
1df4dedd 267/*
f3ed775e
DG
268 * lttng_start_tracing
269 *
270 * Start tracing for all trace of the session.
1df4dedd 271 */
f3ed775e
DG
272int lttng_start_tracing(char *session_name)
273{
274 strncpy(lsm.session_name, session_name, NAME_MAX);
275 return ask_sessiond(LTTNG_START_TRACE, NULL);
276}
1df4dedd
DG
277
278/*
f3ed775e 279 * lttng_stop_tracing
1df4dedd 280 *
f3ed775e
DG
281 * Stop tracing for all trace of the session.
282 */
283int lttng_stop_tracing(char *session_name)
284{
285 strncpy(lsm.session_name, session_name, NAME_MAX);
286 return ask_sessiond(LTTNG_STOP_TRACE, NULL);
287}
288
289/*
290 * BEGIN Kernel control API
1df4dedd 291 */
f3ed775e 292
d65106b1
DG
293/*
294 * lttng_kernel_add_context
295 */
296int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
297 char *event_name, char *channel_name)
298{
94cf3c47 299 if (channel_name != NULL) {
d65106b1
DG
300 strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
301 }
302
94cf3c47 303 if (event_name != NULL) {
d65106b1
DG
304 strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
305 }
306
307 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_kernel_context));
308 return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
309}
310
f3ed775e
DG
311/*
312 * lttng_kernel_enable_event
313 */
314int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
1df4dedd 315{
33a2b854
DG
316 int ret;
317
94cf3c47 318 if (channel_name == NULL) {
f3ed775e 319 strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
33a2b854 320 } else {
f3ed775e
DG
321 strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
322 }
323
324 if (ev == NULL) {
325 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
326 } else {
327 memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
328 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
33a2b854
DG
329 }
330
331 return ret;
1df4dedd
DG
332}
333
334/*
335 * lttng_kernel_disable_event
336 *
337 * Disable an event in the kernel tracer.
338 */
f3ed775e 339int lttng_kernel_disable_event(char *name, char *channel_name)
1df4dedd 340{
f3ed775e 341 int ret;
1df4dedd 342
94cf3c47 343 if (channel_name == NULL) {
f3ed775e
DG
344 strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
345 } else {
346 strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
347 }
348
349 if (name == NULL) {
350 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
351 } else {
352 strncpy(lsm.u.disable.name, name, NAME_MAX);
353 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
354 }
355
356 return ret;
1df4dedd
DG
357}
358
359/*
f3ed775e 360 * lttng_kernel_enable_channel
1df4dedd 361 *
f3ed775e 362 * Enable recording for a channel for the kernel tracer.
1df4dedd 363 */
f3ed775e 364int lttng_kernel_enable_channel(char *name)
1df4dedd 365{
d36b8583 366 strncpy(lsm.u.enable.channel_name, name, NAME_MAX);
f3ed775e 367 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
1df4dedd 368}
a5c5a2bd
DG
369
370/*
f3ed775e 371 * lttng_kernel_disable_channel
a5c5a2bd 372 *
f3ed775e 373 * Disable recording for the channel for the kernel tracer.
a5c5a2bd 374 */
f3ed775e 375int lttng_kernel_disable_channel(char *name)
a5c5a2bd 376{
26cc6b4e 377 strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
f3ed775e 378 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
a5c5a2bd 379}
8c0faa1d
DG
380
381/*
f3ed775e 382 * lttng_kernel_create_channel
8c0faa1d 383 *
f3ed775e 384 * Create a channel in the kernel tracer.
8c0faa1d 385 */
f3ed775e 386int lttng_kernel_create_channel(struct lttng_channel *chan)
8c0faa1d 387{
f3ed775e
DG
388 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
389 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
8c0faa1d 390}
1df4dedd 391
2ef84c95 392/*
f3ed775e 393 * lttng_list_events
2ef84c95
DG
394 *
395 * List all available events in the kernel.
396 *
397 * Return the size (bytes) of the list and set the event_list array.
398 * On error, return negative value.
399 */
400int lttng_kernel_list_events(char **event_list)
401{
f3ed775e 402 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
2ef84c95
DG
403}
404
1df4dedd 405/*
f3ed775e 406 * END Kernel control API
1df4dedd
DG
407 */
408
ca95a216
DG
409/*
410 * lttng_get_readable_code
411 *
412 * Return a human readable string of code
413 */
414const char *lttng_get_readable_code(int code)
415{
416 if (code > -LTTCOMM_OK) {
417 return "Ended with errors";
418 }
419
420 return lttcomm_get_readable_code(code);
421}
422
fac6795d
DG
423/*
424 * lttng_ust_list_apps
425 *
947308c4 426 * Ask the session daemon for all UST traceable applications.
fac6795d 427 *
ca95a216
DG
428 * Return the number of pids.
429 * On error, return negative value.
fac6795d 430 */
f3ed775e 431int lttng_ust_list_traceable_apps(pid_t **pids)
fac6795d 432{
ca95a216 433 int ret;
fac6795d 434
f3ed775e 435 ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
fac6795d 436 if (ret < 0) {
ca95a216 437 return ret;
fac6795d
DG
438 }
439
ca95a216 440 return ret / sizeof(pid_t);
fac6795d
DG
441}
442
1657e9bb
DG
443/*
444 * lttng_list_traces
445 *
947308c4 446 * Ask the session daemon for all traces (kernel and ust) for the session
f3ed775e 447 * identified by name.
1657e9bb
DG
448 *
449 * Return the number of traces.
947308c4 450 * On error, return negative value.
1657e9bb 451 */
f3ed775e
DG
452/*
453int lttng_list_traces(char *session_name, struct lttng_trace **traces)
1657e9bb
DG
454{
455 int ret;
456
f3ed775e 457 strncpy(lsm.session_name, session_name, NAME_MAX);
1657e9bb
DG
458
459 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
460 if (ret < 0) {
461 return ret;
462 }
463
464 return ret / sizeof(struct lttng_trace);
465}
f3ed775e 466*/
1657e9bb 467
aaf97519
DG
468/*
469 * lttng_create_session
470 *
894be886 471 * Create a brand new session using name.
aaf97519 472 */
f3ed775e 473int lttng_create_session(char *name, char *path)
aaf97519 474{
947308c4 475 strncpy(lsm.session_name, name, NAME_MAX);
f3ed775e 476 strncpy(lsm.path, path, PATH_MAX);
947308c4 477 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
478}
479
480/*
481 * lttng_destroy_session
482 *
483 * Destroy session using name.
484 */
f3ed775e 485int lttng_destroy_session(char *name)
8028d920 486{
f3ed775e 487 strncpy(lsm.session_name, name, NAME_MAX);
947308c4 488 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
489}
490
57167058
DG
491/*
492 * lttng_list_sessions
493 *
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
f3ed775e 511void lttng_set_session_name(char *name)
e8be5f4f 512{
f3ed775e 513 strncpy(lsm.session_name, name, NAME_MAX);
e8be5f4f
DG
514}
515
fac6795d
DG
516/*
517 * lttng_set_tracing_group
518 *
519 * Set tracing group variable with name. This function
520 * allocate memory pointed by tracing_group.
521 */
522int lttng_set_tracing_group(const char *name)
523{
524 if (asprintf(&tracing_group, "%s", name) < 0) {
525 return -ENOMEM;
526 }
527
528 return 0;
529}
530
531/*
532 * lttng_check_session_daemon
533 *
947308c4
DG
534 * Yes, return 1
535 * No, return 0
536 * Error, return negative value
fac6795d 537 */
947308c4 538int lttng_session_daemon_alive(void)
fac6795d
DG
539{
540 int ret;
541
542 ret = set_session_daemon_path();
543 if (ret < 0) {
947308c4 544 /* Error */
fac6795d
DG
545 return ret;
546 }
547
548 /* If socket exist, we consider the daemon started */
549 ret = access(sessiond_sock_path, F_OK);
550 if (ret < 0) {
947308c4
DG
551 /* Not alive */
552 return 0;
fac6795d
DG
553 }
554
947308c4
DG
555 /* Is alive */
556 return 1;
fac6795d
DG
557}
558
559/*
560 * lib constructor
561 */
562static void __attribute__((constructor)) init()
563{
564 /* Set default session group */
64a23ac4 565 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 566}
This page took 0.075523 seconds and 4 git commands to generate.