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; |
cdd566a2 | 93 | static int opt_print_version; |
3bd1e081 | 94 | |
0bb7724a DG |
95 | /* Set to 1 when a SIGUSR1 signal is received. */ |
96 | static int recv_child_signal; | |
97 | ||
a23ec3a7 DG |
98 | /* |
99 | * Consumer daemon specific control data. Every value not initialized here is | |
100 | * set to 0 by the static definition. | |
101 | */ | |
3bd1e081 MD |
102 | static struct consumer_data kconsumer_data = { |
103 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
104 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
105 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
106 | .err_sock = -1, |
107 | .cmd_sock = -1, | |
173af62f DG |
108 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
109 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
110 | .cond = PTHREAD_COND_INITIALIZER, |
111 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 112 | }; |
7753dea8 MD |
113 | static struct consumer_data ustconsumer64_data = { |
114 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
115 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
116 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
117 | .err_sock = -1, |
118 | .cmd_sock = -1, | |
173af62f DG |
119 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
120 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
121 | .cond = PTHREAD_COND_INITIALIZER, |
122 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
123 | }; |
124 | static struct consumer_data ustconsumer32_data = { | |
125 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
126 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
127 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
128 | .err_sock = -1, |
129 | .cmd_sock = -1, | |
173af62f DG |
130 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
131 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
132 | .cond = PTHREAD_COND_INITIALIZER, |
133 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
134 | }; |
135 | ||
26296c48 JG |
136 | /* Command line options */ |
137 | static const struct option long_options[] = { | |
0f5ea17c JG |
138 | { "client-sock", required_argument, 0, 'c' }, |
139 | { "apps-sock", required_argument, 0, 'a' }, | |
140 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
141 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
142 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
143 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
144 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
145 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
146 | { "consumerd32-path", required_argument, 0, '\0' }, | |
147 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
148 | { "consumerd64-path", required_argument, 0, '\0' }, | |
149 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
150 | { "daemonize", no_argument, 0, 'd' }, | |
151 | { "background", no_argument, 0, 'b' }, | |
152 | { "sig-parent", no_argument, 0, 'S' }, | |
153 | { "help", no_argument, 0, 'h' }, | |
154 | { "group", required_argument, 0, 'g' }, | |
155 | { "version", no_argument, 0, 'V' }, | |
156 | { "quiet", no_argument, 0, 'q' }, | |
157 | { "verbose", no_argument, 0, 'v' }, | |
158 | { "verbose-consumer", no_argument, 0, '\0' }, | |
159 | { "no-kernel", no_argument, 0, '\0' }, | |
160 | { "pidfile", required_argument, 0, 'p' }, | |
161 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
162 | { "config", required_argument, 0, 'f' }, | |
163 | { "load", required_argument, 0, 'l' }, | |
164 | { "kmod-probes", required_argument, 0, '\0' }, | |
165 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
166 | { NULL, 0, 0, 0 } |
167 | }; | |
168 | ||
169 | /* Command line options to ignore from configuration file */ | |
170 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
171 | ||
26c9d55e | 172 | /* Shared between threads */ |
099e26bd | 173 | static int dispatch_thread_exit; |
fac6795d | 174 | |
54d01ffb DG |
175 | /* Global application Unix socket path */ |
176 | static char apps_unix_sock_path[PATH_MAX]; | |
177 | /* Global client Unix socket path */ | |
178 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
179 | /* global wait shm path for UST */ |
180 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
181 | /* Global health check unix path */ |
182 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 183 | |
1d4b027a | 184 | /* Sockets and FDs */ |
a4b35e07 MD |
185 | static int client_sock = -1; |
186 | static int apps_sock = -1; | |
2f77fc4b | 187 | int kernel_tracer_fd = -1; |
76d7553f | 188 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 189 | |
273ea72c DG |
190 | /* |
191 | * Quit pipe for all threads. This permits a single cancellation point | |
192 | * for all threads when receiving an event on the pipe. | |
193 | */ | |
76d7553f | 194 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 195 | |
099e26bd DG |
196 | /* |
197 | * This pipe is used to inform the thread managing application communication | |
198 | * that a command is queued and ready to be processed. | |
199 | */ | |
76d7553f | 200 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 201 | |
d0b96690 DG |
202 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
203 | ||
1d4b027a | 204 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 205 | static pthread_t apps_thread; |
d0b96690 | 206 | static pthread_t apps_notify_thread; |
099e26bd | 207 | static pthread_t reg_apps_thread; |
1d4b027a | 208 | static pthread_t client_thread; |
7a485870 | 209 | static pthread_t kernel_thread; |
099e26bd | 210 | static pthread_t dispatch_thread; |
44a5e5eb | 211 | static pthread_t health_thread; |
0b2dc8df | 212 | static pthread_t ht_cleanup_thread; |
022d91ba | 213 | static pthread_t agent_reg_thread; |
ef367a93 | 214 | static pthread_t load_session_thread; |
5eb91c98 | 215 | |
099e26bd DG |
216 | /* |
217 | * UST registration command queue. This queue is tied with a futex and uses a N | |
218 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
219 | * | |
b22c5da8 DG |
220 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
221 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
222 | * the line new application socket and monitors it for any I/O error or clean | |
223 | * close that triggers an unregistration of the application. | |
099e26bd DG |
224 | */ |
225 | static struct ust_cmd_queue ust_cmd_queue; | |
226 | ||
b5541356 DG |
227 | /* |
228 | * Pointer initialized before thread creation. | |
229 | * | |
230 | * This points to the tracing session list containing the session count and a | |
231 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
232 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 233 | * |
d063d709 | 234 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 235 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
236 | */ |
237 | static struct ltt_session_list *session_list_ptr; | |
238 | ||
7753dea8 MD |
239 | int ust_consumerd64_fd = -1; |
240 | int ust_consumerd32_fd = -1; | |
241 | ||
fb6f1fa2 YB |
242 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
243 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
244 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
245 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
26296c48 JG |
246 | static int consumerd32_bin_override; |
247 | static int consumerd64_bin_override; | |
248 | static int consumerd32_libdir_override; | |
249 | static int consumerd64_libdir_override; | |
fb09408a | 250 | |
2f77fc4b DG |
251 | static const char *module_proc_lttng = "/proc/lttng"; |
252 | ||
5c827ce0 DG |
253 | /* |
254 | * Consumer daemon state which is changed when spawning it, killing it or in | |
255 | * case of a fatal error. | |
256 | */ | |
257 | enum consumerd_state { | |
258 | CONSUMER_STARTED = 1, | |
259 | CONSUMER_STOPPED = 2, | |
260 | CONSUMER_ERROR = 3, | |
261 | }; | |
262 | ||
263 | /* | |
264 | * This consumer daemon state is used to validate if a client command will be | |
265 | * able to reach the consumer. If not, the client is informed. For instance, | |
266 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
267 | * error to the client. | |
268 | * | |
269 | * The following example shows a possible race condition of this scheme: | |
270 | * | |
271 | * consumer thread error happens | |
272 | * client cmd arrives | |
273 | * client cmd checks state -> still OK | |
274 | * consumer thread exit, sets error | |
275 | * client cmd try to talk to consumer | |
276 | * ... | |
277 | * | |
278 | * However, since the consumer is a different daemon, we have no way of making | |
279 | * sure the command will reach it safely even with this state flag. This is why | |
280 | * we consider that up to the state validation during command processing, the | |
281 | * command is safe. After that, we can not guarantee the correctness of the | |
282 | * client request vis-a-vis the consumer. | |
283 | */ | |
284 | static enum consumerd_state ust_consumerd_state; | |
285 | static enum consumerd_state kernel_consumerd_state; | |
286 | ||
ae9e45b3 DG |
287 | /* |
288 | * Socket timeout for receiving and sending in seconds. | |
289 | */ | |
290 | static int app_socket_timeout; | |
291 | ||
12744796 DG |
292 | /* Set in main() with the current page size. */ |
293 | long page_size; | |
294 | ||
8782cc74 MD |
295 | /* Application health monitoring */ |
296 | struct health_app *health_sessiond; | |
297 | ||
022d91ba DG |
298 | /* Agent TCP port for registration. Used by the agent thread. */ |
299 | unsigned int agent_tcp_port = DEFAULT_AGENT_TCP_PORT; | |
4d076222 | 300 | |
f43f95a9 DG |
301 | /* Am I root or not. */ |
302 | int is_root; /* Set to 1 if the daemon is running as root */ | |
303 | ||
26296c48 JG |
304 | const char * const config_section_name = "sessiond"; |
305 | ||
ef367a93 JG |
306 | /* Load session thread information to operate. */ |
307 | struct load_session_thread_data *load_info; | |
308 | ||
7c1d2758 JG |
309 | /* Global hash tables */ |
310 | struct lttng_ht *agent_apps_ht_by_sock = NULL; | |
311 | ||
97bc1426 MD |
312 | /* |
313 | * Whether sessiond is ready for commands/health check requests. | |
314 | * NR_LTTNG_SESSIOND_READY must match the number of calls to | |
ef367a93 | 315 | * sessiond_notify_ready(). |
97bc1426 | 316 | */ |
b86685ef | 317 | #define NR_LTTNG_SESSIOND_READY 4 |
97bc1426 MD |
318 | int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY; |
319 | ||
5e97de00 JG |
320 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
321 | { | |
322 | return (fd == thread_quit_pipe[0] && (events & LPOLLIN)) ? 1 : 0; | |
323 | } | |
324 | ||
97bc1426 | 325 | /* Notify parents that we are ready for cmd and health check */ |
ef367a93 JG |
326 | LTTNG_HIDDEN |
327 | void sessiond_notify_ready(void) | |
97bc1426 MD |
328 | { |
329 | if (uatomic_sub_return(<tng_sessiond_ready, 1) == 0) { | |
330 | /* | |
331 | * Notify parent pid that we are ready to accept command | |
332 | * for client side. This ppid is the one from the | |
333 | * external process that spawned us. | |
334 | */ | |
335 | if (opt_sig_parent) { | |
336 | kill(ppid, SIGUSR1); | |
337 | } | |
338 | ||
339 | /* | |
340 | * Notify the parent of the fork() process that we are | |
341 | * ready. | |
342 | */ | |
72dd7491 | 343 | if (opt_daemon || opt_background) { |
97bc1426 MD |
344 | kill(child_ppid, SIGUSR1); |
345 | } | |
346 | } | |
347 | } | |
348 | ||
fb09408a | 349 | static |
7753dea8 | 350 | void setup_consumerd_path(void) |
fb09408a | 351 | { |
fc7a59ce | 352 | const char *bin, *libdir; |
fb09408a | 353 | |
7753dea8 MD |
354 | /* |
355 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
356 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
357 | * has not been defined. | |
7753dea8 | 358 | */ |
ebaeda94 | 359 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
360 | if (!consumerd32_bin[0]) { |
361 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
362 | } |
363 | if (!consumerd32_libdir[0]) { | |
364 | consumerd32_libdir = INSTALL_LIB_PATH; | |
365 | } | |
366 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
367 | if (!consumerd64_bin[0]) { |
368 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 369 | } |
ebaeda94 MD |
370 | if (!consumerd64_libdir[0]) { |
371 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
372 | } |
373 | #else | |
374 | #error "Unknown bitness" | |
375 | #endif | |
376 | ||
fb09408a MD |
377 | /* |
378 | * runtime env. var. overrides the build default. | |
379 | */ | |
e8fa9fb0 | 380 | bin = lttng_secure_getenv("LTTNG_CONSUMERD32_BIN"); |
fc7a59ce AM |
381 | if (bin) { |
382 | consumerd32_bin = bin; | |
7753dea8 | 383 | } |
e8fa9fb0 | 384 | bin = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN"); |
fc7a59ce AM |
385 | if (bin) { |
386 | consumerd64_bin = bin; | |
ebaeda94 | 387 | } |
e8fa9fb0 | 388 | libdir = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
389 | if (libdir) { |
390 | consumerd32_libdir = libdir; | |
391 | } | |
e8fa9fb0 | 392 | libdir = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
393 | if (libdir) { |
394 | consumerd64_libdir = libdir; | |
fb09408a MD |
395 | } |
396 | } | |
397 | ||
4a15001e MD |
398 | static |
399 | int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size, | |
400 | int *a_pipe) | |
5eb91c98 DG |
401 | { |
402 | int ret; | |
403 | ||
d0b96690 | 404 | assert(events); |
5eb91c98 DG |
405 | |
406 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
407 | if (ret < 0) { | |
408 | goto error; | |
409 | } | |
410 | ||
411 | /* Add quit pipe */ | |
4a15001e | 412 | ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
413 | if (ret < 0) { |
414 | goto error; | |
415 | } | |
416 | ||
417 | return 0; | |
418 | ||
419 | error: | |
420 | return ret; | |
421 | } | |
422 | ||
4a15001e MD |
423 | /* |
424 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
425 | */ | |
426 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) | |
427 | { | |
428 | return __sessiond_set_thread_pollset(events, size, thread_quit_pipe); | |
429 | } | |
430 | ||
273ea72c | 431 | /* |
5eb91c98 | 432 | * Init thread quit pipe. |
273ea72c DG |
433 | * |
434 | * Return -1 on error or 0 if all pipes are created. | |
435 | */ | |
4a15001e | 436 | static int __init_thread_quit_pipe(int *a_pipe) |
273ea72c | 437 | { |
730389d9 | 438 | int ret, i; |
273ea72c | 439 | |
4a15001e | 440 | ret = pipe(a_pipe); |
273ea72c | 441 | if (ret < 0) { |
730389d9 | 442 | PERROR("thread quit pipe"); |
273ea72c DG |
443 | goto error; |
444 | } | |
445 | ||
730389d9 | 446 | for (i = 0; i < 2; i++) { |
4a15001e | 447 | ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC); |
730389d9 DG |
448 | if (ret < 0) { |
449 | PERROR("fcntl"); | |
450 | goto error; | |
451 | } | |
452 | } | |
453 | ||
273ea72c DG |
454 | error: |
455 | return ret; | |
456 | } | |
457 | ||
4a15001e MD |
458 | static int init_thread_quit_pipe(void) |
459 | { | |
460 | return __init_thread_quit_pipe(thread_quit_pipe); | |
461 | } | |
462 | ||
099e26bd DG |
463 | /* |
464 | * Stop all threads by closing the thread quit pipe. | |
465 | */ | |
cf3af59e MD |
466 | static void stop_threads(void) |
467 | { | |
5eb91c98 DG |
468 | int ret; |
469 | ||
cf3af59e MD |
470 | /* Stopping all threads */ |
471 | DBG("Terminating all threads"); | |
54d01ffb | 472 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
473 | if (ret < 0) { |
474 | ERR("write error on thread quit pipe"); | |
475 | } | |
476 | ||
099e26bd | 477 | /* Dispatch thread */ |
26c9d55e | 478 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 479 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
480 | } |
481 | ||
e975f9f8 DG |
482 | /* |
483 | * Close every consumer sockets. | |
484 | */ | |
485 | static void close_consumer_sockets(void) | |
486 | { | |
487 | int ret; | |
488 | ||
489 | if (kconsumer_data.err_sock >= 0) { | |
490 | ret = close(kconsumer_data.err_sock); | |
491 | if (ret < 0) { | |
492 | PERROR("kernel consumer err_sock close"); | |
493 | } | |
494 | } | |
495 | if (ustconsumer32_data.err_sock >= 0) { | |
496 | ret = close(ustconsumer32_data.err_sock); | |
497 | if (ret < 0) { | |
a76cbd9f | 498 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
499 | } |
500 | } | |
501 | if (ustconsumer64_data.err_sock >= 0) { | |
502 | ret = close(ustconsumer64_data.err_sock); | |
503 | if (ret < 0) { | |
a76cbd9f | 504 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
505 | } |
506 | } | |
507 | if (kconsumer_data.cmd_sock >= 0) { | |
508 | ret = close(kconsumer_data.cmd_sock); | |
509 | if (ret < 0) { | |
510 | PERROR("kernel consumer cmd_sock close"); | |
511 | } | |
512 | } | |
513 | if (ustconsumer32_data.cmd_sock >= 0) { | |
514 | ret = close(ustconsumer32_data.cmd_sock); | |
515 | if (ret < 0) { | |
a76cbd9f | 516 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
517 | } |
518 | } | |
519 | if (ustconsumer64_data.cmd_sock >= 0) { | |
520 | ret = close(ustconsumer64_data.cmd_sock); | |
521 | if (ret < 0) { | |
a76cbd9f | 522 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
523 | } |
524 | } | |
525 | } | |
526 | ||
c9cb3e7d JG |
527 | /* |
528 | * Generate the full lock file path using the rundir. | |
529 | * | |
530 | * Return the snprintf() return value thus a negative value is an error. | |
531 | */ | |
532 | static int generate_lock_file_path(char *path, size_t len) | |
533 | { | |
534 | int ret; | |
535 | ||
536 | assert(path); | |
537 | assert(rundir); | |
538 | ||
539 | /* Build lockfile path from rundir. */ | |
540 | ret = snprintf(path, len, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE, rundir); | |
541 | if (ret < 0) { | |
542 | PERROR("snprintf lockfile path"); | |
543 | } | |
544 | ||
545 | return ret; | |
546 | } | |
547 | ||
4e4714cb JR |
548 | /* |
549 | * Wait on consumer process termination. | |
550 | * | |
551 | * Need to be called with the consumer data lock held or from a context | |
552 | * ensuring no concurrent access to data (e.g: cleanup). | |
553 | */ | |
554 | static void wait_consumer(struct consumer_data *consumer_data) | |
555 | { | |
556 | pid_t ret; | |
557 | int status; | |
558 | ||
559 | if (consumer_data->pid <= 0) { | |
560 | return; | |
561 | } | |
562 | ||
563 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
564 | consumer_data->pid); | |
565 | ret = waitpid(consumer_data->pid, &status, 0); | |
566 | if (ret == -1) { | |
567 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
78f1416a | 568 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
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 | |
78d8cf23 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 | ||
68924dc1 | 1872 | for (;;) { |
12e2b881 MD |
1873 | health_code_update(); |
1874 | ||
099e26bd DG |
1875 | /* Atomically prepare the queue futex */ |
1876 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1877 | ||
68924dc1 MD |
1878 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1879 | break; | |
1880 | } | |
1881 | ||
099e26bd | 1882 | do { |
d0b96690 | 1883 | struct ust_app *app = NULL; |
7972aab2 | 1884 | ust_cmd = NULL; |
d0b96690 | 1885 | |
f45e313d DG |
1886 | /* |
1887 | * Make sure we don't have node(s) that have hung up before receiving | |
1888 | * the notify socket. This is to clean the list in order to avoid | |
1889 | * memory leaks from notify socket that are never seen. | |
1890 | */ | |
1891 | sanitize_wait_queue(&wait_queue); | |
1892 | ||
12e2b881 | 1893 | health_code_update(); |
099e26bd | 1894 | /* Dequeue command for registration */ |
8bdee6e2 | 1895 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1896 | if (node == NULL) { |
00a17c97 | 1897 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1898 | /* Continue thread execution */ |
1899 | break; | |
1900 | } | |
1901 | ||
1902 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1903 | ||
2f50c8a3 DG |
1904 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1905 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1906 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1907 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1908 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1909 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1910 | |
1911 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1912 | wait_node = zmalloc(sizeof(*wait_node)); | |
1913 | if (!wait_node) { | |
1914 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1915 | ret = close(ust_cmd->sock); |
1916 | if (ret < 0) { | |
1917 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1918 | } | |
51dec90d | 1919 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1920 | free(ust_cmd); |
d0b96690 DG |
1921 | goto error; |
1922 | } | |
1923 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1924 | ||
1925 | /* Create application object if socket is CMD. */ | |
1926 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1927 | ust_cmd->sock); | |
1928 | if (!wait_node->app) { | |
1929 | ret = close(ust_cmd->sock); | |
1930 | if (ret < 0) { | |
1931 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1932 | } |
51dec90d | 1933 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1934 | free(wait_node); |
7972aab2 | 1935 | free(ust_cmd); |
d0b96690 DG |
1936 | continue; |
1937 | } | |
1938 | /* | |
1939 | * Add application to the wait queue so we can set the notify | |
1940 | * socket before putting this object in the global ht. | |
1941 | */ | |
f45e313d DG |
1942 | cds_list_add(&wait_node->head, &wait_queue.head); |
1943 | wait_queue.count++; | |
d0b96690 | 1944 | |
7972aab2 | 1945 | free(ust_cmd); |
d0b96690 DG |
1946 | /* |
1947 | * We have to continue here since we don't have the notify | |
1948 | * socket and the application MUST be added to the hash table | |
1949 | * only at that moment. | |
1950 | */ | |
1951 | continue; | |
1952 | } else { | |
1953 | /* | |
1954 | * Look for the application in the local wait queue and set the | |
1955 | * notify socket if found. | |
1956 | */ | |
d88aee68 | 1957 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1958 | &wait_queue.head, head) { |
12e2b881 | 1959 | health_code_update(); |
d0b96690 DG |
1960 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1961 | wait_node->app->notify_sock = ust_cmd->sock; | |
1962 | cds_list_del(&wait_node->head); | |
f45e313d | 1963 | wait_queue.count--; |
d0b96690 DG |
1964 | app = wait_node->app; |
1965 | free(wait_node); | |
1966 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1967 | break; | |
1968 | } | |
1969 | } | |
020d7f60 DG |
1970 | |
1971 | /* | |
1972 | * With no application at this stage the received socket is | |
1973 | * basically useless so close it before we free the cmd data | |
1974 | * structure for good. | |
1975 | */ | |
1976 | if (!app) { | |
1977 | ret = close(ust_cmd->sock); | |
1978 | if (ret < 0) { | |
1979 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1980 | } | |
51dec90d | 1981 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1982 | } |
7972aab2 | 1983 | free(ust_cmd); |
d0b96690 DG |
1984 | } |
1985 | ||
1986 | if (app) { | |
d0b96690 DG |
1987 | /* |
1988 | * @session_lock_list | |
1989 | * | |
1990 | * Lock the global session list so from the register up to the | |
1991 | * registration done message, no thread can see the application | |
1992 | * and change its state. | |
1993 | */ | |
1994 | session_lock_list(); | |
1995 | rcu_read_lock(); | |
d88aee68 | 1996 | |
d0b96690 DG |
1997 | /* |
1998 | * Add application to the global hash table. This needs to be | |
1999 | * done before the update to the UST registry can locate the | |
2000 | * application. | |
2001 | */ | |
2002 | ust_app_add(app); | |
d88aee68 DG |
2003 | |
2004 | /* Set app version. This call will print an error if needed. */ | |
2005 | (void) ust_app_version(app); | |
2006 | ||
2007 | /* Send notify socket through the notify pipe. */ | |
2008 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
2009 | app->notify_sock); | |
2010 | if (ret < 0) { | |
2011 | rcu_read_unlock(); | |
2012 | session_unlock_list(); | |
b85dc84c DG |
2013 | /* |
2014 | * No notify thread, stop the UST tracing. However, this is | |
2015 | * not an internal error of the this thread thus setting | |
2016 | * the health error code to a normal exit. | |
2017 | */ | |
2018 | err = 0; | |
d88aee68 | 2019 | goto error; |
6620da75 | 2020 | } |
d88aee68 | 2021 | |
d0b96690 DG |
2022 | /* |
2023 | * Update newly registered application with the tracing | |
2024 | * registry info already enabled information. | |
2025 | */ | |
2026 | update_ust_app(app->sock); | |
d88aee68 DG |
2027 | |
2028 | /* | |
2029 | * Don't care about return value. Let the manage apps threads | |
2030 | * handle app unregistration upon socket close. | |
2031 | */ | |
fb45065e | 2032 | (void) ust_app_register_done(app); |
d88aee68 DG |
2033 | |
2034 | /* | |
2035 | * Even if the application socket has been closed, send the app | |
2036 | * to the thread and unregistration will take place at that | |
2037 | * place. | |
2038 | */ | |
2039 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 2040 | if (ret < 0) { |
d88aee68 DG |
2041 | rcu_read_unlock(); |
2042 | session_unlock_list(); | |
b85dc84c DG |
2043 | /* |
2044 | * No apps. thread, stop the UST tracing. However, this is | |
2045 | * not an internal error of the this thread thus setting | |
2046 | * the health error code to a normal exit. | |
2047 | */ | |
2048 | err = 0; | |
d88aee68 | 2049 | goto error; |
d0b96690 | 2050 | } |
d88aee68 | 2051 | |
d0b96690 DG |
2052 | rcu_read_unlock(); |
2053 | session_unlock_list(); | |
099e26bd | 2054 | } |
099e26bd DG |
2055 | } while (node != NULL); |
2056 | ||
12e2b881 | 2057 | health_poll_entry(); |
099e26bd DG |
2058 | /* Futex wait on queue. Blocking call on futex() */ |
2059 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 2060 | health_poll_exit(); |
099e26bd | 2061 | } |
12e2b881 MD |
2062 | /* Normal exit, no error */ |
2063 | err = 0; | |
099e26bd DG |
2064 | |
2065 | error: | |
d88aee68 DG |
2066 | /* Clean up wait queue. */ |
2067 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 2068 | &wait_queue.head, head) { |
d88aee68 | 2069 | cds_list_del(&wait_node->head); |
f45e313d | 2070 | wait_queue.count--; |
d88aee68 DG |
2071 | free(wait_node); |
2072 | } | |
2073 | ||
772b8f4d MD |
2074 | /* Empty command queue. */ |
2075 | for (;;) { | |
2076 | /* Dequeue command for registration */ | |
2077 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); | |
2078 | if (node == NULL) { | |
2079 | break; | |
2080 | } | |
2081 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
2082 | ret = close(ust_cmd->sock); | |
2083 | if (ret < 0) { | |
2084 | PERROR("close ust sock exit dispatch %d", ust_cmd->sock); | |
2085 | } | |
2086 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2087 | free(ust_cmd); | |
2088 | } | |
2089 | ||
9ad42ec1 | 2090 | error_testpoint: |
099e26bd | 2091 | DBG("Dispatch thread dying"); |
12e2b881 MD |
2092 | if (err) { |
2093 | health_error(); | |
2094 | ERR("Health error occurred in %s", __func__); | |
2095 | } | |
8782cc74 | 2096 | health_unregister(health_sessiond); |
78d8cf23 | 2097 | rcu_unregister_thread(); |
099e26bd DG |
2098 | return NULL; |
2099 | } | |
2100 | ||
2101 | /* | |
2102 | * This thread manage application registration. | |
2103 | */ | |
2104 | static void *thread_registration_apps(void *data) | |
1d4b027a | 2105 | { |
139ac872 | 2106 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
2107 | uint32_t revents, nb_fd; |
2108 | struct lttng_poll_event events; | |
099e26bd DG |
2109 | /* |
2110 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
2111 | * freed in the manage apps thread. | |
2112 | */ | |
2113 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 2114 | |
099e26bd | 2115 | DBG("[thread] Manage application registration started"); |
1d4b027a | 2116 | |
6c71277b | 2117 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 2118 | |
e547b070 | 2119 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
2120 | goto error_testpoint; |
2121 | } | |
8ac94142 | 2122 | |
1d4b027a DG |
2123 | ret = lttcomm_listen_unix_sock(apps_sock); |
2124 | if (ret < 0) { | |
76d7553f | 2125 | goto error_listen; |
1d4b027a DG |
2126 | } |
2127 | ||
5eb91c98 DG |
2128 | /* |
2129 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
2130 | * more will be added to this poll set. | |
2131 | */ | |
d0b96690 | 2132 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 2133 | if (ret < 0) { |
76d7553f | 2134 | goto error_create_poll; |
5eb91c98 | 2135 | } |
273ea72c | 2136 | |
5eb91c98 DG |
2137 | /* Add the application registration socket */ |
2138 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
2139 | if (ret < 0) { | |
76d7553f | 2140 | goto error_poll_add; |
5eb91c98 | 2141 | } |
273ea72c | 2142 | |
1d4b027a | 2143 | /* Notify all applications to register */ |
0fdd1e2c DG |
2144 | ret = notify_ust_apps(1); |
2145 | if (ret < 0) { | |
2146 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
2147 | "Execution continues but there might be problem for already\n" |
2148 | "running applications that wishes to register."); | |
0fdd1e2c | 2149 | } |
1d4b027a DG |
2150 | |
2151 | while (1) { | |
2152 | DBG("Accepting application registration"); | |
273ea72c DG |
2153 | |
2154 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 2155 | restart: |
a78af745 | 2156 | health_poll_entry(); |
5eb91c98 | 2157 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 2158 | health_poll_exit(); |
273ea72c | 2159 | if (ret < 0) { |
88f2b785 MD |
2160 | /* |
2161 | * Restart interrupted system call. | |
2162 | */ | |
2163 | if (errno == EINTR) { | |
2164 | goto restart; | |
2165 | } | |
273ea72c DG |
2166 | goto error; |
2167 | } | |
2168 | ||
0d9c5d77 DG |
2169 | nb_fd = ret; |
2170 | ||
5eb91c98 | 2171 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 2172 | health_code_update(); |
139ac872 | 2173 | |
5eb91c98 DG |
2174 | /* Fetch once the poll data */ |
2175 | revents = LTTNG_POLL_GETEV(&events, i); | |
2176 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 2177 | |
fd20dac9 MD |
2178 | if (!revents) { |
2179 | /* No activity for this FD (poll implementation). */ | |
2180 | continue; | |
2181 | } | |
2182 | ||
5eb91c98 | 2183 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 2184 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 2185 | if (ret) { |
139ac872 MD |
2186 | err = 0; |
2187 | goto exit; | |
90014c57 | 2188 | } |
1d4b027a | 2189 | |
5eb91c98 DG |
2190 | /* Event on the registration socket */ |
2191 | if (pollfd == apps_sock) { | |
03e43155 | 2192 | if (revents & LPOLLIN) { |
5eb91c98 DG |
2193 | sock = lttcomm_accept_unix_sock(apps_sock); |
2194 | if (sock < 0) { | |
2195 | goto error; | |
2196 | } | |
099e26bd | 2197 | |
16c5c8fa DG |
2198 | /* |
2199 | * Set socket timeout for both receiving and ending. | |
2200 | * app_socket_timeout is in seconds, whereas | |
2201 | * lttcomm_setsockopt_rcv_timeout and | |
2202 | * lttcomm_setsockopt_snd_timeout expect msec as | |
2203 | * parameter. | |
2204 | */ | |
28ce0ff2 JG |
2205 | if (app_socket_timeout >= 0) { |
2206 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
2207 | app_socket_timeout * 1000); | |
2208 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
2209 | app_socket_timeout * 1000); | |
2210 | } | |
16c5c8fa | 2211 | |
b662582b DG |
2212 | /* |
2213 | * Set the CLOEXEC flag. Return code is useless because | |
2214 | * either way, the show must go on. | |
2215 | */ | |
2216 | (void) utils_set_fd_cloexec(sock); | |
2217 | ||
5eb91c98 | 2218 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 2219 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 2220 | if (ust_cmd == NULL) { |
76d7553f | 2221 | PERROR("ust command zmalloc"); |
41ed8e47 MD |
2222 | ret = close(sock); |
2223 | if (ret) { | |
2224 | PERROR("close"); | |
2225 | } | |
5eb91c98 DG |
2226 | goto error; |
2227 | } | |
1d4b027a | 2228 | |
5eb91c98 DG |
2229 | /* |
2230 | * Using message-based transmissions to ensure we don't | |
2231 | * have to deal with partially received messages. | |
2232 | */ | |
4063050c MD |
2233 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
2234 | if (ret < 0) { | |
2235 | ERR("Exhausted file descriptors allowed for applications."); | |
2236 | free(ust_cmd); | |
2237 | ret = close(sock); | |
2238 | if (ret) { | |
2239 | PERROR("close"); | |
2240 | } | |
2241 | sock = -1; | |
2242 | continue; | |
2243 | } | |
d88aee68 | 2244 | |
840cb59c | 2245 | health_code_update(); |
d0b96690 DG |
2246 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
2247 | if (ret < 0) { | |
5eb91c98 | 2248 | free(ust_cmd); |
d0b96690 | 2249 | /* Close socket of the application. */ |
76d7553f MD |
2250 | ret = close(sock); |
2251 | if (ret) { | |
2252 | PERROR("close"); | |
2253 | } | |
4063050c | 2254 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 2255 | sock = -1; |
5eb91c98 DG |
2256 | continue; |
2257 | } | |
840cb59c | 2258 | health_code_update(); |
099e26bd | 2259 | |
5eb91c98 | 2260 | ust_cmd->sock = sock; |
34a2494f | 2261 | sock = -1; |
099e26bd | 2262 | |
5eb91c98 DG |
2263 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
2264 | " gid:%d sock:%d name:%s (version %d.%d)", | |
2265 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
2266 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
2267 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
2268 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 2269 | |
5eb91c98 DG |
2270 | /* |
2271 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 2272 | * has been taken! This apps will be part of the *system*. |
5eb91c98 | 2273 | */ |
8bdee6e2 | 2274 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
5eb91c98 DG |
2275 | |
2276 | /* | |
2277 | * Wake the registration queue futex. Implicit memory | |
8bdee6e2 | 2278 | * barrier with the exchange in cds_wfcq_enqueue. |
5eb91c98 DG |
2279 | */ |
2280 | futex_nto1_wake(&ust_cmd_queue.futex); | |
03e43155 MD |
2281 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2282 | ERR("Register apps socket poll error"); | |
2283 | goto error; | |
2284 | } else { | |
2285 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2286 | goto error; | |
5eb91c98 DG |
2287 | } |
2288 | } | |
90014c57 | 2289 | } |
1d4b027a DG |
2290 | } |
2291 | ||
139ac872 | 2292 | exit: |
1d4b027a | 2293 | error: |
0fdd1e2c DG |
2294 | /* Notify that the registration thread is gone */ |
2295 | notify_ust_apps(0); | |
2296 | ||
a4b35e07 | 2297 | if (apps_sock >= 0) { |
76d7553f MD |
2298 | ret = close(apps_sock); |
2299 | if (ret) { | |
2300 | PERROR("close"); | |
2301 | } | |
a4b35e07 | 2302 | } |
46c3f085 | 2303 | if (sock >= 0) { |
76d7553f MD |
2304 | ret = close(sock); |
2305 | if (ret) { | |
2306 | PERROR("close"); | |
2307 | } | |
4063050c | 2308 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 2309 | } |
273ea72c | 2310 | unlink(apps_unix_sock_path); |
0fdd1e2c | 2311 | |
76d7553f | 2312 | error_poll_add: |
5eb91c98 | 2313 | lttng_poll_clean(&events); |
76d7553f MD |
2314 | error_listen: |
2315 | error_create_poll: | |
6993eeb3 | 2316 | error_testpoint: |
76d7553f | 2317 | DBG("UST Registration thread cleanup complete"); |
9ad42ec1 MD |
2318 | if (err) { |
2319 | health_error(); | |
2320 | ERR("Health error occurred in %s", __func__); | |
2321 | } | |
8782cc74 | 2322 | health_unregister(health_sessiond); |
5eb91c98 | 2323 | |
1d4b027a DG |
2324 | return NULL; |
2325 | } | |
2326 | ||
8c0faa1d | 2327 | /* |
3bd1e081 | 2328 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 2329 | * exec or it will fails. |
8c0faa1d | 2330 | */ |
3bd1e081 | 2331 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 2332 | { |
a23ec3a7 | 2333 | int ret, clock_ret; |
ee0b0061 DG |
2334 | struct timespec timeout; |
2335 | ||
13a7bce3 JG |
2336 | /* |
2337 | * Make sure we set the readiness flag to 0 because we are NOT ready. | |
2338 | * This access to consumer_thread_is_ready does not need to be | |
2339 | * protected by consumer_data.cond_mutex (yet) since the consumer | |
2340 | * management thread has not been started at this point. | |
2341 | */ | |
a23ec3a7 | 2342 | consumer_data->consumer_thread_is_ready = 0; |
8c0faa1d | 2343 | |
a23ec3a7 DG |
2344 | /* Setup pthread condition */ |
2345 | ret = pthread_condattr_init(&consumer_data->condattr); | |
4a15001e | 2346 | if (ret) { |
a23ec3a7 DG |
2347 | errno = ret; |
2348 | PERROR("pthread_condattr_init consumer data"); | |
2349 | goto error; | |
2350 | } | |
2351 | ||
2352 | /* | |
2353 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
2354 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
2355 | * for a more details and how we noticed it. | |
2356 | */ | |
2357 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
4a15001e | 2358 | if (ret) { |
a23ec3a7 DG |
2359 | errno = ret; |
2360 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
2361 | goto error; |
2362 | } | |
8c0faa1d | 2363 | |
a23ec3a7 | 2364 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
4a15001e | 2365 | if (ret) { |
a23ec3a7 DG |
2366 | errno = ret; |
2367 | PERROR("pthread_cond_init consumer data"); | |
2368 | goto error; | |
2369 | } | |
2370 | ||
1a1a34b4 MJ |
2371 | ret = pthread_create(&consumer_data->thread, default_pthread_attr(), |
2372 | thread_manage_consumer, consumer_data); | |
4a15001e MD |
2373 | if (ret) { |
2374 | errno = ret; | |
3bd1e081 | 2375 | PERROR("pthread_create consumer"); |
ee0b0061 | 2376 | ret = -1; |
8c0faa1d DG |
2377 | goto error; |
2378 | } | |
2379 | ||
a23ec3a7 DG |
2380 | /* We are about to wait on a pthread condition */ |
2381 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2382 | ||
ee0b0061 | 2383 | /* Get time for sem_timedwait absolute timeout */ |
389fbf04 | 2384 | clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout); |
a23ec3a7 DG |
2385 | /* |
2386 | * Set the timeout for the condition timed wait even if the clock gettime | |
2387 | * call fails since we might loop on that call and we want to avoid to | |
2388 | * increment the timeout too many times. | |
2389 | */ | |
2390 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2391 | ||
2392 | /* | |
2393 | * The following loop COULD be skipped in some conditions so this is why we | |
2394 | * set ret to 0 in order to make sure at least one round of the loop is | |
2395 | * done. | |
2396 | */ | |
2397 | ret = 0; | |
2398 | ||
2399 | /* | |
2400 | * Loop until the condition is reached or when a timeout is reached. Note | |
2401 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2402 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2403 | * possible. This loop does not take any chances and works with both of | |
2404 | * them. | |
2405 | */ | |
2406 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2407 | if (clock_ret < 0) { | |
2408 | PERROR("clock_gettime spawn consumer"); | |
2409 | /* Infinite wait for the consumerd thread to be ready */ | |
2410 | ret = pthread_cond_wait(&consumer_data->cond, | |
2411 | &consumer_data->cond_mutex); | |
2412 | } else { | |
2413 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2414 | &consumer_data->cond_mutex, &timeout); | |
2415 | } | |
ee0b0061 | 2416 | } |
8c0faa1d | 2417 | |
a23ec3a7 DG |
2418 | /* Release the pthread condition */ |
2419 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2420 | ||
2421 | if (ret != 0) { | |
2422 | errno = ret; | |
2423 | if (ret == ETIMEDOUT) { | |
4282f9a3 DG |
2424 | int pth_ret; |
2425 | ||
ee0b0061 DG |
2426 | /* |
2427 | * Call has timed out so we kill the kconsumerd_thread and return | |
2428 | * an error. | |
2429 | */ | |
a23ec3a7 DG |
2430 | ERR("Condition timed out. The consumer thread was never ready." |
2431 | " Killing it"); | |
4282f9a3 DG |
2432 | pth_ret = pthread_cancel(consumer_data->thread); |
2433 | if (pth_ret < 0) { | |
3bd1e081 | 2434 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2435 | } |
2436 | } else { | |
a23ec3a7 | 2437 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 | 2438 | } |
4282f9a3 DG |
2439 | /* Caller is expecting a negative value on failure. */ |
2440 | ret = -1; | |
ee0b0061 DG |
2441 | goto error; |
2442 | } | |
2443 | ||
3bd1e081 MD |
2444 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2445 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2446 | ERR("Consumerd did not start"); |
3bd1e081 | 2447 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2448 | goto error; |
2449 | } | |
3bd1e081 | 2450 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2451 | |
8c0faa1d DG |
2452 | return 0; |
2453 | ||
2454 | error: | |
2455 | return ret; | |
2456 | } | |
2457 | ||
d9800920 | 2458 | /* |
3bd1e081 | 2459 | * Join consumer thread |
d9800920 | 2460 | */ |
3bd1e081 | 2461 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2462 | { |
2463 | void *status; | |
cf3af59e | 2464 | |
e8209f6b DG |
2465 | /* Consumer pid must be a real one. */ |
2466 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2467 | int ret; |
3bd1e081 | 2468 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2469 | if (ret) { |
4a15001e | 2470 | PERROR("Error killing consumer daemon"); |
cf3af59e MD |
2471 | return ret; |
2472 | } | |
3bd1e081 | 2473 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2474 | } else { |
2475 | return 0; | |
2476 | } | |
2477 | } | |
2478 | ||
8c0faa1d | 2479 | /* |
3bd1e081 | 2480 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2481 | * |
d063d709 | 2482 | * Return pid if successful else -1. |
8c0faa1d | 2483 | */ |
3bd1e081 | 2484 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2485 | { |
2486 | int ret; | |
2487 | pid_t pid; | |
94c55f17 | 2488 | const char *consumer_to_use; |
53086306 | 2489 | const char *verbosity; |
94c55f17 | 2490 | struct stat st; |
8c0faa1d | 2491 | |
3bd1e081 | 2492 | DBG("Spawning consumerd"); |
c49dc785 | 2493 | |
8c0faa1d DG |
2494 | pid = fork(); |
2495 | if (pid == 0) { | |
2496 | /* | |
3bd1e081 | 2497 | * Exec consumerd. |
8c0faa1d | 2498 | */ |
daee5345 | 2499 | if (opt_verbose_consumer) { |
53086306 | 2500 | verbosity = "--verbose"; |
4421f712 | 2501 | } else if (lttng_opt_quiet) { |
53086306 | 2502 | verbosity = "--quiet"; |
4421f712 DG |
2503 | } else { |
2504 | verbosity = ""; | |
53086306 | 2505 | } |
4421f712 | 2506 | |
3bd1e081 MD |
2507 | switch (consumer_data->type) { |
2508 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2509 | /* |
c7704d57 DG |
2510 | * Find out which consumerd to execute. We will first try the |
2511 | * 64-bit path, then the sessiond's installation directory, and | |
2512 | * fallback on the 32-bit one, | |
94c55f17 | 2513 | */ |
63a799e8 AM |
2514 | DBG3("Looking for a kernel consumer at these locations:"); |
2515 | DBG3(" 1) %s", consumerd64_bin); | |
2516 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
2517 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 2518 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 2519 | DBG3("Found location #1"); |
94c55f17 | 2520 | consumer_to_use = consumerd64_bin; |
94c55f17 | 2521 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 2522 | DBG3("Found location #2"); |
94c55f17 | 2523 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 2524 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 2525 | DBG3("Found location #3"); |
eb1e0bd4 | 2526 | consumer_to_use = consumerd32_bin; |
94c55f17 | 2527 | } else { |
63a799e8 | 2528 | DBG("Could not find any valid consumerd executable"); |
4282f9a3 | 2529 | ret = -EINVAL; |
5b49a21f | 2530 | goto error; |
94c55f17 AM |
2531 | } |
2532 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
4282f9a3 | 2533 | ret = execl(consumer_to_use, |
94c55f17 AM |
2534 | "lttng-consumerd", verbosity, "-k", |
2535 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2536 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2537 | "--group", tracing_group_name, |
94c55f17 | 2538 | NULL); |
3bd1e081 | 2539 | break; |
7753dea8 MD |
2540 | case LTTNG_CONSUMER64_UST: |
2541 | { | |
b1e0b6b6 | 2542 | char *tmpnew = NULL; |
8f4905da MD |
2543 | |
2544 | if (consumerd64_libdir[0] != '\0') { | |
2545 | char *tmp; | |
2546 | size_t tmplen; | |
2547 | ||
e8fa9fb0 | 2548 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2549 | if (!tmp) { |
2550 | tmp = ""; | |
2551 | } | |
2552 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2553 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
2554 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2555 | if (!tmpnew) { | |
2556 | ret = -ENOMEM; | |
2557 | goto error; | |
2558 | } | |
2559 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2560 | strcat(tmpnew, consumerd64_libdir); | |
2561 | if (tmp[0] != '\0') { | |
2562 | strcat(tmpnew, ":"); | |
2563 | strcat(tmpnew, tmp); | |
2564 | } | |
2565 | ret = putenv(tmpnew); | |
2566 | if (ret) { | |
2567 | ret = -errno; | |
c6f76da9 | 2568 | free(tmpnew); |
8f4905da MD |
2569 | goto error; |
2570 | } | |
2571 | } | |
94c55f17 | 2572 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 2573 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2574 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2575 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2576 | "--group", tracing_group_name, |
7753dea8 | 2577 | NULL); |
8f4905da MD |
2578 | if (consumerd64_libdir[0] != '\0') { |
2579 | free(tmpnew); | |
2580 | } | |
3bd1e081 | 2581 | break; |
7753dea8 MD |
2582 | } |
2583 | case LTTNG_CONSUMER32_UST: | |
2584 | { | |
937dde8e | 2585 | char *tmpnew = NULL; |
8f4905da MD |
2586 | |
2587 | if (consumerd32_libdir[0] != '\0') { | |
2588 | char *tmp; | |
2589 | size_t tmplen; | |
2590 | ||
e8fa9fb0 | 2591 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2592 | if (!tmp) { |
2593 | tmp = ""; | |
2594 | } | |
2595 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2596 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
2597 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2598 | if (!tmpnew) { | |
2599 | ret = -ENOMEM; | |
2600 | goto error; | |
2601 | } | |
2602 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2603 | strcat(tmpnew, consumerd32_libdir); | |
2604 | if (tmp[0] != '\0') { | |
2605 | strcat(tmpnew, ":"); | |
2606 | strcat(tmpnew, tmp); | |
2607 | } | |
2608 | ret = putenv(tmpnew); | |
2609 | if (ret) { | |
2610 | ret = -errno; | |
c6f76da9 | 2611 | free(tmpnew); |
8f4905da MD |
2612 | goto error; |
2613 | } | |
2614 | } | |
94c55f17 | 2615 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 2616 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2617 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2618 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2619 | "--group", tracing_group_name, |
7753dea8 | 2620 | NULL); |
8f4905da MD |
2621 | if (consumerd32_libdir[0] != '\0') { |
2622 | free(tmpnew); | |
2623 | } | |
7753dea8 MD |
2624 | break; |
2625 | } | |
3bd1e081 | 2626 | default: |
db58870b | 2627 | ERR("unknown consumer type"); |
3bd1e081 MD |
2628 | exit(EXIT_FAILURE); |
2629 | } | |
8c0faa1d | 2630 | if (errno != 0) { |
4282f9a3 | 2631 | PERROR("Consumer execl()"); |
8c0faa1d | 2632 | } |
4282f9a3 | 2633 | /* Reaching this point, we got a failure on our execl(). */ |
8c0faa1d DG |
2634 | exit(EXIT_FAILURE); |
2635 | } else if (pid > 0) { | |
2636 | ret = pid; | |
8c0faa1d | 2637 | } else { |
76d7553f | 2638 | PERROR("start consumer fork"); |
8c0faa1d | 2639 | ret = -errno; |
8c0faa1d | 2640 | } |
8f4905da | 2641 | error: |
8c0faa1d DG |
2642 | return ret; |
2643 | } | |
2644 | ||
693bd40b | 2645 | /* |
3bd1e081 | 2646 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2647 | */ |
3bd1e081 | 2648 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2649 | { |
c617c0c6 | 2650 | int ret; |
edb8b045 DG |
2651 | |
2652 | /* | |
2653 | * Set the listen() state on the socket since there is a possible race | |
2654 | * between the exec() of the consumer daemon and this call if place in the | |
2655 | * consumer thread. See bug #366 for more details. | |
2656 | */ | |
2657 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2658 | if (ret < 0) { | |
2659 | goto error; | |
2660 | } | |
693bd40b | 2661 | |
3bd1e081 MD |
2662 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2663 | if (consumer_data->pid != 0) { | |
2664 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2665 | goto end; |
2666 | } | |
693bd40b | 2667 | |
3bd1e081 | 2668 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2669 | if (ret < 0) { |
3bd1e081 MD |
2670 | ERR("Spawning consumerd failed"); |
2671 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2672 | goto error; |
693bd40b | 2673 | } |
c49dc785 | 2674 | |
3bd1e081 MD |
2675 | /* Setting up the consumer_data pid */ |
2676 | consumer_data->pid = ret; | |
48842b30 | 2677 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2678 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2679 | |
3bd1e081 MD |
2680 | DBG2("Spawning consumer control thread"); |
2681 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2682 | if (ret < 0) { |
3bd1e081 | 2683 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2684 | goto error; |
2685 | } | |
2686 | ||
c49dc785 | 2687 | end: |
693bd40b DG |
2688 | return 0; |
2689 | ||
2690 | error: | |
331744e3 | 2691 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2692 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2693 | int err; |
2694 | ||
edb8b045 DG |
2695 | err = close(consumer_data->err_sock); |
2696 | if (err < 0) { | |
2697 | PERROR("close consumer data error socket"); | |
2698 | } | |
2699 | } | |
693bd40b DG |
2700 | return ret; |
2701 | } | |
2702 | ||
b73401da | 2703 | /* |
096102bd | 2704 | * Setup necessary data for kernel tracer action. |
b73401da | 2705 | */ |
096102bd | 2706 | static int init_kernel_tracer(void) |
b73401da DG |
2707 | { |
2708 | int ret; | |
b73401da | 2709 | |
096102bd DG |
2710 | /* Modprobe lttng kernel modules */ |
2711 | ret = modprobe_lttng_control(); | |
b73401da | 2712 | if (ret < 0) { |
b73401da DG |
2713 | goto error; |
2714 | } | |
2715 | ||
096102bd DG |
2716 | /* Open debugfs lttng */ |
2717 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2718 | if (kernel_tracer_fd < 0) { | |
2719 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 2720 | goto error_open; |
54d01ffb DG |
2721 | } |
2722 | ||
2f77fc4b DG |
2723 | /* Validate kernel version */ |
2724 | ret = kernel_validate_version(kernel_tracer_fd); | |
2725 | if (ret < 0) { | |
2726 | goto error_version; | |
b551a063 | 2727 | } |
54d01ffb | 2728 | |
2f77fc4b DG |
2729 | ret = modprobe_lttng_data(); |
2730 | if (ret < 0) { | |
2731 | goto error_modules; | |
54d01ffb DG |
2732 | } |
2733 | ||
2f77fc4b DG |
2734 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2735 | return 0; | |
2736 | ||
2737 | error_version: | |
2738 | modprobe_remove_lttng_control(); | |
2739 | ret = close(kernel_tracer_fd); | |
2740 | if (ret) { | |
2741 | PERROR("close"); | |
b551a063 | 2742 | } |
2f77fc4b | 2743 | kernel_tracer_fd = -1; |
f73fabfd | 2744 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2745 | |
2f77fc4b DG |
2746 | error_modules: |
2747 | ret = close(kernel_tracer_fd); | |
2748 | if (ret) { | |
2749 | PERROR("close"); | |
b551a063 | 2750 | } |
54d01ffb | 2751 | |
2f77fc4b DG |
2752 | error_open: |
2753 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2754 | |
2755 | error: | |
2f77fc4b DG |
2756 | WARN("No kernel tracer available"); |
2757 | kernel_tracer_fd = -1; | |
2758 | if (!is_root) { | |
f73fabfd | 2759 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2760 | } else { |
f73fabfd | 2761 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2762 | } |
54d01ffb DG |
2763 | } |
2764 | ||
2f77fc4b | 2765 | |
54d01ffb | 2766 | /* |
2f77fc4b DG |
2767 | * Copy consumer output from the tracing session to the domain session. The |
2768 | * function also applies the right modification on a per domain basis for the | |
2769 | * trace files destination directory. | |
36b588ed MD |
2770 | * |
2771 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2772 | */ |
2f77fc4b | 2773 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2774 | { |
2775 | int ret; | |
2f77fc4b DG |
2776 | const char *dir_name; |
2777 | struct consumer_output *consumer; | |
2778 | ||
2779 | assert(session); | |
2780 | assert(session->consumer); | |
54d01ffb | 2781 | |
b551a063 DG |
2782 | switch (domain) { |
2783 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2784 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2785 | /* |
2786 | * XXX: We should audit the session creation and what this function | |
2787 | * does "extra" in order to avoid a destroy since this function is used | |
2788 | * in the domain session creation (kernel and ust) only. Same for UST | |
2789 | * domain. | |
2790 | */ | |
2791 | if (session->kernel_session->consumer) { | |
6addfa37 | 2792 | consumer_output_put(session->kernel_session->consumer); |
09a90bcd | 2793 | } |
2f77fc4b DG |
2794 | session->kernel_session->consumer = |
2795 | consumer_copy_output(session->consumer); | |
2796 | /* Ease our life a bit for the next part */ | |
2797 | consumer = session->kernel_session->consumer; | |
2798 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2799 | break; |
f20baf8e | 2800 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2801 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2802 | case LTTNG_DOMAIN_PYTHON: |
b551a063 | 2803 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2804 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd | 2805 | if (session->ust_session->consumer) { |
6addfa37 | 2806 | consumer_output_put(session->ust_session->consumer); |
09a90bcd | 2807 | } |
2f77fc4b DG |
2808 | session->ust_session->consumer = |
2809 | consumer_copy_output(session->consumer); | |
2810 | /* Ease our life a bit for the next part */ | |
2811 | consumer = session->ust_session->consumer; | |
2812 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2813 | break; |
2814 | default: | |
f73fabfd | 2815 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2816 | goto error; |
2817 | } | |
2818 | ||
2f77fc4b | 2819 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2820 | strncat(consumer->subdir, dir_name, |
2821 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2822 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2823 | ||
f73fabfd | 2824 | ret = LTTNG_OK; |
54d01ffb DG |
2825 | |
2826 | error: | |
2827 | return ret; | |
2828 | } | |
2829 | ||
00e2e675 | 2830 | /* |
2f77fc4b | 2831 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2832 | * |
2833 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2834 | */ |
2f77fc4b DG |
2835 | static int create_ust_session(struct ltt_session *session, |
2836 | struct lttng_domain *domain) | |
00e2e675 DG |
2837 | { |
2838 | int ret; | |
2f77fc4b | 2839 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2840 | |
a4b92340 | 2841 | assert(session); |
2f77fc4b DG |
2842 | assert(domain); |
2843 | assert(session->consumer); | |
a4b92340 | 2844 | |
2f77fc4b | 2845 | switch (domain->type) { |
f20baf8e | 2846 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2847 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2848 | case LTTNG_DOMAIN_PYTHON: |
2f77fc4b DG |
2849 | case LTTNG_DOMAIN_UST: |
2850 | break; | |
2851 | default: | |
2852 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2853 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2854 | goto error; |
2855 | } | |
2856 | ||
2f77fc4b DG |
2857 | DBG("Creating UST session"); |
2858 | ||
dec56f6c | 2859 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2860 | if (lus == NULL) { |
f73fabfd | 2861 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2862 | goto error; |
2863 | } | |
2864 | ||
2f77fc4b DG |
2865 | lus->uid = session->uid; |
2866 | lus->gid = session->gid; | |
2bba9e53 | 2867 | lus->output_traces = session->output_traces; |
27babd3a | 2868 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2869 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2870 | session->ust_session = lus; |
d7ba1388 | 2871 | if (session->shm_path[0]) { |
3d071855 MD |
2872 | strncpy(lus->root_shm_path, session->shm_path, |
2873 | sizeof(lus->root_shm_path)); | |
2874 | lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2875 | strncpy(lus->shm_path, session->shm_path, |
2876 | sizeof(lus->shm_path)); | |
2877 | lus->shm_path[sizeof(lus->shm_path) - 1] = '\0'; | |
2878 | strncat(lus->shm_path, "/ust", | |
2879 | sizeof(lus->shm_path) - strlen(lus->shm_path) - 1); | |
2880 | } | |
2f77fc4b DG |
2881 | /* Copy session output to the newly created UST session */ |
2882 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2883 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2884 | goto error; |
2885 | } | |
2886 | ||
f73fabfd | 2887 | return LTTNG_OK; |
00e2e675 DG |
2888 | |
2889 | error: | |
2f77fc4b DG |
2890 | free(lus); |
2891 | session->ust_session = NULL; | |
00e2e675 DG |
2892 | return ret; |
2893 | } | |
2894 | ||
2895 | /* | |
2f77fc4b | 2896 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2897 | */ |
2f77fc4b | 2898 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2899 | { |
2900 | int ret; | |
a4b92340 | 2901 | |
2f77fc4b | 2902 | DBG("Creating kernel session"); |
00e2e675 | 2903 | |
2f77fc4b DG |
2904 | ret = kernel_create_session(session, kernel_tracer_fd); |
2905 | if (ret < 0) { | |
f73fabfd | 2906 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2907 | goto error; |
2908 | } | |
2909 | ||
2f77fc4b DG |
2910 | /* Code flow safety */ |
2911 | assert(session->kernel_session); | |
2912 | ||
2913 | /* Copy session output to the newly created Kernel session */ | |
2914 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2915 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2916 | goto error; |
2917 | } | |
2918 | ||
2f77fc4b DG |
2919 | /* Create directory(ies) on local filesystem. */ |
2920 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && | |
2921 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
2922 | ret = run_as_mkdir_recursive( | |
2923 | session->kernel_session->consumer->dst.trace_path, | |
2924 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2925 | if (ret < 0) { | |
df5b86c8 | 2926 | if (errno != EEXIST) { |
2f77fc4b | 2927 | ERR("Trace directory creation error"); |
00e2e675 DG |
2928 | goto error; |
2929 | } | |
00e2e675 | 2930 | } |
2f77fc4b | 2931 | } |
00e2e675 | 2932 | |
2f77fc4b DG |
2933 | session->kernel_session->uid = session->uid; |
2934 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2935 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2936 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2937 | |
f73fabfd | 2938 | return LTTNG_OK; |
00e2e675 | 2939 | |
2f77fc4b DG |
2940 | error: |
2941 | trace_kernel_destroy_session(session->kernel_session); | |
2942 | session->kernel_session = NULL; | |
2943 | return ret; | |
2944 | } | |
00e2e675 | 2945 | |
2f77fc4b DG |
2946 | /* |
2947 | * Count number of session permitted by uid/gid. | |
2948 | */ | |
2949 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2950 | { | |
2951 | unsigned int i = 0; | |
2952 | struct ltt_session *session; | |
07424f16 | 2953 | |
2f77fc4b DG |
2954 | DBG("Counting number of available session for UID %d GID %d", |
2955 | uid, gid); | |
2956 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2957 | /* |
2f77fc4b | 2958 | * Only list the sessions the user can control. |
00e2e675 | 2959 | */ |
2f77fc4b DG |
2960 | if (!session_access_ok(session, uid, gid)) { |
2961 | continue; | |
2962 | } | |
2963 | i++; | |
a4b92340 | 2964 | } |
2f77fc4b | 2965 | return i; |
00e2e675 DG |
2966 | } |
2967 | ||
54d01ffb DG |
2968 | /* |
2969 | * Process the command requested by the lttng client within the command | |
2970 | * context structure. This function make sure that the return structure (llm) | |
2971 | * is set and ready for transmission before returning. | |
2972 | * | |
2973 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2974 | * |
2975 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2976 | * |
2977 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2978 | */ |
53a80697 MD |
2979 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2980 | int *sock_error) | |
54d01ffb | 2981 | { |
f73fabfd | 2982 | int ret = LTTNG_OK; |
44d3bd01 | 2983 | int need_tracing_session = 1; |
2e09ba09 | 2984 | int need_domain; |
54d01ffb DG |
2985 | |
2986 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2987 | ||
3745d315 MD |
2988 | assert(!rcu_read_ongoing()); |
2989 | ||
53a80697 MD |
2990 | *sock_error = 0; |
2991 | ||
2e09ba09 MD |
2992 | switch (cmd_ctx->lsm->cmd_type) { |
2993 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2994 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2995 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2996 | case LTTNG_DESTROY_SESSION: |
2997 | case LTTNG_LIST_SESSIONS: | |
2998 | case LTTNG_LIST_DOMAINS: | |
2999 | case LTTNG_START_TRACE: | |
3000 | case LTTNG_STOP_TRACE: | |
6d805429 | 3001 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
3002 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3003 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3004 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
3005 | case LTTNG_SNAPSHOT_RECORD: | |
fb198a11 | 3006 | case LTTNG_SAVE_SESSION: |
d7ba1388 | 3007 | case LTTNG_SET_SESSION_SHM_PATH: |
eded6438 | 3008 | case LTTNG_REGENERATE_METADATA: |
c2561365 | 3009 | case LTTNG_REGENERATE_STATEDUMP: |
2e09ba09 | 3010 | need_domain = 0; |
3aace903 | 3011 | break; |
2e09ba09 MD |
3012 | default: |
3013 | need_domain = 1; | |
3014 | } | |
3015 | ||
3016 | if (opt_no_kernel && need_domain | |
3017 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 | 3018 | if (!is_root) { |
f73fabfd | 3019 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 3020 | } else { |
f73fabfd | 3021 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 3022 | } |
4fba7219 DG |
3023 | goto error; |
3024 | } | |
3025 | ||
8d3113b2 DG |
3026 | /* Deny register consumer if we already have a spawned consumer. */ |
3027 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
3028 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
3029 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 3030 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 3031 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
3032 | goto error; |
3033 | } | |
3034 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
3035 | } | |
3036 | ||
54d01ffb DG |
3037 | /* |
3038 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 3039 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
3040 | * command. |
3041 | */ | |
3042 | switch(cmd_ctx->lsm->cmd_type) { | |
3043 | case LTTNG_LIST_SESSIONS: | |
3044 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 3045 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
3046 | case LTTNG_LIST_DOMAINS: |
3047 | case LTTNG_LIST_CHANNELS: | |
3048 | case LTTNG_LIST_EVENTS: | |
834978fd | 3049 | case LTTNG_LIST_SYSCALLS: |
a5dfbb9d | 3050 | case LTTNG_LIST_TRACKER_PIDS: |
5cd0780d | 3051 | case LTTNG_DATA_PENDING: |
54d01ffb DG |
3052 | break; |
3053 | default: | |
3054 | /* Setup lttng message with no payload */ | |
6e10c9b9 | 3055 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0); |
54d01ffb DG |
3056 | if (ret < 0) { |
3057 | /* This label does not try to unlock the session */ | |
3058 | goto init_setup_error; | |
3059 | } | |
3060 | } | |
3061 | ||
3062 | /* Commands that DO NOT need a session. */ | |
3063 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 3064 | case LTTNG_CREATE_SESSION: |
27babd3a | 3065 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 3066 | case LTTNG_CREATE_SESSION_LIVE: |
54d01ffb DG |
3067 | case LTTNG_LIST_SESSIONS: |
3068 | case LTTNG_LIST_TRACEPOINTS: | |
834978fd | 3069 | case LTTNG_LIST_SYSCALLS: |
f37d259d | 3070 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
fb198a11 | 3071 | case LTTNG_SAVE_SESSION: |
44d3bd01 | 3072 | need_tracing_session = 0; |
54d01ffb DG |
3073 | break; |
3074 | default: | |
3075 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
3076 | /* |
3077 | * We keep the session list lock across _all_ commands | |
3078 | * for now, because the per-session lock does not | |
3079 | * handle teardown properly. | |
3080 | */ | |
74babd95 | 3081 | session_lock_list(); |
54d01ffb DG |
3082 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
3083 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 3084 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
3085 | goto error; |
3086 | } else { | |
3087 | /* Acquire lock for the session */ | |
3088 | session_lock(cmd_ctx->session); | |
3089 | } | |
3090 | break; | |
3091 | } | |
b389abbe | 3092 | |
5f3ecf22 DG |
3093 | /* |
3094 | * Commands that need a valid session but should NOT create one if none | |
3095 | * exists. Instead of creating one and destroying it when the command is | |
3096 | * handled, process that right before so we save some round trip in useless | |
3097 | * code path. | |
3098 | */ | |
3099 | switch (cmd_ctx->lsm->cmd_type) { | |
3100 | case LTTNG_DISABLE_CHANNEL: | |
3101 | case LTTNG_DISABLE_EVENT: | |
5f3ecf22 DG |
3102 | switch (cmd_ctx->lsm->domain.type) { |
3103 | case LTTNG_DOMAIN_KERNEL: | |
3104 | if (!cmd_ctx->session->kernel_session) { | |
3105 | ret = LTTNG_ERR_NO_CHANNEL; | |
3106 | goto error; | |
3107 | } | |
3108 | break; | |
3109 | case LTTNG_DOMAIN_JUL: | |
5cdb6027 | 3110 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3111 | case LTTNG_DOMAIN_PYTHON: |
5f3ecf22 DG |
3112 | case LTTNG_DOMAIN_UST: |
3113 | if (!cmd_ctx->session->ust_session) { | |
3114 | ret = LTTNG_ERR_NO_CHANNEL; | |
3115 | goto error; | |
3116 | } | |
3117 | break; | |
3118 | default: | |
3119 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3120 | goto error; | |
3121 | } | |
3122 | default: | |
3123 | break; | |
3124 | } | |
3125 | ||
2e09ba09 MD |
3126 | if (!need_domain) { |
3127 | goto skip_domain; | |
3128 | } | |
a4b92340 | 3129 | |
54d01ffb DG |
3130 | /* |
3131 | * Check domain type for specific "pre-action". | |
3132 | */ | |
3133 | switch (cmd_ctx->lsm->domain.type) { | |
3134 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 3135 | if (!is_root) { |
f73fabfd | 3136 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
3137 | goto error; |
3138 | } | |
3139 | ||
54d01ffb | 3140 | /* Kernel tracer check */ |
a4b35e07 | 3141 | if (kernel_tracer_fd == -1) { |
54d01ffb | 3142 | /* Basically, load kernel tracer modules */ |
096102bd DG |
3143 | ret = init_kernel_tracer(); |
3144 | if (ret != 0) { | |
54d01ffb DG |
3145 | goto error; |
3146 | } | |
3147 | } | |
5eb91c98 | 3148 | |
5c827ce0 DG |
3149 | /* Consumer is in an ERROR state. Report back to client */ |
3150 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3151 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3152 | goto error; |
3153 | } | |
3154 | ||
54d01ffb | 3155 | /* Need a session for kernel command */ |
44d3bd01 | 3156 | if (need_tracing_session) { |
54d01ffb | 3157 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 3158 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 3159 | if (ret < 0) { |
f73fabfd | 3160 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
3161 | goto error; |
3162 | } | |
b389abbe | 3163 | } |
7d29a247 | 3164 | |
54d01ffb | 3165 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
3166 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
3167 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 3168 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
3169 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
3170 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 3171 | if (ret < 0) { |
f73fabfd | 3172 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 3173 | goto error; |
950131af | 3174 | } |
5c827ce0 | 3175 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
3176 | } else { |
3177 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 3178 | } |
173af62f | 3179 | |
a4b92340 DG |
3180 | /* |
3181 | * The consumer was just spawned so we need to add the socket to | |
3182 | * the consumer output of the session if exist. | |
3183 | */ | |
3184 | ret = consumer_create_socket(&kconsumer_data, | |
3185 | cmd_ctx->session->kernel_session->consumer); | |
3186 | if (ret < 0) { | |
3187 | goto error; | |
173af62f | 3188 | } |
0d0c377a | 3189 | } |
5c827ce0 | 3190 | |
54d01ffb | 3191 | break; |
b9dfb167 | 3192 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3193 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3194 | case LTTNG_DOMAIN_PYTHON: |
2bdd86d4 | 3195 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 3196 | { |
b51ec5b4 MD |
3197 | if (!ust_app_supported()) { |
3198 | ret = LTTNG_ERR_NO_UST; | |
3199 | goto error; | |
3200 | } | |
5c827ce0 DG |
3201 | /* Consumer is in an ERROR state. Report back to client */ |
3202 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3203 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3204 | goto error; |
3205 | } | |
3206 | ||
44d3bd01 | 3207 | if (need_tracing_session) { |
a4b92340 | 3208 | /* Create UST session if none exist. */ |
f6a9efaa | 3209 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 3210 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 3211 | &cmd_ctx->lsm->domain); |
f73fabfd | 3212 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
3213 | goto error; |
3214 | } | |
3215 | } | |
00e2e675 | 3216 | |
7753dea8 MD |
3217 | /* Start the UST consumer daemons */ |
3218 | /* 64-bit */ | |
3219 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 3220 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 3221 | ustconsumer64_data.pid == 0 && |
785d2d0d | 3222 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3223 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3224 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 3225 | if (ret < 0) { |
f73fabfd | 3226 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 3227 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
3228 | goto error; |
3229 | } | |
48842b30 | 3230 | |
173af62f | 3231 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 3232 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 3233 | } else { |
7753dea8 MD |
3234 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3235 | } | |
173af62f DG |
3236 | |
3237 | /* | |
3238 | * Setup socket for consumer 64 bit. No need for atomic access | |
3239 | * since it was set above and can ONLY be set in this thread. | |
3240 | */ | |
a4b92340 DG |
3241 | ret = consumer_create_socket(&ustconsumer64_data, |
3242 | cmd_ctx->session->ust_session->consumer); | |
3243 | if (ret < 0) { | |
3244 | goto error; | |
173af62f DG |
3245 | } |
3246 | ||
7753dea8 | 3247 | /* 32-bit */ |
385b881b | 3248 | pthread_mutex_lock(&ustconsumer32_data.pid_mutex); |
fc7a59ce | 3249 | if (consumerd32_bin[0] != '\0' && |
7753dea8 | 3250 | ustconsumer32_data.pid == 0 && |
785d2d0d | 3251 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3252 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
3253 | ret = start_consumerd(&ustconsumer32_data); | |
3254 | if (ret < 0) { | |
f73fabfd | 3255 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 3256 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
3257 | goto error; |
3258 | } | |
5c827ce0 | 3259 | |
173af62f | 3260 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 3261 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
3262 | } else { |
3263 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 3264 | } |
173af62f DG |
3265 | |
3266 | /* | |
3267 | * Setup socket for consumer 64 bit. No need for atomic access | |
3268 | * since it was set above and can ONLY be set in this thread. | |
3269 | */ | |
a4b92340 DG |
3270 | ret = consumer_create_socket(&ustconsumer32_data, |
3271 | cmd_ctx->session->ust_session->consumer); | |
3272 | if (ret < 0) { | |
3273 | goto error; | |
173af62f | 3274 | } |
44d3bd01 DG |
3275 | } |
3276 | break; | |
48842b30 | 3277 | } |
54d01ffb | 3278 | default: |
54d01ffb DG |
3279 | break; |
3280 | } | |
2e09ba09 | 3281 | skip_domain: |
33a2b854 | 3282 | |
5c827ce0 DG |
3283 | /* Validate consumer daemon state when start/stop trace command */ |
3284 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
3285 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
3286 | switch (cmd_ctx->lsm->domain.type) { | |
e7619875 JG |
3287 | case LTTNG_DOMAIN_NONE: |
3288 | break; | |
b9dfb167 | 3289 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3290 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3291 | case LTTNG_DOMAIN_PYTHON: |
5c827ce0 DG |
3292 | case LTTNG_DOMAIN_UST: |
3293 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3294 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3295 | goto error; |
3296 | } | |
3297 | break; | |
3298 | case LTTNG_DOMAIN_KERNEL: | |
3299 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3300 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3301 | goto error; |
3302 | } | |
3303 | break; | |
825535cc JG |
3304 | default: |
3305 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3306 | goto error; | |
5c827ce0 DG |
3307 | } |
3308 | } | |
3309 | ||
8e0af1b4 MD |
3310 | /* |
3311 | * Check that the UID or GID match that of the tracing session. | |
3312 | * The root user can interact with all sessions. | |
3313 | */ | |
3314 | if (need_tracing_session) { | |
3315 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
3316 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3317 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
f73fabfd | 3318 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
3319 | goto error; |
3320 | } | |
3321 | } | |
3322 | ||
ffe60014 DG |
3323 | /* |
3324 | * Send relayd information to consumer as soon as we have a domain and a | |
3325 | * session defined. | |
3326 | */ | |
3327 | if (cmd_ctx->session && need_domain) { | |
3328 | /* | |
3329 | * Setup relayd if not done yet. If the relayd information was already | |
3330 | * sent to the consumer, this call will gracefully return. | |
3331 | */ | |
3332 | ret = cmd_setup_relayd(cmd_ctx->session); | |
3333 | if (ret != LTTNG_OK) { | |
3334 | goto error; | |
3335 | } | |
3336 | } | |
3337 | ||
54d01ffb DG |
3338 | /* Process by command type */ |
3339 | switch (cmd_ctx->lsm->cmd_type) { | |
3340 | case LTTNG_ADD_CONTEXT: | |
3341 | { | |
2001793c JG |
3342 | /* |
3343 | * An LTTNG_ADD_CONTEXT command might have a supplementary | |
3344 | * payload if the context being added is an application context. | |
3345 | */ | |
3346 | if (cmd_ctx->lsm->u.context.ctx.ctx == | |
3347 | LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
3348 | char *provider_name = NULL, *context_name = NULL; | |
3349 | size_t provider_name_len = | |
3350 | cmd_ctx->lsm->u.context.provider_name_len; | |
3351 | size_t context_name_len = | |
3352 | cmd_ctx->lsm->u.context.context_name_len; | |
3353 | ||
3354 | if (provider_name_len == 0 || context_name_len == 0) { | |
3355 | /* | |
3356 | * Application provider and context names MUST | |
3357 | * be provided. | |
3358 | */ | |
3359 | ret = -LTTNG_ERR_INVALID; | |
3360 | goto error; | |
3361 | } | |
3362 | ||
3363 | provider_name = zmalloc(provider_name_len + 1); | |
3364 | if (!provider_name) { | |
3365 | ret = -LTTNG_ERR_NOMEM; | |
3366 | goto error; | |
3367 | } | |
e0f4d107 JG |
3368 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = |
3369 | provider_name; | |
2001793c JG |
3370 | |
3371 | context_name = zmalloc(context_name_len + 1); | |
3372 | if (!context_name) { | |
3373 | ret = -LTTNG_ERR_NOMEM; | |
3374 | goto error_add_context; | |
3375 | } | |
e0f4d107 JG |
3376 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = |
3377 | context_name; | |
2001793c JG |
3378 | |
3379 | ret = lttcomm_recv_unix_sock(sock, provider_name, | |
3380 | provider_name_len); | |
3381 | if (ret < 0) { | |
3382 | goto error_add_context; | |
3383 | } | |
3384 | ||
3385 | ret = lttcomm_recv_unix_sock(sock, context_name, | |
3386 | context_name_len); | |
3387 | if (ret < 0) { | |
3388 | goto error_add_context; | |
3389 | } | |
2001793c JG |
3390 | } |
3391 | ||
3392 | /* | |
3393 | * cmd_add_context assumes ownership of the provider and context | |
3394 | * names. | |
3395 | */ | |
3396 | ret = cmd_add_context(cmd_ctx->session, | |
3397 | cmd_ctx->lsm->domain.type, | |
54d01ffb | 3398 | cmd_ctx->lsm->u.context.channel_name, |
2001793c JG |
3399 | &cmd_ctx->lsm->u.context.ctx, |
3400 | kernel_poll_pipe[1]); | |
3401 | ||
3402 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL; | |
3403 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL; | |
3404 | error_add_context: | |
3405 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name); | |
3406 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name); | |
3407 | if (ret < 0) { | |
3408 | goto error; | |
3409 | } | |
54d01ffb DG |
3410 | break; |
3411 | } | |
3412 | case LTTNG_DISABLE_CHANNEL: | |
3413 | { | |
3414 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3415 | cmd_ctx->lsm->u.disable.channel_name); | |
3416 | break; | |
3417 | } | |
3418 | case LTTNG_DISABLE_EVENT: | |
3419 | { | |
47b7e2f9 JG |
3420 | |
3421 | /* | |
3422 | * FIXME: handle filter; for now we just receive the filter's | |
3423 | * bytecode along with the filter expression which are sent by | |
3424 | * liblttng-ctl and discard them. | |
3425 | * | |
3426 | * This fixes an issue where the client may block while sending | |
3427 | * the filter payload and encounter an error because the session | |
3428 | * daemon closes the socket without ever handling this data. | |
3429 | */ | |
3430 | size_t count = cmd_ctx->lsm->u.disable.expression_len + | |
3431 | cmd_ctx->lsm->u.disable.bytecode_len; | |
3432 | ||
3433 | if (count) { | |
3434 | char data[LTTNG_FILTER_MAX_LEN]; | |
3435 | ||
3436 | DBG("Discarding disable event command payload of size %zu", count); | |
3437 | while (count) { | |
3438 | ret = lttcomm_recv_unix_sock(sock, data, | |
3439 | count > sizeof(data) ? sizeof(data) : count); | |
3440 | if (ret < 0) { | |
3441 | goto error; | |
3442 | } | |
3443 | ||
3444 | count -= (size_t) ret; | |
3445 | } | |
3446 | } | |
6e911cad | 3447 | /* FIXME: passing packed structure to non-packed pointer */ |
54d01ffb DG |
3448 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
3449 | cmd_ctx->lsm->u.disable.channel_name, | |
6e911cad | 3450 | &cmd_ctx->lsm->u.disable.event); |
33a2b854 DG |
3451 | break; |
3452 | } | |
54d01ffb DG |
3453 | case LTTNG_ENABLE_CHANNEL: |
3454 | { | |
7972aab2 | 3455 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
2f77fc4b | 3456 | &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]); |
54d01ffb DG |
3457 | break; |
3458 | } | |
ccf10263 MD |
3459 | case LTTNG_TRACK_PID: |
3460 | { | |
3461 | ret = cmd_track_pid(cmd_ctx->session, | |
3462 | cmd_ctx->lsm->domain.type, | |
3463 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3464 | break; | |
3465 | } | |
3466 | case LTTNG_UNTRACK_PID: | |
3467 | { | |
3468 | ret = cmd_untrack_pid(cmd_ctx->session, | |
3469 | cmd_ctx->lsm->domain.type, | |
3470 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3471 | break; | |
3472 | } | |
54d01ffb DG |
3473 | case LTTNG_ENABLE_EVENT: |
3474 | { | |
882ef835 JI |
3475 | struct lttng_event_exclusion *exclusion = NULL; |
3476 | struct lttng_filter_bytecode *bytecode = NULL; | |
6b453b5e | 3477 | char *filter_expression = NULL; |
882ef835 | 3478 | |
67676bd8 DG |
3479 | /* Handle exclusion events and receive it from the client. */ |
3480 | if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { | |
3481 | size_t count = cmd_ctx->lsm->u.enable.exclusion_count; | |
3482 | ||
3483 | exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + | |
3484 | (count * LTTNG_SYMBOL_NAME_LEN)); | |
3485 | if (!exclusion) { | |
3486 | ret = LTTNG_ERR_EXCLUSION_NOMEM; | |
3487 | goto error; | |
882ef835 | 3488 | } |
882ef835 | 3489 | |
67676bd8 DG |
3490 | DBG("Receiving var len exclusion event list from client ..."); |
3491 | exclusion->count = count; | |
3492 | ret = lttcomm_recv_unix_sock(sock, exclusion->names, | |
3493 | count * LTTNG_SYMBOL_NAME_LEN); | |
3494 | if (ret <= 0) { | |
3495 | DBG("Nothing recv() from client var len data... continuing"); | |
3496 | *sock_error = 1; | |
3497 | free(exclusion); | |
3498 | ret = LTTNG_ERR_EXCLUSION_INVAL; | |
3499 | goto error; | |
3500 | } | |
3501 | } | |
3502 | ||
6b453b5e JG |
3503 | /* Get filter expression from client. */ |
3504 | if (cmd_ctx->lsm->u.enable.expression_len > 0) { | |
3505 | size_t expression_len = | |
3506 | cmd_ctx->lsm->u.enable.expression_len; | |
3507 | ||
3508 | if (expression_len > LTTNG_FILTER_MAX_LEN) { | |
3509 | ret = LTTNG_ERR_FILTER_INVAL; | |
3510 | free(exclusion); | |
3511 | goto error; | |
3512 | } | |
3513 | ||
3514 | filter_expression = zmalloc(expression_len); | |
3515 | if (!filter_expression) { | |
3516 | free(exclusion); | |
3517 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3518 | goto error; | |
3519 | } | |
3520 | ||
3521 | /* Receive var. len. data */ | |
3522 | DBG("Receiving var len filter's expression from client ..."); | |
3523 | ret = lttcomm_recv_unix_sock(sock, filter_expression, | |
3524 | expression_len); | |
3525 | if (ret <= 0) { | |
3526 | DBG("Nothing recv() from client car len data... continuing"); | |
3527 | *sock_error = 1; | |
3528 | free(filter_expression); | |
3529 | free(exclusion); | |
3530 | ret = LTTNG_ERR_FILTER_INVAL; | |
3531 | goto error; | |
3532 | } | |
3533 | } | |
3534 | ||
67676bd8 DG |
3535 | /* Handle filter and get bytecode from client. */ |
3536 | if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { | |
3537 | size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; | |
3538 | ||
3539 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
3540 | ret = LTTNG_ERR_FILTER_INVAL; | |
49d21f93 | 3541 | free(filter_expression); |
67676bd8 DG |
3542 | free(exclusion); |
3543 | goto error; | |
3544 | } | |
3545 | ||
3546 | bytecode = zmalloc(bytecode_len); | |
3547 | if (!bytecode) { | |
49d21f93 | 3548 | free(filter_expression); |
67676bd8 DG |
3549 | free(exclusion); |
3550 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3551 | goto error; | |
882ef835 JI |
3552 | } |
3553 | ||
67676bd8 DG |
3554 | /* Receive var. len. data */ |
3555 | DBG("Receiving var len filter's bytecode from client ..."); | |
3556 | ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len); | |
3557 | if (ret <= 0) { | |
3558 | DBG("Nothing recv() from client car len data... continuing"); | |
3559 | *sock_error = 1; | |
49d21f93 | 3560 | free(filter_expression); |
67676bd8 DG |
3561 | free(bytecode); |
3562 | free(exclusion); | |
3563 | ret = LTTNG_ERR_FILTER_INVAL; | |
3564 | goto error; | |
3565 | } | |
3566 | ||
3567 | if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { | |
49d21f93 | 3568 | free(filter_expression); |
67676bd8 DG |
3569 | free(bytecode); |
3570 | free(exclusion); | |
3571 | ret = LTTNG_ERR_FILTER_INVAL; | |
3572 | goto error; | |
3573 | } | |
882ef835 | 3574 | } |
67676bd8 DG |
3575 | |
3576 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, | |
3577 | cmd_ctx->lsm->u.enable.channel_name, | |
6b453b5e JG |
3578 | &cmd_ctx->lsm->u.enable.event, |
3579 | filter_expression, bytecode, exclusion, | |
67676bd8 | 3580 | kernel_poll_pipe[1]); |
54d01ffb DG |
3581 | break; |
3582 | } | |
052da939 | 3583 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 3584 | { |
9f19cc17 | 3585 | struct lttng_event *events; |
54d01ffb | 3586 | ssize_t nb_events; |
052da939 | 3587 | |
0845bbfa | 3588 | session_lock_list(); |
54d01ffb | 3589 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
0845bbfa | 3590 | session_unlock_list(); |
54d01ffb | 3591 | if (nb_events < 0) { |
f73fabfd | 3592 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3593 | ret = -nb_events; |
3594 | goto error; | |
2ef84c95 DG |
3595 | } |
3596 | ||
3597 | /* | |
3598 | * Setup lttng message with payload size set to the event list size in | |
3599 | * bytes and then copy list into the llm payload. | |
3600 | */ | |
6e10c9b9 PP |
3601 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3602 | sizeof(struct lttng_event) * nb_events); | |
3603 | free(events); | |
3604 | ||
2ef84c95 DG |
3605 | if (ret < 0) { |
3606 | goto setup_error; | |
3607 | } | |
3608 | ||
f73fabfd | 3609 | ret = LTTNG_OK; |
2ef84c95 DG |
3610 | break; |
3611 | } | |
f37d259d MD |
3612 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
3613 | { | |
3614 | struct lttng_event_field *fields; | |
3615 | ssize_t nb_fields; | |
3616 | ||
0845bbfa | 3617 | session_lock_list(); |
a4b92340 DG |
3618 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
3619 | &fields); | |
0845bbfa | 3620 | session_unlock_list(); |
f37d259d | 3621 | if (nb_fields < 0) { |
f73fabfd | 3622 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
3623 | ret = -nb_fields; |
3624 | goto error; | |
3625 | } | |
3626 | ||
3627 | /* | |
3628 | * Setup lttng message with payload size set to the event list size in | |
3629 | * bytes and then copy list into the llm payload. | |
3630 | */ | |
6e10c9b9 | 3631 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields, |
a4b92340 | 3632 | sizeof(struct lttng_event_field) * nb_fields); |
6e10c9b9 PP |
3633 | free(fields); |
3634 | ||
f37d259d | 3635 | if (ret < 0) { |
f37d259d MD |
3636 | goto setup_error; |
3637 | } | |
3638 | ||
f73fabfd | 3639 | ret = LTTNG_OK; |
f37d259d MD |
3640 | break; |
3641 | } | |
834978fd DG |
3642 | case LTTNG_LIST_SYSCALLS: |
3643 | { | |
3644 | struct lttng_event *events; | |
3645 | ssize_t nb_events; | |
3646 | ||
3647 | nb_events = cmd_list_syscalls(&events); | |
3648 | if (nb_events < 0) { | |
3649 | /* Return value is a negative lttng_error_code. */ | |
3650 | ret = -nb_events; | |
3651 | goto error; | |
3652 | } | |
3653 | ||
3654 | /* | |
3655 | * Setup lttng message with payload size set to the event list size in | |
3656 | * bytes and then copy list into the llm payload. | |
3657 | */ | |
6e10c9b9 PP |
3658 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3659 | sizeof(struct lttng_event) * nb_events); | |
3660 | free(events); | |
3661 | ||
834978fd | 3662 | if (ret < 0) { |
834978fd DG |
3663 | goto setup_error; |
3664 | } | |
3665 | ||
834978fd DG |
3666 | ret = LTTNG_OK; |
3667 | break; | |
3668 | } | |
a5dfbb9d MD |
3669 | case LTTNG_LIST_TRACKER_PIDS: |
3670 | { | |
3671 | int32_t *pids = NULL; | |
3672 | ssize_t nr_pids; | |
3673 | ||
3674 | nr_pids = cmd_list_tracker_pids(cmd_ctx->session, | |
3675 | cmd_ctx->lsm->domain.type, &pids); | |
3676 | if (nr_pids < 0) { | |
3677 | /* Return value is a negative lttng_error_code. */ | |
3678 | ret = -nr_pids; | |
3679 | goto error; | |
3680 | } | |
3681 | ||
3682 | /* | |
3683 | * Setup lttng message with payload size set to the event list size in | |
3684 | * bytes and then copy list into the llm payload. | |
3685 | */ | |
6e10c9b9 PP |
3686 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids, |
3687 | sizeof(int32_t) * nr_pids); | |
3688 | free(pids); | |
3689 | ||
a5dfbb9d | 3690 | if (ret < 0) { |
a5dfbb9d MD |
3691 | goto setup_error; |
3692 | } | |
3693 | ||
a5dfbb9d MD |
3694 | ret = LTTNG_OK; |
3695 | break; | |
3696 | } | |
00e2e675 DG |
3697 | case LTTNG_SET_CONSUMER_URI: |
3698 | { | |
a4b92340 DG |
3699 | size_t nb_uri, len; |
3700 | struct lttng_uri *uris; | |
3701 | ||
3702 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3703 | len = nb_uri * sizeof(struct lttng_uri); | |
3704 | ||
3705 | if (nb_uri == 0) { | |
f73fabfd | 3706 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
3707 | goto error; |
3708 | } | |
3709 | ||
3710 | uris = zmalloc(len); | |
3711 | if (uris == NULL) { | |
f73fabfd | 3712 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3713 | goto error; |
3714 | } | |
3715 | ||
3716 | /* Receive variable len data */ | |
77c7c900 | 3717 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
3718 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3719 | if (ret <= 0) { | |
3720 | DBG("No URIs received from client... continuing"); | |
3721 | *sock_error = 1; | |
f73fabfd | 3722 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3723 | free(uris); |
a4b92340 DG |
3724 | goto error; |
3725 | } | |
3726 | ||
bda32d56 JG |
3727 | ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris); |
3728 | free(uris); | |
f73fabfd | 3729 | if (ret != LTTNG_OK) { |
a4b92340 DG |
3730 | goto error; |
3731 | } | |
3732 | ||
b1d41407 | 3733 | |
00e2e675 DG |
3734 | break; |
3735 | } | |
f3ed775e | 3736 | case LTTNG_START_TRACE: |
8c0faa1d | 3737 | { |
54d01ffb | 3738 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3739 | break; |
3740 | } | |
f3ed775e | 3741 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3742 | { |
54d01ffb | 3743 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3744 | break; |
3745 | } | |
5e16da05 MD |
3746 | case LTTNG_CREATE_SESSION: |
3747 | { | |
a4b92340 DG |
3748 | size_t nb_uri, len; |
3749 | struct lttng_uri *uris = NULL; | |
3750 | ||
3751 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3752 | len = nb_uri * sizeof(struct lttng_uri); | |
3753 | ||
3754 | if (nb_uri > 0) { | |
3755 | uris = zmalloc(len); | |
3756 | if (uris == NULL) { | |
f73fabfd | 3757 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3758 | goto error; |
3759 | } | |
3760 | ||
3761 | /* Receive variable len data */ | |
77c7c900 | 3762 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
3763 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3764 | if (ret <= 0) { | |
3765 | DBG("No URIs received from client... continuing"); | |
3766 | *sock_error = 1; | |
f73fabfd | 3767 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3768 | free(uris); |
a4b92340 DG |
3769 | goto error; |
3770 | } | |
3771 | ||
3772 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3773 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 3774 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3775 | free(uris); |
a4b92340 DG |
3776 | goto error; |
3777 | } | |
3778 | } | |
3779 | ||
3780 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
ecc48a90 | 3781 | &cmd_ctx->creds, 0); |
a4b92340 | 3782 | |
b1d41407 DG |
3783 | free(uris); |
3784 | ||
00e2e675 DG |
3785 | break; |
3786 | } | |
5e16da05 MD |
3787 | case LTTNG_DESTROY_SESSION: |
3788 | { | |
2f77fc4b | 3789 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]); |
a4b92340 DG |
3790 | |
3791 | /* Set session to NULL so we do not unlock it after free. */ | |
256a5576 | 3792 | cmd_ctx->session = NULL; |
5461b305 | 3793 | break; |
5e16da05 | 3794 | } |
9f19cc17 | 3795 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3796 | { |
54d01ffb | 3797 | ssize_t nb_dom; |
fa64dfb4 | 3798 | struct lttng_domain *domains = NULL; |
5461b305 | 3799 | |
54d01ffb DG |
3800 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3801 | if (nb_dom < 0) { | |
f73fabfd | 3802 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3803 | ret = -nb_dom; |
3804 | goto error; | |
ce3d728c | 3805 | } |
520ff687 | 3806 | |
6e10c9b9 PP |
3807 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains, |
3808 | nb_dom * sizeof(struct lttng_domain)); | |
3809 | free(domains); | |
3810 | ||
5461b305 DG |
3811 | if (ret < 0) { |
3812 | goto setup_error; | |
520ff687 | 3813 | } |
57167058 | 3814 | |
f73fabfd | 3815 | ret = LTTNG_OK; |
5e16da05 MD |
3816 | break; |
3817 | } | |
9f19cc17 | 3818 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3819 | { |
53e367f9 | 3820 | ssize_t payload_size; |
ade7ce52 | 3821 | struct lttng_channel *channels = NULL; |
54d01ffb | 3822 | |
53e367f9 | 3823 | payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type, |
b551a063 | 3824 | cmd_ctx->session, &channels); |
53e367f9 | 3825 | if (payload_size < 0) { |
f73fabfd | 3826 | /* Return value is a negative lttng_error_code. */ |
53e367f9 | 3827 | ret = -payload_size; |
54d01ffb | 3828 | goto error; |
5461b305 | 3829 | } |
ca95a216 | 3830 | |
6e10c9b9 | 3831 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels, |
53e367f9 | 3832 | payload_size); |
6e10c9b9 PP |
3833 | free(channels); |
3834 | ||
5461b305 DG |
3835 | if (ret < 0) { |
3836 | goto setup_error; | |
3837 | } | |
9f19cc17 | 3838 | |
f73fabfd | 3839 | ret = LTTNG_OK; |
5461b305 | 3840 | break; |
5e16da05 | 3841 | } |
9f19cc17 | 3842 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3843 | { |
b551a063 | 3844 | ssize_t nb_event; |
684d34d2 | 3845 | struct lttng_event *events = NULL; |
b4e3ceb9 PP |
3846 | struct lttcomm_event_command_header cmd_header; |
3847 | size_t total_size; | |
3848 | ||
b2d66287 | 3849 | memset(&cmd_header, 0, sizeof(cmd_header)); |
b4e3ceb9 PP |
3850 | /* Extended infos are included at the end of events */ |
3851 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, | |
3852 | cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name, | |
3853 | &events, &total_size); | |
9f19cc17 | 3854 | |
54d01ffb | 3855 | if (nb_event < 0) { |
f73fabfd | 3856 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3857 | ret = -nb_event; |
3858 | goto error; | |
5461b305 | 3859 | } |
ca95a216 | 3860 | |
b4e3ceb9 PP |
3861 | cmd_header.nb_events = nb_event; |
3862 | ret = setup_lttng_msg(cmd_ctx, events, total_size, | |
3863 | &cmd_header, sizeof(cmd_header)); | |
6e10c9b9 PP |
3864 | free(events); |
3865 | ||
5461b305 DG |
3866 | if (ret < 0) { |
3867 | goto setup_error; | |
3868 | } | |
9f19cc17 | 3869 | |
f73fabfd | 3870 | ret = LTTNG_OK; |
5461b305 | 3871 | break; |
5e16da05 MD |
3872 | } |
3873 | case LTTNG_LIST_SESSIONS: | |
3874 | { | |
8e0af1b4 | 3875 | unsigned int nr_sessions; |
6e10c9b9 PP |
3876 | void *sessions_payload; |
3877 | size_t payload_len; | |
5461b305 | 3878 | |
8e0af1b4 | 3879 | session_lock_list(); |
730389d9 DG |
3880 | nr_sessions = lttng_sessions_count( |
3881 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
3882 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6e10c9b9 PP |
3883 | payload_len = sizeof(struct lttng_session) * nr_sessions; |
3884 | sessions_payload = zmalloc(payload_len); | |
d32fb093 | 3885 | |
6e10c9b9 | 3886 | if (!sessions_payload) { |
54d01ffb | 3887 | session_unlock_list(); |
6e10c9b9 | 3888 | ret = -ENOMEM; |
5461b305 | 3889 | goto setup_error; |
e065084a | 3890 | } |
5e16da05 | 3891 | |
6e10c9b9 | 3892 | cmd_list_lttng_sessions(sessions_payload, |
730389d9 DG |
3893 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3894 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
54d01ffb | 3895 | session_unlock_list(); |
5e16da05 | 3896 | |
6e10c9b9 PP |
3897 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, |
3898 | payload_len); | |
3899 | free(sessions_payload); | |
3900 | ||
3901 | if (ret < 0) { | |
3902 | goto setup_error; | |
3903 | } | |
3904 | ||
f73fabfd | 3905 | ret = LTTNG_OK; |
5e16da05 MD |
3906 | break; |
3907 | } | |
d9800920 DG |
3908 | case LTTNG_REGISTER_CONSUMER: |
3909 | { | |
2f77fc4b DG |
3910 | struct consumer_data *cdata; |
3911 | ||
3912 | switch (cmd_ctx->lsm->domain.type) { | |
3913 | case LTTNG_DOMAIN_KERNEL: | |
3914 | cdata = &kconsumer_data; | |
3915 | break; | |
3916 | default: | |
f73fabfd | 3917 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3918 | goto error; |
3919 | } | |
3920 | ||
54d01ffb | 3921 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 3922 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
3923 | break; |
3924 | } | |
6d805429 | 3925 | case LTTNG_DATA_PENDING: |
806e2684 | 3926 | { |
f6151c55 | 3927 | int pending_ret; |
6e10c9b9 | 3928 | uint8_t pending_ret_byte; |
f6151c55 JG |
3929 | |
3930 | pending_ret = cmd_data_pending(cmd_ctx->session); | |
6e10c9b9 | 3931 | |
f6151c55 JG |
3932 | /* |
3933 | * FIXME | |
3934 | * | |
3935 | * This function may returns 0 or 1 to indicate whether or not | |
3936 | * there is data pending. In case of error, it should return an | |
3937 | * LTTNG_ERR code. However, some code paths may still return | |
3938 | * a nondescript error code, which we handle by returning an | |
3939 | * "unknown" error. | |
3940 | */ | |
3941 | if (pending_ret == 0 || pending_ret == 1) { | |
6e10c9b9 PP |
3942 | /* |
3943 | * ret will be set to LTTNG_OK at the end of | |
3944 | * this function. | |
3945 | */ | |
f6151c55 JG |
3946 | } else if (pending_ret < 0) { |
3947 | ret = LTTNG_ERR_UNK; | |
3948 | goto setup_error; | |
3949 | } else { | |
3950 | ret = pending_ret; | |
3951 | goto setup_error; | |
3952 | } | |
3953 | ||
6e10c9b9 PP |
3954 | pending_ret_byte = (uint8_t) pending_ret; |
3955 | ||
3956 | /* 1 byte to return whether or not data is pending */ | |
3957 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, | |
3958 | &pending_ret_byte, 1); | |
3959 | ||
3960 | if (ret < 0) { | |
3961 | goto setup_error; | |
3962 | } | |
3963 | ||
3964 | ret = LTTNG_OK; | |
806e2684 DG |
3965 | break; |
3966 | } | |
da3c9ec1 DG |
3967 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3968 | { | |
6dc3064a DG |
3969 | struct lttcomm_lttng_output_id reply; |
3970 | ||
3971 | ret = cmd_snapshot_add_output(cmd_ctx->session, | |
3972 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); | |
3973 | if (ret != LTTNG_OK) { | |
3974 | goto error; | |
3975 | } | |
3976 | ||
6e10c9b9 PP |
3977 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply, |
3978 | sizeof(reply)); | |
6dc3064a DG |
3979 | if (ret < 0) { |
3980 | goto setup_error; | |
3981 | } | |
3982 | ||
3983 | /* Copy output list into message payload */ | |
6dc3064a | 3984 | ret = LTTNG_OK; |
da3c9ec1 DG |
3985 | break; |
3986 | } | |
3987 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3988 | { | |
6dc3064a DG |
3989 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
3990 | &cmd_ctx->lsm->u.snapshot_output.output); | |
da3c9ec1 DG |
3991 | break; |
3992 | } | |
3993 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
3994 | { | |
6dc3064a DG |
3995 | ssize_t nb_output; |
3996 | struct lttng_snapshot_output *outputs = NULL; | |
3997 | ||
3998 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); | |
3999 | if (nb_output < 0) { | |
4000 | ret = -nb_output; | |
4001 | goto error; | |
4002 | } | |
4003 | ||
6e10c9b9 PP |
4004 | assert((nb_output > 0 && outputs) || nb_output == 0); |
4005 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs, | |
6dc3064a | 4006 | nb_output * sizeof(struct lttng_snapshot_output)); |
6e10c9b9 PP |
4007 | free(outputs); |
4008 | ||
6dc3064a | 4009 | if (ret < 0) { |
6dc3064a DG |
4010 | goto setup_error; |
4011 | } | |
4012 | ||
6dc3064a | 4013 | ret = LTTNG_OK; |
da3c9ec1 DG |
4014 | break; |
4015 | } | |
4016 | case LTTNG_SNAPSHOT_RECORD: | |
4017 | { | |
6dc3064a DG |
4018 | ret = cmd_snapshot_record(cmd_ctx->session, |
4019 | &cmd_ctx->lsm->u.snapshot_record.output, | |
4020 | cmd_ctx->lsm->u.snapshot_record.wait); | |
da3c9ec1 DG |
4021 | break; |
4022 | } | |
27babd3a DG |
4023 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
4024 | { | |
4025 | size_t nb_uri, len; | |
4026 | struct lttng_uri *uris = NULL; | |
4027 | ||
4028 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
4029 | len = nb_uri * sizeof(struct lttng_uri); | |
4030 | ||
4031 | if (nb_uri > 0) { | |
4032 | uris = zmalloc(len); | |
4033 | if (uris == NULL) { | |
4034 | ret = LTTNG_ERR_FATAL; | |
4035 | goto error; | |
4036 | } | |
4037 | ||
4038 | /* Receive variable len data */ | |
4039 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
4040 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
4041 | if (ret <= 0) { | |
4042 | DBG("No URIs received from client... continuing"); | |
4043 | *sock_error = 1; | |
4044 | ret = LTTNG_ERR_SESSION_FAIL; | |
4045 | free(uris); | |
4046 | goto error; | |
4047 | } | |
4048 | ||
4049 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
4050 | DBG("Creating session with ONE network URI is a bad call"); | |
4051 | ret = LTTNG_ERR_SESSION_FAIL; | |
4052 | free(uris); | |
4053 | goto error; | |
4054 | } | |
4055 | } | |
4056 | ||
4057 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, | |
4058 | nb_uri, &cmd_ctx->creds); | |
4059 | free(uris); | |
4060 | break; | |
4061 | } | |
ecc48a90 JD |
4062 | case LTTNG_CREATE_SESSION_LIVE: |
4063 | { | |
4064 | size_t nb_uri, len; | |
4065 | struct lttng_uri *uris = NULL; | |
4066 | ||
4067 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
4068 | len = nb_uri * sizeof(struct lttng_uri); | |
4069 | ||
4070 | if (nb_uri > 0) { | |
4071 | uris = zmalloc(len); | |
4072 | if (uris == NULL) { | |
4073 | ret = LTTNG_ERR_FATAL; | |
4074 | goto error; | |
4075 | } | |
4076 | ||
4077 | /* Receive variable len data */ | |
4078 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
4079 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
4080 | if (ret <= 0) { | |
4081 | DBG("No URIs received from client... continuing"); | |
4082 | *sock_error = 1; | |
4083 | ret = LTTNG_ERR_SESSION_FAIL; | |
4084 | free(uris); | |
4085 | goto error; | |
4086 | } | |
4087 | ||
4088 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
4089 | DBG("Creating session with ONE network URI is a bad call"); | |
4090 | ret = LTTNG_ERR_SESSION_FAIL; | |
4091 | free(uris); | |
4092 | goto error; | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, | |
4097 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); | |
4098 | free(uris); | |
4099 | break; | |
4100 | } | |
fb198a11 JG |
4101 | case LTTNG_SAVE_SESSION: |
4102 | { | |
4103 | ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr, | |
4104 | &cmd_ctx->creds); | |
4105 | break; | |
4106 | } | |
d7ba1388 MD |
4107 | case LTTNG_SET_SESSION_SHM_PATH: |
4108 | { | |
4109 | ret = cmd_set_session_shm_path(cmd_ctx->session, | |
4110 | cmd_ctx->lsm->u.set_shm_path.shm_path); | |
4111 | break; | |
4112 | } | |
eded6438 | 4113 | case LTTNG_REGENERATE_METADATA: |
93ec662e | 4114 | { |
eded6438 | 4115 | ret = cmd_regenerate_metadata(cmd_ctx->session); |
93ec662e JD |
4116 | break; |
4117 | } | |
c2561365 JD |
4118 | case LTTNG_REGENERATE_STATEDUMP: |
4119 | { | |
4120 | ret = cmd_regenerate_statedump(cmd_ctx->session); | |
4121 | break; | |
4122 | } | |
5e16da05 | 4123 | default: |
f73fabfd | 4124 | ret = LTTNG_ERR_UND; |
5461b305 | 4125 | break; |
fac6795d DG |
4126 | } |
4127 | ||
5461b305 | 4128 | error: |
5461b305 DG |
4129 | if (cmd_ctx->llm == NULL) { |
4130 | DBG("Missing llm structure. Allocating one."); | |
6e10c9b9 | 4131 | if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) { |
5461b305 DG |
4132 | goto setup_error; |
4133 | } | |
4134 | } | |
54d01ffb | 4135 | /* Set return code */ |
5461b305 | 4136 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 4137 | setup_error: |
b5541356 | 4138 | if (cmd_ctx->session) { |
54d01ffb | 4139 | session_unlock(cmd_ctx->session); |
b5541356 | 4140 | } |
256a5576 MD |
4141 | if (need_tracing_session) { |
4142 | session_unlock_list(); | |
4143 | } | |
54d01ffb | 4144 | init_setup_error: |
3745d315 | 4145 | assert(!rcu_read_ongoing()); |
8028d920 | 4146 | return ret; |
fac6795d DG |
4147 | } |
4148 | ||
44a5e5eb DG |
4149 | /* |
4150 | * Thread managing health check socket. | |
4151 | */ | |
4152 | static void *thread_manage_health(void *data) | |
4153 | { | |
eb4a2943 | 4154 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
4155 | uint32_t revents, nb_fd; |
4156 | struct lttng_poll_event events; | |
0c89d795 MD |
4157 | struct health_comm_msg msg; |
4158 | struct health_comm_reply reply; | |
44a5e5eb DG |
4159 | |
4160 | DBG("[thread] Manage health check started"); | |
4161 | ||
4162 | rcu_register_thread(); | |
4163 | ||
6d737ce4 DG |
4164 | /* We might hit an error path before this is created. */ |
4165 | lttng_poll_init(&events); | |
3cc04881 | 4166 | |
44a5e5eb DG |
4167 | /* Create unix socket */ |
4168 | sock = lttcomm_create_unix_sock(health_unix_sock_path); | |
4169 | if (sock < 0) { | |
4170 | ERR("Unable to create health check Unix socket"); | |
44a5e5eb DG |
4171 | goto error; |
4172 | } | |
4173 | ||
6c71277b MD |
4174 | if (is_root) { |
4175 | /* lttng health client socket path permissions */ | |
4176 | ret = chown(health_unix_sock_path, 0, | |
4177 | utils_get_group_id(tracing_group_name)); | |
4178 | if (ret < 0) { | |
4179 | ERR("Unable to set group on %s", health_unix_sock_path); | |
4180 | PERROR("chown"); | |
6c71277b MD |
4181 | goto error; |
4182 | } | |
4183 | ||
4184 | ret = chmod(health_unix_sock_path, | |
4185 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
4186 | if (ret < 0) { | |
4187 | ERR("Unable to set permissions on %s", health_unix_sock_path); | |
4188 | PERROR("chmod"); | |
6c71277b MD |
4189 | goto error; |
4190 | } | |
4191 | } | |
4192 | ||
b662582b DG |
4193 | /* |
4194 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4195 | * show must go on. | |
4196 | */ | |
4197 | (void) utils_set_fd_cloexec(sock); | |
4198 | ||
44a5e5eb DG |
4199 | ret = lttcomm_listen_unix_sock(sock); |
4200 | if (ret < 0) { | |
4201 | goto error; | |
4202 | } | |
4203 | ||
4204 | /* | |
4205 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4206 | * more will be added to this poll set. | |
4207 | */ | |
d0b96690 | 4208 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
4209 | if (ret < 0) { |
4210 | goto error; | |
4211 | } | |
4212 | ||
4213 | /* Add the application registration socket */ | |
4214 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
4215 | if (ret < 0) { | |
4216 | goto error; | |
4217 | } | |
4218 | ||
ef367a93 | 4219 | sessiond_notify_ready(); |
97bc1426 | 4220 | |
44a5e5eb DG |
4221 | while (1) { |
4222 | DBG("Health check ready"); | |
4223 | ||
44a5e5eb DG |
4224 | /* Inifinite blocking call, waiting for transmission */ |
4225 | restart: | |
4226 | ret = lttng_poll_wait(&events, -1); | |
4227 | if (ret < 0) { | |
4228 | /* | |
4229 | * Restart interrupted system call. | |
4230 | */ | |
4231 | if (errno == EINTR) { | |
4232 | goto restart; | |
4233 | } | |
4234 | goto error; | |
4235 | } | |
4236 | ||
0d9c5d77 DG |
4237 | nb_fd = ret; |
4238 | ||
44a5e5eb DG |
4239 | for (i = 0; i < nb_fd; i++) { |
4240 | /* Fetch once the poll data */ | |
4241 | revents = LTTNG_POLL_GETEV(&events, i); | |
4242 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4243 | ||
fd20dac9 MD |
4244 | if (!revents) { |
4245 | /* No activity for this FD (poll implementation). */ | |
4246 | continue; | |
4247 | } | |
4248 | ||
44a5e5eb | 4249 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4250 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 4251 | if (ret) { |
139ac872 MD |
4252 | err = 0; |
4253 | goto exit; | |
44a5e5eb DG |
4254 | } |
4255 | ||
4256 | /* Event on the registration socket */ | |
4257 | if (pollfd == sock) { | |
03e43155 MD |
4258 | if (revents & LPOLLIN) { |
4259 | continue; | |
4260 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
44a5e5eb DG |
4261 | ERR("Health socket poll error"); |
4262 | goto error; | |
03e43155 MD |
4263 | } else { |
4264 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4265 | goto error; | |
44a5e5eb DG |
4266 | } |
4267 | } | |
4268 | } | |
4269 | ||
4270 | new_sock = lttcomm_accept_unix_sock(sock); | |
4271 | if (new_sock < 0) { | |
4272 | goto error; | |
4273 | } | |
4274 | ||
b662582b DG |
4275 | /* |
4276 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4277 | * show must go on. | |
4278 | */ | |
4279 | (void) utils_set_fd_cloexec(new_sock); | |
4280 | ||
44a5e5eb DG |
4281 | DBG("Receiving data from client for health..."); |
4282 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
4283 | if (ret <= 0) { | |
4284 | DBG("Nothing recv() from client... continuing"); | |
4285 | ret = close(new_sock); | |
4286 | if (ret) { | |
4287 | PERROR("close"); | |
4288 | } | |
44a5e5eb DG |
4289 | continue; |
4290 | } | |
4291 | ||
4292 | rcu_thread_online(); | |
4293 | ||
53efb85a | 4294 | memset(&reply, 0, sizeof(reply)); |
6c71277b MD |
4295 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { |
4296 | /* | |
4297 | * health_check_state returns 0 if health is | |
4298 | * bad. | |
4299 | */ | |
4300 | if (!health_check_state(health_sessiond, i)) { | |
4301 | reply.ret_code |= 1ULL << i; | |
4302 | } | |
44a5e5eb DG |
4303 | } |
4304 | ||
6c71277b | 4305 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
44a5e5eb DG |
4306 | |
4307 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
4308 | if (ret < 0) { | |
4309 | ERR("Failed to send health data back to client"); | |
4310 | } | |
4311 | ||
4312 | /* End of transmission */ | |
4313 | ret = close(new_sock); | |
4314 | if (ret) { | |
4315 | PERROR("close"); | |
4316 | } | |
44a5e5eb DG |
4317 | } |
4318 | ||
139ac872 | 4319 | exit: |
44a5e5eb | 4320 | error: |
139ac872 MD |
4321 | if (err) { |
4322 | ERR("Health error occurred in %s", __func__); | |
4323 | } | |
44a5e5eb DG |
4324 | DBG("Health check thread dying"); |
4325 | unlink(health_unix_sock_path); | |
4326 | if (sock >= 0) { | |
4327 | ret = close(sock); | |
4328 | if (ret) { | |
4329 | PERROR("close"); | |
4330 | } | |
4331 | } | |
44a5e5eb DG |
4332 | |
4333 | lttng_poll_clean(&events); | |
28538a70 | 4334 | stop_threads(); |
44a5e5eb DG |
4335 | rcu_unregister_thread(); |
4336 | return NULL; | |
4337 | } | |
4338 | ||
1d4b027a | 4339 | /* |
d063d709 DG |
4340 | * This thread manage all clients request using the unix client socket for |
4341 | * communication. | |
1d4b027a DG |
4342 | */ |
4343 | static void *thread_manage_clients(void *data) | |
4344 | { | |
139ac872 | 4345 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 4346 | int sock_error; |
5eb91c98 | 4347 | uint32_t revents, nb_fd; |
273ea72c | 4348 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 4349 | struct lttng_poll_event events; |
1d4b027a DG |
4350 | |
4351 | DBG("[thread] Manage client started"); | |
4352 | ||
f6a9efaa DG |
4353 | rcu_register_thread(); |
4354 | ||
6c71277b | 4355 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
927ca06a | 4356 | |
840cb59c | 4357 | health_code_update(); |
44a5e5eb | 4358 | |
1d4b027a DG |
4359 | ret = lttcomm_listen_unix_sock(client_sock); |
4360 | if (ret < 0) { | |
35df2755 | 4361 | goto error_listen; |
1d4b027a DG |
4362 | } |
4363 | ||
5eb91c98 DG |
4364 | /* |
4365 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4366 | * more will be added to this poll set. | |
4367 | */ | |
d0b96690 | 4368 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 4369 | if (ret < 0) { |
35df2755 | 4370 | goto error_create_poll; |
5eb91c98 | 4371 | } |
273ea72c | 4372 | |
5eb91c98 DG |
4373 | /* Add the application registration socket */ |
4374 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
4375 | if (ret < 0) { | |
4376 | goto error; | |
4377 | } | |
273ea72c | 4378 | |
ef367a93 | 4379 | sessiond_notify_ready(); |
09a88287 | 4380 | |
ef367a93 JG |
4381 | ret = sem_post(&load_info->message_thread_ready); |
4382 | if (ret) { | |
4383 | PERROR("sem_post message_thread_ready"); | |
4384 | goto error; | |
4385 | } | |
0bb7724a | 4386 | |
09a88287 JG |
4387 | /* |
4388 | * Wait until all support threads are initialized before accepting | |
4389 | * commands. | |
4390 | */ | |
4391 | while (uatomic_read(<tng_sessiond_ready) != 0) { | |
4392 | fd_set read_fds; | |
4393 | struct timeval timeout; | |
4394 | ||
4395 | FD_ZERO(&read_fds); | |
4396 | FD_SET(thread_quit_pipe[0], &read_fds); | |
4397 | memset(&timeout, 0, sizeof(timeout)); | |
4398 | timeout.tv_usec = 1000; | |
4399 | ||
4400 | /* | |
4401 | * If a support thread failed to launch, it may signal that | |
4402 | * we must exit and the sessiond would never be marked as | |
4403 | * "ready". | |
4404 | * | |
4405 | * The timeout is set to 1ms, which serves as a way to | |
4406 | * pace down this check. | |
4407 | */ | |
4408 | ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL, | |
4409 | &timeout); | |
4410 | if (ret > 0 || (ret < 0 && errno != EINTR)) { | |
4411 | goto exit; | |
4412 | } | |
4413 | } | |
4414 | ||
72453c8c MD |
4415 | /* This testpoint is after we signal readiness to the parent. */ |
4416 | if (testpoint(sessiond_thread_manage_clients)) { | |
4417 | goto error; | |
4418 | } | |
4419 | ||
e547b070 | 4420 | if (testpoint(sessiond_thread_manage_clients_before_loop)) { |
6993eeb3 CB |
4421 | goto error; |
4422 | } | |
8ac94142 | 4423 | |
840cb59c | 4424 | health_code_update(); |
44a5e5eb | 4425 | |
1d4b027a | 4426 | while (1) { |
1d4b027a | 4427 | DBG("Accepting client command ..."); |
273ea72c DG |
4428 | |
4429 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 4430 | restart: |
a78af745 | 4431 | health_poll_entry(); |
5eb91c98 | 4432 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 4433 | health_poll_exit(); |
273ea72c | 4434 | if (ret < 0) { |
88f2b785 MD |
4435 | /* |
4436 | * Restart interrupted system call. | |
4437 | */ | |
4438 | if (errno == EINTR) { | |
4439 | goto restart; | |
4440 | } | |
273ea72c DG |
4441 | goto error; |
4442 | } | |
4443 | ||
0d9c5d77 DG |
4444 | nb_fd = ret; |
4445 | ||
5eb91c98 DG |
4446 | for (i = 0; i < nb_fd; i++) { |
4447 | /* Fetch once the poll data */ | |
4448 | revents = LTTNG_POLL_GETEV(&events, i); | |
4449 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4450 | ||
840cb59c | 4451 | health_code_update(); |
44a5e5eb | 4452 | |
fd20dac9 MD |
4453 | if (!revents) { |
4454 | /* No activity for this FD (poll implementation). */ | |
4455 | continue; | |
4456 | } | |
4457 | ||
5eb91c98 | 4458 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4459 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 4460 | if (ret) { |
139ac872 MD |
4461 | err = 0; |
4462 | goto exit; | |
5eb91c98 DG |
4463 | } |
4464 | ||
4465 | /* Event on the registration socket */ | |
4466 | if (pollfd == client_sock) { | |
03e43155 MD |
4467 | if (revents & LPOLLIN) { |
4468 | continue; | |
4469 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
5eb91c98 DG |
4470 | ERR("Client socket poll error"); |
4471 | goto error; | |
03e43155 MD |
4472 | } else { |
4473 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4474 | goto error; | |
5eb91c98 DG |
4475 | } |
4476 | } | |
4477 | } | |
4478 | ||
4479 | DBG("Wait for client response"); | |
4480 | ||
840cb59c | 4481 | health_code_update(); |
44a5e5eb | 4482 | |
5eb91c98 DG |
4483 | sock = lttcomm_accept_unix_sock(client_sock); |
4484 | if (sock < 0) { | |
273ea72c | 4485 | goto error; |
273ea72c DG |
4486 | } |
4487 | ||
b662582b DG |
4488 | /* |
4489 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4490 | * show must go on. | |
4491 | */ | |
4492 | (void) utils_set_fd_cloexec(sock); | |
4493 | ||
be040666 DG |
4494 | /* Set socket option for credentials retrieval */ |
4495 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
4496 | if (ret < 0) { | |
4497 | goto error; | |
4498 | } | |
4499 | ||
5eb91c98 | 4500 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 4501 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 4502 | if (cmd_ctx == NULL) { |
76d7553f | 4503 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 4504 | goto error; |
5eb91c98 | 4505 | } |
1d4b027a | 4506 | |
5eb91c98 | 4507 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 4508 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 4509 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 4510 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
4511 | goto error; |
4512 | } | |
1d4b027a | 4513 | |
5eb91c98 DG |
4514 | cmd_ctx->llm = NULL; |
4515 | cmd_ctx->session = NULL; | |
1d4b027a | 4516 | |
840cb59c | 4517 | health_code_update(); |
44a5e5eb | 4518 | |
5eb91c98 DG |
4519 | /* |
4520 | * Data is received from the lttng client. The struct | |
4521 | * lttcomm_session_msg (lsm) contains the command and data request of | |
4522 | * the client. | |
4523 | */ | |
4524 | DBG("Receiving data from client ..."); | |
be040666 DG |
4525 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
4526 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
4527 | if (ret <= 0) { |
4528 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
4529 | ret = close(sock); |
4530 | if (ret) { | |
4531 | PERROR("close"); | |
4532 | } | |
4533 | sock = -1; | |
a2c0da86 | 4534 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4535 | continue; |
4536 | } | |
1d4b027a | 4537 | |
840cb59c | 4538 | health_code_update(); |
44a5e5eb | 4539 | |
5eb91c98 DG |
4540 | // TODO: Validate cmd_ctx including sanity check for |
4541 | // security purpose. | |
f7776ea7 | 4542 | |
f6a9efaa | 4543 | rcu_thread_online(); |
5eb91c98 DG |
4544 | /* |
4545 | * This function dispatch the work to the kernel or userspace tracer | |
4546 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
4547 | * informations for the client. The command context struct contains | |
4548 | * everything this function may needs. | |
4549 | */ | |
53a80697 | 4550 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 4551 | rcu_thread_offline(); |
5eb91c98 | 4552 | if (ret < 0) { |
36134aa1 DG |
4553 | ret = close(sock); |
4554 | if (ret) { | |
4555 | PERROR("close"); | |
53a80697 | 4556 | } |
36134aa1 | 4557 | sock = -1; |
bbd973c2 | 4558 | /* |
5eb91c98 | 4559 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 4560 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
4561 | * (ENOMEM). Error detected but still accept |
4562 | * command, unless a socket error has been | |
4563 | * detected. | |
bbd973c2 | 4564 | */ |
bbd973c2 | 4565 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4566 | continue; |
4567 | } | |
d6e4fca4 | 4568 | |
840cb59c | 4569 | health_code_update(); |
44a5e5eb | 4570 | |
a1ac8b97 | 4571 | DBG("Sending response (size: %d, retcode: %s (%d))", |
54d01ffb | 4572 | cmd_ctx->lttng_msg_size, |
a1ac8b97 JG |
4573 | lttng_strerror(-cmd_ctx->llm->ret_code), |
4574 | cmd_ctx->llm->ret_code); | |
54d01ffb | 4575 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
4576 | if (ret < 0) { |
4577 | ERR("Failed to send data back to client"); | |
bbd973c2 | 4578 | } |
1d4b027a | 4579 | |
5eb91c98 | 4580 | /* End of transmission */ |
76d7553f MD |
4581 | ret = close(sock); |
4582 | if (ret) { | |
4583 | PERROR("close"); | |
4584 | } | |
4585 | sock = -1; | |
be040666 DG |
4586 | |
4587 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 4588 | |
840cb59c | 4589 | health_code_update(); |
273ea72c DG |
4590 | } |
4591 | ||
139ac872 | 4592 | exit: |
5eb91c98 | 4593 | error: |
35df2755 CB |
4594 | if (sock >= 0) { |
4595 | ret = close(sock); | |
4596 | if (ret) { | |
4597 | PERROR("close"); | |
4598 | } | |
139ac872 | 4599 | } |
44a5e5eb | 4600 | |
35df2755 CB |
4601 | lttng_poll_clean(&events); |
4602 | clean_command_ctx(&cmd_ctx); | |
4603 | ||
4604 | error_listen: | |
4605 | error_create_poll: | |
273ea72c | 4606 | unlink(client_unix_sock_path); |
76d7553f MD |
4607 | if (client_sock >= 0) { |
4608 | ret = close(client_sock); | |
4609 | if (ret) { | |
4610 | PERROR("close"); | |
4611 | } | |
a4b35e07 | 4612 | } |
35df2755 CB |
4613 | |
4614 | if (err) { | |
840cb59c | 4615 | health_error(); |
35df2755 | 4616 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 4617 | } |
273ea72c | 4618 | |
8782cc74 | 4619 | health_unregister(health_sessiond); |
35df2755 CB |
4620 | |
4621 | DBG("Client thread dying"); | |
f6a9efaa DG |
4622 | |
4623 | rcu_unregister_thread(); | |
4a15001e MD |
4624 | |
4625 | /* | |
4626 | * Since we are creating the consumer threads, we own them, so we need | |
4627 | * to join them before our thread exits. | |
4628 | */ | |
4629 | ret = join_consumer_thread(&kconsumer_data); | |
4630 | if (ret) { | |
4631 | errno = ret; | |
4632 | PERROR("join_consumer"); | |
4633 | } | |
4634 | ||
4635 | ret = join_consumer_thread(&ustconsumer32_data); | |
4636 | if (ret) { | |
4637 | errno = ret; | |
4638 | PERROR("join_consumer ust32"); | |
4639 | } | |
4640 | ||
4641 | ret = join_consumer_thread(&ustconsumer64_data); | |
4642 | if (ret) { | |
4643 | errno = ret; | |
4644 | PERROR("join_consumer ust64"); | |
4645 | } | |
1d4b027a DG |
4646 | return NULL; |
4647 | } | |
4648 | ||
c0232ea5 JG |
4649 | static int string_match(const char *str1, const char *str2) |
4650 | { | |
4651 | return (str1 && str2) && !strcmp(str1, str2); | |
4652 | } | |
4653 | ||
fac6795d | 4654 | /* |
26296c48 JG |
4655 | * Take an option from the getopt output and set it in the right variable to be |
4656 | * used later. | |
4657 | * | |
4658 | * Return 0 on success else a negative value. | |
fac6795d | 4659 | */ |
26296c48 | 4660 | static int set_option(int opt, const char *arg, const char *optname) |
fac6795d | 4661 | { |
26296c48 | 4662 | int ret = 0; |
fac6795d | 4663 | |
c0232ea5 | 4664 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
4665 | if (!arg || *arg == '\0') { |
4666 | ret = -EINVAL; | |
4667 | goto end; | |
4668 | } | |
e8fa9fb0 MD |
4669 | if (lttng_is_setuid_setgid()) { |
4670 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4671 | "-c, --client-sock"); | |
4672 | } else { | |
4673 | snprintf(client_unix_sock_path, PATH_MAX, "%s", arg); | |
4674 | } | |
c0232ea5 | 4675 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
4676 | if (!arg || *arg == '\0') { |
4677 | ret = -EINVAL; | |
4678 | goto end; | |
4679 | } | |
e8fa9fb0 MD |
4680 | if (lttng_is_setuid_setgid()) { |
4681 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4682 | "-a, --apps-sock"); | |
4683 | } else { | |
4684 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg); | |
4685 | } | |
c0232ea5 | 4686 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
26296c48 | 4687 | opt_daemon = 1; |
c0232ea5 | 4688 | } else if (string_match(optname, "background") || opt == 'b') { |
72dd7491 | 4689 | opt_background = 1; |
c0232ea5 | 4690 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
4691 | if (!arg || *arg == '\0') { |
4692 | ret = -EINVAL; | |
4693 | goto end; | |
4694 | } | |
e8fa9fb0 MD |
4695 | if (lttng_is_setuid_setgid()) { |
4696 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4697 | "-g, --group"); | |
4698 | } else { | |
4699 | /* | |
4700 | * If the override option is set, the pointer points to a | |
4701 | * *non* const thus freeing it even though the variable type is | |
4702 | * set to const. | |
4703 | */ | |
4704 | if (tracing_group_name_override) { | |
4705 | free((void *) tracing_group_name); | |
4706 | } | |
4707 | tracing_group_name = strdup(arg); | |
4708 | if (!tracing_group_name) { | |
4709 | PERROR("strdup"); | |
4710 | ret = -ENOMEM; | |
4711 | } | |
4712 | tracing_group_name_override = 1; | |
db322c4d | 4713 | } |
c0232ea5 | 4714 | } else if (string_match(optname, "help") || opt == 'h') { |
8190767e PP |
4715 | ret = utils_show_man_page(8, "lttng-sessiond"); |
4716 | if (ret) { | |
4717 | ERR("Cannot view man page lttng-sessiond(8)"); | |
4718 | perror("exec"); | |
4719 | } | |
4720 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 4721 | } else if (string_match(optname, "version") || opt == 'V') { |
cdd566a2 | 4722 | opt_print_version = 1; |
c0232ea5 | 4723 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
26296c48 | 4724 | opt_sig_parent = 1; |
c0232ea5 | 4725 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
4726 | if (!arg || *arg == '\0') { |
4727 | ret = -EINVAL; | |
4728 | goto end; | |
4729 | } | |
e8fa9fb0 MD |
4730 | if (lttng_is_setuid_setgid()) { |
4731 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4732 | "--kconsumerd-err-sock"); | |
4733 | } else { | |
4734 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg); | |
4735 | } | |
c0232ea5 | 4736 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
4737 | if (!arg || *arg == '\0') { |
4738 | ret = -EINVAL; | |
4739 | goto end; | |
4740 | } | |
e8fa9fb0 MD |
4741 | if (lttng_is_setuid_setgid()) { |
4742 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4743 | "--kconsumerd-cmd-sock"); | |
4744 | } else { | |
4745 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); | |
4746 | } | |
c0232ea5 | 4747 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
4748 | if (!arg || *arg == '\0') { |
4749 | ret = -EINVAL; | |
4750 | goto end; | |
4751 | } | |
e8fa9fb0 MD |
4752 | if (lttng_is_setuid_setgid()) { |
4753 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4754 | "--ustconsumerd64-err-sock"); | |
4755 | } else { | |
4756 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg); | |
4757 | } | |
c0232ea5 | 4758 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
4759 | if (!arg || *arg == '\0') { |
4760 | ret = -EINVAL; | |
4761 | goto end; | |
4762 | } | |
e8fa9fb0 MD |
4763 | if (lttng_is_setuid_setgid()) { |
4764 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4765 | "--ustconsumerd64-cmd-sock"); | |
4766 | } else { | |
4767 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); | |
4768 | } | |
c0232ea5 | 4769 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
4770 | if (!arg || *arg == '\0') { |
4771 | ret = -EINVAL; | |
4772 | goto end; | |
4773 | } | |
e8fa9fb0 MD |
4774 | if (lttng_is_setuid_setgid()) { |
4775 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4776 | "--ustconsumerd32-err-sock"); | |
4777 | } else { | |
4778 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg); | |
4779 | } | |
c0232ea5 | 4780 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
4781 | if (!arg || *arg == '\0') { |
4782 | ret = -EINVAL; | |
4783 | goto end; | |
4784 | } | |
e8fa9fb0 MD |
4785 | if (lttng_is_setuid_setgid()) { |
4786 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4787 | "--ustconsumerd32-cmd-sock"); | |
4788 | } else { | |
4789 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg); | |
4790 | } | |
c0232ea5 | 4791 | } else if (string_match(optname, "no-kernel")) { |
26296c48 | 4792 | opt_no_kernel = 1; |
c0232ea5 | 4793 | } else if (string_match(optname, "quiet") || opt == 'q') { |
26296c48 | 4794 | lttng_opt_quiet = 1; |
c0232ea5 | 4795 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
4796 | /* Verbose level can increase using multiple -v */ |
4797 | if (arg) { | |
13755a18 | 4798 | /* Value obtained from config file */ |
26296c48 JG |
4799 | lttng_opt_verbose = config_parse_value(arg); |
4800 | } else { | |
13755a18 JG |
4801 | /* -v used on command line */ |
4802 | lttng_opt_verbose++; | |
26296c48 | 4803 | } |
13755a18 JG |
4804 | /* Clamp value to [0, 3] */ |
4805 | lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 : | |
4806 | (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3); | |
c0232ea5 | 4807 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 JG |
4808 | if (arg) { |
4809 | opt_verbose_consumer = config_parse_value(arg); | |
4810 | } else { | |
ca939de8 | 4811 | opt_verbose_consumer++; |
26296c48 | 4812 | } |
c0232ea5 | 4813 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
4814 | if (!arg || *arg == '\0') { |
4815 | ret = -EINVAL; | |
4816 | goto end; | |
4817 | } | |
e8fa9fb0 MD |
4818 | if (lttng_is_setuid_setgid()) { |
4819 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4820 | "--consumerd32-path"); | |
4821 | } else { | |
4822 | if (consumerd32_bin_override) { | |
4823 | free((void *) consumerd32_bin); | |
4824 | } | |
4825 | consumerd32_bin = strdup(arg); | |
4826 | if (!consumerd32_bin) { | |
4827 | PERROR("strdup"); | |
4828 | ret = -ENOMEM; | |
4829 | } | |
4830 | consumerd32_bin_override = 1; | |
db322c4d | 4831 | } |
c0232ea5 | 4832 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
4833 | if (!arg || *arg == '\0') { |
4834 | ret = -EINVAL; | |
4835 | goto end; | |
4836 | } | |
e8fa9fb0 MD |
4837 | if (lttng_is_setuid_setgid()) { |
4838 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4839 | "--consumerd32-libdir"); | |
4840 | } else { | |
4841 | if (consumerd32_libdir_override) { | |
4842 | free((void *) consumerd32_libdir); | |
4843 | } | |
4844 | consumerd32_libdir = strdup(arg); | |
4845 | if (!consumerd32_libdir) { | |
4846 | PERROR("strdup"); | |
4847 | ret = -ENOMEM; | |
4848 | } | |
4849 | consumerd32_libdir_override = 1; | |
db322c4d | 4850 | } |
c0232ea5 | 4851 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
4852 | if (!arg || *arg == '\0') { |
4853 | ret = -EINVAL; | |
4854 | goto end; | |
4855 | } | |
e8fa9fb0 MD |
4856 | if (lttng_is_setuid_setgid()) { |
4857 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4858 | "--consumerd64-path"); | |
4859 | } else { | |
4860 | if (consumerd64_bin_override) { | |
4861 | free((void *) consumerd64_bin); | |
4862 | } | |
4863 | consumerd64_bin = strdup(arg); | |
4864 | if (!consumerd64_bin) { | |
4865 | PERROR("strdup"); | |
4866 | ret = -ENOMEM; | |
4867 | } | |
4868 | consumerd64_bin_override = 1; | |
db322c4d | 4869 | } |
c0232ea5 | 4870 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
4871 | if (!arg || *arg == '\0') { |
4872 | ret = -EINVAL; | |
4873 | goto end; | |
4874 | } | |
e8fa9fb0 MD |
4875 | if (lttng_is_setuid_setgid()) { |
4876 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4877 | "--consumerd64-libdir"); | |
4878 | } else { | |
4879 | if (consumerd64_libdir_override) { | |
4880 | free((void *) consumerd64_libdir); | |
4881 | } | |
4882 | consumerd64_libdir = strdup(arg); | |
4883 | if (!consumerd64_libdir) { | |
4884 | PERROR("strdup"); | |
4885 | ret = -ENOMEM; | |
4886 | } | |
4887 | consumerd64_libdir_override = 1; | |
db322c4d | 4888 | } |
c0232ea5 | 4889 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
4890 | if (!arg || *arg == '\0') { |
4891 | ret = -EINVAL; | |
4892 | goto end; | |
4893 | } | |
e8fa9fb0 MD |
4894 | if (lttng_is_setuid_setgid()) { |
4895 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4896 | "-p, --pidfile"); | |
4897 | } else { | |
4898 | free(opt_pidfile); | |
4899 | opt_pidfile = strdup(arg); | |
4900 | if (!opt_pidfile) { | |
4901 | PERROR("strdup"); | |
4902 | ret = -ENOMEM; | |
4903 | } | |
db322c4d | 4904 | } |
c0232ea5 | 4905 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
4906 | if (!arg || *arg == '\0') { |
4907 | ret = -EINVAL; | |
4908 | goto end; | |
4909 | } | |
e8fa9fb0 MD |
4910 | if (lttng_is_setuid_setgid()) { |
4911 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4912 | "--agent-tcp-port"); | |
4913 | } else { | |
4914 | unsigned long v; | |
4d076222 | 4915 | |
e8fa9fb0 MD |
4916 | errno = 0; |
4917 | v = strtoul(arg, NULL, 0); | |
4918 | if (errno != 0 || !isdigit(arg[0])) { | |
4919 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
4920 | return -1; | |
4921 | } | |
4922 | if (v == 0 || v >= 65535) { | |
4923 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
4924 | return -1; | |
4925 | } | |
4926 | agent_tcp_port = (uint32_t) v; | |
4927 | DBG3("Agent TCP port set to non default: %u", agent_tcp_port); | |
26296c48 | 4928 | } |
c0232ea5 | 4929 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
4930 | if (!arg || *arg == '\0') { |
4931 | ret = -EINVAL; | |
4932 | goto end; | |
4933 | } | |
e8fa9fb0 MD |
4934 | if (lttng_is_setuid_setgid()) { |
4935 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4936 | "-l, --load"); | |
4937 | } else { | |
4938 | free(opt_load_session_path); | |
4939 | opt_load_session_path = strdup(arg); | |
4940 | if (!opt_load_session_path) { | |
4941 | PERROR("strdup"); | |
4942 | ret = -ENOMEM; | |
4943 | } | |
ef367a93 | 4944 | } |
c0232ea5 | 4945 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
4946 | if (!arg || *arg == '\0') { |
4947 | ret = -EINVAL; | |
4948 | goto end; | |
4949 | } | |
e8fa9fb0 MD |
4950 | if (lttng_is_setuid_setgid()) { |
4951 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4952 | "--kmod-probes"); | |
4953 | } else { | |
4954 | free(kmod_probes_list); | |
4955 | kmod_probes_list = strdup(arg); | |
4956 | if (!kmod_probes_list) { | |
4957 | PERROR("strdup"); | |
4958 | ret = -ENOMEM; | |
4959 | } | |
c9d42407 | 4960 | } |
c0232ea5 | 4961 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
4962 | if (!arg || *arg == '\0') { |
4963 | ret = -EINVAL; | |
4964 | goto end; | |
4965 | } | |
e8fa9fb0 MD |
4966 | if (lttng_is_setuid_setgid()) { |
4967 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4968 | "--extra-kmod-probes"); | |
4969 | } else { | |
4970 | free(kmod_extra_probes_list); | |
4971 | kmod_extra_probes_list = strdup(arg); | |
4972 | if (!kmod_extra_probes_list) { | |
4973 | PERROR("strdup"); | |
4974 | ret = -ENOMEM; | |
4975 | } | |
db322c4d | 4976 | } |
c0232ea5 JG |
4977 | } else if (string_match(optname, "config") || opt == 'f') { |
4978 | /* This is handled in set_options() thus silent skip. */ | |
4979 | goto end; | |
4980 | } else { | |
26296c48 JG |
4981 | /* Unknown option or other error. |
4982 | * Error is printed by getopt, just return */ | |
4983 | ret = -1; | |
4984 | } | |
4985 | ||
f3dd7ce7 | 4986 | end: |
c5d350b2 JG |
4987 | if (ret == -EINVAL) { |
4988 | const char *opt_name = "unknown"; | |
4989 | int i; | |
4990 | ||
4991 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
4992 | i++) { | |
4993 | if (opt == long_options[i].val) { | |
4994 | opt_name = long_options[i].name; | |
4995 | break; | |
4996 | } | |
4997 | } | |
4998 | ||
4999 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
5000 | opt_name); | |
5001 | } | |
f3dd7ce7 | 5002 | |
26296c48 JG |
5003 | return ret; |
5004 | } | |
5005 | ||
5006 | /* | |
5007 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 5008 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
5009 | * return value conventions. |
5010 | */ | |
5011 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
5012 | { | |
5013 | int ret = 0, i; | |
5014 | ||
5015 | if (!entry || !entry->name || !entry->value) { | |
5016 | ret = -EINVAL; | |
5017 | goto end; | |
5018 | } | |
5019 | ||
5020 | /* Check if the option is to be ignored */ | |
5021 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
5022 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
5023 | goto end; | |
5024 | } | |
5025 | } | |
5026 | ||
5027 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
5028 | i++) { | |
5029 | ||
5030 | /* Ignore if not fully matched. */ | |
5031 | if (strcmp(entry->name, long_options[i].name)) { | |
5032 | continue; | |
5033 | } | |
5034 | ||
5035 | /* | |
5036 | * If the option takes no argument on the command line, we have to | |
5037 | * check if the value is "true". We support non-zero numeric values, | |
5038 | * true, on and yes. | |
5039 | */ | |
5040 | if (!long_options[i].has_arg) { | |
5041 | ret = config_parse_value(entry->value); | |
5042 | if (ret <= 0) { | |
5043 | if (ret) { | |
5044 | WARN("Invalid configuration value \"%s\" for option %s", | |
5045 | entry->value, entry->name); | |
5046 | } | |
5047 | /* False, skip boolean config option. */ | |
5048 | goto end; | |
4d076222 | 5049 | } |
26296c48 JG |
5050 | } |
5051 | ||
5052 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
5053 | goto end; | |
5054 | } | |
5055 | ||
5056 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
5057 | ||
5058 | end: | |
5059 | return ret; | |
5060 | } | |
5061 | ||
cdd566a2 JR |
5062 | static void sessiond_config_log(void) |
5063 | { | |
5064 | DBG("LTTng-sessiond " VERSION " - " VERSION_NAME "%s%s", | |
5065 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, | |
5066 | EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); | |
5067 | if (EXTRA_VERSION_DESCRIPTION[0] != '\0') { | |
5068 | DBG("LTTng-sessiond extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n"); | |
5069 | } | |
b8b6fadb JR |
5070 | if (EXTRA_VERSION_PATCHES[0] != '\0') { |
5071 | DBG("LTTng-sessiond extra patches:\n\t" EXTRA_VERSION_PATCHES "\n"); | |
5072 | } | |
cdd566a2 JR |
5073 | } |
5074 | ||
5075 | static void print_version(void) { | |
5076 | fprintf(stdout, "%s\n", VERSION); | |
5077 | } | |
5078 | ||
26296c48 JG |
5079 | /* |
5080 | * daemon configuration loading and argument parsing | |
5081 | */ | |
5082 | static int set_options(int argc, char **argv) | |
5083 | { | |
5084 | int ret = 0, c = 0, option_index = 0; | |
5085 | int orig_optopt = optopt, orig_optind = optind; | |
5086 | char *optstring; | |
5087 | const char *config_path = NULL; | |
5088 | ||
5089 | optstring = utils_generate_optstring(long_options, | |
5090 | sizeof(long_options) / sizeof(struct option)); | |
5091 | if (!optstring) { | |
5092 | ret = -ENOMEM; | |
5093 | goto end; | |
5094 | } | |
5095 | ||
5096 | /* Check for the --config option */ | |
5097 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
5098 | &option_index)) != -1) { | |
5099 | if (c == '?') { | |
5100 | ret = -EINVAL; | |
5101 | goto end; | |
5102 | } else if (c != 'f') { | |
5103 | /* if not equal to --config option. */ | |
5104 | continue; | |
5105 | } | |
5106 | ||
e8fa9fb0 MD |
5107 | if (lttng_is_setuid_setgid()) { |
5108 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5109 | "-f, --config"); | |
5110 | } else { | |
5111 | config_path = utils_expand_path(optarg); | |
5112 | if (!config_path) { | |
5113 | ERR("Failed to resolve path: %s", optarg); | |
5114 | } | |
26296c48 JG |
5115 | } |
5116 | } | |
5117 | ||
5118 | ret = config_get_section_entries(config_path, config_section_name, | |
5119 | config_entry_handler, NULL); | |
5120 | if (ret) { | |
5121 | if (ret > 0) { | |
5122 | ERR("Invalid configuration option at line %i", ret); | |
5123 | ret = -1; | |
5124 | } | |
5125 | goto end; | |
5126 | } | |
5127 | ||
5128 | /* Reset getopt's global state */ | |
5129 | optopt = orig_optopt; | |
5130 | optind = orig_optind; | |
5131 | while (1) { | |
c0232ea5 JG |
5132 | option_index = -1; |
5133 | /* | |
5134 | * getopt_long() will not set option_index if it encounters a | |
5135 | * short option. | |
5136 | */ | |
5137 | c = getopt_long(argc, argv, optstring, long_options, | |
5138 | &option_index); | |
26296c48 | 5139 | if (c == -1) { |
4d076222 DG |
5140 | break; |
5141 | } | |
26296c48 | 5142 | |
c0232ea5 JG |
5143 | /* |
5144 | * Pass NULL as the long option name if popt left the index | |
5145 | * unset. | |
5146 | */ | |
5147 | ret = set_option(c, optarg, | |
5148 | option_index < 0 ? NULL : | |
5149 | long_options[option_index].name); | |
26296c48 JG |
5150 | if (ret < 0) { |
5151 | break; | |
fac6795d DG |
5152 | } |
5153 | } | |
5154 | ||
26296c48 JG |
5155 | end: |
5156 | free(optstring); | |
5157 | return ret; | |
fac6795d DG |
5158 | } |
5159 | ||
5160 | /* | |
d063d709 | 5161 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
5162 | * apps_sock - The communication socket for all UST apps. |
5163 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 5164 | */ |
cf3af59e | 5165 | static int init_daemon_socket(void) |
fac6795d DG |
5166 | { |
5167 | int ret = 0; | |
5168 | mode_t old_umask; | |
5169 | ||
5170 | old_umask = umask(0); | |
5171 | ||
5172 | /* Create client tool unix socket */ | |
d6f42150 DG |
5173 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
5174 | if (client_sock < 0) { | |
5175 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
5176 | ret = -1; |
5177 | goto end; | |
5178 | } | |
5179 | ||
b662582b DG |
5180 | /* Set the cloexec flag */ |
5181 | ret = utils_set_fd_cloexec(client_sock); | |
5182 | if (ret < 0) { | |
5183 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
5184 | "Continuing but note that the consumer daemon will have a " | |
5185 | "reference to this socket on exec()", client_sock); | |
5186 | } | |
5187 | ||
fac6795d DG |
5188 | /* File permission MUST be 660 */ |
5189 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
5190 | if (ret < 0) { | |
d6f42150 | 5191 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
76d7553f | 5192 | PERROR("chmod"); |
fac6795d DG |
5193 | goto end; |
5194 | } | |
5195 | ||
5196 | /* Create the application unix socket */ | |
d6f42150 DG |
5197 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
5198 | if (apps_sock < 0) { | |
5199 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
5200 | ret = -1; |
5201 | goto end; | |
5202 | } | |
5203 | ||
b662582b DG |
5204 | /* Set the cloexec flag */ |
5205 | ret = utils_set_fd_cloexec(apps_sock); | |
5206 | if (ret < 0) { | |
5207 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
5208 | "Continuing but note that the consumer daemon will have a " | |
5209 | "reference to this socket on exec()", apps_sock); | |
5210 | } | |
5211 | ||
d6f42150 | 5212 | /* File permission MUST be 666 */ |
54d01ffb DG |
5213 | ret = chmod(apps_unix_sock_path, |
5214 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 5215 | if (ret < 0) { |
d6f42150 | 5216 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
76d7553f | 5217 | PERROR("chmod"); |
fac6795d DG |
5218 | goto end; |
5219 | } | |
5220 | ||
b662582b DG |
5221 | DBG3("Session daemon client socket %d and application socket %d created", |
5222 | client_sock, apps_sock); | |
5223 | ||
fac6795d DG |
5224 | end: |
5225 | umask(old_umask); | |
5226 | return ret; | |
5227 | } | |
5228 | ||
5229 | /* | |
54d01ffb DG |
5230 | * Check if the global socket is available, and if a daemon is answering at the |
5231 | * other side. If yes, error is returned. | |
fac6795d | 5232 | */ |
cf3af59e | 5233 | static int check_existing_daemon(void) |
fac6795d | 5234 | { |
7d8234d9 | 5235 | /* Is there anybody out there ? */ |
099e26bd | 5236 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 5237 | return -EEXIST; |
099e26bd | 5238 | } |
b09c7c76 DG |
5239 | |
5240 | return 0; | |
fac6795d DG |
5241 | } |
5242 | ||
fac6795d | 5243 | /* |
d063d709 | 5244 | * Set the tracing group gid onto the client socket. |
5e16da05 | 5245 | * |
d063d709 | 5246 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 5247 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 5248 | */ |
be040666 | 5249 | static int set_permissions(char *rundir) |
fac6795d DG |
5250 | { |
5251 | int ret; | |
996b65c8 | 5252 | gid_t gid; |
fac6795d | 5253 | |
6c71277b | 5254 | gid = utils_get_group_id(tracing_group_name); |
fac6795d | 5255 | |
d6f42150 | 5256 | /* Set lttng run dir */ |
be040666 | 5257 | ret = chown(rundir, 0, gid); |
d6f42150 | 5258 | if (ret < 0) { |
be040666 | 5259 | ERR("Unable to set group on %s", rundir); |
76d7553f | 5260 | PERROR("chown"); |
d6f42150 DG |
5261 | } |
5262 | ||
6c71277b MD |
5263 | /* |
5264 | * Ensure all applications and tracing group can search the run | |
5265 | * dir. Allow everyone to read the directory, since it does not | |
5266 | * buy us anything to hide its content. | |
5267 | */ | |
5268 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
5269 | if (ret < 0) { |
5270 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 5271 | PERROR("chmod"); |
d1613cf5 JN |
5272 | } |
5273 | ||
d6f42150 | 5274 | /* lttng client socket path */ |
996b65c8 | 5275 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 5276 | if (ret < 0) { |
d6f42150 | 5277 | ERR("Unable to set group on %s", client_unix_sock_path); |
76d7553f | 5278 | PERROR("chown"); |
d6f42150 DG |
5279 | } |
5280 | ||
3bd1e081 | 5281 | /* kconsumer error socket path */ |
6c71277b | 5282 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 5283 | if (ret < 0) { |
3bd1e081 | 5284 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 5285 | PERROR("chown"); |
3bd1e081 MD |
5286 | } |
5287 | ||
7753dea8 | 5288 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 5289 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
5290 | if (ret < 0) { |
5291 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 5292 | PERROR("chown"); |
7753dea8 MD |
5293 | } |
5294 | ||
5295 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 5296 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 5297 | if (ret < 0) { |
7753dea8 | 5298 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 5299 | PERROR("chown"); |
fac6795d DG |
5300 | } |
5301 | ||
d6f42150 | 5302 | DBG("All permissions are set"); |
e07ae692 | 5303 | |
fac6795d DG |
5304 | return ret; |
5305 | } | |
5306 | ||
d6f42150 | 5307 | /* |
d063d709 | 5308 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 5309 | */ |
67e40797 | 5310 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
5311 | { |
5312 | int ret; | |
5313 | ||
67e40797 DG |
5314 | DBG3("Creating LTTng run directory: %s", rundir); |
5315 | ||
d1613cf5 | 5316 | ret = mkdir(rundir, S_IRWXU); |
d6f42150 | 5317 | if (ret < 0) { |
b1f11e69 | 5318 | if (errno != EEXIST) { |
67e40797 | 5319 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
5320 | goto error; |
5321 | } else { | |
5322 | ret = 0; | |
5323 | } | |
d6f42150 DG |
5324 | } |
5325 | ||
5326 | error: | |
5327 | return ret; | |
5328 | } | |
5329 | ||
5330 | /* | |
d063d709 DG |
5331 | * Setup sockets and directory needed by the kconsumerd communication with the |
5332 | * session daemon. | |
d6f42150 | 5333 | */ |
67e40797 DG |
5334 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
5335 | const char *rundir) | |
d6f42150 DG |
5336 | { |
5337 | int ret; | |
67e40797 | 5338 | char path[PATH_MAX]; |
d6f42150 | 5339 | |
6c71277b | 5340 | switch (consumer_data->type) { |
7753dea8 | 5341 | case LTTNG_CONSUMER_KERNEL: |
60922cb0 | 5342 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
7753dea8 MD |
5343 | break; |
5344 | case LTTNG_CONSUMER64_UST: | |
60922cb0 | 5345 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
5346 | break; |
5347 | case LTTNG_CONSUMER32_UST: | |
60922cb0 | 5348 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
5349 | break; |
5350 | default: | |
5351 | ERR("Consumer type unknown"); | |
5352 | ret = -EINVAL; | |
5353 | goto error; | |
d6f42150 DG |
5354 | } |
5355 | ||
67e40797 DG |
5356 | DBG2("Creating consumer directory: %s", path); |
5357 | ||
6c71277b | 5358 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
d6f42150 | 5359 | if (ret < 0) { |
6beb2242 | 5360 | if (errno != EEXIST) { |
f11e84c2 | 5361 | PERROR("mkdir"); |
3bd1e081 | 5362 | ERR("Failed to create %s", path); |
6beb2242 DG |
5363 | goto error; |
5364 | } | |
f11e84c2 | 5365 | ret = -1; |
d6f42150 | 5366 | } |
6c71277b MD |
5367 | if (is_root) { |
5368 | ret = chown(path, 0, utils_get_group_id(tracing_group_name)); | |
5369 | if (ret < 0) { | |
5370 | ERR("Unable to set group on %s", path); | |
5371 | PERROR("chown"); | |
5372 | goto error; | |
5373 | } | |
5374 | } | |
d6f42150 DG |
5375 | |
5376 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
5377 | consumer_data->err_sock = |
5378 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
5379 | if (consumer_data->err_sock < 0) { | |
5380 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
5381 | ret = -1; |
5382 | goto error; | |
5383 | } | |
5384 | ||
a24f05ab MD |
5385 | /* |
5386 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
5387 | * show must go on. | |
5388 | */ | |
5389 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
5390 | if (ret < 0) { | |
5391 | PERROR("utils_set_fd_cloexec"); | |
5392 | /* continue anyway */ | |
5393 | } | |
5394 | ||
d6f42150 | 5395 | /* File permission MUST be 660 */ |
3bd1e081 | 5396 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 5397 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 5398 | if (ret < 0) { |
3bd1e081 | 5399 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 5400 | PERROR("chmod"); |
d6f42150 DG |
5401 | goto error; |
5402 | } | |
5403 | ||
5404 | error: | |
5405 | return ret; | |
5406 | } | |
5407 | ||
fac6795d | 5408 | /* |
d063d709 | 5409 | * Signal handler for the daemon |
cf3af59e | 5410 | * |
54d01ffb DG |
5411 | * Simply stop all worker threads, leaving main() return gracefully after |
5412 | * joining all threads and calling cleanup(). | |
fac6795d DG |
5413 | */ |
5414 | static void sighandler(int sig) | |
5415 | { | |
5416 | switch (sig) { | |
cf3af59e | 5417 | case SIGINT: |
af87c45a | 5418 | DBG("SIGINT caught"); |
cf3af59e MD |
5419 | stop_threads(); |
5420 | break; | |
5421 | case SIGTERM: | |
af87c45a | 5422 | DBG("SIGTERM caught"); |
cf3af59e MD |
5423 | stop_threads(); |
5424 | break; | |
0bb7724a DG |
5425 | case SIGUSR1: |
5426 | CMM_STORE_SHARED(recv_child_signal, 1); | |
5427 | break; | |
cf3af59e MD |
5428 | default: |
5429 | break; | |
fac6795d | 5430 | } |
fac6795d DG |
5431 | } |
5432 | ||
5433 | /* | |
d063d709 | 5434 | * Setup signal handler for : |
1d4b027a | 5435 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 5436 | */ |
1d4b027a | 5437 | static int set_signal_handler(void) |
fac6795d | 5438 | { |
1d4b027a DG |
5439 | int ret = 0; |
5440 | struct sigaction sa; | |
5441 | sigset_t sigset; | |
fac6795d | 5442 | |
1d4b027a | 5443 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 5444 | PERROR("sigemptyset"); |
1d4b027a DG |
5445 | return ret; |
5446 | } | |
d6f42150 | 5447 | |
1d4b027a DG |
5448 | sa.sa_mask = sigset; |
5449 | sa.sa_flags = 0; | |
0072e5e2 MD |
5450 | |
5451 | sa.sa_handler = sighandler; | |
1d4b027a | 5452 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 5453 | PERROR("sigaction"); |
1d4b027a | 5454 | return ret; |
d6f42150 DG |
5455 | } |
5456 | ||
1d4b027a | 5457 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 5458 | PERROR("sigaction"); |
1d4b027a | 5459 | return ret; |
d6f42150 | 5460 | } |
aaf26714 | 5461 | |
0072e5e2 | 5462 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 5463 | PERROR("sigaction"); |
1d4b027a | 5464 | return ret; |
8c0faa1d DG |
5465 | } |
5466 | ||
0072e5e2 MD |
5467 | sa.sa_handler = SIG_IGN; |
5468 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
5469 | PERROR("sigaction"); |
5470 | return ret; | |
5471 | } | |
5472 | ||
5473 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
5474 | |
5475 | return ret; | |
fac6795d DG |
5476 | } |
5477 | ||
f3ed775e | 5478 | /* |
d063d709 | 5479 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 5480 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
5481 | */ |
5482 | static void set_ulimit(void) | |
5483 | { | |
5484 | int ret; | |
5485 | struct rlimit lim; | |
5486 | ||
514bb9f0 | 5487 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
5488 | lim.rlim_cur = 65535; |
5489 | lim.rlim_max = 65535; | |
5490 | ||
5491 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
5492 | if (ret < 0) { | |
76d7553f | 5493 | PERROR("failed to set open files limit"); |
f3ed775e DG |
5494 | } |
5495 | } | |
5496 | ||
35f90c40 DG |
5497 | /* |
5498 | * Write pidfile using the rundir and opt_pidfile. | |
5499 | */ | |
4a15001e | 5500 | static int write_pidfile(void) |
35f90c40 DG |
5501 | { |
5502 | int ret; | |
5503 | char pidfile_path[PATH_MAX]; | |
5504 | ||
5505 | assert(rundir); | |
5506 | ||
5507 | if (opt_pidfile) { | |
b8d76705 MD |
5508 | if (lttng_strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path))) { |
5509 | ret = -1; | |
5510 | goto error; | |
5511 | } | |
35f90c40 DG |
5512 | } else { |
5513 | /* Build pidfile path from rundir and opt_pidfile. */ | |
5514 | ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/" | |
5515 | DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir); | |
5516 | if (ret < 0) { | |
5517 | PERROR("snprintf pidfile path"); | |
5518 | goto error; | |
5519 | } | |
5520 | } | |
5521 | ||
5522 | /* | |
4a15001e | 5523 | * Create pid file in rundir. |
35f90c40 | 5524 | */ |
4a15001e | 5525 | ret = utils_create_pid_file(getpid(), pidfile_path); |
35f90c40 | 5526 | error: |
4a15001e | 5527 | return ret; |
35f90c40 DG |
5528 | } |
5529 | ||
c9cb3e7d JG |
5530 | /* |
5531 | * Create lockfile using the rundir and return its fd. | |
5532 | */ | |
5533 | static int create_lockfile(void) | |
5534 | { | |
5535 | int ret; | |
5536 | char lockfile_path[PATH_MAX]; | |
5537 | ||
5538 | ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path)); | |
5539 | if (ret < 0) { | |
5540 | goto error; | |
5541 | } | |
5542 | ||
5543 | ret = utils_create_lock_file(lockfile_path); | |
5544 | error: | |
5545 | return ret; | |
5546 | } | |
5547 | ||
cd9290dd | 5548 | /* |
022d91ba | 5549 | * Write agent TCP port using the rundir. |
cd9290dd | 5550 | */ |
4a15001e | 5551 | static int write_agent_port(void) |
cd9290dd DG |
5552 | { |
5553 | int ret; | |
5554 | char path[PATH_MAX]; | |
5555 | ||
5556 | assert(rundir); | |
5557 | ||
5558 | ret = snprintf(path, sizeof(path), "%s/" | |
022d91ba | 5559 | DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir); |
cd9290dd | 5560 | if (ret < 0) { |
022d91ba | 5561 | PERROR("snprintf agent port path"); |
cd9290dd DG |
5562 | goto error; |
5563 | } | |
5564 | ||
5565 | /* | |
4a15001e | 5566 | * Create TCP agent port file in rundir. |
cd9290dd | 5567 | */ |
4a15001e | 5568 | ret = utils_create_pid_file(agent_tcp_port, path); |
cd9290dd DG |
5569 | |
5570 | error: | |
4a15001e | 5571 | return ret; |
ef367a93 JG |
5572 | } |
5573 | ||
51c59105 JG |
5574 | static int set_clock_plugin_env(void) |
5575 | { | |
5576 | int ret = 0; | |
5577 | const char *original_env_value; | |
5578 | char *full_path = NULL; | |
5579 | char *new_env_value = NULL; | |
5580 | ||
5581 | original_env_value = getenv("LTTNG_UST_CLOCK_PLUGIN"); | |
5582 | if (!original_env_value) { | |
5583 | goto end; | |
5584 | } | |
5585 | ||
5586 | full_path = utils_expand_path(original_env_value); | |
5587 | if (!full_path) { | |
5588 | ERR("Failed to expand LTTNG_UST_CLOCK_PLUGIN path \"%s\"", | |
5589 | original_env_value); | |
5590 | ret = -1; | |
5591 | goto end; | |
5592 | } | |
5593 | ret = asprintf(&new_env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
5594 | full_path); | |
5595 | free(full_path); | |
5596 | if (ret < 0) { | |
5597 | PERROR("asprintf"); | |
5598 | goto end; | |
5599 | } | |
5600 | ||
5601 | DBG("Updating environment: %s", new_env_value); | |
5602 | ret = putenv(new_env_value); | |
5603 | if (ret) { | |
5604 | free(new_env_value); | |
5605 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
5606 | goto end; | |
5607 | } | |
5608 | end: | |
5609 | return ret; | |
5610 | } | |
5611 | ||
fac6795d DG |
5612 | /* |
5613 | * main | |
5614 | */ | |
5615 | int main(int argc, char **argv) | |
5616 | { | |
4a15001e | 5617 | int ret = 0, retval = 0; |
fac6795d | 5618 | void *status; |
ae9e45b3 | 5619 | const char *home_path, *env_app_timeout; |
fac6795d | 5620 | |
335a95b7 MD |
5621 | init_kernel_workarounds(); |
5622 | ||
f6a9efaa DG |
5623 | rcu_register_thread(); |
5624 | ||
4a15001e MD |
5625 | if (set_signal_handler()) { |
5626 | retval = -1; | |
5627 | goto exit_set_signal_handler; | |
0bb7724a DG |
5628 | } |
5629 | ||
7753dea8 | 5630 | setup_consumerd_path(); |
fb09408a | 5631 | |
12744796 DG |
5632 | page_size = sysconf(_SC_PAGESIZE); |
5633 | if (page_size < 0) { | |
5634 | PERROR("sysconf _SC_PAGESIZE"); | |
5635 | page_size = LONG_MAX; | |
5636 | WARN("Fallback page size to %ld", page_size); | |
5637 | } | |
5638 | ||
4a15001e MD |
5639 | /* |
5640 | * Parse arguments and load the daemon configuration file. | |
5641 | * | |
5642 | * We have an exit_options exit path to free memory reserved by | |
5643 | * set_options. This is needed because the rest of sessiond_cleanup() | |
5644 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
5645 | * depends on set_options. | |
5646 | */ | |
fac6795d | 5647 | progname = argv[0]; |
4a15001e MD |
5648 | if (set_options(argc, argv)) { |
5649 | retval = -1; | |
5650 | goto exit_options; | |
fac6795d DG |
5651 | } |
5652 | ||
cdd566a2 JR |
5653 | sessiond_config_log(); |
5654 | ||
5655 | if (opt_print_version) { | |
5656 | print_version(); | |
5657 | retval = 0; | |
5658 | goto exit_options; | |
5659 | } | |
5660 | ||
51c59105 JG |
5661 | ret = set_clock_plugin_env(); |
5662 | if (ret) { | |
5663 | retval = -1; | |
5664 | goto exit_options; | |
5665 | } | |
5666 | ||
fac6795d | 5667 | /* Daemonize */ |
72dd7491 | 5668 | if (opt_daemon || opt_background) { |
ceed52b5 MD |
5669 | int i; |
5670 | ||
72dd7491 MD |
5671 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
5672 | !opt_background); | |
53094c05 | 5673 | if (ret < 0) { |
4a15001e MD |
5674 | retval = -1; |
5675 | goto exit_options; | |
53094c05 | 5676 | } |
0bb7724a | 5677 | |
ceed52b5 | 5678 | /* |
0bb7724a | 5679 | * We are in the child. Make sure all other file descriptors are |
4a15001e MD |
5680 | * closed, in case we are called with more opened file |
5681 | * descriptors than the standard ones. | |
ceed52b5 MD |
5682 | */ |
5683 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
5684 | (void) close(i); | |
5685 | } | |
5686 | } | |
5687 | ||
7567352f MD |
5688 | if (run_as_create_worker(argv[0]) < 0) { |
5689 | goto exit_create_run_as_worker_cleanup; | |
5690 | } | |
5691 | ||
4a15001e MD |
5692 | /* |
5693 | * Starting from here, we can create threads. This needs to be after | |
5694 | * lttng_daemonize due to RCU. | |
5695 | */ | |
5696 | ||
5697 | /* | |
5698 | * Initialize the health check subsystem. This call should set the | |
5699 | * appropriate time values. | |
5700 | */ | |
5701 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
5702 | if (!health_sessiond) { | |
5703 | PERROR("health_app_create error"); | |
5704 | retval = -1; | |
5705 | goto exit_health_sessiond_cleanup; | |
5706 | } | |
5707 | ||
4a15001e | 5708 | /* Create thread to clean up RCU hash tables */ |
5e97de00 | 5709 | if (init_ht_cleanup_thread(&ht_cleanup_thread)) { |
4a15001e MD |
5710 | retval = -1; |
5711 | goto exit_ht_cleanup; | |
5712 | } | |
5713 | ||
ceed52b5 | 5714 | /* Create thread quit pipe */ |
4a15001e MD |
5715 | if (init_thread_quit_pipe()) { |
5716 | retval = -1; | |
5717 | goto exit_init_data; | |
fac6795d DG |
5718 | } |
5719 | ||
5720 | /* Check if daemon is UID = 0 */ | |
5721 | is_root = !getuid(); | |
5722 | ||
fac6795d | 5723 | if (is_root) { |
990570ed | 5724 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
4ea184d1 | 5725 | if (!rundir) { |
4a15001e MD |
5726 | retval = -1; |
5727 | goto exit_init_data; | |
4ea184d1 | 5728 | } |
67e40797 DG |
5729 | |
5730 | /* Create global run dir with root access */ | |
4a15001e MD |
5731 | if (create_lttng_rundir(rundir)) { |
5732 | retval = -1; | |
5733 | goto exit_init_data; | |
d6f42150 DG |
5734 | } |
5735 | ||
fac6795d | 5736 | if (strlen(apps_unix_sock_path) == 0) { |
4a15001e | 5737 | ret = snprintf(apps_unix_sock_path, PATH_MAX, |
d6f42150 | 5738 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); |
4a15001e MD |
5739 | if (ret < 0) { |
5740 | retval = -1; | |
5741 | goto exit_init_data; | |
5742 | } | |
fac6795d DG |
5743 | } |
5744 | ||
5745 | if (strlen(client_unix_sock_path) == 0) { | |
4a15001e | 5746 | ret = snprintf(client_unix_sock_path, PATH_MAX, |
d6f42150 | 5747 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); |
4a15001e MD |
5748 | if (ret < 0) { |
5749 | retval = -1; | |
5750 | goto exit_init_data; | |
5751 | } | |
d6f42150 | 5752 | } |
0fdd1e2c DG |
5753 | |
5754 | /* Set global SHM for ust */ | |
5755 | if (strlen(wait_shm_path) == 0) { | |
4a15001e | 5756 | ret = snprintf(wait_shm_path, PATH_MAX, |
0fdd1e2c | 5757 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); |
4a15001e MD |
5758 | if (ret < 0) { |
5759 | retval = -1; | |
5760 | goto exit_init_data; | |
5761 | } | |
0fdd1e2c | 5762 | } |
67e40797 | 5763 | |
44a5e5eb | 5764 | if (strlen(health_unix_sock_path) == 0) { |
4a15001e MD |
5765 | ret = snprintf(health_unix_sock_path, |
5766 | sizeof(health_unix_sock_path), | |
44a5e5eb | 5767 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); |
4a15001e MD |
5768 | if (ret < 0) { |
5769 | retval = -1; | |
5770 | goto exit_init_data; | |
5771 | } | |
44a5e5eb DG |
5772 | } |
5773 | ||
67e40797 | 5774 | /* Setup kernel consumerd path */ |
4a15001e | 5775 | ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, |
60922cb0 | 5776 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
4a15001e MD |
5777 | if (ret < 0) { |
5778 | retval = -1; | |
5779 | goto exit_init_data; | |
5780 | } | |
5781 | ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, | |
60922cb0 | 5782 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
4a15001e MD |
5783 | if (ret < 0) { |
5784 | retval = -1; | |
5785 | goto exit_init_data; | |
5786 | } | |
67e40797 DG |
5787 | |
5788 | DBG2("Kernel consumer err path: %s", | |
5789 | kconsumer_data.err_unix_sock_path); | |
5790 | DBG2("Kernel consumer cmd path: %s", | |
5791 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 5792 | } else { |
feb0f3e5 | 5793 | home_path = utils_get_home_dir(); |
b082db07 | 5794 | if (home_path == NULL) { |
273ea72c DG |
5795 | /* TODO: Add --socket PATH option */ |
5796 | ERR("Can't get HOME directory for sockets creation."); | |
4a15001e MD |
5797 | retval = -1; |
5798 | goto exit_init_data; | |
b082db07 DG |
5799 | } |
5800 | ||
67e40797 DG |
5801 | /* |
5802 | * Create rundir from home path. This will create something like | |
5803 | * $HOME/.lttng | |
5804 | */ | |
990570ed | 5805 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
67e40797 | 5806 | if (ret < 0) { |
4a15001e MD |
5807 | retval = -1; |
5808 | goto exit_init_data; | |
67e40797 DG |
5809 | } |
5810 | ||
4a15001e MD |
5811 | if (create_lttng_rundir(rundir)) { |
5812 | retval = -1; | |
5813 | goto exit_init_data; | |
67e40797 DG |
5814 | } |
5815 | ||
fac6795d | 5816 | if (strlen(apps_unix_sock_path) == 0) { |
4a15001e MD |
5817 | ret = snprintf(apps_unix_sock_path, PATH_MAX, |
5818 | DEFAULT_HOME_APPS_UNIX_SOCK, | |
5819 | home_path); | |
5820 | if (ret < 0) { | |
5821 | retval = -1; | |
5822 | goto exit_init_data; | |
5823 | } | |
fac6795d DG |
5824 | } |
5825 | ||
5826 | /* Set the cli tool unix socket path */ | |
5827 | if (strlen(client_unix_sock_path) == 0) { | |
4a15001e MD |
5828 | ret = snprintf(client_unix_sock_path, PATH_MAX, |
5829 | DEFAULT_HOME_CLIENT_UNIX_SOCK, | |
5830 | home_path); | |
5831 | if (ret < 0) { | |
5832 | retval = -1; | |
5833 | goto exit_init_data; | |
5834 | } | |
fac6795d | 5835 | } |
0fdd1e2c DG |
5836 | |
5837 | /* Set global SHM for ust */ | |
5838 | if (strlen(wait_shm_path) == 0) { | |
4a15001e MD |
5839 | ret = snprintf(wait_shm_path, PATH_MAX, |
5840 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, | |
5841 | getuid()); | |
5842 | if (ret < 0) { | |
5843 | retval = -1; | |
5844 | goto exit_init_data; | |
5845 | } | |
0fdd1e2c | 5846 | } |
44a5e5eb DG |
5847 | |
5848 | /* Set health check Unix path */ | |
5849 | if (strlen(health_unix_sock_path) == 0) { | |
4a15001e MD |
5850 | ret = snprintf(health_unix_sock_path, |
5851 | sizeof(health_unix_sock_path), | |
5852 | DEFAULT_HOME_HEALTH_UNIX_SOCK, | |
5853 | home_path); | |
5854 | if (ret < 0) { | |
5855 | retval = -1; | |
5856 | goto exit_init_data; | |
5857 | } | |
44a5e5eb | 5858 | } |
fac6795d DG |
5859 | } |
5860 | ||
c9cb3e7d JG |
5861 | lockfile_fd = create_lockfile(); |
5862 | if (lockfile_fd < 0) { | |
4a15001e MD |
5863 | retval = -1; |
5864 | goto exit_init_data; | |
c9cb3e7d JG |
5865 | } |
5866 | ||
5c827ce0 DG |
5867 | /* Set consumer initial state */ |
5868 | kernel_consumerd_state = CONSUMER_STOPPED; | |
5869 | ust_consumerd_state = CONSUMER_STOPPED; | |
5870 | ||
847177cd DG |
5871 | DBG("Client socket path %s", client_unix_sock_path); |
5872 | DBG("Application socket path %s", apps_unix_sock_path); | |
d0b96690 | 5873 | DBG("Application wait path %s", wait_shm_path); |
67e40797 DG |
5874 | DBG("LTTng run directory path: %s", rundir); |
5875 | ||
5876 | /* 32 bits consumerd path setup */ | |
4a15001e | 5877 | ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, |
60922cb0 | 5878 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
4a15001e MD |
5879 | if (ret < 0) { |
5880 | PERROR("snprintf 32-bit consumer error socket path"); | |
5881 | retval = -1; | |
5882 | goto exit_init_data; | |
5883 | } | |
5884 | ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, | |
60922cb0 | 5885 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
4a15001e MD |
5886 | if (ret < 0) { |
5887 | PERROR("snprintf 32-bit consumer command socket path"); | |
5888 | retval = -1; | |
5889 | goto exit_init_data; | |
5890 | } | |
67e40797 DG |
5891 | |
5892 | DBG2("UST consumer 32 bits err path: %s", | |
5893 | ustconsumer32_data.err_unix_sock_path); | |
5894 | DBG2("UST consumer 32 bits cmd path: %s", | |
5895 | ustconsumer32_data.cmd_unix_sock_path); | |
5896 | ||
5897 | /* 64 bits consumerd path setup */ | |
4a15001e | 5898 | ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, |
60922cb0 | 5899 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
4a15001e MD |
5900 | if (ret < 0) { |
5901 | PERROR("snprintf 64-bit consumer error socket path"); | |
5902 | retval = -1; | |
5903 | goto exit_init_data; | |
5904 | } | |
5905 | ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, | |
60922cb0 | 5906 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
4a15001e MD |
5907 | if (ret < 0) { |
5908 | PERROR("snprintf 64-bit consumer command socket path"); | |
5909 | retval = -1; | |
5910 | goto exit_init_data; | |
5911 | } | |
67e40797 DG |
5912 | |
5913 | DBG2("UST consumer 64 bits err path: %s", | |
5914 | ustconsumer64_data.err_unix_sock_path); | |
5915 | DBG2("UST consumer 64 bits cmd path: %s", | |
5916 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 5917 | |
273ea72c | 5918 | /* |
7d8234d9 | 5919 | * See if daemon already exist. |
fac6795d | 5920 | */ |
4a15001e | 5921 | if (check_existing_daemon()) { |
75462a81 | 5922 | ERR("Already running daemon.\n"); |
273ea72c | 5923 | /* |
cf3af59e MD |
5924 | * We do not goto exit because we must not cleanup() |
5925 | * because a daemon is already running. | |
ab118b20 | 5926 | */ |
4a15001e MD |
5927 | retval = -1; |
5928 | goto exit_init_data; | |
a88df331 DG |
5929 | } |
5930 | ||
1427f9b2 DG |
5931 | /* |
5932 | * Init UST app hash table. Alloc hash table before this point since | |
5933 | * cleanup() can get called after that point. | |
5934 | */ | |
4a15001e | 5935 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 5936 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
5937 | retval = -1; |
5938 | goto exit_init_data; | |
5939 | } | |
1427f9b2 | 5940 | |
6a4e4039 JG |
5941 | /* |
5942 | * Initialize agent app hash table. We allocate the hash table here | |
5943 | * since cleanup() can get called after this point. | |
5944 | */ | |
5945 | if (agent_app_ht_alloc()) { | |
5946 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
5947 | retval = -1; |
5948 | goto exit_init_data; | |
f20baf8e DG |
5949 | } |
5950 | ||
a88df331 DG |
5951 | /* |
5952 | * These actions must be executed as root. We do that *after* setting up | |
5953 | * the sockets path because we MUST make the check for another daemon using | |
5954 | * those paths *before* trying to set the kernel consumer sockets and init | |
5955 | * kernel tracer. | |
5956 | */ | |
5957 | if (is_root) { | |
4a15001e MD |
5958 | if (set_consumer_sockets(&kconsumer_data, rundir)) { |
5959 | retval = -1; | |
5960 | goto exit_init_data; | |
7753dea8 MD |
5961 | } |
5962 | ||
a88df331 | 5963 | /* Setup kernel tracer */ |
4fba7219 DG |
5964 | if (!opt_no_kernel) { |
5965 | init_kernel_tracer(); | |
89588270 DG |
5966 | if (kernel_tracer_fd >= 0) { |
5967 | ret = syscall_init_table(); | |
5968 | if (ret < 0) { | |
4a15001e MD |
5969 | ERR("Unable to populate syscall table. " |
5970 | "Syscall tracing won't work " | |
5971 | "for this session daemon."); | |
89588270 | 5972 | } |
834978fd | 5973 | } |
4fba7219 | 5974 | } |
a88df331 DG |
5975 | |
5976 | /* Set ulimit for open files */ | |
5977 | set_ulimit(); | |
fac6795d | 5978 | } |
4063050c MD |
5979 | /* init lttng_fd tracking must be done after set_ulimit. */ |
5980 | lttng_fd_init(); | |
fac6795d | 5981 | |
4a15001e MD |
5982 | if (set_consumer_sockets(&ustconsumer64_data, rundir)) { |
5983 | retval = -1; | |
5984 | goto exit_init_data; | |
67e40797 DG |
5985 | } |
5986 | ||
4a15001e MD |
5987 | if (set_consumer_sockets(&ustconsumer32_data, rundir)) { |
5988 | retval = -1; | |
5989 | goto exit_init_data; | |
67e40797 DG |
5990 | } |
5991 | ||
d6f42150 | 5992 | /* Setup the needed unix socket */ |
4a15001e MD |
5993 | if (init_daemon_socket()) { |
5994 | retval = -1; | |
5995 | goto exit_init_data; | |
fac6795d DG |
5996 | } |
5997 | ||
5998 | /* Set credentials to socket */ | |
4a15001e MD |
5999 | if (is_root && set_permissions(rundir)) { |
6000 | retval = -1; | |
6001 | goto exit_init_data; | |
fac6795d DG |
6002 | } |
6003 | ||
5b8719f5 DG |
6004 | /* Get parent pid if -S, --sig-parent is specified. */ |
6005 | if (opt_sig_parent) { | |
6006 | ppid = getppid(); | |
6007 | } | |
6008 | ||
7a485870 | 6009 | /* Setup the kernel pipe for waking up the kernel thread */ |
6620da75 | 6010 | if (is_root && !opt_no_kernel) { |
4a15001e MD |
6011 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
6012 | retval = -1; | |
6013 | goto exit_init_data; | |
6620da75 | 6014 | } |
7a485870 DG |
6015 | } |
6016 | ||
099e26bd | 6017 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
6018 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
6019 | retval = -1; | |
6020 | goto exit_init_data; | |
099e26bd DG |
6021 | } |
6022 | ||
d0b96690 | 6023 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
6024 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
6025 | retval = -1; | |
6026 | goto exit_init_data; | |
d0b96690 DG |
6027 | } |
6028 | ||
7972aab2 DG |
6029 | /* Initialize global buffer per UID and PID registry. */ |
6030 | buffer_reg_init_uid_registry(); | |
6031 | buffer_reg_init_pid_registry(); | |
6032 | ||
099e26bd | 6033 | /* Init UST command queue. */ |
8bdee6e2 | 6034 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 6035 | |
273ea72c | 6036 | /* |
4a15001e MD |
6037 | * Get session list pointer. This pointer MUST NOT be free'd. This list |
6038 | * is statically declared in session.c | |
273ea72c | 6039 | */ |
54d01ffb | 6040 | session_list_ptr = session_get_list(); |
b5541356 | 6041 | |
2f77fc4b | 6042 | cmd_init(); |
00e2e675 | 6043 | |
ae9e45b3 DG |
6044 | /* Check for the application socket timeout env variable. */ |
6045 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
6046 | if (env_app_timeout) { | |
6047 | app_socket_timeout = atoi(env_app_timeout); | |
6048 | } else { | |
6049 | app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; | |
6050 | } | |
6051 | ||
4a15001e MD |
6052 | ret = write_pidfile(); |
6053 | if (ret) { | |
6054 | ERR("Error in write_pidfile"); | |
6055 | retval = -1; | |
6056 | goto exit_init_data; | |
6057 | } | |
6058 | ret = write_agent_port(); | |
6059 | if (ret) { | |
6060 | ERR("Error in write_agent_port"); | |
6061 | retval = -1; | |
6062 | goto exit_init_data; | |
6063 | } | |
35f90c40 | 6064 | |
554831e7 MD |
6065 | /* Initialize communication library */ |
6066 | lttcomm_init(); | |
4a15001e | 6067 | /* Initialize TCP timeout values */ |
d831c249 | 6068 | lttcomm_inet_init(); |
554831e7 | 6069 | |
ef367a93 | 6070 | if (load_session_init_data(&load_info) < 0) { |
4a15001e MD |
6071 | retval = -1; |
6072 | goto exit_init_data; | |
ef367a93 JG |
6073 | } |
6074 | load_info->path = opt_load_session_path; | |
6075 | ||
d5b9fd2f | 6076 | /* Create health-check thread */ |
1a1a34b4 | 6077 | ret = pthread_create(&health_thread, default_pthread_attr(), |
44a5e5eb | 6078 | thread_manage_health, (void *) NULL); |
4a15001e MD |
6079 | if (ret) { |
6080 | errno = ret; | |
44a5e5eb | 6081 | PERROR("pthread_create health"); |
4a15001e | 6082 | retval = -1; |
44a5e5eb DG |
6083 | goto exit_health; |
6084 | } | |
6085 | ||
cf3af59e | 6086 | /* Create thread to manage the client socket */ |
1a1a34b4 | 6087 | ret = pthread_create(&client_thread, default_pthread_attr(), |
099e26bd | 6088 | thread_manage_clients, (void *) NULL); |
4a15001e MD |
6089 | if (ret) { |
6090 | errno = ret; | |
76d7553f | 6091 | PERROR("pthread_create clients"); |
4a15001e | 6092 | retval = -1; |
cf3af59e MD |
6093 | goto exit_client; |
6094 | } | |
fac6795d | 6095 | |
099e26bd | 6096 | /* Create thread to dispatch registration */ |
1a1a34b4 | 6097 | ret = pthread_create(&dispatch_thread, default_pthread_attr(), |
099e26bd | 6098 | thread_dispatch_ust_registration, (void *) NULL); |
4a15001e MD |
6099 | if (ret) { |
6100 | errno = ret; | |
76d7553f | 6101 | PERROR("pthread_create dispatch"); |
4a15001e | 6102 | retval = -1; |
099e26bd DG |
6103 | goto exit_dispatch; |
6104 | } | |
6105 | ||
6106 | /* Create thread to manage application registration. */ | |
1a1a34b4 | 6107 | ret = pthread_create(®_apps_thread, default_pthread_attr(), |
099e26bd | 6108 | thread_registration_apps, (void *) NULL); |
4a15001e MD |
6109 | if (ret) { |
6110 | errno = ret; | |
76d7553f | 6111 | PERROR("pthread_create registration"); |
4a15001e | 6112 | retval = -1; |
099e26bd DG |
6113 | goto exit_reg_apps; |
6114 | } | |
6115 | ||
cf3af59e | 6116 | /* Create thread to manage application socket */ |
1a1a34b4 | 6117 | ret = pthread_create(&apps_thread, default_pthread_attr(), |
54d01ffb | 6118 | thread_manage_apps, (void *) NULL); |
4a15001e MD |
6119 | if (ret) { |
6120 | errno = ret; | |
d0b96690 | 6121 | PERROR("pthread_create apps"); |
4a15001e | 6122 | retval = -1; |
d0b96690 DG |
6123 | goto exit_apps; |
6124 | } | |
6125 | ||
6126 | /* Create thread to manage application notify socket */ | |
1a1a34b4 | 6127 | ret = pthread_create(&apps_notify_thread, default_pthread_attr(), |
d0b96690 | 6128 | ust_thread_manage_notify, (void *) NULL); |
4a15001e MD |
6129 | if (ret) { |
6130 | errno = ret; | |
f5c32ef1 | 6131 | PERROR("pthread_create notify"); |
4a15001e | 6132 | retval = -1; |
9563b0ad | 6133 | goto exit_apps_notify; |
cf3af59e | 6134 | } |
fac6795d | 6135 | |
022d91ba | 6136 | /* Create agent registration thread. */ |
1a1a34b4 | 6137 | ret = pthread_create(&agent_reg_thread, default_pthread_attr(), |
022d91ba | 6138 | agent_thread_manage_registration, (void *) NULL); |
4a15001e MD |
6139 | if (ret) { |
6140 | errno = ret; | |
022d91ba | 6141 | PERROR("pthread_create agent"); |
4a15001e | 6142 | retval = -1; |
022d91ba | 6143 | goto exit_agent_reg; |
4d076222 DG |
6144 | } |
6145 | ||
6620da75 DG |
6146 | /* Don't start this thread if kernel tracing is not requested nor root */ |
6147 | if (is_root && !opt_no_kernel) { | |
6148 | /* Create kernel thread to manage kernel event */ | |
1a1a34b4 | 6149 | ret = pthread_create(&kernel_thread, default_pthread_attr(), |
6620da75 | 6150 | thread_manage_kernel, (void *) NULL); |
4a15001e MD |
6151 | if (ret) { |
6152 | errno = ret; | |
6620da75 | 6153 | PERROR("pthread_create kernel"); |
4a15001e | 6154 | retval = -1; |
6620da75 DG |
6155 | goto exit_kernel; |
6156 | } | |
ef367a93 | 6157 | } |
7a485870 | 6158 | |
4a15001e | 6159 | /* Create session loading thread. */ |
1a1a34b4 MJ |
6160 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
6161 | thread_load_session, load_info); | |
4a15001e MD |
6162 | if (ret) { |
6163 | errno = ret; | |
6164 | PERROR("pthread_create load_session_thread"); | |
6165 | retval = -1; | |
6166 | goto exit_load_session; | |
6167 | } | |
6168 | ||
6169 | /* | |
6170 | * This is where we start awaiting program completion (e.g. through | |
6171 | * signal that asks threads to teardown). | |
6172 | */ | |
6173 | ||
6174 | ret = pthread_join(load_session_thread, &status); | |
6175 | if (ret) { | |
6176 | errno = ret; | |
6177 | PERROR("pthread_join load_session_thread"); | |
6178 | retval = -1; | |
6179 | } | |
6180 | exit_load_session: | |
ef367a93 JG |
6181 | |
6182 | if (is_root && !opt_no_kernel) { | |
6620da75 | 6183 | ret = pthread_join(kernel_thread, &status); |
4a15001e MD |
6184 | if (ret) { |
6185 | errno = ret; | |
6620da75 | 6186 | PERROR("pthread_join"); |
4a15001e | 6187 | retval = -1; |
6620da75 | 6188 | } |
fac6795d | 6189 | } |
cf3af59e | 6190 | exit_kernel: |
4a15001e | 6191 | |
022d91ba | 6192 | ret = pthread_join(agent_reg_thread, &status); |
4a15001e MD |
6193 | if (ret) { |
6194 | errno = ret; | |
022d91ba | 6195 | PERROR("pthread_join agent"); |
4a15001e | 6196 | retval = -1; |
4d076222 | 6197 | } |
022d91ba | 6198 | exit_agent_reg: |
4a15001e | 6199 | |
9563b0ad | 6200 | ret = pthread_join(apps_notify_thread, &status); |
4a15001e MD |
6201 | if (ret) { |
6202 | errno = ret; | |
9563b0ad | 6203 | PERROR("pthread_join apps notify"); |
4a15001e | 6204 | retval = -1; |
9563b0ad | 6205 | } |
9563b0ad | 6206 | exit_apps_notify: |
4a15001e | 6207 | |
cf3af59e | 6208 | ret = pthread_join(apps_thread, &status); |
4a15001e MD |
6209 | if (ret) { |
6210 | errno = ret; | |
9563b0ad | 6211 | PERROR("pthread_join apps"); |
4a15001e | 6212 | retval = -1; |
cf3af59e | 6213 | } |
cf3af59e | 6214 | exit_apps: |
4a15001e | 6215 | |
099e26bd | 6216 | ret = pthread_join(reg_apps_thread, &status); |
4a15001e MD |
6217 | if (ret) { |
6218 | errno = ret; | |
76d7553f | 6219 | PERROR("pthread_join"); |
4a15001e | 6220 | retval = -1; |
099e26bd | 6221 | } |
099e26bd | 6222 | exit_reg_apps: |
4a15001e | 6223 | |
772b8f4d MD |
6224 | /* |
6225 | * Join dispatch thread after joining reg_apps_thread to ensure | |
6226 | * we don't leak applications in the queue. | |
6227 | */ | |
099e26bd | 6228 | ret = pthread_join(dispatch_thread, &status); |
4a15001e MD |
6229 | if (ret) { |
6230 | errno = ret; | |
76d7553f | 6231 | PERROR("pthread_join"); |
4a15001e | 6232 | retval = -1; |
099e26bd | 6233 | } |
099e26bd | 6234 | exit_dispatch: |
4a15001e | 6235 | |
cf3af59e | 6236 | ret = pthread_join(client_thread, &status); |
4a15001e MD |
6237 | if (ret) { |
6238 | errno = ret; | |
76d7553f | 6239 | PERROR("pthread_join"); |
4a15001e | 6240 | retval = -1; |
cf3af59e | 6241 | } |
4a15001e | 6242 | exit_client: |
a88df331 | 6243 | |
4a15001e MD |
6244 | ret = pthread_join(health_thread, &status); |
6245 | if (ret) { | |
6246 | errno = ret; | |
6247 | PERROR("pthread_join health thread"); | |
6248 | retval = -1; | |
06f525de | 6249 | } |
4a15001e | 6250 | exit_health: |
06f525de | 6251 | |
4a15001e | 6252 | exit_init_data: |
2668465a MD |
6253 | /* |
6254 | * Wait for all pending call_rcu work to complete before tearing | |
6255 | * down data structures. call_rcu worker may be trying to | |
6256 | * perform lookups in those structures. | |
6257 | */ | |
6258 | rcu_barrier(); | |
4a15001e MD |
6259 | /* |
6260 | * sessiond_cleanup() is called when no other thread is running, except | |
6261 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
6262 | */ | |
6263 | rcu_thread_online(); | |
6264 | sessiond_cleanup(); | |
6265 | rcu_thread_offline(); | |
6266 | rcu_unregister_thread(); | |
06f525de | 6267 | |
c4d5de9d JG |
6268 | /* |
6269 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
6270 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
6271 | * the queue is empty before shutting down the clean-up thread. | |
6272 | */ | |
6273 | rcu_barrier(); | |
6274 | ||
5e97de00 | 6275 | ret = fini_ht_cleanup_thread(&ht_cleanup_thread); |
4a15001e | 6276 | if (ret) { |
4a15001e | 6277 | retval = -1; |
0b2dc8df MD |
6278 | } |
6279 | exit_ht_cleanup: | |
4a15001e | 6280 | |
4a15001e MD |
6281 | health_app_destroy(health_sessiond); |
6282 | exit_health_sessiond_cleanup: | |
7567352f | 6283 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
6284 | |
6285 | exit_options: | |
6286 | sessiond_cleanup_options(); | |
6287 | ||
6288 | exit_set_signal_handler: | |
0658924e | 6289 | |
4a15001e | 6290 | if (!retval) { |
cf3af59e | 6291 | exit(EXIT_SUCCESS); |
4a15001e MD |
6292 | } else { |
6293 | exit(EXIT_FAILURE); | |
67e40797 | 6294 | } |
fac6795d | 6295 | } |