Fix bug when finding session by name
[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/* Prototypes */
41static int check_tracing_group(const char *grp_name);
42static int ask_sessiond(enum lttcomm_command_type lct, void **buf);
43static int recv_data_sessiond(void *buf, size_t len);
44static int send_data_sessiond(void);
45static int set_session_daemon_path(void);
46
47/* Variables */
48static char *tracing_group;
49static int connected;
50
51/*
52 * send_data_sessiond
53 *
54 * Send lttcomm_session_msg to the session daemon.
55 *
56 * On success, return 0
57 * On error, return error code
58 */
59static int send_data_sessiond(void)
60{
61 int ret;
62
63 if (!connected) {
64 ret = -ENOTCONN;
65 goto end;
66 }
67
68 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
69
70end:
71 return ret;
72}
73
74/*
75 * recv_data_sessiond
76 *
77 * Receive data from the sessiond socket.
78 *
79 * On success, return 0
80 * On error, return recv() error code
81 */
82static int recv_data_sessiond(void *buf, size_t len)
83{
84 int ret;
85
86 if (!connected) {
87 ret = -ENOTCONN;
88 goto end;
89 }
90
91 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
92 if (ret < 0) {
93 goto end;
94 }
95
96end:
97 return ret;
98}
99
100/*
101 * ask_sessiond
102 *
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).
107 */
108static int ask_sessiond(enum lttcomm_command_type lct, void **buf)
109{
110 int ret;
111 size_t size;
112 void *data = NULL;
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;
120 }
121
122 /* Get header from data transmission */
123 ret = recv_data_sessiond(&llm, sizeof(llm));
124 if (ret < 0) {
125 goto end;
126 }
127
128 /* Check error code if OK */
129 if (llm.ret_code != LTTCOMM_OK) {
130 ret = -llm.ret_code;
131 goto end;
132 }
133
134 size = llm.size_payload;
135 if (size == 0) {
136 goto end;
137 }
138
139 data = (void*) malloc(size);
140
141 /* Get payload data */
142 ret = recv_data_sessiond(data, size);
143 if (ret < 0) {
144 goto end;
145 }
146
147 *buf = data;
148 ret = size;
149
150end:
151 /* Reset lsm data struct */
152 memset(&lsm, 0, sizeof(lsm));
153 return ret;
154}
155
156/*
157 * lttng_get_readable_code
158 *
159 * Return a human readable string of code
160 */
161const 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
170/*
171 * lttng_ust_start_trace
172 *
173 * Request a trace start for pid.
174 */
175int 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
185/*
186 * lttng_ust_stop_trace
187 *
188 * Request a trace stop for pid.
189 */
190int lttng_ust_stop_trace(pid_t pid)
191{
192 int ret;
193
194 lsm.pid = pid;
195 ret = ask_sessiond(UST_STOP_TRACE, NULL);
196
197 return ret;
198}
199
200/*
201 * lttng_ust_create_trace
202 *
203 * Request a trace creation for pid.
204 */
205int lttng_ust_create_trace(pid_t pid)
206{
207 int ret;
208
209 lsm.pid = pid;
210 ret = ask_sessiond(UST_CREATE_TRACE, NULL);
211
212 return ret;
213}
214
215/*
216 * lttng_ust_list_apps
217 *
218 * Ask the session daemon for all UST traceable
219 * applications.
220 *
221 * Return the number of pids.
222 * On error, return negative value.
223 */
224int lttng_ust_list_apps(pid_t **pids)
225{
226 int ret;
227
228 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
229 if (ret < 0) {
230 return ret;
231 }
232
233 return ret / sizeof(pid_t);
234}
235
236/*
237 * lttng_list_traces
238 *
239 * Ask the session daemon for all traces (kernel and ust)
240 * for the session identified by uuid.
241 *
242 * Return the number of traces.
243 */
244int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
245{
246 int ret;
247
248 uuid_copy(lsm.session_id, *uuid);
249
250 ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
251 if (ret < 0) {
252 return ret;
253 }
254
255 return ret / sizeof(struct lttng_trace);
256}
257
258/*
259 * lttng_create_session
260 *
261 * Create a brand new session using name. Allocate
262 * the session_id param pointing to the UUID.
263 */
264int lttng_create_session(char *name, uuid_t *session_id)
265{
266 int ret;
267
268 strncpy(lsm.session_name, name, sizeof(lsm.session_name));
269 lsm.session_name[sizeof(lsm.session_name) - 1] = '\0';
270
271 ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL);
272 if (ret < 0) {
273 goto end;
274 }
275
276 uuid_copy(*session_id, llm.session_id);
277
278end:
279 return ret;
280}
281
282/*
283 * lttng_destroy_session
284 *
285 * Destroy session using name.
286 */
287int lttng_destroy_session(uuid_t *uuid)
288{
289 int ret;
290
291 uuid_copy(lsm.session_id, *uuid);
292
293 ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
294 if (ret < 0) {
295 goto end;
296 }
297
298end:
299 return ret;
300}
301
302/*
303 * lttng_list_sessions
304 *
305 * Ask the session daemon for all available sessions.
306 *
307 * Return number of session.
308 * On error, return negative value.
309 */
310int lttng_list_sessions(struct lttng_session **sessions)
311{
312 int ret;
313
314 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
315 if (ret < 0) {
316 return ret;
317 }
318
319 return ret / sizeof(struct lttng_session);
320}
321
322/*
323 * lttng_connect_sessiond
324 *
325 * Connect to the LTTng session daemon.
326 * On success, return 0
327 * On error, return a negative value
328 */
329int lttng_connect_sessiond(void)
330{
331 int ret;
332
333 ret = set_session_daemon_path();
334 if (ret < 0) {
335 return ret;
336 }
337
338 /* Connect to the sesssion daemon */
339 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
340 if (ret < 0) {
341 return ret;
342 }
343
344 sessiond_socket = ret;
345 connected = 1;
346
347 return 0;
348}
349
350/*
351 * lttng_disconnect_sessiond
352 *
353 * Clean disconnect the session daemon.
354 */
355int lttng_disconnect_sessiond(void)
356{
357 int ret = 0;
358
359 if (connected) {
360 ret = lttcomm_close_unix_sock(sessiond_socket);
361 sessiond_socket = 0;
362 connected = 0;
363 }
364
365 return ret;
366}
367
368/*
369 * lttng_set_current_session_uuid
370 *
371 * Set the session uuid for current lsm.
372 */
373void lttng_set_current_session_uuid(char *uuid)
374{
375 uuid_parse(uuid, lsm.session_id);
376}
377
378/*
379 * lttng_set_tracing_group
380 *
381 * Set tracing group variable with name. This function
382 * allocate memory pointed by tracing_group.
383 */
384int lttng_set_tracing_group(const char *name)
385{
386 if (asprintf(&tracing_group, "%s", name) < 0) {
387 return -ENOMEM;
388 }
389
390 return 0;
391}
392
393/*
394 * lttng_check_session_daemon
395 *
396 * Return 0 if a sesssion daemon is available
397 * else return -1
398 */
399int lttng_check_session_daemon(void)
400{
401 int ret;
402
403 ret = set_session_daemon_path();
404 if (ret < 0) {
405 return ret;
406 }
407
408 /* If socket exist, we consider the daemon started */
409 ret = access(sessiond_sock_path, F_OK);
410 if (ret < 0) {
411 return ret;
412 }
413
414 return 0;
415}
416
417/*
418 * set_session_daemon_path
419 *
420 * Set sessiond socket path by putting it in
421 * the global sessiond_sock_path variable.
422 */
423static int set_session_daemon_path(void)
424{
425 int ret;
426
427 /* Are we in the tracing group ? */
428 ret = check_tracing_group(tracing_group);
429 if (ret < 0) {
430 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
431 getenv("HOME")) < 0) {
432 return -ENOMEM;
433 }
434 } else {
435 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
436 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
437 }
438
439 return 0;
440}
441
442/*
443 * check_tracing_group
444 *
445 * Check if the specified group name exist.
446 * If yes, 0, else -1
447 */
448static int check_tracing_group(const char *grp_name)
449{
450 struct group *grp_tracing; /* no free(). See getgrnam(3) */
451 gid_t *grp_list;
452 int grp_list_size, grp_id, i;
453 int ret = -1;
454
455 /* Get GID of group 'tracing' */
456 grp_tracing = getgrnam(grp_name);
457 if (grp_tracing == NULL) {
458 /* NULL means not found also. getgrnam(3) */
459 if (errno != 0) {
460 perror("getgrnam");
461 }
462 goto end;
463 }
464
465 /* Get number of supplementary group IDs */
466 grp_list_size = getgroups(0, NULL);
467 if (grp_list_size < 0) {
468 perror("getgroups");
469 goto end;
470 }
471
472 /* Alloc group list of the right size */
473 grp_list = malloc(grp_list_size * sizeof(gid_t));
474 grp_id = getgroups(grp_list_size, grp_list);
475 if (grp_id < -1) {
476 perror("getgroups");
477 goto free_list;
478 }
479
480 for (i = 0; i < grp_list_size; i++) {
481 if (grp_list[i] == grp_tracing->gr_gid) {
482 ret = 0;
483 break;
484 }
485 }
486
487free_list:
488 free(grp_list);
489
490end:
491 return ret;
492}
493
494/*
495 * lib constructor
496 */
497static void __attribute__((constructor)) init()
498{
499 /* Set default session group */
500 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
501}
This page took 0.02487 seconds and 4 git commands to generate.