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 modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (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 along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <errno.h> | |
21 | #include <fcntl.h> | |
22 | #include <getopt.h> | |
23 | #include <grp.h> | |
24 | #include <limits.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/wait.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | #include <lttng/liblttngctl.h> | |
34 | ||
35 | #include "lttng.h" | |
36 | #include "lttngerr.h" | |
37 | ||
38 | /* Variables */ | |
39 | static char *progname; | |
40 | ||
41 | /* Prototypes */ | |
42 | static int process_client_opt(void); | |
43 | static int process_opt_list_apps(void); | |
57167058 | 44 | static int process_opt_list_sessions(void); |
aaf97519 | 45 | static int process_opt_create_session(void); |
5b8719f5 DG |
46 | static void sighandler(int sig); |
47 | static int set_signal_handler(void); | |
fac6795d DG |
48 | |
49 | /* | |
50 | * start_client | |
51 | * | |
52 | * Process client request from the command line | |
53 | * options. Every tracing action is done by the | |
54 | * liblttngctl API. | |
55 | */ | |
56 | static int process_client_opt(void) | |
57 | { | |
58 | int ret; | |
8028d920 | 59 | uuid_t uuid; |
fac6795d DG |
60 | |
61 | /* Connect to the session daemon */ | |
62 | ret = lttng_connect_sessiond(); | |
63 | if (ret < 0) { | |
fac6795d DG |
64 | goto end; |
65 | } | |
66 | ||
67 | if (opt_list_apps) { | |
68 | ret = process_opt_list_apps(); | |
69 | if (ret < 0) { | |
fac6795d DG |
70 | goto end; |
71 | } | |
72 | } | |
73 | ||
57167058 DG |
74 | if (opt_list_session) { |
75 | ret = process_opt_list_sessions(); | |
76 | if (ret < 0) { | |
77 | goto end; | |
78 | } | |
79 | } | |
80 | ||
aaf97519 DG |
81 | if (opt_create_session != NULL) { |
82 | ret = process_opt_create_session(); | |
83 | if (ret < 0) { | |
84 | goto end; | |
85 | } | |
86 | } | |
87 | ||
8028d920 DG |
88 | if (opt_destroy_session != NULL) { |
89 | uuid_parse(opt_destroy_session, uuid); | |
90 | ret = lttng_destroy_session(&uuid); | |
91 | if (ret < 0) { | |
92 | goto end; | |
93 | } | |
94 | } | |
95 | ||
e8be5f4f DG |
96 | if (opt_session_uuid != NULL) { |
97 | lttng_set_current_session_uuid(opt_session_uuid); | |
98 | } | |
99 | ||
fac6795d DG |
100 | return 0; |
101 | ||
102 | end: | |
ebafd2a5 | 103 | ERR("%s", lttng_get_readable_code(ret)); |
fac6795d DG |
104 | return ret; |
105 | } | |
106 | ||
aaf97519 DG |
107 | /* |
108 | * process_opt_create_session | |
109 | * | |
110 | * Create a new session using the name pass | |
111 | * to the command line. | |
112 | */ | |
113 | static int process_opt_create_session(void) | |
114 | { | |
115 | int ret; | |
8028d920 DG |
116 | uuid_t session_id; |
117 | char str_uuid[37]; | |
aaf97519 DG |
118 | |
119 | ret = lttng_create_session(opt_create_session, &session_id); | |
120 | if (ret < 0) { | |
121 | goto error; | |
122 | } | |
123 | ||
8028d920 DG |
124 | uuid_unparse(session_id, str_uuid); |
125 | ||
aaf97519 | 126 | MSG("Session created:"); |
8028d920 | 127 | MSG(" %s (%s)", opt_create_session, str_uuid); |
aaf97519 DG |
128 | |
129 | error: | |
130 | return ret; | |
131 | } | |
132 | ||
57167058 DG |
133 | /* |
134 | * process_opt_list_sessions | |
135 | * | |
136 | * Get the list of available sessions from | |
137 | * the session daemon and print it to user. | |
138 | */ | |
139 | static int process_opt_list_sessions(void) | |
140 | { | |
141 | int ret, count, i; | |
142 | struct lttng_session *sess; | |
143 | ||
144 | count = lttng_list_sessions(&sess); | |
145 | if (count < 0) { | |
146 | ret = count; | |
147 | goto error; | |
148 | } | |
149 | ||
150 | MSG("Available sessions [Name (uuid)]:"); | |
151 | for (i = 0; i < count; i++) { | |
152 | MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid); | |
153 | } | |
154 | ||
155 | free(sess); | |
156 | MSG("\nTo select a session, use --session UUID."); | |
157 | ||
158 | return 0; | |
159 | ||
160 | error: | |
161 | return ret; | |
162 | } | |
163 | ||
fac6795d DG |
164 | /* |
165 | * process_opt_list_apps | |
166 | * | |
167 | * Get the UST traceable pid list and print | |
168 | * them to the user. | |
169 | */ | |
170 | static int process_opt_list_apps(void) | |
171 | { | |
e8f07c63 | 172 | int i, ret, count; |
fac6795d DG |
173 | pid_t *pids; |
174 | FILE *fp; | |
175 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ | |
176 | char cmdline[PATH_MAX]; | |
177 | ||
e8f07c63 DG |
178 | count = lttng_ust_list_apps(&pids); |
179 | if (count < 0) { | |
180 | ret = count; | |
fac6795d DG |
181 | goto error; |
182 | } | |
183 | ||
184 | MSG("LTTng UST traceable application [name (pid)]:"); | |
e8f07c63 | 185 | for (i=0; i < count; i++) { |
fac6795d DG |
186 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]); |
187 | fp = fopen(path, "r"); | |
188 | if (fp == NULL) { | |
e8f07c63 | 189 | MSG("\t(not running) (%d)", pids[i]); |
fac6795d DG |
190 | continue; |
191 | } | |
192 | ret = fread(cmdline, 1, sizeof(cmdline), fp); | |
193 | MSG("\t%s (%d)", cmdline, pids[i]); | |
194 | fclose(fp); | |
195 | } | |
196 | ||
e065084a DG |
197 | /* Allocated by lttng_ust_list_apps() */ |
198 | free(pids); | |
199 | ||
fac6795d DG |
200 | return 0; |
201 | ||
202 | error: | |
203 | return ret; | |
204 | } | |
205 | ||
5b8719f5 DG |
206 | /* |
207 | * spawn_sessiond | |
208 | * | |
209 | * Spawn a session daemon by forking and execv. | |
210 | */ | |
211 | static int spawn_sessiond(char *pathname) | |
212 | { | |
213 | int ret = 0; | |
214 | pid_t pid; | |
215 | ||
216 | MSG("Spawning session daemon"); | |
217 | pid = fork(); | |
218 | if (pid == 0) { | |
219 | /* Spawn session daemon and tell | |
220 | * it to signal us when ready. | |
221 | */ | |
75462a81 | 222 | ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
5b8719f5 DG |
223 | if (ret < 0) { |
224 | if (errno == ENOENT) { | |
225 | ERR("No session daemon found. Use --sessiond-path."); | |
226 | } else { | |
227 | perror("execlp"); | |
228 | } | |
229 | kill(getppid(), SIGTERM); | |
230 | exit(EXIT_FAILURE); | |
231 | } | |
232 | exit(EXIT_SUCCESS); | |
233 | } else if (pid > 0) { | |
234 | /* Wait for ltt-sessiond to start */ | |
235 | pause(); | |
236 | goto end; | |
237 | } else { | |
238 | perror("fork"); | |
239 | ret = -1; | |
240 | goto end; | |
241 | } | |
242 | ||
243 | end: | |
244 | return ret; | |
245 | } | |
246 | ||
fac6795d DG |
247 | /* |
248 | * check_ltt_sessiond | |
249 | * | |
250 | * Check if the session daemon is available using | |
5b8719f5 DG |
251 | * the liblttngctl API for the check. If not, try to |
252 | * spawn a daemon. | |
fac6795d DG |
253 | */ |
254 | static int check_ltt_sessiond(void) | |
255 | { | |
256 | int ret; | |
5b8719f5 | 257 | char *pathname = NULL; |
fac6795d DG |
258 | |
259 | ret = lttng_check_session_daemon(); | |
260 | if (ret < 0) { | |
5b8719f5 DG |
261 | /* Try command line option path */ |
262 | if (opt_sessiond_path != NULL) { | |
263 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
264 | if (ret < 0) { | |
265 | ERR("No such file: %s", opt_sessiond_path); | |
266 | goto end; | |
267 | } | |
268 | pathname = opt_sessiond_path; | |
269 | } else { | |
270 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 DG |
271 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
272 | if (pathname != NULL) { | |
273 | /* strdup here in order to make the free() | |
274 | * not fail later on. | |
275 | */ | |
276 | pathname = strdup(pathname); | |
277 | } | |
5b8719f5 DG |
278 | } |
279 | ||
280 | /* Let's rock and roll */ | |
281 | if (pathname == NULL) { | |
282 | ret = asprintf(&pathname, "ltt-sessiond"); | |
283 | if (ret < 0) { | |
284 | goto end; | |
285 | } | |
286 | } | |
287 | ||
288 | ret = spawn_sessiond(pathname); | |
289 | free(pathname); | |
290 | if (ret < 0) { | |
291 | ERR("Problem occurs when starting %s", pathname); | |
292 | goto end; | |
293 | } | |
fac6795d DG |
294 | } |
295 | ||
5b8719f5 | 296 | end: |
fac6795d DG |
297 | return ret; |
298 | } | |
299 | ||
5b8719f5 DG |
300 | /* |
301 | * set_signal_handler | |
302 | * | |
303 | * Setup signal handler for SIGCHLD and SIGTERM. | |
304 | */ | |
305 | static int set_signal_handler(void) | |
306 | { | |
307 | int ret = 0; | |
308 | struct sigaction sa; | |
309 | sigset_t sigset; | |
310 | ||
311 | if ((ret = sigemptyset(&sigset)) < 0) { | |
312 | perror("sigemptyset"); | |
313 | goto end; | |
314 | } | |
315 | ||
316 | sa.sa_handler = sighandler; | |
317 | sa.sa_mask = sigset; | |
318 | sa.sa_flags = 0; | |
319 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
320 | perror("sigaction"); | |
321 | goto end; | |
322 | } | |
fac6795d | 323 | |
5b8719f5 DG |
324 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
325 | perror("sigaction"); | |
326 | goto end; | |
327 | } | |
328 | ||
329 | end: | |
330 | return ret; | |
331 | } | |
332 | ||
333 | /* | |
334 | * sighandler | |
335 | * | |
336 | * Signal handler for the daemon | |
337 | */ | |
338 | static void sighandler(int sig) | |
339 | { | |
340 | DBG("%d received", sig); | |
341 | switch (sig) { | |
342 | case SIGTERM: | |
343 | clean_exit(EXIT_FAILURE); | |
344 | break; | |
345 | case SIGCHLD: | |
346 | /* Notify is done */ | |
347 | break; | |
348 | default: | |
349 | break; | |
350 | } | |
351 | ||
352 | return; | |
353 | } | |
fac6795d DG |
354 | /* |
355 | * clean_exit | |
356 | */ | |
357 | void clean_exit(int code) | |
358 | { | |
359 | DBG("Clean exit"); | |
87378cf5 DG |
360 | if (lttng_disconnect_sessiond() < 0) { |
361 | ERR("Session daemon disconnect failed."); | |
362 | } | |
fac6795d DG |
363 | exit(code); |
364 | } | |
365 | ||
366 | /* | |
5b8719f5 | 367 | * main |
fac6795d DG |
368 | */ |
369 | int main(int argc, char *argv[]) | |
370 | { | |
371 | int ret; | |
372 | ||
373 | progname = argv[0] ? argv[0] : "lttng"; | |
374 | ||
375 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
376 | if (strncmp(progname, "drtrace", 7) == 0) { | |
377 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
378 | } | |
379 | ||
380 | ret = parse_args(argc, (const char **) argv); | |
381 | if (ret < 0) { | |
87378cf5 | 382 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
383 | } |
384 | ||
5b8719f5 DG |
385 | ret = set_signal_handler(); |
386 | if (ret < 0) { | |
87378cf5 | 387 | clean_exit(ret); |
5b8719f5 DG |
388 | } |
389 | ||
fac6795d DG |
390 | if (opt_tracing_group != NULL) { |
391 | DBG("Set tracing group to '%s'", opt_tracing_group); | |
392 | lttng_set_tracing_group(opt_tracing_group); | |
393 | } | |
394 | ||
395 | /* If ask for kernel tracing, need root perms */ | |
396 | if (opt_trace_kernel) { | |
397 | DBG("Kernel tracing activated"); | |
398 | if (getuid() != 0) { | |
399 | ERR("%s must be setuid root", progname); | |
87378cf5 | 400 | clean_exit(-EPERM); |
fac6795d DG |
401 | } |
402 | } | |
403 | ||
404 | /* Check if the lttng session daemon is running. | |
405 | * If no, a daemon will be spawned. | |
406 | */ | |
5b8719f5 | 407 | if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { |
87378cf5 | 408 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
409 | } |
410 | ||
411 | ret = process_client_opt(); | |
412 | if (ret < 0) { | |
87378cf5 | 413 | clean_exit(ret); |
fac6795d DG |
414 | } |
415 | ||
87378cf5 DG |
416 | clean_exit(0); |
417 | ||
fac6795d DG |
418 | return 0; |
419 | } |