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