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