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