Add lttngerr support to ltt-sessiond
[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;
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 ret = lttng_ust_list_apps(&pids);
132 if (ret < 0) {
133 goto error;
134 }
135
136 MSG("LTTng UST traceable application [name (pid)]:");
137 for (i=0; i < ret; i++) {
138 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
139 fp = fopen(path, "r");
140 if (fp == NULL) {
141 continue;
142 }
143 ret = fread(cmdline, 1, sizeof(cmdline), fp);
144 MSG("\t%s (%d)", cmdline, pids[i]);
145 fclose(fp);
146 }
147
148 /* Allocated by lttng_ust_list_apps() */
149 free(pids);
150
151 return 0;
152
153 error:
154 return ret;
155 }
156
157 /*
158 * spawn_sessiond
159 *
160 * Spawn a session daemon by forking and execv.
161 */
162 static int spawn_sessiond(char *pathname)
163 {
164 int ret = 0;
165 pid_t pid;
166
167 MSG("Spawning session daemon");
168 pid = fork();
169 if (pid == 0) {
170 /* Spawn session daemon and tell
171 * it to signal us when ready.
172 */
173 ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
174 if (ret < 0) {
175 if (errno == ENOENT) {
176 ERR("No session daemon found. Use --sessiond-path.");
177 } else {
178 perror("execlp");
179 }
180 kill(getppid(), SIGTERM);
181 exit(EXIT_FAILURE);
182 }
183 exit(EXIT_SUCCESS);
184 } else if (pid > 0) {
185 /* Wait for ltt-sessiond to start */
186 pause();
187 goto end;
188 } else {
189 perror("fork");
190 ret = -1;
191 goto end;
192 }
193
194 end:
195 return ret;
196 }
197
198 /*
199 * check_ltt_sessiond
200 *
201 * Check if the session daemon is available using
202 * the liblttngctl API for the check. If not, try to
203 * spawn a daemon.
204 */
205 static int check_ltt_sessiond(void)
206 {
207 int ret;
208 char *pathname = NULL;
209
210 ret = lttng_check_session_daemon();
211 if (ret < 0) {
212 /* Try command line option path */
213 if (opt_sessiond_path != NULL) {
214 ret = access(opt_sessiond_path, F_OK | X_OK);
215 if (ret < 0) {
216 ERR("No such file: %s", opt_sessiond_path);
217 goto end;
218 }
219 pathname = opt_sessiond_path;
220 } else {
221 /* Try LTTNG_SESSIOND_PATH env variable */
222 pathname = strdup(getenv(LTTNG_SESSIOND_PATH_ENV));
223 }
224
225 /* Let's rock and roll */
226 if (pathname == NULL) {
227 ret = asprintf(&pathname, "ltt-sessiond");
228 if (ret < 0) {
229 goto end;
230 }
231 }
232
233 ret = spawn_sessiond(pathname);
234 free(pathname);
235 if (ret < 0) {
236 ERR("Problem occurs when starting %s", pathname);
237 goto end;
238 }
239 }
240
241 end:
242 return ret;
243 }
244
245 /*
246 * set_signal_handler
247 *
248 * Setup signal handler for SIGCHLD and SIGTERM.
249 */
250 static int set_signal_handler(void)
251 {
252 int ret = 0;
253 struct sigaction sa;
254 sigset_t sigset;
255
256 if ((ret = sigemptyset(&sigset)) < 0) {
257 perror("sigemptyset");
258 goto end;
259 }
260
261 sa.sa_handler = sighandler;
262 sa.sa_mask = sigset;
263 sa.sa_flags = 0;
264 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
265 perror("sigaction");
266 goto end;
267 }
268
269 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
270 perror("sigaction");
271 goto end;
272 }
273
274 end:
275 return ret;
276 }
277
278 /*
279 * sighandler
280 *
281 * Signal handler for the daemon
282 */
283 static void sighandler(int sig)
284 {
285 DBG("%d received", sig);
286 switch (sig) {
287 case SIGTERM:
288 clean_exit(EXIT_FAILURE);
289 break;
290 case SIGCHLD:
291 /* Notify is done */
292 break;
293 default:
294 break;
295 }
296
297 return;
298 }
299 /*
300 * clean_exit
301 */
302 void clean_exit(int code)
303 {
304 DBG("Clean exit");
305 exit(code);
306 }
307
308 /*
309 * main
310 */
311 int main(int argc, char *argv[])
312 {
313 int ret;
314
315 progname = argv[0] ? argv[0] : "lttng";
316
317 /* For Mathieu Desnoyers aka Dr Tracing */
318 if (strncmp(progname, "drtrace", 7) == 0) {
319 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
320 }
321
322 ret = parse_args(argc, (const char **) argv);
323 if (ret < 0) {
324 return EXIT_FAILURE;
325 }
326
327 ret = set_signal_handler();
328 if (ret < 0) {
329 return ret;
330 }
331
332 if (opt_tracing_group != NULL) {
333 DBG("Set tracing group to '%s'", opt_tracing_group);
334 lttng_set_tracing_group(opt_tracing_group);
335 }
336
337 /* If ask for kernel tracing, need root perms */
338 if (opt_trace_kernel) {
339 DBG("Kernel tracing activated");
340 if (getuid() != 0) {
341 ERR("%s must be setuid root", progname);
342 return -EPERM;
343 }
344 }
345
346 /* Check if the lttng session daemon is running.
347 * If no, a daemon will be spawned.
348 */
349 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
350 return EXIT_FAILURE;
351 }
352
353 ret = process_client_opt();
354 if (ret < 0) {
355 return ret;
356 }
357
358 return 0;
359 }
This page took 0.035761 seconds and 4 git commands to generate.