Remove "lib" prefix from .c/.h file names
[lttng-tools.git] / ltt-kconsumerd / ltt-kconsumerd.c
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; only version 2
8 * of the License.
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>
38 #include <sys/mman.h>
39
40 #include "lttngerr.h"
41 #include "kernelctl.h"
42 #include "lttkconsumerd.h"
43
44 /* the two threads (receive fd and poll) */
45 pthread_t threads[2];
46
47 /* to count the number of time the user pressed ctrl+c */
48 static int sigintcount = 0;
49
50 /* Argument variables */
51 int opt_quiet;
52 int opt_verbose;
53 static int opt_daemon;
54 static const char *progname;
55 char command_sock_path[PATH_MAX]; /* Global command socket path */
56 char error_sock_path[PATH_MAX]; /* Global error path */
57
58 /*
59 * sighandler
60 *
61 * Signal handler for the daemon
62 */
63 static void sighandler(int sig)
64 {
65 if (sig == SIGINT && sigintcount++ == 0) {
66 DBG("ignoring first SIGINT");
67 return;
68 }
69
70 kconsumerd_should_exit();
71 }
72
73 /*
74 * set_signal_handler
75 *
76 * Setup signal handler for :
77 * SIGINT, SIGTERM, SIGPIPE
78 */
79 static 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
111 /*
112 * usage function on stderr
113 */
114 static 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 */
136 static 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) {
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);
189 }
190 }
191 }
192
193
194 /*
195 * main
196 */
197 int 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 }
220 kconsumerd_set_command_socket_path(command_sock_path);
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
230 /* create the pipe to wake to receiving thread when needed */
231 ret = kconsumerd_init();
232 if (ret < 0) {
233 goto end;
234 }
235
236 /* Connect to the socket created by ltt-sessiond to report errors */
237 DBG("Connecting to error socket %s", error_sock_path);
238 ret = lttcomm_connect_unix_sock(error_sock_path);
239 /* not a fatal error, but all communication with ltt-sessiond will fail */
240 if (ret < 0) {
241 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
242 }
243 kconsumerd_set_error_socket(ret);
244
245 /* Create the thread to manage the receive of fd */
246 ret = pthread_create(&threads[0], NULL, kconsumerd_thread_receive_fds,
247 (void *) NULL);
248 if (ret != 0) {
249 perror("pthread_create");
250 goto error;
251 }
252
253 /* Create thread to manage the polling/writing of traces */
254 ret = pthread_create(&threads[1], NULL, kconsumerd_thread_poll_fds,
255 (void *) NULL);
256 if (ret != 0) {
257 perror("pthread_create");
258 goto error;
259 }
260
261 for (i = 0; i < 2; i++) {
262 ret = pthread_join(threads[i], &status);
263 if (ret != 0) {
264 perror("pthread_join");
265 goto error;
266 }
267 }
268 ret = EXIT_SUCCESS;
269 kconsumerd_send_error(KCONSUMERD_EXIT_SUCCESS);
270 goto end;
271
272 error:
273 ret = EXIT_FAILURE;
274 kconsumerd_send_error(KCONSUMERD_EXIT_FAILURE);
275
276 end:
277 kconsumerd_cleanup();
278
279 return ret;
280 }
This page took 0.037362 seconds and 5 git commands to generate.