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> | |
8c0faa1d | 24 | #include <semaphore.h> |
fac6795d DG |
25 | #include <signal.h> |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
0fdd1e2c | 29 | #include <sys/mman.h> |
b73401da | 30 | #include <sys/mount.h> |
1e307fab | 31 | #include <sys/resource.h> |
fac6795d DG |
32 | #include <sys/socket.h> |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
0fdd1e2c | 35 | #include <sys/wait.h> |
5c827ce0 | 36 | #include <urcu/uatomic.h> |
fac6795d | 37 | #include <unistd.h> |
3bd1e081 | 38 | #include <config.h> |
fac6795d | 39 | |
990570ed | 40 | #include <common/common.h> |
0ba98ebc | 41 | #include <common/compat/poll.h> |
d27c42b8 | 42 | #include <common/compat/socket.h> |
db758600 DG |
43 | #include <common/defaults.h> |
44 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 45 | #include <common/futex.h> |
00e2e675 | 46 | #include <common/relayd/relayd.h> |
fac6795d | 47 | |
10a8a223 | 48 | #include "lttng-sessiond.h" |
54d01ffb | 49 | #include "channel.h" |
00e2e675 | 50 | #include "consumer.h" |
099e26bd | 51 | #include "context.h" |
54d01ffb | 52 | #include "event.h" |
4771f025 | 53 | #include "kernel.h" |
f1e16794 | 54 | #include "kernel-consumer.h" |
096102bd | 55 | #include "modprobe.h" |
0fdd1e2c | 56 | #include "shm.h" |
1e307fab | 57 | #include "ust-ctl.h" |
00e2e675 | 58 | #include "ust-consumer.h" |
8e68d1c8 | 59 | #include "utils.h" |
4063050c | 60 | #include "fd-limit.h" |
53a80697 | 61 | #include "filter.h" |
44a5e5eb | 62 | #include "health.h" |
fac6795d | 63 | |
ebaeda94 MD |
64 | #define CONSUMERD_FILE "lttng-consumerd" |
65 | ||
75462a81 | 66 | /* Const values */ |
686204ab | 67 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
bbccc3d2 | 68 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
686204ab MD |
69 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
70 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
71 | ||
fac6795d DG |
72 | const char *progname; |
73 | const char *opt_tracing_group; | |
5b8719f5 | 74 | static int opt_sig_parent; |
97e19046 | 75 | static int opt_verbose_consumer; |
fac6795d | 76 | static int opt_daemon; |
4fba7219 | 77 | static int opt_no_kernel; |
fac6795d | 78 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 79 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
67e40797 | 80 | static char *rundir; |
3bd1e081 MD |
81 | |
82 | /* Consumer daemon specific control data */ | |
83 | static struct consumer_data kconsumer_data = { | |
84 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
85 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
86 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
87 | .err_sock = -1, |
88 | .cmd_sock = -1, | |
3bd1e081 | 89 | }; |
7753dea8 MD |
90 | static struct consumer_data ustconsumer64_data = { |
91 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
92 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
93 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
94 | .err_sock = -1, |
95 | .cmd_sock = -1, | |
7753dea8 MD |
96 | }; |
97 | static struct consumer_data ustconsumer32_data = { | |
98 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
99 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
100 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
101 | .err_sock = -1, |
102 | .cmd_sock = -1, | |
3bd1e081 MD |
103 | }; |
104 | ||
099e26bd | 105 | static int dispatch_thread_exit; |
fac6795d | 106 | |
54d01ffb DG |
107 | /* Global application Unix socket path */ |
108 | static char apps_unix_sock_path[PATH_MAX]; | |
109 | /* Global client Unix socket path */ | |
110 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
111 | /* global wait shm path for UST */ |
112 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
113 | /* Global health check unix path */ |
114 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 115 | |
1d4b027a | 116 | /* Sockets and FDs */ |
a4b35e07 MD |
117 | static int client_sock = -1; |
118 | static int apps_sock = -1; | |
119 | static int kernel_tracer_fd = -1; | |
76d7553f | 120 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 121 | |
273ea72c DG |
122 | /* |
123 | * Quit pipe for all threads. This permits a single cancellation point | |
124 | * for all threads when receiving an event on the pipe. | |
125 | */ | |
76d7553f | 126 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 127 | |
099e26bd DG |
128 | /* |
129 | * This pipe is used to inform the thread managing application communication | |
130 | * that a command is queued and ready to be processed. | |
131 | */ | |
76d7553f | 132 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 133 | |
1d4b027a | 134 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 135 | static pthread_t apps_thread; |
099e26bd | 136 | static pthread_t reg_apps_thread; |
1d4b027a | 137 | static pthread_t client_thread; |
7a485870 | 138 | static pthread_t kernel_thread; |
099e26bd | 139 | static pthread_t dispatch_thread; |
44a5e5eb | 140 | static pthread_t health_thread; |
5eb91c98 | 141 | |
099e26bd DG |
142 | /* |
143 | * UST registration command queue. This queue is tied with a futex and uses a N | |
144 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
145 | * | |
146 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
147 | * this queue and the wait/wake scheme. | |
148 | */ | |
149 | static struct ust_cmd_queue ust_cmd_queue; | |
150 | ||
b5541356 DG |
151 | /* |
152 | * Pointer initialized before thread creation. | |
153 | * | |
154 | * This points to the tracing session list containing the session count and a | |
155 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
156 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 157 | * |
d063d709 | 158 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 159 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
160 | */ |
161 | static struct ltt_session_list *session_list_ptr; | |
162 | ||
7753dea8 MD |
163 | int ust_consumerd64_fd = -1; |
164 | int ust_consumerd32_fd = -1; | |
165 | ||
fb6f1fa2 YB |
166 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
167 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
168 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
169 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
fb09408a | 170 | |
5c827ce0 DG |
171 | /* |
172 | * Consumer daemon state which is changed when spawning it, killing it or in | |
173 | * case of a fatal error. | |
174 | */ | |
175 | enum consumerd_state { | |
176 | CONSUMER_STARTED = 1, | |
177 | CONSUMER_STOPPED = 2, | |
178 | CONSUMER_ERROR = 3, | |
179 | }; | |
180 | ||
181 | /* | |
182 | * This consumer daemon state is used to validate if a client command will be | |
183 | * able to reach the consumer. If not, the client is informed. For instance, | |
184 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
185 | * error to the client. | |
186 | * | |
187 | * The following example shows a possible race condition of this scheme: | |
188 | * | |
189 | * consumer thread error happens | |
190 | * client cmd arrives | |
191 | * client cmd checks state -> still OK | |
192 | * consumer thread exit, sets error | |
193 | * client cmd try to talk to consumer | |
194 | * ... | |
195 | * | |
196 | * However, since the consumer is a different daemon, we have no way of making | |
197 | * sure the command will reach it safely even with this state flag. This is why | |
198 | * we consider that up to the state validation during command processing, the | |
199 | * command is safe. After that, we can not guarantee the correctness of the | |
200 | * client request vis-a-vis the consumer. | |
201 | */ | |
202 | static enum consumerd_state ust_consumerd_state; | |
203 | static enum consumerd_state kernel_consumerd_state; | |
204 | ||
00e2e675 DG |
205 | /* |
206 | * Used to keep a unique index for each relayd socket created where this value | |
207 | * is associated with streams on the consumer so it can match the right relayd | |
208 | * to send to. | |
209 | * | |
210 | * This value should be incremented atomically for safety purposes and future | |
211 | * possible concurrent access. | |
212 | */ | |
213 | static unsigned int relayd_net_seq_idx; | |
214 | ||
44a5e5eb DG |
215 | /* Used for the health monitoring of the session daemon. See health.h */ |
216 | struct health_state health_thread_cmd; | |
217 | struct health_state health_thread_app_reg; | |
218 | struct health_state health_thread_kernel; | |
219 | ||
fb09408a | 220 | static |
7753dea8 | 221 | void setup_consumerd_path(void) |
fb09408a | 222 | { |
fc7a59ce | 223 | const char *bin, *libdir; |
fb09408a | 224 | |
7753dea8 MD |
225 | /* |
226 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
227 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
228 | * has not been defined. | |
7753dea8 | 229 | */ |
ebaeda94 | 230 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
231 | if (!consumerd32_bin[0]) { |
232 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
233 | } |
234 | if (!consumerd32_libdir[0]) { | |
235 | consumerd32_libdir = INSTALL_LIB_PATH; | |
236 | } | |
237 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
238 | if (!consumerd64_bin[0]) { |
239 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 240 | } |
ebaeda94 MD |
241 | if (!consumerd64_libdir[0]) { |
242 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
243 | } |
244 | #else | |
245 | #error "Unknown bitness" | |
246 | #endif | |
247 | ||
fb09408a MD |
248 | /* |
249 | * runtime env. var. overrides the build default. | |
250 | */ | |
fc7a59ce AM |
251 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
252 | if (bin) { | |
253 | consumerd32_bin = bin; | |
7753dea8 | 254 | } |
fc7a59ce AM |
255 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
256 | if (bin) { | |
257 | consumerd64_bin = bin; | |
ebaeda94 | 258 | } |
72f579ee | 259 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
260 | if (libdir) { |
261 | consumerd32_libdir = libdir; | |
262 | } | |
72f579ee | 263 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
264 | if (libdir) { |
265 | consumerd64_libdir = libdir; | |
fb09408a MD |
266 | } |
267 | } | |
268 | ||
5eb91c98 DG |
269 | /* |
270 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
271 | */ | |
272 | static int create_thread_poll_set(struct lttng_poll_event *events, | |
273 | unsigned int size) | |
274 | { | |
275 | int ret; | |
276 | ||
277 | if (events == NULL || size == 0) { | |
278 | ret = -1; | |
279 | goto error; | |
280 | } | |
281 | ||
282 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
283 | if (ret < 0) { | |
284 | goto error; | |
285 | } | |
286 | ||
287 | /* Add quit pipe */ | |
288 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); | |
289 | if (ret < 0) { | |
290 | goto error; | |
291 | } | |
292 | ||
293 | return 0; | |
294 | ||
295 | error: | |
296 | return ret; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Check if the thread quit pipe was triggered. | |
301 | * | |
302 | * Return 1 if it was triggered else 0; | |
303 | */ | |
304 | static int check_thread_quit_pipe(int fd, uint32_t events) | |
305 | { | |
306 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
307 | return 1; | |
308 | } | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
0fdd1e2c DG |
313 | /* |
314 | * Return group ID of the tracing group or -1 if not found. | |
315 | */ | |
996b65c8 MD |
316 | static gid_t allowed_group(void) |
317 | { | |
318 | struct group *grp; | |
319 | ||
274e143b MD |
320 | if (opt_tracing_group) { |
321 | grp = getgrnam(opt_tracing_group); | |
322 | } else { | |
323 | grp = getgrnam(default_tracing_group); | |
324 | } | |
996b65c8 MD |
325 | if (!grp) { |
326 | return -1; | |
327 | } else { | |
328 | return grp->gr_gid; | |
329 | } | |
330 | } | |
331 | ||
273ea72c | 332 | /* |
5eb91c98 | 333 | * Init thread quit pipe. |
273ea72c DG |
334 | * |
335 | * Return -1 on error or 0 if all pipes are created. | |
336 | */ | |
337 | static int init_thread_quit_pipe(void) | |
338 | { | |
730389d9 | 339 | int ret, i; |
273ea72c | 340 | |
730389d9 | 341 | ret = pipe(thread_quit_pipe); |
273ea72c | 342 | if (ret < 0) { |
730389d9 | 343 | PERROR("thread quit pipe"); |
273ea72c DG |
344 | goto error; |
345 | } | |
346 | ||
730389d9 DG |
347 | for (i = 0; i < 2; i++) { |
348 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
349 | if (ret < 0) { | |
350 | PERROR("fcntl"); | |
351 | goto error; | |
352 | } | |
353 | } | |
354 | ||
273ea72c DG |
355 | error: |
356 | return ret; | |
357 | } | |
358 | ||
fac6795d | 359 | /* |
d063d709 DG |
360 | * Complete teardown of a kernel session. This free all data structure related |
361 | * to a kernel session and update counter. | |
fac6795d | 362 | */ |
1d4b027a | 363 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 364 | { |
8f7a84dd | 365 | if (!session->kernel_session) { |
99bab54f | 366 | DBG3("No kernel session when tearing down session"); |
84ea9c99 | 367 | return; |
8f7a84dd DG |
368 | } |
369 | ||
84ea9c99 | 370 | DBG("Tearing down kernel session"); |
d9800920 | 371 | |
84ea9c99 MD |
372 | /* |
373 | * If a custom kernel consumer was registered, close the socket before | |
374 | * tearing down the complete kernel session structure | |
375 | */ | |
03550b58 MD |
376 | if (kconsumer_data.cmd_sock >= 0 && |
377 | session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) { | |
84ea9c99 | 378 | lttcomm_close_unix_sock(session->kernel_session->consumer_fd); |
fac6795d | 379 | } |
84ea9c99 MD |
380 | |
381 | trace_kernel_destroy_session(session->kernel_session); | |
fac6795d DG |
382 | } |
383 | ||
f6a9efaa DG |
384 | /* |
385 | * Complete teardown of all UST sessions. This will free everything on his path | |
386 | * and destroy the core essence of all ust sessions :) | |
387 | */ | |
388 | static void teardown_ust_session(struct ltt_session *session) | |
389 | { | |
84cd17c6 MD |
390 | int ret; |
391 | ||
8f7a84dd | 392 | if (!session->ust_session) { |
99bab54f | 393 | DBG3("No UST session when tearing down session"); |
f470a390 | 394 | return; |
8f7a84dd DG |
395 | } |
396 | ||
84ea9c99 | 397 | DBG("Tearing down UST session(s)"); |
8f7a84dd | 398 | |
84cd17c6 MD |
399 | ret = ust_app_destroy_trace_all(session->ust_session); |
400 | if (ret) { | |
401 | ERR("Error in ust_app_destroy_trace_all"); | |
402 | } | |
8f7a84dd | 403 | |
f6a9efaa DG |
404 | trace_ust_destroy_session(session->ust_session); |
405 | } | |
406 | ||
099e26bd DG |
407 | /* |
408 | * Stop all threads by closing the thread quit pipe. | |
409 | */ | |
cf3af59e MD |
410 | static void stop_threads(void) |
411 | { | |
5eb91c98 DG |
412 | int ret; |
413 | ||
cf3af59e MD |
414 | /* Stopping all threads */ |
415 | DBG("Terminating all threads"); | |
54d01ffb | 416 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
417 | if (ret < 0) { |
418 | ERR("write error on thread quit pipe"); | |
419 | } | |
420 | ||
099e26bd DG |
421 | /* Dispatch thread */ |
422 | dispatch_thread_exit = 1; | |
423 | futex_nto1_wake(&ust_cmd_queue.futex); | |
cf3af59e MD |
424 | } |
425 | ||
fac6795d | 426 | /* |
d063d709 | 427 | * Cleanup the daemon |
fac6795d | 428 | */ |
cf3af59e | 429 | static void cleanup(void) |
fac6795d | 430 | { |
ef599319 | 431 | int ret; |
1d4b027a | 432 | char *cmd; |
af9737e9 | 433 | struct ltt_session *sess, *stmp; |
fac6795d | 434 | |
1d4b027a | 435 | DBG("Cleaning up"); |
e07ae692 | 436 | |
67e40797 DG |
437 | DBG("Removing %s directory", rundir); |
438 | ret = asprintf(&cmd, "rm -rf %s", rundir); | |
439 | if (ret < 0) { | |
440 | ERR("asprintf failed. Something is really wrong!"); | |
441 | } | |
5461b305 | 442 | |
67e40797 DG |
443 | /* Remove lttng run directory */ |
444 | ret = system(cmd); | |
445 | if (ret < 0) { | |
446 | ERR("Unable to clean %s", rundir); | |
1d4b027a | 447 | } |
67e40797 | 448 | free(cmd); |
5461b305 | 449 | |
99bab54f | 450 | DBG("Cleaning up all sessions"); |
fac6795d | 451 | |
b5541356 | 452 | /* Destroy session list mutex */ |
273ea72c DG |
453 | if (session_list_ptr != NULL) { |
454 | pthread_mutex_destroy(&session_list_ptr->lock); | |
455 | ||
456 | /* Cleanup ALL session */ | |
54d01ffb DG |
457 | cds_list_for_each_entry_safe(sess, stmp, |
458 | &session_list_ptr->head, list) { | |
273ea72c | 459 | teardown_kernel_session(sess); |
f6a9efaa DG |
460 | teardown_ust_session(sess); |
461 | free(sess); | |
273ea72c DG |
462 | } |
463 | } | |
464 | ||
099e26bd | 465 | DBG("Closing all UST sockets"); |
56fff090 | 466 | ust_app_clean_list(); |
099e26bd | 467 | |
3bd1e081 | 468 | pthread_mutex_destroy(&kconsumer_data.pid_mutex); |
b5541356 | 469 | |
4fba7219 DG |
470 | if (is_root && !opt_no_kernel) { |
471 | DBG2("Closing kernel fd"); | |
a4b35e07 | 472 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
473 | ret = close(kernel_tracer_fd); |
474 | if (ret) { | |
475 | PERROR("close"); | |
476 | } | |
a4b35e07 | 477 | } |
2f50c8a3 | 478 | DBG("Unloading kernel modules"); |
096102bd | 479 | modprobe_remove_lttng_all(); |
2f50c8a3 | 480 | } |
ef599319 DG |
481 | utils_close_pipe(kernel_poll_pipe); |
482 | utils_close_pipe(thread_quit_pipe); | |
483 | utils_close_pipe(apps_cmd_pipe); | |
421cb601 DG |
484 | |
485 | /* <fun> */ | |
f56a39af | 486 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
487 | "Matthew, BEET driven development works!%c[%dm", |
488 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
489 | /* </fun> */ | |
fac6795d DG |
490 | } |
491 | ||
e065084a | 492 | /* |
d063d709 | 493 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 494 | * |
d063d709 | 495 | * Return lttcomm error code. |
e065084a DG |
496 | */ |
497 | static int send_unix_sock(int sock, void *buf, size_t len) | |
498 | { | |
499 | /* Check valid length */ | |
500 | if (len <= 0) { | |
501 | return -1; | |
502 | } | |
503 | ||
504 | return lttcomm_send_unix_sock(sock, buf, len); | |
505 | } | |
506 | ||
5461b305 | 507 | /* |
d063d709 | 508 | * Free memory of a command context structure. |
5461b305 | 509 | */ |
a2fb29a5 | 510 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 511 | { |
a2fb29a5 DG |
512 | DBG("Clean command context structure"); |
513 | if (*cmd_ctx) { | |
514 | if ((*cmd_ctx)->llm) { | |
515 | free((*cmd_ctx)->llm); | |
5461b305 | 516 | } |
a2fb29a5 DG |
517 | if ((*cmd_ctx)->lsm) { |
518 | free((*cmd_ctx)->lsm); | |
5461b305 | 519 | } |
a2fb29a5 DG |
520 | free(*cmd_ctx); |
521 | *cmd_ctx = NULL; | |
5461b305 DG |
522 | } |
523 | } | |
524 | ||
fac6795d | 525 | /* |
0fdd1e2c | 526 | * Notify UST applications using the shm mmap futex. |
fac6795d | 527 | */ |
0fdd1e2c | 528 | static int notify_ust_apps(int active) |
fac6795d | 529 | { |
0fdd1e2c | 530 | char *wait_shm_mmap; |
fac6795d | 531 | |
0fdd1e2c | 532 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 533 | |
0fdd1e2c DG |
534 | /* See shm.c for this call implying mmap, shm and futex calls */ |
535 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
536 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
537 | goto error; |
538 | } | |
539 | ||
0fdd1e2c DG |
540 | /* Wake waiting process */ |
541 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
542 | ||
543 | /* Apps notified successfully */ | |
544 | return 0; | |
fac6795d DG |
545 | |
546 | error: | |
0fdd1e2c | 547 | return -1; |
fac6795d DG |
548 | } |
549 | ||
e065084a | 550 | /* |
d063d709 DG |
551 | * Setup the outgoing data buffer for the response (llm) by allocating the |
552 | * right amount of memory and copying the original information from the lsm | |
553 | * structure. | |
ca95a216 | 554 | * |
d063d709 | 555 | * Return total size of the buffer pointed by buf. |
ca95a216 | 556 | */ |
5461b305 | 557 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 558 | { |
f3ed775e | 559 | int ret, buf_size; |
ca95a216 | 560 | |
f3ed775e | 561 | buf_size = size; |
5461b305 | 562 | |
ba7f0ae5 | 563 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 564 | if (cmd_ctx->llm == NULL) { |
76d7553f | 565 | PERROR("zmalloc"); |
5461b305 | 566 | ret = -ENOMEM; |
ca95a216 DG |
567 | goto error; |
568 | } | |
569 | ||
5461b305 DG |
570 | /* Copy common data */ |
571 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 572 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 573 | |
5461b305 DG |
574 | cmd_ctx->llm->data_size = size; |
575 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
576 | ||
ca95a216 DG |
577 | return buf_size; |
578 | ||
579 | error: | |
580 | return ret; | |
581 | } | |
582 | ||
7a485870 | 583 | /* |
5eb91c98 | 584 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 585 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 586 | */ |
5eb91c98 | 587 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 588 | { |
5eb91c98 | 589 | int ret; |
7a485870 DG |
590 | struct ltt_session *session; |
591 | struct ltt_kernel_channel *channel; | |
592 | ||
5eb91c98 | 593 | DBG("Updating kernel poll set"); |
7a485870 | 594 | |
54d01ffb | 595 | session_lock_list(); |
b5541356 | 596 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 597 | session_lock(session); |
7a485870 | 598 | if (session->kernel_session == NULL) { |
54d01ffb | 599 | session_unlock(session); |
7a485870 DG |
600 | continue; |
601 | } | |
7a485870 | 602 | |
54d01ffb DG |
603 | cds_list_for_each_entry(channel, |
604 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
605 | /* Add channel fd to the kernel poll set */ |
606 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
607 | if (ret < 0) { | |
54d01ffb | 608 | session_unlock(session); |
5eb91c98 DG |
609 | goto error; |
610 | } | |
611 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 612 | } |
54d01ffb | 613 | session_unlock(session); |
7a485870 | 614 | } |
54d01ffb | 615 | session_unlock_list(); |
7a485870 | 616 | |
5eb91c98 | 617 | return 0; |
7a485870 DG |
618 | |
619 | error: | |
54d01ffb | 620 | session_unlock_list(); |
7a485870 DG |
621 | return -1; |
622 | } | |
623 | ||
624 | /* | |
54d01ffb | 625 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 626 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 627 | * |
d063d709 | 628 | * Useful for CPU hotplug feature. |
7a485870 | 629 | */ |
2bdd86d4 | 630 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
631 | { |
632 | int ret = 0; | |
633 | struct ltt_session *session; | |
634 | struct ltt_kernel_channel *channel; | |
635 | ||
636 | DBG("Updating kernel streams for channel fd %d", fd); | |
637 | ||
54d01ffb | 638 | session_lock_list(); |
b5541356 | 639 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 640 | session_lock(session); |
7a485870 | 641 | if (session->kernel_session == NULL) { |
54d01ffb | 642 | session_unlock(session); |
7a485870 DG |
643 | continue; |
644 | } | |
d9800920 | 645 | |
03550b58 MD |
646 | /* This is not suppose to be -1 but this is an extra security check */ |
647 | if (session->kernel_session->consumer_fd < 0) { | |
3bd1e081 | 648 | session->kernel_session->consumer_fd = consumer_data->cmd_sock; |
d9800920 DG |
649 | } |
650 | ||
5eb91c98 DG |
651 | cds_list_for_each_entry(channel, |
652 | &session->kernel_session->channel_list.head, list) { | |
7a485870 DG |
653 | if (channel->fd == fd) { |
654 | DBG("Channel found, updating kernel streams"); | |
655 | ret = kernel_open_channel_stream(channel); | |
656 | if (ret < 0) { | |
b3c750d2 | 657 | goto error; |
7a485870 | 658 | } |
d9800920 | 659 | |
7a485870 | 660 | /* |
5eb91c98 DG |
661 | * Have we already sent fds to the consumer? If yes, it means |
662 | * that tracing is started so it is safe to send our updated | |
663 | * stream fds. | |
7a485870 | 664 | */ |
00e2e675 DG |
665 | if (session->kernel_session->consumer_fds_sent == 1 && |
666 | session->kernel_session->consumer != NULL) { | |
667 | ret = kernel_consumer_send_channel_stream( | |
668 | session->kernel_session->consumer_fd, channel, | |
669 | session->kernel_session); | |
7a485870 | 670 | if (ret < 0) { |
b3c750d2 | 671 | goto error; |
7a485870 DG |
672 | } |
673 | } | |
b3c750d2 | 674 | goto error; |
7a485870 DG |
675 | } |
676 | } | |
54d01ffb | 677 | session_unlock(session); |
7a485870 | 678 | } |
54d01ffb | 679 | session_unlock_list(); |
b3c750d2 | 680 | return ret; |
7a485870 | 681 | |
b3c750d2 | 682 | error: |
54d01ffb DG |
683 | session_unlock(session); |
684 | session_unlock_list(); | |
7a485870 DG |
685 | return ret; |
686 | } | |
687 | ||
487cf67c DG |
688 | /* |
689 | * For each tracing session, update newly registered apps. | |
690 | */ | |
691 | static void update_ust_app(int app_sock) | |
692 | { | |
693 | struct ltt_session *sess, *stmp; | |
694 | ||
4ee14516 DG |
695 | session_lock_list(); |
696 | ||
487cf67c DG |
697 | /* For all tracing session(s) */ |
698 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 699 | session_lock(sess); |
421cb601 DG |
700 | if (sess->ust_session) { |
701 | ust_app_global_update(sess->ust_session, app_sock); | |
702 | } | |
4ee14516 | 703 | session_unlock(sess); |
487cf67c | 704 | } |
4ee14516 DG |
705 | |
706 | session_unlock_list(); | |
487cf67c DG |
707 | } |
708 | ||
7a485870 | 709 | /* |
d063d709 | 710 | * This thread manage event coming from the kernel. |
7a485870 | 711 | * |
d063d709 DG |
712 | * Features supported in this thread: |
713 | * -) CPU Hotplug | |
7a485870 DG |
714 | */ |
715 | static void *thread_manage_kernel(void *data) | |
716 | { | |
5eb91c98 DG |
717 | int ret, i, pollfd, update_poll_flag = 1; |
718 | uint32_t revents, nb_fd; | |
7a485870 | 719 | char tmp; |
5eb91c98 | 720 | struct lttng_poll_event events; |
7a485870 DG |
721 | |
722 | DBG("Thread manage kernel started"); | |
723 | ||
44a5e5eb DG |
724 | health_code_update(&health_thread_kernel); |
725 | ||
5eb91c98 DG |
726 | ret = create_thread_poll_set(&events, 2); |
727 | if (ret < 0) { | |
76d7553f | 728 | goto error_poll_create; |
5eb91c98 DG |
729 | } |
730 | ||
731 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
732 | if (ret < 0) { | |
733 | goto error; | |
734 | } | |
735 | ||
7a485870 | 736 | while (1) { |
44a5e5eb DG |
737 | health_code_update(&health_thread_kernel); |
738 | ||
7a485870 | 739 | if (update_poll_flag == 1) { |
5f822d0a DG |
740 | /* |
741 | * Reset number of fd in the poll set. Always 2 since there is the thread | |
742 | * quit pipe and the kernel pipe. | |
743 | */ | |
744 | events.nb_fd = 2; | |
745 | ||
5eb91c98 DG |
746 | ret = update_kernel_poll(&events); |
747 | if (ret < 0) { | |
7a485870 DG |
748 | goto error; |
749 | } | |
750 | update_poll_flag = 0; | |
751 | } | |
752 | ||
5eb91c98 DG |
753 | nb_fd = LTTNG_POLL_GETNB(&events); |
754 | ||
755 | DBG("Thread kernel polling on %d fds", nb_fd); | |
756 | ||
757 | /* Zeroed the poll events */ | |
758 | lttng_poll_reset(&events); | |
7a485870 DG |
759 | |
760 | /* Poll infinite value of time */ | |
88f2b785 | 761 | restart: |
44a5e5eb | 762 | health_poll_update(&health_thread_kernel); |
5eb91c98 | 763 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 764 | health_poll_update(&health_thread_kernel); |
7a485870 | 765 | if (ret < 0) { |
88f2b785 MD |
766 | /* |
767 | * Restart interrupted system call. | |
768 | */ | |
769 | if (errno == EINTR) { | |
770 | goto restart; | |
771 | } | |
7a485870 DG |
772 | goto error; |
773 | } else if (ret == 0) { | |
774 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
775 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
776 | "This should not have happened! Continuing..."); | |
7a485870 DG |
777 | continue; |
778 | } | |
779 | ||
5eb91c98 DG |
780 | for (i = 0; i < nb_fd; i++) { |
781 | /* Fetch once the poll data */ | |
782 | revents = LTTNG_POLL_GETEV(&events, i); | |
783 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 784 | |
44a5e5eb DG |
785 | health_code_update(&health_thread_kernel); |
786 | ||
5eb91c98 DG |
787 | /* Thread quit pipe has been closed. Killing thread. */ |
788 | ret = check_thread_quit_pipe(pollfd, revents); | |
789 | if (ret) { | |
790 | goto error; | |
791 | } | |
7a485870 | 792 | |
5eb91c98 DG |
793 | /* Check for data on kernel pipe */ |
794 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
795 | ret = read(kernel_poll_pipe[0], &tmp, 1); | |
796 | update_poll_flag = 1; | |
797 | continue; | |
798 | } else { | |
799 | /* | |
800 | * New CPU detected by the kernel. Adding kernel stream to | |
801 | * kernel session and updating the kernel consumer | |
802 | */ | |
803 | if (revents & LPOLLIN) { | |
2bdd86d4 | 804 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
805 | if (ret < 0) { |
806 | continue; | |
807 | } | |
808 | break; | |
809 | /* | |
810 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
811 | * and unregister kernel stream at this point. | |
812 | */ | |
7a485870 | 813 | } |
7a485870 DG |
814 | } |
815 | } | |
816 | } | |
817 | ||
818 | error: | |
5eb91c98 | 819 | lttng_poll_clean(&events); |
76d7553f | 820 | error_poll_create: |
44a5e5eb | 821 | health_reset(&health_thread_kernel); |
76d7553f | 822 | DBG("Kernel thread dying"); |
7a485870 DG |
823 | return NULL; |
824 | } | |
825 | ||
1d4b027a | 826 | /* |
3bd1e081 | 827 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 828 | */ |
3bd1e081 | 829 | static void *thread_manage_consumer(void *data) |
1d4b027a | 830 | { |
76d7553f | 831 | int sock = -1, i, ret, pollfd; |
5eb91c98 | 832 | uint32_t revents, nb_fd; |
1d4b027a | 833 | enum lttcomm_return_code code; |
5eb91c98 | 834 | struct lttng_poll_event events; |
3bd1e081 | 835 | struct consumer_data *consumer_data = data; |
1d4b027a | 836 | |
3bd1e081 | 837 | DBG("[thread] Manage consumer started"); |
1d4b027a | 838 | |
44a5e5eb DG |
839 | health_code_update(&consumer_data->health); |
840 | ||
3bd1e081 | 841 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
1d4b027a | 842 | if (ret < 0) { |
76d7553f | 843 | goto error_listen; |
1d4b027a DG |
844 | } |
845 | ||
5eb91c98 DG |
846 | /* |
847 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. | |
848 | * Nothing more will be added to this poll set. | |
849 | */ | |
850 | ret = create_thread_poll_set(&events, 2); | |
851 | if (ret < 0) { | |
76d7553f | 852 | goto error_poll; |
5eb91c98 | 853 | } |
273ea72c | 854 | |
3bd1e081 | 855 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
856 | if (ret < 0) { |
857 | goto error; | |
858 | } | |
859 | ||
860 | nb_fd = LTTNG_POLL_GETNB(&events); | |
273ea72c | 861 | |
44a5e5eb DG |
862 | health_code_update(&consumer_data->health); |
863 | ||
273ea72c | 864 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 865 | restart: |
44a5e5eb | 866 | health_poll_update(&consumer_data->health); |
5eb91c98 | 867 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 868 | health_poll_update(&consumer_data->health); |
273ea72c | 869 | if (ret < 0) { |
88f2b785 MD |
870 | /* |
871 | * Restart interrupted system call. | |
872 | */ | |
873 | if (errno == EINTR) { | |
874 | goto restart; | |
875 | } | |
273ea72c DG |
876 | goto error; |
877 | } | |
878 | ||
5eb91c98 DG |
879 | for (i = 0; i < nb_fd; i++) { |
880 | /* Fetch once the poll data */ | |
881 | revents = LTTNG_POLL_GETEV(&events, i); | |
882 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
883 | ||
44a5e5eb DG |
884 | health_code_update(&consumer_data->health); |
885 | ||
5eb91c98 DG |
886 | /* Thread quit pipe has been closed. Killing thread. */ |
887 | ret = check_thread_quit_pipe(pollfd, revents); | |
888 | if (ret) { | |
889 | goto error; | |
890 | } | |
891 | ||
892 | /* Event on the registration socket */ | |
3bd1e081 | 893 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 894 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 895 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
896 | goto error; |
897 | } | |
898 | } | |
273ea72c DG |
899 | } |
900 | ||
3bd1e081 | 901 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
902 | if (sock < 0) { |
903 | goto error; | |
904 | } | |
905 | ||
44a5e5eb DG |
906 | health_code_update(&consumer_data->health); |
907 | ||
3bd1e081 | 908 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 909 | |
712ea556 | 910 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
911 | ret = lttcomm_recv_unix_sock(sock, &code, |
912 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
913 | if (ret <= 0) { |
914 | goto error; | |
915 | } | |
916 | ||
44a5e5eb DG |
917 | health_code_update(&consumer_data->health); |
918 | ||
3bd1e081 MD |
919 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
920 | consumer_data->cmd_sock = | |
921 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
922 | if (consumer_data->cmd_sock < 0) { | |
923 | sem_post(&consumer_data->sem); | |
48842b30 | 924 | PERROR("consumer connect"); |
1d4b027a DG |
925 | goto error; |
926 | } | |
927 | /* Signal condition to tell that the kconsumerd is ready */ | |
3bd1e081 MD |
928 | sem_post(&consumer_data->sem); |
929 | DBG("consumer command socket ready"); | |
1d4b027a | 930 | } else { |
3bd1e081 | 931 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
932 | lttcomm_get_readable_code(-code)); |
933 | goto error; | |
934 | } | |
935 | ||
54d01ffb | 936 | /* Remove the kconsumerd error sock since we've established a connexion */ |
3bd1e081 | 937 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 938 | if (ret < 0) { |
72079cae DG |
939 | goto error; |
940 | } | |
941 | ||
5eb91c98 DG |
942 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
943 | if (ret < 0) { | |
72079cae | 944 | goto error; |
5eb91c98 DG |
945 | } |
946 | ||
44a5e5eb DG |
947 | health_code_update(&consumer_data->health); |
948 | ||
5eb91c98 DG |
949 | /* Update number of fd */ |
950 | nb_fd = LTTNG_POLL_GETNB(&events); | |
951 | ||
952 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 953 | restart_poll: |
44a5e5eb | 954 | health_poll_update(&consumer_data->health); |
5eb91c98 | 955 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 956 | health_poll_update(&consumer_data->health); |
5eb91c98 | 957 | if (ret < 0) { |
88f2b785 MD |
958 | /* |
959 | * Restart interrupted system call. | |
960 | */ | |
961 | if (errno == EINTR) { | |
962 | goto restart_poll; | |
963 | } | |
72079cae DG |
964 | goto error; |
965 | } | |
966 | ||
5eb91c98 DG |
967 | for (i = 0; i < nb_fd; i++) { |
968 | /* Fetch once the poll data */ | |
969 | revents = LTTNG_POLL_GETEV(&events, i); | |
970 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
971 | ||
44a5e5eb DG |
972 | health_code_update(&consumer_data->health); |
973 | ||
5eb91c98 DG |
974 | /* Thread quit pipe has been closed. Killing thread. */ |
975 | ret = check_thread_quit_pipe(pollfd, revents); | |
976 | if (ret) { | |
977 | goto error; | |
978 | } | |
979 | ||
980 | /* Event on the kconsumerd socket */ | |
981 | if (pollfd == sock) { | |
982 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 983 | ERR("consumer err socket second poll error"); |
5eb91c98 DG |
984 | goto error; |
985 | } | |
986 | } | |
987 | } | |
988 | ||
44a5e5eb DG |
989 | health_code_update(&consumer_data->health); |
990 | ||
712ea556 | 991 | /* Wait for any kconsumerd error */ |
54d01ffb DG |
992 | ret = lttcomm_recv_unix_sock(sock, &code, |
993 | sizeof(enum lttcomm_return_code)); | |
712ea556 | 994 | if (ret <= 0) { |
3bd1e081 | 995 | ERR("consumer closed the command socket"); |
712ea556 | 996 | goto error; |
6f61d021 | 997 | } |
1d4b027a | 998 | |
3bd1e081 | 999 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
712ea556 | 1000 | |
1d4b027a | 1001 | error: |
5c827ce0 DG |
1002 | /* Immediately set the consumerd state to stopped */ |
1003 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1004 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1005 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1006 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1007 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1008 | } else { | |
1009 | /* Code flow error... */ | |
1010 | assert(0); | |
1011 | } | |
1012 | ||
76d7553f MD |
1013 | if (consumer_data->err_sock >= 0) { |
1014 | ret = close(consumer_data->err_sock); | |
1015 | if (ret) { | |
1016 | PERROR("close"); | |
1017 | } | |
1018 | } | |
1019 | if (consumer_data->cmd_sock >= 0) { | |
1020 | ret = close(consumer_data->cmd_sock); | |
1021 | if (ret) { | |
1022 | PERROR("close"); | |
1023 | } | |
1024 | } | |
1025 | if (sock >= 0) { | |
1026 | ret = close(sock); | |
1027 | if (ret) { | |
1028 | PERROR("close"); | |
1029 | } | |
1030 | } | |
273ea72c | 1031 | |
3bd1e081 MD |
1032 | unlink(consumer_data->err_unix_sock_path); |
1033 | unlink(consumer_data->cmd_unix_sock_path); | |
1034 | consumer_data->pid = 0; | |
1d4b027a | 1035 | |
5eb91c98 | 1036 | lttng_poll_clean(&events); |
76d7553f MD |
1037 | error_poll: |
1038 | error_listen: | |
44a5e5eb | 1039 | health_reset(&consumer_data->health); |
76d7553f | 1040 | DBG("consumer thread cleanup completed"); |
0177d773 | 1041 | |
5eb91c98 | 1042 | return NULL; |
099e26bd DG |
1043 | } |
1044 | ||
099e26bd DG |
1045 | /* |
1046 | * This thread manage application communication. | |
1d4b027a DG |
1047 | */ |
1048 | static void *thread_manage_apps(void *data) | |
099e26bd | 1049 | { |
5eb91c98 DG |
1050 | int i, ret, pollfd; |
1051 | uint32_t revents, nb_fd; | |
099e26bd | 1052 | struct ust_command ust_cmd; |
5eb91c98 | 1053 | struct lttng_poll_event events; |
099e26bd DG |
1054 | |
1055 | DBG("[thread] Manage application started"); | |
1056 | ||
f6a9efaa DG |
1057 | rcu_register_thread(); |
1058 | rcu_thread_online(); | |
1059 | ||
44a5e5eb DG |
1060 | health_code_update(&health_thread_app_reg); |
1061 | ||
5eb91c98 DG |
1062 | ret = create_thread_poll_set(&events, 2); |
1063 | if (ret < 0) { | |
76d7553f | 1064 | goto error_poll_create; |
5eb91c98 | 1065 | } |
099e26bd | 1066 | |
5eb91c98 DG |
1067 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1068 | if (ret < 0) { | |
1069 | goto error; | |
1070 | } | |
099e26bd | 1071 | |
44a5e5eb DG |
1072 | health_code_update(&health_thread_app_reg); |
1073 | ||
5eb91c98 DG |
1074 | while (1) { |
1075 | /* Zeroed the events structure */ | |
1076 | lttng_poll_reset(&events); | |
0177d773 | 1077 | |
5eb91c98 | 1078 | nb_fd = LTTNG_POLL_GETNB(&events); |
099e26bd DG |
1079 | |
1080 | DBG("Apps thread polling on %d fds", nb_fd); | |
1081 | ||
1082 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1083 | restart: |
44a5e5eb | 1084 | health_poll_update(&health_thread_app_reg); |
5eb91c98 | 1085 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 1086 | health_poll_update(&health_thread_app_reg); |
099e26bd | 1087 | if (ret < 0) { |
88f2b785 MD |
1088 | /* |
1089 | * Restart interrupted system call. | |
1090 | */ | |
1091 | if (errno == EINTR) { | |
1092 | goto restart; | |
1093 | } | |
099e26bd DG |
1094 | goto error; |
1095 | } | |
1096 | ||
5eb91c98 DG |
1097 | for (i = 0; i < nb_fd; i++) { |
1098 | /* Fetch once the poll data */ | |
1099 | revents = LTTNG_POLL_GETEV(&events, i); | |
1100 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1101 | ||
44a5e5eb DG |
1102 | health_code_update(&health_thread_app_reg); |
1103 | ||
5eb91c98 DG |
1104 | /* Thread quit pipe has been closed. Killing thread. */ |
1105 | ret = check_thread_quit_pipe(pollfd, revents); | |
1106 | if (ret) { | |
099e26bd | 1107 | goto error; |
5eb91c98 | 1108 | } |
099e26bd | 1109 | |
5eb91c98 DG |
1110 | /* Inspect the apps cmd pipe */ |
1111 | if (pollfd == apps_cmd_pipe[0]) { | |
1112 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1113 | ERR("Apps command pipe error"); | |
0177d773 | 1114 | goto error; |
5eb91c98 DG |
1115 | } else if (revents & LPOLLIN) { |
1116 | /* Empty pipe */ | |
1117 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); | |
1118 | if (ret < 0 || ret < sizeof(ust_cmd)) { | |
76d7553f | 1119 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1120 | goto error; |
1121 | } | |
099e26bd | 1122 | |
44a5e5eb DG |
1123 | health_code_update(&health_thread_app_reg); |
1124 | ||
5eb91c98 | 1125 | /* Register applicaton to the session daemon */ |
56fff090 | 1126 | ret = ust_app_register(&ust_cmd.reg_msg, |
54d01ffb | 1127 | ust_cmd.sock); |
88ff5b7f | 1128 | if (ret == -ENOMEM) { |
5eb91c98 | 1129 | goto error; |
88ff5b7f MD |
1130 | } else if (ret < 0) { |
1131 | break; | |
5eb91c98 DG |
1132 | } |
1133 | ||
44a5e5eb DG |
1134 | health_code_update(&health_thread_app_reg); |
1135 | ||
acc7b41b | 1136 | /* |
e0c7ec2b | 1137 | * Validate UST version compatibility. |
acc7b41b | 1138 | */ |
e0c7ec2b DG |
1139 | ret = ust_app_validate_version(ust_cmd.sock); |
1140 | if (ret >= 0) { | |
1141 | /* | |
1142 | * Add channel(s) and event(s) to newly registered apps | |
1143 | * from lttng global UST domain. | |
1144 | */ | |
1145 | update_ust_app(ust_cmd.sock); | |
1146 | } | |
acc7b41b | 1147 | |
44a5e5eb DG |
1148 | health_code_update(&health_thread_app_reg); |
1149 | ||
f2ca2e25 | 1150 | ret = ust_app_register_done(ust_cmd.sock); |
5eb91c98 DG |
1151 | if (ret < 0) { |
1152 | /* | |
1153 | * If the registration is not possible, we simply | |
1154 | * unregister the apps and continue | |
1155 | */ | |
56fff090 | 1156 | ust_app_unregister(ust_cmd.sock); |
5eb91c98 DG |
1157 | } else { |
1158 | /* | |
1159 | * We just need here to monitor the close of the UST | |
1160 | * socket and poll set monitor those by default. | |
a9ade966 MD |
1161 | * Listen on POLLIN (even if we never expect any |
1162 | * data) to ensure that hangup wakes us. | |
5eb91c98 | 1163 | */ |
a9ade966 | 1164 | ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN); |
5eb91c98 DG |
1165 | if (ret < 0) { |
1166 | goto error; | |
1167 | } | |
1168 | ||
54d01ffb DG |
1169 | DBG("Apps with sock %d added to poll set", |
1170 | ust_cmd.sock); | |
5eb91c98 | 1171 | } |
487cf67c | 1172 | |
44a5e5eb DG |
1173 | health_code_update(&health_thread_app_reg); |
1174 | ||
5eb91c98 | 1175 | break; |
0177d773 | 1176 | } |
5eb91c98 DG |
1177 | } else { |
1178 | /* | |
54d01ffb DG |
1179 | * At this point, we know that a registered application made |
1180 | * the event at poll_wait. | |
5eb91c98 DG |
1181 | */ |
1182 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1183 | /* Removing from the poll set */ | |
1184 | ret = lttng_poll_del(&events, pollfd); | |
1185 | if (ret < 0) { | |
1186 | goto error; | |
1187 | } | |
099e26bd | 1188 | |
b9d9b220 | 1189 | /* Socket closed on remote end. */ |
56fff090 | 1190 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1191 | break; |
1192 | } | |
099e26bd | 1193 | } |
44a5e5eb DG |
1194 | |
1195 | health_code_update(&health_thread_app_reg); | |
099e26bd | 1196 | } |
099e26bd DG |
1197 | } |
1198 | ||
1199 | error: | |
5eb91c98 | 1200 | lttng_poll_clean(&events); |
76d7553f | 1201 | error_poll_create: |
44a5e5eb | 1202 | health_reset(&health_thread_app_reg); |
76d7553f | 1203 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1204 | rcu_thread_offline(); |
1205 | rcu_unregister_thread(); | |
099e26bd DG |
1206 | return NULL; |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * Dispatch request from the registration threads to the application | |
1211 | * communication thread. | |
1212 | */ | |
1213 | static void *thread_dispatch_ust_registration(void *data) | |
1214 | { | |
1215 | int ret; | |
1216 | struct cds_wfq_node *node; | |
1217 | struct ust_command *ust_cmd = NULL; | |
1218 | ||
1219 | DBG("[thread] Dispatch UST command started"); | |
1220 | ||
1221 | while (!dispatch_thread_exit) { | |
1222 | /* Atomically prepare the queue futex */ | |
1223 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1224 | ||
1225 | do { | |
1226 | /* Dequeue command for registration */ | |
1227 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1228 | if (node == NULL) { | |
00a17c97 | 1229 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1230 | /* Continue thread execution */ |
1231 | break; | |
1232 | } | |
1233 | ||
1234 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1235 | ||
2f50c8a3 DG |
1236 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1237 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1238 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1239 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1240 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1241 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
099e26bd DG |
1242 | /* |
1243 | * Inform apps thread of the new application registration. This | |
1244 | * call is blocking so we can be assured that the data will be read | |
1245 | * at some point in time or wait to the end of the world :) | |
1246 | */ | |
1247 | ret = write(apps_cmd_pipe[1], ust_cmd, | |
1248 | sizeof(struct ust_command)); | |
1249 | if (ret < 0) { | |
76d7553f | 1250 | PERROR("write apps cmd pipe"); |
099e26bd DG |
1251 | if (errno == EBADF) { |
1252 | /* | |
1253 | * We can't inform the application thread to process | |
1254 | * registration. We will exit or else application | |
1255 | * registration will not occur and tracing will never | |
1256 | * start. | |
1257 | */ | |
1258 | goto error; | |
1259 | } | |
1260 | } | |
1261 | free(ust_cmd); | |
1262 | } while (node != NULL); | |
1263 | ||
1264 | /* Futex wait on queue. Blocking call on futex() */ | |
1265 | futex_nto1_wait(&ust_cmd_queue.futex); | |
1266 | } | |
1267 | ||
1268 | error: | |
1269 | DBG("Dispatch thread dying"); | |
1270 | return NULL; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * This thread manage application registration. | |
1275 | */ | |
1276 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1277 | { |
a4b35e07 | 1278 | int sock = -1, i, ret, pollfd; |
5eb91c98 DG |
1279 | uint32_t revents, nb_fd; |
1280 | struct lttng_poll_event events; | |
099e26bd DG |
1281 | /* |
1282 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1283 | * freed in the manage apps thread. | |
1284 | */ | |
1285 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1286 | |
099e26bd | 1287 | DBG("[thread] Manage application registration started"); |
1d4b027a DG |
1288 | |
1289 | ret = lttcomm_listen_unix_sock(apps_sock); | |
1290 | if (ret < 0) { | |
76d7553f | 1291 | goto error_listen; |
1d4b027a DG |
1292 | } |
1293 | ||
5eb91c98 DG |
1294 | /* |
1295 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1296 | * more will be added to this poll set. | |
1297 | */ | |
1298 | ret = create_thread_poll_set(&events, 2); | |
1299 | if (ret < 0) { | |
76d7553f | 1300 | goto error_create_poll; |
5eb91c98 | 1301 | } |
273ea72c | 1302 | |
5eb91c98 DG |
1303 | /* Add the application registration socket */ |
1304 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1305 | if (ret < 0) { | |
76d7553f | 1306 | goto error_poll_add; |
5eb91c98 | 1307 | } |
273ea72c | 1308 | |
1d4b027a | 1309 | /* Notify all applications to register */ |
0fdd1e2c DG |
1310 | ret = notify_ust_apps(1); |
1311 | if (ret < 0) { | |
1312 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1313 | "Execution continues but there might be problem for already\n" |
1314 | "running applications that wishes to register."); | |
0fdd1e2c | 1315 | } |
1d4b027a DG |
1316 | |
1317 | while (1) { | |
1318 | DBG("Accepting application registration"); | |
273ea72c | 1319 | |
5eb91c98 DG |
1320 | nb_fd = LTTNG_POLL_GETNB(&events); |
1321 | ||
273ea72c | 1322 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 1323 | restart: |
5eb91c98 | 1324 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 1325 | if (ret < 0) { |
88f2b785 MD |
1326 | /* |
1327 | * Restart interrupted system call. | |
1328 | */ | |
1329 | if (errno == EINTR) { | |
1330 | goto restart; | |
1331 | } | |
273ea72c DG |
1332 | goto error; |
1333 | } | |
1334 | ||
5eb91c98 DG |
1335 | for (i = 0; i < nb_fd; i++) { |
1336 | /* Fetch once the poll data */ | |
1337 | revents = LTTNG_POLL_GETEV(&events, i); | |
1338 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1339 | |
5eb91c98 DG |
1340 | /* Thread quit pipe has been closed. Killing thread. */ |
1341 | ret = check_thread_quit_pipe(pollfd, revents); | |
1342 | if (ret) { | |
90014c57 DG |
1343 | goto error; |
1344 | } | |
1d4b027a | 1345 | |
5eb91c98 DG |
1346 | /* Event on the registration socket */ |
1347 | if (pollfd == apps_sock) { | |
1348 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1349 | ERR("Register apps socket poll error"); | |
1350 | goto error; | |
1351 | } else if (revents & LPOLLIN) { | |
1352 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1353 | if (sock < 0) { | |
1354 | goto error; | |
1355 | } | |
099e26bd | 1356 | |
5eb91c98 | 1357 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1358 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1359 | if (ust_cmd == NULL) { |
76d7553f | 1360 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
1361 | goto error; |
1362 | } | |
1d4b027a | 1363 | |
5eb91c98 DG |
1364 | /* |
1365 | * Using message-based transmissions to ensure we don't | |
1366 | * have to deal with partially received messages. | |
1367 | */ | |
4063050c MD |
1368 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1369 | if (ret < 0) { | |
1370 | ERR("Exhausted file descriptors allowed for applications."); | |
1371 | free(ust_cmd); | |
1372 | ret = close(sock); | |
1373 | if (ret) { | |
1374 | PERROR("close"); | |
1375 | } | |
1376 | sock = -1; | |
1377 | continue; | |
1378 | } | |
5eb91c98 DG |
1379 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, |
1380 | sizeof(struct ust_register_msg)); | |
1381 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { | |
1382 | if (ret < 0) { | |
76d7553f | 1383 | PERROR("lttcomm_recv_unix_sock register apps"); |
5eb91c98 DG |
1384 | } else { |
1385 | ERR("Wrong size received on apps register"); | |
1386 | } | |
1387 | free(ust_cmd); | |
76d7553f MD |
1388 | ret = close(sock); |
1389 | if (ret) { | |
1390 | PERROR("close"); | |
1391 | } | |
4063050c | 1392 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1393 | sock = -1; |
5eb91c98 DG |
1394 | continue; |
1395 | } | |
099e26bd | 1396 | |
5eb91c98 | 1397 | ust_cmd->sock = sock; |
34a2494f | 1398 | sock = -1; |
099e26bd | 1399 | |
5eb91c98 DG |
1400 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1401 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1402 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1403 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1404 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1405 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1406 | |
5eb91c98 DG |
1407 | /* |
1408 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1409 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1410 | */ |
1411 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1412 | ||
1413 | /* | |
1414 | * Wake the registration queue futex. Implicit memory | |
1415 | * barrier with the exchange in cds_wfq_enqueue. | |
1416 | */ | |
1417 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1418 | } | |
1419 | } | |
90014c57 | 1420 | } |
1d4b027a DG |
1421 | } |
1422 | ||
1423 | error: | |
0fdd1e2c DG |
1424 | /* Notify that the registration thread is gone */ |
1425 | notify_ust_apps(0); | |
1426 | ||
a4b35e07 | 1427 | if (apps_sock >= 0) { |
76d7553f MD |
1428 | ret = close(apps_sock); |
1429 | if (ret) { | |
1430 | PERROR("close"); | |
1431 | } | |
a4b35e07 | 1432 | } |
46c3f085 | 1433 | if (sock >= 0) { |
76d7553f MD |
1434 | ret = close(sock); |
1435 | if (ret) { | |
1436 | PERROR("close"); | |
1437 | } | |
4063050c | 1438 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1439 | } |
273ea72c | 1440 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1441 | |
76d7553f | 1442 | error_poll_add: |
5eb91c98 | 1443 | lttng_poll_clean(&events); |
76d7553f MD |
1444 | error_listen: |
1445 | error_create_poll: | |
1446 | DBG("UST Registration thread cleanup complete"); | |
5eb91c98 | 1447 | |
1d4b027a DG |
1448 | return NULL; |
1449 | } | |
1450 | ||
8c0faa1d | 1451 | /* |
3bd1e081 | 1452 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1453 | * exec or it will fails. |
8c0faa1d | 1454 | */ |
3bd1e081 | 1455 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d DG |
1456 | { |
1457 | int ret; | |
ee0b0061 DG |
1458 | struct timespec timeout; |
1459 | ||
1460 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; | |
1461 | timeout.tv_nsec = 0; | |
8c0faa1d DG |
1462 | |
1463 | /* Setup semaphore */ | |
3bd1e081 | 1464 | ret = sem_init(&consumer_data->sem, 0, 0); |
ee0b0061 | 1465 | if (ret < 0) { |
3bd1e081 | 1466 | PERROR("sem_init consumer semaphore"); |
ee0b0061 DG |
1467 | goto error; |
1468 | } | |
8c0faa1d | 1469 | |
3bd1e081 MD |
1470 | ret = pthread_create(&consumer_data->thread, NULL, |
1471 | thread_manage_consumer, consumer_data); | |
8c0faa1d | 1472 | if (ret != 0) { |
3bd1e081 | 1473 | PERROR("pthread_create consumer"); |
ee0b0061 | 1474 | ret = -1; |
8c0faa1d DG |
1475 | goto error; |
1476 | } | |
1477 | ||
ee0b0061 DG |
1478 | /* Get time for sem_timedwait absolute timeout */ |
1479 | ret = clock_gettime(CLOCK_REALTIME, &timeout); | |
1480 | if (ret < 0) { | |
3bd1e081 | 1481 | PERROR("clock_gettime spawn consumer"); |
ee0b0061 | 1482 | /* Infinite wait for the kconsumerd thread to be ready */ |
3bd1e081 | 1483 | ret = sem_wait(&consumer_data->sem); |
ee0b0061 DG |
1484 | } else { |
1485 | /* Normal timeout if the gettime was successful */ | |
1486 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
3bd1e081 | 1487 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
ee0b0061 | 1488 | } |
8c0faa1d | 1489 | |
ee0b0061 DG |
1490 | if (ret < 0) { |
1491 | if (errno == ETIMEDOUT) { | |
1492 | /* | |
1493 | * Call has timed out so we kill the kconsumerd_thread and return | |
1494 | * an error. | |
1495 | */ | |
3bd1e081 MD |
1496 | ERR("The consumer thread was never ready. Killing it"); |
1497 | ret = pthread_cancel(consumer_data->thread); | |
ee0b0061 | 1498 | if (ret < 0) { |
3bd1e081 | 1499 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
1500 | } |
1501 | } else { | |
3bd1e081 | 1502 | PERROR("semaphore wait failed consumer thread"); |
ee0b0061 DG |
1503 | } |
1504 | goto error; | |
1505 | } | |
1506 | ||
3bd1e081 MD |
1507 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1508 | if (consumer_data->pid == 0) { | |
712ea556 | 1509 | ERR("Kconsumerd did not start"); |
3bd1e081 | 1510 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
1511 | goto error; |
1512 | } | |
3bd1e081 | 1513 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 1514 | |
8c0faa1d DG |
1515 | return 0; |
1516 | ||
1517 | error: | |
1518 | return ret; | |
1519 | } | |
1520 | ||
d9800920 | 1521 | /* |
3bd1e081 | 1522 | * Join consumer thread |
d9800920 | 1523 | */ |
3bd1e081 | 1524 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
1525 | { |
1526 | void *status; | |
1527 | int ret; | |
1528 | ||
3bd1e081 MD |
1529 | if (consumer_data->pid != 0) { |
1530 | ret = kill(consumer_data->pid, SIGTERM); | |
cf3af59e | 1531 | if (ret) { |
3bd1e081 | 1532 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
1533 | return ret; |
1534 | } | |
3bd1e081 | 1535 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
1536 | } else { |
1537 | return 0; | |
1538 | } | |
1539 | } | |
1540 | ||
8c0faa1d | 1541 | /* |
3bd1e081 | 1542 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 1543 | * |
d063d709 | 1544 | * Return pid if successful else -1. |
8c0faa1d | 1545 | */ |
3bd1e081 | 1546 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
1547 | { |
1548 | int ret; | |
1549 | pid_t pid; | |
94c55f17 | 1550 | const char *consumer_to_use; |
53086306 | 1551 | const char *verbosity; |
94c55f17 | 1552 | struct stat st; |
8c0faa1d | 1553 | |
3bd1e081 | 1554 | DBG("Spawning consumerd"); |
c49dc785 | 1555 | |
8c0faa1d DG |
1556 | pid = fork(); |
1557 | if (pid == 0) { | |
1558 | /* | |
3bd1e081 | 1559 | * Exec consumerd. |
8c0faa1d | 1560 | */ |
daee5345 | 1561 | if (opt_verbose_consumer) { |
53086306 DG |
1562 | verbosity = "--verbose"; |
1563 | } else { | |
1564 | verbosity = "--quiet"; | |
1565 | } | |
3bd1e081 MD |
1566 | switch (consumer_data->type) { |
1567 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 1568 | /* |
c7704d57 DG |
1569 | * Find out which consumerd to execute. We will first try the |
1570 | * 64-bit path, then the sessiond's installation directory, and | |
1571 | * fallback on the 32-bit one, | |
94c55f17 | 1572 | */ |
63a799e8 AM |
1573 | DBG3("Looking for a kernel consumer at these locations:"); |
1574 | DBG3(" 1) %s", consumerd64_bin); | |
1575 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
1576 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 1577 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 1578 | DBG3("Found location #1"); |
94c55f17 | 1579 | consumer_to_use = consumerd64_bin; |
94c55f17 | 1580 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 1581 | DBG3("Found location #2"); |
94c55f17 | 1582 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 1583 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 1584 | DBG3("Found location #3"); |
eb1e0bd4 | 1585 | consumer_to_use = consumerd32_bin; |
94c55f17 | 1586 | } else { |
63a799e8 | 1587 | DBG("Could not find any valid consumerd executable"); |
94c55f17 AM |
1588 | break; |
1589 | } | |
1590 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
1591 | execl(consumer_to_use, | |
1592 | "lttng-consumerd", verbosity, "-k", | |
1593 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
1594 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1595 | NULL); | |
3bd1e081 | 1596 | break; |
7753dea8 MD |
1597 | case LTTNG_CONSUMER64_UST: |
1598 | { | |
b1e0b6b6 | 1599 | char *tmpnew = NULL; |
8f4905da MD |
1600 | |
1601 | if (consumerd64_libdir[0] != '\0') { | |
1602 | char *tmp; | |
1603 | size_t tmplen; | |
1604 | ||
1605 | tmp = getenv("LD_LIBRARY_PATH"); | |
1606 | if (!tmp) { | |
1607 | tmp = ""; | |
1608 | } | |
1609 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1610 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
1611 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1612 | if (!tmpnew) { | |
1613 | ret = -ENOMEM; | |
1614 | goto error; | |
1615 | } | |
1616 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1617 | strcat(tmpnew, consumerd64_libdir); | |
1618 | if (tmp[0] != '\0') { | |
1619 | strcat(tmpnew, ":"); | |
1620 | strcat(tmpnew, tmp); | |
1621 | } | |
1622 | ret = putenv(tmpnew); | |
1623 | if (ret) { | |
1624 | ret = -errno; | |
1625 | goto error; | |
1626 | } | |
1627 | } | |
94c55f17 | 1628 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 1629 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1630 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1631 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1632 | NULL); | |
8f4905da MD |
1633 | if (consumerd64_libdir[0] != '\0') { |
1634 | free(tmpnew); | |
1635 | } | |
1636 | if (ret) { | |
1637 | goto error; | |
1638 | } | |
3bd1e081 | 1639 | break; |
7753dea8 MD |
1640 | } |
1641 | case LTTNG_CONSUMER32_UST: | |
1642 | { | |
937dde8e | 1643 | char *tmpnew = NULL; |
8f4905da MD |
1644 | |
1645 | if (consumerd32_libdir[0] != '\0') { | |
1646 | char *tmp; | |
1647 | size_t tmplen; | |
1648 | ||
1649 | tmp = getenv("LD_LIBRARY_PATH"); | |
1650 | if (!tmp) { | |
1651 | tmp = ""; | |
1652 | } | |
1653 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1654 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
1655 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1656 | if (!tmpnew) { | |
1657 | ret = -ENOMEM; | |
1658 | goto error; | |
1659 | } | |
1660 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1661 | strcat(tmpnew, consumerd32_libdir); | |
1662 | if (tmp[0] != '\0') { | |
1663 | strcat(tmpnew, ":"); | |
1664 | strcat(tmpnew, tmp); | |
1665 | } | |
1666 | ret = putenv(tmpnew); | |
1667 | if (ret) { | |
1668 | ret = -errno; | |
1669 | goto error; | |
1670 | } | |
1671 | } | |
94c55f17 | 1672 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 1673 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1674 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1675 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1676 | NULL); | |
8f4905da MD |
1677 | if (consumerd32_libdir[0] != '\0') { |
1678 | free(tmpnew); | |
1679 | } | |
1680 | if (ret) { | |
1681 | goto error; | |
1682 | } | |
7753dea8 MD |
1683 | break; |
1684 | } | |
3bd1e081 | 1685 | default: |
76d7553f | 1686 | PERROR("unknown consumer type"); |
3bd1e081 MD |
1687 | exit(EXIT_FAILURE); |
1688 | } | |
8c0faa1d | 1689 | if (errno != 0) { |
76d7553f | 1690 | PERROR("kernel start consumer exec"); |
8c0faa1d DG |
1691 | } |
1692 | exit(EXIT_FAILURE); | |
1693 | } else if (pid > 0) { | |
1694 | ret = pid; | |
8c0faa1d | 1695 | } else { |
76d7553f | 1696 | PERROR("start consumer fork"); |
8c0faa1d | 1697 | ret = -errno; |
8c0faa1d | 1698 | } |
8f4905da | 1699 | error: |
8c0faa1d DG |
1700 | return ret; |
1701 | } | |
1702 | ||
693bd40b | 1703 | /* |
3bd1e081 | 1704 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 1705 | */ |
3bd1e081 | 1706 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b DG |
1707 | { |
1708 | int ret; | |
1709 | ||
3bd1e081 MD |
1710 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1711 | if (consumer_data->pid != 0) { | |
1712 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
1713 | goto end; |
1714 | } | |
693bd40b | 1715 | |
3bd1e081 | 1716 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 1717 | if (ret < 0) { |
3bd1e081 MD |
1718 | ERR("Spawning consumerd failed"); |
1719 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 1720 | goto error; |
693bd40b | 1721 | } |
c49dc785 | 1722 | |
3bd1e081 MD |
1723 | /* Setting up the consumer_data pid */ |
1724 | consumer_data->pid = ret; | |
48842b30 | 1725 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 1726 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 1727 | |
3bd1e081 MD |
1728 | DBG2("Spawning consumer control thread"); |
1729 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 1730 | if (ret < 0) { |
3bd1e081 | 1731 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
1732 | goto error; |
1733 | } | |
1734 | ||
c49dc785 | 1735 | end: |
693bd40b DG |
1736 | return 0; |
1737 | ||
1738 | error: | |
1739 | return ret; | |
1740 | } | |
1741 | ||
44a5e5eb DG |
1742 | /* |
1743 | * Compute health status of each consumer. | |
1744 | */ | |
1745 | static int check_consumer_health(void) | |
1746 | { | |
1747 | int ret; | |
1748 | ||
1749 | ret = | |
1750 | health_check_state(&kconsumer_data.health) & | |
1751 | health_check_state(&ustconsumer32_data.health) & | |
1752 | health_check_state(&ustconsumer64_data.health); | |
1753 | ||
1754 | DBG3("Health consumer check %d", ret); | |
1755 | ||
1756 | return ret; | |
1757 | } | |
1758 | ||
b73401da | 1759 | /* |
096102bd | 1760 | * Check version of the lttng-modules. |
b73401da | 1761 | */ |
096102bd | 1762 | static int validate_lttng_modules_version(void) |
b73401da | 1763 | { |
096102bd | 1764 | return kernel_validate_version(kernel_tracer_fd); |
ab147185 MD |
1765 | } |
1766 | ||
b73401da | 1767 | /* |
096102bd | 1768 | * Setup necessary data for kernel tracer action. |
b73401da | 1769 | */ |
096102bd | 1770 | static int init_kernel_tracer(void) |
b73401da DG |
1771 | { |
1772 | int ret; | |
b73401da | 1773 | |
096102bd DG |
1774 | /* Modprobe lttng kernel modules */ |
1775 | ret = modprobe_lttng_control(); | |
b73401da | 1776 | if (ret < 0) { |
b73401da DG |
1777 | goto error; |
1778 | } | |
1779 | ||
096102bd DG |
1780 | /* Open debugfs lttng */ |
1781 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
1782 | if (kernel_tracer_fd < 0) { | |
1783 | DBG("Failed to open %s", module_proc_lttng); | |
1784 | ret = -1; | |
1785 | goto error_open; | |
b73401da DG |
1786 | } |
1787 | ||
096102bd DG |
1788 | /* Validate kernel version */ |
1789 | ret = validate_lttng_modules_version(); | |
b73401da | 1790 | if (ret < 0) { |
096102bd | 1791 | goto error_version; |
b73401da DG |
1792 | } |
1793 | ||
096102bd | 1794 | ret = modprobe_lttng_data(); |
b73401da | 1795 | if (ret < 0) { |
096102bd | 1796 | goto error_modules; |
8c0faa1d DG |
1797 | } |
1798 | ||
f3ed775e | 1799 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
096102bd DG |
1800 | return 0; |
1801 | ||
1802 | error_version: | |
1803 | modprobe_remove_lttng_control(); | |
76d7553f MD |
1804 | ret = close(kernel_tracer_fd); |
1805 | if (ret) { | |
1806 | PERROR("close"); | |
1807 | } | |
1808 | kernel_tracer_fd = -1; | |
096102bd DG |
1809 | return LTTCOMM_KERN_VERSION; |
1810 | ||
1811 | error_modules: | |
76d7553f MD |
1812 | ret = close(kernel_tracer_fd); |
1813 | if (ret) { | |
1814 | PERROR("close"); | |
1815 | } | |
096102bd DG |
1816 | |
1817 | error_open: | |
1818 | modprobe_remove_lttng_control(); | |
b73401da DG |
1819 | |
1820 | error: | |
b73401da | 1821 | WARN("No kernel tracer available"); |
a4b35e07 | 1822 | kernel_tracer_fd = -1; |
531d29f9 MD |
1823 | if (!is_root) { |
1824 | return LTTCOMM_NEED_ROOT_SESSIOND; | |
1825 | } else { | |
1826 | return LTTCOMM_KERN_NA; | |
1827 | } | |
f3ed775e | 1828 | } |
33a2b854 | 1829 | |
f3ed775e | 1830 | /* |
54d01ffb | 1831 | * Init tracing by creating trace directory and sending fds kernel consumer. |
f3ed775e | 1832 | */ |
54d01ffb | 1833 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
f3ed775e | 1834 | { |
f40799e8 | 1835 | int ret = 0; |
8c0faa1d | 1836 | |
00e2e675 | 1837 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
d9800920 | 1838 | /* |
54d01ffb | 1839 | * Assign default kernel consumer socket if no consumer assigned to the |
03550b58 | 1840 | * kernel session. At this point, it's NOT supposed to be -1 but this is |
54d01ffb | 1841 | * an extra security check. |
d9800920 | 1842 | */ |
03550b58 | 1843 | if (session->consumer_fd < 0) { |
3bd1e081 | 1844 | session->consumer_fd = kconsumer_data.cmd_sock; |
d9800920 DG |
1845 | } |
1846 | ||
00e2e675 | 1847 | ret = kernel_consumer_send_session(session->consumer_fd, session); |
f3ed775e | 1848 | if (ret < 0) { |
f3ed775e DG |
1849 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
1850 | goto error; | |
1851 | } | |
00e2e675 | 1852 | } |
1d4b027a | 1853 | |
00e2e675 DG |
1854 | error: |
1855 | return ret; | |
1856 | } | |
1857 | ||
1858 | /* | |
1859 | * Create a socket to the relayd using the URI. | |
1860 | * | |
1861 | * On success, the relayd_sock pointer is set to the created socket. | |
1862 | * Else, it is untouched and an lttcomm error code is returned. | |
1863 | */ | |
1864 | static int create_connect_relayd(struct consumer_output *output, | |
1865 | const char *session_name, struct lttng_uri *uri, | |
1866 | struct lttcomm_sock **relayd_sock) | |
1867 | { | |
1868 | int ret; | |
1869 | struct lttcomm_sock *sock; | |
1870 | ||
1871 | /* Create socket object from URI */ | |
1872 | sock = lttcomm_alloc_sock_from_uri(uri); | |
1873 | if (sock == NULL) { | |
1874 | ret = LTTCOMM_FATAL; | |
1875 | goto error; | |
1876 | } | |
1877 | ||
1878 | ret = lttcomm_create_sock(sock); | |
1879 | if (ret < 0) { | |
1880 | ret = LTTCOMM_FATAL; | |
1881 | goto error; | |
1882 | } | |
1883 | ||
1884 | /* Connect to relayd so we can proceed with a session creation. */ | |
1885 | ret = relayd_connect(sock); | |
1886 | if (ret < 0) { | |
1887 | ERR("Unable to reach lttng-relayd"); | |
1888 | ret = LTTCOMM_RELAYD_SESSION_FAIL; | |
1889 | goto free_sock; | |
f3ed775e | 1890 | } |
1d4b027a | 1891 | |
00e2e675 DG |
1892 | /* Create socket for control stream. */ |
1893 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
1894 | DBG3("Creating relayd stream socket from URI"); | |
1895 | ||
1896 | /* Check relayd version */ | |
1897 | ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0); | |
1898 | if (ret < 0) { | |
1899 | ret = LTTCOMM_RELAYD_VERSION_FAIL; | |
1900 | goto close_sock; | |
1901 | } | |
1902 | } else if (uri->stype == LTTNG_STREAM_DATA) { | |
1903 | DBG3("Creating relayd data socket from URI"); | |
1904 | } else { | |
1905 | /* Command is not valid */ | |
1906 | ERR("Relayd invalid stream type: %d", uri->stype); | |
1907 | ret = LTTCOMM_INVALID; | |
1908 | goto close_sock; | |
1909 | } | |
1910 | ||
1911 | *relayd_sock = sock; | |
1912 | ||
1913 | return LTTCOMM_OK; | |
1914 | ||
1915 | close_sock: | |
1916 | if (sock) { | |
1917 | (void) relayd_close(sock); | |
1918 | } | |
1919 | free_sock: | |
1920 | if (sock) { | |
1921 | lttcomm_destroy_sock(sock); | |
1922 | } | |
1923 | error: | |
1924 | return ret; | |
1925 | } | |
1926 | ||
1927 | /* | |
1928 | * Connect to the relayd using URI and send the socket to the right consumer. | |
1929 | */ | |
1930 | static int send_socket_relayd_consumer(int domain, struct ltt_session *session, | |
1931 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, | |
1932 | int consumer_fd) | |
1933 | { | |
1934 | int ret; | |
1935 | struct lttcomm_sock *sock = NULL; | |
1936 | ||
1937 | /* Set the network sequence index if not set. */ | |
1938 | if (consumer->net_seq_index == -1) { | |
1939 | /* | |
1940 | * Increment net_seq_idx because we are about to transfer the | |
1941 | * new relayd socket to the consumer. | |
1942 | */ | |
1943 | uatomic_inc(&relayd_net_seq_idx); | |
1944 | /* Assign unique key so the consumer can match streams */ | |
1945 | consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx); | |
1946 | } | |
1947 | ||
1948 | /* Connect to relayd and make version check if uri is the control. */ | |
1949 | ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock); | |
1950 | if (ret != LTTCOMM_OK) { | |
1951 | goto close_sock; | |
1952 | } | |
1953 | ||
1954 | /* If the control socket is connected, network session is ready */ | |
1955 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
1956 | session->net_handle = 1; | |
1957 | } | |
1958 | ||
1959 | switch (domain) { | |
1960 | case LTTNG_DOMAIN_KERNEL: | |
1961 | /* Send relayd socket to consumer. */ | |
1962 | ret = kernel_consumer_send_relayd_socket(consumer_fd, sock, | |
1963 | consumer, relayd_uri->stype); | |
1964 | if (ret < 0) { | |
1965 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
1966 | goto close_sock; | |
1967 | } | |
1968 | break; | |
1969 | case LTTNG_DOMAIN_UST: | |
1970 | /* Send relayd socket to consumer. */ | |
1971 | ret = ust_consumer_send_relayd_socket(consumer_fd, sock, | |
1972 | consumer, relayd_uri->stype); | |
1973 | if (ret < 0) { | |
1974 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
1975 | goto close_sock; | |
1976 | } | |
1977 | break; | |
1978 | } | |
1979 | ||
1980 | ret = LTTCOMM_OK; | |
1981 | ||
1982 | /* | |
1983 | * Close socket which was dup on the consumer side. The session daemon does | |
1984 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
1985 | */ | |
1986 | ||
1987 | close_sock: | |
1988 | if (sock) { | |
1989 | (void) relayd_close(sock); | |
1990 | lttcomm_destroy_sock(sock); | |
1991 | } | |
1992 | ||
1993 | return ret; | |
1994 | } | |
1995 | ||
1996 | /* | |
1997 | * Send both relayd sockets to a specific consumer and domain. This is a | |
1998 | * helper function to facilitate sending the information to the consumer for a | |
1999 | * session. | |
2000 | */ | |
2001 | static int send_sockets_relayd_consumer(int domain, | |
2002 | struct ltt_session *session, struct consumer_output *consumer, int fd) | |
2003 | { | |
2004 | int ret; | |
2005 | ||
2006 | /* Sending control relayd socket. */ | |
2007 | ret = send_socket_relayd_consumer(domain, session, | |
2008 | &consumer->dst.net.control, consumer, fd); | |
2009 | if (ret != LTTCOMM_OK) { | |
2010 | goto error; | |
2011 | } | |
2012 | ||
2013 | /* Sending data relayd socket. */ | |
2014 | ret = send_socket_relayd_consumer(domain, session, | |
2015 | &consumer->dst.net.data, consumer, fd); | |
2016 | if (ret != LTTCOMM_OK) { | |
2017 | goto error; | |
2018 | } | |
2019 | ||
2020 | error: | |
2021 | return ret; | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * Setup relayd connections for a tracing session. First creates the socket to | |
2026 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
2027 | * network. | |
2028 | */ | |
2029 | static int setup_relayd(struct ltt_session *session) | |
2030 | { | |
2031 | int ret = LTTCOMM_OK; | |
2032 | struct ltt_ust_session *usess; | |
2033 | struct ltt_kernel_session *ksess; | |
2034 | ||
2035 | assert(session); | |
2036 | ||
2037 | usess = session->ust_session; | |
2038 | ksess = session->kernel_session; | |
2039 | ||
2040 | DBG2("Setting relayd for session %s", session->name); | |
2041 | ||
2042 | if (usess && usess->consumer->sock == -1 && | |
2043 | usess->consumer->type == CONSUMER_DST_NET && | |
2044 | usess->consumer->enabled) { | |
2045 | /* Setup relayd for 64 bits consumer */ | |
2046 | if (ust_consumerd64_fd >= 0) { | |
2047 | send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session, | |
2048 | usess->consumer, ust_consumerd64_fd); | |
2049 | if (ret != LTTCOMM_OK) { | |
2050 | goto error; | |
2051 | } | |
2052 | } | |
2053 | ||
2054 | /* Setup relayd for 32 bits consumer */ | |
2055 | if (ust_consumerd32_fd >= 0) { | |
2056 | send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session, | |
2057 | usess->consumer, ust_consumerd32_fd); | |
2058 | if (ret != LTTCOMM_OK) { | |
2059 | goto error; | |
2060 | } | |
2061 | } | |
2062 | } else if (ksess && ksess->consumer->sock == -1 && | |
2063 | ksess->consumer->type == CONSUMER_DST_NET && | |
2064 | ksess->consumer->enabled) { | |
2065 | send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session, | |
2066 | ksess->consumer, ksess->consumer_fd); | |
2067 | if (ret != LTTCOMM_OK) { | |
2068 | goto error; | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | error: | |
2073 | return ret; | |
2074 | } | |
2075 | ||
2076 | /* | |
2077 | * Copy consumer output from the tracing session to the domain session. The | |
2078 | * function also applies the right modification on a per domain basis for the | |
2079 | * trace files destination directory. | |
2080 | */ | |
2081 | static int copy_session_consumer(int domain, struct ltt_session *session) | |
2082 | { | |
2083 | int ret; | |
2084 | const char *dir_name; | |
2085 | struct consumer_output *consumer; | |
2086 | ||
2087 | switch (domain) { | |
2088 | case LTTNG_DOMAIN_KERNEL: | |
2089 | DBG3("Copying tracing session consumer output in kernel session"); | |
2090 | session->kernel_session->consumer = | |
2091 | consumer_copy_output(session->consumer); | |
2092 | /* Ease our life a bit for the next part */ | |
2093 | consumer = session->kernel_session->consumer; | |
2094 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
2095 | break; | |
2096 | case LTTNG_DOMAIN_UST: | |
2097 | DBG3("Copying tracing session consumer output in UST session"); | |
2098 | session->ust_session->consumer = | |
2099 | consumer_copy_output(session->consumer); | |
2100 | /* Ease our life a bit for the next part */ | |
2101 | consumer = session->ust_session->consumer; | |
2102 | dir_name = DEFAULT_UST_TRACE_DIR; | |
2103 | break; | |
2104 | default: | |
2105 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2106 | goto error; | |
2107 | } | |
2108 | ||
2109 | /* Append correct directory to subdir */ | |
2110 | strncat(consumer->subdir, dir_name, sizeof(consumer->subdir)); | |
2111 | DBG3("Copy session consumer subdir %s", consumer->subdir); | |
2112 | ||
2113 | /* Add default trace directory name */ | |
2114 | if (consumer->type == CONSUMER_DST_LOCAL) { | |
2115 | strncat(consumer->dst.trace_path, dir_name, | |
2116 | sizeof(consumer->dst.trace_path)); | |
2117 | } | |
2118 | ||
2119 | ret = LTTCOMM_OK; | |
2120 | ||
1d4b027a DG |
2121 | error: |
2122 | return ret; | |
8c0faa1d DG |
2123 | } |
2124 | ||
0177d773 DG |
2125 | /* |
2126 | * Create an UST session and add it to the session ust list. | |
2127 | */ | |
44d3bd01 | 2128 | static int create_ust_session(struct ltt_session *session, |
6df2e2c9 | 2129 | struct lttng_domain *domain) |
0177d773 | 2130 | { |
6df2e2c9 | 2131 | int ret; |
00e2e675 DG |
2132 | struct ltt_ust_session *lus = NULL; |
2133 | ||
2134 | assert(session); | |
2135 | assert(session->consumer); | |
44d3bd01 DG |
2136 | |
2137 | switch (domain->type) { | |
48842b30 | 2138 | case LTTNG_DOMAIN_UST: |
44d3bd01 DG |
2139 | break; |
2140 | default: | |
00e2e675 | 2141 | ERR("Unknown UST domain on create session %d", domain->type); |
13384287 | 2142 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
44d3bd01 DG |
2143 | goto error; |
2144 | } | |
0177d773 DG |
2145 | |
2146 | DBG("Creating UST session"); | |
2147 | ||
a991f516 | 2148 | lus = trace_ust_create_session(session->path, session->id, domain); |
0177d773 | 2149 | if (lus == NULL) { |
44d3bd01 | 2150 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
2151 | goto error; |
2152 | } | |
2153 | ||
00e2e675 DG |
2154 | if (session->consumer->type == CONSUMER_DST_LOCAL) { |
2155 | ret = run_as_mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG, | |
2156 | session->uid, session->gid); | |
2157 | if (ret < 0) { | |
2158 | if (ret != -EEXIST) { | |
2159 | ERR("Trace directory creation error"); | |
2160 | ret = LTTCOMM_UST_SESS_FAIL; | |
2161 | goto error; | |
2162 | } | |
0177d773 DG |
2163 | } |
2164 | } | |
2165 | ||
6df2e2c9 MD |
2166 | lus->uid = session->uid; |
2167 | lus->gid = session->gid; | |
f6a9efaa | 2168 | session->ust_session = lus; |
44d3bd01 | 2169 | |
00e2e675 DG |
2170 | /* Copy session output to the newly created UST session */ |
2171 | ret = copy_session_consumer(domain->type, session); | |
2172 | if (ret != LTTCOMM_OK) { | |
2173 | goto error; | |
2174 | } | |
2175 | ||
44d3bd01 | 2176 | return LTTCOMM_OK; |
0177d773 DG |
2177 | |
2178 | error: | |
2179 | free(lus); | |
00e2e675 | 2180 | session->ust_session = NULL; |
0177d773 DG |
2181 | return ret; |
2182 | } | |
2183 | ||
333f285e | 2184 | /* |
d063d709 | 2185 | * Create a kernel tracer session then create the default channel. |
333f285e | 2186 | */ |
6df2e2c9 | 2187 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 2188 | { |
f3ed775e | 2189 | int ret; |
f3ed775e DG |
2190 | |
2191 | DBG("Creating kernel session"); | |
2192 | ||
2193 | ret = kernel_create_session(session, kernel_tracer_fd); | |
2194 | if (ret < 0) { | |
2195 | ret = LTTCOMM_KERN_SESS_FAIL; | |
2196 | goto error; | |
333f285e DG |
2197 | } |
2198 | ||
d9800920 | 2199 | /* Set kernel consumer socket fd */ |
03550b58 | 2200 | if (kconsumer_data.cmd_sock >= 0) { |
3bd1e081 | 2201 | session->kernel_session->consumer_fd = kconsumer_data.cmd_sock; |
d9800920 DG |
2202 | } |
2203 | ||
00e2e675 DG |
2204 | /* Copy session output to the newly created Kernel session */ |
2205 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
2206 | if (ret != LTTCOMM_OK) { | |
2207 | goto error; | |
2208 | } | |
2209 | ||
2210 | /* Create directory(ies) on local filesystem. */ | |
2211 | if (session->consumer->type == CONSUMER_DST_LOCAL) { | |
2212 | ret = run_as_mkdir_recursive( | |
2213 | session->kernel_session->consumer->dst.trace_path, | |
2214 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2215 | if (ret < 0) { | |
2216 | if (ret != -EEXIST) { | |
2217 | ERR("Trace directory creation error"); | |
2218 | goto error; | |
2219 | } | |
7a485870 DG |
2220 | } |
2221 | } | |
00e2e675 | 2222 | |
6df2e2c9 MD |
2223 | session->kernel_session->uid = session->uid; |
2224 | session->kernel_session->gid = session->gid; | |
7a485870 | 2225 | |
00e2e675 DG |
2226 | return LTTCOMM_OK; |
2227 | ||
f3ed775e | 2228 | error: |
00e2e675 DG |
2229 | trace_kernel_destroy_session(session->kernel_session); |
2230 | session->kernel_session = NULL; | |
f3ed775e | 2231 | return ret; |
333f285e DG |
2232 | } |
2233 | ||
8e0af1b4 | 2234 | /* |
c7704d57 DG |
2235 | * Check if the UID or GID match the session. Root user has access to all |
2236 | * sessions. | |
8e0af1b4 | 2237 | */ |
c7704d57 | 2238 | static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) |
8e0af1b4 | 2239 | { |
c7704d57 | 2240 | if (uid != session->uid && gid != session->gid && uid != 0) { |
8e0af1b4 MD |
2241 | return 0; |
2242 | } else { | |
2243 | return 1; | |
2244 | } | |
2245 | } | |
2246 | ||
00e2e675 DG |
2247 | /* |
2248 | * Count number of session permitted by uid/gid. | |
2249 | */ | |
8e0af1b4 MD |
2250 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) |
2251 | { | |
2252 | unsigned int i = 0; | |
2253 | struct ltt_session *session; | |
2254 | ||
2255 | DBG("Counting number of available session for UID %d GID %d", | |
2256 | uid, gid); | |
2257 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
2258 | /* | |
2259 | * Only list the sessions the user can control. | |
2260 | */ | |
2261 | if (!session_access_ok(session, uid, gid)) { | |
2262 | continue; | |
2263 | } | |
2264 | i++; | |
2265 | } | |
2266 | return i; | |
2267 | } | |
2268 | ||
6c9cc2ab DG |
2269 | /* |
2270 | * Using the session list, filled a lttng_session array to send back to the | |
2271 | * client for session listing. | |
2272 | * | |
2273 | * The session list lock MUST be acquired before calling this function. Use | |
54d01ffb | 2274 | * session_lock_list() and session_unlock_list(). |
6c9cc2ab | 2275 | */ |
c7704d57 DG |
2276 | static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid, |
2277 | gid_t gid) | |
6c9cc2ab | 2278 | { |
8e0af1b4 | 2279 | unsigned int i = 0; |
6c9cc2ab DG |
2280 | struct ltt_session *session; |
2281 | ||
8e0af1b4 MD |
2282 | DBG("Getting all available session for UID %d GID %d", |
2283 | uid, gid); | |
6c9cc2ab DG |
2284 | /* |
2285 | * Iterate over session list and append data after the control struct in | |
2286 | * the buffer. | |
2287 | */ | |
2288 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
8e0af1b4 MD |
2289 | /* |
2290 | * Only list the sessions the user can control. | |
2291 | */ | |
2292 | if (!session_access_ok(session, uid, gid)) { | |
2293 | continue; | |
2294 | } | |
6c9cc2ab | 2295 | strncpy(sessions[i].path, session->path, PATH_MAX); |
99497cd0 | 2296 | sessions[i].path[PATH_MAX - 1] = '\0'; |
6c9cc2ab | 2297 | strncpy(sessions[i].name, session->name, NAME_MAX); |
99497cd0 | 2298 | sessions[i].name[NAME_MAX - 1] = '\0'; |
464dd62d | 2299 | sessions[i].enabled = session->enabled; |
6c9cc2ab DG |
2300 | i++; |
2301 | } | |
2302 | } | |
2303 | ||
9f19cc17 DG |
2304 | /* |
2305 | * Fill lttng_channel array of all channels. | |
2306 | */ | |
b551a063 | 2307 | static void list_lttng_channels(int domain, struct ltt_session *session, |
9f19cc17 DG |
2308 | struct lttng_channel *channels) |
2309 | { | |
2310 | int i = 0; | |
2311 | struct ltt_kernel_channel *kchan; | |
2312 | ||
2313 | DBG("Listing channels for session %s", session->name); | |
2314 | ||
b551a063 DG |
2315 | switch (domain) { |
2316 | case LTTNG_DOMAIN_KERNEL: | |
2317 | /* Kernel channels */ | |
2318 | if (session->kernel_session != NULL) { | |
2319 | cds_list_for_each_entry(kchan, | |
2320 | &session->kernel_session->channel_list.head, list) { | |
2321 | /* Copy lttng_channel struct to array */ | |
2322 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
2323 | channels[i].enabled = kchan->enabled; | |
2324 | i++; | |
2325 | } | |
2326 | } | |
2327 | break; | |
2328 | case LTTNG_DOMAIN_UST: | |
2329 | { | |
bec39940 | 2330 | struct lttng_ht_iter iter; |
b551a063 DG |
2331 | struct ltt_ust_channel *uchan; |
2332 | ||
bec39940 DG |
2333 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
2334 | &iter.iter, uchan, node.node) { | |
b551a063 DG |
2335 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); |
2336 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
2337 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
2338 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
2339 | channels[i].attr.switch_timer_interval = | |
2340 | uchan->attr.switch_timer_interval; | |
2341 | channels[i].attr.read_timer_interval = | |
2342 | uchan->attr.read_timer_interval; | |
aea829b3 | 2343 | channels[i].enabled = uchan->enabled; |
5edd7e09 DG |
2344 | switch (uchan->attr.output) { |
2345 | case LTTNG_UST_MMAP: | |
2346 | default: | |
2347 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
2348 | break; | |
2349 | } | |
befe0905 | 2350 | i++; |
b551a063 DG |
2351 | } |
2352 | break; | |
2353 | } | |
2354 | default: | |
2355 | break; | |
2356 | } | |
2357 | } | |
2358 | ||
2359 | /* | |
2360 | * Create a list of ust global domain events. | |
2361 | */ | |
2362 | static int list_lttng_ust_global_events(char *channel_name, | |
2363 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
2364 | { | |
2365 | int i = 0, ret = 0; | |
2366 | unsigned int nb_event = 0; | |
bec39940 DG |
2367 | struct lttng_ht_iter iter; |
2368 | struct lttng_ht_node_str *node; | |
b551a063 DG |
2369 | struct ltt_ust_channel *uchan; |
2370 | struct ltt_ust_event *uevent; | |
2371 | struct lttng_event *tmp; | |
2372 | ||
2373 | DBG("Listing UST global events for channel %s", channel_name); | |
2374 | ||
2375 | rcu_read_lock(); | |
2376 | ||
bec39940 DG |
2377 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); |
2378 | node = lttng_ht_iter_get_node_str(&iter); | |
d7b75b30 DG |
2379 | if (node == NULL) { |
2380 | ret = -LTTCOMM_UST_CHAN_NOT_FOUND; | |
2381 | goto error; | |
b551a063 DG |
2382 | } |
2383 | ||
bec39940 | 2384 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
d7b75b30 | 2385 | |
bec39940 | 2386 | nb_event += lttng_ht_get_count(uchan->events); |
d7b75b30 | 2387 | |
b551a063 DG |
2388 | if (nb_event == 0) { |
2389 | ret = nb_event; | |
2390 | goto error; | |
2391 | } | |
2392 | ||
2393 | DBG3("Listing UST global %d events", nb_event); | |
2394 | ||
2395 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2396 | if (tmp == NULL) { | |
2397 | ret = -LTTCOMM_FATAL; | |
2398 | goto error; | |
2399 | } | |
2400 | ||
bec39940 | 2401 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
d7b75b30 DG |
2402 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
2403 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2404 | tmp[i].enabled = uevent->enabled; | |
2405 | switch (uevent->attr.instrumentation) { | |
2406 | case LTTNG_UST_TRACEPOINT: | |
2407 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
2408 | break; | |
2409 | case LTTNG_UST_PROBE: | |
2410 | tmp[i].type = LTTNG_EVENT_PROBE; | |
2411 | break; | |
2412 | case LTTNG_UST_FUNCTION: | |
2413 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
2414 | break; | |
0cda4f28 | 2415 | } |
8005f29a | 2416 | tmp[i].loglevel = uevent->attr.loglevel; |
0cda4f28 | 2417 | switch (uevent->attr.loglevel_type) { |
8005f29a | 2418 | case LTTNG_UST_LOGLEVEL_ALL: |
22e25b71 | 2419 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
0cda4f28 | 2420 | break; |
8005f29a | 2421 | case LTTNG_UST_LOGLEVEL_RANGE: |
22e25b71 | 2422 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
8005f29a MD |
2423 | break; |
2424 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
22e25b71 | 2425 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
ac3bd9c0 | 2426 | break; |
9f19cc17 | 2427 | } |
fceb65df MD |
2428 | if (uevent->filter) { |
2429 | tmp[i].filter = 1; | |
2430 | } | |
d7b75b30 | 2431 | i++; |
9f19cc17 DG |
2432 | } |
2433 | ||
b551a063 DG |
2434 | ret = nb_event; |
2435 | *events = tmp; | |
2436 | ||
2437 | error: | |
2438 | rcu_read_unlock(); | |
2439 | return ret; | |
9f19cc17 DG |
2440 | } |
2441 | ||
2442 | /* | |
b551a063 | 2443 | * Fill lttng_event array of all kernel events in the channel. |
9f19cc17 | 2444 | */ |
b551a063 DG |
2445 | static int list_lttng_kernel_events(char *channel_name, |
2446 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
9f19cc17 | 2447 | { |
b551a063 DG |
2448 | int i = 0, ret; |
2449 | unsigned int nb_event; | |
9f19cc17 | 2450 | struct ltt_kernel_event *event; |
b551a063 DG |
2451 | struct ltt_kernel_channel *kchan; |
2452 | ||
2453 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
2454 | if (kchan == NULL) { | |
2455 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2456 | goto error; | |
2457 | } | |
2458 | ||
2459 | nb_event = kchan->event_count; | |
9f19cc17 DG |
2460 | |
2461 | DBG("Listing events for channel %s", kchan->channel->name); | |
2462 | ||
b551a063 DG |
2463 | if (nb_event == 0) { |
2464 | ret = nb_event; | |
2465 | goto error; | |
2466 | } | |
2467 | ||
2468 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2469 | if (*events == NULL) { | |
2470 | ret = LTTCOMM_FATAL; | |
2471 | goto error; | |
2472 | } | |
2473 | ||
9f19cc17 DG |
2474 | /* Kernel channels */ |
2475 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
b551a063 DG |
2476 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
2477 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2478 | (*events)[i].enabled = event->enabled; | |
9f19cc17 DG |
2479 | switch (event->event->instrumentation) { |
2480 | case LTTNG_KERNEL_TRACEPOINT: | |
b551a063 | 2481 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; |
9f19cc17 DG |
2482 | break; |
2483 | case LTTNG_KERNEL_KPROBE: | |
2484 | case LTTNG_KERNEL_KRETPROBE: | |
b551a063 DG |
2485 | (*events)[i].type = LTTNG_EVENT_PROBE; |
2486 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
9f19cc17 DG |
2487 | sizeof(struct lttng_kernel_kprobe)); |
2488 | break; | |
2489 | case LTTNG_KERNEL_FUNCTION: | |
b551a063 DG |
2490 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
2491 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
9f19cc17 DG |
2492 | sizeof(struct lttng_kernel_function)); |
2493 | break; | |
0133c199 | 2494 | case LTTNG_KERNEL_NOOP: |
b551a063 | 2495 | (*events)[i].type = LTTNG_EVENT_NOOP; |
0133c199 | 2496 | break; |
a54bd42d | 2497 | case LTTNG_KERNEL_SYSCALL: |
b551a063 | 2498 | (*events)[i].type = LTTNG_EVENT_SYSCALL; |
0133c199 | 2499 | break; |
7a3d1328 MD |
2500 | case LTTNG_KERNEL_ALL: |
2501 | assert(0); | |
2502 | break; | |
9f19cc17 DG |
2503 | } |
2504 | i++; | |
2505 | } | |
b551a063 DG |
2506 | |
2507 | return nb_event; | |
2508 | ||
2509 | error: | |
2510 | return ret; | |
9f19cc17 DG |
2511 | } |
2512 | ||
fac6795d | 2513 | /* |
54d01ffb | 2514 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
fac6795d | 2515 | */ |
54d01ffb DG |
2516 | static int cmd_disable_channel(struct ltt_session *session, |
2517 | int domain, char *channel_name) | |
fac6795d | 2518 | { |
54d01ffb | 2519 | int ret; |
78f0bacd DG |
2520 | struct ltt_ust_session *usess; |
2521 | ||
2522 | usess = session->ust_session; | |
379473d2 | 2523 | |
54d01ffb | 2524 | switch (domain) { |
78f0bacd DG |
2525 | case LTTNG_DOMAIN_KERNEL: |
2526 | { | |
2527 | ret = channel_kernel_disable(session->kernel_session, | |
2528 | channel_name); | |
2529 | if (ret != LTTCOMM_OK) { | |
2530 | goto error; | |
2531 | } | |
54d01ffb | 2532 | |
78f0bacd DG |
2533 | kernel_wait_quiescent(kernel_tracer_fd); |
2534 | break; | |
2535 | } | |
2536 | case LTTNG_DOMAIN_UST: | |
2537 | { | |
2538 | struct ltt_ust_channel *uchan; | |
bec39940 | 2539 | struct lttng_ht *chan_ht; |
78f0bacd | 2540 | |
7885e399 DG |
2541 | chan_ht = usess->domain_global.channels; |
2542 | ||
2543 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
78f0bacd DG |
2544 | if (uchan == NULL) { |
2545 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
54d01ffb | 2546 | goto error; |
78f0bacd DG |
2547 | } |
2548 | ||
7885e399 DG |
2549 | ret = channel_ust_disable(usess, domain, uchan); |
2550 | if (ret != LTTCOMM_OK) { | |
78f0bacd DG |
2551 | goto error; |
2552 | } | |
78f0bacd DG |
2553 | break; |
2554 | } | |
d78d6610 | 2555 | #if 0 |
78f0bacd DG |
2556 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
2557 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2558 | case LTTNG_DOMAIN_UST_PID: | |
d78d6610 | 2559 | #endif |
78f0bacd DG |
2560 | default: |
2561 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2562 | goto error; | |
20fe2104 DG |
2563 | } |
2564 | ||
54d01ffb | 2565 | ret = LTTCOMM_OK; |
d65106b1 | 2566 | |
54d01ffb DG |
2567 | error: |
2568 | return ret; | |
2569 | } | |
2570 | ||
2571 | /* | |
2572 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
2573 | */ | |
44d3bd01 | 2574 | static int cmd_enable_channel(struct ltt_session *session, |
aea829b3 | 2575 | int domain, struct lttng_channel *attr) |
54d01ffb DG |
2576 | { |
2577 | int ret; | |
f6a9efaa | 2578 | struct ltt_ust_session *usess = session->ust_session; |
bec39940 | 2579 | struct lttng_ht *chan_ht; |
f6a9efaa | 2580 | |
5d56b5aa | 2581 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
d65106b1 | 2582 | |
aea829b3 | 2583 | switch (domain) { |
44d3bd01 DG |
2584 | case LTTNG_DOMAIN_KERNEL: |
2585 | { | |
2586 | struct ltt_kernel_channel *kchan; | |
54d01ffb | 2587 | |
44d3bd01 DG |
2588 | kchan = trace_kernel_get_channel_by_name(attr->name, |
2589 | session->kernel_session); | |
2590 | if (kchan == NULL) { | |
2591 | ret = channel_kernel_create(session->kernel_session, | |
2592 | attr, kernel_poll_pipe[1]); | |
2593 | } else { | |
2594 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
2595 | } | |
2596 | ||
2597 | if (ret != LTTCOMM_OK) { | |
2598 | goto error; | |
2599 | } | |
2600 | ||
2601 | kernel_wait_quiescent(kernel_tracer_fd); | |
2602 | break; | |
2603 | } | |
f6a9efaa DG |
2604 | case LTTNG_DOMAIN_UST: |
2605 | { | |
2606 | struct ltt_ust_channel *uchan; | |
2607 | ||
7885e399 | 2608 | chan_ht = usess->domain_global.channels; |
f6a9efaa | 2609 | |
7885e399 | 2610 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
f6a9efaa | 2611 | if (uchan == NULL) { |
7885e399 | 2612 | ret = channel_ust_create(usess, domain, attr); |
f6a9efaa | 2613 | } else { |
7885e399 | 2614 | ret = channel_ust_enable(usess, domain, uchan); |
48842b30 | 2615 | } |
f6a9efaa DG |
2616 | break; |
2617 | } | |
d78d6610 | 2618 | #if 0 |
78f0bacd DG |
2619 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
2620 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
44d3bd01 | 2621 | case LTTNG_DOMAIN_UST_PID: |
d78d6610 | 2622 | #endif |
44d3bd01 DG |
2623 | default: |
2624 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2625 | goto error; | |
54d01ffb DG |
2626 | } |
2627 | ||
54d01ffb DG |
2628 | error: |
2629 | return ret; | |
2630 | } | |
2631 | ||
2632 | /* | |
2633 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
2634 | */ | |
2635 | static int cmd_disable_event(struct ltt_session *session, int domain, | |
2636 | char *channel_name, char *event_name) | |
2637 | { | |
2638 | int ret; | |
54d01ffb DG |
2639 | |
2640 | switch (domain) { | |
2641 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
2642 | { |
2643 | struct ltt_kernel_channel *kchan; | |
b0a40d28 | 2644 | struct ltt_kernel_session *ksess; |
2bdd86d4 | 2645 | |
b0a40d28 DG |
2646 | ksess = session->kernel_session; |
2647 | ||
2648 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
2649 | if (kchan == NULL) { |
2650 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2651 | goto error; | |
d65106b1 DG |
2652 | } |
2653 | ||
b0a40d28 | 2654 | ret = event_kernel_disable_tracepoint(ksess, kchan, event_name); |
54d01ffb DG |
2655 | if (ret != LTTCOMM_OK) { |
2656 | goto error; | |
2657 | } | |
2658 | ||
2659 | kernel_wait_quiescent(kernel_tracer_fd); | |
d65106b1 | 2660 | break; |
2bdd86d4 MD |
2661 | } |
2662 | case LTTNG_DOMAIN_UST: | |
b0a40d28 | 2663 | { |
b0a40d28 | 2664 | struct ltt_ust_channel *uchan; |
7f79d3a1 | 2665 | struct ltt_ust_session *usess; |
b0a40d28 DG |
2666 | |
2667 | usess = session->ust_session; | |
2668 | ||
2669 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2670 | channel_name); | |
2671 | if (uchan == NULL) { | |
2672 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2673 | goto error; | |
2674 | } | |
2675 | ||
7f79d3a1 DG |
2676 | ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name); |
2677 | if (ret != LTTCOMM_OK) { | |
b0a40d28 DG |
2678 | goto error; |
2679 | } | |
2680 | ||
7f79d3a1 | 2681 | DBG3("Disable UST event %s in channel %s completed", event_name, |
b0a40d28 | 2682 | channel_name); |
b0a40d28 DG |
2683 | break; |
2684 | } | |
d78d6610 | 2685 | #if 0 |
2bdd86d4 MD |
2686 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2687 | case LTTNG_DOMAIN_UST_PID: | |
2688 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 2689 | #endif |
54d01ffb | 2690 | default: |
1ab1ea0b | 2691 | ret = LTTCOMM_UND; |
54d01ffb | 2692 | goto error; |
d65106b1 | 2693 | } |
26cc6b4e | 2694 | |
54d01ffb DG |
2695 | ret = LTTCOMM_OK; |
2696 | ||
2697 | error: | |
2698 | return ret; | |
2699 | } | |
2700 | ||
2701 | /* | |
2702 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. | |
2703 | */ | |
2704 | static int cmd_disable_event_all(struct ltt_session *session, int domain, | |
2705 | char *channel_name) | |
2706 | { | |
2707 | int ret; | |
54d01ffb DG |
2708 | |
2709 | switch (domain) { | |
2710 | case LTTNG_DOMAIN_KERNEL: | |
9730260e DG |
2711 | { |
2712 | struct ltt_kernel_session *ksess; | |
2713 | struct ltt_kernel_channel *kchan; | |
2714 | ||
2715 | ksess = session->kernel_session; | |
2716 | ||
2717 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
2718 | if (kchan == NULL) { |
2719 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2720 | goto error; | |
26cc6b4e DG |
2721 | } |
2722 | ||
9730260e | 2723 | ret = event_kernel_disable_all(ksess, kchan); |
54d01ffb | 2724 | if (ret != LTTCOMM_OK) { |
0b97ec54 | 2725 | goto error; |
26cc6b4e DG |
2726 | } |
2727 | ||
54d01ffb | 2728 | kernel_wait_quiescent(kernel_tracer_fd); |
26cc6b4e | 2729 | break; |
9730260e DG |
2730 | } |
2731 | case LTTNG_DOMAIN_UST: | |
2732 | { | |
2733 | struct ltt_ust_session *usess; | |
2734 | struct ltt_ust_channel *uchan; | |
2735 | ||
2736 | usess = session->ust_session; | |
2737 | ||
2738 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2739 | channel_name); | |
2740 | if (uchan == NULL) { | |
2741 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2742 | goto error; | |
2743 | } | |
2744 | ||
7f79d3a1 DG |
2745 | ret = event_ust_disable_all_tracepoints(usess, domain, uchan); |
2746 | if (ret != 0) { | |
9730260e DG |
2747 | goto error; |
2748 | } | |
2749 | ||
7f79d3a1 | 2750 | DBG3("Disable all UST events in channel %s completed", channel_name); |
9730260e DG |
2751 | |
2752 | break; | |
2753 | } | |
d78d6610 | 2754 | #if 0 |
9730260e DG |
2755 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2756 | case LTTNG_DOMAIN_UST_PID: | |
2757 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 2758 | #endif |
54d01ffb | 2759 | default: |
1ab1ea0b | 2760 | ret = LTTCOMM_UND; |
54d01ffb | 2761 | goto error; |
26cc6b4e | 2762 | } |
e953ef25 | 2763 | |
54d01ffb | 2764 | ret = LTTCOMM_OK; |
e953ef25 | 2765 | |
54d01ffb DG |
2766 | error: |
2767 | return ret; | |
2768 | } | |
f5177a38 | 2769 | |
54d01ffb DG |
2770 | /* |
2771 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
2772 | */ | |
2773 | static int cmd_add_context(struct ltt_session *session, int domain, | |
2774 | char *channel_name, char *event_name, struct lttng_event_context *ctx) | |
2775 | { | |
2776 | int ret; | |
f5177a38 | 2777 | |
54d01ffb DG |
2778 | switch (domain) { |
2779 | case LTTNG_DOMAIN_KERNEL: | |
2780 | /* Add kernel context to kernel tracer */ | |
2781 | ret = context_kernel_add(session->kernel_session, ctx, | |
2782 | event_name, channel_name); | |
2783 | if (ret != LTTCOMM_OK) { | |
f5177a38 | 2784 | goto error; |
e953ef25 | 2785 | } |
2bdd86d4 MD |
2786 | break; |
2787 | case LTTNG_DOMAIN_UST: | |
2788 | { | |
55cc08a6 | 2789 | struct ltt_ust_session *usess = session->ust_session; |
e953ef25 | 2790 | |
55cc08a6 DG |
2791 | ret = context_ust_add(usess, domain, ctx, event_name, channel_name); |
2792 | if (ret != LTTCOMM_OK) { | |
2793 | goto error; | |
2bdd86d4 | 2794 | } |
e953ef25 | 2795 | break; |
2bdd86d4 | 2796 | } |
d78d6610 | 2797 | #if 0 |
55cc08a6 DG |
2798 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2799 | case LTTNG_DOMAIN_UST_PID: | |
2800 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 2801 | #endif |
54d01ffb | 2802 | default: |
1ab1ea0b | 2803 | ret = LTTCOMM_UND; |
54d01ffb | 2804 | goto error; |
e953ef25 | 2805 | } |
950131af | 2806 | |
54d01ffb | 2807 | ret = LTTCOMM_OK; |
950131af | 2808 | |
54d01ffb DG |
2809 | error: |
2810 | return ret; | |
2811 | } | |
2812 | ||
53a80697 MD |
2813 | /* |
2814 | * Command LTTNG_SET_FILTER processed by the client thread. | |
2815 | */ | |
2816 | static int cmd_set_filter(struct ltt_session *session, int domain, | |
2817 | char *channel_name, char *event_name, | |
2818 | struct lttng_filter_bytecode *bytecode) | |
2819 | { | |
2820 | int ret; | |
2821 | ||
2822 | switch (domain) { | |
2823 | case LTTNG_DOMAIN_KERNEL: | |
2824 | ret = LTTCOMM_FATAL; | |
2825 | break; | |
2826 | case LTTNG_DOMAIN_UST: | |
2827 | { | |
2828 | struct ltt_ust_session *usess = session->ust_session; | |
2829 | ||
2830 | ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name); | |
2831 | if (ret != LTTCOMM_OK) { | |
2832 | goto error; | |
2833 | } | |
2834 | break; | |
2835 | } | |
2836 | #if 0 | |
2837 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2838 | case LTTNG_DOMAIN_UST_PID: | |
2839 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
2840 | #endif | |
2841 | default: | |
2842 | ret = LTTCOMM_UND; | |
2843 | goto error; | |
2844 | } | |
2845 | ||
2846 | ret = LTTCOMM_OK; | |
2847 | ||
2848 | error: | |
2849 | return ret; | |
2850 | ||
2851 | } | |
2852 | ||
54d01ffb DG |
2853 | /* |
2854 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2855 | */ | |
2856 | static int cmd_enable_event(struct ltt_session *session, int domain, | |
2857 | char *channel_name, struct lttng_event *event) | |
2858 | { | |
2859 | int ret; | |
f6cd6b0f | 2860 | struct lttng_channel *attr; |
48842b30 | 2861 | struct ltt_ust_session *usess = session->ust_session; |
54d01ffb DG |
2862 | |
2863 | switch (domain) { | |
2864 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
2865 | { |
2866 | struct ltt_kernel_channel *kchan; | |
2867 | ||
54d01ffb DG |
2868 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2869 | session->kernel_session); | |
2870 | if (kchan == NULL) { | |
f6cd6b0f DG |
2871 | attr = channel_new_default_attr(domain); |
2872 | if (attr == NULL) { | |
2873 | ret = LTTCOMM_FATAL; | |
2874 | goto error; | |
2875 | } | |
2876 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
2877 | ||
54d01ffb | 2878 | /* This call will notify the kernel thread */ |
44d3bd01 | 2879 | ret = channel_kernel_create(session->kernel_session, |
f6cd6b0f | 2880 | attr, kernel_poll_pipe[1]); |
54d01ffb | 2881 | if (ret != LTTCOMM_OK) { |
ff4d74e6 | 2882 | free(attr); |
f5177a38 DG |
2883 | goto error; |
2884 | } | |
ff4d74e6 | 2885 | free(attr); |
54d01ffb | 2886 | } |
950131af | 2887 | |
54d01ffb DG |
2888 | /* Get the newly created kernel channel pointer */ |
2889 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2890 | session->kernel_session); | |
2891 | if (kchan == NULL) { | |
2892 | /* This sould not happen... */ | |
2893 | ret = LTTCOMM_FATAL; | |
2894 | goto error; | |
2895 | } | |
f5177a38 | 2896 | |
48842b30 DG |
2897 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, |
2898 | event); | |
54d01ffb | 2899 | if (ret != LTTCOMM_OK) { |
f5177a38 | 2900 | goto error; |
950131af DG |
2901 | } |
2902 | ||
54d01ffb | 2903 | kernel_wait_quiescent(kernel_tracer_fd); |
950131af | 2904 | break; |
2bdd86d4 MD |
2905 | } |
2906 | case LTTNG_DOMAIN_UST: | |
2907 | { | |
aea829b3 | 2908 | struct lttng_channel *attr; |
edb67388 | 2909 | struct ltt_ust_channel *uchan; |
2bdd86d4 | 2910 | |
edb67388 | 2911 | /* Get channel from global UST domain */ |
48842b30 DG |
2912 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
2913 | channel_name); | |
2914 | if (uchan == NULL) { | |
aea829b3 DG |
2915 | /* Create default channel */ |
2916 | attr = channel_new_default_attr(domain); | |
2917 | if (attr == NULL) { | |
2918 | ret = LTTCOMM_FATAL; | |
2919 | goto error; | |
2920 | } | |
596219f7 | 2921 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
edb67388 | 2922 | attr->name[NAME_MAX - 1] = '\0'; |
aea829b3 | 2923 | |
7885e399 | 2924 | ret = channel_ust_create(usess, domain, attr); |
edb67388 | 2925 | if (ret != LTTCOMM_OK) { |
ff4d74e6 | 2926 | free(attr); |
aea829b3 DG |
2927 | goto error; |
2928 | } | |
aea829b3 DG |
2929 | free(attr); |
2930 | ||
2931 | /* Get the newly created channel reference back */ | |
2932 | uchan = trace_ust_find_channel_by_name( | |
2933 | usess->domain_global.channels, channel_name); | |
2934 | if (uchan == NULL) { | |
2935 | /* Something is really wrong */ | |
2936 | ret = LTTCOMM_FATAL; | |
2937 | goto error; | |
2938 | } | |
48842b30 | 2939 | } |
2bdd86d4 | 2940 | |
edb67388 | 2941 | /* At this point, the session and channel exist on the tracer */ |
edb67388 DG |
2942 | ret = event_ust_enable_tracepoint(usess, domain, uchan, event); |
2943 | if (ret != LTTCOMM_OK) { | |
48842b30 | 2944 | goto error; |
2bdd86d4 MD |
2945 | } |
2946 | break; | |
2947 | } | |
d78d6610 | 2948 | #if 0 |
2bdd86d4 MD |
2949 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2950 | case LTTNG_DOMAIN_UST_PID: | |
2951 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 2952 | #endif |
54d01ffb | 2953 | default: |
1ab1ea0b | 2954 | ret = LTTCOMM_UND; |
54d01ffb | 2955 | goto error; |
950131af | 2956 | } |
d36b8583 | 2957 | |
54d01ffb | 2958 | ret = LTTCOMM_OK; |
d36b8583 | 2959 | |
54d01ffb DG |
2960 | error: |
2961 | return ret; | |
2962 | } | |
7d29a247 | 2963 | |
54d01ffb DG |
2964 | /* |
2965 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. | |
2966 | */ | |
2967 | static int cmd_enable_event_all(struct ltt_session *session, int domain, | |
8c9ae521 | 2968 | char *channel_name, int event_type) |
54d01ffb DG |
2969 | { |
2970 | int ret; | |
2971 | struct ltt_kernel_channel *kchan; | |
7d29a247 | 2972 | |
54d01ffb DG |
2973 | switch (domain) { |
2974 | case LTTNG_DOMAIN_KERNEL: | |
2975 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2976 | session->kernel_session); | |
2977 | if (kchan == NULL) { | |
2978 | /* This call will notify the kernel thread */ | |
44d3bd01 DG |
2979 | ret = channel_kernel_create(session->kernel_session, NULL, |
2980 | kernel_poll_pipe[1]); | |
54d01ffb DG |
2981 | if (ret != LTTCOMM_OK) { |
2982 | goto error; | |
d36b8583 | 2983 | } |
0d0c377a | 2984 | |
8f69e5eb DG |
2985 | /* Get the newly created kernel channel pointer */ |
2986 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2987 | session->kernel_session); | |
2988 | if (kchan == NULL) { | |
2989 | /* This sould not happen... */ | |
2990 | ret = LTTCOMM_FATAL; | |
2991 | goto error; | |
2992 | } | |
2993 | ||
54d01ffb | 2994 | } |
0fdd1e2c | 2995 | |
7a3d1328 | 2996 | switch (event_type) { |
76d45b40 | 2997 | case LTTNG_EVENT_SYSCALL: |
7a3d1328 | 2998 | ret = event_kernel_enable_all_syscalls(session->kernel_session, |
8c9ae521 | 2999 | kchan, kernel_tracer_fd); |
7a3d1328 | 3000 | break; |
76d45b40 | 3001 | case LTTNG_EVENT_TRACEPOINT: |
8c9ae521 | 3002 | /* |
7a3d1328 MD |
3003 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
3004 | * events already registered to the channel. | |
8c9ae521 | 3005 | */ |
7a3d1328 MD |
3006 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, |
3007 | kchan, kernel_tracer_fd); | |
3008 | break; | |
76d45b40 | 3009 | case LTTNG_EVENT_ALL: |
7a3d1328 | 3010 | /* Enable syscalls and tracepoints */ |
8c9ae521 DG |
3011 | ret = event_kernel_enable_all(session->kernel_session, |
3012 | kchan, kernel_tracer_fd); | |
7a3d1328 MD |
3013 | break; |
3014 | default: | |
3015 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
3016 | goto error; | |
8c9ae521 | 3017 | } |
8f69e5eb DG |
3018 | |
3019 | /* Manage return value */ | |
54d01ffb | 3020 | if (ret != LTTCOMM_OK) { |
0d0c377a | 3021 | goto error; |
d36b8583 DG |
3022 | } |
3023 | ||
54d01ffb | 3024 | kernel_wait_quiescent(kernel_tracer_fd); |
d36b8583 | 3025 | break; |
76d45b40 DG |
3026 | case LTTNG_DOMAIN_UST: |
3027 | { | |
3028 | struct lttng_channel *attr; | |
3029 | struct ltt_ust_channel *uchan; | |
3030 | struct ltt_ust_session *usess = session->ust_session; | |
3031 | ||
3032 | /* Get channel from global UST domain */ | |
3033 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
3034 | channel_name); | |
3035 | if (uchan == NULL) { | |
3036 | /* Create default channel */ | |
3037 | attr = channel_new_default_attr(domain); | |
3038 | if (attr == NULL) { | |
3039 | ret = LTTCOMM_FATAL; | |
3040 | goto error; | |
3041 | } | |
3042 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
3043 | attr->name[NAME_MAX - 1] = '\0'; | |
3044 | ||
3045 | /* Use the internal command enable channel */ | |
7885e399 | 3046 | ret = channel_ust_create(usess, domain, attr); |
76d45b40 DG |
3047 | if (ret != LTTCOMM_OK) { |
3048 | free(attr); | |
3049 | goto error; | |
3050 | } | |
3051 | free(attr); | |
3052 | ||
3053 | /* Get the newly created channel reference back */ | |
3054 | uchan = trace_ust_find_channel_by_name( | |
3055 | usess->domain_global.channels, channel_name); | |
3056 | if (uchan == NULL) { | |
3057 | /* Something is really wrong */ | |
3058 | ret = LTTCOMM_FATAL; | |
3059 | goto error; | |
3060 | } | |
3061 | } | |
3062 | ||
3063 | /* At this point, the session and channel exist on the tracer */ | |
3064 | ||
3065 | switch (event_type) { | |
3066 | case LTTNG_EVENT_ALL: | |
3067 | case LTTNG_EVENT_TRACEPOINT: | |
3068 | ret = event_ust_enable_all_tracepoints(usess, domain, uchan); | |
3069 | if (ret != LTTCOMM_OK) { | |
3070 | goto error; | |
3071 | } | |
3072 | break; | |
3073 | default: | |
3074 | ret = LTTCOMM_UST_ENABLE_FAIL; | |
3075 | goto error; | |
3076 | } | |
3077 | ||
3078 | /* Manage return value */ | |
3079 | if (ret != LTTCOMM_OK) { | |
3080 | goto error; | |
3081 | } | |
3082 | ||
3083 | break; | |
3084 | } | |
d78d6610 | 3085 | #if 0 |
76d45b40 DG |
3086 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
3087 | case LTTNG_DOMAIN_UST_PID: | |
3088 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 3089 | #endif |
54d01ffb | 3090 | default: |
1ab1ea0b | 3091 | ret = LTTCOMM_UND; |
54d01ffb | 3092 | goto error; |
d36b8583 | 3093 | } |
f3ed775e | 3094 | |
54d01ffb DG |
3095 | ret = LTTCOMM_OK; |
3096 | ||
3097 | error: | |
3098 | return ret; | |
3099 | } | |
3100 | ||
3101 | /* | |
3102 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
3103 | */ | |
3104 | static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) | |
3105 | { | |
3106 | int ret; | |
3107 | ssize_t nb_events = 0; | |
3108 | ||
3109 | switch (domain) { | |
3110 | case LTTNG_DOMAIN_KERNEL: | |
3111 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
3112 | if (nb_events < 0) { | |
3113 | ret = LTTCOMM_KERN_LIST_FAIL; | |
3114 | goto error; | |
894be886 | 3115 | } |
54d01ffb | 3116 | break; |
b551a063 DG |
3117 | case LTTNG_DOMAIN_UST: |
3118 | nb_events = ust_app_list_events(events); | |
3119 | if (nb_events < 0) { | |
3120 | ret = LTTCOMM_UST_LIST_FAIL; | |
3121 | goto error; | |
3122 | } | |
3123 | break; | |
54d01ffb | 3124 | default: |
1ab1ea0b | 3125 | ret = LTTCOMM_UND; |
54d01ffb DG |
3126 | goto error; |
3127 | } | |
894be886 | 3128 | |
54d01ffb | 3129 | return nb_events; |
7d29a247 | 3130 | |
54d01ffb DG |
3131 | error: |
3132 | /* Return negative value to differentiate return code */ | |
3133 | return -ret; | |
3134 | } | |
b389abbe | 3135 | |
f37d259d MD |
3136 | /* |
3137 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
3138 | */ | |
3139 | static ssize_t cmd_list_tracepoint_fields(int domain, | |
3140 | struct lttng_event_field **fields) | |
3141 | { | |
3142 | int ret; | |
3143 | ssize_t nb_fields = 0; | |
3144 | ||
3145 | switch (domain) { | |
3146 | case LTTNG_DOMAIN_UST: | |
3147 | nb_fields = ust_app_list_event_fields(fields); | |
3148 | if (nb_fields < 0) { | |
3149 | ret = LTTCOMM_UST_LIST_FAIL; | |
3150 | goto error; | |
3151 | } | |
3152 | break; | |
3153 | case LTTNG_DOMAIN_KERNEL: | |
3154 | default: /* fall-through */ | |
3155 | ret = LTTCOMM_UND; | |
3156 | goto error; | |
3157 | } | |
3158 | ||
3159 | return nb_fields; | |
3160 | ||
3161 | error: | |
3162 | /* Return negative value to differentiate return code */ | |
3163 | return -ret; | |
3164 | } | |
3165 | ||
54d01ffb DG |
3166 | /* |
3167 | * Command LTTNG_START_TRACE processed by the client thread. | |
3168 | */ | |
3169 | static int cmd_start_trace(struct ltt_session *session) | |
3170 | { | |
3171 | int ret; | |
54d01ffb | 3172 | struct ltt_kernel_session *ksession; |
b9d9b220 | 3173 | struct ltt_ust_session *usess; |
00e2e675 | 3174 | struct ltt_kernel_channel *kchan; |
b389abbe | 3175 | |
00e2e675 | 3176 | /* Ease our life a bit ;) */ |
54d01ffb | 3177 | ksession = session->kernel_session; |
b9d9b220 | 3178 | usess = session->ust_session; |
5eb91c98 | 3179 | |
b9ef1c83 | 3180 | if (session->enabled) { |
80e327fa DG |
3181 | /* Already started. */ |
3182 | ret = LTTCOMM_TRACE_ALREADY_STARTED; | |
b9ef1c83 DG |
3183 | goto error; |
3184 | } | |
3185 | ||
464dd62d MD |
3186 | session->enabled = 1; |
3187 | ||
00e2e675 DG |
3188 | ret = setup_relayd(session); |
3189 | if (ret != LTTCOMM_OK) { | |
3190 | ERR("Error setting up relayd for session %s", session->name); | |
3191 | goto error; | |
3192 | } | |
3193 | ||
54d01ffb DG |
3194 | /* Kernel tracing */ |
3195 | if (ksession != NULL) { | |
3196 | /* Open kernel metadata */ | |
3197 | if (ksession->metadata == NULL) { | |
00e2e675 DG |
3198 | ret = kernel_open_metadata(ksession, |
3199 | ksession->consumer->dst.trace_path); | |
54d01ffb DG |
3200 | if (ret < 0) { |
3201 | ret = LTTCOMM_KERN_META_FAIL; | |
3202 | goto error; | |
b389abbe | 3203 | } |
54d01ffb | 3204 | } |
7d29a247 | 3205 | |
54d01ffb | 3206 | /* Open kernel metadata stream */ |
03550b58 | 3207 | if (ksession->metadata_stream_fd < 0) { |
54d01ffb DG |
3208 | ret = kernel_open_metadata_stream(ksession); |
3209 | if (ret < 0) { | |
3210 | ERR("Kernel create metadata stream failed"); | |
3211 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
3212 | goto error; | |
3213 | } | |
3214 | } | |
3215 | ||
3216 | /* For each channel */ | |
3217 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
3218 | if (kchan->stream_count == 0) { | |
3219 | ret = kernel_open_channel_stream(kchan); | |
3220 | if (ret < 0) { | |
3221 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
7d29a247 DG |
3222 | goto error; |
3223 | } | |
54d01ffb DG |
3224 | /* Update the stream global counter */ |
3225 | ksession->stream_count_global += ret; | |
7d29a247 | 3226 | } |
54d01ffb DG |
3227 | } |
3228 | ||
3229 | /* Setup kernel consumer socket and send fds to it */ | |
3230 | ret = init_kernel_tracing(ksession); | |
3231 | if (ret < 0) { | |
3232 | ret = LTTCOMM_KERN_START_FAIL; | |
3233 | goto error; | |
3234 | } | |
3235 | ||
3236 | /* This start the kernel tracing */ | |
3237 | ret = kernel_start_session(ksession); | |
3238 | if (ret < 0) { | |
3239 | ret = LTTCOMM_KERN_START_FAIL; | |
3240 | goto error; | |
3241 | } | |
3242 | ||
3243 | /* Quiescent wait after starting trace */ | |
3244 | kernel_wait_quiescent(kernel_tracer_fd); | |
3245 | } | |
3246 | ||
36dc12cc | 3247 | /* Flag session that trace should start automatically */ |
b9d9b220 DG |
3248 | if (usess) { |
3249 | usess->start_trace = 1; | |
36dc12cc | 3250 | |
b9d9b220 DG |
3251 | ret = ust_app_start_trace_all(usess); |
3252 | if (ret < 0) { | |
3253 | ret = LTTCOMM_UST_START_FAIL; | |
3254 | goto error; | |
3255 | } | |
2bdd86d4 | 3256 | } |
54d01ffb DG |
3257 | |
3258 | ret = LTTCOMM_OK; | |
3259 | ||
3260 | error: | |
3261 | return ret; | |
3262 | } | |
3263 | ||
3264 | /* | |
3265 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
3266 | */ | |
3267 | static int cmd_stop_trace(struct ltt_session *session) | |
3268 | { | |
3269 | int ret; | |
3270 | struct ltt_kernel_channel *kchan; | |
3271 | struct ltt_kernel_session *ksession; | |
8be98f9a | 3272 | struct ltt_ust_session *usess; |
54d01ffb DG |
3273 | |
3274 | /* Short cut */ | |
3275 | ksession = session->kernel_session; | |
8be98f9a | 3276 | usess = session->ust_session; |
54d01ffb | 3277 | |
b9ef1c83 | 3278 | if (!session->enabled) { |
80e327fa | 3279 | ret = LTTCOMM_TRACE_ALREADY_STOPPED; |
b9ef1c83 DG |
3280 | goto error; |
3281 | } | |
3282 | ||
464dd62d MD |
3283 | session->enabled = 0; |
3284 | ||
54d01ffb DG |
3285 | /* Kernel tracer */ |
3286 | if (ksession != NULL) { | |
3287 | DBG("Stop kernel tracing"); | |
3288 | ||
00e2e675 DG |
3289 | /* Flush metadata if exist */ |
3290 | if (ksession->metadata_stream_fd >= 0) { | |
3291 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
3292 | if (ret < 0) { | |
3293 | ERR("Kernel metadata flush failed"); | |
3294 | } | |
54d01ffb | 3295 | } |
f34daff7 | 3296 | |
00e2e675 | 3297 | /* Flush all buffers before stopping */ |
54d01ffb DG |
3298 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
3299 | ret = kernel_flush_buffer(kchan); | |
0d0c377a | 3300 | if (ret < 0) { |
54d01ffb | 3301 | ERR("Kernel flush buffer error"); |
7d29a247 | 3302 | } |
54d01ffb | 3303 | } |
19e70852 | 3304 | |
54d01ffb DG |
3305 | ret = kernel_stop_session(ksession); |
3306 | if (ret < 0) { | |
3307 | ret = LTTCOMM_KERN_STOP_FAIL; | |
19e70852 | 3308 | goto error; |
f3ed775e | 3309 | } |
54d01ffb DG |
3310 | |
3311 | kernel_wait_quiescent(kernel_tracer_fd); | |
894be886 | 3312 | } |
54d01ffb | 3313 | |
8be98f9a MD |
3314 | if (usess) { |
3315 | usess->start_trace = 0; | |
2bdd86d4 | 3316 | |
8be98f9a | 3317 | ret = ust_app_stop_trace_all(usess); |
2bdd86d4 | 3318 | if (ret < 0) { |
190df5ac | 3319 | ret = LTTCOMM_UST_STOP_FAIL; |
2bdd86d4 MD |
3320 | goto error; |
3321 | } | |
2bdd86d4 | 3322 | } |
54d01ffb DG |
3323 | |
3324 | ret = LTTCOMM_OK; | |
3325 | ||
3326 | error: | |
3327 | return ret; | |
3328 | } | |
3329 | ||
3330 | /* | |
00e2e675 | 3331 | * Command LTTNG_CREATE_SESSION_URI processed by the client thread. |
54d01ffb | 3332 | */ |
00e2e675 DG |
3333 | static int cmd_create_session_uri(char *name, struct lttng_uri *ctrl_uri, |
3334 | struct lttng_uri *data_uri, unsigned int enable_consumer, | |
3335 | lttng_sock_cred *creds) | |
54d01ffb DG |
3336 | { |
3337 | int ret; | |
00e2e675 DG |
3338 | char *path = NULL; |
3339 | struct ltt_session *session; | |
3340 | struct consumer_output *consumer; | |
3341 | ||
3342 | /* Verify if the session already exist */ | |
3343 | session = session_find_by_name(name); | |
3344 | if (session != NULL) { | |
3345 | ret = LTTCOMM_EXIST_SESS; | |
3346 | goto error; | |
3347 | } | |
3348 | ||
3349 | /* TODO: validate URIs */ | |
3350 | ||
3351 | /* Create default consumer output */ | |
3352 | consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
3353 | if (consumer == NULL) { | |
3354 | ret = LTTCOMM_FATAL; | |
3355 | goto error; | |
3356 | } | |
3357 | strncpy(consumer->subdir, ctrl_uri->subdir, sizeof(consumer->subdir)); | |
3358 | DBG2("Consumer subdir set to %s", consumer->subdir); | |
3359 | ||
3360 | switch (ctrl_uri->dtype) { | |
3361 | case LTTNG_DST_IPV4: | |
3362 | case LTTNG_DST_IPV6: | |
3363 | /* Set control URI into consumer output object */ | |
3364 | ret = consumer_set_network_uri(consumer, ctrl_uri); | |
3365 | if (ret < 0) { | |
3366 | ret = LTTCOMM_FATAL; | |
3367 | goto error; | |
3368 | } | |
3369 | ||
3370 | /* Set data URI into consumer output object */ | |
3371 | ret = consumer_set_network_uri(consumer, data_uri); | |
3372 | if (ret < 0) { | |
3373 | ret = LTTCOMM_FATAL; | |
3374 | goto error; | |
3375 | } | |
3376 | ||
3377 | /* Empty path since the session is network */ | |
3378 | path = ""; | |
3379 | break; | |
3380 | case LTTNG_DST_PATH: | |
3381 | /* Very volatile pointer. Only used for the create session. */ | |
3382 | path = ctrl_uri->dst.path; | |
3383 | strncpy(consumer->dst.trace_path, path, | |
3384 | sizeof(consumer->dst.trace_path)); | |
3385 | break; | |
3386 | } | |
3387 | ||
3388 | /* Set if the consumer is enabled or not */ | |
3389 | consumer->enabled = enable_consumer; | |
54d01ffb | 3390 | |
730389d9 DG |
3391 | ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds), |
3392 | LTTNG_SOCK_GET_GID_CRED(creds)); | |
54d01ffb | 3393 | if (ret != LTTCOMM_OK) { |
00e2e675 | 3394 | goto consumer_error; |
54d01ffb DG |
3395 | } |
3396 | ||
00e2e675 DG |
3397 | /* Get the newly created session pointer back */ |
3398 | session = session_find_by_name(name); | |
3399 | assert(session); | |
3400 | ||
3401 | /* Assign consumer to session */ | |
3402 | session->consumer = consumer; | |
3403 | ||
3404 | return LTTCOMM_OK; | |
3405 | ||
3406 | consumer_error: | |
3407 | consumer_destroy_output(consumer); | |
3408 | error: | |
3409 | return ret; | |
3410 | } | |
3411 | ||
3412 | /* | |
3413 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
3414 | */ | |
3415 | static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds) | |
3416 | { | |
3417 | int ret; | |
3418 | struct lttng_uri uri; | |
3419 | ||
3420 | /* Zeroed temporary URI */ | |
3421 | memset(&uri, 0, sizeof(uri)); | |
3422 | ||
3423 | uri.dtype = LTTNG_DST_PATH; | |
3424 | uri.utype = LTTNG_URI_DST; | |
3425 | strncpy(uri.dst.path, path, sizeof(uri.dst.path)); | |
3426 | ||
3427 | /* TODO: Strip date-time from path and put it in uri's subdir */ | |
3428 | ||
3429 | ret = cmd_create_session_uri(name, &uri, NULL, 1, creds); | |
3430 | if (ret != LTTCOMM_OK) { | |
3431 | goto error; | |
3432 | } | |
54d01ffb DG |
3433 | |
3434 | error: | |
3435 | return ret; | |
3436 | } | |
3437 | ||
3438 | /* | |
3439 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
3440 | */ | |
3441 | static int cmd_destroy_session(struct ltt_session *session, char *name) | |
3442 | { | |
3443 | int ret; | |
3444 | ||
3445 | /* Clean kernel session teardown */ | |
3446 | teardown_kernel_session(session); | |
84cd17c6 MD |
3447 | /* UST session teardown */ |
3448 | teardown_ust_session(session); | |
54d01ffb DG |
3449 | |
3450 | /* | |
3451 | * Must notify the kernel thread here to update it's poll setin order | |
3452 | * to remove the channel(s)' fd just destroyed. | |
3453 | */ | |
3454 | ret = notify_thread_pipe(kernel_poll_pipe[1]); | |
3455 | if (ret < 0) { | |
76d7553f | 3456 | PERROR("write kernel poll pipe"); |
54d01ffb DG |
3457 | } |
3458 | ||
271933a4 | 3459 | ret = session_destroy(session); |
54d01ffb DG |
3460 | |
3461 | return ret; | |
3462 | } | |
3463 | ||
3464 | /* | |
3465 | * Command LTTNG_CALIBRATE processed by the client thread. | |
3466 | */ | |
3467 | static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) | |
3468 | { | |
3469 | int ret; | |
3470 | ||
3471 | switch (domain) { | |
3472 | case LTTNG_DOMAIN_KERNEL: | |
33a2b854 | 3473 | { |
54d01ffb | 3474 | struct lttng_kernel_calibrate kcalibrate; |
33a2b854 | 3475 | |
54d01ffb DG |
3476 | kcalibrate.type = calibrate->type; |
3477 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); | |
33a2b854 | 3478 | if (ret < 0) { |
54d01ffb DG |
3479 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
3480 | goto error; | |
33a2b854 | 3481 | } |
54d01ffb DG |
3482 | break; |
3483 | } | |
4466912f DG |
3484 | case LTTNG_DOMAIN_UST: |
3485 | { | |
3486 | struct lttng_ust_calibrate ucalibrate; | |
3487 | ||
3488 | ucalibrate.type = calibrate->type; | |
3489 | ret = ust_app_calibrate_glb(&ucalibrate); | |
3490 | if (ret < 0) { | |
3491 | ret = LTTCOMM_UST_CALIBRATE_FAIL; | |
3492 | goto error; | |
3493 | } | |
3494 | break; | |
3495 | } | |
54d01ffb | 3496 | default: |
1ab1ea0b | 3497 | ret = LTTCOMM_UND; |
54d01ffb DG |
3498 | goto error; |
3499 | } | |
33a2b854 | 3500 | |
54d01ffb | 3501 | ret = LTTCOMM_OK; |
33a2b854 | 3502 | |
54d01ffb DG |
3503 | error: |
3504 | return ret; | |
3505 | } | |
7d29a247 | 3506 | |
54d01ffb DG |
3507 | /* |
3508 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3509 | */ | |
3510 | static int cmd_register_consumer(struct ltt_session *session, int domain, | |
3511 | char *sock_path) | |
3512 | { | |
3513 | int ret, sock; | |
b389abbe | 3514 | |
54d01ffb DG |
3515 | switch (domain) { |
3516 | case LTTNG_DOMAIN_KERNEL: | |
3517 | /* Can't register a consumer if there is already one */ | |
48c4f28c | 3518 | if (session->kernel_session->consumer_fds_sent != 0) { |
54d01ffb DG |
3519 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
3520 | goto error; | |
3521 | } | |
3522 | ||
3523 | sock = lttcomm_connect_unix_sock(sock_path); | |
3524 | if (sock < 0) { | |
3525 | ret = LTTCOMM_CONNECT_FAIL; | |
3526 | goto error; | |
3527 | } | |
3528 | ||
3529 | session->kernel_session->consumer_fd = sock; | |
3530 | break; | |
3531 | default: | |
3532 | /* TODO: Userspace tracing */ | |
1ab1ea0b | 3533 | ret = LTTCOMM_UND; |
54d01ffb DG |
3534 | goto error; |
3535 | } | |
3536 | ||
3537 | ret = LTTCOMM_OK; | |
3538 | ||
3539 | error: | |
3540 | return ret; | |
3541 | } | |
3542 | ||
3543 | /* | |
3544 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3545 | */ | |
3546 | static ssize_t cmd_list_domains(struct ltt_session *session, | |
3547 | struct lttng_domain **domains) | |
3548 | { | |
b551a063 | 3549 | int ret, index = 0; |
54d01ffb DG |
3550 | ssize_t nb_dom = 0; |
3551 | ||
3552 | if (session->kernel_session != NULL) { | |
b551a063 | 3553 | DBG3("Listing domains found kernel domain"); |
54d01ffb DG |
3554 | nb_dom++; |
3555 | } | |
3556 | ||
b551a063 DG |
3557 | if (session->ust_session != NULL) { |
3558 | DBG3("Listing domains found UST global domain"); | |
3559 | nb_dom++; | |
3560 | } | |
54d01ffb | 3561 | |
b551a063 | 3562 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
54d01ffb DG |
3563 | if (*domains == NULL) { |
3564 | ret = -LTTCOMM_FATAL; | |
3565 | goto error; | |
3566 | } | |
3567 | ||
b551a063 DG |
3568 | if (session->kernel_session != NULL) { |
3569 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
3570 | index++; | |
3571 | } | |
3572 | ||
3573 | if (session->ust_session != NULL) { | |
3574 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
3575 | index++; | |
3576 | } | |
54d01ffb | 3577 | |
54d01ffb DG |
3578 | return nb_dom; |
3579 | ||
3580 | error: | |
3581 | return ret; | |
3582 | } | |
3583 | ||
3584 | /* | |
3585 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3586 | */ | |
b551a063 | 3587 | static ssize_t cmd_list_channels(int domain, struct ltt_session *session, |
54d01ffb DG |
3588 | struct lttng_channel **channels) |
3589 | { | |
3590 | int ret; | |
3591 | ssize_t nb_chan = 0; | |
3592 | ||
b551a063 DG |
3593 | switch (domain) { |
3594 | case LTTNG_DOMAIN_KERNEL: | |
3595 | if (session->kernel_session != NULL) { | |
3596 | nb_chan = session->kernel_session->channel_count; | |
3597 | } | |
0df502fd | 3598 | DBG3("Number of kernel channels %zd", nb_chan); |
b551a063 DG |
3599 | break; |
3600 | case LTTNG_DOMAIN_UST: | |
3601 | if (session->ust_session != NULL) { | |
bec39940 | 3602 | nb_chan = lttng_ht_get_count( |
b551a063 DG |
3603 | session->ust_session->domain_global.channels); |
3604 | } | |
0df502fd | 3605 | DBG3("Number of UST global channels %zd", nb_chan); |
b551a063 DG |
3606 | break; |
3607 | default: | |
3608 | *channels = NULL; | |
1ab1ea0b | 3609 | ret = -LTTCOMM_UND; |
54d01ffb DG |
3610 | goto error; |
3611 | } | |
3612 | ||
b551a063 DG |
3613 | if (nb_chan > 0) { |
3614 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); | |
3615 | if (*channels == NULL) { | |
3616 | ret = -LTTCOMM_FATAL; | |
3617 | goto error; | |
3618 | } | |
54d01ffb | 3619 | |
b551a063 DG |
3620 | list_lttng_channels(domain, session, *channels); |
3621 | } else { | |
3622 | *channels = NULL; | |
3623 | } | |
2bdd86d4 | 3624 | |
54d01ffb DG |
3625 | return nb_chan; |
3626 | ||
3627 | error: | |
3628 | return ret; | |
3629 | } | |
3630 | ||
3631 | /* | |
3632 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3633 | */ | |
b551a063 | 3634 | static ssize_t cmd_list_events(int domain, struct ltt_session *session, |
54d01ffb DG |
3635 | char *channel_name, struct lttng_event **events) |
3636 | { | |
b551a063 | 3637 | int ret = 0; |
54d01ffb | 3638 | ssize_t nb_event = 0; |
54d01ffb | 3639 | |
b551a063 DG |
3640 | switch (domain) { |
3641 | case LTTNG_DOMAIN_KERNEL: | |
3642 | if (session->kernel_session != NULL) { | |
3643 | nb_event = list_lttng_kernel_events(channel_name, | |
3644 | session->kernel_session, events); | |
54d01ffb | 3645 | } |
b551a063 DG |
3646 | break; |
3647 | case LTTNG_DOMAIN_UST: | |
3648 | { | |
3649 | if (session->ust_session != NULL) { | |
3650 | nb_event = list_lttng_ust_global_events(channel_name, | |
3651 | &session->ust_session->domain_global, events); | |
3652 | } | |
3653 | break; | |
54d01ffb | 3654 | } |
b551a063 | 3655 | default: |
1ab1ea0b | 3656 | ret = -LTTCOMM_UND; |
54d01ffb DG |
3657 | goto error; |
3658 | } | |
3659 | ||
b551a063 | 3660 | ret = nb_event; |
54d01ffb DG |
3661 | |
3662 | error: | |
3663 | return ret; | |
3664 | } | |
3665 | ||
00e2e675 DG |
3666 | /* |
3667 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
3668 | */ | |
3669 | static int cmd_set_consumer_uri(int domain, struct ltt_session *session, | |
3670 | struct lttng_uri *uri) | |
3671 | { | |
3672 | int ret; | |
3673 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3674 | struct ltt_ust_session *usess = session->ust_session; | |
3675 | struct consumer_output *consumer; | |
3676 | ||
3677 | /* Can't enable consumer after session started. */ | |
3678 | if (session->enabled) { | |
3679 | ret = LTTCOMM_TRACE_ALREADY_STARTED; | |
3680 | goto error; | |
3681 | } | |
3682 | ||
3683 | switch (domain) { | |
3684 | case LTTNG_DOMAIN_KERNEL: | |
3685 | /* Code flow error if we don't have a kernel session here. */ | |
3686 | assert(ksess); | |
3687 | ||
3688 | /* Create consumer output if none exists */ | |
3689 | consumer = ksess->tmp_consumer; | |
3690 | if (consumer == NULL) { | |
3691 | consumer = consumer_copy_output(ksess->consumer); | |
3692 | if (consumer == NULL) { | |
3693 | ret = LTTCOMM_FATAL; | |
3694 | goto error; | |
3695 | } | |
3696 | /* Reassign new pointer */ | |
3697 | ksess->tmp_consumer = consumer; | |
3698 | } | |
3699 | ||
3700 | switch (uri->dtype) { | |
3701 | case LTTNG_DST_IPV4: | |
3702 | case LTTNG_DST_IPV6: | |
3703 | DBG2("Setting network URI for kernel session %s", session->name); | |
3704 | ||
3705 | /* Set URI into consumer output object */ | |
3706 | ret = consumer_set_network_uri(consumer, uri); | |
3707 | if (ret < 0) { | |
3708 | ret = LTTCOMM_FATAL; | |
3709 | goto error; | |
3710 | } | |
3711 | ||
3712 | /* On a new subdir, reappend the default trace dir. */ | |
3713 | if (strlen(uri->subdir) != 0) { | |
3714 | strncat(consumer->subdir, DEFAULT_KERNEL_TRACE_DIR, | |
3715 | sizeof(consumer->subdir)); | |
3716 | } | |
3717 | ||
3718 | ret = send_socket_relayd_consumer(domain, session, uri, consumer, | |
3719 | ksess->consumer_fd); | |
3720 | if (ret != LTTCOMM_OK) { | |
3721 | goto error; | |
3722 | } | |
3723 | break; | |
3724 | case LTTNG_DST_PATH: | |
3725 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
3726 | memset(consumer->dst.trace_path, 0, | |
3727 | sizeof(consumer->dst.trace_path)); | |
3728 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
3729 | sizeof(consumer->dst.trace_path)); | |
3730 | /* Append default kernel trace dir */ | |
3731 | strncat(consumer->dst.trace_path, DEFAULT_KERNEL_TRACE_DIR, | |
3732 | sizeof(consumer->dst.trace_path)); | |
3733 | break; | |
3734 | } | |
3735 | ||
3736 | /* All good! */ | |
3737 | break; | |
3738 | case LTTNG_DOMAIN_UST: | |
3739 | /* Code flow error if we don't have a kernel session here. */ | |
3740 | assert(usess); | |
3741 | ||
3742 | /* Create consumer output if none exists */ | |
3743 | consumer = usess->tmp_consumer; | |
3744 | if (consumer == NULL) { | |
3745 | consumer = consumer_copy_output(usess->consumer); | |
3746 | if (consumer == NULL) { | |
3747 | ret = LTTCOMM_FATAL; | |
3748 | goto error; | |
3749 | } | |
3750 | /* Reassign new pointer */ | |
3751 | usess->tmp_consumer = consumer; | |
3752 | } | |
3753 | ||
3754 | switch (uri->dtype) { | |
3755 | case LTTNG_DST_IPV4: | |
3756 | case LTTNG_DST_IPV6: | |
3757 | { | |
3758 | DBG2("Setting network URI for UST session %s", session->name); | |
3759 | ||
3760 | /* Set URI into consumer object */ | |
3761 | ret = consumer_set_network_uri(consumer, uri); | |
3762 | if (ret < 0) { | |
3763 | ret = LTTCOMM_FATAL; | |
3764 | goto error; | |
3765 | } | |
3766 | ||
3767 | /* On a new subdir, reappend the default trace dir. */ | |
3768 | if (strlen(uri->subdir) != 0) { | |
3769 | strncat(consumer->subdir, DEFAULT_UST_TRACE_DIR, | |
3770 | sizeof(consumer->subdir)); | |
3771 | } | |
3772 | ||
3773 | if (ust_consumerd64_fd >= 0) { | |
3774 | ret = send_socket_relayd_consumer(domain, session, uri, | |
3775 | consumer, ust_consumerd64_fd); | |
3776 | if (ret != LTTCOMM_OK) { | |
3777 | goto error; | |
3778 | } | |
3779 | } | |
3780 | ||
3781 | if (ust_consumerd32_fd >= 0) { | |
3782 | ret = send_socket_relayd_consumer(domain, session, uri, | |
3783 | consumer, ust_consumerd32_fd); | |
3784 | if (ret != LTTCOMM_OK) { | |
3785 | goto error; | |
3786 | } | |
3787 | } | |
3788 | ||
3789 | break; | |
3790 | } | |
3791 | case LTTNG_DST_PATH: | |
3792 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
3793 | memset(consumer->dst.trace_path, 0, | |
3794 | sizeof(consumer->dst.trace_path)); | |
3795 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
3796 | sizeof(consumer->dst.trace_path)); | |
3797 | /* Append default UST trace dir */ | |
3798 | strncat(consumer->dst.trace_path, DEFAULT_UST_TRACE_DIR, | |
3799 | sizeof(consumer->dst.trace_path)); | |
3800 | break; | |
3801 | } | |
3802 | break; | |
3803 | } | |
3804 | ||
3805 | /* All good! */ | |
3806 | ret = LTTCOMM_OK; | |
3807 | ||
3808 | error: | |
3809 | return ret; | |
3810 | } | |
3811 | ||
3812 | /* | |
3813 | * Command LTTNG_DISABLE_CONSUMER processed by the client thread. | |
3814 | */ | |
3815 | static int cmd_disable_consumer(int domain, struct ltt_session *session) | |
3816 | { | |
3817 | int ret; | |
3818 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3819 | struct ltt_ust_session *usess = session->ust_session; | |
3820 | struct consumer_output *consumer; | |
3821 | ||
3822 | if (session->enabled) { | |
3823 | /* Can't disable consumer on an already started session */ | |
3824 | ret = LTTCOMM_TRACE_ALREADY_STARTED; | |
3825 | goto error; | |
3826 | } | |
3827 | ||
3828 | switch (domain) { | |
3829 | case LTTNG_DOMAIN_KERNEL: | |
3830 | /* Code flow error if we don't have a kernel session here. */ | |
3831 | assert(ksess); | |
3832 | ||
3833 | DBG("Disabling kernel consumer"); | |
3834 | consumer = ksess->consumer; | |
3835 | ||
3836 | break; | |
3837 | case LTTNG_DOMAIN_UST: | |
3838 | /* Code flow error if we don't have a UST session here. */ | |
3839 | assert(usess); | |
3840 | ||
3841 | DBG("Disabling UST consumer"); | |
3842 | consumer = usess->consumer; | |
3843 | ||
3844 | break; | |
3845 | default: | |
3846 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
3847 | goto error; | |
3848 | } | |
3849 | ||
3850 | assert(consumer); | |
3851 | consumer->enabled = 0; | |
3852 | ||
3853 | /* Success at this point */ | |
3854 | ret = LTTCOMM_OK; | |
3855 | ||
3856 | error: | |
3857 | return ret; | |
3858 | } | |
3859 | ||
3860 | /* | |
3861 | * Command LTTNG_ENABLE_CONSUMER processed by the client thread. | |
3862 | */ | |
3863 | static int cmd_enable_consumer(int domain, struct ltt_session *session) | |
3864 | { | |
3865 | int ret; | |
3866 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3867 | struct ltt_ust_session *usess = session->ust_session; | |
3868 | struct consumer_output *tmp_out; | |
3869 | ||
3870 | /* Can't enable consumer after session started. */ | |
3871 | if (session->enabled) { | |
3872 | ret = LTTCOMM_TRACE_ALREADY_STARTED; | |
3873 | goto error; | |
3874 | } | |
3875 | ||
3876 | switch (domain) { | |
3877 | case LTTNG_DOMAIN_KERNEL: | |
3878 | /* Code flow error if we don't have a kernel session here. */ | |
3879 | assert(ksess); | |
3880 | ||
3881 | /* | |
3882 | * Check if we have already sent fds to the consumer. In that case, | |
3883 | * the enable-consumer command can't be used because a start trace | |
3884 | * had previously occured. | |
3885 | */ | |
3886 | if (ksess->consumer_fds_sent) { | |
3887 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
3888 | goto error; | |
3889 | } | |
3890 | ||
3891 | tmp_out = ksess->tmp_consumer; | |
3892 | if (tmp_out == NULL) { | |
3893 | /* No temp. consumer output exists. Using the current one. */ | |
3894 | DBG3("No temporary consumer. Using default"); | |
3895 | ret = LTTCOMM_OK; | |
3896 | goto error; | |
3897 | } | |
3898 | ||
3899 | switch (tmp_out->type) { | |
3900 | case CONSUMER_DST_LOCAL: | |
3901 | DBG2("Consumer output is local. Creating directory(ies)"); | |
3902 | ||
3903 | /* Create directory(ies) */ | |
3904 | ret = run_as_mkdir_recursive(tmp_out->dst.trace_path, | |
3905 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
3906 | if (ret < 0) { | |
3907 | if (ret != -EEXIST) { | |
3908 | ERR("Trace directory creation error"); | |
3909 | ret = LTTCOMM_FATAL; | |
3910 | goto error; | |
3911 | } | |
3912 | } | |
3913 | break; | |
3914 | case CONSUMER_DST_NET: | |
3915 | DBG2("Consumer output is network. Validating URIs"); | |
3916 | /* Validate if we have both control and data path set. */ | |
3917 | if (!tmp_out->dst.net.control_isset) { | |
3918 | ret = LTTCOMM_URI_CTRL_MISS; | |
3919 | goto error; | |
3920 | } | |
3921 | ||
3922 | if (!tmp_out->dst.net.data_isset) { | |
3923 | ret = LTTCOMM_URI_DATA_MISS; | |
3924 | goto error; | |
3925 | } | |
3926 | ||
3927 | /* Check established network session state */ | |
3928 | if (session->net_handle == 0) { | |
3929 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
3930 | ERR("Session network handle is not set on enable-consumer"); | |
3931 | goto error; | |
3932 | } | |
3933 | ||
3934 | /* Append default kernel trace dir to subdir */ | |
3935 | strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR, | |
3936 | sizeof(ksess->consumer->subdir)); | |
3937 | ||
3938 | break; | |
3939 | } | |
3940 | ||
3941 | /* | |
3942 | * @session-lock | |
3943 | * This is race free for now since the session lock is acquired before | |
3944 | * ending up in this function. No other threads can access this kernel | |
3945 | * session without this lock hence freeing the consumer output object | |
3946 | * is valid. | |
3947 | */ | |
3948 | consumer_destroy_output(ksess->consumer); | |
3949 | ksess->consumer = tmp_out; | |
3950 | ksess->tmp_consumer = NULL; | |
3951 | ||
3952 | break; | |
3953 | case LTTNG_DOMAIN_UST: | |
3954 | /* Code flow error if we don't have a UST session here. */ | |
3955 | assert(usess); | |
3956 | ||
3957 | /* | |
3958 | * Check if we have already sent fds to the consumer. In that case, | |
3959 | * the enable-consumer command can't be used because a start trace | |
3960 | * had previously occured. | |
3961 | */ | |
3962 | if (usess->start_trace) { | |
3963 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
3964 | goto error; | |
3965 | } | |
3966 | ||
3967 | tmp_out = usess->tmp_consumer; | |
3968 | if (tmp_out == NULL) { | |
3969 | /* No temp. consumer output exists. Using the current one. */ | |
3970 | DBG3("No temporary consumer. Using default"); | |
3971 | ret = LTTCOMM_OK; | |
3972 | goto error; | |
3973 | } | |
3974 | ||
3975 | switch (tmp_out->type) { | |
3976 | case CONSUMER_DST_LOCAL: | |
3977 | DBG2("Consumer output is local. Creating directory(ies)"); | |
3978 | ||
3979 | /* Create directory(ies) */ | |
3980 | ret = run_as_mkdir_recursive(tmp_out->dst.trace_path, | |
3981 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
3982 | if (ret < 0) { | |
3983 | if (ret != -EEXIST) { | |
3984 | ERR("Trace directory creation error"); | |
3985 | ret = LTTCOMM_FATAL; | |
3986 | goto error; | |
3987 | } | |
3988 | } | |
3989 | break; | |
3990 | case CONSUMER_DST_NET: | |
3991 | DBG2("Consumer output is network. Validating URIs"); | |
3992 | /* Validate if we have both control and data path set. */ | |
3993 | if (!tmp_out->dst.net.control_isset) { | |
3994 | ret = LTTCOMM_URI_CTRL_MISS; | |
3995 | goto error; | |
3996 | } | |
3997 | ||
3998 | if (!tmp_out->dst.net.data_isset) { | |
3999 | ret = LTTCOMM_URI_DATA_MISS; | |
4000 | goto error; | |
4001 | } | |
4002 | ||
4003 | /* Check established network session state */ | |
4004 | if (session->net_handle == 0) { | |
4005 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
4006 | DBG2("Session network handle is not set on enable-consumer"); | |
4007 | goto error; | |
4008 | } | |
4009 | ||
4010 | if (tmp_out->net_seq_index == -1) { | |
4011 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
4012 | DBG2("Network index is not set on the consumer"); | |
4013 | goto error; | |
4014 | } | |
4015 | ||
4016 | /* Append default kernel trace dir to subdir */ | |
4017 | strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR, | |
4018 | sizeof(usess->consumer->subdir)); | |
4019 | ||
4020 | break; | |
4021 | } | |
4022 | ||
4023 | /* | |
4024 | * @session-lock | |
4025 | * This is race free for now since the session lock is acquired before | |
4026 | * ending up in this function. No other threads can access this kernel | |
4027 | * session without this lock hence freeing the consumer output object | |
4028 | * is valid. | |
4029 | */ | |
4030 | consumer_destroy_output(usess->consumer); | |
4031 | usess->consumer = tmp_out; | |
4032 | usess->tmp_consumer = NULL; | |
4033 | ||
4034 | break; | |
4035 | } | |
4036 | ||
4037 | /* Success at this point */ | |
4038 | ret = LTTCOMM_OK; | |
4039 | ||
4040 | error: | |
4041 | return ret; | |
4042 | } | |
4043 | ||
54d01ffb DG |
4044 | /* |
4045 | * Process the command requested by the lttng client within the command | |
4046 | * context structure. This function make sure that the return structure (llm) | |
4047 | * is set and ready for transmission before returning. | |
4048 | * | |
4049 | * Return any error encountered or 0 for success. | |
53a80697 MD |
4050 | * |
4051 | * "sock" is only used for special-case var. len data. | |
54d01ffb | 4052 | */ |
53a80697 MD |
4053 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
4054 | int *sock_error) | |
54d01ffb DG |
4055 | { |
4056 | int ret = LTTCOMM_OK; | |
44d3bd01 | 4057 | int need_tracing_session = 1; |
2e09ba09 | 4058 | int need_domain; |
54d01ffb DG |
4059 | |
4060 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
4061 | ||
53a80697 MD |
4062 | *sock_error = 0; |
4063 | ||
2e09ba09 MD |
4064 | switch (cmd_ctx->lsm->cmd_type) { |
4065 | case LTTNG_CREATE_SESSION: | |
00e2e675 | 4066 | case LTTNG_CREATE_SESSION_URI: |
2e09ba09 MD |
4067 | case LTTNG_DESTROY_SESSION: |
4068 | case LTTNG_LIST_SESSIONS: | |
4069 | case LTTNG_LIST_DOMAINS: | |
4070 | case LTTNG_START_TRACE: | |
4071 | case LTTNG_STOP_TRACE: | |
4072 | need_domain = 0; | |
3aace903 | 4073 | break; |
2e09ba09 MD |
4074 | default: |
4075 | need_domain = 1; | |
4076 | } | |
4077 | ||
4078 | if (opt_no_kernel && need_domain | |
4079 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 MD |
4080 | if (!is_root) { |
4081 | ret = LTTCOMM_NEED_ROOT_SESSIOND; | |
4082 | } else { | |
4083 | ret = LTTCOMM_KERN_NA; | |
4084 | } | |
4fba7219 DG |
4085 | goto error; |
4086 | } | |
4087 | ||
54d01ffb DG |
4088 | /* |
4089 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 4090 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
4091 | * command. |
4092 | */ | |
4093 | switch(cmd_ctx->lsm->cmd_type) { | |
4094 | case LTTNG_LIST_SESSIONS: | |
4095 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 4096 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
4097 | case LTTNG_LIST_DOMAINS: |
4098 | case LTTNG_LIST_CHANNELS: | |
4099 | case LTTNG_LIST_EVENTS: | |
4100 | break; | |
4101 | default: | |
4102 | /* Setup lttng message with no payload */ | |
4103 | ret = setup_lttng_msg(cmd_ctx, 0); | |
4104 | if (ret < 0) { | |
4105 | /* This label does not try to unlock the session */ | |
4106 | goto init_setup_error; | |
4107 | } | |
4108 | } | |
4109 | ||
4110 | /* Commands that DO NOT need a session. */ | |
4111 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 4112 | case LTTNG_CREATE_SESSION: |
00e2e675 | 4113 | case LTTNG_CREATE_SESSION_URI: |
2e09ba09 | 4114 | case LTTNG_CALIBRATE: |
54d01ffb DG |
4115 | case LTTNG_LIST_SESSIONS: |
4116 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 4117 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
44d3bd01 | 4118 | need_tracing_session = 0; |
54d01ffb DG |
4119 | break; |
4120 | default: | |
4121 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
4122 | /* |
4123 | * We keep the session list lock across _all_ commands | |
4124 | * for now, because the per-session lock does not | |
4125 | * handle teardown properly. | |
4126 | */ | |
74babd95 | 4127 | session_lock_list(); |
54d01ffb DG |
4128 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
4129 | if (cmd_ctx->session == NULL) { | |
4130 | if (cmd_ctx->lsm->session.name != NULL) { | |
4131 | ret = LTTCOMM_SESS_NOT_FOUND; | |
4132 | } else { | |
4133 | /* If no session name specified */ | |
4134 | ret = LTTCOMM_SELECT_SESS; | |
4135 | } | |
4136 | goto error; | |
4137 | } else { | |
4138 | /* Acquire lock for the session */ | |
4139 | session_lock(cmd_ctx->session); | |
4140 | } | |
4141 | break; | |
4142 | } | |
b389abbe | 4143 | |
2e09ba09 MD |
4144 | if (!need_domain) { |
4145 | goto skip_domain; | |
4146 | } | |
54d01ffb DG |
4147 | /* |
4148 | * Check domain type for specific "pre-action". | |
4149 | */ | |
4150 | switch (cmd_ctx->lsm->domain.type) { | |
4151 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 4152 | if (!is_root) { |
531d29f9 | 4153 | ret = LTTCOMM_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
4154 | goto error; |
4155 | } | |
4156 | ||
54d01ffb | 4157 | /* Kernel tracer check */ |
a4b35e07 | 4158 | if (kernel_tracer_fd == -1) { |
54d01ffb | 4159 | /* Basically, load kernel tracer modules */ |
096102bd DG |
4160 | ret = init_kernel_tracer(); |
4161 | if (ret != 0) { | |
54d01ffb DG |
4162 | goto error; |
4163 | } | |
4164 | } | |
5eb91c98 | 4165 | |
5c827ce0 DG |
4166 | /* Consumer is in an ERROR state. Report back to client */ |
4167 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
4168 | ret = LTTCOMM_NO_KERNCONSUMERD; | |
4169 | goto error; | |
4170 | } | |
4171 | ||
54d01ffb | 4172 | /* Need a session for kernel command */ |
44d3bd01 | 4173 | if (need_tracing_session) { |
54d01ffb | 4174 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 4175 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 4176 | if (ret < 0) { |
54d01ffb | 4177 | ret = LTTCOMM_KERN_SESS_FAIL; |
5eb91c98 DG |
4178 | goto error; |
4179 | } | |
b389abbe | 4180 | } |
7d29a247 | 4181 | |
54d01ffb | 4182 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
4183 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
4184 | if (kconsumer_data.pid == 0 && | |
54d01ffb | 4185 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
4186 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
4187 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 4188 | if (ret < 0) { |
54d01ffb DG |
4189 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
4190 | goto error; | |
950131af | 4191 | } |
5c827ce0 | 4192 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
00e2e675 DG |
4193 | |
4194 | /* Set consumer fd of the session */ | |
4195 | cmd_ctx->session->kernel_session->consumer_fd = | |
4196 | kconsumer_data.cmd_sock; | |
3ff2ecac MD |
4197 | } else { |
4198 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 4199 | } |
0d0c377a | 4200 | } |
5c827ce0 | 4201 | |
54d01ffb | 4202 | break; |
2bdd86d4 | 4203 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 4204 | { |
5c827ce0 DG |
4205 | /* Consumer is in an ERROR state. Report back to client */ |
4206 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
4207 | ret = LTTCOMM_NO_USTCONSUMERD; | |
4208 | goto error; | |
4209 | } | |
4210 | ||
44d3bd01 | 4211 | if (need_tracing_session) { |
f6a9efaa | 4212 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 4213 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 4214 | &cmd_ctx->lsm->domain); |
44d3bd01 DG |
4215 | if (ret != LTTCOMM_OK) { |
4216 | goto error; | |
4217 | } | |
4218 | } | |
00e2e675 | 4219 | |
7753dea8 MD |
4220 | /* Start the UST consumer daemons */ |
4221 | /* 64-bit */ | |
4222 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 4223 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 4224 | ustconsumer64_data.pid == 0 && |
2bdd86d4 | 4225 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
4226 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
4227 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 4228 | if (ret < 0) { |
7753dea8 MD |
4229 | ret = LTTCOMM_UST_CONSUMER64_FAIL; |
4230 | ust_consumerd64_fd = -EINVAL; | |
2bdd86d4 MD |
4231 | goto error; |
4232 | } | |
48842b30 | 4233 | |
7753dea8 | 4234 | ust_consumerd64_fd = ustconsumer64_data.cmd_sock; |
5c827ce0 | 4235 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 4236 | } else { |
7753dea8 MD |
4237 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
4238 | } | |
4239 | /* 32-bit */ | |
fc7a59ce | 4240 | if (consumerd32_bin[0] != '\0' && |
7753dea8 MD |
4241 | ustconsumer32_data.pid == 0 && |
4242 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { | |
4243 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
4244 | ret = start_consumerd(&ustconsumer32_data); | |
4245 | if (ret < 0) { | |
4246 | ret = LTTCOMM_UST_CONSUMER32_FAIL; | |
4247 | ust_consumerd32_fd = -EINVAL; | |
4248 | goto error; | |
4249 | } | |
5c827ce0 | 4250 | |
7753dea8 | 4251 | ust_consumerd32_fd = ustconsumer32_data.cmd_sock; |
5c827ce0 | 4252 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
4253 | } else { |
4254 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 4255 | } |
44d3bd01 DG |
4256 | } |
4257 | break; | |
48842b30 | 4258 | } |
54d01ffb | 4259 | default: |
54d01ffb DG |
4260 | break; |
4261 | } | |
2e09ba09 | 4262 | skip_domain: |
33a2b854 | 4263 | |
5c827ce0 DG |
4264 | /* Validate consumer daemon state when start/stop trace command */ |
4265 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
4266 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
4267 | switch (cmd_ctx->lsm->domain.type) { | |
4268 | case LTTNG_DOMAIN_UST: | |
4269 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
4270 | ret = LTTCOMM_NO_USTCONSUMERD; | |
4271 | goto error; | |
4272 | } | |
4273 | break; | |
4274 | case LTTNG_DOMAIN_KERNEL: | |
4275 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
4276 | ret = LTTCOMM_NO_KERNCONSUMERD; | |
4277 | goto error; | |
4278 | } | |
4279 | break; | |
4280 | } | |
4281 | } | |
4282 | ||
8e0af1b4 MD |
4283 | /* |
4284 | * Check that the UID or GID match that of the tracing session. | |
4285 | * The root user can interact with all sessions. | |
4286 | */ | |
4287 | if (need_tracing_session) { | |
4288 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
4289 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
4290 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
8e0af1b4 MD |
4291 | ret = LTTCOMM_EPERM; |
4292 | goto error; | |
4293 | } | |
4294 | } | |
4295 | ||
54d01ffb DG |
4296 | /* Process by command type */ |
4297 | switch (cmd_ctx->lsm->cmd_type) { | |
4298 | case LTTNG_ADD_CONTEXT: | |
4299 | { | |
4300 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4301 | cmd_ctx->lsm->u.context.channel_name, | |
4302 | cmd_ctx->lsm->u.context.event_name, | |
4303 | &cmd_ctx->lsm->u.context.ctx); | |
4304 | break; | |
4305 | } | |
4306 | case LTTNG_DISABLE_CHANNEL: | |
4307 | { | |
4308 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4309 | cmd_ctx->lsm->u.disable.channel_name); | |
4310 | break; | |
4311 | } | |
4312 | case LTTNG_DISABLE_EVENT: | |
4313 | { | |
4314 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4315 | cmd_ctx->lsm->u.disable.channel_name, | |
4316 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
4317 | break; |
4318 | } | |
54d01ffb DG |
4319 | case LTTNG_DISABLE_ALL_EVENT: |
4320 | { | |
2bdd86d4 | 4321 | DBG("Disabling all events"); |
54d01ffb DG |
4322 | |
4323 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4324 | cmd_ctx->lsm->u.disable.channel_name); | |
4325 | break; | |
4326 | } | |
00e2e675 DG |
4327 | case LTTNG_DISABLE_CONSUMER: |
4328 | { | |
4329 | ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session); | |
4330 | break; | |
4331 | } | |
54d01ffb DG |
4332 | case LTTNG_ENABLE_CHANNEL: |
4333 | { | |
aea829b3 | 4334 | ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
54d01ffb DG |
4335 | &cmd_ctx->lsm->u.channel.chan); |
4336 | break; | |
4337 | } | |
00e2e675 DG |
4338 | case LTTNG_ENABLE_CONSUMER: |
4339 | { | |
4340 | ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session); | |
4341 | break; | |
4342 | } | |
54d01ffb DG |
4343 | case LTTNG_ENABLE_EVENT: |
4344 | { | |
4345 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4346 | cmd_ctx->lsm->u.enable.channel_name, | |
4347 | &cmd_ctx->lsm->u.enable.event); | |
4348 | break; | |
4349 | } | |
4350 | case LTTNG_ENABLE_ALL_EVENT: | |
4351 | { | |
2bdd86d4 | 4352 | DBG("Enabling all events"); |
54d01ffb DG |
4353 | |
4354 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
8c9ae521 DG |
4355 | cmd_ctx->lsm->u.enable.channel_name, |
4356 | cmd_ctx->lsm->u.enable.event.type); | |
54d01ffb DG |
4357 | break; |
4358 | } | |
052da939 | 4359 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 4360 | { |
9f19cc17 | 4361 | struct lttng_event *events; |
54d01ffb | 4362 | ssize_t nb_events; |
052da939 | 4363 | |
54d01ffb DG |
4364 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
4365 | if (nb_events < 0) { | |
4366 | ret = -nb_events; | |
4367 | goto error; | |
2ef84c95 DG |
4368 | } |
4369 | ||
4370 | /* | |
4371 | * Setup lttng message with payload size set to the event list size in | |
4372 | * bytes and then copy list into the llm payload. | |
4373 | */ | |
052da939 | 4374 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 4375 | if (ret < 0) { |
052da939 | 4376 | free(events); |
2ef84c95 DG |
4377 | goto setup_error; |
4378 | } | |
4379 | ||
4380 | /* Copy event list into message payload */ | |
9f19cc17 | 4381 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 4382 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 4383 | |
9f19cc17 | 4384 | free(events); |
2ef84c95 DG |
4385 | |
4386 | ret = LTTCOMM_OK; | |
4387 | break; | |
4388 | } | |
f37d259d MD |
4389 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
4390 | { | |
4391 | struct lttng_event_field *fields; | |
4392 | ssize_t nb_fields; | |
4393 | ||
4394 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, &fields); | |
4395 | if (nb_fields < 0) { | |
4396 | ret = -nb_fields; | |
4397 | goto error; | |
4398 | } | |
4399 | ||
4400 | /* | |
4401 | * Setup lttng message with payload size set to the event list size in | |
4402 | * bytes and then copy list into the llm payload. | |
4403 | */ | |
4404 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event_field) * nb_fields); | |
4405 | if (ret < 0) { | |
4406 | free(fields); | |
4407 | goto setup_error; | |
4408 | } | |
4409 | ||
4410 | /* Copy event list into message payload */ | |
4411 | memcpy(cmd_ctx->llm->payload, fields, | |
4412 | sizeof(struct lttng_event_field) * nb_fields); | |
4413 | ||
4414 | free(fields); | |
4415 | ||
4416 | ret = LTTCOMM_OK; | |
4417 | break; | |
4418 | } | |
00e2e675 DG |
4419 | case LTTNG_SET_CONSUMER_URI: |
4420 | { | |
4421 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, | |
4422 | &cmd_ctx->lsm->u.uri); | |
4423 | break; | |
4424 | } | |
f3ed775e | 4425 | case LTTNG_START_TRACE: |
8c0faa1d | 4426 | { |
54d01ffb | 4427 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
4428 | break; |
4429 | } | |
f3ed775e | 4430 | case LTTNG_STOP_TRACE: |
8c0faa1d | 4431 | { |
54d01ffb | 4432 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
4433 | break; |
4434 | } | |
5e16da05 MD |
4435 | case LTTNG_CREATE_SESSION: |
4436 | { | |
54d01ffb | 4437 | ret = cmd_create_session(cmd_ctx->lsm->session.name, |
6df2e2c9 | 4438 | cmd_ctx->lsm->session.path, &cmd_ctx->creds); |
5e16da05 MD |
4439 | break; |
4440 | } | |
00e2e675 DG |
4441 | case LTTNG_CREATE_SESSION_URI: |
4442 | { | |
4443 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, | |
4444 | &cmd_ctx->lsm->u.create_uri.ctrl_uri, | |
4445 | &cmd_ctx->lsm->u.create_uri.data_uri, | |
4446 | cmd_ctx->lsm->u.create_uri.enable_consumer, &cmd_ctx->creds); | |
4447 | break; | |
4448 | } | |
5e16da05 MD |
4449 | case LTTNG_DESTROY_SESSION: |
4450 | { | |
54d01ffb DG |
4451 | ret = cmd_destroy_session(cmd_ctx->session, |
4452 | cmd_ctx->lsm->session.name); | |
256a5576 MD |
4453 | /* |
4454 | * Set session to NULL so we do not unlock it after | |
4455 | * free. | |
4456 | */ | |
4457 | cmd_ctx->session = NULL; | |
5461b305 | 4458 | break; |
5e16da05 | 4459 | } |
9f19cc17 | 4460 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 4461 | { |
54d01ffb DG |
4462 | ssize_t nb_dom; |
4463 | struct lttng_domain *domains; | |
5461b305 | 4464 | |
54d01ffb DG |
4465 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
4466 | if (nb_dom < 0) { | |
4467 | ret = -nb_dom; | |
4468 | goto error; | |
ce3d728c | 4469 | } |
520ff687 | 4470 | |
54d01ffb | 4471 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 DG |
4472 | if (ret < 0) { |
4473 | goto setup_error; | |
520ff687 | 4474 | } |
57167058 | 4475 | |
54d01ffb DG |
4476 | /* Copy event list into message payload */ |
4477 | memcpy(cmd_ctx->llm->payload, domains, | |
4478 | nb_dom * sizeof(struct lttng_domain)); | |
4479 | ||
4480 | free(domains); | |
5e16da05 | 4481 | |
5461b305 | 4482 | ret = LTTCOMM_OK; |
5e16da05 MD |
4483 | break; |
4484 | } | |
9f19cc17 | 4485 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 4486 | { |
6775595e | 4487 | int nb_chan; |
54d01ffb DG |
4488 | struct lttng_channel *channels; |
4489 | ||
b551a063 DG |
4490 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
4491 | cmd_ctx->session, &channels); | |
54d01ffb DG |
4492 | if (nb_chan < 0) { |
4493 | ret = -nb_chan; | |
4494 | goto error; | |
5461b305 | 4495 | } |
ca95a216 | 4496 | |
54d01ffb | 4497 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 DG |
4498 | if (ret < 0) { |
4499 | goto setup_error; | |
4500 | } | |
9f19cc17 | 4501 | |
54d01ffb DG |
4502 | /* Copy event list into message payload */ |
4503 | memcpy(cmd_ctx->llm->payload, channels, | |
4504 | nb_chan * sizeof(struct lttng_channel)); | |
4505 | ||
4506 | free(channels); | |
9f19cc17 DG |
4507 | |
4508 | ret = LTTCOMM_OK; | |
5461b305 | 4509 | break; |
5e16da05 | 4510 | } |
9f19cc17 | 4511 | case LTTNG_LIST_EVENTS: |
5e16da05 | 4512 | { |
b551a063 | 4513 | ssize_t nb_event; |
684d34d2 | 4514 | struct lttng_event *events = NULL; |
9f19cc17 | 4515 | |
b551a063 | 4516 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
54d01ffb DG |
4517 | cmd_ctx->lsm->u.list.channel_name, &events); |
4518 | if (nb_event < 0) { | |
4519 | ret = -nb_event; | |
4520 | goto error; | |
5461b305 | 4521 | } |
ca95a216 | 4522 | |
54d01ffb | 4523 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 DG |
4524 | if (ret < 0) { |
4525 | goto setup_error; | |
4526 | } | |
9f19cc17 | 4527 | |
54d01ffb DG |
4528 | /* Copy event list into message payload */ |
4529 | memcpy(cmd_ctx->llm->payload, events, | |
4530 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 4531 | |
54d01ffb | 4532 | free(events); |
9f19cc17 DG |
4533 | |
4534 | ret = LTTCOMM_OK; | |
5461b305 | 4535 | break; |
5e16da05 MD |
4536 | } |
4537 | case LTTNG_LIST_SESSIONS: | |
4538 | { | |
8e0af1b4 | 4539 | unsigned int nr_sessions; |
5461b305 | 4540 | |
8e0af1b4 | 4541 | session_lock_list(); |
730389d9 DG |
4542 | nr_sessions = lttng_sessions_count( |
4543 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
4544 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
d32fb093 | 4545 | |
8e0af1b4 | 4546 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
5461b305 | 4547 | if (ret < 0) { |
54d01ffb | 4548 | session_unlock_list(); |
5461b305 | 4549 | goto setup_error; |
e065084a | 4550 | } |
5e16da05 | 4551 | |
6c9cc2ab | 4552 | /* Filled the session array */ |
8e0af1b4 | 4553 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
730389d9 DG |
4554 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
4555 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6c9cc2ab | 4556 | |
54d01ffb | 4557 | session_unlock_list(); |
5e16da05 | 4558 | |
5461b305 | 4559 | ret = LTTCOMM_OK; |
5e16da05 MD |
4560 | break; |
4561 | } | |
d0254c7c MD |
4562 | case LTTNG_CALIBRATE: |
4563 | { | |
54d01ffb DG |
4564 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
4565 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
4566 | break; |
4567 | } | |
d9800920 DG |
4568 | case LTTNG_REGISTER_CONSUMER: |
4569 | { | |
54d01ffb DG |
4570 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
4571 | cmd_ctx->lsm->u.reg.path); | |
d9800920 DG |
4572 | break; |
4573 | } | |
53a80697 MD |
4574 | case LTTNG_SET_FILTER: |
4575 | { | |
4576 | struct lttng_filter_bytecode *bytecode; | |
4577 | ||
4578 | if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) { | |
4579 | ret = LTTCOMM_FILTER_INVAL; | |
4580 | goto error; | |
4581 | } | |
4582 | bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len); | |
4583 | if (!bytecode) { | |
4584 | ret = LTTCOMM_FILTER_NOMEM; | |
4585 | goto error; | |
4586 | } | |
4587 | /* Receive var. len. data */ | |
4588 | DBG("Receiving var len data from client ..."); | |
4589 | ret = lttcomm_recv_unix_sock(sock, bytecode, | |
4590 | cmd_ctx->lsm->u.filter.bytecode_len); | |
4591 | if (ret <= 0) { | |
4592 | DBG("Nothing recv() from client var len data... continuing"); | |
4593 | *sock_error = 1; | |
4594 | ret = LTTCOMM_FILTER_INVAL; | |
4595 | goto error; | |
4596 | } | |
4597 | ||
4598 | if (bytecode->len + sizeof(*bytecode) | |
4599 | != cmd_ctx->lsm->u.filter.bytecode_len) { | |
4600 | free(bytecode); | |
4601 | ret = LTTCOMM_FILTER_INVAL; | |
4602 | goto error; | |
4603 | } | |
4604 | ||
4605 | ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
4606 | cmd_ctx->lsm->u.filter.channel_name, | |
4607 | cmd_ctx->lsm->u.filter.event_name, | |
4608 | bytecode); | |
4609 | break; | |
4610 | } | |
5e16da05 | 4611 | default: |
5e16da05 | 4612 | ret = LTTCOMM_UND; |
5461b305 | 4613 | break; |
fac6795d DG |
4614 | } |
4615 | ||
5461b305 | 4616 | error: |
5461b305 DG |
4617 | if (cmd_ctx->llm == NULL) { |
4618 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 4619 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
4620 | goto setup_error; |
4621 | } | |
4622 | } | |
54d01ffb | 4623 | /* Set return code */ |
5461b305 | 4624 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 4625 | setup_error: |
b5541356 | 4626 | if (cmd_ctx->session) { |
54d01ffb | 4627 | session_unlock(cmd_ctx->session); |
b5541356 | 4628 | } |
256a5576 MD |
4629 | if (need_tracing_session) { |
4630 | session_unlock_list(); | |
4631 | } | |
54d01ffb | 4632 | init_setup_error: |
8028d920 | 4633 | return ret; |
fac6795d DG |
4634 | } |
4635 | ||
44a5e5eb DG |
4636 | /* |
4637 | * Thread managing health check socket. | |
4638 | */ | |
4639 | static void *thread_manage_health(void *data) | |
4640 | { | |
4641 | int sock = -1, new_sock, ret, i, pollfd; | |
4642 | uint32_t revents, nb_fd; | |
4643 | struct lttng_poll_event events; | |
4644 | struct lttcomm_health_msg msg; | |
4645 | struct lttcomm_health_data reply; | |
4646 | ||
4647 | DBG("[thread] Manage health check started"); | |
4648 | ||
4649 | rcu_register_thread(); | |
4650 | ||
4651 | /* Create unix socket */ | |
4652 | sock = lttcomm_create_unix_sock(health_unix_sock_path); | |
4653 | if (sock < 0) { | |
4654 | ERR("Unable to create health check Unix socket"); | |
4655 | ret = -1; | |
4656 | goto error; | |
4657 | } | |
4658 | ||
4659 | ret = lttcomm_listen_unix_sock(sock); | |
4660 | if (ret < 0) { | |
4661 | goto error; | |
4662 | } | |
4663 | ||
4664 | /* | |
4665 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4666 | * more will be added to this poll set. | |
4667 | */ | |
4668 | ret = create_thread_poll_set(&events, 2); | |
4669 | if (ret < 0) { | |
4670 | goto error; | |
4671 | } | |
4672 | ||
4673 | /* Add the application registration socket */ | |
4674 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
4675 | if (ret < 0) { | |
4676 | goto error; | |
4677 | } | |
4678 | ||
4679 | while (1) { | |
4680 | DBG("Health check ready"); | |
4681 | ||
4682 | nb_fd = LTTNG_POLL_GETNB(&events); | |
4683 | ||
4684 | /* Inifinite blocking call, waiting for transmission */ | |
4685 | restart: | |
4686 | ret = lttng_poll_wait(&events, -1); | |
4687 | if (ret < 0) { | |
4688 | /* | |
4689 | * Restart interrupted system call. | |
4690 | */ | |
4691 | if (errno == EINTR) { | |
4692 | goto restart; | |
4693 | } | |
4694 | goto error; | |
4695 | } | |
4696 | ||
4697 | for (i = 0; i < nb_fd; i++) { | |
4698 | /* Fetch once the poll data */ | |
4699 | revents = LTTNG_POLL_GETEV(&events, i); | |
4700 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4701 | ||
4702 | /* Thread quit pipe has been closed. Killing thread. */ | |
4703 | ret = check_thread_quit_pipe(pollfd, revents); | |
4704 | if (ret) { | |
4705 | goto error; | |
4706 | } | |
4707 | ||
4708 | /* Event on the registration socket */ | |
4709 | if (pollfd == sock) { | |
4710 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
4711 | ERR("Health socket poll error"); | |
4712 | goto error; | |
4713 | } | |
4714 | } | |
4715 | } | |
4716 | ||
4717 | new_sock = lttcomm_accept_unix_sock(sock); | |
4718 | if (new_sock < 0) { | |
4719 | goto error; | |
4720 | } | |
4721 | ||
4722 | DBG("Receiving data from client for health..."); | |
4723 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
4724 | if (ret <= 0) { | |
4725 | DBG("Nothing recv() from client... continuing"); | |
4726 | ret = close(new_sock); | |
4727 | if (ret) { | |
4728 | PERROR("close"); | |
4729 | } | |
4730 | new_sock = -1; | |
4731 | continue; | |
4732 | } | |
4733 | ||
4734 | rcu_thread_online(); | |
4735 | ||
4736 | switch (msg.component) { | |
4737 | case LTTNG_HEALTH_CMD: | |
4738 | reply.ret_code = health_check_state(&health_thread_cmd); | |
4739 | break; | |
4740 | case LTTNG_HEALTH_APP_REG: | |
4741 | reply.ret_code = health_check_state(&health_thread_app_reg); | |
4742 | break; | |
4743 | case LTTNG_HEALTH_KERNEL: | |
4744 | reply.ret_code = health_check_state(&health_thread_kernel); | |
4745 | break; | |
4746 | case LTTNG_HEALTH_CONSUMER: | |
4747 | reply.ret_code = check_consumer_health(); | |
4748 | break; | |
4749 | case LTTNG_HEALTH_ALL: | |
4750 | ret = check_consumer_health(); | |
4751 | ||
4752 | reply.ret_code = | |
4753 | health_check_state(&health_thread_app_reg) & | |
4754 | health_check_state(&health_thread_cmd) & | |
4755 | health_check_state(&health_thread_kernel) & | |
4756 | ret; | |
4757 | break; | |
4758 | default: | |
4759 | reply.ret_code = LTTCOMM_UND; | |
4760 | break; | |
4761 | } | |
4762 | ||
4763 | /* | |
4764 | * Flip ret value since 0 is a success and 1 indicates a bad health for | |
4765 | * the client where in the sessiond it is the opposite. Again, this is | |
4766 | * just to make things easier for us poor developer which enjoy a lot | |
4767 | * lazyness. | |
4768 | */ | |
4769 | if (reply.ret_code == 0 || reply.ret_code == 1) { | |
4770 | reply.ret_code = !reply.ret_code; | |
4771 | } | |
4772 | ||
4773 | DBG2("Health check return value %d", reply.ret_code); | |
4774 | ||
4775 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
4776 | if (ret < 0) { | |
4777 | ERR("Failed to send health data back to client"); | |
4778 | } | |
4779 | ||
4780 | /* End of transmission */ | |
4781 | ret = close(new_sock); | |
4782 | if (ret) { | |
4783 | PERROR("close"); | |
4784 | } | |
4785 | new_sock = -1; | |
4786 | } | |
4787 | ||
4788 | error: | |
4789 | DBG("Health check thread dying"); | |
4790 | unlink(health_unix_sock_path); | |
4791 | if (sock >= 0) { | |
4792 | ret = close(sock); | |
4793 | if (ret) { | |
4794 | PERROR("close"); | |
4795 | } | |
4796 | } | |
4797 | if (new_sock >= 0) { | |
4798 | ret = close(new_sock); | |
4799 | if (ret) { | |
4800 | PERROR("close"); | |
4801 | } | |
4802 | } | |
4803 | ||
4804 | lttng_poll_clean(&events); | |
4805 | ||
4806 | rcu_unregister_thread(); | |
4807 | return NULL; | |
4808 | } | |
4809 | ||
1d4b027a | 4810 | /* |
d063d709 DG |
4811 | * This thread manage all clients request using the unix client socket for |
4812 | * communication. | |
1d4b027a DG |
4813 | */ |
4814 | static void *thread_manage_clients(void *data) | |
4815 | { | |
a4b35e07 | 4816 | int sock = -1, ret, i, pollfd; |
53a80697 | 4817 | int sock_error; |
5eb91c98 | 4818 | uint32_t revents, nb_fd; |
273ea72c | 4819 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 4820 | struct lttng_poll_event events; |
1d4b027a DG |
4821 | |
4822 | DBG("[thread] Manage client started"); | |
4823 | ||
f6a9efaa DG |
4824 | rcu_register_thread(); |
4825 | ||
44a5e5eb DG |
4826 | health_code_update(&health_thread_cmd); |
4827 | ||
1d4b027a DG |
4828 | ret = lttcomm_listen_unix_sock(client_sock); |
4829 | if (ret < 0) { | |
4830 | goto error; | |
4831 | } | |
4832 | ||
5eb91c98 DG |
4833 | /* |
4834 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4835 | * more will be added to this poll set. | |
4836 | */ | |
4837 | ret = create_thread_poll_set(&events, 2); | |
4838 | if (ret < 0) { | |
4839 | goto error; | |
4840 | } | |
273ea72c | 4841 | |
5eb91c98 DG |
4842 | /* Add the application registration socket */ |
4843 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
4844 | if (ret < 0) { | |
4845 | goto error; | |
4846 | } | |
273ea72c | 4847 | |
bbd973c2 DG |
4848 | /* |
4849 | * Notify parent pid that we are ready to accept command for client side. | |
1d4b027a DG |
4850 | */ |
4851 | if (opt_sig_parent) { | |
8db8d1dc | 4852 | kill(ppid, SIGUSR1); |
1d4b027a DG |
4853 | } |
4854 | ||
44a5e5eb DG |
4855 | health_code_update(&health_thread_cmd); |
4856 | ||
1d4b027a | 4857 | while (1) { |
1d4b027a | 4858 | DBG("Accepting client command ..."); |
273ea72c | 4859 | |
5eb91c98 DG |
4860 | nb_fd = LTTNG_POLL_GETNB(&events); |
4861 | ||
273ea72c | 4862 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 4863 | restart: |
44a5e5eb | 4864 | health_poll_update(&health_thread_cmd); |
5eb91c98 | 4865 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 4866 | health_poll_update(&health_thread_cmd); |
273ea72c | 4867 | if (ret < 0) { |
88f2b785 MD |
4868 | /* |
4869 | * Restart interrupted system call. | |
4870 | */ | |
4871 | if (errno == EINTR) { | |
4872 | goto restart; | |
4873 | } | |
273ea72c DG |
4874 | goto error; |
4875 | } | |
4876 | ||
5eb91c98 DG |
4877 | for (i = 0; i < nb_fd; i++) { |
4878 | /* Fetch once the poll data */ | |
4879 | revents = LTTNG_POLL_GETEV(&events, i); | |
4880 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4881 | ||
44a5e5eb DG |
4882 | health_code_update(&health_thread_cmd); |
4883 | ||
5eb91c98 DG |
4884 | /* Thread quit pipe has been closed. Killing thread. */ |
4885 | ret = check_thread_quit_pipe(pollfd, revents); | |
4886 | if (ret) { | |
4887 | goto error; | |
4888 | } | |
4889 | ||
4890 | /* Event on the registration socket */ | |
4891 | if (pollfd == client_sock) { | |
4892 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
4893 | ERR("Client socket poll error"); | |
4894 | goto error; | |
4895 | } | |
4896 | } | |
4897 | } | |
4898 | ||
4899 | DBG("Wait for client response"); | |
4900 | ||
44a5e5eb DG |
4901 | health_code_update(&health_thread_cmd); |
4902 | ||
5eb91c98 DG |
4903 | sock = lttcomm_accept_unix_sock(client_sock); |
4904 | if (sock < 0) { | |
273ea72c | 4905 | goto error; |
273ea72c DG |
4906 | } |
4907 | ||
be040666 DG |
4908 | /* Set socket option for credentials retrieval */ |
4909 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
4910 | if (ret < 0) { | |
4911 | goto error; | |
4912 | } | |
4913 | ||
5eb91c98 | 4914 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 4915 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 4916 | if (cmd_ctx == NULL) { |
76d7553f | 4917 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 4918 | goto error; |
5eb91c98 | 4919 | } |
1d4b027a | 4920 | |
5eb91c98 | 4921 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 4922 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 4923 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 4924 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
4925 | goto error; |
4926 | } | |
1d4b027a | 4927 | |
5eb91c98 DG |
4928 | cmd_ctx->llm = NULL; |
4929 | cmd_ctx->session = NULL; | |
1d4b027a | 4930 | |
44a5e5eb DG |
4931 | health_code_update(&health_thread_cmd); |
4932 | ||
5eb91c98 DG |
4933 | /* |
4934 | * Data is received from the lttng client. The struct | |
4935 | * lttcomm_session_msg (lsm) contains the command and data request of | |
4936 | * the client. | |
4937 | */ | |
4938 | DBG("Receiving data from client ..."); | |
be040666 DG |
4939 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
4940 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
4941 | if (ret <= 0) { |
4942 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
4943 | ret = close(sock); |
4944 | if (ret) { | |
4945 | PERROR("close"); | |
4946 | } | |
4947 | sock = -1; | |
a2c0da86 | 4948 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4949 | continue; |
4950 | } | |
1d4b027a | 4951 | |
44a5e5eb DG |
4952 | health_code_update(&health_thread_cmd); |
4953 | ||
5eb91c98 DG |
4954 | // TODO: Validate cmd_ctx including sanity check for |
4955 | // security purpose. | |
f7776ea7 | 4956 | |
f6a9efaa | 4957 | rcu_thread_online(); |
5eb91c98 DG |
4958 | /* |
4959 | * This function dispatch the work to the kernel or userspace tracer | |
4960 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
4961 | * informations for the client. The command context struct contains | |
4962 | * everything this function may needs. | |
4963 | */ | |
53a80697 | 4964 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 4965 | rcu_thread_offline(); |
5eb91c98 | 4966 | if (ret < 0) { |
53a80697 MD |
4967 | if (sock_error) { |
4968 | ret = close(sock); | |
4969 | if (ret) { | |
4970 | PERROR("close"); | |
4971 | } | |
4972 | sock = -1; | |
4973 | } | |
bbd973c2 | 4974 | /* |
5eb91c98 | 4975 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 4976 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
4977 | * (ENOMEM). Error detected but still accept |
4978 | * command, unless a socket error has been | |
4979 | * detected. | |
bbd973c2 | 4980 | */ |
bbd973c2 | 4981 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4982 | continue; |
4983 | } | |
d6e4fca4 | 4984 | |
44a5e5eb DG |
4985 | health_code_update(&health_thread_cmd); |
4986 | ||
54d01ffb DG |
4987 | DBG("Sending response (size: %d, retcode: %s)", |
4988 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 4989 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 4990 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
4991 | if (ret < 0) { |
4992 | ERR("Failed to send data back to client"); | |
bbd973c2 | 4993 | } |
1d4b027a | 4994 | |
5eb91c98 | 4995 | /* End of transmission */ |
76d7553f MD |
4996 | ret = close(sock); |
4997 | if (ret) { | |
4998 | PERROR("close"); | |
4999 | } | |
5000 | sock = -1; | |
be040666 DG |
5001 | |
5002 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb DG |
5003 | |
5004 | health_code_update(&health_thread_cmd); | |
273ea72c DG |
5005 | } |
5006 | ||
5eb91c98 | 5007 | error: |
44a5e5eb DG |
5008 | health_reset(&health_thread_cmd); |
5009 | ||
5eb91c98 | 5010 | DBG("Client thread dying"); |
273ea72c | 5011 | unlink(client_unix_sock_path); |
76d7553f MD |
5012 | if (client_sock >= 0) { |
5013 | ret = close(client_sock); | |
5014 | if (ret) { | |
5015 | PERROR("close"); | |
5016 | } | |
a4b35e07 MD |
5017 | } |
5018 | if (sock >= 0) { | |
76d7553f MD |
5019 | ret = close(sock); |
5020 | if (ret) { | |
5021 | PERROR("close"); | |
5022 | } | |
a4b35e07 | 5023 | } |
273ea72c | 5024 | |
5eb91c98 | 5025 | lttng_poll_clean(&events); |
a2fb29a5 | 5026 | clean_command_ctx(&cmd_ctx); |
f6a9efaa DG |
5027 | |
5028 | rcu_unregister_thread(); | |
1d4b027a DG |
5029 | return NULL; |
5030 | } | |
5031 | ||
5032 | ||
fac6795d DG |
5033 | /* |
5034 | * usage function on stderr | |
5035 | */ | |
5036 | static void usage(void) | |
5037 | { | |
b716ce68 | 5038 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
5039 | fprintf(stderr, " -h, --help Display this usage.\n"); |
5040 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
5041 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
5042 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
5043 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
7753dea8 MD |
5044 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
5045 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); | |
5046 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); | |
5047 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); | |
ebaeda94 MD |
5048 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
5049 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); | |
5050 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); | |
5051 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); | |
d6f42150 DG |
5052 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
5053 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
5054 | fprintf(stderr, " -V, --version Show version number.\n"); | |
5055 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
5056 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
5057 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
3bd1e081 | 5058 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
4fba7219 | 5059 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
fac6795d DG |
5060 | } |
5061 | ||
5062 | /* | |
5063 | * daemon argument parsing | |
5064 | */ | |
5065 | static int parse_args(int argc, char **argv) | |
5066 | { | |
5067 | int c; | |
5068 | ||
5069 | static struct option long_options[] = { | |
5070 | { "client-sock", 1, 0, 'c' }, | |
5071 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
5072 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
5073 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
7753dea8 MD |
5074 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
5075 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
ebaeda94 MD |
5076 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
5077 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
5078 | { "consumerd32-path", 1, 0, 'u' }, | |
5079 | { "consumerd32-libdir", 1, 0, 'U' }, | |
5080 | { "consumerd64-path", 1, 0, 't' }, | |
5081 | { "consumerd64-libdir", 1, 0, 'T' }, | |
fac6795d | 5082 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 5083 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
5084 | { "help", 0, 0, 'h' }, |
5085 | { "group", 1, 0, 'g' }, | |
5086 | { "version", 0, 0, 'V' }, | |
75462a81 | 5087 | { "quiet", 0, 0, 'q' }, |
3f9947db | 5088 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 5089 | { "verbose-consumer", 0, 0, 'Z' }, |
4fba7219 | 5090 | { "no-kernel", 0, 0, 'N' }, |
fac6795d DG |
5091 | { NULL, 0, 0, 0 } |
5092 | }; | |
5093 | ||
5094 | while (1) { | |
5095 | int option_index = 0; | |
4fba7219 | 5096 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t", |
54d01ffb | 5097 | long_options, &option_index); |
fac6795d DG |
5098 | if (c == -1) { |
5099 | break; | |
5100 | } | |
5101 | ||
5102 | switch (c) { | |
5103 | case 0: | |
5104 | fprintf(stderr, "option %s", long_options[option_index].name); | |
5105 | if (optarg) { | |
5106 | fprintf(stderr, " with arg %s\n", optarg); | |
5107 | } | |
5108 | break; | |
b716ce68 | 5109 | case 'c': |
fac6795d DG |
5110 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
5111 | break; | |
5112 | case 'a': | |
5113 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
5114 | break; | |
5115 | case 'd': | |
5116 | opt_daemon = 1; | |
5117 | break; | |
5118 | case 'g': | |
fb09408a | 5119 | opt_tracing_group = optarg; |
fac6795d DG |
5120 | break; |
5121 | case 'h': | |
5122 | usage(); | |
5123 | exit(EXIT_FAILURE); | |
5124 | case 'V': | |
5125 | fprintf(stdout, "%s\n", VERSION); | |
5126 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
5127 | case 'S': |
5128 | opt_sig_parent = 1; | |
5129 | break; | |
d6f42150 | 5130 | case 'E': |
3bd1e081 | 5131 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
5132 | break; |
5133 | case 'C': | |
3bd1e081 MD |
5134 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
5135 | break; | |
5136 | case 'F': | |
7753dea8 | 5137 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
3bd1e081 MD |
5138 | break; |
5139 | case 'D': | |
7753dea8 MD |
5140 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
5141 | break; | |
5142 | case 'H': | |
5143 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
5144 | break; | |
5145 | case 'G': | |
5146 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 5147 | break; |
4fba7219 DG |
5148 | case 'N': |
5149 | opt_no_kernel = 1; | |
5150 | break; | |
75462a81 | 5151 | case 'q': |
97e19046 | 5152 | lttng_opt_quiet = 1; |
75462a81 | 5153 | break; |
3f9947db | 5154 | case 'v': |
53086306 | 5155 | /* Verbose level can increase using multiple -v */ |
97e19046 | 5156 | lttng_opt_verbose += 1; |
3f9947db | 5157 | break; |
31f73cc9 | 5158 | case 'Z': |
3bd1e081 | 5159 | opt_verbose_consumer += 1; |
31f73cc9 | 5160 | break; |
fb09408a | 5161 | case 'u': |
fc7a59ce | 5162 | consumerd32_bin= optarg; |
ebaeda94 MD |
5163 | break; |
5164 | case 'U': | |
5165 | consumerd32_libdir = optarg; | |
7753dea8 MD |
5166 | break; |
5167 | case 't': | |
fc7a59ce | 5168 | consumerd64_bin = optarg; |
ebaeda94 MD |
5169 | break; |
5170 | case 'T': | |
5171 | consumerd64_libdir = optarg; | |
fb09408a | 5172 | break; |
fac6795d DG |
5173 | default: |
5174 | /* Unknown option or other error. | |
5175 | * Error is printed by getopt, just return */ | |
5176 | return -1; | |
5177 | } | |
5178 | } | |
5179 | ||
5180 | return 0; | |
5181 | } | |
5182 | ||
5183 | /* | |
d063d709 | 5184 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
5185 | * apps_sock - The communication socket for all UST apps. |
5186 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 5187 | */ |
cf3af59e | 5188 | static int init_daemon_socket(void) |
fac6795d DG |
5189 | { |
5190 | int ret = 0; | |
5191 | mode_t old_umask; | |
5192 | ||
5193 | old_umask = umask(0); | |
5194 | ||
5195 | /* Create client tool unix socket */ | |
d6f42150 DG |
5196 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
5197 | if (client_sock < 0) { | |
5198 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
5199 | ret = -1; |
5200 | goto end; | |
5201 | } | |
5202 | ||
5203 | /* File permission MUST be 660 */ | |
5204 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
5205 | if (ret < 0) { | |
d6f42150 | 5206 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
76d7553f | 5207 | PERROR("chmod"); |
fac6795d DG |
5208 | goto end; |
5209 | } | |
5210 | ||
5211 | /* Create the application unix socket */ | |
d6f42150 DG |
5212 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
5213 | if (apps_sock < 0) { | |
5214 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
5215 | ret = -1; |
5216 | goto end; | |
5217 | } | |
5218 | ||
d6f42150 | 5219 | /* File permission MUST be 666 */ |
54d01ffb DG |
5220 | ret = chmod(apps_unix_sock_path, |
5221 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 5222 | if (ret < 0) { |
d6f42150 | 5223 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
76d7553f | 5224 | PERROR("chmod"); |
fac6795d DG |
5225 | goto end; |
5226 | } | |
5227 | ||
5228 | end: | |
5229 | umask(old_umask); | |
5230 | return ret; | |
5231 | } | |
5232 | ||
5233 | /* | |
54d01ffb DG |
5234 | * Check if the global socket is available, and if a daemon is answering at the |
5235 | * other side. If yes, error is returned. | |
fac6795d | 5236 | */ |
cf3af59e | 5237 | static int check_existing_daemon(void) |
fac6795d | 5238 | { |
7d8234d9 | 5239 | /* Is there anybody out there ? */ |
099e26bd | 5240 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 5241 | return -EEXIST; |
099e26bd | 5242 | } |
b09c7c76 DG |
5243 | |
5244 | return 0; | |
fac6795d DG |
5245 | } |
5246 | ||
fac6795d | 5247 | /* |
d063d709 | 5248 | * Set the tracing group gid onto the client socket. |
5e16da05 | 5249 | * |
d063d709 | 5250 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 5251 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 5252 | */ |
be040666 | 5253 | static int set_permissions(char *rundir) |
fac6795d DG |
5254 | { |
5255 | int ret; | |
996b65c8 | 5256 | gid_t gid; |
fac6795d | 5257 | |
6775595e DG |
5258 | ret = allowed_group(); |
5259 | if (ret < 0) { | |
be040666 DG |
5260 | WARN("No tracing group detected"); |
5261 | ret = 0; | |
fac6795d DG |
5262 | goto end; |
5263 | } | |
5264 | ||
6775595e DG |
5265 | gid = ret; |
5266 | ||
d6f42150 | 5267 | /* Set lttng run dir */ |
be040666 | 5268 | ret = chown(rundir, 0, gid); |
d6f42150 | 5269 | if (ret < 0) { |
be040666 | 5270 | ERR("Unable to set group on %s", rundir); |
76d7553f | 5271 | PERROR("chown"); |
d6f42150 DG |
5272 | } |
5273 | ||
d1613cf5 | 5274 | /* Ensure tracing group can search the run dir */ |
61076f74 | 5275 | ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH); |
d1613cf5 JN |
5276 | if (ret < 0) { |
5277 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 5278 | PERROR("chmod"); |
d1613cf5 JN |
5279 | } |
5280 | ||
d6f42150 | 5281 | /* lttng client socket path */ |
996b65c8 | 5282 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 5283 | if (ret < 0) { |
d6f42150 | 5284 | ERR("Unable to set group on %s", client_unix_sock_path); |
76d7553f | 5285 | PERROR("chown"); |
d6f42150 DG |
5286 | } |
5287 | ||
3bd1e081 MD |
5288 | /* kconsumer error socket path */ |
5289 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); | |
d6f42150 | 5290 | if (ret < 0) { |
3bd1e081 | 5291 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 5292 | PERROR("chown"); |
3bd1e081 MD |
5293 | } |
5294 | ||
7753dea8 MD |
5295 | /* 64-bit ustconsumer error socket path */ |
5296 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid); | |
5297 | if (ret < 0) { | |
5298 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 5299 | PERROR("chown"); |
7753dea8 MD |
5300 | } |
5301 | ||
5302 | /* 32-bit ustconsumer compat32 error socket path */ | |
5303 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid); | |
3bd1e081 | 5304 | if (ret < 0) { |
7753dea8 | 5305 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 5306 | PERROR("chown"); |
fac6795d DG |
5307 | } |
5308 | ||
d6f42150 | 5309 | DBG("All permissions are set"); |
e07ae692 | 5310 | |
fac6795d DG |
5311 | end: |
5312 | return ret; | |
5313 | } | |
5314 | ||
d6f42150 | 5315 | /* |
d063d709 | 5316 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 5317 | */ |
67e40797 | 5318 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
5319 | { |
5320 | int ret; | |
5321 | ||
67e40797 DG |
5322 | DBG3("Creating LTTng run directory: %s", rundir); |
5323 | ||
d1613cf5 | 5324 | ret = mkdir(rundir, S_IRWXU); |
d6f42150 | 5325 | if (ret < 0) { |
b1f11e69 | 5326 | if (errno != EEXIST) { |
67e40797 | 5327 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
5328 | goto error; |
5329 | } else { | |
5330 | ret = 0; | |
5331 | } | |
d6f42150 DG |
5332 | } |
5333 | ||
5334 | error: | |
5335 | return ret; | |
5336 | } | |
5337 | ||
5338 | /* | |
d063d709 DG |
5339 | * Setup sockets and directory needed by the kconsumerd communication with the |
5340 | * session daemon. | |
d6f42150 | 5341 | */ |
67e40797 DG |
5342 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
5343 | const char *rundir) | |
d6f42150 DG |
5344 | { |
5345 | int ret; | |
67e40797 | 5346 | char path[PATH_MAX]; |
d6f42150 | 5347 | |
67e40797 | 5348 | switch (consumer_data->type) { |
7753dea8 | 5349 | case LTTNG_CONSUMER_KERNEL: |
60922cb0 | 5350 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
7753dea8 MD |
5351 | break; |
5352 | case LTTNG_CONSUMER64_UST: | |
60922cb0 | 5353 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
5354 | break; |
5355 | case LTTNG_CONSUMER32_UST: | |
60922cb0 | 5356 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
5357 | break; |
5358 | default: | |
5359 | ERR("Consumer type unknown"); | |
5360 | ret = -EINVAL; | |
5361 | goto error; | |
d6f42150 DG |
5362 | } |
5363 | ||
67e40797 DG |
5364 | DBG2("Creating consumer directory: %s", path); |
5365 | ||
d1613cf5 | 5366 | ret = mkdir(path, S_IRWXU); |
d6f42150 | 5367 | if (ret < 0) { |
6beb2242 | 5368 | if (errno != EEXIST) { |
f11e84c2 | 5369 | PERROR("mkdir"); |
3bd1e081 | 5370 | ERR("Failed to create %s", path); |
6beb2242 DG |
5371 | goto error; |
5372 | } | |
f11e84c2 | 5373 | ret = -1; |
d6f42150 DG |
5374 | } |
5375 | ||
5376 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
5377 | consumer_data->err_sock = |
5378 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
5379 | if (consumer_data->err_sock < 0) { | |
5380 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
5381 | ret = -1; |
5382 | goto error; | |
5383 | } | |
5384 | ||
5385 | /* File permission MUST be 660 */ | |
3bd1e081 | 5386 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 5387 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 5388 | if (ret < 0) { |
3bd1e081 | 5389 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 5390 | PERROR("chmod"); |
d6f42150 DG |
5391 | goto error; |
5392 | } | |
5393 | ||
5394 | error: | |
5395 | return ret; | |
5396 | } | |
5397 | ||
fac6795d | 5398 | /* |
d063d709 | 5399 | * Signal handler for the daemon |
cf3af59e | 5400 | * |
54d01ffb DG |
5401 | * Simply stop all worker threads, leaving main() return gracefully after |
5402 | * joining all threads and calling cleanup(). | |
fac6795d DG |
5403 | */ |
5404 | static void sighandler(int sig) | |
5405 | { | |
5406 | switch (sig) { | |
cf3af59e | 5407 | case SIGPIPE: |
af87c45a | 5408 | DBG("SIGPIPE caught"); |
cf3af59e MD |
5409 | return; |
5410 | case SIGINT: | |
af87c45a | 5411 | DBG("SIGINT caught"); |
cf3af59e MD |
5412 | stop_threads(); |
5413 | break; | |
5414 | case SIGTERM: | |
af87c45a | 5415 | DBG("SIGTERM caught"); |
cf3af59e MD |
5416 | stop_threads(); |
5417 | break; | |
5418 | default: | |
5419 | break; | |
fac6795d | 5420 | } |
fac6795d DG |
5421 | } |
5422 | ||
5423 | /* | |
d063d709 | 5424 | * Setup signal handler for : |
1d4b027a | 5425 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 5426 | */ |
1d4b027a | 5427 | static int set_signal_handler(void) |
fac6795d | 5428 | { |
1d4b027a DG |
5429 | int ret = 0; |
5430 | struct sigaction sa; | |
5431 | sigset_t sigset; | |
fac6795d | 5432 | |
1d4b027a | 5433 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 5434 | PERROR("sigemptyset"); |
1d4b027a DG |
5435 | return ret; |
5436 | } | |
d6f42150 | 5437 | |
1d4b027a DG |
5438 | sa.sa_handler = sighandler; |
5439 | sa.sa_mask = sigset; | |
5440 | sa.sa_flags = 0; | |
5441 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
76d7553f | 5442 | PERROR("sigaction"); |
1d4b027a | 5443 | return ret; |
d6f42150 DG |
5444 | } |
5445 | ||
1d4b027a | 5446 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 5447 | PERROR("sigaction"); |
1d4b027a | 5448 | return ret; |
d6f42150 | 5449 | } |
aaf26714 | 5450 | |
1d4b027a | 5451 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
76d7553f | 5452 | PERROR("sigaction"); |
1d4b027a | 5453 | return ret; |
8c0faa1d DG |
5454 | } |
5455 | ||
1d4b027a DG |
5456 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
5457 | ||
5458 | return ret; | |
fac6795d DG |
5459 | } |
5460 | ||
f3ed775e | 5461 | /* |
d063d709 DG |
5462 | * Set open files limit to unlimited. This daemon can open a large number of |
5463 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
5464 | */ |
5465 | static void set_ulimit(void) | |
5466 | { | |
5467 | int ret; | |
5468 | struct rlimit lim; | |
5469 | ||
a88df331 | 5470 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
5471 | lim.rlim_cur = 65535; |
5472 | lim.rlim_max = 65535; | |
5473 | ||
5474 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
5475 | if (ret < 0) { | |
76d7553f | 5476 | PERROR("failed to set open files limit"); |
f3ed775e DG |
5477 | } |
5478 | } | |
5479 | ||
fac6795d DG |
5480 | /* |
5481 | * main | |
5482 | */ | |
5483 | int main(int argc, char **argv) | |
5484 | { | |
fac6795d DG |
5485 | int ret = 0; |
5486 | void *status; | |
b082db07 | 5487 | const char *home_path; |
fac6795d | 5488 | |
335a95b7 MD |
5489 | init_kernel_workarounds(); |
5490 | ||
f6a9efaa DG |
5491 | rcu_register_thread(); |
5492 | ||
7753dea8 | 5493 | setup_consumerd_path(); |
fb09408a | 5494 | |
fac6795d DG |
5495 | /* Parse arguments */ |
5496 | progname = argv[0]; | |
5497 | if ((ret = parse_args(argc, argv) < 0)) { | |
cf3af59e | 5498 | goto error; |
fac6795d DG |
5499 | } |
5500 | ||
5501 | /* Daemonize */ | |
5502 | if (opt_daemon) { | |
ceed52b5 MD |
5503 | int i; |
5504 | ||
5505 | /* | |
5506 | * fork | |
5507 | * child: setsid, close FD 0, 1, 2, chdir / | |
5508 | * parent: exit (if fork is successful) | |
5509 | */ | |
53094c05 DG |
5510 | ret = daemon(0, 0); |
5511 | if (ret < 0) { | |
76d7553f | 5512 | PERROR("daemon"); |
cf3af59e | 5513 | goto error; |
53094c05 | 5514 | } |
ceed52b5 MD |
5515 | /* |
5516 | * We are in the child. Make sure all other file | |
5517 | * descriptors are closed, in case we are called with | |
5518 | * more opened file descriptors than the standard ones. | |
5519 | */ | |
5520 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
5521 | (void) close(i); | |
5522 | } | |
5523 | } | |
5524 | ||
5525 | /* Create thread quit pipe */ | |
5526 | if ((ret = init_thread_quit_pipe()) < 0) { | |
5527 | goto error; | |
fac6795d DG |
5528 | } |
5529 | ||
5530 | /* Check if daemon is UID = 0 */ | |
5531 | is_root = !getuid(); | |
5532 | ||
fac6795d | 5533 | if (is_root) { |
990570ed | 5534 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
67e40797 DG |
5535 | |
5536 | /* Create global run dir with root access */ | |
5537 | ret = create_lttng_rundir(rundir); | |
d6f42150 | 5538 | if (ret < 0) { |
cf3af59e | 5539 | goto error; |
d6f42150 DG |
5540 | } |
5541 | ||
fac6795d | 5542 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
5543 | snprintf(apps_unix_sock_path, PATH_MAX, |
5544 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
5545 | } |
5546 | ||
5547 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
5548 | snprintf(client_unix_sock_path, PATH_MAX, |
5549 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
5550 | } | |
0fdd1e2c DG |
5551 | |
5552 | /* Set global SHM for ust */ | |
5553 | if (strlen(wait_shm_path) == 0) { | |
5554 | snprintf(wait_shm_path, PATH_MAX, | |
5555 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
5556 | } | |
67e40797 | 5557 | |
44a5e5eb DG |
5558 | if (strlen(health_unix_sock_path) == 0) { |
5559 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
5560 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); | |
5561 | } | |
5562 | ||
67e40797 DG |
5563 | /* Setup kernel consumerd path */ |
5564 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 5565 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
67e40797 | 5566 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 5567 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
67e40797 DG |
5568 | |
5569 | DBG2("Kernel consumer err path: %s", | |
5570 | kconsumer_data.err_unix_sock_path); | |
5571 | DBG2("Kernel consumer cmd path: %s", | |
5572 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 5573 | } else { |
b082db07 DG |
5574 | home_path = get_home_dir(); |
5575 | if (home_path == NULL) { | |
273ea72c DG |
5576 | /* TODO: Add --socket PATH option */ |
5577 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
5578 | ret = -EPERM; |
5579 | goto error; | |
b082db07 DG |
5580 | } |
5581 | ||
67e40797 DG |
5582 | /* |
5583 | * Create rundir from home path. This will create something like | |
5584 | * $HOME/.lttng | |
5585 | */ | |
990570ed | 5586 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
67e40797 DG |
5587 | if (ret < 0) { |
5588 | ret = -ENOMEM; | |
5589 | goto error; | |
5590 | } | |
5591 | ||
5592 | ret = create_lttng_rundir(rundir); | |
5593 | if (ret < 0) { | |
5594 | goto error; | |
5595 | } | |
5596 | ||
fac6795d | 5597 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 5598 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 5599 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
5600 | } |
5601 | ||
5602 | /* Set the cli tool unix socket path */ | |
5603 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 5604 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 5605 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 5606 | } |
0fdd1e2c DG |
5607 | |
5608 | /* Set global SHM for ust */ | |
5609 | if (strlen(wait_shm_path) == 0) { | |
5610 | snprintf(wait_shm_path, PATH_MAX, | |
5611 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid()); | |
5612 | } | |
44a5e5eb DG |
5613 | |
5614 | /* Set health check Unix path */ | |
5615 | if (strlen(health_unix_sock_path) == 0) { | |
5616 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
5617 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); | |
5618 | } | |
fac6795d DG |
5619 | } |
5620 | ||
5c827ce0 DG |
5621 | /* Set consumer initial state */ |
5622 | kernel_consumerd_state = CONSUMER_STOPPED; | |
5623 | ust_consumerd_state = CONSUMER_STOPPED; | |
5624 | ||
847177cd DG |
5625 | DBG("Client socket path %s", client_unix_sock_path); |
5626 | DBG("Application socket path %s", apps_unix_sock_path); | |
67e40797 DG |
5627 | DBG("LTTng run directory path: %s", rundir); |
5628 | ||
5629 | /* 32 bits consumerd path setup */ | |
5630 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 5631 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
67e40797 | 5632 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 5633 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
67e40797 DG |
5634 | |
5635 | DBG2("UST consumer 32 bits err path: %s", | |
5636 | ustconsumer32_data.err_unix_sock_path); | |
5637 | DBG2("UST consumer 32 bits cmd path: %s", | |
5638 | ustconsumer32_data.cmd_unix_sock_path); | |
5639 | ||
5640 | /* 64 bits consumerd path setup */ | |
5641 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 5642 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
67e40797 | 5643 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 5644 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
67e40797 DG |
5645 | |
5646 | DBG2("UST consumer 64 bits err path: %s", | |
5647 | ustconsumer64_data.err_unix_sock_path); | |
5648 | DBG2("UST consumer 64 bits cmd path: %s", | |
5649 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 5650 | |
273ea72c | 5651 | /* |
7d8234d9 | 5652 | * See if daemon already exist. |
fac6795d | 5653 | */ |
7d8234d9 | 5654 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 5655 | ERR("Already running daemon.\n"); |
273ea72c | 5656 | /* |
cf3af59e MD |
5657 | * We do not goto exit because we must not cleanup() |
5658 | * because a daemon is already running. | |
ab118b20 | 5659 | */ |
cf3af59e | 5660 | goto error; |
a88df331 DG |
5661 | } |
5662 | ||
1427f9b2 DG |
5663 | /* |
5664 | * Init UST app hash table. Alloc hash table before this point since | |
5665 | * cleanup() can get called after that point. | |
5666 | */ | |
5667 | ust_app_ht_alloc(); | |
5668 | ||
54d01ffb | 5669 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
5670 | |
5671 | /* | |
5672 | * These actions must be executed as root. We do that *after* setting up | |
5673 | * the sockets path because we MUST make the check for another daemon using | |
5674 | * those paths *before* trying to set the kernel consumer sockets and init | |
5675 | * kernel tracer. | |
5676 | */ | |
5677 | if (is_root) { | |
67e40797 | 5678 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
7753dea8 MD |
5679 | if (ret < 0) { |
5680 | goto exit; | |
5681 | } | |
5682 | ||
a88df331 | 5683 | /* Setup kernel tracer */ |
4fba7219 DG |
5684 | if (!opt_no_kernel) { |
5685 | init_kernel_tracer(); | |
5686 | } | |
a88df331 DG |
5687 | |
5688 | /* Set ulimit for open files */ | |
5689 | set_ulimit(); | |
fac6795d | 5690 | } |
4063050c MD |
5691 | /* init lttng_fd tracking must be done after set_ulimit. */ |
5692 | lttng_fd_init(); | |
fac6795d | 5693 | |
67e40797 DG |
5694 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
5695 | if (ret < 0) { | |
5696 | goto exit; | |
5697 | } | |
5698 | ||
5699 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); | |
5700 | if (ret < 0) { | |
5701 | goto exit; | |
5702 | } | |
5703 | ||
cf3af59e MD |
5704 | if ((ret = set_signal_handler()) < 0) { |
5705 | goto exit; | |
fac6795d DG |
5706 | } |
5707 | ||
d6f42150 | 5708 | /* Setup the needed unix socket */ |
cf3af59e MD |
5709 | if ((ret = init_daemon_socket()) < 0) { |
5710 | goto exit; | |
fac6795d DG |
5711 | } |
5712 | ||
5713 | /* Set credentials to socket */ | |
be040666 | 5714 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
cf3af59e | 5715 | goto exit; |
fac6795d DG |
5716 | } |
5717 | ||
5b8719f5 DG |
5718 | /* Get parent pid if -S, --sig-parent is specified. */ |
5719 | if (opt_sig_parent) { | |
5720 | ppid = getppid(); | |
5721 | } | |
5722 | ||
7a485870 | 5723 | /* Setup the kernel pipe for waking up the kernel thread */ |
ef599319 | 5724 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { |
cf3af59e | 5725 | goto exit; |
7a485870 DG |
5726 | } |
5727 | ||
099e26bd | 5728 | /* Setup the thread apps communication pipe. */ |
ef599319 | 5729 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
099e26bd DG |
5730 | goto exit; |
5731 | } | |
5732 | ||
5733 | /* Init UST command queue. */ | |
5734 | cds_wfq_init(&ust_cmd_queue.queue); | |
5735 | ||
273ea72c | 5736 | /* |
54d01ffb DG |
5737 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
5738 | * statically declared in session.c | |
273ea72c | 5739 | */ |
54d01ffb | 5740 | session_list_ptr = session_get_list(); |
b5541356 | 5741 | |
5eb91c98 DG |
5742 | /* Set up max poll set size */ |
5743 | lttng_poll_set_max_size(); | |
5744 | ||
00e2e675 DG |
5745 | /* |
5746 | * Set network sequence index to 1 for streams to match a relayd socket on | |
5747 | * the consumer side. | |
5748 | */ | |
5749 | uatomic_set(&relayd_net_seq_idx, 1); | |
5750 | ||
44a5e5eb DG |
5751 | /* Init all health thread counters. */ |
5752 | health_init(&health_thread_cmd); | |
5753 | health_init(&health_thread_kernel); | |
5754 | health_init(&health_thread_app_reg); | |
5755 | ||
5756 | /* | |
5757 | * Init health counters of the consumer thread. We do a quick hack here to | |
5758 | * the state of the consumer health is fine even if the thread is not | |
5759 | * started. This is simply to ease our life and has no cost what so ever. | |
5760 | */ | |
5761 | health_init(&kconsumer_data.health); | |
5762 | health_poll_update(&kconsumer_data.health); | |
5763 | health_init(&ustconsumer32_data.health); | |
5764 | health_poll_update(&ustconsumer32_data.health); | |
5765 | health_init(&ustconsumer64_data.health); | |
5766 | health_poll_update(&ustconsumer64_data.health); | |
5767 | ||
5768 | /* Create thread to manage the client socket */ | |
5769 | ret = pthread_create(&health_thread, NULL, | |
5770 | thread_manage_health, (void *) NULL); | |
5771 | if (ret != 0) { | |
5772 | PERROR("pthread_create health"); | |
5773 | goto exit_health; | |
5774 | } | |
5775 | ||
cf3af59e | 5776 | /* Create thread to manage the client socket */ |
099e26bd DG |
5777 | ret = pthread_create(&client_thread, NULL, |
5778 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 5779 | if (ret != 0) { |
76d7553f | 5780 | PERROR("pthread_create clients"); |
cf3af59e MD |
5781 | goto exit_client; |
5782 | } | |
fac6795d | 5783 | |
099e26bd DG |
5784 | /* Create thread to dispatch registration */ |
5785 | ret = pthread_create(&dispatch_thread, NULL, | |
5786 | thread_dispatch_ust_registration, (void *) NULL); | |
5787 | if (ret != 0) { | |
76d7553f | 5788 | PERROR("pthread_create dispatch"); |
099e26bd DG |
5789 | goto exit_dispatch; |
5790 | } | |
5791 | ||
5792 | /* Create thread to manage application registration. */ | |
5793 | ret = pthread_create(®_apps_thread, NULL, | |
5794 | thread_registration_apps, (void *) NULL); | |
5795 | if (ret != 0) { | |
76d7553f | 5796 | PERROR("pthread_create registration"); |
099e26bd DG |
5797 | goto exit_reg_apps; |
5798 | } | |
5799 | ||
cf3af59e | 5800 | /* Create thread to manage application socket */ |
54d01ffb DG |
5801 | ret = pthread_create(&apps_thread, NULL, |
5802 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 5803 | if (ret != 0) { |
76d7553f | 5804 | PERROR("pthread_create apps"); |
cf3af59e MD |
5805 | goto exit_apps; |
5806 | } | |
fac6795d | 5807 | |
cf3af59e | 5808 | /* Create kernel thread to manage kernel event */ |
54d01ffb DG |
5809 | ret = pthread_create(&kernel_thread, NULL, |
5810 | thread_manage_kernel, (void *) NULL); | |
cf3af59e | 5811 | if (ret != 0) { |
76d7553f | 5812 | PERROR("pthread_create kernel"); |
cf3af59e MD |
5813 | goto exit_kernel; |
5814 | } | |
7a485870 | 5815 | |
cf3af59e MD |
5816 | ret = pthread_join(kernel_thread, &status); |
5817 | if (ret != 0) { | |
76d7553f | 5818 | PERROR("pthread_join"); |
cf3af59e | 5819 | goto error; /* join error, exit without cleanup */ |
fac6795d DG |
5820 | } |
5821 | ||
cf3af59e MD |
5822 | exit_kernel: |
5823 | ret = pthread_join(apps_thread, &status); | |
5824 | if (ret != 0) { | |
76d7553f | 5825 | PERROR("pthread_join"); |
cf3af59e MD |
5826 | goto error; /* join error, exit without cleanup */ |
5827 | } | |
fac6795d | 5828 | |
cf3af59e | 5829 | exit_apps: |
099e26bd DG |
5830 | ret = pthread_join(reg_apps_thread, &status); |
5831 | if (ret != 0) { | |
76d7553f | 5832 | PERROR("pthread_join"); |
099e26bd DG |
5833 | goto error; /* join error, exit without cleanup */ |
5834 | } | |
5835 | ||
5836 | exit_reg_apps: | |
5837 | ret = pthread_join(dispatch_thread, &status); | |
5838 | if (ret != 0) { | |
76d7553f | 5839 | PERROR("pthread_join"); |
099e26bd DG |
5840 | goto error; /* join error, exit without cleanup */ |
5841 | } | |
5842 | ||
5843 | exit_dispatch: | |
cf3af59e MD |
5844 | ret = pthread_join(client_thread, &status); |
5845 | if (ret != 0) { | |
76d7553f | 5846 | PERROR("pthread_join"); |
cf3af59e MD |
5847 | goto error; /* join error, exit without cleanup */ |
5848 | } | |
5849 | ||
3bd1e081 | 5850 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 5851 | if (ret != 0) { |
76d7553f | 5852 | PERROR("join_consumer"); |
cf3af59e MD |
5853 | goto error; /* join error, exit without cleanup */ |
5854 | } | |
a88df331 | 5855 | |
cf3af59e | 5856 | exit_client: |
44a5e5eb | 5857 | exit_health: |
a88df331 | 5858 | exit: |
cf3af59e MD |
5859 | /* |
5860 | * cleanup() is called when no other thread is running. | |
5861 | */ | |
f6a9efaa | 5862 | rcu_thread_online(); |
cf3af59e | 5863 | cleanup(); |
f6a9efaa DG |
5864 | rcu_thread_offline(); |
5865 | rcu_unregister_thread(); | |
67e40797 | 5866 | if (!ret) { |
cf3af59e | 5867 | exit(EXIT_SUCCESS); |
67e40797 | 5868 | } |
cf3af59e | 5869 | error: |
5e16da05 | 5870 | exit(EXIT_FAILURE); |
fac6795d | 5871 | } |