Fix message argument type
[lttng-tools.git] / liblttngctl / liblttngctl.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
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.
fac6795d
DG
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
5b97ec60 27#include <lttng/lttng.h>
fac6795d
DG
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 */
fac6795d 37static struct lttcomm_session_msg lsm;
5461b305 38static struct lttcomm_lttng_msg llm;
fac6795d 39
fac6795d
DG
40/* Variables */
41static char *tracing_group;
42static int connected;
43
44/*
ca95a216 45 * send_data_sessiond
fac6795d 46 *
e065084a 47 * Send lttcomm_session_msg to the session daemon.
fac6795d
DG
48 *
49 * On success, return 0
50 * On error, return error code
51 */
ca95a216 52static int send_data_sessiond(void)
fac6795d
DG
53{
54 int ret;
55
56 if (!connected) {
e065084a
DG
57 ret = -ENOTCONN;
58 goto end;
fac6795d
DG
59 }
60
61 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
e065084a
DG
62
63end:
64 return ret;
65}
66
67/*
ca95a216 68 * recv_data_sessiond
e065084a
DG
69 *
70 * Receive data from the sessiond socket.
71 *
72 * On success, return 0
73 * On error, return recv() error code
74 */
ca95a216 75static int recv_data_sessiond(void *buf, size_t len)
e065084a
DG
76{
77 int ret;
78
79 if (!connected) {
80 ret = -ENOTCONN;
81 goto end;
fac6795d
DG
82 }
83
ca95a216 84 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
fac6795d 85
e065084a 86end:
fac6795d
DG
87 return ret;
88}
89
90/*
ca95a216 91 * ask_sessiond
fac6795d 92 *
947308c4 93 * Ask the session daemon a specific command and put the data into buf.
ca95a216
DG
94 *
95 * Return size of data (only payload, not header).
fac6795d 96 */
6abb15de 97static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
fac6795d 98{
ca95a216
DG
99 int ret;
100 size_t size;
101 void *data = NULL;
ca95a216 102
7442b2ba
DG
103 ret = lttng_connect_sessiond();
104 if (ret < 0) {
105 goto end;
106 }
107
ca95a216
DG
108 lsm.cmd_type = lct;
109
110 /* Send command to session daemon */
111 ret = send_data_sessiond();
112 if (ret < 0) {
113 goto end;
fac6795d
DG
114 }
115
ca95a216 116 /* Get header from data transmission */
5461b305 117 ret = recv_data_sessiond(&llm, sizeof(llm));
ca95a216
DG
118 if (ret < 0) {
119 goto end;
120 }
fac6795d 121
ca95a216 122 /* Check error code if OK */
5461b305
DG
123 if (llm.ret_code != LTTCOMM_OK) {
124 ret = -llm.ret_code;
ca95a216
DG
125 goto end;
126 }
fac6795d 127
5461b305 128 size = llm.trace_name_offset + llm.data_size;
ca95a216
DG
129 if (size == 0) {
130 goto end;
fac6795d
DG
131 }
132
ca95a216
DG
133 data = (void*) malloc(size);
134
135 /* Get payload data */
136 ret = recv_data_sessiond(data, size);
fac6795d 137 if (ret < 0) {
947308c4 138 free(data);
fac6795d
DG
139 goto end;
140 }
141
ca95a216
DG
142 *buf = data;
143 ret = size;
fac6795d
DG
144
145end:
7442b2ba 146 lttng_disconnect_sessiond();
fac6795d
DG
147 return ret;
148}
149
947308c4
DG
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
1df4dedd
DG
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{
33a2b854
DG
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;
1df4dedd
DG
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 */
20fe2104 276int lttng_kernel_create_channel(void)
1df4dedd 277{
1df4dedd
DG
278 return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
279}
a5c5a2bd
DG
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}
8c0faa1d
DG
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}
1df4dedd 300
2ef84c95
DG
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
1df4dedd
DG
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
ca95a216
DG
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
ce3d728c
DG
352/*
353 * lttng_ust_start_trace
354 *
355 * Request a trace start for pid.
356 */
357int lttng_ust_start_trace(pid_t pid)
358{
ce3d728c 359 lsm.pid = pid;
947308c4 360 return ask_sessiond(UST_START_TRACE, NULL);
ce3d728c
DG
361}
362
520ff687
DG
363/*
364 * lttng_ust_stop_trace
365 *
366 * Request a trace stop for pid.
367 */
368int lttng_ust_stop_trace(pid_t pid)
369{
520ff687 370 lsm.pid = pid;
947308c4 371 return ask_sessiond(UST_STOP_TRACE, NULL);
520ff687
DG
372}
373
df0da139
DG
374/*
375 * lttng_ust_create_trace
376 *
377 * Request a trace creation for pid.
378 */
379int lttng_ust_create_trace(pid_t pid)
380{
df0da139 381 lsm.pid = pid;
947308c4 382 return ask_sessiond(UST_CREATE_TRACE, NULL);
df0da139
DG
383}
384
fac6795d
DG
385/*
386 * lttng_ust_list_apps
387 *
947308c4 388 * Ask the session daemon for all UST traceable applications.
fac6795d 389 *
ca95a216
DG
390 * Return the number of pids.
391 * On error, return negative value.
fac6795d 392 */
ca95a216 393int lttng_ust_list_apps(pid_t **pids)
fac6795d 394{
ca95a216 395 int ret;
fac6795d 396
ca95a216 397 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
fac6795d 398 if (ret < 0) {
ca95a216 399 return ret;
fac6795d
DG
400 }
401
ca95a216 402 return ret / sizeof(pid_t);
fac6795d
DG
403}
404
1657e9bb
DG
405/*
406 * lttng_list_traces
407 *
947308c4
DG
408 * Ask the session daemon for all traces (kernel and ust) for the session
409 * identified by uuid.
1657e9bb
DG
410 *
411 * Return the number of traces.
947308c4 412 * On error, return negative value.
1657e9bb
DG
413 */
414int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
415{
416 int ret;
417
5461b305 418 uuid_copy(lsm.session_uuid, *uuid);
1657e9bb
DG
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
aaf97519
DG
428/*
429 * lttng_create_session
430 *
894be886 431 * Create a brand new session using name.
aaf97519 432 */
894be886 433int lttng_create_session(char *name)
aaf97519 434{
947308c4
DG
435 strncpy(lsm.session_name, name, NAME_MAX);
436 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
437}
438
439/*
440 * lttng_destroy_session
441 *
442 * Destroy session using name.
443 */
444int lttng_destroy_session(uuid_t *uuid)
445{
5461b305 446 uuid_copy(lsm.session_uuid, *uuid);
947308c4 447 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
448}
449
57167058
DG
450/*
451 * lttng_list_sessions
452 *
453 * Ask the session daemon for all available sessions.
454 *
ca95a216
DG
455 * Return number of session.
456 * On error, return negative value.
57167058 457 */
ca95a216 458int lttng_list_sessions(struct lttng_session **sessions)
57167058 459{
ca95a216 460 int ret;
57167058 461
ca95a216 462 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 463 if (ret < 0) {
ca95a216 464 return ret;
57167058
DG
465 }
466
ca95a216 467 return ret / sizeof(struct lttng_session);
57167058
DG
468}
469
fac6795d
DG
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
87378cf5
DG
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
e8be5f4f
DG
516/*
517 * lttng_set_current_session_uuid
518 *
519 * Set the session uuid for current lsm.
520 */
96243366 521void lttng_set_current_session_uuid(uuid_t *uuid)
e8be5f4f 522{
5461b305 523 uuid_copy(lsm.session_uuid, *uuid);
e8be5f4f
DG
524}
525
fac6795d
DG
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 *
947308c4
DG
544 * Yes, return 1
545 * No, return 0
546 * Error, return negative value
fac6795d 547 */
947308c4 548int lttng_session_daemon_alive(void)
fac6795d
DG
549{
550 int ret;
551
552 ret = set_session_daemon_path();
553 if (ret < 0) {
947308c4 554 /* Error */
fac6795d
DG
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) {
947308c4
DG
561 /* Not alive */
562 return 0;
fac6795d
DG
563 }
564
947308c4
DG
565 /* Is alive */
566 return 1;
fac6795d
DG
567}
568
569/*
570 * lib constructor
571 */
572static void __attribute__((constructor)) init()
573{
574 /* Set default session group */
64a23ac4 575 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 576}
This page took 0.07348 seconds and 4 git commands to generate.