Add destroy session feature
[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
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 */
56 static int process_client_opt(void)
57 {
58 int ret;
59 uuid_t uuid;
60
61 /* Connect to the session daemon */
62 ret = lttng_connect_sessiond();
63 if (ret < 0) {
64 goto end;
65 }
66
67 if (opt_list_apps) {
68 ret = process_opt_list_apps();
69 if (ret < 0) {
70 goto end;
71 }
72 }
73
74 if (opt_list_session) {
75 ret = process_opt_list_sessions();
76 if (ret < 0) {
77 goto end;
78 }
79 }
80
81 if (opt_create_session != NULL) {
82 ret = process_opt_create_session();
83 if (ret < 0) {
84 goto end;
85 }
86 }
87
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
96 return 0;
97
98 end:
99 ERR("%s", lttng_get_readable_code(ret));
100 return ret;
101 }
102
103 /*
104 * process_opt_create_session
105 *
106 * Create a new session using the name pass
107 * to the command line.
108 */
109 static int process_opt_create_session(void)
110 {
111 int ret;
112 uuid_t session_id;
113 char str_uuid[37];
114
115 ret = lttng_create_session(opt_create_session, &session_id);
116 if (ret < 0) {
117 goto error;
118 }
119
120 uuid_unparse(session_id, str_uuid);
121
122 MSG("Session created:");
123 MSG(" %s (%s)", opt_create_session, str_uuid);
124
125 error:
126 return ret;
127 }
128
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 */
135 static 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
156 error:
157 return ret;
158 }
159
160 /*
161 * process_opt_list_apps
162 *
163 * Get the UST traceable pid list and print
164 * them to the user.
165 */
166 static int process_opt_list_apps(void)
167 {
168 int i, ret, count;
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
174 count = lttng_ust_list_apps(&pids);
175 if (count < 0) {
176 ret = count;
177 goto error;
178 }
179
180 MSG("LTTng UST traceable application [name (pid)]:");
181 for (i=0; i < count; i++) {
182 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
183 fp = fopen(path, "r");
184 if (fp == NULL) {
185 MSG("\t(not running) (%d)", pids[i]);
186 continue;
187 }
188 ret = fread(cmdline, 1, sizeof(cmdline), fp);
189 MSG("\t%s (%d)", cmdline, pids[i]);
190 fclose(fp);
191 }
192
193 /* Allocated by lttng_ust_list_apps() */
194 free(pids);
195
196 return 0;
197
198 error:
199 return ret;
200 }
201
202 /*
203 * spawn_sessiond
204 *
205 * Spawn a session daemon by forking and execv.
206 */
207 static 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 */
218 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
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
239 end:
240 return ret;
241 }
242
243 /*
244 * check_ltt_sessiond
245 *
246 * Check if the session daemon is available using
247 * the liblttngctl API for the check. If not, try to
248 * spawn a daemon.
249 */
250 static int check_ltt_sessiond(void)
251 {
252 int ret;
253 char *pathname = NULL;
254
255 ret = lttng_check_session_daemon();
256 if (ret < 0) {
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 */
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 }
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 }
290 }
291
292 end:
293 return ret;
294 }
295
296 /*
297 * set_signal_handler
298 *
299 * Setup signal handler for SIGCHLD and SIGTERM.
300 */
301 static 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 }
319
320 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
321 perror("sigaction");
322 goto end;
323 }
324
325 end:
326 return ret;
327 }
328
329 /*
330 * sighandler
331 *
332 * Signal handler for the daemon
333 */
334 static 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 }
350 /*
351 * clean_exit
352 */
353 void clean_exit(int code)
354 {
355 DBG("Clean exit");
356 exit(code);
357 }
358
359 /*
360 * main
361 */
362 int 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
378 ret = set_signal_handler();
379 if (ret < 0) {
380 return ret;
381 }
382
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 */
400 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
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.037647 seconds and 5 git commands to generate.