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