Fix bug when finding session by name
[lttng-tools.git] / lttng / lttng.c
CommitLineData
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 */
39static char *progname;
40
41/* Prototypes */
42static int process_client_opt(void);
43static int process_opt_list_apps(void);
57167058 44static int process_opt_list_sessions(void);
1657e9bb 45static int process_opt_list_traces(void);
aaf97519 46static int process_opt_create_session(void);
5b8719f5
DG
47static void sighandler(int sig);
48static int set_signal_handler(void);
8548ff30 49static int validate_options(void);
47b74d63 50static char *get_cmdline_by_pid(pid_t pid);
fac6795d
DG
51
52/*
53 * start_client
54 *
55 * Process client request from the command line
56 * options. Every tracing action is done by the
57 * liblttngctl API.
58 */
59static int process_client_opt(void)
60{
61 int ret;
8028d920 62 uuid_t uuid;
fac6795d
DG
63
64 /* Connect to the session daemon */
65 ret = lttng_connect_sessiond();
66 if (ret < 0) {
fac6795d
DG
67 goto end;
68 }
69
70 if (opt_list_apps) {
71 ret = process_opt_list_apps();
72 if (ret < 0) {
fac6795d
DG
73 goto end;
74 }
75 }
76
57167058
DG
77 if (opt_list_session) {
78 ret = process_opt_list_sessions();
79 if (ret < 0) {
80 goto end;
81 }
82 }
83
1657e9bb
DG
84 if (opt_list_traces) {
85 ret = process_opt_list_traces();
86 if (ret < 0) {
87 goto end;
88 }
89 }
90
aaf97519
DG
91 if (opt_create_session != NULL) {
92 ret = process_opt_create_session();
93 if (ret < 0) {
94 goto end;
95 }
96 }
97
8028d920
DG
98 if (opt_destroy_session != NULL) {
99 uuid_parse(opt_destroy_session, uuid);
100 ret = lttng_destroy_session(&uuid);
101 if (ret < 0) {
102 goto end;
103 }
104 }
105
e8be5f4f 106 if (opt_session_uuid != NULL) {
ce3d728c 107 DBG("Set session uuid to %s", opt_session_uuid);
e8be5f4f
DG
108 lttng_set_current_session_uuid(opt_session_uuid);
109 }
110
ad874cce
DG
111 if (opt_trace_kernel) {
112 ERR("Not implemented yet");
113 goto end;
df0da139
DG
114 }
115
ad874cce
DG
116 if (opt_trace_pid != 0) {
117 if (opt_create_trace) {
118 DBG("Create a userspace trace for pid %d", opt_trace_pid);
119 ret = lttng_ust_create_trace(opt_trace_pid);
120 if (ret < 0) {
121 goto end;
122 }
123 MSG("Trace created successfully!\nUse --start to start tracing.");
ce3d728c 124 }
ce3d728c 125
ad874cce
DG
126 if (opt_start_trace) {
127 DBG("Start trace for pid %d", opt_trace_pid);
128 ret = lttng_ust_start_trace(opt_trace_pid);
129 if (ret < 0) {
130 goto end;
131 }
132 MSG("Trace started successfully!");
133 } else if (opt_stop_trace) {
134 DBG("Stop trace for pid %d", opt_trace_pid);
135 ret = lttng_ust_stop_trace(opt_trace_pid);
136 if (ret < 0) {
137 goto end;
138 }
139 MSG("Trace stopped successfully!");
520ff687 140 }
ad874cce 141
520ff687
DG
142 }
143
fac6795d
DG
144 return 0;
145
146end:
ebafd2a5 147 ERR("%s", lttng_get_readable_code(ret));
fac6795d
DG
148 return ret;
149}
150
1657e9bb
DG
151/*
152 * process_opt_list_traces
153 *
154 * Get list of all traces for a specific session uuid.
155 */
156static int process_opt_list_traces(void)
157{
158 int ret, i;
159 uuid_t uuid;
160 struct lttng_trace *traces;
161
162 uuid_parse(opt_session_uuid, uuid);
163 ret = lttng_list_traces(&uuid, &traces);
164 if (ret < 0) {
165 goto error;
166 }
167
168 MSG("Userspace traces:");
169 for (i = 0; i < ret; i++) {
170 if (traces[i].type == USERSPACE) {
47b74d63
DG
171 MSG("\t%d) %s (pid: %d): %s",
172 i, traces[i].name, traces[i].pid,
173 get_cmdline_by_pid(traces[i].pid));
1657e9bb
DG
174 } else {
175 break;
176 }
177 }
178
179 MSG("Kernel traces:");
180 for (;i < ret; i++) {
181 if (traces[i].type == KERNEL) {
182 MSG("\t%d) %s", i, traces[i].name);
183 }
184 }
185
186 free(traces);
187
188error:
189 return ret;
190}
191
aaf97519
DG
192/*
193 * process_opt_create_session
194 *
195 * Create a new session using the name pass
196 * to the command line.
197 */
198static int process_opt_create_session(void)
199{
200 int ret;
8028d920
DG
201 uuid_t session_id;
202 char str_uuid[37];
aaf97519
DG
203
204 ret = lttng_create_session(opt_create_session, &session_id);
205 if (ret < 0) {
206 goto error;
207 }
208
8028d920
DG
209 uuid_unparse(session_id, str_uuid);
210
aaf97519 211 MSG("Session created:");
8028d920 212 MSG(" %s (%s)", opt_create_session, str_uuid);
aaf97519
DG
213
214error:
215 return ret;
216}
217
57167058
DG
218/*
219 * process_opt_list_sessions
220 *
221 * Get the list of available sessions from
222 * the session daemon and print it to user.
223 */
224static int process_opt_list_sessions(void)
225{
226 int ret, count, i;
227 struct lttng_session *sess;
228
229 count = lttng_list_sessions(&sess);
230 if (count < 0) {
231 ret = count;
232 goto error;
233 }
234
235 MSG("Available sessions [Name (uuid)]:");
236 for (i = 0; i < count; i++) {
237 MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid);
238 }
239
240 free(sess);
241 MSG("\nTo select a session, use --session UUID.");
242
243 return 0;
244
245error:
246 return ret;
247}
248
fac6795d
DG
249/*
250 * process_opt_list_apps
251 *
252 * Get the UST traceable pid list and print
253 * them to the user.
254 */
255static int process_opt_list_apps(void)
256{
e8f07c63 257 int i, ret, count;
fac6795d 258 pid_t *pids;
1c9f7941 259 char *cmdline;
fac6795d 260
e8f07c63
DG
261 count = lttng_ust_list_apps(&pids);
262 if (count < 0) {
263 ret = count;
fac6795d
DG
264 goto error;
265 }
266
267 MSG("LTTng UST traceable application [name (pid)]:");
e8f07c63 268 for (i=0; i < count; i++) {
47b74d63
DG
269 cmdline = get_cmdline_by_pid(pids[i]);
270 if (cmdline == NULL) {
e8f07c63 271 MSG("\t(not running) (%d)", pids[i]);
fac6795d
DG
272 continue;
273 }
fac6795d 274 MSG("\t%s (%d)", cmdline, pids[i]);
1c9f7941 275 free(cmdline);
fac6795d
DG
276 }
277
e065084a
DG
278 /* Allocated by lttng_ust_list_apps() */
279 free(pids);
280
fac6795d
DG
281 return 0;
282
283error:
284 return ret;
285}
286
1c9f7941
DG
287/*
288 * get_cmdline_by_pid
289 *
47b74d63 290 * Get command line from /proc for a specific pid.
1c9f7941 291 *
47b74d63
DG
292 * On success, return an allocated string pointer pointing to
293 * the proc cmdline.
294 * On error, return NULL.
1c9f7941 295 */
47b74d63 296static char *get_cmdline_by_pid(pid_t pid)
1c9f7941
DG
297{
298 int ret;
299 FILE *fp;
47b74d63 300 char *cmdline = NULL;
1c9f7941
DG
301 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
302
303 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
304 fp = fopen(path, "r");
305 if (fp == NULL) {
47b74d63 306 goto end;
1c9f7941
DG
307 }
308
309 /* Caller must free() *cmdline */
47b74d63
DG
310 cmdline = malloc(PATH_MAX);
311 ret = fread(cmdline, 1, PATH_MAX, fp);
1c9f7941
DG
312 fclose(fp);
313
47b74d63
DG
314end:
315 return cmdline;
1c9f7941
DG
316}
317
8548ff30
DG
318/*
319 * validate_options
320 *
321 * Make sure that all options passed to the command line
322 * are compatible with each others.
323 *
324 * On error, return -1
325 * On success, return 0
326 */
327static int validate_options(void)
328{
ad874cce
DG
329 /* Conflicting command */
330 if (opt_start_trace && opt_stop_trace) {
331 ERR("Can't use --start and --stop together.");
332 goto error;
333 /* Must have a session UUID for trace action. */
334 } else if ((opt_session_uuid == NULL) &&
335 (opt_create_trace || opt_start_trace || opt_stop_trace || opt_list_traces)) {
1657e9bb 336 ERR("You need to specify a session UUID.\nPlease use --session UUID to do so.");
8548ff30 337 goto error;
ad874cce
DG
338 /* If no PID specified and trace_kernel is off */
339 } else if ((opt_trace_pid == 0 && opt_trace_kernel == 0) &&
340 (opt_create_trace || opt_start_trace || opt_stop_trace)) {
341 ERR("Please specify a PID using -p, --pid PID.");
342 goto error;
8548ff30
DG
343 }
344
345 return 0;
346
347error:
348 return -1;
349}
350
5b8719f5
DG
351/*
352 * spawn_sessiond
353 *
354 * Spawn a session daemon by forking and execv.
355 */
356static int spawn_sessiond(char *pathname)
357{
358 int ret = 0;
359 pid_t pid;
360
361 MSG("Spawning session daemon");
362 pid = fork();
363 if (pid == 0) {
364 /* Spawn session daemon and tell
365 * it to signal us when ready.
366 */
75462a81 367 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
5b8719f5
DG
368 if (ret < 0) {
369 if (errno == ENOENT) {
370 ERR("No session daemon found. Use --sessiond-path.");
371 } else {
372 perror("execlp");
373 }
374 kill(getppid(), SIGTERM);
375 exit(EXIT_FAILURE);
376 }
377 exit(EXIT_SUCCESS);
378 } else if (pid > 0) {
379 /* Wait for ltt-sessiond to start */
380 pause();
381 goto end;
382 } else {
383 perror("fork");
384 ret = -1;
385 goto end;
386 }
387
388end:
389 return ret;
390}
391
fac6795d
DG
392/*
393 * check_ltt_sessiond
394 *
395 * Check if the session daemon is available using
5b8719f5
DG
396 * the liblttngctl API for the check. If not, try to
397 * spawn a daemon.
fac6795d
DG
398 */
399static int check_ltt_sessiond(void)
400{
401 int ret;
5b8719f5 402 char *pathname = NULL;
fac6795d
DG
403
404 ret = lttng_check_session_daemon();
405 if (ret < 0) {
5b8719f5
DG
406 /* Try command line option path */
407 if (opt_sessiond_path != NULL) {
408 ret = access(opt_sessiond_path, F_OK | X_OK);
409 if (ret < 0) {
410 ERR("No such file: %s", opt_sessiond_path);
411 goto end;
412 }
413 pathname = opt_sessiond_path;
414 } else {
415 /* Try LTTNG_SESSIOND_PATH env variable */
e8f07c63
DG
416 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
417 if (pathname != NULL) {
418 /* strdup here in order to make the free()
419 * not fail later on.
420 */
421 pathname = strdup(pathname);
422 }
5b8719f5
DG
423 }
424
425 /* Let's rock and roll */
426 if (pathname == NULL) {
427 ret = asprintf(&pathname, "ltt-sessiond");
428 if (ret < 0) {
429 goto end;
430 }
431 }
432
433 ret = spawn_sessiond(pathname);
434 free(pathname);
435 if (ret < 0) {
436 ERR("Problem occurs when starting %s", pathname);
437 goto end;
438 }
fac6795d
DG
439 }
440
5b8719f5 441end:
fac6795d
DG
442 return ret;
443}
444
5b8719f5
DG
445/*
446 * set_signal_handler
447 *
448 * Setup signal handler for SIGCHLD and SIGTERM.
449 */
450static int set_signal_handler(void)
451{
452 int ret = 0;
453 struct sigaction sa;
454 sigset_t sigset;
455
456 if ((ret = sigemptyset(&sigset)) < 0) {
457 perror("sigemptyset");
458 goto end;
459 }
460
461 sa.sa_handler = sighandler;
462 sa.sa_mask = sigset;
463 sa.sa_flags = 0;
464 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
465 perror("sigaction");
466 goto end;
467 }
fac6795d 468
5b8719f5
DG
469 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
470 perror("sigaction");
471 goto end;
472 }
473
474end:
475 return ret;
476}
477
478/*
479 * sighandler
480 *
481 * Signal handler for the daemon
482 */
483static void sighandler(int sig)
484{
5b8719f5
DG
485 switch (sig) {
486 case SIGTERM:
ce3d728c 487 DBG("SIGTERM catched");
5b8719f5
DG
488 clean_exit(EXIT_FAILURE);
489 break;
490 case SIGCHLD:
491 /* Notify is done */
ce3d728c 492 DBG("SIGCHLD catched");
5b8719f5
DG
493 break;
494 default:
ce3d728c 495 DBG("Unknown signal %d catched", sig);
5b8719f5
DG
496 break;
497 }
498
499 return;
500}
fac6795d
DG
501/*
502 * clean_exit
503 */
504void clean_exit(int code)
505{
506 DBG("Clean exit");
87378cf5
DG
507 if (lttng_disconnect_sessiond() < 0) {
508 ERR("Session daemon disconnect failed.");
509 }
fac6795d
DG
510 exit(code);
511}
512
513/*
5b8719f5 514 * main
fac6795d
DG
515 */
516int main(int argc, char *argv[])
517{
518 int ret;
519
520 progname = argv[0] ? argv[0] : "lttng";
521
522 /* For Mathieu Desnoyers aka Dr Tracing */
523 if (strncmp(progname, "drtrace", 7) == 0) {
524 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
525 }
526
527 ret = parse_args(argc, (const char **) argv);
528 if (ret < 0) {
87378cf5 529 clean_exit(EXIT_FAILURE);
fac6795d
DG
530 }
531
8548ff30
DG
532 ret = validate_options();
533 if (ret < 0) {
534 return EXIT_FAILURE;
535 }
536
5b8719f5
DG
537 ret = set_signal_handler();
538 if (ret < 0) {
87378cf5 539 clean_exit(ret);
5b8719f5
DG
540 }
541
fac6795d
DG
542 if (opt_tracing_group != NULL) {
543 DBG("Set tracing group to '%s'", opt_tracing_group);
544 lttng_set_tracing_group(opt_tracing_group);
545 }
546
547 /* If ask for kernel tracing, need root perms */
548 if (opt_trace_kernel) {
549 DBG("Kernel tracing activated");
550 if (getuid() != 0) {
551 ERR("%s must be setuid root", progname);
87378cf5 552 clean_exit(-EPERM);
fac6795d
DG
553 }
554 }
555
556 /* Check if the lttng session daemon is running.
557 * If no, a daemon will be spawned.
558 */
5b8719f5 559 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
87378cf5 560 clean_exit(EXIT_FAILURE);
fac6795d
DG
561 }
562
563 ret = process_client_opt();
564 if (ret < 0) {
87378cf5 565 clean_exit(ret);
fac6795d
DG
566 }
567
87378cf5
DG
568 clean_exit(0);
569
fac6795d
DG
570 return 0;
571}
This page took 0.047363 seconds and 5 git commands to generate.