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