d8481496d042b6c3c2b2c9375edf9c374e1a0aae
[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_lttng_msg llm;
38 static struct lttcomm_session_msg lsm;
39
40 /* Prototypes */
41 static int check_tracing_group(const char *grp_name);
42 static int ask_sessiond(void);
43 static int recvfrom_sessiond(void);
44 static int set_session_daemon_path(void);
45 static void reset_data_struct(void);
46
47 int lttng_connect_sessiond(void);
48 int lttng_create_session(const char *name, char *session_id);
49 int lttng_check_session_daemon(void);
50
51 /* Variables */
52 static char *tracing_group;
53 static int connected;
54
55 /*
56 * ask_sessiond
57 *
58 * Send lttcomm_session_msg to the session daemon.
59 *
60 * On success, return 0
61 * On error, return error code
62 */
63 static int ask_sessiond(void)
64 {
65 int ret;
66
67 if (!connected) {
68 ret = -ENOTCONN;
69 goto end;
70 }
71
72 ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm));
73
74 end:
75 return ret;
76 }
77
78 /*
79 * recvfrom_sessiond
80 *
81 * Receive data from the sessiond socket.
82 *
83 * On success, return 0
84 * On error, return recv() error code
85 */
86 static int recvfrom_sessiond(void)
87 {
88 int ret;
89
90 if (!connected) {
91 ret = -ENOTCONN;
92 goto end;
93 }
94
95 ret = lttcomm_recv_unix_sock(sessiond_socket, &llm, sizeof(llm));
96 if (ret < 0) {
97 goto end;
98 }
99
100 /* Check return code */
101 if (llm.ret_code != LTTCOMM_OK) {
102 ret = -llm.ret_code;
103 goto end;
104 }
105
106 end:
107 return ret;
108 }
109
110 /*
111 * lttng_get_readable_code
112 *
113 * Return a human readable string of code
114 */
115 const char *lttng_get_readable_code(int code)
116 {
117 if (code > -LTTCOMM_OK) {
118 return "Ended with errors";
119 }
120
121 return lttcomm_get_readable_code(code);
122 }
123
124 /*
125 * lttng_create_session
126 *
127 * Create a tracing session using "name" to the session daemon.
128 * If no name is given, the auto session creation is set and
129 * the daemon will take care of finding a name.
130 *
131 * On success, return 0 and session_id point to the uuid str.
132 * On error, negative value is returned.
133 */
134 int lttng_create_session(const char *name, char *session_id)
135 {
136 int ret;
137
138 lsm.cmd_type = LTTNG_CREATE_SESSION;
139 if (name == NULL) {
140 lsm.u.create_session.auto_session = 1;
141 } else {
142 strncpy(lsm.session_name, name, strlen(name));
143 lsm.u.create_session.auto_session = 0;
144 }
145
146 /* Ask the session daemon */
147 ret = ask_sessiond();
148 if (ret < 0) {
149 goto end;
150 }
151
152 /* Unparse session ID */
153 uuid_unparse(llm.session_id, session_id);
154
155 end:
156 reset_data_struct();
157
158 return ret;
159 }
160
161 /*
162 * lttng_ust_list_apps
163 *
164 * Ask the session daemon for all UST traceable
165 * applications.
166 *
167 * Return the size of pids.
168 */
169 size_t lttng_ust_list_apps(pid_t **pids)
170 {
171 int ret, first = 0;
172 size_t size = 0;
173 pid_t *p = NULL;
174
175 lsm.cmd_type = UST_LIST_APPS;
176
177 ret = ask_sessiond();
178 if (ret < 0) {
179 goto error;
180 }
181
182 do {
183 ret = recvfrom_sessiond();
184 if (ret < 0) {
185 goto error;
186 }
187
188 if (first == 0) {
189 first = 1;
190 size = llm.num_pckt;
191 p = malloc(sizeof(pid_t) * size);
192 }
193 p[size - llm.num_pckt] = llm.u.list_apps.pid;
194 } while ((llm.num_pckt-1) != 0);
195
196 *pids = p;
197
198 return size;
199
200 error:
201 return ret;
202 }
203
204 /*
205 * lttng_connect_sessiond
206 *
207 * Connect to the LTTng session daemon.
208 * On success, return 0
209 * On error, return a negative value
210 */
211 int lttng_connect_sessiond(void)
212 {
213 int ret;
214
215 ret = set_session_daemon_path();
216 if (ret < 0) {
217 return ret;
218 }
219
220 /* Connect to the sesssion daemon */
221 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
222 if (ret < 0) {
223 return ret;
224 }
225
226 sessiond_socket = ret;
227 connected = 1;
228
229 return 0;
230 }
231
232 /*
233 * lttng_set_tracing_group
234 *
235 * Set tracing group variable with name. This function
236 * allocate memory pointed by tracing_group.
237 */
238 int lttng_set_tracing_group(const char *name)
239 {
240 if (asprintf(&tracing_group, "%s", name) < 0) {
241 return -ENOMEM;
242 }
243
244 return 0;
245 }
246
247 /*
248 * lttng_check_session_daemon
249 *
250 * Return 0 if a sesssion daemon is available
251 * else return -1
252 */
253 int lttng_check_session_daemon(void)
254 {
255 int ret;
256
257 ret = set_session_daemon_path();
258 if (ret < 0) {
259 return ret;
260 }
261
262 /* If socket exist, we consider the daemon started */
263 ret = access(sessiond_sock_path, F_OK);
264 if (ret < 0) {
265 return ret;
266 }
267
268 return 0;
269 }
270
271 /*
272 * reset_data_struct
273 *
274 * Reset session daemon structures.
275 */
276 static void reset_data_struct(void)
277 {
278 memset(&lsm, 0, sizeof(lsm));
279 memset(&llm, 0, sizeof(llm));
280 }
281
282 /*
283 * set_session_daemon_path
284 *
285 * Set sessiond socket path by putting it in
286 * the global sessiond_sock_path variable.
287 */
288 static int set_session_daemon_path(void)
289 {
290 int ret;
291
292 /* Are we in the tracing group ? */
293 ret = check_tracing_group(tracing_group);
294 if (ret < 0) {
295 if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
296 getenv("HOME")) < 0) {
297 return -ENOMEM;
298 }
299 } else {
300 strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
301 sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
302 }
303
304 return 0;
305 }
306
307 /*
308 * check_tracing_group
309 *
310 * Check if the specified group name exist.
311 * If yes, 0, else -1
312 */
313 static int check_tracing_group(const char *grp_name)
314 {
315 struct group *grp_tracing; /* no free(). See getgrnam(3) */
316 gid_t *grp_list;
317 int grp_list_size, grp_id, i;
318 int ret = -1;
319
320 /* Get GID of group 'tracing' */
321 grp_tracing = getgrnam(grp_name);
322 if (grp_tracing == NULL) {
323 /* NULL means not found also. getgrnam(3) */
324 if (errno != 0) {
325 perror("getgrnam");
326 }
327 goto end;
328 }
329
330 /* Get number of supplementary group IDs */
331 grp_list_size = getgroups(0, NULL);
332 if (grp_list_size < 0) {
333 perror("getgroups");
334 goto end;
335 }
336
337 /* Alloc group list of the right size */
338 grp_list = malloc(grp_list_size * sizeof(gid_t));
339 grp_id = getgroups(grp_list_size, grp_list);
340 if (grp_id < -1) {
341 perror("getgroups");
342 goto free_list;
343 }
344
345 for (i = 0; i < grp_list_size; i++) {
346 if (grp_list[i] == grp_tracing->gr_gid) {
347 ret = 0;
348 break;
349 }
350 }
351
352 free_list:
353 free(grp_list);
354
355 end:
356 return ret;
357 }
358
359 /*
360 * lib constructor
361 */
362 static void __attribute__((constructor)) init()
363 {
364 /* Set default session group */
365 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
366 }
This page took 0.054839 seconds and 4 git commands to generate.