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