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