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