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