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