Fix segfault for ltt-sessiond pathname
[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 void sighandler(int sig);
46 static int set_signal_handler(void);
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 */
55 static 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) {
62 goto end;
63 }
64
65 if (opt_list_apps) {
66 ret = process_opt_list_apps();
67 if (ret < 0) {
68 goto end;
69 }
70 }
71
72 if (opt_list_session) {
73 ret = process_opt_list_sessions();
74 if (ret < 0) {
75 goto end;
76 }
77 }
78
79 return 0;
80
81 end:
82 ERR("%s", lttng_get_readable_code(ret));
83 return ret;
84 }
85
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 */
92 static 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
113 error:
114 return ret;
115 }
116
117 /*
118 * process_opt_list_apps
119 *
120 * Get the UST traceable pid list and print
121 * them to the user.
122 */
123 static int process_opt_list_apps(void)
124 {
125 int i, ret, count;
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
131 count = lttng_ust_list_apps(&pids);
132 if (count < 0) {
133 ret = count;
134 goto error;
135 }
136
137 MSG("LTTng UST traceable application [name (pid)]:");
138 for (i=0; i < count; i++) {
139 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
140 fp = fopen(path, "r");
141 if (fp == NULL) {
142 MSG("\t(not running) (%d)", pids[i]);
143 continue;
144 }
145 ret = fread(cmdline, 1, sizeof(cmdline), fp);
146 MSG("\t%s (%d)", cmdline, pids[i]);
147 fclose(fp);
148 }
149
150 /* Allocated by lttng_ust_list_apps() */
151 free(pids);
152
153 return 0;
154
155 error:
156 return ret;
157 }
158
159 /*
160 * spawn_sessiond
161 *
162 * Spawn a session daemon by forking and execv.
163 */
164 static 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 */
175 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
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
196 end:
197 return ret;
198 }
199
200 /*
201 * check_ltt_sessiond
202 *
203 * Check if the session daemon is available using
204 * the liblttngctl API for the check. If not, try to
205 * spawn a daemon.
206 */
207 static int check_ltt_sessiond(void)
208 {
209 int ret;
210 char *pathname = NULL;
211
212 ret = lttng_check_session_daemon();
213 if (ret < 0) {
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 */
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 }
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 }
247 }
248
249 end:
250 return ret;
251 }
252
253 /*
254 * set_signal_handler
255 *
256 * Setup signal handler for SIGCHLD and SIGTERM.
257 */
258 static 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 }
276
277 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
278 perror("sigaction");
279 goto end;
280 }
281
282 end:
283 return ret;
284 }
285
286 /*
287 * sighandler
288 *
289 * Signal handler for the daemon
290 */
291 static 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 }
307 /*
308 * clean_exit
309 */
310 void clean_exit(int code)
311 {
312 DBG("Clean exit");
313 exit(code);
314 }
315
316 /*
317 * main
318 */
319 int 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
335 ret = set_signal_handler();
336 if (ret < 0) {
337 return ret;
338 }
339
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 */
357 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
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.055375 seconds and 4 git commands to generate.