Commit | Line | Data |
---|---|---|
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 */ | |
33 | static int sessiond_socket; | |
34 | static char sessiond_sock_path[PATH_MAX]; | |
35 | ||
36 | /* Communication structure to ltt-sessiond */ | |
fac6795d | 37 | static struct lttcomm_session_msg lsm; |
5461b305 | 38 | static struct lttcomm_lttng_msg llm; |
fac6795d DG |
39 | |
40 | /* Prototypes */ | |
41 | static int check_tracing_group(const char *grp_name); | |
6abb15de | 42 | static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf); |
ca95a216 DG |
43 | static int recv_data_sessiond(void *buf, size_t len); |
44 | static int send_data_sessiond(void); | |
fac6795d | 45 | static int set_session_daemon_path(void); |
fac6795d DG |
46 | |
47 | /* Variables */ | |
48 | static char *tracing_group; | |
49 | static 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 | 59 | static 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 | |
70 | end: | |
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 | 82 | static 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 | 96 | end: |
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 | 108 | static 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 | |
155 | end: | |
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 | */ | |
169 | int 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 | */ | |
180 | int 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 | */ | |
191 | int 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 | 201 | int lttng_kernel_create_channel(void) |
1df4dedd | 202 | { |
1df4dedd DG |
203 | return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL); |
204 | } | |
205 | ||
206 | /* | |
207 | * lttng_kernel_start_tracing | |
208 | * | |
209 | * Start kernel tracing. | |
210 | */ | |
211 | int lttng_kernel_start_tracing(void) | |
212 | { | |
213 | return ask_sessiond(KERNEL_START_TRACE, NULL); | |
214 | } | |
215 | ||
216 | /* | |
217 | * lttng_kernel_stop_tracing | |
218 | * | |
219 | * Stop kernel tracing. | |
220 | */ | |
221 | int lttng_kernel_stop_tracing(void) | |
222 | { | |
223 | return ask_sessiond(KERNEL_STOP_TRACE, NULL); | |
224 | } | |
225 | ||
226 | /* | |
227 | * END KERNEL CONTROL | |
228 | */ | |
229 | ||
ca95a216 DG |
230 | /* |
231 | * lttng_get_readable_code | |
232 | * | |
233 | * Return a human readable string of code | |
234 | */ | |
235 | const char *lttng_get_readable_code(int code) | |
236 | { | |
237 | if (code > -LTTCOMM_OK) { | |
238 | return "Ended with errors"; | |
239 | } | |
240 | ||
241 | return lttcomm_get_readable_code(code); | |
242 | } | |
243 | ||
ce3d728c DG |
244 | /* |
245 | * lttng_ust_start_trace | |
246 | * | |
247 | * Request a trace start for pid. | |
248 | */ | |
249 | int lttng_ust_start_trace(pid_t pid) | |
250 | { | |
251 | int ret; | |
252 | ||
253 | lsm.pid = pid; | |
254 | ret = ask_sessiond(UST_START_TRACE, NULL); | |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
520ff687 DG |
259 | /* |
260 | * lttng_ust_stop_trace | |
261 | * | |
262 | * Request a trace stop for pid. | |
263 | */ | |
264 | int lttng_ust_stop_trace(pid_t pid) | |
265 | { | |
266 | int ret; | |
267 | ||
268 | lsm.pid = pid; | |
269 | ret = ask_sessiond(UST_STOP_TRACE, NULL); | |
270 | ||
271 | return ret; | |
272 | } | |
273 | ||
df0da139 DG |
274 | /* |
275 | * lttng_ust_create_trace | |
276 | * | |
277 | * Request a trace creation for pid. | |
278 | */ | |
279 | int lttng_ust_create_trace(pid_t pid) | |
280 | { | |
281 | int ret; | |
282 | ||
283 | lsm.pid = pid; | |
284 | ret = ask_sessiond(UST_CREATE_TRACE, NULL); | |
285 | ||
286 | return ret; | |
287 | } | |
288 | ||
fac6795d DG |
289 | /* |
290 | * lttng_ust_list_apps | |
291 | * | |
292 | * Ask the session daemon for all UST traceable | |
293 | * applications. | |
294 | * | |
ca95a216 DG |
295 | * Return the number of pids. |
296 | * On error, return negative value. | |
fac6795d | 297 | */ |
ca95a216 | 298 | int lttng_ust_list_apps(pid_t **pids) |
fac6795d | 299 | { |
ca95a216 | 300 | int ret; |
fac6795d | 301 | |
ca95a216 | 302 | ret = ask_sessiond(UST_LIST_APPS, (void**) pids); |
fac6795d | 303 | if (ret < 0) { |
ca95a216 | 304 | return ret; |
fac6795d DG |
305 | } |
306 | ||
ca95a216 | 307 | return ret / sizeof(pid_t); |
fac6795d DG |
308 | } |
309 | ||
1657e9bb DG |
310 | /* |
311 | * lttng_list_traces | |
312 | * | |
313 | * Ask the session daemon for all traces (kernel and ust) | |
314 | * for the session identified by uuid. | |
315 | * | |
316 | * Return the number of traces. | |
317 | */ | |
318 | int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces) | |
319 | { | |
320 | int ret; | |
321 | ||
5461b305 | 322 | uuid_copy(lsm.session_uuid, *uuid); |
1657e9bb DG |
323 | |
324 | ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces); | |
325 | if (ret < 0) { | |
326 | return ret; | |
327 | } | |
328 | ||
329 | return ret / sizeof(struct lttng_trace); | |
330 | } | |
331 | ||
aaf97519 DG |
332 | /* |
333 | * lttng_create_session | |
334 | * | |
894be886 | 335 | * Create a brand new session using name. |
aaf97519 | 336 | */ |
894be886 | 337 | int lttng_create_session(char *name) |
aaf97519 DG |
338 | { |
339 | int ret; | |
aaf97519 DG |
340 | |
341 | strncpy(lsm.session_name, name, sizeof(lsm.session_name)); | |
8028d920 | 342 | lsm.session_name[sizeof(lsm.session_name) - 1] = '\0'; |
aaf97519 DG |
343 | |
344 | ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL); | |
345 | if (ret < 0) { | |
346 | goto end; | |
347 | } | |
348 | ||
8028d920 DG |
349 | end: |
350 | return ret; | |
351 | } | |
352 | ||
353 | /* | |
354 | * lttng_destroy_session | |
355 | * | |
356 | * Destroy session using name. | |
357 | */ | |
358 | int lttng_destroy_session(uuid_t *uuid) | |
359 | { | |
360 | int ret; | |
361 | ||
5461b305 | 362 | uuid_copy(lsm.session_uuid, *uuid); |
8028d920 DG |
363 | |
364 | ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL); | |
365 | if (ret < 0) { | |
366 | goto end; | |
367 | } | |
aaf97519 DG |
368 | |
369 | end: | |
370 | return ret; | |
371 | } | |
372 | ||
57167058 DG |
373 | /* |
374 | * lttng_list_sessions | |
375 | * | |
376 | * Ask the session daemon for all available sessions. | |
377 | * | |
ca95a216 DG |
378 | * Return number of session. |
379 | * On error, return negative value. | |
57167058 | 380 | */ |
ca95a216 | 381 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 382 | { |
ca95a216 | 383 | int ret; |
57167058 | 384 | |
ca95a216 | 385 | ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions); |
57167058 | 386 | if (ret < 0) { |
ca95a216 | 387 | return ret; |
57167058 DG |
388 | } |
389 | ||
ca95a216 | 390 | return ret / sizeof(struct lttng_session); |
57167058 DG |
391 | } |
392 | ||
fac6795d DG |
393 | /* |
394 | * lttng_connect_sessiond | |
395 | * | |
396 | * Connect to the LTTng session daemon. | |
397 | * On success, return 0 | |
398 | * On error, return a negative value | |
399 | */ | |
400 | int lttng_connect_sessiond(void) | |
401 | { | |
402 | int ret; | |
403 | ||
404 | ret = set_session_daemon_path(); | |
405 | if (ret < 0) { | |
406 | return ret; | |
407 | } | |
408 | ||
409 | /* Connect to the sesssion daemon */ | |
410 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
411 | if (ret < 0) { | |
412 | return ret; | |
413 | } | |
414 | ||
415 | sessiond_socket = ret; | |
416 | connected = 1; | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
87378cf5 DG |
421 | /* |
422 | * lttng_disconnect_sessiond | |
423 | * | |
424 | * Clean disconnect the session daemon. | |
425 | */ | |
426 | int lttng_disconnect_sessiond(void) | |
427 | { | |
428 | int ret = 0; | |
429 | ||
430 | if (connected) { | |
431 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
432 | sessiond_socket = 0; | |
433 | connected = 0; | |
434 | } | |
435 | ||
436 | return ret; | |
437 | } | |
438 | ||
e8be5f4f DG |
439 | /* |
440 | * lttng_set_current_session_uuid | |
441 | * | |
442 | * Set the session uuid for current lsm. | |
443 | */ | |
96243366 | 444 | void lttng_set_current_session_uuid(uuid_t *uuid) |
e8be5f4f | 445 | { |
5461b305 | 446 | uuid_copy(lsm.session_uuid, *uuid); |
e8be5f4f DG |
447 | } |
448 | ||
fac6795d DG |
449 | /* |
450 | * lttng_set_tracing_group | |
451 | * | |
452 | * Set tracing group variable with name. This function | |
453 | * allocate memory pointed by tracing_group. | |
454 | */ | |
455 | int lttng_set_tracing_group(const char *name) | |
456 | { | |
457 | if (asprintf(&tracing_group, "%s", name) < 0) { | |
458 | return -ENOMEM; | |
459 | } | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | /* | |
465 | * lttng_check_session_daemon | |
466 | * | |
467 | * Return 0 if a sesssion daemon is available | |
468 | * else return -1 | |
469 | */ | |
470 | int lttng_check_session_daemon(void) | |
471 | { | |
472 | int ret; | |
473 | ||
474 | ret = set_session_daemon_path(); | |
475 | if (ret < 0) { | |
476 | return ret; | |
477 | } | |
478 | ||
479 | /* If socket exist, we consider the daemon started */ | |
480 | ret = access(sessiond_sock_path, F_OK); | |
481 | if (ret < 0) { | |
482 | return ret; | |
483 | } | |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
fac6795d DG |
488 | /* |
489 | * set_session_daemon_path | |
490 | * | |
491 | * Set sessiond socket path by putting it in | |
492 | * the global sessiond_sock_path variable. | |
493 | */ | |
494 | static int set_session_daemon_path(void) | |
495 | { | |
496 | int ret; | |
497 | ||
498 | /* Are we in the tracing group ? */ | |
499 | ret = check_tracing_group(tracing_group); | |
847177cd | 500 | if (ret < 0 && getuid() != 0) { |
fac6795d DG |
501 | if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK, |
502 | getenv("HOME")) < 0) { | |
503 | return -ENOMEM; | |
504 | } | |
505 | } else { | |
506 | strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
507 | sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); | |
508 | } | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | /* | |
514 | * check_tracing_group | |
515 | * | |
516 | * Check if the specified group name exist. | |
517 | * If yes, 0, else -1 | |
518 | */ | |
519 | static int check_tracing_group(const char *grp_name) | |
520 | { | |
521 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
522 | gid_t *grp_list; | |
523 | int grp_list_size, grp_id, i; | |
524 | int ret = -1; | |
525 | ||
526 | /* Get GID of group 'tracing' */ | |
527 | grp_tracing = getgrnam(grp_name); | |
528 | if (grp_tracing == NULL) { | |
529 | /* NULL means not found also. getgrnam(3) */ | |
530 | if (errno != 0) { | |
531 | perror("getgrnam"); | |
532 | } | |
533 | goto end; | |
534 | } | |
535 | ||
536 | /* Get number of supplementary group IDs */ | |
537 | grp_list_size = getgroups(0, NULL); | |
538 | if (grp_list_size < 0) { | |
539 | perror("getgroups"); | |
540 | goto end; | |
541 | } | |
542 | ||
543 | /* Alloc group list of the right size */ | |
544 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
545 | grp_id = getgroups(grp_list_size, grp_list); | |
546 | if (grp_id < -1) { | |
547 | perror("getgroups"); | |
548 | goto free_list; | |
549 | } | |
550 | ||
551 | for (i = 0; i < grp_list_size; i++) { | |
552 | if (grp_list[i] == grp_tracing->gr_gid) { | |
553 | ret = 0; | |
554 | break; | |
555 | } | |
556 | } | |
557 | ||
558 | free_list: | |
559 | free(grp_list); | |
560 | ||
561 | end: | |
562 | return ret; | |
563 | } | |
564 | ||
565 | /* | |
566 | * lib constructor | |
567 | */ | |
568 | static void __attribute__((constructor)) init() | |
569 | { | |
570 | /* Set default session group */ | |
571 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); | |
572 | } |