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