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