Add kernel metadata support to lttng and 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
32 /* Socket to session daemon for communication */
33 static int sessiond_socket;
34 static char sessiond_sock_path[PATH_MAX];
35
36 /* Communication structure to ltt-sessiond */
37 static struct lttcomm_session_msg lsm;
38 static struct lttcomm_lttng_msg llm;
39
40 /* Prototypes */
41 static int check_tracing_group(const char *grp_name);
42 static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf);
43 static int recv_data_sessiond(void *buf, size_t len);
44 static int send_data_sessiond(void);
45 static int set_session_daemon_path(void);
46
47 /* Variables */
48 static char *tracing_group;
49 static int connected;
50
51 /*
52 * send_data_sessiond
53 *
54 * Send lttcomm_session_msg to the session daemon.
55 *
56 * On success, return 0
57 * On error, return error code
58 */
59 static int send_data_sessiond(void)
60 {
61 int ret;
62
63 if (!connected) {
64 ret = -ENOTCONN;
65 goto end;
66 }
67
68 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
69
70 end:
71 return ret;
72 }
73
74 /*
75 * recv_data_sessiond
76 *
77 * Receive data from the sessiond socket.
78 *
79 * On success, return 0
80 * On error, return recv() error code
81 */
82 static int recv_data_sessiond(void *buf, size_t len)
83 {
84 int ret;
85
86 if (!connected) {
87 ret = -ENOTCONN;
88 goto end;
89 }
90
91 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
92 if (ret < 0) {
93 goto end;
94 }
95
96 end:
97 return ret;
98 }
99
100 /*
101 * ask_sessiond
102 *
103 * Ask the session daemon a specific command
104 * and put the data into buf.
105 *
106 * Return size of data (only payload, not header).
107 */
108 static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
109 {
110 int ret;
111 size_t size;
112 void *data = NULL;
113
114 ret = lttng_connect_sessiond();
115 if (ret < 0) {
116 goto end;
117 }
118
119 lsm.cmd_type = lct;
120
121 /* Send command to session daemon */
122 ret = send_data_sessiond();
123 if (ret < 0) {
124 goto end;
125 }
126
127 /* Get header from data transmission */
128 ret = recv_data_sessiond(&llm, sizeof(llm));
129 if (ret < 0) {
130 goto end;
131 }
132
133 /* Check error code if OK */
134 if (llm.ret_code != LTTCOMM_OK) {
135 ret = -llm.ret_code;
136 goto end;
137 }
138
139 size = llm.trace_name_offset + llm.data_size;
140 if (size == 0) {
141 goto end;
142 }
143
144 data = (void*) malloc(size);
145
146 /* Get payload data */
147 ret = recv_data_sessiond(data, size);
148 if (ret < 0) {
149 goto end;
150 }
151
152 *buf = data;
153 ret = size;
154
155 end:
156 lttng_disconnect_sessiond();
157 return ret;
158 }
159
160 /*
161 * BEGIN KERNEL CONTROL
162 */
163
164 /*
165 * lttng_kernel_enable_event
166 *
167 * Enable an event in the kernel tracer.
168 */
169 int lttng_kernel_enable_event(char *event_name)
170 {
171 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
172 return ask_sessiond(KERNEL_ENABLE_EVENT, NULL);
173 }
174
175 /*
176 * lttng_kernel_disable_event
177 *
178 * Disable an event in the kernel tracer.
179 */
180 int lttng_kernel_disable_event(char *event_name)
181 {
182 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
183 return ask_sessiond(KERNEL_DISABLE_EVENT, NULL);
184 }
185
186 /*
187 * lttng_kernel_create_session
188 *
189 * Create a session in the kernel tracer.
190 */
191 int lttng_kernel_create_session(void)
192 {
193 return ask_sessiond(KERNEL_CREATE_SESSION, NULL);
194 }
195
196 /*
197 * lttng_kernel_create_channel
198 *
199 * Create a channel in the kernel tracer.
200 */
201 int lttng_kernel_create_channel(void)
202 {
203 return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
204 }
205
206 /*
207 * lttng_kernel_open_metadata
208 *
209 * Open metadata in the kernel tracer.
210 */
211 int lttng_kernel_open_metadata(void)
212 {
213 return ask_sessiond(KERNEL_OPEN_METADATA, NULL);
214 }
215
216 /*
217 * lttng_kernel_start_tracing
218 *
219 * Start kernel tracing.
220 */
221 int lttng_kernel_start_tracing(void)
222 {
223 return ask_sessiond(KERNEL_START_TRACE, NULL);
224 }
225
226 /*
227 * lttng_kernel_stop_tracing
228 *
229 * Stop kernel tracing.
230 */
231 int lttng_kernel_stop_tracing(void)
232 {
233 return ask_sessiond(KERNEL_STOP_TRACE, NULL);
234 }
235
236 /*
237 * END KERNEL CONTROL
238 */
239
240 /*
241 * lttng_get_readable_code
242 *
243 * Return a human readable string of code
244 */
245 const char *lttng_get_readable_code(int code)
246 {
247 if (code > -LTTCOMM_OK) {
248 return "Ended with errors";
249 }
250
251 return lttcomm_get_readable_code(code);
252 }
253
254 /*
255 * lttng_ust_start_trace
256 *
257 * Request a trace start for pid.
258 */
259 int lttng_ust_start_trace(pid_t pid)
260 {
261 int ret;
262
263 lsm.pid = pid;
264 ret = ask_sessiond(UST_START_TRACE, NULL);
265
266 return ret;
267 }
268
269 /*
270 * lttng_ust_stop_trace
271 *
272 * Request a trace stop for pid.
273 */
274 int lttng_ust_stop_trace(pid_t pid)
275 {
276 int ret;
277
278 lsm.pid = pid;
279 ret = ask_sessiond(UST_STOP_TRACE, NULL);
280
281 return ret;
282 }
283
284 /*
285 * lttng_ust_create_trace
286 *
287 * Request a trace creation for pid.
288 */
289 int lttng_ust_create_trace(pid_t pid)
290 {
291 int ret;
292
293 lsm.pid = pid;
294 ret = ask_sessiond(UST_CREATE_TRACE, NULL);
295
296 return ret;
297 }
298
299 /*
300 * lttng_ust_list_apps
301 *
302 * Ask the session daemon for all UST traceable
303 * applications.
304 *
305 * Return the number of pids.
306 * On error, return negative value.
307 */
308 int lttng_ust_list_apps(pid_t **pids)
309 {
310 int ret;
311
312 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
313 if (ret < 0) {
314 return ret;
315 }
316
317 return ret / sizeof(pid_t);
318 }
319
320 /*
321 * lttng_list_traces
322 *
323 * Ask the session daemon for all traces (kernel and ust)
324 * for the session identified by uuid.
325 *
326 * Return the number of traces.
327 */
328 int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
329 {
330 int ret;
331
332 uuid_copy(lsm.session_uuid, *uuid);
333
334 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
335 if (ret < 0) {
336 return ret;
337 }
338
339 return ret / sizeof(struct lttng_trace);
340 }
341
342 /*
343 * lttng_create_session
344 *
345 * Create a brand new session using name.
346 */
347 int lttng_create_session(char *name)
348 {
349 int ret;
350
351 strncpy(lsm.session_name, name, sizeof(lsm.session_name));
352 lsm.session_name[sizeof(lsm.session_name) - 1] = '\0';
353
354 ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL);
355 if (ret < 0) {
356 goto end;
357 }
358
359 end:
360 return ret;
361 }
362
363 /*
364 * lttng_destroy_session
365 *
366 * Destroy session using name.
367 */
368 int lttng_destroy_session(uuid_t *uuid)
369 {
370 int ret;
371
372 uuid_copy(lsm.session_uuid, *uuid);
373
374 ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
375 if (ret < 0) {
376 goto end;
377 }
378
379 end:
380 return ret;
381 }
382
383 /*
384 * lttng_list_sessions
385 *
386 * Ask the session daemon for all available sessions.
387 *
388 * Return number of session.
389 * On error, return negative value.
390 */
391 int lttng_list_sessions(struct lttng_session **sessions)
392 {
393 int ret;
394
395 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
396 if (ret < 0) {
397 return ret;
398 }
399
400 return ret / sizeof(struct lttng_session);
401 }
402
403 /*
404 * lttng_connect_sessiond
405 *
406 * Connect to the LTTng session daemon.
407 * On success, return 0
408 * On error, return a negative value
409 */
410 int lttng_connect_sessiond(void)
411 {
412 int ret;
413
414 ret = set_session_daemon_path();
415 if (ret < 0) {
416 return ret;
417 }
418
419 /* Connect to the sesssion daemon */
420 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
421 if (ret < 0) {
422 return ret;
423 }
424
425 sessiond_socket = ret;
426 connected = 1;
427
428 return 0;
429 }
430
431 /*
432 * lttng_disconnect_sessiond
433 *
434 * Clean disconnect the session daemon.
435 */
436 int lttng_disconnect_sessiond(void)
437 {
438 int ret = 0;
439
440 if (connected) {
441 ret = lttcomm_close_unix_sock(sessiond_socket);
442 sessiond_socket = 0;
443 connected = 0;
444 }
445
446 return ret;
447 }
448
449 /*
450 * lttng_set_current_session_uuid
451 *
452 * Set the session uuid for current lsm.
453 */
454 void lttng_set_current_session_uuid(uuid_t *uuid)
455 {
456 uuid_copy(lsm.session_uuid, *uuid);
457 }
458
459 /*
460 * lttng_set_tracing_group
461 *
462 * Set tracing group variable with name. This function
463 * allocate memory pointed by tracing_group.
464 */
465 int lttng_set_tracing_group(const char *name)
466 {
467 if (asprintf(&tracing_group, "%s", name) < 0) {
468 return -ENOMEM;
469 }
470
471 return 0;
472 }
473
474 /*
475 * lttng_check_session_daemon
476 *
477 * Return 0 if a sesssion daemon is available
478 * else return -1
479 */
480 int lttng_check_session_daemon(void)
481 {
482 int ret;
483
484 ret = set_session_daemon_path();
485 if (ret < 0) {
486 return ret;
487 }
488
489 /* If socket exist, we consider the daemon started */
490 ret = access(sessiond_sock_path, F_OK);
491 if (ret < 0) {
492 return ret;
493 }
494
495 return 0;
496 }
497
498 /*
499 * set_session_daemon_path
500 *
501 * Set sessiond socket path by putting it in
502 * the global sessiond_sock_path variable.
503 */
504 static int set_session_daemon_path(void)
505 {
506 int ret;
507
508 /* Are we in the tracing group ? */
509 ret = check_tracing_group(tracing_group);
510 if (ret < 0 && getuid() != 0) {
511 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
512 getenv("HOME")) < 0) {
513 return -ENOMEM;
514 }
515 } else {
516 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
517 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
518 }
519
520 return 0;
521 }
522
523 /*
524 * check_tracing_group
525 *
526 * Check if the specified group name exist.
527 * If yes, 0, else -1
528 */
529 static int check_tracing_group(const char *grp_name)
530 {
531 struct group *grp_tracing; /* no free(). See getgrnam(3) */
532 gid_t *grp_list;
533 int grp_list_size, grp_id, i;
534 int ret = -1;
535
536 /* Get GID of group 'tracing' */
537 grp_tracing = getgrnam(grp_name);
538 if (grp_tracing == NULL) {
539 /* NULL means not found also. getgrnam(3) */
540 if (errno != 0) {
541 perror("getgrnam");
542 }
543 goto end;
544 }
545
546 /* Get number of supplementary group IDs */
547 grp_list_size = getgroups(0, NULL);
548 if (grp_list_size < 0) {
549 perror("getgroups");
550 goto end;
551 }
552
553 /* Alloc group list of the right size */
554 grp_list = malloc(grp_list_size * sizeof(gid_t));
555 grp_id = getgroups(grp_list_size, grp_list);
556 if (grp_id < -1) {
557 perror("getgroups");
558 goto free_list;
559 }
560
561 for (i = 0; i < grp_list_size; i++) {
562 if (grp_list[i] == grp_tracing->gr_gid) {
563 ret = 0;
564 break;
565 }
566 }
567
568 free_list:
569 free(grp_list);
570
571 end:
572 return ret;
573 }
574
575 /*
576 * lib constructor
577 */
578 static void __attribute__((constructor)) init()
579 {
580 /* Set default session group */
581 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
582 }
This page took 0.06409 seconds and 4 git commands to generate.