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