Remove useless zero initialization
[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{
238 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
239 return ask_sessiond(KERNEL_ENABLE_EVENT, NULL);
240}
241
242/*
243 * lttng_kernel_disable_event
244 *
245 * Disable an event in the kernel tracer.
246 */
247int lttng_kernel_disable_event(char *event_name)
248{
249 strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
250 return ask_sessiond(KERNEL_DISABLE_EVENT, NULL);
251}
252
253/*
254 * lttng_kernel_create_session
255 *
256 * Create a session in the kernel tracer.
257 */
258int lttng_kernel_create_session(void)
259{
260 return ask_sessiond(KERNEL_CREATE_SESSION, NULL);
261}
262
263/*
264 * lttng_kernel_create_channel
265 *
266 * Create a channel in the kernel tracer.
267 */
20fe2104 268int lttng_kernel_create_channel(void)
1df4dedd 269{
1df4dedd
DG
270 return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
271}
a5c5a2bd
DG
272
273/*
274 * lttng_kernel_open_metadata
275 *
276 * Open metadata in the kernel tracer.
277 */
278int lttng_kernel_open_metadata(void)
279{
280 return ask_sessiond(KERNEL_OPEN_METADATA, NULL);
281}
8c0faa1d
DG
282
283/*
284 * lttng_kernel_create_stream
285 *
286 * Create stream in the kernel tracer.
287 */
288int lttng_kernel_create_stream(void)
289{
290 return ask_sessiond(KERNEL_CREATE_STREAM, NULL);
291}
1df4dedd
DG
292
293/*
294 * lttng_kernel_start_tracing
295 *
296 * Start kernel tracing.
297 */
298int lttng_kernel_start_tracing(void)
299{
300 return ask_sessiond(KERNEL_START_TRACE, NULL);
301}
302
303/*
304 * lttng_kernel_stop_tracing
305 *
306 * Stop kernel tracing.
307 */
308int lttng_kernel_stop_tracing(void)
309{
310 return ask_sessiond(KERNEL_STOP_TRACE, NULL);
311}
312
313/*
314 * END KERNEL CONTROL
315 */
316
ca95a216
DG
317/*
318 * lttng_get_readable_code
319 *
320 * Return a human readable string of code
321 */
322const char *lttng_get_readable_code(int code)
323{
324 if (code > -LTTCOMM_OK) {
325 return "Ended with errors";
326 }
327
328 return lttcomm_get_readable_code(code);
329}
330
ce3d728c
DG
331/*
332 * lttng_ust_start_trace
333 *
334 * Request a trace start for pid.
335 */
336int lttng_ust_start_trace(pid_t pid)
337{
ce3d728c 338 lsm.pid = pid;
947308c4 339 return ask_sessiond(UST_START_TRACE, NULL);
ce3d728c
DG
340}
341
520ff687
DG
342/*
343 * lttng_ust_stop_trace
344 *
345 * Request a trace stop for pid.
346 */
347int lttng_ust_stop_trace(pid_t pid)
348{
520ff687 349 lsm.pid = pid;
947308c4 350 return ask_sessiond(UST_STOP_TRACE, NULL);
520ff687
DG
351}
352
df0da139
DG
353/*
354 * lttng_ust_create_trace
355 *
356 * Request a trace creation for pid.
357 */
358int lttng_ust_create_trace(pid_t pid)
359{
df0da139 360 lsm.pid = pid;
947308c4 361 return ask_sessiond(UST_CREATE_TRACE, NULL);
df0da139
DG
362}
363
fac6795d
DG
364/*
365 * lttng_ust_list_apps
366 *
947308c4 367 * Ask the session daemon for all UST traceable applications.
fac6795d 368 *
ca95a216
DG
369 * Return the number of pids.
370 * On error, return negative value.
fac6795d 371 */
ca95a216 372int lttng_ust_list_apps(pid_t **pids)
fac6795d 373{
ca95a216 374 int ret;
fac6795d 375
ca95a216 376 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
fac6795d 377 if (ret < 0) {
ca95a216 378 return ret;
fac6795d
DG
379 }
380
ca95a216 381 return ret / sizeof(pid_t);
fac6795d
DG
382}
383
1657e9bb
DG
384/*
385 * lttng_list_traces
386 *
947308c4
DG
387 * Ask the session daemon for all traces (kernel and ust) for the session
388 * identified by uuid.
1657e9bb
DG
389 *
390 * Return the number of traces.
947308c4 391 * On error, return negative value.
1657e9bb
DG
392 */
393int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
394{
395 int ret;
396
5461b305 397 uuid_copy(lsm.session_uuid, *uuid);
1657e9bb
DG
398
399 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
400 if (ret < 0) {
401 return ret;
402 }
403
404 return ret / sizeof(struct lttng_trace);
405}
406
aaf97519
DG
407/*
408 * lttng_create_session
409 *
894be886 410 * Create a brand new session using name.
aaf97519 411 */
894be886 412int lttng_create_session(char *name)
aaf97519 413{
947308c4
DG
414 strncpy(lsm.session_name, name, NAME_MAX);
415 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
8028d920
DG
416}
417
418/*
419 * lttng_destroy_session
420 *
421 * Destroy session using name.
422 */
423int lttng_destroy_session(uuid_t *uuid)
424{
5461b305 425 uuid_copy(lsm.session_uuid, *uuid);
947308c4 426 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
aaf97519
DG
427}
428
57167058
DG
429/*
430 * lttng_list_sessions
431 *
432 * Ask the session daemon for all available sessions.
433 *
ca95a216
DG
434 * Return number of session.
435 * On error, return negative value.
57167058 436 */
ca95a216 437int lttng_list_sessions(struct lttng_session **sessions)
57167058 438{
ca95a216 439 int ret;
57167058 440
ca95a216 441 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 442 if (ret < 0) {
ca95a216 443 return ret;
57167058
DG
444 }
445
ca95a216 446 return ret / sizeof(struct lttng_session);
57167058
DG
447}
448
fac6795d
DG
449/*
450 * lttng_connect_sessiond
451 *
452 * Connect to the LTTng session daemon.
453 * On success, return 0
454 * On error, return a negative value
455 */
456int lttng_connect_sessiond(void)
457{
458 int ret;
459
460 ret = set_session_daemon_path();
461 if (ret < 0) {
462 return ret;
463 }
464
465 /* Connect to the sesssion daemon */
466 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
467 if (ret < 0) {
468 return ret;
469 }
470
471 sessiond_socket = ret;
472 connected = 1;
473
474 return 0;
475}
476
87378cf5
DG
477/*
478 * lttng_disconnect_sessiond
479 *
480 * Clean disconnect the session daemon.
481 */
482int lttng_disconnect_sessiond(void)
483{
484 int ret = 0;
485
486 if (connected) {
487 ret = lttcomm_close_unix_sock(sessiond_socket);
488 sessiond_socket = 0;
489 connected = 0;
490 }
491
492 return ret;
493}
494
e8be5f4f
DG
495/*
496 * lttng_set_current_session_uuid
497 *
498 * Set the session uuid for current lsm.
499 */
96243366 500void lttng_set_current_session_uuid(uuid_t *uuid)
e8be5f4f 501{
5461b305 502 uuid_copy(lsm.session_uuid, *uuid);
e8be5f4f
DG
503}
504
fac6795d
DG
505/*
506 * lttng_set_tracing_group
507 *
508 * Set tracing group variable with name. This function
509 * allocate memory pointed by tracing_group.
510 */
511int lttng_set_tracing_group(const char *name)
512{
513 if (asprintf(&tracing_group, "%s", name) < 0) {
514 return -ENOMEM;
515 }
516
517 return 0;
518}
519
520/*
521 * lttng_check_session_daemon
522 *
947308c4
DG
523 * Yes, return 1
524 * No, return 0
525 * Error, return negative value
fac6795d 526 */
947308c4 527int lttng_session_daemon_alive(void)
fac6795d
DG
528{
529 int ret;
530
531 ret = set_session_daemon_path();
532 if (ret < 0) {
947308c4 533 /* Error */
fac6795d
DG
534 return ret;
535 }
536
537 /* If socket exist, we consider the daemon started */
538 ret = access(sessiond_sock_path, F_OK);
539 if (ret < 0) {
947308c4
DG
540 /* Not alive */
541 return 0;
fac6795d
DG
542 }
543
947308c4
DG
544 /* Is alive */
545 return 1;
fac6795d
DG
546}
547
548/*
549 * lib constructor
550 */
551static void __attribute__((constructor)) init()
552{
553 /* Set default session group */
64a23ac4 554 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
fac6795d 555}
This page took 0.072409 seconds and 4 git commands to generate.