Add ust create trace feature
[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
27#include <lttng/liblttngctl.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 */
fac6795d 37static struct lttcomm_session_msg lsm;
aaf97519 38static struct lttcomm_lttng_msg llm;
fac6795d
DG
39
40/* Prototypes */
41static int check_tracing_group(const char *grp_name);
ca95a216
DG
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);
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 */
ca95a216 108static 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
150end:
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 */
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
df0da139
DG
170/*
171 * lttng_ust_create_trace
172 *
173 * Request a trace creation for pid.
174 */
175int lttng_ust_create_trace(pid_t pid)
176{
177 int ret;
178
179 lsm.pid = pid;
180 ret = ask_sessiond(UST_CREATE_TRACE, NULL);
181
182 return ret;
183}
184
fac6795d
DG
185/*
186 * lttng_ust_list_apps
187 *
188 * Ask the session daemon for all UST traceable
189 * applications.
190 *
ca95a216
DG
191 * Return the number of pids.
192 * On error, return negative value.
fac6795d 193 */
ca95a216 194int lttng_ust_list_apps(pid_t **pids)
fac6795d 195{
ca95a216 196 int ret;
fac6795d 197
ca95a216 198 ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
fac6795d 199 if (ret < 0) {
ca95a216 200 return ret;
fac6795d
DG
201 }
202
ca95a216 203 return ret / sizeof(pid_t);
fac6795d
DG
204}
205
aaf97519
DG
206/*
207 * lttng_create_session
208 *
209 * Create a brand new session using name. Allocate
210 * the session_id param pointing to the UUID.
211 */
8028d920 212int lttng_create_session(char *name, uuid_t *session_id)
aaf97519
DG
213{
214 int ret;
aaf97519
DG
215
216 strncpy(lsm.session_name, name, sizeof(lsm.session_name));
8028d920 217 lsm.session_name[sizeof(lsm.session_name) - 1] = '\0';
aaf97519
DG
218
219 ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL);
220 if (ret < 0) {
221 goto end;
222 }
223
8028d920 224 uuid_copy(*session_id, llm.session_id);
aaf97519 225
8028d920
DG
226end:
227 return ret;
228}
229
230/*
231 * lttng_destroy_session
232 *
233 * Destroy session using name.
234 */
235int lttng_destroy_session(uuid_t *uuid)
236{
237 int ret;
238
239 uuid_copy(lsm.session_id, *uuid);
240
241 ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
242 if (ret < 0) {
243 goto end;
244 }
aaf97519
DG
245
246end:
247 return ret;
248}
249
57167058
DG
250/*
251 * lttng_list_sessions
252 *
253 * Ask the session daemon for all available sessions.
254 *
ca95a216
DG
255 * Return number of session.
256 * On error, return negative value.
57167058 257 */
ca95a216 258int lttng_list_sessions(struct lttng_session **sessions)
57167058 259{
ca95a216 260 int ret;
57167058 261
ca95a216 262 ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions);
57167058 263 if (ret < 0) {
ca95a216 264 return ret;
57167058
DG
265 }
266
ca95a216 267 return ret / sizeof(struct lttng_session);
57167058
DG
268}
269
fac6795d
DG
270/*
271 * lttng_connect_sessiond
272 *
273 * Connect to the LTTng session daemon.
274 * On success, return 0
275 * On error, return a negative value
276 */
277int lttng_connect_sessiond(void)
278{
279 int ret;
280
281 ret = set_session_daemon_path();
282 if (ret < 0) {
283 return ret;
284 }
285
286 /* Connect to the sesssion daemon */
287 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
288 if (ret < 0) {
289 return ret;
290 }
291
292 sessiond_socket = ret;
293 connected = 1;
294
295 return 0;
296}
297
87378cf5
DG
298/*
299 * lttng_disconnect_sessiond
300 *
301 * Clean disconnect the session daemon.
302 */
303int lttng_disconnect_sessiond(void)
304{
305 int ret = 0;
306
307 if (connected) {
308 ret = lttcomm_close_unix_sock(sessiond_socket);
309 sessiond_socket = 0;
310 connected = 0;
311 }
312
313 return ret;
314}
315
e8be5f4f
DG
316/*
317 * lttng_set_current_session_uuid
318 *
319 * Set the session uuid for current lsm.
320 */
321void lttng_set_current_session_uuid(char *uuid)
322{
323 uuid_parse(uuid, lsm.session_id);
324}
325
fac6795d
DG
326/*
327 * lttng_set_tracing_group
328 *
329 * Set tracing group variable with name. This function
330 * allocate memory pointed by tracing_group.
331 */
332int lttng_set_tracing_group(const char *name)
333{
334 if (asprintf(&tracing_group, "%s", name) < 0) {
335 return -ENOMEM;
336 }
337
338 return 0;
339}
340
341/*
342 * lttng_check_session_daemon
343 *
344 * Return 0 if a sesssion daemon is available
345 * else return -1
346 */
347int lttng_check_session_daemon(void)
348{
349 int ret;
350
351 ret = set_session_daemon_path();
352 if (ret < 0) {
353 return ret;
354 }
355
356 /* If socket exist, we consider the daemon started */
357 ret = access(sessiond_sock_path, F_OK);
358 if (ret < 0) {
359 return ret;
360 }
361
362 return 0;
363}
364
fac6795d
DG
365/*
366 * set_session_daemon_path
367 *
368 * Set sessiond socket path by putting it in
369 * the global sessiond_sock_path variable.
370 */
371static int set_session_daemon_path(void)
372{
373 int ret;
374
375 /* Are we in the tracing group ? */
376 ret = check_tracing_group(tracing_group);
377 if (ret < 0) {
378 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
379 getenv("HOME")) < 0) {
380 return -ENOMEM;
381 }
382 } else {
383 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
384 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
385 }
386
387 return 0;
388}
389
390/*
391 * check_tracing_group
392 *
393 * Check if the specified group name exist.
394 * If yes, 0, else -1
395 */
396static int check_tracing_group(const char *grp_name)
397{
398 struct group *grp_tracing; /* no free(). See getgrnam(3) */
399 gid_t *grp_list;
400 int grp_list_size, grp_id, i;
401 int ret = -1;
402
403 /* Get GID of group 'tracing' */
404 grp_tracing = getgrnam(grp_name);
405 if (grp_tracing == NULL) {
406 /* NULL means not found also. getgrnam(3) */
407 if (errno != 0) {
408 perror("getgrnam");
409 }
410 goto end;
411 }
412
413 /* Get number of supplementary group IDs */
414 grp_list_size = getgroups(0, NULL);
415 if (grp_list_size < 0) {
416 perror("getgroups");
417 goto end;
418 }
419
420 /* Alloc group list of the right size */
421 grp_list = malloc(grp_list_size * sizeof(gid_t));
422 grp_id = getgroups(grp_list_size, grp_list);
423 if (grp_id < -1) {
424 perror("getgroups");
425 goto free_list;
426 }
427
428 for (i = 0; i < grp_list_size; i++) {
429 if (grp_list[i] == grp_tracing->gr_gid) {
430 ret = 0;
431 break;
432 }
433 }
434
435free_list:
436 free(grp_list);
437
438end:
439 return ret;
440}
441
442/*
443 * lib constructor
444 */
445static void __attribute__((constructor)) init()
446{
447 /* Set default session group */
448 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
449}
This page took 0.07002 seconds and 4 git commands to generate.