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