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