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