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