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 | ||
5b97ec60 | 33 | #include <lttng/lttng.h> |
fac6795d DG |
34 | |
35 | #include "lttng.h" | |
36 | #include "lttngerr.h" | |
37 | ||
38 | /* Variables */ | |
39 | static char *progname; | |
96243366 | 40 | static char *session_name; |
96243366 | 41 | static uuid_t current_uuid; |
894be886 DG |
42 | static int auto_session; |
43 | static int auto_trace; | |
fac6795d DG |
44 | |
45 | /* Prototypes */ | |
46 | static int process_client_opt(void); | |
47 | static int process_opt_list_apps(void); | |
57167058 | 48 | static int process_opt_list_sessions(void); |
1657e9bb | 49 | static int process_opt_list_traces(void); |
aaf97519 | 50 | static int process_opt_create_session(void); |
894be886 DG |
51 | static int process_kernel_create_trace(void); |
52 | static int process_opt_kernel_event(void); | |
96243366 | 53 | static int set_session_uuid(void); |
5b8719f5 DG |
54 | static void sighandler(int sig); |
55 | static int set_signal_handler(void); | |
8548ff30 | 56 | static int validate_options(void); |
47b74d63 | 57 | static char *get_cmdline_by_pid(pid_t pid); |
96243366 | 58 | static void set_opt_session_info(void); |
fac6795d DG |
59 | |
60 | /* | |
61 | * start_client | |
62 | * | |
63 | * Process client request from the command line | |
64 | * options. Every tracing action is done by the | |
65 | * liblttngctl API. | |
66 | */ | |
67 | static int process_client_opt(void) | |
68 | { | |
69 | int ret; | |
96243366 DG |
70 | |
71 | set_opt_session_info(); | |
fac6795d | 72 | |
fac6795d DG |
73 | if (opt_list_apps) { |
74 | ret = process_opt_list_apps(); | |
75 | if (ret < 0) { | |
fac6795d DG |
76 | goto end; |
77 | } | |
894be886 | 78 | goto error; |
fac6795d DG |
79 | } |
80 | ||
57167058 DG |
81 | if (opt_list_session) { |
82 | ret = process_opt_list_sessions(); | |
83 | if (ret < 0) { | |
84 | goto end; | |
85 | } | |
894be886 | 86 | goto error; |
57167058 DG |
87 | } |
88 | ||
894be886 DG |
89 | /* Session creation or auto session set on */ |
90 | if (auto_session || opt_create_session) { | |
91 | DBG("Creating a new session"); | |
92 | ret = process_opt_create_session(); | |
1657e9bb DG |
93 | if (ret < 0) { |
94 | goto end; | |
95 | } | |
96 | } | |
97 | ||
894be886 DG |
98 | ret = set_session_uuid(); |
99 | if (ret < 0) { | |
100 | ERR("Session %s not found", opt_session_name); | |
101 | goto error; | |
102 | } | |
aaf97519 | 103 | |
894be886 DG |
104 | if (opt_destroy_session) { |
105 | ret = lttng_destroy_session(¤t_uuid); | |
8028d920 | 106 | if (ret < 0) { |
894be886 | 107 | goto end; |
8028d920 | 108 | } |
894be886 | 109 | MSG("Session %s destroyed.", opt_session_name); |
8028d920 DG |
110 | } |
111 | ||
96243366 DG |
112 | if (opt_list_traces) { |
113 | ret = process_opt_list_traces(); | |
7442b2ba | 114 | if (ret < 0) { |
96243366 | 115 | goto end; |
7442b2ba | 116 | } |
e8be5f4f DG |
117 | } |
118 | ||
96243366 DG |
119 | /* |
120 | * Action on traces (kernel or/and userspace). | |
121 | */ | |
894be886 | 122 | |
ad874cce | 123 | if (opt_trace_kernel) { |
894be886 DG |
124 | if (auto_trace || opt_create_trace) { |
125 | DBG("Creating a kernel trace"); | |
126 | ret = process_kernel_create_trace(); | |
127 | if (ret < 0) { | |
128 | goto end; | |
129 | } | |
130 | } | |
131 | ||
132 | if (opt_event_list != NULL) { | |
133 | ret = process_opt_kernel_event(); | |
134 | } else { | |
135 | // Enable all events | |
136 | } | |
137 | ||
138 | goto error; | |
df0da139 DG |
139 | } |
140 | ||
ad874cce | 141 | if (opt_trace_pid != 0) { |
894be886 | 142 | if (auto_trace || opt_create_trace) { |
ad874cce DG |
143 | DBG("Create a userspace trace for pid %d", opt_trace_pid); |
144 | ret = lttng_ust_create_trace(opt_trace_pid); | |
145 | if (ret < 0) { | |
146 | goto end; | |
147 | } | |
894be886 | 148 | MSG("Trace created successfully!"); |
ce3d728c | 149 | } |
ce3d728c | 150 | |
894be886 | 151 | if (auto_trace || opt_start_trace) { |
ad874cce DG |
152 | DBG("Start trace for pid %d", opt_trace_pid); |
153 | ret = lttng_ust_start_trace(opt_trace_pid); | |
154 | if (ret < 0) { | |
155 | goto end; | |
156 | } | |
157 | MSG("Trace started successfully!"); | |
158 | } else if (opt_stop_trace) { | |
159 | DBG("Stop trace for pid %d", opt_trace_pid); | |
160 | ret = lttng_ust_stop_trace(opt_trace_pid); | |
161 | if (ret < 0) { | |
162 | goto end; | |
163 | } | |
164 | MSG("Trace stopped successfully!"); | |
520ff687 | 165 | } |
ad874cce | 166 | |
520ff687 DG |
167 | } |
168 | ||
fac6795d DG |
169 | return 0; |
170 | ||
171 | end: | |
ebafd2a5 | 172 | ERR("%s", lttng_get_readable_code(ret)); |
5e16da05 | 173 | error: /* fall through */ |
7442b2ba DG |
174 | return ret; |
175 | } | |
176 | ||
177 | /* | |
894be886 | 178 | * process_kernel_create_trace |
96243366 | 179 | * |
894be886 | 180 | * Create a kernel trace. |
96243366 | 181 | */ |
894be886 | 182 | static int process_kernel_create_trace(void) |
96243366 | 183 | { |
894be886 DG |
184 | return 0; |
185 | } | |
96243366 | 186 | |
894be886 DG |
187 | /* |
188 | * process_kernel_event | |
189 | * | |
190 | * Enable kernel event from the command line list given. | |
191 | */ | |
192 | static int process_opt_kernel_event(void) | |
193 | { | |
194 | int ret; | |
195 | char *event_name; | |
96243366 | 196 | |
894be886 DG |
197 | event_name = strtok(opt_event_list, ","); |
198 | while (event_name != NULL) { | |
199 | DBG("Enabling kernel event %s", event_name); | |
200 | ret = lttng_kernel_enable_event(event_name); | |
201 | if (ret < 0) { | |
202 | ERR("%s %s", lttng_get_readable_code(ret), event_name); | |
203 | } else { | |
204 | MSG("Kernel event %s enabled.", event_name); | |
96243366 | 205 | } |
894be886 DG |
206 | /* Next event */ |
207 | event_name = strtok(NULL, ","); | |
96243366 DG |
208 | } |
209 | ||
894be886 | 210 | return 0; |
96243366 DG |
211 | } |
212 | ||
213 | /* | |
894be886 | 214 | * set_opt_session_info |
96243366 | 215 | * |
894be886 DG |
216 | * Setup session_name, current_uuid, short_str_uuid and |
217 | * long_str_uuid using the command line options. | |
96243366 | 218 | */ |
894be886 | 219 | static void set_opt_session_info(void) |
96243366 | 220 | { |
894be886 DG |
221 | if (opt_session_name != NULL) { |
222 | session_name = strndup(opt_session_name, NAME_MAX); | |
223 | DBG("Session name set to %s", session_name); | |
224 | } | |
96243366 DG |
225 | } |
226 | ||
227 | /* | |
228 | * set_session_uuid | |
7442b2ba | 229 | * |
894be886 DG |
230 | * Set current session uuid to the current flow of command(s) using the |
231 | * session_name. | |
7442b2ba | 232 | */ |
96243366 | 233 | static int set_session_uuid(void) |
7442b2ba | 234 | { |
894be886 | 235 | int ret, count, i, found = 0; |
7442b2ba DG |
236 | struct lttng_session *sessions; |
237 | ||
96243366 DG |
238 | if (!uuid_is_null(current_uuid)) { |
239 | lttng_set_current_session_uuid(¤t_uuid); | |
240 | goto end; | |
241 | } | |
242 | ||
7442b2ba DG |
243 | count = lttng_list_sessions(&sessions); |
244 | if (count < 0) { | |
245 | ret = count; | |
246 | goto error; | |
247 | } | |
248 | ||
249 | for (i = 0; i < count; i++) { | |
894be886 | 250 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
96243366 | 251 | lttng_set_current_session_uuid(&sessions[i].uuid); |
894be886 DG |
252 | uuid_copy(current_uuid, sessions[i].uuid); |
253 | found = 1; | |
7442b2ba DG |
254 | break; |
255 | } | |
256 | } | |
257 | ||
258 | free(sessions); | |
259 | ||
894be886 DG |
260 | if (!found) { |
261 | return -1; | |
262 | } | |
263 | ||
96243366 | 264 | end: |
894be886 | 265 | DBG("Session UUID set"); |
7442b2ba DG |
266 | return 0; |
267 | ||
268 | error: | |
269 | return ret; | |
fac6795d DG |
270 | } |
271 | ||
1657e9bb DG |
272 | /* |
273 | * process_opt_list_traces | |
274 | * | |
275 | * Get list of all traces for a specific session uuid. | |
276 | */ | |
277 | static int process_opt_list_traces(void) | |
278 | { | |
279 | int ret, i; | |
1657e9bb DG |
280 | struct lttng_trace *traces; |
281 | ||
96243366 | 282 | ret = lttng_list_traces(¤t_uuid, &traces); |
894be886 | 283 | DBG("Number of traces to list %d", ret); |
1657e9bb DG |
284 | if (ret < 0) { |
285 | goto error; | |
286 | } | |
287 | ||
894be886 DG |
288 | /* No traces */ |
289 | if (ret == 0) { | |
290 | MSG("No traces found."); | |
291 | goto error; | |
292 | } | |
293 | ||
1657e9bb DG |
294 | MSG("Userspace traces:"); |
295 | for (i = 0; i < ret; i++) { | |
296 | if (traces[i].type == USERSPACE) { | |
47b74d63 DG |
297 | MSG("\t%d) %s (pid: %d): %s", |
298 | i, traces[i].name, traces[i].pid, | |
299 | get_cmdline_by_pid(traces[i].pid)); | |
1657e9bb DG |
300 | } else { |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | MSG("Kernel traces:"); | |
306 | for (;i < ret; i++) { | |
307 | if (traces[i].type == KERNEL) { | |
308 | MSG("\t%d) %s", i, traces[i].name); | |
309 | } | |
310 | } | |
311 | ||
312 | free(traces); | |
313 | ||
314 | error: | |
315 | return ret; | |
316 | } | |
317 | ||
aaf97519 DG |
318 | /* |
319 | * process_opt_create_session | |
320 | * | |
321 | * Create a new session using the name pass | |
322 | * to the command line. | |
323 | */ | |
324 | static int process_opt_create_session(void) | |
325 | { | |
326 | int ret; | |
96243366 DG |
327 | char name[NAME_MAX]; |
328 | time_t rawtime; | |
329 | struct tm *timeinfo; | |
330 | ||
894be886 DG |
331 | /* Auto session name creation */ |
332 | if (opt_session_name == NULL) { | |
96243366 DG |
333 | time(&rawtime); |
334 | timeinfo = localtime(&rawtime); | |
894be886 | 335 | strftime(name, sizeof(name), "auto-%Y%m%d-%H%M%S", timeinfo); |
96243366 | 336 | session_name = strndup(name, sizeof(name)); |
894be886 | 337 | DBG("Auto session name set to %s", session_name); |
96243366 | 338 | } |
aaf97519 | 339 | |
894be886 | 340 | ret = lttng_create_session(session_name); |
aaf97519 DG |
341 | if (ret < 0) { |
342 | goto error; | |
343 | } | |
344 | ||
894be886 | 345 | MSG("Session created: %s", session_name); |
aaf97519 DG |
346 | |
347 | error: | |
348 | return ret; | |
349 | } | |
350 | ||
57167058 DG |
351 | /* |
352 | * process_opt_list_sessions | |
353 | * | |
354 | * Get the list of available sessions from | |
355 | * the session daemon and print it to user. | |
356 | */ | |
357 | static int process_opt_list_sessions(void) | |
358 | { | |
359 | int ret, count, i; | |
894be886 | 360 | struct lttng_session *sessions; |
57167058 | 361 | |
894be886 | 362 | count = lttng_list_sessions(&sessions); |
7442b2ba | 363 | DBG("Session count %d", count); |
57167058 DG |
364 | if (count < 0) { |
365 | ret = count; | |
366 | goto error; | |
367 | } | |
368 | ||
7442b2ba | 369 | MSG("Available sessions (UUIDs):"); |
57167058 | 370 | for (i = 0; i < count; i++) { |
894be886 | 371 | MSG(" %d) %s", i+1, sessions[i].name); |
57167058 DG |
372 | } |
373 | ||
894be886 | 374 | free(sessions); |
7442b2ba | 375 | MSG("\nTo select a session, use -s, --session UUID."); |
57167058 DG |
376 | |
377 | return 0; | |
378 | ||
379 | error: | |
380 | return ret; | |
381 | } | |
382 | ||
fac6795d DG |
383 | /* |
384 | * process_opt_list_apps | |
385 | * | |
386 | * Get the UST traceable pid list and print | |
387 | * them to the user. | |
388 | */ | |
389 | static int process_opt_list_apps(void) | |
390 | { | |
e8f07c63 | 391 | int i, ret, count; |
fac6795d | 392 | pid_t *pids; |
1c9f7941 | 393 | char *cmdline; |
fac6795d | 394 | |
e8f07c63 DG |
395 | count = lttng_ust_list_apps(&pids); |
396 | if (count < 0) { | |
397 | ret = count; | |
fac6795d DG |
398 | goto error; |
399 | } | |
400 | ||
401 | MSG("LTTng UST traceable application [name (pid)]:"); | |
e8f07c63 | 402 | for (i=0; i < count; i++) { |
47b74d63 DG |
403 | cmdline = get_cmdline_by_pid(pids[i]); |
404 | if (cmdline == NULL) { | |
e8f07c63 | 405 | MSG("\t(not running) (%d)", pids[i]); |
fac6795d DG |
406 | continue; |
407 | } | |
fac6795d | 408 | MSG("\t%s (%d)", cmdline, pids[i]); |
1c9f7941 | 409 | free(cmdline); |
fac6795d DG |
410 | } |
411 | ||
e065084a DG |
412 | /* Allocated by lttng_ust_list_apps() */ |
413 | free(pids); | |
414 | ||
fac6795d DG |
415 | return 0; |
416 | ||
417 | error: | |
418 | return ret; | |
419 | } | |
420 | ||
1c9f7941 DG |
421 | /* |
422 | * get_cmdline_by_pid | |
423 | * | |
47b74d63 | 424 | * Get command line from /proc for a specific pid. |
1c9f7941 | 425 | * |
47b74d63 DG |
426 | * On success, return an allocated string pointer pointing to |
427 | * the proc cmdline. | |
428 | * On error, return NULL. | |
1c9f7941 | 429 | */ |
47b74d63 | 430 | static char *get_cmdline_by_pid(pid_t pid) |
1c9f7941 DG |
431 | { |
432 | int ret; | |
433 | FILE *fp; | |
47b74d63 | 434 | char *cmdline = NULL; |
1c9f7941 DG |
435 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ |
436 | ||
437 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); | |
438 | fp = fopen(path, "r"); | |
439 | if (fp == NULL) { | |
47b74d63 | 440 | goto end; |
1c9f7941 DG |
441 | } |
442 | ||
443 | /* Caller must free() *cmdline */ | |
47b74d63 DG |
444 | cmdline = malloc(PATH_MAX); |
445 | ret = fread(cmdline, 1, PATH_MAX, fp); | |
1c9f7941 DG |
446 | fclose(fp); |
447 | ||
47b74d63 DG |
448 | end: |
449 | return cmdline; | |
1c9f7941 DG |
450 | } |
451 | ||
8548ff30 DG |
452 | /* |
453 | * validate_options | |
454 | * | |
894be886 DG |
455 | * Make sure that all options passed to the command line are compatible with |
456 | * each others. | |
8548ff30 DG |
457 | * |
458 | * On error, return -1 | |
459 | * On success, return 0 | |
460 | */ | |
461 | static int validate_options(void) | |
462 | { | |
894be886 DG |
463 | /* If listing options, jump validation */ |
464 | if (opt_list_apps || opt_list_session) { | |
465 | goto end; | |
466 | } | |
ad874cce DG |
467 | /* Conflicting command */ |
468 | if (opt_start_trace && opt_stop_trace) { | |
469 | ERR("Can't use --start and --stop together."); | |
470 | goto error; | |
ad874cce | 471 | /* If no PID specified and trace_kernel is off */ |
894be886 DG |
472 | } else if ((opt_trace_pid == 0 && !opt_trace_kernel) && |
473 | (opt_create_trace || opt_start_trace || opt_stop_trace || opt_destroy_trace)) { | |
474 | ERR("Please specify for which tracer (-k or -p PID)."); | |
96243366 | 475 | goto error; |
894be886 DG |
476 | /* List traces, we need a session name */ |
477 | } else if (opt_list_traces && opt_session_name == NULL) { | |
96243366 DG |
478 | ERR("Can't use -t without -s, --session option."); |
479 | goto error; | |
894be886 DG |
480 | /* Can't set event for both kernel and userspace at the same time */ |
481 | } else if (opt_event_list != NULL && (opt_trace_kernel && opt_trace_pid)) { | |
482 | ERR("Please don't use --event for both kernel and userspace.\nOne at a time to enable events."); | |
483 | goto error; | |
484 | /* Don't need a trace name for kernel tracig */ | |
485 | } else if (opt_trace_name != NULL && opt_trace_kernel) { | |
486 | ERR("For action on a kernel trace, please don't specify a trace name."); | |
487 | goto error; | |
488 | } else if (opt_destroy_trace && opt_session_name == NULL) { | |
489 | ERR("Please specify a session in order to destroy a trace"); | |
490 | goto error; | |
491 | } else if (opt_create_trace || opt_destroy_trace) { | |
492 | /* Both kernel and user-space are denied for these options */ | |
493 | if (opt_trace_pid != 0 && opt_trace_kernel) { | |
494 | ERR("Kernel and user-space trace creation and destruction can't be used together."); | |
495 | goto error; | |
496 | /* Need a trace name for user-space tracing */ | |
497 | } else if (opt_trace_name == NULL && opt_trace_pid != 0) { | |
498 | ERR("Please specify a trace name for user-space tracing"); | |
499 | goto error; | |
500 | } | |
501 | } else if (opt_stop_trace && opt_trace_pid != 0 && opt_trace_name == NULL) { | |
502 | ERR("Please specify a trace name for user-space tracing"); | |
503 | goto error; | |
7442b2ba DG |
504 | } |
505 | ||
894be886 DG |
506 | /* If start trace, auto start tracing */ |
507 | if (opt_start_trace) { | |
508 | DBG("Requesting auto tracing"); | |
509 | auto_trace = 1; | |
510 | } | |
511 | ||
512 | /* If no session, auto create one */ | |
513 | if (opt_session_name == NULL) { | |
514 | DBG("Requesting an auto session creation"); | |
515 | auto_session = 1; | |
516 | } | |
517 | ||
518 | end: | |
8548ff30 DG |
519 | return 0; |
520 | ||
521 | error: | |
522 | return -1; | |
523 | } | |
524 | ||
5b8719f5 DG |
525 | /* |
526 | * spawn_sessiond | |
527 | * | |
528 | * Spawn a session daemon by forking and execv. | |
529 | */ | |
530 | static int spawn_sessiond(char *pathname) | |
531 | { | |
532 | int ret = 0; | |
533 | pid_t pid; | |
534 | ||
535 | MSG("Spawning session daemon"); | |
536 | pid = fork(); | |
537 | if (pid == 0) { | |
5e16da05 MD |
538 | /* |
539 | * Spawn session daemon and tell | |
5b8719f5 DG |
540 | * it to signal us when ready. |
541 | */ | |
5e16da05 MD |
542 | execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
543 | /* execlp only returns if error happened */ | |
544 | if (errno == ENOENT) { | |
545 | ERR("No session daemon found. Use --sessiond-path."); | |
546 | } else { | |
547 | perror("execlp"); | |
5b8719f5 | 548 | } |
5e16da05 MD |
549 | kill(getppid(), SIGTERM); /* unpause parent */ |
550 | exit(EXIT_FAILURE); | |
5b8719f5 DG |
551 | } else if (pid > 0) { |
552 | /* Wait for ltt-sessiond to start */ | |
553 | pause(); | |
554 | goto end; | |
555 | } else { | |
556 | perror("fork"); | |
557 | ret = -1; | |
558 | goto end; | |
559 | } | |
560 | ||
561 | end: | |
562 | return ret; | |
563 | } | |
564 | ||
fac6795d DG |
565 | /* |
566 | * check_ltt_sessiond | |
567 | * | |
568 | * Check if the session daemon is available using | |
5b8719f5 DG |
569 | * the liblttngctl API for the check. If not, try to |
570 | * spawn a daemon. | |
fac6795d DG |
571 | */ |
572 | static int check_ltt_sessiond(void) | |
573 | { | |
574 | int ret; | |
5e16da05 | 575 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d DG |
576 | |
577 | ret = lttng_check_session_daemon(); | |
578 | if (ret < 0) { | |
5b8719f5 DG |
579 | /* Try command line option path */ |
580 | if (opt_sessiond_path != NULL) { | |
581 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
582 | if (ret < 0) { | |
583 | ERR("No such file: %s", opt_sessiond_path); | |
584 | goto end; | |
585 | } | |
586 | pathname = opt_sessiond_path; | |
587 | } else { | |
588 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 | 589 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
590 | } |
591 | ||
592 | /* Let's rock and roll */ | |
593 | if (pathname == NULL) { | |
5e16da05 | 594 | ret = asprintf(&alloc_pathname, "ltt-sessiond"); |
5b8719f5 DG |
595 | if (ret < 0) { |
596 | goto end; | |
597 | } | |
5e16da05 | 598 | pathname = alloc_pathname; |
5b8719f5 DG |
599 | } |
600 | ||
601 | ret = spawn_sessiond(pathname); | |
5e16da05 | 602 | free(alloc_pathname); |
5b8719f5 DG |
603 | if (ret < 0) { |
604 | ERR("Problem occurs when starting %s", pathname); | |
605 | goto end; | |
606 | } | |
fac6795d DG |
607 | } |
608 | ||
5b8719f5 | 609 | end: |
fac6795d DG |
610 | return ret; |
611 | } | |
612 | ||
5b8719f5 DG |
613 | /* |
614 | * set_signal_handler | |
615 | * | |
616 | * Setup signal handler for SIGCHLD and SIGTERM. | |
617 | */ | |
618 | static int set_signal_handler(void) | |
619 | { | |
620 | int ret = 0; | |
621 | struct sigaction sa; | |
622 | sigset_t sigset; | |
623 | ||
624 | if ((ret = sigemptyset(&sigset)) < 0) { | |
625 | perror("sigemptyset"); | |
626 | goto end; | |
627 | } | |
628 | ||
629 | sa.sa_handler = sighandler; | |
630 | sa.sa_mask = sigset; | |
631 | sa.sa_flags = 0; | |
632 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
633 | perror("sigaction"); | |
634 | goto end; | |
635 | } | |
fac6795d | 636 | |
5b8719f5 DG |
637 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
638 | perror("sigaction"); | |
639 | goto end; | |
640 | } | |
641 | ||
642 | end: | |
643 | return ret; | |
644 | } | |
645 | ||
646 | /* | |
647 | * sighandler | |
648 | * | |
649 | * Signal handler for the daemon | |
650 | */ | |
651 | static void sighandler(int sig) | |
652 | { | |
5b8719f5 DG |
653 | switch (sig) { |
654 | case SIGTERM: | |
ce3d728c | 655 | DBG("SIGTERM catched"); |
5b8719f5 DG |
656 | clean_exit(EXIT_FAILURE); |
657 | break; | |
658 | case SIGCHLD: | |
659 | /* Notify is done */ | |
ce3d728c | 660 | DBG("SIGCHLD catched"); |
5b8719f5 DG |
661 | break; |
662 | default: | |
ce3d728c | 663 | DBG("Unknown signal %d catched", sig); |
5b8719f5 DG |
664 | break; |
665 | } | |
666 | ||
667 | return; | |
668 | } | |
7442b2ba | 669 | |
fac6795d DG |
670 | /* |
671 | * clean_exit | |
672 | */ | |
673 | void clean_exit(int code) | |
674 | { | |
675 | DBG("Clean exit"); | |
96243366 DG |
676 | if (session_name) { |
677 | free(session_name); | |
678 | } | |
679 | ||
fac6795d DG |
680 | exit(code); |
681 | } | |
682 | ||
683 | /* | |
5b8719f5 | 684 | * main |
fac6795d DG |
685 | */ |
686 | int main(int argc, char *argv[]) | |
687 | { | |
688 | int ret; | |
689 | ||
690 | progname = argv[0] ? argv[0] : "lttng"; | |
691 | ||
692 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
693 | if (strncmp(progname, "drtrace", 7) == 0) { | |
694 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
695 | } | |
696 | ||
697 | ret = parse_args(argc, (const char **) argv); | |
698 | if (ret < 0) { | |
87378cf5 | 699 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
700 | } |
701 | ||
8548ff30 DG |
702 | ret = validate_options(); |
703 | if (ret < 0) { | |
704 | return EXIT_FAILURE; | |
705 | } | |
706 | ||
5b8719f5 DG |
707 | ret = set_signal_handler(); |
708 | if (ret < 0) { | |
87378cf5 | 709 | clean_exit(ret); |
5b8719f5 DG |
710 | } |
711 | ||
fac6795d DG |
712 | if (opt_tracing_group != NULL) { |
713 | DBG("Set tracing group to '%s'", opt_tracing_group); | |
714 | lttng_set_tracing_group(opt_tracing_group); | |
715 | } | |
716 | ||
717 | /* If ask for kernel tracing, need root perms */ | |
718 | if (opt_trace_kernel) { | |
719 | DBG("Kernel tracing activated"); | |
720 | if (getuid() != 0) { | |
721 | ERR("%s must be setuid root", progname); | |
87378cf5 | 722 | clean_exit(-EPERM); |
fac6795d DG |
723 | } |
724 | } | |
725 | ||
726 | /* Check if the lttng session daemon is running. | |
727 | * If no, a daemon will be spawned. | |
728 | */ | |
5b8719f5 | 729 | if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { |
87378cf5 | 730 | clean_exit(EXIT_FAILURE); |
fac6795d DG |
731 | } |
732 | ||
733 | ret = process_client_opt(); | |
734 | if (ret < 0) { | |
87378cf5 | 735 | clean_exit(ret); |
fac6795d DG |
736 | } |
737 | ||
87378cf5 DG |
738 | clean_exit(0); |
739 | ||
fac6795d DG |
740 | return 0; |
741 | } |