Remove useless zero initialization
[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 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 */
268int lttng_kernel_create_channel(void)
269{
270 return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
271}
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}
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}
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
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
331/*
332 * lttng_ust_start_trace
333 *
334 * Request a trace start for pid.
335 */
336int lttng_ust_start_trace(pid_t pid)
337{
338 lsm.pid = pid;
339 return ask_sessiond(UST_START_TRACE, NULL);
340}
341
342/*
343 * lttng_ust_stop_trace
344 *
345 * Request a trace stop for pid.
346 */
347int lttng_ust_stop_trace(pid_t pid)
348{
349 lsm.pid = pid;
350 return ask_sessiond(UST_STOP_TRACE, NULL);
351}
352
353/*
354 * lttng_ust_create_trace
355 *
356 * Request a trace creation for pid.
357 */
358int lttng_ust_create_trace(pid_t pid)
359{
360 lsm.pid = pid;
361 return ask_sessiond(UST_CREATE_TRACE, NULL);
362}
363
364/*
365 * lttng_ust_list_apps
366 *
367 * Ask the session daemon for all UST traceable applications.
368 *
369 * Return the number of pids.
370 * On error, return negative value.
371 */
372int lttng_ust_list_apps(pid_t **pids)
373{
374 int ret;
375
376 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
377 if (ret < 0) {
378 return ret;
379 }
380
381 return ret / sizeof(pid_t);
382}
383
384/*
385 * lttng_list_traces
386 *
387 * Ask the session daemon for all traces (kernel and ust) for the session
388 * identified by uuid.
389 *
390 * Return the number of traces.
391 * On error, return negative value.
392 */
393int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
394{
395 int ret;
396
397 uuid_copy(lsm.session_uuid, *uuid);
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
407/*
408 * lttng_create_session
409 *
410 * Create a brand new session using name.
411 */
412int lttng_create_session(char *name)
413{
414 strncpy(lsm.session_name, name, NAME_MAX);
415 return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
416}
417
418/*
419 * lttng_destroy_session
420 *
421 * Destroy session using name.
422 */
423int lttng_destroy_session(uuid_t *uuid)
424{
425 uuid_copy(lsm.session_uuid, *uuid);
426 return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
427}
428
429/*
430 * lttng_list_sessions
431 *
432 * Ask the session daemon for all available sessions.
433 *
434 * Return number of session.
435 * On error, return negative value.
436 */
437int lttng_list_sessions(struct lttng_session **sessions)
438{
439 int ret;
440
441 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
442 if (ret < 0) {
443 return ret;
444 }
445
446 return ret / sizeof(struct lttng_session);
447}
448
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
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
495/*
496 * lttng_set_current_session_uuid
497 *
498 * Set the session uuid for current lsm.
499 */
500void lttng_set_current_session_uuid(uuid_t *uuid)
501{
502 uuid_copy(lsm.session_uuid, *uuid);
503}
504
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 *
523 * Yes, return 1
524 * No, return 0
525 * Error, return negative value
526 */
527int lttng_session_daemon_alive(void)
528{
529 int ret;
530
531 ret = set_session_daemon_path();
532 if (ret < 0) {
533 /* Error */
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) {
540 /* Not alive */
541 return 0;
542 }
543
544 /* Is alive */
545 return 1;
546}
547
548/*
549 * lib constructor
550 */
551static void __attribute__((constructor)) init()
552{
553 /* Set default session group */
554 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
555}
This page took 0.023712 seconds and 4 git commands to generate.