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