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 DG |
37 | static struct lttcomm_session_msg lsm; |
38 | ||
39 | /* Prototypes */ | |
40 | static int check_tracing_group(const char *grp_name); | |
ca95a216 DG |
41 | static int ask_sessiond(enum lttcomm_command_type lct, void **buf); |
42 | static int recv_data_sessiond(void *buf, size_t len); | |
43 | static int send_data_sessiond(void); | |
fac6795d | 44 | static int set_session_daemon_path(void); |
fac6795d DG |
45 | |
46 | /* Variables */ | |
47 | static char *tracing_group; | |
48 | static int connected; | |
49 | ||
50 | /* | |
ca95a216 | 51 | * send_data_sessiond |
fac6795d | 52 | * |
e065084a | 53 | * Send lttcomm_session_msg to the session daemon. |
fac6795d DG |
54 | * |
55 | * On success, return 0 | |
56 | * On error, return error code | |
57 | */ | |
ca95a216 | 58 | static int send_data_sessiond(void) |
fac6795d DG |
59 | { |
60 | int ret; | |
61 | ||
62 | if (!connected) { | |
e065084a DG |
63 | ret = -ENOTCONN; |
64 | goto end; | |
fac6795d DG |
65 | } |
66 | ||
67 | ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm)); | |
e065084a DG |
68 | |
69 | end: | |
70 | return ret; | |
71 | } | |
72 | ||
73 | /* | |
ca95a216 | 74 | * recv_data_sessiond |
e065084a DG |
75 | * |
76 | * Receive data from the sessiond socket. | |
77 | * | |
78 | * On success, return 0 | |
79 | * On error, return recv() error code | |
80 | */ | |
ca95a216 | 81 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
82 | { |
83 | int ret; | |
84 | ||
85 | if (!connected) { | |
86 | ret = -ENOTCONN; | |
87 | goto end; | |
fac6795d DG |
88 | } |
89 | ||
ca95a216 | 90 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
fac6795d | 91 | if (ret < 0) { |
e065084a | 92 | goto end; |
fac6795d DG |
93 | } |
94 | ||
e065084a | 95 | end: |
fac6795d DG |
96 | return ret; |
97 | } | |
98 | ||
99 | /* | |
ca95a216 | 100 | * ask_sessiond |
fac6795d | 101 | * |
ca95a216 DG |
102 | * Ask the session daemon a specific command |
103 | * and put the data into buf. | |
104 | * | |
105 | * Return size of data (only payload, not header). | |
fac6795d | 106 | */ |
ca95a216 | 107 | static int ask_sessiond(enum lttcomm_command_type lct, void **buf) |
fac6795d | 108 | { |
ca95a216 DG |
109 | int ret; |
110 | size_t size; | |
111 | void *data = NULL; | |
112 | struct lttcomm_lttng_msg llm; | |
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 | ||
fac6795d DG |
170 | /* |
171 | * lttng_ust_list_apps | |
172 | * | |
173 | * Ask the session daemon for all UST traceable | |
174 | * applications. | |
175 | * | |
ca95a216 DG |
176 | * Return the number of pids. |
177 | * On error, return negative value. | |
fac6795d | 178 | */ |
ca95a216 | 179 | int lttng_ust_list_apps(pid_t **pids) |
fac6795d | 180 | { |
ca95a216 | 181 | int ret; |
fac6795d | 182 | |
ca95a216 | 183 | ret = ask_sessiond(UST_LIST_APPS, (void**) pids); |
fac6795d | 184 | if (ret < 0) { |
ca95a216 | 185 | return ret; |
fac6795d DG |
186 | } |
187 | ||
ca95a216 | 188 | return ret / sizeof(pid_t); |
fac6795d DG |
189 | } |
190 | ||
57167058 DG |
191 | /* |
192 | * lttng_list_sessions | |
193 | * | |
194 | * Ask the session daemon for all available sessions. | |
195 | * | |
ca95a216 DG |
196 | * Return number of session. |
197 | * On error, return negative value. | |
57167058 | 198 | */ |
ca95a216 | 199 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 200 | { |
ca95a216 | 201 | int ret; |
57167058 | 202 | |
ca95a216 | 203 | ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions); |
57167058 | 204 | if (ret < 0) { |
ca95a216 | 205 | return ret; |
57167058 DG |
206 | } |
207 | ||
ca95a216 | 208 | return ret / sizeof(struct lttng_session); |
57167058 DG |
209 | } |
210 | ||
fac6795d DG |
211 | /* |
212 | * lttng_connect_sessiond | |
213 | * | |
214 | * Connect to the LTTng session daemon. | |
215 | * On success, return 0 | |
216 | * On error, return a negative value | |
217 | */ | |
218 | int lttng_connect_sessiond(void) | |
219 | { | |
220 | int ret; | |
221 | ||
222 | ret = set_session_daemon_path(); | |
223 | if (ret < 0) { | |
224 | return ret; | |
225 | } | |
226 | ||
227 | /* Connect to the sesssion daemon */ | |
228 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
229 | if (ret < 0) { | |
230 | return ret; | |
231 | } | |
232 | ||
233 | sessiond_socket = ret; | |
234 | connected = 1; | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | /* | |
240 | * lttng_set_tracing_group | |
241 | * | |
242 | * Set tracing group variable with name. This function | |
243 | * allocate memory pointed by tracing_group. | |
244 | */ | |
245 | int lttng_set_tracing_group(const char *name) | |
246 | { | |
247 | if (asprintf(&tracing_group, "%s", name) < 0) { | |
248 | return -ENOMEM; | |
249 | } | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
254 | /* | |
255 | * lttng_check_session_daemon | |
256 | * | |
257 | * Return 0 if a sesssion daemon is available | |
258 | * else return -1 | |
259 | */ | |
260 | int lttng_check_session_daemon(void) | |
261 | { | |
262 | int ret; | |
263 | ||
264 | ret = set_session_daemon_path(); | |
265 | if (ret < 0) { | |
266 | return ret; | |
267 | } | |
268 | ||
269 | /* If socket exist, we consider the daemon started */ | |
270 | ret = access(sessiond_sock_path, F_OK); | |
271 | if (ret < 0) { | |
272 | return ret; | |
273 | } | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
fac6795d DG |
278 | /* |
279 | * set_session_daemon_path | |
280 | * | |
281 | * Set sessiond socket path by putting it in | |
282 | * the global sessiond_sock_path variable. | |
283 | */ | |
284 | static int set_session_daemon_path(void) | |
285 | { | |
286 | int ret; | |
287 | ||
288 | /* Are we in the tracing group ? */ | |
289 | ret = check_tracing_group(tracing_group); | |
290 | if (ret < 0) { | |
291 | if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK, | |
292 | getenv("HOME")) < 0) { | |
293 | return -ENOMEM; | |
294 | } | |
295 | } else { | |
296 | strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, | |
297 | sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); | |
298 | } | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | /* | |
304 | * check_tracing_group | |
305 | * | |
306 | * Check if the specified group name exist. | |
307 | * If yes, 0, else -1 | |
308 | */ | |
309 | static int check_tracing_group(const char *grp_name) | |
310 | { | |
311 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
312 | gid_t *grp_list; | |
313 | int grp_list_size, grp_id, i; | |
314 | int ret = -1; | |
315 | ||
316 | /* Get GID of group 'tracing' */ | |
317 | grp_tracing = getgrnam(grp_name); | |
318 | if (grp_tracing == NULL) { | |
319 | /* NULL means not found also. getgrnam(3) */ | |
320 | if (errno != 0) { | |
321 | perror("getgrnam"); | |
322 | } | |
323 | goto end; | |
324 | } | |
325 | ||
326 | /* Get number of supplementary group IDs */ | |
327 | grp_list_size = getgroups(0, NULL); | |
328 | if (grp_list_size < 0) { | |
329 | perror("getgroups"); | |
330 | goto end; | |
331 | } | |
332 | ||
333 | /* Alloc group list of the right size */ | |
334 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
335 | grp_id = getgroups(grp_list_size, grp_list); | |
336 | if (grp_id < -1) { | |
337 | perror("getgroups"); | |
338 | goto free_list; | |
339 | } | |
340 | ||
341 | for (i = 0; i < grp_list_size; i++) { | |
342 | if (grp_list[i] == grp_tracing->gr_gid) { | |
343 | ret = 0; | |
344 | break; | |
345 | } | |
346 | } | |
347 | ||
348 | free_list: | |
349 | free(grp_list); | |
350 | ||
351 | end: | |
352 | return ret; | |
353 | } | |
354 | ||
355 | /* | |
356 | * lib constructor | |
357 | */ | |
358 | static void __attribute__((constructor)) init() | |
359 | { | |
360 | /* Set default session group */ | |
361 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); | |
362 | } |