Locking fix.
[lttng-tools.git] / ltt-kconsumerd / ltt-kconsumerd.c
CommitLineData
d4a1283e
JD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ipc.h>
31#include <sys/shm.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <urcu/list.h>
36#include <poll.h>
37#include <unistd.h>
03424a9b 38#include <sys/mman.h>
d4a1283e
JD
39
40#include "lttngerr.h"
41#include "libkernelctl.h"
1ce86c9a 42#include "liblttkconsumerd.h"
d4a1283e
JD
43
44/* the two threads (receive fd and poll) */
1ce86c9a 45pthread_t threads[2];
d4a1283e 46
13e44745
JD
47/* to count the number of time the user pressed ctrl+c */
48static int sigintcount = 0;
49
d4a1283e
JD
50/* Argument variables */
51int opt_quiet;
52int opt_verbose;
53static int opt_daemon;
54static const char *progname;
1ce86c9a
JD
55char command_sock_path[PATH_MAX]; /* Global command socket path */
56char error_sock_path[PATH_MAX]; /* Global error path */
d4a1283e
JD
57
58/*
59 * sighandler
60 *
61 * Signal handler for the daemon
62 */
63static void sighandler(int sig)
64{
13e44745
JD
65 if (sig == SIGINT && sigintcount++ == 0) {
66 DBG("ignoring first SIGINT");
67 return;
68 }
69
1ce86c9a 70 kconsumerd_cleanup();
d4a1283e
JD
71}
72
73/*
74 * set_signal_handler
75 *
76 * Setup signal handler for :
77 * SIGINT, SIGTERM, SIGPIPE
78 */
79static int set_signal_handler(void)
80{
81 int ret = 0;
82 struct sigaction sa;
83 sigset_t sigset;
84
85 if ((ret = sigemptyset(&sigset)) < 0) {
86 perror("sigemptyset");
87 return ret;
88 }
89
90 sa.sa_handler = sighandler;
91 sa.sa_mask = sigset;
92 sa.sa_flags = 0;
93 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
94 perror("sigaction");
95 return ret;
96 }
97
98 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
99 perror("sigaction");
100 return ret;
101 }
102
103 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
104 perror("sigaction");
105 return ret;
106 }
107
108 return ret;
109}
110
d4a1283e
JD
111/*
112 * usage function on stderr
113 */
114static void usage(void)
115{
116 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
117 fprintf(stderr, " -h, --help "
118 "Display this usage.\n");
119 fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH "
120 "Specify path for the command socket\n");
121 fprintf(stderr, " -e, --kconsumerd-err-sock PATH "
122 "Specify path for the error socket\n");
123 fprintf(stderr, " -d, --daemonize "
124 "Start as a daemon.\n");
125 fprintf(stderr, " -q, --quiet "
126 "No output at all.\n");
127 fprintf(stderr, " -v, --verbose "
128 "Verbose mode. Activate DBG() macro.\n");
129 fprintf(stderr, " -V, --version "
130 "Show version number.\n");
131}
132
133/*
134 * daemon argument parsing
135 */
136static void parse_args(int argc, char **argv)
137{
138 int c;
139
140 static struct option long_options[] = {
141 { "kconsumerd-cmd-sock", 1, 0, 'c' },
142 { "kconsumerd-err-sock", 1, 0, 'e' },
143 { "daemonize", 0, 0, 'd' },
144 { "help", 0, 0, 'h' },
145 { "quiet", 0, 0, 'q' },
146 { "verbose", 0, 0, 'v' },
147 { "version", 0, 0, 'V' },
148 { NULL, 0, 0, 0 }
149 };
150
151 while (1) {
152 int option_index = 0;
153 c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index);
154 if (c == -1) {
155 break;
156 }
157
158 switch (c) {
914a571b
JD
159 case 0:
160 fprintf(stderr, "option %s", long_options[option_index].name);
161 if (optarg) {
162 fprintf(stderr, " with arg %s\n", optarg);
163 }
164 break;
165 case 'c':
166 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
167 break;
168 case 'e':
169 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
170 break;
171 case 'd':
172 opt_daemon = 1;
173 break;
174 case 'h':
175 usage();
176 exit(EXIT_FAILURE);
177 case 'q':
178 opt_quiet = 1;
179 break;
180 case 'v':
181 opt_verbose = 1;
182 break;
183 case 'V':
184 fprintf(stdout, "%s\n", VERSION);
185 exit(EXIT_SUCCESS);
186 default:
187 usage();
188 exit(EXIT_FAILURE);
d4a1283e
JD
189 }
190 }
191}
192
193
194/*
195 * main
196 */
197int main(int argc, char **argv)
198{
199 int i;
200 int ret = 0;
201 void *status;
202
203 /* Parse arguments */
204 progname = argv[0];
205 parse_args(argc, argv);
206
207 /* Daemonize */
208 if (opt_daemon) {
209 ret = daemon(0, 0);
210 if (ret < 0) {
211 perror("daemon");
212 goto error;
213 }
214 }
215
216 if (strlen(command_sock_path) == 0) {
217 snprintf(command_sock_path, PATH_MAX,
218 KCONSUMERD_CMD_SOCK_PATH);
219 }
1ce86c9a 220 kconsumerd_set_command_socket_path(command_sock_path);
d4a1283e
JD
221 if (strlen(error_sock_path) == 0) {
222 snprintf(error_sock_path, PATH_MAX,
223 KCONSUMERD_ERR_SOCK_PATH);
224 }
225
226 if (set_signal_handler() < 0) {
227 goto error;
228 }
229
252fd492 230 /* create the pipe to wake to polling thread when needed */
1ce86c9a 231 ret = kconsumerd_create_poll_pipe();
252fd492
JD
232 if (ret < 0) {
233 perror("Error creating poll pipe");
234 goto end;
235 }
236
d4a1283e
JD
237 /* Connect to the socket created by ltt-sessiond to report errors */
238 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 239 ret = lttcomm_connect_unix_sock(error_sock_path);
d4a1283e 240 /* not a fatal error, but all communication with ltt-sessiond will fail */
1ce86c9a 241 if (ret < 0) {
d4a1283e
JD
242 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
243 }
1ce86c9a 244 kconsumerd_set_error_socket(ret);
d4a1283e
JD
245
246 /* Create the thread to manage the receive of fd */
1ce86c9a
JD
247 ret = pthread_create(&threads[0], NULL, kconsumerd_thread_receive_fds,
248 (void *) NULL);
d4a1283e
JD
249 if (ret != 0) {
250 perror("pthread_create");
251 goto error;
252 }
253
254 /* Create thread to manage the polling/writing of traces */
1ce86c9a
JD
255 ret = pthread_create(&threads[1], NULL, kconsumerd_thread_poll_fds,
256 (void *) NULL);
d4a1283e
JD
257 if (ret != 0) {
258 perror("pthread_create");
259 goto error;
260 }
261
262 for (i = 0; i < 2; i++) {
263 ret = pthread_join(threads[i], &status);
264 if (ret != 0) {
265 perror("pthread_join");
266 goto error;
267 }
268 }
269 ret = EXIT_SUCCESS;
1ce86c9a 270 kconsumerd_send_error(KCONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
271 goto end;
272
273error:
274 ret = EXIT_FAILURE;
1ce86c9a 275 kconsumerd_send_error(KCONSUMERD_EXIT_FAILURE);
d4a1283e
JD
276
277end:
1ce86c9a 278 kconsumerd_cleanup();
d4a1283e
JD
279
280 return ret;
281}
This page took 0.038106 seconds and 4 git commands to generate.