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