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