Import kernel control interface
[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 memset(&lsm, 0, sizeof(lsm));
158 return ret;
159 }
160
161 /*
162 * BEGIN KERNEL CONTROL
163 */
164
165 /*
166 * lttng_kernel_enable_event
167 *
168 * Enable an event in the kernel tracer.
169 */
170 int lttng_kernel_enable_event(char *event_name)
171 {
172 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
173 return ask_sessiond(KERNEL_ENABLE_EVENT, NULL);
174 }
175
176 /*
177 * lttng_kernel_disable_event
178 *
179 * Disable an event in the kernel tracer.
180 */
181 int lttng_kernel_disable_event(char *event_name)
182 {
183 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
184 return ask_sessiond(KERNEL_DISABLE_EVENT, NULL);
185 }
186
187 /*
188 * lttng_kernel_create_session
189 *
190 * Create a session in the kernel tracer.
191 */
192 int lttng_kernel_create_session(void)
193 {
194 return ask_sessiond(KERNEL_CREATE_SESSION, NULL);
195 }
196
197 /*
198 * lttng_kernel_create_channel
199 *
200 * Create a channel in the kernel tracer.
201 */
202 int lttng_kernel_create_channel(int overwrite,
203 u64 subbuf_size, u64 num_subbuf,
204 unsigned int switch_timer_interval,
205 unsigned int read_timer_interval)
206 {
207 /* Write setting to the session message */
208 lsm.u.create_channel.overwrite = overwrite;
209 lsm.u.create_channel.subbuf_size = subbuf_size;
210 lsm.u.create_channel.num_subbuf = num_subbuf;
211 lsm.u.create_channel.switch_timer_interval = switch_timer_interval;
212 lsm.u.create_channel.read_timer_interval = read_timer_interval;
213
214 return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
215 }
216
217 /*
218 * lttng_kernel_start_tracing
219 *
220 * Start kernel tracing.
221 */
222 int lttng_kernel_start_tracing(void)
223 {
224 return ask_sessiond(KERNEL_START_TRACE, NULL);
225 }
226
227 /*
228 * lttng_kernel_stop_tracing
229 *
230 * Stop kernel tracing.
231 */
232 int lttng_kernel_stop_tracing(void)
233 {
234 return ask_sessiond(KERNEL_STOP_TRACE, NULL);
235 }
236
237 /*
238 * END KERNEL CONTROL
239 */
240
241 /*
242 * lttng_get_readable_code
243 *
244 * Return a human readable string of code
245 */
246 const char *lttng_get_readable_code(int code)
247 {
248 if (code > -LTTCOMM_OK) {
249 return "Ended with errors";
250 }
251
252 return lttcomm_get_readable_code(code);
253 }
254
255 /*
256 * lttng_ust_start_trace
257 *
258 * Request a trace start for pid.
259 */
260 int lttng_ust_start_trace(pid_t pid)
261 {
262 int ret;
263
264 lsm.pid = pid;
265 ret = ask_sessiond(UST_START_TRACE, NULL);
266
267 return ret;
268 }
269
270 /*
271 * lttng_ust_stop_trace
272 *
273 * Request a trace stop for pid.
274 */
275 int lttng_ust_stop_trace(pid_t pid)
276 {
277 int ret;
278
279 lsm.pid = pid;
280 ret = ask_sessiond(UST_STOP_TRACE, NULL);
281
282 return ret;
283 }
284
285 /*
286 * lttng_ust_create_trace
287 *
288 * Request a trace creation for pid.
289 */
290 int lttng_ust_create_trace(pid_t pid)
291 {
292 int ret;
293
294 lsm.pid = pid;
295 ret = ask_sessiond(UST_CREATE_TRACE, NULL);
296
297 return ret;
298 }
299
300 /*
301 * lttng_ust_list_apps
302 *
303 * Ask the session daemon for all UST traceable
304 * applications.
305 *
306 * Return the number of pids.
307 * On error, return negative value.
308 */
309 int lttng_ust_list_apps(pid_t **pids)
310 {
311 int ret;
312
313 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
314 if (ret < 0) {
315 return ret;
316 }
317
318 return ret / sizeof(pid_t);
319 }
320
321 /*
322 * lttng_list_traces
323 *
324 * Ask the session daemon for all traces (kernel and ust)
325 * for the session identified by uuid.
326 *
327 * Return the number of traces.
328 */
329 int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
330 {
331 int ret;
332
333 uuid_copy(lsm.session_uuid, *uuid);
334
335 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
336 if (ret < 0) {
337 return ret;
338 }
339
340 return ret / sizeof(struct lttng_trace);
341 }
342
343 /*
344 * lttng_create_session
345 *
346 * Create a brand new session using name. Allocate
347 * the session_id param pointing to the UUID.
348 */
349 int lttng_create_session(char *name, uuid_t *session_id)
350 {
351 int ret;
352
353 strncpy(lsm.session_name, name, sizeof(lsm.session_name));
354 lsm.session_name[sizeof(lsm.session_name) - 1] = '\0';
355
356 ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL);
357 if (ret < 0) {
358 goto end;
359 }
360
361 uuid_copy(*session_id, llm.session_uuid);
362
363 end:
364 return ret;
365 }
366
367 /*
368 * lttng_destroy_session
369 *
370 * Destroy session using name.
371 */
372 int lttng_destroy_session(uuid_t *uuid)
373 {
374 int ret;
375
376 uuid_copy(lsm.session_uuid, *uuid);
377
378 ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
379 if (ret < 0) {
380 goto end;
381 }
382
383 end:
384 return ret;
385 }
386
387 /*
388 * lttng_list_sessions
389 *
390 * Ask the session daemon for all available sessions.
391 *
392 * Return number of session.
393 * On error, return negative value.
394 */
395 int lttng_list_sessions(struct lttng_session **sessions)
396 {
397 int ret;
398
399 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
400 if (ret < 0) {
401 return ret;
402 }
403
404 return ret / sizeof(struct lttng_session);
405 }
406
407 /*
408 * lttng_connect_sessiond
409 *
410 * Connect to the LTTng session daemon.
411 * On success, return 0
412 * On error, return a negative value
413 */
414 int lttng_connect_sessiond(void)
415 {
416 int ret;
417
418 ret = set_session_daemon_path();
419 if (ret < 0) {
420 return ret;
421 }
422
423 /* Connect to the sesssion daemon */
424 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
425 if (ret < 0) {
426 return ret;
427 }
428
429 sessiond_socket = ret;
430 connected = 1;
431
432 return 0;
433 }
434
435 /*
436 * lttng_disconnect_sessiond
437 *
438 * Clean disconnect the session daemon.
439 */
440 int lttng_disconnect_sessiond(void)
441 {
442 int ret = 0;
443
444 if (connected) {
445 ret = lttcomm_close_unix_sock(sessiond_socket);
446 sessiond_socket = 0;
447 connected = 0;
448 }
449
450 return ret;
451 }
452
453 /*
454 * lttng_set_current_session_uuid
455 *
456 * Set the session uuid for current lsm.
457 */
458 void lttng_set_current_session_uuid(uuid_t *uuid)
459 {
460 uuid_copy(lsm.session_uuid, *uuid);
461 }
462
463 /*
464 * lttng_set_tracing_group
465 *
466 * Set tracing group variable with name. This function
467 * allocate memory pointed by tracing_group.
468 */
469 int lttng_set_tracing_group(const char *name)
470 {
471 if (asprintf(&tracing_group, "%s", name) < 0) {
472 return -ENOMEM;
473 }
474
475 return 0;
476 }
477
478 /*
479 * lttng_check_session_daemon
480 *
481 * Return 0 if a sesssion daemon is available
482 * else return -1
483 */
484 int lttng_check_session_daemon(void)
485 {
486 int ret;
487
488 ret = set_session_daemon_path();
489 if (ret < 0) {
490 return ret;
491 }
492
493 /* If socket exist, we consider the daemon started */
494 ret = access(sessiond_sock_path, F_OK);
495 if (ret < 0) {
496 return ret;
497 }
498
499 return 0;
500 }
501
502 /*
503 * set_session_daemon_path
504 *
505 * Set sessiond socket path by putting it in
506 * the global sessiond_sock_path variable.
507 */
508 static int set_session_daemon_path(void)
509 {
510 int ret;
511
512 /* Are we in the tracing group ? */
513 ret = check_tracing_group(tracing_group);
514 if (ret < 0 && getuid() != 0) {
515 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
516 getenv("HOME")) < 0) {
517 return -ENOMEM;
518 }
519 } else {
520 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
521 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
522 }
523
524 return 0;
525 }
526
527 /*
528 * check_tracing_group
529 *
530 * Check if the specified group name exist.
531 * If yes, 0, else -1
532 */
533 static int check_tracing_group(const char *grp_name)
534 {
535 struct group *grp_tracing; /* no free(). See getgrnam(3) */
536 gid_t *grp_list;
537 int grp_list_size, grp_id, i;
538 int ret = -1;
539
540 /* Get GID of group 'tracing' */
541 grp_tracing = getgrnam(grp_name);
542 if (grp_tracing == NULL) {
543 /* NULL means not found also. getgrnam(3) */
544 if (errno != 0) {
545 perror("getgrnam");
546 }
547 goto end;
548 }
549
550 /* Get number of supplementary group IDs */
551 grp_list_size = getgroups(0, NULL);
552 if (grp_list_size < 0) {
553 perror("getgroups");
554 goto end;
555 }
556
557 /* Alloc group list of the right size */
558 grp_list = malloc(grp_list_size * sizeof(gid_t));
559 grp_id = getgroups(grp_list_size, grp_list);
560 if (grp_id < -1) {
561 perror("getgroups");
562 goto free_list;
563 }
564
565 for (i = 0; i < grp_list_size; i++) {
566 if (grp_list[i] == grp_tracing->gr_gid) {
567 ret = 0;
568 break;
569 }
570 }
571
572 free_list:
573 free(grp_list);
574
575 end:
576 return ret;
577 }
578
579 /*
580 * lib constructor
581 */
582 static void __attribute__((constructor)) init()
583 {
584 /* Set default session group */
585 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
586 }
This page took 0.040824 seconds and 5 git commands to generate.