Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
26296c48 | 4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
91d76f53 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
91d76f53 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
18 | */ |
19 | ||
20 | #define _GNU_SOURCE | |
6c1c0768 | 21 | #define _LGPL_SOURCE |
fac6795d DG |
22 | #include <getopt.h> |
23 | #include <grp.h> | |
24 | #include <limits.h> | |
0bb7724a | 25 | #include <paths.h> |
fac6795d DG |
26 | #include <pthread.h> |
27 | #include <signal.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
331744e3 | 31 | #include <inttypes.h> |
0fdd1e2c | 32 | #include <sys/mman.h> |
b73401da | 33 | #include <sys/mount.h> |
1e307fab | 34 | #include <sys/resource.h> |
fac6795d DG |
35 | #include <sys/socket.h> |
36 | #include <sys/stat.h> | |
37 | #include <sys/types.h> | |
0fdd1e2c | 38 | #include <sys/wait.h> |
5c827ce0 | 39 | #include <urcu/uatomic.h> |
fac6795d | 40 | #include <unistd.h> |
3bd1e081 | 41 | #include <config.h> |
fac6795d | 42 | |
990570ed | 43 | #include <common/common.h> |
d27c42b8 | 44 | #include <common/compat/socket.h> |
db758600 DG |
45 | #include <common/defaults.h> |
46 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 47 | #include <common/futex.h> |
00e2e675 | 48 | #include <common/relayd/relayd.h> |
81b86775 | 49 | #include <common/utils.h> |
3ccdf997 | 50 | #include <common/daemonize.h> |
26296c48 | 51 | #include <common/config/config.h> |
fac6795d | 52 | |
10a8a223 | 53 | #include "lttng-sessiond.h" |
7972aab2 | 54 | #include "buffer-registry.h" |
54d01ffb | 55 | #include "channel.h" |
2f77fc4b | 56 | #include "cmd.h" |
00e2e675 | 57 | #include "consumer.h" |
099e26bd | 58 | #include "context.h" |
54d01ffb | 59 | #include "event.h" |
4771f025 | 60 | #include "kernel.h" |
f1e16794 | 61 | #include "kernel-consumer.h" |
096102bd | 62 | #include "modprobe.h" |
0fdd1e2c | 63 | #include "shm.h" |
1e307fab | 64 | #include "ust-ctl.h" |
00e2e675 | 65 | #include "ust-consumer.h" |
8e68d1c8 | 66 | #include "utils.h" |
4063050c | 67 | #include "fd-limit.h" |
8782cc74 | 68 | #include "health-sessiond.h" |
8ac94142 | 69 | #include "testpoint.h" |
d0b96690 | 70 | #include "ust-thread.h" |
022d91ba | 71 | #include "agent-thread.h" |
fb198a11 | 72 | #include "save.h" |
ef367a93 | 73 | #include "load-session-thread.h" |
834978fd | 74 | #include "syscall.h" |
fac6795d | 75 | |
ebaeda94 MD |
76 | #define CONSUMERD_FILE "lttng-consumerd" |
77 | ||
fac6795d | 78 | const char *progname; |
6c71277b | 79 | static const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
26296c48 JG |
80 | static int tracing_group_name_override; |
81 | static char *opt_pidfile; | |
5b8719f5 | 82 | static int opt_sig_parent; |
97e19046 | 83 | static int opt_verbose_consumer; |
72dd7491 | 84 | static int opt_daemon, opt_background; |
4fba7219 | 85 | static int opt_no_kernel; |
ef367a93 | 86 | static char *opt_load_session_path; |
1d4b027a | 87 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
0bb7724a | 88 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ |
67e40797 | 89 | static char *rundir; |
c9cb3e7d | 90 | static int lockfile_fd = -1; |
3bd1e081 | 91 | |
0bb7724a DG |
92 | /* Set to 1 when a SIGUSR1 signal is received. */ |
93 | static int recv_child_signal; | |
94 | ||
a23ec3a7 DG |
95 | /* |
96 | * Consumer daemon specific control data. Every value not initialized here is | |
97 | * set to 0 by the static definition. | |
98 | */ | |
3bd1e081 MD |
99 | static struct consumer_data kconsumer_data = { |
100 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
101 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
102 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
103 | .err_sock = -1, |
104 | .cmd_sock = -1, | |
173af62f DG |
105 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
106 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
107 | .cond = PTHREAD_COND_INITIALIZER, |
108 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 109 | }; |
7753dea8 MD |
110 | static struct consumer_data ustconsumer64_data = { |
111 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
112 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
113 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
114 | .err_sock = -1, |
115 | .cmd_sock = -1, | |
173af62f DG |
116 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
117 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
118 | .cond = PTHREAD_COND_INITIALIZER, |
119 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
120 | }; |
121 | static struct consumer_data ustconsumer32_data = { | |
122 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
123 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
124 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
125 | .err_sock = -1, |
126 | .cmd_sock = -1, | |
173af62f DG |
127 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
128 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
129 | .cond = PTHREAD_COND_INITIALIZER, |
130 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
131 | }; |
132 | ||
26296c48 JG |
133 | /* Command line options */ |
134 | static const struct option long_options[] = { | |
135 | { "client-sock", 1, 0, 'c' }, | |
136 | { "apps-sock", 1, 0, 'a' }, | |
137 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, | |
138 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
139 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, | |
140 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
141 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, | |
142 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
143 | { "consumerd32-path", 1, 0, 'u' }, | |
144 | { "consumerd32-libdir", 1, 0, 'U' }, | |
145 | { "consumerd64-path", 1, 0, 't' }, | |
146 | { "consumerd64-libdir", 1, 0, 'T' }, | |
147 | { "daemonize", 0, 0, 'd' }, | |
72dd7491 | 148 | { "background", 0, 0, 'b' }, |
26296c48 JG |
149 | { "sig-parent", 0, 0, 'S' }, |
150 | { "help", 0, 0, 'h' }, | |
151 | { "group", 1, 0, 'g' }, | |
152 | { "version", 0, 0, 'V' }, | |
153 | { "quiet", 0, 0, 'q' }, | |
154 | { "verbose", 0, 0, 'v' }, | |
155 | { "verbose-consumer", 0, 0, 'Z' }, | |
156 | { "no-kernel", 0, 0, 'N' }, | |
157 | { "pidfile", 1, 0, 'p' }, | |
1b2ef7fe | 158 | { "agent-tcp-port", 1, 0, 'J' }, |
26296c48 | 159 | { "config", 1, 0, 'f' }, |
ef367a93 | 160 | { "load", 1, 0, 'l' }, |
fbb9748b | 161 | { "kmod-probes", 1, 0, 'P' }, |
c9d42407 | 162 | { "extra-kmod-probes", 1, 0, 'e' }, |
26296c48 JG |
163 | { NULL, 0, 0, 0 } |
164 | }; | |
165 | ||
166 | /* Command line options to ignore from configuration file */ | |
167 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
168 | ||
26c9d55e | 169 | /* Shared between threads */ |
099e26bd | 170 | static int dispatch_thread_exit; |
fac6795d | 171 | |
54d01ffb DG |
172 | /* Global application Unix socket path */ |
173 | static char apps_unix_sock_path[PATH_MAX]; | |
174 | /* Global client Unix socket path */ | |
175 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
176 | /* global wait shm path for UST */ |
177 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
178 | /* Global health check unix path */ |
179 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 180 | |
1d4b027a | 181 | /* Sockets and FDs */ |
a4b35e07 MD |
182 | static int client_sock = -1; |
183 | static int apps_sock = -1; | |
2f77fc4b | 184 | int kernel_tracer_fd = -1; |
76d7553f | 185 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 186 | |
273ea72c DG |
187 | /* |
188 | * Quit pipe for all threads. This permits a single cancellation point | |
189 | * for all threads when receiving an event on the pipe. | |
190 | */ | |
76d7553f | 191 | static int thread_quit_pipe[2] = { -1, -1 }; |
4a15001e | 192 | static int ht_cleanup_quit_pipe[2] = { -1, -1 }; |
273ea72c | 193 | |
099e26bd DG |
194 | /* |
195 | * This pipe is used to inform the thread managing application communication | |
196 | * that a command is queued and ready to be processed. | |
197 | */ | |
76d7553f | 198 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 199 | |
d0b96690 DG |
200 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
201 | ||
1d4b027a | 202 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 203 | static pthread_t apps_thread; |
d0b96690 | 204 | static pthread_t apps_notify_thread; |
099e26bd | 205 | static pthread_t reg_apps_thread; |
1d4b027a | 206 | static pthread_t client_thread; |
7a485870 | 207 | static pthread_t kernel_thread; |
099e26bd | 208 | static pthread_t dispatch_thread; |
44a5e5eb | 209 | static pthread_t health_thread; |
0b2dc8df | 210 | static pthread_t ht_cleanup_thread; |
022d91ba | 211 | static pthread_t agent_reg_thread; |
ef367a93 | 212 | static pthread_t load_session_thread; |
5eb91c98 | 213 | |
099e26bd DG |
214 | /* |
215 | * UST registration command queue. This queue is tied with a futex and uses a N | |
216 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
217 | * | |
b22c5da8 DG |
218 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
219 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
220 | * the line new application socket and monitors it for any I/O error or clean | |
221 | * close that triggers an unregistration of the application. | |
099e26bd DG |
222 | */ |
223 | static struct ust_cmd_queue ust_cmd_queue; | |
224 | ||
b5541356 DG |
225 | /* |
226 | * Pointer initialized before thread creation. | |
227 | * | |
228 | * This points to the tracing session list containing the session count and a | |
229 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
230 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 231 | * |
d063d709 | 232 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 233 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
234 | */ |
235 | static struct ltt_session_list *session_list_ptr; | |
236 | ||
7753dea8 MD |
237 | int ust_consumerd64_fd = -1; |
238 | int ust_consumerd32_fd = -1; | |
239 | ||
fb6f1fa2 YB |
240 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
241 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
242 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
243 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
26296c48 JG |
244 | static int consumerd32_bin_override; |
245 | static int consumerd64_bin_override; | |
246 | static int consumerd32_libdir_override; | |
247 | static int consumerd64_libdir_override; | |
fb09408a | 248 | |
2f77fc4b DG |
249 | static const char *module_proc_lttng = "/proc/lttng"; |
250 | ||
5c827ce0 DG |
251 | /* |
252 | * Consumer daemon state which is changed when spawning it, killing it or in | |
253 | * case of a fatal error. | |
254 | */ | |
255 | enum consumerd_state { | |
256 | CONSUMER_STARTED = 1, | |
257 | CONSUMER_STOPPED = 2, | |
258 | CONSUMER_ERROR = 3, | |
259 | }; | |
260 | ||
261 | /* | |
262 | * This consumer daemon state is used to validate if a client command will be | |
263 | * able to reach the consumer. If not, the client is informed. For instance, | |
264 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
265 | * error to the client. | |
266 | * | |
267 | * The following example shows a possible race condition of this scheme: | |
268 | * | |
269 | * consumer thread error happens | |
270 | * client cmd arrives | |
271 | * client cmd checks state -> still OK | |
272 | * consumer thread exit, sets error | |
273 | * client cmd try to talk to consumer | |
274 | * ... | |
275 | * | |
276 | * However, since the consumer is a different daemon, we have no way of making | |
277 | * sure the command will reach it safely even with this state flag. This is why | |
278 | * we consider that up to the state validation during command processing, the | |
279 | * command is safe. After that, we can not guarantee the correctness of the | |
280 | * client request vis-a-vis the consumer. | |
281 | */ | |
282 | static enum consumerd_state ust_consumerd_state; | |
283 | static enum consumerd_state kernel_consumerd_state; | |
284 | ||
ae9e45b3 DG |
285 | /* |
286 | * Socket timeout for receiving and sending in seconds. | |
287 | */ | |
288 | static int app_socket_timeout; | |
289 | ||
12744796 DG |
290 | /* Set in main() with the current page size. */ |
291 | long page_size; | |
292 | ||
8782cc74 MD |
293 | /* Application health monitoring */ |
294 | struct health_app *health_sessiond; | |
295 | ||
022d91ba DG |
296 | /* Agent TCP port for registration. Used by the agent thread. */ |
297 | unsigned int agent_tcp_port = DEFAULT_AGENT_TCP_PORT; | |
4d076222 | 298 | |
f43f95a9 DG |
299 | /* Am I root or not. */ |
300 | int is_root; /* Set to 1 if the daemon is running as root */ | |
301 | ||
26296c48 JG |
302 | const char * const config_section_name = "sessiond"; |
303 | ||
ef367a93 JG |
304 | /* Load session thread information to operate. */ |
305 | struct load_session_thread_data *load_info; | |
306 | ||
97bc1426 MD |
307 | /* |
308 | * Whether sessiond is ready for commands/health check requests. | |
309 | * NR_LTTNG_SESSIOND_READY must match the number of calls to | |
ef367a93 | 310 | * sessiond_notify_ready(). |
97bc1426 | 311 | */ |
ef367a93 | 312 | #define NR_LTTNG_SESSIOND_READY 3 |
97bc1426 MD |
313 | int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY; |
314 | ||
315 | /* Notify parents that we are ready for cmd and health check */ | |
ef367a93 JG |
316 | LTTNG_HIDDEN |
317 | void sessiond_notify_ready(void) | |
97bc1426 MD |
318 | { |
319 | if (uatomic_sub_return(<tng_sessiond_ready, 1) == 0) { | |
320 | /* | |
321 | * Notify parent pid that we are ready to accept command | |
322 | * for client side. This ppid is the one from the | |
323 | * external process that spawned us. | |
324 | */ | |
325 | if (opt_sig_parent) { | |
326 | kill(ppid, SIGUSR1); | |
327 | } | |
328 | ||
329 | /* | |
330 | * Notify the parent of the fork() process that we are | |
331 | * ready. | |
332 | */ | |
72dd7491 | 333 | if (opt_daemon || opt_background) { |
97bc1426 MD |
334 | kill(child_ppid, SIGUSR1); |
335 | } | |
336 | } | |
337 | } | |
338 | ||
fb09408a | 339 | static |
7753dea8 | 340 | void setup_consumerd_path(void) |
fb09408a | 341 | { |
fc7a59ce | 342 | const char *bin, *libdir; |
fb09408a | 343 | |
7753dea8 MD |
344 | /* |
345 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
346 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
347 | * has not been defined. | |
7753dea8 | 348 | */ |
ebaeda94 | 349 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
350 | if (!consumerd32_bin[0]) { |
351 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
352 | } |
353 | if (!consumerd32_libdir[0]) { | |
354 | consumerd32_libdir = INSTALL_LIB_PATH; | |
355 | } | |
356 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
357 | if (!consumerd64_bin[0]) { |
358 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 359 | } |
ebaeda94 MD |
360 | if (!consumerd64_libdir[0]) { |
361 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
362 | } |
363 | #else | |
364 | #error "Unknown bitness" | |
365 | #endif | |
366 | ||
fb09408a MD |
367 | /* |
368 | * runtime env. var. overrides the build default. | |
369 | */ | |
fc7a59ce AM |
370 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
371 | if (bin) { | |
372 | consumerd32_bin = bin; | |
7753dea8 | 373 | } |
fc7a59ce AM |
374 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
375 | if (bin) { | |
376 | consumerd64_bin = bin; | |
ebaeda94 | 377 | } |
72f579ee | 378 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
379 | if (libdir) { |
380 | consumerd32_libdir = libdir; | |
381 | } | |
72f579ee | 382 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
383 | if (libdir) { |
384 | consumerd64_libdir = libdir; | |
fb09408a MD |
385 | } |
386 | } | |
387 | ||
4a15001e MD |
388 | static |
389 | int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size, | |
390 | int *a_pipe) | |
5eb91c98 DG |
391 | { |
392 | int ret; | |
393 | ||
d0b96690 | 394 | assert(events); |
5eb91c98 DG |
395 | |
396 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
397 | if (ret < 0) { | |
398 | goto error; | |
399 | } | |
400 | ||
401 | /* Add quit pipe */ | |
4a15001e | 402 | ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
403 | if (ret < 0) { |
404 | goto error; | |
405 | } | |
406 | ||
407 | return 0; | |
408 | ||
409 | error: | |
410 | return ret; | |
411 | } | |
412 | ||
4a15001e MD |
413 | /* |
414 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
415 | */ | |
416 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) | |
417 | { | |
418 | return __sessiond_set_thread_pollset(events, size, thread_quit_pipe); | |
419 | } | |
420 | ||
421 | /* | |
422 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
423 | */ | |
424 | int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events, | |
425 | size_t size) | |
426 | { | |
427 | return __sessiond_set_thread_pollset(events, size, | |
428 | ht_cleanup_quit_pipe); | |
429 | } | |
430 | ||
431 | static | |
432 | int __sessiond_check_thread_quit_pipe(int fd, uint32_t events, int a_pipe) | |
433 | { | |
434 | if (fd == a_pipe && (events & LPOLLIN)) { | |
435 | return 1; | |
436 | } | |
437 | return 0; | |
438 | } | |
439 | ||
5eb91c98 DG |
440 | /* |
441 | * Check if the thread quit pipe was triggered. | |
442 | * | |
443 | * Return 1 if it was triggered else 0; | |
444 | */ | |
d0b96690 | 445 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
5eb91c98 | 446 | { |
4a15001e MD |
447 | return __sessiond_check_thread_quit_pipe(fd, events, |
448 | thread_quit_pipe[0]); | |
449 | } | |
5eb91c98 | 450 | |
4a15001e MD |
451 | /* |
452 | * Check if the ht_cleanup thread quit pipe was triggered. | |
453 | * | |
454 | * Return 1 if it was triggered else 0; | |
455 | */ | |
456 | int sessiond_check_ht_cleanup_quit(int fd, uint32_t events) | |
457 | { | |
458 | return __sessiond_check_thread_quit_pipe(fd, events, | |
459 | ht_cleanup_quit_pipe[0]); | |
5eb91c98 DG |
460 | } |
461 | ||
273ea72c | 462 | /* |
5eb91c98 | 463 | * Init thread quit pipe. |
273ea72c DG |
464 | * |
465 | * Return -1 on error or 0 if all pipes are created. | |
466 | */ | |
4a15001e | 467 | static int __init_thread_quit_pipe(int *a_pipe) |
273ea72c | 468 | { |
730389d9 | 469 | int ret, i; |
273ea72c | 470 | |
4a15001e | 471 | ret = pipe(a_pipe); |
273ea72c | 472 | if (ret < 0) { |
730389d9 | 473 | PERROR("thread quit pipe"); |
273ea72c DG |
474 | goto error; |
475 | } | |
476 | ||
730389d9 | 477 | for (i = 0; i < 2; i++) { |
4a15001e | 478 | ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC); |
730389d9 DG |
479 | if (ret < 0) { |
480 | PERROR("fcntl"); | |
481 | goto error; | |
482 | } | |
483 | } | |
484 | ||
273ea72c DG |
485 | error: |
486 | return ret; | |
487 | } | |
488 | ||
4a15001e MD |
489 | static int init_thread_quit_pipe(void) |
490 | { | |
491 | return __init_thread_quit_pipe(thread_quit_pipe); | |
492 | } | |
493 | ||
494 | static int init_ht_cleanup_quit_pipe(void) | |
495 | { | |
496 | return __init_thread_quit_pipe(ht_cleanup_quit_pipe); | |
497 | } | |
498 | ||
099e26bd DG |
499 | /* |
500 | * Stop all threads by closing the thread quit pipe. | |
501 | */ | |
cf3af59e MD |
502 | static void stop_threads(void) |
503 | { | |
5eb91c98 DG |
504 | int ret; |
505 | ||
cf3af59e MD |
506 | /* Stopping all threads */ |
507 | DBG("Terminating all threads"); | |
54d01ffb | 508 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
509 | if (ret < 0) { |
510 | ERR("write error on thread quit pipe"); | |
511 | } | |
512 | ||
099e26bd | 513 | /* Dispatch thread */ |
26c9d55e | 514 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 515 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
516 | } |
517 | ||
e975f9f8 DG |
518 | /* |
519 | * Close every consumer sockets. | |
520 | */ | |
521 | static void close_consumer_sockets(void) | |
522 | { | |
523 | int ret; | |
524 | ||
525 | if (kconsumer_data.err_sock >= 0) { | |
526 | ret = close(kconsumer_data.err_sock); | |
527 | if (ret < 0) { | |
528 | PERROR("kernel consumer err_sock close"); | |
529 | } | |
530 | } | |
531 | if (ustconsumer32_data.err_sock >= 0) { | |
532 | ret = close(ustconsumer32_data.err_sock); | |
533 | if (ret < 0) { | |
a76cbd9f | 534 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
535 | } |
536 | } | |
537 | if (ustconsumer64_data.err_sock >= 0) { | |
538 | ret = close(ustconsumer64_data.err_sock); | |
539 | if (ret < 0) { | |
a76cbd9f | 540 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
541 | } |
542 | } | |
543 | if (kconsumer_data.cmd_sock >= 0) { | |
544 | ret = close(kconsumer_data.cmd_sock); | |
545 | if (ret < 0) { | |
546 | PERROR("kernel consumer cmd_sock close"); | |
547 | } | |
548 | } | |
549 | if (ustconsumer32_data.cmd_sock >= 0) { | |
550 | ret = close(ustconsumer32_data.cmd_sock); | |
551 | if (ret < 0) { | |
a76cbd9f | 552 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
553 | } |
554 | } | |
555 | if (ustconsumer64_data.cmd_sock >= 0) { | |
556 | ret = close(ustconsumer64_data.cmd_sock); | |
557 | if (ret < 0) { | |
a76cbd9f | 558 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
559 | } |
560 | } | |
561 | } | |
562 | ||
c9cb3e7d JG |
563 | /* |
564 | * Generate the full lock file path using the rundir. | |
565 | * | |
566 | * Return the snprintf() return value thus a negative value is an error. | |
567 | */ | |
568 | static int generate_lock_file_path(char *path, size_t len) | |
569 | { | |
570 | int ret; | |
571 | ||
572 | assert(path); | |
573 | assert(rundir); | |
574 | ||
575 | /* Build lockfile path from rundir. */ | |
576 | ret = snprintf(path, len, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE, rundir); | |
577 | if (ret < 0) { | |
578 | PERROR("snprintf lockfile path"); | |
579 | } | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
fac6795d | 584 | /* |
4a15001e | 585 | * Cleanup the session daemon's data structures. |
fac6795d | 586 | */ |
4a15001e | 587 | static void sessiond_cleanup(void) |
fac6795d | 588 | { |
ef599319 | 589 | int ret; |
af9737e9 | 590 | struct ltt_session *sess, *stmp; |
8c6c56c2 | 591 | char path[PATH_MAX]; |
fac6795d | 592 | |
4a15001e | 593 | DBG("Cleanup sessiond"); |
e07ae692 | 594 | |
4e449f3f MD |
595 | /* |
596 | * Close the thread quit pipe. It has already done its job, | |
597 | * since we are now called. | |
598 | */ | |
2f77fc4b DG |
599 | utils_close_pipe(thread_quit_pipe); |
600 | ||
35f90c40 DG |
601 | /* |
602 | * If opt_pidfile is undefined, the default file will be wiped when | |
603 | * removing the rundir. | |
604 | */ | |
605 | if (opt_pidfile) { | |
606 | ret = remove(opt_pidfile); | |
607 | if (ret < 0) { | |
608 | PERROR("remove pidfile %s", opt_pidfile); | |
609 | } | |
610 | } | |
611 | ||
8c6c56c2 MD |
612 | DBG("Removing sessiond and consumerd content of directory %s", rundir); |
613 | ||
614 | /* sessiond */ | |
615 | snprintf(path, PATH_MAX, | |
616 | "%s/%s", | |
617 | rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE); | |
618 | DBG("Removing %s", path); | |
619 | (void) unlink(path); | |
620 | ||
cd9290dd | 621 | snprintf(path, PATH_MAX, "%s/%s", rundir, |
022d91ba | 622 | DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE); |
cd9290dd DG |
623 | DBG("Removing %s", path); |
624 | (void) unlink(path); | |
625 | ||
8c6c56c2 MD |
626 | /* kconsumerd */ |
627 | snprintf(path, PATH_MAX, | |
628 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
629 | rundir); | |
630 | DBG("Removing %s", path); | |
631 | (void) unlink(path); | |
632 | ||
633 | snprintf(path, PATH_MAX, | |
634 | DEFAULT_KCONSUMERD_PATH, | |
635 | rundir); | |
636 | DBG("Removing directory %s", path); | |
637 | (void) rmdir(path); | |
638 | ||
639 | /* ust consumerd 32 */ | |
640 | snprintf(path, PATH_MAX, | |
641 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
642 | rundir); | |
643 | DBG("Removing %s", path); | |
644 | (void) unlink(path); | |
645 | ||
646 | snprintf(path, PATH_MAX, | |
647 | DEFAULT_USTCONSUMERD32_PATH, | |
648 | rundir); | |
649 | DBG("Removing directory %s", path); | |
650 | (void) rmdir(path); | |
651 | ||
652 | /* ust consumerd 64 */ | |
653 | snprintf(path, PATH_MAX, | |
654 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
655 | rundir); | |
656 | DBG("Removing %s", path); | |
657 | (void) unlink(path); | |
658 | ||
659 | snprintf(path, PATH_MAX, | |
660 | DEFAULT_USTCONSUMERD64_PATH, | |
661 | rundir); | |
662 | DBG("Removing directory %s", path); | |
663 | (void) rmdir(path); | |
5461b305 | 664 | |
99bab54f | 665 | DBG("Cleaning up all sessions"); |
fac6795d | 666 | |
b5541356 | 667 | /* Destroy session list mutex */ |
273ea72c DG |
668 | if (session_list_ptr != NULL) { |
669 | pthread_mutex_destroy(&session_list_ptr->lock); | |
670 | ||
671 | /* Cleanup ALL session */ | |
54d01ffb DG |
672 | cds_list_for_each_entry_safe(sess, stmp, |
673 | &session_list_ptr->head, list) { | |
2f77fc4b | 674 | cmd_destroy_session(sess, kernel_poll_pipe[1]); |
273ea72c DG |
675 | } |
676 | } | |
677 | ||
099e26bd | 678 | DBG("Closing all UST sockets"); |
56fff090 | 679 | ust_app_clean_list(); |
7972aab2 | 680 | buffer_reg_destroy_registries(); |
099e26bd | 681 | |
4fba7219 DG |
682 | if (is_root && !opt_no_kernel) { |
683 | DBG2("Closing kernel fd"); | |
a4b35e07 | 684 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
685 | ret = close(kernel_tracer_fd); |
686 | if (ret) { | |
687 | PERROR("close"); | |
688 | } | |
a4b35e07 | 689 | } |
2f50c8a3 | 690 | DBG("Unloading kernel modules"); |
096102bd | 691 | modprobe_remove_lttng_all(); |
834978fd | 692 | free(syscall_table); |
2f50c8a3 | 693 | } |
2f77fc4b | 694 | |
e975f9f8 DG |
695 | close_consumer_sockets(); |
696 | ||
ef367a93 JG |
697 | if (load_info) { |
698 | load_session_destroy_data(load_info); | |
699 | free(load_info); | |
700 | } | |
701 | ||
c9cb3e7d JG |
702 | /* |
703 | * Cleanup lock file by deleting it and finaly closing it which will | |
704 | * release the file system lock. | |
705 | */ | |
706 | if (lockfile_fd >= 0) { | |
707 | char lockfile_path[PATH_MAX]; | |
708 | ||
4a15001e MD |
709 | ret = generate_lock_file_path(lockfile_path, |
710 | sizeof(lockfile_path)); | |
c9cb3e7d JG |
711 | if (ret > 0) { |
712 | ret = remove(lockfile_path); | |
713 | if (ret < 0) { | |
714 | PERROR("remove lock file"); | |
715 | } | |
716 | ret = close(lockfile_fd); | |
717 | if (ret < 0) { | |
718 | PERROR("close lock file"); | |
719 | } | |
720 | } | |
721 | } | |
722 | ||
723 | /* | |
724 | * We do NOT rmdir rundir because there are other processes | |
725 | * using it, for instance lttng-relayd, which can start in | |
726 | * parallel with this teardown. | |
727 | */ | |
728 | ||
729 | free(rundir); | |
4a15001e MD |
730 | } |
731 | ||
732 | /* | |
733 | * Cleanup the daemon's option data structures. | |
734 | */ | |
735 | static void sessiond_cleanup_options(void) | |
736 | { | |
737 | DBG("Cleaning up options"); | |
738 | ||
739 | /* | |
740 | * If the override option is set, the pointer points to a *non* const | |
741 | * thus freeing it even though the variable type is set to const. | |
742 | */ | |
743 | if (tracing_group_name_override) { | |
744 | free((void *) tracing_group_name); | |
745 | } | |
746 | if (consumerd32_bin_override) { | |
747 | free((void *) consumerd32_bin); | |
748 | } | |
749 | if (consumerd64_bin_override) { | |
750 | free((void *) consumerd64_bin); | |
751 | } | |
752 | if (consumerd32_libdir_override) { | |
753 | free((void *) consumerd32_libdir); | |
754 | } | |
755 | if (consumerd64_libdir_override) { | |
756 | free((void *) consumerd64_libdir); | |
757 | } | |
758 | ||
759 | free(opt_pidfile); | |
760 | free(opt_load_session_path); | |
761 | free(kmod_probes_list); | |
762 | free(kmod_extra_probes_list); | |
c9cb3e7d | 763 | |
421cb601 | 764 | /* <fun> */ |
f56a39af | 765 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
766 | "Matthew, BEET driven development works!%c[%dm", |
767 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
768 | /* </fun> */ | |
fac6795d DG |
769 | } |
770 | ||
e065084a | 771 | /* |
d063d709 | 772 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 773 | * |
d063d709 | 774 | * Return lttcomm error code. |
e065084a DG |
775 | */ |
776 | static int send_unix_sock(int sock, void *buf, size_t len) | |
777 | { | |
778 | /* Check valid length */ | |
c617c0c6 | 779 | if (len == 0) { |
e065084a DG |
780 | return -1; |
781 | } | |
782 | ||
783 | return lttcomm_send_unix_sock(sock, buf, len); | |
784 | } | |
785 | ||
5461b305 | 786 | /* |
d063d709 | 787 | * Free memory of a command context structure. |
5461b305 | 788 | */ |
a2fb29a5 | 789 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 790 | { |
a2fb29a5 DG |
791 | DBG("Clean command context structure"); |
792 | if (*cmd_ctx) { | |
793 | if ((*cmd_ctx)->llm) { | |
794 | free((*cmd_ctx)->llm); | |
5461b305 | 795 | } |
a2fb29a5 DG |
796 | if ((*cmd_ctx)->lsm) { |
797 | free((*cmd_ctx)->lsm); | |
5461b305 | 798 | } |
a2fb29a5 DG |
799 | free(*cmd_ctx); |
800 | *cmd_ctx = NULL; | |
5461b305 DG |
801 | } |
802 | } | |
803 | ||
fac6795d | 804 | /* |
0fdd1e2c | 805 | * Notify UST applications using the shm mmap futex. |
fac6795d | 806 | */ |
0fdd1e2c | 807 | static int notify_ust_apps(int active) |
fac6795d | 808 | { |
0fdd1e2c | 809 | char *wait_shm_mmap; |
fac6795d | 810 | |
0fdd1e2c | 811 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 812 | |
0fdd1e2c DG |
813 | /* See shm.c for this call implying mmap, shm and futex calls */ |
814 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
815 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
816 | goto error; |
817 | } | |
818 | ||
0fdd1e2c DG |
819 | /* Wake waiting process */ |
820 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
821 | ||
822 | /* Apps notified successfully */ | |
823 | return 0; | |
fac6795d DG |
824 | |
825 | error: | |
0fdd1e2c | 826 | return -1; |
fac6795d DG |
827 | } |
828 | ||
e065084a | 829 | /* |
d063d709 DG |
830 | * Setup the outgoing data buffer for the response (llm) by allocating the |
831 | * right amount of memory and copying the original information from the lsm | |
832 | * structure. | |
ca95a216 | 833 | * |
d063d709 | 834 | * Return total size of the buffer pointed by buf. |
ca95a216 | 835 | */ |
5461b305 | 836 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 837 | { |
f3ed775e | 838 | int ret, buf_size; |
ca95a216 | 839 | |
f3ed775e | 840 | buf_size = size; |
5461b305 | 841 | |
ba7f0ae5 | 842 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 843 | if (cmd_ctx->llm == NULL) { |
76d7553f | 844 | PERROR("zmalloc"); |
5461b305 | 845 | ret = -ENOMEM; |
ca95a216 DG |
846 | goto error; |
847 | } | |
848 | ||
5461b305 DG |
849 | /* Copy common data */ |
850 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 851 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 852 | |
5461b305 DG |
853 | cmd_ctx->llm->data_size = size; |
854 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
855 | ||
ca95a216 DG |
856 | return buf_size; |
857 | ||
858 | error: | |
859 | return ret; | |
860 | } | |
861 | ||
7a485870 | 862 | /* |
5eb91c98 | 863 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 864 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 865 | */ |
5eb91c98 | 866 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 867 | { |
5eb91c98 | 868 | int ret; |
7a485870 DG |
869 | struct ltt_session *session; |
870 | struct ltt_kernel_channel *channel; | |
871 | ||
5eb91c98 | 872 | DBG("Updating kernel poll set"); |
7a485870 | 873 | |
54d01ffb | 874 | session_lock_list(); |
b5541356 | 875 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 876 | session_lock(session); |
7a485870 | 877 | if (session->kernel_session == NULL) { |
54d01ffb | 878 | session_unlock(session); |
7a485870 DG |
879 | continue; |
880 | } | |
7a485870 | 881 | |
54d01ffb DG |
882 | cds_list_for_each_entry(channel, |
883 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
884 | /* Add channel fd to the kernel poll set */ |
885 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
886 | if (ret < 0) { | |
54d01ffb | 887 | session_unlock(session); |
5eb91c98 DG |
888 | goto error; |
889 | } | |
890 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 891 | } |
54d01ffb | 892 | session_unlock(session); |
7a485870 | 893 | } |
54d01ffb | 894 | session_unlock_list(); |
7a485870 | 895 | |
5eb91c98 | 896 | return 0; |
7a485870 DG |
897 | |
898 | error: | |
54d01ffb | 899 | session_unlock_list(); |
7a485870 DG |
900 | return -1; |
901 | } | |
902 | ||
903 | /* | |
54d01ffb | 904 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 905 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 906 | * |
d063d709 | 907 | * Useful for CPU hotplug feature. |
7a485870 | 908 | */ |
2bdd86d4 | 909 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
910 | { |
911 | int ret = 0; | |
912 | struct ltt_session *session; | |
173af62f | 913 | struct ltt_kernel_session *ksess; |
7a485870 DG |
914 | struct ltt_kernel_channel *channel; |
915 | ||
916 | DBG("Updating kernel streams for channel fd %d", fd); | |
917 | ||
54d01ffb | 918 | session_lock_list(); |
b5541356 | 919 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 920 | session_lock(session); |
7a485870 | 921 | if (session->kernel_session == NULL) { |
54d01ffb | 922 | session_unlock(session); |
7a485870 DG |
923 | continue; |
924 | } | |
173af62f | 925 | ksess = session->kernel_session; |
d9800920 | 926 | |
4a15001e MD |
927 | cds_list_for_each_entry(channel, |
928 | &ksess->channel_list.head, list) { | |
929 | struct lttng_ht_iter iter; | |
930 | struct consumer_socket *socket; | |
d9800920 | 931 | |
4a15001e MD |
932 | if (channel->fd != fd) { |
933 | continue; | |
934 | } | |
935 | DBG("Channel found, updating kernel streams"); | |
936 | ret = kernel_open_channel_stream(channel); | |
937 | if (ret < 0) { | |
938 | goto error; | |
939 | } | |
940 | /* Update the stream global counter */ | |
941 | ksess->stream_count_global += ret; | |
942 | ||
943 | /* | |
944 | * Have we already sent fds to the consumer? If yes, it | |
945 | * means that tracing is started so it is safe to send | |
946 | * our updated stream fds. | |
947 | */ | |
948 | if (ksess->consumer_fds_sent != 1 | |
949 | || ksess->consumer == NULL) { | |
950 | ret = -1; | |
951 | goto error; | |
952 | } | |
953 | ||
954 | rcu_read_lock(); | |
955 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, | |
956 | &iter.iter, socket, node.node) { | |
957 | pthread_mutex_lock(socket->lock); | |
958 | ret = kernel_consumer_send_channel_stream(socket, | |
959 | channel, ksess, | |
960 | session->output_traces ? 1 : 0); | |
961 | pthread_mutex_unlock(socket->lock); | |
962 | if (ret < 0) { | |
e7fe706f | 963 | rcu_read_unlock(); |
4a15001e | 964 | goto error; |
7a485870 | 965 | } |
7a485870 | 966 | } |
4a15001e | 967 | rcu_read_unlock(); |
7a485870 | 968 | } |
54d01ffb | 969 | session_unlock(session); |
7a485870 | 970 | } |
54d01ffb | 971 | session_unlock_list(); |
b3c750d2 | 972 | return ret; |
7a485870 | 973 | |
b3c750d2 | 974 | error: |
54d01ffb DG |
975 | session_unlock(session); |
976 | session_unlock_list(); | |
7a485870 DG |
977 | return ret; |
978 | } | |
979 | ||
487cf67c | 980 | /* |
ffe60014 DG |
981 | * For each tracing session, update newly registered apps. The session list |
982 | * lock MUST be acquired before calling this. | |
487cf67c DG |
983 | */ |
984 | static void update_ust_app(int app_sock) | |
985 | { | |
986 | struct ltt_session *sess, *stmp; | |
987 | ||
fdadac08 DG |
988 | /* Consumer is in an ERROR state. Stop any application update. */ |
989 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
990 | /* Stop the update process since the consumer is dead. */ | |
991 | return; | |
992 | } | |
993 | ||
487cf67c DG |
994 | /* For all tracing session(s) */ |
995 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 996 | session_lock(sess); |
421cb601 DG |
997 | if (sess->ust_session) { |
998 | ust_app_global_update(sess->ust_session, app_sock); | |
999 | } | |
4ee14516 | 1000 | session_unlock(sess); |
487cf67c DG |
1001 | } |
1002 | } | |
1003 | ||
7a485870 | 1004 | /* |
d063d709 | 1005 | * This thread manage event coming from the kernel. |
7a485870 | 1006 | * |
d063d709 DG |
1007 | * Features supported in this thread: |
1008 | * -) CPU Hotplug | |
7a485870 DG |
1009 | */ |
1010 | static void *thread_manage_kernel(void *data) | |
1011 | { | |
139ac872 | 1012 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 1013 | uint32_t revents, nb_fd; |
7a485870 | 1014 | char tmp; |
5eb91c98 | 1015 | struct lttng_poll_event events; |
7a485870 | 1016 | |
6993eeb3 | 1017 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 1018 | |
6c71277b | 1019 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 1020 | |
d5d63bf1 DG |
1021 | /* |
1022 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 1023 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 1024 | */ |
6d737ce4 | 1025 | lttng_poll_init(&events); |
d5d63bf1 | 1026 | |
e547b070 | 1027 | if (testpoint(sessiond_thread_manage_kernel)) { |
6993eeb3 CB |
1028 | goto error_testpoint; |
1029 | } | |
8ac94142 | 1030 | |
840cb59c | 1031 | health_code_update(); |
44a5e5eb | 1032 | |
e547b070 | 1033 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
d21b0d71 | 1034 | goto error_testpoint; |
6993eeb3 CB |
1035 | } |
1036 | ||
7a485870 | 1037 | while (1) { |
840cb59c | 1038 | health_code_update(); |
44a5e5eb | 1039 | |
7a485870 | 1040 | if (update_poll_flag == 1) { |
d21b0d71 DG |
1041 | /* Clean events object. We are about to populate it again. */ |
1042 | lttng_poll_clean(&events); | |
1043 | ||
d0b96690 | 1044 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
1045 | if (ret < 0) { |
1046 | goto error_poll_create; | |
1047 | } | |
1048 | ||
1049 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
1050 | if (ret < 0) { | |
1051 | goto error; | |
1052 | } | |
5f822d0a | 1053 | |
d21b0d71 | 1054 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
1055 | ret = update_kernel_poll(&events); |
1056 | if (ret < 0) { | |
7a485870 DG |
1057 | goto error; |
1058 | } | |
1059 | update_poll_flag = 0; | |
1060 | } | |
1061 | ||
7fa2082e | 1062 | DBG("Thread kernel polling"); |
7a485870 DG |
1063 | |
1064 | /* Poll infinite value of time */ | |
88f2b785 | 1065 | restart: |
a78af745 | 1066 | health_poll_entry(); |
5eb91c98 | 1067 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1068 | DBG("Thread kernel return from poll on %d fds", |
1069 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1070 | health_poll_exit(); |
7a485870 | 1071 | if (ret < 0) { |
88f2b785 MD |
1072 | /* |
1073 | * Restart interrupted system call. | |
1074 | */ | |
1075 | if (errno == EINTR) { | |
1076 | goto restart; | |
1077 | } | |
7a485870 DG |
1078 | goto error; |
1079 | } else if (ret == 0) { | |
1080 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
1081 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
1082 | "This should not have happened! Continuing..."); | |
7a485870 DG |
1083 | continue; |
1084 | } | |
1085 | ||
0d9c5d77 DG |
1086 | nb_fd = ret; |
1087 | ||
5eb91c98 DG |
1088 | for (i = 0; i < nb_fd; i++) { |
1089 | /* Fetch once the poll data */ | |
1090 | revents = LTTNG_POLL_GETEV(&events, i); | |
1091 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 1092 | |
840cb59c | 1093 | health_code_update(); |
44a5e5eb | 1094 | |
fd20dac9 MD |
1095 | if (!revents) { |
1096 | /* No activity for this FD (poll implementation). */ | |
1097 | continue; | |
1098 | } | |
1099 | ||
5eb91c98 | 1100 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1101 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1102 | if (ret) { |
139ac872 MD |
1103 | err = 0; |
1104 | goto exit; | |
5eb91c98 | 1105 | } |
7a485870 | 1106 | |
5eb91c98 DG |
1107 | /* Check for data on kernel pipe */ |
1108 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
6cd525e8 MD |
1109 | (void) lttng_read(kernel_poll_pipe[0], |
1110 | &tmp, 1); | |
f921c78f DG |
1111 | /* |
1112 | * Ret value is useless here, if this pipe gets any actions an | |
1113 | * update is required anyway. | |
1114 | */ | |
5eb91c98 DG |
1115 | update_poll_flag = 1; |
1116 | continue; | |
1117 | } else { | |
1118 | /* | |
1119 | * New CPU detected by the kernel. Adding kernel stream to | |
1120 | * kernel session and updating the kernel consumer | |
1121 | */ | |
1122 | if (revents & LPOLLIN) { | |
2bdd86d4 | 1123 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
1124 | if (ret < 0) { |
1125 | continue; | |
1126 | } | |
1127 | break; | |
1128 | /* | |
1129 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
1130 | * and unregister kernel stream at this point. | |
1131 | */ | |
7a485870 | 1132 | } |
7a485870 DG |
1133 | } |
1134 | } | |
1135 | } | |
1136 | ||
139ac872 | 1137 | exit: |
7a485870 | 1138 | error: |
5eb91c98 | 1139 | lttng_poll_clean(&events); |
76d7553f | 1140 | error_poll_create: |
6993eeb3 | 1141 | error_testpoint: |
6620da75 DG |
1142 | utils_close_pipe(kernel_poll_pipe); |
1143 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 1144 | if (err) { |
840cb59c | 1145 | health_error(); |
139ac872 | 1146 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
1147 | WARN("Kernel thread died unexpectedly. " |
1148 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 1149 | } |
8782cc74 | 1150 | health_unregister(health_sessiond); |
76d7553f | 1151 | DBG("Kernel thread dying"); |
7a485870 DG |
1152 | return NULL; |
1153 | } | |
1154 | ||
a23ec3a7 DG |
1155 | /* |
1156 | * Signal pthread condition of the consumer data that the thread. | |
1157 | */ | |
1158 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
1159 | { | |
1160 | pthread_mutex_lock(&data->cond_mutex); | |
1161 | ||
1162 | /* | |
1163 | * The state is set before signaling. It can be any value, it's the waiter | |
1164 | * job to correctly interpret this condition variable associated to the | |
1165 | * consumer pthread_cond. | |
1166 | * | |
1167 | * A value of 0 means that the corresponding thread of the consumer data | |
1168 | * was not started. 1 indicates that the thread has started and is ready | |
1169 | * for action. A negative value means that there was an error during the | |
1170 | * thread bootstrap. | |
1171 | */ | |
1172 | data->consumer_thread_is_ready = state; | |
1173 | (void) pthread_cond_signal(&data->cond); | |
1174 | ||
1175 | pthread_mutex_unlock(&data->cond_mutex); | |
1176 | } | |
1177 | ||
1d4b027a | 1178 | /* |
3bd1e081 | 1179 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 1180 | */ |
3bd1e081 | 1181 | static void *thread_manage_consumer(void *data) |
1d4b027a | 1182 | { |
42fc1d0b | 1183 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 1184 | uint32_t revents, nb_fd; |
1d4b027a | 1185 | enum lttcomm_return_code code; |
5eb91c98 | 1186 | struct lttng_poll_event events; |
3bd1e081 | 1187 | struct consumer_data *consumer_data = data; |
1d4b027a | 1188 | |
3bd1e081 | 1189 | DBG("[thread] Manage consumer started"); |
1d4b027a | 1190 | |
6c71277b | 1191 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 1192 | |
855060f8 | 1193 | health_code_update(); |
9449cc75 | 1194 | |
5eb91c98 | 1195 | /* |
331744e3 JD |
1196 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
1197 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 1198 | */ |
331744e3 | 1199 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 1200 | if (ret < 0) { |
76d7553f | 1201 | goto error_poll; |
5eb91c98 | 1202 | } |
273ea72c | 1203 | |
edb8b045 DG |
1204 | /* |
1205 | * The error socket here is already in a listening state which was done | |
1206 | * just before spawning this thread to avoid a race between the consumer | |
1207 | * daemon exec trying to connect and the listen() call. | |
1208 | */ | |
3bd1e081 | 1209 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
1210 | if (ret < 0) { |
1211 | goto error; | |
1212 | } | |
1213 | ||
840cb59c | 1214 | health_code_update(); |
44a5e5eb | 1215 | |
331744e3 | 1216 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1217 | restart: |
a78af745 | 1218 | health_poll_entry(); |
8ac94142 | 1219 | |
e547b070 | 1220 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
1221 | goto error; |
1222 | } | |
8ac94142 | 1223 | |
5eb91c98 | 1224 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1225 | health_poll_exit(); |
273ea72c | 1226 | if (ret < 0) { |
88f2b785 MD |
1227 | /* |
1228 | * Restart interrupted system call. | |
1229 | */ | |
1230 | if (errno == EINTR) { | |
1231 | goto restart; | |
1232 | } | |
273ea72c DG |
1233 | goto error; |
1234 | } | |
1235 | ||
0d9c5d77 DG |
1236 | nb_fd = ret; |
1237 | ||
5eb91c98 DG |
1238 | for (i = 0; i < nb_fd; i++) { |
1239 | /* Fetch once the poll data */ | |
1240 | revents = LTTNG_POLL_GETEV(&events, i); | |
1241 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1242 | ||
840cb59c | 1243 | health_code_update(); |
44a5e5eb | 1244 | |
fd20dac9 MD |
1245 | if (!revents) { |
1246 | /* No activity for this FD (poll implementation). */ | |
1247 | continue; | |
1248 | } | |
1249 | ||
5eb91c98 | 1250 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1251 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1252 | if (ret) { |
139ac872 MD |
1253 | err = 0; |
1254 | goto exit; | |
5eb91c98 DG |
1255 | } |
1256 | ||
1257 | /* Event on the registration socket */ | |
3bd1e081 | 1258 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 1259 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 1260 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
1261 | goto error; |
1262 | } | |
1263 | } | |
273ea72c DG |
1264 | } |
1265 | ||
3bd1e081 | 1266 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
1267 | if (sock < 0) { |
1268 | goto error; | |
1269 | } | |
1270 | ||
b662582b DG |
1271 | /* |
1272 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1273 | * show must go on. | |
1274 | */ | |
1275 | (void) utils_set_fd_cloexec(sock); | |
1276 | ||
840cb59c | 1277 | health_code_update(); |
44a5e5eb | 1278 | |
3bd1e081 | 1279 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 1280 | |
712ea556 | 1281 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
1282 | ret = lttcomm_recv_unix_sock(sock, &code, |
1283 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
1284 | if (ret <= 0) { |
1285 | goto error; | |
1286 | } | |
1287 | ||
840cb59c | 1288 | health_code_update(); |
f73fabfd | 1289 | if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
331744e3 | 1290 | /* Connect both socket, command and metadata. */ |
3bd1e081 MD |
1291 | consumer_data->cmd_sock = |
1292 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
4ce514c4 | 1293 | consumer_data->metadata_fd = |
331744e3 | 1294 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
92db7cdc DG |
1295 | if (consumer_data->cmd_sock < 0 |
1296 | || consumer_data->metadata_fd < 0) { | |
331744e3 | 1297 | PERROR("consumer connect cmd socket"); |
a23ec3a7 DG |
1298 | /* On error, signal condition and quit. */ |
1299 | signal_consumer_condition(consumer_data, -1); | |
1d4b027a DG |
1300 | goto error; |
1301 | } | |
9363801e | 1302 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; |
331744e3 JD |
1303 | /* Create metadata socket lock. */ |
1304 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
1305 | if (consumer_data->metadata_sock.lock == NULL) { | |
1306 | PERROR("zmalloc pthread mutex"); | |
1307 | ret = -1; | |
1308 | goto error; | |
1309 | } | |
1310 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
1311 | ||
a23ec3a7 | 1312 | signal_consumer_condition(consumer_data, 1); |
331744e3 JD |
1313 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); |
1314 | DBG("Consumer metadata socket ready (fd: %d)", | |
4ce514c4 | 1315 | consumer_data->metadata_fd); |
1d4b027a | 1316 | } else { |
3bd1e081 | 1317 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1318 | lttcomm_get_readable_code(-code)); |
1319 | goto error; | |
1320 | } | |
1321 | ||
331744e3 | 1322 | /* Remove the consumerd error sock since we've established a connexion */ |
3bd1e081 | 1323 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1324 | if (ret < 0) { |
72079cae DG |
1325 | goto error; |
1326 | } | |
1327 | ||
331744e3 | 1328 | /* Add new accepted error socket. */ |
5eb91c98 DG |
1329 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1330 | if (ret < 0) { | |
72079cae | 1331 | goto error; |
5eb91c98 DG |
1332 | } |
1333 | ||
331744e3 | 1334 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 1335 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
1336 | LPOLLIN | LPOLLRDHUP); |
1337 | if (ret < 0) { | |
1338 | goto error; | |
1339 | } | |
1340 | ||
840cb59c | 1341 | health_code_update(); |
44a5e5eb | 1342 | |
331744e3 | 1343 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1344 | restart_poll: |
331744e3 | 1345 | while (1) { |
42fc1d0b DG |
1346 | health_code_update(); |
1347 | ||
1348 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
1349 | if (should_quit) { | |
1350 | /* Not a health error. */ | |
1351 | err = 0; | |
1352 | goto exit; | |
1353 | } | |
1354 | ||
331744e3 JD |
1355 | health_poll_entry(); |
1356 | ret = lttng_poll_wait(&events, -1); | |
1357 | health_poll_exit(); | |
1358 | if (ret < 0) { | |
1359 | /* | |
1360 | * Restart interrupted system call. | |
1361 | */ | |
1362 | if (errno == EINTR) { | |
1363 | goto restart_poll; | |
1364 | } | |
1365 | goto error; | |
88f2b785 | 1366 | } |
72079cae | 1367 | |
331744e3 | 1368 | nb_fd = ret; |
0d9c5d77 | 1369 | |
331744e3 JD |
1370 | for (i = 0; i < nb_fd; i++) { |
1371 | /* Fetch once the poll data */ | |
1372 | revents = LTTNG_POLL_GETEV(&events, i); | |
1373 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 1374 | |
331744e3 | 1375 | health_code_update(); |
44a5e5eb | 1376 | |
fd20dac9 MD |
1377 | if (!revents) { |
1378 | /* No activity for this FD (poll implementation). */ | |
1379 | continue; | |
1380 | } | |
1381 | ||
42fc1d0b DG |
1382 | /* |
1383 | * Thread quit pipe has been triggered, flag that we should stop | |
1384 | * but continue the current loop to handle potential data from | |
1385 | * consumer. | |
1386 | */ | |
1387 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 1388 | |
331744e3 JD |
1389 | if (pollfd == sock) { |
1390 | /* Event on the consumerd socket */ | |
1391 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1392 | ERR("consumer err socket second poll error"); | |
1393 | goto error; | |
1394 | } | |
1395 | health_code_update(); | |
1396 | /* Wait for any kconsumerd error */ | |
1397 | ret = lttcomm_recv_unix_sock(sock, &code, | |
1398 | sizeof(enum lttcomm_return_code)); | |
1399 | if (ret <= 0) { | |
1400 | ERR("consumer closed the command socket"); | |
1401 | goto error; | |
1402 | } | |
1403 | ||
1404 | ERR("consumer return code : %s", | |
1405 | lttcomm_get_readable_code(-code)); | |
1406 | ||
1407 | goto exit; | |
4ce514c4 | 1408 | } else if (pollfd == consumer_data->metadata_fd) { |
331744e3 JD |
1409 | /* UST metadata requests */ |
1410 | ret = ust_consumer_metadata_request( | |
1411 | &consumer_data->metadata_sock); | |
1412 | if (ret < 0) { | |
1413 | ERR("Handling metadata request"); | |
1414 | goto error; | |
1415 | } | |
5eb91c98 | 1416 | } |
42fc1d0b | 1417 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 1418 | } |
331744e3 | 1419 | health_code_update(); |
5eb91c98 DG |
1420 | } |
1421 | ||
139ac872 | 1422 | exit: |
1d4b027a | 1423 | error: |
fdadac08 DG |
1424 | /* |
1425 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1426 | * thread might be using them so get exclusive access which will abort all |
1427 | * other consumer command by other threads. | |
fdadac08 DG |
1428 | */ |
1429 | pthread_mutex_lock(&consumer_data->lock); | |
1430 | ||
5c827ce0 DG |
1431 | /* Immediately set the consumerd state to stopped */ |
1432 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1433 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1434 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1435 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1436 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1437 | } else { | |
1438 | /* Code flow error... */ | |
1439 | assert(0); | |
1440 | } | |
1441 | ||
76d7553f MD |
1442 | if (consumer_data->err_sock >= 0) { |
1443 | ret = close(consumer_data->err_sock); | |
1444 | if (ret) { | |
1445 | PERROR("close"); | |
1446 | } | |
a76cbd9f | 1447 | consumer_data->err_sock = -1; |
76d7553f MD |
1448 | } |
1449 | if (consumer_data->cmd_sock >= 0) { | |
1450 | ret = close(consumer_data->cmd_sock); | |
1451 | if (ret) { | |
1452 | PERROR("close"); | |
1453 | } | |
a76cbd9f | 1454 | consumer_data->cmd_sock = -1; |
76d7553f | 1455 | } |
96544455 SS |
1456 | if (consumer_data->metadata_sock.fd_ptr && |
1457 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 1458 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
1459 | if (ret) { |
1460 | PERROR("close"); | |
1461 | } | |
1462 | } | |
76d7553f MD |
1463 | if (sock >= 0) { |
1464 | ret = close(sock); | |
1465 | if (ret) { | |
1466 | PERROR("close"); | |
1467 | } | |
1468 | } | |
273ea72c | 1469 | |
3bd1e081 MD |
1470 | unlink(consumer_data->err_unix_sock_path); |
1471 | unlink(consumer_data->cmd_unix_sock_path); | |
1472 | consumer_data->pid = 0; | |
fdadac08 | 1473 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1474 | |
fdadac08 | 1475 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
1476 | if (consumer_data->metadata_sock.lock) { |
1477 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1478 | free(consumer_data->metadata_sock.lock); | |
1479 | } | |
5eb91c98 | 1480 | lttng_poll_clean(&events); |
76d7553f | 1481 | error_poll: |
139ac872 | 1482 | if (err) { |
840cb59c | 1483 | health_error(); |
139ac872 MD |
1484 | ERR("Health error occurred in %s", __func__); |
1485 | } | |
8782cc74 | 1486 | health_unregister(health_sessiond); |
76d7553f | 1487 | DBG("consumer thread cleanup completed"); |
0177d773 | 1488 | |
5eb91c98 | 1489 | return NULL; |
099e26bd DG |
1490 | } |
1491 | ||
099e26bd DG |
1492 | /* |
1493 | * This thread manage application communication. | |
1d4b027a DG |
1494 | */ |
1495 | static void *thread_manage_apps(void *data) | |
099e26bd | 1496 | { |
139ac872 | 1497 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1498 | ssize_t size_ret; |
5eb91c98 | 1499 | uint32_t revents, nb_fd; |
5eb91c98 | 1500 | struct lttng_poll_event events; |
099e26bd DG |
1501 | |
1502 | DBG("[thread] Manage application started"); | |
1503 | ||
f6a9efaa DG |
1504 | rcu_register_thread(); |
1505 | rcu_thread_online(); | |
1506 | ||
6c71277b | 1507 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1508 | |
e547b070 | 1509 | if (testpoint(sessiond_thread_manage_apps)) { |
6993eeb3 CB |
1510 | goto error_testpoint; |
1511 | } | |
1512 | ||
840cb59c | 1513 | health_code_update(); |
44a5e5eb | 1514 | |
d0b96690 | 1515 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1516 | if (ret < 0) { |
76d7553f | 1517 | goto error_poll_create; |
5eb91c98 | 1518 | } |
099e26bd | 1519 | |
5eb91c98 DG |
1520 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1521 | if (ret < 0) { | |
1522 | goto error; | |
1523 | } | |
099e26bd | 1524 | |
e547b070 | 1525 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
6993eeb3 CB |
1526 | goto error; |
1527 | } | |
8ac94142 | 1528 | |
840cb59c | 1529 | health_code_update(); |
44a5e5eb | 1530 | |
5eb91c98 | 1531 | while (1) { |
7fa2082e | 1532 | DBG("Apps thread polling"); |
099e26bd DG |
1533 | |
1534 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1535 | restart: |
a78af745 | 1536 | health_poll_entry(); |
5eb91c98 | 1537 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1538 | DBG("Apps thread return from poll on %d fds", |
1539 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1540 | health_poll_exit(); |
099e26bd | 1541 | if (ret < 0) { |
88f2b785 MD |
1542 | /* |
1543 | * Restart interrupted system call. | |
1544 | */ | |
1545 | if (errno == EINTR) { | |
1546 | goto restart; | |
1547 | } | |
099e26bd DG |
1548 | goto error; |
1549 | } | |
1550 | ||
0d9c5d77 DG |
1551 | nb_fd = ret; |
1552 | ||
5eb91c98 DG |
1553 | for (i = 0; i < nb_fd; i++) { |
1554 | /* Fetch once the poll data */ | |
1555 | revents = LTTNG_POLL_GETEV(&events, i); | |
1556 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1557 | ||
840cb59c | 1558 | health_code_update(); |
44a5e5eb | 1559 | |
fd20dac9 MD |
1560 | if (!revents) { |
1561 | /* No activity for this FD (poll implementation). */ | |
1562 | continue; | |
1563 | } | |
1564 | ||
5eb91c98 | 1565 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1566 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1567 | if (ret) { |
139ac872 MD |
1568 | err = 0; |
1569 | goto exit; | |
5eb91c98 | 1570 | } |
099e26bd | 1571 | |
5eb91c98 DG |
1572 | /* Inspect the apps cmd pipe */ |
1573 | if (pollfd == apps_cmd_pipe[0]) { | |
1574 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1575 | ERR("Apps command pipe error"); | |
0177d773 | 1576 | goto error; |
5eb91c98 | 1577 | } else if (revents & LPOLLIN) { |
d0b96690 DG |
1578 | int sock; |
1579 | ||
5eb91c98 | 1580 | /* Empty pipe */ |
6cd525e8 MD |
1581 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1582 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1583 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1584 | goto error; |
1585 | } | |
099e26bd | 1586 | |
840cb59c | 1587 | health_code_update(); |
44a5e5eb | 1588 | |
ffe60014 | 1589 | /* |
d0b96690 DG |
1590 | * We only monitor the error events of the socket. This |
1591 | * thread does not handle any incoming data from UST | |
1592 | * (POLLIN). | |
ffe60014 | 1593 | */ |
d0b96690 DG |
1594 | ret = lttng_poll_add(&events, sock, |
1595 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1596 | if (ret < 0) { | |
5eb91c98 | 1597 | goto error; |
e0c7ec2b | 1598 | } |
acc7b41b | 1599 | |
d0b96690 | 1600 | DBG("Apps with sock %d added to poll set", sock); |
0177d773 | 1601 | } |
5eb91c98 DG |
1602 | } else { |
1603 | /* | |
54d01ffb DG |
1604 | * At this point, we know that a registered application made |
1605 | * the event at poll_wait. | |
5eb91c98 DG |
1606 | */ |
1607 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1608 | /* Removing from the poll set */ | |
1609 | ret = lttng_poll_del(&events, pollfd); | |
1610 | if (ret < 0) { | |
1611 | goto error; | |
1612 | } | |
099e26bd | 1613 | |
b9d9b220 | 1614 | /* Socket closed on remote end. */ |
56fff090 | 1615 | ust_app_unregister(pollfd); |
5eb91c98 | 1616 | } |
099e26bd | 1617 | } |
44a5e5eb | 1618 | |
840cb59c | 1619 | health_code_update(); |
099e26bd | 1620 | } |
099e26bd DG |
1621 | } |
1622 | ||
139ac872 | 1623 | exit: |
099e26bd | 1624 | error: |
5eb91c98 | 1625 | lttng_poll_clean(&events); |
76d7553f | 1626 | error_poll_create: |
6993eeb3 | 1627 | error_testpoint: |
6620da75 DG |
1628 | utils_close_pipe(apps_cmd_pipe); |
1629 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1630 | ||
1631 | /* | |
1632 | * We don't clean the UST app hash table here since already registered | |
1633 | * applications can still be controlled so let them be until the session | |
1634 | * daemon dies or the applications stop. | |
1635 | */ | |
1636 | ||
139ac872 | 1637 | if (err) { |
840cb59c | 1638 | health_error(); |
139ac872 MD |
1639 | ERR("Health error occurred in %s", __func__); |
1640 | } | |
8782cc74 | 1641 | health_unregister(health_sessiond); |
76d7553f | 1642 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1643 | rcu_thread_offline(); |
1644 | rcu_unregister_thread(); | |
099e26bd DG |
1645 | return NULL; |
1646 | } | |
1647 | ||
d0b96690 | 1648 | /* |
d88aee68 DG |
1649 | * Send a socket to a thread This is called from the dispatch UST registration |
1650 | * thread once all sockets are set for the application. | |
d0b96690 | 1651 | * |
b85dc84c DG |
1652 | * The sock value can be invalid, we don't really care, the thread will handle |
1653 | * it and make the necessary cleanup if so. | |
1654 | * | |
d0b96690 DG |
1655 | * On success, return 0 else a negative value being the errno message of the |
1656 | * write(). | |
1657 | */ | |
d88aee68 | 1658 | static int send_socket_to_thread(int fd, int sock) |
d0b96690 | 1659 | { |
6cd525e8 | 1660 | ssize_t ret; |
d0b96690 | 1661 | |
b85dc84c DG |
1662 | /* |
1663 | * It's possible that the FD is set as invalid with -1 concurrently just | |
1664 | * before calling this function being a shutdown state of the thread. | |
1665 | */ | |
1666 | if (fd < 0) { | |
1667 | ret = -EBADF; | |
1668 | goto error; | |
1669 | } | |
d0b96690 | 1670 | |
6cd525e8 MD |
1671 | ret = lttng_write(fd, &sock, sizeof(sock)); |
1672 | if (ret < sizeof(sock)) { | |
d88aee68 | 1673 | PERROR("write apps pipe %d", fd); |
d0b96690 DG |
1674 | if (ret < 0) { |
1675 | ret = -errno; | |
1676 | } | |
1677 | goto error; | |
1678 | } | |
1679 | ||
1680 | /* All good. Don't send back the write positive ret value. */ | |
1681 | ret = 0; | |
1682 | error: | |
6cd525e8 | 1683 | return (int) ret; |
d0b96690 DG |
1684 | } |
1685 | ||
f45e313d DG |
1686 | /* |
1687 | * Sanitize the wait queue of the dispatch registration thread meaning removing | |
1688 | * invalid nodes from it. This is to avoid memory leaks for the case the UST | |
1689 | * notify socket is never received. | |
1690 | */ | |
1691 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) | |
1692 | { | |
1693 | int ret, nb_fd = 0, i; | |
1694 | unsigned int fd_added = 0; | |
1695 | struct lttng_poll_event events; | |
1696 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; | |
1697 | ||
1698 | assert(wait_queue); | |
1699 | ||
1700 | lttng_poll_init(&events); | |
1701 | ||
1702 | /* Just skip everything for an empty queue. */ | |
1703 | if (!wait_queue->count) { | |
1704 | goto end; | |
1705 | } | |
1706 | ||
1707 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); | |
1708 | if (ret < 0) { | |
1709 | goto error_create; | |
1710 | } | |
1711 | ||
1712 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1713 | &wait_queue->head, head) { | |
1714 | assert(wait_node->app); | |
1715 | ret = lttng_poll_add(&events, wait_node->app->sock, | |
1716 | LPOLLHUP | LPOLLERR); | |
1717 | if (ret < 0) { | |
1718 | goto error; | |
1719 | } | |
1720 | ||
1721 | fd_added = 1; | |
1722 | } | |
1723 | ||
1724 | if (!fd_added) { | |
1725 | goto end; | |
1726 | } | |
1727 | ||
1728 | /* | |
1729 | * Poll but don't block so we can quickly identify the faulty events and | |
1730 | * clean them afterwards from the wait queue. | |
1731 | */ | |
1732 | ret = lttng_poll_wait(&events, 0); | |
1733 | if (ret < 0) { | |
1734 | goto error; | |
1735 | } | |
1736 | nb_fd = ret; | |
1737 | ||
1738 | for (i = 0; i < nb_fd; i++) { | |
1739 | /* Get faulty FD. */ | |
1740 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
1741 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
1742 | ||
fd20dac9 MD |
1743 | if (!revents) { |
1744 | /* No activity for this FD (poll implementation). */ | |
1745 | continue; | |
1746 | } | |
1747 | ||
f45e313d DG |
1748 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
1749 | &wait_queue->head, head) { | |
1750 | if (pollfd == wait_node->app->sock && | |
1751 | (revents & (LPOLLHUP | LPOLLERR))) { | |
1752 | cds_list_del(&wait_node->head); | |
1753 | wait_queue->count--; | |
1754 | ust_app_destroy(wait_node->app); | |
1755 | free(wait_node); | |
1756 | break; | |
1757 | } | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | if (nb_fd > 0) { | |
1762 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); | |
1763 | } | |
1764 | ||
1765 | end: | |
1766 | lttng_poll_clean(&events); | |
1767 | return; | |
1768 | ||
1769 | error: | |
1770 | lttng_poll_clean(&events); | |
1771 | error_create: | |
1772 | ERR("Unable to sanitize wait queue"); | |
1773 | return; | |
1774 | } | |
1775 | ||
099e26bd DG |
1776 | /* |
1777 | * Dispatch request from the registration threads to the application | |
1778 | * communication thread. | |
1779 | */ | |
1780 | static void *thread_dispatch_ust_registration(void *data) | |
1781 | { | |
12e2b881 | 1782 | int ret, err = -1; |
8bdee6e2 | 1783 | struct cds_wfcq_node *node; |
099e26bd | 1784 | struct ust_command *ust_cmd = NULL; |
f45e313d DG |
1785 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
1786 | struct ust_reg_wait_queue wait_queue = { | |
1787 | .count = 0, | |
1788 | }; | |
d0b96690 | 1789 | |
6c71277b | 1790 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
12e2b881 | 1791 | |
9ad42ec1 MD |
1792 | if (testpoint(sessiond_thread_app_reg_dispatch)) { |
1793 | goto error_testpoint; | |
1794 | } | |
1795 | ||
12e2b881 MD |
1796 | health_code_update(); |
1797 | ||
f45e313d | 1798 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
099e26bd DG |
1799 | |
1800 | DBG("[thread] Dispatch UST command started"); | |
1801 | ||
26c9d55e | 1802 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
12e2b881 MD |
1803 | health_code_update(); |
1804 | ||
099e26bd DG |
1805 | /* Atomically prepare the queue futex */ |
1806 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1807 | ||
1808 | do { | |
d0b96690 | 1809 | struct ust_app *app = NULL; |
7972aab2 | 1810 | ust_cmd = NULL; |
d0b96690 | 1811 | |
f45e313d DG |
1812 | /* |
1813 | * Make sure we don't have node(s) that have hung up before receiving | |
1814 | * the notify socket. This is to clean the list in order to avoid | |
1815 | * memory leaks from notify socket that are never seen. | |
1816 | */ | |
1817 | sanitize_wait_queue(&wait_queue); | |
1818 | ||
12e2b881 | 1819 | health_code_update(); |
099e26bd | 1820 | /* Dequeue command for registration */ |
8bdee6e2 | 1821 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1822 | if (node == NULL) { |
00a17c97 | 1823 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1824 | /* Continue thread execution */ |
1825 | break; | |
1826 | } | |
1827 | ||
1828 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1829 | ||
2f50c8a3 DG |
1830 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1831 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1832 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1833 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1834 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1835 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1836 | |
1837 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1838 | wait_node = zmalloc(sizeof(*wait_node)); | |
1839 | if (!wait_node) { | |
1840 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1841 | ret = close(ust_cmd->sock); |
1842 | if (ret < 0) { | |
1843 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1844 | } | |
51dec90d | 1845 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1846 | free(ust_cmd); |
d0b96690 DG |
1847 | goto error; |
1848 | } | |
1849 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1850 | ||
1851 | /* Create application object if socket is CMD. */ | |
1852 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1853 | ust_cmd->sock); | |
1854 | if (!wait_node->app) { | |
1855 | ret = close(ust_cmd->sock); | |
1856 | if (ret < 0) { | |
1857 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1858 | } |
51dec90d | 1859 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1860 | free(wait_node); |
7972aab2 | 1861 | free(ust_cmd); |
d0b96690 DG |
1862 | continue; |
1863 | } | |
1864 | /* | |
1865 | * Add application to the wait queue so we can set the notify | |
1866 | * socket before putting this object in the global ht. | |
1867 | */ | |
f45e313d DG |
1868 | cds_list_add(&wait_node->head, &wait_queue.head); |
1869 | wait_queue.count++; | |
d0b96690 | 1870 | |
7972aab2 | 1871 | free(ust_cmd); |
d0b96690 DG |
1872 | /* |
1873 | * We have to continue here since we don't have the notify | |
1874 | * socket and the application MUST be added to the hash table | |
1875 | * only at that moment. | |
1876 | */ | |
1877 | continue; | |
1878 | } else { | |
1879 | /* | |
1880 | * Look for the application in the local wait queue and set the | |
1881 | * notify socket if found. | |
1882 | */ | |
d88aee68 | 1883 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1884 | &wait_queue.head, head) { |
12e2b881 | 1885 | health_code_update(); |
d0b96690 DG |
1886 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1887 | wait_node->app->notify_sock = ust_cmd->sock; | |
1888 | cds_list_del(&wait_node->head); | |
f45e313d | 1889 | wait_queue.count--; |
d0b96690 DG |
1890 | app = wait_node->app; |
1891 | free(wait_node); | |
1892 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1893 | break; | |
1894 | } | |
1895 | } | |
020d7f60 DG |
1896 | |
1897 | /* | |
1898 | * With no application at this stage the received socket is | |
1899 | * basically useless so close it before we free the cmd data | |
1900 | * structure for good. | |
1901 | */ | |
1902 | if (!app) { | |
1903 | ret = close(ust_cmd->sock); | |
1904 | if (ret < 0) { | |
1905 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1906 | } | |
51dec90d | 1907 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1908 | } |
7972aab2 | 1909 | free(ust_cmd); |
d0b96690 DG |
1910 | } |
1911 | ||
1912 | if (app) { | |
d0b96690 DG |
1913 | /* |
1914 | * @session_lock_list | |
1915 | * | |
1916 | * Lock the global session list so from the register up to the | |
1917 | * registration done message, no thread can see the application | |
1918 | * and change its state. | |
1919 | */ | |
1920 | session_lock_list(); | |
1921 | rcu_read_lock(); | |
d88aee68 | 1922 | |
d0b96690 DG |
1923 | /* |
1924 | * Add application to the global hash table. This needs to be | |
1925 | * done before the update to the UST registry can locate the | |
1926 | * application. | |
1927 | */ | |
1928 | ust_app_add(app); | |
d88aee68 DG |
1929 | |
1930 | /* Set app version. This call will print an error if needed. */ | |
1931 | (void) ust_app_version(app); | |
1932 | ||
1933 | /* Send notify socket through the notify pipe. */ | |
1934 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
1935 | app->notify_sock); | |
1936 | if (ret < 0) { | |
1937 | rcu_read_unlock(); | |
1938 | session_unlock_list(); | |
b85dc84c DG |
1939 | /* |
1940 | * No notify thread, stop the UST tracing. However, this is | |
1941 | * not an internal error of the this thread thus setting | |
1942 | * the health error code to a normal exit. | |
1943 | */ | |
1944 | err = 0; | |
d88aee68 | 1945 | goto error; |
6620da75 | 1946 | } |
d88aee68 | 1947 | |
d0b96690 DG |
1948 | /* |
1949 | * Update newly registered application with the tracing | |
1950 | * registry info already enabled information. | |
1951 | */ | |
1952 | update_ust_app(app->sock); | |
d88aee68 DG |
1953 | |
1954 | /* | |
1955 | * Don't care about return value. Let the manage apps threads | |
1956 | * handle app unregistration upon socket close. | |
1957 | */ | |
1958 | (void) ust_app_register_done(app->sock); | |
1959 | ||
1960 | /* | |
1961 | * Even if the application socket has been closed, send the app | |
1962 | * to the thread and unregistration will take place at that | |
1963 | * place. | |
1964 | */ | |
1965 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 1966 | if (ret < 0) { |
d88aee68 DG |
1967 | rcu_read_unlock(); |
1968 | session_unlock_list(); | |
b85dc84c DG |
1969 | /* |
1970 | * No apps. thread, stop the UST tracing. However, this is | |
1971 | * not an internal error of the this thread thus setting | |
1972 | * the health error code to a normal exit. | |
1973 | */ | |
1974 | err = 0; | |
d88aee68 | 1975 | goto error; |
d0b96690 | 1976 | } |
d88aee68 | 1977 | |
d0b96690 DG |
1978 | rcu_read_unlock(); |
1979 | session_unlock_list(); | |
099e26bd | 1980 | } |
099e26bd DG |
1981 | } while (node != NULL); |
1982 | ||
12e2b881 | 1983 | health_poll_entry(); |
099e26bd DG |
1984 | /* Futex wait on queue. Blocking call on futex() */ |
1985 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 1986 | health_poll_exit(); |
099e26bd | 1987 | } |
12e2b881 MD |
1988 | /* Normal exit, no error */ |
1989 | err = 0; | |
099e26bd DG |
1990 | |
1991 | error: | |
d88aee68 DG |
1992 | /* Clean up wait queue. */ |
1993 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 1994 | &wait_queue.head, head) { |
d88aee68 | 1995 | cds_list_del(&wait_node->head); |
f45e313d | 1996 | wait_queue.count--; |
d88aee68 DG |
1997 | free(wait_node); |
1998 | } | |
1999 | ||
9ad42ec1 | 2000 | error_testpoint: |
099e26bd | 2001 | DBG("Dispatch thread dying"); |
12e2b881 MD |
2002 | if (err) { |
2003 | health_error(); | |
2004 | ERR("Health error occurred in %s", __func__); | |
2005 | } | |
8782cc74 | 2006 | health_unregister(health_sessiond); |
099e26bd DG |
2007 | return NULL; |
2008 | } | |
2009 | ||
2010 | /* | |
2011 | * This thread manage application registration. | |
2012 | */ | |
2013 | static void *thread_registration_apps(void *data) | |
1d4b027a | 2014 | { |
139ac872 | 2015 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
2016 | uint32_t revents, nb_fd; |
2017 | struct lttng_poll_event events; | |
099e26bd DG |
2018 | /* |
2019 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
2020 | * freed in the manage apps thread. | |
2021 | */ | |
2022 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 2023 | |
099e26bd | 2024 | DBG("[thread] Manage application registration started"); |
1d4b027a | 2025 | |
6c71277b | 2026 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 2027 | |
e547b070 | 2028 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
2029 | goto error_testpoint; |
2030 | } | |
8ac94142 | 2031 | |
1d4b027a DG |
2032 | ret = lttcomm_listen_unix_sock(apps_sock); |
2033 | if (ret < 0) { | |
76d7553f | 2034 | goto error_listen; |
1d4b027a DG |
2035 | } |
2036 | ||
5eb91c98 DG |
2037 | /* |
2038 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
2039 | * more will be added to this poll set. | |
2040 | */ | |
d0b96690 | 2041 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 2042 | if (ret < 0) { |
76d7553f | 2043 | goto error_create_poll; |
5eb91c98 | 2044 | } |
273ea72c | 2045 | |
5eb91c98 DG |
2046 | /* Add the application registration socket */ |
2047 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
2048 | if (ret < 0) { | |
76d7553f | 2049 | goto error_poll_add; |
5eb91c98 | 2050 | } |
273ea72c | 2051 | |
1d4b027a | 2052 | /* Notify all applications to register */ |
0fdd1e2c DG |
2053 | ret = notify_ust_apps(1); |
2054 | if (ret < 0) { | |
2055 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
2056 | "Execution continues but there might be problem for already\n" |
2057 | "running applications that wishes to register."); | |
0fdd1e2c | 2058 | } |
1d4b027a DG |
2059 | |
2060 | while (1) { | |
2061 | DBG("Accepting application registration"); | |
273ea72c DG |
2062 | |
2063 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 2064 | restart: |
a78af745 | 2065 | health_poll_entry(); |
5eb91c98 | 2066 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 2067 | health_poll_exit(); |
273ea72c | 2068 | if (ret < 0) { |
88f2b785 MD |
2069 | /* |
2070 | * Restart interrupted system call. | |
2071 | */ | |
2072 | if (errno == EINTR) { | |
2073 | goto restart; | |
2074 | } | |
273ea72c DG |
2075 | goto error; |
2076 | } | |
2077 | ||
0d9c5d77 DG |
2078 | nb_fd = ret; |
2079 | ||
5eb91c98 | 2080 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 2081 | health_code_update(); |
139ac872 | 2082 | |
5eb91c98 DG |
2083 | /* Fetch once the poll data */ |
2084 | revents = LTTNG_POLL_GETEV(&events, i); | |
2085 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 2086 | |
fd20dac9 MD |
2087 | if (!revents) { |
2088 | /* No activity for this FD (poll implementation). */ | |
2089 | continue; | |
2090 | } | |
2091 | ||
5eb91c98 | 2092 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 2093 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 2094 | if (ret) { |
139ac872 MD |
2095 | err = 0; |
2096 | goto exit; | |
90014c57 | 2097 | } |
1d4b027a | 2098 | |
5eb91c98 DG |
2099 | /* Event on the registration socket */ |
2100 | if (pollfd == apps_sock) { | |
2101 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2102 | ERR("Register apps socket poll error"); | |
2103 | goto error; | |
2104 | } else if (revents & LPOLLIN) { | |
2105 | sock = lttcomm_accept_unix_sock(apps_sock); | |
2106 | if (sock < 0) { | |
2107 | goto error; | |
2108 | } | |
099e26bd | 2109 | |
16c5c8fa DG |
2110 | /* |
2111 | * Set socket timeout for both receiving and ending. | |
2112 | * app_socket_timeout is in seconds, whereas | |
2113 | * lttcomm_setsockopt_rcv_timeout and | |
2114 | * lttcomm_setsockopt_snd_timeout expect msec as | |
2115 | * parameter. | |
2116 | */ | |
2117 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
2118 | app_socket_timeout * 1000); | |
2119 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
2120 | app_socket_timeout * 1000); | |
2121 | ||
b662582b DG |
2122 | /* |
2123 | * Set the CLOEXEC flag. Return code is useless because | |
2124 | * either way, the show must go on. | |
2125 | */ | |
2126 | (void) utils_set_fd_cloexec(sock); | |
2127 | ||
5eb91c98 | 2128 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 2129 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 2130 | if (ust_cmd == NULL) { |
76d7553f | 2131 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
2132 | goto error; |
2133 | } | |
1d4b027a | 2134 | |
5eb91c98 DG |
2135 | /* |
2136 | * Using message-based transmissions to ensure we don't | |
2137 | * have to deal with partially received messages. | |
2138 | */ | |
4063050c MD |
2139 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
2140 | if (ret < 0) { | |
2141 | ERR("Exhausted file descriptors allowed for applications."); | |
2142 | free(ust_cmd); | |
2143 | ret = close(sock); | |
2144 | if (ret) { | |
2145 | PERROR("close"); | |
2146 | } | |
2147 | sock = -1; | |
2148 | continue; | |
2149 | } | |
d88aee68 | 2150 | |
840cb59c | 2151 | health_code_update(); |
d0b96690 DG |
2152 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
2153 | if (ret < 0) { | |
5eb91c98 | 2154 | free(ust_cmd); |
d0b96690 | 2155 | /* Close socket of the application. */ |
76d7553f MD |
2156 | ret = close(sock); |
2157 | if (ret) { | |
2158 | PERROR("close"); | |
2159 | } | |
4063050c | 2160 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 2161 | sock = -1; |
5eb91c98 DG |
2162 | continue; |
2163 | } | |
840cb59c | 2164 | health_code_update(); |
099e26bd | 2165 | |
5eb91c98 | 2166 | ust_cmd->sock = sock; |
34a2494f | 2167 | sock = -1; |
099e26bd | 2168 | |
5eb91c98 DG |
2169 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
2170 | " gid:%d sock:%d name:%s (version %d.%d)", | |
2171 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
2172 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
2173 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
2174 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 2175 | |
5eb91c98 DG |
2176 | /* |
2177 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 2178 | * has been taken! This apps will be part of the *system*. |
5eb91c98 | 2179 | */ |
8bdee6e2 | 2180 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
5eb91c98 DG |
2181 | |
2182 | /* | |
2183 | * Wake the registration queue futex. Implicit memory | |
8bdee6e2 | 2184 | * barrier with the exchange in cds_wfcq_enqueue. |
5eb91c98 DG |
2185 | */ |
2186 | futex_nto1_wake(&ust_cmd_queue.futex); | |
2187 | } | |
2188 | } | |
90014c57 | 2189 | } |
1d4b027a DG |
2190 | } |
2191 | ||
139ac872 | 2192 | exit: |
1d4b027a | 2193 | error: |
0fdd1e2c DG |
2194 | /* Notify that the registration thread is gone */ |
2195 | notify_ust_apps(0); | |
2196 | ||
a4b35e07 | 2197 | if (apps_sock >= 0) { |
76d7553f MD |
2198 | ret = close(apps_sock); |
2199 | if (ret) { | |
2200 | PERROR("close"); | |
2201 | } | |
a4b35e07 | 2202 | } |
46c3f085 | 2203 | if (sock >= 0) { |
76d7553f MD |
2204 | ret = close(sock); |
2205 | if (ret) { | |
2206 | PERROR("close"); | |
2207 | } | |
4063050c | 2208 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 2209 | } |
273ea72c | 2210 | unlink(apps_unix_sock_path); |
0fdd1e2c | 2211 | |
76d7553f | 2212 | error_poll_add: |
5eb91c98 | 2213 | lttng_poll_clean(&events); |
76d7553f MD |
2214 | error_listen: |
2215 | error_create_poll: | |
6993eeb3 | 2216 | error_testpoint: |
76d7553f | 2217 | DBG("UST Registration thread cleanup complete"); |
9ad42ec1 MD |
2218 | if (err) { |
2219 | health_error(); | |
2220 | ERR("Health error occurred in %s", __func__); | |
2221 | } | |
8782cc74 | 2222 | health_unregister(health_sessiond); |
5eb91c98 | 2223 | |
1d4b027a DG |
2224 | return NULL; |
2225 | } | |
2226 | ||
8c0faa1d | 2227 | /* |
3bd1e081 | 2228 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 2229 | * exec or it will fails. |
8c0faa1d | 2230 | */ |
3bd1e081 | 2231 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 2232 | { |
a23ec3a7 | 2233 | int ret, clock_ret; |
ee0b0061 DG |
2234 | struct timespec timeout; |
2235 | ||
a23ec3a7 DG |
2236 | /* Make sure we set the readiness flag to 0 because we are NOT ready */ |
2237 | consumer_data->consumer_thread_is_ready = 0; | |
8c0faa1d | 2238 | |
a23ec3a7 DG |
2239 | /* Setup pthread condition */ |
2240 | ret = pthread_condattr_init(&consumer_data->condattr); | |
4a15001e | 2241 | if (ret) { |
a23ec3a7 DG |
2242 | errno = ret; |
2243 | PERROR("pthread_condattr_init consumer data"); | |
2244 | goto error; | |
2245 | } | |
2246 | ||
2247 | /* | |
2248 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
2249 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
2250 | * for a more details and how we noticed it. | |
2251 | */ | |
2252 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
4a15001e | 2253 | if (ret) { |
a23ec3a7 DG |
2254 | errno = ret; |
2255 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
2256 | goto error; |
2257 | } | |
8c0faa1d | 2258 | |
a23ec3a7 | 2259 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
4a15001e | 2260 | if (ret) { |
a23ec3a7 DG |
2261 | errno = ret; |
2262 | PERROR("pthread_cond_init consumer data"); | |
2263 | goto error; | |
2264 | } | |
2265 | ||
2266 | ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer, | |
2267 | consumer_data); | |
4a15001e MD |
2268 | if (ret) { |
2269 | errno = ret; | |
3bd1e081 | 2270 | PERROR("pthread_create consumer"); |
ee0b0061 | 2271 | ret = -1; |
8c0faa1d DG |
2272 | goto error; |
2273 | } | |
2274 | ||
a23ec3a7 DG |
2275 | /* We are about to wait on a pthread condition */ |
2276 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2277 | ||
ee0b0061 | 2278 | /* Get time for sem_timedwait absolute timeout */ |
a23ec3a7 DG |
2279 | clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
2280 | /* | |
2281 | * Set the timeout for the condition timed wait even if the clock gettime | |
2282 | * call fails since we might loop on that call and we want to avoid to | |
2283 | * increment the timeout too many times. | |
2284 | */ | |
2285 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2286 | ||
2287 | /* | |
2288 | * The following loop COULD be skipped in some conditions so this is why we | |
2289 | * set ret to 0 in order to make sure at least one round of the loop is | |
2290 | * done. | |
2291 | */ | |
2292 | ret = 0; | |
2293 | ||
2294 | /* | |
2295 | * Loop until the condition is reached or when a timeout is reached. Note | |
2296 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2297 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2298 | * possible. This loop does not take any chances and works with both of | |
2299 | * them. | |
2300 | */ | |
2301 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2302 | if (clock_ret < 0) { | |
2303 | PERROR("clock_gettime spawn consumer"); | |
2304 | /* Infinite wait for the consumerd thread to be ready */ | |
2305 | ret = pthread_cond_wait(&consumer_data->cond, | |
2306 | &consumer_data->cond_mutex); | |
2307 | } else { | |
2308 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2309 | &consumer_data->cond_mutex, &timeout); | |
2310 | } | |
ee0b0061 | 2311 | } |
8c0faa1d | 2312 | |
a23ec3a7 DG |
2313 | /* Release the pthread condition */ |
2314 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2315 | ||
2316 | if (ret != 0) { | |
2317 | errno = ret; | |
2318 | if (ret == ETIMEDOUT) { | |
4282f9a3 DG |
2319 | int pth_ret; |
2320 | ||
ee0b0061 DG |
2321 | /* |
2322 | * Call has timed out so we kill the kconsumerd_thread and return | |
2323 | * an error. | |
2324 | */ | |
a23ec3a7 DG |
2325 | ERR("Condition timed out. The consumer thread was never ready." |
2326 | " Killing it"); | |
4282f9a3 DG |
2327 | pth_ret = pthread_cancel(consumer_data->thread); |
2328 | if (pth_ret < 0) { | |
3bd1e081 | 2329 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2330 | } |
2331 | } else { | |
a23ec3a7 | 2332 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 | 2333 | } |
4282f9a3 DG |
2334 | /* Caller is expecting a negative value on failure. */ |
2335 | ret = -1; | |
ee0b0061 DG |
2336 | goto error; |
2337 | } | |
2338 | ||
3bd1e081 MD |
2339 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2340 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2341 | ERR("Consumerd did not start"); |
3bd1e081 | 2342 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2343 | goto error; |
2344 | } | |
3bd1e081 | 2345 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2346 | |
8c0faa1d DG |
2347 | return 0; |
2348 | ||
2349 | error: | |
2350 | return ret; | |
2351 | } | |
2352 | ||
d9800920 | 2353 | /* |
3bd1e081 | 2354 | * Join consumer thread |
d9800920 | 2355 | */ |
3bd1e081 | 2356 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2357 | { |
2358 | void *status; | |
cf3af59e | 2359 | |
e8209f6b DG |
2360 | /* Consumer pid must be a real one. */ |
2361 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2362 | int ret; |
3bd1e081 | 2363 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2364 | if (ret) { |
4a15001e | 2365 | PERROR("Error killing consumer daemon"); |
cf3af59e MD |
2366 | return ret; |
2367 | } | |
3bd1e081 | 2368 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2369 | } else { |
2370 | return 0; | |
2371 | } | |
2372 | } | |
2373 | ||
8c0faa1d | 2374 | /* |
3bd1e081 | 2375 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2376 | * |
d063d709 | 2377 | * Return pid if successful else -1. |
8c0faa1d | 2378 | */ |
3bd1e081 | 2379 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2380 | { |
2381 | int ret; | |
2382 | pid_t pid; | |
94c55f17 | 2383 | const char *consumer_to_use; |
53086306 | 2384 | const char *verbosity; |
94c55f17 | 2385 | struct stat st; |
8c0faa1d | 2386 | |
3bd1e081 | 2387 | DBG("Spawning consumerd"); |
c49dc785 | 2388 | |
8c0faa1d DG |
2389 | pid = fork(); |
2390 | if (pid == 0) { | |
2391 | /* | |
3bd1e081 | 2392 | * Exec consumerd. |
8c0faa1d | 2393 | */ |
daee5345 | 2394 | if (opt_verbose_consumer) { |
53086306 | 2395 | verbosity = "--verbose"; |
4421f712 | 2396 | } else if (lttng_opt_quiet) { |
53086306 | 2397 | verbosity = "--quiet"; |
4421f712 DG |
2398 | } else { |
2399 | verbosity = ""; | |
53086306 | 2400 | } |
4421f712 | 2401 | |
3bd1e081 MD |
2402 | switch (consumer_data->type) { |
2403 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2404 | /* |
c7704d57 DG |
2405 | * Find out which consumerd to execute. We will first try the |
2406 | * 64-bit path, then the sessiond's installation directory, and | |
2407 | * fallback on the 32-bit one, | |
94c55f17 | 2408 | */ |
63a799e8 AM |
2409 | DBG3("Looking for a kernel consumer at these locations:"); |
2410 | DBG3(" 1) %s", consumerd64_bin); | |
2411 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
2412 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 2413 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 2414 | DBG3("Found location #1"); |
94c55f17 | 2415 | consumer_to_use = consumerd64_bin; |
94c55f17 | 2416 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 2417 | DBG3("Found location #2"); |
94c55f17 | 2418 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 2419 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 2420 | DBG3("Found location #3"); |
eb1e0bd4 | 2421 | consumer_to_use = consumerd32_bin; |
94c55f17 | 2422 | } else { |
63a799e8 | 2423 | DBG("Could not find any valid consumerd executable"); |
4282f9a3 | 2424 | ret = -EINVAL; |
94c55f17 AM |
2425 | break; |
2426 | } | |
2427 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
4282f9a3 | 2428 | ret = execl(consumer_to_use, |
94c55f17 AM |
2429 | "lttng-consumerd", verbosity, "-k", |
2430 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2431 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2432 | "--group", tracing_group_name, |
94c55f17 | 2433 | NULL); |
3bd1e081 | 2434 | break; |
7753dea8 MD |
2435 | case LTTNG_CONSUMER64_UST: |
2436 | { | |
b1e0b6b6 | 2437 | char *tmpnew = NULL; |
8f4905da MD |
2438 | |
2439 | if (consumerd64_libdir[0] != '\0') { | |
2440 | char *tmp; | |
2441 | size_t tmplen; | |
2442 | ||
2443 | tmp = getenv("LD_LIBRARY_PATH"); | |
2444 | if (!tmp) { | |
2445 | tmp = ""; | |
2446 | } | |
2447 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2448 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
2449 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2450 | if (!tmpnew) { | |
2451 | ret = -ENOMEM; | |
2452 | goto error; | |
2453 | } | |
2454 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2455 | strcat(tmpnew, consumerd64_libdir); | |
2456 | if (tmp[0] != '\0') { | |
2457 | strcat(tmpnew, ":"); | |
2458 | strcat(tmpnew, tmp); | |
2459 | } | |
2460 | ret = putenv(tmpnew); | |
2461 | if (ret) { | |
2462 | ret = -errno; | |
c6f76da9 | 2463 | free(tmpnew); |
8f4905da MD |
2464 | goto error; |
2465 | } | |
2466 | } | |
94c55f17 | 2467 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 2468 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2469 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2470 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2471 | "--group", tracing_group_name, |
7753dea8 | 2472 | NULL); |
8f4905da MD |
2473 | if (consumerd64_libdir[0] != '\0') { |
2474 | free(tmpnew); | |
2475 | } | |
3bd1e081 | 2476 | break; |
7753dea8 MD |
2477 | } |
2478 | case LTTNG_CONSUMER32_UST: | |
2479 | { | |
937dde8e | 2480 | char *tmpnew = NULL; |
8f4905da MD |
2481 | |
2482 | if (consumerd32_libdir[0] != '\0') { | |
2483 | char *tmp; | |
2484 | size_t tmplen; | |
2485 | ||
2486 | tmp = getenv("LD_LIBRARY_PATH"); | |
2487 | if (!tmp) { | |
2488 | tmp = ""; | |
2489 | } | |
2490 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2491 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
2492 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2493 | if (!tmpnew) { | |
2494 | ret = -ENOMEM; | |
2495 | goto error; | |
2496 | } | |
2497 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2498 | strcat(tmpnew, consumerd32_libdir); | |
2499 | if (tmp[0] != '\0') { | |
2500 | strcat(tmpnew, ":"); | |
2501 | strcat(tmpnew, tmp); | |
2502 | } | |
2503 | ret = putenv(tmpnew); | |
2504 | if (ret) { | |
2505 | ret = -errno; | |
c6f76da9 | 2506 | free(tmpnew); |
8f4905da MD |
2507 | goto error; |
2508 | } | |
2509 | } | |
94c55f17 | 2510 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 2511 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2512 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2513 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2514 | "--group", tracing_group_name, |
7753dea8 | 2515 | NULL); |
8f4905da MD |
2516 | if (consumerd32_libdir[0] != '\0') { |
2517 | free(tmpnew); | |
2518 | } | |
7753dea8 MD |
2519 | break; |
2520 | } | |
3bd1e081 | 2521 | default: |
76d7553f | 2522 | PERROR("unknown consumer type"); |
3bd1e081 MD |
2523 | exit(EXIT_FAILURE); |
2524 | } | |
8c0faa1d | 2525 | if (errno != 0) { |
4282f9a3 | 2526 | PERROR("Consumer execl()"); |
8c0faa1d | 2527 | } |
4282f9a3 | 2528 | /* Reaching this point, we got a failure on our execl(). */ |
8c0faa1d DG |
2529 | exit(EXIT_FAILURE); |
2530 | } else if (pid > 0) { | |
2531 | ret = pid; | |
8c0faa1d | 2532 | } else { |
76d7553f | 2533 | PERROR("start consumer fork"); |
8c0faa1d | 2534 | ret = -errno; |
8c0faa1d | 2535 | } |
8f4905da | 2536 | error: |
8c0faa1d DG |
2537 | return ret; |
2538 | } | |
2539 | ||
693bd40b | 2540 | /* |
3bd1e081 | 2541 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2542 | */ |
3bd1e081 | 2543 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2544 | { |
c617c0c6 | 2545 | int ret; |
edb8b045 DG |
2546 | |
2547 | /* | |
2548 | * Set the listen() state on the socket since there is a possible race | |
2549 | * between the exec() of the consumer daemon and this call if place in the | |
2550 | * consumer thread. See bug #366 for more details. | |
2551 | */ | |
2552 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2553 | if (ret < 0) { | |
2554 | goto error; | |
2555 | } | |
693bd40b | 2556 | |
3bd1e081 MD |
2557 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2558 | if (consumer_data->pid != 0) { | |
2559 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2560 | goto end; |
2561 | } | |
693bd40b | 2562 | |
3bd1e081 | 2563 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2564 | if (ret < 0) { |
3bd1e081 MD |
2565 | ERR("Spawning consumerd failed"); |
2566 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2567 | goto error; |
693bd40b | 2568 | } |
c49dc785 | 2569 | |
3bd1e081 MD |
2570 | /* Setting up the consumer_data pid */ |
2571 | consumer_data->pid = ret; | |
48842b30 | 2572 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2573 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2574 | |
3bd1e081 MD |
2575 | DBG2("Spawning consumer control thread"); |
2576 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2577 | if (ret < 0) { |
3bd1e081 | 2578 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2579 | goto error; |
2580 | } | |
2581 | ||
c49dc785 | 2582 | end: |
693bd40b DG |
2583 | return 0; |
2584 | ||
2585 | error: | |
331744e3 | 2586 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2587 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2588 | int err; |
2589 | ||
edb8b045 DG |
2590 | err = close(consumer_data->err_sock); |
2591 | if (err < 0) { | |
2592 | PERROR("close consumer data error socket"); | |
2593 | } | |
2594 | } | |
693bd40b DG |
2595 | return ret; |
2596 | } | |
2597 | ||
b73401da | 2598 | /* |
096102bd | 2599 | * Setup necessary data for kernel tracer action. |
b73401da | 2600 | */ |
096102bd | 2601 | static int init_kernel_tracer(void) |
b73401da DG |
2602 | { |
2603 | int ret; | |
b73401da | 2604 | |
096102bd DG |
2605 | /* Modprobe lttng kernel modules */ |
2606 | ret = modprobe_lttng_control(); | |
b73401da | 2607 | if (ret < 0) { |
b73401da DG |
2608 | goto error; |
2609 | } | |
2610 | ||
096102bd DG |
2611 | /* Open debugfs lttng */ |
2612 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2613 | if (kernel_tracer_fd < 0) { | |
2614 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b DG |
2615 | ret = -1; |
2616 | goto error_open; | |
54d01ffb DG |
2617 | } |
2618 | ||
2f77fc4b DG |
2619 | /* Validate kernel version */ |
2620 | ret = kernel_validate_version(kernel_tracer_fd); | |
2621 | if (ret < 0) { | |
2622 | goto error_version; | |
b551a063 | 2623 | } |
54d01ffb | 2624 | |
2f77fc4b DG |
2625 | ret = modprobe_lttng_data(); |
2626 | if (ret < 0) { | |
2627 | goto error_modules; | |
54d01ffb DG |
2628 | } |
2629 | ||
2f77fc4b DG |
2630 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2631 | return 0; | |
2632 | ||
2633 | error_version: | |
2634 | modprobe_remove_lttng_control(); | |
2635 | ret = close(kernel_tracer_fd); | |
2636 | if (ret) { | |
2637 | PERROR("close"); | |
b551a063 | 2638 | } |
2f77fc4b | 2639 | kernel_tracer_fd = -1; |
f73fabfd | 2640 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2641 | |
2f77fc4b DG |
2642 | error_modules: |
2643 | ret = close(kernel_tracer_fd); | |
2644 | if (ret) { | |
2645 | PERROR("close"); | |
b551a063 | 2646 | } |
54d01ffb | 2647 | |
2f77fc4b DG |
2648 | error_open: |
2649 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2650 | |
2651 | error: | |
2f77fc4b DG |
2652 | WARN("No kernel tracer available"); |
2653 | kernel_tracer_fd = -1; | |
2654 | if (!is_root) { | |
f73fabfd | 2655 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2656 | } else { |
f73fabfd | 2657 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2658 | } |
54d01ffb DG |
2659 | } |
2660 | ||
2f77fc4b | 2661 | |
54d01ffb | 2662 | /* |
2f77fc4b DG |
2663 | * Copy consumer output from the tracing session to the domain session. The |
2664 | * function also applies the right modification on a per domain basis for the | |
2665 | * trace files destination directory. | |
36b588ed MD |
2666 | * |
2667 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2668 | */ |
2f77fc4b | 2669 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2670 | { |
2671 | int ret; | |
2f77fc4b DG |
2672 | const char *dir_name; |
2673 | struct consumer_output *consumer; | |
2674 | ||
2675 | assert(session); | |
2676 | assert(session->consumer); | |
54d01ffb | 2677 | |
b551a063 DG |
2678 | switch (domain) { |
2679 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2680 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2681 | /* |
2682 | * XXX: We should audit the session creation and what this function | |
2683 | * does "extra" in order to avoid a destroy since this function is used | |
2684 | * in the domain session creation (kernel and ust) only. Same for UST | |
2685 | * domain. | |
2686 | */ | |
2687 | if (session->kernel_session->consumer) { | |
2688 | consumer_destroy_output(session->kernel_session->consumer); | |
2689 | } | |
2f77fc4b DG |
2690 | session->kernel_session->consumer = |
2691 | consumer_copy_output(session->consumer); | |
2692 | /* Ease our life a bit for the next part */ | |
2693 | consumer = session->kernel_session->consumer; | |
2694 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2695 | break; |
f20baf8e | 2696 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2697 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2698 | case LTTNG_DOMAIN_PYTHON: |
b551a063 | 2699 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2700 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd DG |
2701 | if (session->ust_session->consumer) { |
2702 | consumer_destroy_output(session->ust_session->consumer); | |
2703 | } | |
2f77fc4b DG |
2704 | session->ust_session->consumer = |
2705 | consumer_copy_output(session->consumer); | |
2706 | /* Ease our life a bit for the next part */ | |
2707 | consumer = session->ust_session->consumer; | |
2708 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2709 | break; |
2710 | default: | |
f73fabfd | 2711 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2712 | goto error; |
2713 | } | |
2714 | ||
2f77fc4b | 2715 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2716 | strncat(consumer->subdir, dir_name, |
2717 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2718 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2719 | ||
f73fabfd | 2720 | ret = LTTNG_OK; |
54d01ffb DG |
2721 | |
2722 | error: | |
2723 | return ret; | |
2724 | } | |
2725 | ||
00e2e675 | 2726 | /* |
2f77fc4b | 2727 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2728 | * |
2729 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2730 | */ |
2f77fc4b DG |
2731 | static int create_ust_session(struct ltt_session *session, |
2732 | struct lttng_domain *domain) | |
00e2e675 DG |
2733 | { |
2734 | int ret; | |
2f77fc4b | 2735 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2736 | |
a4b92340 | 2737 | assert(session); |
2f77fc4b DG |
2738 | assert(domain); |
2739 | assert(session->consumer); | |
a4b92340 | 2740 | |
2f77fc4b | 2741 | switch (domain->type) { |
f20baf8e | 2742 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2743 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2744 | case LTTNG_DOMAIN_PYTHON: |
2f77fc4b DG |
2745 | case LTTNG_DOMAIN_UST: |
2746 | break; | |
2747 | default: | |
2748 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2749 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2750 | goto error; |
2751 | } | |
2752 | ||
2f77fc4b DG |
2753 | DBG("Creating UST session"); |
2754 | ||
dec56f6c | 2755 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2756 | if (lus == NULL) { |
f73fabfd | 2757 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2758 | goto error; |
2759 | } | |
2760 | ||
2f77fc4b DG |
2761 | lus->uid = session->uid; |
2762 | lus->gid = session->gid; | |
2bba9e53 | 2763 | lus->output_traces = session->output_traces; |
27babd3a | 2764 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2765 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2766 | session->ust_session = lus; |
00e2e675 | 2767 | |
2f77fc4b DG |
2768 | /* Copy session output to the newly created UST session */ |
2769 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2770 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2771 | goto error; |
2772 | } | |
2773 | ||
f73fabfd | 2774 | return LTTNG_OK; |
00e2e675 DG |
2775 | |
2776 | error: | |
2f77fc4b DG |
2777 | free(lus); |
2778 | session->ust_session = NULL; | |
00e2e675 DG |
2779 | return ret; |
2780 | } | |
2781 | ||
2782 | /* | |
2f77fc4b | 2783 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2784 | */ |
2f77fc4b | 2785 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2786 | { |
2787 | int ret; | |
a4b92340 | 2788 | |
2f77fc4b | 2789 | DBG("Creating kernel session"); |
00e2e675 | 2790 | |
2f77fc4b DG |
2791 | ret = kernel_create_session(session, kernel_tracer_fd); |
2792 | if (ret < 0) { | |
f73fabfd | 2793 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2794 | goto error; |
2795 | } | |
2796 | ||
2f77fc4b DG |
2797 | /* Code flow safety */ |
2798 | assert(session->kernel_session); | |
2799 | ||
2800 | /* Copy session output to the newly created Kernel session */ | |
2801 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2802 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2803 | goto error; |
2804 | } | |
2805 | ||
2f77fc4b DG |
2806 | /* Create directory(ies) on local filesystem. */ |
2807 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && | |
2808 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
2809 | ret = run_as_mkdir_recursive( | |
2810 | session->kernel_session->consumer->dst.trace_path, | |
2811 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2812 | if (ret < 0) { | |
2813 | if (ret != -EEXIST) { | |
2814 | ERR("Trace directory creation error"); | |
00e2e675 DG |
2815 | goto error; |
2816 | } | |
00e2e675 | 2817 | } |
2f77fc4b | 2818 | } |
00e2e675 | 2819 | |
2f77fc4b DG |
2820 | session->kernel_session->uid = session->uid; |
2821 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2822 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2823 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2824 | |
f73fabfd | 2825 | return LTTNG_OK; |
00e2e675 | 2826 | |
2f77fc4b DG |
2827 | error: |
2828 | trace_kernel_destroy_session(session->kernel_session); | |
2829 | session->kernel_session = NULL; | |
2830 | return ret; | |
2831 | } | |
00e2e675 | 2832 | |
2f77fc4b DG |
2833 | /* |
2834 | * Count number of session permitted by uid/gid. | |
2835 | */ | |
2836 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2837 | { | |
2838 | unsigned int i = 0; | |
2839 | struct ltt_session *session; | |
07424f16 | 2840 | |
2f77fc4b DG |
2841 | DBG("Counting number of available session for UID %d GID %d", |
2842 | uid, gid); | |
2843 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2844 | /* |
2f77fc4b | 2845 | * Only list the sessions the user can control. |
00e2e675 | 2846 | */ |
2f77fc4b DG |
2847 | if (!session_access_ok(session, uid, gid)) { |
2848 | continue; | |
2849 | } | |
2850 | i++; | |
a4b92340 | 2851 | } |
2f77fc4b | 2852 | return i; |
00e2e675 DG |
2853 | } |
2854 | ||
54d01ffb DG |
2855 | /* |
2856 | * Process the command requested by the lttng client within the command | |
2857 | * context structure. This function make sure that the return structure (llm) | |
2858 | * is set and ready for transmission before returning. | |
2859 | * | |
2860 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2861 | * |
2862 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2863 | * |
2864 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2865 | */ |
53a80697 MD |
2866 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2867 | int *sock_error) | |
54d01ffb | 2868 | { |
f73fabfd | 2869 | int ret = LTTNG_OK; |
44d3bd01 | 2870 | int need_tracing_session = 1; |
2e09ba09 | 2871 | int need_domain; |
54d01ffb DG |
2872 | |
2873 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2874 | ||
53a80697 MD |
2875 | *sock_error = 0; |
2876 | ||
2e09ba09 MD |
2877 | switch (cmd_ctx->lsm->cmd_type) { |
2878 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2879 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2880 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2881 | case LTTNG_DESTROY_SESSION: |
2882 | case LTTNG_LIST_SESSIONS: | |
2883 | case LTTNG_LIST_DOMAINS: | |
2884 | case LTTNG_START_TRACE: | |
2885 | case LTTNG_STOP_TRACE: | |
6d805429 | 2886 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
2887 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
2888 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
2889 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
2890 | case LTTNG_SNAPSHOT_RECORD: | |
fb198a11 | 2891 | case LTTNG_SAVE_SESSION: |
2e09ba09 | 2892 | need_domain = 0; |
3aace903 | 2893 | break; |
2e09ba09 MD |
2894 | default: |
2895 | need_domain = 1; | |
2896 | } | |
2897 | ||
2898 | if (opt_no_kernel && need_domain | |
2899 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 | 2900 | if (!is_root) { |
f73fabfd | 2901 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 2902 | } else { |
f73fabfd | 2903 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 2904 | } |
4fba7219 DG |
2905 | goto error; |
2906 | } | |
2907 | ||
8d3113b2 DG |
2908 | /* Deny register consumer if we already have a spawned consumer. */ |
2909 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
2910 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
2911 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 2912 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 2913 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
2914 | goto error; |
2915 | } | |
2916 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
2917 | } | |
2918 | ||
54d01ffb DG |
2919 | /* |
2920 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2921 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2922 | * command. |
2923 | */ | |
2924 | switch(cmd_ctx->lsm->cmd_type) { | |
2925 | case LTTNG_LIST_SESSIONS: | |
2926 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2927 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
2928 | case LTTNG_LIST_DOMAINS: |
2929 | case LTTNG_LIST_CHANNELS: | |
2930 | case LTTNG_LIST_EVENTS: | |
834978fd | 2931 | case LTTNG_LIST_SYSCALLS: |
54d01ffb DG |
2932 | break; |
2933 | default: | |
2934 | /* Setup lttng message with no payload */ | |
2935 | ret = setup_lttng_msg(cmd_ctx, 0); | |
2936 | if (ret < 0) { | |
2937 | /* This label does not try to unlock the session */ | |
2938 | goto init_setup_error; | |
2939 | } | |
2940 | } | |
2941 | ||
2942 | /* Commands that DO NOT need a session. */ | |
2943 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 2944 | case LTTNG_CREATE_SESSION: |
27babd3a | 2945 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2946 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 | 2947 | case LTTNG_CALIBRATE: |
54d01ffb DG |
2948 | case LTTNG_LIST_SESSIONS: |
2949 | case LTTNG_LIST_TRACEPOINTS: | |
834978fd | 2950 | case LTTNG_LIST_SYSCALLS: |
f37d259d | 2951 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
fb198a11 | 2952 | case LTTNG_SAVE_SESSION: |
44d3bd01 | 2953 | need_tracing_session = 0; |
54d01ffb DG |
2954 | break; |
2955 | default: | |
2956 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
2957 | /* |
2958 | * We keep the session list lock across _all_ commands | |
2959 | * for now, because the per-session lock does not | |
2960 | * handle teardown properly. | |
2961 | */ | |
74babd95 | 2962 | session_lock_list(); |
54d01ffb DG |
2963 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
2964 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 2965 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
2966 | goto error; |
2967 | } else { | |
2968 | /* Acquire lock for the session */ | |
2969 | session_lock(cmd_ctx->session); | |
2970 | } | |
2971 | break; | |
2972 | } | |
b389abbe | 2973 | |
5f3ecf22 DG |
2974 | /* |
2975 | * Commands that need a valid session but should NOT create one if none | |
2976 | * exists. Instead of creating one and destroying it when the command is | |
2977 | * handled, process that right before so we save some round trip in useless | |
2978 | * code path. | |
2979 | */ | |
2980 | switch (cmd_ctx->lsm->cmd_type) { | |
2981 | case LTTNG_DISABLE_CHANNEL: | |
2982 | case LTTNG_DISABLE_EVENT: | |
5f3ecf22 DG |
2983 | switch (cmd_ctx->lsm->domain.type) { |
2984 | case LTTNG_DOMAIN_KERNEL: | |
2985 | if (!cmd_ctx->session->kernel_session) { | |
2986 | ret = LTTNG_ERR_NO_CHANNEL; | |
2987 | goto error; | |
2988 | } | |
2989 | break; | |
2990 | case LTTNG_DOMAIN_JUL: | |
5cdb6027 | 2991 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2992 | case LTTNG_DOMAIN_PYTHON: |
5f3ecf22 DG |
2993 | case LTTNG_DOMAIN_UST: |
2994 | if (!cmd_ctx->session->ust_session) { | |
2995 | ret = LTTNG_ERR_NO_CHANNEL; | |
2996 | goto error; | |
2997 | } | |
2998 | break; | |
2999 | default: | |
3000 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3001 | goto error; | |
3002 | } | |
3003 | default: | |
3004 | break; | |
3005 | } | |
3006 | ||
2e09ba09 MD |
3007 | if (!need_domain) { |
3008 | goto skip_domain; | |
3009 | } | |
a4b92340 | 3010 | |
54d01ffb DG |
3011 | /* |
3012 | * Check domain type for specific "pre-action". | |
3013 | */ | |
3014 | switch (cmd_ctx->lsm->domain.type) { | |
3015 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 3016 | if (!is_root) { |
f73fabfd | 3017 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
3018 | goto error; |
3019 | } | |
3020 | ||
54d01ffb | 3021 | /* Kernel tracer check */ |
a4b35e07 | 3022 | if (kernel_tracer_fd == -1) { |
54d01ffb | 3023 | /* Basically, load kernel tracer modules */ |
096102bd DG |
3024 | ret = init_kernel_tracer(); |
3025 | if (ret != 0) { | |
54d01ffb DG |
3026 | goto error; |
3027 | } | |
3028 | } | |
5eb91c98 | 3029 | |
5c827ce0 DG |
3030 | /* Consumer is in an ERROR state. Report back to client */ |
3031 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3032 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3033 | goto error; |
3034 | } | |
3035 | ||
54d01ffb | 3036 | /* Need a session for kernel command */ |
44d3bd01 | 3037 | if (need_tracing_session) { |
54d01ffb | 3038 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 3039 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 3040 | if (ret < 0) { |
f73fabfd | 3041 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
3042 | goto error; |
3043 | } | |
b389abbe | 3044 | } |
7d29a247 | 3045 | |
54d01ffb | 3046 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
3047 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
3048 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 3049 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
3050 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
3051 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 3052 | if (ret < 0) { |
f73fabfd | 3053 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 3054 | goto error; |
950131af | 3055 | } |
5c827ce0 | 3056 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
3057 | } else { |
3058 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 3059 | } |
173af62f | 3060 | |
a4b92340 DG |
3061 | /* |
3062 | * The consumer was just spawned so we need to add the socket to | |
3063 | * the consumer output of the session if exist. | |
3064 | */ | |
3065 | ret = consumer_create_socket(&kconsumer_data, | |
3066 | cmd_ctx->session->kernel_session->consumer); | |
3067 | if (ret < 0) { | |
3068 | goto error; | |
173af62f | 3069 | } |
0d0c377a | 3070 | } |
5c827ce0 | 3071 | |
54d01ffb | 3072 | break; |
b9dfb167 | 3073 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3074 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3075 | case LTTNG_DOMAIN_PYTHON: |
2bdd86d4 | 3076 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 3077 | { |
b51ec5b4 MD |
3078 | if (!ust_app_supported()) { |
3079 | ret = LTTNG_ERR_NO_UST; | |
3080 | goto error; | |
3081 | } | |
5c827ce0 DG |
3082 | /* Consumer is in an ERROR state. Report back to client */ |
3083 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3084 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3085 | goto error; |
3086 | } | |
3087 | ||
44d3bd01 | 3088 | if (need_tracing_session) { |
a4b92340 | 3089 | /* Create UST session if none exist. */ |
f6a9efaa | 3090 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 3091 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 3092 | &cmd_ctx->lsm->domain); |
f73fabfd | 3093 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
3094 | goto error; |
3095 | } | |
3096 | } | |
00e2e675 | 3097 | |
7753dea8 MD |
3098 | /* Start the UST consumer daemons */ |
3099 | /* 64-bit */ | |
3100 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 3101 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 3102 | ustconsumer64_data.pid == 0 && |
785d2d0d | 3103 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |