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 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
fac6795d DG |
21 | #include <getopt.h> |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
0bb7724a | 24 | #include <paths.h> |
fac6795d DG |
25 | #include <pthread.h> |
26 | #include <signal.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
331744e3 | 30 | #include <inttypes.h> |
0fdd1e2c | 31 | #include <sys/mman.h> |
b73401da | 32 | #include <sys/mount.h> |
1e307fab | 33 | #include <sys/resource.h> |
fac6795d DG |
34 | #include <sys/socket.h> |
35 | #include <sys/stat.h> | |
36 | #include <sys/types.h> | |
0fdd1e2c | 37 | #include <sys/wait.h> |
5c827ce0 | 38 | #include <urcu/uatomic.h> |
fac6795d | 39 | #include <unistd.h> |
4f0b90ee | 40 | #include <ctype.h> |
fac6795d | 41 | |
990570ed | 42 | #include <common/common.h> |
d27c42b8 | 43 | #include <common/compat/socket.h> |
e8fa9fb0 | 44 | #include <common/compat/getenv.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> |
f40ef1d5 | 51 | #include <common/config/session-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" |
b3530820 JG |
74 | #include "notification-thread.h" |
75 | #include "notification-thread-commands.h" | |
db66e574 | 76 | #include "rotation-thread.h" |
0dbc2034 | 77 | #include "lttng-syscall.h" |
7c1d2758 | 78 | #include "agent.h" |
5e97de00 | 79 | #include "ht-cleanup.h" |
e6142f2e | 80 | #include "sessiond-config.h" |
d086f507 | 81 | #include "sessiond-timer.h" |
ebaeda94 | 82 | |
4fc83d94 PP |
83 | static const char *help_msg = |
84 | #ifdef LTTNG_EMBED_HELP | |
85 | #include <lttng-sessiond.8.h> | |
86 | #else | |
87 | NULL | |
88 | #endif | |
89 | ; | |
90 | ||
fac6795d | 91 | const char *progname; |
1d4b027a | 92 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
0bb7724a | 93 | static pid_t child_ppid; /* Internal parent PID use with daemonize. */ |
c9cb3e7d | 94 | static int lockfile_fd = -1; |
3bd1e081 | 95 | |
0bb7724a DG |
96 | /* Set to 1 when a SIGUSR1 signal is received. */ |
97 | static int recv_child_signal; | |
98 | ||
88076e89 JD |
99 | static struct lttng_kernel_tracer_version kernel_tracer_version; |
100 | static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version; | |
101 | ||
a23ec3a7 DG |
102 | /* |
103 | * Consumer daemon specific control data. Every value not initialized here is | |
104 | * set to 0 by the static definition. | |
105 | */ | |
3bd1e081 MD |
106 | static struct consumer_data kconsumer_data = { |
107 | .type = LTTNG_CONSUMER_KERNEL, | |
03550b58 MD |
108 | .err_sock = -1, |
109 | .cmd_sock = -1, | |
b3530820 | 110 | .channel_monitor_pipe = -1, |
62c43103 | 111 | .channel_rotate_pipe = -1, |
173af62f DG |
112 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
113 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
114 | .cond = PTHREAD_COND_INITIALIZER, |
115 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 116 | }; |
7753dea8 MD |
117 | static struct consumer_data ustconsumer64_data = { |
118 | .type = LTTNG_CONSUMER64_UST, | |
03550b58 MD |
119 | .err_sock = -1, |
120 | .cmd_sock = -1, | |
b3530820 | 121 | .channel_monitor_pipe = -1, |
62c43103 | 122 | .channel_rotate_pipe = -1, |
173af62f DG |
123 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
124 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
125 | .cond = PTHREAD_COND_INITIALIZER, |
126 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
127 | }; |
128 | static struct consumer_data ustconsumer32_data = { | |
129 | .type = LTTNG_CONSUMER32_UST, | |
03550b58 MD |
130 | .err_sock = -1, |
131 | .cmd_sock = -1, | |
b3530820 | 132 | .channel_monitor_pipe = -1, |
62c43103 | 133 | .channel_rotate_pipe = -1, |
173af62f DG |
134 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
135 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
136 | .cond = PTHREAD_COND_INITIALIZER, |
137 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
138 | }; |
139 | ||
26296c48 JG |
140 | /* Command line options */ |
141 | static const struct option long_options[] = { | |
0f5ea17c JG |
142 | { "client-sock", required_argument, 0, 'c' }, |
143 | { "apps-sock", required_argument, 0, 'a' }, | |
144 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
145 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
146 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
147 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
148 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
149 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
150 | { "consumerd32-path", required_argument, 0, '\0' }, | |
151 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
152 | { "consumerd64-path", required_argument, 0, '\0' }, | |
153 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
154 | { "daemonize", no_argument, 0, 'd' }, | |
155 | { "background", no_argument, 0, 'b' }, | |
156 | { "sig-parent", no_argument, 0, 'S' }, | |
157 | { "help", no_argument, 0, 'h' }, | |
158 | { "group", required_argument, 0, 'g' }, | |
159 | { "version", no_argument, 0, 'V' }, | |
160 | { "quiet", no_argument, 0, 'q' }, | |
161 | { "verbose", no_argument, 0, 'v' }, | |
162 | { "verbose-consumer", no_argument, 0, '\0' }, | |
163 | { "no-kernel", no_argument, 0, '\0' }, | |
164 | { "pidfile", required_argument, 0, 'p' }, | |
165 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
166 | { "config", required_argument, 0, 'f' }, | |
167 | { "load", required_argument, 0, 'l' }, | |
168 | { "kmod-probes", required_argument, 0, '\0' }, | |
169 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
170 | { NULL, 0, 0, 0 } |
171 | }; | |
172 | ||
e6142f2e JG |
173 | struct sessiond_config config; |
174 | ||
26296c48 JG |
175 | /* Command line options to ignore from configuration file */ |
176 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
177 | ||
26c9d55e | 178 | /* Shared between threads */ |
099e26bd | 179 | static int dispatch_thread_exit; |
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 }; |
273ea72c | 192 | |
099e26bd DG |
193 | /* |
194 | * This pipe is used to inform the thread managing application communication | |
195 | * that a command is queued and ready to be processed. | |
196 | */ | |
76d7553f | 197 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 198 | |
d0b96690 DG |
199 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
200 | ||
1d4b027a | 201 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 202 | static pthread_t apps_thread; |
d0b96690 | 203 | static pthread_t apps_notify_thread; |
099e26bd | 204 | static pthread_t reg_apps_thread; |
1d4b027a | 205 | static pthread_t client_thread; |
7a485870 | 206 | static pthread_t kernel_thread; |
099e26bd | 207 | static pthread_t dispatch_thread; |
44a5e5eb | 208 | static pthread_t health_thread; |
0b2dc8df | 209 | static pthread_t ht_cleanup_thread; |
022d91ba | 210 | static pthread_t agent_reg_thread; |
ef367a93 | 211 | static pthread_t load_session_thread; |
b3530820 | 212 | static pthread_t notification_thread; |
db66e574 | 213 | static pthread_t rotation_thread; |
d086f507 | 214 | static pthread_t timer_thread; |
5eb91c98 | 215 | |
099e26bd DG |
216 | /* |
217 | * UST registration command queue. This queue is tied with a futex and uses a N | |
218 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
219 | * | |
b22c5da8 DG |
220 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
221 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
222 | * the line new application socket and monitors it for any I/O error or clean | |
223 | * close that triggers an unregistration of the application. | |
099e26bd DG |
224 | */ |
225 | static struct ust_cmd_queue ust_cmd_queue; | |
226 | ||
b5541356 DG |
227 | /* |
228 | * Pointer initialized before thread creation. | |
229 | * | |
230 | * This points to the tracing session list containing the session count and a | |
231 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
232 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 233 | * |
d063d709 | 234 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 235 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
236 | */ |
237 | static struct ltt_session_list *session_list_ptr; | |
238 | ||
7753dea8 MD |
239 | int ust_consumerd64_fd = -1; |
240 | int ust_consumerd32_fd = -1; | |
241 | ||
2f77fc4b DG |
242 | static const char *module_proc_lttng = "/proc/lttng"; |
243 | ||
5c827ce0 DG |
244 | /* |
245 | * Consumer daemon state which is changed when spawning it, killing it or in | |
246 | * case of a fatal error. | |
247 | */ | |
248 | enum consumerd_state { | |
249 | CONSUMER_STARTED = 1, | |
250 | CONSUMER_STOPPED = 2, | |
251 | CONSUMER_ERROR = 3, | |
252 | }; | |
253 | ||
254 | /* | |
255 | * This consumer daemon state is used to validate if a client command will be | |
256 | * able to reach the consumer. If not, the client is informed. For instance, | |
257 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
258 | * error to the client. | |
259 | * | |
260 | * The following example shows a possible race condition of this scheme: | |
261 | * | |
262 | * consumer thread error happens | |
263 | * client cmd arrives | |
264 | * client cmd checks state -> still OK | |
265 | * consumer thread exit, sets error | |
266 | * client cmd try to talk to consumer | |
267 | * ... | |
268 | * | |
269 | * However, since the consumer is a different daemon, we have no way of making | |
270 | * sure the command will reach it safely even with this state flag. This is why | |
271 | * we consider that up to the state validation during command processing, the | |
272 | * command is safe. After that, we can not guarantee the correctness of the | |
273 | * client request vis-a-vis the consumer. | |
274 | */ | |
275 | static enum consumerd_state ust_consumerd_state; | |
276 | static enum consumerd_state kernel_consumerd_state; | |
277 | ||
12744796 DG |
278 | /* Set in main() with the current page size. */ |
279 | long page_size; | |
280 | ||
8782cc74 MD |
281 | /* Application health monitoring */ |
282 | struct health_app *health_sessiond; | |
283 | ||
f43f95a9 DG |
284 | /* Am I root or not. */ |
285 | int is_root; /* Set to 1 if the daemon is running as root */ | |
286 | ||
26296c48 JG |
287 | const char * const config_section_name = "sessiond"; |
288 | ||
ef367a93 JG |
289 | /* Load session thread information to operate. */ |
290 | struct load_session_thread_data *load_info; | |
291 | ||
b3530820 JG |
292 | /* Notification thread handle. */ |
293 | struct notification_thread_handle *notification_thread_handle; | |
294 | ||
db66e574 JD |
295 | /* Rotation thread handle. */ |
296 | struct rotation_thread_handle *rotation_thread_handle; | |
297 | ||
7c1d2758 JG |
298 | /* Global hash tables */ |
299 | struct lttng_ht *agent_apps_ht_by_sock = NULL; | |
300 | ||
97bc1426 | 301 | /* |
9cc1ba3b JG |
302 | * The initialization of the session daemon is done in multiple phases. |
303 | * | |
304 | * While all threads are launched near-simultaneously, only some of them | |
305 | * are needed to ensure the session daemon can start to respond to client | |
b3530820 | 306 | * requests. |
9cc1ba3b JG |
307 | * |
308 | * There are two important guarantees that we wish to offer with respect | |
309 | * to the initialisation of the session daemon: | |
310 | * - When the daemonize/background launcher process exits, the sessiond | |
311 | * is fully able to respond to client requests, | |
312 | * - Auto-loaded sessions are visible to clients. | |
313 | * | |
314 | * In order to achieve this, a number of support threads have to be launched | |
315 | * to allow the "client" thread to function properly. Moreover, since the | |
316 | * "load session" thread needs the client thread, we must provide a way | |
317 | * for the "load session" thread to know that the "client" thread is up | |
318 | * and running. | |
319 | * | |
320 | * Hence, the support threads decrement the lttng_sessiond_ready counter | |
321 | * while the "client" threads waits for it to reach 0. Once the "client" thread | |
322 | * unblocks, it posts the message_thread_ready semaphore which allows the | |
323 | * "load session" thread to progress. | |
324 | * | |
325 | * This implies that the "load session" thread is the last to be initialized | |
326 | * and will explicitly call sessiond_signal_parents(), which signals the parents | |
327 | * that the session daemon is fully initialized. | |
328 | * | |
329 | * The four (4) support threads are: | |
330 | * - agent_thread | |
331 | * - notification_thread | |
332 | * - rotation_thread | |
333 | * - health_thread | |
97bc1426 | 334 | */ |
bc4b3ff1 JG |
335 | #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4 |
336 | int lttng_sessiond_ready = NR_LTTNG_SESSIOND_SUPPORT_THREADS; | |
97bc1426 | 337 | |
5e97de00 JG |
338 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
339 | { | |
340 | return (fd == thread_quit_pipe[0] && (events & LPOLLIN)) ? 1 : 0; | |
341 | } | |
342 | ||
97bc1426 | 343 | /* Notify parents that we are ready for cmd and health check */ |
ef367a93 | 344 | LTTNG_HIDDEN |
9cc1ba3b | 345 | void sessiond_signal_parents(void) |
97bc1426 | 346 | { |
9cc1ba3b JG |
347 | /* |
348 | * Notify parent pid that we are ready to accept command | |
349 | * for client side. This ppid is the one from the | |
350 | * external process that spawned us. | |
351 | */ | |
352 | if (config.sig_parent) { | |
353 | kill(ppid, SIGUSR1); | |
354 | } | |
97bc1426 | 355 | |
9cc1ba3b JG |
356 | /* |
357 | * Notify the parent of the fork() process that we are | |
358 | * ready. | |
359 | */ | |
360 | if (config.daemonize || config.background) { | |
361 | kill(child_ppid, SIGUSR1); | |
97bc1426 MD |
362 | } |
363 | } | |
364 | ||
9cc1ba3b JG |
365 | LTTNG_HIDDEN |
366 | void sessiond_notify_ready(void) | |
367 | { | |
368 | /* | |
524423d6 JG |
369 | * This memory barrier is paired with the one performed by |
370 | * the client thread after it has seen that 'lttng_sessiond_ready' is 0. | |
371 | * | |
372 | * The purpose of these memory barriers is to ensure that all | |
373 | * initialization operations of the various threads that call this | |
374 | * function to signal that they are ready are commited/published | |
375 | * before the client thread can see the 'lttng_sessiond_ready' counter | |
376 | * reach 0. | |
377 | * | |
378 | * Note that this could be a 'write' memory barrier, but a full barrier | |
379 | * is used in case the code using this utility changes. The performance | |
380 | * implications of this choice are minimal since this is a slow path. | |
9cc1ba3b | 381 | */ |
524423d6 JG |
382 | cmm_smp_mb(); |
383 | uatomic_sub(<tng_sessiond_ready, 1); | |
9cc1ba3b JG |
384 | } |
385 | ||
4a15001e MD |
386 | static |
387 | int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size, | |
388 | int *a_pipe) | |
5eb91c98 DG |
389 | { |
390 | int ret; | |
391 | ||
d0b96690 | 392 | assert(events); |
5eb91c98 DG |
393 | |
394 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
395 | if (ret < 0) { | |
396 | goto error; | |
397 | } | |
398 | ||
399 | /* Add quit pipe */ | |
4a15001e | 400 | ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
401 | if (ret < 0) { |
402 | goto error; | |
403 | } | |
404 | ||
405 | return 0; | |
406 | ||
407 | error: | |
408 | return ret; | |
409 | } | |
410 | ||
4a15001e MD |
411 | /* |
412 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
413 | */ | |
414 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) | |
415 | { | |
416 | return __sessiond_set_thread_pollset(events, size, thread_quit_pipe); | |
417 | } | |
418 | ||
273ea72c | 419 | /* |
5eb91c98 | 420 | * Init thread quit pipe. |
273ea72c DG |
421 | * |
422 | * Return -1 on error or 0 if all pipes are created. | |
423 | */ | |
4a15001e | 424 | static int __init_thread_quit_pipe(int *a_pipe) |
273ea72c | 425 | { |
730389d9 | 426 | int ret, i; |
273ea72c | 427 | |
4a15001e | 428 | ret = pipe(a_pipe); |
273ea72c | 429 | if (ret < 0) { |
730389d9 | 430 | PERROR("thread quit pipe"); |
273ea72c DG |
431 | goto error; |
432 | } | |
433 | ||
730389d9 | 434 | for (i = 0; i < 2; i++) { |
4a15001e | 435 | ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC); |
730389d9 DG |
436 | if (ret < 0) { |
437 | PERROR("fcntl"); | |
438 | goto error; | |
439 | } | |
440 | } | |
441 | ||
273ea72c DG |
442 | error: |
443 | return ret; | |
444 | } | |
445 | ||
4a15001e MD |
446 | static int init_thread_quit_pipe(void) |
447 | { | |
448 | return __init_thread_quit_pipe(thread_quit_pipe); | |
449 | } | |
450 | ||
099e26bd DG |
451 | /* |
452 | * Stop all threads by closing the thread quit pipe. | |
453 | */ | |
cf3af59e MD |
454 | static void stop_threads(void) |
455 | { | |
5eb91c98 DG |
456 | int ret; |
457 | ||
cf3af59e MD |
458 | /* Stopping all threads */ |
459 | DBG("Terminating all threads"); | |
54d01ffb | 460 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
461 | if (ret < 0) { |
462 | ERR("write error on thread quit pipe"); | |
463 | } | |
464 | ||
099e26bd | 465 | /* Dispatch thread */ |
26c9d55e | 466 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 467 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
468 | } |
469 | ||
e975f9f8 DG |
470 | /* |
471 | * Close every consumer sockets. | |
472 | */ | |
473 | static void close_consumer_sockets(void) | |
474 | { | |
475 | int ret; | |
476 | ||
477 | if (kconsumer_data.err_sock >= 0) { | |
478 | ret = close(kconsumer_data.err_sock); | |
479 | if (ret < 0) { | |
480 | PERROR("kernel consumer err_sock close"); | |
481 | } | |
482 | } | |
483 | if (ustconsumer32_data.err_sock >= 0) { | |
484 | ret = close(ustconsumer32_data.err_sock); | |
485 | if (ret < 0) { | |
a76cbd9f | 486 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
487 | } |
488 | } | |
489 | if (ustconsumer64_data.err_sock >= 0) { | |
490 | ret = close(ustconsumer64_data.err_sock); | |
491 | if (ret < 0) { | |
a76cbd9f | 492 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
493 | } |
494 | } | |
495 | if (kconsumer_data.cmd_sock >= 0) { | |
496 | ret = close(kconsumer_data.cmd_sock); | |
497 | if (ret < 0) { | |
498 | PERROR("kernel consumer cmd_sock close"); | |
499 | } | |
500 | } | |
501 | if (ustconsumer32_data.cmd_sock >= 0) { | |
502 | ret = close(ustconsumer32_data.cmd_sock); | |
503 | if (ret < 0) { | |
a76cbd9f | 504 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
505 | } |
506 | } | |
507 | if (ustconsumer64_data.cmd_sock >= 0) { | |
508 | ret = close(ustconsumer64_data.cmd_sock); | |
509 | if (ret < 0) { | |
a76cbd9f | 510 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
511 | } |
512 | } | |
b3530820 JG |
513 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
514 | ret = close(kconsumer_data.channel_monitor_pipe); | |
515 | if (ret < 0) { | |
516 | PERROR("kernel consumer channel monitor pipe close"); | |
517 | } | |
518 | } | |
519 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
520 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
521 | if (ret < 0) { | |
522 | PERROR("UST consumerd32 channel monitor pipe close"); | |
523 | } | |
524 | } | |
525 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
526 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
527 | if (ret < 0) { | |
528 | PERROR("UST consumerd64 channel monitor pipe close"); | |
529 | } | |
530 | } | |
62c43103 JD |
531 | if (kconsumer_data.channel_rotate_pipe >= 0) { |
532 | ret = close(kconsumer_data.channel_rotate_pipe); | |
533 | if (ret < 0) { | |
534 | PERROR("kernel consumer channel rotate pipe close"); | |
535 | } | |
536 | } | |
537 | if (ustconsumer32_data.channel_rotate_pipe >= 0) { | |
538 | ret = close(ustconsumer32_data.channel_rotate_pipe); | |
539 | if (ret < 0) { | |
540 | PERROR("UST consumerd32 channel rotate pipe close"); | |
541 | } | |
542 | } | |
543 | if (ustconsumer64_data.channel_rotate_pipe >= 0) { | |
544 | ret = close(ustconsumer64_data.channel_rotate_pipe); | |
545 | if (ret < 0) { | |
546 | PERROR("UST consumerd64 channel rotate pipe close"); | |
547 | } | |
548 | } | |
e975f9f8 DG |
549 | } |
550 | ||
4e4714cb JR |
551 | /* |
552 | * Wait on consumer process termination. | |
553 | * | |
554 | * Need to be called with the consumer data lock held or from a context | |
555 | * ensuring no concurrent access to data (e.g: cleanup). | |
556 | */ | |
557 | static void wait_consumer(struct consumer_data *consumer_data) | |
558 | { | |
559 | pid_t ret; | |
560 | int status; | |
561 | ||
562 | if (consumer_data->pid <= 0) { | |
563 | return; | |
564 | } | |
565 | ||
566 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
567 | consumer_data->pid); | |
568 | ret = waitpid(consumer_data->pid, &status, 0); | |
569 | if (ret == -1) { | |
570 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 571 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
572 | ERR("consumerd termination with error: %d", |
573 | WEXITSTATUS(ret)); | |
574 | } | |
575 | consumer_data->pid = 0; | |
576 | } | |
577 | ||
fac6795d | 578 | /* |
4a15001e | 579 | * Cleanup the session daemon's data structures. |
fac6795d | 580 | */ |
4a15001e | 581 | static void sessiond_cleanup(void) |
fac6795d | 582 | { |
ef599319 | 583 | int ret; |
af9737e9 | 584 | struct ltt_session *sess, *stmp; |
fac6795d | 585 | |
4a15001e | 586 | DBG("Cleanup sessiond"); |
e07ae692 | 587 | |
4e449f3f MD |
588 | /* |
589 | * Close the thread quit pipe. It has already done its job, | |
590 | * since we are now called. | |
591 | */ | |
2f77fc4b DG |
592 | utils_close_pipe(thread_quit_pipe); |
593 | ||
c9a2957d JG |
594 | ret = remove(config.pid_file_path.value); |
595 | if (ret < 0) { | |
596 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
597 | } |
598 | ||
e6142f2e JG |
599 | DBG("Removing sessiond and consumerd content of directory %s", |
600 | config.rundir.value); | |
8c6c56c2 MD |
601 | |
602 | /* sessiond */ | |
e6142f2e JG |
603 | DBG("Removing %s", config.pid_file_path.value); |
604 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 605 | |
e6142f2e JG |
606 | DBG("Removing %s", config.agent_port_file_path.value); |
607 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 608 | |
8c6c56c2 | 609 | /* kconsumerd */ |
e6142f2e JG |
610 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
611 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
612 | ||
613 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
614 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
615 | |
616 | /* ust consumerd 32 */ | |
e6142f2e JG |
617 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
618 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
619 | ||
620 | DBG("Removing directory %s", config.consumerd32_path.value); | |
621 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
622 | |
623 | /* ust consumerd 64 */ | |
e6142f2e JG |
624 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
625 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
626 | ||
627 | DBG("Removing directory %s", config.consumerd64_path.value); | |
628 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 629 | |
99bab54f | 630 | DBG("Cleaning up all sessions"); |
fac6795d | 631 | |
b5541356 | 632 | /* Destroy session list mutex */ |
273ea72c DG |
633 | if (session_list_ptr != NULL) { |
634 | pthread_mutex_destroy(&session_list_ptr->lock); | |
635 | ||
636 | /* Cleanup ALL session */ | |
54d01ffb DG |
637 | cds_list_for_each_entry_safe(sess, stmp, |
638 | &session_list_ptr->head, list) { | |
90936dcf JD |
639 | cmd_destroy_session(sess, kernel_poll_pipe[1], |
640 | notification_thread_handle); | |
273ea72c DG |
641 | } |
642 | } | |
643 | ||
4e4714cb JR |
644 | wait_consumer(&kconsumer_data); |
645 | wait_consumer(&ustconsumer64_data); | |
646 | wait_consumer(&ustconsumer32_data); | |
647 | ||
6a4e4039 JG |
648 | DBG("Cleaning up all agent apps"); |
649 | agent_app_ht_clean(); | |
650 | ||
099e26bd | 651 | DBG("Closing all UST sockets"); |
56fff090 | 652 | ust_app_clean_list(); |
7972aab2 | 653 | buffer_reg_destroy_registries(); |
099e26bd | 654 | |
e6142f2e | 655 | if (is_root && !config.no_kernel) { |
4fba7219 | 656 | DBG2("Closing kernel fd"); |
a4b35e07 | 657 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
658 | ret = close(kernel_tracer_fd); |
659 | if (ret) { | |
660 | PERROR("close"); | |
661 | } | |
a4b35e07 | 662 | } |
2f50c8a3 | 663 | DBG("Unloading kernel modules"); |
096102bd | 664 | modprobe_remove_lttng_all(); |
834978fd | 665 | free(syscall_table); |
2f50c8a3 | 666 | } |
2f77fc4b | 667 | |
e975f9f8 DG |
668 | close_consumer_sockets(); |
669 | ||
ef367a93 JG |
670 | if (load_info) { |
671 | load_session_destroy_data(load_info); | |
672 | free(load_info); | |
673 | } | |
674 | ||
c9cb3e7d JG |
675 | /* |
676 | * We do NOT rmdir rundir because there are other processes | |
677 | * using it, for instance lttng-relayd, which can start in | |
678 | * parallel with this teardown. | |
679 | */ | |
4a15001e MD |
680 | } |
681 | ||
682 | /* | |
683 | * Cleanup the daemon's option data structures. | |
684 | */ | |
685 | static void sessiond_cleanup_options(void) | |
686 | { | |
687 | DBG("Cleaning up options"); | |
688 | ||
e6142f2e | 689 | sessiond_config_fini(&config); |
c9cb3e7d | 690 | |
7567352f | 691 | run_as_destroy_worker(); |
fac6795d DG |
692 | } |
693 | ||
e065084a | 694 | /* |
d063d709 | 695 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 696 | * |
d063d709 | 697 | * Return lttcomm error code. |
e065084a DG |
698 | */ |
699 | static int send_unix_sock(int sock, void *buf, size_t len) | |
700 | { | |
701 | /* Check valid length */ | |
c617c0c6 | 702 | if (len == 0) { |
e065084a DG |
703 | return -1; |
704 | } | |
705 | ||
706 | return lttcomm_send_unix_sock(sock, buf, len); | |
707 | } | |
708 | ||
5461b305 | 709 | /* |
d063d709 | 710 | * Free memory of a command context structure. |
5461b305 | 711 | */ |
a2fb29a5 | 712 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 713 | { |
a2fb29a5 DG |
714 | DBG("Clean command context structure"); |
715 | if (*cmd_ctx) { | |
716 | if ((*cmd_ctx)->llm) { | |
717 | free((*cmd_ctx)->llm); | |
5461b305 | 718 | } |
a2fb29a5 DG |
719 | if ((*cmd_ctx)->lsm) { |
720 | free((*cmd_ctx)->lsm); | |
5461b305 | 721 | } |
a2fb29a5 DG |
722 | free(*cmd_ctx); |
723 | *cmd_ctx = NULL; | |
5461b305 DG |
724 | } |
725 | } | |
726 | ||
fac6795d | 727 | /* |
0fdd1e2c | 728 | * Notify UST applications using the shm mmap futex. |
fac6795d | 729 | */ |
0fdd1e2c | 730 | static int notify_ust_apps(int active) |
fac6795d | 731 | { |
0fdd1e2c | 732 | char *wait_shm_mmap; |
fac6795d | 733 | |
0fdd1e2c | 734 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 735 | |
0fdd1e2c | 736 | /* See shm.c for this call implying mmap, shm and futex calls */ |
e6142f2e | 737 | wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root); |
0fdd1e2c | 738 | if (wait_shm_mmap == NULL) { |
fac6795d DG |
739 | goto error; |
740 | } | |
741 | ||
0fdd1e2c DG |
742 | /* Wake waiting process */ |
743 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
744 | ||
745 | /* Apps notified successfully */ | |
746 | return 0; | |
fac6795d DG |
747 | |
748 | error: | |
0fdd1e2c | 749 | return -1; |
fac6795d DG |
750 | } |
751 | ||
e065084a | 752 | /* |
d063d709 DG |
753 | * Setup the outgoing data buffer for the response (llm) by allocating the |
754 | * right amount of memory and copying the original information from the lsm | |
755 | * structure. | |
ca95a216 | 756 | * |
6e10c9b9 | 757 | * Return 0 on success, negative value on error. |
ca95a216 | 758 | */ |
6e10c9b9 PP |
759 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, |
760 | const void *payload_buf, size_t payload_len, | |
761 | const void *cmd_header_buf, size_t cmd_header_len) | |
ca95a216 | 762 | { |
6e10c9b9 PP |
763 | int ret = 0; |
764 | const size_t header_len = sizeof(struct lttcomm_lttng_msg); | |
765 | const size_t cmd_header_offset = header_len; | |
766 | const size_t payload_offset = cmd_header_offset + cmd_header_len; | |
767 | const size_t total_msg_size = header_len + cmd_header_len + payload_len; | |
ca95a216 | 768 | |
6e10c9b9 | 769 | cmd_ctx->llm = zmalloc(total_msg_size); |
5461b305 | 770 | |
5461b305 | 771 | if (cmd_ctx->llm == NULL) { |
76d7553f | 772 | PERROR("zmalloc"); |
5461b305 | 773 | ret = -ENOMEM; |
6e10c9b9 | 774 | goto end; |
ca95a216 DG |
775 | } |
776 | ||
5461b305 DG |
777 | /* Copy common data */ |
778 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 779 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
6e10c9b9 PP |
780 | cmd_ctx->llm->cmd_header_size = cmd_header_len; |
781 | cmd_ctx->llm->data_size = payload_len; | |
782 | cmd_ctx->lttng_msg_size = total_msg_size; | |
5461b305 | 783 | |
6e10c9b9 | 784 | /* Copy command header */ |
b4e3ceb9 PP |
785 | if (cmd_header_len) { |
786 | memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf, | |
787 | cmd_header_len); | |
788 | } | |
5461b305 | 789 | |
6e10c9b9 | 790 | /* Copy payload */ |
b4e3ceb9 PP |
791 | if (payload_len) { |
792 | memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf, | |
793 | payload_len); | |
794 | } | |
ca95a216 | 795 | |
6e10c9b9 | 796 | end: |
ca95a216 DG |
797 | return ret; |
798 | } | |
799 | ||
6e10c9b9 PP |
800 | /* |
801 | * Version of setup_lttng_msg() without command header. | |
802 | */ | |
803 | static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx, | |
804 | void *payload_buf, size_t payload_len) | |
805 | { | |
806 | return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0); | |
807 | } | |
7a485870 | 808 | /* |
5eb91c98 | 809 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 810 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 811 | */ |
5eb91c98 | 812 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 813 | { |
5eb91c98 | 814 | int ret; |
7a485870 DG |
815 | struct ltt_session *session; |
816 | struct ltt_kernel_channel *channel; | |
817 | ||
5eb91c98 | 818 | DBG("Updating kernel poll set"); |
7a485870 | 819 | |
54d01ffb | 820 | session_lock_list(); |
b5541356 | 821 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 822 | session_lock(session); |
7a485870 | 823 | if (session->kernel_session == NULL) { |
54d01ffb | 824 | session_unlock(session); |
7a485870 DG |
825 | continue; |
826 | } | |
7a485870 | 827 | |
54d01ffb DG |
828 | cds_list_for_each_entry(channel, |
829 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
830 | /* Add channel fd to the kernel poll set */ |
831 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
832 | if (ret < 0) { | |
54d01ffb | 833 | session_unlock(session); |
5eb91c98 DG |
834 | goto error; |
835 | } | |
836 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 837 | } |
54d01ffb | 838 | session_unlock(session); |
7a485870 | 839 | } |
54d01ffb | 840 | session_unlock_list(); |
7a485870 | 841 | |
5eb91c98 | 842 | return 0; |
7a485870 DG |
843 | |
844 | error: | |
54d01ffb | 845 | session_unlock_list(); |
7a485870 DG |
846 | return -1; |
847 | } | |
848 | ||
849 | /* | |
54d01ffb | 850 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 851 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 852 | * |
d063d709 | 853 | * Useful for CPU hotplug feature. |
7a485870 | 854 | */ |
7f032058 | 855 | static int update_kernel_stream(int fd) |
7a485870 DG |
856 | { |
857 | int ret = 0; | |
858 | struct ltt_session *session; | |
173af62f | 859 | struct ltt_kernel_session *ksess; |
7a485870 DG |
860 | struct ltt_kernel_channel *channel; |
861 | ||
862 | DBG("Updating kernel streams for channel fd %d", fd); | |
863 | ||
54d01ffb | 864 | session_lock_list(); |
b5541356 | 865 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 866 | session_lock(session); |
7a485870 | 867 | if (session->kernel_session == NULL) { |
54d01ffb | 868 | session_unlock(session); |
7a485870 DG |
869 | continue; |
870 | } | |
173af62f | 871 | ksess = session->kernel_session; |
d9800920 | 872 | |
4a15001e MD |
873 | cds_list_for_each_entry(channel, |
874 | &ksess->channel_list.head, list) { | |
875 | struct lttng_ht_iter iter; | |
876 | struct consumer_socket *socket; | |
d9800920 | 877 | |
4a15001e MD |
878 | if (channel->fd != fd) { |
879 | continue; | |
880 | } | |
881 | DBG("Channel found, updating kernel streams"); | |
882 | ret = kernel_open_channel_stream(channel); | |
883 | if (ret < 0) { | |
884 | goto error; | |
885 | } | |
886 | /* Update the stream global counter */ | |
887 | ksess->stream_count_global += ret; | |
888 | ||
889 | /* | |
890 | * Have we already sent fds to the consumer? If yes, it | |
891 | * means that tracing is started so it is safe to send | |
892 | * our updated stream fds. | |
893 | */ | |
894 | if (ksess->consumer_fds_sent != 1 | |
895 | || ksess->consumer == NULL) { | |
896 | ret = -1; | |
897 | goto error; | |
898 | } | |
899 | ||
900 | rcu_read_lock(); | |
901 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, | |
902 | &iter.iter, socket, node.node) { | |
903 | pthread_mutex_lock(socket->lock); | |
1fc1b7c8 | 904 | ret = kernel_consumer_send_channel_streams(socket, |
4a15001e MD |
905 | channel, ksess, |
906 | session->output_traces ? 1 : 0); | |
907 | pthread_mutex_unlock(socket->lock); | |
908 | if (ret < 0) { | |
e7fe706f | 909 | rcu_read_unlock(); |
4a15001e | 910 | goto error; |
7a485870 | 911 | } |
7a485870 | 912 | } |
4a15001e | 913 | rcu_read_unlock(); |
7a485870 | 914 | } |
54d01ffb | 915 | session_unlock(session); |
7a485870 | 916 | } |
54d01ffb | 917 | session_unlock_list(); |
b3c750d2 | 918 | return ret; |
7a485870 | 919 | |
b3c750d2 | 920 | error: |
54d01ffb DG |
921 | session_unlock(session); |
922 | session_unlock_list(); | |
7a485870 DG |
923 | return ret; |
924 | } | |
925 | ||
487cf67c | 926 | /* |
ffe60014 DG |
927 | * For each tracing session, update newly registered apps. The session list |
928 | * lock MUST be acquired before calling this. | |
487cf67c DG |
929 | */ |
930 | static void update_ust_app(int app_sock) | |
931 | { | |
932 | struct ltt_session *sess, *stmp; | |
933 | ||
fdadac08 DG |
934 | /* Consumer is in an ERROR state. Stop any application update. */ |
935 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
936 | /* Stop the update process since the consumer is dead. */ | |
937 | return; | |
938 | } | |
939 | ||
487cf67c DG |
940 | /* For all tracing session(s) */ |
941 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
a9ad0c8f MD |
942 | struct ust_app *app; |
943 | ||
4ee14516 | 944 | session_lock(sess); |
a9ad0c8f MD |
945 | if (!sess->ust_session) { |
946 | goto unlock_session; | |
947 | } | |
948 | ||
949 | rcu_read_lock(); | |
950 | assert(app_sock >= 0); | |
951 | app = ust_app_find_by_sock(app_sock); | |
952 | if (app == NULL) { | |
953 | /* | |
954 | * Application can be unregistered before so | |
955 | * this is possible hence simply stopping the | |
956 | * update. | |
957 | */ | |
958 | DBG3("UST app update failed to find app sock %d", | |
959 | app_sock); | |
960 | goto unlock_rcu; | |
421cb601 | 961 | } |
a9ad0c8f MD |
962 | ust_app_global_update(sess->ust_session, app); |
963 | unlock_rcu: | |
964 | rcu_read_unlock(); | |
965 | unlock_session: | |
4ee14516 | 966 | session_unlock(sess); |
487cf67c DG |
967 | } |
968 | } | |
969 | ||
7a485870 | 970 | /* |
d063d709 | 971 | * This thread manage event coming from the kernel. |
7a485870 | 972 | * |
d063d709 DG |
973 | * Features supported in this thread: |
974 | * -) CPU Hotplug | |
7a485870 DG |
975 | */ |
976 | static void *thread_manage_kernel(void *data) | |
977 | { | |
139ac872 | 978 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 979 | uint32_t revents, nb_fd; |
7a485870 | 980 | char tmp; |
5eb91c98 | 981 | struct lttng_poll_event events; |
7a485870 | 982 | |
6993eeb3 | 983 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 984 | |
6c71277b | 985 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 986 | |
d5d63bf1 DG |
987 | /* |
988 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 989 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 990 | */ |
6d737ce4 | 991 | lttng_poll_init(&events); |
d5d63bf1 | 992 | |
e547b070 | 993 | if (testpoint(sessiond_thread_manage_kernel)) { |
6993eeb3 CB |
994 | goto error_testpoint; |
995 | } | |
8ac94142 | 996 | |
840cb59c | 997 | health_code_update(); |
44a5e5eb | 998 | |
e547b070 | 999 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
d21b0d71 | 1000 | goto error_testpoint; |
6993eeb3 CB |
1001 | } |
1002 | ||
7a485870 | 1003 | while (1) { |
840cb59c | 1004 | health_code_update(); |
44a5e5eb | 1005 | |
7a485870 | 1006 | if (update_poll_flag == 1) { |
d21b0d71 DG |
1007 | /* Clean events object. We are about to populate it again. */ |
1008 | lttng_poll_clean(&events); | |
1009 | ||
d0b96690 | 1010 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
1011 | if (ret < 0) { |
1012 | goto error_poll_create; | |
1013 | } | |
1014 | ||
1015 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
1016 | if (ret < 0) { | |
1017 | goto error; | |
1018 | } | |
5f822d0a | 1019 | |
d21b0d71 | 1020 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
1021 | ret = update_kernel_poll(&events); |
1022 | if (ret < 0) { | |
7a485870 DG |
1023 | goto error; |
1024 | } | |
1025 | update_poll_flag = 0; | |
1026 | } | |
1027 | ||
7fa2082e | 1028 | DBG("Thread kernel polling"); |
7a485870 DG |
1029 | |
1030 | /* Poll infinite value of time */ | |
88f2b785 | 1031 | restart: |
a78af745 | 1032 | health_poll_entry(); |
5eb91c98 | 1033 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1034 | DBG("Thread kernel return from poll on %d fds", |
1035 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1036 | health_poll_exit(); |
7a485870 | 1037 | if (ret < 0) { |
88f2b785 MD |
1038 | /* |
1039 | * Restart interrupted system call. | |
1040 | */ | |
1041 | if (errno == EINTR) { | |
1042 | goto restart; | |
1043 | } | |
7a485870 DG |
1044 | goto error; |
1045 | } else if (ret == 0) { | |
1046 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
1047 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
1048 | "This should not have happened! Continuing..."); | |
7a485870 DG |
1049 | continue; |
1050 | } | |
1051 | ||
0d9c5d77 DG |
1052 | nb_fd = ret; |
1053 | ||
5eb91c98 DG |
1054 | for (i = 0; i < nb_fd; i++) { |
1055 | /* Fetch once the poll data */ | |
1056 | revents = LTTNG_POLL_GETEV(&events, i); | |
1057 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 1058 | |
840cb59c | 1059 | health_code_update(); |
44a5e5eb | 1060 | |
fd20dac9 MD |
1061 | if (!revents) { |
1062 | /* No activity for this FD (poll implementation). */ | |
1063 | continue; | |
1064 | } | |
1065 | ||
5eb91c98 | 1066 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1067 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1068 | if (ret) { |
139ac872 MD |
1069 | err = 0; |
1070 | goto exit; | |
5eb91c98 | 1071 | } |
7a485870 | 1072 | |
5eb91c98 | 1073 | /* Check for data on kernel pipe */ |
03e43155 MD |
1074 | if (revents & LPOLLIN) { |
1075 | if (pollfd == kernel_poll_pipe[0]) { | |
1076 | (void) lttng_read(kernel_poll_pipe[0], | |
1077 | &tmp, 1); | |
1078 | /* | |
1079 | * Ret value is useless here, if this pipe gets any actions an | |
1080 | * update is required anyway. | |
1081 | */ | |
1082 | update_poll_flag = 1; | |
1083 | continue; | |
1084 | } else { | |
1085 | /* | |
1086 | * New CPU detected by the kernel. Adding kernel stream to | |
1087 | * kernel session and updating the kernel consumer | |
1088 | */ | |
7f032058 | 1089 | ret = update_kernel_stream(pollfd); |
5eb91c98 DG |
1090 | if (ret < 0) { |
1091 | continue; | |
1092 | } | |
1093 | break; | |
7a485870 | 1094 | } |
03e43155 MD |
1095 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1096 | update_poll_flag = 1; | |
1097 | continue; | |
1098 | } else { | |
1099 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1100 | goto error; | |
7a485870 DG |
1101 | } |
1102 | } | |
1103 | } | |
1104 | ||
139ac872 | 1105 | exit: |
7a485870 | 1106 | error: |
5eb91c98 | 1107 | lttng_poll_clean(&events); |
76d7553f | 1108 | error_poll_create: |
6993eeb3 | 1109 | error_testpoint: |
6620da75 DG |
1110 | utils_close_pipe(kernel_poll_pipe); |
1111 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 1112 | if (err) { |
840cb59c | 1113 | health_error(); |
139ac872 | 1114 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
1115 | WARN("Kernel thread died unexpectedly. " |
1116 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 1117 | } |
8782cc74 | 1118 | health_unregister(health_sessiond); |
76d7553f | 1119 | DBG("Kernel thread dying"); |
7a485870 DG |
1120 | return NULL; |
1121 | } | |
1122 | ||
a23ec3a7 DG |
1123 | /* |
1124 | * Signal pthread condition of the consumer data that the thread. | |
1125 | */ | |
1126 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
1127 | { | |
1128 | pthread_mutex_lock(&data->cond_mutex); | |
1129 | ||
1130 | /* | |
1131 | * The state is set before signaling. It can be any value, it's the waiter | |
1132 | * job to correctly interpret this condition variable associated to the | |
1133 | * consumer pthread_cond. | |
1134 | * | |
1135 | * A value of 0 means that the corresponding thread of the consumer data | |
1136 | * was not started. 1 indicates that the thread has started and is ready | |
1137 | * for action. A negative value means that there was an error during the | |
1138 | * thread bootstrap. | |
1139 | */ | |
1140 | data->consumer_thread_is_ready = state; | |
1141 | (void) pthread_cond_signal(&data->cond); | |
1142 | ||
1143 | pthread_mutex_unlock(&data->cond_mutex); | |
1144 | } | |
1145 | ||
1d4b027a | 1146 | /* |
3bd1e081 | 1147 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 1148 | */ |
3bd1e081 | 1149 | static void *thread_manage_consumer(void *data) |
1d4b027a | 1150 | { |
42fc1d0b | 1151 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 1152 | uint32_t revents, nb_fd; |
1d4b027a | 1153 | enum lttcomm_return_code code; |
5eb91c98 | 1154 | struct lttng_poll_event events; |
3bd1e081 | 1155 | struct consumer_data *consumer_data = data; |
b3530820 | 1156 | struct consumer_socket *cmd_socket_wrapper = NULL; |
1d4b027a | 1157 | |
3bd1e081 | 1158 | DBG("[thread] Manage consumer started"); |
1d4b027a | 1159 | |
34c1e15a MD |
1160 | rcu_register_thread(); |
1161 | rcu_thread_online(); | |
1162 | ||
6c71277b | 1163 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 1164 | |
855060f8 | 1165 | health_code_update(); |
9449cc75 | 1166 | |
5eb91c98 | 1167 | /* |
331744e3 JD |
1168 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
1169 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 1170 | */ |
331744e3 | 1171 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 1172 | if (ret < 0) { |
76d7553f | 1173 | goto error_poll; |
5eb91c98 | 1174 | } |
273ea72c | 1175 | |
edb8b045 DG |
1176 | /* |
1177 | * The error socket here is already in a listening state which was done | |
1178 | * just before spawning this thread to avoid a race between the consumer | |
1179 | * daemon exec trying to connect and the listen() call. | |
1180 | */ | |
3bd1e081 | 1181 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
1182 | if (ret < 0) { |
1183 | goto error; | |
1184 | } | |
1185 | ||
840cb59c | 1186 | health_code_update(); |
44a5e5eb | 1187 | |
331744e3 | 1188 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1189 | restart: |
a78af745 | 1190 | health_poll_entry(); |
8ac94142 | 1191 | |
e547b070 | 1192 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
1193 | goto error; |
1194 | } | |
8ac94142 | 1195 | |
5eb91c98 | 1196 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1197 | health_poll_exit(); |
273ea72c | 1198 | if (ret < 0) { |
88f2b785 MD |
1199 | /* |
1200 | * Restart interrupted system call. | |
1201 | */ | |
1202 | if (errno == EINTR) { | |
1203 | goto restart; | |
1204 | } | |
273ea72c DG |
1205 | goto error; |
1206 | } | |
1207 | ||
0d9c5d77 DG |
1208 | nb_fd = ret; |
1209 | ||
5eb91c98 DG |
1210 | for (i = 0; i < nb_fd; i++) { |
1211 | /* Fetch once the poll data */ | |
1212 | revents = LTTNG_POLL_GETEV(&events, i); | |
1213 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1214 | ||
840cb59c | 1215 | health_code_update(); |
44a5e5eb | 1216 | |
fd20dac9 MD |
1217 | if (!revents) { |
1218 | /* No activity for this FD (poll implementation). */ | |
1219 | continue; | |
1220 | } | |
1221 | ||
5eb91c98 | 1222 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1223 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1224 | if (ret) { |
139ac872 MD |
1225 | err = 0; |
1226 | goto exit; | |
5eb91c98 DG |
1227 | } |
1228 | ||
1229 | /* Event on the registration socket */ | |
3bd1e081 | 1230 | if (pollfd == consumer_data->err_sock) { |
03e43155 MD |
1231 | if (revents & LPOLLIN) { |
1232 | continue; | |
1233 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 1234 | ERR("consumer err socket poll error"); |
5eb91c98 | 1235 | goto error; |
03e43155 MD |
1236 | } else { |
1237 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1238 | goto error; | |
5eb91c98 DG |
1239 | } |
1240 | } | |
273ea72c DG |
1241 | } |
1242 | ||
3bd1e081 | 1243 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
1244 | if (sock < 0) { |
1245 | goto error; | |
1246 | } | |
1247 | ||
b662582b DG |
1248 | /* |
1249 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1250 | * show must go on. | |
1251 | */ | |
1252 | (void) utils_set_fd_cloexec(sock); | |
1253 | ||
840cb59c | 1254 | health_code_update(); |
44a5e5eb | 1255 | |
3bd1e081 | 1256 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 1257 | |
712ea556 | 1258 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
1259 | ret = lttcomm_recv_unix_sock(sock, &code, |
1260 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
1261 | if (ret <= 0) { |
1262 | goto error; | |
1263 | } | |
1264 | ||
840cb59c | 1265 | health_code_update(); |
b3530820 | 1266 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 | 1267 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1268 | lttcomm_get_readable_code(-code)); |
1269 | goto error; | |
1270 | } | |
1271 | ||
b3530820 JG |
1272 | /* Connect both command and metadata sockets. */ |
1273 | consumer_data->cmd_sock = | |
1274 | lttcomm_connect_unix_sock( | |
1275 | consumer_data->cmd_unix_sock_path); | |
1276 | consumer_data->metadata_fd = | |
1277 | lttcomm_connect_unix_sock( | |
1278 | consumer_data->cmd_unix_sock_path); | |
1279 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { | |
1280 | PERROR("consumer connect cmd socket"); | |
1281 | /* On error, signal condition and quit. */ | |
1282 | signal_consumer_condition(consumer_data, -1); | |
1283 | goto error; | |
1284 | } | |
1285 | ||
1286 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; | |
1287 | ||
1288 | /* Create metadata socket lock. */ | |
1289 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
1290 | if (consumer_data->metadata_sock.lock == NULL) { | |
1291 | PERROR("zmalloc pthread mutex"); | |
1292 | goto error; | |
1293 | } | |
1294 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
1295 | ||
1296 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); | |
1297 | DBG("Consumer metadata socket ready (fd: %d)", | |
1298 | consumer_data->metadata_fd); | |
1299 | ||
1300 | /* | |
1301 | * Remove the consumerd error sock since we've established a connection. | |
1302 | */ | |
3bd1e081 | 1303 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1304 | if (ret < 0) { |
72079cae DG |
1305 | goto error; |
1306 | } | |
1307 | ||
331744e3 | 1308 | /* Add new accepted error socket. */ |
5eb91c98 DG |
1309 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1310 | if (ret < 0) { | |
72079cae | 1311 | goto error; |
5eb91c98 DG |
1312 | } |
1313 | ||
331744e3 | 1314 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 1315 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
1316 | LPOLLIN | LPOLLRDHUP); |
1317 | if (ret < 0) { | |
1318 | goto error; | |
1319 | } | |
1320 | ||
840cb59c | 1321 | health_code_update(); |
44a5e5eb | 1322 | |
b3530820 | 1323 | /* |
62c43103 JD |
1324 | * Transfer the write-end of the channel monitoring and rotate pipe |
1325 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and | |
1326 | * SET_CHANNEL_ROTATE_PIPE commands. | |
b3530820 JG |
1327 | */ |
1328 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); | |
1329 | if (!cmd_socket_wrapper) { | |
1330 | goto error; | |
1331 | } | |
3e4dc117 | 1332 | cmd_socket_wrapper->lock = &consumer_data->lock; |
b3530820 JG |
1333 | |
1334 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, | |
1335 | consumer_data->channel_monitor_pipe); | |
1336 | if (ret) { | |
1337 | goto error; | |
1338 | } | |
62c43103 JD |
1339 | |
1340 | ret = consumer_send_channel_rotate_pipe(cmd_socket_wrapper, | |
1341 | consumer_data->channel_rotate_pipe); | |
1342 | if (ret) { | |
1343 | goto error; | |
1344 | } | |
1345 | ||
b3530820 JG |
1346 | /* Discard the socket wrapper as it is no longer needed. */ |
1347 | consumer_destroy_socket(cmd_socket_wrapper); | |
1348 | cmd_socket_wrapper = NULL; | |
1349 | ||
1350 | /* The thread is completely initialized, signal that it is ready. */ | |
1351 | signal_consumer_condition(consumer_data, 1); | |
1352 | ||
331744e3 | 1353 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1354 | restart_poll: |
331744e3 | 1355 | while (1) { |
42fc1d0b DG |
1356 | health_code_update(); |
1357 | ||
1358 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
1359 | if (should_quit) { | |
1360 | /* Not a health error. */ | |
1361 | err = 0; | |
1362 | goto exit; | |
1363 | } | |
1364 | ||
331744e3 JD |
1365 | health_poll_entry(); |
1366 | ret = lttng_poll_wait(&events, -1); | |
1367 | health_poll_exit(); | |
1368 | if (ret < 0) { | |
1369 | /* | |
1370 | * Restart interrupted system call. | |
1371 | */ | |
1372 | if (errno == EINTR) { | |
1373 | goto restart_poll; | |
1374 | } | |
1375 | goto error; | |
88f2b785 | 1376 | } |
72079cae | 1377 | |
331744e3 | 1378 | nb_fd = ret; |
0d9c5d77 | 1379 | |
331744e3 JD |
1380 | for (i = 0; i < nb_fd; i++) { |
1381 | /* Fetch once the poll data */ | |
1382 | revents = LTTNG_POLL_GETEV(&events, i); | |
1383 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 1384 | |
331744e3 | 1385 | health_code_update(); |
44a5e5eb | 1386 | |
fd20dac9 MD |
1387 | if (!revents) { |
1388 | /* No activity for this FD (poll implementation). */ | |
1389 | continue; | |
1390 | } | |
1391 | ||
42fc1d0b DG |
1392 | /* |
1393 | * Thread quit pipe has been triggered, flag that we should stop | |
1394 | * but continue the current loop to handle potential data from | |
1395 | * consumer. | |
1396 | */ | |
1397 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 1398 | |
331744e3 JD |
1399 | if (pollfd == sock) { |
1400 | /* Event on the consumerd socket */ | |
03e43155 MD |
1401 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
1402 | && !(revents & LPOLLIN)) { | |
331744e3 JD |
1403 | ERR("consumer err socket second poll error"); |
1404 | goto error; | |
1405 | } | |
1406 | health_code_update(); | |
1407 | /* Wait for any kconsumerd error */ | |
1408 | ret = lttcomm_recv_unix_sock(sock, &code, | |
1409 | sizeof(enum lttcomm_return_code)); | |
1410 | if (ret <= 0) { | |
1411 | ERR("consumer closed the command socket"); | |
1412 | goto error; | |
1413 | } | |
1414 | ||
1415 | ERR("consumer return code : %s", | |
1416 | lttcomm_get_readable_code(-code)); | |
1417 | ||
1418 | goto exit; | |
4ce514c4 | 1419 | } else if (pollfd == consumer_data->metadata_fd) { |
03e43155 MD |
1420 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
1421 | && !(revents & LPOLLIN)) { | |
1422 | ERR("consumer err metadata socket second poll error"); | |
1423 | goto error; | |
1424 | } | |
331744e3 JD |
1425 | /* UST metadata requests */ |
1426 | ret = ust_consumer_metadata_request( | |
1427 | &consumer_data->metadata_sock); | |
1428 | if (ret < 0) { | |
1429 | ERR("Handling metadata request"); | |
1430 | goto error; | |
1431 | } | |
5eb91c98 | 1432 | } |
42fc1d0b | 1433 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 1434 | } |
331744e3 | 1435 | health_code_update(); |
5eb91c98 DG |
1436 | } |
1437 | ||
139ac872 | 1438 | exit: |
1d4b027a | 1439 | error: |
fdadac08 DG |
1440 | /* |
1441 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1442 | * thread might be using them so get exclusive access which will abort all |
1443 | * other consumer command by other threads. | |
fdadac08 DG |
1444 | */ |
1445 | pthread_mutex_lock(&consumer_data->lock); | |
1446 | ||
5c827ce0 DG |
1447 | /* Immediately set the consumerd state to stopped */ |
1448 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1449 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1450 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1451 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1452 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1453 | } else { | |
1454 | /* Code flow error... */ | |
1455 | assert(0); | |
1456 | } | |
1457 | ||
76d7553f MD |
1458 | if (consumer_data->err_sock >= 0) { |
1459 | ret = close(consumer_data->err_sock); | |
1460 | if (ret) { | |
1461 | PERROR("close"); | |
1462 | } | |
a76cbd9f | 1463 | consumer_data->err_sock = -1; |
76d7553f MD |
1464 | } |
1465 | if (consumer_data->cmd_sock >= 0) { | |
1466 | ret = close(consumer_data->cmd_sock); | |
1467 | if (ret) { | |
1468 | PERROR("close"); | |
1469 | } | |
a76cbd9f | 1470 | consumer_data->cmd_sock = -1; |
76d7553f | 1471 | } |
96544455 SS |
1472 | if (consumer_data->metadata_sock.fd_ptr && |
1473 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 1474 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
1475 | if (ret) { |
1476 | PERROR("close"); | |
1477 | } | |
1478 | } | |
76d7553f MD |
1479 | if (sock >= 0) { |
1480 | ret = close(sock); | |
1481 | if (ret) { | |
1482 | PERROR("close"); | |
1483 | } | |
1484 | } | |
273ea72c | 1485 | |
3bd1e081 MD |
1486 | unlink(consumer_data->err_unix_sock_path); |
1487 | unlink(consumer_data->cmd_unix_sock_path); | |
fdadac08 | 1488 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1489 | |
fdadac08 | 1490 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
1491 | if (consumer_data->metadata_sock.lock) { |
1492 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1493 | free(consumer_data->metadata_sock.lock); | |
1494 | } | |
5eb91c98 | 1495 | lttng_poll_clean(&events); |
b3530820 JG |
1496 | |
1497 | if (cmd_socket_wrapper) { | |
1498 | consumer_destroy_socket(cmd_socket_wrapper); | |
1499 | } | |
76d7553f | 1500 | error_poll: |
139ac872 | 1501 | if (err) { |
840cb59c | 1502 | health_error(); |
139ac872 MD |
1503 | ERR("Health error occurred in %s", __func__); |
1504 | } | |
8782cc74 | 1505 | health_unregister(health_sessiond); |
76d7553f | 1506 | DBG("consumer thread cleanup completed"); |
0177d773 | 1507 | |
34c1e15a MD |
1508 | rcu_thread_offline(); |
1509 | rcu_unregister_thread(); | |
1510 | ||
5eb91c98 | 1511 | return NULL; |
099e26bd DG |
1512 | } |
1513 | ||
099e26bd | 1514 | /* |
81f04d5f JG |
1515 | * This thread receives application command sockets (FDs) on the |
1516 | * apps_cmd_pipe and waits (polls) on them until they are closed | |
1517 | * or an error occurs. | |
1518 | * | |
1519 | * At that point, it flushes the data (tracing and metadata) associated | |
1520 | * with this application and tears down ust app sessions and other | |
1521 | * associated data structures through ust_app_unregister(). | |
1522 | * | |
1523 | * Note that this thread never sends commands to the applications | |
1524 | * through the command sockets; it merely listens for hang-ups | |
1525 | * and errors on those sockets and cleans-up as they occur. | |
1d4b027a DG |
1526 | */ |
1527 | static void *thread_manage_apps(void *data) | |
099e26bd | 1528 | { |
139ac872 | 1529 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1530 | ssize_t size_ret; |
5eb91c98 | 1531 | uint32_t revents, nb_fd; |
5eb91c98 | 1532 | struct lttng_poll_event events; |
099e26bd DG |
1533 | |
1534 | DBG("[thread] Manage application started"); | |
1535 | ||
f6a9efaa DG |
1536 | rcu_register_thread(); |
1537 | rcu_thread_online(); | |
1538 | ||
6c71277b | 1539 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1540 | |
e547b070 | 1541 | if (testpoint(sessiond_thread_manage_apps)) { |
6993eeb3 CB |
1542 | goto error_testpoint; |
1543 | } | |
1544 | ||
840cb59c | 1545 | health_code_update(); |
44a5e5eb | 1546 | |
d0b96690 | 1547 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1548 | if (ret < 0) { |
76d7553f | 1549 | goto error_poll_create; |
5eb91c98 | 1550 | } |
099e26bd | 1551 | |
5eb91c98 DG |
1552 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1553 | if (ret < 0) { | |
1554 | goto error; | |
1555 | } | |
099e26bd | 1556 | |
e547b070 | 1557 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
6993eeb3 CB |
1558 | goto error; |
1559 | } | |
8ac94142 | 1560 | |
840cb59c | 1561 | health_code_update(); |
44a5e5eb | 1562 | |
5eb91c98 | 1563 | while (1) { |
7fa2082e | 1564 | DBG("Apps thread polling"); |
099e26bd DG |
1565 | |
1566 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1567 | restart: |
a78af745 | 1568 | health_poll_entry(); |
5eb91c98 | 1569 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1570 | DBG("Apps thread return from poll on %d fds", |
1571 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1572 | health_poll_exit(); |
099e26bd | 1573 | if (ret < 0) { |
88f2b785 MD |
1574 | /* |
1575 | * Restart interrupted system call. | |
1576 | */ | |
1577 | if (errno == EINTR) { | |
1578 | goto restart; | |
1579 | } | |
099e26bd DG |
1580 | goto error; |
1581 | } | |
1582 | ||
0d9c5d77 DG |
1583 | nb_fd = ret; |
1584 | ||
5eb91c98 DG |
1585 | for (i = 0; i < nb_fd; i++) { |
1586 | /* Fetch once the poll data */ | |
1587 | revents = LTTNG_POLL_GETEV(&events, i); | |
1588 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1589 | ||
840cb59c | 1590 | health_code_update(); |
44a5e5eb | 1591 | |
fd20dac9 MD |
1592 | if (!revents) { |
1593 | /* No activity for this FD (poll implementation). */ | |
1594 | continue; | |
1595 | } | |
1596 | ||
5eb91c98 | 1597 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1598 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1599 | if (ret) { |
139ac872 MD |
1600 | err = 0; |
1601 | goto exit; | |
5eb91c98 | 1602 | } |
099e26bd | 1603 | |
5eb91c98 DG |
1604 | /* Inspect the apps cmd pipe */ |
1605 | if (pollfd == apps_cmd_pipe[0]) { | |
03e43155 | 1606 | if (revents & LPOLLIN) { |
d0b96690 DG |
1607 | int sock; |
1608 | ||
5eb91c98 | 1609 | /* Empty pipe */ |
6cd525e8 MD |
1610 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1611 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1612 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1613 | goto error; |
1614 | } | |
099e26bd | 1615 | |
840cb59c | 1616 | health_code_update(); |
44a5e5eb | 1617 | |
ffe60014 | 1618 | /* |
03e43155 MD |
1619 | * Since this is a command socket (write then read), |
1620 | * we only monitor the error events of the socket. | |
ffe60014 | 1621 | */ |
d0b96690 DG |
1622 | ret = lttng_poll_add(&events, sock, |
1623 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1624 | if (ret < 0) { | |
5eb91c98 | 1625 | goto error; |
e0c7ec2b | 1626 | } |
acc7b41b | 1627 | |
d0b96690 | 1628 | DBG("Apps with sock %d added to poll set", sock); |
03e43155 MD |
1629 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1630 | ERR("Apps command pipe error"); | |
1631 | goto error; | |
1632 | } else { | |
1633 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
1634 | goto error; | |
0177d773 | 1635 | } |
5eb91c98 DG |
1636 | } else { |
1637 | /* | |
54d01ffb DG |
1638 | * At this point, we know that a registered application made |
1639 | * the event at poll_wait. | |
5eb91c98 DG |
1640 | */ |
1641 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1642 | /* Removing from the poll set */ | |
1643 | ret = lttng_poll_del(&events, pollfd); | |
1644 | if (ret < 0) { | |
1645 | goto error; | |
1646 | } | |
099e26bd | 1647 | |
b9d9b220 | 1648 | /* Socket closed on remote end. */ |
56fff090 | 1649 | ust_app_unregister(pollfd); |
03e43155 MD |
1650 | } else { |
1651 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1652 | goto error; | |
5eb91c98 | 1653 | } |
099e26bd | 1654 | } |
44a5e5eb | 1655 | |
840cb59c | 1656 | health_code_update(); |
099e26bd | 1657 | } |
099e26bd DG |
1658 | } |
1659 | ||
139ac872 | 1660 | exit: |
099e26bd | 1661 | error: |
5eb91c98 | 1662 | lttng_poll_clean(&events); |
76d7553f | 1663 | error_poll_create: |
6993eeb3 | 1664 | error_testpoint: |
6620da75 DG |
1665 | utils_close_pipe(apps_cmd_pipe); |
1666 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1667 | ||
1668 | /* | |
1669 | * We don't clean the UST app hash table here since already registered | |
1670 | * applications can still be controlled so let them be until the session | |
1671 | * daemon dies or the applications stop. | |
1672 | */ | |
1673 | ||
139ac872 | 1674 | if (err) { |
840cb59c | 1675 | health_error(); |
139ac872 MD |
1676 | ERR("Health error occurred in %s", __func__); |
1677 | } | |
8782cc74 | 1678 | health_unregister(health_sessiond); |
76d7553f | 1679 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1680 | rcu_thread_offline(); |
1681 | rcu_unregister_thread(); | |
099e26bd DG |
1682 | return NULL; |
1683 | } | |
1684 | ||
d0b96690 | 1685 | /* |
d88aee68 DG |
1686 | * Send a socket to a thread This is called from the dispatch UST registration |
1687 | * thread once all sockets are set for the application. | |
d0b96690 | 1688 | * |
b85dc84c DG |
1689 | * The sock value can be invalid, we don't really care, the thread will handle |
1690 | * it and make the necessary cleanup if so. | |
1691 | * | |
d0b96690 DG |
1692 | * On success, return 0 else a negative value being the errno message of the |
1693 | * write(). | |
1694 | */ | |
d88aee68 | 1695 | static int send_socket_to_thread(int fd, int sock) |
d0b96690 | 1696 | { |
6cd525e8 | 1697 | ssize_t ret; |
d0b96690 | 1698 | |
b85dc84c DG |
1699 | /* |
1700 | * It's possible that the FD is set as invalid with -1 concurrently just | |
1701 | * before calling this function being a shutdown state of the thread. | |
1702 | */ | |
1703 | if (fd < 0) { | |
1704 | ret = -EBADF; | |
1705 | goto error; | |
1706 | } | |
d0b96690 | 1707 | |
6cd525e8 MD |
1708 | ret = lttng_write(fd, &sock, sizeof(sock)); |
1709 | if (ret < sizeof(sock)) { | |
d88aee68 | 1710 | PERROR("write apps pipe %d", fd); |
d0b96690 DG |
1711 | if (ret < 0) { |
1712 | ret = -errno; | |
1713 | } | |
1714 | goto error; | |
1715 | } | |
1716 | ||
1717 | /* All good. Don't send back the write positive ret value. */ | |
1718 | ret = 0; | |
1719 | error: | |
6cd525e8 | 1720 | return (int) ret; |
d0b96690 DG |
1721 | } |
1722 | ||
f45e313d DG |
1723 | /* |
1724 | * Sanitize the wait queue of the dispatch registration thread meaning removing | |
1725 | * invalid nodes from it. This is to avoid memory leaks for the case the UST | |
1726 | * notify socket is never received. | |
1727 | */ | |
1728 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) | |
1729 | { | |
1730 | int ret, nb_fd = 0, i; | |
1731 | unsigned int fd_added = 0; | |
1732 | struct lttng_poll_event events; | |
1733 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; | |
1734 | ||
1735 | assert(wait_queue); | |
1736 | ||
1737 | lttng_poll_init(&events); | |
1738 | ||
1739 | /* Just skip everything for an empty queue. */ | |
1740 | if (!wait_queue->count) { | |
1741 | goto end; | |
1742 | } | |
1743 | ||
1744 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); | |
1745 | if (ret < 0) { | |
1746 | goto error_create; | |
1747 | } | |
1748 | ||
1749 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1750 | &wait_queue->head, head) { | |
1751 | assert(wait_node->app); | |
1752 | ret = lttng_poll_add(&events, wait_node->app->sock, | |
1753 | LPOLLHUP | LPOLLERR); | |
1754 | if (ret < 0) { | |
1755 | goto error; | |
1756 | } | |
1757 | ||
1758 | fd_added = 1; | |
1759 | } | |
1760 | ||
1761 | if (!fd_added) { | |
1762 | goto end; | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | * Poll but don't block so we can quickly identify the faulty events and | |
1767 | * clean them afterwards from the wait queue. | |
1768 | */ | |
1769 | ret = lttng_poll_wait(&events, 0); | |
1770 | if (ret < 0) { | |
1771 | goto error; | |
1772 | } | |
1773 | nb_fd = ret; | |
1774 | ||
1775 | for (i = 0; i < nb_fd; i++) { | |
1776 | /* Get faulty FD. */ | |
1777 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
1778 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
1779 | ||
fd20dac9 MD |
1780 | if (!revents) { |
1781 | /* No activity for this FD (poll implementation). */ | |
1782 | continue; | |
1783 | } | |
1784 | ||
f45e313d DG |
1785 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
1786 | &wait_queue->head, head) { | |
1787 | if (pollfd == wait_node->app->sock && | |
1788 | (revents & (LPOLLHUP | LPOLLERR))) { | |
1789 | cds_list_del(&wait_node->head); | |
1790 | wait_queue->count--; | |
1791 | ust_app_destroy(wait_node->app); | |
1792 | free(wait_node); | |
48b40bcf JG |
1793 | /* |
1794 | * Silence warning of use-after-free in | |
1795 | * cds_list_for_each_entry_safe which uses | |
1796 | * __typeof__(*wait_node). | |
1797 | */ | |
1798 | wait_node = NULL; | |
f45e313d | 1799 | break; |
03e43155 MD |
1800 | } else { |
1801 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1802 | goto error; | |
f45e313d DG |
1803 | } |
1804 | } | |
1805 | } | |
1806 | ||
1807 | if (nb_fd > 0) { | |
1808 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); | |
1809 | } | |
1810 | ||
1811 | end: | |
1812 | lttng_poll_clean(&events); | |
1813 | return; | |
1814 | ||
1815 | error: | |
1816 | lttng_poll_clean(&events); | |
1817 | error_create: | |
1818 | ERR("Unable to sanitize wait queue"); | |
1819 | return; | |
1820 | } | |
1821 | ||
099e26bd DG |
1822 | /* |
1823 | * Dispatch request from the registration threads to the application | |
1824 | * communication thread. | |
1825 | */ | |
1826 | static void *thread_dispatch_ust_registration(void *data) | |
1827 | { | |
12e2b881 | 1828 | int ret, err = -1; |
8bdee6e2 | 1829 | struct cds_wfcq_node *node; |
099e26bd | 1830 | struct ust_command *ust_cmd = NULL; |
f45e313d DG |
1831 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
1832 | struct ust_reg_wait_queue wait_queue = { | |
1833 | .count = 0, | |
1834 | }; | |
d0b96690 | 1835 | |
967e3668 MD |
1836 | rcu_register_thread(); |
1837 | ||
6c71277b | 1838 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
12e2b881 | 1839 | |
9ad42ec1 MD |
1840 | if (testpoint(sessiond_thread_app_reg_dispatch)) { |
1841 | goto error_testpoint; | |
1842 | } | |
1843 | ||
12e2b881 MD |
1844 | health_code_update(); |
1845 | ||
f45e313d | 1846 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
099e26bd DG |
1847 | |
1848 | DBG("[thread] Dispatch UST command started"); | |
1849 | ||
0ed3b1a8 | 1850 | for (;;) { |
12e2b881 MD |
1851 | health_code_update(); |
1852 | ||
099e26bd DG |
1853 | /* Atomically prepare the queue futex */ |
1854 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1855 | ||
0ed3b1a8 MD |
1856 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1857 | break; | |
1858 | } | |
1859 | ||
099e26bd | 1860 | do { |
d0b96690 | 1861 | struct ust_app *app = NULL; |
7972aab2 | 1862 | ust_cmd = NULL; |
d0b96690 | 1863 | |
f45e313d DG |
1864 | /* |
1865 | * Make sure we don't have node(s) that have hung up before receiving | |
1866 | * the notify socket. This is to clean the list in order to avoid | |
1867 | * memory leaks from notify socket that are never seen. | |
1868 | */ | |
1869 | sanitize_wait_queue(&wait_queue); | |
1870 | ||
12e2b881 | 1871 | health_code_update(); |
099e26bd | 1872 | /* Dequeue command for registration */ |
8bdee6e2 | 1873 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1874 | if (node == NULL) { |
00a17c97 | 1875 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1876 | /* Continue thread execution */ |
1877 | break; | |
1878 | } | |
1879 | ||
1880 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1881 | ||
2f50c8a3 DG |
1882 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1883 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1884 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1885 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1886 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1887 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1888 | |
1889 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1890 | wait_node = zmalloc(sizeof(*wait_node)); | |
1891 | if (!wait_node) { | |
1892 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1893 | ret = close(ust_cmd->sock); |
1894 | if (ret < 0) { | |
1895 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1896 | } | |
51dec90d | 1897 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1898 | free(ust_cmd); |
d0b96690 DG |
1899 | goto error; |
1900 | } | |
1901 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1902 | ||
1903 | /* Create application object if socket is CMD. */ | |
1904 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1905 | ust_cmd->sock); | |
1906 | if (!wait_node->app) { | |
1907 | ret = close(ust_cmd->sock); | |
1908 | if (ret < 0) { | |
1909 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1910 | } |
51dec90d | 1911 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1912 | free(wait_node); |
7972aab2 | 1913 | free(ust_cmd); |
d0b96690 DG |
1914 | continue; |
1915 | } | |
1916 | /* | |
1917 | * Add application to the wait queue so we can set the notify | |
1918 | * socket before putting this object in the global ht. | |
1919 | */ | |
f45e313d DG |
1920 | cds_list_add(&wait_node->head, &wait_queue.head); |
1921 | wait_queue.count++; | |
d0b96690 | 1922 | |
7972aab2 | 1923 | free(ust_cmd); |
d0b96690 DG |
1924 | /* |
1925 | * We have to continue here since we don't have the notify | |
1926 | * socket and the application MUST be added to the hash table | |
1927 | * only at that moment. | |
1928 | */ | |
1929 | continue; | |
1930 | } else { | |
1931 | /* | |
1932 | * Look for the application in the local wait queue and set the | |
1933 | * notify socket if found. | |
1934 | */ | |
d88aee68 | 1935 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1936 | &wait_queue.head, head) { |
12e2b881 | 1937 | health_code_update(); |
d0b96690 DG |
1938 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1939 | wait_node->app->notify_sock = ust_cmd->sock; | |
1940 | cds_list_del(&wait_node->head); | |
f45e313d | 1941 | wait_queue.count--; |
d0b96690 DG |
1942 | app = wait_node->app; |
1943 | free(wait_node); | |
1944 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1945 | break; | |
1946 | } | |
1947 | } | |
020d7f60 DG |
1948 | |
1949 | /* | |
1950 | * With no application at this stage the received socket is | |
1951 | * basically useless so close it before we free the cmd data | |
1952 | * structure for good. | |
1953 | */ | |
1954 | if (!app) { | |
1955 | ret = close(ust_cmd->sock); | |
1956 | if (ret < 0) { | |
1957 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1958 | } | |
51dec90d | 1959 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1960 | } |
7972aab2 | 1961 | free(ust_cmd); |
d0b96690 DG |
1962 | } |
1963 | ||
1964 | if (app) { | |
d0b96690 DG |
1965 | /* |
1966 | * @session_lock_list | |
1967 | * | |
1968 | * Lock the global session list so from the register up to the | |
1969 | * registration done message, no thread can see the application | |
1970 | * and change its state. | |
1971 | */ | |
1972 | session_lock_list(); | |
1973 | rcu_read_lock(); | |
d88aee68 | 1974 | |
d0b96690 DG |
1975 | /* |
1976 | * Add application to the global hash table. This needs to be | |
1977 | * done before the update to the UST registry can locate the | |
1978 | * application. | |
1979 | */ | |
1980 | ust_app_add(app); | |
d88aee68 DG |
1981 | |
1982 | /* Set app version. This call will print an error if needed. */ | |
1983 | (void) ust_app_version(app); | |
1984 | ||
1985 | /* Send notify socket through the notify pipe. */ | |
1986 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
1987 | app->notify_sock); | |
1988 | if (ret < 0) { | |
1989 | rcu_read_unlock(); | |
1990 | session_unlock_list(); | |
b85dc84c DG |
1991 | /* |
1992 | * No notify thread, stop the UST tracing. However, this is | |
1993 | * not an internal error of the this thread thus setting | |
1994 | * the health error code to a normal exit. | |
1995 | */ | |
1996 | err = 0; | |
d88aee68 | 1997 | goto error; |
6620da75 | 1998 | } |
d88aee68 | 1999 | |
d0b96690 DG |
2000 | /* |
2001 | * Update newly registered application with the tracing | |
2002 | * registry info already enabled information. | |
2003 | */ | |
2004 | update_ust_app(app->sock); | |
d88aee68 DG |
2005 | |
2006 | /* | |
2007 | * Don't care about return value. Let the manage apps threads | |
2008 | * handle app unregistration upon socket close. | |
2009 | */ | |
fb45065e | 2010 | (void) ust_app_register_done(app); |
d88aee68 DG |
2011 | |
2012 | /* | |
2013 | * Even if the application socket has been closed, send the app | |
2014 | * to the thread and unregistration will take place at that | |
2015 | * place. | |
2016 | */ | |
2017 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 2018 | if (ret < 0) { |
d88aee68 DG |
2019 | rcu_read_unlock(); |
2020 | session_unlock_list(); | |
b85dc84c DG |
2021 | /* |
2022 | * No apps. thread, stop the UST tracing. However, this is | |
2023 | * not an internal error of the this thread thus setting | |
2024 | * the health error code to a normal exit. | |
2025 | */ | |
2026 | err = 0; | |
d88aee68 | 2027 | goto error; |
d0b96690 | 2028 | } |
d88aee68 | 2029 | |
d0b96690 DG |
2030 | rcu_read_unlock(); |
2031 | session_unlock_list(); | |
099e26bd | 2032 | } |
099e26bd DG |
2033 | } while (node != NULL); |
2034 | ||
12e2b881 | 2035 | health_poll_entry(); |
099e26bd DG |
2036 | /* Futex wait on queue. Blocking call on futex() */ |
2037 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 2038 | health_poll_exit(); |
099e26bd | 2039 | } |
12e2b881 MD |
2040 | /* Normal exit, no error */ |
2041 | err = 0; | |
099e26bd DG |
2042 | |
2043 | error: | |
d88aee68 DG |
2044 | /* Clean up wait queue. */ |
2045 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 2046 | &wait_queue.head, head) { |
d88aee68 | 2047 | cds_list_del(&wait_node->head); |
f45e313d | 2048 | wait_queue.count--; |
d88aee68 DG |
2049 | free(wait_node); |
2050 | } | |
2051 | ||
772b8f4d MD |
2052 | /* Empty command queue. */ |
2053 | for (;;) { | |
2054 | /* Dequeue command for registration */ | |
2055 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); | |
2056 | if (node == NULL) { | |
2057 | break; | |
2058 | } | |
2059 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
2060 | ret = close(ust_cmd->sock); | |
2061 | if (ret < 0) { | |
2062 | PERROR("close ust sock exit dispatch %d", ust_cmd->sock); | |
2063 | } | |
2064 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2065 | free(ust_cmd); | |
2066 | } | |
2067 | ||
9ad42ec1 | 2068 | error_testpoint: |
099e26bd | 2069 | DBG("Dispatch thread dying"); |
12e2b881 MD |
2070 | if (err) { |
2071 | health_error(); | |
2072 | ERR("Health error occurred in %s", __func__); | |
2073 | } | |
8782cc74 | 2074 | health_unregister(health_sessiond); |
967e3668 | 2075 | rcu_unregister_thread(); |
099e26bd DG |
2076 | return NULL; |
2077 | } | |
2078 | ||
2079 | /* | |
2080 | * This thread manage application registration. | |
2081 | */ | |
2082 | static void *thread_registration_apps(void *data) | |
1d4b027a | 2083 | { |
139ac872 | 2084 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
2085 | uint32_t revents, nb_fd; |
2086 | struct lttng_poll_event events; | |
099e26bd DG |
2087 | /* |
2088 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
2089 | * freed in the manage apps thread. | |
2090 | */ | |
2091 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 2092 | |
099e26bd | 2093 | DBG("[thread] Manage application registration started"); |
1d4b027a | 2094 | |
6c71277b | 2095 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 2096 | |
e547b070 | 2097 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
2098 | goto error_testpoint; |
2099 | } | |
8ac94142 | 2100 | |
1d4b027a DG |
2101 | ret = lttcomm_listen_unix_sock(apps_sock); |
2102 | if (ret < 0) { | |
76d7553f | 2103 | goto error_listen; |
1d4b027a DG |
2104 | } |
2105 | ||
5eb91c98 DG |
2106 | /* |
2107 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
2108 | * more will be added to this poll set. | |
2109 | */ | |
d0b96690 | 2110 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 2111 | if (ret < 0) { |
76d7553f | 2112 | goto error_create_poll; |
5eb91c98 | 2113 | } |
273ea72c | 2114 | |
5eb91c98 DG |
2115 | /* Add the application registration socket */ |
2116 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
2117 | if (ret < 0) { | |
76d7553f | 2118 | goto error_poll_add; |
5eb91c98 | 2119 | } |
273ea72c | 2120 | |
1d4b027a | 2121 | /* Notify all applications to register */ |
0fdd1e2c DG |
2122 | ret = notify_ust_apps(1); |
2123 | if (ret < 0) { | |
2124 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
2125 | "Execution continues but there might be problem for already\n" |
2126 | "running applications that wishes to register."); | |
0fdd1e2c | 2127 | } |
1d4b027a DG |
2128 | |
2129 | while (1) { | |
2130 | DBG("Accepting application registration"); | |
273ea72c DG |
2131 | |
2132 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 2133 | restart: |
a78af745 | 2134 | health_poll_entry(); |
5eb91c98 | 2135 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 2136 | health_poll_exit(); |
273ea72c | 2137 | if (ret < 0) { |
88f2b785 MD |
2138 | /* |
2139 | * Restart interrupted system call. | |
2140 | */ | |
2141 | if (errno == EINTR) { | |
2142 | goto restart; | |
2143 | } | |
273ea72c DG |
2144 | goto error; |
2145 | } | |
2146 | ||
0d9c5d77 DG |
2147 | nb_fd = ret; |
2148 | ||
5eb91c98 | 2149 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 2150 | health_code_update(); |
139ac872 | 2151 | |
5eb91c98 DG |
2152 | /* Fetch once the poll data */ |
2153 | revents = LTTNG_POLL_GETEV(&events, i); | |
2154 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 2155 | |
fd20dac9 MD |
2156 | if (!revents) { |
2157 | /* No activity for this FD (poll implementation). */ | |
2158 | continue; | |
2159 | } | |
2160 | ||
5eb91c98 | 2161 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 2162 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 2163 | if (ret) { |
139ac872 MD |
2164 | err = 0; |
2165 | goto exit; | |
90014c57 | 2166 | } |
1d4b027a | 2167 | |
5eb91c98 DG |
2168 | /* Event on the registration socket */ |
2169 | if (pollfd == apps_sock) { | |
03e43155 | 2170 | if (revents & LPOLLIN) { |
5eb91c98 DG |
2171 | sock = lttcomm_accept_unix_sock(apps_sock); |
2172 | if (sock < 0) { | |
2173 | goto error; | |
2174 | } | |
099e26bd | 2175 | |
16c5c8fa DG |
2176 | /* |
2177 | * Set socket timeout for both receiving and ending. | |
2178 | * app_socket_timeout is in seconds, whereas | |
2179 | * lttcomm_setsockopt_rcv_timeout and | |
2180 | * lttcomm_setsockopt_snd_timeout expect msec as | |
2181 | * parameter. | |
2182 | */ | |
e6142f2e | 2183 | if (config.app_socket_timeout >= 0) { |
28ce0ff2 | 2184 | (void) lttcomm_setsockopt_rcv_timeout(sock, |
e6142f2e | 2185 | config.app_socket_timeout * 1000); |
28ce0ff2 | 2186 | (void) lttcomm_setsockopt_snd_timeout(sock, |
e6142f2e | 2187 | config.app_socket_timeout * 1000); |
28ce0ff2 | 2188 | } |
16c5c8fa | 2189 | |
b662582b DG |
2190 | /* |
2191 | * Set the CLOEXEC flag. Return code is useless because | |
2192 | * either way, the show must go on. | |
2193 | */ | |
2194 | (void) utils_set_fd_cloexec(sock); | |
2195 | ||
5eb91c98 | 2196 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 2197 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 2198 | if (ust_cmd == NULL) { |
76d7553f | 2199 | PERROR("ust command zmalloc"); |
41ed8e47 MD |
2200 | ret = close(sock); |
2201 | if (ret) { | |
2202 | PERROR("close"); | |
2203 | } | |
5eb91c98 DG |
2204 | goto error; |
2205 | } | |
1d4b027a | 2206 | |
5eb91c98 DG |
2207 | /* |
2208 | * Using message-based transmissions to ensure we don't | |
2209 | * have to deal with partially received messages. | |
2210 | */ | |
4063050c MD |
2211 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
2212 | if (ret < 0) { | |
2213 | ERR("Exhausted file descriptors allowed for applications."); | |
2214 | free(ust_cmd); | |
2215 | ret = close(sock); | |
2216 | if (ret) { | |
2217 | PERROR("close"); | |
2218 | } | |
2219 | sock = -1; | |
2220 | continue; | |
2221 | } | |
d88aee68 | 2222 | |
840cb59c | 2223 | health_code_update(); |
d0b96690 DG |
2224 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
2225 | if (ret < 0) { | |
5eb91c98 | 2226 | free(ust_cmd); |
d0b96690 | 2227 | /* Close socket of the application. */ |
76d7553f MD |
2228 | ret = close(sock); |
2229 | if (ret) { | |
2230 | PERROR("close"); | |
2231 | } | |
4063050c | 2232 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 2233 | sock = -1; |
5eb91c98 DG |
2234 | continue; |
2235 | } | |
840cb59c | 2236 | health_code_update(); |
099e26bd | 2237 | |
5eb91c98 | 2238 | ust_cmd->sock = sock; |
34a2494f | 2239 | sock = -1; |
099e26bd | 2240 | |
5eb91c98 DG |
2241 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
2242 | " gid:%d sock:%d name:%s (version %d.%d)", | |
2243 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
2244 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
2245 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
2246 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 2247 | |
5eb91c98 DG |
2248 | /* |
2249 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 2250 | * has been taken! This apps will be part of the *system*. |
5eb91c98 | 2251 | */ |
8bdee6e2 | 2252 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
5eb91c98 DG |
2253 | |
2254 | /* | |
2255 | * Wake the registration queue futex. Implicit memory | |
8bdee6e2 | 2256 | * barrier with the exchange in cds_wfcq_enqueue. |
5eb91c98 DG |
2257 | */ |
2258 | futex_nto1_wake(&ust_cmd_queue.futex); | |
03e43155 MD |
2259 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2260 | ERR("Register apps socket poll error"); | |
2261 | goto error; | |
2262 | } else { | |
2263 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2264 | goto error; | |
5eb91c98 DG |
2265 | } |
2266 | } | |
90014c57 | 2267 | } |
1d4b027a DG |
2268 | } |
2269 | ||
139ac872 | 2270 | exit: |
1d4b027a | 2271 | error: |
0fdd1e2c DG |
2272 | /* Notify that the registration thread is gone */ |
2273 | notify_ust_apps(0); | |
2274 | ||
a4b35e07 | 2275 | if (apps_sock >= 0) { |
76d7553f MD |
2276 | ret = close(apps_sock); |
2277 | if (ret) { | |
2278 | PERROR("close"); | |
2279 | } | |
a4b35e07 | 2280 | } |
46c3f085 | 2281 | if (sock >= 0) { |
76d7553f MD |
2282 | ret = close(sock); |
2283 | if (ret) { | |
2284 | PERROR("close"); | |
2285 | } | |
4063050c | 2286 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 2287 | } |
e6142f2e | 2288 | unlink(config.apps_unix_sock_path.value); |
0fdd1e2c | 2289 | |
76d7553f | 2290 | error_poll_add: |
5eb91c98 | 2291 | lttng_poll_clean(&events); |
76d7553f MD |
2292 | error_listen: |
2293 | error_create_poll: | |
6993eeb3 | 2294 | error_testpoint: |
76d7553f | 2295 | DBG("UST Registration thread cleanup complete"); |
9ad42ec1 MD |
2296 | if (err) { |
2297 | health_error(); | |
2298 | ERR("Health error occurred in %s", __func__); | |
2299 | } | |
8782cc74 | 2300 | health_unregister(health_sessiond); |
5eb91c98 | 2301 | |
1d4b027a DG |
2302 | return NULL; |
2303 | } | |
2304 | ||
8c0faa1d | 2305 | /* |
3bd1e081 | 2306 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 2307 | * exec or it will fails. |
8c0faa1d | 2308 | */ |
3bd1e081 | 2309 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 2310 | { |
a23ec3a7 | 2311 | int ret, clock_ret; |
ee0b0061 DG |
2312 | struct timespec timeout; |
2313 | ||
13a7bce3 JG |
2314 | /* |
2315 | * Make sure we set the readiness flag to 0 because we are NOT ready. | |
2316 | * This access to consumer_thread_is_ready does not need to be | |
2317 | * protected by consumer_data.cond_mutex (yet) since the consumer | |
2318 | * management thread has not been started at this point. | |
2319 | */ | |
a23ec3a7 | 2320 | consumer_data->consumer_thread_is_ready = 0; |
8c0faa1d | 2321 | |
a23ec3a7 DG |
2322 | /* Setup pthread condition */ |
2323 | ret = pthread_condattr_init(&consumer_data->condattr); | |
4a15001e | 2324 | if (ret) { |
a23ec3a7 DG |
2325 | errno = ret; |
2326 | PERROR("pthread_condattr_init consumer data"); | |
2327 | goto error; | |
2328 | } | |
2329 | ||
2330 | /* | |
2331 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
2332 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
2333 | * for a more details and how we noticed it. | |
2334 | */ | |
2335 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
4a15001e | 2336 | if (ret) { |
a23ec3a7 DG |
2337 | errno = ret; |
2338 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
2339 | goto error; |
2340 | } | |
8c0faa1d | 2341 | |
a23ec3a7 | 2342 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
4a15001e | 2343 | if (ret) { |
a23ec3a7 DG |
2344 | errno = ret; |
2345 | PERROR("pthread_cond_init consumer data"); | |
2346 | goto error; | |
2347 | } | |
2348 | ||
1a1a34b4 MJ |
2349 | ret = pthread_create(&consumer_data->thread, default_pthread_attr(), |
2350 | thread_manage_consumer, consumer_data); | |
4a15001e MD |
2351 | if (ret) { |
2352 | errno = ret; | |
3bd1e081 | 2353 | PERROR("pthread_create consumer"); |
ee0b0061 | 2354 | ret = -1; |
8c0faa1d DG |
2355 | goto error; |
2356 | } | |
2357 | ||
a23ec3a7 DG |
2358 | /* We are about to wait on a pthread condition */ |
2359 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2360 | ||
ee0b0061 | 2361 | /* Get time for sem_timedwait absolute timeout */ |
389fbf04 | 2362 | clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout); |
a23ec3a7 DG |
2363 | /* |
2364 | * Set the timeout for the condition timed wait even if the clock gettime | |
2365 | * call fails since we might loop on that call and we want to avoid to | |
2366 | * increment the timeout too many times. | |
2367 | */ | |
2368 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2369 | ||
2370 | /* | |
2371 | * The following loop COULD be skipped in some conditions so this is why we | |
2372 | * set ret to 0 in order to make sure at least one round of the loop is | |
2373 | * done. | |
2374 | */ | |
2375 | ret = 0; | |
2376 | ||
2377 | /* | |
2378 | * Loop until the condition is reached or when a timeout is reached. Note | |
2379 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2380 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2381 | * possible. This loop does not take any chances and works with both of | |
2382 | * them. | |
2383 | */ | |
2384 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2385 | if (clock_ret < 0) { | |
2386 | PERROR("clock_gettime spawn consumer"); | |
2387 | /* Infinite wait for the consumerd thread to be ready */ | |
2388 | ret = pthread_cond_wait(&consumer_data->cond, | |
2389 | &consumer_data->cond_mutex); | |
2390 | } else { | |
2391 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2392 | &consumer_data->cond_mutex, &timeout); | |
2393 | } | |
ee0b0061 | 2394 | } |
8c0faa1d | 2395 | |
a23ec3a7 DG |
2396 | /* Release the pthread condition */ |
2397 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2398 | ||
2399 | if (ret != 0) { | |
2400 | errno = ret; | |
2401 | if (ret == ETIMEDOUT) { | |
4282f9a3 DG |
2402 | int pth_ret; |
2403 | ||
ee0b0061 DG |
2404 | /* |
2405 | * Call has timed out so we kill the kconsumerd_thread and return | |
2406 | * an error. | |
2407 | */ | |
a23ec3a7 DG |
2408 | ERR("Condition timed out. The consumer thread was never ready." |
2409 | " Killing it"); | |
4282f9a3 DG |
2410 | pth_ret = pthread_cancel(consumer_data->thread); |
2411 | if (pth_ret < 0) { | |
3bd1e081 | 2412 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2413 | } |
2414 | } else { | |
a23ec3a7 | 2415 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 | 2416 | } |
4282f9a3 DG |
2417 | /* Caller is expecting a negative value on failure. */ |
2418 | ret = -1; | |
ee0b0061 DG |
2419 | goto error; |
2420 | } | |
2421 | ||
3bd1e081 MD |
2422 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2423 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2424 | ERR("Consumerd did not start"); |
3bd1e081 | 2425 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2426 | goto error; |
2427 | } | |
3bd1e081 | 2428 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2429 | |
8c0faa1d DG |
2430 | return 0; |
2431 | ||
2432 | error: | |
2433 | return ret; | |
2434 | } | |
2435 | ||
d9800920 | 2436 | /* |
3bd1e081 | 2437 | * Join consumer thread |
d9800920 | 2438 | */ |
3bd1e081 | 2439 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2440 | { |
2441 | void *status; | |
cf3af59e | 2442 | |
e8209f6b DG |
2443 | /* Consumer pid must be a real one. */ |
2444 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2445 | int ret; |
3bd1e081 | 2446 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2447 | if (ret) { |
4a15001e | 2448 | PERROR("Error killing consumer daemon"); |
cf3af59e MD |
2449 | return ret; |
2450 | } | |
3bd1e081 | 2451 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2452 | } else { |
2453 | return 0; | |
2454 | } | |
2455 | } | |
2456 | ||
8c0faa1d | 2457 | /* |
3bd1e081 | 2458 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2459 | * |
d063d709 | 2460 | * Return pid if successful else -1. |
8c0faa1d | 2461 | */ |
3bd1e081 | 2462 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2463 | { |
2464 | int ret; | |
2465 | pid_t pid; | |
94c55f17 | 2466 | const char *consumer_to_use; |
53086306 | 2467 | const char *verbosity; |
94c55f17 | 2468 | struct stat st; |
8c0faa1d | 2469 | |
3bd1e081 | 2470 | DBG("Spawning consumerd"); |
c49dc785 | 2471 | |
8c0faa1d DG |
2472 | pid = fork(); |
2473 | if (pid == 0) { | |
2474 | /* | |
3bd1e081 | 2475 | * Exec consumerd. |
8c0faa1d | 2476 | */ |
e6142f2e | 2477 | if (config.verbose_consumer) { |
53086306 | 2478 | verbosity = "--verbose"; |
4421f712 | 2479 | } else if (lttng_opt_quiet) { |
53086306 | 2480 | verbosity = "--quiet"; |
4421f712 DG |
2481 | } else { |
2482 | verbosity = ""; | |
53086306 | 2483 | } |
4421f712 | 2484 | |
3bd1e081 MD |
2485 | switch (consumer_data->type) { |
2486 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2487 | /* |
c7704d57 DG |
2488 | * Find out which consumerd to execute. We will first try the |
2489 | * 64-bit path, then the sessiond's installation directory, and | |
2490 | * fallback on the 32-bit one, | |
94c55f17 | 2491 | */ |
63a799e8 | 2492 | DBG3("Looking for a kernel consumer at these locations:"); |
59ee5091 | 2493 | DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL"); |
e6142f2e | 2494 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE); |
59ee5091 | 2495 | DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL"); |
e6142f2e | 2496 | if (stat(config.consumerd64_bin_path.value, &st) == 0) { |
63a799e8 | 2497 | DBG3("Found location #1"); |
e6142f2e JG |
2498 | consumer_to_use = config.consumerd64_bin_path.value; |
2499 | } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) { | |
63a799e8 | 2500 | DBG3("Found location #2"); |
e6142f2e JG |
2501 | consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE; |
2502 | } else if (stat(config.consumerd32_bin_path.value, &st) == 0) { | |
63a799e8 | 2503 | DBG3("Found location #3"); |
e6142f2e | 2504 | consumer_to_use = config.consumerd32_bin_path.value; |
94c55f17 | 2505 | } else { |
63a799e8 | 2506 | DBG("Could not find any valid consumerd executable"); |
4282f9a3 | 2507 | ret = -EINVAL; |
3d678709 | 2508 | goto error; |
94c55f17 AM |
2509 | } |
2510 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
6e2cc8d8 | 2511 | (void) execl(consumer_to_use, |
94c55f17 AM |
2512 | "lttng-consumerd", verbosity, "-k", |
2513 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2514 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2515 | "--group", config.tracing_group_name.value, |
94c55f17 | 2516 | NULL); |
3bd1e081 | 2517 | break; |
7753dea8 MD |
2518 | case LTTNG_CONSUMER64_UST: |
2519 | { | |
46b23495 | 2520 | if (config.consumerd64_lib_dir.value) { |
8f4905da MD |
2521 | char *tmp; |
2522 | size_t tmplen; | |
ddaec4a2 | 2523 | char *tmpnew; |
8f4905da | 2524 | |
e8fa9fb0 | 2525 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2526 | if (!tmp) { |
2527 | tmp = ""; | |
2528 | } | |
d222983e | 2529 | tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp); |
8f4905da MD |
2530 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
2531 | if (!tmpnew) { | |
2532 | ret = -ENOMEM; | |
2533 | goto error; | |
2534 | } | |
e6142f2e | 2535 | strcat(tmpnew, config.consumerd64_lib_dir.value); |
8f4905da MD |
2536 | if (tmp[0] != '\0') { |
2537 | strcat(tmpnew, ":"); | |
2538 | strcat(tmpnew, tmp); | |
2539 | } | |
d222983e | 2540 | ret = setenv("LD_LIBRARY_PATH", tmpnew, 1); |
ddaec4a2 | 2541 | free(tmpnew); |
8f4905da MD |
2542 | if (ret) { |
2543 | ret = -errno; | |
2544 | goto error; | |
2545 | } | |
2546 | } | |
e6142f2e JG |
2547 | DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value); |
2548 | (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u", | |
7753dea8 MD |
2549 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2550 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2551 | "--group", config.tracing_group_name.value, |
7753dea8 | 2552 | NULL); |
3bd1e081 | 2553 | break; |
7753dea8 MD |
2554 | } |
2555 | case LTTNG_CONSUMER32_UST: | |
2556 | { | |
46b23495 | 2557 | if (config.consumerd32_lib_dir.value) { |
8f4905da MD |
2558 | char *tmp; |
2559 | size_t tmplen; | |
ddaec4a2 | 2560 | char *tmpnew; |
8f4905da | 2561 | |
e8fa9fb0 | 2562 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2563 | if (!tmp) { |
2564 | tmp = ""; | |
2565 | } | |
d222983e | 2566 | tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp); |
8f4905da MD |
2567 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
2568 | if (!tmpnew) { | |
2569 | ret = -ENOMEM; | |
2570 | goto error; | |
2571 | } | |
e6142f2e | 2572 | strcat(tmpnew, config.consumerd32_lib_dir.value); |
8f4905da MD |
2573 | if (tmp[0] != '\0') { |
2574 | strcat(tmpnew, ":"); | |
2575 | strcat(tmpnew, tmp); | |
2576 | } | |
d222983e | 2577 | ret = setenv("LD_LIBRARY_PATH", tmpnew, 1); |
ddaec4a2 | 2578 | free(tmpnew); |
8f4905da MD |
2579 | if (ret) { |
2580 | ret = -errno; | |
2581 | goto error; | |
2582 | } | |
2583 | } | |
e6142f2e JG |
2584 | DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value); |
2585 | (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u", | |
7753dea8 MD |
2586 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2587 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2588 | "--group", config.tracing_group_name.value, |
7753dea8 MD |
2589 | NULL); |
2590 | break; | |
2591 | } | |
3bd1e081 | 2592 | default: |
b60e7183 | 2593 | ERR("unknown consumer type"); |
e9d6b496 | 2594 | errno = 0; |
3bd1e081 | 2595 | } |
8c0faa1d | 2596 | if (errno != 0) { |
4282f9a3 | 2597 | PERROR("Consumer execl()"); |
8c0faa1d | 2598 | } |
4282f9a3 | 2599 | /* Reaching this point, we got a failure on our execl(). */ |
8c0faa1d DG |
2600 | exit(EXIT_FAILURE); |
2601 | } else if (pid > 0) { | |
2602 | ret = pid; | |
8c0faa1d | 2603 | } else { |
76d7553f | 2604 | PERROR("start consumer fork"); |
8c0faa1d | 2605 | ret = -errno; |
8c0faa1d | 2606 | } |
8f4905da | 2607 | error: |
8c0faa1d DG |
2608 | return ret; |
2609 | } | |
2610 | ||
693bd40b | 2611 | /* |
3bd1e081 | 2612 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2613 | */ |
3bd1e081 | 2614 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2615 | { |
c617c0c6 | 2616 | int ret; |
edb8b045 DG |
2617 | |
2618 | /* | |
2619 | * Set the listen() state on the socket since there is a possible race | |
2620 | * between the exec() of the consumer daemon and this call if place in the | |
2621 | * consumer thread. See bug #366 for more details. | |
2622 | */ | |
2623 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2624 | if (ret < 0) { | |
2625 | goto error; | |
2626 | } | |
693bd40b | 2627 | |
3bd1e081 MD |
2628 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2629 | if (consumer_data->pid != 0) { | |
2630 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2631 | goto end; |
2632 | } | |
693bd40b | 2633 | |
3bd1e081 | 2634 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2635 | if (ret < 0) { |
3bd1e081 MD |
2636 | ERR("Spawning consumerd failed"); |
2637 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2638 | goto error; |
693bd40b | 2639 | } |
c49dc785 | 2640 | |
3bd1e081 MD |
2641 | /* Setting up the consumer_data pid */ |
2642 | consumer_data->pid = ret; | |
48842b30 | 2643 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2644 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2645 | |
3bd1e081 MD |
2646 | DBG2("Spawning consumer control thread"); |
2647 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2648 | if (ret < 0) { |
3bd1e081 | 2649 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2650 | goto error; |
2651 | } | |
2652 | ||
c49dc785 | 2653 | end: |
693bd40b DG |
2654 | return 0; |
2655 | ||
2656 | error: | |
331744e3 | 2657 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2658 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2659 | int err; |
2660 | ||
edb8b045 DG |
2661 | err = close(consumer_data->err_sock); |
2662 | if (err < 0) { | |
2663 | PERROR("close consumer data error socket"); | |
2664 | } | |
2665 | } | |
693bd40b DG |
2666 | return ret; |
2667 | } | |
2668 | ||
b73401da | 2669 | /* |
096102bd | 2670 | * Setup necessary data for kernel tracer action. |
b73401da | 2671 | */ |
096102bd | 2672 | static int init_kernel_tracer(void) |
b73401da DG |
2673 | { |
2674 | int ret; | |
b73401da | 2675 | |
096102bd DG |
2676 | /* Modprobe lttng kernel modules */ |
2677 | ret = modprobe_lttng_control(); | |
b73401da | 2678 | if (ret < 0) { |
b73401da DG |
2679 | goto error; |
2680 | } | |
2681 | ||
096102bd DG |
2682 | /* Open debugfs lttng */ |
2683 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2684 | if (kernel_tracer_fd < 0) { | |
2685 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 2686 | goto error_open; |
54d01ffb DG |
2687 | } |
2688 | ||
2f77fc4b | 2689 | /* Validate kernel version */ |
88076e89 JD |
2690 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
2691 | &kernel_tracer_abi_version); | |
2f77fc4b DG |
2692 | if (ret < 0) { |
2693 | goto error_version; | |
b551a063 | 2694 | } |
54d01ffb | 2695 | |
2f77fc4b DG |
2696 | ret = modprobe_lttng_data(); |
2697 | if (ret < 0) { | |
2698 | goto error_modules; | |
54d01ffb DG |
2699 | } |
2700 | ||
6e21424e JR |
2701 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
2702 | kernel_tracer_fd); | |
2703 | if (ret < 0) { | |
2704 | goto error_modules; | |
2705 | } | |
2706 | ||
2707 | if (ret < 1) { | |
2708 | WARN("Kernel tracer does not support buffer monitoring. " | |
2709 | "The monitoring timer of channels in the kernel domain " | |
2710 | "will be set to 0 (disabled)."); | |
2711 | } | |
2712 | ||
2f77fc4b DG |
2713 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2714 | return 0; | |
2715 | ||
2716 | error_version: | |
2717 | modprobe_remove_lttng_control(); | |
2718 | ret = close(kernel_tracer_fd); | |
2719 | if (ret) { | |
2720 | PERROR("close"); | |
b551a063 | 2721 | } |
2f77fc4b | 2722 | kernel_tracer_fd = -1; |
f73fabfd | 2723 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2724 | |
2f77fc4b DG |
2725 | error_modules: |
2726 | ret = close(kernel_tracer_fd); | |
2727 | if (ret) { | |
2728 | PERROR("close"); | |
b551a063 | 2729 | } |
54d01ffb | 2730 | |
2f77fc4b DG |
2731 | error_open: |
2732 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2733 | |
2734 | error: | |
2f77fc4b DG |
2735 | WARN("No kernel tracer available"); |
2736 | kernel_tracer_fd = -1; | |
2737 | if (!is_root) { | |
f73fabfd | 2738 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2739 | } else { |
f73fabfd | 2740 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2741 | } |
54d01ffb DG |
2742 | } |
2743 | ||
2f77fc4b | 2744 | |
54d01ffb | 2745 | /* |
2f77fc4b DG |
2746 | * Copy consumer output from the tracing session to the domain session. The |
2747 | * function also applies the right modification on a per domain basis for the | |
2748 | * trace files destination directory. | |
36b588ed MD |
2749 | * |
2750 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2751 | */ |
2f77fc4b | 2752 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2753 | { |
2754 | int ret; | |
2f77fc4b DG |
2755 | const char *dir_name; |
2756 | struct consumer_output *consumer; | |
2757 | ||
2758 | assert(session); | |
2759 | assert(session->consumer); | |
54d01ffb | 2760 | |
b551a063 DG |
2761 | switch (domain) { |
2762 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2763 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2764 | /* |
2765 | * XXX: We should audit the session creation and what this function | |
2766 | * does "extra" in order to avoid a destroy since this function is used | |
2767 | * in the domain session creation (kernel and ust) only. Same for UST | |
2768 | * domain. | |
2769 | */ | |
2770 | if (session->kernel_session->consumer) { | |
6addfa37 | 2771 | consumer_output_put(session->kernel_session->consumer); |
09a90bcd | 2772 | } |
2f77fc4b DG |
2773 | session->kernel_session->consumer = |
2774 | consumer_copy_output(session->consumer); | |
2775 | /* Ease our life a bit for the next part */ | |
2776 | consumer = session->kernel_session->consumer; | |
2777 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2778 | break; |
f20baf8e | 2779 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2780 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2781 | case LTTNG_DOMAIN_PYTHON: |
b551a063 | 2782 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2783 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd | 2784 | if (session->ust_session->consumer) { |
6addfa37 | 2785 | consumer_output_put(session->ust_session->consumer); |
09a90bcd | 2786 | } |
2f77fc4b DG |
2787 | session->ust_session->consumer = |
2788 | consumer_copy_output(session->consumer); | |
2789 | /* Ease our life a bit for the next part */ | |
2790 | consumer = session->ust_session->consumer; | |
2791 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2792 | break; |
2793 | default: | |
f73fabfd | 2794 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2795 | goto error; |
2796 | } | |
2797 | ||
2f77fc4b | 2798 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2799 | strncat(consumer->subdir, dir_name, |
2800 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2801 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2802 | ||
f73fabfd | 2803 | ret = LTTNG_OK; |
54d01ffb DG |
2804 | |
2805 | error: | |
2806 | return ret; | |
2807 | } | |
2808 | ||
00e2e675 | 2809 | /* |
2f77fc4b | 2810 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2811 | * |
2812 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2813 | */ |
2f77fc4b DG |
2814 | static int create_ust_session(struct ltt_session *session, |
2815 | struct lttng_domain *domain) | |
00e2e675 DG |
2816 | { |
2817 | int ret; | |
2f77fc4b | 2818 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2819 | |
a4b92340 | 2820 | assert(session); |
2f77fc4b DG |
2821 | assert(domain); |
2822 | assert(session->consumer); | |
a4b92340 | 2823 | |
2f77fc4b | 2824 | switch (domain->type) { |
f20baf8e | 2825 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2826 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2827 | case LTTNG_DOMAIN_PYTHON: |
2f77fc4b DG |
2828 | case LTTNG_DOMAIN_UST: |
2829 | break; | |
2830 | default: | |
2831 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2832 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2833 | goto error; |
2834 | } | |
2835 | ||
2f77fc4b DG |
2836 | DBG("Creating UST session"); |
2837 | ||
dec56f6c | 2838 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2839 | if (lus == NULL) { |
f73fabfd | 2840 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2841 | goto error; |
2842 | } | |
2843 | ||
2f77fc4b DG |
2844 | lus->uid = session->uid; |
2845 | lus->gid = session->gid; | |
2bba9e53 | 2846 | lus->output_traces = session->output_traces; |
27babd3a | 2847 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2848 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2849 | session->ust_session = lus; |
d7ba1388 | 2850 | if (session->shm_path[0]) { |
3d071855 MD |
2851 | strncpy(lus->root_shm_path, session->shm_path, |
2852 | sizeof(lus->root_shm_path)); | |
2853 | lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2854 | strncpy(lus->shm_path, session->shm_path, |
2855 | sizeof(lus->shm_path)); | |
2856 | lus->shm_path[sizeof(lus->shm_path) - 1] = '\0'; | |
2857 | strncat(lus->shm_path, "/ust", | |
2858 | sizeof(lus->shm_path) - strlen(lus->shm_path) - 1); | |
2859 | } | |
2f77fc4b DG |
2860 | /* Copy session output to the newly created UST session */ |
2861 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2862 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2863 | goto error; |
2864 | } | |
2865 | ||
f73fabfd | 2866 | return LTTNG_OK; |
00e2e675 DG |
2867 | |
2868 | error: | |
2f77fc4b DG |
2869 | free(lus); |
2870 | session->ust_session = NULL; | |
00e2e675 DG |
2871 | return ret; |
2872 | } | |
2873 | ||
2874 | /* | |
2f77fc4b | 2875 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2876 | */ |
2f77fc4b | 2877 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2878 | { |
2879 | int ret; | |
a4b92340 | 2880 | |
2f77fc4b | 2881 | DBG("Creating kernel session"); |
00e2e675 | 2882 | |
2f77fc4b DG |
2883 | ret = kernel_create_session(session, kernel_tracer_fd); |
2884 | if (ret < 0) { | |
f73fabfd | 2885 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2886 | goto error; |
2887 | } | |
2888 | ||
2f77fc4b DG |
2889 | /* Code flow safety */ |
2890 | assert(session->kernel_session); | |
2891 | ||
2892 | /* Copy session output to the newly created Kernel session */ | |
2893 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2894 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2895 | goto error; |
2896 | } | |
2897 | ||
2f77fc4b DG |
2898 | session->kernel_session->uid = session->uid; |
2899 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2900 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2901 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2902 | |
f73fabfd | 2903 | return LTTNG_OK; |
00e2e675 | 2904 | |
2f77fc4b DG |
2905 | error: |
2906 | trace_kernel_destroy_session(session->kernel_session); | |
2907 | session->kernel_session = NULL; | |
2908 | return ret; | |
2909 | } | |
00e2e675 | 2910 | |
2f77fc4b DG |
2911 | /* |
2912 | * Count number of session permitted by uid/gid. | |
2913 | */ | |
2914 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2915 | { | |
2916 | unsigned int i = 0; | |
2917 | struct ltt_session *session; | |
07424f16 | 2918 | |
2f77fc4b DG |
2919 | DBG("Counting number of available session for UID %d GID %d", |
2920 | uid, gid); | |
2921 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2922 | /* |
2f77fc4b | 2923 | * Only list the sessions the user can control. |
00e2e675 | 2924 | */ |
2f77fc4b DG |
2925 | if (!session_access_ok(session, uid, gid)) { |
2926 | continue; | |
2927 | } | |
2928 | i++; | |
a4b92340 | 2929 | } |
2f77fc4b | 2930 | return i; |
00e2e675 DG |
2931 | } |
2932 | ||
5c408ad8 JD |
2933 | /* |
2934 | * Check if the current kernel tracer supports the session rotation feature. | |
2935 | * Return 1 if it does, 0 otherwise. | |
2936 | */ | |
2937 | static int check_rotate_compatible(void) | |
2938 | { | |
2939 | int ret = 1; | |
2940 | ||
2941 | if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) { | |
2942 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
2943 | ret = 0; | |
2944 | } | |
2945 | ||
2946 | return ret; | |
2947 | } | |
2948 | ||
54d01ffb DG |
2949 | /* |
2950 | * Process the command requested by the lttng client within the command | |
2951 | * context structure. This function make sure that the return structure (llm) | |
2952 | * is set and ready for transmission before returning. | |
2953 | * | |
2954 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2955 | * |
2956 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2957 | * |
2958 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2959 | */ |
53a80697 MD |
2960 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2961 | int *sock_error) | |
54d01ffb | 2962 | { |
f73fabfd | 2963 | int ret = LTTNG_OK; |
44d3bd01 | 2964 | int need_tracing_session = 1; |
2e09ba09 | 2965 | int need_domain; |
54d01ffb DG |
2966 | |
2967 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2968 | ||
3745d315 MD |
2969 | assert(!rcu_read_ongoing()); |
2970 | ||
53a80697 MD |
2971 | *sock_error = 0; |
2972 | ||
2e09ba09 MD |
2973 | switch (cmd_ctx->lsm->cmd_type) { |
2974 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2975 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2976 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2977 | case LTTNG_DESTROY_SESSION: |
2978 | case LTTNG_LIST_SESSIONS: | |
2979 | case LTTNG_LIST_DOMAINS: | |
2980 | case LTTNG_START_TRACE: | |
2981 | case LTTNG_STOP_TRACE: | |
6d805429 | 2982 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
2983 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
2984 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
2985 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
2986 | case LTTNG_SNAPSHOT_RECORD: | |
fb198a11 | 2987 | case LTTNG_SAVE_SESSION: |
d7ba1388 | 2988 | case LTTNG_SET_SESSION_SHM_PATH: |
eded6438 | 2989 | case LTTNG_REGENERATE_METADATA: |
c2561365 | 2990 | case LTTNG_REGENERATE_STATEDUMP: |
b3530820 JG |
2991 | case LTTNG_REGISTER_TRIGGER: |
2992 | case LTTNG_UNREGISTER_TRIGGER: | |
5c408ad8 | 2993 | case LTTNG_ROTATE_SESSION: |
d68c9a04 JD |
2994 | case LTTNG_ROTATION_GET_INFO: |
2995 | case LTTNG_SESSION_GET_CURRENT_OUTPUT: | |
259c2674 | 2996 | case LTTNG_ROTATION_SET_SCHEDULE: |
66ea93b1 | 2997 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
2e09ba09 | 2998 | need_domain = 0; |
3aace903 | 2999 | break; |
2e09ba09 MD |
3000 | default: |
3001 | need_domain = 1; | |
3002 | } | |
3003 | ||
e6142f2e | 3004 | if (config.no_kernel && need_domain |
2e09ba09 | 3005 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { |
531d29f9 | 3006 | if (!is_root) { |
f73fabfd | 3007 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 3008 | } else { |
f73fabfd | 3009 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 3010 | } |
4fba7219 DG |
3011 | goto error; |
3012 | } | |
3013 | ||
8d3113b2 DG |
3014 | /* Deny register consumer if we already have a spawned consumer. */ |
3015 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
3016 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
3017 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 3018 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 3019 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
3020 | goto error; |
3021 | } | |
3022 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
3023 | } | |
3024 | ||
54d01ffb DG |
3025 | /* |
3026 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 3027 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
3028 | * command. |
3029 | */ | |
3030 | switch(cmd_ctx->lsm->cmd_type) { | |
3031 | case LTTNG_LIST_SESSIONS: | |
3032 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 3033 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
3034 | case LTTNG_LIST_DOMAINS: |
3035 | case LTTNG_LIST_CHANNELS: | |
3036 | case LTTNG_LIST_EVENTS: | |
834978fd | 3037 | case LTTNG_LIST_SYSCALLS: |
a5dfbb9d | 3038 | case LTTNG_LIST_TRACKER_PIDS: |
5cd0780d | 3039 | case LTTNG_DATA_PENDING: |
5c408ad8 | 3040 | case LTTNG_ROTATE_SESSION: |
d68c9a04 | 3041 | case LTTNG_ROTATION_GET_INFO: |
66ea93b1 | 3042 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
54d01ffb DG |
3043 | break; |
3044 | default: | |
3045 | /* Setup lttng message with no payload */ | |
6e10c9b9 | 3046 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0); |
54d01ffb DG |
3047 | if (ret < 0) { |
3048 | /* This label does not try to unlock the session */ | |
3049 | goto init_setup_error; | |
3050 | } | |
3051 | } | |
3052 | ||
3053 | /* Commands that DO NOT need a session. */ | |
3054 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 3055 | case LTTNG_CREATE_SESSION: |
27babd3a | 3056 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 3057 | case LTTNG_CREATE_SESSION_LIVE: |
54d01ffb DG |
3058 | case LTTNG_LIST_SESSIONS: |
3059 | case LTTNG_LIST_TRACEPOINTS: | |
834978fd | 3060 | case LTTNG_LIST_SYSCALLS: |
f37d259d | 3061 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
fb198a11 | 3062 | case LTTNG_SAVE_SESSION: |
b3530820 JG |
3063 | case LTTNG_REGISTER_TRIGGER: |
3064 | case LTTNG_UNREGISTER_TRIGGER: | |
44d3bd01 | 3065 | need_tracing_session = 0; |
54d01ffb DG |
3066 | break; |
3067 | default: | |
3068 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
3069 | /* |
3070 | * We keep the session list lock across _all_ commands | |
3071 | * for now, because the per-session lock does not | |
3072 | * handle teardown properly. | |
3073 | */ | |
74babd95 | 3074 | session_lock_list(); |
54d01ffb DG |
3075 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
3076 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 3077 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
3078 | goto error; |
3079 | } else { | |
3080 | /* Acquire lock for the session */ | |
3081 | session_lock(cmd_ctx->session); | |
3082 | } | |
3083 | break; | |
3084 | } | |
b389abbe | 3085 | |
5f3ecf22 DG |
3086 | /* |
3087 | * Commands that need a valid session but should NOT create one if none | |
3088 | * exists. Instead of creating one and destroying it when the command is | |
3089 | * handled, process that right before so we save some round trip in useless | |
3090 | * code path. | |
3091 | */ | |
3092 | switch (cmd_ctx->lsm->cmd_type) { | |
3093 | case LTTNG_DISABLE_CHANNEL: | |
3094 | case LTTNG_DISABLE_EVENT: | |
5f3ecf22 DG |
3095 | switch (cmd_ctx->lsm->domain.type) { |
3096 | case LTTNG_DOMAIN_KERNEL: | |
3097 | if (!cmd_ctx->session->kernel_session) { | |
3098 | ret = LTTNG_ERR_NO_CHANNEL; | |
3099 | goto error; | |
3100 | } | |
3101 | break; | |
3102 | case LTTNG_DOMAIN_JUL: | |
5cdb6027 | 3103 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3104 | case LTTNG_DOMAIN_PYTHON: |
5f3ecf22 DG |
3105 | case LTTNG_DOMAIN_UST: |
3106 | if (!cmd_ctx->session->ust_session) { | |
3107 | ret = LTTNG_ERR_NO_CHANNEL; | |
3108 | goto error; | |
3109 | } | |
3110 | break; | |
3111 | default: | |
3112 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3113 | goto error; | |
3114 | } | |
3115 | default: | |
3116 | break; | |
3117 | } | |
3118 | ||
2e09ba09 MD |
3119 | if (!need_domain) { |
3120 | goto skip_domain; | |
3121 | } | |
a4b92340 | 3122 | |
54d01ffb DG |
3123 | /* |
3124 | * Check domain type for specific "pre-action". | |
3125 | */ | |
3126 | switch (cmd_ctx->lsm->domain.type) { | |
3127 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 3128 | if (!is_root) { |
f73fabfd | 3129 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
3130 | goto error; |
3131 | } | |
3132 | ||
54d01ffb | 3133 | /* Kernel tracer check */ |
a4b35e07 | 3134 | if (kernel_tracer_fd == -1) { |
54d01ffb | 3135 | /* Basically, load kernel tracer modules */ |
096102bd DG |
3136 | ret = init_kernel_tracer(); |
3137 | if (ret != 0) { | |
54d01ffb DG |
3138 | goto error; |
3139 | } | |
3140 | } | |
5eb91c98 | 3141 | |
5c827ce0 DG |
3142 | /* Consumer is in an ERROR state. Report back to client */ |
3143 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3144 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3145 | goto error; |
3146 | } | |
3147 | ||
54d01ffb | 3148 | /* Need a session for kernel command */ |
44d3bd01 | 3149 | if (need_tracing_session) { |
54d01ffb | 3150 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 3151 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 3152 | if (ret < 0) { |
f73fabfd | 3153 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
3154 | goto error; |
3155 | } | |
b389abbe | 3156 | } |
7d29a247 | 3157 | |
54d01ffb | 3158 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
3159 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
3160 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 3161 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
3162 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
3163 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 3164 | if (ret < 0) { |
f73fabfd | 3165 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 3166 | goto error; |
950131af | 3167 | } |
5c827ce0 | 3168 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
3169 | } else { |
3170 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 3171 | } |
173af62f | 3172 | |
a4b92340 DG |
3173 | /* |
3174 | * The consumer was just spawned so we need to add the socket to | |
3175 | * the consumer output of the session if exist. | |
3176 | */ | |
3177 | ret = consumer_create_socket(&kconsumer_data, | |
3178 | cmd_ctx->session->kernel_session->consumer); | |
3179 | if (ret < 0) { | |
3180 | goto error; | |
173af62f | 3181 | } |
0d0c377a | 3182 | } |
5c827ce0 | 3183 | |
54d01ffb | 3184 | break; |
b9dfb167 | 3185 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3186 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3187 | case LTTNG_DOMAIN_PYTHON: |
2bdd86d4 | 3188 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 3189 | { |
b51ec5b4 MD |
3190 | if (!ust_app_supported()) { |
3191 | ret = LTTNG_ERR_NO_UST; | |
3192 | goto error; | |
3193 | } | |
5c827ce0 DG |
3194 | /* Consumer is in an ERROR state. Report back to client */ |
3195 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3196 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3197 | goto error; |
3198 | } | |
3199 | ||
44d3bd01 | 3200 | if (need_tracing_session) { |
a4b92340 | 3201 | /* Create UST session if none exist. */ |
f6a9efaa | 3202 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 3203 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 3204 | &cmd_ctx->lsm->domain); |
f73fabfd | 3205 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
3206 | goto error; |
3207 | } | |
3208 | } | |
00e2e675 | 3209 | |
7753dea8 MD |
3210 | /* Start the UST consumer daemons */ |
3211 | /* 64-bit */ | |
3212 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
e6142f2e | 3213 | if (config.consumerd64_bin_path.value && |
7753dea8 | 3214 | ustconsumer64_data.pid == 0 && |
785d2d0d | 3215 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3216 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3217 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 3218 | if (ret < 0) { |
f73fabfd | 3219 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 3220 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
3221 | goto error; |
3222 | } | |
48842b30 | 3223 | |
173af62f | 3224 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 3225 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 3226 | } else { |
7753dea8 MD |
3227 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3228 | } | |
173af62f DG |
3229 | |
3230 | /* | |
3231 | * Setup socket for consumer 64 bit. No need for atomic access | |
3232 | * since it was set above and can ONLY be set in this thread. | |
3233 | */ | |
a4b92340 DG |
3234 | ret = consumer_create_socket(&ustconsumer64_data, |
3235 | cmd_ctx->session->ust_session->consumer); | |
3236 | if (ret < 0) { | |
3237 | goto error; | |
173af62f DG |
3238 | } |
3239 | ||
7753dea8 | 3240 | /* 32-bit */ |
385b881b | 3241 | pthread_mutex_lock(&ustconsumer32_data.pid_mutex); |
e6142f2e | 3242 | if (config.consumerd32_bin_path.value && |
7753dea8 | 3243 | ustconsumer32_data.pid == 0 && |
785d2d0d | 3244 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3245 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
3246 | ret = start_consumerd(&ustconsumer32_data); | |
3247 | if (ret < 0) { | |
f73fabfd | 3248 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 3249 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
3250 | goto error; |
3251 | } | |
5c827ce0 | 3252 | |
173af62f | 3253 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 3254 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
3255 | } else { |
3256 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 3257 | } |
173af62f DG |
3258 | |
3259 | /* | |
3260 | * Setup socket for consumer 64 bit. No need for atomic access | |
3261 | * since it was set above and can ONLY be set in this thread. | |
3262 | */ | |
a4b92340 DG |
3263 | ret = consumer_create_socket(&ustconsumer32_data, |
3264 | cmd_ctx->session->ust_session->consumer); | |
3265 | if (ret < 0) { | |
3266 | goto error; | |
173af62f | 3267 | } |
44d3bd01 DG |
3268 | } |
3269 | break; | |
48842b30 | 3270 | } |
54d01ffb | 3271 | default: |
54d01ffb DG |
3272 | break; |
3273 | } | |
2e09ba09 | 3274 | skip_domain: |
33a2b854 | 3275 | |
5c827ce0 DG |
3276 | /* Validate consumer daemon state when start/stop trace command */ |
3277 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
3278 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
3279 | switch (cmd_ctx->lsm->domain.type) { | |
e7619875 JG |
3280 | case LTTNG_DOMAIN_NONE: |
3281 | break; | |
b9dfb167 | 3282 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3283 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3284 | case LTTNG_DOMAIN_PYTHON: |
5c827ce0 DG |
3285 | case LTTNG_DOMAIN_UST: |
3286 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3287 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3288 | goto error; |
3289 | } | |
3290 | break; | |
3291 | case LTTNG_DOMAIN_KERNEL: | |
3292 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3293 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3294 | goto error; |
3295 | } | |
3296 | break; | |
825535cc JG |
3297 | default: |
3298 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3299 | goto error; | |
5c827ce0 DG |
3300 | } |
3301 | } | |
3302 | ||
8e0af1b4 MD |
3303 | /* |
3304 | * Check that the UID or GID match that of the tracing session. | |
3305 | * The root user can interact with all sessions. | |
3306 | */ | |
3307 | if (need_tracing_session) { | |
3308 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
3309 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3310 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
f73fabfd | 3311 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
3312 | goto error; |
3313 | } | |
3314 | } | |
3315 | ||
ffe60014 DG |
3316 | /* |
3317 | * Send relayd information to consumer as soon as we have a domain and a | |
3318 | * session defined. | |
3319 | */ | |
3320 | if (cmd_ctx->session && need_domain) { | |
3321 | /* | |
3322 | * Setup relayd if not done yet. If the relayd information was already | |
3323 | * sent to the consumer, this call will gracefully return. | |
3324 | */ | |
3325 | ret = cmd_setup_relayd(cmd_ctx->session); | |
3326 | if (ret != LTTNG_OK) { | |
3327 | goto error; | |
3328 | } | |
3329 | } | |
3330 | ||
54d01ffb DG |
3331 | /* Process by command type */ |
3332 | switch (cmd_ctx->lsm->cmd_type) { | |
3333 | case LTTNG_ADD_CONTEXT: | |
3334 | { | |
2001793c JG |
3335 | /* |
3336 | * An LTTNG_ADD_CONTEXT command might have a supplementary | |
3337 | * payload if the context being added is an application context. | |
3338 | */ | |
3339 | if (cmd_ctx->lsm->u.context.ctx.ctx == | |
3340 | LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
3341 | char *provider_name = NULL, *context_name = NULL; | |
3342 | size_t provider_name_len = | |
3343 | cmd_ctx->lsm->u.context.provider_name_len; | |
3344 | size_t context_name_len = | |
3345 | cmd_ctx->lsm->u.context.context_name_len; | |
3346 | ||
3347 | if (provider_name_len == 0 || context_name_len == 0) { | |
3348 | /* | |
3349 | * Application provider and context names MUST | |
3350 | * be provided. | |
3351 | */ | |
3352 | ret = -LTTNG_ERR_INVALID; | |
3353 | goto error; | |
3354 | } | |
3355 | ||
3356 | provider_name = zmalloc(provider_name_len + 1); | |
3357 | if (!provider_name) { | |
3358 | ret = -LTTNG_ERR_NOMEM; | |
3359 | goto error; | |
3360 | } | |
e0f4d107 JG |
3361 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = |
3362 | provider_name; | |
2001793c JG |
3363 | |
3364 | context_name = zmalloc(context_name_len + 1); | |
3365 | if (!context_name) { | |
3366 | ret = -LTTNG_ERR_NOMEM; | |
3367 | goto error_add_context; | |
3368 | } | |
e0f4d107 JG |
3369 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = |
3370 | context_name; | |
2001793c JG |
3371 | |
3372 | ret = lttcomm_recv_unix_sock(sock, provider_name, | |
3373 | provider_name_len); | |
3374 | if (ret < 0) { | |
3375 | goto error_add_context; | |
3376 | } | |
3377 | ||
3378 | ret = lttcomm_recv_unix_sock(sock, context_name, | |
3379 | context_name_len); | |
3380 | if (ret < 0) { | |
3381 | goto error_add_context; | |
3382 | } | |
2001793c JG |
3383 | } |
3384 | ||
3385 | /* | |
3386 | * cmd_add_context assumes ownership of the provider and context | |
3387 | * names. | |
3388 | */ | |
3389 | ret = cmd_add_context(cmd_ctx->session, | |
3390 | cmd_ctx->lsm->domain.type, | |
54d01ffb | 3391 | cmd_ctx->lsm->u.context.channel_name, |
2001793c JG |
3392 | &cmd_ctx->lsm->u.context.ctx, |
3393 | kernel_poll_pipe[1]); | |
3394 | ||
3395 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL; | |
3396 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL; | |
3397 | error_add_context: | |
3398 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name); | |
3399 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name); | |
3400 | if (ret < 0) { | |
3401 | goto error; | |
3402 | } | |
54d01ffb DG |
3403 | break; |
3404 | } | |
3405 | case LTTNG_DISABLE_CHANNEL: | |
3406 | { | |
3407 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3408 | cmd_ctx->lsm->u.disable.channel_name); | |
3409 | break; | |
3410 | } | |
3411 | case LTTNG_DISABLE_EVENT: | |
3412 | { | |
47b7e2f9 JG |
3413 | |
3414 | /* | |
3415 | * FIXME: handle filter; for now we just receive the filter's | |
3416 | * bytecode along with the filter expression which are sent by | |
3417 | * liblttng-ctl and discard them. | |
3418 | * | |
3419 | * This fixes an issue where the client may block while sending | |
3420 | * the filter payload and encounter an error because the session | |
3421 | * daemon closes the socket without ever handling this data. | |
3422 | */ | |
3423 | size_t count = cmd_ctx->lsm->u.disable.expression_len + | |
3424 | cmd_ctx->lsm->u.disable.bytecode_len; | |
3425 | ||
3426 | if (count) { | |
3427 | char data[LTTNG_FILTER_MAX_LEN]; | |
3428 | ||
3429 | DBG("Discarding disable event command payload of size %zu", count); | |
3430 | while (count) { | |
3431 | ret = lttcomm_recv_unix_sock(sock, data, | |
3432 | count > sizeof(data) ? sizeof(data) : count); | |
3433 | if (ret < 0) { | |
3434 | goto error; | |
3435 | } | |
3436 | ||
3437 | count -= (size_t) ret; | |
3438 | } | |
3439 | } | |
6e911cad | 3440 | /* FIXME: passing packed structure to non-packed pointer */ |
54d01ffb DG |
3441 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
3442 | cmd_ctx->lsm->u.disable.channel_name, | |
6e911cad | 3443 | &cmd_ctx->lsm->u.disable.event); |
33a2b854 DG |
3444 | break; |
3445 | } | |
54d01ffb DG |
3446 | case LTTNG_ENABLE_CHANNEL: |
3447 | { | |
b3530820 JG |
3448 | cmd_ctx->lsm->u.channel.chan.attr.extended.ptr = |
3449 | (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended; | |
7972aab2 | 3450 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
b3530820 JG |
3451 | &cmd_ctx->lsm->u.channel.chan, |
3452 | kernel_poll_pipe[1]); | |
54d01ffb DG |
3453 | break; |
3454 | } | |
ccf10263 MD |
3455 | case LTTNG_TRACK_PID: |
3456 | { | |
3457 | ret = cmd_track_pid(cmd_ctx->session, | |
3458 | cmd_ctx->lsm->domain.type, | |
3459 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3460 | break; | |
3461 | } | |
3462 | case LTTNG_UNTRACK_PID: | |
3463 | { | |
3464 | ret = cmd_untrack_pid(cmd_ctx->session, | |
3465 | cmd_ctx->lsm->domain.type, | |
3466 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3467 | break; | |
3468 | } | |
54d01ffb DG |
3469 | case LTTNG_ENABLE_EVENT: |
3470 | { | |
882ef835 JI |
3471 | struct lttng_event_exclusion *exclusion = NULL; |
3472 | struct lttng_filter_bytecode *bytecode = NULL; | |
6b453b5e | 3473 | char *filter_expression = NULL; |
882ef835 | 3474 | |
67676bd8 DG |
3475 | /* Handle exclusion events and receive it from the client. */ |
3476 | if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { | |
3477 | size_t count = cmd_ctx->lsm->u.enable.exclusion_count; | |
3478 | ||
3479 | exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + | |
3480 | (count * LTTNG_SYMBOL_NAME_LEN)); | |
3481 | if (!exclusion) { | |
3482 | ret = LTTNG_ERR_EXCLUSION_NOMEM; | |
3483 | goto error; | |
882ef835 | 3484 | } |
882ef835 | 3485 | |
67676bd8 DG |
3486 | DBG("Receiving var len exclusion event list from client ..."); |
3487 | exclusion->count = count; | |
3488 | ret = lttcomm_recv_unix_sock(sock, exclusion->names, | |
3489 | count * LTTNG_SYMBOL_NAME_LEN); | |
3490 | if (ret <= 0) { | |
3491 | DBG("Nothing recv() from client var len data... continuing"); | |
3492 | *sock_error = 1; | |
3493 | free(exclusion); | |
3494 | ret = LTTNG_ERR_EXCLUSION_INVAL; | |
3495 | goto error; | |
3496 | } | |
3497 | } | |
3498 | ||
6b453b5e JG |
3499 | /* Get filter expression from client. */ |
3500 | if (cmd_ctx->lsm->u.enable.expression_len > 0) { | |
3501 | size_t expression_len = | |
3502 | cmd_ctx->lsm->u.enable.expression_len; | |
3503 | ||
3504 | if (expression_len > LTTNG_FILTER_MAX_LEN) { | |
3505 | ret = LTTNG_ERR_FILTER_INVAL; | |
3506 | free(exclusion); | |
3507 | goto error; | |
3508 | } | |
3509 | ||
3510 | filter_expression = zmalloc(expression_len); | |
3511 | if (!filter_expression) { | |
3512 | free(exclusion); | |
3513 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3514 | goto error; | |
3515 | } | |
3516 | ||
3517 | /* Receive var. len. data */ | |
3518 | DBG("Receiving var len filter's expression from client ..."); | |
3519 | ret = lttcomm_recv_unix_sock(sock, filter_expression, | |
3520 | expression_len); | |
3521 | if (ret <= 0) { | |
3522 | DBG("Nothing recv() from client car len data... continuing"); | |
3523 | *sock_error = 1; | |
3524 | free(filter_expression); | |
3525 | free(exclusion); | |
3526 | ret = LTTNG_ERR_FILTER_INVAL; | |
3527 | goto error; | |
3528 | } | |
3529 | } | |
3530 | ||
67676bd8 DG |
3531 | /* Handle filter and get bytecode from client. */ |
3532 | if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { | |
3533 | size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; | |
3534 | ||
3535 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
3536 | ret = LTTNG_ERR_FILTER_INVAL; | |
49d21f93 | 3537 | free(filter_expression); |
67676bd8 DG |
3538 | free(exclusion); |
3539 | goto error; | |
3540 | } | |
3541 | ||
3542 | bytecode = zmalloc(bytecode_len); | |
3543 | if (!bytecode) { | |
49d21f93 | 3544 | free(filter_expression); |
67676bd8 DG |
3545 | free(exclusion); |
3546 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3547 | goto error; | |
882ef835 JI |
3548 | } |
3549 | ||
67676bd8 DG |
3550 | /* Receive var. len. data */ |
3551 | DBG("Receiving var len filter's bytecode from client ..."); | |
3552 | ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len); | |
3553 | if (ret <= 0) { | |
3554 | DBG("Nothing recv() from client car len data... continuing"); | |
3555 | *sock_error = 1; | |
49d21f93 | 3556 | free(filter_expression); |
67676bd8 DG |
3557 | free(bytecode); |
3558 | free(exclusion); | |
3559 | ret = LTTNG_ERR_FILTER_INVAL; | |
3560 | goto error; | |
3561 | } | |
3562 | ||
3563 | if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { | |
49d21f93 | 3564 | free(filter_expression); |
67676bd8 DG |
3565 | free(bytecode); |
3566 | free(exclusion); | |
3567 | ret = LTTNG_ERR_FILTER_INVAL; | |
3568 | goto error; | |
3569 | } | |
882ef835 | 3570 | } |
67676bd8 DG |
3571 | |
3572 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, | |
3573 | cmd_ctx->lsm->u.enable.channel_name, | |
6b453b5e JG |
3574 | &cmd_ctx->lsm->u.enable.event, |
3575 | filter_expression, bytecode, exclusion, | |
67676bd8 | 3576 | kernel_poll_pipe[1]); |
54d01ffb DG |
3577 | break; |
3578 | } | |
052da939 | 3579 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 3580 | { |
9f19cc17 | 3581 | struct lttng_event *events; |
54d01ffb | 3582 | ssize_t nb_events; |
052da939 | 3583 | |
0845bbfa | 3584 | session_lock_list(); |
54d01ffb | 3585 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
0845bbfa | 3586 | session_unlock_list(); |
54d01ffb | 3587 | if (nb_events < 0) { |
f73fabfd | 3588 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3589 | ret = -nb_events; |
3590 | goto error; | |
2ef84c95 DG |
3591 | } |
3592 | ||
3593 | /* | |
3594 | * Setup lttng message with payload size set to the event list size in | |
3595 | * bytes and then copy list into the llm payload. | |
3596 | */ | |
6e10c9b9 PP |
3597 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3598 | sizeof(struct lttng_event) * nb_events); | |
3599 | free(events); | |
3600 | ||
2ef84c95 DG |
3601 | if (ret < 0) { |
3602 | goto setup_error; | |
3603 | } | |
3604 | ||
f73fabfd | 3605 | ret = LTTNG_OK; |
2ef84c95 DG |
3606 | break; |
3607 | } | |
f37d259d MD |
3608 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
3609 | { | |
3610 | struct lttng_event_field *fields; | |
3611 | ssize_t nb_fields; | |
3612 | ||
0845bbfa | 3613 | session_lock_list(); |
a4b92340 DG |
3614 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
3615 | &fields); | |
0845bbfa | 3616 | session_unlock_list(); |
f37d259d | 3617 | if (nb_fields < 0) { |
f73fabfd | 3618 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
3619 | ret = -nb_fields; |
3620 | goto error; | |
3621 | } | |
3622 | ||
3623 | /* | |
3624 | * Setup lttng message with payload size set to the event list size in | |
3625 | * bytes and then copy list into the llm payload. | |
3626 | */ | |
6e10c9b9 | 3627 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields, |
a4b92340 | 3628 | sizeof(struct lttng_event_field) * nb_fields); |
6e10c9b9 PP |
3629 | free(fields); |
3630 | ||
f37d259d | 3631 | if (ret < 0) { |
f37d259d MD |
3632 | goto setup_error; |
3633 | } | |
3634 | ||
f73fabfd | 3635 | ret = LTTNG_OK; |
f37d259d MD |
3636 | break; |
3637 | } | |
834978fd DG |
3638 | case LTTNG_LIST_SYSCALLS: |
3639 | { | |
3640 | struct lttng_event *events; | |
3641 | ssize_t nb_events; | |
3642 | ||
3643 | nb_events = cmd_list_syscalls(&events); | |
3644 | if (nb_events < 0) { | |
3645 | /* Return value is a negative lttng_error_code. */ | |
3646 | ret = -nb_events; | |
3647 | goto error; | |
3648 | } | |
3649 | ||
3650 | /* | |
3651 | * Setup lttng message with payload size set to the event list size in | |
3652 | * bytes and then copy list into the llm payload. | |
3653 | */ | |
6e10c9b9 PP |
3654 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3655 | sizeof(struct lttng_event) * nb_events); | |
3656 | free(events); | |
3657 | ||
834978fd | 3658 | if (ret < 0) { |
834978fd DG |
3659 | goto setup_error; |
3660 | } | |
3661 | ||
834978fd DG |
3662 | ret = LTTNG_OK; |
3663 | break; | |
3664 | } | |
a5dfbb9d MD |
3665 | case LTTNG_LIST_TRACKER_PIDS: |
3666 | { | |
3667 | int32_t *pids = NULL; | |
3668 | ssize_t nr_pids; | |
3669 | ||
3670 | nr_pids = cmd_list_tracker_pids(cmd_ctx->session, | |
3671 | cmd_ctx->lsm->domain.type, &pids); | |
3672 | if (nr_pids < 0) { | |
3673 | /* Return value is a negative lttng_error_code. */ | |
3674 | ret = -nr_pids; | |
3675 | goto error; | |
3676 | } | |
3677 | ||
3678 | /* | |
3679 | * Setup lttng message with payload size set to the event list size in | |
3680 | * bytes and then copy list into the llm payload. | |
3681 | */ | |
6e10c9b9 PP |
3682 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids, |
3683 | sizeof(int32_t) * nr_pids); | |
3684 | free(pids); | |
3685 | ||
a5dfbb9d | 3686 | if (ret < 0) { |
a5dfbb9d MD |
3687 | goto setup_error; |
3688 | } | |
3689 | ||
a5dfbb9d MD |
3690 | ret = LTTNG_OK; |
3691 | break; | |
3692 | } | |
00e2e675 DG |
3693 | case LTTNG_SET_CONSUMER_URI: |
3694 | { | |
a4b92340 DG |
3695 | size_t nb_uri, len; |
3696 | struct lttng_uri *uris; | |
3697 | ||
3698 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3699 | len = nb_uri * sizeof(struct lttng_uri); | |
3700 | ||
3701 | if (nb_uri == 0) { | |
f73fabfd | 3702 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
3703 | goto error; |
3704 | } | |
3705 | ||
3706 | uris = zmalloc(len); | |
3707 | if (uris == NULL) { | |
f73fabfd | 3708 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3709 | goto error; |
3710 | } | |
3711 | ||
3712 | /* Receive variable len data */ | |
77c7c900 | 3713 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
3714 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3715 | if (ret <= 0) { | |
3716 | DBG("No URIs received from client... continuing"); | |
3717 | *sock_error = 1; | |
f73fabfd | 3718 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3719 | free(uris); |
a4b92340 DG |
3720 | goto error; |
3721 | } | |
3722 | ||
bda32d56 JG |
3723 | ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris); |
3724 | free(uris); | |
f73fabfd | 3725 | if (ret != LTTNG_OK) { |
a4b92340 DG |
3726 | goto error; |
3727 | } | |
3728 | ||
b1d41407 | 3729 | |
00e2e675 DG |
3730 | break; |
3731 | } | |
f3ed775e | 3732 | case LTTNG_START_TRACE: |
8c0faa1d | 3733 | { |
259c2674 JD |
3734 | /* |
3735 | * On the first start, if we have a kernel session and we have | |
3736 | * enabled time or size-based rotations, we have to make sure | |
3737 | * the kernel tracer supports it. | |
3738 | */ | |
3739 | if (!cmd_ctx->session->has_been_started && \ | |
3740 | cmd_ctx->session->kernel_session && \ | |
3741 | (cmd_ctx->session->rotate_timer_period || \ | |
3742 | cmd_ctx->session->rotate_size) && \ | |
3743 | !check_rotate_compatible()) { | |
3744 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
3745 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
3746 | goto error; | |
3747 | } | |
54d01ffb | 3748 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3749 | break; |
3750 | } | |
f3ed775e | 3751 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3752 | { |
54d01ffb | 3753 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3754 | break; |
3755 | } | |
5e16da05 MD |
3756 | case LTTNG_CREATE_SESSION: |
3757 | { | |
a4b92340 DG |
3758 | size_t nb_uri, len; |
3759 | struct lttng_uri *uris = NULL; | |
3760 | ||
3761 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3762 | len = nb_uri * sizeof(struct lttng_uri); | |
3763 | ||
3764 | if (nb_uri > 0) { | |
3765 | uris = zmalloc(len); | |
3766 | if (uris == NULL) { | |
f73fabfd | 3767 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3768 | goto error; |
3769 | } | |
3770 | ||
3771 | /* Receive variable len data */ | |
77c7c900 | 3772 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
3773 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3774 | if (ret <= 0) { | |
3775 | DBG("No URIs received from client... continuing"); | |
3776 | *sock_error = 1; | |
f73fabfd | 3777 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3778 | free(uris); |
a4b92340 DG |
3779 | goto error; |
3780 | } | |
3781 | ||
3782 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3783 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 3784 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3785 | free(uris); |
a4b92340 DG |
3786 | goto error; |
3787 | } | |
3788 | } | |
3789 | ||
3790 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
ecc48a90 | 3791 | &cmd_ctx->creds, 0); |
a4b92340 | 3792 | |
b1d41407 DG |
3793 | free(uris); |
3794 | ||
00e2e675 DG |
3795 | break; |
3796 | } | |
5e16da05 MD |
3797 | case LTTNG_DESTROY_SESSION: |
3798 | { | |
90936dcf JD |
3799 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1], |
3800 | notification_thread_handle); | |
a4b92340 DG |
3801 | |
3802 | /* Set session to NULL so we do not unlock it after free. */ | |
256a5576 | 3803 | cmd_ctx->session = NULL; |
5461b305 | 3804 | break; |
5e16da05 | 3805 | } |
9f19cc17 | 3806 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3807 | { |
54d01ffb | 3808 | ssize_t nb_dom; |
fa64dfb4 | 3809 | struct lttng_domain *domains = NULL; |
5461b305 | 3810 | |
54d01ffb DG |
3811 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3812 | if (nb_dom < 0) { | |
f73fabfd | 3813 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3814 | ret = -nb_dom; |
3815 | goto error; | |
ce3d728c | 3816 | } |
520ff687 | 3817 | |
6e10c9b9 PP |
3818 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains, |
3819 | nb_dom * sizeof(struct lttng_domain)); | |
3820 | free(domains); | |
3821 | ||
5461b305 DG |
3822 | if (ret < 0) { |
3823 | goto setup_error; | |
520ff687 | 3824 | } |
57167058 | 3825 | |
f73fabfd | 3826 | ret = LTTNG_OK; |
5e16da05 MD |
3827 | break; |
3828 | } | |
9f19cc17 | 3829 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3830 | { |
53e367f9 | 3831 | ssize_t payload_size; |
ade7ce52 | 3832 | struct lttng_channel *channels = NULL; |
54d01ffb | 3833 | |
53e367f9 | 3834 | payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type, |
b551a063 | 3835 | cmd_ctx->session, &channels); |
53e367f9 | 3836 | if (payload_size < 0) { |
f73fabfd | 3837 | /* Return value is a negative lttng_error_code. */ |
53e367f9 | 3838 | ret = -payload_size; |
54d01ffb | 3839 | goto error; |
5461b305 | 3840 | } |
ca95a216 | 3841 | |
6e10c9b9 | 3842 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels, |
53e367f9 | 3843 | payload_size); |
6e10c9b9 PP |
3844 | free(channels); |
3845 | ||
5461b305 DG |
3846 | if (ret < 0) { |
3847 | goto setup_error; | |
3848 | } | |
9f19cc17 | 3849 | |
f73fabfd | 3850 | ret = LTTNG_OK; |
5461b305 | 3851 | break; |
5e16da05 | 3852 | } |
9f19cc17 | 3853 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3854 | { |
b551a063 | 3855 | ssize_t nb_event; |
684d34d2 | 3856 | struct lttng_event *events = NULL; |
b4e3ceb9 PP |
3857 | struct lttcomm_event_command_header cmd_header; |
3858 | size_t total_size; | |
3859 | ||
b2d66287 | 3860 | memset(&cmd_header, 0, sizeof(cmd_header)); |
b4e3ceb9 PP |
3861 | /* Extended infos are included at the end of events */ |
3862 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, | |
3863 | cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name, | |
3864 | &events, &total_size); | |
9f19cc17 | 3865 | |
54d01ffb | 3866 | if (nb_event < 0) { |
f73fabfd | 3867 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3868 | ret = -nb_event; |
3869 | goto error; | |
5461b305 | 3870 | } |
ca95a216 | 3871 | |
b4e3ceb9 PP |
3872 | cmd_header.nb_events = nb_event; |
3873 | ret = setup_lttng_msg(cmd_ctx, events, total_size, | |
3874 | &cmd_header, sizeof(cmd_header)); | |
6e10c9b9 PP |
3875 | free(events); |
3876 | ||
5461b305 DG |
3877 | if (ret < 0) { |
3878 | goto setup_error; | |
3879 | } | |
9f19cc17 | 3880 | |
f73fabfd | 3881 | ret = LTTNG_OK; |
5461b305 | 3882 | break; |
5e16da05 MD |
3883 | } |
3884 | case LTTNG_LIST_SESSIONS: | |
3885 | { | |
8e0af1b4 | 3886 | unsigned int nr_sessions; |
6e10c9b9 PP |
3887 | void *sessions_payload; |
3888 | size_t payload_len; | |
5461b305 | 3889 | |
8e0af1b4 | 3890 | session_lock_list(); |
730389d9 DG |
3891 | nr_sessions = lttng_sessions_count( |
3892 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
3893 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6e10c9b9 PP |
3894 | payload_len = sizeof(struct lttng_session) * nr_sessions; |
3895 | sessions_payload = zmalloc(payload_len); | |
d32fb093 | 3896 | |
6e10c9b9 | 3897 | if (!sessions_payload) { |
54d01ffb | 3898 | session_unlock_list(); |
6e10c9b9 | 3899 | ret = -ENOMEM; |
5461b305 | 3900 | goto setup_error; |
e065084a | 3901 | } |
5e16da05 | 3902 | |
6e10c9b9 | 3903 | cmd_list_lttng_sessions(sessions_payload, |
730389d9 DG |
3904 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3905 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
54d01ffb | 3906 | session_unlock_list(); |
5e16da05 | 3907 | |
6e10c9b9 PP |
3908 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, |
3909 | payload_len); | |
3910 | free(sessions_payload); | |
3911 | ||
3912 | if (ret < 0) { | |
3913 | goto setup_error; | |
3914 | } | |
3915 | ||
f73fabfd | 3916 | ret = LTTNG_OK; |
5e16da05 MD |
3917 | break; |
3918 | } | |
d9800920 DG |
3919 | case LTTNG_REGISTER_CONSUMER: |
3920 | { | |
2f77fc4b DG |
3921 | struct consumer_data *cdata; |
3922 | ||
3923 | switch (cmd_ctx->lsm->domain.type) { | |
3924 | case LTTNG_DOMAIN_KERNEL: | |
3925 | cdata = &kconsumer_data; | |
3926 | break; | |
3927 | default: | |
f73fabfd | 3928 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3929 | goto error; |
3930 | } | |
3931 | ||
54d01ffb | 3932 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 3933 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
3934 | break; |
3935 | } | |
6d805429 | 3936 | case LTTNG_DATA_PENDING: |
806e2684 | 3937 | { |
f6151c55 | 3938 | int pending_ret; |
6e10c9b9 | 3939 | uint8_t pending_ret_byte; |
f6151c55 JG |
3940 | |
3941 | pending_ret = cmd_data_pending(cmd_ctx->session); | |
6e10c9b9 | 3942 | |
f6151c55 JG |
3943 | /* |
3944 | * FIXME | |
3945 | * | |
3946 | * This function may returns 0 or 1 to indicate whether or not | |
3947 | * there is data pending. In case of error, it should return an | |
3948 | * LTTNG_ERR code. However, some code paths may still return | |
3949 | * a nondescript error code, which we handle by returning an | |
3950 | * "unknown" error. | |
3951 | */ | |
3952 | if (pending_ret == 0 || pending_ret == 1) { | |
6e10c9b9 PP |
3953 | /* |
3954 | * ret will be set to LTTNG_OK at the end of | |
3955 | * this function. | |
3956 | */ | |
f6151c55 JG |
3957 | } else if (pending_ret < 0) { |
3958 | ret = LTTNG_ERR_UNK; | |
3959 | goto setup_error; | |
3960 | } else { | |
3961 | ret = pending_ret; | |
3962 | goto setup_error; | |
3963 | } | |
3964 | ||
6e10c9b9 PP |
3965 | pending_ret_byte = (uint8_t) pending_ret; |
3966 | ||
3967 | /* 1 byte to return whether or not data is pending */ | |
3968 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, | |
3969 | &pending_ret_byte, 1); | |
3970 | ||
3971 | if (ret < 0) { | |
3972 | goto setup_error; | |
3973 | } | |
3974 | ||
3975 | ret = LTTNG_OK; | |
806e2684 DG |
3976 | break; |
3977 | } | |
da3c9ec1 DG |
3978 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3979 | { | |
6dc3064a DG |
3980 | struct lttcomm_lttng_output_id reply; |
3981 | ||
3982 | ret = cmd_snapshot_add_output(cmd_ctx->session, | |
3983 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); | |
3984 | if (ret != LTTNG_OK) { | |
3985 | goto error; | |
3986 | } | |
3987 | ||
6e10c9b9 PP |
3988 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply, |
3989 | sizeof(reply)); | |
6dc3064a DG |
3990 | if (ret < 0) { |
3991 | goto setup_error; | |
3992 | } | |
3993 | ||
3994 | /* Copy output list into message payload */ | |
6dc3064a | 3995 | ret = LTTNG_OK; |
da3c9ec1 DG |
3996 | break; |
3997 | } | |
3998 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3999 | { | |
6dc3064a DG |
4000 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
4001 | &cmd_ctx->lsm->u.snapshot_output.output); | |
da3c9ec1 DG |
4002 | break; |
4003 | } | |
4004 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
4005 | { | |
6dc3064a DG |
4006 | ssize_t nb_output; |
4007 | struct lttng_snapshot_output *outputs = NULL; | |
4008 | ||
4009 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); | |
4010 | if (nb_output < 0) { | |
4011 | ret = -nb_output; | |
4012 | goto error; | |
4013 | } | |
4014 | ||
6e10c9b9 PP |
4015 | assert((nb_output > 0 && outputs) || nb_output == 0); |
4016 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs, | |
6dc3064a | 4017 | nb_output * sizeof(struct lttng_snapshot_output)); |
6e10c9b9 PP |
4018 | free(outputs); |
4019 | ||
6dc3064a | 4020 | if (ret < 0) { |
6dc3064a DG |
4021 | goto setup_error; |
4022 | } | |
4023 | ||
6dc3064a | 4024 | ret = LTTNG_OK; |
da3c9ec1 DG |
4025 | break; |
4026 | } | |
4027 | case LTTNG_SNAPSHOT_RECORD: | |
4028 | { | |
6dc3064a DG |
4029 | ret = cmd_snapshot_record(cmd_ctx->session, |
4030 | &cmd_ctx->lsm->u.snapshot_record.output, | |
4031 | cmd_ctx->lsm->u.snapshot_record.wait); | |
da3c9ec1 DG |
4032 | break; |
4033 | } | |
27babd3a DG |
4034 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
4035 | { | |
4036 | size_t nb_uri, len; | |
4037 | struct lttng_uri *uris = NULL; | |
4038 | ||
4039 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
4040 | len = nb_uri * sizeof(struct lttng_uri); | |
4041 | ||
4042 | if (nb_uri > 0) { | |
4043 | uris = zmalloc(len); | |
4044 | if (uris == NULL) { | |
4045 | ret = LTTNG_ERR_FATAL; | |
4046 | goto error; | |
4047 | } | |
4048 | ||
4049 | /* Receive variable len data */ | |
4050 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
4051 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
4052 | if (ret <= 0) { | |
4053 | DBG("No URIs received from client... continuing"); | |
4054 | *sock_error = 1; | |
4055 | ret = LTTNG_ERR_SESSION_FAIL; | |
4056 | free(uris); | |
4057 | goto error; | |
4058 | } | |
4059 | ||
4060 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
4061 | DBG("Creating session with ONE network URI is a bad call"); | |
4062 | ret = LTTNG_ERR_SESSION_FAIL; | |
4063 | free(uris); | |
4064 | goto error; | |
4065 | } | |
4066 | } | |
4067 | ||
4068 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, | |
4069 | nb_uri, &cmd_ctx->creds); | |
4070 | free(uris); | |
4071 | break; | |
4072 | } | |
ecc48a90 JD |
4073 | case LTTNG_CREATE_SESSION_LIVE: |
4074 | { | |
4075 | size_t nb_uri, len; | |
4076 | struct lttng_uri *uris = NULL; | |
4077 | ||
4078 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
4079 | len = nb_uri * sizeof(struct lttng_uri); | |
4080 | ||
4081 | if (nb_uri > 0) { | |
4082 | uris = zmalloc(len); | |
4083 | if (uris == NULL) { | |
4084 | ret = LTTNG_ERR_FATAL; | |
4085 | goto error; | |
4086 | } | |
4087 | ||
4088 | /* Receive variable len data */ | |
4089 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
4090 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
4091 | if (ret <= 0) { | |
4092 | DBG("No URIs received from client... continuing"); | |
4093 | *sock_error = 1; | |
4094 | ret = LTTNG_ERR_SESSION_FAIL; | |
4095 | free(uris); | |
4096 | goto error; | |
4097 | } | |
4098 | ||
4099 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
4100 | DBG("Creating session with ONE network URI is a bad call"); | |
4101 | ret = LTTNG_ERR_SESSION_FAIL; | |
4102 | free(uris); | |
4103 | goto error; | |
4104 | } | |
4105 | } | |
4106 | ||
4107 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, | |
4108 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); | |
4109 | free(uris); | |
4110 | break; | |
4111 | } | |
fb198a11 JG |
4112 | case LTTNG_SAVE_SESSION: |
4113 | { | |
4114 | ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr, | |
4115 | &cmd_ctx->creds); | |
4116 | break; | |
4117 | } | |
d7ba1388 MD |
4118 | case LTTNG_SET_SESSION_SHM_PATH: |
4119 | { | |
4120 | ret = cmd_set_session_shm_path(cmd_ctx->session, | |
4121 | cmd_ctx->lsm->u.set_shm_path.shm_path); | |
4122 | break; | |
4123 | } | |
eded6438 | 4124 | case LTTNG_REGENERATE_METADATA: |
93ec662e | 4125 | { |
eded6438 | 4126 | ret = cmd_regenerate_metadata(cmd_ctx->session); |
93ec662e JD |
4127 | break; |
4128 | } | |
c2561365 JD |
4129 | case LTTNG_REGENERATE_STATEDUMP: |
4130 | { | |
4131 | ret = cmd_regenerate_statedump(cmd_ctx->session); | |
4132 | break; | |
4133 | } | |
b3530820 JG |
4134 | case LTTNG_REGISTER_TRIGGER: |
4135 | { | |
4136 | ret = cmd_register_trigger(cmd_ctx, sock, | |
4137 | notification_thread_handle); | |
4138 | break; | |
4139 | } | |
4140 | case LTTNG_UNREGISTER_TRIGGER: | |
4141 | { | |
4142 | ret = cmd_unregister_trigger(cmd_ctx, sock, | |
4143 | notification_thread_handle); | |
4144 | break; | |
4145 | } | |
5c408ad8 JD |
4146 | case LTTNG_ROTATE_SESSION: |
4147 | { | |
4148 | struct lttng_rotate_session_return rotate_return; | |
4149 | ||
4150 | DBG("Client rotate session \"%s\"", cmd_ctx->session->name); | |
4151 | ||
d68c9a04 | 4152 | memset(&rotate_return, 0, sizeof(rotate_return)); |
5c408ad8 JD |
4153 | if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) { |
4154 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
4155 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
4156 | goto error; | |
4157 | } | |
4158 | ||
4159 | ret = cmd_rotate_session(cmd_ctx->session, &rotate_return); | |
4160 | if (ret < 0) { | |
4161 | ret = -ret; | |
4162 | goto error; | |
4163 | } | |
4164 | ||
4165 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return, | |
4166 | sizeof(rotate_return)); | |
4167 | if (ret < 0) { | |
4168 | ret = -ret; | |
4169 | goto error; | |
4170 | } | |
4171 | ||
4172 | ret = LTTNG_OK; | |
4173 | break; | |
4174 | } | |
d68c9a04 | 4175 | case LTTNG_ROTATION_GET_INFO: |
5c408ad8 | 4176 | { |
d68c9a04 | 4177 | struct lttng_rotation_get_info_return get_info_return; |
5c408ad8 | 4178 | |
d68c9a04 JD |
4179 | memset(&get_info_return, 0, sizeof(get_info_return)); |
4180 | ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return, | |
4181 | cmd_ctx->lsm->u.get_rotation_info.rotation_id); | |
5c408ad8 JD |
4182 | if (ret < 0) { |
4183 | ret = -ret; | |
4184 | goto error; | |
4185 | } | |
4186 | ||
d68c9a04 JD |
4187 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return, |
4188 | sizeof(get_info_return)); | |
5c408ad8 JD |
4189 | if (ret < 0) { |
4190 | ret = -ret; | |
4191 | goto error; | |
4192 | } | |
4193 | ||
4194 | ret = LTTNG_OK; | |
4195 | break; | |
4196 | } | |
d68c9a04 | 4197 | case LTTNG_SESSION_GET_CURRENT_OUTPUT: |
5c408ad8 | 4198 | { |
d68c9a04 | 4199 | struct lttng_session_get_current_output_return output_return; |
5c408ad8 | 4200 | |
d68c9a04 JD |
4201 | memset(&output_return, 0, sizeof(output_return)); |
4202 | ret = cmd_session_get_current_output(cmd_ctx->session, | |
4203 | &output_return); | |
5c408ad8 JD |
4204 | if (ret < 0) { |
4205 | ret = -ret; | |
4206 | goto error; | |
4207 | } | |
4208 | ||
d68c9a04 JD |
4209 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &output_return, |
4210 | sizeof(output_return)); | |
5c408ad8 JD |
4211 | if (ret < 0) { |
4212 | ret = -ret; | |
4213 | goto error; | |
4214 | } | |
4215 | ||
4216 | ret = LTTNG_OK; | |
4217 | break; | |
4218 | } | |
259c2674 JD |
4219 | case LTTNG_ROTATION_SET_SCHEDULE: |
4220 | { | |
66ea93b1 JG |
4221 | bool set_schedule; |
4222 | enum lttng_rotation_schedule_type schedule_type; | |
4223 | uint64_t value; | |
4224 | ||
259c2674 JD |
4225 | if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) { |
4226 | DBG("Kernel tracer version does not support session rotations"); | |
4227 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
4228 | goto error; | |
4229 | } | |
4230 | ||
66ea93b1 JG |
4231 | set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1; |
4232 | schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type; | |
4233 | value = cmd_ctx->lsm->u.rotation_set_schedule.value; | |
4234 | ||
259c2674 | 4235 | ret = cmd_rotation_set_schedule(cmd_ctx->session, |
66ea93b1 JG |
4236 | set_schedule, |
4237 | schedule_type, | |
4238 | value, | |
90936dcf | 4239 | notification_thread_handle); |
a2026546 | 4240 | if (ret != LTTNG_OK) { |
259c2674 JD |
4241 | goto error; |
4242 | } | |
4243 | ||
259c2674 JD |
4244 | break; |
4245 | } | |
66ea93b1 | 4246 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
329f3443 | 4247 | { |
66ea93b1 JG |
4248 | struct lttng_session_list_schedules_return schedules = { |
4249 | .periodic.set = !!cmd_ctx->session->rotate_timer_period, | |
4250 | .periodic.value = cmd_ctx->session->rotate_timer_period, | |
4251 | .size.set = !!cmd_ctx->session->rotate_size, | |
4252 | .size.value = cmd_ctx->session->rotate_size, | |
4253 | }; | |
4254 | ||
4255 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules, | |
4256 | sizeof(schedules)); | |
329f3443 JD |
4257 | if (ret < 0) { |
4258 | ret = -ret; | |
4259 | goto error; | |
4260 | } | |
4261 | ||
4262 | ret = LTTNG_OK; | |
4263 | break; | |
4264 | } | |
5e16da05 | 4265 | default: |
f73fabfd | 4266 | ret = LTTNG_ERR_UND; |
5461b305 | 4267 | break; |
fac6795d DG |
4268 | } |
4269 | ||
5461b305 | 4270 | error: |
5461b305 DG |
4271 | if (cmd_ctx->llm == NULL) { |
4272 | DBG("Missing llm structure. Allocating one."); | |
6e10c9b9 | 4273 | if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) { |
5461b305 DG |
4274 | goto setup_error; |
4275 | } | |
4276 | } | |
54d01ffb | 4277 | /* Set return code */ |
5461b305 | 4278 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 4279 | setup_error: |
b5541356 | 4280 | if (cmd_ctx->session) { |
54d01ffb | 4281 | session_unlock(cmd_ctx->session); |
b5541356 | 4282 | } |
256a5576 MD |
4283 | if (need_tracing_session) { |
4284 | session_unlock_list(); | |
4285 | } | |
54d01ffb | 4286 | init_setup_error: |
3745d315 | 4287 | assert(!rcu_read_ongoing()); |
8028d920 | 4288 | return ret; |
fac6795d DG |
4289 | } |
4290 | ||
44a5e5eb DG |
4291 | /* |
4292 | * Thread managing health check socket. | |
4293 | */ | |
4294 | static void *thread_manage_health(void *data) | |
4295 | { | |
eb4a2943 | 4296 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
4297 | uint32_t revents, nb_fd; |
4298 | struct lttng_poll_event events; | |
0c89d795 MD |
4299 | struct health_comm_msg msg; |
4300 | struct health_comm_reply reply; | |
44a5e5eb DG |
4301 | |
4302 | DBG("[thread] Manage health check started"); | |
4303 | ||
4304 | rcu_register_thread(); | |
4305 | ||
6d737ce4 DG |
4306 | /* We might hit an error path before this is created. */ |
4307 | lttng_poll_init(&events); | |
3cc04881 | 4308 | |
44a5e5eb | 4309 | /* Create unix socket */ |
e6142f2e | 4310 | sock = lttcomm_create_unix_sock(config.health_unix_sock_path.value); |
44a5e5eb DG |
4311 | if (sock < 0) { |
4312 | ERR("Unable to create health check Unix socket"); | |
44a5e5eb DG |
4313 | goto error; |
4314 | } | |
4315 | ||
6c71277b MD |
4316 | if (is_root) { |
4317 | /* lttng health client socket path permissions */ | |
e6142f2e JG |
4318 | ret = chown(config.health_unix_sock_path.value, 0, |
4319 | utils_get_group_id(config.tracing_group_name.value)); | |
6c71277b | 4320 | if (ret < 0) { |
e6142f2e | 4321 | ERR("Unable to set group on %s", config.health_unix_sock_path.value); |
6c71277b | 4322 | PERROR("chown"); |
6c71277b MD |
4323 | goto error; |
4324 | } | |
4325 | ||
e6142f2e | 4326 | ret = chmod(config.health_unix_sock_path.value, |
6c71277b MD |
4327 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
4328 | if (ret < 0) { | |
e6142f2e | 4329 | ERR("Unable to set permissions on %s", config.health_unix_sock_path.value); |
6c71277b | 4330 | PERROR("chmod"); |
6c71277b MD |
4331 | goto error; |
4332 | } | |
4333 | } | |
4334 | ||
b662582b DG |
4335 | /* |
4336 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4337 | * show must go on. | |
4338 | */ | |
4339 | (void) utils_set_fd_cloexec(sock); | |
4340 | ||
44a5e5eb DG |
4341 | ret = lttcomm_listen_unix_sock(sock); |
4342 | if (ret < 0) { | |
4343 | goto error; | |
4344 | } | |
4345 | ||
4346 | /* | |
4347 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4348 | * more will be added to this poll set. | |
4349 | */ | |
d0b96690 | 4350 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
4351 | if (ret < 0) { |
4352 | goto error; | |
4353 | } | |
4354 | ||
4355 | /* Add the application registration socket */ | |
4356 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
4357 | if (ret < 0) { | |
4358 | goto error; | |
4359 | } | |
4360 | ||
ef367a93 | 4361 | sessiond_notify_ready(); |
97bc1426 | 4362 | |
44a5e5eb DG |
4363 | while (1) { |
4364 | DBG("Health check ready"); | |
4365 | ||
44a5e5eb DG |
4366 | /* Inifinite blocking call, waiting for transmission */ |
4367 | restart: | |
4368 | ret = lttng_poll_wait(&events, -1); | |
4369 | if (ret < 0) { | |
4370 | /* | |
4371 | * Restart interrupted system call. | |
4372 | */ | |
4373 | if (errno == EINTR) { | |
4374 | goto restart; | |
4375 | } | |
4376 | goto error; | |
4377 | } | |
4378 | ||
0d9c5d77 DG |
4379 | nb_fd = ret; |
4380 | ||
44a5e5eb DG |
4381 | for (i = 0; i < nb_fd; i++) { |
4382 | /* Fetch once the poll data */ | |
4383 | revents = LTTNG_POLL_GETEV(&events, i); | |
4384 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4385 | ||
fd20dac9 MD |
4386 | if (!revents) { |
4387 | /* No activity for this FD (poll implementation). */ | |
4388 | continue; | |
4389 | } | |
4390 | ||
44a5e5eb | 4391 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4392 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 4393 | if (ret) { |
139ac872 MD |
4394 | err = 0; |
4395 | goto exit; | |
44a5e5eb DG |
4396 | } |
4397 | ||
4398 | /* Event on the registration socket */ | |
4399 | if (pollfd == sock) { | |
03e43155 MD |
4400 | if (revents & LPOLLIN) { |
4401 | continue; | |
4402 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
44a5e5eb DG |
4403 | ERR("Health socket poll error"); |
4404 | goto error; | |
03e43155 MD |
4405 | } else { |
4406 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4407 | goto error; | |
44a5e5eb DG |
4408 | } |
4409 | } | |
4410 | } | |
4411 | ||
4412 | new_sock = lttcomm_accept_unix_sock(sock); | |
4413 | if (new_sock < 0) { | |
4414 | goto error; | |
4415 | } | |
4416 | ||
b662582b DG |
4417 | /* |
4418 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4419 | * show must go on. | |
4420 | */ | |
4421 | (void) utils_set_fd_cloexec(new_sock); | |
4422 | ||
44a5e5eb DG |
4423 | DBG("Receiving data from client for health..."); |
4424 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
4425 | if (ret <= 0) { | |
4426 | DBG("Nothing recv() from client... continuing"); | |
4427 | ret = close(new_sock); | |
4428 | if (ret) { | |
4429 | PERROR("close"); | |
4430 | } | |
44a5e5eb DG |
4431 | continue; |
4432 | } | |
4433 | ||
4434 | rcu_thread_online(); | |
4435 | ||
53efb85a | 4436 | memset(&reply, 0, sizeof(reply)); |
6c71277b MD |
4437 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { |
4438 | /* | |
4439 | * health_check_state returns 0 if health is | |
4440 | * bad. | |
4441 | */ | |
4442 | if (!health_check_state(health_sessiond, i)) { | |
4443 | reply.ret_code |= 1ULL << i; | |
4444 | } | |
44a5e5eb DG |
4445 | } |
4446 | ||
6c71277b | 4447 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
44a5e5eb DG |
4448 | |
4449 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
4450 | if (ret < 0) { | |
4451 | ERR("Failed to send health data back to client"); | |
4452 | } | |
4453 | ||
4454 | /* End of transmission */ | |
4455 | ret = close(new_sock); | |
4456 | if (ret) { | |
4457 | PERROR("close"); | |
4458 | } | |
44a5e5eb DG |
4459 | } |
4460 | ||
139ac872 | 4461 | exit: |
44a5e5eb | 4462 | error: |
139ac872 MD |
4463 | if (err) { |
4464 | ERR("Health error occurred in %s", __func__); | |
4465 | } | |
44a5e5eb | 4466 | DBG("Health check thread dying"); |
e6142f2e | 4467 | unlink(config.health_unix_sock_path.value); |
44a5e5eb DG |
4468 | if (sock >= 0) { |
4469 | ret = close(sock); | |
4470 | if (ret) { | |
4471 | PERROR("close"); | |
4472 | } | |
4473 | } | |
44a5e5eb DG |
4474 | |
4475 | lttng_poll_clean(&events); | |
b11d6a7f | 4476 | stop_threads(); |
44a5e5eb DG |
4477 | rcu_unregister_thread(); |
4478 | return NULL; | |
4479 | } | |
4480 | ||
1d4b027a | 4481 | /* |
d063d709 DG |
4482 | * This thread manage all clients request using the unix client socket for |
4483 | * communication. | |
1d4b027a DG |
4484 | */ |
4485 | static void *thread_manage_clients(void *data) | |
4486 | { | |
139ac872 | 4487 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 4488 | int sock_error; |
5eb91c98 | 4489 | uint32_t revents, nb_fd; |
273ea72c | 4490 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 4491 | struct lttng_poll_event events; |
1d4b027a DG |
4492 | |
4493 | DBG("[thread] Manage client started"); | |
4494 | ||
f6a9efaa DG |
4495 | rcu_register_thread(); |
4496 | ||
6c71277b | 4497 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
927ca06a | 4498 | |
840cb59c | 4499 | health_code_update(); |
44a5e5eb | 4500 | |
1d4b027a DG |
4501 | ret = lttcomm_listen_unix_sock(client_sock); |
4502 | if (ret < 0) { | |
35df2755 | 4503 | goto error_listen; |
1d4b027a DG |
4504 | } |
4505 | ||
5eb91c98 DG |
4506 | /* |
4507 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4508 | * more will be added to this poll set. | |
4509 | */ | |
d0b96690 | 4510 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 4511 | if (ret < 0) { |
35df2755 | 4512 | goto error_create_poll; |
5eb91c98 | 4513 | } |
273ea72c | 4514 | |
5eb91c98 DG |
4515 | /* Add the application registration socket */ |
4516 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
4517 | if (ret < 0) { | |
4518 | goto error; | |
4519 | } | |
273ea72c | 4520 | |
ef367a93 JG |
4521 | ret = sem_post(&load_info->message_thread_ready); |
4522 | if (ret) { | |
4523 | PERROR("sem_post message_thread_ready"); | |
4524 | goto error; | |
4525 | } | |
0bb7724a | 4526 | |
f28f9e44 JG |
4527 | /* |
4528 | * Wait until all support threads are initialized before accepting | |
4529 | * commands. | |
4530 | */ | |
4531 | while (uatomic_read(<tng_sessiond_ready) != 0) { | |
4532 | fd_set read_fds; | |
4533 | struct timeval timeout; | |
4534 | ||
4535 | FD_ZERO(&read_fds); | |
4536 | FD_SET(thread_quit_pipe[0], &read_fds); | |
4537 | memset(&timeout, 0, sizeof(timeout)); | |
4538 | timeout.tv_usec = 1000; | |
4539 | ||
4540 | /* | |
4541 | * If a support thread failed to launch, it may signal that | |
4542 | * we must exit and the sessiond would never be marked as | |
4543 | * "ready". | |
4544 | * | |
4545 | * The timeout is set to 1ms, which serves as a way to | |
4546 | * pace down this check. | |
4547 | */ | |
4548 | ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL, | |
4549 | &timeout); | |
4550 | if (ret > 0 || (ret < 0 && errno != EINTR)) { | |
4551 | goto exit; | |
4552 | } | |
4553 | } | |
8b0d4636 JG |
4554 | /* |
4555 | * This barrier is paired with the one in sessiond_notify_ready() to | |
4556 | * ensure that loads accessing data initialized by the other threads, | |
4557 | * on which this thread was waiting, are not performed before this point. | |
4558 | * | |
4559 | * Note that this could be a 'read' memory barrier, but a full barrier | |
4560 | * is used in case the code changes. The performance implications of | |
4561 | * this choice are minimal since this is a slow path. | |
4562 | */ | |
4563 | cmm_smp_mb(); | |
f28f9e44 | 4564 | |
72453c8c MD |
4565 | /* This testpoint is after we signal readiness to the parent. */ |
4566 | if (testpoint(sessiond_thread_manage_clients)) { | |
4567 | goto error; | |
4568 | } | |
4569 | ||
e547b070 | 4570 | if (testpoint(sessiond_thread_manage_clients_before_loop)) { |
6993eeb3 CB |
4571 | goto error; |
4572 | } | |
8ac94142 | 4573 | |
840cb59c | 4574 | health_code_update(); |
44a5e5eb | 4575 | |
1d4b027a | 4576 | while (1) { |
a503e1ef JG |
4577 | const struct cmd_completion_handler *cmd_completion_handler; |
4578 | ||
1d4b027a | 4579 | DBG("Accepting client command ..."); |
273ea72c DG |
4580 | |
4581 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 4582 | restart: |
a78af745 | 4583 | health_poll_entry(); |
5eb91c98 | 4584 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 4585 | health_poll_exit(); |
273ea72c | 4586 | if (ret < 0) { |
88f2b785 MD |
4587 | /* |
4588 | * Restart interrupted system call. | |
4589 | */ | |
4590 | if (errno == EINTR) { | |
4591 | goto restart; | |
4592 | } | |
273ea72c DG |
4593 | goto error; |
4594 | } | |
4595 | ||
0d9c5d77 DG |
4596 | nb_fd = ret; |
4597 | ||
5eb91c98 DG |
4598 | for (i = 0; i < nb_fd; i++) { |
4599 | /* Fetch once the poll data */ | |
4600 | revents = LTTNG_POLL_GETEV(&events, i); | |
4601 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4602 | ||
840cb59c | 4603 | health_code_update(); |
44a5e5eb | 4604 | |
fd20dac9 MD |
4605 | if (!revents) { |
4606 | /* No activity for this FD (poll implementation). */ | |
4607 | continue; | |
4608 | } | |
4609 | ||
5eb91c98 | 4610 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4611 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 4612 | if (ret) { |
139ac872 MD |
4613 | err = 0; |
4614 | goto exit; | |
5eb91c98 DG |
4615 | } |
4616 | ||
4617 | /* Event on the registration socket */ | |
4618 | if (pollfd == client_sock) { | |
03e43155 MD |
4619 | if (revents & LPOLLIN) { |
4620 | continue; | |
4621 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
5eb91c98 DG |
4622 | ERR("Client socket poll error"); |
4623 | goto error; | |
03e43155 MD |
4624 | } else { |
4625 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4626 | goto error; | |
5eb91c98 DG |
4627 | } |
4628 | } | |
4629 | } | |
4630 | ||
4631 | DBG("Wait for client response"); | |
4632 | ||
840cb59c | 4633 | health_code_update(); |
44a5e5eb | 4634 | |
5eb91c98 DG |
4635 | sock = lttcomm_accept_unix_sock(client_sock); |
4636 | if (sock < 0) { | |
273ea72c | 4637 | goto error; |
273ea72c DG |
4638 | } |
4639 | ||
b662582b DG |
4640 | /* |
4641 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4642 | * show must go on. | |
4643 | */ | |
4644 | (void) utils_set_fd_cloexec(sock); | |
4645 | ||
be040666 DG |
4646 | /* Set socket option for credentials retrieval */ |
4647 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
4648 | if (ret < 0) { | |
4649 | goto error; | |
4650 | } | |
4651 | ||
5eb91c98 | 4652 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 4653 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 4654 | if (cmd_ctx == NULL) { |
76d7553f | 4655 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 4656 | goto error; |
5eb91c98 | 4657 | } |
1d4b027a | 4658 | |
5eb91c98 | 4659 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 4660 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 4661 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 4662 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
4663 | goto error; |
4664 | } | |
1d4b027a | 4665 | |
5eb91c98 DG |
4666 | cmd_ctx->llm = NULL; |
4667 | cmd_ctx->session = NULL; | |
1d4b027a | 4668 | |
840cb59c | 4669 | health_code_update(); |
44a5e5eb | 4670 | |
5eb91c98 DG |
4671 | /* |
4672 | * Data is received from the lttng client. The struct | |
4673 | * lttcomm_session_msg (lsm) contains the command and data request of | |
4674 | * the client. | |
4675 | */ | |
4676 | DBG("Receiving data from client ..."); | |
be040666 DG |
4677 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
4678 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
4679 | if (ret <= 0) { |
4680 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
4681 | ret = close(sock); |
4682 | if (ret) { | |
4683 | PERROR("close"); | |
4684 | } | |
4685 | sock = -1; | |
a2c0da86 | 4686 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4687 | continue; |
4688 | } | |
1d4b027a | 4689 | |
840cb59c | 4690 | health_code_update(); |
44a5e5eb | 4691 | |
5eb91c98 DG |
4692 | // TODO: Validate cmd_ctx including sanity check for |
4693 | // security purpose. | |
f7776ea7 | 4694 | |
f6a9efaa | 4695 | rcu_thread_online(); |
5eb91c98 DG |
4696 | /* |
4697 | * This function dispatch the work to the kernel or userspace tracer | |
4698 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
4699 | * informations for the client. The command context struct contains | |
4700 | * everything this function may needs. | |
4701 | */ | |
53a80697 | 4702 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 4703 | rcu_thread_offline(); |
5eb91c98 | 4704 | if (ret < 0) { |
36134aa1 DG |
4705 | ret = close(sock); |
4706 | if (ret) { | |
4707 | PERROR("close"); | |
53a80697 | 4708 | } |
36134aa1 | 4709 | sock = -1; |
bbd973c2 | 4710 | /* |
5eb91c98 | 4711 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 4712 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
4713 | * (ENOMEM). Error detected but still accept |
4714 | * command, unless a socket error has been | |
4715 | * detected. | |
bbd973c2 | 4716 | */ |
bbd973c2 | 4717 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4718 | continue; |
4719 | } | |
d6e4fca4 | 4720 | |
a503e1ef JG |
4721 | cmd_completion_handler = cmd_pop_completion_handler(); |
4722 | if (cmd_completion_handler) { | |
4723 | enum lttng_error_code completion_code; | |
4724 | ||
4725 | completion_code = cmd_completion_handler->run( | |
4726 | cmd_completion_handler->data); | |
4727 | if (completion_code != LTTNG_OK) { | |
4728 | clean_command_ctx(&cmd_ctx); | |
4729 | continue; | |
4730 | } | |
4731 | } | |
4732 | ||
840cb59c | 4733 | health_code_update(); |
44a5e5eb | 4734 | |
a1ac8b97 | 4735 | DBG("Sending response (size: %d, retcode: %s (%d))", |
54d01ffb | 4736 | cmd_ctx->lttng_msg_size, |
a1ac8b97 JG |
4737 | lttng_strerror(-cmd_ctx->llm->ret_code), |
4738 | cmd_ctx->llm->ret_code); | |
54d01ffb | 4739 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
4740 | if (ret < 0) { |
4741 | ERR("Failed to send data back to client"); | |
bbd973c2 | 4742 | } |
1d4b027a | 4743 | |
5eb91c98 | 4744 | /* End of transmission */ |
76d7553f MD |
4745 | ret = close(sock); |
4746 | if (ret) { | |
4747 | PERROR("close"); | |
4748 | } | |
4749 | sock = -1; | |
be040666 DG |
4750 | |
4751 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 4752 | |
840cb59c | 4753 | health_code_update(); |
273ea72c DG |
4754 | } |
4755 | ||
139ac872 | 4756 | exit: |
5eb91c98 | 4757 | error: |
35df2755 CB |
4758 | if (sock >= 0) { |
4759 | ret = close(sock); | |
4760 | if (ret) { | |
4761 | PERROR("close"); | |
4762 | } | |
139ac872 | 4763 | } |
44a5e5eb | 4764 | |
35df2755 CB |
4765 | lttng_poll_clean(&events); |
4766 | clean_command_ctx(&cmd_ctx); | |
4767 | ||
4768 | error_listen: | |
4769 | error_create_poll: | |
e6142f2e | 4770 | unlink(config.client_unix_sock_path.value); |
76d7553f MD |
4771 | if (client_sock >= 0) { |
4772 | ret = close(client_sock); | |
4773 | if (ret) { | |
4774 | PERROR("close"); | |
4775 | } | |
a4b35e07 | 4776 | } |
35df2755 CB |
4777 | |
4778 | if (err) { | |
840cb59c | 4779 | health_error(); |
35df2755 | 4780 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 4781 | } |
273ea72c | 4782 | |
8782cc74 | 4783 | health_unregister(health_sessiond); |
35df2755 CB |
4784 | |
4785 | DBG("Client thread dying"); | |
f6a9efaa DG |
4786 | |
4787 | rcu_unregister_thread(); | |
4a15001e MD |
4788 | |
4789 | /* | |
4790 | * Since we are creating the consumer threads, we own them, so we need | |
4791 | * to join them before our thread exits. | |
4792 | */ | |
4793 | ret = join_consumer_thread(&kconsumer_data); | |
4794 | if (ret) { | |
4795 | errno = ret; | |
4796 | PERROR("join_consumer"); | |
4797 | } | |
4798 | ||
4799 | ret = join_consumer_thread(&ustconsumer32_data); | |
4800 | if (ret) { | |
4801 | errno = ret; | |
4802 | PERROR("join_consumer ust32"); | |
4803 | } | |
4804 | ||
4805 | ret = join_consumer_thread(&ustconsumer64_data); | |
4806 | if (ret) { | |
4807 | errno = ret; | |
4808 | PERROR("join_consumer ust64"); | |
4809 | } | |
1d4b027a DG |
4810 | return NULL; |
4811 | } | |
4812 | ||
c0232ea5 JG |
4813 | static int string_match(const char *str1, const char *str2) |
4814 | { | |
4815 | return (str1 && str2) && !strcmp(str1, str2); | |
4816 | } | |
4817 | ||
fac6795d | 4818 | /* |
26296c48 JG |
4819 | * Take an option from the getopt output and set it in the right variable to be |
4820 | * used later. | |
4821 | * | |
4822 | * Return 0 on success else a negative value. | |
fac6795d | 4823 | */ |
26296c48 | 4824 | static int set_option(int opt, const char *arg, const char *optname) |
fac6795d | 4825 | { |
26296c48 | 4826 | int ret = 0; |
fac6795d | 4827 | |
c0232ea5 | 4828 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
4829 | if (!arg || *arg == '\0') { |
4830 | ret = -EINVAL; | |
4831 | goto end; | |
4832 | } | |
e8fa9fb0 MD |
4833 | if (lttng_is_setuid_setgid()) { |
4834 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4835 | "-c, --client-sock"); | |
4836 | } else { | |
e6142f2e JG |
4837 | config_string_set(&config.client_unix_sock_path, |
4838 | strdup(arg)); | |
4839 | if (!config.client_unix_sock_path.value) { | |
4840 | ret = -ENOMEM; | |
4841 | PERROR("strdup"); | |
4842 | } | |
e8fa9fb0 | 4843 | } |
c0232ea5 | 4844 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
4845 | if (!arg || *arg == '\0') { |
4846 | ret = -EINVAL; | |
4847 | goto end; | |
4848 | } | |
e8fa9fb0 MD |
4849 | if (lttng_is_setuid_setgid()) { |
4850 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4851 | "-a, --apps-sock"); | |
4852 | } else { | |
e6142f2e JG |
4853 | config_string_set(&config.apps_unix_sock_path, |
4854 | strdup(arg)); | |
4855 | if (!config.apps_unix_sock_path.value) { | |
4856 | ret = -ENOMEM; | |
4857 | PERROR("strdup"); | |
4858 | } | |
e8fa9fb0 | 4859 | } |
c0232ea5 | 4860 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 4861 | config.daemonize = true; |
c0232ea5 | 4862 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 4863 | config.background = true; |
c0232ea5 | 4864 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
4865 | if (!arg || *arg == '\0') { |
4866 | ret = -EINVAL; | |
4867 | goto end; | |
4868 | } | |
e8fa9fb0 MD |
4869 | if (lttng_is_setuid_setgid()) { |
4870 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4871 | "-g, --group"); | |
4872 | } else { | |
e6142f2e JG |
4873 | config_string_set(&config.tracing_group_name, |
4874 | strdup(arg)); | |
4875 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 4876 | ret = -ENOMEM; |
e6142f2e | 4877 | PERROR("strdup"); |
e8fa9fb0 | 4878 | } |
db322c4d | 4879 | } |
c0232ea5 | 4880 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 4881 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 4882 | if (ret) { |
4fc83d94 | 4883 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
4884 | perror("exec"); |
4885 | } | |
4886 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 4887 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
4888 | fprintf(stdout, "%s\n", VERSION); |
4889 | exit(EXIT_SUCCESS); | |
c0232ea5 | 4890 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 4891 | config.sig_parent = true; |
c0232ea5 | 4892 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
4893 | if (!arg || *arg == '\0') { |
4894 | ret = -EINVAL; | |
4895 | goto end; | |
4896 | } | |
e8fa9fb0 MD |
4897 | if (lttng_is_setuid_setgid()) { |
4898 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4899 | "--kconsumerd-err-sock"); | |
4900 | } else { | |
e6142f2e JG |
4901 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
4902 | strdup(arg)); | |
4903 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
4904 | ret = -ENOMEM; | |
4905 | PERROR("strdup"); | |
4906 | } | |
e8fa9fb0 | 4907 | } |
c0232ea5 | 4908 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
4909 | if (!arg || *arg == '\0') { |
4910 | ret = -EINVAL; | |
4911 | goto end; | |
4912 | } | |
e8fa9fb0 MD |
4913 | if (lttng_is_setuid_setgid()) { |
4914 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4915 | "--kconsumerd-cmd-sock"); | |
4916 | } else { | |
e6142f2e JG |
4917 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
4918 | strdup(arg)); | |
4919 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
4920 | ret = -ENOMEM; | |
4921 | PERROR("strdup"); | |
4922 | } | |
e8fa9fb0 | 4923 | } |
c0232ea5 | 4924 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
4925 | if (!arg || *arg == '\0') { |
4926 | ret = -EINVAL; | |
4927 | goto end; | |
4928 | } | |
e8fa9fb0 MD |
4929 | if (lttng_is_setuid_setgid()) { |
4930 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4931 | "--ustconsumerd64-err-sock"); | |
4932 | } else { | |
e6142f2e JG |
4933 | config_string_set(&config.consumerd64_err_unix_sock_path, |
4934 | strdup(arg)); | |
4935 | if (!config.consumerd64_err_unix_sock_path.value) { | |
4936 | ret = -ENOMEM; | |
4937 | PERROR("strdup"); | |
4938 | } | |
e8fa9fb0 | 4939 | } |
c0232ea5 | 4940 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
4941 | if (!arg || *arg == '\0') { |
4942 | ret = -EINVAL; | |
4943 | goto end; | |
4944 | } | |
e8fa9fb0 MD |
4945 | if (lttng_is_setuid_setgid()) { |
4946 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4947 | "--ustconsumerd64-cmd-sock"); | |
4948 | } else { | |
e6142f2e JG |
4949 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
4950 | strdup(arg)); | |
4951 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
4952 | ret = -ENOMEM; | |
4953 | PERROR("strdup"); | |
4954 | } | |
e8fa9fb0 | 4955 | } |
c0232ea5 | 4956 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
4957 | if (!arg || *arg == '\0') { |
4958 | ret = -EINVAL; | |
4959 | goto end; | |
4960 | } | |
e8fa9fb0 MD |
4961 | if (lttng_is_setuid_setgid()) { |
4962 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4963 | "--ustconsumerd32-err-sock"); | |
4964 | } else { | |
e6142f2e JG |
4965 | config_string_set(&config.consumerd32_err_unix_sock_path, |
4966 | strdup(arg)); | |
4967 | if (!config.consumerd32_err_unix_sock_path.value) { | |
4968 | ret = -ENOMEM; | |
4969 | PERROR("strdup"); | |
4970 | } | |
e8fa9fb0 | 4971 | } |
c0232ea5 | 4972 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
4973 | if (!arg || *arg == '\0') { |
4974 | ret = -EINVAL; | |
4975 | goto end; | |
4976 | } | |
e8fa9fb0 MD |
4977 | if (lttng_is_setuid_setgid()) { |
4978 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4979 | "--ustconsumerd32-cmd-sock"); | |
4980 | } else { | |
e6142f2e JG |
4981 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
4982 | strdup(arg)); | |
4983 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
4984 | ret = -ENOMEM; | |
4985 | PERROR("strdup"); | |
4986 | } | |
e8fa9fb0 | 4987 | } |
c0232ea5 | 4988 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 4989 | config.no_kernel = true; |
c0232ea5 | 4990 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 4991 | config.quiet = true; |
c0232ea5 | 4992 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
4993 | /* Verbose level can increase using multiple -v */ |
4994 | if (arg) { | |
13755a18 | 4995 | /* Value obtained from config file */ |
e6142f2e | 4996 | config.verbose = config_parse_value(arg); |
26296c48 | 4997 | } else { |
13755a18 | 4998 | /* -v used on command line */ |
e6142f2e | 4999 | config.verbose++; |
26296c48 | 5000 | } |
13755a18 | 5001 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
5002 | config.verbose = config.verbose < 0 ? 0 : |
5003 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 5004 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 5005 | if (arg) { |
e6142f2e | 5006 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 5007 | } else { |
e6142f2e | 5008 | config.verbose_consumer++; |
26296c48 | 5009 | } |
c0232ea5 | 5010 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
5011 | if (!arg || *arg == '\0') { |
5012 | ret = -EINVAL; | |
5013 | goto end; | |
5014 | } | |
e8fa9fb0 MD |
5015 | if (lttng_is_setuid_setgid()) { |
5016 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5017 | "--consumerd32-path"); | |
5018 | } else { | |
e6142f2e JG |
5019 | config_string_set(&config.consumerd32_bin_path, |
5020 | strdup(arg)); | |
5021 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
5022 | PERROR("strdup"); |
5023 | ret = -ENOMEM; | |
5024 | } | |
db322c4d | 5025 | } |
c0232ea5 | 5026 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
5027 | if (!arg || *arg == '\0') { |
5028 | ret = -EINVAL; | |
5029 | goto end; | |
5030 | } | |
e8fa9fb0 MD |
5031 | if (lttng_is_setuid_setgid()) { |
5032 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5033 | "--consumerd32-libdir"); | |
5034 | } else { | |
e6142f2e JG |
5035 | config_string_set(&config.consumerd32_lib_dir, |
5036 | strdup(arg)); | |
5037 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
5038 | PERROR("strdup"); |
5039 | ret = -ENOMEM; | |
5040 | } | |
db322c4d | 5041 | } |
c0232ea5 | 5042 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
5043 | if (!arg || *arg == '\0') { |
5044 | ret = -EINVAL; | |
5045 | goto end; | |
5046 | } | |
e8fa9fb0 MD |
5047 | if (lttng_is_setuid_setgid()) { |
5048 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5049 | "--consumerd64-path"); | |
5050 | } else { | |
e6142f2e JG |
5051 | config_string_set(&config.consumerd64_bin_path, |
5052 | strdup(arg)); | |
5053 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
5054 | PERROR("strdup"); |
5055 | ret = -ENOMEM; | |
5056 | } | |
db322c4d | 5057 | } |
c0232ea5 | 5058 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
5059 | if (!arg || *arg == '\0') { |
5060 | ret = -EINVAL; | |
5061 | goto end; | |
5062 | } | |
e8fa9fb0 MD |
5063 | if (lttng_is_setuid_setgid()) { |
5064 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5065 | "--consumerd64-libdir"); | |
5066 | } else { | |
e6142f2e JG |
5067 | config_string_set(&config.consumerd64_lib_dir, |
5068 | strdup(arg)); | |
5069 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
5070 | PERROR("strdup"); |
5071 | ret = -ENOMEM; | |
5072 | } | |
db322c4d | 5073 | } |
c0232ea5 | 5074 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
5075 | if (!arg || *arg == '\0') { |
5076 | ret = -EINVAL; | |
5077 | goto end; | |
5078 | } | |
e8fa9fb0 MD |
5079 | if (lttng_is_setuid_setgid()) { |
5080 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5081 | "-p, --pidfile"); | |
5082 | } else { | |
e6142f2e JG |
5083 | config_string_set(&config.pid_file_path, strdup(arg)); |
5084 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
5085 | PERROR("strdup"); |
5086 | ret = -ENOMEM; | |
5087 | } | |
db322c4d | 5088 | } |
c0232ea5 | 5089 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
5090 | if (!arg || *arg == '\0') { |
5091 | ret = -EINVAL; | |
5092 | goto end; | |
5093 | } | |
e8fa9fb0 MD |
5094 | if (lttng_is_setuid_setgid()) { |
5095 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5096 | "--agent-tcp-port"); | |
5097 | } else { | |
5098 | unsigned long v; | |
4d076222 | 5099 | |
e8fa9fb0 MD |
5100 | errno = 0; |
5101 | v = strtoul(arg, NULL, 0); | |
5102 | if (errno != 0 || !isdigit(arg[0])) { | |
5103 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
5104 | return -1; | |
5105 | } | |
5106 | if (v == 0 || v >= 65535) { | |
5107 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
5108 | return -1; | |
5109 | } | |
2288467f JG |
5110 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
5111 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 5112 | } |
c0232ea5 | 5113 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
5114 | if (!arg || *arg == '\0') { |
5115 | ret = -EINVAL; | |
5116 | goto end; | |
5117 | } | |
e8fa9fb0 MD |
5118 | if (lttng_is_setuid_setgid()) { |
5119 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5120 | "-l, --load"); | |
5121 | } else { | |
e6142f2e JG |
5122 | config_string_set(&config.load_session_path, strdup(arg)); |
5123 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
5124 | PERROR("strdup"); |
5125 | ret = -ENOMEM; | |
5126 | } | |
ef367a93 | 5127 | } |
c0232ea5 | 5128 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
5129 | if (!arg || *arg == '\0') { |
5130 | ret = -EINVAL; | |
5131 | goto end; | |
5132 | } | |
e8fa9fb0 MD |
5133 | if (lttng_is_setuid_setgid()) { |
5134 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5135 | "--kmod-probes"); | |
5136 | } else { | |
e6142f2e JG |
5137 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
5138 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
5139 | PERROR("strdup"); |
5140 | ret = -ENOMEM; | |
5141 | } | |
c9d42407 | 5142 | } |
c0232ea5 | 5143 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
5144 | if (!arg || *arg == '\0') { |
5145 | ret = -EINVAL; | |
5146 | goto end; | |
5147 | } | |
e8fa9fb0 MD |
5148 | if (lttng_is_setuid_setgid()) { |
5149 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5150 | "--extra-kmod-probes"); | |
5151 | } else { | |
e6142f2e JG |
5152 | config_string_set(&config.kmod_extra_probes_list, |
5153 | strdup(arg)); | |
5154 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
5155 | PERROR("strdup"); |
5156 | ret = -ENOMEM; | |
5157 | } | |
db322c4d | 5158 | } |
c0232ea5 JG |
5159 | } else if (string_match(optname, "config") || opt == 'f') { |
5160 | /* This is handled in set_options() thus silent skip. */ | |
5161 | goto end; | |
5162 | } else { | |
26296c48 JG |
5163 | /* Unknown option or other error. |
5164 | * Error is printed by getopt, just return */ | |
5165 | ret = -1; | |
5166 | } | |
5167 | ||
f3dd7ce7 | 5168 | end: |
c5d350b2 JG |
5169 | if (ret == -EINVAL) { |
5170 | const char *opt_name = "unknown"; | |
5171 | int i; | |
5172 | ||
5173 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
5174 | i++) { | |
5175 | if (opt == long_options[i].val) { | |
5176 | opt_name = long_options[i].name; | |
5177 | break; | |
5178 | } | |
5179 | } | |
5180 | ||
5181 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
5182 | opt_name); | |
5183 | } | |
f3dd7ce7 | 5184 | |
26296c48 JG |
5185 | return ret; |
5186 | } | |
5187 | ||
5188 | /* | |
5189 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 5190 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
5191 | * return value conventions. |
5192 | */ | |
5193 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
5194 | { | |
5195 | int ret = 0, i; | |
5196 | ||
5197 | if (!entry || !entry->name || !entry->value) { | |
5198 | ret = -EINVAL; | |
5199 | goto end; | |
5200 | } | |
5201 | ||
5202 | /* Check if the option is to be ignored */ | |
5203 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
5204 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
5205 | goto end; | |
5206 | } | |
5207 | } | |
5208 | ||
5209 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
5210 | i++) { | |
5211 | ||
5212 | /* Ignore if not fully matched. */ | |
5213 | if (strcmp(entry->name, long_options[i].name)) { | |
5214 | continue; | |
5215 | } | |
5216 | ||
5217 | /* | |
5218 | * If the option takes no argument on the command line, we have to | |
5219 | * check if the value is "true". We support non-zero numeric values, | |
5220 | * true, on and yes. | |
5221 | */ | |
5222 | if (!long_options[i].has_arg) { | |
5223 | ret = config_parse_value(entry->value); | |
5224 | if (ret <= 0) { | |
5225 | if (ret) { | |
5226 | WARN("Invalid configuration value \"%s\" for option %s", | |
5227 | entry->value, entry->name); | |
5228 | } | |
5229 | /* False, skip boolean config option. */ | |
5230 | goto end; | |
4d076222 | 5231 | } |
26296c48 JG |
5232 | } |
5233 | ||
5234 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
5235 | goto end; | |
5236 | } | |
5237 | ||
5238 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
5239 | ||
5240 | end: | |
5241 | return ret; | |
5242 | } | |
5243 | ||
5244 | /* | |
5245 | * daemon configuration loading and argument parsing | |
5246 | */ | |
5247 | static int set_options(int argc, char **argv) | |
5248 | { | |
5249 | int ret = 0, c = 0, option_index = 0; | |
5250 | int orig_optopt = optopt, orig_optind = optind; | |
5251 | char *optstring; | |
5252 | const char *config_path = NULL; | |
5253 | ||
5254 | optstring = utils_generate_optstring(long_options, | |
5255 | sizeof(long_options) / sizeof(struct option)); | |
5256 | if (!optstring) { | |
5257 | ret = -ENOMEM; | |
5258 | goto end; | |
5259 | } | |
5260 | ||
5261 | /* Check for the --config option */ | |
5262 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
5263 | &option_index)) != -1) { | |
5264 | if (c == '?') { | |
5265 | ret = -EINVAL; | |
5266 | goto end; | |
5267 | } else if (c != 'f') { | |
5268 | /* if not equal to --config option. */ | |
5269 | continue; | |
5270 | } | |
5271 | ||
e8fa9fb0 MD |
5272 | if (lttng_is_setuid_setgid()) { |
5273 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5274 | "-f, --config"); | |
5275 | } else { | |
5276 | config_path = utils_expand_path(optarg); | |
5277 | if (!config_path) { | |
5278 | ERR("Failed to resolve path: %s", optarg); | |
5279 | } | |
26296c48 JG |
5280 | } |
5281 | } | |
5282 | ||
5283 | ret = config_get_section_entries(config_path, config_section_name, | |
5284 | config_entry_handler, NULL); | |
5285 | if (ret) { | |
5286 | if (ret > 0) { | |
5287 | ERR("Invalid configuration option at line %i", ret); | |
5288 | ret = -1; | |
5289 | } | |
5290 | goto end; | |
5291 | } | |
5292 | ||
5293 | /* Reset getopt's global state */ | |
5294 | optopt = orig_optopt; | |
5295 | optind = orig_optind; | |
5296 | while (1) { | |
c0232ea5 JG |
5297 | option_index = -1; |
5298 | /* | |
5299 | * getopt_long() will not set option_index if it encounters a | |
5300 | * short option. | |
5301 | */ | |
5302 | c = getopt_long(argc, argv, optstring, long_options, | |
5303 | &option_index); | |
26296c48 | 5304 | if (c == -1) { |
4d076222 DG |
5305 | break; |
5306 | } | |
26296c48 | 5307 | |
c0232ea5 JG |
5308 | /* |
5309 | * Pass NULL as the long option name if popt left the index | |
5310 | * unset. | |
5311 | */ | |
5312 | ret = set_option(c, optarg, | |
5313 | option_index < 0 ? NULL : | |
5314 | long_options[option_index].name); | |
26296c48 JG |
5315 | if (ret < 0) { |
5316 | break; | |
fac6795d DG |
5317 | } |
5318 | } | |
5319 | ||
26296c48 JG |
5320 | end: |
5321 | free(optstring); | |
5322 | return ret; | |
fac6795d DG |
5323 | } |
5324 | ||
5325 | /* | |
d063d709 | 5326 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
5327 | * apps_sock - The communication socket for all UST apps. |
5328 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 5329 | */ |
cf3af59e | 5330 | static int init_daemon_socket(void) |
fac6795d DG |
5331 | { |
5332 | int ret = 0; | |
5333 | mode_t old_umask; | |
5334 | ||
5335 | old_umask = umask(0); | |
5336 | ||
5337 | /* Create client tool unix socket */ | |
e6142f2e | 5338 | client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value); |
d6f42150 | 5339 | if (client_sock < 0) { |
e6142f2e | 5340 | ERR("Create unix sock failed: %s", config.client_unix_sock_path.value); |
fac6795d DG |
5341 | ret = -1; |
5342 | goto end; | |
5343 | } | |
5344 | ||
b662582b DG |
5345 | /* Set the cloexec flag */ |
5346 | ret = utils_set_fd_cloexec(client_sock); | |
5347 | if (ret < 0) { | |
5348 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
5349 | "Continuing but note that the consumer daemon will have a " | |
5350 | "reference to this socket on exec()", client_sock); | |
5351 | } | |
5352 | ||
fac6795d | 5353 | /* File permission MUST be 660 */ |
e6142f2e | 5354 | ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
fac6795d | 5355 | if (ret < 0) { |
e6142f2e | 5356 | ERR("Set file permissions failed: %s", config.client_unix_sock_path.value); |
76d7553f | 5357 | PERROR("chmod"); |
fac6795d DG |
5358 | goto end; |
5359 | } | |
5360 | ||
5361 | /* Create the application unix socket */ | |
e6142f2e | 5362 | apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value); |
d6f42150 | 5363 | if (apps_sock < 0) { |
e6142f2e | 5364 | ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value); |
fac6795d DG |
5365 | ret = -1; |
5366 | goto end; | |
5367 | } | |
5368 | ||
b662582b DG |
5369 | /* Set the cloexec flag */ |
5370 | ret = utils_set_fd_cloexec(apps_sock); | |
5371 | if (ret < 0) { | |
5372 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
5373 | "Continuing but note that the consumer daemon will have a " | |
5374 | "reference to this socket on exec()", apps_sock); | |
5375 | } | |
5376 | ||
d6f42150 | 5377 | /* File permission MUST be 666 */ |
e6142f2e | 5378 | ret = chmod(config.apps_unix_sock_path.value, |
54d01ffb | 5379 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
fac6795d | 5380 | if (ret < 0) { |
e6142f2e | 5381 | ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value); |
76d7553f | 5382 | PERROR("chmod"); |
fac6795d DG |
5383 | goto end; |
5384 | } | |
5385 | ||
b662582b DG |
5386 | DBG3("Session daemon client socket %d and application socket %d created", |
5387 | client_sock, apps_sock); | |
5388 | ||
fac6795d DG |
5389 | end: |
5390 | umask(old_umask); | |
5391 | return ret; | |
5392 | } | |
5393 | ||
f472090a JG |
5394 | /* |
5395 | * Create lockfile using the rundir and return its fd. | |
5396 | */ | |
5397 | static int create_lockfile(void) | |
5398 | { | |
5399 | return utils_create_lock_file(config.lock_file_path.value); | |
5400 | } | |
5401 | ||
fac6795d | 5402 | /* |
54d01ffb DG |
5403 | * Check if the global socket is available, and if a daemon is answering at the |
5404 | * other side. If yes, error is returned. | |
f472090a JG |
5405 | * |
5406 | * Also attempts to create and hold the lock file. | |
fac6795d | 5407 | */ |
cf3af59e | 5408 | static int check_existing_daemon(void) |
fac6795d | 5409 | { |
f472090a JG |
5410 | int ret = 0; |
5411 | ||
7d8234d9 | 5412 | /* Is there anybody out there ? */ |
099e26bd | 5413 | if (lttng_session_daemon_alive()) { |
f472090a JG |
5414 | ret = -EEXIST; |
5415 | goto end; | |
099e26bd | 5416 | } |
b09c7c76 | 5417 | |
f472090a JG |
5418 | lockfile_fd = create_lockfile(); |
5419 | if (lockfile_fd < 0) { | |
5420 | ret = -EEXIST; | |
5421 | goto end; | |
5422 | } | |
5423 | end: | |
5424 | return ret; | |
5425 | } | |
5426 | ||
5427 | static void sessiond_cleanup_lock_file(void) | |
5428 | { | |
5429 | int ret; | |
5430 | ||
5431 | /* | |
5432 | * Cleanup lock file by deleting it and finaly closing it which will | |
5433 | * release the file system lock. | |
5434 | */ | |
5435 | if (lockfile_fd >= 0) { | |
5436 | ret = remove(config.lock_file_path.value); | |
5437 | if (ret < 0) { | |
5438 | PERROR("remove lock file"); | |
5439 | } | |
5440 | ret = close(lockfile_fd); | |
5441 | if (ret < 0) { | |
5442 | PERROR("close lock file"); | |
5443 | } | |
5444 | } | |
fac6795d DG |
5445 | } |
5446 | ||
fac6795d | 5447 | /* |
d063d709 | 5448 | * Set the tracing group gid onto the client socket. |
5e16da05 | 5449 | * |
d063d709 | 5450 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 5451 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 5452 | */ |
be040666 | 5453 | static int set_permissions(char *rundir) |
fac6795d DG |
5454 | { |
5455 | int ret; | |
996b65c8 | 5456 | gid_t gid; |
fac6795d | 5457 | |
e6142f2e | 5458 | gid = utils_get_group_id(config.tracing_group_name.value); |
fac6795d | 5459 | |
d6f42150 | 5460 | /* Set lttng run dir */ |
be040666 | 5461 | ret = chown(rundir, 0, gid); |
d6f42150 | 5462 | if (ret < 0) { |
be040666 | 5463 | ERR("Unable to set group on %s", rundir); |
76d7553f | 5464 | PERROR("chown"); |
d6f42150 DG |
5465 | } |
5466 | ||
6c71277b MD |
5467 | /* |
5468 | * Ensure all applications and tracing group can search the run | |
5469 | * dir. Allow everyone to read the directory, since it does not | |
5470 | * buy us anything to hide its content. | |
5471 | */ | |
5472 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
5473 | if (ret < 0) { |
5474 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 5475 | PERROR("chmod"); |
d1613cf5 JN |
5476 | } |
5477 | ||
d6f42150 | 5478 | /* lttng client socket path */ |
e6142f2e | 5479 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 5480 | if (ret < 0) { |
e6142f2e | 5481 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 5482 | PERROR("chown"); |
d6f42150 DG |
5483 | } |
5484 | ||
3bd1e081 | 5485 | /* kconsumer error socket path */ |
6c71277b | 5486 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 5487 | if (ret < 0) { |
3bd1e081 | 5488 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 5489 | PERROR("chown"); |
3bd1e081 MD |
5490 | } |
5491 | ||
7753dea8 | 5492 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 5493 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
5494 | if (ret < 0) { |
5495 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 5496 | PERROR("chown"); |
7753dea8 MD |
5497 | } |
5498 | ||
5499 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 5500 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 5501 | if (ret < 0) { |
7753dea8 | 5502 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 5503 | PERROR("chown"); |
fac6795d DG |
5504 | } |
5505 | ||
d6f42150 | 5506 | DBG("All permissions are set"); |
e07ae692 | 5507 | |
fac6795d DG |
5508 | return ret; |
5509 | } | |
5510 | ||
d6f42150 | 5511 | /* |
d063d709 | 5512 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 5513 | */ |
e6142f2e | 5514 | static int create_lttng_rundir(void) |
d6f42150 DG |
5515 | { |
5516 | int ret; | |
5517 | ||
e6142f2e | 5518 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 5519 | |
e6142f2e | 5520 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 5521 | if (ret < 0) { |
b1f11e69 | 5522 | if (errno != EEXIST) { |
e6142f2e | 5523 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
5524 | goto error; |
5525 | } else { | |
5526 | ret = 0; | |
5527 | } | |
d6f42150 DG |
5528 | } |
5529 | ||
5530 | error: | |
5531 | return ret; | |
5532 | } | |
5533 | ||
5534 | /* | |
e6142f2e | 5535 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 5536 | * session daemon. |
d6f42150 | 5537 | */ |
e6142f2e | 5538 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
5539 | { |
5540 | int ret; | |
e6142f2e | 5541 | char *path = NULL; |
d6f42150 | 5542 | |
6c71277b | 5543 | switch (consumer_data->type) { |
7753dea8 | 5544 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 5545 | path = config.kconsumerd_path.value; |
7753dea8 MD |
5546 | break; |
5547 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 5548 | path = config.consumerd64_path.value; |
7753dea8 MD |
5549 | break; |
5550 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 5551 | path = config.consumerd32_path.value; |
7753dea8 MD |
5552 | break; |
5553 | default: | |
5554 | ERR("Consumer type unknown"); | |
5555 | ret = -EINVAL; | |
5556 | goto error; | |
d6f42150 | 5557 | } |
e6142f2e | 5558 | assert(path); |
d6f42150 | 5559 | |
67e40797 DG |
5560 | DBG2("Creating consumer directory: %s", path); |
5561 | ||
6c71277b | 5562 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
5563 | if (ret < 0 && errno != EEXIST) { |
5564 | PERROR("mkdir"); | |
5565 | ERR("Failed to create %s", path); | |
5566 | goto error; | |
d6f42150 | 5567 | } |
6c71277b | 5568 | if (is_root) { |
e6142f2e | 5569 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
6c71277b MD |
5570 | if (ret < 0) { |
5571 | ERR("Unable to set group on %s", path); | |
5572 | PERROR("chown"); | |
5573 | goto error; | |
5574 | } | |
5575 | } | |
d6f42150 | 5576 | |
e6142f2e | 5577 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
5578 | consumer_data->err_sock = |
5579 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
5580 | if (consumer_data->err_sock < 0) { | |
5581 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
5582 | ret = -1; |
5583 | goto error; | |
5584 | } | |
5585 | ||
a24f05ab MD |
5586 | /* |
5587 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
5588 | * show must go on. | |
5589 | */ | |
5590 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
5591 | if (ret < 0) { | |
5592 | PERROR("utils_set_fd_cloexec"); | |
5593 | /* continue anyway */ | |
5594 | } | |
5595 | ||
d6f42150 | 5596 | /* File permission MUST be 660 */ |
3bd1e081 | 5597 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 5598 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 5599 | if (ret < 0) { |
3bd1e081 | 5600 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 5601 | PERROR("chmod"); |
d6f42150 DG |
5602 | goto error; |
5603 | } | |
5604 | ||
5605 | error: | |
5606 | return ret; | |
5607 | } | |
5608 | ||
fac6795d | 5609 | /* |
d063d709 | 5610 | * Signal handler for the daemon |
cf3af59e | 5611 | * |
54d01ffb DG |
5612 | * Simply stop all worker threads, leaving main() return gracefully after |
5613 | * joining all threads and calling cleanup(). | |
fac6795d DG |
5614 | */ |
5615 | static void sighandler(int sig) | |
5616 | { | |
5617 | switch (sig) { | |
cf3af59e | 5618 | case SIGINT: |
af87c45a | 5619 | DBG("SIGINT caught"); |
cf3af59e MD |
5620 | stop_threads(); |
5621 | break; | |
5622 | case SIGTERM: | |
af87c45a | 5623 | DBG("SIGTERM caught"); |
cf3af59e MD |
5624 | stop_threads(); |
5625 | break; | |
0bb7724a DG |
5626 | case SIGUSR1: |
5627 | CMM_STORE_SHARED(recv_child_signal, 1); | |
5628 | break; | |
cf3af59e MD |
5629 | default: |
5630 | break; | |
fac6795d | 5631 | } |
fac6795d DG |
5632 | } |
5633 | ||
5634 | /* | |
d063d709 | 5635 | * Setup signal handler for : |
1d4b027a | 5636 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 5637 | */ |
1d4b027a | 5638 | static int set_signal_handler(void) |
fac6795d | 5639 | { |
1d4b027a DG |
5640 | int ret = 0; |
5641 | struct sigaction sa; | |
5642 | sigset_t sigset; | |
fac6795d | 5643 | |
1d4b027a | 5644 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 5645 | PERROR("sigemptyset"); |
1d4b027a DG |
5646 | return ret; |
5647 | } | |
d6f42150 | 5648 | |
1d4b027a DG |
5649 | sa.sa_mask = sigset; |
5650 | sa.sa_flags = 0; | |
0072e5e2 MD |
5651 | |
5652 | sa.sa_handler = sighandler; | |
1d4b027a | 5653 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 5654 | PERROR("sigaction"); |
1d4b027a | 5655 | return ret; |
d6f42150 DG |
5656 | } |
5657 | ||
1d4b027a | 5658 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 5659 | PERROR("sigaction"); |
1d4b027a | 5660 | return ret; |
d6f42150 | 5661 | } |
aaf26714 | 5662 | |
0072e5e2 | 5663 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 5664 | PERROR("sigaction"); |
1d4b027a | 5665 | return ret; |
8c0faa1d DG |
5666 | } |
5667 | ||
0072e5e2 MD |
5668 | sa.sa_handler = SIG_IGN; |
5669 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
5670 | PERROR("sigaction"); |
5671 | return ret; | |
5672 | } | |
5673 | ||
5674 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
5675 | |
5676 | return ret; | |
fac6795d DG |
5677 | } |
5678 | ||
f3ed775e | 5679 | /* |
d063d709 | 5680 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 5681 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
5682 | */ |
5683 | static void set_ulimit(void) | |
5684 | { | |
5685 | int ret; | |
5686 | struct rlimit lim; | |
5687 | ||
514bb9f0 | 5688 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
5689 | lim.rlim_cur = 65535; |
5690 | lim.rlim_max = 65535; | |
5691 | ||
5692 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
5693 | if (ret < 0) { | |
76d7553f | 5694 | PERROR("failed to set open files limit"); |
f3ed775e DG |
5695 | } |
5696 | } | |
5697 | ||
4a15001e | 5698 | static int write_pidfile(void) |
35f90c40 | 5699 | { |
e6142f2e | 5700 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
5701 | } |
5702 | ||
e6142f2e JG |
5703 | static int set_clock_plugin_env(void) |
5704 | { | |
5705 | int ret = 0; | |
5706 | char *env_value = NULL; | |
cd9290dd | 5707 | |
e6142f2e JG |
5708 | if (!config.lttng_ust_clock_plugin.value) { |
5709 | goto end; | |
5710 | } | |
5711 | ||
5712 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
5713 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 5714 | if (ret < 0) { |
e6142f2e JG |
5715 | PERROR("asprintf"); |
5716 | goto end; | |
cd9290dd DG |
5717 | } |
5718 | ||
e6142f2e JG |
5719 | ret = putenv(env_value); |
5720 | if (ret) { | |
5721 | free(env_value); | |
5722 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
5723 | goto end; | |
5724 | } | |
cd9290dd | 5725 | |
e6142f2e JG |
5726 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
5727 | config.lttng_ust_clock_plugin.value); | |
5728 | end: | |
4a15001e | 5729 | return ret; |
ef367a93 JG |
5730 | } |
5731 | ||
d086f507 JD |
5732 | static |
5733 | struct rotation_thread_timer_queue *create_rotate_timer_queue(void) | |
5734 | { | |
5735 | struct rotation_thread_timer_queue *queue = NULL; | |
5736 | ||
5737 | queue = zmalloc(sizeof(struct rotation_thread_timer_queue)); | |
5738 | if (!queue) { | |
5739 | PERROR("Failed to allocate timer rotate queue"); | |
5740 | goto end; | |
5741 | } | |
5742 | ||
5743 | queue->event_pipe = lttng_pipe_open(FD_CLOEXEC | O_NONBLOCK); | |
5744 | CDS_INIT_LIST_HEAD(&queue->list); | |
5745 | pthread_mutex_init(&queue->lock, NULL); | |
5746 | ||
5747 | end: | |
5748 | return queue; | |
5749 | } | |
5750 | ||
5751 | static | |
5752 | void destroy_rotate_timer_queue(struct rotation_thread_timer_queue *queue) | |
5753 | { | |
5754 | struct sessiond_rotation_timer *node, *tmp_node; | |
5755 | ||
5756 | if (!queue) { | |
5757 | return; | |
5758 | } | |
5759 | ||
5760 | lttng_pipe_destroy(queue->event_pipe); | |
5761 | ||
5762 | pthread_mutex_lock(&queue->lock); | |
5763 | /* Empty wait queue. */ | |
5764 | cds_list_for_each_entry_safe(node, tmp_node, &queue->list, head) { | |
5765 | cds_list_del(&node->head); | |
5766 | free(node); | |
5767 | } | |
5768 | pthread_mutex_unlock(&queue->lock); | |
5769 | ||
5770 | pthread_mutex_destroy(&queue->lock); | |
5771 | free(queue); | |
5772 | } | |
5773 | ||
fac6795d DG |
5774 | /* |
5775 | * main | |
5776 | */ | |
5777 | int main(int argc, char **argv) | |
5778 | { | |
4a15001e | 5779 | int ret = 0, retval = 0; |
fac6795d | 5780 | void *status; |
e6142f2e | 5781 | const char *env_app_timeout; |
b3530820 JG |
5782 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
5783 | *ust64_channel_monitor_pipe = NULL, | |
5784 | *kernel_channel_monitor_pipe = NULL; | |
99611076 JG |
5785 | bool notification_thread_launched = false; |
5786 | bool rotation_thread_launched = false; | |
5787 | bool timer_thread_launched = false; | |
62c43103 JD |
5788 | struct lttng_pipe *ust32_channel_rotate_pipe = NULL, |
5789 | *ust64_channel_rotate_pipe = NULL, | |
5790 | *kernel_channel_rotate_pipe = NULL; | |
d086f507 JD |
5791 | struct timer_thread_parameters timer_thread_ctx; |
5792 | /* Queue of rotation jobs populated by the sessiond-timer. */ | |
5793 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
90936dcf | 5794 | sem_t notification_thread_ready; |
fac6795d | 5795 | |
335a95b7 MD |
5796 | init_kernel_workarounds(); |
5797 | ||
f6a9efaa DG |
5798 | rcu_register_thread(); |
5799 | ||
4a15001e MD |
5800 | if (set_signal_handler()) { |
5801 | retval = -1; | |
5802 | goto exit_set_signal_handler; | |
0bb7724a DG |
5803 | } |
5804 | ||
d086f507 JD |
5805 | if (sessiond_timer_signal_init()) { |
5806 | retval = -1; | |
5807 | goto exit_set_signal_handler; | |
5808 | } | |
5809 | ||
12744796 DG |
5810 | page_size = sysconf(_SC_PAGESIZE); |
5811 | if (page_size < 0) { | |
5812 | PERROR("sysconf _SC_PAGESIZE"); | |
5813 | page_size = LONG_MAX; | |
5814 | WARN("Fallback page size to %ld", page_size); | |
5815 | } | |
5816 | ||
e6142f2e JG |
5817 | ret = sessiond_config_init(&config); |
5818 | if (ret) { | |
5819 | retval = -1; | |
5820 | goto exit_set_signal_handler; | |
5821 | } | |
5822 | ||
2788b494 JR |
5823 | /* |
5824 | * Init config from environment variables. | |
5825 | * Command line option override env configuration per-doc. Do env first. | |
5826 | */ | |
5827 | sessiond_config_apply_env_config(&config); | |
5828 | ||
4a15001e MD |
5829 | /* |
5830 | * Parse arguments and load the daemon configuration file. | |
5831 | * | |
5832 | * We have an exit_options exit path to free memory reserved by | |
5833 | * set_options. This is needed because the rest of sessiond_cleanup() | |
5834 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
5835 | * depends on set_options. | |
5836 | */ | |
fac6795d | 5837 | progname = argv[0]; |
4a15001e MD |
5838 | if (set_options(argc, argv)) { |
5839 | retval = -1; | |
5840 | goto exit_options; | |
fac6795d DG |
5841 | } |
5842 | ||
e6142f2e JG |
5843 | /* |
5844 | * Resolve all paths received as arguments, configuration option, or | |
5845 | * through environment variable as absolute paths. This is necessary | |
5846 | * since daemonizing causes the sessiond's current working directory | |
5847 | * to '/'. | |
5848 | */ | |
5849 | ret = sessiond_config_resolve_paths(&config); | |
5850 | if (ret) { | |
5851 | goto exit_options; | |
5852 | } | |
5853 | ||
5854 | /* Apply config. */ | |
5855 | lttng_opt_verbose = config.verbose; | |
5856 | lttng_opt_quiet = config.quiet; | |
5857 | kconsumer_data.err_unix_sock_path = | |
5858 | config.kconsumerd_err_unix_sock_path.value; | |
5859 | kconsumer_data.cmd_unix_sock_path = | |
5860 | config.kconsumerd_cmd_unix_sock_path.value; | |
5861 | ustconsumer32_data.err_unix_sock_path = | |
5862 | config.consumerd32_err_unix_sock_path.value; | |
5863 | ustconsumer32_data.cmd_unix_sock_path = | |
5864 | config.consumerd32_cmd_unix_sock_path.value; | |
5865 | ustconsumer64_data.err_unix_sock_path = | |
5866 | config.consumerd64_err_unix_sock_path.value; | |
5867 | ustconsumer64_data.cmd_unix_sock_path = | |
5868 | config.consumerd64_cmd_unix_sock_path.value; | |
5869 | set_clock_plugin_env(); | |
5870 | ||
5871 | sessiond_config_log(&config); | |
5872 | ||
f472090a JG |
5873 | if (create_lttng_rundir()) { |
5874 | retval = -1; | |
5875 | goto exit_options; | |
5876 | } | |
5877 | ||
5878 | /* Abort launch if a session daemon is already running. */ | |
5879 | if (check_existing_daemon()) { | |
5880 | ERR("A session daemon is already running."); | |
5881 | retval = -1; | |
5882 | goto exit_options; | |
5883 | } | |
5884 | ||
fac6795d | 5885 | /* Daemonize */ |
e6142f2e | 5886 | if (config.daemonize || config.background) { |
ceed52b5 MD |
5887 | int i; |
5888 | ||
72dd7491 | 5889 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 5890 | !config.background); |
53094c05 | 5891 | if (ret < 0) { |
4a15001e MD |
5892 | retval = -1; |
5893 | goto exit_options; | |
53094c05 | 5894 | } |
0bb7724a | 5895 | |
ceed52b5 | 5896 | /* |
0bb7724a | 5897 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 5898 | * closed, in case we are called with more opened file |
f472090a | 5899 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
5900 | */ |
5901 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
5902 | if (i == lockfile_fd) { |
5903 | continue; | |
5904 | } | |
ceed52b5 MD |
5905 | (void) close(i); |
5906 | } | |
5907 | } | |
5908 | ||
7567352f MD |
5909 | if (run_as_create_worker(argv[0]) < 0) { |
5910 | goto exit_create_run_as_worker_cleanup; | |
5911 | } | |
5912 | ||
4a15001e MD |
5913 | /* |
5914 | * Starting from here, we can create threads. This needs to be after | |
5915 | * lttng_daemonize due to RCU. | |
5916 | */ | |
5917 | ||
5918 | /* | |
5919 | * Initialize the health check subsystem. This call should set the | |
5920 | * appropriate time values. | |
5921 | */ | |
5922 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
5923 | if (!health_sessiond) { | |
5924 | PERROR("health_app_create error"); | |
5925 | retval = -1; | |
5926 | goto exit_health_sessiond_cleanup; | |
5927 | } | |
5928 | ||
4a15001e | 5929 | /* Create thread to clean up RCU hash tables */ |
5e97de00 | 5930 | if (init_ht_cleanup_thread(&ht_cleanup_thread)) { |
4a15001e MD |
5931 | retval = -1; |
5932 | goto exit_ht_cleanup; | |
5933 | } | |
5934 | ||
ceed52b5 | 5935 | /* Create thread quit pipe */ |
4a15001e MD |
5936 | if (init_thread_quit_pipe()) { |
5937 | retval = -1; | |
5938 | goto exit_init_data; | |
fac6795d DG |
5939 | } |
5940 | ||
5941 | /* Check if daemon is UID = 0 */ | |
5942 | is_root = !getuid(); | |
fac6795d | 5943 | if (is_root) { |
67e40797 | 5944 | /* Create global run dir with root access */ |
67e40797 | 5945 | |
b3530820 JG |
5946 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
5947 | if (!kernel_channel_monitor_pipe) { | |
5948 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
5949 | retval = -1; | |
5950 | goto exit_init_data; | |
5951 | } | |
5952 | kconsumer_data.channel_monitor_pipe = | |
5953 | lttng_pipe_release_writefd( | |
5954 | kernel_channel_monitor_pipe); | |
5955 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
5956 | retval = -1; | |
5957 | goto exit_init_data; | |
5958 | } | |
62c43103 JD |
5959 | kernel_channel_rotate_pipe = lttng_pipe_open(0); |
5960 | if (!kernel_channel_rotate_pipe) { | |
5961 | ERR("Failed to create kernel consumer channel rotate pipe"); | |
5962 | retval = -1; | |
5963 | goto exit_init_data; | |
5964 | } | |
5965 | kconsumer_data.channel_rotate_pipe = | |
5966 | lttng_pipe_release_writefd( | |
5967 | kernel_channel_rotate_pipe); | |
5968 | if (kconsumer_data.channel_rotate_pipe < 0) { | |
5969 | retval = -1; | |
5970 | goto exit_init_data; | |
5971 | } | |
fac6795d DG |
5972 | } |
5973 | ||
5c827ce0 DG |
5974 | /* Set consumer initial state */ |
5975 | kernel_consumerd_state = CONSUMER_STOPPED; | |
5976 | ust_consumerd_state = CONSUMER_STOPPED; | |
5977 | ||
b3530820 JG |
5978 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
5979 | if (!ust32_channel_monitor_pipe) { | |
5980 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
5981 | retval = -1; | |
5982 | goto exit_init_data; | |
5983 | } | |
5984 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
5985 | ust32_channel_monitor_pipe); | |
5986 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
5987 | retval = -1; | |
5988 | goto exit_init_data; | |
5989 | } | |
62c43103 JD |
5990 | ust32_channel_rotate_pipe = lttng_pipe_open(0); |
5991 | if (!ust32_channel_rotate_pipe) { | |
5992 | ERR("Failed to create 32-bit user space consumer channel rotate pipe"); | |
5993 | retval = -1; | |
5994 | goto exit_init_data; | |
5995 | } | |
5996 | ustconsumer32_data.channel_rotate_pipe = lttng_pipe_release_writefd( | |
5997 | ust32_channel_rotate_pipe); | |
5998 | if (ustconsumer32_data.channel_rotate_pipe < 0) { | |
5999 | retval = -1; | |
6000 | goto exit_init_data; | |
6001 | } | |
6002 | ||
d086f507 JD |
6003 | /* |
6004 | * The rotation_timer_queue structure is shared between the sessiond timer | |
6005 | * thread and the rotation thread. The main() keeps the ownership and | |
6006 | * destroys it when both threads have quit. | |
6007 | */ | |
6008 | rotation_timer_queue = create_rotate_timer_queue(); | |
6009 | if (!rotation_timer_queue) { | |
6010 | retval = -1; | |
6011 | goto exit_init_data; | |
6012 | } | |
6013 | timer_thread_ctx.rotation_timer_queue = rotation_timer_queue; | |
67e40797 | 6014 | |
b3530820 JG |
6015 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
6016 | if (!ust64_channel_monitor_pipe) { | |
6017 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
6018 | retval = -1; | |
6019 | goto exit_init_data; | |
6020 | } | |
6021 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
6022 | ust64_channel_monitor_pipe); | |
6023 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
6024 | retval = -1; | |
6025 | goto exit_init_data; | |
6026 | } | |
62c43103 JD |
6027 | ust64_channel_rotate_pipe = lttng_pipe_open(0); |
6028 | if (!ust64_channel_rotate_pipe) { | |
6029 | ERR("Failed to create 64-bit user space consumer channel rotate pipe"); | |
6030 | retval = -1; | |
6031 | goto exit_init_data; | |
6032 | } | |
6033 | ustconsumer64_data.channel_rotate_pipe = lttng_pipe_release_writefd( | |
6034 | ust64_channel_rotate_pipe); | |
6035 | if (ustconsumer64_data.channel_rotate_pipe < 0) { | |
6036 | retval = -1; | |
6037 | goto exit_init_data; | |
6038 | } | |
847177cd | 6039 | |
1427f9b2 DG |
6040 | /* |
6041 | * Init UST app hash table. Alloc hash table before this point since | |
6042 | * cleanup() can get called after that point. | |
6043 | */ | |
4a15001e | 6044 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 6045 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
6046 | retval = -1; |
6047 | goto exit_init_data; | |
6048 | } | |
1427f9b2 | 6049 | |
6a4e4039 JG |
6050 | /* |
6051 | * Initialize agent app hash table. We allocate the hash table here | |
6052 | * since cleanup() can get called after this point. | |
6053 | */ | |
6054 | if (agent_app_ht_alloc()) { | |
6055 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
6056 | retval = -1; |
6057 | goto exit_init_data; | |
f20baf8e DG |
6058 | } |
6059 | ||
a88df331 DG |
6060 | /* |
6061 | * These actions must be executed as root. We do that *after* setting up | |
6062 | * the sockets path because we MUST make the check for another daemon using | |
6063 | * those paths *before* trying to set the kernel consumer sockets and init | |
6064 | * kernel tracer. | |
6065 | */ | |
6066 | if (is_root) { | |
e6142f2e | 6067 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e MD |
6068 | retval = -1; |
6069 | goto exit_init_data; | |
7753dea8 MD |
6070 | } |
6071 | ||
a88df331 | 6072 | /* Setup kernel tracer */ |
e6142f2e | 6073 | if (!config.no_kernel) { |
4fba7219 | 6074 | init_kernel_tracer(); |
89588270 DG |
6075 | if (kernel_tracer_fd >= 0) { |
6076 | ret = syscall_init_table(); | |
6077 | if (ret < 0) { | |
4a15001e MD |
6078 | ERR("Unable to populate syscall table. " |
6079 | "Syscall tracing won't work " | |
6080 | "for this session daemon."); | |
89588270 | 6081 | } |
834978fd | 6082 | } |
4fba7219 | 6083 | } |
a88df331 DG |
6084 | |
6085 | /* Set ulimit for open files */ | |
6086 | set_ulimit(); | |
fac6795d | 6087 | } |
4063050c MD |
6088 | /* init lttng_fd tracking must be done after set_ulimit. */ |
6089 | lttng_fd_init(); | |
fac6795d | 6090 | |
e6142f2e | 6091 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e MD |
6092 | retval = -1; |
6093 | goto exit_init_data; | |
67e40797 DG |
6094 | } |
6095 | ||
e6142f2e | 6096 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e MD |
6097 | retval = -1; |
6098 | goto exit_init_data; | |
67e40797 DG |
6099 | } |
6100 | ||
d6f42150 | 6101 | /* Setup the needed unix socket */ |
4a15001e MD |
6102 | if (init_daemon_socket()) { |
6103 | retval = -1; | |
6104 | goto exit_init_data; | |
fac6795d DG |
6105 | } |
6106 | ||
6107 | /* Set credentials to socket */ | |
e6142f2e | 6108 | if (is_root && set_permissions(config.rundir.value)) { |
4a15001e MD |
6109 | retval = -1; |
6110 | goto exit_init_data; | |
fac6795d DG |
6111 | } |
6112 | ||
5b8719f5 | 6113 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 6114 | if (config.sig_parent) { |
5b8719f5 DG |
6115 | ppid = getppid(); |
6116 | } | |
6117 | ||
7a485870 | 6118 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 6119 | if (is_root && !config.no_kernel) { |
4a15001e MD |
6120 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
6121 | retval = -1; | |
6122 | goto exit_init_data; | |
6620da75 | 6123 | } |
7a485870 DG |
6124 | } |
6125 | ||
099e26bd | 6126 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
6127 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
6128 | retval = -1; | |
6129 | goto exit_init_data; | |
099e26bd DG |
6130 | } |
6131 | ||
d0b96690 | 6132 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
6133 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
6134 | retval = -1; | |
6135 | goto exit_init_data; | |
d0b96690 DG |
6136 | } |
6137 | ||
7972aab2 DG |
6138 | /* Initialize global buffer per UID and PID registry. */ |
6139 | buffer_reg_init_uid_registry(); | |
6140 | buffer_reg_init_pid_registry(); | |
6141 | ||
099e26bd | 6142 | /* Init UST command queue. */ |
8bdee6e2 | 6143 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 6144 | |
273ea72c | 6145 | /* |
4a15001e MD |
6146 | * Get session list pointer. This pointer MUST NOT be free'd. This list |
6147 | * is statically declared in session.c | |
273ea72c | 6148 | */ |
54d01ffb | 6149 | session_list_ptr = session_get_list(); |
b5541356 | 6150 | |
2f77fc4b | 6151 | cmd_init(); |
00e2e675 | 6152 | |
ae9e45b3 DG |
6153 | /* Check for the application socket timeout env variable. */ |
6154 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
6155 | if (env_app_timeout) { | |
e6142f2e | 6156 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 6157 | } else { |
e6142f2e | 6158 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
6159 | } |
6160 | ||
4a15001e MD |
6161 | ret = write_pidfile(); |
6162 | if (ret) { | |
6163 | ERR("Error in write_pidfile"); | |
6164 | retval = -1; | |
6165 | goto exit_init_data; | |
6166 | } | |
35f90c40 | 6167 | |
554831e7 MD |
6168 | /* Initialize communication library */ |
6169 | lttcomm_init(); | |
4a15001e | 6170 | /* Initialize TCP timeout values */ |
d831c249 | 6171 | lttcomm_inet_init(); |
554831e7 | 6172 | |
ef367a93 | 6173 | if (load_session_init_data(&load_info) < 0) { |
4a15001e MD |
6174 | retval = -1; |
6175 | goto exit_init_data; | |
ef367a93 | 6176 | } |
e6142f2e | 6177 | load_info->path = config.load_session_path.value; |
ef367a93 | 6178 | |
12b4fb37 | 6179 | /* Create health-check thread. */ |
1a1a34b4 | 6180 | ret = pthread_create(&health_thread, default_pthread_attr(), |
44a5e5eb | 6181 | thread_manage_health, (void *) NULL); |
4a15001e MD |
6182 | if (ret) { |
6183 | errno = ret; | |
44a5e5eb | 6184 | PERROR("pthread_create health"); |
4a15001e | 6185 | retval = -1; |
44a5e5eb DG |
6186 | goto exit_health; |
6187 | } | |
6188 | ||
90936dcf JD |
6189 | /* |
6190 | * The rotation thread needs the notification thread to be ready before | |
6191 | * creating the rotate_notification_channel, so we use this semaphore as | |
6192 | * a rendez-vous point. | |
6193 | */ | |
6194 | sem_init(¬ification_thread_ready, 0, 0); | |
6195 | ||
b3530820 JG |
6196 | /* notification_thread_data acquires the pipes' read side. */ |
6197 | notification_thread_handle = notification_thread_handle_create( | |
6198 | ust32_channel_monitor_pipe, | |
6199 | ust64_channel_monitor_pipe, | |
90936dcf JD |
6200 | kernel_channel_monitor_pipe, |
6201 | ¬ification_thread_ready); | |
b3530820 JG |
6202 | if (!notification_thread_handle) { |
6203 | retval = -1; | |
6204 | ERR("Failed to create notification thread shared data"); | |
6205 | stop_threads(); | |
6206 | goto exit_notification; | |
6207 | } | |
6208 | ||
6209 | /* Create notification thread. */ | |
6210 | ret = pthread_create(¬ification_thread, default_pthread_attr(), | |
6211 | thread_notification, notification_thread_handle); | |
6212 | if (ret) { | |
6213 | errno = ret; | |
6214 | PERROR("pthread_create notification"); | |
6215 | retval = -1; | |
6216 | stop_threads(); | |
6217 | goto exit_notification; | |
6218 | } | |
99611076 | 6219 | notification_thread_launched = true; |
b3530820 | 6220 | |
d086f507 JD |
6221 | /* Create timer thread. */ |
6222 | ret = pthread_create(&timer_thread, default_pthread_attr(), | |
6223 | sessiond_timer_thread, &timer_thread_ctx); | |
6224 | if (ret) { | |
6225 | errno = ret; | |
6226 | PERROR("pthread_create timer"); | |
6227 | retval = -1; | |
6228 | stop_threads(); | |
6229 | goto exit_notification; | |
6230 | } | |
99611076 | 6231 | timer_thread_launched = true; |
d086f507 | 6232 | |
db66e574 JD |
6233 | /* rotation_thread_data acquires the pipes' read side. */ |
6234 | rotation_thread_handle = rotation_thread_handle_create( | |
6235 | ust32_channel_rotate_pipe, | |
6236 | ust64_channel_rotate_pipe, | |
6237 | kernel_channel_rotate_pipe, | |
d086f507 | 6238 | thread_quit_pipe[0], |
90936dcf JD |
6239 | rotation_timer_queue, |
6240 | notification_thread_handle, | |
6241 | ¬ification_thread_ready); | |
db66e574 JD |
6242 | if (!rotation_thread_handle) { |
6243 | retval = -1; | |
6244 | ERR("Failed to create rotation thread shared data"); | |
6245 | stop_threads(); | |
6246 | goto exit_rotation; | |
6247 | } | |
db66e574 JD |
6248 | |
6249 | /* Create rotation thread. */ | |
6250 | ret = pthread_create(&rotation_thread, default_pthread_attr(), | |
6251 | thread_rotation, rotation_thread_handle); | |
6252 | if (ret) { | |
6253 | errno = ret; | |
6254 | PERROR("pthread_create rotation"); | |
6255 | retval = -1; | |
6256 | stop_threads(); | |
6257 | goto exit_rotation; | |
6258 | } | |
99611076 | 6259 | rotation_thread_launched = true; |
db66e574 | 6260 | |
cf3af59e | 6261 | /* Create thread to manage the client socket */ |
1a1a34b4 | 6262 | ret = pthread_create(&client_thread, default_pthread_attr(), |
099e26bd | 6263 | thread_manage_clients, (void *) NULL); |
4a15001e MD |
6264 | if (ret) { |
6265 | errno = ret; | |
76d7553f | 6266 | PERROR("pthread_create clients"); |
4a15001e | 6267 | retval = -1; |
b3530820 | 6268 | stop_threads(); |
cf3af59e MD |
6269 | goto exit_client; |
6270 | } | |
fac6795d | 6271 | |
099e26bd | 6272 | /* Create thread to dispatch registration */ |
1a1a34b4 | 6273 | ret = pthread_create(&dispatch_thread, default_pthread_attr(), |
099e26bd | 6274 | thread_dispatch_ust_registration, (void *) NULL); |
4a15001e MD |
6275 | if (ret) { |
6276 | errno = ret; | |
76d7553f | 6277 | PERROR("pthread_create dispatch"); |
4a15001e | 6278 | retval = -1; |
b3530820 | 6279 | stop_threads(); |
099e26bd DG |
6280 | goto exit_dispatch; |
6281 | } | |
6282 | ||
6283 | /* Create thread to manage application registration. */ | |
1a1a34b4 | 6284 | ret = pthread_create(®_apps_thread, default_pthread_attr(), |
099e26bd | 6285 | thread_registration_apps, (void *) NULL); |
4a15001e MD |
6286 | if (ret) { |
6287 | errno = ret; | |
76d7553f | 6288 | PERROR("pthread_create registration"); |
4a15001e | 6289 | retval = -1; |
b3530820 | 6290 | stop_threads(); |
099e26bd DG |
6291 | goto exit_reg_apps; |
6292 | } | |
6293 | ||
cf3af59e | 6294 | /* Create thread to manage application socket */ |
1a1a34b4 | 6295 | ret = pthread_create(&apps_thread, default_pthread_attr(), |
54d01ffb | 6296 | thread_manage_apps, (void *) NULL); |
4a15001e MD |
6297 | if (ret) { |
6298 | errno = ret; | |
d0b96690 | 6299 | PERROR("pthread_create apps"); |
4a15001e | 6300 | retval = -1; |
b3530820 | 6301 | stop_threads(); |
d0b96690 DG |
6302 | goto exit_apps; |
6303 | } | |
6304 | ||
6305 | /* Create thread to manage application notify socket */ | |
1a1a34b4 | 6306 | ret = pthread_create(&apps_notify_thread, default_pthread_attr(), |
d0b96690 | 6307 | ust_thread_manage_notify, (void *) NULL); |
4a15001e MD |
6308 | if (ret) { |
6309 | errno = ret; | |
f5c32ef1 | 6310 | PERROR("pthread_create notify"); |
4a15001e | 6311 | retval = -1; |
b3530820 | 6312 | stop_threads(); |
9563b0ad | 6313 | goto exit_apps_notify; |
cf3af59e | 6314 | } |
fac6795d | 6315 | |
022d91ba | 6316 | /* Create agent registration thread. */ |
1a1a34b4 | 6317 | ret = pthread_create(&agent_reg_thread, default_pthread_attr(), |
022d91ba | 6318 | agent_thread_manage_registration, (void *) NULL); |
4a15001e MD |
6319 | if (ret) { |
6320 | errno = ret; | |
022d91ba | 6321 | PERROR("pthread_create agent"); |
4a15001e | 6322 | retval = -1; |
b3530820 | 6323 | stop_threads(); |
022d91ba | 6324 | goto exit_agent_reg; |
4d076222 DG |
6325 | } |
6326 | ||
6620da75 | 6327 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 6328 | if (is_root && !config.no_kernel) { |
6620da75 | 6329 | /* Create kernel thread to manage kernel event */ |
1a1a34b4 | 6330 | ret = pthread_create(&kernel_thread, default_pthread_attr(), |
6620da75 | 6331 | thread_manage_kernel, (void *) NULL); |
4a15001e MD |
6332 | if (ret) { |
6333 | errno = ret; | |
6620da75 | 6334 | PERROR("pthread_create kernel"); |
4a15001e | 6335 | retval = -1; |
b3530820 | 6336 | stop_threads(); |
6620da75 DG |
6337 | goto exit_kernel; |
6338 | } | |
ef367a93 | 6339 | } |
7a485870 | 6340 | |
4a15001e | 6341 | /* Create session loading thread. */ |
1a1a34b4 MJ |
6342 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
6343 | thread_load_session, load_info); | |
4a15001e MD |
6344 | if (ret) { |
6345 | errno = ret; | |
6346 | PERROR("pthread_create load_session_thread"); | |
6347 | retval = -1; | |
b3530820 | 6348 | stop_threads(); |
4a15001e MD |
6349 | goto exit_load_session; |
6350 | } | |
6351 | ||
6352 | /* | |
6353 | * This is where we start awaiting program completion (e.g. through | |
6354 | * signal that asks threads to teardown). | |
6355 | */ | |
6356 | ||
6357 | ret = pthread_join(load_session_thread, &status); | |
6358 | if (ret) { | |
6359 | errno = ret; | |
6360 | PERROR("pthread_join load_session_thread"); | |
6361 | retval = -1; | |
6362 | } | |
6363 | exit_load_session: | |
ef367a93 | 6364 | |
e6142f2e | 6365 | if (is_root && !config.no_kernel) { |
6620da75 | 6366 | ret = pthread_join(kernel_thread, &status); |
4a15001e MD |
6367 | if (ret) { |
6368 | errno = ret; | |
6620da75 | 6369 | PERROR("pthread_join"); |
4a15001e | 6370 | retval = -1; |
6620da75 | 6371 | } |
fac6795d | 6372 | } |
cf3af59e | 6373 | exit_kernel: |
4a15001e | 6374 | |
022d91ba | 6375 | ret = pthread_join(agent_reg_thread, &status); |
4a15001e MD |
6376 | if (ret) { |
6377 | errno = ret; | |
022d91ba | 6378 | PERROR("pthread_join agent"); |
4a15001e | 6379 | retval = -1; |
4d076222 | 6380 | } |
022d91ba | 6381 | exit_agent_reg: |
4a15001e | 6382 | |
9563b0ad | 6383 | ret = pthread_join(apps_notify_thread, &status); |
4a15001e MD |
6384 | if (ret) { |
6385 | errno = ret; | |
9563b0ad | 6386 | PERROR("pthread_join apps notify"); |
4a15001e | 6387 | retval = -1; |
9563b0ad | 6388 | } |
9563b0ad | 6389 | exit_apps_notify: |
4a15001e | 6390 | |
cf3af59e | 6391 | ret = pthread_join(apps_thread, &status); |
4a15001e MD |
6392 | if (ret) { |
6393 | errno = ret; | |
9563b0ad | 6394 | PERROR("pthread_join apps"); |
4a15001e | 6395 | retval = -1; |
cf3af59e | 6396 | } |
cf3af59e | 6397 | exit_apps: |
4a15001e | 6398 | |
099e26bd | 6399 | ret = pthread_join(reg_apps_thread, &status); |
4a15001e MD |
6400 | if (ret) { |
6401 | errno = ret; | |
76d7553f | 6402 | PERROR("pthread_join"); |
4a15001e | 6403 | retval = -1; |
099e26bd | 6404 | } |
099e26bd | 6405 | exit_reg_apps: |
4a15001e | 6406 | |
772b8f4d MD |
6407 | /* |
6408 | * Join dispatch thread after joining reg_apps_thread to ensure | |
6409 | * we don't leak applications in the queue. | |
6410 | */ | |
099e26bd | 6411 | ret = pthread_join(dispatch_thread, &status); |
4a15001e MD |
6412 | if (ret) { |
6413 | errno = ret; | |
76d7553f | 6414 | PERROR("pthread_join"); |
4a15001e | 6415 | retval = -1; |
099e26bd | 6416 | } |
099e26bd | 6417 | exit_dispatch: |
4a15001e | 6418 | |
cf3af59e | 6419 | ret = pthread_join(client_thread, &status); |
4a15001e MD |
6420 | if (ret) { |
6421 | errno = ret; | |
76d7553f | 6422 | PERROR("pthread_join"); |
4a15001e | 6423 | retval = -1; |
cf3af59e | 6424 | } |
a88df331 | 6425 | |
b3530820 | 6426 | exit_client: |
db66e574 | 6427 | exit_rotation: |
b3530820 | 6428 | exit_notification: |
90936dcf | 6429 | sem_destroy(¬ification_thread_ready); |
4a15001e MD |
6430 | ret = pthread_join(health_thread, &status); |
6431 | if (ret) { | |
6432 | errno = ret; | |
6433 | PERROR("pthread_join health thread"); | |
6434 | retval = -1; | |
06f525de DG |
6435 | } |
6436 | ||
b3530820 | 6437 | exit_health: |
4a15001e | 6438 | exit_init_data: |
4d62fbf8 MD |
6439 | /* |
6440 | * Wait for all pending call_rcu work to complete before tearing | |
6441 | * down data structures. call_rcu worker may be trying to | |
6442 | * perform lookups in those structures. | |
6443 | */ | |
6444 | rcu_barrier(); | |
4a15001e MD |
6445 | /* |
6446 | * sessiond_cleanup() is called when no other thread is running, except | |
6447 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
6448 | */ | |
6449 | rcu_thread_online(); | |
6450 | sessiond_cleanup(); | |
06f525de | 6451 | |
c4d5de9d JG |
6452 | /* |
6453 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
6454 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
6455 | * the queue is empty before shutting down the clean-up thread. | |
6456 | */ | |
6457 | rcu_barrier(); | |
6458 | ||
b3530820 JG |
6459 | /* |
6460 | * The teardown of the notification system is performed after the | |
6461 | * session daemon's teardown in order to allow it to be notified | |
6462 | * of the active session and channels at the moment of the teardown. | |
6463 | */ | |
6464 | if (notification_thread_handle) { | |
99611076 | 6465 | if (notification_thread_launched) { |
6be1a386 JG |
6466 | notification_thread_command_quit( |
6467 | notification_thread_handle); | |
6468 | ret = pthread_join(notification_thread, &status); | |
6469 | if (ret) { | |
6470 | errno = ret; | |
6471 | PERROR("pthread_join notification thread"); | |
6472 | retval = -1; | |
6473 | } | |
3dec304b | 6474 | } |
6be1a386 | 6475 | notification_thread_handle_destroy(notification_thread_handle); |
b3530820 JG |
6476 | } |
6477 | ||
db66e574 | 6478 | if (rotation_thread_handle) { |
99611076 | 6479 | if (rotation_thread_launched) { |
db66e574 JD |
6480 | ret = pthread_join(rotation_thread, &status); |
6481 | if (ret) { | |
6482 | errno = ret; | |
6483 | PERROR("pthread_join rotation thread"); | |
6484 | retval = -1; | |
6485 | } | |
6486 | } | |
6487 | rotation_thread_handle_destroy(rotation_thread_handle); | |
6488 | } | |
6489 | ||
99611076 | 6490 | if (timer_thread_launched) { |
d086f507 JD |
6491 | kill(getpid(), LTTNG_SESSIOND_SIG_EXIT); |
6492 | ret = pthread_join(timer_thread, &status); | |
6493 | if (ret) { | |
6494 | errno = ret; | |
6495 | PERROR("pthread_join timer thread"); | |
6496 | retval = -1; | |
6497 | } | |
6498 | } | |
6499 | ||
6500 | /* | |
6501 | * After the rotation and timer thread have quit, we can safely destroy | |
6502 | * the rotation_timer_queue. | |
6503 | */ | |
6504 | destroy_rotate_timer_queue(rotation_timer_queue); | |
6505 | ||
b3530820 JG |
6506 | rcu_thread_offline(); |
6507 | rcu_unregister_thread(); | |
6508 | ||
5e97de00 | 6509 | ret = fini_ht_cleanup_thread(&ht_cleanup_thread); |
4a15001e | 6510 | if (ret) { |
4a15001e | 6511 | retval = -1; |
0b2dc8df | 6512 | } |
b3530820 JG |
6513 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
6514 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
6515 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
62c43103 JD |
6516 | lttng_pipe_destroy(ust32_channel_rotate_pipe); |
6517 | lttng_pipe_destroy(ust64_channel_rotate_pipe); | |
6518 | lttng_pipe_destroy(kernel_channel_rotate_pipe); | |
0b2dc8df | 6519 | exit_ht_cleanup: |
4a15001e | 6520 | |
4a15001e MD |
6521 | health_app_destroy(health_sessiond); |
6522 | exit_health_sessiond_cleanup: | |
7567352f | 6523 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
6524 | |
6525 | exit_options: | |
f472090a | 6526 | sessiond_cleanup_lock_file(); |
4a15001e MD |
6527 | sessiond_cleanup_options(); |
6528 | ||
6529 | exit_set_signal_handler: | |
6530 | if (!retval) { | |
cf3af59e | 6531 | exit(EXIT_SUCCESS); |
4a15001e MD |
6532 | } else { |
6533 | exit(EXIT_FAILURE); | |
67e40797 | 6534 | } |
fac6795d | 6535 | } |