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