Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fac6795d | 4 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
91d76f53 | 8 | * |
d14d33bf AM |
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. | |
91d76f53 | 13 | * |
d14d33bf AM |
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 | |
fac6795d DG |
20 | #include <getopt.h> |
21 | #include <grp.h> | |
22 | #include <limits.h> | |
0bb7724a | 23 | #include <paths.h> |
fac6795d DG |
24 | #include <pthread.h> |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
331744e3 | 29 | #include <inttypes.h> |
0fdd1e2c | 30 | #include <sys/mman.h> |
b73401da | 31 | #include <sys/mount.h> |
1e307fab | 32 | #include <sys/resource.h> |
fac6795d DG |
33 | #include <sys/socket.h> |
34 | #include <sys/stat.h> | |
35 | #include <sys/types.h> | |
0fdd1e2c | 36 | #include <sys/wait.h> |
5c827ce0 | 37 | #include <urcu/uatomic.h> |
fac6795d | 38 | #include <unistd.h> |
3bd1e081 | 39 | #include <config.h> |
fac6795d | 40 | |
990570ed | 41 | #include <common/common.h> |
d27c42b8 | 42 | #include <common/compat/socket.h> |
db758600 DG |
43 | #include <common/defaults.h> |
44 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 45 | #include <common/futex.h> |
00e2e675 | 46 | #include <common/relayd/relayd.h> |
81b86775 | 47 | #include <common/utils.h> |
449935a1 | 48 | #include <common/daemonize.h> |
fac6795d | 49 | |
10a8a223 | 50 | #include "lttng-sessiond.h" |
7972aab2 | 51 | #include "buffer-registry.h" |
54d01ffb | 52 | #include "channel.h" |
2f77fc4b | 53 | #include "cmd.h" |
00e2e675 | 54 | #include "consumer.h" |
099e26bd | 55 | #include "context.h" |
54d01ffb | 56 | #include "event.h" |
4771f025 | 57 | #include "kernel.h" |
f1e16794 | 58 | #include "kernel-consumer.h" |
096102bd | 59 | #include "modprobe.h" |
0fdd1e2c | 60 | #include "shm.h" |
1e307fab | 61 | #include "ust-ctl.h" |
00e2e675 | 62 | #include "ust-consumer.h" |
8e68d1c8 | 63 | #include "utils.h" |
4063050c | 64 | #include "fd-limit.h" |
8782cc74 | 65 | #include "health-sessiond.h" |
8ac94142 | 66 | #include "testpoint.h" |
d0b96690 | 67 | #include "ust-thread.h" |
4d076222 | 68 | #include "jul-thread.h" |
fac6795d | 69 | |
ebaeda94 MD |
70 | #define CONSUMERD_FILE "lttng-consumerd" |
71 | ||
fac6795d | 72 | const char *progname; |
6c71277b | 73 | static const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
35f90c40 | 74 | static const char *opt_pidfile; |
5b8719f5 | 75 | static int opt_sig_parent; |
97e19046 | 76 | static int opt_verbose_consumer; |
929de64e | 77 | static int opt_daemon, opt_background; |
4fba7219 | 78 | static int opt_no_kernel; |
fac6795d | 79 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 80 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
0bb7724a | 81 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ |
67e40797 | 82 | static char *rundir; |
3bd1e081 | 83 | |
0bb7724a DG |
84 | /* Set to 1 when a SIGUSR1 signal is received. */ |
85 | static int recv_child_signal; | |
86 | ||
a23ec3a7 DG |
87 | /* |
88 | * Consumer daemon specific control data. Every value not initialized here is | |
89 | * set to 0 by the static definition. | |
90 | */ | |
3bd1e081 MD |
91 | static struct consumer_data kconsumer_data = { |
92 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
93 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
94 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
95 | .err_sock = -1, |
96 | .cmd_sock = -1, | |
173af62f DG |
97 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
98 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
99 | .cond = PTHREAD_COND_INITIALIZER, |
100 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 101 | }; |
7753dea8 MD |
102 | static struct consumer_data ustconsumer64_data = { |
103 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
104 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
105 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
106 | .err_sock = -1, |
107 | .cmd_sock = -1, | |
173af62f DG |
108 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
109 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
110 | .cond = PTHREAD_COND_INITIALIZER, |
111 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
112 | }; |
113 | static struct consumer_data ustconsumer32_data = { | |
114 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
115 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
116 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
117 | .err_sock = -1, |
118 | .cmd_sock = -1, | |
173af62f DG |
119 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
120 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
121 | .cond = PTHREAD_COND_INITIALIZER, |
122 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
123 | }; |
124 | ||
26c9d55e | 125 | /* Shared between threads */ |
099e26bd | 126 | static int dispatch_thread_exit; |
fac6795d | 127 | |
54d01ffb DG |
128 | /* Global application Unix socket path */ |
129 | static char apps_unix_sock_path[PATH_MAX]; | |
130 | /* Global client Unix socket path */ | |
131 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
132 | /* global wait shm path for UST */ |
133 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
134 | /* Global health check unix path */ |
135 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 136 | |
1d4b027a | 137 | /* Sockets and FDs */ |
a4b35e07 MD |
138 | static int client_sock = -1; |
139 | static int apps_sock = -1; | |
2f77fc4b | 140 | int kernel_tracer_fd = -1; |
76d7553f | 141 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 142 | |
273ea72c DG |
143 | /* |
144 | * Quit pipe for all threads. This permits a single cancellation point | |
145 | * for all threads when receiving an event on the pipe. | |
146 | */ | |
76d7553f | 147 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 148 | |
099e26bd DG |
149 | /* |
150 | * This pipe is used to inform the thread managing application communication | |
151 | * that a command is queued and ready to be processed. | |
152 | */ | |
76d7553f | 153 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 154 | |
d0b96690 DG |
155 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
156 | ||
1d4b027a | 157 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 158 | static pthread_t apps_thread; |
d0b96690 | 159 | static pthread_t apps_notify_thread; |
099e26bd | 160 | static pthread_t reg_apps_thread; |
1d4b027a | 161 | static pthread_t client_thread; |
7a485870 | 162 | static pthread_t kernel_thread; |
099e26bd | 163 | static pthread_t dispatch_thread; |
44a5e5eb | 164 | static pthread_t health_thread; |
0b2dc8df | 165 | static pthread_t ht_cleanup_thread; |
4d076222 | 166 | static pthread_t jul_reg_thread; |
5eb91c98 | 167 | |
099e26bd DG |
168 | /* |
169 | * UST registration command queue. This queue is tied with a futex and uses a N | |
170 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
171 | * | |
172 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
173 | * this queue and the wait/wake scheme. | |
174 | */ | |
175 | static struct ust_cmd_queue ust_cmd_queue; | |
176 | ||
b5541356 DG |
177 | /* |
178 | * Pointer initialized before thread creation. | |
179 | * | |
180 | * This points to the tracing session list containing the session count and a | |
181 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
182 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 183 | * |
d063d709 | 184 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 185 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
186 | */ |
187 | static struct ltt_session_list *session_list_ptr; | |
188 | ||
7753dea8 MD |
189 | int ust_consumerd64_fd = -1; |
190 | int ust_consumerd32_fd = -1; | |
191 | ||
fb6f1fa2 YB |
192 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
193 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
194 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
195 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
fb09408a | 196 | |
2f77fc4b DG |
197 | static const char *module_proc_lttng = "/proc/lttng"; |
198 | ||
5c827ce0 DG |
199 | /* |
200 | * Consumer daemon state which is changed when spawning it, killing it or in | |
201 | * case of a fatal error. | |
202 | */ | |
203 | enum consumerd_state { | |
204 | CONSUMER_STARTED = 1, | |
205 | CONSUMER_STOPPED = 2, | |
206 | CONSUMER_ERROR = 3, | |
207 | }; | |
208 | ||
209 | /* | |
210 | * This consumer daemon state is used to validate if a client command will be | |
211 | * able to reach the consumer. If not, the client is informed. For instance, | |
212 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
213 | * error to the client. | |
214 | * | |
215 | * The following example shows a possible race condition of this scheme: | |
216 | * | |
217 | * consumer thread error happens | |
218 | * client cmd arrives | |
219 | * client cmd checks state -> still OK | |
220 | * consumer thread exit, sets error | |
221 | * client cmd try to talk to consumer | |
222 | * ... | |
223 | * | |
224 | * However, since the consumer is a different daemon, we have no way of making | |
225 | * sure the command will reach it safely even with this state flag. This is why | |
226 | * we consider that up to the state validation during command processing, the | |
227 | * command is safe. After that, we can not guarantee the correctness of the | |
228 | * client request vis-a-vis the consumer. | |
229 | */ | |
230 | static enum consumerd_state ust_consumerd_state; | |
231 | static enum consumerd_state kernel_consumerd_state; | |
232 | ||
ae9e45b3 DG |
233 | /* |
234 | * Socket timeout for receiving and sending in seconds. | |
235 | */ | |
236 | static int app_socket_timeout; | |
237 | ||
12744796 DG |
238 | /* Set in main() with the current page size. */ |
239 | long page_size; | |
240 | ||
8782cc74 MD |
241 | /* Application health monitoring */ |
242 | struct health_app *health_sessiond; | |
243 | ||
4d076222 DG |
244 | /* JUL TCP port for registration. Used by the JUL thread. */ |
245 | unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT; | |
246 | ||
56045742 MD |
247 | /* |
248 | * Whether sessiond is ready for commands/health check requests. | |
249 | * NR_LTTNG_SESSIOND_READY must match the number of calls to | |
250 | * lttng_sessiond_notify_ready(). | |
251 | */ | |
252 | #define NR_LTTNG_SESSIOND_READY 2 | |
253 | int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY; | |
254 | ||
255 | /* Notify parents that we are ready for cmd and health check */ | |
256 | static | |
257 | void lttng_sessiond_notify_ready(void) | |
258 | { | |
259 | if (uatomic_sub_return(<tng_sessiond_ready, 1) == 0) { | |
260 | /* | |
261 | * Notify parent pid that we are ready to accept command | |
262 | * for client side. This ppid is the one from the | |
263 | * external process that spawned us. | |
264 | */ | |
265 | if (opt_sig_parent) { | |
266 | kill(ppid, SIGUSR1); | |
267 | } | |
268 | ||
269 | /* | |
270 | * Notify the parent of the fork() process that we are | |
271 | * ready. | |
272 | */ | |
929de64e | 273 | if (opt_daemon || opt_background) { |
56045742 MD |
274 | kill(child_ppid, SIGUSR1); |
275 | } | |
276 | } | |
277 | } | |
278 | ||
fb09408a | 279 | static |
7753dea8 | 280 | void setup_consumerd_path(void) |
fb09408a | 281 | { |
fc7a59ce | 282 | const char *bin, *libdir; |
fb09408a | 283 | |
7753dea8 MD |
284 | /* |
285 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
286 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
287 | * has not been defined. | |
7753dea8 | 288 | */ |
ebaeda94 | 289 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
290 | if (!consumerd32_bin[0]) { |
291 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
292 | } |
293 | if (!consumerd32_libdir[0]) { | |
294 | consumerd32_libdir = INSTALL_LIB_PATH; | |
295 | } | |
296 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
297 | if (!consumerd64_bin[0]) { |
298 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 299 | } |
ebaeda94 MD |
300 | if (!consumerd64_libdir[0]) { |
301 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
302 | } |
303 | #else | |
304 | #error "Unknown bitness" | |
305 | #endif | |
306 | ||
fb09408a MD |
307 | /* |
308 | * runtime env. var. overrides the build default. | |
309 | */ | |
fc7a59ce AM |
310 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
311 | if (bin) { | |
312 | consumerd32_bin = bin; | |
7753dea8 | 313 | } |
fc7a59ce AM |
314 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
315 | if (bin) { | |
316 | consumerd64_bin = bin; | |
ebaeda94 | 317 | } |
72f579ee | 318 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
319 | if (libdir) { |
320 | consumerd32_libdir = libdir; | |
321 | } | |
72f579ee | 322 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
323 | if (libdir) { |
324 | consumerd64_libdir = libdir; | |
fb09408a MD |
325 | } |
326 | } | |
327 | ||
5eb91c98 DG |
328 | /* |
329 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
330 | */ | |
d0b96690 | 331 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) |
5eb91c98 DG |
332 | { |
333 | int ret; | |
334 | ||
d0b96690 | 335 | assert(events); |
5eb91c98 DG |
336 | |
337 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
338 | if (ret < 0) { | |
339 | goto error; | |
340 | } | |
341 | ||
342 | /* Add quit pipe */ | |
d0b96690 | 343 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
344 | if (ret < 0) { |
345 | goto error; | |
346 | } | |
347 | ||
348 | return 0; | |
349 | ||
350 | error: | |
351 | return ret; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Check if the thread quit pipe was triggered. | |
356 | * | |
357 | * Return 1 if it was triggered else 0; | |
358 | */ | |
d0b96690 | 359 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
5eb91c98 DG |
360 | { |
361 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
362 | return 1; | |
363 | } | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
273ea72c | 368 | /* |
5eb91c98 | 369 | * Init thread quit pipe. |
273ea72c DG |
370 | * |
371 | * Return -1 on error or 0 if all pipes are created. | |
372 | */ | |
373 | static int init_thread_quit_pipe(void) | |
374 | { | |
730389d9 | 375 | int ret, i; |
273ea72c | 376 | |
730389d9 | 377 | ret = pipe(thread_quit_pipe); |
273ea72c | 378 | if (ret < 0) { |
730389d9 | 379 | PERROR("thread quit pipe"); |
273ea72c DG |
380 | goto error; |
381 | } | |
382 | ||
730389d9 DG |
383 | for (i = 0; i < 2; i++) { |
384 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
385 | if (ret < 0) { | |
386 | PERROR("fcntl"); | |
387 | goto error; | |
388 | } | |
389 | } | |
390 | ||
273ea72c DG |
391 | error: |
392 | return ret; | |
393 | } | |
394 | ||
099e26bd DG |
395 | /* |
396 | * Stop all threads by closing the thread quit pipe. | |
397 | */ | |
cf3af59e MD |
398 | static void stop_threads(void) |
399 | { | |
5eb91c98 DG |
400 | int ret; |
401 | ||
cf3af59e MD |
402 | /* Stopping all threads */ |
403 | DBG("Terminating all threads"); | |
54d01ffb | 404 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
405 | if (ret < 0) { |
406 | ERR("write error on thread quit pipe"); | |
407 | } | |
408 | ||
099e26bd | 409 | /* Dispatch thread */ |
26c9d55e | 410 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 411 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
412 | } |
413 | ||
e975f9f8 DG |
414 | /* |
415 | * Close every consumer sockets. | |
416 | */ | |
417 | static void close_consumer_sockets(void) | |
418 | { | |
419 | int ret; | |
420 | ||
421 | if (kconsumer_data.err_sock >= 0) { | |
422 | ret = close(kconsumer_data.err_sock); | |
423 | if (ret < 0) { | |
424 | PERROR("kernel consumer err_sock close"); | |
425 | } | |
426 | } | |
427 | if (ustconsumer32_data.err_sock >= 0) { | |
428 | ret = close(ustconsumer32_data.err_sock); | |
429 | if (ret < 0) { | |
a76cbd9f | 430 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
431 | } |
432 | } | |
433 | if (ustconsumer64_data.err_sock >= 0) { | |
434 | ret = close(ustconsumer64_data.err_sock); | |
435 | if (ret < 0) { | |
a76cbd9f | 436 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
437 | } |
438 | } | |
439 | if (kconsumer_data.cmd_sock >= 0) { | |
440 | ret = close(kconsumer_data.cmd_sock); | |
441 | if (ret < 0) { | |
442 | PERROR("kernel consumer cmd_sock close"); | |
443 | } | |
444 | } | |
445 | if (ustconsumer32_data.cmd_sock >= 0) { | |
446 | ret = close(ustconsumer32_data.cmd_sock); | |
447 | if (ret < 0) { | |
a76cbd9f | 448 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
449 | } |
450 | } | |
451 | if (ustconsumer64_data.cmd_sock >= 0) { | |
452 | ret = close(ustconsumer64_data.cmd_sock); | |
453 | if (ret < 0) { | |
a76cbd9f | 454 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
455 | } |
456 | } | |
457 | } | |
458 | ||
fac6795d | 459 | /* |
d063d709 | 460 | * Cleanup the daemon |
fac6795d | 461 | */ |
cf3af59e | 462 | static void cleanup(void) |
fac6795d | 463 | { |
ef599319 | 464 | int ret; |
af9737e9 | 465 | struct ltt_session *sess, *stmp; |
8c6c56c2 | 466 | char path[PATH_MAX]; |
fac6795d | 467 | |
1d4b027a | 468 | DBG("Cleaning up"); |
e07ae692 | 469 | |
4e449f3f MD |
470 | /* |
471 | * Close the thread quit pipe. It has already done its job, | |
472 | * since we are now called. | |
473 | */ | |
2f77fc4b DG |
474 | utils_close_pipe(thread_quit_pipe); |
475 | ||
35f90c40 DG |
476 | /* |
477 | * If opt_pidfile is undefined, the default file will be wiped when | |
478 | * removing the rundir. | |
479 | */ | |
480 | if (opt_pidfile) { | |
481 | ret = remove(opt_pidfile); | |
482 | if (ret < 0) { | |
483 | PERROR("remove pidfile %s", opt_pidfile); | |
484 | } | |
485 | } | |
486 | ||
8c6c56c2 MD |
487 | DBG("Removing sessiond and consumerd content of directory %s", rundir); |
488 | ||
489 | /* sessiond */ | |
490 | snprintf(path, PATH_MAX, | |
491 | "%s/%s", | |
492 | rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE); | |
493 | DBG("Removing %s", path); | |
494 | (void) unlink(path); | |
495 | ||
cd9290dd DG |
496 | snprintf(path, PATH_MAX, "%s/%s", rundir, |
497 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE); | |
498 | DBG("Removing %s", path); | |
499 | (void) unlink(path); | |
500 | ||
8c6c56c2 MD |
501 | /* kconsumerd */ |
502 | snprintf(path, PATH_MAX, | |
503 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
504 | rundir); | |
505 | DBG("Removing %s", path); | |
506 | (void) unlink(path); | |
507 | ||
508 | snprintf(path, PATH_MAX, | |
509 | DEFAULT_KCONSUMERD_PATH, | |
510 | rundir); | |
511 | DBG("Removing directory %s", path); | |
512 | (void) rmdir(path); | |
513 | ||
514 | /* ust consumerd 32 */ | |
515 | snprintf(path, PATH_MAX, | |
516 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
517 | rundir); | |
518 | DBG("Removing %s", path); | |
519 | (void) unlink(path); | |
520 | ||
521 | snprintf(path, PATH_MAX, | |
522 | DEFAULT_USTCONSUMERD32_PATH, | |
523 | rundir); | |
524 | DBG("Removing directory %s", path); | |
525 | (void) rmdir(path); | |
526 | ||
527 | /* ust consumerd 64 */ | |
528 | snprintf(path, PATH_MAX, | |
529 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
530 | rundir); | |
531 | DBG("Removing %s", path); | |
532 | (void) unlink(path); | |
533 | ||
534 | snprintf(path, PATH_MAX, | |
535 | DEFAULT_USTCONSUMERD64_PATH, | |
536 | rundir); | |
537 | DBG("Removing directory %s", path); | |
538 | (void) rmdir(path); | |
5461b305 | 539 | |
dcbcae3e MD |
540 | /* |
541 | * We do NOT rmdir rundir because there are other processes | |
542 | * using it, for instance lttng-relayd, which can start in | |
543 | * parallel with this teardown. | |
544 | */ | |
545 | ||
bd69add2 | 546 | free(rundir); |
5461b305 | 547 | |
99bab54f | 548 | DBG("Cleaning up all sessions"); |
fac6795d | 549 | |
b5541356 | 550 | /* Destroy session list mutex */ |
273ea72c DG |
551 | if (session_list_ptr != NULL) { |
552 | pthread_mutex_destroy(&session_list_ptr->lock); | |
553 | ||
554 | /* Cleanup ALL session */ | |
54d01ffb DG |
555 | cds_list_for_each_entry_safe(sess, stmp, |
556 | &session_list_ptr->head, list) { | |
2f77fc4b | 557 | cmd_destroy_session(sess, kernel_poll_pipe[1]); |
273ea72c DG |
558 | } |
559 | } | |
560 | ||
099e26bd | 561 | DBG("Closing all UST sockets"); |
56fff090 | 562 | ust_app_clean_list(); |
7972aab2 | 563 | buffer_reg_destroy_registries(); |
099e26bd | 564 | |
4fba7219 DG |
565 | if (is_root && !opt_no_kernel) { |
566 | DBG2("Closing kernel fd"); | |
a4b35e07 | 567 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
568 | ret = close(kernel_tracer_fd); |
569 | if (ret) { | |
570 | PERROR("close"); | |
571 | } | |
a4b35e07 | 572 | } |
2f50c8a3 | 573 | DBG("Unloading kernel modules"); |
096102bd | 574 | modprobe_remove_lttng_all(); |
2f50c8a3 | 575 | } |
2f77fc4b | 576 | |
e975f9f8 DG |
577 | close_consumer_sockets(); |
578 | ||
421cb601 | 579 | /* <fun> */ |
f56a39af | 580 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
581 | "Matthew, BEET driven development works!%c[%dm", |
582 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
583 | /* </fun> */ | |
fac6795d DG |
584 | } |
585 | ||
e065084a | 586 | /* |
d063d709 | 587 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 588 | * |
d063d709 | 589 | * Return lttcomm error code. |
e065084a DG |
590 | */ |
591 | static int send_unix_sock(int sock, void *buf, size_t len) | |
592 | { | |
593 | /* Check valid length */ | |
c617c0c6 | 594 | if (len == 0) { |
e065084a DG |
595 | return -1; |
596 | } | |
597 | ||
598 | return lttcomm_send_unix_sock(sock, buf, len); | |
599 | } | |
600 | ||
5461b305 | 601 | /* |
d063d709 | 602 | * Free memory of a command context structure. |
5461b305 | 603 | */ |
a2fb29a5 | 604 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 605 | { |
a2fb29a5 DG |
606 | DBG("Clean command context structure"); |
607 | if (*cmd_ctx) { | |
608 | if ((*cmd_ctx)->llm) { | |
609 | free((*cmd_ctx)->llm); | |
5461b305 | 610 | } |
a2fb29a5 DG |
611 | if ((*cmd_ctx)->lsm) { |
612 | free((*cmd_ctx)->lsm); | |
5461b305 | 613 | } |
a2fb29a5 DG |
614 | free(*cmd_ctx); |
615 | *cmd_ctx = NULL; | |
5461b305 DG |
616 | } |
617 | } | |
618 | ||
fac6795d | 619 | /* |
0fdd1e2c | 620 | * Notify UST applications using the shm mmap futex. |
fac6795d | 621 | */ |
0fdd1e2c | 622 | static int notify_ust_apps(int active) |
fac6795d | 623 | { |
0fdd1e2c | 624 | char *wait_shm_mmap; |
fac6795d | 625 | |
0fdd1e2c | 626 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 627 | |
0fdd1e2c DG |
628 | /* See shm.c for this call implying mmap, shm and futex calls */ |
629 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
630 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
631 | goto error; |
632 | } | |
633 | ||
0fdd1e2c DG |
634 | /* Wake waiting process */ |
635 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
636 | ||
637 | /* Apps notified successfully */ | |
638 | return 0; | |
fac6795d DG |
639 | |
640 | error: | |
0fdd1e2c | 641 | return -1; |
fac6795d DG |
642 | } |
643 | ||
e065084a | 644 | /* |
d063d709 DG |
645 | * Setup the outgoing data buffer for the response (llm) by allocating the |
646 | * right amount of memory and copying the original information from the lsm | |
647 | * structure. | |
ca95a216 | 648 | * |
d063d709 | 649 | * Return total size of the buffer pointed by buf. |
ca95a216 | 650 | */ |
5461b305 | 651 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 652 | { |
f3ed775e | 653 | int ret, buf_size; |
ca95a216 | 654 | |
f3ed775e | 655 | buf_size = size; |
5461b305 | 656 | |
ba7f0ae5 | 657 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 658 | if (cmd_ctx->llm == NULL) { |
76d7553f | 659 | PERROR("zmalloc"); |
5461b305 | 660 | ret = -ENOMEM; |
ca95a216 DG |
661 | goto error; |
662 | } | |
663 | ||
5461b305 DG |
664 | /* Copy common data */ |
665 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 666 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 667 | |
5461b305 DG |
668 | cmd_ctx->llm->data_size = size; |
669 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
670 | ||
ca95a216 DG |
671 | return buf_size; |
672 | ||
673 | error: | |
674 | return ret; | |
675 | } | |
676 | ||
7a485870 | 677 | /* |
5eb91c98 | 678 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 679 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 680 | */ |
5eb91c98 | 681 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 682 | { |
5eb91c98 | 683 | int ret; |
7a485870 DG |
684 | struct ltt_session *session; |
685 | struct ltt_kernel_channel *channel; | |
686 | ||
5eb91c98 | 687 | DBG("Updating kernel poll set"); |
7a485870 | 688 | |
54d01ffb | 689 | session_lock_list(); |
b5541356 | 690 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 691 | session_lock(session); |
7a485870 | 692 | if (session->kernel_session == NULL) { |
54d01ffb | 693 | session_unlock(session); |
7a485870 DG |
694 | continue; |
695 | } | |
7a485870 | 696 | |
54d01ffb DG |
697 | cds_list_for_each_entry(channel, |
698 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
699 | /* Add channel fd to the kernel poll set */ |
700 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
701 | if (ret < 0) { | |
54d01ffb | 702 | session_unlock(session); |
5eb91c98 DG |
703 | goto error; |
704 | } | |
705 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 706 | } |
54d01ffb | 707 | session_unlock(session); |
7a485870 | 708 | } |
54d01ffb | 709 | session_unlock_list(); |
7a485870 | 710 | |
5eb91c98 | 711 | return 0; |
7a485870 DG |
712 | |
713 | error: | |
54d01ffb | 714 | session_unlock_list(); |
7a485870 DG |
715 | return -1; |
716 | } | |
717 | ||
718 | /* | |
54d01ffb | 719 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 720 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 721 | * |
d063d709 | 722 | * Useful for CPU hotplug feature. |
7a485870 | 723 | */ |
2bdd86d4 | 724 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
725 | { |
726 | int ret = 0; | |
727 | struct ltt_session *session; | |
173af62f | 728 | struct ltt_kernel_session *ksess; |
7a485870 DG |
729 | struct ltt_kernel_channel *channel; |
730 | ||
731 | DBG("Updating kernel streams for channel fd %d", fd); | |
732 | ||
54d01ffb | 733 | session_lock_list(); |
b5541356 | 734 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 735 | session_lock(session); |
7a485870 | 736 | if (session->kernel_session == NULL) { |
54d01ffb | 737 | session_unlock(session); |
7a485870 DG |
738 | continue; |
739 | } | |
173af62f | 740 | ksess = session->kernel_session; |
d9800920 | 741 | |
173af62f | 742 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
7a485870 DG |
743 | if (channel->fd == fd) { |
744 | DBG("Channel found, updating kernel streams"); | |
745 | ret = kernel_open_channel_stream(channel); | |
746 | if (ret < 0) { | |
b3c750d2 | 747 | goto error; |
7a485870 | 748 | } |
5c786ded JD |
749 | /* Update the stream global counter */ |
750 | ksess->stream_count_global += ret; | |
d9800920 | 751 | |
7a485870 | 752 | /* |
5eb91c98 DG |
753 | * Have we already sent fds to the consumer? If yes, it means |
754 | * that tracing is started so it is safe to send our updated | |
755 | * stream fds. | |
7a485870 | 756 | */ |
173af62f DG |
757 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
758 | struct lttng_ht_iter iter; | |
759 | struct consumer_socket *socket; | |
760 | ||
e7fe706f | 761 | rcu_read_lock(); |
173af62f DG |
762 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
763 | &iter.iter, socket, node.node) { | |
173af62f | 764 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 765 | ret = kernel_consumer_send_channel_stream(socket, |
2bba9e53 DG |
766 | channel, ksess, |
767 | session->output_traces ? 1 : 0); | |
173af62f DG |
768 | pthread_mutex_unlock(socket->lock); |
769 | if (ret < 0) { | |
e7fe706f | 770 | rcu_read_unlock(); |
173af62f DG |
771 | goto error; |
772 | } | |
7a485870 | 773 | } |
e7fe706f | 774 | rcu_read_unlock(); |
7a485870 | 775 | } |
b3c750d2 | 776 | goto error; |
7a485870 DG |
777 | } |
778 | } | |
54d01ffb | 779 | session_unlock(session); |
7a485870 | 780 | } |
54d01ffb | 781 | session_unlock_list(); |
b3c750d2 | 782 | return ret; |
7a485870 | 783 | |
b3c750d2 | 784 | error: |
54d01ffb DG |
785 | session_unlock(session); |
786 | session_unlock_list(); | |
7a485870 DG |
787 | return ret; |
788 | } | |
789 | ||
487cf67c | 790 | /* |
ffe60014 DG |
791 | * For each tracing session, update newly registered apps. The session list |
792 | * lock MUST be acquired before calling this. | |
487cf67c DG |
793 | */ |
794 | static void update_ust_app(int app_sock) | |
795 | { | |
796 | struct ltt_session *sess, *stmp; | |
797 | ||
fdadac08 DG |
798 | /* Consumer is in an ERROR state. Stop any application update. */ |
799 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
800 | /* Stop the update process since the consumer is dead. */ | |
801 | return; | |
802 | } | |
803 | ||
487cf67c DG |
804 | /* For all tracing session(s) */ |
805 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 806 | session_lock(sess); |
421cb601 DG |
807 | if (sess->ust_session) { |
808 | ust_app_global_update(sess->ust_session, app_sock); | |
809 | } | |
4ee14516 | 810 | session_unlock(sess); |
487cf67c DG |
811 | } |
812 | } | |
813 | ||
7a485870 | 814 | /* |
d063d709 | 815 | * This thread manage event coming from the kernel. |
7a485870 | 816 | * |
d063d709 DG |
817 | * Features supported in this thread: |
818 | * -) CPU Hotplug | |
7a485870 DG |
819 | */ |
820 | static void *thread_manage_kernel(void *data) | |
821 | { | |
139ac872 | 822 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 823 | uint32_t revents, nb_fd; |
7a485870 | 824 | char tmp; |
5eb91c98 | 825 | struct lttng_poll_event events; |
7a485870 | 826 | |
6993eeb3 | 827 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 828 | |
6c71277b | 829 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 830 | |
d5d63bf1 DG |
831 | /* |
832 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 833 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 834 | */ |
6d737ce4 | 835 | lttng_poll_init(&events); |
d5d63bf1 | 836 | |
71ddb4ed | 837 | if (testpoint(sessiond_thread_manage_kernel)) { |
6993eeb3 CB |
838 | goto error_testpoint; |
839 | } | |
8ac94142 | 840 | |
840cb59c | 841 | health_code_update(); |
44a5e5eb | 842 | |
71ddb4ed | 843 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
d21b0d71 | 844 | goto error_testpoint; |
6993eeb3 CB |
845 | } |
846 | ||
7a485870 | 847 | while (1) { |
840cb59c | 848 | health_code_update(); |
44a5e5eb | 849 | |
7a485870 | 850 | if (update_poll_flag == 1) { |
d21b0d71 DG |
851 | /* Clean events object. We are about to populate it again. */ |
852 | lttng_poll_clean(&events); | |
853 | ||
d0b96690 | 854 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
855 | if (ret < 0) { |
856 | goto error_poll_create; | |
857 | } | |
858 | ||
859 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
860 | if (ret < 0) { | |
861 | goto error; | |
862 | } | |
5f822d0a | 863 | |
d21b0d71 | 864 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
865 | ret = update_kernel_poll(&events); |
866 | if (ret < 0) { | |
7a485870 DG |
867 | goto error; |
868 | } | |
869 | update_poll_flag = 0; | |
870 | } | |
871 | ||
d21b0d71 | 872 | DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events)); |
7a485870 DG |
873 | |
874 | /* Poll infinite value of time */ | |
88f2b785 | 875 | restart: |
a78af745 | 876 | health_poll_entry(); |
5eb91c98 | 877 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 878 | health_poll_exit(); |
7a485870 | 879 | if (ret < 0) { |
88f2b785 MD |
880 | /* |
881 | * Restart interrupted system call. | |
882 | */ | |
883 | if (errno == EINTR) { | |
884 | goto restart; | |
885 | } | |
7a485870 DG |
886 | goto error; |
887 | } else if (ret == 0) { | |
888 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
889 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
890 | "This should not have happened! Continuing..."); | |
7a485870 DG |
891 | continue; |
892 | } | |
893 | ||
0d9c5d77 DG |
894 | nb_fd = ret; |
895 | ||
5eb91c98 DG |
896 | for (i = 0; i < nb_fd; i++) { |
897 | /* Fetch once the poll data */ | |
898 | revents = LTTNG_POLL_GETEV(&events, i); | |
899 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 900 | |
840cb59c | 901 | health_code_update(); |
44a5e5eb | 902 | |
5eb91c98 | 903 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 904 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 905 | if (ret) { |
139ac872 MD |
906 | err = 0; |
907 | goto exit; | |
5eb91c98 | 908 | } |
7a485870 | 909 | |
5eb91c98 DG |
910 | /* Check for data on kernel pipe */ |
911 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
6cd525e8 MD |
912 | (void) lttng_read(kernel_poll_pipe[0], |
913 | &tmp, 1); | |
f921c78f DG |
914 | /* |
915 | * Ret value is useless here, if this pipe gets any actions an | |
916 | * update is required anyway. | |
917 | */ | |
5eb91c98 DG |
918 | update_poll_flag = 1; |
919 | continue; | |
920 | } else { | |
921 | /* | |
922 | * New CPU detected by the kernel. Adding kernel stream to | |
923 | * kernel session and updating the kernel consumer | |
924 | */ | |
925 | if (revents & LPOLLIN) { | |
2bdd86d4 | 926 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
927 | if (ret < 0) { |
928 | continue; | |
929 | } | |
930 | break; | |
931 | /* | |
932 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
933 | * and unregister kernel stream at this point. | |
934 | */ | |
7a485870 | 935 | } |
7a485870 DG |
936 | } |
937 | } | |
938 | } | |
939 | ||
139ac872 | 940 | exit: |
7a485870 | 941 | error: |
5eb91c98 | 942 | lttng_poll_clean(&events); |
76d7553f | 943 | error_poll_create: |
6993eeb3 | 944 | error_testpoint: |
6620da75 DG |
945 | utils_close_pipe(kernel_poll_pipe); |
946 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 947 | if (err) { |
840cb59c | 948 | health_error(); |
139ac872 | 949 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
950 | WARN("Kernel thread died unexpectedly. " |
951 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 952 | } |
8782cc74 | 953 | health_unregister(health_sessiond); |
76d7553f | 954 | DBG("Kernel thread dying"); |
7a485870 DG |
955 | return NULL; |
956 | } | |
957 | ||
a23ec3a7 DG |
958 | /* |
959 | * Signal pthread condition of the consumer data that the thread. | |
960 | */ | |
961 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
962 | { | |
963 | pthread_mutex_lock(&data->cond_mutex); | |
964 | ||
965 | /* | |
966 | * The state is set before signaling. It can be any value, it's the waiter | |
967 | * job to correctly interpret this condition variable associated to the | |
968 | * consumer pthread_cond. | |
969 | * | |
970 | * A value of 0 means that the corresponding thread of the consumer data | |
971 | * was not started. 1 indicates that the thread has started and is ready | |
972 | * for action. A negative value means that there was an error during the | |
973 | * thread bootstrap. | |
974 | */ | |
975 | data->consumer_thread_is_ready = state; | |
976 | (void) pthread_cond_signal(&data->cond); | |
977 | ||
978 | pthread_mutex_unlock(&data->cond_mutex); | |
979 | } | |
980 | ||
1d4b027a | 981 | /* |
3bd1e081 | 982 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 983 | */ |
3bd1e081 | 984 | static void *thread_manage_consumer(void *data) |
1d4b027a | 985 | { |
139ac872 | 986 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 | 987 | uint32_t revents, nb_fd; |
1d4b027a | 988 | enum lttcomm_return_code code; |
5eb91c98 | 989 | struct lttng_poll_event events; |
3bd1e081 | 990 | struct consumer_data *consumer_data = data; |
1d4b027a | 991 | |
3bd1e081 | 992 | DBG("[thread] Manage consumer started"); |
1d4b027a | 993 | |
6c71277b | 994 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 995 | |
855060f8 | 996 | health_code_update(); |
9449cc75 | 997 | |
5eb91c98 | 998 | /* |
331744e3 JD |
999 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
1000 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 1001 | */ |
331744e3 | 1002 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 1003 | if (ret < 0) { |
76d7553f | 1004 | goto error_poll; |
5eb91c98 | 1005 | } |
273ea72c | 1006 | |
edb8b045 DG |
1007 | /* |
1008 | * The error socket here is already in a listening state which was done | |
1009 | * just before spawning this thread to avoid a race between the consumer | |
1010 | * daemon exec trying to connect and the listen() call. | |
1011 | */ | |
3bd1e081 | 1012 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
1013 | if (ret < 0) { |
1014 | goto error; | |
1015 | } | |
1016 | ||
840cb59c | 1017 | health_code_update(); |
44a5e5eb | 1018 | |
331744e3 | 1019 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1020 | restart: |
a78af745 | 1021 | health_poll_entry(); |
8ac94142 | 1022 | |
71ddb4ed | 1023 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
1024 | goto error; |
1025 | } | |
8ac94142 | 1026 | |
5eb91c98 | 1027 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1028 | health_poll_exit(); |
273ea72c | 1029 | if (ret < 0) { |
88f2b785 MD |
1030 | /* |
1031 | * Restart interrupted system call. | |
1032 | */ | |
1033 | if (errno == EINTR) { | |
1034 | goto restart; | |
1035 | } | |
273ea72c DG |
1036 | goto error; |
1037 | } | |
1038 | ||
0d9c5d77 DG |
1039 | nb_fd = ret; |
1040 | ||
5eb91c98 DG |
1041 | for (i = 0; i < nb_fd; i++) { |
1042 | /* Fetch once the poll data */ | |
1043 | revents = LTTNG_POLL_GETEV(&events, i); | |
1044 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1045 | ||
840cb59c | 1046 | health_code_update(); |
44a5e5eb | 1047 | |
5eb91c98 | 1048 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1049 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1050 | if (ret) { |
139ac872 MD |
1051 | err = 0; |
1052 | goto exit; | |
5eb91c98 DG |
1053 | } |
1054 | ||
1055 | /* Event on the registration socket */ | |
3bd1e081 | 1056 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 1057 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 1058 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
1059 | goto error; |
1060 | } | |
1061 | } | |
273ea72c DG |
1062 | } |
1063 | ||
3bd1e081 | 1064 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
1065 | if (sock < 0) { |
1066 | goto error; | |
1067 | } | |
1068 | ||
b662582b DG |
1069 | /* |
1070 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1071 | * show must go on. | |
1072 | */ | |
1073 | (void) utils_set_fd_cloexec(sock); | |
1074 | ||
840cb59c | 1075 | health_code_update(); |
44a5e5eb | 1076 | |
3bd1e081 | 1077 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 1078 | |
712ea556 | 1079 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
1080 | ret = lttcomm_recv_unix_sock(sock, &code, |
1081 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
1082 | if (ret <= 0) { |
1083 | goto error; | |
1084 | } | |
1085 | ||
840cb59c | 1086 | health_code_update(); |
44a5e5eb | 1087 | |
f73fabfd | 1088 | if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
331744e3 | 1089 | /* Connect both socket, command and metadata. */ |
3bd1e081 MD |
1090 | consumer_data->cmd_sock = |
1091 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
4ce514c4 | 1092 | consumer_data->metadata_fd = |
331744e3 | 1093 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
92db7cdc DG |
1094 | if (consumer_data->cmd_sock < 0 |
1095 | || consumer_data->metadata_fd < 0) { | |
331744e3 | 1096 | PERROR("consumer connect cmd socket"); |
a23ec3a7 DG |
1097 | /* On error, signal condition and quit. */ |
1098 | signal_consumer_condition(consumer_data, -1); | |
1d4b027a DG |
1099 | goto error; |
1100 | } | |
9363801e | 1101 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; |
331744e3 JD |
1102 | /* Create metadata socket lock. */ |
1103 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
1104 | if (consumer_data->metadata_sock.lock == NULL) { | |
1105 | PERROR("zmalloc pthread mutex"); | |
1106 | ret = -1; | |
1107 | goto error; | |
1108 | } | |
1109 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
1110 | ||
a23ec3a7 | 1111 | signal_consumer_condition(consumer_data, 1); |
331744e3 JD |
1112 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); |
1113 | DBG("Consumer metadata socket ready (fd: %d)", | |
4ce514c4 | 1114 | consumer_data->metadata_fd); |
1d4b027a | 1115 | } else { |
3bd1e081 | 1116 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1117 | lttcomm_get_readable_code(-code)); |
1118 | goto error; | |
1119 | } | |
1120 | ||
331744e3 | 1121 | /* Remove the consumerd error sock since we've established a connexion */ |
3bd1e081 | 1122 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1123 | if (ret < 0) { |
72079cae DG |
1124 | goto error; |
1125 | } | |
1126 | ||
331744e3 | 1127 | /* Add new accepted error socket. */ |
5eb91c98 DG |
1128 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1129 | if (ret < 0) { | |
72079cae | 1130 | goto error; |
5eb91c98 DG |
1131 | } |
1132 | ||
331744e3 | 1133 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 1134 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
1135 | LPOLLIN | LPOLLRDHUP); |
1136 | if (ret < 0) { | |
1137 | goto error; | |
1138 | } | |
1139 | ||
840cb59c | 1140 | health_code_update(); |
44a5e5eb | 1141 | |
331744e3 | 1142 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1143 | restart_poll: |
331744e3 JD |
1144 | while (1) { |
1145 | health_poll_entry(); | |
1146 | ret = lttng_poll_wait(&events, -1); | |
1147 | health_poll_exit(); | |
1148 | if (ret < 0) { | |
1149 | /* | |
1150 | * Restart interrupted system call. | |
1151 | */ | |
1152 | if (errno == EINTR) { | |
1153 | goto restart_poll; | |
1154 | } | |
1155 | goto error; | |
88f2b785 | 1156 | } |
72079cae | 1157 | |
331744e3 | 1158 | nb_fd = ret; |
0d9c5d77 | 1159 | |
331744e3 JD |
1160 | for (i = 0; i < nb_fd; i++) { |
1161 | /* Fetch once the poll data */ | |
1162 | revents = LTTNG_POLL_GETEV(&events, i); | |
1163 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 1164 | |
331744e3 | 1165 | health_code_update(); |
44a5e5eb | 1166 | |
331744e3 JD |
1167 | /* Thread quit pipe has been closed. Killing thread. */ |
1168 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
1169 | if (ret) { | |
1170 | err = 0; | |
1171 | goto exit; | |
1172 | } | |
5eb91c98 | 1173 | |
331744e3 JD |
1174 | if (pollfd == sock) { |
1175 | /* Event on the consumerd socket */ | |
1176 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1177 | ERR("consumer err socket second poll error"); | |
1178 | goto error; | |
1179 | } | |
1180 | health_code_update(); | |
1181 | /* Wait for any kconsumerd error */ | |
1182 | ret = lttcomm_recv_unix_sock(sock, &code, | |
1183 | sizeof(enum lttcomm_return_code)); | |
1184 | if (ret <= 0) { | |
1185 | ERR("consumer closed the command socket"); | |
1186 | goto error; | |
1187 | } | |
1188 | ||
1189 | ERR("consumer return code : %s", | |
1190 | lttcomm_get_readable_code(-code)); | |
1191 | ||
1192 | goto exit; | |
4ce514c4 | 1193 | } else if (pollfd == consumer_data->metadata_fd) { |
331744e3 JD |
1194 | /* UST metadata requests */ |
1195 | ret = ust_consumer_metadata_request( | |
1196 | &consumer_data->metadata_sock); | |
1197 | if (ret < 0) { | |
1198 | ERR("Handling metadata request"); | |
1199 | goto error; | |
1200 | } | |
1201 | break; | |
1202 | } else { | |
1203 | ERR("Unknown pollfd"); | |
5eb91c98 DG |
1204 | goto error; |
1205 | } | |
1206 | } | |
331744e3 | 1207 | health_code_update(); |
5eb91c98 DG |
1208 | } |
1209 | ||
139ac872 | 1210 | exit: |
1d4b027a | 1211 | error: |
fdadac08 DG |
1212 | /* |
1213 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1214 | * thread might be using them so get exclusive access which will abort all |
1215 | * other consumer command by other threads. | |
fdadac08 DG |
1216 | */ |
1217 | pthread_mutex_lock(&consumer_data->lock); | |
1218 | ||
5c827ce0 DG |
1219 | /* Immediately set the consumerd state to stopped */ |
1220 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1221 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1222 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1223 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1224 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1225 | } else { | |
1226 | /* Code flow error... */ | |
1227 | assert(0); | |
1228 | } | |
1229 | ||
76d7553f MD |
1230 | if (consumer_data->err_sock >= 0) { |
1231 | ret = close(consumer_data->err_sock); | |
1232 | if (ret) { | |
1233 | PERROR("close"); | |
1234 | } | |
a76cbd9f | 1235 | consumer_data->err_sock = -1; |
76d7553f MD |
1236 | } |
1237 | if (consumer_data->cmd_sock >= 0) { | |
1238 | ret = close(consumer_data->cmd_sock); | |
1239 | if (ret) { | |
1240 | PERROR("close"); | |
1241 | } | |
a76cbd9f | 1242 | consumer_data->cmd_sock = -1; |
76d7553f | 1243 | } |
9363801e DG |
1244 | if (*consumer_data->metadata_sock.fd_ptr >= 0) { |
1245 | ret = close(*consumer_data->metadata_sock.fd_ptr); | |
331744e3 JD |
1246 | if (ret) { |
1247 | PERROR("close"); | |
1248 | } | |
1249 | } | |
331744e3 | 1250 | |
76d7553f MD |
1251 | if (sock >= 0) { |
1252 | ret = close(sock); | |
1253 | if (ret) { | |
1254 | PERROR("close"); | |
1255 | } | |
1256 | } | |
273ea72c | 1257 | |
3bd1e081 MD |
1258 | unlink(consumer_data->err_unix_sock_path); |
1259 | unlink(consumer_data->cmd_unix_sock_path); | |
1260 | consumer_data->pid = 0; | |
fdadac08 | 1261 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1262 | |
fdadac08 DG |
1263 | /* Cleanup metadata socket mutex. */ |
1264 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1265 | free(consumer_data->metadata_sock.lock); | |
1d4b027a | 1266 | |
5eb91c98 | 1267 | lttng_poll_clean(&events); |
76d7553f | 1268 | error_poll: |
139ac872 | 1269 | if (err) { |
840cb59c | 1270 | health_error(); |
139ac872 MD |
1271 | ERR("Health error occurred in %s", __func__); |
1272 | } | |
8782cc74 | 1273 | health_unregister(health_sessiond); |
76d7553f | 1274 | DBG("consumer thread cleanup completed"); |
0177d773 | 1275 | |
5eb91c98 | 1276 | return NULL; |
099e26bd DG |
1277 | } |
1278 | ||
099e26bd DG |
1279 | /* |
1280 | * This thread manage application communication. | |
1d4b027a DG |
1281 | */ |
1282 | static void *thread_manage_apps(void *data) | |
099e26bd | 1283 | { |
139ac872 | 1284 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1285 | ssize_t size_ret; |
5eb91c98 | 1286 | uint32_t revents, nb_fd; |
5eb91c98 | 1287 | struct lttng_poll_event events; |
099e26bd DG |
1288 | |
1289 | DBG("[thread] Manage application started"); | |
1290 | ||
f6a9efaa DG |
1291 | rcu_register_thread(); |
1292 | rcu_thread_online(); | |
1293 | ||
6c71277b | 1294 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1295 | |
71ddb4ed | 1296 | if (testpoint(sessiond_thread_manage_apps)) { |
6993eeb3 CB |
1297 | goto error_testpoint; |
1298 | } | |
1299 | ||
840cb59c | 1300 | health_code_update(); |
44a5e5eb | 1301 | |
d0b96690 | 1302 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1303 | if (ret < 0) { |
76d7553f | 1304 | goto error_poll_create; |
5eb91c98 | 1305 | } |
099e26bd | 1306 | |
5eb91c98 DG |
1307 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1308 | if (ret < 0) { | |
1309 | goto error; | |
1310 | } | |
099e26bd | 1311 | |
71ddb4ed | 1312 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
6993eeb3 CB |
1313 | goto error; |
1314 | } | |
8ac94142 | 1315 | |
840cb59c | 1316 | health_code_update(); |
44a5e5eb | 1317 | |
5eb91c98 | 1318 | while (1) { |
d21b0d71 | 1319 | DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events)); |
099e26bd DG |
1320 | |
1321 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1322 | restart: |
a78af745 | 1323 | health_poll_entry(); |
5eb91c98 | 1324 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1325 | health_poll_exit(); |
099e26bd | 1326 | if (ret < 0) { |
88f2b785 MD |
1327 | /* |
1328 | * Restart interrupted system call. | |
1329 | */ | |
1330 | if (errno == EINTR) { | |
1331 | goto restart; | |
1332 | } | |
099e26bd DG |
1333 | goto error; |
1334 | } | |
1335 | ||
0d9c5d77 DG |
1336 | nb_fd = ret; |
1337 | ||
5eb91c98 DG |
1338 | for (i = 0; i < nb_fd; i++) { |
1339 | /* Fetch once the poll data */ | |
1340 | revents = LTTNG_POLL_GETEV(&events, i); | |
1341 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1342 | ||
840cb59c | 1343 | health_code_update(); |
44a5e5eb | 1344 | |
5eb91c98 | 1345 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1346 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1347 | if (ret) { |
139ac872 MD |
1348 | err = 0; |
1349 | goto exit; | |
5eb91c98 | 1350 | } |
099e26bd | 1351 | |
5eb91c98 DG |
1352 | /* Inspect the apps cmd pipe */ |
1353 | if (pollfd == apps_cmd_pipe[0]) { | |
1354 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1355 | ERR("Apps command pipe error"); | |
0177d773 | 1356 | goto error; |
5eb91c98 | 1357 | } else if (revents & LPOLLIN) { |
d0b96690 DG |
1358 | int sock; |
1359 | ||
5eb91c98 | 1360 | /* Empty pipe */ |
6cd525e8 MD |
1361 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1362 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1363 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1364 | goto error; |
1365 | } | |
099e26bd | 1366 | |
840cb59c | 1367 | health_code_update(); |
44a5e5eb | 1368 | |
ffe60014 | 1369 | /* |
d0b96690 DG |
1370 | * We only monitor the error events of the socket. This |
1371 | * thread does not handle any incoming data from UST | |
1372 | * (POLLIN). | |
ffe60014 | 1373 | */ |
d0b96690 DG |
1374 | ret = lttng_poll_add(&events, sock, |
1375 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1376 | if (ret < 0) { | |
5eb91c98 | 1377 | goto error; |
e0c7ec2b | 1378 | } |
acc7b41b | 1379 | |
d0b96690 | 1380 | DBG("Apps with sock %d added to poll set", sock); |
0177d773 | 1381 | } |
5eb91c98 DG |
1382 | } else { |
1383 | /* | |
54d01ffb DG |
1384 | * At this point, we know that a registered application made |
1385 | * the event at poll_wait. | |
5eb91c98 DG |
1386 | */ |
1387 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1388 | /* Removing from the poll set */ | |
1389 | ret = lttng_poll_del(&events, pollfd); | |
1390 | if (ret < 0) { | |
1391 | goto error; | |
1392 | } | |
099e26bd | 1393 | |
b9d9b220 | 1394 | /* Socket closed on remote end. */ |
56fff090 | 1395 | ust_app_unregister(pollfd); |
5eb91c98 | 1396 | } |
099e26bd | 1397 | } |
44a5e5eb | 1398 | |
840cb59c | 1399 | health_code_update(); |
099e26bd | 1400 | } |
099e26bd DG |
1401 | } |
1402 | ||
139ac872 | 1403 | exit: |
099e26bd | 1404 | error: |
5eb91c98 | 1405 | lttng_poll_clean(&events); |
76d7553f | 1406 | error_poll_create: |
6993eeb3 | 1407 | error_testpoint: |
6620da75 DG |
1408 | utils_close_pipe(apps_cmd_pipe); |
1409 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1410 | ||
1411 | /* | |
1412 | * We don't clean the UST app hash table here since already registered | |
1413 | * applications can still be controlled so let them be until the session | |
1414 | * daemon dies or the applications stop. | |
1415 | */ | |
1416 | ||
139ac872 | 1417 | if (err) { |
840cb59c | 1418 | health_error(); |
139ac872 MD |
1419 | ERR("Health error occurred in %s", __func__); |
1420 | } | |
8782cc74 | 1421 | health_unregister(health_sessiond); |
76d7553f | 1422 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1423 | rcu_thread_offline(); |
1424 | rcu_unregister_thread(); | |
099e26bd DG |
1425 | return NULL; |
1426 | } | |
1427 | ||
d0b96690 | 1428 | /* |
d88aee68 DG |
1429 | * Send a socket to a thread This is called from the dispatch UST registration |
1430 | * thread once all sockets are set for the application. | |
d0b96690 | 1431 | * |
b85dc84c DG |
1432 | * The sock value can be invalid, we don't really care, the thread will handle |
1433 | * it and make the necessary cleanup if so. | |
1434 | * | |
d0b96690 DG |
1435 | * On success, return 0 else a negative value being the errno message of the |
1436 | * write(). | |
1437 | */ | |
d88aee68 | 1438 | static int send_socket_to_thread(int fd, int sock) |
d0b96690 | 1439 | { |
6cd525e8 | 1440 | ssize_t ret; |
d0b96690 | 1441 | |
b85dc84c DG |
1442 | /* |
1443 | * It's possible that the FD is set as invalid with -1 concurrently just | |
1444 | * before calling this function being a shutdown state of the thread. | |
1445 | */ | |
1446 | if (fd < 0) { | |
1447 | ret = -EBADF; | |
1448 | goto error; | |
1449 | } | |
d0b96690 | 1450 | |
6cd525e8 MD |
1451 | ret = lttng_write(fd, &sock, sizeof(sock)); |
1452 | if (ret < sizeof(sock)) { | |
d88aee68 | 1453 | PERROR("write apps pipe %d", fd); |
d0b96690 DG |
1454 | if (ret < 0) { |
1455 | ret = -errno; | |
1456 | } | |
1457 | goto error; | |
1458 | } | |
1459 | ||
1460 | /* All good. Don't send back the write positive ret value. */ | |
1461 | ret = 0; | |
1462 | error: | |
6cd525e8 | 1463 | return (int) ret; |
d0b96690 DG |
1464 | } |
1465 | ||
f45e313d DG |
1466 | /* |
1467 | * Sanitize the wait queue of the dispatch registration thread meaning removing | |
1468 | * invalid nodes from it. This is to avoid memory leaks for the case the UST | |
1469 | * notify socket is never received. | |
1470 | */ | |
1471 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) | |
1472 | { | |
1473 | int ret, nb_fd = 0, i; | |
1474 | unsigned int fd_added = 0; | |
1475 | struct lttng_poll_event events; | |
1476 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; | |
1477 | ||
1478 | assert(wait_queue); | |
1479 | ||
1480 | lttng_poll_init(&events); | |
1481 | ||
1482 | /* Just skip everything for an empty queue. */ | |
1483 | if (!wait_queue->count) { | |
1484 | goto end; | |
1485 | } | |
1486 | ||
1487 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); | |
1488 | if (ret < 0) { | |
1489 | goto error_create; | |
1490 | } | |
1491 | ||
1492 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1493 | &wait_queue->head, head) { | |
1494 | assert(wait_node->app); | |
1495 | ret = lttng_poll_add(&events, wait_node->app->sock, | |
1496 | LPOLLHUP | LPOLLERR); | |
1497 | if (ret < 0) { | |
1498 | goto error; | |
1499 | } | |
1500 | ||
1501 | fd_added = 1; | |
1502 | } | |
1503 | ||
1504 | if (!fd_added) { | |
1505 | goto end; | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | * Poll but don't block so we can quickly identify the faulty events and | |
1510 | * clean them afterwards from the wait queue. | |
1511 | */ | |
1512 | ret = lttng_poll_wait(&events, 0); | |
1513 | if (ret < 0) { | |
1514 | goto error; | |
1515 | } | |
1516 | nb_fd = ret; | |
1517 | ||
1518 | for (i = 0; i < nb_fd; i++) { | |
1519 | /* Get faulty FD. */ | |
1520 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
1521 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
1522 | ||
1523 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1524 | &wait_queue->head, head) { | |
1525 | if (pollfd == wait_node->app->sock && | |
1526 | (revents & (LPOLLHUP | LPOLLERR))) { | |
1527 | cds_list_del(&wait_node->head); | |
1528 | wait_queue->count--; | |
1529 | ust_app_destroy(wait_node->app); | |
1530 | free(wait_node); | |
1531 | break; | |
1532 | } | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | if (nb_fd > 0) { | |
1537 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); | |
1538 | } | |
1539 | ||
1540 | end: | |
1541 | lttng_poll_clean(&events); | |
1542 | return; | |
1543 | ||
1544 | error: | |
1545 | lttng_poll_clean(&events); | |
1546 | error_create: | |
1547 | ERR("Unable to sanitize wait queue"); | |
1548 | return; | |
1549 | } | |
1550 | ||
099e26bd DG |
1551 | /* |
1552 | * Dispatch request from the registration threads to the application | |
1553 | * communication thread. | |
1554 | */ | |
1555 | static void *thread_dispatch_ust_registration(void *data) | |
1556 | { | |
12e2b881 | 1557 | int ret, err = -1; |
099e26bd DG |
1558 | struct cds_wfq_node *node; |
1559 | struct ust_command *ust_cmd = NULL; | |
f45e313d DG |
1560 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
1561 | struct ust_reg_wait_queue wait_queue = { | |
1562 | .count = 0, | |
1563 | }; | |
d0b96690 | 1564 | |
6c71277b | 1565 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
12e2b881 | 1566 | |
a09bf708 MD |
1567 | if (testpoint(sessiond_thread_app_reg_dispatch)) { |
1568 | goto error_testpoint; | |
1569 | } | |
1570 | ||
12e2b881 MD |
1571 | health_code_update(); |
1572 | ||
f45e313d | 1573 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
099e26bd DG |
1574 | |
1575 | DBG("[thread] Dispatch UST command started"); | |
1576 | ||
26c9d55e | 1577 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
12e2b881 MD |
1578 | health_code_update(); |
1579 | ||
099e26bd DG |
1580 | /* Atomically prepare the queue futex */ |
1581 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1582 | ||
1583 | do { | |
d0b96690 | 1584 | struct ust_app *app = NULL; |
7972aab2 | 1585 | ust_cmd = NULL; |
d0b96690 | 1586 | |
f45e313d DG |
1587 | /* |
1588 | * Make sure we don't have node(s) that have hung up before receiving | |
1589 | * the notify socket. This is to clean the list in order to avoid | |
1590 | * memory leaks from notify socket that are never seen. | |
1591 | */ | |
1592 | sanitize_wait_queue(&wait_queue); | |
1593 | ||
12e2b881 | 1594 | health_code_update(); |
099e26bd DG |
1595 | /* Dequeue command for registration */ |
1596 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1597 | if (node == NULL) { | |
00a17c97 | 1598 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1599 | /* Continue thread execution */ |
1600 | break; | |
1601 | } | |
1602 | ||
1603 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1604 | ||
2f50c8a3 DG |
1605 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1606 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1607 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1608 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1609 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1610 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1611 | |
1612 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1613 | wait_node = zmalloc(sizeof(*wait_node)); | |
1614 | if (!wait_node) { | |
1615 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1616 | ret = close(ust_cmd->sock); |
1617 | if (ret < 0) { | |
1618 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1619 | } | |
51dec90d | 1620 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1621 | free(ust_cmd); |
d0b96690 DG |
1622 | goto error; |
1623 | } | |
1624 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1625 | ||
1626 | /* Create application object if socket is CMD. */ | |
1627 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1628 | ust_cmd->sock); | |
1629 | if (!wait_node->app) { | |
1630 | ret = close(ust_cmd->sock); | |
1631 | if (ret < 0) { | |
1632 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1633 | } |
51dec90d | 1634 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1635 | free(wait_node); |
7972aab2 | 1636 | free(ust_cmd); |
d0b96690 DG |
1637 | continue; |
1638 | } | |
1639 | /* | |
1640 | * Add application to the wait queue so we can set the notify | |
1641 | * socket before putting this object in the global ht. | |
1642 | */ | |
f45e313d DG |
1643 | cds_list_add(&wait_node->head, &wait_queue.head); |
1644 | wait_queue.count++; | |
d0b96690 | 1645 | |
7972aab2 | 1646 | free(ust_cmd); |
d0b96690 DG |
1647 | /* |
1648 | * We have to continue here since we don't have the notify | |
1649 | * socket and the application MUST be added to the hash table | |
1650 | * only at that moment. | |
1651 | */ | |
1652 | continue; | |
1653 | } else { | |
1654 | /* | |
1655 | * Look for the application in the local wait queue and set the | |
1656 | * notify socket if found. | |
1657 | */ | |
d88aee68 | 1658 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1659 | &wait_queue.head, head) { |
12e2b881 | 1660 | health_code_update(); |
d0b96690 DG |
1661 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1662 | wait_node->app->notify_sock = ust_cmd->sock; | |
1663 | cds_list_del(&wait_node->head); | |
f45e313d | 1664 | wait_queue.count--; |
d0b96690 DG |
1665 | app = wait_node->app; |
1666 | free(wait_node); | |
1667 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1668 | break; | |
1669 | } | |
1670 | } | |
020d7f60 DG |
1671 | |
1672 | /* | |
1673 | * With no application at this stage the received socket is | |
1674 | * basically useless so close it before we free the cmd data | |
1675 | * structure for good. | |
1676 | */ | |
1677 | if (!app) { | |
1678 | ret = close(ust_cmd->sock); | |
1679 | if (ret < 0) { | |
1680 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1681 | } | |
51dec90d | 1682 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1683 | } |
7972aab2 | 1684 | free(ust_cmd); |
d0b96690 DG |
1685 | } |
1686 | ||
1687 | if (app) { | |
d0b96690 DG |
1688 | /* |
1689 | * @session_lock_list | |
1690 | * | |
1691 | * Lock the global session list so from the register up to the | |
1692 | * registration done message, no thread can see the application | |
1693 | * and change its state. | |
1694 | */ | |
1695 | session_lock_list(); | |
1696 | rcu_read_lock(); | |
d88aee68 | 1697 | |
d0b96690 DG |
1698 | /* |
1699 | * Add application to the global hash table. This needs to be | |
1700 | * done before the update to the UST registry can locate the | |
1701 | * application. | |
1702 | */ | |
1703 | ust_app_add(app); | |
d88aee68 DG |
1704 | |
1705 | /* Set app version. This call will print an error if needed. */ | |
1706 | (void) ust_app_version(app); | |
1707 | ||
1708 | /* Send notify socket through the notify pipe. */ | |
1709 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
1710 | app->notify_sock); | |
1711 | if (ret < 0) { | |
1712 | rcu_read_unlock(); | |
1713 | session_unlock_list(); | |
b85dc84c DG |
1714 | /* |
1715 | * No notify thread, stop the UST tracing. However, this is | |
1716 | * not an internal error of the this thread thus setting | |
1717 | * the health error code to a normal exit. | |
1718 | */ | |
1719 | err = 0; | |
d88aee68 | 1720 | goto error; |
6620da75 | 1721 | } |
d88aee68 | 1722 | |
d0b96690 DG |
1723 | /* |
1724 | * Update newly registered application with the tracing | |
1725 | * registry info already enabled information. | |
1726 | */ | |
1727 | update_ust_app(app->sock); | |
d88aee68 DG |
1728 | |
1729 | /* | |
1730 | * Don't care about return value. Let the manage apps threads | |
1731 | * handle app unregistration upon socket close. | |
1732 | */ | |
1733 | (void) ust_app_register_done(app->sock); | |
1734 | ||
1735 | /* | |
1736 | * Even if the application socket has been closed, send the app | |
1737 | * to the thread and unregistration will take place at that | |
1738 | * place. | |
1739 | */ | |
1740 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 1741 | if (ret < 0) { |
d88aee68 DG |
1742 | rcu_read_unlock(); |
1743 | session_unlock_list(); | |
b85dc84c DG |
1744 | /* |
1745 | * No apps. thread, stop the UST tracing. However, this is | |
1746 | * not an internal error of the this thread thus setting | |
1747 | * the health error code to a normal exit. | |
1748 | */ | |
1749 | err = 0; | |
d88aee68 | 1750 | goto error; |
d0b96690 | 1751 | } |
d88aee68 | 1752 | |
d0b96690 DG |
1753 | rcu_read_unlock(); |
1754 | session_unlock_list(); | |
099e26bd | 1755 | } |
099e26bd DG |
1756 | } while (node != NULL); |
1757 | ||
12e2b881 | 1758 | health_poll_entry(); |
099e26bd DG |
1759 | /* Futex wait on queue. Blocking call on futex() */ |
1760 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 1761 | health_poll_exit(); |
099e26bd | 1762 | } |
12e2b881 MD |
1763 | /* Normal exit, no error */ |
1764 | err = 0; | |
099e26bd DG |
1765 | |
1766 | error: | |
d88aee68 DG |
1767 | /* Clean up wait queue. */ |
1768 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 1769 | &wait_queue.head, head) { |
d88aee68 | 1770 | cds_list_del(&wait_node->head); |
f45e313d | 1771 | wait_queue.count--; |
d88aee68 DG |
1772 | free(wait_node); |
1773 | } | |
1774 | ||
a09bf708 | 1775 | error_testpoint: |
099e26bd | 1776 | DBG("Dispatch thread dying"); |
12e2b881 MD |
1777 | if (err) { |
1778 | health_error(); | |
1779 | ERR("Health error occurred in %s", __func__); | |
1780 | } | |
8782cc74 | 1781 | health_unregister(health_sessiond); |
099e26bd DG |
1782 | return NULL; |
1783 | } | |
1784 | ||
1785 | /* | |
1786 | * This thread manage application registration. | |
1787 | */ | |
1788 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1789 | { |
139ac872 | 1790 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1791 | uint32_t revents, nb_fd; |
1792 | struct lttng_poll_event events; | |
099e26bd DG |
1793 | /* |
1794 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1795 | * freed in the manage apps thread. | |
1796 | */ | |
1797 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1798 | |
099e26bd | 1799 | DBG("[thread] Manage application registration started"); |
1d4b027a | 1800 | |
6c71277b | 1801 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 1802 | |
71ddb4ed | 1803 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
1804 | goto error_testpoint; |
1805 | } | |
8ac94142 | 1806 | |
1d4b027a DG |
1807 | ret = lttcomm_listen_unix_sock(apps_sock); |
1808 | if (ret < 0) { | |
76d7553f | 1809 | goto error_listen; |
1d4b027a DG |
1810 | } |
1811 | ||
5eb91c98 DG |
1812 | /* |
1813 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1814 | * more will be added to this poll set. | |
1815 | */ | |
d0b96690 | 1816 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1817 | if (ret < 0) { |
76d7553f | 1818 | goto error_create_poll; |
5eb91c98 | 1819 | } |
273ea72c | 1820 | |
5eb91c98 DG |
1821 | /* Add the application registration socket */ |
1822 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1823 | if (ret < 0) { | |
76d7553f | 1824 | goto error_poll_add; |
5eb91c98 | 1825 | } |
273ea72c | 1826 | |
1d4b027a | 1827 | /* Notify all applications to register */ |
0fdd1e2c DG |
1828 | ret = notify_ust_apps(1); |
1829 | if (ret < 0) { | |
1830 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1831 | "Execution continues but there might be problem for already\n" |
1832 | "running applications that wishes to register."); | |
0fdd1e2c | 1833 | } |
1d4b027a DG |
1834 | |
1835 | while (1) { | |
1836 | DBG("Accepting application registration"); | |
273ea72c DG |
1837 | |
1838 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1839 | restart: |
a78af745 | 1840 | health_poll_entry(); |
5eb91c98 | 1841 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1842 | health_poll_exit(); |
273ea72c | 1843 | if (ret < 0) { |
88f2b785 MD |
1844 | /* |
1845 | * Restart interrupted system call. | |
1846 | */ | |
1847 | if (errno == EINTR) { | |
1848 | goto restart; | |
1849 | } | |
273ea72c DG |
1850 | goto error; |
1851 | } | |
1852 | ||
0d9c5d77 DG |
1853 | nb_fd = ret; |
1854 | ||
5eb91c98 | 1855 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 1856 | health_code_update(); |
139ac872 | 1857 | |
5eb91c98 DG |
1858 | /* Fetch once the poll data */ |
1859 | revents = LTTNG_POLL_GETEV(&events, i); | |
1860 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1861 | |
5eb91c98 | 1862 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1863 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1864 | if (ret) { |
139ac872 MD |
1865 | err = 0; |
1866 | goto exit; | |
90014c57 | 1867 | } |
1d4b027a | 1868 | |
5eb91c98 DG |
1869 | /* Event on the registration socket */ |
1870 | if (pollfd == apps_sock) { | |
1871 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1872 | ERR("Register apps socket poll error"); | |
1873 | goto error; | |
1874 | } else if (revents & LPOLLIN) { | |
1875 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1876 | if (sock < 0) { | |
1877 | goto error; | |
1878 | } | |
099e26bd | 1879 | |
16c5c8fa DG |
1880 | /* |
1881 | * Set socket timeout for both receiving and ending. | |
1882 | * app_socket_timeout is in seconds, whereas | |
1883 | * lttcomm_setsockopt_rcv_timeout and | |
1884 | * lttcomm_setsockopt_snd_timeout expect msec as | |
1885 | * parameter. | |
1886 | */ | |
1887 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
1888 | app_socket_timeout * 1000); | |
1889 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
1890 | app_socket_timeout * 1000); | |
1891 | ||
b662582b DG |
1892 | /* |
1893 | * Set the CLOEXEC flag. Return code is useless because | |
1894 | * either way, the show must go on. | |
1895 | */ | |
1896 | (void) utils_set_fd_cloexec(sock); | |
1897 | ||
5eb91c98 | 1898 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1899 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1900 | if (ust_cmd == NULL) { |
76d7553f | 1901 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
1902 | goto error; |
1903 | } | |
1d4b027a | 1904 | |
5eb91c98 DG |
1905 | /* |
1906 | * Using message-based transmissions to ensure we don't | |
1907 | * have to deal with partially received messages. | |
1908 | */ | |
4063050c MD |
1909 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1910 | if (ret < 0) { | |
1911 | ERR("Exhausted file descriptors allowed for applications."); | |
1912 | free(ust_cmd); | |
1913 | ret = close(sock); | |
1914 | if (ret) { | |
1915 | PERROR("close"); | |
1916 | } | |
1917 | sock = -1; | |
1918 | continue; | |
1919 | } | |
d88aee68 | 1920 | |
840cb59c | 1921 | health_code_update(); |
d0b96690 DG |
1922 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
1923 | if (ret < 0) { | |
5eb91c98 | 1924 | free(ust_cmd); |
d0b96690 | 1925 | /* Close socket of the application. */ |
76d7553f MD |
1926 | ret = close(sock); |
1927 | if (ret) { | |
1928 | PERROR("close"); | |
1929 | } | |
4063050c | 1930 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1931 | sock = -1; |
5eb91c98 DG |
1932 | continue; |
1933 | } | |
840cb59c | 1934 | health_code_update(); |
099e26bd | 1935 | |
5eb91c98 | 1936 | ust_cmd->sock = sock; |
34a2494f | 1937 | sock = -1; |
099e26bd | 1938 | |
5eb91c98 DG |
1939 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1940 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1941 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1942 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1943 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1944 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1945 | |
5eb91c98 DG |
1946 | /* |
1947 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1948 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1949 | */ |
1950 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1951 | ||
1952 | /* | |
1953 | * Wake the registration queue futex. Implicit memory | |
1954 | * barrier with the exchange in cds_wfq_enqueue. | |
1955 | */ | |
1956 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1957 | } | |
1958 | } | |
90014c57 | 1959 | } |
1d4b027a DG |
1960 | } |
1961 | ||
139ac872 | 1962 | exit: |
1d4b027a | 1963 | error: |
0fdd1e2c DG |
1964 | /* Notify that the registration thread is gone */ |
1965 | notify_ust_apps(0); | |
1966 | ||
a4b35e07 | 1967 | if (apps_sock >= 0) { |
76d7553f MD |
1968 | ret = close(apps_sock); |
1969 | if (ret) { | |
1970 | PERROR("close"); | |
1971 | } | |
a4b35e07 | 1972 | } |
46c3f085 | 1973 | if (sock >= 0) { |
76d7553f MD |
1974 | ret = close(sock); |
1975 | if (ret) { | |
1976 | PERROR("close"); | |
1977 | } | |
4063050c | 1978 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1979 | } |
273ea72c | 1980 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1981 | |
76d7553f | 1982 | error_poll_add: |
5eb91c98 | 1983 | lttng_poll_clean(&events); |
76d7553f MD |
1984 | error_listen: |
1985 | error_create_poll: | |
6993eeb3 | 1986 | error_testpoint: |
76d7553f | 1987 | DBG("UST Registration thread cleanup complete"); |
a09bf708 MD |
1988 | if (err) { |
1989 | health_error(); | |
1990 | ERR("Health error occurred in %s", __func__); | |
1991 | } | |
8782cc74 | 1992 | health_unregister(health_sessiond); |
5eb91c98 | 1993 | |
1d4b027a DG |
1994 | return NULL; |
1995 | } | |
1996 | ||
8c0faa1d | 1997 | /* |
3bd1e081 | 1998 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1999 | * exec or it will fails. |
8c0faa1d | 2000 | */ |
3bd1e081 | 2001 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 2002 | { |
a23ec3a7 | 2003 | int ret, clock_ret; |
ee0b0061 DG |
2004 | struct timespec timeout; |
2005 | ||
a23ec3a7 DG |
2006 | /* Make sure we set the readiness flag to 0 because we are NOT ready */ |
2007 | consumer_data->consumer_thread_is_ready = 0; | |
8c0faa1d | 2008 | |
a23ec3a7 DG |
2009 | /* Setup pthread condition */ |
2010 | ret = pthread_condattr_init(&consumer_data->condattr); | |
2011 | if (ret != 0) { | |
2012 | errno = ret; | |
2013 | PERROR("pthread_condattr_init consumer data"); | |
2014 | goto error; | |
2015 | } | |
2016 | ||
2017 | /* | |
2018 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
2019 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
2020 | * for a more details and how we noticed it. | |
2021 | */ | |
2022 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
2023 | if (ret != 0) { | |
2024 | errno = ret; | |
2025 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
2026 | goto error; |
2027 | } | |
8c0faa1d | 2028 | |
a23ec3a7 DG |
2029 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
2030 | if (ret != 0) { | |
2031 | errno = ret; | |
2032 | PERROR("pthread_cond_init consumer data"); | |
2033 | goto error; | |
2034 | } | |
2035 | ||
2036 | ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer, | |
2037 | consumer_data); | |
8c0faa1d | 2038 | if (ret != 0) { |
3bd1e081 | 2039 | PERROR("pthread_create consumer"); |
ee0b0061 | 2040 | ret = -1; |
8c0faa1d DG |
2041 | goto error; |
2042 | } | |
2043 | ||
a23ec3a7 DG |
2044 | /* We are about to wait on a pthread condition */ |
2045 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2046 | ||
ee0b0061 | 2047 | /* Get time for sem_timedwait absolute timeout */ |
a23ec3a7 DG |
2048 | clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
2049 | /* | |
2050 | * Set the timeout for the condition timed wait even if the clock gettime | |
2051 | * call fails since we might loop on that call and we want to avoid to | |
2052 | * increment the timeout too many times. | |
2053 | */ | |
2054 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2055 | ||
2056 | /* | |
2057 | * The following loop COULD be skipped in some conditions so this is why we | |
2058 | * set ret to 0 in order to make sure at least one round of the loop is | |
2059 | * done. | |
2060 | */ | |
2061 | ret = 0; | |
2062 | ||
2063 | /* | |
2064 | * Loop until the condition is reached or when a timeout is reached. Note | |
2065 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2066 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2067 | * possible. This loop does not take any chances and works with both of | |
2068 | * them. | |
2069 | */ | |
2070 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2071 | if (clock_ret < 0) { | |
2072 | PERROR("clock_gettime spawn consumer"); | |
2073 | /* Infinite wait for the consumerd thread to be ready */ | |
2074 | ret = pthread_cond_wait(&consumer_data->cond, | |
2075 | &consumer_data->cond_mutex); | |
2076 | } else { | |
2077 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2078 | &consumer_data->cond_mutex, &timeout); | |
2079 | } | |
ee0b0061 | 2080 | } |
8c0faa1d | 2081 | |
a23ec3a7 DG |
2082 | /* Release the pthread condition */ |
2083 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2084 | ||
2085 | if (ret != 0) { | |
2086 | errno = ret; | |
2087 | if (ret == ETIMEDOUT) { | |
ee0b0061 DG |
2088 | /* |
2089 | * Call has timed out so we kill the kconsumerd_thread and return | |
2090 | * an error. | |
2091 | */ | |
a23ec3a7 DG |
2092 | ERR("Condition timed out. The consumer thread was never ready." |
2093 | " Killing it"); | |
3bd1e081 | 2094 | ret = pthread_cancel(consumer_data->thread); |
ee0b0061 | 2095 | if (ret < 0) { |
3bd1e081 | 2096 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2097 | } |
2098 | } else { | |
a23ec3a7 | 2099 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 DG |
2100 | } |
2101 | goto error; | |
2102 | } | |
2103 | ||
3bd1e081 MD |
2104 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2105 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2106 | ERR("Consumerd did not start"); |
3bd1e081 | 2107 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2108 | goto error; |
2109 | } | |
3bd1e081 | 2110 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2111 | |
8c0faa1d DG |
2112 | return 0; |
2113 | ||
2114 | error: | |
2115 | return ret; | |
2116 | } | |
2117 | ||
d9800920 | 2118 | /* |
3bd1e081 | 2119 | * Join consumer thread |
d9800920 | 2120 | */ |
3bd1e081 | 2121 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2122 | { |
2123 | void *status; | |
cf3af59e | 2124 | |
e8209f6b DG |
2125 | /* Consumer pid must be a real one. */ |
2126 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2127 | int ret; |
3bd1e081 | 2128 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2129 | if (ret) { |
3bd1e081 | 2130 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
2131 | return ret; |
2132 | } | |
3bd1e081 | 2133 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2134 | } else { |
2135 | return 0; | |
2136 | } | |
2137 | } | |
2138 | ||
8c0faa1d | 2139 | /* |
3bd1e081 | 2140 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2141 | * |
d063d709 | 2142 | * Return pid if successful else -1. |
8c0faa1d | 2143 | */ |
3bd1e081 | 2144 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2145 | { |
2146 | int ret; | |
2147 | pid_t pid; | |
94c55f17 | 2148 | const char *consumer_to_use; |
53086306 | 2149 | const char *verbosity; |
94c55f17 | 2150 | struct stat st; |
8c0faa1d | 2151 | |
3bd1e081 | 2152 | DBG("Spawning consumerd"); |
c49dc785 | 2153 | |
8c0faa1d DG |
2154 | pid = fork(); |
2155 | if (pid == 0) { | |
2156 | /* | |
3bd1e081 | 2157 | * Exec consumerd. |
8c0faa1d | 2158 | */ |
daee5345 | 2159 | if (opt_verbose_consumer) { |
53086306 DG |
2160 | verbosity = "--verbose"; |
2161 | } else { | |
2162 | verbosity = "--quiet"; | |
2163 | } | |
3bd1e081 MD |
2164 | switch (consumer_data->type) { |
2165 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2166 | /* |
c7704d57 DG |
2167 | * Find out which consumerd to execute. We will first try the |
2168 | * 64-bit path, then the sessiond's installation directory, and | |
2169 | * fallback on the 32-bit one, | |
94c55f17 | 2170 | */ |
63a799e8 AM |
2171 | DBG3("Looking for a kernel consumer at these locations:"); |
2172 | DBG3(" 1) %s", consumerd64_bin); | |
2173 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
2174 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 2175 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 2176 | DBG3("Found location #1"); |
94c55f17 | 2177 | consumer_to_use = consumerd64_bin; |
94c55f17 | 2178 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 2179 | DBG3("Found location #2"); |
94c55f17 | 2180 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 2181 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 2182 | DBG3("Found location #3"); |
eb1e0bd4 | 2183 | consumer_to_use = consumerd32_bin; |
94c55f17 | 2184 | } else { |
63a799e8 | 2185 | DBG("Could not find any valid consumerd executable"); |
94c55f17 AM |
2186 | break; |
2187 | } | |
2188 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
2189 | execl(consumer_to_use, | |
2190 | "lttng-consumerd", verbosity, "-k", | |
2191 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2192 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2193 | "--group", tracing_group_name, |
94c55f17 | 2194 | NULL); |
3bd1e081 | 2195 | break; |
7753dea8 MD |
2196 | case LTTNG_CONSUMER64_UST: |
2197 | { | |
b1e0b6b6 | 2198 | char *tmpnew = NULL; |
8f4905da MD |
2199 | |
2200 | if (consumerd64_libdir[0] != '\0') { | |
2201 | char *tmp; | |
2202 | size_t tmplen; | |
2203 | ||
2204 | tmp = getenv("LD_LIBRARY_PATH"); | |
2205 | if (!tmp) { | |
2206 | tmp = ""; | |
2207 | } | |
2208 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2209 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
2210 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2211 | if (!tmpnew) { | |
2212 | ret = -ENOMEM; | |
2213 | goto error; | |
2214 | } | |
2215 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2216 | strcat(tmpnew, consumerd64_libdir); | |
2217 | if (tmp[0] != '\0') { | |
2218 | strcat(tmpnew, ":"); | |
2219 | strcat(tmpnew, tmp); | |
2220 | } | |
2221 | ret = putenv(tmpnew); | |
2222 | if (ret) { | |
2223 | ret = -errno; | |
c6f76da9 | 2224 | free(tmpnew); |
8f4905da MD |
2225 | goto error; |
2226 | } | |
2227 | } | |
94c55f17 | 2228 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 2229 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2230 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2231 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2232 | "--group", tracing_group_name, |
7753dea8 | 2233 | NULL); |
8f4905da MD |
2234 | if (consumerd64_libdir[0] != '\0') { |
2235 | free(tmpnew); | |
2236 | } | |
2237 | if (ret) { | |
2238 | goto error; | |
2239 | } | |
3bd1e081 | 2240 | break; |
7753dea8 MD |
2241 | } |
2242 | case LTTNG_CONSUMER32_UST: | |
2243 | { | |
937dde8e | 2244 | char *tmpnew = NULL; |
8f4905da MD |
2245 | |
2246 | if (consumerd32_libdir[0] != '\0') { | |
2247 | char *tmp; | |
2248 | size_t tmplen; | |
2249 | ||
2250 | tmp = getenv("LD_LIBRARY_PATH"); | |
2251 | if (!tmp) { | |
2252 | tmp = ""; | |
2253 | } | |
2254 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2255 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
2256 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2257 | if (!tmpnew) { | |
2258 | ret = -ENOMEM; | |
2259 | goto error; | |
2260 | } | |
2261 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2262 | strcat(tmpnew, consumerd32_libdir); | |
2263 | if (tmp[0] != '\0') { | |
2264 | strcat(tmpnew, ":"); | |
2265 | strcat(tmpnew, tmp); | |
2266 | } | |
2267 | ret = putenv(tmpnew); | |
2268 | if (ret) { | |
2269 | ret = -errno; | |
c6f76da9 | 2270 | free(tmpnew); |
8f4905da MD |
2271 | goto error; |
2272 | } | |
2273 | } | |
94c55f17 | 2274 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 2275 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2276 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2277 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2278 | "--group", tracing_group_name, |
7753dea8 | 2279 | NULL); |
8f4905da MD |
2280 | if (consumerd32_libdir[0] != '\0') { |
2281 | free(tmpnew); | |
2282 | } | |
2283 | if (ret) { | |
2284 | goto error; | |
2285 | } | |
7753dea8 MD |
2286 | break; |
2287 | } | |
3bd1e081 | 2288 | default: |
76d7553f | 2289 | PERROR("unknown consumer type"); |
3bd1e081 MD |
2290 | exit(EXIT_FAILURE); |
2291 | } | |
8c0faa1d | 2292 | if (errno != 0) { |
76d7553f | 2293 | PERROR("kernel start consumer exec"); |
8c0faa1d DG |
2294 | } |
2295 | exit(EXIT_FAILURE); | |
2296 | } else if (pid > 0) { | |
2297 | ret = pid; | |
8c0faa1d | 2298 | } else { |
76d7553f | 2299 | PERROR("start consumer fork"); |
8c0faa1d | 2300 | ret = -errno; |
8c0faa1d | 2301 | } |
8f4905da | 2302 | error: |
8c0faa1d DG |
2303 | return ret; |
2304 | } | |
2305 | ||
693bd40b | 2306 | /* |
3bd1e081 | 2307 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2308 | */ |
3bd1e081 | 2309 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2310 | { |
c617c0c6 | 2311 | int ret; |
edb8b045 DG |
2312 | |
2313 | /* | |
2314 | * Set the listen() state on the socket since there is a possible race | |
2315 | * between the exec() of the consumer daemon and this call if place in the | |
2316 | * consumer thread. See bug #366 for more details. | |
2317 | */ | |
2318 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2319 | if (ret < 0) { | |
2320 | goto error; | |
2321 | } | |
693bd40b | 2322 | |
3bd1e081 MD |
2323 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2324 | if (consumer_data->pid != 0) { | |
2325 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2326 | goto end; |
2327 | } | |
693bd40b | 2328 | |
3bd1e081 | 2329 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2330 | if (ret < 0) { |
3bd1e081 MD |
2331 | ERR("Spawning consumerd failed"); |
2332 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2333 | goto error; |
693bd40b | 2334 | } |
c49dc785 | 2335 | |
3bd1e081 MD |
2336 | /* Setting up the consumer_data pid */ |
2337 | consumer_data->pid = ret; | |
48842b30 | 2338 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2339 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2340 | |
3bd1e081 MD |
2341 | DBG2("Spawning consumer control thread"); |
2342 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2343 | if (ret < 0) { |
3bd1e081 | 2344 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2345 | goto error; |
2346 | } | |
2347 | ||
c49dc785 | 2348 | end: |
693bd40b DG |
2349 | return 0; |
2350 | ||
2351 | error: | |
331744e3 | 2352 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2353 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2354 | int err; |
2355 | ||
edb8b045 DG |
2356 | err = close(consumer_data->err_sock); |
2357 | if (err < 0) { | |
2358 | PERROR("close consumer data error socket"); | |
2359 | } | |
2360 | } | |
693bd40b DG |
2361 | return ret; |
2362 | } | |
2363 | ||
b73401da | 2364 | /* |
096102bd | 2365 | * Setup necessary data for kernel tracer action. |
b73401da | 2366 | */ |
096102bd | 2367 | static int init_kernel_tracer(void) |
b73401da DG |
2368 | { |
2369 | int ret; | |
b73401da | 2370 | |
096102bd DG |
2371 | /* Modprobe lttng kernel modules */ |
2372 | ret = modprobe_lttng_control(); | |
b73401da | 2373 | if (ret < 0) { |
b73401da DG |
2374 | goto error; |
2375 | } | |
2376 | ||
096102bd DG |
2377 | /* Open debugfs lttng */ |
2378 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2379 | if (kernel_tracer_fd < 0) { | |
2380 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b DG |
2381 | ret = -1; |
2382 | goto error_open; | |
54d01ffb DG |
2383 | } |
2384 | ||
2f77fc4b DG |
2385 | /* Validate kernel version */ |
2386 | ret = kernel_validate_version(kernel_tracer_fd); | |
2387 | if (ret < 0) { | |
2388 | goto error_version; | |
b551a063 | 2389 | } |
54d01ffb | 2390 | |
2f77fc4b DG |
2391 | ret = modprobe_lttng_data(); |
2392 | if (ret < 0) { | |
2393 | goto error_modules; | |
54d01ffb DG |
2394 | } |
2395 | ||
2f77fc4b DG |
2396 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2397 | return 0; | |
2398 | ||
2399 | error_version: | |
2400 | modprobe_remove_lttng_control(); | |
2401 | ret = close(kernel_tracer_fd); | |
2402 | if (ret) { | |
2403 | PERROR("close"); | |
b551a063 | 2404 | } |
2f77fc4b | 2405 | kernel_tracer_fd = -1; |
f73fabfd | 2406 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2407 | |
2f77fc4b DG |
2408 | error_modules: |
2409 | ret = close(kernel_tracer_fd); | |
2410 | if (ret) { | |
2411 | PERROR("close"); | |
b551a063 | 2412 | } |
54d01ffb | 2413 | |
2f77fc4b DG |
2414 | error_open: |
2415 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2416 | |
2417 | error: | |
2f77fc4b DG |
2418 | WARN("No kernel tracer available"); |
2419 | kernel_tracer_fd = -1; | |
2420 | if (!is_root) { | |
f73fabfd | 2421 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2422 | } else { |
f73fabfd | 2423 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2424 | } |
54d01ffb DG |
2425 | } |
2426 | ||
2f77fc4b | 2427 | |
54d01ffb | 2428 | /* |
2f77fc4b DG |
2429 | * Copy consumer output from the tracing session to the domain session. The |
2430 | * function also applies the right modification on a per domain basis for the | |
2431 | * trace files destination directory. | |
36b588ed MD |
2432 | * |
2433 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2434 | */ |
2f77fc4b | 2435 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2436 | { |
2437 | int ret; | |
2f77fc4b DG |
2438 | const char *dir_name; |
2439 | struct consumer_output *consumer; | |
2440 | ||
2441 | assert(session); | |
2442 | assert(session->consumer); | |
54d01ffb | 2443 | |
b551a063 DG |
2444 | switch (domain) { |
2445 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2446 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2447 | /* |
2448 | * XXX: We should audit the session creation and what this function | |
2449 | * does "extra" in order to avoid a destroy since this function is used | |
2450 | * in the domain session creation (kernel and ust) only. Same for UST | |
2451 | * domain. | |
2452 | */ | |
2453 | if (session->kernel_session->consumer) { | |
2454 | consumer_destroy_output(session->kernel_session->consumer); | |
2455 | } | |
2f77fc4b DG |
2456 | session->kernel_session->consumer = |
2457 | consumer_copy_output(session->consumer); | |
2458 | /* Ease our life a bit for the next part */ | |
2459 | consumer = session->kernel_session->consumer; | |
2460 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2461 | break; |
f20baf8e | 2462 | case LTTNG_DOMAIN_JUL: |
b551a063 | 2463 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2464 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd DG |
2465 | if (session->ust_session->consumer) { |
2466 | consumer_destroy_output(session->ust_session->consumer); | |
2467 | } | |
2f77fc4b DG |
2468 | session->ust_session->consumer = |
2469 | consumer_copy_output(session->consumer); | |
2470 | /* Ease our life a bit for the next part */ | |
2471 | consumer = session->ust_session->consumer; | |
2472 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2473 | break; |
2474 | default: | |
f73fabfd | 2475 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2476 | goto error; |
2477 | } | |
2478 | ||
2f77fc4b | 2479 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2480 | strncat(consumer->subdir, dir_name, |
2481 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2482 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2483 | ||
f73fabfd | 2484 | ret = LTTNG_OK; |
54d01ffb DG |
2485 | |
2486 | error: | |
2487 | return ret; | |
2488 | } | |
2489 | ||
00e2e675 | 2490 | /* |
2f77fc4b | 2491 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2492 | * |
2493 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2494 | */ |
2f77fc4b DG |
2495 | static int create_ust_session(struct ltt_session *session, |
2496 | struct lttng_domain *domain) | |
00e2e675 DG |
2497 | { |
2498 | int ret; | |
2f77fc4b | 2499 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2500 | |
a4b92340 | 2501 | assert(session); |
2f77fc4b DG |
2502 | assert(domain); |
2503 | assert(session->consumer); | |
a4b92340 | 2504 | |
2f77fc4b | 2505 | switch (domain->type) { |
f20baf8e | 2506 | case LTTNG_DOMAIN_JUL: |
2f77fc4b DG |
2507 | case LTTNG_DOMAIN_UST: |
2508 | break; | |
2509 | default: | |
2510 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2511 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2512 | goto error; |
2513 | } | |
2514 | ||
2f77fc4b DG |
2515 | DBG("Creating UST session"); |
2516 | ||
dec56f6c | 2517 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2518 | if (lus == NULL) { |
f73fabfd | 2519 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2520 | goto error; |
2521 | } | |
2522 | ||
2f77fc4b DG |
2523 | lus->uid = session->uid; |
2524 | lus->gid = session->gid; | |
2bba9e53 | 2525 | lus->output_traces = session->output_traces; |
27babd3a | 2526 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2527 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2528 | session->ust_session = lus; |
00e2e675 | 2529 | |
2f77fc4b DG |
2530 | /* Copy session output to the newly created UST session */ |
2531 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2532 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2533 | goto error; |
2534 | } | |
2535 | ||
f73fabfd | 2536 | return LTTNG_OK; |
00e2e675 DG |
2537 | |
2538 | error: | |
2f77fc4b DG |
2539 | free(lus); |
2540 | session->ust_session = NULL; | |
00e2e675 DG |
2541 | return ret; |
2542 | } | |
2543 | ||
2544 | /* | |
2f77fc4b | 2545 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2546 | */ |
2f77fc4b | 2547 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2548 | { |
2549 | int ret; | |
a4b92340 | 2550 | |
2f77fc4b | 2551 | DBG("Creating kernel session"); |
00e2e675 | 2552 | |
2f77fc4b DG |
2553 | ret = kernel_create_session(session, kernel_tracer_fd); |
2554 | if (ret < 0) { | |
f73fabfd | 2555 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2556 | goto error; |
2557 | } | |
2558 | ||
2f77fc4b DG |
2559 | /* Code flow safety */ |
2560 | assert(session->kernel_session); | |
2561 | ||
2562 | /* Copy session output to the newly created Kernel session */ | |
2563 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2564 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2565 | goto error; |
2566 | } | |
2567 | ||
2f77fc4b DG |
2568 | /* Create directory(ies) on local filesystem. */ |
2569 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && | |
2570 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
2571 | ret = run_as_mkdir_recursive( | |
2572 | session->kernel_session->consumer->dst.trace_path, | |
2573 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2574 | if (ret < 0) { | |
2575 | if (ret != -EEXIST) { | |
2576 | ERR("Trace directory creation error"); | |
00e2e675 DG |
2577 | goto error; |
2578 | } | |
00e2e675 | 2579 | } |
2f77fc4b | 2580 | } |
00e2e675 | 2581 | |
2f77fc4b DG |
2582 | session->kernel_session->uid = session->uid; |
2583 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2584 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2585 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2586 | |
f73fabfd | 2587 | return LTTNG_OK; |
00e2e675 | 2588 | |
2f77fc4b DG |
2589 | error: |
2590 | trace_kernel_destroy_session(session->kernel_session); | |
2591 | session->kernel_session = NULL; | |
2592 | return ret; | |
2593 | } | |
00e2e675 | 2594 | |
2f77fc4b DG |
2595 | /* |
2596 | * Count number of session permitted by uid/gid. | |
2597 | */ | |
2598 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2599 | { | |
2600 | unsigned int i = 0; | |
2601 | struct ltt_session *session; | |
07424f16 | 2602 | |
2f77fc4b DG |
2603 | DBG("Counting number of available session for UID %d GID %d", |
2604 | uid, gid); | |
2605 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2606 | /* |
2f77fc4b | 2607 | * Only list the sessions the user can control. |
00e2e675 | 2608 | */ |
2f77fc4b DG |
2609 | if (!session_access_ok(session, uid, gid)) { |
2610 | continue; | |
2611 | } | |
2612 | i++; | |
a4b92340 | 2613 | } |
2f77fc4b | 2614 | return i; |
00e2e675 DG |
2615 | } |
2616 | ||
54d01ffb DG |
2617 | /* |
2618 | * Process the command requested by the lttng client within the command | |
2619 | * context structure. This function make sure that the return structure (llm) | |
2620 | * is set and ready for transmission before returning. | |
2621 | * | |
2622 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2623 | * |
2624 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2625 | * |
2626 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2627 | */ |
53a80697 MD |
2628 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2629 | int *sock_error) | |
54d01ffb | 2630 | { |
f73fabfd | 2631 | int ret = LTTNG_OK; |
44d3bd01 | 2632 | int need_tracing_session = 1; |
2e09ba09 | 2633 | int need_domain; |
54d01ffb DG |
2634 | |
2635 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2636 | ||
53a80697 MD |
2637 | *sock_error = 0; |
2638 | ||
2e09ba09 MD |
2639 | switch (cmd_ctx->lsm->cmd_type) { |
2640 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2641 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2642 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2643 | case LTTNG_DESTROY_SESSION: |
2644 | case LTTNG_LIST_SESSIONS: | |
2645 | case LTTNG_LIST_DOMAINS: | |
2646 | case LTTNG_START_TRACE: | |
2647 | case LTTNG_STOP_TRACE: | |
6d805429 | 2648 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
2649 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
2650 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
2651 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
2652 | case LTTNG_SNAPSHOT_RECORD: | |
2e09ba09 | 2653 | need_domain = 0; |
3aace903 | 2654 | break; |
2e09ba09 MD |
2655 | default: |
2656 | need_domain = 1; | |
2657 | } | |
2658 | ||
2659 | if (opt_no_kernel && need_domain | |
2660 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 | 2661 | if (!is_root) { |
f73fabfd | 2662 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 2663 | } else { |
f73fabfd | 2664 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 2665 | } |
4fba7219 DG |
2666 | goto error; |
2667 | } | |
2668 | ||
8d3113b2 DG |
2669 | /* Deny register consumer if we already have a spawned consumer. */ |
2670 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
2671 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
2672 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 2673 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 2674 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
2675 | goto error; |
2676 | } | |
2677 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
2678 | } | |
2679 | ||
54d01ffb DG |
2680 | /* |
2681 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2682 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2683 | * command. |
2684 | */ | |
2685 | switch(cmd_ctx->lsm->cmd_type) { | |
2686 | case LTTNG_LIST_SESSIONS: | |
2687 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2688 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
2689 | case LTTNG_LIST_DOMAINS: |
2690 | case LTTNG_LIST_CHANNELS: | |
2691 | case LTTNG_LIST_EVENTS: | |
2692 | break; | |
2693 | default: | |
2694 | /* Setup lttng message with no payload */ | |
2695 | ret = setup_lttng_msg(cmd_ctx, 0); | |
2696 | if (ret < 0) { | |
2697 | /* This label does not try to unlock the session */ | |
2698 | goto init_setup_error; | |
2699 | } | |
2700 | } | |
2701 | ||
2702 | /* Commands that DO NOT need a session. */ | |
2703 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 2704 | case LTTNG_CREATE_SESSION: |
27babd3a | 2705 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2706 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 | 2707 | case LTTNG_CALIBRATE: |
54d01ffb DG |
2708 | case LTTNG_LIST_SESSIONS: |
2709 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2710 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
44d3bd01 | 2711 | need_tracing_session = 0; |
54d01ffb DG |
2712 | break; |
2713 | default: | |
2714 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
2715 | /* |
2716 | * We keep the session list lock across _all_ commands | |
2717 | * for now, because the per-session lock does not | |
2718 | * handle teardown properly. | |
2719 | */ | |
74babd95 | 2720 | session_lock_list(); |
54d01ffb DG |
2721 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
2722 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 2723 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
2724 | goto error; |
2725 | } else { | |
2726 | /* Acquire lock for the session */ | |
2727 | session_lock(cmd_ctx->session); | |
2728 | } | |
2729 | break; | |
2730 | } | |
b389abbe | 2731 | |
2e09ba09 MD |
2732 | if (!need_domain) { |
2733 | goto skip_domain; | |
2734 | } | |
a4b92340 | 2735 | |
54d01ffb DG |
2736 | /* |
2737 | * Check domain type for specific "pre-action". | |
2738 | */ | |
2739 | switch (cmd_ctx->lsm->domain.type) { | |
2740 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 2741 | if (!is_root) { |
f73fabfd | 2742 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
2743 | goto error; |
2744 | } | |
2745 | ||
54d01ffb | 2746 | /* Kernel tracer check */ |
a4b35e07 | 2747 | if (kernel_tracer_fd == -1) { |
54d01ffb | 2748 | /* Basically, load kernel tracer modules */ |
096102bd DG |
2749 | ret = init_kernel_tracer(); |
2750 | if (ret != 0) { | |
54d01ffb DG |
2751 | goto error; |
2752 | } | |
2753 | } | |
5eb91c98 | 2754 | |
5c827ce0 DG |
2755 | /* Consumer is in an ERROR state. Report back to client */ |
2756 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2757 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2758 | goto error; |
2759 | } | |
2760 | ||
54d01ffb | 2761 | /* Need a session for kernel command */ |
44d3bd01 | 2762 | if (need_tracing_session) { |
54d01ffb | 2763 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 2764 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 2765 | if (ret < 0) { |
f73fabfd | 2766 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
2767 | goto error; |
2768 | } | |
b389abbe | 2769 | } |
7d29a247 | 2770 | |
54d01ffb | 2771 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
2772 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
2773 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 2774 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
2775 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
2776 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 2777 | if (ret < 0) { |
f73fabfd | 2778 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 2779 | goto error; |
950131af | 2780 | } |
5c827ce0 | 2781 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
2782 | } else { |
2783 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 2784 | } |
173af62f | 2785 | |
a4b92340 DG |
2786 | /* |
2787 | * The consumer was just spawned so we need to add the socket to | |
2788 | * the consumer output of the session if exist. | |
2789 | */ | |
2790 | ret = consumer_create_socket(&kconsumer_data, | |
2791 | cmd_ctx->session->kernel_session->consumer); | |
2792 | if (ret < 0) { | |
2793 | goto error; | |
173af62f | 2794 | } |
0d0c377a | 2795 | } |
5c827ce0 | 2796 | |
54d01ffb | 2797 | break; |
b9dfb167 | 2798 | case LTTNG_DOMAIN_JUL: |
2bdd86d4 | 2799 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 2800 | { |
b51ec5b4 MD |
2801 | if (!ust_app_supported()) { |
2802 | ret = LTTNG_ERR_NO_UST; | |
2803 | goto error; | |
2804 | } | |
5c827ce0 DG |
2805 | /* Consumer is in an ERROR state. Report back to client */ |
2806 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2807 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2808 | goto error; |
2809 | } | |
2810 | ||
44d3bd01 | 2811 | if (need_tracing_session) { |
a4b92340 | 2812 | /* Create UST session if none exist. */ |
f6a9efaa | 2813 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 2814 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 2815 | &cmd_ctx->lsm->domain); |
f73fabfd | 2816 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
2817 | goto error; |
2818 | } | |
2819 | } | |
00e2e675 | 2820 | |
7753dea8 MD |
2821 | /* Start the UST consumer daemons */ |
2822 | /* 64-bit */ | |
2823 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 2824 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 2825 | ustconsumer64_data.pid == 0 && |
785d2d0d | 2826 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2827 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2828 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 2829 | if (ret < 0) { |
f73fabfd | 2830 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 2831 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
2832 | goto error; |
2833 | } | |
48842b30 | 2834 | |
173af62f | 2835 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 2836 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 2837 | } else { |
7753dea8 MD |
2838 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2839 | } | |
173af62f DG |
2840 | |
2841 | /* | |
2842 | * Setup socket for consumer 64 bit. No need for atomic access | |
2843 | * since it was set above and can ONLY be set in this thread. | |
2844 | */ | |
a4b92340 DG |
2845 | ret = consumer_create_socket(&ustconsumer64_data, |
2846 | cmd_ctx->session->ust_session->consumer); | |
2847 | if (ret < 0) { | |
2848 | goto error; | |
173af62f DG |
2849 | } |
2850 | ||
7753dea8 | 2851 | /* 32-bit */ |
fc7a59ce | 2852 | if (consumerd32_bin[0] != '\0' && |
7753dea8 | 2853 | ustconsumer32_data.pid == 0 && |
785d2d0d | 2854 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2855 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
2856 | ret = start_consumerd(&ustconsumer32_data); | |
2857 | if (ret < 0) { | |
f73fabfd | 2858 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 2859 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
2860 | goto error; |
2861 | } | |
5c827ce0 | 2862 | |
173af62f | 2863 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 2864 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
2865 | } else { |
2866 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 2867 | } |
173af62f DG |
2868 | |
2869 | /* | |
2870 | * Setup socket for consumer 64 bit. No need for atomic access | |
2871 | * since it was set above and can ONLY be set in this thread. | |
2872 | */ | |
a4b92340 DG |
2873 | ret = consumer_create_socket(&ustconsumer32_data, |
2874 | cmd_ctx->session->ust_session->consumer); | |
2875 | if (ret < 0) { | |
2876 | goto error; | |
173af62f | 2877 | } |
44d3bd01 DG |
2878 | } |
2879 | break; | |
48842b30 | 2880 | } |
54d01ffb | 2881 | default: |
54d01ffb DG |
2882 | break; |
2883 | } | |
2e09ba09 | 2884 | skip_domain: |
33a2b854 | 2885 | |
5c827ce0 DG |
2886 | /* Validate consumer daemon state when start/stop trace command */ |
2887 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
2888 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
2889 | switch (cmd_ctx->lsm->domain.type) { | |
b9dfb167 | 2890 | case LTTNG_DOMAIN_JUL: |
5c827ce0 DG |
2891 | case LTTNG_DOMAIN_UST: |
2892 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2893 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2894 | goto error; |
2895 | } | |
2896 | break; | |
2897 | case LTTNG_DOMAIN_KERNEL: | |
2898 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2899 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2900 | goto error; |
2901 | } | |
2902 | break; | |
2903 | } | |
2904 | } | |
2905 | ||
8e0af1b4 MD |
2906 | /* |
2907 | * Check that the UID or GID match that of the tracing session. | |
2908 | * The root user can interact with all sessions. | |
2909 | */ | |
2910 | if (need_tracing_session) { | |
2911 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
2912 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
2913 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
f73fabfd | 2914 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
2915 | goto error; |
2916 | } | |
2917 | } | |
2918 | ||
ffe60014 DG |
2919 | /* |
2920 | * Send relayd information to consumer as soon as we have a domain and a | |
2921 | * session defined. | |
2922 | */ | |
2923 | if (cmd_ctx->session && need_domain) { | |
2924 | /* | |
2925 | * Setup relayd if not done yet. If the relayd information was already | |
2926 | * sent to the consumer, this call will gracefully return. | |
2927 | */ | |
2928 | ret = cmd_setup_relayd(cmd_ctx->session); | |
2929 | if (ret != LTTNG_OK) { | |
2930 | goto error; | |
2931 | } | |
2932 | } | |
2933 | ||
54d01ffb DG |
2934 | /* Process by command type */ |
2935 | switch (cmd_ctx->lsm->cmd_type) { | |
2936 | case LTTNG_ADD_CONTEXT: | |
2937 | { | |
2938 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2939 | cmd_ctx->lsm->u.context.channel_name, | |
979e618e | 2940 | &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]); |
54d01ffb DG |
2941 | break; |
2942 | } | |
2943 | case LTTNG_DISABLE_CHANNEL: | |
2944 | { | |
2945 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2946 | cmd_ctx->lsm->u.disable.channel_name); | |
2947 | break; | |
2948 | } | |
2949 | case LTTNG_DISABLE_EVENT: | |
2950 | { | |
2951 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2952 | cmd_ctx->lsm->u.disable.channel_name, | |
2953 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
2954 | break; |
2955 | } | |
54d01ffb DG |
2956 | case LTTNG_DISABLE_ALL_EVENT: |
2957 | { | |
2bdd86d4 | 2958 | DBG("Disabling all events"); |
54d01ffb DG |
2959 | |
2960 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2961 | cmd_ctx->lsm->u.disable.channel_name); | |
2962 | break; | |
2963 | } | |
2964 | case LTTNG_ENABLE_CHANNEL: | |
2965 | { | |
7972aab2 | 2966 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
2f77fc4b | 2967 | &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]); |
54d01ffb DG |
2968 | break; |
2969 | } | |
2970 | case LTTNG_ENABLE_EVENT: | |
2971 | { | |
882ef835 JI |
2972 | struct lttng_event_exclusion *exclusion = NULL; |
2973 | struct lttng_filter_bytecode *bytecode = NULL; | |
2974 | ||
67676bd8 DG |
2975 | /* Handle exclusion events and receive it from the client. */ |
2976 | if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { | |
2977 | size_t count = cmd_ctx->lsm->u.enable.exclusion_count; | |
2978 | ||
2979 | exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + | |
2980 | (count * LTTNG_SYMBOL_NAME_LEN)); | |
2981 | if (!exclusion) { | |
2982 | ret = LTTNG_ERR_EXCLUSION_NOMEM; | |
2983 | goto error; | |
882ef835 | 2984 | } |
882ef835 | 2985 | |
67676bd8 DG |
2986 | DBG("Receiving var len exclusion event list from client ..."); |
2987 | exclusion->count = count; | |
2988 | ret = lttcomm_recv_unix_sock(sock, exclusion->names, | |
2989 | count * LTTNG_SYMBOL_NAME_LEN); | |
2990 | if (ret <= 0) { | |
2991 | DBG("Nothing recv() from client var len data... continuing"); | |
2992 | *sock_error = 1; | |
2993 | free(exclusion); | |
2994 | ret = LTTNG_ERR_EXCLUSION_INVAL; | |
2995 | goto error; | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | /* Handle filter and get bytecode from client. */ | |
3000 | if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { | |
3001 | size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; | |
3002 | ||
3003 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
3004 | ret = LTTNG_ERR_FILTER_INVAL; | |
3005 | free(exclusion); | |
3006 | goto error; | |
3007 | } | |
3008 | ||
3009 | bytecode = zmalloc(bytecode_len); | |
3010 | if (!bytecode) { | |
3011 | free(exclusion); | |
3012 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3013 | goto error; | |
882ef835 JI |
3014 | } |
3015 | ||
67676bd8 DG |
3016 | /* Receive var. len. data */ |
3017 | DBG("Receiving var len filter's bytecode from client ..."); | |
3018 | ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len); | |
3019 | if (ret <= 0) { | |
3020 | DBG("Nothing recv() from client car len data... continuing"); | |
3021 | *sock_error = 1; | |
3022 | free(bytecode); | |
3023 | free(exclusion); | |
3024 | ret = LTTNG_ERR_FILTER_INVAL; | |
3025 | goto error; | |
3026 | } | |
3027 | ||
3028 | if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { | |
3029 | free(bytecode); | |
3030 | free(exclusion); | |
3031 | ret = LTTNG_ERR_FILTER_INVAL; | |
3032 | goto error; | |
3033 | } | |
882ef835 | 3034 | } |
67676bd8 DG |
3035 | |
3036 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, | |
3037 | cmd_ctx->lsm->u.enable.channel_name, | |
3038 | &cmd_ctx->lsm->u.enable.event, bytecode, exclusion, | |
3039 | kernel_poll_pipe[1]); | |
54d01ffb DG |
3040 | break; |
3041 | } | |
3042 | case LTTNG_ENABLE_ALL_EVENT: | |
3043 | { | |
2bdd86d4 | 3044 | DBG("Enabling all events"); |
54d01ffb | 3045 | |
7972aab2 | 3046 | ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain, |
8c9ae521 | 3047 | cmd_ctx->lsm->u.enable.channel_name, |
025faf73 | 3048 | cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]); |
54d01ffb DG |
3049 | break; |
3050 | } | |
052da939 | 3051 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 3052 | { |
9f19cc17 | 3053 | struct lttng_event *events; |
54d01ffb | 3054 | ssize_t nb_events; |
052da939 | 3055 | |
54d01ffb DG |
3056 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
3057 | if (nb_events < 0) { | |
f73fabfd | 3058 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3059 | ret = -nb_events; |
3060 | goto error; | |
2ef84c95 DG |
3061 | } |
3062 | ||
3063 | /* | |
3064 | * Setup lttng message with payload size set to the event list size in | |
3065 | * bytes and then copy list into the llm payload. | |
3066 | */ | |
052da939 | 3067 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 3068 | if (ret < 0) { |
052da939 | 3069 | free(events); |
2ef84c95 DG |
3070 | goto setup_error; |
3071 | } | |
3072 | ||
3073 | /* Copy event list into message payload */ | |
9f19cc17 | 3074 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 3075 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 3076 | |
9f19cc17 | 3077 | free(events); |
2ef84c95 | 3078 | |
f73fabfd | 3079 | ret = LTTNG_OK; |
2ef84c95 DG |
3080 | break; |
3081 | } | |
f37d259d MD |
3082 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
3083 | { | |
3084 | struct lttng_event_field *fields; | |
3085 | ssize_t nb_fields; | |
3086 | ||
a4b92340 DG |
3087 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
3088 | &fields); | |
f37d259d | 3089 | if (nb_fields < 0) { |
f73fabfd | 3090 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
3091 | ret = -nb_fields; |
3092 | goto error; | |
3093 | } | |
3094 | ||
3095 | /* | |
3096 | * Setup lttng message with payload size set to the event list size in | |
3097 | * bytes and then copy list into the llm payload. | |
3098 | */ | |
a4b92340 DG |
3099 | ret = setup_lttng_msg(cmd_ctx, |
3100 | sizeof(struct lttng_event_field) * nb_fields); | |
f37d259d MD |
3101 | if (ret < 0) { |
3102 | free(fields); | |
3103 | goto setup_error; | |
3104 | } | |
3105 | ||
3106 | /* Copy event list into message payload */ | |
3107 | memcpy(cmd_ctx->llm->payload, fields, | |
3108 | sizeof(struct lttng_event_field) * nb_fields); | |
3109 | ||
3110 | free(fields); | |
3111 | ||
f73fabfd | 3112 | ret = LTTNG_OK; |
f37d259d MD |
3113 | break; |
3114 | } | |
00e2e675 DG |
3115 | case LTTNG_SET_CONSUMER_URI: |
3116 | { | |
a4b92340 DG |
3117 | size_t nb_uri, len; |
3118 | struct lttng_uri *uris; | |
3119 | ||
3120 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3121 | len = nb_uri * sizeof(struct lttng_uri); | |
3122 | ||
3123 | if (nb_uri == 0) { | |
f73fabfd | 3124 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
3125 | goto error; |
3126 | } | |
3127 | ||
3128 | uris = zmalloc(len); | |
3129 | if (uris == NULL) { | |
f73fabfd | 3130 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3131 | goto error; |
3132 | } | |
3133 | ||
3134 | /* Receive variable len data */ | |
77c7c900 | 3135 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
3136 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3137 | if (ret <= 0) { | |
3138 | DBG("No URIs received from client... continuing"); | |
3139 | *sock_error = 1; | |
f73fabfd | 3140 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3141 | free(uris); |
a4b92340 DG |
3142 | goto error; |
3143 | } | |
3144 | ||
00e2e675 | 3145 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
a4b92340 | 3146 | nb_uri, uris); |
f73fabfd | 3147 | if (ret != LTTNG_OK) { |
b1d41407 | 3148 | free(uris); |
a4b92340 DG |
3149 | goto error; |
3150 | } | |
3151 | ||
3152 | /* | |
3153 | * XXX: 0 means that this URI should be applied on the session. Should | |
3154 | * be a DOMAIN enuam. | |
3155 | */ | |
3156 | if (cmd_ctx->lsm->domain.type == 0) { | |
3157 | /* Add the URI for the UST session if a consumer is present. */ | |
3158 | if (cmd_ctx->session->ust_session && | |
3159 | cmd_ctx->session->ust_session->consumer) { | |
3160 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session, | |
3161 | nb_uri, uris); | |
3162 | } else if (cmd_ctx->session->kernel_session && | |
3163 | cmd_ctx->session->kernel_session->consumer) { | |
3164 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL, | |
3165 | cmd_ctx->session, nb_uri, uris); | |
3166 | } | |
3167 | } | |
3168 | ||
b1d41407 DG |
3169 | free(uris); |
3170 | ||
00e2e675 DG |
3171 | break; |
3172 | } | |
f3ed775e | 3173 | case LTTNG_START_TRACE: |
8c0faa1d | 3174 | { |
54d01ffb | 3175 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3176 | break; |
3177 | } | |
f3ed775e | 3178 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3179 | { |
54d01ffb | 3180 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3181 | break; |
3182 | } | |
5e16da05 MD |
3183 | case LTTNG_CREATE_SESSION: |
3184 | { | |
a4b92340 DG |
3185 | size_t nb_uri, len; |
3186 | struct lttng_uri *uris = NULL; | |
3187 | ||
3188 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3189 | len = nb_uri * sizeof(struct lttng_uri); | |
3190 | ||
3191 | if (nb_uri > 0) { | |
3192 | uris = zmalloc(len); | |
3193 | if (uris == NULL) { | |
f73fabfd | 3194 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3195 | goto error; |
3196 | } | |
3197 | ||
3198 | /* Receive variable len data */ | |
77c7c900 | 3199 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
3200 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3201 | if (ret <= 0) { | |
3202 | DBG("No URIs received from client... continuing"); | |
3203 | *sock_error = 1; | |
f73fabfd | 3204 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3205 | free(uris); |
a4b92340 DG |
3206 | goto error; |
3207 | } | |
3208 | ||
3209 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3210 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 3211 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3212 | free(uris); |
a4b92340 DG |
3213 | goto error; |
3214 | } | |
3215 | } | |
3216 | ||
3217 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
ecc48a90 | 3218 | &cmd_ctx->creds, 0); |
a4b92340 | 3219 | |
b1d41407 DG |
3220 | free(uris); |
3221 | ||
00e2e675 DG |
3222 | break; |
3223 | } | |
5e16da05 MD |
3224 | case LTTNG_DESTROY_SESSION: |
3225 | { | |
2f77fc4b | 3226 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]); |
a4b92340 DG |
3227 | |
3228 | /* Set session to NULL so we do not unlock it after free. */ | |
256a5576 | 3229 | cmd_ctx->session = NULL; |
5461b305 | 3230 | break; |
5e16da05 | 3231 | } |
9f19cc17 | 3232 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3233 | { |
54d01ffb DG |
3234 | ssize_t nb_dom; |
3235 | struct lttng_domain *domains; | |
5461b305 | 3236 | |
54d01ffb DG |
3237 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3238 | if (nb_dom < 0) { | |
f73fabfd | 3239 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3240 | ret = -nb_dom; |
3241 | goto error; | |
ce3d728c | 3242 | } |
520ff687 | 3243 | |
54d01ffb | 3244 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 | 3245 | if (ret < 0) { |
ae9c20bc | 3246 | free(domains); |
5461b305 | 3247 | goto setup_error; |
520ff687 | 3248 | } |
57167058 | 3249 | |
54d01ffb DG |
3250 | /* Copy event list into message payload */ |
3251 | memcpy(cmd_ctx->llm->payload, domains, | |
3252 | nb_dom * sizeof(struct lttng_domain)); | |
3253 | ||
3254 | free(domains); | |
5e16da05 | 3255 | |
f73fabfd | 3256 | ret = LTTNG_OK; |
5e16da05 MD |
3257 | break; |
3258 | } | |
9f19cc17 | 3259 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3260 | { |
6775595e | 3261 | int nb_chan; |
54d01ffb DG |
3262 | struct lttng_channel *channels; |
3263 | ||
b551a063 DG |
3264 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
3265 | cmd_ctx->session, &channels); | |
54d01ffb | 3266 | if (nb_chan < 0) { |
f73fabfd | 3267 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3268 | ret = -nb_chan; |
3269 | goto error; | |
5461b305 | 3270 | } |
ca95a216 | 3271 | |
54d01ffb | 3272 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 | 3273 | if (ret < 0) { |
ae9c20bc | 3274 | free(channels); |
5461b305 DG |
3275 | goto setup_error; |
3276 | } | |
9f19cc17 | 3277 | |
54d01ffb DG |
3278 | /* Copy event list into message payload */ |
3279 | memcpy(cmd_ctx->llm->payload, channels, | |
3280 | nb_chan * sizeof(struct lttng_channel)); | |
3281 | ||
3282 | free(channels); | |
9f19cc17 | 3283 | |
f73fabfd | 3284 | ret = LTTNG_OK; |
5461b305 | 3285 | break; |
5e16da05 | 3286 | } |
9f19cc17 | 3287 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3288 | { |
b551a063 | 3289 | ssize_t nb_event; |
684d34d2 | 3290 | struct lttng_event *events = NULL; |
9f19cc17 | 3291 | |
b551a063 | 3292 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
54d01ffb DG |
3293 | cmd_ctx->lsm->u.list.channel_name, &events); |
3294 | if (nb_event < 0) { | |
f73fabfd | 3295 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3296 | ret = -nb_event; |
3297 | goto error; | |
5461b305 | 3298 | } |
ca95a216 | 3299 | |
54d01ffb | 3300 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 | 3301 | if (ret < 0) { |
ae9c20bc | 3302 | free(events); |
5461b305 DG |
3303 | goto setup_error; |
3304 | } | |
9f19cc17 | 3305 | |
54d01ffb DG |
3306 | /* Copy event list into message payload */ |
3307 | memcpy(cmd_ctx->llm->payload, events, | |
3308 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 3309 | |
54d01ffb | 3310 | free(events); |
9f19cc17 | 3311 | |
f73fabfd | 3312 | ret = LTTNG_OK; |
5461b305 | 3313 | break; |
5e16da05 MD |
3314 | } |
3315 | case LTTNG_LIST_SESSIONS: | |
3316 | { | |
8e0af1b4 | 3317 | unsigned int nr_sessions; |
5461b305 | 3318 | |
8e0af1b4 | 3319 | session_lock_list(); |
730389d9 DG |
3320 | nr_sessions = lttng_sessions_count( |
3321 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
3322 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
d32fb093 | 3323 | |
8e0af1b4 | 3324 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
5461b305 | 3325 | if (ret < 0) { |
54d01ffb | 3326 | session_unlock_list(); |
5461b305 | 3327 | goto setup_error; |
e065084a | 3328 | } |
5e16da05 | 3329 | |
6c9cc2ab | 3330 | /* Filled the session array */ |
2f77fc4b | 3331 | cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
730389d9 DG |
3332 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3333 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6c9cc2ab | 3334 | |
54d01ffb | 3335 | session_unlock_list(); |
5e16da05 | 3336 | |
f73fabfd | 3337 | ret = LTTNG_OK; |
5e16da05 MD |
3338 | break; |
3339 | } | |
d0254c7c MD |
3340 | case LTTNG_CALIBRATE: |
3341 | { | |
54d01ffb DG |
3342 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
3343 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
3344 | break; |
3345 | } | |
d9800920 DG |
3346 | case LTTNG_REGISTER_CONSUMER: |
3347 | { | |
2f77fc4b DG |
3348 | struct consumer_data *cdata; |
3349 | ||
3350 | switch (cmd_ctx->lsm->domain.type) { | |
3351 | case LTTNG_DOMAIN_KERNEL: | |
3352 | cdata = &kconsumer_data; | |
3353 | break; | |
3354 | default: | |
f73fabfd | 3355 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3356 | goto error; |
3357 | } | |
3358 | ||
54d01ffb | 3359 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 3360 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
3361 | break; |
3362 | } | |
6d805429 | 3363 | case LTTNG_DATA_PENDING: |
806e2684 | 3364 | { |
6d805429 | 3365 | ret = cmd_data_pending(cmd_ctx->session); |
806e2684 DG |
3366 | break; |
3367 | } | |
da3c9ec1 DG |
3368 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3369 | { | |
6dc3064a DG |
3370 | struct lttcomm_lttng_output_id reply; |
3371 | ||
3372 | ret = cmd_snapshot_add_output(cmd_ctx->session, | |
3373 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); | |
3374 | if (ret != LTTNG_OK) { | |
3375 | goto error; | |
3376 | } | |
3377 | ||
3378 | ret = setup_lttng_msg(cmd_ctx, sizeof(reply)); | |
3379 | if (ret < 0) { | |
3380 | goto setup_error; | |
3381 | } | |
3382 | ||
3383 | /* Copy output list into message payload */ | |
3384 | memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply)); | |
3385 | ret = LTTNG_OK; | |
da3c9ec1 DG |
3386 | break; |
3387 | } | |
3388 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3389 | { | |
6dc3064a DG |
3390 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
3391 | &cmd_ctx->lsm->u.snapshot_output.output); | |
da3c9ec1 DG |
3392 | break; |
3393 | } | |
3394 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
3395 | { | |
6dc3064a DG |
3396 | ssize_t nb_output; |
3397 | struct lttng_snapshot_output *outputs = NULL; | |
3398 | ||
3399 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); | |
3400 | if (nb_output < 0) { | |
3401 | ret = -nb_output; | |
3402 | goto error; | |
3403 | } | |
3404 | ||
3405 | ret = setup_lttng_msg(cmd_ctx, | |
3406 | nb_output * sizeof(struct lttng_snapshot_output)); | |
3407 | if (ret < 0) { | |
3408 | free(outputs); | |
3409 | goto setup_error; | |
3410 | } | |
3411 | ||
3412 | if (outputs) { | |
3413 | /* Copy output list into message payload */ | |
3414 | memcpy(cmd_ctx->llm->payload, outputs, | |
3415 | nb_output * sizeof(struct lttng_snapshot_output)); | |
3416 | free(outputs); | |
3417 | } | |
3418 | ||
3419 | ret = LTTNG_OK; | |
da3c9ec1 DG |
3420 | break; |
3421 | } | |
3422 | case LTTNG_SNAPSHOT_RECORD: | |
3423 | { | |
6dc3064a DG |
3424 | ret = cmd_snapshot_record(cmd_ctx->session, |
3425 | &cmd_ctx->lsm->u.snapshot_record.output, | |
3426 | cmd_ctx->lsm->u.snapshot_record.wait); | |
da3c9ec1 DG |
3427 | break; |
3428 | } | |
27babd3a DG |
3429 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
3430 | { | |
3431 | size_t nb_uri, len; | |
3432 | struct lttng_uri *uris = NULL; | |
3433 | ||
3434 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3435 | len = nb_uri * sizeof(struct lttng_uri); | |
3436 | ||
3437 | if (nb_uri > 0) { | |
3438 | uris = zmalloc(len); | |
3439 | if (uris == NULL) { | |
3440 | ret = LTTNG_ERR_FATAL; | |
3441 | goto error; | |
3442 | } | |
3443 | ||
3444 | /* Receive variable len data */ | |
3445 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
3446 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
3447 | if (ret <= 0) { | |
3448 | DBG("No URIs received from client... continuing"); | |
3449 | *sock_error = 1; | |
3450 | ret = LTTNG_ERR_SESSION_FAIL; | |
3451 | free(uris); | |
3452 | goto error; | |
3453 | } | |
3454 | ||
3455 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3456 | DBG("Creating session with ONE network URI is a bad call"); | |
3457 | ret = LTTNG_ERR_SESSION_FAIL; | |
3458 | free(uris); | |
3459 | goto error; | |
3460 | } | |
3461 | } | |
3462 | ||
3463 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, | |
3464 | nb_uri, &cmd_ctx->creds); | |
3465 | free(uris); | |
3466 | break; | |
3467 | } | |
ecc48a90 JD |
3468 | case LTTNG_CREATE_SESSION_LIVE: |
3469 | { | |
3470 | size_t nb_uri, len; | |
3471 | struct lttng_uri *uris = NULL; | |
3472 | ||
3473 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3474 | len = nb_uri * sizeof(struct lttng_uri); | |
3475 | ||
3476 | if (nb_uri > 0) { | |
3477 | uris = zmalloc(len); | |
3478 | if (uris == NULL) { | |
3479 | ret = LTTNG_ERR_FATAL; | |
3480 | goto error; | |
3481 | } | |
3482 | ||
3483 | /* Receive variable len data */ | |
3484 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
3485 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
3486 | if (ret <= 0) { | |
3487 | DBG("No URIs received from client... continuing"); | |
3488 | *sock_error = 1; | |
3489 | ret = LTTNG_ERR_SESSION_FAIL; | |
3490 | free(uris); | |
3491 | goto error; | |
3492 | } | |
3493 | ||
3494 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3495 | DBG("Creating session with ONE network URI is a bad call"); | |
3496 | ret = LTTNG_ERR_SESSION_FAIL; | |
3497 | free(uris); | |
3498 | goto error; | |
3499 | } | |
3500 | } | |
3501 | ||
3502 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, | |
3503 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); | |
3504 | free(uris); | |
3505 | break; | |
3506 | } | |
5e16da05 | 3507 | default: |
f73fabfd | 3508 | ret = LTTNG_ERR_UND; |
5461b305 | 3509 | break; |
fac6795d DG |
3510 | } |
3511 | ||
5461b305 | 3512 | error: |
5461b305 DG |
3513 | if (cmd_ctx->llm == NULL) { |
3514 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 3515 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
3516 | goto setup_error; |
3517 | } | |
3518 | } | |
54d01ffb | 3519 | /* Set return code */ |
5461b305 | 3520 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 3521 | setup_error: |
b5541356 | 3522 | if (cmd_ctx->session) { |
54d01ffb | 3523 | session_unlock(cmd_ctx->session); |
b5541356 | 3524 | } |
256a5576 MD |
3525 | if (need_tracing_session) { |
3526 | session_unlock_list(); | |
3527 | } | |
54d01ffb | 3528 | init_setup_error: |
8028d920 | 3529 | return ret; |
fac6795d DG |
3530 | } |
3531 | ||
44a5e5eb DG |
3532 | /* |
3533 | * Thread managing health check socket. | |
3534 | */ | |
3535 | static void *thread_manage_health(void *data) | |
3536 | { | |
eb4a2943 | 3537 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
3538 | uint32_t revents, nb_fd; |
3539 | struct lttng_poll_event events; | |
0c89d795 MD |
3540 | struct health_comm_msg msg; |
3541 | struct health_comm_reply reply; | |
44a5e5eb DG |
3542 | |
3543 | DBG("[thread] Manage health check started"); | |
3544 | ||
3545 | rcu_register_thread(); | |
3546 | ||
6d737ce4 DG |
3547 | /* We might hit an error path before this is created. */ |
3548 | lttng_poll_init(&events); | |
3cc04881 | 3549 | |
44a5e5eb DG |
3550 | /* Create unix socket */ |
3551 | sock = lttcomm_create_unix_sock(health_unix_sock_path); | |
3552 | if (sock < 0) { | |
3553 | ERR("Unable to create health check Unix socket"); | |
3554 | ret = -1; | |
3555 | goto error; | |
3556 | } | |
3557 | ||
6c71277b MD |
3558 | if (is_root) { |
3559 | /* lttng health client socket path permissions */ | |
3560 | ret = chown(health_unix_sock_path, 0, | |
3561 | utils_get_group_id(tracing_group_name)); | |
3562 | if (ret < 0) { | |
3563 | ERR("Unable to set group on %s", health_unix_sock_path); | |
3564 | PERROR("chown"); | |
3565 | ret = -1; | |
3566 | goto error; | |
3567 | } | |
3568 | ||
3569 | ret = chmod(health_unix_sock_path, | |
3570 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3571 | if (ret < 0) { | |
3572 | ERR("Unable to set permissions on %s", health_unix_sock_path); | |
3573 | PERROR("chmod"); | |
3574 | ret = -1; | |
3575 | goto error; | |
3576 | } | |
3577 | } | |
3578 | ||
b662582b DG |
3579 | /* |
3580 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3581 | * show must go on. | |
3582 | */ | |
3583 | (void) utils_set_fd_cloexec(sock); | |
3584 | ||
44a5e5eb DG |
3585 | ret = lttcomm_listen_unix_sock(sock); |
3586 | if (ret < 0) { | |
3587 | goto error; | |
3588 | } | |
3589 | ||
3590 | /* | |
3591 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3592 | * more will be added to this poll set. | |
3593 | */ | |
d0b96690 | 3594 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
3595 | if (ret < 0) { |
3596 | goto error; | |
3597 | } | |
3598 | ||
3599 | /* Add the application registration socket */ | |
3600 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
3601 | if (ret < 0) { | |
3602 | goto error; | |
3603 | } | |
3604 | ||
56045742 MD |
3605 | lttng_sessiond_notify_ready(); |
3606 | ||
44a5e5eb DG |
3607 | while (1) { |
3608 | DBG("Health check ready"); | |
3609 | ||
44a5e5eb DG |
3610 | /* Inifinite blocking call, waiting for transmission */ |
3611 | restart: | |
3612 | ret = lttng_poll_wait(&events, -1); | |
3613 | if (ret < 0) { | |
3614 | /* | |
3615 | * Restart interrupted system call. | |
3616 | */ | |
3617 | if (errno == EINTR) { | |
3618 | goto restart; | |
3619 | } | |
3620 | goto error; | |
3621 | } | |
3622 | ||
0d9c5d77 DG |
3623 | nb_fd = ret; |
3624 | ||
44a5e5eb DG |
3625 | for (i = 0; i < nb_fd; i++) { |
3626 | /* Fetch once the poll data */ | |
3627 | revents = LTTNG_POLL_GETEV(&events, i); | |
3628 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3629 | ||
3630 | /* Thread quit pipe has been closed. Killing thread. */ | |
d0b96690 | 3631 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 3632 | if (ret) { |
139ac872 MD |
3633 | err = 0; |
3634 | goto exit; | |
44a5e5eb DG |
3635 | } |
3636 | ||
3637 | /* Event on the registration socket */ | |
3638 | if (pollfd == sock) { | |
3639 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3640 | ERR("Health socket poll error"); | |
3641 | goto error; | |
3642 | } | |
3643 | } | |
3644 | } | |
3645 | ||
3646 | new_sock = lttcomm_accept_unix_sock(sock); | |
3647 | if (new_sock < 0) { | |
3648 | goto error; | |
3649 | } | |
3650 | ||
b662582b DG |
3651 | /* |
3652 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3653 | * show must go on. | |
3654 | */ | |
3655 | (void) utils_set_fd_cloexec(new_sock); | |
3656 | ||
44a5e5eb DG |
3657 | DBG("Receiving data from client for health..."); |
3658 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
3659 | if (ret <= 0) { | |
3660 | DBG("Nothing recv() from client... continuing"); | |
3661 | ret = close(new_sock); | |
3662 | if (ret) { | |
3663 | PERROR("close"); | |
3664 | } | |
3665 | new_sock = -1; | |
3666 | continue; | |
3667 | } | |
3668 | ||
3669 | rcu_thread_online(); | |
3670 | ||
6c71277b MD |
3671 | reply.ret_code = 0; |
3672 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { | |
3673 | /* | |
3674 | * health_check_state returns 0 if health is | |
3675 | * bad. | |
3676 | */ | |
3677 | if (!health_check_state(health_sessiond, i)) { | |
3678 | reply.ret_code |= 1ULL << i; | |
3679 | } | |
44a5e5eb DG |
3680 | } |
3681 | ||
6c71277b | 3682 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
44a5e5eb DG |
3683 | |
3684 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
3685 | if (ret < 0) { | |
3686 | ERR("Failed to send health data back to client"); | |
3687 | } | |
3688 | ||
3689 | /* End of transmission */ | |
3690 | ret = close(new_sock); | |
3691 | if (ret) { | |
3692 | PERROR("close"); | |
3693 | } | |
3694 | new_sock = -1; | |
3695 | } | |
3696 | ||
139ac872 | 3697 | exit: |
44a5e5eb | 3698 | error: |
139ac872 MD |
3699 | if (err) { |
3700 | ERR("Health error occurred in %s", __func__); | |
3701 | } | |
44a5e5eb DG |
3702 | DBG("Health check thread dying"); |
3703 | unlink(health_unix_sock_path); | |
3704 | if (sock >= 0) { | |
3705 | ret = close(sock); | |
3706 | if (ret) { | |
3707 | PERROR("close"); | |
3708 | } | |
3709 | } | |
44a5e5eb DG |
3710 | |
3711 | lttng_poll_clean(&events); | |
3712 | ||
3713 | rcu_unregister_thread(); | |
3714 | return NULL; | |
3715 | } | |
3716 | ||
1d4b027a | 3717 | /* |
d063d709 DG |
3718 | * This thread manage all clients request using the unix client socket for |
3719 | * communication. | |
1d4b027a DG |
3720 | */ |
3721 | static void *thread_manage_clients(void *data) | |
3722 | { | |
139ac872 | 3723 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 3724 | int sock_error; |
5eb91c98 | 3725 | uint32_t revents, nb_fd; |
273ea72c | 3726 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 3727 | struct lttng_poll_event events; |
1d4b027a DG |
3728 | |
3729 | DBG("[thread] Manage client started"); | |
3730 | ||
f6a9efaa DG |
3731 | rcu_register_thread(); |
3732 | ||
6c71277b | 3733 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
927ca06a | 3734 | |
840cb59c | 3735 | health_code_update(); |
44a5e5eb | 3736 | |
1d4b027a DG |
3737 | ret = lttcomm_listen_unix_sock(client_sock); |
3738 | if (ret < 0) { | |
35df2755 | 3739 | goto error_listen; |
1d4b027a DG |
3740 | } |
3741 | ||
5eb91c98 DG |
3742 | /* |
3743 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3744 | * more will be added to this poll set. | |
3745 | */ | |
d0b96690 | 3746 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 3747 | if (ret < 0) { |
35df2755 | 3748 | goto error_create_poll; |
5eb91c98 | 3749 | } |
273ea72c | 3750 | |
5eb91c98 DG |
3751 | /* Add the application registration socket */ |
3752 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
3753 | if (ret < 0) { | |
3754 | goto error; | |
3755 | } | |
273ea72c | 3756 | |
56045742 | 3757 | lttng_sessiond_notify_ready(); |
0bb7724a | 3758 | |
62c131b7 MD |
3759 | /* This testpoint is after we signal readiness to the parent. */ |
3760 | if (testpoint(sessiond_thread_manage_clients)) { | |
3761 | goto error; | |
3762 | } | |
3763 | ||
71ddb4ed | 3764 | if (testpoint(sessiond_thread_manage_clients_before_loop)) { |
6993eeb3 CB |
3765 | goto error; |
3766 | } | |
8ac94142 | 3767 | |
840cb59c | 3768 | health_code_update(); |
44a5e5eb | 3769 | |
1d4b027a | 3770 | while (1) { |
1d4b027a | 3771 | DBG("Accepting client command ..."); |
273ea72c DG |
3772 | |
3773 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 3774 | restart: |
a78af745 | 3775 | health_poll_entry(); |
5eb91c98 | 3776 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 3777 | health_poll_exit(); |
273ea72c | 3778 | if (ret < 0) { |
88f2b785 MD |
3779 | /* |
3780 | * Restart interrupted system call. | |
3781 | */ | |
3782 | if (errno == EINTR) { | |
3783 | goto restart; | |
3784 | } | |
273ea72c DG |
3785 | goto error; |
3786 | } | |
3787 | ||
0d9c5d77 DG |
3788 | nb_fd = ret; |
3789 | ||
5eb91c98 DG |
3790 | for (i = 0; i < nb_fd; i++) { |
3791 | /* Fetch once the poll data */ | |
3792 | revents = LTTNG_POLL_GETEV(&events, i); | |
3793 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3794 | ||
840cb59c | 3795 | health_code_update(); |
44a5e5eb | 3796 | |
5eb91c98 | 3797 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 3798 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 3799 | if (ret) { |
139ac872 MD |
3800 | err = 0; |
3801 | goto exit; | |
5eb91c98 DG |
3802 | } |
3803 | ||
3804 | /* Event on the registration socket */ | |
3805 | if (pollfd == client_sock) { | |
3806 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3807 | ERR("Client socket poll error"); | |
3808 | goto error; | |
3809 | } | |
3810 | } | |
3811 | } | |
3812 | ||
3813 | DBG("Wait for client response"); | |
3814 | ||
840cb59c | 3815 | health_code_update(); |
44a5e5eb | 3816 | |
5eb91c98 DG |
3817 | sock = lttcomm_accept_unix_sock(client_sock); |
3818 | if (sock < 0) { | |
273ea72c | 3819 | goto error; |
273ea72c DG |
3820 | } |
3821 | ||
b662582b DG |
3822 | /* |
3823 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3824 | * show must go on. | |
3825 | */ | |
3826 | (void) utils_set_fd_cloexec(sock); | |
3827 | ||
be040666 DG |
3828 | /* Set socket option for credentials retrieval */ |
3829 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
3830 | if (ret < 0) { | |
3831 | goto error; | |
3832 | } | |
3833 | ||
5eb91c98 | 3834 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 3835 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 3836 | if (cmd_ctx == NULL) { |
76d7553f | 3837 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 3838 | goto error; |
5eb91c98 | 3839 | } |
1d4b027a | 3840 | |
5eb91c98 | 3841 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 3842 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 3843 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 3844 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
3845 | goto error; |
3846 | } | |
1d4b027a | 3847 | |
5eb91c98 DG |
3848 | cmd_ctx->llm = NULL; |
3849 | cmd_ctx->session = NULL; | |
1d4b027a | 3850 | |
840cb59c | 3851 | health_code_update(); |
44a5e5eb | 3852 | |
5eb91c98 DG |
3853 | /* |
3854 | * Data is received from the lttng client. The struct | |
3855 | * lttcomm_session_msg (lsm) contains the command and data request of | |
3856 | * the client. | |
3857 | */ | |
3858 | DBG("Receiving data from client ..."); | |
be040666 DG |
3859 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
3860 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
3861 | if (ret <= 0) { |
3862 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
3863 | ret = close(sock); |
3864 | if (ret) { | |
3865 | PERROR("close"); | |
3866 | } | |
3867 | sock = -1; | |
a2c0da86 | 3868 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3869 | continue; |
3870 | } | |
1d4b027a | 3871 | |
840cb59c | 3872 | health_code_update(); |
44a5e5eb | 3873 | |
5eb91c98 DG |
3874 | // TODO: Validate cmd_ctx including sanity check for |
3875 | // security purpose. | |
f7776ea7 | 3876 | |
f6a9efaa | 3877 | rcu_thread_online(); |
5eb91c98 DG |
3878 | /* |
3879 | * This function dispatch the work to the kernel or userspace tracer | |
3880 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
3881 | * informations for the client. The command context struct contains | |
3882 | * everything this function may needs. | |
3883 | */ | |
53a80697 | 3884 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 3885 | rcu_thread_offline(); |
5eb91c98 | 3886 | if (ret < 0) { |
36134aa1 DG |
3887 | ret = close(sock); |
3888 | if (ret) { | |
3889 | PERROR("close"); | |
53a80697 | 3890 | } |
36134aa1 | 3891 | sock = -1; |
bbd973c2 | 3892 | /* |
5eb91c98 | 3893 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 3894 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
3895 | * (ENOMEM). Error detected but still accept |
3896 | * command, unless a socket error has been | |
3897 | * detected. | |
bbd973c2 | 3898 | */ |
bbd973c2 | 3899 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3900 | continue; |
3901 | } | |
d6e4fca4 | 3902 | |
840cb59c | 3903 | health_code_update(); |
44a5e5eb | 3904 | |
54d01ffb DG |
3905 | DBG("Sending response (size: %d, retcode: %s)", |
3906 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 3907 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 3908 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
3909 | if (ret < 0) { |
3910 | ERR("Failed to send data back to client"); | |
bbd973c2 | 3911 | } |
1d4b027a | 3912 | |
5eb91c98 | 3913 | /* End of transmission */ |
76d7553f MD |
3914 | ret = close(sock); |
3915 | if (ret) { | |
3916 | PERROR("close"); | |
3917 | } | |
3918 | sock = -1; | |
be040666 DG |
3919 | |
3920 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 3921 | |
840cb59c | 3922 | health_code_update(); |
273ea72c DG |
3923 | } |
3924 | ||
139ac872 | 3925 | exit: |
5eb91c98 | 3926 | error: |
35df2755 CB |
3927 | if (sock >= 0) { |
3928 | ret = close(sock); | |
3929 | if (ret) { | |
3930 | PERROR("close"); | |
3931 | } | |
139ac872 | 3932 | } |
44a5e5eb | 3933 | |
35df2755 CB |
3934 | lttng_poll_clean(&events); |
3935 | clean_command_ctx(&cmd_ctx); | |
3936 | ||
3937 | error_listen: | |
3938 | error_create_poll: | |
273ea72c | 3939 | unlink(client_unix_sock_path); |
76d7553f MD |
3940 | if (client_sock >= 0) { |
3941 | ret = close(client_sock); | |
3942 | if (ret) { | |
3943 | PERROR("close"); | |
3944 | } | |
a4b35e07 | 3945 | } |
35df2755 CB |
3946 | |
3947 | if (err) { | |
840cb59c | 3948 | health_error(); |
35df2755 | 3949 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 3950 | } |
273ea72c | 3951 | |
8782cc74 | 3952 | health_unregister(health_sessiond); |
35df2755 CB |
3953 | |
3954 | DBG("Client thread dying"); | |
f6a9efaa DG |
3955 | |
3956 | rcu_unregister_thread(); | |
1d4b027a DG |
3957 | return NULL; |
3958 | } | |
3959 | ||
3960 | ||
fac6795d DG |
3961 | /* |
3962 | * usage function on stderr | |
3963 | */ | |
3964 | static void usage(void) | |
3965 | { | |
b716ce68 | 3966 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
3967 | fprintf(stderr, " -h, --help Display this usage.\n"); |
3968 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
3969 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
3970 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
3971 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
7753dea8 MD |
3972 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
3973 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); | |
3974 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); | |
3975 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); | |
ebaeda94 MD |
3976 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
3977 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); | |
3978 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); | |
3979 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); | |
d6f42150 | 3980 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
929de64e | 3981 | fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n"); |
d6f42150 DG |
3982 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); |
3983 | fprintf(stderr, " -V, --version Show version number.\n"); | |
7fe6d2c5 | 3984 | fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n"); |
d6f42150 DG |
3985 | fprintf(stderr, " -q, --quiet No output at all.\n"); |
3986 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
35f90c40 | 3987 | fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n"); |
3bd1e081 | 3988 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
4fba7219 | 3989 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
4d076222 | 3990 | fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n"); |
fac6795d DG |
3991 | } |
3992 | ||
3993 | /* | |
3994 | * daemon argument parsing | |
3995 | */ | |
3996 | static int parse_args(int argc, char **argv) | |
3997 | { | |
3998 | int c; | |
3999 | ||
4000 | static struct option long_options[] = { | |
4001 | { "client-sock", 1, 0, 'c' }, | |
4002 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
4003 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
4004 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
7753dea8 MD |
4005 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
4006 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
ebaeda94 MD |
4007 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
4008 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
4009 | { "consumerd32-path", 1, 0, 'u' }, | |
4010 | { "consumerd32-libdir", 1, 0, 'U' }, | |
4011 | { "consumerd64-path", 1, 0, 't' }, | |
4012 | { "consumerd64-libdir", 1, 0, 'T' }, | |
fac6795d | 4013 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 4014 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
4015 | { "help", 0, 0, 'h' }, |
4016 | { "group", 1, 0, 'g' }, | |
4017 | { "version", 0, 0, 'V' }, | |
75462a81 | 4018 | { "quiet", 0, 0, 'q' }, |
3f9947db | 4019 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 4020 | { "verbose-consumer", 0, 0, 'Z' }, |
4fba7219 | 4021 | { "no-kernel", 0, 0, 'N' }, |
35f90c40 | 4022 | { "pidfile", 1, 0, 'p' }, |
4d076222 | 4023 | { "jul-tcp-port", 1, 0, 'J' }, |
06b55dbb | 4024 | { "background", 0, 0, 'b' }, |
fac6795d DG |
4025 | { NULL, 0, 0, 0 } |
4026 | }; | |
4027 | ||
4028 | while (1) { | |
4029 | int option_index = 0; | |
06b55dbb | 4030 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:b", |
54d01ffb | 4031 | long_options, &option_index); |
fac6795d DG |
4032 | if (c == -1) { |
4033 | break; | |
4034 | } | |
4035 | ||
4036 | switch (c) { | |
4037 | case 0: | |
4038 | fprintf(stderr, "option %s", long_options[option_index].name); | |
4039 | if (optarg) { | |
4040 | fprintf(stderr, " with arg %s\n", optarg); | |
4041 | } | |
4042 | break; | |
b716ce68 | 4043 | case 'c': |
fac6795d DG |
4044 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
4045 | break; | |
4046 | case 'a': | |
4047 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
4048 | break; | |
4049 | case 'd': | |
4050 | opt_daemon = 1; | |
4051 | break; | |
06b55dbb DG |
4052 | case 'b': |
4053 | opt_background = 1; | |
4054 | break; | |
fac6795d | 4055 | case 'g': |
6c71277b | 4056 | tracing_group_name = optarg; |
fac6795d DG |
4057 | break; |
4058 | case 'h': | |
4059 | usage(); | |
4060 | exit(EXIT_FAILURE); | |
4061 | case 'V': | |
4062 | fprintf(stdout, "%s\n", VERSION); | |
4063 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
4064 | case 'S': |
4065 | opt_sig_parent = 1; | |
4066 | break; | |
d6f42150 | 4067 | case 'E': |
3bd1e081 | 4068 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
4069 | break; |
4070 | case 'C': | |
3bd1e081 MD |
4071 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
4072 | break; | |
4073 | case 'F': | |
7753dea8 | 4074 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
3bd1e081 MD |
4075 | break; |
4076 | case 'D': | |
7753dea8 MD |
4077 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
4078 | break; | |
4079 | case 'H': | |
4080 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
4081 | break; | |
4082 | case 'G': | |
4083 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 4084 | break; |
4fba7219 DG |
4085 | case 'N': |
4086 | opt_no_kernel = 1; | |
4087 | break; | |
75462a81 | 4088 | case 'q': |
97e19046 | 4089 | lttng_opt_quiet = 1; |
75462a81 | 4090 | break; |
3f9947db | 4091 | case 'v': |
53086306 | 4092 | /* Verbose level can increase using multiple -v */ |
97e19046 | 4093 | lttng_opt_verbose += 1; |
3f9947db | 4094 | break; |
31f73cc9 | 4095 | case 'Z': |
3bd1e081 | 4096 | opt_verbose_consumer += 1; |
31f73cc9 | 4097 | break; |
fb09408a | 4098 | case 'u': |
fc7a59ce | 4099 | consumerd32_bin= optarg; |
ebaeda94 MD |
4100 | break; |
4101 | case 'U': | |
4102 | consumerd32_libdir = optarg; | |
7753dea8 MD |
4103 | break; |
4104 | case 't': | |
fc7a59ce | 4105 | consumerd64_bin = optarg; |
ebaeda94 MD |
4106 | break; |
4107 | case 'T': | |
4108 | consumerd64_libdir = optarg; | |
fb09408a | 4109 | break; |
35f90c40 DG |
4110 | case 'p': |
4111 | opt_pidfile = optarg; | |
4112 | break; | |
4d076222 DG |
4113 | case 'J': /* JUL TCP port. */ |
4114 | { | |
4115 | unsigned long v; | |
4116 | ||
4117 | errno = 0; | |
4118 | v = strtoul(optarg, NULL, 0); | |
4119 | if (errno != 0 || !isdigit(optarg[0])) { | |
4120 | ERR("Wrong value in --jul-tcp-port parameter: %s", optarg); | |
4121 | return -1; | |
4122 | } | |
4123 | if (v == 0 || v >= 65535) { | |
4124 | ERR("Port overflow in --jul-tcp-port parameter: %s", optarg); | |
4125 | return -1; | |
4126 | } | |
4127 | jul_tcp_port = (uint32_t) v; | |
4128 | DBG3("JUL TCP port set to non default: %u", jul_tcp_port); | |
4129 | break; | |
4130 | } | |
fac6795d DG |
4131 | default: |
4132 | /* Unknown option or other error. | |
4133 | * Error is printed by getopt, just return */ | |
4134 | return -1; | |
4135 | } | |
4136 | } | |
4137 | ||
4138 | return 0; | |
4139 | } | |
4140 | ||
4141 | /* | |
d063d709 | 4142 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
4143 | * apps_sock - The communication socket for all UST apps. |
4144 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 4145 | */ |
cf3af59e | 4146 | static int init_daemon_socket(void) |
fac6795d DG |
4147 | { |
4148 | int ret = 0; | |
4149 | mode_t old_umask; | |
4150 | ||
4151 | old_umask = umask(0); | |
4152 | ||
4153 | /* Create client tool unix socket */ | |
d6f42150 DG |
4154 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
4155 | if (client_sock < 0) { | |
4156 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
4157 | ret = -1; |
4158 | goto end; | |
4159 | } | |
4160 | ||
b662582b DG |
4161 | /* Set the cloexec flag */ |
4162 | ret = utils_set_fd_cloexec(client_sock); | |
4163 | if (ret < 0) { | |
4164 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
4165 | "Continuing but note that the consumer daemon will have a " | |
4166 | "reference to this socket on exec()", client_sock); | |
4167 | } | |
4168 | ||
fac6795d DG |
4169 | /* File permission MUST be 660 */ |
4170 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
4171 | if (ret < 0) { | |
d6f42150 | 4172 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
76d7553f | 4173 | PERROR("chmod"); |
fac6795d DG |
4174 | goto end; |
4175 | } | |
4176 | ||
4177 | /* Create the application unix socket */ | |
d6f42150 DG |
4178 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
4179 | if (apps_sock < 0) { | |
4180 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
4181 | ret = -1; |
4182 | goto end; | |
4183 | } | |
4184 | ||
b662582b DG |
4185 | /* Set the cloexec flag */ |
4186 | ret = utils_set_fd_cloexec(apps_sock); | |
4187 | if (ret < 0) { | |
4188 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
4189 | "Continuing but note that the consumer daemon will have a " | |
4190 | "reference to this socket on exec()", apps_sock); | |
4191 | } | |
4192 | ||
d6f42150 | 4193 | /* File permission MUST be 666 */ |
54d01ffb DG |
4194 | ret = chmod(apps_unix_sock_path, |
4195 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 4196 | if (ret < 0) { |
d6f42150 | 4197 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
76d7553f | 4198 | PERROR("chmod"); |
fac6795d DG |
4199 | goto end; |
4200 | } | |
4201 | ||
b662582b DG |
4202 | DBG3("Session daemon client socket %d and application socket %d created", |
4203 | client_sock, apps_sock); | |
4204 | ||
fac6795d DG |
4205 | end: |
4206 | umask(old_umask); | |
4207 | return ret; | |
4208 | } | |
4209 | ||
4210 | /* | |
54d01ffb DG |
4211 | * Check if the global socket is available, and if a daemon is answering at the |
4212 | * other side. If yes, error is returned. | |
fac6795d | 4213 | */ |
cf3af59e | 4214 | static int check_existing_daemon(void) |
fac6795d | 4215 | { |
7d8234d9 | 4216 | /* Is there anybody out there ? */ |
099e26bd | 4217 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 4218 | return -EEXIST; |
099e26bd | 4219 | } |
b09c7c76 DG |
4220 | |
4221 | return 0; | |
fac6795d DG |
4222 | } |
4223 | ||
fac6795d | 4224 | /* |
d063d709 | 4225 | * Set the tracing group gid onto the client socket. |
5e16da05 | 4226 | * |
d063d709 | 4227 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 4228 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 4229 | */ |
be040666 | 4230 | static int set_permissions(char *rundir) |
fac6795d DG |
4231 | { |
4232 | int ret; | |
996b65c8 | 4233 | gid_t gid; |
fac6795d | 4234 | |
6c71277b | 4235 | gid = utils_get_group_id(tracing_group_name); |
fac6795d | 4236 | |
d6f42150 | 4237 | /* Set lttng run dir */ |
be040666 | 4238 | ret = chown(rundir, 0, gid); |
d6f42150 | 4239 | if (ret < 0) { |
be040666 | 4240 | ERR("Unable to set group on %s", rundir); |
76d7553f | 4241 | PERROR("chown"); |
d6f42150 DG |
4242 | } |
4243 | ||
6c71277b MD |
4244 | /* |
4245 | * Ensure all applications and tracing group can search the run | |
4246 | * dir. Allow everyone to read the directory, since it does not | |
4247 | * buy us anything to hide its content. | |
4248 | */ | |
4249 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
4250 | if (ret < 0) { |
4251 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 4252 | PERROR("chmod"); |
d1613cf5 JN |
4253 | } |
4254 | ||
d6f42150 | 4255 | /* lttng client socket path */ |
996b65c8 | 4256 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 4257 | if (ret < 0) { |
d6f42150 | 4258 | ERR("Unable to set group on %s", client_unix_sock_path); |
76d7553f | 4259 | PERROR("chown"); |
d6f42150 DG |
4260 | } |
4261 | ||
3bd1e081 | 4262 | /* kconsumer error socket path */ |
6c71277b | 4263 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 4264 | if (ret < 0) { |
3bd1e081 | 4265 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 4266 | PERROR("chown"); |
3bd1e081 MD |
4267 | } |
4268 | ||
7753dea8 | 4269 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 4270 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
4271 | if (ret < 0) { |
4272 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 4273 | PERROR("chown"); |
7753dea8 MD |
4274 | } |
4275 | ||
4276 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 4277 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 4278 | if (ret < 0) { |
7753dea8 | 4279 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 4280 | PERROR("chown"); |
fac6795d DG |
4281 | } |
4282 | ||
d6f42150 | 4283 | DBG("All permissions are set"); |
e07ae692 | 4284 | |
fac6795d DG |
4285 | return ret; |
4286 | } | |
4287 | ||
d6f42150 | 4288 | /* |
d063d709 | 4289 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 4290 | */ |
67e40797 | 4291 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
4292 | { |
4293 | int ret; | |
4294 | ||
67e40797 DG |
4295 | DBG3("Creating LTTng run directory: %s", rundir); |
4296 | ||
d1613cf5 | 4297 | ret = mkdir(rundir, S_IRWXU); |
d6f42150 | 4298 | if (ret < 0) { |
b1f11e69 | 4299 | if (errno != EEXIST) { |
67e40797 | 4300 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
4301 | goto error; |
4302 | } else { | |
4303 | ret = 0; | |
4304 | } | |
d6f42150 DG |
4305 | } |
4306 | ||
4307 | error: | |
4308 | return ret; | |
4309 | } | |
4310 | ||
4311 | /* | |
d063d709 DG |
4312 | * Setup sockets and directory needed by the kconsumerd communication with the |
4313 | * session daemon. | |
d6f42150 | 4314 | */ |
67e40797 DG |
4315 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
4316 | const char *rundir) | |
d6f42150 DG |
4317 | { |
4318 | int ret; | |
67e40797 | 4319 | char path[PATH_MAX]; |
d6f42150 | 4320 | |
6c71277b | 4321 | switch (consumer_data->type) { |
7753dea8 | 4322 | case LTTNG_CONSUMER_KERNEL: |
60922cb0 | 4323 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
7753dea8 MD |
4324 | break; |
4325 | case LTTNG_CONSUMER64_UST: | |
60922cb0 | 4326 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
4327 | break; |
4328 | case LTTNG_CONSUMER32_UST: | |
60922cb0 | 4329 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
4330 | break; |
4331 | default: | |
4332 | ERR("Consumer type unknown"); | |
4333 | ret = -EINVAL; | |
4334 | goto error; | |
d6f42150 DG |
4335 | } |
4336 | ||
67e40797 DG |
4337 | DBG2("Creating consumer directory: %s", path); |
4338 | ||
6c71277b | 4339 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
d6f42150 | 4340 | if (ret < 0) { |
6beb2242 | 4341 | if (errno != EEXIST) { |
f11e84c2 | 4342 | PERROR("mkdir"); |
3bd1e081 | 4343 | ERR("Failed to create %s", path); |
6beb2242 DG |
4344 | goto error; |
4345 | } | |
f11e84c2 | 4346 | ret = -1; |
d6f42150 | 4347 | } |
6c71277b MD |
4348 | if (is_root) { |
4349 | ret = chown(path, 0, utils_get_group_id(tracing_group_name)); | |
4350 | if (ret < 0) { | |
4351 | ERR("Unable to set group on %s", path); | |
4352 | PERROR("chown"); | |
4353 | goto error; | |
4354 | } | |
4355 | } | |
d6f42150 DG |
4356 | |
4357 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
4358 | consumer_data->err_sock = |
4359 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
4360 | if (consumer_data->err_sock < 0) { | |
4361 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
4362 | ret = -1; |
4363 | goto error; | |
4364 | } | |
4365 | ||
a24f05ab MD |
4366 | /* |
4367 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4368 | * show must go on. | |
4369 | */ | |
4370 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
4371 | if (ret < 0) { | |
4372 | PERROR("utils_set_fd_cloexec"); | |
4373 | /* continue anyway */ | |
4374 | } | |
4375 | ||
d6f42150 | 4376 | /* File permission MUST be 660 */ |
3bd1e081 | 4377 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 4378 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 4379 | if (ret < 0) { |
3bd1e081 | 4380 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 4381 | PERROR("chmod"); |
d6f42150 DG |
4382 | goto error; |
4383 | } | |
4384 | ||
4385 | error: | |
4386 | return ret; | |
4387 | } | |
4388 | ||
fac6795d | 4389 | /* |
d063d709 | 4390 | * Signal handler for the daemon |
cf3af59e | 4391 | * |
54d01ffb DG |
4392 | * Simply stop all worker threads, leaving main() return gracefully after |
4393 | * joining all threads and calling cleanup(). | |
fac6795d DG |
4394 | */ |
4395 | static void sighandler(int sig) | |
4396 | { | |
4397 | switch (sig) { | |
cf3af59e | 4398 | case SIGPIPE: |
af87c45a | 4399 | DBG("SIGPIPE caught"); |
cf3af59e MD |
4400 | return; |
4401 | case SIGINT: | |
af87c45a | 4402 | DBG("SIGINT caught"); |
cf3af59e MD |
4403 | stop_threads(); |
4404 | break; | |
4405 | case SIGTERM: | |
af87c45a | 4406 | DBG("SIGTERM caught"); |
cf3af59e MD |
4407 | stop_threads(); |
4408 | break; | |
0bb7724a DG |
4409 | case SIGUSR1: |
4410 | CMM_STORE_SHARED(recv_child_signal, 1); | |
4411 | break; | |
cf3af59e MD |
4412 | default: |
4413 | break; | |
fac6795d | 4414 | } |
fac6795d DG |
4415 | } |
4416 | ||
4417 | /* | |
d063d709 | 4418 | * Setup signal handler for : |
1d4b027a | 4419 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 4420 | */ |
1d4b027a | 4421 | static int set_signal_handler(void) |
fac6795d | 4422 | { |
1d4b027a DG |
4423 | int ret = 0; |
4424 | struct sigaction sa; | |
4425 | sigset_t sigset; | |
fac6795d | 4426 | |
1d4b027a | 4427 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 4428 | PERROR("sigemptyset"); |
1d4b027a DG |
4429 | return ret; |
4430 | } | |
d6f42150 | 4431 | |
1d4b027a DG |
4432 | sa.sa_handler = sighandler; |
4433 | sa.sa_mask = sigset; | |
4434 | sa.sa_flags = 0; | |
4435 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
76d7553f | 4436 | PERROR("sigaction"); |
1d4b027a | 4437 | return ret; |
d6f42150 DG |
4438 | } |
4439 | ||
1d4b027a | 4440 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 4441 | PERROR("sigaction"); |
1d4b027a | 4442 | return ret; |
d6f42150 | 4443 | } |
aaf26714 | 4444 | |
1d4b027a | 4445 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
76d7553f | 4446 | PERROR("sigaction"); |
1d4b027a | 4447 | return ret; |
8c0faa1d DG |
4448 | } |
4449 | ||
0bb7724a DG |
4450 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
4451 | PERROR("sigaction"); | |
4452 | return ret; | |
4453 | } | |
4454 | ||
4455 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
4456 | |
4457 | return ret; | |
fac6795d DG |
4458 | } |
4459 | ||
f3ed775e | 4460 | /* |
d063d709 DG |
4461 | * Set open files limit to unlimited. This daemon can open a large number of |
4462 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
4463 | */ |
4464 | static void set_ulimit(void) | |
4465 | { | |
4466 | int ret; | |
4467 | struct rlimit lim; | |
4468 | ||
a88df331 | 4469 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
4470 | lim.rlim_cur = 65535; |
4471 | lim.rlim_max = 65535; | |
4472 | ||
4473 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
4474 | if (ret < 0) { | |
76d7553f | 4475 | PERROR("failed to set open files limit"); |
f3ed775e DG |
4476 | } |
4477 | } | |
4478 | ||
35f90c40 DG |
4479 | /* |
4480 | * Write pidfile using the rundir and opt_pidfile. | |
4481 | */ | |
4482 | static void write_pidfile(void) | |
4483 | { | |
4484 | int ret; | |
4485 | char pidfile_path[PATH_MAX]; | |
4486 | ||
4487 | assert(rundir); | |
4488 | ||
4489 | if (opt_pidfile) { | |
4490 | strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path)); | |
4491 | } else { | |
4492 | /* Build pidfile path from rundir and opt_pidfile. */ | |
4493 | ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/" | |
4494 | DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir); | |
4495 | if (ret < 0) { | |
4496 | PERROR("snprintf pidfile path"); | |
4497 | goto error; | |
4498 | } | |
4499 | } | |
4500 | ||
4501 | /* | |
4502 | * Create pid file in rundir. Return value is of no importance. The | |
4503 | * execution will continue even though we are not able to write the file. | |
4504 | */ | |
4505 | (void) utils_create_pid_file(getpid(), pidfile_path); | |
4506 | ||
4507 | error: | |
4508 | return; | |
4509 | } | |
4510 | ||
cd9290dd DG |
4511 | /* |
4512 | * Write JUL TCP port using the rundir. | |
4513 | */ | |
4514 | static void write_julport(void) | |
4515 | { | |
4516 | int ret; | |
4517 | char path[PATH_MAX]; | |
4518 | ||
4519 | assert(rundir); | |
4520 | ||
4521 | ret = snprintf(path, sizeof(path), "%s/" | |
4522 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir); | |
4523 | if (ret < 0) { | |
4524 | PERROR("snprintf julport path"); | |
4525 | goto error; | |
4526 | } | |
4527 | ||
4528 | /* | |
4529 | * Create TCP JUL port file in rundir. Return value is of no importance. | |
4530 | * The execution will continue even though we are not able to write the | |
4531 | * file. | |
4532 | */ | |
4533 | (void) utils_create_pid_file(jul_tcp_port, path); | |
4534 | ||
4535 | error: | |
4536 | return; | |
4537 | } | |
4538 | ||
fac6795d DG |
4539 | /* |
4540 | * main | |
4541 | */ | |
4542 | int main(int argc, char **argv) | |
4543 | { | |
fac6795d DG |
4544 | int ret = 0; |
4545 | void *status; | |
ae9e45b3 | 4546 | const char *home_path, *env_app_timeout; |
fac6795d | 4547 | |
335a95b7 MD |
4548 | init_kernel_workarounds(); |
4549 | ||
f6a9efaa DG |
4550 | rcu_register_thread(); |
4551 | ||
0bb7724a DG |
4552 | if ((ret = set_signal_handler()) < 0) { |
4553 | goto error; | |
4554 | } | |
4555 | ||
7753dea8 | 4556 | setup_consumerd_path(); |
fb09408a | 4557 | |
12744796 DG |
4558 | page_size = sysconf(_SC_PAGESIZE); |
4559 | if (page_size < 0) { | |
4560 | PERROR("sysconf _SC_PAGESIZE"); | |
4561 | page_size = LONG_MAX; | |
4562 | WARN("Fallback page size to %ld", page_size); | |
4563 | } | |
4564 | ||
fac6795d DG |
4565 | /* Parse arguments */ |
4566 | progname = argv[0]; | |
c617c0c6 | 4567 | if ((ret = parse_args(argc, argv)) < 0) { |
cf3af59e | 4568 | goto error; |
fac6795d DG |
4569 | } |
4570 | ||
4571 | /* Daemonize */ | |
929de64e | 4572 | if (opt_daemon || opt_background) { |
ceed52b5 MD |
4573 | int i; |
4574 | ||
929de64e MD |
4575 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
4576 | !opt_background); | |
53094c05 | 4577 | if (ret < 0) { |
cf3af59e | 4578 | goto error; |
53094c05 | 4579 | } |
0bb7724a | 4580 | |
ceed52b5 | 4581 | /* |
0bb7724a DG |
4582 | * We are in the child. Make sure all other file descriptors are |
4583 | * closed, in case we are called with more opened file descriptors than | |
4584 | * the standard ones. | |
ceed52b5 MD |
4585 | */ |
4586 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
4587 | (void) close(i); | |
4588 | } | |
4589 | } | |
4590 | ||
4591 | /* Create thread quit pipe */ | |
4592 | if ((ret = init_thread_quit_pipe()) < 0) { | |
4593 | goto error; | |
fac6795d DG |
4594 | } |
4595 | ||
4596 | /* Check if daemon is UID = 0 */ | |
4597 | is_root = !getuid(); | |
4598 | ||
fac6795d | 4599 | if (is_root) { |
990570ed | 4600 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
67e40797 DG |
4601 | |
4602 | /* Create global run dir with root access */ | |
4603 | ret = create_lttng_rundir(rundir); | |
d6f42150 | 4604 | if (ret < 0) { |
cf3af59e | 4605 | goto error; |
d6f42150 DG |
4606 | } |
4607 | ||
fac6795d | 4608 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
4609 | snprintf(apps_unix_sock_path, PATH_MAX, |
4610 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
4611 | } |
4612 | ||
4613 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
4614 | snprintf(client_unix_sock_path, PATH_MAX, |
4615 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
4616 | } | |
0fdd1e2c DG |
4617 | |
4618 | /* Set global SHM for ust */ | |
4619 | if (strlen(wait_shm_path) == 0) { | |
4620 | snprintf(wait_shm_path, PATH_MAX, | |
4621 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
4622 | } | |
67e40797 | 4623 | |
44a5e5eb DG |
4624 | if (strlen(health_unix_sock_path) == 0) { |
4625 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4626 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); | |
4627 | } | |
4628 | ||
67e40797 DG |
4629 | /* Setup kernel consumerd path */ |
4630 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4631 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
67e40797 | 4632 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4633 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4634 | |
4635 | DBG2("Kernel consumer err path: %s", | |
4636 | kconsumer_data.err_unix_sock_path); | |
4637 | DBG2("Kernel consumer cmd path: %s", | |
4638 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 4639 | } else { |
feb0f3e5 | 4640 | home_path = utils_get_home_dir(); |
b082db07 | 4641 | if (home_path == NULL) { |
273ea72c DG |
4642 | /* TODO: Add --socket PATH option */ |
4643 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
4644 | ret = -EPERM; |
4645 | goto error; | |
b082db07 DG |
4646 | } |
4647 | ||
67e40797 DG |
4648 | /* |
4649 | * Create rundir from home path. This will create something like | |
4650 | * $HOME/.lttng | |
4651 | */ | |
990570ed | 4652 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
67e40797 DG |
4653 | if (ret < 0) { |
4654 | ret = -ENOMEM; | |
4655 | goto error; | |
4656 | } | |
4657 | ||
4658 | ret = create_lttng_rundir(rundir); | |
4659 | if (ret < 0) { | |
4660 | goto error; | |
4661 | } | |
4662 | ||
fac6795d | 4663 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 4664 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 4665 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
4666 | } |
4667 | ||
4668 | /* Set the cli tool unix socket path */ | |
4669 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 4670 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 4671 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 4672 | } |
0fdd1e2c DG |
4673 | |
4674 | /* Set global SHM for ust */ | |
4675 | if (strlen(wait_shm_path) == 0) { | |
4676 | snprintf(wait_shm_path, PATH_MAX, | |
d0b96690 | 4677 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); |
0fdd1e2c | 4678 | } |
44a5e5eb DG |
4679 | |
4680 | /* Set health check Unix path */ | |
4681 | if (strlen(health_unix_sock_path) == 0) { | |
4682 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4683 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); | |
4684 | } | |
fac6795d DG |
4685 | } |
4686 | ||
5c827ce0 DG |
4687 | /* Set consumer initial state */ |
4688 | kernel_consumerd_state = CONSUMER_STOPPED; | |
4689 | ust_consumerd_state = CONSUMER_STOPPED; | |
4690 | ||
847177cd DG |
4691 | DBG("Client socket path %s", client_unix_sock_path); |
4692 | DBG("Application socket path %s", apps_unix_sock_path); | |
d0b96690 | 4693 | DBG("Application wait path %s", wait_shm_path); |
67e40797 DG |
4694 | DBG("LTTng run directory path: %s", rundir); |
4695 | ||
4696 | /* 32 bits consumerd path setup */ | |
4697 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4698 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
67e40797 | 4699 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4700 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4701 | |
4702 | DBG2("UST consumer 32 bits err path: %s", | |
4703 | ustconsumer32_data.err_unix_sock_path); | |
4704 | DBG2("UST consumer 32 bits cmd path: %s", | |
4705 | ustconsumer32_data.cmd_unix_sock_path); | |
4706 | ||
4707 | /* 64 bits consumerd path setup */ | |
4708 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4709 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
67e40797 | 4710 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4711 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4712 | |
4713 | DBG2("UST consumer 64 bits err path: %s", | |
4714 | ustconsumer64_data.err_unix_sock_path); | |
4715 | DBG2("UST consumer 64 bits cmd path: %s", | |
4716 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 4717 | |
273ea72c | 4718 | /* |
7d8234d9 | 4719 | * See if daemon already exist. |
fac6795d | 4720 | */ |
7d8234d9 | 4721 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 4722 | ERR("Already running daemon.\n"); |
273ea72c | 4723 | /* |
cf3af59e MD |
4724 | * We do not goto exit because we must not cleanup() |
4725 | * because a daemon is already running. | |
ab118b20 | 4726 | */ |
cf3af59e | 4727 | goto error; |
a88df331 DG |
4728 | } |
4729 | ||
1427f9b2 DG |
4730 | /* |
4731 | * Init UST app hash table. Alloc hash table before this point since | |
4732 | * cleanup() can get called after that point. | |
4733 | */ | |
4734 | ust_app_ht_alloc(); | |
4735 | ||
f20baf8e DG |
4736 | /* Initialize JUL domain subsystem. */ |
4737 | if ((ret = jul_init()) < 0) { | |
4738 | /* ENOMEM at this point. */ | |
4739 | goto error; | |
4740 | } | |
4741 | ||
54d01ffb | 4742 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
4743 | |
4744 | /* | |
4745 | * These actions must be executed as root. We do that *after* setting up | |
4746 | * the sockets path because we MUST make the check for another daemon using | |
4747 | * those paths *before* trying to set the kernel consumer sockets and init | |
4748 | * kernel tracer. | |
4749 | */ | |
4750 | if (is_root) { | |
67e40797 | 4751 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
7753dea8 MD |
4752 | if (ret < 0) { |
4753 | goto exit; | |
4754 | } | |
4755 | ||
a88df331 | 4756 | /* Setup kernel tracer */ |
4fba7219 DG |
4757 | if (!opt_no_kernel) { |
4758 | init_kernel_tracer(); | |
4759 | } | |
a88df331 DG |
4760 | |
4761 | /* Set ulimit for open files */ | |
4762 | set_ulimit(); | |
fac6795d | 4763 | } |
4063050c MD |
4764 | /* init lttng_fd tracking must be done after set_ulimit. */ |
4765 | lttng_fd_init(); | |
fac6795d | 4766 | |
67e40797 DG |
4767 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
4768 | if (ret < 0) { | |
4769 | goto exit; | |
4770 | } | |
4771 | ||
4772 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); | |
4773 | if (ret < 0) { | |
4774 | goto exit; | |
4775 | } | |
4776 | ||
d6f42150 | 4777 | /* Setup the needed unix socket */ |
cf3af59e MD |
4778 | if ((ret = init_daemon_socket()) < 0) { |
4779 | goto exit; | |
fac6795d DG |
4780 | } |
4781 | ||
4782 | /* Set credentials to socket */ | |
be040666 | 4783 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
cf3af59e | 4784 | goto exit; |
fac6795d DG |
4785 | } |
4786 | ||
5b8719f5 DG |
4787 | /* Get parent pid if -S, --sig-parent is specified. */ |
4788 | if (opt_sig_parent) { | |
4789 | ppid = getppid(); | |
4790 | } | |
4791 | ||
7a485870 | 4792 | /* Setup the kernel pipe for waking up the kernel thread */ |
6620da75 DG |
4793 | if (is_root && !opt_no_kernel) { |
4794 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { | |
4795 | goto exit; | |
4796 | } | |
7a485870 DG |
4797 | } |
4798 | ||
0b2dc8df MD |
4799 | /* Setup the thread ht_cleanup communication pipe. */ |
4800 | if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) { | |
4801 | goto exit; | |
4802 | } | |
4803 | ||
099e26bd | 4804 | /* Setup the thread apps communication pipe. */ |
ef599319 | 4805 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
099e26bd DG |
4806 | goto exit; |
4807 | } | |
4808 | ||
d0b96690 DG |
4809 | /* Setup the thread apps notify communication pipe. */ |
4810 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) { | |
4811 | goto exit; | |
4812 | } | |
4813 | ||
7972aab2 DG |
4814 | /* Initialize global buffer per UID and PID registry. */ |
4815 | buffer_reg_init_uid_registry(); | |
4816 | buffer_reg_init_pid_registry(); | |
4817 | ||
099e26bd DG |
4818 | /* Init UST command queue. */ |
4819 | cds_wfq_init(&ust_cmd_queue.queue); | |
4820 | ||
273ea72c | 4821 | /* |
54d01ffb DG |
4822 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
4823 | * statically declared in session.c | |
273ea72c | 4824 | */ |
54d01ffb | 4825 | session_list_ptr = session_get_list(); |
b5541356 | 4826 | |
5eb91c98 DG |
4827 | /* Set up max poll set size */ |
4828 | lttng_poll_set_max_size(); | |
4829 | ||
2f77fc4b | 4830 | cmd_init(); |
00e2e675 | 4831 | |
ae9e45b3 DG |
4832 | /* Check for the application socket timeout env variable. */ |
4833 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
4834 | if (env_app_timeout) { | |
4835 | app_socket_timeout = atoi(env_app_timeout); | |
4836 | } else { | |
4837 | app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; | |
4838 | } | |
4839 | ||
35f90c40 | 4840 | write_pidfile(); |
cd9290dd | 4841 | write_julport(); |
35f90c40 | 4842 | |
554831e7 MD |
4843 | /* Initialize communication library */ |
4844 | lttcomm_init(); | |
d831c249 DG |
4845 | /* This is to get the TCP timeout value. */ |
4846 | lttcomm_inet_init(); | |
554831e7 | 4847 | |
67e05644 DG |
4848 | /* |
4849 | * Initialize the health check subsystem. This call should set the | |
4850 | * appropriate time values. | |
4851 | */ | |
6c71277b | 4852 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
8782cc74 MD |
4853 | if (!health_sessiond) { |
4854 | PERROR("health_app_create error"); | |
4855 | goto exit_health_sessiond_cleanup; | |
4856 | } | |
67e05644 | 4857 | |
d5b9fd2f | 4858 | /* Create thread to clean up RCU hash tables */ |
0b2dc8df MD |
4859 | ret = pthread_create(&ht_cleanup_thread, NULL, |
4860 | thread_ht_cleanup, (void *) NULL); | |
4861 | if (ret != 0) { | |
4862 | PERROR("pthread_create ht_cleanup"); | |
4863 | goto exit_ht_cleanup; | |
4864 | } | |
4865 | ||
d5b9fd2f | 4866 | /* Create health-check thread */ |
44a5e5eb DG |
4867 | ret = pthread_create(&health_thread, NULL, |
4868 | thread_manage_health, (void *) NULL); | |
4869 | if (ret != 0) { | |
4870 | PERROR("pthread_create health"); | |
4871 | goto exit_health; | |
4872 | } | |
4873 | ||
cf3af59e | 4874 | /* Create thread to manage the client socket */ |
099e26bd DG |
4875 | ret = pthread_create(&client_thread, NULL, |
4876 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 4877 | if (ret != 0) { |
76d7553f | 4878 | PERROR("pthread_create clients"); |
cf3af59e MD |
4879 | goto exit_client; |
4880 | } | |
fac6795d | 4881 | |
099e26bd DG |
4882 | /* Create thread to dispatch registration */ |
4883 | ret = pthread_create(&dispatch_thread, NULL, | |
4884 | thread_dispatch_ust_registration, (void *) NULL); | |
4885 | if (ret != 0) { | |
76d7553f | 4886 | PERROR("pthread_create dispatch"); |
099e26bd DG |
4887 | goto exit_dispatch; |
4888 | } | |
4889 | ||
4890 | /* Create thread to manage application registration. */ | |
4891 | ret = pthread_create(®_apps_thread, NULL, | |
4892 | thread_registration_apps, (void *) NULL); | |
4893 | if (ret != 0) { | |
76d7553f | 4894 | PERROR("pthread_create registration"); |
099e26bd DG |
4895 | goto exit_reg_apps; |
4896 | } | |
4897 | ||
cf3af59e | 4898 | /* Create thread to manage application socket */ |
54d01ffb DG |
4899 | ret = pthread_create(&apps_thread, NULL, |
4900 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 4901 | if (ret != 0) { |
d0b96690 DG |
4902 | PERROR("pthread_create apps"); |
4903 | goto exit_apps; | |
4904 | } | |
4905 | ||
4906 | /* Create thread to manage application notify socket */ | |
4907 | ret = pthread_create(&apps_notify_thread, NULL, | |
4908 | ust_thread_manage_notify, (void *) NULL); | |
4909 | if (ret != 0) { | |
76d7553f | 4910 | PERROR("pthread_create apps"); |
9563b0ad | 4911 | goto exit_apps_notify; |
cf3af59e | 4912 | } |
fac6795d | 4913 | |
4d076222 DG |
4914 | /* Create JUL registration thread. */ |
4915 | ret = pthread_create(&jul_reg_thread, NULL, | |
4916 | jul_thread_manage_registration, (void *) NULL); | |
4917 | if (ret != 0) { | |
4918 | PERROR("pthread_create apps"); | |
4919 | goto exit_jul_reg; | |
4920 | } | |
4921 | ||
6620da75 DG |
4922 | /* Don't start this thread if kernel tracing is not requested nor root */ |
4923 | if (is_root && !opt_no_kernel) { | |
4924 | /* Create kernel thread to manage kernel event */ | |
4925 | ret = pthread_create(&kernel_thread, NULL, | |
4926 | thread_manage_kernel, (void *) NULL); | |
4927 | if (ret != 0) { | |
4928 | PERROR("pthread_create kernel"); | |
4929 | goto exit_kernel; | |
4930 | } | |
7a485870 | 4931 | |
6620da75 DG |
4932 | ret = pthread_join(kernel_thread, &status); |
4933 | if (ret != 0) { | |
4934 | PERROR("pthread_join"); | |
4935 | goto error; /* join error, exit without cleanup */ | |
4936 | } | |
fac6795d DG |
4937 | } |
4938 | ||
cf3af59e | 4939 | exit_kernel: |
4d076222 DG |
4940 | ret = pthread_join(jul_reg_thread, &status); |
4941 | if (ret != 0) { | |
4942 | PERROR("pthread_join JUL"); | |
4943 | goto error; /* join error, exit without cleanup */ | |
4944 | } | |
4945 | ||
4946 | exit_jul_reg: | |
9563b0ad DG |
4947 | ret = pthread_join(apps_notify_thread, &status); |
4948 | if (ret != 0) { | |
4949 | PERROR("pthread_join apps notify"); | |
4950 | goto error; /* join error, exit without cleanup */ | |
4951 | } | |
4952 | ||
4953 | exit_apps_notify: | |
cf3af59e MD |
4954 | ret = pthread_join(apps_thread, &status); |
4955 | if (ret != 0) { | |
9563b0ad | 4956 | PERROR("pthread_join apps"); |
cf3af59e MD |
4957 | goto error; /* join error, exit without cleanup */ |
4958 | } | |
fac6795d | 4959 | |
4ccd8c8f | 4960 | |
cf3af59e | 4961 | exit_apps: |
099e26bd DG |
4962 | ret = pthread_join(reg_apps_thread, &status); |
4963 | if (ret != 0) { | |
76d7553f | 4964 | PERROR("pthread_join"); |
099e26bd DG |
4965 | goto error; /* join error, exit without cleanup */ |
4966 | } | |
4967 | ||
4968 | exit_reg_apps: | |
4969 | ret = pthread_join(dispatch_thread, &status); | |
4970 | if (ret != 0) { | |
76d7553f | 4971 | PERROR("pthread_join"); |
099e26bd DG |
4972 | goto error; /* join error, exit without cleanup */ |
4973 | } | |
4974 | ||
4975 | exit_dispatch: | |
cf3af59e MD |
4976 | ret = pthread_join(client_thread, &status); |
4977 | if (ret != 0) { | |
76d7553f | 4978 | PERROR("pthread_join"); |
cf3af59e MD |
4979 | goto error; /* join error, exit without cleanup */ |
4980 | } | |
4981 | ||
3bd1e081 | 4982 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 4983 | if (ret != 0) { |
76d7553f | 4984 | PERROR("join_consumer"); |
cf3af59e MD |
4985 | goto error; /* join error, exit without cleanup */ |
4986 | } | |
a88df331 | 4987 | |
06f525de DG |
4988 | ret = join_consumer_thread(&ustconsumer32_data); |
4989 | if (ret != 0) { | |
4990 | PERROR("join_consumer ust32"); | |
4991 | goto error; /* join error, exit without cleanup */ | |
4992 | } | |
4993 | ||
4994 | ret = join_consumer_thread(&ustconsumer64_data); | |
4995 | if (ret != 0) { | |
4996 | PERROR("join_consumer ust64"); | |
4997 | goto error; /* join error, exit without cleanup */ | |
4998 | } | |
4999 | ||
cf3af59e | 5000 | exit_client: |
06f525de DG |
5001 | ret = pthread_join(health_thread, &status); |
5002 | if (ret != 0) { | |
5003 | PERROR("pthread_join health thread"); | |
5004 | goto error; /* join error, exit without cleanup */ | |
5005 | } | |
5006 | ||
44a5e5eb | 5007 | exit_health: |
0b2dc8df MD |
5008 | ret = pthread_join(ht_cleanup_thread, &status); |
5009 | if (ret != 0) { | |
5010 | PERROR("pthread_join ht cleanup thread"); | |
5011 | goto error; /* join error, exit without cleanup */ | |
5012 | } | |
5013 | exit_ht_cleanup: | |
8782cc74 MD |
5014 | health_app_destroy(health_sessiond); |
5015 | exit_health_sessiond_cleanup: | |
a88df331 | 5016 | exit: |
cf3af59e MD |
5017 | /* |
5018 | * cleanup() is called when no other thread is running. | |
5019 | */ | |
f6a9efaa | 5020 | rcu_thread_online(); |
cf3af59e | 5021 | cleanup(); |
f6a9efaa DG |
5022 | rcu_thread_offline(); |
5023 | rcu_unregister_thread(); | |
67e40797 | 5024 | if (!ret) { |
cf3af59e | 5025 | exit(EXIT_SUCCESS); |
67e40797 | 5026 | } |
cf3af59e | 5027 | error: |
5e16da05 | 5028 | exit(EXIT_FAILURE); |
fac6795d | 5029 | } |