Remove connect/disconnect sessiond from lttng API
[lttng-tools.git] / liblttngctl / liblttngctl.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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
27 #include <lttng/lttng.h>
28
29 #include "liblttsessiondcomm.h"
30 #include "lttngerr.h"
31 #include "lttng-share.h"
32
33 /* Socket to session daemon for communication */
34 static int sessiond_socket;
35 static char sessiond_sock_path[PATH_MAX];
36
37 /* Communication structure to ltt-sessiond */
38 static struct lttcomm_session_msg lsm;
39 static struct lttcomm_lttng_msg llm;
40
41 /* Variables */
42 static char *tracing_group;
43 static int connected;
44
45 /*
46 * send_data_sessiond
47 *
48 * Send lttcomm_session_msg to the session daemon.
49 *
50 * On success, return 0
51 * On error, return error code
52 */
53 static int send_data_sessiond(void)
54 {
55 int ret;
56
57 if (!connected) {
58 ret = -ENOTCONN;
59 goto end;
60 }
61
62 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
63
64 end:
65 return ret;
66 }
67
68 /*
69 * recv_data_sessiond
70 *
71 * Receive data from the sessiond socket.
72 *
73 * On success, return 0
74 * On error, return recv() error code
75 */
76 static int recv_data_sessiond(void *buf, size_t len)
77 {
78 int ret;
79
80 if (!connected) {
81 ret = -ENOTCONN;
82 goto end;
83 }
84
85 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
86
87 end:
88 return ret;
89 }
90
91 /*
92 * Check if the specified group name exist.
93 *
94 * If yes return 0, else return -1.
95 */
96 static 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
135 free_list:
136 free(grp_list);
137
138 end:
139 return ret;
140 }
141
142 /*
143 * Set sessiond socket path by putting it in the global sessiond_sock_path
144 * variable.
145 */
146 static 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
165 /*
166 * Connect to the LTTng session daemon.
167 *
168 * On success, return 0. On error, return -1.
169 */
170 static 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 */
194 static 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 */
214 static 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
262 end:
263 disconnect_sessiond();
264 return ret;
265 }
266
267 /*
268 * lttng_start_tracing
269 *
270 * Start tracing for all trace of the session.
271 */
272 int 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 }
277
278 /*
279 * lttng_stop_tracing
280 *
281 * Stop tracing for all trace of the session.
282 */
283 int 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
291 */
292
293 /*
294 * lttng_kernel_add_context
295 */
296 int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
297 char *event_name, char *channel_name)
298 {
299 if (channel_name != NULL) {
300 strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
301 }
302
303 if (event_name != NULL) {
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
311 /*
312 * lttng_kernel_enable_event
313 */
314 int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
315 {
316 int ret;
317
318 if (channel_name == NULL) {
319 strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
320 } else {
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);
329 }
330
331 return ret;
332 }
333
334 /*
335 * lttng_kernel_disable_event
336 *
337 * Disable an event in the kernel tracer.
338 */
339 int lttng_kernel_disable_event(char *name, char *channel_name)
340 {
341 int ret;
342
343 if (channel_name == NULL) {
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;
357 }
358
359 /*
360 * lttng_kernel_enable_channel
361 *
362 * Enable recording for a channel for the kernel tracer.
363 */
364 int lttng_kernel_enable_channel(char *name)
365 {
366 strncpy(lsm.u.enable.channel_name, name, NAME_MAX);
367 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
368 }
369
370 /*
371 * lttng_kernel_disable_channel
372 *
373 * Disable recording for the channel for the kernel tracer.
374 */
375 int lttng_kernel_disable_channel(char *name)
376 {
377 strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
378 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
379 }
380
381 /*
382 * lttng_kernel_create_channel
383 *
384 * Create a channel in the kernel tracer.
385 */
386 int lttng_kernel_create_channel(struct lttng_channel *chan)
387 {
388 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
389 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
390 }
391
392 /*
393 * lttng_list_events
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 */
400 int lttng_kernel_list_events(char **event_list)
401 {
402 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
403 }
404
405 /*
406 * END Kernel control API
407 */
408
409 /*
410 * lttng_get_readable_code
411 *
412 * Return a human readable string of code
413 */
414 const 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
423 /*
424 * lttng_ust_list_apps
425 *
426 * Ask the session daemon for all UST traceable applications.
427 *
428 * Return the number of pids.
429 * On error, return negative value.
430 */
431 int lttng_ust_list_traceable_apps(pid_t **pids)
432 {
433 int ret;
434
435 ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
436 if (ret < 0) {
437 return ret;
438 }
439
440 return ret / sizeof(pid_t);
441 }
442
443 /*
444 * lttng_list_traces
445 *
446 * Ask the session daemon for all traces (kernel and ust) for the session
447 * identified by name.
448 *
449 * Return the number of traces.
450 * On error, return negative value.
451 */
452 /*
453 int lttng_list_traces(char *session_name, struct lttng_trace **traces)
454 {
455 int ret;
456
457 strncpy(lsm.session_name, session_name, NAME_MAX);
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 }
466 */
467
468 /*
469 * lttng_create_session
470 *
471 * Create a brand new session using name.
472 */
473 int lttng_create_session(char *name, char *path)
474 {
475 strncpy(lsm.session_name, name, NAME_MAX);
476 strncpy(lsm.path, path, PATH_MAX);
477 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
478 }
479
480 /*
481 * lttng_destroy_session
482 *
483 * Destroy session using name.
484 */
485 int lttng_destroy_session(char *name)
486 {
487 strncpy(lsm.session_name, name, NAME_MAX);
488 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
489 }
490
491 /*
492 * lttng_list_sessions
493 *
494 * Ask the session daemon for all available sessions.
495 *
496 * Return number of session.
497 * On error, return negative value.
498 */
499 int lttng_list_sessions(struct lttng_session **sessions)
500 {
501 int ret;
502
503 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
504 if (ret < 0) {
505 return ret;
506 }
507
508 return ret / sizeof(struct lttng_session);
509 }
510
511 void lttng_set_session_name(char *name)
512 {
513 strncpy(lsm.session_name, name, NAME_MAX);
514 }
515
516 /*
517 * lttng_set_tracing_group
518 *
519 * Set tracing group variable with name. This function
520 * allocate memory pointed by tracing_group.
521 */
522 int 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 *
534 * Yes, return 1
535 * No, return 0
536 * Error, return negative value
537 */
538 int lttng_session_daemon_alive(void)
539 {
540 int ret;
541
542 ret = set_session_daemon_path();
543 if (ret < 0) {
544 /* Error */
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) {
551 /* Not alive */
552 return 0;
553 }
554
555 /* Is alive */
556 return 1;
557 }
558
559 /*
560 * lib constructor
561 */
562 static void __attribute__((constructor)) init()
563 {
564 /* Set default session group */
565 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
566 }
This page took 0.040634 seconds and 5 git commands to generate.