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