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