Major changes
[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 * ask_sessiond
93 *
94 * Ask the session daemon a specific command and put the data into buf.
95 *
96 * Return size of data (only payload, not header).
97 */
98 static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
99 {
100 int ret;
101 size_t size;
102 void *data = NULL;
103
104 ret = lttng_connect_sessiond();
105 if (ret < 0) {
106 goto end;
107 }
108
109 lsm.cmd_type = lct;
110
111 /* Send command to session daemon */
112 ret = send_data_sessiond();
113 if (ret < 0) {
114 goto end;
115 }
116
117 /* Get header from data transmission */
118 ret = recv_data_sessiond(&llm, sizeof(llm));
119 if (ret < 0) {
120 goto end;
121 }
122
123 /* Check error code if OK */
124 if (llm.ret_code != LTTCOMM_OK) {
125 ret = -llm.ret_code;
126 goto end;
127 }
128
129 size = llm.data_size;
130 if (size == 0) {
131 goto end;
132 }
133
134 data = (void*) malloc(size);
135
136 /* Get payload data */
137 ret = recv_data_sessiond(data, size);
138 if (ret < 0) {
139 free(data);
140 goto end;
141 }
142
143 *buf = data;
144 ret = size;
145
146 end:
147 lttng_disconnect_sessiond();
148 return ret;
149 }
150
151 /*
152 * check_tracing_group
153 *
154 * Check if the specified group name exist.
155 * If yes, 0, else -1
156 */
157 static 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
196 free_list:
197 free(grp_list);
198
199 end:
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 */
209 static 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
228 /*
229 * lttng_start_tracing
230 *
231 * Start tracing for all trace of the session.
232 */
233 int 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 }
238
239 /*
240 * lttng_stop_tracing
241 *
242 * Stop tracing for all trace of the session.
243 */
244 int 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
252 */
253
254 /*
255 * lttng_kernel_enable_event
256 */
257 int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
258 {
259 int ret;
260
261 if (strlen(channel_name) == 0) {
262 strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
263 } else {
264 strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
265 }
266
267 if (ev == NULL) {
268 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
269 } else {
270 memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
271 ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
272 }
273
274 return ret;
275 }
276
277 /*
278 * lttng_kernel_disable_event
279 *
280 * Disable an event in the kernel tracer.
281 */
282 int lttng_kernel_disable_event(char *name, char *channel_name)
283 {
284 int ret;
285
286 if (strlen(channel_name) == 0) {
287 strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
288 } else {
289 strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
290 }
291
292 if (name == NULL) {
293 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
294 } else {
295 strncpy(lsm.u.disable.name, name, NAME_MAX);
296 ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
297 }
298
299 return ret;
300 }
301
302 /*
303 * lttng_kernel_enable_channel
304 *
305 * Enable recording for a channel for the kernel tracer.
306 */
307 int lttng_kernel_enable_channel(char *name)
308 {
309 return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
310 }
311
312 /*
313 * lttng_kernel_disable_channel
314 *
315 * Disable recording for the channel for the kernel tracer.
316 */
317 int lttng_kernel_disable_channel(char *name)
318 {
319 return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
320 }
321
322 /*
323 * lttng_kernel_create_channel
324 *
325 * Create a channel in the kernel tracer.
326 */
327 int lttng_kernel_create_channel(struct lttng_channel *chan)
328 {
329 memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
330 return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
331 }
332
333 /*
334 * lttng_list_events
335 *
336 * List all available events in the kernel.
337 *
338 * Return the size (bytes) of the list and set the event_list array.
339 * On error, return negative value.
340 */
341 int lttng_kernel_list_events(char **event_list)
342 {
343 return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
344 }
345
346 /*
347 * END Kernel control API
348 */
349
350 /*
351 * lttng_get_readable_code
352 *
353 * Return a human readable string of code
354 */
355 const char *lttng_get_readable_code(int code)
356 {
357 if (code > -LTTCOMM_OK) {
358 return "Ended with errors";
359 }
360
361 return lttcomm_get_readable_code(code);
362 }
363
364 /*
365 * lttng_ust_list_apps
366 *
367 * Ask the session daemon for all UST traceable applications.
368 *
369 * Return the number of pids.
370 * On error, return negative value.
371 */
372 int lttng_ust_list_traceable_apps(pid_t **pids)
373 {
374 int ret;
375
376 ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
377 if (ret < 0) {
378 return ret;
379 }
380
381 return ret / sizeof(pid_t);
382 }
383
384 /*
385 * lttng_list_traces
386 *
387 * Ask the session daemon for all traces (kernel and ust) for the session
388 * identified by name.
389 *
390 * Return the number of traces.
391 * On error, return negative value.
392 */
393 /*
394 int lttng_list_traces(char *session_name, struct lttng_trace **traces)
395 {
396 int ret;
397
398 strncpy(lsm.session_name, session_name, NAME_MAX);
399
400 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
401 if (ret < 0) {
402 return ret;
403 }
404
405 return ret / sizeof(struct lttng_trace);
406 }
407 */
408
409 /*
410 * lttng_create_session
411 *
412 * Create a brand new session using name.
413 */
414 int lttng_create_session(char *name, char *path)
415 {
416 strncpy(lsm.session_name, name, NAME_MAX);
417 strncpy(lsm.path, path, PATH_MAX);
418 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
419 }
420
421 /*
422 * lttng_destroy_session
423 *
424 * Destroy session using name.
425 */
426 int lttng_destroy_session(char *name)
427 {
428 strncpy(lsm.session_name, name, NAME_MAX);
429 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
430 }
431
432 /*
433 * lttng_list_sessions
434 *
435 * Ask the session daemon for all available sessions.
436 *
437 * Return number of session.
438 * On error, return negative value.
439 */
440 int lttng_list_sessions(struct lttng_session **sessions)
441 {
442 int ret;
443
444 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
445 if (ret < 0) {
446 return ret;
447 }
448
449 return ret / sizeof(struct lttng_session);
450 }
451
452 /*
453 * lttng_connect_sessiond
454 *
455 * Connect to the LTTng session daemon.
456 * On success, return 0
457 * On error, return a negative value
458 */
459 int lttng_connect_sessiond(void)
460 {
461 int ret;
462
463 ret = set_session_daemon_path();
464 if (ret < 0) {
465 return ret;
466 }
467
468 /* Connect to the sesssion daemon */
469 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
470 if (ret < 0) {
471 return ret;
472 }
473
474 sessiond_socket = ret;
475 connected = 1;
476
477 return 0;
478 }
479
480 /*
481 * lttng_disconnect_sessiond
482 *
483 * Clean disconnect the session daemon.
484 */
485 int lttng_disconnect_sessiond(void)
486 {
487 int ret = 0;
488
489 if (connected) {
490 ret = lttcomm_close_unix_sock(sessiond_socket);
491 sessiond_socket = 0;
492 connected = 0;
493 }
494
495 return ret;
496 }
497
498 void lttng_set_session_name(char *name)
499 {
500 strncpy(lsm.session_name, name, NAME_MAX);
501 }
502
503 /*
504 * lttng_set_tracing_group
505 *
506 * Set tracing group variable with name. This function
507 * allocate memory pointed by tracing_group.
508 */
509 int lttng_set_tracing_group(const char *name)
510 {
511 if (asprintf(&tracing_group, "%s", name) < 0) {
512 return -ENOMEM;
513 }
514
515 return 0;
516 }
517
518 /*
519 * lttng_check_session_daemon
520 *
521 * Yes, return 1
522 * No, return 0
523 * Error, return negative value
524 */
525 int lttng_session_daemon_alive(void)
526 {
527 int ret;
528
529 ret = set_session_daemon_path();
530 if (ret < 0) {
531 /* Error */
532 return ret;
533 }
534
535 /* If socket exist, we consider the daemon started */
536 ret = access(sessiond_sock_path, F_OK);
537 if (ret < 0) {
538 /* Not alive */
539 return 0;
540 }
541
542 /* Is alive */
543 return 1;
544 }
545
546 /*
547 * lib constructor
548 */
549 static void __attribute__((constructor)) init()
550 {
551 /* Set default session group */
552 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
553 }
This page took 0.040083 seconds and 5 git commands to generate.