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