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