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