Replace daemonize function by std glibc daemon()
[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);
5b8719f5
DG
44static void sighandler(int sig);
45static int set_signal_handler(void);
fac6795d
DG
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 */
54static 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) {
fac6795d
DG
61 goto end;
62 }
63
64 if (opt_list_apps) {
65 ret = process_opt_list_apps();
66 if (ret < 0) {
fac6795d
DG
67 goto end;
68 }
69 }
70
71 return 0;
72
73end:
ebafd2a5 74 ERR("%s", lttng_get_readable_code(ret));
fac6795d
DG
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 */
84static 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
e065084a
DG
109 /* Allocated by lttng_ust_list_apps() */
110 free(pids);
111
fac6795d
DG
112 return 0;
113
114error:
115 return ret;
116}
117
5b8719f5
DG
118/*
119 * spawn_sessiond
120 *
121 * Spawn a session daemon by forking and execv.
122 */
123static 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
155end:
156 return ret;
157}
158
fac6795d
DG
159/*
160 * check_ltt_sessiond
161 *
162 * Check if the session daemon is available using
5b8719f5
DG
163 * the liblttngctl API for the check. If not, try to
164 * spawn a daemon.
fac6795d
DG
165 */
166static int check_ltt_sessiond(void)
167{
168 int ret;
5b8719f5 169 char *pathname = NULL;
fac6795d
DG
170
171 ret = lttng_check_session_daemon();
172 if (ret < 0) {
5b8719f5
DG
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 }
fac6795d
DG
200 }
201
5b8719f5 202end:
fac6795d
DG
203 return ret;
204}
205
5b8719f5
DG
206/*
207 * set_signal_handler
208 *
209 * Setup signal handler for SIGCHLD and SIGTERM.
210 */
211static 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 }
fac6795d 229
5b8719f5
DG
230 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
231 perror("sigaction");
232 goto end;
233 }
234
235end:
236 return ret;
237}
238
239/*
240 * sighandler
241 *
242 * Signal handler for the daemon
243 */
244static 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}
fac6795d
DG
260/*
261 * clean_exit
262 */
263void clean_exit(int code)
264{
265 DBG("Clean exit");
266 exit(code);
267}
268
269/*
5b8719f5 270 * main
fac6795d
DG
271 */
272int 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
5b8719f5
DG
288 ret = set_signal_handler();
289 if (ret < 0) {
290 return ret;
291 }
292
fac6795d
DG
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 */
5b8719f5 310 if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) {
fac6795d
DG
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.036769 seconds and 5 git commands to generate.