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