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