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