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