Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fac6795d | 4 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
91d76f53 | 8 | * |
d14d33bf AM |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
91d76f53 | 13 | * |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
fac6795d DG |
20 | #include <getopt.h> |
21 | #include <grp.h> | |
22 | #include <limits.h> | |
23 | #include <pthread.h> | |
24 | #include <signal.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
331744e3 | 28 | #include <inttypes.h> |
0fdd1e2c | 29 | #include <sys/mman.h> |
b73401da | 30 | #include <sys/mount.h> |
1e307fab | 31 | #include <sys/resource.h> |
fac6795d DG |
32 | #include <sys/socket.h> |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
0fdd1e2c | 35 | #include <sys/wait.h> |
5c827ce0 | 36 | #include <urcu/uatomic.h> |
fac6795d | 37 | #include <unistd.h> |
3bd1e081 | 38 | #include <config.h> |
fac6795d | 39 | |
990570ed | 40 | #include <common/common.h> |
d27c42b8 | 41 | #include <common/compat/socket.h> |
db758600 DG |
42 | #include <common/defaults.h> |
43 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 44 | #include <common/futex.h> |
00e2e675 | 45 | #include <common/relayd/relayd.h> |
81b86775 | 46 | #include <common/utils.h> |
fac6795d | 47 | |
10a8a223 | 48 | #include "lttng-sessiond.h" |
7972aab2 | 49 | #include "buffer-registry.h" |
54d01ffb | 50 | #include "channel.h" |
2f77fc4b | 51 | #include "cmd.h" |
00e2e675 | 52 | #include "consumer.h" |
099e26bd | 53 | #include "context.h" |
54d01ffb | 54 | #include "event.h" |
4771f025 | 55 | #include "kernel.h" |
f1e16794 | 56 | #include "kernel-consumer.h" |
096102bd | 57 | #include "modprobe.h" |
0fdd1e2c | 58 | #include "shm.h" |
1e307fab | 59 | #include "ust-ctl.h" |
00e2e675 | 60 | #include "ust-consumer.h" |
8e68d1c8 | 61 | #include "utils.h" |
4063050c | 62 | #include "fd-limit.h" |
8782cc74 | 63 | #include "health-sessiond.h" |
8ac94142 | 64 | #include "testpoint.h" |
d0b96690 | 65 | #include "ust-thread.h" |
4d076222 | 66 | #include "jul-thread.h" |
fac6795d | 67 | |
ebaeda94 MD |
68 | #define CONSUMERD_FILE "lttng-consumerd" |
69 | ||
fac6795d | 70 | const char *progname; |
6c71277b | 71 | static const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
35f90c40 | 72 | static const char *opt_pidfile; |
5b8719f5 | 73 | static int opt_sig_parent; |
97e19046 | 74 | static int opt_verbose_consumer; |
fac6795d | 75 | static int opt_daemon; |
4fba7219 | 76 | static int opt_no_kernel; |
fac6795d | 77 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 78 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
67e40797 | 79 | static char *rundir; |
3bd1e081 | 80 | |
a23ec3a7 DG |
81 | /* |
82 | * Consumer daemon specific control data. Every value not initialized here is | |
83 | * set to 0 by the static definition. | |
84 | */ | |
3bd1e081 MD |
85 | static struct consumer_data kconsumer_data = { |
86 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
87 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
88 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
89 | .err_sock = -1, |
90 | .cmd_sock = -1, | |
173af62f DG |
91 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
92 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
93 | .cond = PTHREAD_COND_INITIALIZER, |
94 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 95 | }; |
7753dea8 MD |
96 | static struct consumer_data ustconsumer64_data = { |
97 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
98 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
99 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
100 | .err_sock = -1, |
101 | .cmd_sock = -1, | |
173af62f DG |
102 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
103 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
104 | .cond = PTHREAD_COND_INITIALIZER, |
105 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
106 | }; |
107 | static struct consumer_data ustconsumer32_data = { | |
108 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
109 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
110 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
111 | .err_sock = -1, |
112 | .cmd_sock = -1, | |
173af62f DG |
113 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
114 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
115 | .cond = PTHREAD_COND_INITIALIZER, |
116 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
117 | }; |
118 | ||
26c9d55e | 119 | /* Shared between threads */ |
099e26bd | 120 | static int dispatch_thread_exit; |
fac6795d | 121 | |
54d01ffb DG |
122 | /* Global application Unix socket path */ |
123 | static char apps_unix_sock_path[PATH_MAX]; | |
124 | /* Global client Unix socket path */ | |
125 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
126 | /* global wait shm path for UST */ |
127 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
128 | /* Global health check unix path */ |
129 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 130 | |
1d4b027a | 131 | /* Sockets and FDs */ |
a4b35e07 MD |
132 | static int client_sock = -1; |
133 | static int apps_sock = -1; | |
2f77fc4b | 134 | int kernel_tracer_fd = -1; |
76d7553f | 135 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 136 | |
273ea72c DG |
137 | /* |
138 | * Quit pipe for all threads. This permits a single cancellation point | |
139 | * for all threads when receiving an event on the pipe. | |
140 | */ | |
76d7553f | 141 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 142 | |
099e26bd DG |
143 | /* |
144 | * This pipe is used to inform the thread managing application communication | |
145 | * that a command is queued and ready to be processed. | |
146 | */ | |
76d7553f | 147 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 148 | |
d0b96690 DG |
149 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
150 | ||
1d4b027a | 151 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 152 | static pthread_t apps_thread; |
d0b96690 | 153 | static pthread_t apps_notify_thread; |
099e26bd | 154 | static pthread_t reg_apps_thread; |
1d4b027a | 155 | static pthread_t client_thread; |
7a485870 | 156 | static pthread_t kernel_thread; |
099e26bd | 157 | static pthread_t dispatch_thread; |
44a5e5eb | 158 | static pthread_t health_thread; |
0b2dc8df | 159 | static pthread_t ht_cleanup_thread; |
4d076222 | 160 | static pthread_t jul_reg_thread; |
5eb91c98 | 161 | |
099e26bd DG |
162 | /* |
163 | * UST registration command queue. This queue is tied with a futex and uses a N | |
164 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
165 | * | |
166 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
167 | * this queue and the wait/wake scheme. | |
168 | */ | |
169 | static struct ust_cmd_queue ust_cmd_queue; | |
170 | ||
b5541356 DG |
171 | /* |
172 | * Pointer initialized before thread creation. | |
173 | * | |
174 | * This points to the tracing session list containing the session count and a | |
175 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
176 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 177 | * |
d063d709 | 178 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 179 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
180 | */ |
181 | static struct ltt_session_list *session_list_ptr; | |
182 | ||
7753dea8 MD |
183 | int ust_consumerd64_fd = -1; |
184 | int ust_consumerd32_fd = -1; | |
185 | ||
fb6f1fa2 YB |
186 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
187 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
188 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
189 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
fb09408a | 190 | |
2f77fc4b DG |
191 | static const char *module_proc_lttng = "/proc/lttng"; |
192 | ||
5c827ce0 DG |
193 | /* |
194 | * Consumer daemon state which is changed when spawning it, killing it or in | |
195 | * case of a fatal error. | |
196 | */ | |
197 | enum consumerd_state { | |
198 | CONSUMER_STARTED = 1, | |
199 | CONSUMER_STOPPED = 2, | |
200 | CONSUMER_ERROR = 3, | |
201 | }; | |
202 | ||
203 | /* | |
204 | * This consumer daemon state is used to validate if a client command will be | |
205 | * able to reach the consumer. If not, the client is informed. For instance, | |
206 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
207 | * error to the client. | |
208 | * | |
209 | * The following example shows a possible race condition of this scheme: | |
210 | * | |
211 | * consumer thread error happens | |
212 | * client cmd arrives | |
213 | * client cmd checks state -> still OK | |
214 | * consumer thread exit, sets error | |
215 | * client cmd try to talk to consumer | |
216 | * ... | |
217 | * | |
218 | * However, since the consumer is a different daemon, we have no way of making | |
219 | * sure the command will reach it safely even with this state flag. This is why | |
220 | * we consider that up to the state validation during command processing, the | |
221 | * command is safe. After that, we can not guarantee the correctness of the | |
222 | * client request vis-a-vis the consumer. | |
223 | */ | |
224 | static enum consumerd_state ust_consumerd_state; | |
225 | static enum consumerd_state kernel_consumerd_state; | |
226 | ||
ae9e45b3 DG |
227 | /* |
228 | * Socket timeout for receiving and sending in seconds. | |
229 | */ | |
230 | static int app_socket_timeout; | |
231 | ||
12744796 DG |
232 | /* Set in main() with the current page size. */ |
233 | long page_size; | |
234 | ||
8782cc74 MD |
235 | /* Application health monitoring */ |
236 | struct health_app *health_sessiond; | |
237 | ||
4d076222 DG |
238 | /* JUL TCP port for registration. Used by the JUL thread. */ |
239 | unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT; | |
240 | ||
fb09408a | 241 | static |
7753dea8 | 242 | void setup_consumerd_path(void) |
fb09408a | 243 | { |
fc7a59ce | 244 | const char *bin, *libdir; |
fb09408a | 245 | |
7753dea8 MD |
246 | /* |
247 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
248 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
249 | * has not been defined. | |
7753dea8 | 250 | */ |
ebaeda94 | 251 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
252 | if (!consumerd32_bin[0]) { |
253 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
254 | } |
255 | if (!consumerd32_libdir[0]) { | |
256 | consumerd32_libdir = INSTALL_LIB_PATH; | |
257 | } | |
258 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
259 | if (!consumerd64_bin[0]) { |
260 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 261 | } |
ebaeda94 MD |
262 | if (!consumerd64_libdir[0]) { |
263 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
264 | } |
265 | #else | |
266 | #error "Unknown bitness" | |
267 | #endif | |
268 | ||
fb09408a MD |
269 | /* |
270 | * runtime env. var. overrides the build default. | |
271 | */ | |
fc7a59ce AM |
272 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
273 | if (bin) { | |
274 | consumerd32_bin = bin; | |
7753dea8 | 275 | } |
fc7a59ce AM |
276 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
277 | if (bin) { | |
278 | consumerd64_bin = bin; | |
ebaeda94 | 279 | } |
72f579ee | 280 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
281 | if (libdir) { |
282 | consumerd32_libdir = libdir; | |
283 | } | |
72f579ee | 284 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
285 | if (libdir) { |
286 | consumerd64_libdir = libdir; | |
fb09408a MD |
287 | } |
288 | } | |
289 | ||
5eb91c98 DG |
290 | /* |
291 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
292 | */ | |
d0b96690 | 293 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) |
5eb91c98 DG |
294 | { |
295 | int ret; | |
296 | ||
d0b96690 | 297 | assert(events); |
5eb91c98 DG |
298 | |
299 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
300 | if (ret < 0) { | |
301 | goto error; | |
302 | } | |
303 | ||
304 | /* Add quit pipe */ | |
d0b96690 | 305 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
306 | if (ret < 0) { |
307 | goto error; | |
308 | } | |
309 | ||
310 | return 0; | |
311 | ||
312 | error: | |
313 | return ret; | |
314 | } | |
315 | ||
316 | /* | |
317 | * Check if the thread quit pipe was triggered. | |
318 | * | |
319 | * Return 1 if it was triggered else 0; | |
320 | */ | |
d0b96690 | 321 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
5eb91c98 DG |
322 | { |
323 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
324 | return 1; | |
325 | } | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
273ea72c | 330 | /* |
5eb91c98 | 331 | * Init thread quit pipe. |
273ea72c DG |
332 | * |
333 | * Return -1 on error or 0 if all pipes are created. | |
334 | */ | |
335 | static int init_thread_quit_pipe(void) | |
336 | { | |
730389d9 | 337 | int ret, i; |
273ea72c | 338 | |
730389d9 | 339 | ret = pipe(thread_quit_pipe); |
273ea72c | 340 | if (ret < 0) { |
730389d9 | 341 | PERROR("thread quit pipe"); |
273ea72c DG |
342 | goto error; |
343 | } | |
344 | ||
730389d9 DG |
345 | for (i = 0; i < 2; i++) { |
346 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
347 | if (ret < 0) { | |
348 | PERROR("fcntl"); | |
349 | goto error; | |
350 | } | |
351 | } | |
352 | ||
273ea72c DG |
353 | error: |
354 | return ret; | |
355 | } | |
356 | ||
099e26bd DG |
357 | /* |
358 | * Stop all threads by closing the thread quit pipe. | |
359 | */ | |
cf3af59e MD |
360 | static void stop_threads(void) |
361 | { | |
5eb91c98 DG |
362 | int ret; |
363 | ||
cf3af59e MD |
364 | /* Stopping all threads */ |
365 | DBG("Terminating all threads"); | |
54d01ffb | 366 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
367 | if (ret < 0) { |
368 | ERR("write error on thread quit pipe"); | |
369 | } | |
370 | ||
099e26bd | 371 | /* Dispatch thread */ |
26c9d55e | 372 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 373 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
374 | } |
375 | ||
e975f9f8 DG |
376 | /* |
377 | * Close every consumer sockets. | |
378 | */ | |
379 | static void close_consumer_sockets(void) | |
380 | { | |
381 | int ret; | |
382 | ||
383 | if (kconsumer_data.err_sock >= 0) { | |
384 | ret = close(kconsumer_data.err_sock); | |
385 | if (ret < 0) { | |
386 | PERROR("kernel consumer err_sock close"); | |
387 | } | |
388 | } | |
389 | if (ustconsumer32_data.err_sock >= 0) { | |
390 | ret = close(ustconsumer32_data.err_sock); | |
391 | if (ret < 0) { | |
a76cbd9f | 392 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
393 | } |
394 | } | |
395 | if (ustconsumer64_data.err_sock >= 0) { | |
396 | ret = close(ustconsumer64_data.err_sock); | |
397 | if (ret < 0) { | |
a76cbd9f | 398 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
399 | } |
400 | } | |
401 | if (kconsumer_data.cmd_sock >= 0) { | |
402 | ret = close(kconsumer_data.cmd_sock); | |
403 | if (ret < 0) { | |
404 | PERROR("kernel consumer cmd_sock close"); | |
405 | } | |
406 | } | |
407 | if (ustconsumer32_data.cmd_sock >= 0) { | |
408 | ret = close(ustconsumer32_data.cmd_sock); | |
409 | if (ret < 0) { | |
a76cbd9f | 410 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
411 | } |
412 | } | |
413 | if (ustconsumer64_data.cmd_sock >= 0) { | |
414 | ret = close(ustconsumer64_data.cmd_sock); | |
415 | if (ret < 0) { | |
a76cbd9f | 416 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
417 | } |
418 | } | |
419 | } | |
420 | ||
fac6795d | 421 | /* |
d063d709 | 422 | * Cleanup the daemon |
fac6795d | 423 | */ |
cf3af59e | 424 | static void cleanup(void) |
fac6795d | 425 | { |
ef599319 | 426 | int ret; |
af9737e9 | 427 | struct ltt_session *sess, *stmp; |
8c6c56c2 | 428 | char path[PATH_MAX]; |
fac6795d | 429 | |
1d4b027a | 430 | DBG("Cleaning up"); |
e07ae692 | 431 | |
4e449f3f MD |
432 | /* |
433 | * Close the thread quit pipe. It has already done its job, | |
434 | * since we are now called. | |
435 | */ | |
2f77fc4b DG |
436 | utils_close_pipe(thread_quit_pipe); |
437 | ||
35f90c40 DG |
438 | /* |
439 | * If opt_pidfile is undefined, the default file will be wiped when | |
440 | * removing the rundir. | |
441 | */ | |
442 | if (opt_pidfile) { | |
443 | ret = remove(opt_pidfile); | |
444 | if (ret < 0) { | |
445 | PERROR("remove pidfile %s", opt_pidfile); | |
446 | } | |
447 | } | |
448 | ||
8c6c56c2 MD |
449 | DBG("Removing sessiond and consumerd content of directory %s", rundir); |
450 | ||
451 | /* sessiond */ | |
452 | snprintf(path, PATH_MAX, | |
453 | "%s/%s", | |
454 | rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE); | |
455 | DBG("Removing %s", path); | |
456 | (void) unlink(path); | |
457 | ||
cd9290dd DG |
458 | snprintf(path, PATH_MAX, "%s/%s", rundir, |
459 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE); | |
460 | DBG("Removing %s", path); | |
461 | (void) unlink(path); | |
462 | ||
8c6c56c2 MD |
463 | /* kconsumerd */ |
464 | snprintf(path, PATH_MAX, | |
465 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
466 | rundir); | |
467 | DBG("Removing %s", path); | |
468 | (void) unlink(path); | |
469 | ||
470 | snprintf(path, PATH_MAX, | |
471 | DEFAULT_KCONSUMERD_PATH, | |
472 | rundir); | |
473 | DBG("Removing directory %s", path); | |
474 | (void) rmdir(path); | |
475 | ||
476 | /* ust consumerd 32 */ | |
477 | snprintf(path, PATH_MAX, | |
478 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
479 | rundir); | |
480 | DBG("Removing %s", path); | |
481 | (void) unlink(path); | |
482 | ||
483 | snprintf(path, PATH_MAX, | |
484 | DEFAULT_USTCONSUMERD32_PATH, | |
485 | rundir); | |
486 | DBG("Removing directory %s", path); | |
487 | (void) rmdir(path); | |
488 | ||
489 | /* ust consumerd 64 */ | |
490 | snprintf(path, PATH_MAX, | |
491 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
492 | rundir); | |
493 | DBG("Removing %s", path); | |
494 | (void) unlink(path); | |
495 | ||
496 | snprintf(path, PATH_MAX, | |
497 | DEFAULT_USTCONSUMERD64_PATH, | |
498 | rundir); | |
499 | DBG("Removing directory %s", path); | |
500 | (void) rmdir(path); | |
5461b305 | 501 | |
dcbcae3e MD |
502 | /* |
503 | * We do NOT rmdir rundir because there are other processes | |
504 | * using it, for instance lttng-relayd, which can start in | |
505 | * parallel with this teardown. | |
506 | */ | |
507 | ||
bd69add2 | 508 | free(rundir); |
5461b305 | 509 | |
99bab54f | 510 | DBG("Cleaning up all sessions"); |
fac6795d | 511 | |
b5541356 | 512 | /* Destroy session list mutex */ |
273ea72c DG |
513 | if (session_list_ptr != NULL) { |
514 | pthread_mutex_destroy(&session_list_ptr->lock); | |
515 | ||
516 | /* Cleanup ALL session */ | |
54d01ffb DG |
517 | cds_list_for_each_entry_safe(sess, stmp, |
518 | &session_list_ptr->head, list) { | |
2f77fc4b | 519 | cmd_destroy_session(sess, kernel_poll_pipe[1]); |
273ea72c DG |
520 | } |
521 | } | |
522 | ||
099e26bd | 523 | DBG("Closing all UST sockets"); |
56fff090 | 524 | ust_app_clean_list(); |
7972aab2 | 525 | buffer_reg_destroy_registries(); |
099e26bd | 526 | |
4fba7219 DG |
527 | if (is_root && !opt_no_kernel) { |
528 | DBG2("Closing kernel fd"); | |
a4b35e07 | 529 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
530 | ret = close(kernel_tracer_fd); |
531 | if (ret) { | |
532 | PERROR("close"); | |
533 | } | |
a4b35e07 | 534 | } |
2f50c8a3 | 535 | DBG("Unloading kernel modules"); |
096102bd | 536 | modprobe_remove_lttng_all(); |
2f50c8a3 | 537 | } |
2f77fc4b | 538 | |
e975f9f8 DG |
539 | close_consumer_sockets(); |
540 | ||
421cb601 | 541 | /* <fun> */ |
f56a39af | 542 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
543 | "Matthew, BEET driven development works!%c[%dm", |
544 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
545 | /* </fun> */ | |
fac6795d DG |
546 | } |
547 | ||
e065084a | 548 | /* |
d063d709 | 549 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 550 | * |
d063d709 | 551 | * Return lttcomm error code. |
e065084a DG |
552 | */ |
553 | static int send_unix_sock(int sock, void *buf, size_t len) | |
554 | { | |
555 | /* Check valid length */ | |
c617c0c6 | 556 | if (len == 0) { |
e065084a DG |
557 | return -1; |
558 | } | |
559 | ||
560 | return lttcomm_send_unix_sock(sock, buf, len); | |
561 | } | |
562 | ||
5461b305 | 563 | /* |
d063d709 | 564 | * Free memory of a command context structure. |
5461b305 | 565 | */ |
a2fb29a5 | 566 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 567 | { |
a2fb29a5 DG |
568 | DBG("Clean command context structure"); |
569 | if (*cmd_ctx) { | |
570 | if ((*cmd_ctx)->llm) { | |
571 | free((*cmd_ctx)->llm); | |
5461b305 | 572 | } |
a2fb29a5 DG |
573 | if ((*cmd_ctx)->lsm) { |
574 | free((*cmd_ctx)->lsm); | |
5461b305 | 575 | } |
a2fb29a5 DG |
576 | free(*cmd_ctx); |
577 | *cmd_ctx = NULL; | |
5461b305 DG |
578 | } |
579 | } | |
580 | ||
fac6795d | 581 | /* |
0fdd1e2c | 582 | * Notify UST applications using the shm mmap futex. |
fac6795d | 583 | */ |
0fdd1e2c | 584 | static int notify_ust_apps(int active) |
fac6795d | 585 | { |
0fdd1e2c | 586 | char *wait_shm_mmap; |
fac6795d | 587 | |
0fdd1e2c | 588 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 589 | |
0fdd1e2c DG |
590 | /* See shm.c for this call implying mmap, shm and futex calls */ |
591 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
592 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
593 | goto error; |
594 | } | |
595 | ||
0fdd1e2c DG |
596 | /* Wake waiting process */ |
597 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
598 | ||
599 | /* Apps notified successfully */ | |
600 | return 0; | |
fac6795d DG |
601 | |
602 | error: | |
0fdd1e2c | 603 | return -1; |
fac6795d DG |
604 | } |
605 | ||
e065084a | 606 | /* |
d063d709 DG |
607 | * Setup the outgoing data buffer for the response (llm) by allocating the |
608 | * right amount of memory and copying the original information from the lsm | |
609 | * structure. | |
ca95a216 | 610 | * |
d063d709 | 611 | * Return total size of the buffer pointed by buf. |
ca95a216 | 612 | */ |
5461b305 | 613 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 614 | { |
f3ed775e | 615 | int ret, buf_size; |
ca95a216 | 616 | |
f3ed775e | 617 | buf_size = size; |
5461b305 | 618 | |
ba7f0ae5 | 619 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 620 | if (cmd_ctx->llm == NULL) { |
76d7553f | 621 | PERROR("zmalloc"); |
5461b305 | 622 | ret = -ENOMEM; |
ca95a216 DG |
623 | goto error; |
624 | } | |
625 | ||
5461b305 DG |
626 | /* Copy common data */ |
627 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 628 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 629 | |
5461b305 DG |
630 | cmd_ctx->llm->data_size = size; |
631 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
632 | ||
ca95a216 DG |
633 | return buf_size; |
634 | ||
635 | error: | |
636 | return ret; | |
637 | } | |
638 | ||
7a485870 | 639 | /* |
5eb91c98 | 640 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 641 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 642 | */ |
5eb91c98 | 643 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 644 | { |
5eb91c98 | 645 | int ret; |
7a485870 DG |
646 | struct ltt_session *session; |
647 | struct ltt_kernel_channel *channel; | |
648 | ||
5eb91c98 | 649 | DBG("Updating kernel poll set"); |
7a485870 | 650 | |
54d01ffb | 651 | session_lock_list(); |
b5541356 | 652 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 653 | session_lock(session); |
7a485870 | 654 | if (session->kernel_session == NULL) { |
54d01ffb | 655 | session_unlock(session); |
7a485870 DG |
656 | continue; |
657 | } | |
7a485870 | 658 | |
54d01ffb DG |
659 | cds_list_for_each_entry(channel, |
660 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
661 | /* Add channel fd to the kernel poll set */ |
662 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
663 | if (ret < 0) { | |
54d01ffb | 664 | session_unlock(session); |
5eb91c98 DG |
665 | goto error; |
666 | } | |
667 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 668 | } |
54d01ffb | 669 | session_unlock(session); |
7a485870 | 670 | } |
54d01ffb | 671 | session_unlock_list(); |
7a485870 | 672 | |
5eb91c98 | 673 | return 0; |
7a485870 DG |
674 | |
675 | error: | |
54d01ffb | 676 | session_unlock_list(); |
7a485870 DG |
677 | return -1; |
678 | } | |
679 | ||
680 | /* | |
54d01ffb | 681 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 682 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 683 | * |
d063d709 | 684 | * Useful for CPU hotplug feature. |
7a485870 | 685 | */ |
2bdd86d4 | 686 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
687 | { |
688 | int ret = 0; | |
689 | struct ltt_session *session; | |
173af62f | 690 | struct ltt_kernel_session *ksess; |
7a485870 DG |
691 | struct ltt_kernel_channel *channel; |
692 | ||
693 | DBG("Updating kernel streams for channel fd %d", fd); | |
694 | ||
54d01ffb | 695 | session_lock_list(); |
b5541356 | 696 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 697 | session_lock(session); |
7a485870 | 698 | if (session->kernel_session == NULL) { |
54d01ffb | 699 | session_unlock(session); |
7a485870 DG |
700 | continue; |
701 | } | |
173af62f | 702 | ksess = session->kernel_session; |
d9800920 | 703 | |
173af62f | 704 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
7a485870 DG |
705 | if (channel->fd == fd) { |
706 | DBG("Channel found, updating kernel streams"); | |
707 | ret = kernel_open_channel_stream(channel); | |
708 | if (ret < 0) { | |
b3c750d2 | 709 | goto error; |
7a485870 | 710 | } |
5c786ded JD |
711 | /* Update the stream global counter */ |
712 | ksess->stream_count_global += ret; | |
d9800920 | 713 | |
7a485870 | 714 | /* |
5eb91c98 DG |
715 | * Have we already sent fds to the consumer? If yes, it means |
716 | * that tracing is started so it is safe to send our updated | |
717 | * stream fds. | |
7a485870 | 718 | */ |
173af62f DG |
719 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
720 | struct lttng_ht_iter iter; | |
721 | struct consumer_socket *socket; | |
722 | ||
e7fe706f | 723 | rcu_read_lock(); |
173af62f DG |
724 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
725 | &iter.iter, socket, node.node) { | |
173af62f | 726 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 727 | ret = kernel_consumer_send_channel_stream(socket, |
2bba9e53 DG |
728 | channel, ksess, |
729 | session->output_traces ? 1 : 0); | |
173af62f DG |
730 | pthread_mutex_unlock(socket->lock); |
731 | if (ret < 0) { | |
e7fe706f | 732 | rcu_read_unlock(); |
173af62f DG |
733 | goto error; |
734 | } | |
7a485870 | 735 | } |
e7fe706f | 736 | rcu_read_unlock(); |
7a485870 | 737 | } |
b3c750d2 | 738 | goto error; |
7a485870 DG |
739 | } |
740 | } | |
54d01ffb | 741 | session_unlock(session); |
7a485870 | 742 | } |
54d01ffb | 743 | session_unlock_list(); |
b3c750d2 | 744 | return ret; |
7a485870 | 745 | |
b3c750d2 | 746 | error: |
54d01ffb DG |
747 | session_unlock(session); |
748 | session_unlock_list(); | |
7a485870 DG |
749 | return ret; |
750 | } | |
751 | ||
487cf67c | 752 | /* |
ffe60014 DG |
753 | * For each tracing session, update newly registered apps. The session list |
754 | * lock MUST be acquired before calling this. | |
487cf67c DG |
755 | */ |
756 | static void update_ust_app(int app_sock) | |
757 | { | |
758 | struct ltt_session *sess, *stmp; | |
759 | ||
fdadac08 DG |
760 | /* Consumer is in an ERROR state. Stop any application update. */ |
761 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
762 | /* Stop the update process since the consumer is dead. */ | |
763 | return; | |
764 | } | |
765 | ||
487cf67c DG |
766 | /* For all tracing session(s) */ |
767 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 768 | session_lock(sess); |
421cb601 DG |
769 | if (sess->ust_session) { |
770 | ust_app_global_update(sess->ust_session, app_sock); | |
771 | } | |
4ee14516 | 772 | session_unlock(sess); |
487cf67c DG |
773 | } |
774 | } | |
775 | ||
7a485870 | 776 | /* |
d063d709 | 777 | * This thread manage event coming from the kernel. |
7a485870 | 778 | * |
d063d709 DG |
779 | * Features supported in this thread: |
780 | * -) CPU Hotplug | |
7a485870 DG |
781 | */ |
782 | static void *thread_manage_kernel(void *data) | |
783 | { | |
139ac872 | 784 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
6cd525e8 | 785 | ssize_t size_ret; |
5eb91c98 | 786 | uint32_t revents, nb_fd; |
7a485870 | 787 | char tmp; |
5eb91c98 | 788 | struct lttng_poll_event events; |
7a485870 | 789 | |
6993eeb3 | 790 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 791 | |
6c71277b | 792 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 793 | |
d5d63bf1 DG |
794 | /* |
795 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 796 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 797 | */ |
6d737ce4 | 798 | lttng_poll_init(&events); |
d5d63bf1 | 799 | |
6993eeb3 CB |
800 | if (testpoint(thread_manage_kernel)) { |
801 | goto error_testpoint; | |
802 | } | |
8ac94142 | 803 | |
840cb59c | 804 | health_code_update(); |
44a5e5eb | 805 | |
6993eeb3 | 806 | if (testpoint(thread_manage_kernel_before_loop)) { |
d21b0d71 | 807 | goto error_testpoint; |
6993eeb3 CB |
808 | } |
809 | ||
7a485870 | 810 | while (1) { |
840cb59c | 811 | health_code_update(); |
44a5e5eb | 812 | |
7a485870 | 813 | if (update_poll_flag == 1) { |
d21b0d71 DG |
814 | /* Clean events object. We are about to populate it again. */ |
815 | lttng_poll_clean(&events); | |
816 | ||
d0b96690 | 817 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
818 | if (ret < 0) { |
819 | goto error_poll_create; | |
820 | } | |
821 | ||
822 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
823 | if (ret < 0) { | |
824 | goto error; | |
825 | } | |
5f822d0a | 826 | |
d21b0d71 | 827 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
828 | ret = update_kernel_poll(&events); |
829 | if (ret < 0) { | |
7a485870 DG |
830 | goto error; |
831 | } | |
832 | update_poll_flag = 0; | |
833 | } | |
834 | ||
d21b0d71 | 835 | DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events)); |
7a485870 DG |
836 | |
837 | /* Poll infinite value of time */ | |
88f2b785 | 838 | restart: |
a78af745 | 839 | health_poll_entry(); |
5eb91c98 | 840 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 841 | health_poll_exit(); |
7a485870 | 842 | if (ret < 0) { |
88f2b785 MD |
843 | /* |
844 | * Restart interrupted system call. | |
845 | */ | |
846 | if (errno == EINTR) { | |
847 | goto restart; | |
848 | } | |
7a485870 DG |
849 | goto error; |
850 | } else if (ret == 0) { | |
851 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
852 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
853 | "This should not have happened! Continuing..."); | |
7a485870 DG |
854 | continue; |
855 | } | |
856 | ||
0d9c5d77 DG |
857 | nb_fd = ret; |
858 | ||
5eb91c98 DG |
859 | for (i = 0; i < nb_fd; i++) { |
860 | /* Fetch once the poll data */ | |
861 | revents = LTTNG_POLL_GETEV(&events, i); | |
862 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 863 | |
840cb59c | 864 | health_code_update(); |
44a5e5eb | 865 | |
5eb91c98 | 866 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 867 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 868 | if (ret) { |
139ac872 MD |
869 | err = 0; |
870 | goto exit; | |
5eb91c98 | 871 | } |
7a485870 | 872 | |
5eb91c98 DG |
873 | /* Check for data on kernel pipe */ |
874 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
6cd525e8 MD |
875 | (void) lttng_read(kernel_poll_pipe[0], |
876 | &tmp, 1); | |
f921c78f DG |
877 | /* |
878 | * Ret value is useless here, if this pipe gets any actions an | |
879 | * update is required anyway. | |
880 | */ | |
5eb91c98 DG |
881 | update_poll_flag = 1; |
882 | continue; | |
883 | } else { | |
884 | /* | |
885 | * New CPU detected by the kernel. Adding kernel stream to | |
886 | * kernel session and updating the kernel consumer | |
887 | */ | |
888 | if (revents & LPOLLIN) { | |
2bdd86d4 | 889 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
890 | if (ret < 0) { |
891 | continue; | |
892 | } | |
893 | break; | |
894 | /* | |
895 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
896 | * and unregister kernel stream at this point. | |
897 | */ | |
7a485870 | 898 | } |
7a485870 DG |
899 | } |
900 | } | |
901 | } | |
902 | ||
139ac872 | 903 | exit: |
7a485870 | 904 | error: |
5eb91c98 | 905 | lttng_poll_clean(&events); |
76d7553f | 906 | error_poll_create: |
6993eeb3 | 907 | error_testpoint: |
6620da75 DG |
908 | utils_close_pipe(kernel_poll_pipe); |
909 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 910 | if (err) { |
840cb59c | 911 | health_error(); |
139ac872 | 912 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
913 | WARN("Kernel thread died unexpectedly. " |
914 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 915 | } |
8782cc74 | 916 | health_unregister(health_sessiond); |
76d7553f | 917 | DBG("Kernel thread dying"); |
7a485870 DG |
918 | return NULL; |
919 | } | |
920 | ||
a23ec3a7 DG |
921 | /* |
922 | * Signal pthread condition of the consumer data that the thread. | |
923 | */ | |
924 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
925 | { | |
926 | pthread_mutex_lock(&data->cond_mutex); | |
927 | ||
928 | /* | |
929 | * The state is set before signaling. It can be any value, it's the waiter | |
930 | * job to correctly interpret this condition variable associated to the | |
931 | * consumer pthread_cond. | |
932 | * | |
933 | * A value of 0 means that the corresponding thread of the consumer data | |
934 | * was not started. 1 indicates that the thread has started and is ready | |
935 | * for action. A negative value means that there was an error during the | |
936 | * thread bootstrap. | |
937 | */ | |
938 | data->consumer_thread_is_ready = state; | |
939 | (void) pthread_cond_signal(&data->cond); | |
940 | ||
941 | pthread_mutex_unlock(&data->cond_mutex); | |
942 | } | |
943 | ||
1d4b027a | 944 | /* |
3bd1e081 | 945 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 946 | */ |
3bd1e081 | 947 | static void *thread_manage_consumer(void *data) |
1d4b027a | 948 | { |
139ac872 | 949 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 | 950 | uint32_t revents, nb_fd; |
1d4b027a | 951 | enum lttcomm_return_code code; |
5eb91c98 | 952 | struct lttng_poll_event events; |
3bd1e081 | 953 | struct consumer_data *consumer_data = data; |
1d4b027a | 954 | |
3bd1e081 | 955 | DBG("[thread] Manage consumer started"); |
1d4b027a | 956 | |
6c71277b | 957 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 958 | |
855060f8 | 959 | health_code_update(); |
9449cc75 | 960 | |
5eb91c98 | 961 | /* |
331744e3 JD |
962 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
963 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 964 | */ |
331744e3 | 965 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 966 | if (ret < 0) { |
76d7553f | 967 | goto error_poll; |
5eb91c98 | 968 | } |
273ea72c | 969 | |
edb8b045 DG |
970 | /* |
971 | * The error socket here is already in a listening state which was done | |
972 | * just before spawning this thread to avoid a race between the consumer | |
973 | * daemon exec trying to connect and the listen() call. | |
974 | */ | |
3bd1e081 | 975 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
976 | if (ret < 0) { |
977 | goto error; | |
978 | } | |
979 | ||
840cb59c | 980 | health_code_update(); |
44a5e5eb | 981 | |
331744e3 | 982 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 983 | restart: |
a78af745 | 984 | health_poll_entry(); |
8ac94142 | 985 | |
6993eeb3 CB |
986 | if (testpoint(thread_manage_consumer)) { |
987 | goto error; | |
988 | } | |
8ac94142 | 989 | |
5eb91c98 | 990 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 991 | health_poll_exit(); |
273ea72c | 992 | if (ret < 0) { |
88f2b785 MD |
993 | /* |
994 | * Restart interrupted system call. | |
995 | */ | |
996 | if (errno == EINTR) { | |
997 | goto restart; | |
998 | } | |
273ea72c DG |
999 | goto error; |
1000 | } | |
1001 | ||
0d9c5d77 DG |
1002 | nb_fd = ret; |
1003 | ||
5eb91c98 DG |
1004 | for (i = 0; i < nb_fd; i++) { |
1005 | /* Fetch once the poll data */ | |
1006 | revents = LTTNG_POLL_GETEV(&events, i); | |
1007 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1008 | ||
840cb59c | 1009 | health_code_update(); |
44a5e5eb | 1010 | |
5eb91c98 | 1011 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1012 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1013 | if (ret) { |
139ac872 MD |
1014 | err = 0; |
1015 | goto exit; | |
5eb91c98 DG |
1016 | } |
1017 | ||
1018 | /* Event on the registration socket */ | |
3bd1e081 | 1019 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 1020 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 1021 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
1022 | goto error; |
1023 | } | |
1024 | } | |
273ea72c DG |
1025 | } |
1026 | ||
3bd1e081 | 1027 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
1028 | if (sock < 0) { |
1029 | goto error; | |
1030 | } | |
1031 | ||
b662582b DG |
1032 | /* |
1033 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1034 | * show must go on. | |
1035 | */ | |
1036 | (void) utils_set_fd_cloexec(sock); | |
1037 | ||
840cb59c | 1038 | health_code_update(); |
44a5e5eb | 1039 | |
3bd1e081 | 1040 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 1041 | |
712ea556 | 1042 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
1043 | ret = lttcomm_recv_unix_sock(sock, &code, |
1044 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
1045 | if (ret <= 0) { |
1046 | goto error; | |
1047 | } | |
1048 | ||
840cb59c | 1049 | health_code_update(); |
44a5e5eb | 1050 | |
f73fabfd | 1051 | if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
331744e3 | 1052 | /* Connect both socket, command and metadata. */ |
3bd1e081 MD |
1053 | consumer_data->cmd_sock = |
1054 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
4ce514c4 | 1055 | consumer_data->metadata_fd = |
331744e3 | 1056 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); |
92db7cdc DG |
1057 | if (consumer_data->cmd_sock < 0 |
1058 | || consumer_data->metadata_fd < 0) { | |
331744e3 | 1059 | PERROR("consumer connect cmd socket"); |
a23ec3a7 DG |
1060 | /* On error, signal condition and quit. */ |
1061 | signal_consumer_condition(consumer_data, -1); | |
1d4b027a DG |
1062 | goto error; |
1063 | } | |
9363801e | 1064 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; |
331744e3 JD |
1065 | /* Create metadata socket lock. */ |
1066 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
1067 | if (consumer_data->metadata_sock.lock == NULL) { | |
1068 | PERROR("zmalloc pthread mutex"); | |
1069 | ret = -1; | |
1070 | goto error; | |
1071 | } | |
1072 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
1073 | ||
a23ec3a7 | 1074 | signal_consumer_condition(consumer_data, 1); |
331744e3 JD |
1075 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); |
1076 | DBG("Consumer metadata socket ready (fd: %d)", | |
4ce514c4 | 1077 | consumer_data->metadata_fd); |
1d4b027a | 1078 | } else { |
3bd1e081 | 1079 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1080 | lttcomm_get_readable_code(-code)); |
1081 | goto error; | |
1082 | } | |
1083 | ||
331744e3 | 1084 | /* Remove the consumerd error sock since we've established a connexion */ |
3bd1e081 | 1085 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1086 | if (ret < 0) { |
72079cae DG |
1087 | goto error; |
1088 | } | |
1089 | ||
331744e3 | 1090 | /* Add new accepted error socket. */ |
5eb91c98 DG |
1091 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1092 | if (ret < 0) { | |
72079cae | 1093 | goto error; |
5eb91c98 DG |
1094 | } |
1095 | ||
331744e3 | 1096 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 1097 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
1098 | LPOLLIN | LPOLLRDHUP); |
1099 | if (ret < 0) { | |
1100 | goto error; | |
1101 | } | |
1102 | ||
840cb59c | 1103 | health_code_update(); |
44a5e5eb | 1104 | |
331744e3 | 1105 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1106 | restart_poll: |
331744e3 JD |
1107 | while (1) { |
1108 | health_poll_entry(); | |
1109 | ret = lttng_poll_wait(&events, -1); | |
1110 | health_poll_exit(); | |
1111 | if (ret < 0) { | |
1112 | /* | |
1113 | * Restart interrupted system call. | |
1114 | */ | |
1115 | if (errno == EINTR) { | |
1116 | goto restart_poll; | |
1117 | } | |
1118 | goto error; | |
88f2b785 | 1119 | } |
72079cae | 1120 | |
331744e3 | 1121 | nb_fd = ret; |
0d9c5d77 | 1122 | |
331744e3 JD |
1123 | for (i = 0; i < nb_fd; i++) { |
1124 | /* Fetch once the poll data */ | |
1125 | revents = LTTNG_POLL_GETEV(&events, i); | |
1126 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 1127 | |
331744e3 | 1128 | health_code_update(); |
44a5e5eb | 1129 | |
331744e3 JD |
1130 | /* Thread quit pipe has been closed. Killing thread. */ |
1131 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
1132 | if (ret) { | |
1133 | err = 0; | |
1134 | goto exit; | |
1135 | } | |
5eb91c98 | 1136 | |
331744e3 JD |
1137 | if (pollfd == sock) { |
1138 | /* Event on the consumerd socket */ | |
1139 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1140 | ERR("consumer err socket second poll error"); | |
1141 | goto error; | |
1142 | } | |
1143 | health_code_update(); | |
1144 | /* Wait for any kconsumerd error */ | |
1145 | ret = lttcomm_recv_unix_sock(sock, &code, | |
1146 | sizeof(enum lttcomm_return_code)); | |
1147 | if (ret <= 0) { | |
1148 | ERR("consumer closed the command socket"); | |
1149 | goto error; | |
1150 | } | |
1151 | ||
1152 | ERR("consumer return code : %s", | |
1153 | lttcomm_get_readable_code(-code)); | |
1154 | ||
1155 | goto exit; | |
4ce514c4 | 1156 | } else if (pollfd == consumer_data->metadata_fd) { |
331744e3 JD |
1157 | /* UST metadata requests */ |
1158 | ret = ust_consumer_metadata_request( | |
1159 | &consumer_data->metadata_sock); | |
1160 | if (ret < 0) { | |
1161 | ERR("Handling metadata request"); | |
1162 | goto error; | |
1163 | } | |
1164 | break; | |
1165 | } else { | |
1166 | ERR("Unknown pollfd"); | |
5eb91c98 DG |
1167 | goto error; |
1168 | } | |
1169 | } | |
331744e3 | 1170 | health_code_update(); |
5eb91c98 DG |
1171 | } |
1172 | ||
139ac872 | 1173 | exit: |
1d4b027a | 1174 | error: |
fdadac08 DG |
1175 | /* |
1176 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1177 | * thread might be using them so get exclusive access which will abort all |
1178 | * other consumer command by other threads. | |
fdadac08 DG |
1179 | */ |
1180 | pthread_mutex_lock(&consumer_data->lock); | |
1181 | ||
5c827ce0 DG |
1182 | /* Immediately set the consumerd state to stopped */ |
1183 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1184 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1185 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1186 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1187 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1188 | } else { | |
1189 | /* Code flow error... */ | |
1190 | assert(0); | |
1191 | } | |
1192 | ||
76d7553f MD |
1193 | if (consumer_data->err_sock >= 0) { |
1194 | ret = close(consumer_data->err_sock); | |
1195 | if (ret) { | |
1196 | PERROR("close"); | |
1197 | } | |
a76cbd9f | 1198 | consumer_data->err_sock = -1; |
76d7553f MD |
1199 | } |
1200 | if (consumer_data->cmd_sock >= 0) { | |
1201 | ret = close(consumer_data->cmd_sock); | |
1202 | if (ret) { | |
1203 | PERROR("close"); | |
1204 | } | |
a76cbd9f | 1205 | consumer_data->cmd_sock = -1; |
76d7553f | 1206 | } |
9363801e DG |
1207 | if (*consumer_data->metadata_sock.fd_ptr >= 0) { |
1208 | ret = close(*consumer_data->metadata_sock.fd_ptr); | |
331744e3 JD |
1209 | if (ret) { |
1210 | PERROR("close"); | |
1211 | } | |
1212 | } | |
331744e3 | 1213 | |
76d7553f MD |
1214 | if (sock >= 0) { |
1215 | ret = close(sock); | |
1216 | if (ret) { | |
1217 | PERROR("close"); | |
1218 | } | |
1219 | } | |
273ea72c | 1220 | |
3bd1e081 MD |
1221 | unlink(consumer_data->err_unix_sock_path); |
1222 | unlink(consumer_data->cmd_unix_sock_path); | |
1223 | consumer_data->pid = 0; | |
fdadac08 | 1224 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1225 | |
fdadac08 DG |
1226 | /* Cleanup metadata socket mutex. */ |
1227 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1228 | free(consumer_data->metadata_sock.lock); | |
1d4b027a | 1229 | |
5eb91c98 | 1230 | lttng_poll_clean(&events); |
76d7553f | 1231 | error_poll: |
139ac872 | 1232 | if (err) { |
840cb59c | 1233 | health_error(); |
139ac872 MD |
1234 | ERR("Health error occurred in %s", __func__); |
1235 | } | |
8782cc74 | 1236 | health_unregister(health_sessiond); |
76d7553f | 1237 | DBG("consumer thread cleanup completed"); |
0177d773 | 1238 | |
5eb91c98 | 1239 | return NULL; |
099e26bd DG |
1240 | } |
1241 | ||
099e26bd DG |
1242 | /* |
1243 | * This thread manage application communication. | |
1d4b027a DG |
1244 | */ |
1245 | static void *thread_manage_apps(void *data) | |
099e26bd | 1246 | { |
139ac872 | 1247 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1248 | ssize_t size_ret; |
5eb91c98 | 1249 | uint32_t revents, nb_fd; |
5eb91c98 | 1250 | struct lttng_poll_event events; |
099e26bd DG |
1251 | |
1252 | DBG("[thread] Manage application started"); | |
1253 | ||
f6a9efaa DG |
1254 | rcu_register_thread(); |
1255 | rcu_thread_online(); | |
1256 | ||
6c71277b | 1257 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1258 | |
6993eeb3 CB |
1259 | if (testpoint(thread_manage_apps)) { |
1260 | goto error_testpoint; | |
1261 | } | |
1262 | ||
840cb59c | 1263 | health_code_update(); |
44a5e5eb | 1264 | |
d0b96690 | 1265 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1266 | if (ret < 0) { |
76d7553f | 1267 | goto error_poll_create; |
5eb91c98 | 1268 | } |
099e26bd | 1269 | |
5eb91c98 DG |
1270 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1271 | if (ret < 0) { | |
1272 | goto error; | |
1273 | } | |
099e26bd | 1274 | |
6993eeb3 CB |
1275 | if (testpoint(thread_manage_apps_before_loop)) { |
1276 | goto error; | |
1277 | } | |
8ac94142 | 1278 | |
840cb59c | 1279 | health_code_update(); |
44a5e5eb | 1280 | |
5eb91c98 | 1281 | while (1) { |
d21b0d71 | 1282 | DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events)); |
099e26bd DG |
1283 | |
1284 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1285 | restart: |
a78af745 | 1286 | health_poll_entry(); |
5eb91c98 | 1287 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1288 | health_poll_exit(); |
099e26bd | 1289 | if (ret < 0) { |
88f2b785 MD |
1290 | /* |
1291 | * Restart interrupted system call. | |
1292 | */ | |
1293 | if (errno == EINTR) { | |
1294 | goto restart; | |
1295 | } | |
099e26bd DG |
1296 | goto error; |
1297 | } | |
1298 | ||
0d9c5d77 DG |
1299 | nb_fd = ret; |
1300 | ||
5eb91c98 DG |
1301 | for (i = 0; i < nb_fd; i++) { |
1302 | /* Fetch once the poll data */ | |
1303 | revents = LTTNG_POLL_GETEV(&events, i); | |
1304 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1305 | ||
840cb59c | 1306 | health_code_update(); |
44a5e5eb | 1307 | |
5eb91c98 | 1308 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1309 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1310 | if (ret) { |
139ac872 MD |
1311 | err = 0; |
1312 | goto exit; | |
5eb91c98 | 1313 | } |
099e26bd | 1314 | |
5eb91c98 DG |
1315 | /* Inspect the apps cmd pipe */ |
1316 | if (pollfd == apps_cmd_pipe[0]) { | |
1317 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1318 | ERR("Apps command pipe error"); | |
0177d773 | 1319 | goto error; |
5eb91c98 | 1320 | } else if (revents & LPOLLIN) { |
d0b96690 DG |
1321 | int sock; |
1322 | ||
5eb91c98 | 1323 | /* Empty pipe */ |
6cd525e8 MD |
1324 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1325 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1326 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1327 | goto error; |
1328 | } | |
099e26bd | 1329 | |
840cb59c | 1330 | health_code_update(); |
44a5e5eb | 1331 | |
ffe60014 | 1332 | /* |
d0b96690 DG |
1333 | * We only monitor the error events of the socket. This |
1334 | * thread does not handle any incoming data from UST | |
1335 | * (POLLIN). | |
ffe60014 | 1336 | */ |
d0b96690 DG |
1337 | ret = lttng_poll_add(&events, sock, |
1338 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1339 | if (ret < 0) { | |
5eb91c98 | 1340 | goto error; |
e0c7ec2b | 1341 | } |
acc7b41b | 1342 | |
d0b96690 | 1343 | DBG("Apps with sock %d added to poll set", sock); |
487cf67c | 1344 | |
840cb59c | 1345 | health_code_update(); |
44a5e5eb | 1346 | |
5eb91c98 | 1347 | break; |
0177d773 | 1348 | } |
5eb91c98 DG |
1349 | } else { |
1350 | /* | |
54d01ffb DG |
1351 | * At this point, we know that a registered application made |
1352 | * the event at poll_wait. | |
5eb91c98 DG |
1353 | */ |
1354 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1355 | /* Removing from the poll set */ | |
1356 | ret = lttng_poll_del(&events, pollfd); | |
1357 | if (ret < 0) { | |
1358 | goto error; | |
1359 | } | |
099e26bd | 1360 | |
b9d9b220 | 1361 | /* Socket closed on remote end. */ |
56fff090 | 1362 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1363 | break; |
1364 | } | |
099e26bd | 1365 | } |
44a5e5eb | 1366 | |
840cb59c | 1367 | health_code_update(); |
099e26bd | 1368 | } |
099e26bd DG |
1369 | } |
1370 | ||
139ac872 | 1371 | exit: |
099e26bd | 1372 | error: |
5eb91c98 | 1373 | lttng_poll_clean(&events); |
76d7553f | 1374 | error_poll_create: |
6993eeb3 | 1375 | error_testpoint: |
6620da75 DG |
1376 | utils_close_pipe(apps_cmd_pipe); |
1377 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1378 | ||
1379 | /* | |
1380 | * We don't clean the UST app hash table here since already registered | |
1381 | * applications can still be controlled so let them be until the session | |
1382 | * daemon dies or the applications stop. | |
1383 | */ | |
1384 | ||
139ac872 | 1385 | if (err) { |
840cb59c | 1386 | health_error(); |
139ac872 MD |
1387 | ERR("Health error occurred in %s", __func__); |
1388 | } | |
8782cc74 | 1389 | health_unregister(health_sessiond); |
76d7553f | 1390 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1391 | rcu_thread_offline(); |
1392 | rcu_unregister_thread(); | |
099e26bd DG |
1393 | return NULL; |
1394 | } | |
1395 | ||
d0b96690 | 1396 | /* |
d88aee68 DG |
1397 | * Send a socket to a thread This is called from the dispatch UST registration |
1398 | * thread once all sockets are set for the application. | |
d0b96690 | 1399 | * |
b85dc84c DG |
1400 | * The sock value can be invalid, we don't really care, the thread will handle |
1401 | * it and make the necessary cleanup if so. | |
1402 | * | |
d0b96690 DG |
1403 | * On success, return 0 else a negative value being the errno message of the |
1404 | * write(). | |
1405 | */ | |
d88aee68 | 1406 | static int send_socket_to_thread(int fd, int sock) |
d0b96690 | 1407 | { |
6cd525e8 | 1408 | ssize_t ret; |
d0b96690 | 1409 | |
b85dc84c DG |
1410 | /* |
1411 | * It's possible that the FD is set as invalid with -1 concurrently just | |
1412 | * before calling this function being a shutdown state of the thread. | |
1413 | */ | |
1414 | if (fd < 0) { | |
1415 | ret = -EBADF; | |
1416 | goto error; | |
1417 | } | |
d0b96690 | 1418 | |
6cd525e8 MD |
1419 | ret = lttng_write(fd, &sock, sizeof(sock)); |
1420 | if (ret < sizeof(sock)) { | |
d88aee68 | 1421 | PERROR("write apps pipe %d", fd); |
d0b96690 DG |
1422 | if (ret < 0) { |
1423 | ret = -errno; | |
1424 | } | |
1425 | goto error; | |
1426 | } | |
1427 | ||
1428 | /* All good. Don't send back the write positive ret value. */ | |
1429 | ret = 0; | |
1430 | error: | |
6cd525e8 | 1431 | return (int) ret; |
d0b96690 DG |
1432 | } |
1433 | ||
f45e313d DG |
1434 | /* |
1435 | * Sanitize the wait queue of the dispatch registration thread meaning removing | |
1436 | * invalid nodes from it. This is to avoid memory leaks for the case the UST | |
1437 | * notify socket is never received. | |
1438 | */ | |
1439 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) | |
1440 | { | |
1441 | int ret, nb_fd = 0, i; | |
1442 | unsigned int fd_added = 0; | |
1443 | struct lttng_poll_event events; | |
1444 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; | |
1445 | ||
1446 | assert(wait_queue); | |
1447 | ||
1448 | lttng_poll_init(&events); | |
1449 | ||
1450 | /* Just skip everything for an empty queue. */ | |
1451 | if (!wait_queue->count) { | |
1452 | goto end; | |
1453 | } | |
1454 | ||
1455 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); | |
1456 | if (ret < 0) { | |
1457 | goto error_create; | |
1458 | } | |
1459 | ||
1460 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1461 | &wait_queue->head, head) { | |
1462 | assert(wait_node->app); | |
1463 | ret = lttng_poll_add(&events, wait_node->app->sock, | |
1464 | LPOLLHUP | LPOLLERR); | |
1465 | if (ret < 0) { | |
1466 | goto error; | |
1467 | } | |
1468 | ||
1469 | fd_added = 1; | |
1470 | } | |
1471 | ||
1472 | if (!fd_added) { | |
1473 | goto end; | |
1474 | } | |
1475 | ||
1476 | /* | |
1477 | * Poll but don't block so we can quickly identify the faulty events and | |
1478 | * clean them afterwards from the wait queue. | |
1479 | */ | |
1480 | ret = lttng_poll_wait(&events, 0); | |
1481 | if (ret < 0) { | |
1482 | goto error; | |
1483 | } | |
1484 | nb_fd = ret; | |
1485 | ||
1486 | for (i = 0; i < nb_fd; i++) { | |
1487 | /* Get faulty FD. */ | |
1488 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
1489 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
1490 | ||
1491 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1492 | &wait_queue->head, head) { | |
1493 | if (pollfd == wait_node->app->sock && | |
1494 | (revents & (LPOLLHUP | LPOLLERR))) { | |
1495 | cds_list_del(&wait_node->head); | |
1496 | wait_queue->count--; | |
1497 | ust_app_destroy(wait_node->app); | |
1498 | free(wait_node); | |
1499 | break; | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | if (nb_fd > 0) { | |
1505 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); | |
1506 | } | |
1507 | ||
1508 | end: | |
1509 | lttng_poll_clean(&events); | |
1510 | return; | |
1511 | ||
1512 | error: | |
1513 | lttng_poll_clean(&events); | |
1514 | error_create: | |
1515 | ERR("Unable to sanitize wait queue"); | |
1516 | return; | |
1517 | } | |
1518 | ||
099e26bd DG |
1519 | /* |
1520 | * Dispatch request from the registration threads to the application | |
1521 | * communication thread. | |
1522 | */ | |
1523 | static void *thread_dispatch_ust_registration(void *data) | |
1524 | { | |
12e2b881 | 1525 | int ret, err = -1; |
099e26bd DG |
1526 | struct cds_wfq_node *node; |
1527 | struct ust_command *ust_cmd = NULL; | |
f45e313d DG |
1528 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
1529 | struct ust_reg_wait_queue wait_queue = { | |
1530 | .count = 0, | |
1531 | }; | |
d0b96690 | 1532 | |
6c71277b | 1533 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
12e2b881 MD |
1534 | |
1535 | health_code_update(); | |
1536 | ||
f45e313d | 1537 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
099e26bd DG |
1538 | |
1539 | DBG("[thread] Dispatch UST command started"); | |
1540 | ||
26c9d55e | 1541 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
12e2b881 MD |
1542 | health_code_update(); |
1543 | ||
099e26bd DG |
1544 | /* Atomically prepare the queue futex */ |
1545 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1546 | ||
1547 | do { | |
d0b96690 | 1548 | struct ust_app *app = NULL; |
7972aab2 | 1549 | ust_cmd = NULL; |
d0b96690 | 1550 | |
f45e313d DG |
1551 | /* |
1552 | * Make sure we don't have node(s) that have hung up before receiving | |
1553 | * the notify socket. This is to clean the list in order to avoid | |
1554 | * memory leaks from notify socket that are never seen. | |
1555 | */ | |
1556 | sanitize_wait_queue(&wait_queue); | |
1557 | ||
12e2b881 | 1558 | health_code_update(); |
099e26bd DG |
1559 | /* Dequeue command for registration */ |
1560 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1561 | if (node == NULL) { | |
00a17c97 | 1562 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1563 | /* Continue thread execution */ |
1564 | break; | |
1565 | } | |
1566 | ||
1567 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1568 | ||
2f50c8a3 DG |
1569 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1570 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1571 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1572 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1573 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1574 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1575 | |
1576 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1577 | wait_node = zmalloc(sizeof(*wait_node)); | |
1578 | if (!wait_node) { | |
1579 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1580 | ret = close(ust_cmd->sock); |
1581 | if (ret < 0) { | |
1582 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1583 | } | |
51dec90d | 1584 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1585 | free(ust_cmd); |
d0b96690 DG |
1586 | goto error; |
1587 | } | |
1588 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1589 | ||
1590 | /* Create application object if socket is CMD. */ | |
1591 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1592 | ust_cmd->sock); | |
1593 | if (!wait_node->app) { | |
1594 | ret = close(ust_cmd->sock); | |
1595 | if (ret < 0) { | |
1596 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1597 | } |
51dec90d | 1598 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1599 | free(wait_node); |
7972aab2 | 1600 | free(ust_cmd); |
d0b96690 DG |
1601 | continue; |
1602 | } | |
1603 | /* | |
1604 | * Add application to the wait queue so we can set the notify | |
1605 | * socket before putting this object in the global ht. | |
1606 | */ | |
f45e313d DG |
1607 | cds_list_add(&wait_node->head, &wait_queue.head); |
1608 | wait_queue.count++; | |
d0b96690 | 1609 | |
7972aab2 | 1610 | free(ust_cmd); |
d0b96690 DG |
1611 | /* |
1612 | * We have to continue here since we don't have the notify | |
1613 | * socket and the application MUST be added to the hash table | |
1614 | * only at that moment. | |
1615 | */ | |
1616 | continue; | |
1617 | } else { | |
1618 | /* | |
1619 | * Look for the application in the local wait queue and set the | |
1620 | * notify socket if found. | |
1621 | */ | |
d88aee68 | 1622 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1623 | &wait_queue.head, head) { |
12e2b881 | 1624 | health_code_update(); |
d0b96690 DG |
1625 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1626 | wait_node->app->notify_sock = ust_cmd->sock; | |
1627 | cds_list_del(&wait_node->head); | |
f45e313d | 1628 | wait_queue.count--; |
d0b96690 DG |
1629 | app = wait_node->app; |
1630 | free(wait_node); | |
1631 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1632 | break; | |
1633 | } | |
1634 | } | |
020d7f60 DG |
1635 | |
1636 | /* | |
1637 | * With no application at this stage the received socket is | |
1638 | * basically useless so close it before we free the cmd data | |
1639 | * structure for good. | |
1640 | */ | |
1641 | if (!app) { | |
1642 | ret = close(ust_cmd->sock); | |
1643 | if (ret < 0) { | |
1644 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1645 | } | |
51dec90d | 1646 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1647 | } |
7972aab2 | 1648 | free(ust_cmd); |
d0b96690 DG |
1649 | } |
1650 | ||
1651 | if (app) { | |
d0b96690 DG |
1652 | /* |
1653 | * @session_lock_list | |
1654 | * | |
1655 | * Lock the global session list so from the register up to the | |
1656 | * registration done message, no thread can see the application | |
1657 | * and change its state. | |
1658 | */ | |
1659 | session_lock_list(); | |
1660 | rcu_read_lock(); | |
d88aee68 | 1661 | |
d0b96690 DG |
1662 | /* |
1663 | * Add application to the global hash table. This needs to be | |
1664 | * done before the update to the UST registry can locate the | |
1665 | * application. | |
1666 | */ | |
1667 | ust_app_add(app); | |
d88aee68 DG |
1668 | |
1669 | /* Set app version. This call will print an error if needed. */ | |
1670 | (void) ust_app_version(app); | |
1671 | ||
1672 | /* Send notify socket through the notify pipe. */ | |
1673 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
1674 | app->notify_sock); | |
1675 | if (ret < 0) { | |
1676 | rcu_read_unlock(); | |
1677 | session_unlock_list(); | |
b85dc84c DG |
1678 | /* |
1679 | * No notify thread, stop the UST tracing. However, this is | |
1680 | * not an internal error of the this thread thus setting | |
1681 | * the health error code to a normal exit. | |
1682 | */ | |
1683 | err = 0; | |
d88aee68 | 1684 | goto error; |
6620da75 | 1685 | } |
d88aee68 | 1686 | |
d0b96690 DG |
1687 | /* |
1688 | * Update newly registered application with the tracing | |
1689 | * registry info already enabled information. | |
1690 | */ | |
1691 | update_ust_app(app->sock); | |
d88aee68 DG |
1692 | |
1693 | /* | |
1694 | * Don't care about return value. Let the manage apps threads | |
1695 | * handle app unregistration upon socket close. | |
1696 | */ | |
1697 | (void) ust_app_register_done(app->sock); | |
1698 | ||
1699 | /* | |
1700 | * Even if the application socket has been closed, send the app | |
1701 | * to the thread and unregistration will take place at that | |
1702 | * place. | |
1703 | */ | |
1704 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 1705 | if (ret < 0) { |
d88aee68 DG |
1706 | rcu_read_unlock(); |
1707 | session_unlock_list(); | |
b85dc84c DG |
1708 | /* |
1709 | * No apps. thread, stop the UST tracing. However, this is | |
1710 | * not an internal error of the this thread thus setting | |
1711 | * the health error code to a normal exit. | |
1712 | */ | |
1713 | err = 0; | |
d88aee68 | 1714 | goto error; |
d0b96690 | 1715 | } |
d88aee68 | 1716 | |
d0b96690 DG |
1717 | rcu_read_unlock(); |
1718 | session_unlock_list(); | |
099e26bd | 1719 | } |
099e26bd DG |
1720 | } while (node != NULL); |
1721 | ||
12e2b881 | 1722 | health_poll_entry(); |
099e26bd DG |
1723 | /* Futex wait on queue. Blocking call on futex() */ |
1724 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 1725 | health_poll_exit(); |
099e26bd | 1726 | } |
12e2b881 MD |
1727 | /* Normal exit, no error */ |
1728 | err = 0; | |
099e26bd DG |
1729 | |
1730 | error: | |
d88aee68 DG |
1731 | /* Clean up wait queue. */ |
1732 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 1733 | &wait_queue.head, head) { |
d88aee68 | 1734 | cds_list_del(&wait_node->head); |
f45e313d | 1735 | wait_queue.count--; |
d88aee68 DG |
1736 | free(wait_node); |
1737 | } | |
1738 | ||
099e26bd | 1739 | DBG("Dispatch thread dying"); |
12e2b881 MD |
1740 | if (err) { |
1741 | health_error(); | |
1742 | ERR("Health error occurred in %s", __func__); | |
1743 | } | |
8782cc74 | 1744 | health_unregister(health_sessiond); |
099e26bd DG |
1745 | return NULL; |
1746 | } | |
1747 | ||
1748 | /* | |
1749 | * This thread manage application registration. | |
1750 | */ | |
1751 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1752 | { |
139ac872 | 1753 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1754 | uint32_t revents, nb_fd; |
1755 | struct lttng_poll_event events; | |
099e26bd DG |
1756 | /* |
1757 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1758 | * freed in the manage apps thread. | |
1759 | */ | |
1760 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1761 | |
099e26bd | 1762 | DBG("[thread] Manage application registration started"); |
1d4b027a | 1763 | |
6c71277b | 1764 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 1765 | |
6993eeb3 CB |
1766 | if (testpoint(thread_registration_apps)) { |
1767 | goto error_testpoint; | |
1768 | } | |
8ac94142 | 1769 | |
1d4b027a DG |
1770 | ret = lttcomm_listen_unix_sock(apps_sock); |
1771 | if (ret < 0) { | |
76d7553f | 1772 | goto error_listen; |
1d4b027a DG |
1773 | } |
1774 | ||
5eb91c98 DG |
1775 | /* |
1776 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1777 | * more will be added to this poll set. | |
1778 | */ | |
d0b96690 | 1779 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1780 | if (ret < 0) { |
76d7553f | 1781 | goto error_create_poll; |
5eb91c98 | 1782 | } |
273ea72c | 1783 | |
5eb91c98 DG |
1784 | /* Add the application registration socket */ |
1785 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1786 | if (ret < 0) { | |
76d7553f | 1787 | goto error_poll_add; |
5eb91c98 | 1788 | } |
273ea72c | 1789 | |
1d4b027a | 1790 | /* Notify all applications to register */ |
0fdd1e2c DG |
1791 | ret = notify_ust_apps(1); |
1792 | if (ret < 0) { | |
1793 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1794 | "Execution continues but there might be problem for already\n" |
1795 | "running applications that wishes to register."); | |
0fdd1e2c | 1796 | } |
1d4b027a DG |
1797 | |
1798 | while (1) { | |
1799 | DBG("Accepting application registration"); | |
273ea72c DG |
1800 | |
1801 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1802 | restart: |
a78af745 | 1803 | health_poll_entry(); |
5eb91c98 | 1804 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1805 | health_poll_exit(); |
273ea72c | 1806 | if (ret < 0) { |
88f2b785 MD |
1807 | /* |
1808 | * Restart interrupted system call. | |
1809 | */ | |
1810 | if (errno == EINTR) { | |
1811 | goto restart; | |
1812 | } | |
273ea72c DG |
1813 | goto error; |
1814 | } | |
1815 | ||
0d9c5d77 DG |
1816 | nb_fd = ret; |
1817 | ||
5eb91c98 | 1818 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 1819 | health_code_update(); |
139ac872 | 1820 | |
5eb91c98 DG |
1821 | /* Fetch once the poll data */ |
1822 | revents = LTTNG_POLL_GETEV(&events, i); | |
1823 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1824 | |
5eb91c98 | 1825 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1826 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1827 | if (ret) { |
139ac872 MD |
1828 | err = 0; |
1829 | goto exit; | |
90014c57 | 1830 | } |
1d4b027a | 1831 | |
5eb91c98 DG |
1832 | /* Event on the registration socket */ |
1833 | if (pollfd == apps_sock) { | |
1834 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1835 | ERR("Register apps socket poll error"); | |
1836 | goto error; | |
1837 | } else if (revents & LPOLLIN) { | |
1838 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1839 | if (sock < 0) { | |
1840 | goto error; | |
1841 | } | |
099e26bd | 1842 | |
16c5c8fa DG |
1843 | /* |
1844 | * Set socket timeout for both receiving and ending. | |
1845 | * app_socket_timeout is in seconds, whereas | |
1846 | * lttcomm_setsockopt_rcv_timeout and | |
1847 | * lttcomm_setsockopt_snd_timeout expect msec as | |
1848 | * parameter. | |
1849 | */ | |
1850 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
1851 | app_socket_timeout * 1000); | |
1852 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
1853 | app_socket_timeout * 1000); | |
1854 | ||
b662582b DG |
1855 | /* |
1856 | * Set the CLOEXEC flag. Return code is useless because | |
1857 | * either way, the show must go on. | |
1858 | */ | |
1859 | (void) utils_set_fd_cloexec(sock); | |
1860 | ||
5eb91c98 | 1861 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1862 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1863 | if (ust_cmd == NULL) { |
76d7553f | 1864 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
1865 | goto error; |
1866 | } | |
1d4b027a | 1867 | |
5eb91c98 DG |
1868 | /* |
1869 | * Using message-based transmissions to ensure we don't | |
1870 | * have to deal with partially received messages. | |
1871 | */ | |
4063050c MD |
1872 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1873 | if (ret < 0) { | |
1874 | ERR("Exhausted file descriptors allowed for applications."); | |
1875 | free(ust_cmd); | |
1876 | ret = close(sock); | |
1877 | if (ret) { | |
1878 | PERROR("close"); | |
1879 | } | |
1880 | sock = -1; | |
1881 | continue; | |
1882 | } | |
d88aee68 | 1883 | |
840cb59c | 1884 | health_code_update(); |
d0b96690 DG |
1885 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
1886 | if (ret < 0) { | |
5eb91c98 | 1887 | free(ust_cmd); |
d0b96690 | 1888 | /* Close socket of the application. */ |
76d7553f MD |
1889 | ret = close(sock); |
1890 | if (ret) { | |
1891 | PERROR("close"); | |
1892 | } | |
4063050c | 1893 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1894 | sock = -1; |
5eb91c98 DG |
1895 | continue; |
1896 | } | |
840cb59c | 1897 | health_code_update(); |
099e26bd | 1898 | |
5eb91c98 | 1899 | ust_cmd->sock = sock; |
34a2494f | 1900 | sock = -1; |
099e26bd | 1901 | |
5eb91c98 DG |
1902 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1903 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1904 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1905 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1906 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1907 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1908 | |
5eb91c98 DG |
1909 | /* |
1910 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1911 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1912 | */ |
1913 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1914 | ||
1915 | /* | |
1916 | * Wake the registration queue futex. Implicit memory | |
1917 | * barrier with the exchange in cds_wfq_enqueue. | |
1918 | */ | |
1919 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1920 | } | |
1921 | } | |
90014c57 | 1922 | } |
1d4b027a DG |
1923 | } |
1924 | ||
139ac872 | 1925 | exit: |
1d4b027a | 1926 | error: |
139ac872 | 1927 | if (err) { |
840cb59c | 1928 | health_error(); |
139ac872 MD |
1929 | ERR("Health error occurred in %s", __func__); |
1930 | } | |
139ac872 | 1931 | |
0fdd1e2c DG |
1932 | /* Notify that the registration thread is gone */ |
1933 | notify_ust_apps(0); | |
1934 | ||
a4b35e07 | 1935 | if (apps_sock >= 0) { |
76d7553f MD |
1936 | ret = close(apps_sock); |
1937 | if (ret) { | |
1938 | PERROR("close"); | |
1939 | } | |
a4b35e07 | 1940 | } |
46c3f085 | 1941 | if (sock >= 0) { |
76d7553f MD |
1942 | ret = close(sock); |
1943 | if (ret) { | |
1944 | PERROR("close"); | |
1945 | } | |
4063050c | 1946 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1947 | } |
273ea72c | 1948 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1949 | |
76d7553f | 1950 | error_poll_add: |
5eb91c98 | 1951 | lttng_poll_clean(&events); |
76d7553f MD |
1952 | error_listen: |
1953 | error_create_poll: | |
6993eeb3 | 1954 | error_testpoint: |
76d7553f | 1955 | DBG("UST Registration thread cleanup complete"); |
8782cc74 | 1956 | health_unregister(health_sessiond); |
5eb91c98 | 1957 | |
1d4b027a DG |
1958 | return NULL; |
1959 | } | |
1960 | ||
8c0faa1d | 1961 | /* |
3bd1e081 | 1962 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1963 | * exec or it will fails. |
8c0faa1d | 1964 | */ |
3bd1e081 | 1965 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 1966 | { |
a23ec3a7 | 1967 | int ret, clock_ret; |
ee0b0061 DG |
1968 | struct timespec timeout; |
1969 | ||
a23ec3a7 DG |
1970 | /* Make sure we set the readiness flag to 0 because we are NOT ready */ |
1971 | consumer_data->consumer_thread_is_ready = 0; | |
8c0faa1d | 1972 | |
a23ec3a7 DG |
1973 | /* Setup pthread condition */ |
1974 | ret = pthread_condattr_init(&consumer_data->condattr); | |
1975 | if (ret != 0) { | |
1976 | errno = ret; | |
1977 | PERROR("pthread_condattr_init consumer data"); | |
1978 | goto error; | |
1979 | } | |
1980 | ||
1981 | /* | |
1982 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
1983 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
1984 | * for a more details and how we noticed it. | |
1985 | */ | |
1986 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
1987 | if (ret != 0) { | |
1988 | errno = ret; | |
1989 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
1990 | goto error; |
1991 | } | |
8c0faa1d | 1992 | |
a23ec3a7 DG |
1993 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
1994 | if (ret != 0) { | |
1995 | errno = ret; | |
1996 | PERROR("pthread_cond_init consumer data"); | |
1997 | goto error; | |
1998 | } | |
1999 | ||
2000 | ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer, | |
2001 | consumer_data); | |
8c0faa1d | 2002 | if (ret != 0) { |
3bd1e081 | 2003 | PERROR("pthread_create consumer"); |
ee0b0061 | 2004 | ret = -1; |
8c0faa1d DG |
2005 | goto error; |
2006 | } | |
2007 | ||
a23ec3a7 DG |
2008 | /* We are about to wait on a pthread condition */ |
2009 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2010 | ||
ee0b0061 | 2011 | /* Get time for sem_timedwait absolute timeout */ |
a23ec3a7 DG |
2012 | clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
2013 | /* | |
2014 | * Set the timeout for the condition timed wait even if the clock gettime | |
2015 | * call fails since we might loop on that call and we want to avoid to | |
2016 | * increment the timeout too many times. | |
2017 | */ | |
2018 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2019 | ||
2020 | /* | |
2021 | * The following loop COULD be skipped in some conditions so this is why we | |
2022 | * set ret to 0 in order to make sure at least one round of the loop is | |
2023 | * done. | |
2024 | */ | |
2025 | ret = 0; | |
2026 | ||
2027 | /* | |
2028 | * Loop until the condition is reached or when a timeout is reached. Note | |
2029 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2030 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2031 | * possible. This loop does not take any chances and works with both of | |
2032 | * them. | |
2033 | */ | |
2034 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2035 | if (clock_ret < 0) { | |
2036 | PERROR("clock_gettime spawn consumer"); | |
2037 | /* Infinite wait for the consumerd thread to be ready */ | |
2038 | ret = pthread_cond_wait(&consumer_data->cond, | |
2039 | &consumer_data->cond_mutex); | |
2040 | } else { | |
2041 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2042 | &consumer_data->cond_mutex, &timeout); | |
2043 | } | |
ee0b0061 | 2044 | } |
8c0faa1d | 2045 | |
a23ec3a7 DG |
2046 | /* Release the pthread condition */ |
2047 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2048 | ||
2049 | if (ret != 0) { | |
2050 | errno = ret; | |
2051 | if (ret == ETIMEDOUT) { | |
ee0b0061 DG |
2052 | /* |
2053 | * Call has timed out so we kill the kconsumerd_thread and return | |
2054 | * an error. | |
2055 | */ | |
a23ec3a7 DG |
2056 | ERR("Condition timed out. The consumer thread was never ready." |
2057 | " Killing it"); | |
3bd1e081 | 2058 | ret = pthread_cancel(consumer_data->thread); |
ee0b0061 | 2059 | if (ret < 0) { |
3bd1e081 | 2060 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2061 | } |
2062 | } else { | |
a23ec3a7 | 2063 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 DG |
2064 | } |
2065 | goto error; | |
2066 | } | |
2067 | ||
3bd1e081 MD |
2068 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2069 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2070 | ERR("Consumerd did not start"); |
3bd1e081 | 2071 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2072 | goto error; |
2073 | } | |
3bd1e081 | 2074 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2075 | |
8c0faa1d DG |
2076 | return 0; |
2077 | ||
2078 | error: | |
2079 | return ret; | |
2080 | } | |
2081 | ||
d9800920 | 2082 | /* |
3bd1e081 | 2083 | * Join consumer thread |
d9800920 | 2084 | */ |
3bd1e081 | 2085 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2086 | { |
2087 | void *status; | |
cf3af59e | 2088 | |
e8209f6b DG |
2089 | /* Consumer pid must be a real one. */ |
2090 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2091 | int ret; |
3bd1e081 | 2092 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2093 | if (ret) { |
3bd1e081 | 2094 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
2095 | return ret; |
2096 | } | |
3bd1e081 | 2097 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2098 | } else { |
2099 | return 0; | |
2100 | } | |
2101 | } | |
2102 | ||
8c0faa1d | 2103 | /* |
3bd1e081 | 2104 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2105 | * |
d063d709 | 2106 | * Return pid if successful else -1. |
8c0faa1d | 2107 | */ |
3bd1e081 | 2108 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2109 | { |
2110 | int ret; | |
2111 | pid_t pid; | |
94c55f17 | 2112 | const char *consumer_to_use; |
53086306 | 2113 | const char *verbosity; |
94c55f17 | 2114 | struct stat st; |
8c0faa1d | 2115 | |
3bd1e081 | 2116 | DBG("Spawning consumerd"); |
c49dc785 | 2117 | |
8c0faa1d DG |
2118 | pid = fork(); |
2119 | if (pid == 0) { | |
2120 | /* | |
3bd1e081 | 2121 | * Exec consumerd. |
8c0faa1d | 2122 | */ |
daee5345 | 2123 | if (opt_verbose_consumer) { |
53086306 DG |
2124 | verbosity = "--verbose"; |
2125 | } else { | |
2126 | verbosity = "--quiet"; | |
2127 | } | |
3bd1e081 MD |
2128 | switch (consumer_data->type) { |
2129 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2130 | /* |
c7704d57 DG |
2131 | * Find out which consumerd to execute. We will first try the |
2132 | * 64-bit path, then the sessiond's installation directory, and | |
2133 | * fallback on the 32-bit one, | |
94c55f17 | 2134 | */ |
63a799e8 AM |
2135 | DBG3("Looking for a kernel consumer at these locations:"); |
2136 | DBG3(" 1) %s", consumerd64_bin); | |
2137 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
2138 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 2139 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 2140 | DBG3("Found location #1"); |
94c55f17 | 2141 | consumer_to_use = consumerd64_bin; |
94c55f17 | 2142 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 2143 | DBG3("Found location #2"); |
94c55f17 | 2144 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 2145 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 2146 | DBG3("Found location #3"); |
eb1e0bd4 | 2147 | consumer_to_use = consumerd32_bin; |
94c55f17 | 2148 | } else { |
63a799e8 | 2149 | DBG("Could not find any valid consumerd executable"); |
94c55f17 AM |
2150 | break; |
2151 | } | |
2152 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
2153 | execl(consumer_to_use, | |
2154 | "lttng-consumerd", verbosity, "-k", | |
2155 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2156 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2157 | "--group", tracing_group_name, |
94c55f17 | 2158 | NULL); |
3bd1e081 | 2159 | break; |
7753dea8 MD |
2160 | case LTTNG_CONSUMER64_UST: |
2161 | { | |
b1e0b6b6 | 2162 | char *tmpnew = NULL; |
8f4905da MD |
2163 | |
2164 | if (consumerd64_libdir[0] != '\0') { | |
2165 | char *tmp; | |
2166 | size_t tmplen; | |
2167 | ||
2168 | tmp = getenv("LD_LIBRARY_PATH"); | |
2169 | if (!tmp) { | |
2170 | tmp = ""; | |
2171 | } | |
2172 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2173 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
2174 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2175 | if (!tmpnew) { | |
2176 | ret = -ENOMEM; | |
2177 | goto error; | |
2178 | } | |
2179 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2180 | strcat(tmpnew, consumerd64_libdir); | |
2181 | if (tmp[0] != '\0') { | |
2182 | strcat(tmpnew, ":"); | |
2183 | strcat(tmpnew, tmp); | |
2184 | } | |
2185 | ret = putenv(tmpnew); | |
2186 | if (ret) { | |
2187 | ret = -errno; | |
c6f76da9 | 2188 | free(tmpnew); |
8f4905da MD |
2189 | goto error; |
2190 | } | |
2191 | } | |
94c55f17 | 2192 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 2193 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2194 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2195 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2196 | "--group", tracing_group_name, |
7753dea8 | 2197 | NULL); |
8f4905da MD |
2198 | if (consumerd64_libdir[0] != '\0') { |
2199 | free(tmpnew); | |
2200 | } | |
2201 | if (ret) { | |
2202 | goto error; | |
2203 | } | |
3bd1e081 | 2204 | break; |
7753dea8 MD |
2205 | } |
2206 | case LTTNG_CONSUMER32_UST: | |
2207 | { | |
937dde8e | 2208 | char *tmpnew = NULL; |
8f4905da MD |
2209 | |
2210 | if (consumerd32_libdir[0] != '\0') { | |
2211 | char *tmp; | |
2212 | size_t tmplen; | |
2213 | ||
2214 | tmp = getenv("LD_LIBRARY_PATH"); | |
2215 | if (!tmp) { | |
2216 | tmp = ""; | |
2217 | } | |
2218 | tmplen = strlen("LD_LIBRARY_PATH=") | |
2219 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
2220 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
2221 | if (!tmpnew) { | |
2222 | ret = -ENOMEM; | |
2223 | goto error; | |
2224 | } | |
2225 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
2226 | strcat(tmpnew, consumerd32_libdir); | |
2227 | if (tmp[0] != '\0') { | |
2228 | strcat(tmpnew, ":"); | |
2229 | strcat(tmpnew, tmp); | |
2230 | } | |
2231 | ret = putenv(tmpnew); | |
2232 | if (ret) { | |
2233 | ret = -errno; | |
c6f76da9 | 2234 | free(tmpnew); |
8f4905da MD |
2235 | goto error; |
2236 | } | |
2237 | } | |
94c55f17 | 2238 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 2239 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
2240 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2241 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
6c71277b | 2242 | "--group", tracing_group_name, |
7753dea8 | 2243 | NULL); |
8f4905da MD |
2244 | if (consumerd32_libdir[0] != '\0') { |
2245 | free(tmpnew); | |
2246 | } | |
2247 | if (ret) { | |
2248 | goto error; | |
2249 | } | |
7753dea8 MD |
2250 | break; |
2251 | } | |
3bd1e081 | 2252 | default: |
76d7553f | 2253 | PERROR("unknown consumer type"); |
3bd1e081 MD |
2254 | exit(EXIT_FAILURE); |
2255 | } | |
8c0faa1d | 2256 | if (errno != 0) { |
76d7553f | 2257 | PERROR("kernel start consumer exec"); |
8c0faa1d DG |
2258 | } |
2259 | exit(EXIT_FAILURE); | |
2260 | } else if (pid > 0) { | |
2261 | ret = pid; | |
8c0faa1d | 2262 | } else { |
76d7553f | 2263 | PERROR("start consumer fork"); |
8c0faa1d | 2264 | ret = -errno; |
8c0faa1d | 2265 | } |
8f4905da | 2266 | error: |
8c0faa1d DG |
2267 | return ret; |
2268 | } | |
2269 | ||
693bd40b | 2270 | /* |
3bd1e081 | 2271 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2272 | */ |
3bd1e081 | 2273 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2274 | { |
c617c0c6 | 2275 | int ret; |
edb8b045 DG |
2276 | |
2277 | /* | |
2278 | * Set the listen() state on the socket since there is a possible race | |
2279 | * between the exec() of the consumer daemon and this call if place in the | |
2280 | * consumer thread. See bug #366 for more details. | |
2281 | */ | |
2282 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2283 | if (ret < 0) { | |
2284 | goto error; | |
2285 | } | |
693bd40b | 2286 | |
3bd1e081 MD |
2287 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2288 | if (consumer_data->pid != 0) { | |
2289 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2290 | goto end; |
2291 | } | |
693bd40b | 2292 | |
3bd1e081 | 2293 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2294 | if (ret < 0) { |
3bd1e081 MD |
2295 | ERR("Spawning consumerd failed"); |
2296 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2297 | goto error; |
693bd40b | 2298 | } |
c49dc785 | 2299 | |
3bd1e081 MD |
2300 | /* Setting up the consumer_data pid */ |
2301 | consumer_data->pid = ret; | |
48842b30 | 2302 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2303 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2304 | |
3bd1e081 MD |
2305 | DBG2("Spawning consumer control thread"); |
2306 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2307 | if (ret < 0) { |
3bd1e081 | 2308 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2309 | goto error; |
2310 | } | |
2311 | ||
c49dc785 | 2312 | end: |
693bd40b DG |
2313 | return 0; |
2314 | ||
2315 | error: | |
331744e3 | 2316 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2317 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2318 | int err; |
2319 | ||
edb8b045 DG |
2320 | err = close(consumer_data->err_sock); |
2321 | if (err < 0) { | |
2322 | PERROR("close consumer data error socket"); | |
2323 | } | |
2324 | } | |
693bd40b DG |
2325 | return ret; |
2326 | } | |
2327 | ||
b73401da | 2328 | /* |
096102bd | 2329 | * Setup necessary data for kernel tracer action. |
b73401da | 2330 | */ |
096102bd | 2331 | static int init_kernel_tracer(void) |
b73401da DG |
2332 | { |
2333 | int ret; | |
b73401da | 2334 | |
096102bd DG |
2335 | /* Modprobe lttng kernel modules */ |
2336 | ret = modprobe_lttng_control(); | |
b73401da | 2337 | if (ret < 0) { |
b73401da DG |
2338 | goto error; |
2339 | } | |
2340 | ||
096102bd DG |
2341 | /* Open debugfs lttng */ |
2342 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2343 | if (kernel_tracer_fd < 0) { | |
2344 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b DG |
2345 | ret = -1; |
2346 | goto error_open; | |
54d01ffb DG |
2347 | } |
2348 | ||
2f77fc4b DG |
2349 | /* Validate kernel version */ |
2350 | ret = kernel_validate_version(kernel_tracer_fd); | |
2351 | if (ret < 0) { | |
2352 | goto error_version; | |
b551a063 | 2353 | } |
54d01ffb | 2354 | |
2f77fc4b DG |
2355 | ret = modprobe_lttng_data(); |
2356 | if (ret < 0) { | |
2357 | goto error_modules; | |
54d01ffb DG |
2358 | } |
2359 | ||
2f77fc4b DG |
2360 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2361 | return 0; | |
2362 | ||
2363 | error_version: | |
2364 | modprobe_remove_lttng_control(); | |
2365 | ret = close(kernel_tracer_fd); | |
2366 | if (ret) { | |
2367 | PERROR("close"); | |
b551a063 | 2368 | } |
2f77fc4b | 2369 | kernel_tracer_fd = -1; |
f73fabfd | 2370 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2371 | |
2f77fc4b DG |
2372 | error_modules: |
2373 | ret = close(kernel_tracer_fd); | |
2374 | if (ret) { | |
2375 | PERROR("close"); | |
b551a063 | 2376 | } |
54d01ffb | 2377 | |
2f77fc4b DG |
2378 | error_open: |
2379 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2380 | |
2381 | error: | |
2f77fc4b DG |
2382 | WARN("No kernel tracer available"); |
2383 | kernel_tracer_fd = -1; | |
2384 | if (!is_root) { | |
f73fabfd | 2385 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2386 | } else { |
f73fabfd | 2387 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2388 | } |
54d01ffb DG |
2389 | } |
2390 | ||
2f77fc4b | 2391 | |
54d01ffb | 2392 | /* |
2f77fc4b DG |
2393 | * Copy consumer output from the tracing session to the domain session. The |
2394 | * function also applies the right modification on a per domain basis for the | |
2395 | * trace files destination directory. | |
36b588ed MD |
2396 | * |
2397 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2398 | */ |
2f77fc4b | 2399 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2400 | { |
2401 | int ret; | |
2f77fc4b DG |
2402 | const char *dir_name; |
2403 | struct consumer_output *consumer; | |
2404 | ||
2405 | assert(session); | |
2406 | assert(session->consumer); | |
54d01ffb | 2407 | |
b551a063 DG |
2408 | switch (domain) { |
2409 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2410 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2411 | /* |
2412 | * XXX: We should audit the session creation and what this function | |
2413 | * does "extra" in order to avoid a destroy since this function is used | |
2414 | * in the domain session creation (kernel and ust) only. Same for UST | |
2415 | * domain. | |
2416 | */ | |
2417 | if (session->kernel_session->consumer) { | |
2418 | consumer_destroy_output(session->kernel_session->consumer); | |
2419 | } | |
2f77fc4b DG |
2420 | session->kernel_session->consumer = |
2421 | consumer_copy_output(session->consumer); | |
2422 | /* Ease our life a bit for the next part */ | |
2423 | consumer = session->kernel_session->consumer; | |
2424 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2425 | break; |
f20baf8e | 2426 | case LTTNG_DOMAIN_JUL: |
b551a063 | 2427 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2428 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd DG |
2429 | if (session->ust_session->consumer) { |
2430 | consumer_destroy_output(session->ust_session->consumer); | |
2431 | } | |
2f77fc4b DG |
2432 | session->ust_session->consumer = |
2433 | consumer_copy_output(session->consumer); | |
2434 | /* Ease our life a bit for the next part */ | |
2435 | consumer = session->ust_session->consumer; | |
2436 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2437 | break; |
2438 | default: | |
f73fabfd | 2439 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2440 | goto error; |
2441 | } | |
2442 | ||
2f77fc4b | 2443 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2444 | strncat(consumer->subdir, dir_name, |
2445 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2446 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2447 | ||
f73fabfd | 2448 | ret = LTTNG_OK; |
54d01ffb DG |
2449 | |
2450 | error: | |
2451 | return ret; | |
2452 | } | |
2453 | ||
00e2e675 | 2454 | /* |
2f77fc4b | 2455 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2456 | * |
2457 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2458 | */ |
2f77fc4b DG |
2459 | static int create_ust_session(struct ltt_session *session, |
2460 | struct lttng_domain *domain) | |
00e2e675 DG |
2461 | { |
2462 | int ret; | |
2f77fc4b | 2463 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2464 | |
a4b92340 | 2465 | assert(session); |
2f77fc4b DG |
2466 | assert(domain); |
2467 | assert(session->consumer); | |
a4b92340 | 2468 | |
2f77fc4b | 2469 | switch (domain->type) { |
f20baf8e | 2470 | case LTTNG_DOMAIN_JUL: |
2f77fc4b DG |
2471 | case LTTNG_DOMAIN_UST: |
2472 | break; | |
2473 | default: | |
2474 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2475 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2476 | goto error; |
2477 | } | |
2478 | ||
2f77fc4b DG |
2479 | DBG("Creating UST session"); |
2480 | ||
dec56f6c | 2481 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2482 | if (lus == NULL) { |
f73fabfd | 2483 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2484 | goto error; |
2485 | } | |
2486 | ||
2f77fc4b DG |
2487 | lus->uid = session->uid; |
2488 | lus->gid = session->gid; | |
2bba9e53 | 2489 | lus->output_traces = session->output_traces; |
27babd3a | 2490 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2491 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2492 | session->ust_session = lus; |
00e2e675 | 2493 | |
2f77fc4b DG |
2494 | /* Copy session output to the newly created UST session */ |
2495 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2496 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2497 | goto error; |
2498 | } | |
2499 | ||
f73fabfd | 2500 | return LTTNG_OK; |
00e2e675 DG |
2501 | |
2502 | error: | |
2f77fc4b DG |
2503 | free(lus); |
2504 | session->ust_session = NULL; | |
00e2e675 DG |
2505 | return ret; |
2506 | } | |
2507 | ||
2508 | /* | |
2f77fc4b | 2509 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2510 | */ |
2f77fc4b | 2511 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2512 | { |
2513 | int ret; | |
a4b92340 | 2514 | |
2f77fc4b | 2515 | DBG("Creating kernel session"); |
00e2e675 | 2516 | |
2f77fc4b DG |
2517 | ret = kernel_create_session(session, kernel_tracer_fd); |
2518 | if (ret < 0) { | |
f73fabfd | 2519 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2520 | goto error; |
2521 | } | |
2522 | ||
2f77fc4b DG |
2523 | /* Code flow safety */ |
2524 | assert(session->kernel_session); | |
2525 | ||
2526 | /* Copy session output to the newly created Kernel session */ | |
2527 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2528 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2529 | goto error; |
2530 | } | |
2531 | ||
2f77fc4b DG |
2532 | /* Create directory(ies) on local filesystem. */ |
2533 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && | |
2534 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
2535 | ret = run_as_mkdir_recursive( | |
2536 | session->kernel_session->consumer->dst.trace_path, | |
2537 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2538 | if (ret < 0) { | |
2539 | if (ret != -EEXIST) { | |
2540 | ERR("Trace directory creation error"); | |
00e2e675 DG |
2541 | goto error; |
2542 | } | |
00e2e675 | 2543 | } |
2f77fc4b | 2544 | } |
00e2e675 | 2545 | |
2f77fc4b DG |
2546 | session->kernel_session->uid = session->uid; |
2547 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2548 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2549 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2550 | |
f73fabfd | 2551 | return LTTNG_OK; |
00e2e675 | 2552 | |
2f77fc4b DG |
2553 | error: |
2554 | trace_kernel_destroy_session(session->kernel_session); | |
2555 | session->kernel_session = NULL; | |
2556 | return ret; | |
2557 | } | |
00e2e675 | 2558 | |
2f77fc4b DG |
2559 | /* |
2560 | * Count number of session permitted by uid/gid. | |
2561 | */ | |
2562 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2563 | { | |
2564 | unsigned int i = 0; | |
2565 | struct ltt_session *session; | |
07424f16 | 2566 | |
2f77fc4b DG |
2567 | DBG("Counting number of available session for UID %d GID %d", |
2568 | uid, gid); | |
2569 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2570 | /* |
2f77fc4b | 2571 | * Only list the sessions the user can control. |
00e2e675 | 2572 | */ |
2f77fc4b DG |
2573 | if (!session_access_ok(session, uid, gid)) { |
2574 | continue; | |
2575 | } | |
2576 | i++; | |
a4b92340 | 2577 | } |
2f77fc4b | 2578 | return i; |
00e2e675 DG |
2579 | } |
2580 | ||
54d01ffb DG |
2581 | /* |
2582 | * Process the command requested by the lttng client within the command | |
2583 | * context structure. This function make sure that the return structure (llm) | |
2584 | * is set and ready for transmission before returning. | |
2585 | * | |
2586 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2587 | * |
2588 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2589 | * |
2590 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2591 | */ |
53a80697 MD |
2592 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2593 | int *sock_error) | |
54d01ffb | 2594 | { |
f73fabfd | 2595 | int ret = LTTNG_OK; |
44d3bd01 | 2596 | int need_tracing_session = 1; |
2e09ba09 | 2597 | int need_domain; |
54d01ffb DG |
2598 | |
2599 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2600 | ||
53a80697 MD |
2601 | *sock_error = 0; |
2602 | ||
2e09ba09 MD |
2603 | switch (cmd_ctx->lsm->cmd_type) { |
2604 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2605 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2606 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2607 | case LTTNG_DESTROY_SESSION: |
2608 | case LTTNG_LIST_SESSIONS: | |
2609 | case LTTNG_LIST_DOMAINS: | |
2610 | case LTTNG_START_TRACE: | |
2611 | case LTTNG_STOP_TRACE: | |
6d805429 | 2612 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
2613 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
2614 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
2615 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
2616 | case LTTNG_SNAPSHOT_RECORD: | |
2e09ba09 | 2617 | need_domain = 0; |
3aace903 | 2618 | break; |
2e09ba09 MD |
2619 | default: |
2620 | need_domain = 1; | |
2621 | } | |
2622 | ||
2623 | if (opt_no_kernel && need_domain | |
2624 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 | 2625 | if (!is_root) { |
f73fabfd | 2626 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 2627 | } else { |
f73fabfd | 2628 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 2629 | } |
4fba7219 DG |
2630 | goto error; |
2631 | } | |
2632 | ||
8d3113b2 DG |
2633 | /* Deny register consumer if we already have a spawned consumer. */ |
2634 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
2635 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
2636 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 2637 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 2638 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
2639 | goto error; |
2640 | } | |
2641 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
2642 | } | |
2643 | ||
54d01ffb DG |
2644 | /* |
2645 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2646 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2647 | * command. |
2648 | */ | |
2649 | switch(cmd_ctx->lsm->cmd_type) { | |
2650 | case LTTNG_LIST_SESSIONS: | |
2651 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2652 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
2653 | case LTTNG_LIST_DOMAINS: |
2654 | case LTTNG_LIST_CHANNELS: | |
2655 | case LTTNG_LIST_EVENTS: | |
2656 | break; | |
2657 | default: | |
2658 | /* Setup lttng message with no payload */ | |
2659 | ret = setup_lttng_msg(cmd_ctx, 0); | |
2660 | if (ret < 0) { | |
2661 | /* This label does not try to unlock the session */ | |
2662 | goto init_setup_error; | |
2663 | } | |
2664 | } | |
2665 | ||
2666 | /* Commands that DO NOT need a session. */ | |
2667 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 2668 | case LTTNG_CREATE_SESSION: |
27babd3a | 2669 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2670 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 | 2671 | case LTTNG_CALIBRATE: |
54d01ffb DG |
2672 | case LTTNG_LIST_SESSIONS: |
2673 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2674 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
44d3bd01 | 2675 | need_tracing_session = 0; |
54d01ffb DG |
2676 | break; |
2677 | default: | |
2678 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
2679 | /* |
2680 | * We keep the session list lock across _all_ commands | |
2681 | * for now, because the per-session lock does not | |
2682 | * handle teardown properly. | |
2683 | */ | |
74babd95 | 2684 | session_lock_list(); |
54d01ffb DG |
2685 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
2686 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 2687 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
2688 | goto error; |
2689 | } else { | |
2690 | /* Acquire lock for the session */ | |
2691 | session_lock(cmd_ctx->session); | |
2692 | } | |
2693 | break; | |
2694 | } | |
b389abbe | 2695 | |
2e09ba09 MD |
2696 | if (!need_domain) { |
2697 | goto skip_domain; | |
2698 | } | |
a4b92340 | 2699 | |
54d01ffb DG |
2700 | /* |
2701 | * Check domain type for specific "pre-action". | |
2702 | */ | |
2703 | switch (cmd_ctx->lsm->domain.type) { | |
2704 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 2705 | if (!is_root) { |
f73fabfd | 2706 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
2707 | goto error; |
2708 | } | |
2709 | ||
54d01ffb | 2710 | /* Kernel tracer check */ |
a4b35e07 | 2711 | if (kernel_tracer_fd == -1) { |
54d01ffb | 2712 | /* Basically, load kernel tracer modules */ |
096102bd DG |
2713 | ret = init_kernel_tracer(); |
2714 | if (ret != 0) { | |
54d01ffb DG |
2715 | goto error; |
2716 | } | |
2717 | } | |
5eb91c98 | 2718 | |
5c827ce0 DG |
2719 | /* Consumer is in an ERROR state. Report back to client */ |
2720 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2721 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2722 | goto error; |
2723 | } | |
2724 | ||
54d01ffb | 2725 | /* Need a session for kernel command */ |
44d3bd01 | 2726 | if (need_tracing_session) { |
54d01ffb | 2727 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 2728 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 2729 | if (ret < 0) { |
f73fabfd | 2730 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
2731 | goto error; |
2732 | } | |
b389abbe | 2733 | } |
7d29a247 | 2734 | |
54d01ffb | 2735 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
2736 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
2737 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 2738 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
2739 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
2740 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 2741 | if (ret < 0) { |
f73fabfd | 2742 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 2743 | goto error; |
950131af | 2744 | } |
5c827ce0 | 2745 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
2746 | } else { |
2747 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 2748 | } |
173af62f | 2749 | |
a4b92340 DG |
2750 | /* |
2751 | * The consumer was just spawned so we need to add the socket to | |
2752 | * the consumer output of the session if exist. | |
2753 | */ | |
2754 | ret = consumer_create_socket(&kconsumer_data, | |
2755 | cmd_ctx->session->kernel_session->consumer); | |
2756 | if (ret < 0) { | |
2757 | goto error; | |
173af62f | 2758 | } |
0d0c377a | 2759 | } |
5c827ce0 | 2760 | |
54d01ffb | 2761 | break; |
b9dfb167 | 2762 | case LTTNG_DOMAIN_JUL: |
2bdd86d4 | 2763 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 2764 | { |
b51ec5b4 MD |
2765 | if (!ust_app_supported()) { |
2766 | ret = LTTNG_ERR_NO_UST; | |
2767 | goto error; | |
2768 | } | |
5c827ce0 DG |
2769 | /* Consumer is in an ERROR state. Report back to client */ |
2770 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2771 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2772 | goto error; |
2773 | } | |
2774 | ||
44d3bd01 | 2775 | if (need_tracing_session) { |
a4b92340 | 2776 | /* Create UST session if none exist. */ |
f6a9efaa | 2777 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 2778 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 2779 | &cmd_ctx->lsm->domain); |
f73fabfd | 2780 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
2781 | goto error; |
2782 | } | |
2783 | } | |
00e2e675 | 2784 | |
7753dea8 MD |
2785 | /* Start the UST consumer daemons */ |
2786 | /* 64-bit */ | |
2787 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 2788 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 2789 | ustconsumer64_data.pid == 0 && |
785d2d0d | 2790 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2791 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2792 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 2793 | if (ret < 0) { |
f73fabfd | 2794 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 2795 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
2796 | goto error; |
2797 | } | |
48842b30 | 2798 | |
173af62f | 2799 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 2800 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 2801 | } else { |
7753dea8 MD |
2802 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2803 | } | |
173af62f DG |
2804 | |
2805 | /* | |
2806 | * Setup socket for consumer 64 bit. No need for atomic access | |
2807 | * since it was set above and can ONLY be set in this thread. | |
2808 | */ | |
a4b92340 DG |
2809 | ret = consumer_create_socket(&ustconsumer64_data, |
2810 | cmd_ctx->session->ust_session->consumer); | |
2811 | if (ret < 0) { | |
2812 | goto error; | |
173af62f DG |
2813 | } |
2814 | ||
7753dea8 | 2815 | /* 32-bit */ |
fc7a59ce | 2816 | if (consumerd32_bin[0] != '\0' && |
7753dea8 | 2817 | ustconsumer32_data.pid == 0 && |
785d2d0d | 2818 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2819 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
2820 | ret = start_consumerd(&ustconsumer32_data); | |
2821 | if (ret < 0) { | |
f73fabfd | 2822 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 2823 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
2824 | goto error; |
2825 | } | |
5c827ce0 | 2826 | |
173af62f | 2827 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 2828 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
2829 | } else { |
2830 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 2831 | } |
173af62f DG |
2832 | |
2833 | /* | |
2834 | * Setup socket for consumer 64 bit. No need for atomic access | |
2835 | * since it was set above and can ONLY be set in this thread. | |
2836 | */ | |
a4b92340 DG |
2837 | ret = consumer_create_socket(&ustconsumer32_data, |
2838 | cmd_ctx->session->ust_session->consumer); | |
2839 | if (ret < 0) { | |
2840 | goto error; | |
173af62f | 2841 | } |
44d3bd01 DG |
2842 | } |
2843 | break; | |
48842b30 | 2844 | } |
54d01ffb | 2845 | default: |
54d01ffb DG |
2846 | break; |
2847 | } | |
2e09ba09 | 2848 | skip_domain: |
33a2b854 | 2849 | |
5c827ce0 DG |
2850 | /* Validate consumer daemon state when start/stop trace command */ |
2851 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
2852 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
2853 | switch (cmd_ctx->lsm->domain.type) { | |
b9dfb167 | 2854 | case LTTNG_DOMAIN_JUL: |
5c827ce0 DG |
2855 | case LTTNG_DOMAIN_UST: |
2856 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2857 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2858 | goto error; |
2859 | } | |
2860 | break; | |
2861 | case LTTNG_DOMAIN_KERNEL: | |
2862 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2863 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2864 | goto error; |
2865 | } | |
2866 | break; | |
2867 | } | |
2868 | } | |
2869 | ||
8e0af1b4 MD |
2870 | /* |
2871 | * Check that the UID or GID match that of the tracing session. | |
2872 | * The root user can interact with all sessions. | |
2873 | */ | |
2874 | if (need_tracing_session) { | |
2875 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
2876 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
2877 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
f73fabfd | 2878 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
2879 | goto error; |
2880 | } | |
2881 | } | |
2882 | ||
ffe60014 DG |
2883 | /* |
2884 | * Send relayd information to consumer as soon as we have a domain and a | |
2885 | * session defined. | |
2886 | */ | |
2887 | if (cmd_ctx->session && need_domain) { | |
2888 | /* | |
2889 | * Setup relayd if not done yet. If the relayd information was already | |
2890 | * sent to the consumer, this call will gracefully return. | |
2891 | */ | |
2892 | ret = cmd_setup_relayd(cmd_ctx->session); | |
2893 | if (ret != LTTNG_OK) { | |
2894 | goto error; | |
2895 | } | |
2896 | } | |
2897 | ||
54d01ffb DG |
2898 | /* Process by command type */ |
2899 | switch (cmd_ctx->lsm->cmd_type) { | |
2900 | case LTTNG_ADD_CONTEXT: | |
2901 | { | |
2902 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2903 | cmd_ctx->lsm->u.context.channel_name, | |
979e618e | 2904 | &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]); |
54d01ffb DG |
2905 | break; |
2906 | } | |
2907 | case LTTNG_DISABLE_CHANNEL: | |
2908 | { | |
2909 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2910 | cmd_ctx->lsm->u.disable.channel_name); | |
2911 | break; | |
2912 | } | |
2913 | case LTTNG_DISABLE_EVENT: | |
2914 | { | |
2915 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2916 | cmd_ctx->lsm->u.disable.channel_name, | |
2917 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
2918 | break; |
2919 | } | |
54d01ffb DG |
2920 | case LTTNG_DISABLE_ALL_EVENT: |
2921 | { | |
2bdd86d4 | 2922 | DBG("Disabling all events"); |
54d01ffb DG |
2923 | |
2924 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2925 | cmd_ctx->lsm->u.disable.channel_name); | |
2926 | break; | |
2927 | } | |
2928 | case LTTNG_ENABLE_CHANNEL: | |
2929 | { | |
7972aab2 | 2930 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
2f77fc4b | 2931 | &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]); |
54d01ffb DG |
2932 | break; |
2933 | } | |
2934 | case LTTNG_ENABLE_EVENT: | |
2935 | { | |
7972aab2 | 2936 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, |
54d01ffb | 2937 | cmd_ctx->lsm->u.enable.channel_name, |
025faf73 | 2938 | &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]); |
54d01ffb DG |
2939 | break; |
2940 | } | |
2941 | case LTTNG_ENABLE_ALL_EVENT: | |
2942 | { | |
2bdd86d4 | 2943 | DBG("Enabling all events"); |
54d01ffb | 2944 | |
7972aab2 | 2945 | ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain, |
8c9ae521 | 2946 | cmd_ctx->lsm->u.enable.channel_name, |
025faf73 | 2947 | cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]); |
54d01ffb DG |
2948 | break; |
2949 | } | |
052da939 | 2950 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 2951 | { |
9f19cc17 | 2952 | struct lttng_event *events; |
54d01ffb | 2953 | ssize_t nb_events; |
052da939 | 2954 | |
54d01ffb DG |
2955 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
2956 | if (nb_events < 0) { | |
f73fabfd | 2957 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
2958 | ret = -nb_events; |
2959 | goto error; | |
2ef84c95 DG |
2960 | } |
2961 | ||
2962 | /* | |
2963 | * Setup lttng message with payload size set to the event list size in | |
2964 | * bytes and then copy list into the llm payload. | |
2965 | */ | |
052da939 | 2966 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2967 | if (ret < 0) { |
052da939 | 2968 | free(events); |
2ef84c95 DG |
2969 | goto setup_error; |
2970 | } | |
2971 | ||
2972 | /* Copy event list into message payload */ | |
9f19cc17 | 2973 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 2974 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2975 | |
9f19cc17 | 2976 | free(events); |
2ef84c95 | 2977 | |
f73fabfd | 2978 | ret = LTTNG_OK; |
2ef84c95 DG |
2979 | break; |
2980 | } | |
f37d259d MD |
2981 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
2982 | { | |
2983 | struct lttng_event_field *fields; | |
2984 | ssize_t nb_fields; | |
2985 | ||
a4b92340 DG |
2986 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
2987 | &fields); | |
f37d259d | 2988 | if (nb_fields < 0) { |
f73fabfd | 2989 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
2990 | ret = -nb_fields; |
2991 | goto error; | |
2992 | } | |
2993 | ||
2994 | /* | |
2995 | * Setup lttng message with payload size set to the event list size in | |
2996 | * bytes and then copy list into the llm payload. | |
2997 | */ | |
a4b92340 DG |
2998 | ret = setup_lttng_msg(cmd_ctx, |
2999 | sizeof(struct lttng_event_field) * nb_fields); | |
f37d259d MD |
3000 | if (ret < 0) { |
3001 | free(fields); | |
3002 | goto setup_error; | |
3003 | } | |
3004 | ||
3005 | /* Copy event list into message payload */ | |
3006 | memcpy(cmd_ctx->llm->payload, fields, | |
3007 | sizeof(struct lttng_event_field) * nb_fields); | |
3008 | ||
3009 | free(fields); | |
3010 | ||
f73fabfd | 3011 | ret = LTTNG_OK; |
f37d259d MD |
3012 | break; |
3013 | } | |
00e2e675 DG |
3014 | case LTTNG_SET_CONSUMER_URI: |
3015 | { | |
a4b92340 DG |
3016 | size_t nb_uri, len; |
3017 | struct lttng_uri *uris; | |
3018 | ||
3019 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3020 | len = nb_uri * sizeof(struct lttng_uri); | |
3021 | ||
3022 | if (nb_uri == 0) { | |
f73fabfd | 3023 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
3024 | goto error; |
3025 | } | |
3026 | ||
3027 | uris = zmalloc(len); | |
3028 | if (uris == NULL) { | |
f73fabfd | 3029 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3030 | goto error; |
3031 | } | |
3032 | ||
3033 | /* Receive variable len data */ | |
77c7c900 | 3034 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
3035 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3036 | if (ret <= 0) { | |
3037 | DBG("No URIs received from client... continuing"); | |
3038 | *sock_error = 1; | |
f73fabfd | 3039 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3040 | free(uris); |
a4b92340 DG |
3041 | goto error; |
3042 | } | |
3043 | ||
00e2e675 | 3044 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
a4b92340 | 3045 | nb_uri, uris); |
f73fabfd | 3046 | if (ret != LTTNG_OK) { |
b1d41407 | 3047 | free(uris); |
a4b92340 DG |
3048 | goto error; |
3049 | } | |
3050 | ||
3051 | /* | |
3052 | * XXX: 0 means that this URI should be applied on the session. Should | |
3053 | * be a DOMAIN enuam. | |
3054 | */ | |
3055 | if (cmd_ctx->lsm->domain.type == 0) { | |
3056 | /* Add the URI for the UST session if a consumer is present. */ | |
3057 | if (cmd_ctx->session->ust_session && | |
3058 | cmd_ctx->session->ust_session->consumer) { | |
3059 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session, | |
3060 | nb_uri, uris); | |
3061 | } else if (cmd_ctx->session->kernel_session && | |
3062 | cmd_ctx->session->kernel_session->consumer) { | |
3063 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL, | |
3064 | cmd_ctx->session, nb_uri, uris); | |
3065 | } | |
3066 | } | |
3067 | ||
b1d41407 DG |
3068 | free(uris); |
3069 | ||
00e2e675 DG |
3070 | break; |
3071 | } | |
f3ed775e | 3072 | case LTTNG_START_TRACE: |
8c0faa1d | 3073 | { |
54d01ffb | 3074 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3075 | break; |
3076 | } | |
f3ed775e | 3077 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3078 | { |
54d01ffb | 3079 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3080 | break; |
3081 | } | |
5e16da05 MD |
3082 | case LTTNG_CREATE_SESSION: |
3083 | { | |
a4b92340 DG |
3084 | size_t nb_uri, len; |
3085 | struct lttng_uri *uris = NULL; | |
3086 | ||
3087 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3088 | len = nb_uri * sizeof(struct lttng_uri); | |
3089 | ||
3090 | if (nb_uri > 0) { | |
3091 | uris = zmalloc(len); | |
3092 | if (uris == NULL) { | |
f73fabfd | 3093 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3094 | goto error; |
3095 | } | |
3096 | ||
3097 | /* Receive variable len data */ | |
77c7c900 | 3098 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
3099 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3100 | if (ret <= 0) { | |
3101 | DBG("No URIs received from client... continuing"); | |
3102 | *sock_error = 1; | |
f73fabfd | 3103 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3104 | free(uris); |
a4b92340 DG |
3105 | goto error; |
3106 | } | |
3107 | ||
3108 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3109 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 3110 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3111 | free(uris); |
a4b92340 DG |
3112 | goto error; |
3113 | } | |
3114 | } | |
3115 | ||
3116 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
ecc48a90 | 3117 | &cmd_ctx->creds, 0); |
a4b92340 | 3118 | |
b1d41407 DG |
3119 | free(uris); |
3120 | ||
00e2e675 DG |
3121 | break; |
3122 | } | |
5e16da05 MD |
3123 | case LTTNG_DESTROY_SESSION: |
3124 | { | |
2f77fc4b | 3125 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]); |
a4b92340 DG |
3126 | |
3127 | /* Set session to NULL so we do not unlock it after free. */ | |
256a5576 | 3128 | cmd_ctx->session = NULL; |
5461b305 | 3129 | break; |
5e16da05 | 3130 | } |
9f19cc17 | 3131 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3132 | { |
54d01ffb DG |
3133 | ssize_t nb_dom; |
3134 | struct lttng_domain *domains; | |
5461b305 | 3135 | |
54d01ffb DG |
3136 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3137 | if (nb_dom < 0) { | |
f73fabfd | 3138 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3139 | ret = -nb_dom; |
3140 | goto error; | |
ce3d728c | 3141 | } |
520ff687 | 3142 | |
54d01ffb | 3143 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 | 3144 | if (ret < 0) { |
ae9c20bc | 3145 | free(domains); |
5461b305 | 3146 | goto setup_error; |
520ff687 | 3147 | } |
57167058 | 3148 | |
54d01ffb DG |
3149 | /* Copy event list into message payload */ |
3150 | memcpy(cmd_ctx->llm->payload, domains, | |
3151 | nb_dom * sizeof(struct lttng_domain)); | |
3152 | ||
3153 | free(domains); | |
5e16da05 | 3154 | |
f73fabfd | 3155 | ret = LTTNG_OK; |
5e16da05 MD |
3156 | break; |
3157 | } | |
9f19cc17 | 3158 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3159 | { |
6775595e | 3160 | int nb_chan; |
54d01ffb DG |
3161 | struct lttng_channel *channels; |
3162 | ||
b551a063 DG |
3163 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
3164 | cmd_ctx->session, &channels); | |
54d01ffb | 3165 | if (nb_chan < 0) { |
f73fabfd | 3166 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3167 | ret = -nb_chan; |
3168 | goto error; | |
5461b305 | 3169 | } |
ca95a216 | 3170 | |
54d01ffb | 3171 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 | 3172 | if (ret < 0) { |
ae9c20bc | 3173 | free(channels); |
5461b305 DG |
3174 | goto setup_error; |
3175 | } | |
9f19cc17 | 3176 | |
54d01ffb DG |
3177 | /* Copy event list into message payload */ |
3178 | memcpy(cmd_ctx->llm->payload, channels, | |
3179 | nb_chan * sizeof(struct lttng_channel)); | |
3180 | ||
3181 | free(channels); | |
9f19cc17 | 3182 | |
f73fabfd | 3183 | ret = LTTNG_OK; |
5461b305 | 3184 | break; |
5e16da05 | 3185 | } |
9f19cc17 | 3186 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3187 | { |
b551a063 | 3188 | ssize_t nb_event; |
684d34d2 | 3189 | struct lttng_event *events = NULL; |
9f19cc17 | 3190 | |
b551a063 | 3191 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
54d01ffb DG |
3192 | cmd_ctx->lsm->u.list.channel_name, &events); |
3193 | if (nb_event < 0) { | |
f73fabfd | 3194 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3195 | ret = -nb_event; |
3196 | goto error; | |
5461b305 | 3197 | } |
ca95a216 | 3198 | |
54d01ffb | 3199 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 | 3200 | if (ret < 0) { |
ae9c20bc | 3201 | free(events); |
5461b305 DG |
3202 | goto setup_error; |
3203 | } | |
9f19cc17 | 3204 | |
54d01ffb DG |
3205 | /* Copy event list into message payload */ |
3206 | memcpy(cmd_ctx->llm->payload, events, | |
3207 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 3208 | |
54d01ffb | 3209 | free(events); |
9f19cc17 | 3210 | |
f73fabfd | 3211 | ret = LTTNG_OK; |
5461b305 | 3212 | break; |
5e16da05 MD |
3213 | } |
3214 | case LTTNG_LIST_SESSIONS: | |
3215 | { | |
8e0af1b4 | 3216 | unsigned int nr_sessions; |
5461b305 | 3217 | |
8e0af1b4 | 3218 | session_lock_list(); |
730389d9 DG |
3219 | nr_sessions = lttng_sessions_count( |
3220 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
3221 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
d32fb093 | 3222 | |
8e0af1b4 | 3223 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
5461b305 | 3224 | if (ret < 0) { |
54d01ffb | 3225 | session_unlock_list(); |
5461b305 | 3226 | goto setup_error; |
e065084a | 3227 | } |
5e16da05 | 3228 | |
6c9cc2ab | 3229 | /* Filled the session array */ |
2f77fc4b | 3230 | cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
730389d9 DG |
3231 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3232 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6c9cc2ab | 3233 | |
54d01ffb | 3234 | session_unlock_list(); |
5e16da05 | 3235 | |
f73fabfd | 3236 | ret = LTTNG_OK; |
5e16da05 MD |
3237 | break; |
3238 | } | |
d0254c7c MD |
3239 | case LTTNG_CALIBRATE: |
3240 | { | |
54d01ffb DG |
3241 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
3242 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
3243 | break; |
3244 | } | |
d9800920 DG |
3245 | case LTTNG_REGISTER_CONSUMER: |
3246 | { | |
2f77fc4b DG |
3247 | struct consumer_data *cdata; |
3248 | ||
3249 | switch (cmd_ctx->lsm->domain.type) { | |
3250 | case LTTNG_DOMAIN_KERNEL: | |
3251 | cdata = &kconsumer_data; | |
3252 | break; | |
3253 | default: | |
f73fabfd | 3254 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3255 | goto error; |
3256 | } | |
3257 | ||
54d01ffb | 3258 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 3259 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
3260 | break; |
3261 | } | |
025faf73 | 3262 | case LTTNG_ENABLE_EVENT_WITH_FILTER: |
53a80697 MD |
3263 | { |
3264 | struct lttng_filter_bytecode *bytecode; | |
3265 | ||
025faf73 | 3266 | if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) { |
f73fabfd | 3267 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
3268 | goto error; |
3269 | } | |
52df2401 MD |
3270 | if (cmd_ctx->lsm->u.enable.bytecode_len == 0) { |
3271 | ret = LTTNG_ERR_FILTER_INVAL; | |
3272 | goto error; | |
3273 | } | |
025faf73 | 3274 | bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len); |
53a80697 | 3275 | if (!bytecode) { |
f73fabfd | 3276 | ret = LTTNG_ERR_FILTER_NOMEM; |
53a80697 MD |
3277 | goto error; |
3278 | } | |
3279 | /* Receive var. len. data */ | |
3280 | DBG("Receiving var len data from client ..."); | |
3281 | ret = lttcomm_recv_unix_sock(sock, bytecode, | |
025faf73 | 3282 | cmd_ctx->lsm->u.enable.bytecode_len); |
53a80697 MD |
3283 | if (ret <= 0) { |
3284 | DBG("Nothing recv() from client var len data... continuing"); | |
3285 | *sock_error = 1; | |
f73fabfd | 3286 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
3287 | goto error; |
3288 | } | |
3289 | ||
3290 | if (bytecode->len + sizeof(*bytecode) | |
025faf73 | 3291 | != cmd_ctx->lsm->u.enable.bytecode_len) { |
53a80697 | 3292 | free(bytecode); |
f73fabfd | 3293 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
3294 | goto error; |
3295 | } | |
3296 | ||
7972aab2 | 3297 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, |
025faf73 DG |
3298 | cmd_ctx->lsm->u.enable.channel_name, |
3299 | &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]); | |
53a80697 MD |
3300 | break; |
3301 | } | |
6d805429 | 3302 | case LTTNG_DATA_PENDING: |
806e2684 | 3303 | { |
6d805429 | 3304 | ret = cmd_data_pending(cmd_ctx->session); |
806e2684 DG |
3305 | break; |
3306 | } | |
da3c9ec1 DG |
3307 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3308 | { | |
6dc3064a DG |
3309 | struct lttcomm_lttng_output_id reply; |
3310 | ||
3311 | ret = cmd_snapshot_add_output(cmd_ctx->session, | |
3312 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); | |
3313 | if (ret != LTTNG_OK) { | |
3314 | goto error; | |
3315 | } | |
3316 | ||
3317 | ret = setup_lttng_msg(cmd_ctx, sizeof(reply)); | |
3318 | if (ret < 0) { | |
3319 | goto setup_error; | |
3320 | } | |
3321 | ||
3322 | /* Copy output list into message payload */ | |
3323 | memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply)); | |
3324 | ret = LTTNG_OK; | |
da3c9ec1 DG |
3325 | break; |
3326 | } | |
3327 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3328 | { | |
6dc3064a DG |
3329 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
3330 | &cmd_ctx->lsm->u.snapshot_output.output); | |
da3c9ec1 DG |
3331 | break; |
3332 | } | |
3333 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
3334 | { | |
6dc3064a DG |
3335 | ssize_t nb_output; |
3336 | struct lttng_snapshot_output *outputs = NULL; | |
3337 | ||
3338 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); | |
3339 | if (nb_output < 0) { | |
3340 | ret = -nb_output; | |
3341 | goto error; | |
3342 | } | |
3343 | ||
3344 | ret = setup_lttng_msg(cmd_ctx, | |
3345 | nb_output * sizeof(struct lttng_snapshot_output)); | |
3346 | if (ret < 0) { | |
3347 | free(outputs); | |
3348 | goto setup_error; | |
3349 | } | |
3350 | ||
3351 | if (outputs) { | |
3352 | /* Copy output list into message payload */ | |
3353 | memcpy(cmd_ctx->llm->payload, outputs, | |
3354 | nb_output * sizeof(struct lttng_snapshot_output)); | |
3355 | free(outputs); | |
3356 | } | |
3357 | ||
3358 | ret = LTTNG_OK; | |
da3c9ec1 DG |
3359 | break; |
3360 | } | |
3361 | case LTTNG_SNAPSHOT_RECORD: | |
3362 | { | |
6dc3064a DG |
3363 | ret = cmd_snapshot_record(cmd_ctx->session, |
3364 | &cmd_ctx->lsm->u.snapshot_record.output, | |
3365 | cmd_ctx->lsm->u.snapshot_record.wait); | |
da3c9ec1 DG |
3366 | break; |
3367 | } | |
27babd3a DG |
3368 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
3369 | { | |
3370 | size_t nb_uri, len; | |
3371 | struct lttng_uri *uris = NULL; | |
3372 | ||
3373 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3374 | len = nb_uri * sizeof(struct lttng_uri); | |
3375 | ||
3376 | if (nb_uri > 0) { | |
3377 | uris = zmalloc(len); | |
3378 | if (uris == NULL) { | |
3379 | ret = LTTNG_ERR_FATAL; | |
3380 | goto error; | |
3381 | } | |
3382 | ||
3383 | /* Receive variable len data */ | |
3384 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
3385 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
3386 | if (ret <= 0) { | |
3387 | DBG("No URIs received from client... continuing"); | |
3388 | *sock_error = 1; | |
3389 | ret = LTTNG_ERR_SESSION_FAIL; | |
3390 | free(uris); | |
3391 | goto error; | |
3392 | } | |
3393 | ||
3394 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3395 | DBG("Creating session with ONE network URI is a bad call"); | |
3396 | ret = LTTNG_ERR_SESSION_FAIL; | |
3397 | free(uris); | |
3398 | goto error; | |
3399 | } | |
3400 | } | |
3401 | ||
3402 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, | |
3403 | nb_uri, &cmd_ctx->creds); | |
3404 | free(uris); | |
3405 | break; | |
3406 | } | |
ecc48a90 JD |
3407 | case LTTNG_CREATE_SESSION_LIVE: |
3408 | { | |
3409 | size_t nb_uri, len; | |
3410 | struct lttng_uri *uris = NULL; | |
3411 | ||
3412 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3413 | len = nb_uri * sizeof(struct lttng_uri); | |
3414 | ||
3415 | if (nb_uri > 0) { | |
3416 | uris = zmalloc(len); | |
3417 | if (uris == NULL) { | |
3418 | ret = LTTNG_ERR_FATAL; | |
3419 | goto error; | |
3420 | } | |
3421 | ||
3422 | /* Receive variable len data */ | |
3423 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
3424 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
3425 | if (ret <= 0) { | |
3426 | DBG("No URIs received from client... continuing"); | |
3427 | *sock_error = 1; | |
3428 | ret = LTTNG_ERR_SESSION_FAIL; | |
3429 | free(uris); | |
3430 | goto error; | |
3431 | } | |
3432 | ||
3433 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3434 | DBG("Creating session with ONE network URI is a bad call"); | |
3435 | ret = LTTNG_ERR_SESSION_FAIL; | |
3436 | free(uris); | |
3437 | goto error; | |
3438 | } | |
3439 | } | |
3440 | ||
3441 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, | |
3442 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); | |
3443 | free(uris); | |
3444 | break; | |
3445 | } | |
5e16da05 | 3446 | default: |
f73fabfd | 3447 | ret = LTTNG_ERR_UND; |
5461b305 | 3448 | break; |
fac6795d DG |
3449 | } |
3450 | ||
5461b305 | 3451 | error: |
5461b305 DG |
3452 | if (cmd_ctx->llm == NULL) { |
3453 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 3454 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
3455 | goto setup_error; |
3456 | } | |
3457 | } | |
54d01ffb | 3458 | /* Set return code */ |
5461b305 | 3459 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 3460 | setup_error: |
b5541356 | 3461 | if (cmd_ctx->session) { |
54d01ffb | 3462 | session_unlock(cmd_ctx->session); |
b5541356 | 3463 | } |
256a5576 MD |
3464 | if (need_tracing_session) { |
3465 | session_unlock_list(); | |
3466 | } | |
54d01ffb | 3467 | init_setup_error: |
8028d920 | 3468 | return ret; |
fac6795d DG |
3469 | } |
3470 | ||
44a5e5eb DG |
3471 | /* |
3472 | * Thread managing health check socket. | |
3473 | */ | |
3474 | static void *thread_manage_health(void *data) | |
3475 | { | |
eb4a2943 | 3476 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
3477 | uint32_t revents, nb_fd; |
3478 | struct lttng_poll_event events; | |
0c89d795 MD |
3479 | struct health_comm_msg msg; |
3480 | struct health_comm_reply reply; | |
44a5e5eb DG |
3481 | |
3482 | DBG("[thread] Manage health check started"); | |
3483 | ||
3484 | rcu_register_thread(); | |
3485 | ||
6d737ce4 DG |
3486 | /* We might hit an error path before this is created. */ |
3487 | lttng_poll_init(&events); | |
3cc04881 | 3488 | |
44a5e5eb DG |
3489 | /* Create unix socket */ |
3490 | sock = lttcomm_create_unix_sock(health_unix_sock_path); | |
3491 | if (sock < 0) { | |
3492 | ERR("Unable to create health check Unix socket"); | |
3493 | ret = -1; | |
3494 | goto error; | |
3495 | } | |
3496 | ||
6c71277b MD |
3497 | if (is_root) { |
3498 | /* lttng health client socket path permissions */ | |
3499 | ret = chown(health_unix_sock_path, 0, | |
3500 | utils_get_group_id(tracing_group_name)); | |
3501 | if (ret < 0) { | |
3502 | ERR("Unable to set group on %s", health_unix_sock_path); | |
3503 | PERROR("chown"); | |
3504 | ret = -1; | |
3505 | goto error; | |
3506 | } | |
3507 | ||
3508 | ret = chmod(health_unix_sock_path, | |
3509 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3510 | if (ret < 0) { | |
3511 | ERR("Unable to set permissions on %s", health_unix_sock_path); | |
3512 | PERROR("chmod"); | |
3513 | ret = -1; | |
3514 | goto error; | |
3515 | } | |
3516 | } | |
3517 | ||
b662582b DG |
3518 | /* |
3519 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3520 | * show must go on. | |
3521 | */ | |
3522 | (void) utils_set_fd_cloexec(sock); | |
3523 | ||
44a5e5eb DG |
3524 | ret = lttcomm_listen_unix_sock(sock); |
3525 | if (ret < 0) { | |
3526 | goto error; | |
3527 | } | |
3528 | ||
3529 | /* | |
3530 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3531 | * more will be added to this poll set. | |
3532 | */ | |
d0b96690 | 3533 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
3534 | if (ret < 0) { |
3535 | goto error; | |
3536 | } | |
3537 | ||
3538 | /* Add the application registration socket */ | |
3539 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
3540 | if (ret < 0) { | |
3541 | goto error; | |
3542 | } | |
3543 | ||
3544 | while (1) { | |
3545 | DBG("Health check ready"); | |
3546 | ||
44a5e5eb DG |
3547 | /* Inifinite blocking call, waiting for transmission */ |
3548 | restart: | |
3549 | ret = lttng_poll_wait(&events, -1); | |
3550 | if (ret < 0) { | |
3551 | /* | |
3552 | * Restart interrupted system call. | |
3553 | */ | |
3554 | if (errno == EINTR) { | |
3555 | goto restart; | |
3556 | } | |
3557 | goto error; | |
3558 | } | |
3559 | ||
0d9c5d77 DG |
3560 | nb_fd = ret; |
3561 | ||
44a5e5eb DG |
3562 | for (i = 0; i < nb_fd; i++) { |
3563 | /* Fetch once the poll data */ | |
3564 | revents = LTTNG_POLL_GETEV(&events, i); | |
3565 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3566 | ||
3567 | /* Thread quit pipe has been closed. Killing thread. */ | |
d0b96690 | 3568 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 3569 | if (ret) { |
139ac872 MD |
3570 | err = 0; |
3571 | goto exit; | |
44a5e5eb DG |
3572 | } |
3573 | ||
3574 | /* Event on the registration socket */ | |
3575 | if (pollfd == sock) { | |
3576 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3577 | ERR("Health socket poll error"); | |
3578 | goto error; | |
3579 | } | |
3580 | } | |
3581 | } | |
3582 | ||
3583 | new_sock = lttcomm_accept_unix_sock(sock); | |
3584 | if (new_sock < 0) { | |
3585 | goto error; | |
3586 | } | |
3587 | ||
b662582b DG |
3588 | /* |
3589 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3590 | * show must go on. | |
3591 | */ | |
3592 | (void) utils_set_fd_cloexec(new_sock); | |
3593 | ||
44a5e5eb DG |
3594 | DBG("Receiving data from client for health..."); |
3595 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
3596 | if (ret <= 0) { | |
3597 | DBG("Nothing recv() from client... continuing"); | |
3598 | ret = close(new_sock); | |
3599 | if (ret) { | |
3600 | PERROR("close"); | |
3601 | } | |
3602 | new_sock = -1; | |
3603 | continue; | |
3604 | } | |
3605 | ||
3606 | rcu_thread_online(); | |
3607 | ||
6c71277b MD |
3608 | reply.ret_code = 0; |
3609 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { | |
3610 | /* | |
3611 | * health_check_state returns 0 if health is | |
3612 | * bad. | |
3613 | */ | |
3614 | if (!health_check_state(health_sessiond, i)) { | |
3615 | reply.ret_code |= 1ULL << i; | |
3616 | } | |
44a5e5eb DG |
3617 | } |
3618 | ||
6c71277b | 3619 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
44a5e5eb DG |
3620 | |
3621 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
3622 | if (ret < 0) { | |
3623 | ERR("Failed to send health data back to client"); | |
3624 | } | |
3625 | ||
3626 | /* End of transmission */ | |
3627 | ret = close(new_sock); | |
3628 | if (ret) { | |
3629 | PERROR("close"); | |
3630 | } | |
3631 | new_sock = -1; | |
3632 | } | |
3633 | ||
139ac872 | 3634 | exit: |
44a5e5eb | 3635 | error: |
139ac872 MD |
3636 | if (err) { |
3637 | ERR("Health error occurred in %s", __func__); | |
3638 | } | |
44a5e5eb DG |
3639 | DBG("Health check thread dying"); |
3640 | unlink(health_unix_sock_path); | |
3641 | if (sock >= 0) { | |
3642 | ret = close(sock); | |
3643 | if (ret) { | |
3644 | PERROR("close"); | |
3645 | } | |
3646 | } | |
44a5e5eb DG |
3647 | |
3648 | lttng_poll_clean(&events); | |
3649 | ||
3650 | rcu_unregister_thread(); | |
3651 | return NULL; | |
3652 | } | |
3653 | ||
1d4b027a | 3654 | /* |
d063d709 DG |
3655 | * This thread manage all clients request using the unix client socket for |
3656 | * communication. | |
1d4b027a DG |
3657 | */ |
3658 | static void *thread_manage_clients(void *data) | |
3659 | { | |
139ac872 | 3660 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 3661 | int sock_error; |
5eb91c98 | 3662 | uint32_t revents, nb_fd; |
273ea72c | 3663 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 3664 | struct lttng_poll_event events; |
1d4b027a DG |
3665 | |
3666 | DBG("[thread] Manage client started"); | |
3667 | ||
f6a9efaa DG |
3668 | rcu_register_thread(); |
3669 | ||
6c71277b | 3670 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
927ca06a | 3671 | |
6993eeb3 CB |
3672 | if (testpoint(thread_manage_clients)) { |
3673 | goto error_testpoint; | |
3674 | } | |
3675 | ||
840cb59c | 3676 | health_code_update(); |
44a5e5eb | 3677 | |
1d4b027a DG |
3678 | ret = lttcomm_listen_unix_sock(client_sock); |
3679 | if (ret < 0) { | |
35df2755 | 3680 | goto error_listen; |
1d4b027a DG |
3681 | } |
3682 | ||
5eb91c98 DG |
3683 | /* |
3684 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3685 | * more will be added to this poll set. | |
3686 | */ | |
d0b96690 | 3687 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 3688 | if (ret < 0) { |
35df2755 | 3689 | goto error_create_poll; |
5eb91c98 | 3690 | } |
273ea72c | 3691 | |
5eb91c98 DG |
3692 | /* Add the application registration socket */ |
3693 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
3694 | if (ret < 0) { | |
3695 | goto error; | |
3696 | } | |
273ea72c | 3697 | |
bbd973c2 DG |
3698 | /* |
3699 | * Notify parent pid that we are ready to accept command for client side. | |
1d4b027a DG |
3700 | */ |
3701 | if (opt_sig_parent) { | |
8db8d1dc | 3702 | kill(ppid, SIGUSR1); |
1d4b027a DG |
3703 | } |
3704 | ||
6993eeb3 CB |
3705 | if (testpoint(thread_manage_clients_before_loop)) { |
3706 | goto error; | |
3707 | } | |
8ac94142 | 3708 | |
840cb59c | 3709 | health_code_update(); |
44a5e5eb | 3710 | |
1d4b027a | 3711 | while (1) { |
1d4b027a | 3712 | DBG("Accepting client command ..."); |
273ea72c DG |
3713 | |
3714 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 3715 | restart: |
a78af745 | 3716 | health_poll_entry(); |
5eb91c98 | 3717 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 3718 | health_poll_exit(); |
273ea72c | 3719 | if (ret < 0) { |
88f2b785 MD |
3720 | /* |
3721 | * Restart interrupted system call. | |
3722 | */ | |
3723 | if (errno == EINTR) { | |
3724 | goto restart; | |
3725 | } | |
273ea72c DG |
3726 | goto error; |
3727 | } | |
3728 | ||
0d9c5d77 DG |
3729 | nb_fd = ret; |
3730 | ||
5eb91c98 DG |
3731 | for (i = 0; i < nb_fd; i++) { |
3732 | /* Fetch once the poll data */ | |
3733 | revents = LTTNG_POLL_GETEV(&events, i); | |
3734 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3735 | ||
840cb59c | 3736 | health_code_update(); |
44a5e5eb | 3737 | |
5eb91c98 | 3738 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 3739 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 3740 | if (ret) { |
139ac872 MD |
3741 | err = 0; |
3742 | goto exit; | |
5eb91c98 DG |
3743 | } |
3744 | ||
3745 | /* Event on the registration socket */ | |
3746 | if (pollfd == client_sock) { | |
3747 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3748 | ERR("Client socket poll error"); | |
3749 | goto error; | |
3750 | } | |
3751 | } | |
3752 | } | |
3753 | ||
3754 | DBG("Wait for client response"); | |
3755 | ||
840cb59c | 3756 | health_code_update(); |
44a5e5eb | 3757 | |
5eb91c98 DG |
3758 | sock = lttcomm_accept_unix_sock(client_sock); |
3759 | if (sock < 0) { | |
273ea72c | 3760 | goto error; |
273ea72c DG |
3761 | } |
3762 | ||
b662582b DG |
3763 | /* |
3764 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3765 | * show must go on. | |
3766 | */ | |
3767 | (void) utils_set_fd_cloexec(sock); | |
3768 | ||
be040666 DG |
3769 | /* Set socket option for credentials retrieval */ |
3770 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
3771 | if (ret < 0) { | |
3772 | goto error; | |
3773 | } | |
3774 | ||
5eb91c98 | 3775 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 3776 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 3777 | if (cmd_ctx == NULL) { |
76d7553f | 3778 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 3779 | goto error; |
5eb91c98 | 3780 | } |
1d4b027a | 3781 | |
5eb91c98 | 3782 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 3783 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 3784 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 3785 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
3786 | goto error; |
3787 | } | |
1d4b027a | 3788 | |
5eb91c98 DG |
3789 | cmd_ctx->llm = NULL; |
3790 | cmd_ctx->session = NULL; | |
1d4b027a | 3791 | |
840cb59c | 3792 | health_code_update(); |
44a5e5eb | 3793 | |
5eb91c98 DG |
3794 | /* |
3795 | * Data is received from the lttng client. The struct | |
3796 | * lttcomm_session_msg (lsm) contains the command and data request of | |
3797 | * the client. | |
3798 | */ | |
3799 | DBG("Receiving data from client ..."); | |
be040666 DG |
3800 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
3801 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
3802 | if (ret <= 0) { |
3803 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
3804 | ret = close(sock); |
3805 | if (ret) { | |
3806 | PERROR("close"); | |
3807 | } | |
3808 | sock = -1; | |
a2c0da86 | 3809 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3810 | continue; |
3811 | } | |
1d4b027a | 3812 | |
840cb59c | 3813 | health_code_update(); |
44a5e5eb | 3814 | |
5eb91c98 DG |
3815 | // TODO: Validate cmd_ctx including sanity check for |
3816 | // security purpose. | |
f7776ea7 | 3817 | |
f6a9efaa | 3818 | rcu_thread_online(); |
5eb91c98 DG |
3819 | /* |
3820 | * This function dispatch the work to the kernel or userspace tracer | |
3821 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
3822 | * informations for the client. The command context struct contains | |
3823 | * everything this function may needs. | |
3824 | */ | |
53a80697 | 3825 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 3826 | rcu_thread_offline(); |
5eb91c98 | 3827 | if (ret < 0) { |
36134aa1 DG |
3828 | ret = close(sock); |
3829 | if (ret) { | |
3830 | PERROR("close"); | |
53a80697 | 3831 | } |
36134aa1 | 3832 | sock = -1; |
bbd973c2 | 3833 | /* |
5eb91c98 | 3834 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 3835 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
3836 | * (ENOMEM). Error detected but still accept |
3837 | * command, unless a socket error has been | |
3838 | * detected. | |
bbd973c2 | 3839 | */ |
bbd973c2 | 3840 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3841 | continue; |
3842 | } | |
d6e4fca4 | 3843 | |
840cb59c | 3844 | health_code_update(); |
44a5e5eb | 3845 | |
54d01ffb DG |
3846 | DBG("Sending response (size: %d, retcode: %s)", |
3847 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 3848 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 3849 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
3850 | if (ret < 0) { |
3851 | ERR("Failed to send data back to client"); | |
bbd973c2 | 3852 | } |
1d4b027a | 3853 | |
5eb91c98 | 3854 | /* End of transmission */ |
76d7553f MD |
3855 | ret = close(sock); |
3856 | if (ret) { | |
3857 | PERROR("close"); | |
3858 | } | |
3859 | sock = -1; | |
be040666 DG |
3860 | |
3861 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 3862 | |
840cb59c | 3863 | health_code_update(); |
273ea72c DG |
3864 | } |
3865 | ||
139ac872 | 3866 | exit: |
5eb91c98 | 3867 | error: |
35df2755 CB |
3868 | if (sock >= 0) { |
3869 | ret = close(sock); | |
3870 | if (ret) { | |
3871 | PERROR("close"); | |
3872 | } | |
139ac872 | 3873 | } |
44a5e5eb | 3874 | |
35df2755 CB |
3875 | lttng_poll_clean(&events); |
3876 | clean_command_ctx(&cmd_ctx); | |
3877 | ||
3878 | error_listen: | |
3879 | error_create_poll: | |
6993eeb3 | 3880 | error_testpoint: |
273ea72c | 3881 | unlink(client_unix_sock_path); |
76d7553f MD |
3882 | if (client_sock >= 0) { |
3883 | ret = close(client_sock); | |
3884 | if (ret) { | |
3885 | PERROR("close"); | |
3886 | } | |
a4b35e07 | 3887 | } |
35df2755 CB |
3888 | |
3889 | if (err) { | |
840cb59c | 3890 | health_error(); |
35df2755 | 3891 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 3892 | } |
273ea72c | 3893 | |
8782cc74 | 3894 | health_unregister(health_sessiond); |
35df2755 CB |
3895 | |
3896 | DBG("Client thread dying"); | |
f6a9efaa DG |
3897 | |
3898 | rcu_unregister_thread(); | |
1d4b027a DG |
3899 | return NULL; |
3900 | } | |
3901 | ||
3902 | ||
fac6795d DG |
3903 | /* |
3904 | * usage function on stderr | |
3905 | */ | |
3906 | static void usage(void) | |
3907 | { | |
b716ce68 | 3908 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
3909 | fprintf(stderr, " -h, --help Display this usage.\n"); |
3910 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
3911 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
3912 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
3913 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
7753dea8 MD |
3914 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
3915 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); | |
3916 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); | |
3917 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); | |
ebaeda94 MD |
3918 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
3919 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); | |
3920 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); | |
3921 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); | |
d6f42150 DG |
3922 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
3923 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
3924 | fprintf(stderr, " -V, --version Show version number.\n"); | |
3925 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
3926 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
3927 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
35f90c40 | 3928 | fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n"); |
3bd1e081 | 3929 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
4fba7219 | 3930 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
4d076222 | 3931 | fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n"); |
fac6795d DG |
3932 | } |
3933 | ||
3934 | /* | |
3935 | * daemon argument parsing | |
3936 | */ | |
3937 | static int parse_args(int argc, char **argv) | |
3938 | { | |
3939 | int c; | |
3940 | ||
3941 | static struct option long_options[] = { | |
3942 | { "client-sock", 1, 0, 'c' }, | |
3943 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
3944 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
3945 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
7753dea8 MD |
3946 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
3947 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
ebaeda94 MD |
3948 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
3949 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
3950 | { "consumerd32-path", 1, 0, 'u' }, | |
3951 | { "consumerd32-libdir", 1, 0, 'U' }, | |
3952 | { "consumerd64-path", 1, 0, 't' }, | |
3953 | { "consumerd64-libdir", 1, 0, 'T' }, | |
fac6795d | 3954 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 3955 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
3956 | { "help", 0, 0, 'h' }, |
3957 | { "group", 1, 0, 'g' }, | |
3958 | { "version", 0, 0, 'V' }, | |
75462a81 | 3959 | { "quiet", 0, 0, 'q' }, |
3f9947db | 3960 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 3961 | { "verbose-consumer", 0, 0, 'Z' }, |
4fba7219 | 3962 | { "no-kernel", 0, 0, 'N' }, |
35f90c40 | 3963 | { "pidfile", 1, 0, 'p' }, |
4d076222 | 3964 | { "jul-tcp-port", 1, 0, 'J' }, |
fac6795d DG |
3965 | { NULL, 0, 0, 0 } |
3966 | }; | |
3967 | ||
3968 | while (1) { | |
3969 | int option_index = 0; | |
4d076222 | 3970 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:", |
54d01ffb | 3971 | long_options, &option_index); |
fac6795d DG |
3972 | if (c == -1) { |
3973 | break; | |
3974 | } | |
3975 | ||
3976 | switch (c) { | |
3977 | case 0: | |
3978 | fprintf(stderr, "option %s", long_options[option_index].name); | |
3979 | if (optarg) { | |
3980 | fprintf(stderr, " with arg %s\n", optarg); | |
3981 | } | |
3982 | break; | |
b716ce68 | 3983 | case 'c': |
fac6795d DG |
3984 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
3985 | break; | |
3986 | case 'a': | |
3987 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
3988 | break; | |
3989 | case 'd': | |
3990 | opt_daemon = 1; | |
3991 | break; | |
3992 | case 'g': | |
6c71277b | 3993 | tracing_group_name = optarg; |
fac6795d DG |
3994 | break; |
3995 | case 'h': | |
3996 | usage(); | |
3997 | exit(EXIT_FAILURE); | |
3998 | case 'V': | |
3999 | fprintf(stdout, "%s\n", VERSION); | |
4000 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
4001 | case 'S': |
4002 | opt_sig_parent = 1; | |
4003 | break; | |
d6f42150 | 4004 | case 'E': |
3bd1e081 | 4005 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
4006 | break; |
4007 | case 'C': | |
3bd1e081 MD |
4008 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
4009 | break; | |
4010 | case 'F': | |
7753dea8 | 4011 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
3bd1e081 MD |
4012 | break; |
4013 | case 'D': | |
7753dea8 MD |
4014 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
4015 | break; | |
4016 | case 'H': | |
4017 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
4018 | break; | |
4019 | case 'G': | |
4020 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 4021 | break; |
4fba7219 DG |
4022 | case 'N': |
4023 | opt_no_kernel = 1; | |
4024 | break; | |
75462a81 | 4025 | case 'q': |
97e19046 | 4026 | lttng_opt_quiet = 1; |
75462a81 | 4027 | break; |
3f9947db | 4028 | case 'v': |
53086306 | 4029 | /* Verbose level can increase using multiple -v */ |
97e19046 | 4030 | lttng_opt_verbose += 1; |
3f9947db | 4031 | break; |
31f73cc9 | 4032 | case 'Z': |
3bd1e081 | 4033 | opt_verbose_consumer += 1; |
31f73cc9 | 4034 | break; |
fb09408a | 4035 | case 'u': |
fc7a59ce | 4036 | consumerd32_bin= optarg; |
ebaeda94 MD |
4037 | break; |
4038 | case 'U': | |
4039 | consumerd32_libdir = optarg; | |
7753dea8 MD |
4040 | break; |
4041 | case 't': | |
fc7a59ce | 4042 | consumerd64_bin = optarg; |
ebaeda94 MD |
4043 | break; |
4044 | case 'T': | |
4045 | consumerd64_libdir = optarg; | |
fb09408a | 4046 | break; |
35f90c40 DG |
4047 | case 'p': |
4048 | opt_pidfile = optarg; | |
4049 | break; | |
4d076222 DG |
4050 | case 'J': /* JUL TCP port. */ |
4051 | { | |
4052 | unsigned long v; | |
4053 | ||
4054 | errno = 0; | |
4055 | v = strtoul(optarg, NULL, 0); | |
4056 | if (errno != 0 || !isdigit(optarg[0])) { | |
4057 | ERR("Wrong value in --jul-tcp-port parameter: %s", optarg); | |
4058 | return -1; | |
4059 | } | |
4060 | if (v == 0 || v >= 65535) { | |
4061 | ERR("Port overflow in --jul-tcp-port parameter: %s", optarg); | |
4062 | return -1; | |
4063 | } | |
4064 | jul_tcp_port = (uint32_t) v; | |
4065 | DBG3("JUL TCP port set to non default: %u", jul_tcp_port); | |
4066 | break; | |
4067 | } | |
fac6795d DG |
4068 | default: |
4069 | /* Unknown option or other error. | |
4070 | * Error is printed by getopt, just return */ | |
4071 | return -1; | |
4072 | } | |
4073 | } | |
4074 | ||
4075 | return 0; | |
4076 | } | |
4077 | ||
4078 | /* | |
d063d709 | 4079 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
4080 | * apps_sock - The communication socket for all UST apps. |
4081 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 4082 | */ |
cf3af59e | 4083 | static int init_daemon_socket(void) |
fac6795d DG |
4084 | { |
4085 | int ret = 0; | |
4086 | mode_t old_umask; | |
4087 | ||
4088 | old_umask = umask(0); | |
4089 | ||
4090 | /* Create client tool unix socket */ | |
d6f42150 DG |
4091 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
4092 | if (client_sock < 0) { | |
4093 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
4094 | ret = -1; |
4095 | goto end; | |
4096 | } | |
4097 | ||
b662582b DG |
4098 | /* Set the cloexec flag */ |
4099 | ret = utils_set_fd_cloexec(client_sock); | |
4100 | if (ret < 0) { | |
4101 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
4102 | "Continuing but note that the consumer daemon will have a " | |
4103 | "reference to this socket on exec()", client_sock); | |
4104 | } | |
4105 | ||
fac6795d DG |
4106 | /* File permission MUST be 660 */ |
4107 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
4108 | if (ret < 0) { | |
d6f42150 | 4109 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
76d7553f | 4110 | PERROR("chmod"); |
fac6795d DG |
4111 | goto end; |
4112 | } | |
4113 | ||
4114 | /* Create the application unix socket */ | |
d6f42150 DG |
4115 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
4116 | if (apps_sock < 0) { | |
4117 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
4118 | ret = -1; |
4119 | goto end; | |
4120 | } | |
4121 | ||
b662582b DG |
4122 | /* Set the cloexec flag */ |
4123 | ret = utils_set_fd_cloexec(apps_sock); | |
4124 | if (ret < 0) { | |
4125 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
4126 | "Continuing but note that the consumer daemon will have a " | |
4127 | "reference to this socket on exec()", apps_sock); | |
4128 | } | |
4129 | ||
d6f42150 | 4130 | /* File permission MUST be 666 */ |
54d01ffb DG |
4131 | ret = chmod(apps_unix_sock_path, |
4132 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 4133 | if (ret < 0) { |
d6f42150 | 4134 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
76d7553f | 4135 | PERROR("chmod"); |
fac6795d DG |
4136 | goto end; |
4137 | } | |
4138 | ||
b662582b DG |
4139 | DBG3("Session daemon client socket %d and application socket %d created", |
4140 | client_sock, apps_sock); | |
4141 | ||
fac6795d DG |
4142 | end: |
4143 | umask(old_umask); | |
4144 | return ret; | |
4145 | } | |
4146 | ||
4147 | /* | |
54d01ffb DG |
4148 | * Check if the global socket is available, and if a daemon is answering at the |
4149 | * other side. If yes, error is returned. | |
fac6795d | 4150 | */ |
cf3af59e | 4151 | static int check_existing_daemon(void) |
fac6795d | 4152 | { |
7d8234d9 | 4153 | /* Is there anybody out there ? */ |
099e26bd | 4154 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 4155 | return -EEXIST; |
099e26bd | 4156 | } |
b09c7c76 DG |
4157 | |
4158 | return 0; | |
fac6795d DG |
4159 | } |
4160 | ||
fac6795d | 4161 | /* |
d063d709 | 4162 | * Set the tracing group gid onto the client socket. |
5e16da05 | 4163 | * |
d063d709 | 4164 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 4165 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 4166 | */ |
be040666 | 4167 | static int set_permissions(char *rundir) |
fac6795d DG |
4168 | { |
4169 | int ret; | |
996b65c8 | 4170 | gid_t gid; |
fac6795d | 4171 | |
6c71277b | 4172 | gid = utils_get_group_id(tracing_group_name); |
fac6795d | 4173 | |
d6f42150 | 4174 | /* Set lttng run dir */ |
be040666 | 4175 | ret = chown(rundir, 0, gid); |
d6f42150 | 4176 | if (ret < 0) { |
be040666 | 4177 | ERR("Unable to set group on %s", rundir); |
76d7553f | 4178 | PERROR("chown"); |
d6f42150 DG |
4179 | } |
4180 | ||
6c71277b MD |
4181 | /* |
4182 | * Ensure all applications and tracing group can search the run | |
4183 | * dir. Allow everyone to read the directory, since it does not | |
4184 | * buy us anything to hide its content. | |
4185 | */ | |
4186 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
4187 | if (ret < 0) { |
4188 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 4189 | PERROR("chmod"); |
d1613cf5 JN |
4190 | } |
4191 | ||
d6f42150 | 4192 | /* lttng client socket path */ |
996b65c8 | 4193 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 4194 | if (ret < 0) { |
d6f42150 | 4195 | ERR("Unable to set group on %s", client_unix_sock_path); |
76d7553f | 4196 | PERROR("chown"); |
d6f42150 DG |
4197 | } |
4198 | ||
3bd1e081 | 4199 | /* kconsumer error socket path */ |
6c71277b | 4200 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 4201 | if (ret < 0) { |
3bd1e081 | 4202 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 4203 | PERROR("chown"); |
3bd1e081 MD |
4204 | } |
4205 | ||
7753dea8 | 4206 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 4207 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
4208 | if (ret < 0) { |
4209 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 4210 | PERROR("chown"); |
7753dea8 MD |
4211 | } |
4212 | ||
4213 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 4214 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 4215 | if (ret < 0) { |
7753dea8 | 4216 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 4217 | PERROR("chown"); |
fac6795d DG |
4218 | } |
4219 | ||
d6f42150 | 4220 | DBG("All permissions are set"); |
e07ae692 | 4221 | |
fac6795d DG |
4222 | return ret; |
4223 | } | |
4224 | ||
d6f42150 | 4225 | /* |
d063d709 | 4226 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 4227 | */ |
67e40797 | 4228 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
4229 | { |
4230 | int ret; | |
4231 | ||
67e40797 DG |
4232 | DBG3("Creating LTTng run directory: %s", rundir); |
4233 | ||
d1613cf5 | 4234 | ret = mkdir(rundir, S_IRWXU); |
d6f42150 | 4235 | if (ret < 0) { |
b1f11e69 | 4236 | if (errno != EEXIST) { |
67e40797 | 4237 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
4238 | goto error; |
4239 | } else { | |
4240 | ret = 0; | |
4241 | } | |
d6f42150 DG |
4242 | } |
4243 | ||
4244 | error: | |
4245 | return ret; | |
4246 | } | |
4247 | ||
4248 | /* | |
d063d709 DG |
4249 | * Setup sockets and directory needed by the kconsumerd communication with the |
4250 | * session daemon. | |
d6f42150 | 4251 | */ |
67e40797 DG |
4252 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
4253 | const char *rundir) | |
d6f42150 DG |
4254 | { |
4255 | int ret; | |
67e40797 | 4256 | char path[PATH_MAX]; |
d6f42150 | 4257 | |
6c71277b | 4258 | switch (consumer_data->type) { |
7753dea8 | 4259 | case LTTNG_CONSUMER_KERNEL: |
60922cb0 | 4260 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
7753dea8 MD |
4261 | break; |
4262 | case LTTNG_CONSUMER64_UST: | |
60922cb0 | 4263 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
4264 | break; |
4265 | case LTTNG_CONSUMER32_UST: | |
60922cb0 | 4266 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
4267 | break; |
4268 | default: | |
4269 | ERR("Consumer type unknown"); | |
4270 | ret = -EINVAL; | |
4271 | goto error; | |
d6f42150 DG |
4272 | } |
4273 | ||
67e40797 DG |
4274 | DBG2("Creating consumer directory: %s", path); |
4275 | ||
6c71277b | 4276 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
d6f42150 | 4277 | if (ret < 0) { |
6beb2242 | 4278 | if (errno != EEXIST) { |
f11e84c2 | 4279 | PERROR("mkdir"); |
3bd1e081 | 4280 | ERR("Failed to create %s", path); |
6beb2242 DG |
4281 | goto error; |
4282 | } | |
f11e84c2 | 4283 | ret = -1; |
d6f42150 | 4284 | } |
6c71277b MD |
4285 | if (is_root) { |
4286 | ret = chown(path, 0, utils_get_group_id(tracing_group_name)); | |
4287 | if (ret < 0) { | |
4288 | ERR("Unable to set group on %s", path); | |
4289 | PERROR("chown"); | |
4290 | goto error; | |
4291 | } | |
4292 | } | |
d6f42150 DG |
4293 | |
4294 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
4295 | consumer_data->err_sock = |
4296 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
4297 | if (consumer_data->err_sock < 0) { | |
4298 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
4299 | ret = -1; |
4300 | goto error; | |
4301 | } | |
4302 | ||
a24f05ab MD |
4303 | /* |
4304 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4305 | * show must go on. | |
4306 | */ | |
4307 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
4308 | if (ret < 0) { | |
4309 | PERROR("utils_set_fd_cloexec"); | |
4310 | /* continue anyway */ | |
4311 | } | |
4312 | ||
d6f42150 | 4313 | /* File permission MUST be 660 */ |
3bd1e081 | 4314 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 4315 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 4316 | if (ret < 0) { |
3bd1e081 | 4317 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 4318 | PERROR("chmod"); |
d6f42150 DG |
4319 | goto error; |
4320 | } | |
4321 | ||
4322 | error: | |
4323 | return ret; | |
4324 | } | |
4325 | ||
fac6795d | 4326 | /* |
d063d709 | 4327 | * Signal handler for the daemon |
cf3af59e | 4328 | * |
54d01ffb DG |
4329 | * Simply stop all worker threads, leaving main() return gracefully after |
4330 | * joining all threads and calling cleanup(). | |
fac6795d DG |
4331 | */ |
4332 | static void sighandler(int sig) | |
4333 | { | |
4334 | switch (sig) { | |
cf3af59e | 4335 | case SIGPIPE: |
af87c45a | 4336 | DBG("SIGPIPE caught"); |
cf3af59e MD |
4337 | return; |
4338 | case SIGINT: | |
af87c45a | 4339 | DBG("SIGINT caught"); |
cf3af59e MD |
4340 | stop_threads(); |
4341 | break; | |
4342 | case SIGTERM: | |
af87c45a | 4343 | DBG("SIGTERM caught"); |
cf3af59e MD |
4344 | stop_threads(); |
4345 | break; | |
4346 | default: | |
4347 | break; | |
fac6795d | 4348 | } |
fac6795d DG |
4349 | } |
4350 | ||
4351 | /* | |
d063d709 | 4352 | * Setup signal handler for : |
1d4b027a | 4353 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 4354 | */ |
1d4b027a | 4355 | static int set_signal_handler(void) |
fac6795d | 4356 | { |
1d4b027a DG |
4357 | int ret = 0; |
4358 | struct sigaction sa; | |
4359 | sigset_t sigset; | |
fac6795d | 4360 | |
1d4b027a | 4361 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 4362 | PERROR("sigemptyset"); |
1d4b027a DG |
4363 | return ret; |
4364 | } | |
d6f42150 | 4365 | |
1d4b027a DG |
4366 | sa.sa_handler = sighandler; |
4367 | sa.sa_mask = sigset; | |
4368 | sa.sa_flags = 0; | |
4369 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
76d7553f | 4370 | PERROR("sigaction"); |
1d4b027a | 4371 | return ret; |
d6f42150 DG |
4372 | } |
4373 | ||
1d4b027a | 4374 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 4375 | PERROR("sigaction"); |
1d4b027a | 4376 | return ret; |
d6f42150 | 4377 | } |
aaf26714 | 4378 | |
1d4b027a | 4379 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
76d7553f | 4380 | PERROR("sigaction"); |
1d4b027a | 4381 | return ret; |
8c0faa1d DG |
4382 | } |
4383 | ||
1d4b027a DG |
4384 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
4385 | ||
4386 | return ret; | |
fac6795d DG |
4387 | } |
4388 | ||
f3ed775e | 4389 | /* |
d063d709 DG |
4390 | * Set open files limit to unlimited. This daemon can open a large number of |
4391 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
4392 | */ |
4393 | static void set_ulimit(void) | |
4394 | { | |
4395 | int ret; | |
4396 | struct rlimit lim; | |
4397 | ||
a88df331 | 4398 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
4399 | lim.rlim_cur = 65535; |
4400 | lim.rlim_max = 65535; | |
4401 | ||
4402 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
4403 | if (ret < 0) { | |
76d7553f | 4404 | PERROR("failed to set open files limit"); |
f3ed775e DG |
4405 | } |
4406 | } | |
4407 | ||
35f90c40 DG |
4408 | /* |
4409 | * Write pidfile using the rundir and opt_pidfile. | |
4410 | */ | |
4411 | static void write_pidfile(void) | |
4412 | { | |
4413 | int ret; | |
4414 | char pidfile_path[PATH_MAX]; | |
4415 | ||
4416 | assert(rundir); | |
4417 | ||
4418 | if (opt_pidfile) { | |
4419 | strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path)); | |
4420 | } else { | |
4421 | /* Build pidfile path from rundir and opt_pidfile. */ | |
4422 | ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/" | |
4423 | DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir); | |
4424 | if (ret < 0) { | |
4425 | PERROR("snprintf pidfile path"); | |
4426 | goto error; | |
4427 | } | |
4428 | } | |
4429 | ||
4430 | /* | |
4431 | * Create pid file in rundir. Return value is of no importance. The | |
4432 | * execution will continue even though we are not able to write the file. | |
4433 | */ | |
4434 | (void) utils_create_pid_file(getpid(), pidfile_path); | |
4435 | ||
4436 | error: | |
4437 | return; | |
4438 | } | |
4439 | ||
cd9290dd DG |
4440 | /* |
4441 | * Write JUL TCP port using the rundir. | |
4442 | */ | |
4443 | static void write_julport(void) | |
4444 | { | |
4445 | int ret; | |
4446 | char path[PATH_MAX]; | |
4447 | ||
4448 | assert(rundir); | |
4449 | ||
4450 | ret = snprintf(path, sizeof(path), "%s/" | |
4451 | DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir); | |
4452 | if (ret < 0) { | |
4453 | PERROR("snprintf julport path"); | |
4454 | goto error; | |
4455 | } | |
4456 | ||
4457 | /* | |
4458 | * Create TCP JUL port file in rundir. Return value is of no importance. | |
4459 | * The execution will continue even though we are not able to write the | |
4460 | * file. | |
4461 | */ | |
4462 | (void) utils_create_pid_file(jul_tcp_port, path); | |
4463 | ||
4464 | error: | |
4465 | return; | |
4466 | } | |
4467 | ||
fac6795d DG |
4468 | /* |
4469 | * main | |
4470 | */ | |
4471 | int main(int argc, char **argv) | |
4472 | { | |
fac6795d DG |
4473 | int ret = 0; |
4474 | void *status; | |
ae9e45b3 | 4475 | const char *home_path, *env_app_timeout; |
fac6795d | 4476 | |
335a95b7 MD |
4477 | init_kernel_workarounds(); |
4478 | ||
f6a9efaa DG |
4479 | rcu_register_thread(); |
4480 | ||
7753dea8 | 4481 | setup_consumerd_path(); |
fb09408a | 4482 | |
12744796 DG |
4483 | page_size = sysconf(_SC_PAGESIZE); |
4484 | if (page_size < 0) { | |
4485 | PERROR("sysconf _SC_PAGESIZE"); | |
4486 | page_size = LONG_MAX; | |
4487 | WARN("Fallback page size to %ld", page_size); | |
4488 | } | |
4489 | ||
fac6795d DG |
4490 | /* Parse arguments */ |
4491 | progname = argv[0]; | |
c617c0c6 | 4492 | if ((ret = parse_args(argc, argv)) < 0) { |
cf3af59e | 4493 | goto error; |
fac6795d DG |
4494 | } |
4495 | ||
4496 | /* Daemonize */ | |
4497 | if (opt_daemon) { | |
ceed52b5 MD |
4498 | int i; |
4499 | ||
4500 | /* | |
4501 | * fork | |
4502 | * child: setsid, close FD 0, 1, 2, chdir / | |
4503 | * parent: exit (if fork is successful) | |
4504 | */ | |
53094c05 DG |
4505 | ret = daemon(0, 0); |
4506 | if (ret < 0) { | |
76d7553f | 4507 | PERROR("daemon"); |
cf3af59e | 4508 | goto error; |
53094c05 | 4509 | } |
ceed52b5 MD |
4510 | /* |
4511 | * We are in the child. Make sure all other file | |
4512 | * descriptors are closed, in case we are called with | |
4513 | * more opened file descriptors than the standard ones. | |
4514 | */ | |
4515 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
4516 | (void) close(i); | |
4517 | } | |
4518 | } | |
4519 | ||
4520 | /* Create thread quit pipe */ | |
4521 | if ((ret = init_thread_quit_pipe()) < 0) { | |
4522 | goto error; | |
fac6795d DG |
4523 | } |
4524 | ||
4525 | /* Check if daemon is UID = 0 */ | |
4526 | is_root = !getuid(); | |
4527 | ||
fac6795d | 4528 | if (is_root) { |
990570ed | 4529 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
67e40797 DG |
4530 | |
4531 | /* Create global run dir with root access */ | |
4532 | ret = create_lttng_rundir(rundir); | |
d6f42150 | 4533 | if (ret < 0) { |
cf3af59e | 4534 | goto error; |
d6f42150 DG |
4535 | } |
4536 | ||
fac6795d | 4537 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
4538 | snprintf(apps_unix_sock_path, PATH_MAX, |
4539 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
4540 | } |
4541 | ||
4542 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
4543 | snprintf(client_unix_sock_path, PATH_MAX, |
4544 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
4545 | } | |
0fdd1e2c DG |
4546 | |
4547 | /* Set global SHM for ust */ | |
4548 | if (strlen(wait_shm_path) == 0) { | |
4549 | snprintf(wait_shm_path, PATH_MAX, | |
4550 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
4551 | } | |
67e40797 | 4552 | |
44a5e5eb DG |
4553 | if (strlen(health_unix_sock_path) == 0) { |
4554 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4555 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); | |
4556 | } | |
4557 | ||
67e40797 DG |
4558 | /* Setup kernel consumerd path */ |
4559 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4560 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
67e40797 | 4561 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4562 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4563 | |
4564 | DBG2("Kernel consumer err path: %s", | |
4565 | kconsumer_data.err_unix_sock_path); | |
4566 | DBG2("Kernel consumer cmd path: %s", | |
4567 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 4568 | } else { |
feb0f3e5 | 4569 | home_path = utils_get_home_dir(); |
b082db07 | 4570 | if (home_path == NULL) { |
273ea72c DG |
4571 | /* TODO: Add --socket PATH option */ |
4572 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
4573 | ret = -EPERM; |
4574 | goto error; | |
b082db07 DG |
4575 | } |
4576 | ||
67e40797 DG |
4577 | /* |
4578 | * Create rundir from home path. This will create something like | |
4579 | * $HOME/.lttng | |
4580 | */ | |
990570ed | 4581 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
67e40797 DG |
4582 | if (ret < 0) { |
4583 | ret = -ENOMEM; | |
4584 | goto error; | |
4585 | } | |
4586 | ||
4587 | ret = create_lttng_rundir(rundir); | |
4588 | if (ret < 0) { | |
4589 | goto error; | |
4590 | } | |
4591 | ||
fac6795d | 4592 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 4593 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 4594 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
4595 | } |
4596 | ||
4597 | /* Set the cli tool unix socket path */ | |
4598 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 4599 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 4600 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 4601 | } |
0fdd1e2c DG |
4602 | |
4603 | /* Set global SHM for ust */ | |
4604 | if (strlen(wait_shm_path) == 0) { | |
4605 | snprintf(wait_shm_path, PATH_MAX, | |
d0b96690 | 4606 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); |
0fdd1e2c | 4607 | } |
44a5e5eb DG |
4608 | |
4609 | /* Set health check Unix path */ | |
4610 | if (strlen(health_unix_sock_path) == 0) { | |
4611 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4612 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); | |
4613 | } | |
fac6795d DG |
4614 | } |
4615 | ||
5c827ce0 DG |
4616 | /* Set consumer initial state */ |
4617 | kernel_consumerd_state = CONSUMER_STOPPED; | |
4618 | ust_consumerd_state = CONSUMER_STOPPED; | |
4619 | ||
847177cd DG |
4620 | DBG("Client socket path %s", client_unix_sock_path); |
4621 | DBG("Application socket path %s", apps_unix_sock_path); | |
d0b96690 | 4622 | DBG("Application wait path %s", wait_shm_path); |
67e40797 DG |
4623 | DBG("LTTng run directory path: %s", rundir); |
4624 | ||
4625 | /* 32 bits consumerd path setup */ | |
4626 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4627 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
67e40797 | 4628 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4629 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4630 | |
4631 | DBG2("UST consumer 32 bits err path: %s", | |
4632 | ustconsumer32_data.err_unix_sock_path); | |
4633 | DBG2("UST consumer 32 bits cmd path: %s", | |
4634 | ustconsumer32_data.cmd_unix_sock_path); | |
4635 | ||
4636 | /* 64 bits consumerd path setup */ | |
4637 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4638 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
67e40797 | 4639 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4640 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4641 | |
4642 | DBG2("UST consumer 64 bits err path: %s", | |
4643 | ustconsumer64_data.err_unix_sock_path); | |
4644 | DBG2("UST consumer 64 bits cmd path: %s", | |
4645 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 4646 | |
273ea72c | 4647 | /* |
7d8234d9 | 4648 | * See if daemon already exist. |
fac6795d | 4649 | */ |
7d8234d9 | 4650 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 4651 | ERR("Already running daemon.\n"); |
273ea72c | 4652 | /* |
cf3af59e MD |
4653 | * We do not goto exit because we must not cleanup() |
4654 | * because a daemon is already running. | |
ab118b20 | 4655 | */ |
cf3af59e | 4656 | goto error; |
a88df331 DG |
4657 | } |
4658 | ||
1427f9b2 DG |
4659 | /* |
4660 | * Init UST app hash table. Alloc hash table before this point since | |
4661 | * cleanup() can get called after that point. | |
4662 | */ | |
4663 | ust_app_ht_alloc(); | |
4664 | ||
f20baf8e DG |
4665 | /* Initialize JUL domain subsystem. */ |
4666 | if ((ret = jul_init()) < 0) { | |
4667 | /* ENOMEM at this point. */ | |
4668 | goto error; | |
4669 | } | |
4670 | ||
54d01ffb | 4671 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
4672 | |
4673 | /* | |
4674 | * These actions must be executed as root. We do that *after* setting up | |
4675 | * the sockets path because we MUST make the check for another daemon using | |
4676 | * those paths *before* trying to set the kernel consumer sockets and init | |
4677 | * kernel tracer. | |
4678 | */ | |
4679 | if (is_root) { | |
67e40797 | 4680 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
7753dea8 MD |
4681 | if (ret < 0) { |
4682 | goto exit; | |
4683 | } | |
4684 | ||
a88df331 | 4685 | /* Setup kernel tracer */ |
4fba7219 DG |
4686 | if (!opt_no_kernel) { |
4687 | init_kernel_tracer(); | |
4688 | } | |
a88df331 DG |
4689 | |
4690 | /* Set ulimit for open files */ | |
4691 | set_ulimit(); | |
fac6795d | 4692 | } |
4063050c MD |
4693 | /* init lttng_fd tracking must be done after set_ulimit. */ |
4694 | lttng_fd_init(); | |
fac6795d | 4695 | |
67e40797 DG |
4696 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
4697 | if (ret < 0) { | |
4698 | goto exit; | |
4699 | } | |
4700 | ||
4701 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); | |
4702 | if (ret < 0) { | |
4703 | goto exit; | |
4704 | } | |
4705 | ||
cf3af59e MD |
4706 | if ((ret = set_signal_handler()) < 0) { |
4707 | goto exit; | |
fac6795d DG |
4708 | } |
4709 | ||
d6f42150 | 4710 | /* Setup the needed unix socket */ |
cf3af59e MD |
4711 | if ((ret = init_daemon_socket()) < 0) { |
4712 | goto exit; | |
fac6795d DG |
4713 | } |
4714 | ||
4715 | /* Set credentials to socket */ | |
be040666 | 4716 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
cf3af59e | 4717 | goto exit; |
fac6795d DG |
4718 | } |
4719 | ||
5b8719f5 DG |
4720 | /* Get parent pid if -S, --sig-parent is specified. */ |
4721 | if (opt_sig_parent) { | |
4722 | ppid = getppid(); | |
4723 | } | |
4724 | ||
7a485870 | 4725 | /* Setup the kernel pipe for waking up the kernel thread */ |
6620da75 DG |
4726 | if (is_root && !opt_no_kernel) { |
4727 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { | |
4728 | goto exit; | |
4729 | } | |
7a485870 DG |
4730 | } |
4731 | ||
0b2dc8df MD |
4732 | /* Setup the thread ht_cleanup communication pipe. */ |
4733 | if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) { | |
4734 | goto exit; | |
4735 | } | |
4736 | ||
099e26bd | 4737 | /* Setup the thread apps communication pipe. */ |
ef599319 | 4738 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
099e26bd DG |
4739 | goto exit; |
4740 | } | |
4741 | ||
d0b96690 DG |
4742 | /* Setup the thread apps notify communication pipe. */ |
4743 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) { | |
4744 | goto exit; | |
4745 | } | |
4746 | ||
7972aab2 DG |
4747 | /* Initialize global buffer per UID and PID registry. */ |
4748 | buffer_reg_init_uid_registry(); | |
4749 | buffer_reg_init_pid_registry(); | |
4750 | ||
099e26bd DG |
4751 | /* Init UST command queue. */ |
4752 | cds_wfq_init(&ust_cmd_queue.queue); | |
4753 | ||
273ea72c | 4754 | /* |
54d01ffb DG |
4755 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
4756 | * statically declared in session.c | |
273ea72c | 4757 | */ |
54d01ffb | 4758 | session_list_ptr = session_get_list(); |
b5541356 | 4759 | |
5eb91c98 DG |
4760 | /* Set up max poll set size */ |
4761 | lttng_poll_set_max_size(); | |
4762 | ||
2f77fc4b | 4763 | cmd_init(); |
00e2e675 | 4764 | |
ae9e45b3 DG |
4765 | /* Check for the application socket timeout env variable. */ |
4766 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
4767 | if (env_app_timeout) { | |
4768 | app_socket_timeout = atoi(env_app_timeout); | |
4769 | } else { | |
4770 | app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; | |
4771 | } | |
4772 | ||
35f90c40 | 4773 | write_pidfile(); |
cd9290dd | 4774 | write_julport(); |
35f90c40 | 4775 | |
554831e7 MD |
4776 | /* Initialize communication library */ |
4777 | lttcomm_init(); | |
d831c249 DG |
4778 | /* This is to get the TCP timeout value. */ |
4779 | lttcomm_inet_init(); | |
554831e7 | 4780 | |
67e05644 DG |
4781 | /* |
4782 | * Initialize the health check subsystem. This call should set the | |
4783 | * appropriate time values. | |
4784 | */ | |
6c71277b | 4785 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
8782cc74 MD |
4786 | if (!health_sessiond) { |
4787 | PERROR("health_app_create error"); | |
4788 | goto exit_health_sessiond_cleanup; | |
4789 | } | |
67e05644 | 4790 | |
0b2dc8df MD |
4791 | /* Create thread to manage the client socket */ |
4792 | ret = pthread_create(&ht_cleanup_thread, NULL, | |
4793 | thread_ht_cleanup, (void *) NULL); | |
4794 | if (ret != 0) { | |
4795 | PERROR("pthread_create ht_cleanup"); | |
4796 | goto exit_ht_cleanup; | |
4797 | } | |
4798 | ||
44a5e5eb DG |
4799 | /* Create thread to manage the client socket */ |
4800 | ret = pthread_create(&health_thread, NULL, | |
4801 | thread_manage_health, (void *) NULL); | |
4802 | if (ret != 0) { | |
4803 | PERROR("pthread_create health"); | |
4804 | goto exit_health; | |
4805 | } | |
4806 | ||
cf3af59e | 4807 | /* Create thread to manage the client socket */ |
099e26bd DG |
4808 | ret = pthread_create(&client_thread, NULL, |
4809 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 4810 | if (ret != 0) { |
76d7553f | 4811 | PERROR("pthread_create clients"); |
cf3af59e MD |
4812 | goto exit_client; |
4813 | } | |
fac6795d | 4814 | |
099e26bd DG |
4815 | /* Create thread to dispatch registration */ |
4816 | ret = pthread_create(&dispatch_thread, NULL, | |
4817 | thread_dispatch_ust_registration, (void *) NULL); | |
4818 | if (ret != 0) { | |
76d7553f | 4819 | PERROR("pthread_create dispatch"); |
099e26bd DG |
4820 | goto exit_dispatch; |
4821 | } | |
4822 | ||
4823 | /* Create thread to manage application registration. */ | |
4824 | ret = pthread_create(®_apps_thread, NULL, | |
4825 | thread_registration_apps, (void *) NULL); | |
4826 | if (ret != 0) { | |
76d7553f | 4827 | PERROR("pthread_create registration"); |
099e26bd DG |
4828 | goto exit_reg_apps; |
4829 | } | |
4830 | ||
cf3af59e | 4831 | /* Create thread to manage application socket */ |
54d01ffb DG |
4832 | ret = pthread_create(&apps_thread, NULL, |
4833 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 4834 | if (ret != 0) { |
d0b96690 DG |
4835 | PERROR("pthread_create apps"); |
4836 | goto exit_apps; | |
4837 | } | |
4838 | ||
4839 | /* Create thread to manage application notify socket */ | |
4840 | ret = pthread_create(&apps_notify_thread, NULL, | |
4841 | ust_thread_manage_notify, (void *) NULL); | |
4842 | if (ret != 0) { | |
76d7553f | 4843 | PERROR("pthread_create apps"); |
9563b0ad | 4844 | goto exit_apps_notify; |
cf3af59e | 4845 | } |
fac6795d | 4846 | |
4d076222 DG |
4847 | /* Create JUL registration thread. */ |
4848 | ret = pthread_create(&jul_reg_thread, NULL, | |
4849 | jul_thread_manage_registration, (void *) NULL); | |
4850 | if (ret != 0) { | |
4851 | PERROR("pthread_create apps"); | |
4852 | goto exit_jul_reg; | |
4853 | } | |
4854 | ||
6620da75 DG |
4855 | /* Don't start this thread if kernel tracing is not requested nor root */ |
4856 | if (is_root && !opt_no_kernel) { | |
4857 | /* Create kernel thread to manage kernel event */ | |
4858 | ret = pthread_create(&kernel_thread, NULL, | |
4859 | thread_manage_kernel, (void *) NULL); | |
4860 | if (ret != 0) { | |
4861 | PERROR("pthread_create kernel"); | |
4862 | goto exit_kernel; | |
4863 | } | |
7a485870 | 4864 | |
6620da75 DG |
4865 | ret = pthread_join(kernel_thread, &status); |
4866 | if (ret != 0) { | |
4867 | PERROR("pthread_join"); | |
4868 | goto error; /* join error, exit without cleanup */ | |
4869 | } | |
fac6795d DG |
4870 | } |
4871 | ||
cf3af59e | 4872 | exit_kernel: |
4d076222 DG |
4873 | ret = pthread_join(jul_reg_thread, &status); |
4874 | if (ret != 0) { | |
4875 | PERROR("pthread_join JUL"); | |
4876 | goto error; /* join error, exit without cleanup */ | |
4877 | } | |
4878 | ||
4879 | exit_jul_reg: | |
9563b0ad DG |
4880 | ret = pthread_join(apps_notify_thread, &status); |
4881 | if (ret != 0) { | |
4882 | PERROR("pthread_join apps notify"); | |
4883 | goto error; /* join error, exit without cleanup */ | |
4884 | } | |
4885 | ||
4886 | exit_apps_notify: | |
cf3af59e MD |
4887 | ret = pthread_join(apps_thread, &status); |
4888 | if (ret != 0) { | |
9563b0ad | 4889 | PERROR("pthread_join apps"); |
cf3af59e MD |
4890 | goto error; /* join error, exit without cleanup */ |
4891 | } | |
fac6795d | 4892 | |
4ccd8c8f | 4893 | |
cf3af59e | 4894 | exit_apps: |
099e26bd DG |
4895 | ret = pthread_join(reg_apps_thread, &status); |
4896 | if (ret != 0) { | |
76d7553f | 4897 | PERROR("pthread_join"); |
099e26bd DG |
4898 | goto error; /* join error, exit without cleanup */ |
4899 | } | |
4900 | ||
4901 | exit_reg_apps: | |
4902 | ret = pthread_join(dispatch_thread, &status); | |
4903 | if (ret != 0) { | |
76d7553f | 4904 | PERROR("pthread_join"); |
099e26bd DG |
4905 | goto error; /* join error, exit without cleanup */ |
4906 | } | |
4907 | ||
4908 | exit_dispatch: | |
cf3af59e MD |
4909 | ret = pthread_join(client_thread, &status); |
4910 | if (ret != 0) { | |
76d7553f | 4911 | PERROR("pthread_join"); |
cf3af59e MD |
4912 | goto error; /* join error, exit without cleanup */ |
4913 | } | |
4914 | ||
3bd1e081 | 4915 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 4916 | if (ret != 0) { |
76d7553f | 4917 | PERROR("join_consumer"); |
cf3af59e MD |
4918 | goto error; /* join error, exit without cleanup */ |
4919 | } | |
a88df331 | 4920 | |
06f525de DG |
4921 | ret = join_consumer_thread(&ustconsumer32_data); |
4922 | if (ret != 0) { | |
4923 | PERROR("join_consumer ust32"); | |
4924 | goto error; /* join error, exit without cleanup */ | |
4925 | } | |
4926 | ||
4927 | ret = join_consumer_thread(&ustconsumer64_data); | |
4928 | if (ret != 0) { | |
4929 | PERROR("join_consumer ust64"); | |
4930 | goto error; /* join error, exit without cleanup */ | |
4931 | } | |
4932 | ||
cf3af59e | 4933 | exit_client: |
06f525de DG |
4934 | ret = pthread_join(health_thread, &status); |
4935 | if (ret != 0) { | |
4936 | PERROR("pthread_join health thread"); | |
4937 | goto error; /* join error, exit without cleanup */ | |
4938 | } | |
4939 | ||
44a5e5eb | 4940 | exit_health: |
0b2dc8df MD |
4941 | ret = pthread_join(ht_cleanup_thread, &status); |
4942 | if (ret != 0) { | |
4943 | PERROR("pthread_join ht cleanup thread"); | |
4944 | goto error; /* join error, exit without cleanup */ | |
4945 | } | |
4946 | exit_ht_cleanup: | |
8782cc74 MD |
4947 | health_app_destroy(health_sessiond); |
4948 | exit_health_sessiond_cleanup: | |
a88df331 | 4949 | exit: |
cf3af59e MD |
4950 | /* |
4951 | * cleanup() is called when no other thread is running. | |
4952 | */ | |
f6a9efaa | 4953 | rcu_thread_online(); |
cf3af59e | 4954 | cleanup(); |
f6a9efaa DG |
4955 | rcu_thread_offline(); |
4956 | rcu_unregister_thread(); | |
67e40797 | 4957 | if (!ret) { |
cf3af59e | 4958 | exit(EXIT_SUCCESS); |
67e40797 | 4959 | } |
cf3af59e | 4960 | error: |
5e16da05 | 4961 | exit(EXIT_FAILURE); |
fac6795d | 4962 | } |