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