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