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