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