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