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