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 | ||
27 | #include <lttng/liblttngctl.h> | |
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; |
aaf97519 | 38 | static struct lttcomm_lttng_msg llm; |
fac6795d DG |
39 | |
40 | /* Prototypes */ | |
41 | static int check_tracing_group(const char *grp_name); | |
ca95a216 DG |
42 | static int ask_sessiond(enum lttcomm_command_type lct, void **buf); |
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 | */ |
ca95a216 | 108 | static int ask_sessiond(enum lttcomm_command_type lct, void **buf) |
fac6795d | 109 | { |
ca95a216 DG |
110 | int ret; |
111 | size_t size; | |
112 | void *data = NULL; | |
ca95a216 DG |
113 | |
114 | lsm.cmd_type = lct; | |
115 | ||
116 | /* Send command to session daemon */ | |
117 | ret = send_data_sessiond(); | |
118 | if (ret < 0) { | |
119 | goto end; | |
fac6795d DG |
120 | } |
121 | ||
ca95a216 DG |
122 | /* Get header from data transmission */ |
123 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
124 | if (ret < 0) { | |
125 | goto end; | |
126 | } | |
fac6795d | 127 | |
ca95a216 DG |
128 | /* Check error code if OK */ |
129 | if (llm.ret_code != LTTCOMM_OK) { | |
130 | ret = -llm.ret_code; | |
131 | goto end; | |
132 | } | |
fac6795d | 133 | |
ca95a216 DG |
134 | size = llm.size_payload; |
135 | if (size == 0) { | |
136 | goto end; | |
fac6795d DG |
137 | } |
138 | ||
ca95a216 DG |
139 | data = (void*) malloc(size); |
140 | ||
141 | /* Get payload data */ | |
142 | ret = recv_data_sessiond(data, size); | |
fac6795d DG |
143 | if (ret < 0) { |
144 | goto end; | |
145 | } | |
146 | ||
ca95a216 DG |
147 | *buf = data; |
148 | ret = size; | |
fac6795d DG |
149 | |
150 | end: | |
ca95a216 DG |
151 | /* Reset lsm data struct */ |
152 | memset(&lsm, 0, sizeof(lsm)); | |
fac6795d DG |
153 | return ret; |
154 | } | |
155 | ||
ca95a216 DG |
156 | /* |
157 | * lttng_get_readable_code | |
158 | * | |
159 | * Return a human readable string of code | |
160 | */ | |
161 | const char *lttng_get_readable_code(int code) | |
162 | { | |
163 | if (code > -LTTCOMM_OK) { | |
164 | return "Ended with errors"; | |
165 | } | |
166 | ||
167 | return lttcomm_get_readable_code(code); | |
168 | } | |
169 | ||
ce3d728c DG |
170 | /* |
171 | * lttng_ust_start_trace | |
172 | * | |
173 | * Request a trace start for pid. | |
174 | */ | |
175 | int lttng_ust_start_trace(pid_t pid) | |
176 | { | |
177 | int ret; | |
178 | ||
179 | lsm.pid = pid; | |
180 | ret = ask_sessiond(UST_START_TRACE, NULL); | |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
df0da139 DG |
185 | /* |
186 | * lttng_ust_create_trace | |
187 | * | |
188 | * Request a trace creation for pid. | |
189 | */ | |
190 | int lttng_ust_create_trace(pid_t pid) | |
191 | { | |
192 | int ret; | |
193 | ||
194 | lsm.pid = pid; | |
195 | ret = ask_sessiond(UST_CREATE_TRACE, NULL); | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
fac6795d DG |
200 | /* |
201 | * lttng_ust_list_apps | |
202 | * | |
203 | * Ask the session daemon for all UST traceable | |
204 | * applications. | |
205 | * | |
ca95a216 DG |
206 | * Return the number of pids. |
207 | * On error, return negative value. | |
fac6795d | 208 | */ |
ca95a216 | 209 | int lttng_ust_list_apps(pid_t **pids) |
fac6795d | 210 | { |
ca95a216 | 211 | int ret; |
fac6795d | 212 | |
ca95a216 | 213 | ret = ask_sessiond(UST_LIST_APPS, (void**) pids); |
fac6795d | 214 | if (ret < 0) { |
ca95a216 | 215 | return ret; |
fac6795d DG |
216 | } |
217 | ||
ca95a216 | 218 | return ret / sizeof(pid_t); |
fac6795d DG |
219 | } |
220 | ||
1657e9bb DG |
221 | /* |
222 | * lttng_list_traces | |
223 | * | |
224 | * Ask the session daemon for all traces (kernel and ust) | |
225 | * for the session identified by uuid. | |
226 | * | |
227 | * Return the number of traces. | |
228 | */ | |
229 | int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces) | |
230 | { | |
231 | int ret; | |
232 | ||
233 | uuid_copy(lsm.session_id, *uuid); | |
234 | ||
235 | ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces); | |
236 | if (ret < 0) { | |
237 | return ret; | |
238 | } | |
239 | ||
240 | return ret / sizeof(struct lttng_trace); | |
241 | } | |
242 | ||
aaf97519 DG |
243 | /* |
244 | * lttng_create_session | |
245 | * | |
246 | * Create a brand new session using name. Allocate | |
247 | * the session_id param pointing to the UUID. | |
248 | */ | |
8028d920 | 249 | int lttng_create_session(char *name, uuid_t *session_id) |
aaf97519 DG |
250 | { |
251 | int ret; | |
aaf97519 DG |
252 | |
253 | strncpy(lsm.session_name, name, sizeof(lsm.session_name)); | |
8028d920 | 254 | lsm.session_name[sizeof(lsm.session_name) - 1] = '\0'; |
aaf97519 DG |
255 | |
256 | ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL); | |
257 | if (ret < 0) { | |
258 | goto end; | |
259 | } | |
260 | ||
8028d920 | 261 | uuid_copy(*session_id, llm.session_id); |
aaf97519 | 262 | |
8028d920 DG |
263 | end: |
264 | return ret; | |
265 | } | |
266 | ||
267 | /* | |
268 | * lttng_destroy_session | |
269 | * | |
270 | * Destroy session using name. | |
271 | */ | |
272 | int lttng_destroy_session(uuid_t *uuid) | |
273 | { | |
274 | int ret; | |
275 | ||
276 | uuid_copy(lsm.session_id, *uuid); | |
277 | ||
278 | ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL); | |
279 | if (ret < 0) { | |
280 | goto end; | |
281 | } | |
aaf97519 DG |
282 | |
283 | end: | |
284 | return ret; | |
285 | } | |
286 | ||
57167058 DG |
287 | /* |
288 | * lttng_list_sessions | |
289 | * | |
290 | * Ask the session daemon for all available sessions. | |
291 | * | |
ca95a216 DG |
292 | * Return number of session. |
293 | * On error, return negative value. | |
57167058 | 294 | */ |
ca95a216 | 295 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 296 | { |
ca95a216 | 297 | int ret; |
57167058 | 298 | |
ca95a216 | 299 | ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions); |
57167058 | 300 | if (ret < 0) { |
ca95a216 | 301 | return ret; |
57167058 DG |
302 | } |
303 | ||
ca95a216 | 304 | return ret / sizeof(struct lttng_session); |
57167058 DG |
305 | } |
306 | ||
fac6795d DG |
307 | /* |
308 | * lttng_connect_sessiond | |
309 | * | |
310 | * Connect to the LTTng session daemon. | |
311 | * On success, return 0 | |
312 | * On error, return a negative value | |
313 | */ | |
314 | int lttng_connect_sessiond(void) | |
315 | { | |
316 | int ret; | |
317 | ||
318 | ret = set_session_daemon_path(); | |
319 | if (ret < 0) { | |
320 | return ret; | |
321 | } | |
322 | ||
323 | /* Connect to the sesssion daemon */ | |
324 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
325 | if (ret < 0) { | |
326 | return ret; | |
327 | } | |
328 | ||
329 | sessiond_socket = ret; | |
330 | connected = 1; | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
87378cf5 DG |
335 | /* |
336 | * lttng_disconnect_sessiond | |
337 | * | |
338 | * Clean disconnect the session daemon. | |
339 | */ | |
340 | int lttng_disconnect_sessiond(void) | |
341 | { | |
342 | int ret = 0; | |
343 | ||
344 | if (connected) { | |
345 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
346 | sessiond_socket = 0; | |
347 | connected = 0; | |
348 | } | |
349 | ||
350 | return ret; | |
351 | } | |
352 | ||
e8be5f4f DG |
353 | /* |
354 | * lttng_set_current_session_uuid | |
355 | * | |
356 | * Set the session uuid for current lsm. | |
357 | */ | |
358 | void lttng_set_current_session_uuid(char *uuid) | |
359 | { | |
360 | uuid_parse(uuid, lsm.session_id); | |
361 | } | |
362 | ||
fac6795d DG |
363 | /* |
364 | * lttng_set_tracing_group | |
365 | * | |
366 | * Set tracing group variable with name. This function | |
367 | * allocate memory pointed by tracing_group. | |
368 | */ | |
369 | int lttng_set_tracing_group(const char *name) | |
370 | { | |
371 | if (asprintf(&tracing_group, "%s", name) < 0) { | |
372 | return -ENOMEM; | |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | /* | |
379 | * lttng_check_session_daemon | |
380 | * | |
381 | * Return 0 if a sesssion daemon is available | |
382 | * else return -1 | |
383 | */ | |
384 | int lttng_check_session_daemon(void) | |
385 | { | |
386 | int ret; | |
387 | ||
388 | ret = set_session_daemon_path(); | |
389 | if (ret < 0) { | |
390 | return ret; | |
391 | } | |
392 | ||
393 | /* If socket exist, we consider the daemon started */ | |
394 | ret = access(sessiond_sock_path, F_OK); | |
395 | if (ret < 0) { | |
396 | return ret; | |
397 | } | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
fac6795d DG |
402 | /* |
403 | * set_session_daemon_path | |
404 | * | |
405 | * Set sessiond socket path by putting it in | |
406 | * the global sessiond_sock_path variable. | |
407 | */ | |
408 | static int set_session_daemon_path(void) | |
409 | { | |
410 | int ret; | |
411 | ||
412 | /* Are we in the tracing group ? */ | |
413 | ret = check_tracing_group(tracing_group); | |
414 | if (ret < 0) { | |
415 | if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK, | |
416 | getenv("HOME")) < 0) { | |
417 | return -ENOMEM; | |
418 | } | |
419 | } else { | |
420 | strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
421 | sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
427 | /* | |
428 | * check_tracing_group | |
429 | * | |
430 | * Check if the specified group name exist. | |
431 | * If yes, 0, else -1 | |
432 | */ | |
433 | static int check_tracing_group(const char *grp_name) | |
434 | { | |
435 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
436 | gid_t *grp_list; | |
437 | int grp_list_size, grp_id, i; | |
438 | int ret = -1; | |
439 | ||
440 | /* Get GID of group 'tracing' */ | |
441 | grp_tracing = getgrnam(grp_name); | |
442 | if (grp_tracing == NULL) { | |
443 | /* NULL means not found also. getgrnam(3) */ | |
444 | if (errno != 0) { | |
445 | perror("getgrnam"); | |
446 | } | |
447 | goto end; | |
448 | } | |
449 | ||
450 | /* Get number of supplementary group IDs */ | |
451 | grp_list_size = getgroups(0, NULL); | |
452 | if (grp_list_size < 0) { | |
453 | perror("getgroups"); | |
454 | goto end; | |
455 | } | |
456 | ||
457 | /* Alloc group list of the right size */ | |
458 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
459 | grp_id = getgroups(grp_list_size, grp_list); | |
460 | if (grp_id < -1) { | |
461 | perror("getgroups"); | |
462 | goto free_list; | |
463 | } | |
464 | ||
465 | for (i = 0; i < grp_list_size; i++) { | |
466 | if (grp_list[i] == grp_tracing->gr_gid) { | |
467 | ret = 0; | |
468 | break; | |
469 | } | |
470 | } | |
471 | ||
472 | free_list: | |
473 | free(grp_list); | |
474 | ||
475 | end: | |
476 | return ret; | |
477 | } | |
478 | ||
479 | /* | |
480 | * lib constructor | |
481 | */ | |
482 | static void __attribute__((constructor)) init() | |
483 | { | |
484 | /* Set default session group */ | |
485 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); | |
486 | } |