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> |
81b86775 | 47 | #include <common/utils.h> |
fac6795d | 48 | |
10a8a223 | 49 | #include "lttng-sessiond.h" |
54d01ffb | 50 | #include "channel.h" |
00e2e675 | 51 | #include "consumer.h" |
099e26bd | 52 | #include "context.h" |
54d01ffb | 53 | #include "event.h" |
4771f025 | 54 | #include "kernel.h" |
f1e16794 | 55 | #include "kernel-consumer.h" |
096102bd | 56 | #include "modprobe.h" |
0fdd1e2c | 57 | #include "shm.h" |
1e307fab | 58 | #include "ust-ctl.h" |
00e2e675 | 59 | #include "ust-consumer.h" |
8e68d1c8 | 60 | #include "utils.h" |
4063050c | 61 | #include "fd-limit.h" |
53a80697 | 62 | #include "filter.h" |
44a5e5eb | 63 | #include "health.h" |
fac6795d | 64 | |
ebaeda94 MD |
65 | #define CONSUMERD_FILE "lttng-consumerd" |
66 | ||
75462a81 | 67 | /* Const values */ |
686204ab | 68 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
bbccc3d2 | 69 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
686204ab MD |
70 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
71 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
72 | ||
fac6795d DG |
73 | const char *progname; |
74 | const char *opt_tracing_group; | |
5b8719f5 | 75 | static int opt_sig_parent; |
97e19046 | 76 | static int opt_verbose_consumer; |
fac6795d | 77 | static int opt_daemon; |
4fba7219 | 78 | static int opt_no_kernel; |
fac6795d | 79 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 80 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
67e40797 | 81 | static char *rundir; |
3bd1e081 MD |
82 | |
83 | /* Consumer daemon specific control data */ | |
84 | static struct consumer_data kconsumer_data = { | |
85 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
86 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
87 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
88 | .err_sock = -1, |
89 | .cmd_sock = -1, | |
173af62f DG |
90 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
91 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 92 | }; |
7753dea8 MD |
93 | static struct consumer_data ustconsumer64_data = { |
94 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
95 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
96 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
97 | .err_sock = -1, |
98 | .cmd_sock = -1, | |
173af62f DG |
99 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
100 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
101 | }; |
102 | static struct consumer_data ustconsumer32_data = { | |
103 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
104 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
105 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
106 | .err_sock = -1, |
107 | .cmd_sock = -1, | |
173af62f DG |
108 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
109 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
110 | }; |
111 | ||
26c9d55e | 112 | /* Shared between threads */ |
099e26bd | 113 | static int dispatch_thread_exit; |
fac6795d | 114 | |
54d01ffb DG |
115 | /* Global application Unix socket path */ |
116 | static char apps_unix_sock_path[PATH_MAX]; | |
117 | /* Global client Unix socket path */ | |
118 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
119 | /* global wait shm path for UST */ |
120 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
121 | /* Global health check unix path */ |
122 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 123 | |
1d4b027a | 124 | /* Sockets and FDs */ |
a4b35e07 MD |
125 | static int client_sock = -1; |
126 | static int apps_sock = -1; | |
127 | static int kernel_tracer_fd = -1; | |
76d7553f | 128 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 129 | |
273ea72c DG |
130 | /* |
131 | * Quit pipe for all threads. This permits a single cancellation point | |
132 | * for all threads when receiving an event on the pipe. | |
133 | */ | |
76d7553f | 134 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 135 | |
099e26bd DG |
136 | /* |
137 | * This pipe is used to inform the thread managing application communication | |
138 | * that a command is queued and ready to be processed. | |
139 | */ | |
76d7553f | 140 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 141 | |
1d4b027a | 142 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 143 | static pthread_t apps_thread; |
099e26bd | 144 | static pthread_t reg_apps_thread; |
1d4b027a | 145 | static pthread_t client_thread; |
7a485870 | 146 | static pthread_t kernel_thread; |
099e26bd | 147 | static pthread_t dispatch_thread; |
44a5e5eb | 148 | static pthread_t health_thread; |
5eb91c98 | 149 | |
099e26bd DG |
150 | /* |
151 | * UST registration command queue. This queue is tied with a futex and uses a N | |
152 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
153 | * | |
154 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
155 | * this queue and the wait/wake scheme. | |
156 | */ | |
157 | static struct ust_cmd_queue ust_cmd_queue; | |
158 | ||
b5541356 DG |
159 | /* |
160 | * Pointer initialized before thread creation. | |
161 | * | |
162 | * This points to the tracing session list containing the session count and a | |
163 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
164 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 165 | * |
d063d709 | 166 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 167 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
168 | */ |
169 | static struct ltt_session_list *session_list_ptr; | |
170 | ||
7753dea8 MD |
171 | int ust_consumerd64_fd = -1; |
172 | int ust_consumerd32_fd = -1; | |
173 | ||
fb6f1fa2 YB |
174 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
175 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
176 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
177 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
fb09408a | 178 | |
5c827ce0 DG |
179 | /* |
180 | * Consumer daemon state which is changed when spawning it, killing it or in | |
181 | * case of a fatal error. | |
182 | */ | |
183 | enum consumerd_state { | |
184 | CONSUMER_STARTED = 1, | |
185 | CONSUMER_STOPPED = 2, | |
186 | CONSUMER_ERROR = 3, | |
187 | }; | |
188 | ||
189 | /* | |
190 | * This consumer daemon state is used to validate if a client command will be | |
191 | * able to reach the consumer. If not, the client is informed. For instance, | |
192 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
193 | * error to the client. | |
194 | * | |
195 | * The following example shows a possible race condition of this scheme: | |
196 | * | |
197 | * consumer thread error happens | |
198 | * client cmd arrives | |
199 | * client cmd checks state -> still OK | |
200 | * consumer thread exit, sets error | |
201 | * client cmd try to talk to consumer | |
202 | * ... | |
203 | * | |
204 | * However, since the consumer is a different daemon, we have no way of making | |
205 | * sure the command will reach it safely even with this state flag. This is why | |
206 | * we consider that up to the state validation during command processing, the | |
207 | * command is safe. After that, we can not guarantee the correctness of the | |
208 | * client request vis-a-vis the consumer. | |
209 | */ | |
210 | static enum consumerd_state ust_consumerd_state; | |
211 | static enum consumerd_state kernel_consumerd_state; | |
212 | ||
00e2e675 DG |
213 | /* |
214 | * Used to keep a unique index for each relayd socket created where this value | |
215 | * is associated with streams on the consumer so it can match the right relayd | |
216 | * to send to. | |
217 | * | |
218 | * This value should be incremented atomically for safety purposes and future | |
219 | * possible concurrent access. | |
220 | */ | |
221 | static unsigned int relayd_net_seq_idx; | |
222 | ||
44a5e5eb DG |
223 | /* Used for the health monitoring of the session daemon. See health.h */ |
224 | struct health_state health_thread_cmd; | |
139ac872 | 225 | struct health_state health_thread_app_manage; |
44a5e5eb DG |
226 | struct health_state health_thread_app_reg; |
227 | struct health_state health_thread_kernel; | |
228 | ||
fb09408a | 229 | static |
7753dea8 | 230 | void setup_consumerd_path(void) |
fb09408a | 231 | { |
fc7a59ce | 232 | const char *bin, *libdir; |
fb09408a | 233 | |
7753dea8 MD |
234 | /* |
235 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
236 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
237 | * has not been defined. | |
7753dea8 | 238 | */ |
ebaeda94 | 239 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
240 | if (!consumerd32_bin[0]) { |
241 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
242 | } |
243 | if (!consumerd32_libdir[0]) { | |
244 | consumerd32_libdir = INSTALL_LIB_PATH; | |
245 | } | |
246 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
247 | if (!consumerd64_bin[0]) { |
248 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 249 | } |
ebaeda94 MD |
250 | if (!consumerd64_libdir[0]) { |
251 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
252 | } |
253 | #else | |
254 | #error "Unknown bitness" | |
255 | #endif | |
256 | ||
fb09408a MD |
257 | /* |
258 | * runtime env. var. overrides the build default. | |
259 | */ | |
fc7a59ce AM |
260 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
261 | if (bin) { | |
262 | consumerd32_bin = bin; | |
7753dea8 | 263 | } |
fc7a59ce AM |
264 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
265 | if (bin) { | |
266 | consumerd64_bin = bin; | |
ebaeda94 | 267 | } |
72f579ee | 268 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
269 | if (libdir) { |
270 | consumerd32_libdir = libdir; | |
271 | } | |
72f579ee | 272 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
273 | if (libdir) { |
274 | consumerd64_libdir = libdir; | |
fb09408a MD |
275 | } |
276 | } | |
277 | ||
5eb91c98 DG |
278 | /* |
279 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
280 | */ | |
281 | static int create_thread_poll_set(struct lttng_poll_event *events, | |
282 | unsigned int size) | |
283 | { | |
284 | int ret; | |
285 | ||
286 | if (events == NULL || size == 0) { | |
287 | ret = -1; | |
288 | goto error; | |
289 | } | |
290 | ||
291 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
292 | if (ret < 0) { | |
293 | goto error; | |
294 | } | |
295 | ||
296 | /* Add quit pipe */ | |
297 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); | |
298 | if (ret < 0) { | |
299 | goto error; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | ||
304 | error: | |
305 | return ret; | |
306 | } | |
307 | ||
308 | /* | |
309 | * Check if the thread quit pipe was triggered. | |
310 | * | |
311 | * Return 1 if it was triggered else 0; | |
312 | */ | |
313 | static int check_thread_quit_pipe(int fd, uint32_t events) | |
314 | { | |
315 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
316 | return 1; | |
317 | } | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
0fdd1e2c DG |
322 | /* |
323 | * Return group ID of the tracing group or -1 if not found. | |
324 | */ | |
996b65c8 MD |
325 | static gid_t allowed_group(void) |
326 | { | |
327 | struct group *grp; | |
328 | ||
274e143b MD |
329 | if (opt_tracing_group) { |
330 | grp = getgrnam(opt_tracing_group); | |
331 | } else { | |
332 | grp = getgrnam(default_tracing_group); | |
333 | } | |
996b65c8 MD |
334 | if (!grp) { |
335 | return -1; | |
336 | } else { | |
337 | return grp->gr_gid; | |
338 | } | |
339 | } | |
340 | ||
273ea72c | 341 | /* |
5eb91c98 | 342 | * Init thread quit pipe. |
273ea72c DG |
343 | * |
344 | * Return -1 on error or 0 if all pipes are created. | |
345 | */ | |
346 | static int init_thread_quit_pipe(void) | |
347 | { | |
730389d9 | 348 | int ret, i; |
273ea72c | 349 | |
730389d9 | 350 | ret = pipe(thread_quit_pipe); |
273ea72c | 351 | if (ret < 0) { |
730389d9 | 352 | PERROR("thread quit pipe"); |
273ea72c DG |
353 | goto error; |
354 | } | |
355 | ||
730389d9 DG |
356 | for (i = 0; i < 2; i++) { |
357 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
358 | if (ret < 0) { | |
359 | PERROR("fcntl"); | |
360 | goto error; | |
361 | } | |
362 | } | |
363 | ||
273ea72c DG |
364 | error: |
365 | return ret; | |
366 | } | |
367 | ||
fac6795d | 368 | /* |
d063d709 DG |
369 | * Complete teardown of a kernel session. This free all data structure related |
370 | * to a kernel session and update counter. | |
fac6795d | 371 | */ |
1d4b027a | 372 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 373 | { |
173af62f DG |
374 | int ret; |
375 | struct lttng_ht_iter iter; | |
376 | struct ltt_kernel_session *ksess; | |
377 | struct consumer_socket *socket; | |
378 | ||
8f7a84dd | 379 | if (!session->kernel_session) { |
99bab54f | 380 | DBG3("No kernel session when tearing down session"); |
84ea9c99 | 381 | return; |
8f7a84dd DG |
382 | } |
383 | ||
173af62f DG |
384 | ksess = session->kernel_session; |
385 | ||
84ea9c99 | 386 | DBG("Tearing down kernel session"); |
d9800920 | 387 | |
173af62f DG |
388 | /* |
389 | * Destroy relayd associated with the session consumer. This action is | |
390 | * valid since in order to destroy a session we must acquire the session | |
391 | * lock. This means that there CAN NOT be stream(s) being sent to a | |
392 | * consumer since this action also requires the session lock at any time. | |
393 | * | |
394 | * At this point, we are sure that not streams data will be lost after this | |
395 | * command is issued. | |
396 | */ | |
397 | if (ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET) { | |
398 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket, | |
399 | node.node) { | |
400 | ret = consumer_send_destroy_relayd(socket, ksess->consumer); | |
401 | if (ret < 0) { | |
402 | ERR("Unable to send destroy relayd command to consumer"); | |
403 | /* Continue since we MUST delete everything at this point. */ | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
84ea9c99 MD |
408 | /* |
409 | * If a custom kernel consumer was registered, close the socket before | |
410 | * tearing down the complete kernel session structure | |
411 | */ | |
173af62f DG |
412 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket, |
413 | node.node) { | |
414 | if (socket->fd != kconsumer_data.cmd_sock) { | |
415 | rcu_read_lock(); | |
416 | consumer_del_socket(socket, ksess->consumer); | |
417 | lttcomm_close_unix_sock(socket->fd); | |
418 | consumer_destroy_socket(socket); | |
419 | rcu_read_unlock(); | |
420 | } | |
fac6795d | 421 | } |
84ea9c99 | 422 | |
173af62f | 423 | trace_kernel_destroy_session(ksess); |
fac6795d DG |
424 | } |
425 | ||
f6a9efaa DG |
426 | /* |
427 | * Complete teardown of all UST sessions. This will free everything on his path | |
428 | * and destroy the core essence of all ust sessions :) | |
429 | */ | |
430 | static void teardown_ust_session(struct ltt_session *session) | |
431 | { | |
84cd17c6 | 432 | int ret; |
173af62f DG |
433 | struct lttng_ht_iter iter; |
434 | struct ltt_ust_session *usess; | |
435 | struct consumer_socket *socket; | |
84cd17c6 | 436 | |
8f7a84dd | 437 | if (!session->ust_session) { |
99bab54f | 438 | DBG3("No UST session when tearing down session"); |
f470a390 | 439 | return; |
8f7a84dd | 440 | } |
173af62f | 441 | usess = session->ust_session; |
8f7a84dd | 442 | |
84ea9c99 | 443 | DBG("Tearing down UST session(s)"); |
8f7a84dd | 444 | |
173af62f DG |
445 | /* |
446 | * Destroy relayd associated with the session consumer. This action is | |
447 | * valid since in order to destroy a session we must acquire the session | |
448 | * lock. This means that there CAN NOT be stream(s) being sent to a | |
449 | * consumer since this action also requires the session lock at any time. | |
450 | * | |
3f8e211f DG |
451 | * At this point, we are sure that no data will be lost after this command |
452 | * is issued. | |
173af62f DG |
453 | */ |
454 | if (usess->consumer && usess->consumer->type == CONSUMER_DST_NET) { | |
455 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, socket, | |
456 | node.node) { | |
457 | ret = consumer_send_destroy_relayd(socket, usess->consumer); | |
458 | if (ret < 0) { | |
459 | ERR("Unable to send destroy relayd command to consumer"); | |
460 | /* Continue since we MUST delete everything at this point. */ | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
465 | ret = ust_app_destroy_trace_all(usess); | |
84cd17c6 MD |
466 | if (ret) { |
467 | ERR("Error in ust_app_destroy_trace_all"); | |
468 | } | |
8f7a84dd | 469 | |
173af62f | 470 | trace_ust_destroy_session(usess); |
f6a9efaa DG |
471 | } |
472 | ||
099e26bd DG |
473 | /* |
474 | * Stop all threads by closing the thread quit pipe. | |
475 | */ | |
cf3af59e MD |
476 | static void stop_threads(void) |
477 | { | |
5eb91c98 DG |
478 | int ret; |
479 | ||
cf3af59e MD |
480 | /* Stopping all threads */ |
481 | DBG("Terminating all threads"); | |
54d01ffb | 482 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
483 | if (ret < 0) { |
484 | ERR("write error on thread quit pipe"); | |
485 | } | |
486 | ||
099e26bd | 487 | /* Dispatch thread */ |
26c9d55e | 488 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 489 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
490 | } |
491 | ||
fac6795d | 492 | /* |
d063d709 | 493 | * Cleanup the daemon |
fac6795d | 494 | */ |
cf3af59e | 495 | static void cleanup(void) |
fac6795d | 496 | { |
ef599319 | 497 | int ret; |
1d4b027a | 498 | char *cmd; |
af9737e9 | 499 | struct ltt_session *sess, *stmp; |
fac6795d | 500 | |
1d4b027a | 501 | DBG("Cleaning up"); |
e07ae692 | 502 | |
67e40797 DG |
503 | DBG("Removing %s directory", rundir); |
504 | ret = asprintf(&cmd, "rm -rf %s", rundir); | |
505 | if (ret < 0) { | |
506 | ERR("asprintf failed. Something is really wrong!"); | |
507 | } | |
5461b305 | 508 | |
67e40797 DG |
509 | /* Remove lttng run directory */ |
510 | ret = system(cmd); | |
511 | if (ret < 0) { | |
512 | ERR("Unable to clean %s", rundir); | |
1d4b027a | 513 | } |
67e40797 | 514 | free(cmd); |
5461b305 | 515 | |
99bab54f | 516 | DBG("Cleaning up all sessions"); |
fac6795d | 517 | |
b5541356 | 518 | /* Destroy session list mutex */ |
273ea72c DG |
519 | if (session_list_ptr != NULL) { |
520 | pthread_mutex_destroy(&session_list_ptr->lock); | |
521 | ||
522 | /* Cleanup ALL session */ | |
54d01ffb DG |
523 | cds_list_for_each_entry_safe(sess, stmp, |
524 | &session_list_ptr->head, list) { | |
273ea72c | 525 | teardown_kernel_session(sess); |
f6a9efaa DG |
526 | teardown_ust_session(sess); |
527 | free(sess); | |
273ea72c DG |
528 | } |
529 | } | |
530 | ||
099e26bd | 531 | DBG("Closing all UST sockets"); |
56fff090 | 532 | ust_app_clean_list(); |
099e26bd | 533 | |
4fba7219 DG |
534 | if (is_root && !opt_no_kernel) { |
535 | DBG2("Closing kernel fd"); | |
a4b35e07 | 536 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
537 | ret = close(kernel_tracer_fd); |
538 | if (ret) { | |
539 | PERROR("close"); | |
540 | } | |
a4b35e07 | 541 | } |
2f50c8a3 | 542 | DBG("Unloading kernel modules"); |
096102bd | 543 | modprobe_remove_lttng_all(); |
2f50c8a3 | 544 | } |
ef599319 DG |
545 | utils_close_pipe(kernel_poll_pipe); |
546 | utils_close_pipe(thread_quit_pipe); | |
547 | utils_close_pipe(apps_cmd_pipe); | |
421cb601 DG |
548 | |
549 | /* <fun> */ | |
f56a39af | 550 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
551 | "Matthew, BEET driven development works!%c[%dm", |
552 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
553 | /* </fun> */ | |
fac6795d DG |
554 | } |
555 | ||
e065084a | 556 | /* |
d063d709 | 557 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 558 | * |
d063d709 | 559 | * Return lttcomm error code. |
e065084a DG |
560 | */ |
561 | static int send_unix_sock(int sock, void *buf, size_t len) | |
562 | { | |
563 | /* Check valid length */ | |
564 | if (len <= 0) { | |
565 | return -1; | |
566 | } | |
567 | ||
568 | return lttcomm_send_unix_sock(sock, buf, len); | |
569 | } | |
570 | ||
5461b305 | 571 | /* |
d063d709 | 572 | * Free memory of a command context structure. |
5461b305 | 573 | */ |
a2fb29a5 | 574 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 575 | { |
a2fb29a5 DG |
576 | DBG("Clean command context structure"); |
577 | if (*cmd_ctx) { | |
578 | if ((*cmd_ctx)->llm) { | |
579 | free((*cmd_ctx)->llm); | |
5461b305 | 580 | } |
a2fb29a5 DG |
581 | if ((*cmd_ctx)->lsm) { |
582 | free((*cmd_ctx)->lsm); | |
5461b305 | 583 | } |
a2fb29a5 DG |
584 | free(*cmd_ctx); |
585 | *cmd_ctx = NULL; | |
5461b305 DG |
586 | } |
587 | } | |
588 | ||
fac6795d | 589 | /* |
0fdd1e2c | 590 | * Notify UST applications using the shm mmap futex. |
fac6795d | 591 | */ |
0fdd1e2c | 592 | static int notify_ust_apps(int active) |
fac6795d | 593 | { |
0fdd1e2c | 594 | char *wait_shm_mmap; |
fac6795d | 595 | |
0fdd1e2c | 596 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 597 | |
0fdd1e2c DG |
598 | /* See shm.c for this call implying mmap, shm and futex calls */ |
599 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
600 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
601 | goto error; |
602 | } | |
603 | ||
0fdd1e2c DG |
604 | /* Wake waiting process */ |
605 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
606 | ||
607 | /* Apps notified successfully */ | |
608 | return 0; | |
fac6795d DG |
609 | |
610 | error: | |
0fdd1e2c | 611 | return -1; |
fac6795d DG |
612 | } |
613 | ||
e065084a | 614 | /* |
d063d709 DG |
615 | * Setup the outgoing data buffer for the response (llm) by allocating the |
616 | * right amount of memory and copying the original information from the lsm | |
617 | * structure. | |
ca95a216 | 618 | * |
d063d709 | 619 | * Return total size of the buffer pointed by buf. |
ca95a216 | 620 | */ |
5461b305 | 621 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 622 | { |
f3ed775e | 623 | int ret, buf_size; |
ca95a216 | 624 | |
f3ed775e | 625 | buf_size = size; |
5461b305 | 626 | |
ba7f0ae5 | 627 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 628 | if (cmd_ctx->llm == NULL) { |
76d7553f | 629 | PERROR("zmalloc"); |
5461b305 | 630 | ret = -ENOMEM; |
ca95a216 DG |
631 | goto error; |
632 | } | |
633 | ||
5461b305 DG |
634 | /* Copy common data */ |
635 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 636 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 637 | |
5461b305 DG |
638 | cmd_ctx->llm->data_size = size; |
639 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
640 | ||
ca95a216 DG |
641 | return buf_size; |
642 | ||
643 | error: | |
644 | return ret; | |
645 | } | |
646 | ||
7a485870 | 647 | /* |
5eb91c98 | 648 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 649 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 650 | */ |
5eb91c98 | 651 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 652 | { |
5eb91c98 | 653 | int ret; |
7a485870 DG |
654 | struct ltt_session *session; |
655 | struct ltt_kernel_channel *channel; | |
656 | ||
5eb91c98 | 657 | DBG("Updating kernel poll set"); |
7a485870 | 658 | |
54d01ffb | 659 | session_lock_list(); |
b5541356 | 660 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 661 | session_lock(session); |
7a485870 | 662 | if (session->kernel_session == NULL) { |
54d01ffb | 663 | session_unlock(session); |
7a485870 DG |
664 | continue; |
665 | } | |
7a485870 | 666 | |
54d01ffb DG |
667 | cds_list_for_each_entry(channel, |
668 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
669 | /* Add channel fd to the kernel poll set */ |
670 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
671 | if (ret < 0) { | |
54d01ffb | 672 | session_unlock(session); |
5eb91c98 DG |
673 | goto error; |
674 | } | |
675 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 676 | } |
54d01ffb | 677 | session_unlock(session); |
7a485870 | 678 | } |
54d01ffb | 679 | session_unlock_list(); |
7a485870 | 680 | |
5eb91c98 | 681 | return 0; |
7a485870 DG |
682 | |
683 | error: | |
54d01ffb | 684 | session_unlock_list(); |
7a485870 DG |
685 | return -1; |
686 | } | |
687 | ||
688 | /* | |
54d01ffb | 689 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 690 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 691 | * |
d063d709 | 692 | * Useful for CPU hotplug feature. |
7a485870 | 693 | */ |
2bdd86d4 | 694 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
695 | { |
696 | int ret = 0; | |
697 | struct ltt_session *session; | |
173af62f | 698 | struct ltt_kernel_session *ksess; |
7a485870 DG |
699 | struct ltt_kernel_channel *channel; |
700 | ||
701 | DBG("Updating kernel streams for channel fd %d", fd); | |
702 | ||
54d01ffb | 703 | session_lock_list(); |
b5541356 | 704 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 705 | session_lock(session); |
7a485870 | 706 | if (session->kernel_session == NULL) { |
54d01ffb | 707 | session_unlock(session); |
7a485870 DG |
708 | continue; |
709 | } | |
173af62f | 710 | ksess = session->kernel_session; |
d9800920 | 711 | |
173af62f | 712 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
7a485870 DG |
713 | if (channel->fd == fd) { |
714 | DBG("Channel found, updating kernel streams"); | |
715 | ret = kernel_open_channel_stream(channel); | |
716 | if (ret < 0) { | |
b3c750d2 | 717 | goto error; |
7a485870 | 718 | } |
d9800920 | 719 | |
7a485870 | 720 | /* |
5eb91c98 DG |
721 | * Have we already sent fds to the consumer? If yes, it means |
722 | * that tracing is started so it is safe to send our updated | |
723 | * stream fds. | |
7a485870 | 724 | */ |
173af62f DG |
725 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
726 | struct lttng_ht_iter iter; | |
727 | struct consumer_socket *socket; | |
728 | ||
729 | ||
730 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, | |
731 | &iter.iter, socket, node.node) { | |
732 | /* Code flow error */ | |
733 | assert(socket->fd >= 0); | |
734 | ||
735 | pthread_mutex_lock(socket->lock); | |
736 | ret = kernel_consumer_send_channel_stream(socket->fd, | |
737 | channel, ksess); | |
738 | pthread_mutex_unlock(socket->lock); | |
739 | if (ret < 0) { | |
740 | goto error; | |
741 | } | |
7a485870 DG |
742 | } |
743 | } | |
b3c750d2 | 744 | goto error; |
7a485870 DG |
745 | } |
746 | } | |
54d01ffb | 747 | session_unlock(session); |
7a485870 | 748 | } |
54d01ffb | 749 | session_unlock_list(); |
b3c750d2 | 750 | return ret; |
7a485870 | 751 | |
b3c750d2 | 752 | error: |
54d01ffb DG |
753 | session_unlock(session); |
754 | session_unlock_list(); | |
7a485870 DG |
755 | return ret; |
756 | } | |
757 | ||
487cf67c DG |
758 | /* |
759 | * For each tracing session, update newly registered apps. | |
760 | */ | |
761 | static void update_ust_app(int app_sock) | |
762 | { | |
763 | struct ltt_session *sess, *stmp; | |
764 | ||
4ee14516 DG |
765 | session_lock_list(); |
766 | ||
487cf67c DG |
767 | /* For all tracing session(s) */ |
768 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 769 | session_lock(sess); |
421cb601 DG |
770 | if (sess->ust_session) { |
771 | ust_app_global_update(sess->ust_session, app_sock); | |
772 | } | |
4ee14516 | 773 | session_unlock(sess); |
487cf67c | 774 | } |
4ee14516 DG |
775 | |
776 | session_unlock_list(); | |
487cf67c DG |
777 | } |
778 | ||
7a485870 | 779 | /* |
d063d709 | 780 | * This thread manage event coming from the kernel. |
7a485870 | 781 | * |
d063d709 DG |
782 | * Features supported in this thread: |
783 | * -) CPU Hotplug | |
7a485870 DG |
784 | */ |
785 | static void *thread_manage_kernel(void *data) | |
786 | { | |
139ac872 | 787 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 788 | uint32_t revents, nb_fd; |
7a485870 | 789 | char tmp; |
5eb91c98 | 790 | struct lttng_poll_event events; |
7a485870 DG |
791 | |
792 | DBG("Thread manage kernel started"); | |
793 | ||
44a5e5eb DG |
794 | health_code_update(&health_thread_kernel); |
795 | ||
5eb91c98 DG |
796 | ret = create_thread_poll_set(&events, 2); |
797 | if (ret < 0) { | |
76d7553f | 798 | goto error_poll_create; |
5eb91c98 DG |
799 | } |
800 | ||
801 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
802 | if (ret < 0) { | |
803 | goto error; | |
804 | } | |
805 | ||
7a485870 | 806 | while (1) { |
44a5e5eb DG |
807 | health_code_update(&health_thread_kernel); |
808 | ||
7a485870 | 809 | if (update_poll_flag == 1) { |
5f822d0a DG |
810 | /* |
811 | * Reset number of fd in the poll set. Always 2 since there is the thread | |
812 | * quit pipe and the kernel pipe. | |
813 | */ | |
814 | events.nb_fd = 2; | |
815 | ||
5eb91c98 DG |
816 | ret = update_kernel_poll(&events); |
817 | if (ret < 0) { | |
7a485870 DG |
818 | goto error; |
819 | } | |
820 | update_poll_flag = 0; | |
821 | } | |
822 | ||
5eb91c98 DG |
823 | nb_fd = LTTNG_POLL_GETNB(&events); |
824 | ||
825 | DBG("Thread kernel polling on %d fds", nb_fd); | |
826 | ||
827 | /* Zeroed the poll events */ | |
828 | lttng_poll_reset(&events); | |
7a485870 DG |
829 | |
830 | /* Poll infinite value of time */ | |
88f2b785 | 831 | restart: |
44a5e5eb | 832 | health_poll_update(&health_thread_kernel); |
5eb91c98 | 833 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 834 | health_poll_update(&health_thread_kernel); |
7a485870 | 835 | if (ret < 0) { |
88f2b785 MD |
836 | /* |
837 | * Restart interrupted system call. | |
838 | */ | |
839 | if (errno == EINTR) { | |
840 | goto restart; | |
841 | } | |
7a485870 DG |
842 | goto error; |
843 | } else if (ret == 0) { | |
844 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
845 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
846 | "This should not have happened! Continuing..."); | |
7a485870 DG |
847 | continue; |
848 | } | |
849 | ||
5eb91c98 DG |
850 | for (i = 0; i < nb_fd; i++) { |
851 | /* Fetch once the poll data */ | |
852 | revents = LTTNG_POLL_GETEV(&events, i); | |
853 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 854 | |
44a5e5eb DG |
855 | health_code_update(&health_thread_kernel); |
856 | ||
5eb91c98 DG |
857 | /* Thread quit pipe has been closed. Killing thread. */ |
858 | ret = check_thread_quit_pipe(pollfd, revents); | |
859 | if (ret) { | |
139ac872 MD |
860 | err = 0; |
861 | goto exit; | |
5eb91c98 | 862 | } |
7a485870 | 863 | |
5eb91c98 DG |
864 | /* Check for data on kernel pipe */ |
865 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
866 | ret = read(kernel_poll_pipe[0], &tmp, 1); | |
867 | update_poll_flag = 1; | |
868 | continue; | |
869 | } else { | |
870 | /* | |
871 | * New CPU detected by the kernel. Adding kernel stream to | |
872 | * kernel session and updating the kernel consumer | |
873 | */ | |
874 | if (revents & LPOLLIN) { | |
2bdd86d4 | 875 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
876 | if (ret < 0) { |
877 | continue; | |
878 | } | |
879 | break; | |
880 | /* | |
881 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
882 | * and unregister kernel stream at this point. | |
883 | */ | |
7a485870 | 884 | } |
7a485870 DG |
885 | } |
886 | } | |
887 | } | |
888 | ||
139ac872 | 889 | exit: |
7a485870 | 890 | error: |
5eb91c98 | 891 | lttng_poll_clean(&events); |
76d7553f | 892 | error_poll_create: |
139ac872 MD |
893 | if (err) { |
894 | health_error(&health_thread_kernel); | |
895 | ERR("Health error occurred in %s", __func__); | |
896 | } | |
897 | health_exit(&health_thread_kernel); | |
76d7553f | 898 | DBG("Kernel thread dying"); |
7a485870 DG |
899 | return NULL; |
900 | } | |
901 | ||
1d4b027a | 902 | /* |
3bd1e081 | 903 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 904 | */ |
3bd1e081 | 905 | static void *thread_manage_consumer(void *data) |
1d4b027a | 906 | { |
139ac872 | 907 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 | 908 | uint32_t revents, nb_fd; |
1d4b027a | 909 | enum lttcomm_return_code code; |
5eb91c98 | 910 | struct lttng_poll_event events; |
3bd1e081 | 911 | struct consumer_data *consumer_data = data; |
1d4b027a | 912 | |
3bd1e081 | 913 | DBG("[thread] Manage consumer started"); |
1d4b027a | 914 | |
44a5e5eb DG |
915 | health_code_update(&consumer_data->health); |
916 | ||
3bd1e081 | 917 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
1d4b027a | 918 | if (ret < 0) { |
76d7553f | 919 | goto error_listen; |
1d4b027a DG |
920 | } |
921 | ||
5eb91c98 DG |
922 | /* |
923 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. | |
924 | * Nothing more will be added to this poll set. | |
925 | */ | |
926 | ret = create_thread_poll_set(&events, 2); | |
927 | if (ret < 0) { | |
76d7553f | 928 | goto error_poll; |
5eb91c98 | 929 | } |
273ea72c | 930 | |
3bd1e081 | 931 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
932 | if (ret < 0) { |
933 | goto error; | |
934 | } | |
935 | ||
936 | nb_fd = LTTNG_POLL_GETNB(&events); | |
273ea72c | 937 | |
44a5e5eb DG |
938 | health_code_update(&consumer_data->health); |
939 | ||
273ea72c | 940 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 941 | restart: |
44a5e5eb | 942 | health_poll_update(&consumer_data->health); |
5eb91c98 | 943 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 944 | health_poll_update(&consumer_data->health); |
273ea72c | 945 | if (ret < 0) { |
88f2b785 MD |
946 | /* |
947 | * Restart interrupted system call. | |
948 | */ | |
949 | if (errno == EINTR) { | |
950 | goto restart; | |
951 | } | |
273ea72c DG |
952 | goto error; |
953 | } | |
954 | ||
5eb91c98 DG |
955 | for (i = 0; i < nb_fd; i++) { |
956 | /* Fetch once the poll data */ | |
957 | revents = LTTNG_POLL_GETEV(&events, i); | |
958 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
959 | ||
44a5e5eb DG |
960 | health_code_update(&consumer_data->health); |
961 | ||
5eb91c98 DG |
962 | /* Thread quit pipe has been closed. Killing thread. */ |
963 | ret = check_thread_quit_pipe(pollfd, revents); | |
964 | if (ret) { | |
139ac872 MD |
965 | err = 0; |
966 | goto exit; | |
5eb91c98 DG |
967 | } |
968 | ||
969 | /* Event on the registration socket */ | |
3bd1e081 | 970 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 971 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 972 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
973 | goto error; |
974 | } | |
975 | } | |
273ea72c DG |
976 | } |
977 | ||
3bd1e081 | 978 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
979 | if (sock < 0) { |
980 | goto error; | |
981 | } | |
982 | ||
44a5e5eb DG |
983 | health_code_update(&consumer_data->health); |
984 | ||
3bd1e081 | 985 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 986 | |
712ea556 | 987 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
988 | ret = lttcomm_recv_unix_sock(sock, &code, |
989 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
990 | if (ret <= 0) { |
991 | goto error; | |
992 | } | |
993 | ||
44a5e5eb DG |
994 | health_code_update(&consumer_data->health); |
995 | ||
3bd1e081 MD |
996 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
997 | consumer_data->cmd_sock = | |
998 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
999 | if (consumer_data->cmd_sock < 0) { | |
1000 | sem_post(&consumer_data->sem); | |
48842b30 | 1001 | PERROR("consumer connect"); |
1d4b027a DG |
1002 | goto error; |
1003 | } | |
1004 | /* Signal condition to tell that the kconsumerd is ready */ | |
3bd1e081 MD |
1005 | sem_post(&consumer_data->sem); |
1006 | DBG("consumer command socket ready"); | |
1d4b027a | 1007 | } else { |
3bd1e081 | 1008 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1009 | lttcomm_get_readable_code(-code)); |
1010 | goto error; | |
1011 | } | |
1012 | ||
54d01ffb | 1013 | /* Remove the kconsumerd error sock since we've established a connexion */ |
3bd1e081 | 1014 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1015 | if (ret < 0) { |
72079cae DG |
1016 | goto error; |
1017 | } | |
1018 | ||
5eb91c98 DG |
1019 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1020 | if (ret < 0) { | |
72079cae | 1021 | goto error; |
5eb91c98 DG |
1022 | } |
1023 | ||
44a5e5eb DG |
1024 | health_code_update(&consumer_data->health); |
1025 | ||
5eb91c98 DG |
1026 | /* Update number of fd */ |
1027 | nb_fd = LTTNG_POLL_GETNB(&events); | |
1028 | ||
1029 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1030 | restart_poll: |
44a5e5eb | 1031 | health_poll_update(&consumer_data->health); |
5eb91c98 | 1032 | ret = lttng_poll_wait(&events, -1); |
44a5e5eb | 1033 | health_poll_update(&consumer_data->health); |
5eb91c98 | 1034 | if (ret < 0) { |
88f2b785 MD |
1035 | /* |
1036 | * Restart interrupted system call. | |
1037 | */ | |
1038 | if (errno == EINTR) { | |
1039 | goto restart_poll; | |
1040 | } | |
72079cae DG |
1041 | goto error; |
1042 | } | |
1043 | ||
5eb91c98 DG |
1044 | for (i = 0; i < nb_fd; i++) { |
1045 | /* Fetch once the poll data */ | |
1046 | revents = LTTNG_POLL_GETEV(&events, i); | |
1047 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1048 | ||
44a5e5eb DG |
1049 | health_code_update(&consumer_data->health); |
1050 | ||
5eb91c98 DG |
1051 | /* Thread quit pipe has been closed. Killing thread. */ |
1052 | ret = check_thread_quit_pipe(pollfd, revents); | |
1053 | if (ret) { | |
139ac872 MD |
1054 | err = 0; |
1055 | goto exit; | |
5eb91c98 DG |
1056 | } |
1057 | ||
1058 | /* Event on the kconsumerd socket */ | |
1059 | if (pollfd == sock) { | |
1060 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 1061 | ERR("consumer err socket second poll error"); |
5eb91c98 DG |
1062 | goto error; |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | ||
44a5e5eb DG |
1067 | health_code_update(&consumer_data->health); |
1068 | ||
712ea556 | 1069 | /* Wait for any kconsumerd error */ |
54d01ffb DG |
1070 | ret = lttcomm_recv_unix_sock(sock, &code, |
1071 | sizeof(enum lttcomm_return_code)); | |
712ea556 | 1072 | if (ret <= 0) { |
3bd1e081 | 1073 | ERR("consumer closed the command socket"); |
712ea556 | 1074 | goto error; |
6f61d021 | 1075 | } |
1d4b027a | 1076 | |
3bd1e081 | 1077 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
712ea556 | 1078 | |
139ac872 | 1079 | exit: |
1d4b027a | 1080 | error: |
5c827ce0 DG |
1081 | /* Immediately set the consumerd state to stopped */ |
1082 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1083 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1084 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1085 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1086 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1087 | } else { | |
1088 | /* Code flow error... */ | |
1089 | assert(0); | |
1090 | } | |
1091 | ||
76d7553f MD |
1092 | if (consumer_data->err_sock >= 0) { |
1093 | ret = close(consumer_data->err_sock); | |
1094 | if (ret) { | |
1095 | PERROR("close"); | |
1096 | } | |
1097 | } | |
1098 | if (consumer_data->cmd_sock >= 0) { | |
1099 | ret = close(consumer_data->cmd_sock); | |
1100 | if (ret) { | |
1101 | PERROR("close"); | |
1102 | } | |
1103 | } | |
1104 | if (sock >= 0) { | |
1105 | ret = close(sock); | |
1106 | if (ret) { | |
1107 | PERROR("close"); | |
1108 | } | |
1109 | } | |
273ea72c | 1110 | |
3bd1e081 MD |
1111 | unlink(consumer_data->err_unix_sock_path); |
1112 | unlink(consumer_data->cmd_unix_sock_path); | |
1113 | consumer_data->pid = 0; | |
1d4b027a | 1114 | |
5eb91c98 | 1115 | lttng_poll_clean(&events); |
76d7553f MD |
1116 | error_poll: |
1117 | error_listen: | |
139ac872 MD |
1118 | if (err) { |
1119 | health_error(&consumer_data->health); | |
1120 | ERR("Health error occurred in %s", __func__); | |
1121 | } | |
1122 | health_exit(&consumer_data->health); | |
76d7553f | 1123 | DBG("consumer thread cleanup completed"); |
0177d773 | 1124 | |
5eb91c98 | 1125 | return NULL; |
099e26bd DG |
1126 | } |
1127 | ||
099e26bd DG |
1128 | /* |
1129 | * This thread manage application communication. | |
1d4b027a DG |
1130 | */ |
1131 | static void *thread_manage_apps(void *data) | |
099e26bd | 1132 | { |
139ac872 | 1133 | int i, ret, pollfd, err = -1; |
5eb91c98 | 1134 | uint32_t revents, nb_fd; |
099e26bd | 1135 | struct ust_command ust_cmd; |
5eb91c98 | 1136 | struct lttng_poll_event events; |
099e26bd DG |
1137 | |
1138 | DBG("[thread] Manage application started"); | |
1139 | ||
f6a9efaa DG |
1140 | rcu_register_thread(); |
1141 | rcu_thread_online(); | |
1142 | ||
139ac872 | 1143 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1144 | |
5eb91c98 DG |
1145 | ret = create_thread_poll_set(&events, 2); |
1146 | if (ret < 0) { | |
76d7553f | 1147 | goto error_poll_create; |
5eb91c98 | 1148 | } |
099e26bd | 1149 | |
5eb91c98 DG |
1150 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1151 | if (ret < 0) { | |
1152 | goto error; | |
1153 | } | |
099e26bd | 1154 | |
139ac872 | 1155 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1156 | |
5eb91c98 DG |
1157 | while (1) { |
1158 | /* Zeroed the events structure */ | |
1159 | lttng_poll_reset(&events); | |
0177d773 | 1160 | |
5eb91c98 | 1161 | nb_fd = LTTNG_POLL_GETNB(&events); |
099e26bd DG |
1162 | |
1163 | DBG("Apps thread polling on %d fds", nb_fd); | |
1164 | ||
1165 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1166 | restart: |
139ac872 | 1167 | health_poll_update(&health_thread_app_manage); |
5eb91c98 | 1168 | ret = lttng_poll_wait(&events, -1); |
139ac872 | 1169 | health_poll_update(&health_thread_app_manage); |
099e26bd | 1170 | if (ret < 0) { |
88f2b785 MD |
1171 | /* |
1172 | * Restart interrupted system call. | |
1173 | */ | |
1174 | if (errno == EINTR) { | |
1175 | goto restart; | |
1176 | } | |
099e26bd DG |
1177 | goto error; |
1178 | } | |
1179 | ||
5eb91c98 DG |
1180 | for (i = 0; i < nb_fd; i++) { |
1181 | /* Fetch once the poll data */ | |
1182 | revents = LTTNG_POLL_GETEV(&events, i); | |
1183 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1184 | ||
139ac872 | 1185 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1186 | |
5eb91c98 DG |
1187 | /* Thread quit pipe has been closed. Killing thread. */ |
1188 | ret = check_thread_quit_pipe(pollfd, revents); | |
1189 | if (ret) { | |
139ac872 MD |
1190 | err = 0; |
1191 | goto exit; | |
5eb91c98 | 1192 | } |
099e26bd | 1193 | |
5eb91c98 DG |
1194 | /* Inspect the apps cmd pipe */ |
1195 | if (pollfd == apps_cmd_pipe[0]) { | |
1196 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1197 | ERR("Apps command pipe error"); | |
0177d773 | 1198 | goto error; |
5eb91c98 DG |
1199 | } else if (revents & LPOLLIN) { |
1200 | /* Empty pipe */ | |
1201 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); | |
1202 | if (ret < 0 || ret < sizeof(ust_cmd)) { | |
76d7553f | 1203 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1204 | goto error; |
1205 | } | |
099e26bd | 1206 | |
139ac872 | 1207 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1208 | |
5eb91c98 | 1209 | /* Register applicaton to the session daemon */ |
56fff090 | 1210 | ret = ust_app_register(&ust_cmd.reg_msg, |
54d01ffb | 1211 | ust_cmd.sock); |
88ff5b7f | 1212 | if (ret == -ENOMEM) { |
5eb91c98 | 1213 | goto error; |
88ff5b7f MD |
1214 | } else if (ret < 0) { |
1215 | break; | |
5eb91c98 DG |
1216 | } |
1217 | ||
139ac872 | 1218 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1219 | |
acc7b41b | 1220 | /* |
e0c7ec2b | 1221 | * Validate UST version compatibility. |
acc7b41b | 1222 | */ |
e0c7ec2b DG |
1223 | ret = ust_app_validate_version(ust_cmd.sock); |
1224 | if (ret >= 0) { | |
1225 | /* | |
1226 | * Add channel(s) and event(s) to newly registered apps | |
1227 | * from lttng global UST domain. | |
1228 | */ | |
1229 | update_ust_app(ust_cmd.sock); | |
1230 | } | |
acc7b41b | 1231 | |
139ac872 | 1232 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1233 | |
f2ca2e25 | 1234 | ret = ust_app_register_done(ust_cmd.sock); |
5eb91c98 DG |
1235 | if (ret < 0) { |
1236 | /* | |
1237 | * If the registration is not possible, we simply | |
1238 | * unregister the apps and continue | |
1239 | */ | |
56fff090 | 1240 | ust_app_unregister(ust_cmd.sock); |
5eb91c98 DG |
1241 | } else { |
1242 | /* | |
1243 | * We just need here to monitor the close of the UST | |
1244 | * socket and poll set monitor those by default. | |
a9ade966 MD |
1245 | * Listen on POLLIN (even if we never expect any |
1246 | * data) to ensure that hangup wakes us. | |
5eb91c98 | 1247 | */ |
a9ade966 | 1248 | ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN); |
5eb91c98 DG |
1249 | if (ret < 0) { |
1250 | goto error; | |
1251 | } | |
1252 | ||
54d01ffb DG |
1253 | DBG("Apps with sock %d added to poll set", |
1254 | ust_cmd.sock); | |
5eb91c98 | 1255 | } |
487cf67c | 1256 | |
139ac872 | 1257 | health_code_update(&health_thread_app_manage); |
44a5e5eb | 1258 | |
5eb91c98 | 1259 | break; |
0177d773 | 1260 | } |
5eb91c98 DG |
1261 | } else { |
1262 | /* | |
54d01ffb DG |
1263 | * At this point, we know that a registered application made |
1264 | * the event at poll_wait. | |
5eb91c98 DG |
1265 | */ |
1266 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1267 | /* Removing from the poll set */ | |
1268 | ret = lttng_poll_del(&events, pollfd); | |
1269 | if (ret < 0) { | |
1270 | goto error; | |
1271 | } | |
099e26bd | 1272 | |
b9d9b220 | 1273 | /* Socket closed on remote end. */ |
56fff090 | 1274 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1275 | break; |
1276 | } | |
099e26bd | 1277 | } |
44a5e5eb | 1278 | |
139ac872 | 1279 | health_code_update(&health_thread_app_manage); |
099e26bd | 1280 | } |
099e26bd DG |
1281 | } |
1282 | ||
139ac872 | 1283 | exit: |
099e26bd | 1284 | error: |
5eb91c98 | 1285 | lttng_poll_clean(&events); |
76d7553f | 1286 | error_poll_create: |
139ac872 MD |
1287 | if (err) { |
1288 | health_error(&health_thread_app_manage); | |
1289 | ERR("Health error occurred in %s", __func__); | |
1290 | } | |
1291 | health_exit(&health_thread_app_manage); | |
76d7553f | 1292 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1293 | rcu_thread_offline(); |
1294 | rcu_unregister_thread(); | |
099e26bd DG |
1295 | return NULL; |
1296 | } | |
1297 | ||
1298 | /* | |
1299 | * Dispatch request from the registration threads to the application | |
1300 | * communication thread. | |
1301 | */ | |
1302 | static void *thread_dispatch_ust_registration(void *data) | |
1303 | { | |
1304 | int ret; | |
1305 | struct cds_wfq_node *node; | |
1306 | struct ust_command *ust_cmd = NULL; | |
1307 | ||
1308 | DBG("[thread] Dispatch UST command started"); | |
1309 | ||
26c9d55e | 1310 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
099e26bd DG |
1311 | /* Atomically prepare the queue futex */ |
1312 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1313 | ||
1314 | do { | |
1315 | /* Dequeue command for registration */ | |
1316 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1317 | if (node == NULL) { | |
00a17c97 | 1318 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1319 | /* Continue thread execution */ |
1320 | break; | |
1321 | } | |
1322 | ||
1323 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1324 | ||
2f50c8a3 DG |
1325 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1326 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1327 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1328 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1329 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1330 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
099e26bd DG |
1331 | /* |
1332 | * Inform apps thread of the new application registration. This | |
1333 | * call is blocking so we can be assured that the data will be read | |
1334 | * at some point in time or wait to the end of the world :) | |
1335 | */ | |
1336 | ret = write(apps_cmd_pipe[1], ust_cmd, | |
1337 | sizeof(struct ust_command)); | |
1338 | if (ret < 0) { | |
76d7553f | 1339 | PERROR("write apps cmd pipe"); |
099e26bd DG |
1340 | if (errno == EBADF) { |
1341 | /* | |
1342 | * We can't inform the application thread to process | |
1343 | * registration. We will exit or else application | |
1344 | * registration will not occur and tracing will never | |
1345 | * start. | |
1346 | */ | |
1347 | goto error; | |
1348 | } | |
1349 | } | |
1350 | free(ust_cmd); | |
1351 | } while (node != NULL); | |
1352 | ||
1353 | /* Futex wait on queue. Blocking call on futex() */ | |
1354 | futex_nto1_wait(&ust_cmd_queue.futex); | |
1355 | } | |
1356 | ||
1357 | error: | |
1358 | DBG("Dispatch thread dying"); | |
1359 | return NULL; | |
1360 | } | |
1361 | ||
1362 | /* | |
1363 | * This thread manage application registration. | |
1364 | */ | |
1365 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1366 | { |
139ac872 | 1367 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1368 | uint32_t revents, nb_fd; |
1369 | struct lttng_poll_event events; | |
099e26bd DG |
1370 | /* |
1371 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1372 | * freed in the manage apps thread. | |
1373 | */ | |
1374 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1375 | |
099e26bd | 1376 | DBG("[thread] Manage application registration started"); |
1d4b027a DG |
1377 | |
1378 | ret = lttcomm_listen_unix_sock(apps_sock); | |
1379 | if (ret < 0) { | |
76d7553f | 1380 | goto error_listen; |
1d4b027a DG |
1381 | } |
1382 | ||
5eb91c98 DG |
1383 | /* |
1384 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1385 | * more will be added to this poll set. | |
1386 | */ | |
1387 | ret = create_thread_poll_set(&events, 2); | |
1388 | if (ret < 0) { | |
76d7553f | 1389 | goto error_create_poll; |
5eb91c98 | 1390 | } |
273ea72c | 1391 | |
5eb91c98 DG |
1392 | /* Add the application registration socket */ |
1393 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1394 | if (ret < 0) { | |
76d7553f | 1395 | goto error_poll_add; |
5eb91c98 | 1396 | } |
273ea72c | 1397 | |
1d4b027a | 1398 | /* Notify all applications to register */ |
0fdd1e2c DG |
1399 | ret = notify_ust_apps(1); |
1400 | if (ret < 0) { | |
1401 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1402 | "Execution continues but there might be problem for already\n" |
1403 | "running applications that wishes to register."); | |
0fdd1e2c | 1404 | } |
1d4b027a DG |
1405 | |
1406 | while (1) { | |
1407 | DBG("Accepting application registration"); | |
273ea72c | 1408 | |
5eb91c98 DG |
1409 | nb_fd = LTTNG_POLL_GETNB(&events); |
1410 | ||
273ea72c | 1411 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 1412 | restart: |
139ac872 | 1413 | health_poll_update(&health_thread_app_reg); |
5eb91c98 | 1414 | ret = lttng_poll_wait(&events, -1); |
139ac872 | 1415 | health_poll_update(&health_thread_app_reg); |
273ea72c | 1416 | if (ret < 0) { |
88f2b785 MD |
1417 | /* |
1418 | * Restart interrupted system call. | |
1419 | */ | |
1420 | if (errno == EINTR) { | |
1421 | goto restart; | |
1422 | } | |
273ea72c DG |
1423 | goto error; |
1424 | } | |
1425 | ||
5eb91c98 | 1426 | for (i = 0; i < nb_fd; i++) { |
139ac872 MD |
1427 | health_code_update(&health_thread_app_reg); |
1428 | ||
5eb91c98 DG |
1429 | /* Fetch once the poll data */ |
1430 | revents = LTTNG_POLL_GETEV(&events, i); | |
1431 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1432 | |
5eb91c98 DG |
1433 | /* Thread quit pipe has been closed. Killing thread. */ |
1434 | ret = check_thread_quit_pipe(pollfd, revents); | |
1435 | if (ret) { | |
139ac872 MD |
1436 | err = 0; |
1437 | goto exit; | |
90014c57 | 1438 | } |
1d4b027a | 1439 | |
5eb91c98 DG |
1440 | /* Event on the registration socket */ |
1441 | if (pollfd == apps_sock) { | |
1442 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1443 | ERR("Register apps socket poll error"); | |
1444 | goto error; | |
1445 | } else if (revents & LPOLLIN) { | |
1446 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1447 | if (sock < 0) { | |
1448 | goto error; | |
1449 | } | |
099e26bd | 1450 | |
5eb91c98 | 1451 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1452 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1453 | if (ust_cmd == NULL) { |
76d7553f | 1454 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
1455 | goto error; |
1456 | } | |
1d4b027a | 1457 | |
5eb91c98 DG |
1458 | /* |
1459 | * Using message-based transmissions to ensure we don't | |
1460 | * have to deal with partially received messages. | |
1461 | */ | |
4063050c MD |
1462 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1463 | if (ret < 0) { | |
1464 | ERR("Exhausted file descriptors allowed for applications."); | |
1465 | free(ust_cmd); | |
1466 | ret = close(sock); | |
1467 | if (ret) { | |
1468 | PERROR("close"); | |
1469 | } | |
1470 | sock = -1; | |
1471 | continue; | |
1472 | } | |
139ac872 | 1473 | health_code_update(&health_thread_app_reg); |
5eb91c98 DG |
1474 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, |
1475 | sizeof(struct ust_register_msg)); | |
1476 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { | |
1477 | if (ret < 0) { | |
76d7553f | 1478 | PERROR("lttcomm_recv_unix_sock register apps"); |
5eb91c98 DG |
1479 | } else { |
1480 | ERR("Wrong size received on apps register"); | |
1481 | } | |
1482 | free(ust_cmd); | |
76d7553f MD |
1483 | ret = close(sock); |
1484 | if (ret) { | |
1485 | PERROR("close"); | |
1486 | } | |
4063050c | 1487 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1488 | sock = -1; |
5eb91c98 DG |
1489 | continue; |
1490 | } | |
139ac872 | 1491 | health_code_update(&health_thread_app_reg); |
099e26bd | 1492 | |
5eb91c98 | 1493 | ust_cmd->sock = sock; |
34a2494f | 1494 | sock = -1; |
099e26bd | 1495 | |
5eb91c98 DG |
1496 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1497 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1498 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1499 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1500 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1501 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1502 | |
5eb91c98 DG |
1503 | /* |
1504 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1505 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1506 | */ |
1507 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1508 | ||
1509 | /* | |
1510 | * Wake the registration queue futex. Implicit memory | |
1511 | * barrier with the exchange in cds_wfq_enqueue. | |
1512 | */ | |
1513 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1514 | } | |
1515 | } | |
90014c57 | 1516 | } |
1d4b027a DG |
1517 | } |
1518 | ||
139ac872 | 1519 | exit: |
1d4b027a | 1520 | error: |
139ac872 MD |
1521 | if (err) { |
1522 | health_error(&health_thread_app_reg); | |
1523 | ERR("Health error occurred in %s", __func__); | |
1524 | } | |
1525 | health_exit(&health_thread_app_reg); | |
1526 | ||
0fdd1e2c DG |
1527 | /* Notify that the registration thread is gone */ |
1528 | notify_ust_apps(0); | |
1529 | ||
a4b35e07 | 1530 | if (apps_sock >= 0) { |
76d7553f MD |
1531 | ret = close(apps_sock); |
1532 | if (ret) { | |
1533 | PERROR("close"); | |
1534 | } | |
a4b35e07 | 1535 | } |
46c3f085 | 1536 | if (sock >= 0) { |
76d7553f MD |
1537 | ret = close(sock); |
1538 | if (ret) { | |
1539 | PERROR("close"); | |
1540 | } | |
4063050c | 1541 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1542 | } |
273ea72c | 1543 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1544 | |
76d7553f | 1545 | error_poll_add: |
5eb91c98 | 1546 | lttng_poll_clean(&events); |
76d7553f MD |
1547 | error_listen: |
1548 | error_create_poll: | |
1549 | DBG("UST Registration thread cleanup complete"); | |
5eb91c98 | 1550 | |
1d4b027a DG |
1551 | return NULL; |
1552 | } | |
1553 | ||
8c0faa1d | 1554 | /* |
3bd1e081 | 1555 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1556 | * exec or it will fails. |
8c0faa1d | 1557 | */ |
3bd1e081 | 1558 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d DG |
1559 | { |
1560 | int ret; | |
ee0b0061 DG |
1561 | struct timespec timeout; |
1562 | ||
1563 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; | |
1564 | timeout.tv_nsec = 0; | |
8c0faa1d DG |
1565 | |
1566 | /* Setup semaphore */ | |
3bd1e081 | 1567 | ret = sem_init(&consumer_data->sem, 0, 0); |
ee0b0061 | 1568 | if (ret < 0) { |
3bd1e081 | 1569 | PERROR("sem_init consumer semaphore"); |
ee0b0061 DG |
1570 | goto error; |
1571 | } | |
8c0faa1d | 1572 | |
3bd1e081 MD |
1573 | ret = pthread_create(&consumer_data->thread, NULL, |
1574 | thread_manage_consumer, consumer_data); | |
8c0faa1d | 1575 | if (ret != 0) { |
3bd1e081 | 1576 | PERROR("pthread_create consumer"); |
ee0b0061 | 1577 | ret = -1; |
8c0faa1d DG |
1578 | goto error; |
1579 | } | |
1580 | ||
ee0b0061 DG |
1581 | /* Get time for sem_timedwait absolute timeout */ |
1582 | ret = clock_gettime(CLOCK_REALTIME, &timeout); | |
1583 | if (ret < 0) { | |
3bd1e081 | 1584 | PERROR("clock_gettime spawn consumer"); |
ee0b0061 | 1585 | /* Infinite wait for the kconsumerd thread to be ready */ |
3bd1e081 | 1586 | ret = sem_wait(&consumer_data->sem); |
ee0b0061 DG |
1587 | } else { |
1588 | /* Normal timeout if the gettime was successful */ | |
1589 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
3bd1e081 | 1590 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
ee0b0061 | 1591 | } |
8c0faa1d | 1592 | |
ee0b0061 DG |
1593 | if (ret < 0) { |
1594 | if (errno == ETIMEDOUT) { | |
1595 | /* | |
1596 | * Call has timed out so we kill the kconsumerd_thread and return | |
1597 | * an error. | |
1598 | */ | |
3bd1e081 MD |
1599 | ERR("The consumer thread was never ready. Killing it"); |
1600 | ret = pthread_cancel(consumer_data->thread); | |
ee0b0061 | 1601 | if (ret < 0) { |
3bd1e081 | 1602 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
1603 | } |
1604 | } else { | |
3bd1e081 | 1605 | PERROR("semaphore wait failed consumer thread"); |
ee0b0061 DG |
1606 | } |
1607 | goto error; | |
1608 | } | |
1609 | ||
3bd1e081 MD |
1610 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1611 | if (consumer_data->pid == 0) { | |
712ea556 | 1612 | ERR("Kconsumerd did not start"); |
3bd1e081 | 1613 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
1614 | goto error; |
1615 | } | |
3bd1e081 | 1616 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 1617 | |
8c0faa1d DG |
1618 | return 0; |
1619 | ||
1620 | error: | |
1621 | return ret; | |
1622 | } | |
1623 | ||
d9800920 | 1624 | /* |
3bd1e081 | 1625 | * Join consumer thread |
d9800920 | 1626 | */ |
3bd1e081 | 1627 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
1628 | { |
1629 | void *status; | |
1630 | int ret; | |
1631 | ||
e8209f6b DG |
1632 | /* Consumer pid must be a real one. */ |
1633 | if (consumer_data->pid > 0) { | |
3bd1e081 | 1634 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 1635 | if (ret) { |
3bd1e081 | 1636 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
1637 | return ret; |
1638 | } | |
3bd1e081 | 1639 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
1640 | } else { |
1641 | return 0; | |
1642 | } | |
1643 | } | |
1644 | ||
8c0faa1d | 1645 | /* |
3bd1e081 | 1646 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 1647 | * |
d063d709 | 1648 | * Return pid if successful else -1. |
8c0faa1d | 1649 | */ |
3bd1e081 | 1650 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
1651 | { |
1652 | int ret; | |
1653 | pid_t pid; | |
94c55f17 | 1654 | const char *consumer_to_use; |
53086306 | 1655 | const char *verbosity; |
94c55f17 | 1656 | struct stat st; |
8c0faa1d | 1657 | |
3bd1e081 | 1658 | DBG("Spawning consumerd"); |
c49dc785 | 1659 | |
8c0faa1d DG |
1660 | pid = fork(); |
1661 | if (pid == 0) { | |
1662 | /* | |
3bd1e081 | 1663 | * Exec consumerd. |
8c0faa1d | 1664 | */ |
daee5345 | 1665 | if (opt_verbose_consumer) { |
53086306 DG |
1666 | verbosity = "--verbose"; |
1667 | } else { | |
1668 | verbosity = "--quiet"; | |
1669 | } | |
3bd1e081 MD |
1670 | switch (consumer_data->type) { |
1671 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 1672 | /* |
c7704d57 DG |
1673 | * Find out which consumerd to execute. We will first try the |
1674 | * 64-bit path, then the sessiond's installation directory, and | |
1675 | * fallback on the 32-bit one, | |
94c55f17 | 1676 | */ |
63a799e8 AM |
1677 | DBG3("Looking for a kernel consumer at these locations:"); |
1678 | DBG3(" 1) %s", consumerd64_bin); | |
1679 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
1680 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 1681 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 1682 | DBG3("Found location #1"); |
94c55f17 | 1683 | consumer_to_use = consumerd64_bin; |
94c55f17 | 1684 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 1685 | DBG3("Found location #2"); |
94c55f17 | 1686 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 1687 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 1688 | DBG3("Found location #3"); |
eb1e0bd4 | 1689 | consumer_to_use = consumerd32_bin; |
94c55f17 | 1690 | } else { |
63a799e8 | 1691 | DBG("Could not find any valid consumerd executable"); |
94c55f17 AM |
1692 | break; |
1693 | } | |
1694 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
1695 | execl(consumer_to_use, | |
1696 | "lttng-consumerd", verbosity, "-k", | |
1697 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
1698 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1699 | NULL); | |
3bd1e081 | 1700 | break; |
7753dea8 MD |
1701 | case LTTNG_CONSUMER64_UST: |
1702 | { | |
b1e0b6b6 | 1703 | char *tmpnew = NULL; |
8f4905da MD |
1704 | |
1705 | if (consumerd64_libdir[0] != '\0') { | |
1706 | char *tmp; | |
1707 | size_t tmplen; | |
1708 | ||
1709 | tmp = getenv("LD_LIBRARY_PATH"); | |
1710 | if (!tmp) { | |
1711 | tmp = ""; | |
1712 | } | |
1713 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1714 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
1715 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1716 | if (!tmpnew) { | |
1717 | ret = -ENOMEM; | |
1718 | goto error; | |
1719 | } | |
1720 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1721 | strcat(tmpnew, consumerd64_libdir); | |
1722 | if (tmp[0] != '\0') { | |
1723 | strcat(tmpnew, ":"); | |
1724 | strcat(tmpnew, tmp); | |
1725 | } | |
1726 | ret = putenv(tmpnew); | |
1727 | if (ret) { | |
1728 | ret = -errno; | |
1729 | goto error; | |
1730 | } | |
1731 | } | |
94c55f17 | 1732 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 1733 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1734 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1735 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1736 | NULL); | |
8f4905da MD |
1737 | if (consumerd64_libdir[0] != '\0') { |
1738 | free(tmpnew); | |
1739 | } | |
1740 | if (ret) { | |
1741 | goto error; | |
1742 | } | |
3bd1e081 | 1743 | break; |
7753dea8 MD |
1744 | } |
1745 | case LTTNG_CONSUMER32_UST: | |
1746 | { | |
937dde8e | 1747 | char *tmpnew = NULL; |
8f4905da MD |
1748 | |
1749 | if (consumerd32_libdir[0] != '\0') { | |
1750 | char *tmp; | |
1751 | size_t tmplen; | |
1752 | ||
1753 | tmp = getenv("LD_LIBRARY_PATH"); | |
1754 | if (!tmp) { | |
1755 | tmp = ""; | |
1756 | } | |
1757 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1758 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
1759 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1760 | if (!tmpnew) { | |
1761 | ret = -ENOMEM; | |
1762 | goto error; | |
1763 | } | |
1764 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1765 | strcat(tmpnew, consumerd32_libdir); | |
1766 | if (tmp[0] != '\0') { | |
1767 | strcat(tmpnew, ":"); | |
1768 | strcat(tmpnew, tmp); | |
1769 | } | |
1770 | ret = putenv(tmpnew); | |
1771 | if (ret) { | |
1772 | ret = -errno; | |
1773 | goto error; | |
1774 | } | |
1775 | } | |
94c55f17 | 1776 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 1777 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1778 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1779 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1780 | NULL); | |
8f4905da MD |
1781 | if (consumerd32_libdir[0] != '\0') { |
1782 | free(tmpnew); | |
1783 | } | |
1784 | if (ret) { | |
1785 | goto error; | |
1786 | } | |
7753dea8 MD |
1787 | break; |
1788 | } | |
3bd1e081 | 1789 | default: |
76d7553f | 1790 | PERROR("unknown consumer type"); |
3bd1e081 MD |
1791 | exit(EXIT_FAILURE); |
1792 | } | |
8c0faa1d | 1793 | if (errno != 0) { |
76d7553f | 1794 | PERROR("kernel start consumer exec"); |
8c0faa1d DG |
1795 | } |
1796 | exit(EXIT_FAILURE); | |
1797 | } else if (pid > 0) { | |
1798 | ret = pid; | |
8c0faa1d | 1799 | } else { |
76d7553f | 1800 | PERROR("start consumer fork"); |
8c0faa1d | 1801 | ret = -errno; |
8c0faa1d | 1802 | } |
8f4905da | 1803 | error: |
8c0faa1d DG |
1804 | return ret; |
1805 | } | |
1806 | ||
693bd40b | 1807 | /* |
3bd1e081 | 1808 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 1809 | */ |
3bd1e081 | 1810 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b DG |
1811 | { |
1812 | int ret; | |
1813 | ||
3bd1e081 MD |
1814 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1815 | if (consumer_data->pid != 0) { | |
1816 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
1817 | goto end; |
1818 | } | |
693bd40b | 1819 | |
3bd1e081 | 1820 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 1821 | if (ret < 0) { |
3bd1e081 MD |
1822 | ERR("Spawning consumerd failed"); |
1823 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 1824 | goto error; |
693bd40b | 1825 | } |
c49dc785 | 1826 | |
3bd1e081 MD |
1827 | /* Setting up the consumer_data pid */ |
1828 | consumer_data->pid = ret; | |
48842b30 | 1829 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 1830 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 1831 | |
3bd1e081 MD |
1832 | DBG2("Spawning consumer control thread"); |
1833 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 1834 | if (ret < 0) { |
3bd1e081 | 1835 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
1836 | goto error; |
1837 | } | |
1838 | ||
c49dc785 | 1839 | end: |
693bd40b DG |
1840 | return 0; |
1841 | ||
1842 | error: | |
1843 | return ret; | |
1844 | } | |
1845 | ||
44a5e5eb | 1846 | /* |
139ac872 MD |
1847 | * Compute health status of each consumer. If one of them is zero (bad |
1848 | * state), we return 0. | |
44a5e5eb DG |
1849 | */ |
1850 | static int check_consumer_health(void) | |
1851 | { | |
1852 | int ret; | |
1853 | ||
139ac872 MD |
1854 | ret = health_check_state(&kconsumer_data.health) && |
1855 | health_check_state(&ustconsumer32_data.health) && | |
44a5e5eb DG |
1856 | health_check_state(&ustconsumer64_data.health); |
1857 | ||
1858 | DBG3("Health consumer check %d", ret); | |
1859 | ||
1860 | return ret; | |
1861 | } | |
1862 | ||
b73401da | 1863 | /* |
096102bd | 1864 | * Check version of the lttng-modules. |
b73401da | 1865 | */ |
096102bd | 1866 | static int validate_lttng_modules_version(void) |
b73401da | 1867 | { |
096102bd | 1868 | return kernel_validate_version(kernel_tracer_fd); |
ab147185 MD |
1869 | } |
1870 | ||
b73401da | 1871 | /* |
096102bd | 1872 | * Setup necessary data for kernel tracer action. |
b73401da | 1873 | */ |
096102bd | 1874 | static int init_kernel_tracer(void) |
b73401da DG |
1875 | { |
1876 | int ret; | |
b73401da | 1877 | |
096102bd DG |
1878 | /* Modprobe lttng kernel modules */ |
1879 | ret = modprobe_lttng_control(); | |
b73401da | 1880 | if (ret < 0) { |
b73401da DG |
1881 | goto error; |
1882 | } | |
1883 | ||
096102bd DG |
1884 | /* Open debugfs lttng */ |
1885 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
1886 | if (kernel_tracer_fd < 0) { | |
1887 | DBG("Failed to open %s", module_proc_lttng); | |
1888 | ret = -1; | |
1889 | goto error_open; | |
b73401da DG |
1890 | } |
1891 | ||
096102bd DG |
1892 | /* Validate kernel version */ |
1893 | ret = validate_lttng_modules_version(); | |
b73401da | 1894 | if (ret < 0) { |
096102bd | 1895 | goto error_version; |
b73401da DG |
1896 | } |
1897 | ||
096102bd | 1898 | ret = modprobe_lttng_data(); |
b73401da | 1899 | if (ret < 0) { |
096102bd | 1900 | goto error_modules; |
8c0faa1d DG |
1901 | } |
1902 | ||
f3ed775e | 1903 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
096102bd DG |
1904 | return 0; |
1905 | ||
1906 | error_version: | |
1907 | modprobe_remove_lttng_control(); | |
76d7553f MD |
1908 | ret = close(kernel_tracer_fd); |
1909 | if (ret) { | |
1910 | PERROR("close"); | |
1911 | } | |
1912 | kernel_tracer_fd = -1; | |
096102bd DG |
1913 | return LTTCOMM_KERN_VERSION; |
1914 | ||
1915 | error_modules: | |
76d7553f MD |
1916 | ret = close(kernel_tracer_fd); |
1917 | if (ret) { | |
1918 | PERROR("close"); | |
1919 | } | |
096102bd DG |
1920 | |
1921 | error_open: | |
1922 | modprobe_remove_lttng_control(); | |
b73401da DG |
1923 | |
1924 | error: | |
b73401da | 1925 | WARN("No kernel tracer available"); |
a4b35e07 | 1926 | kernel_tracer_fd = -1; |
531d29f9 MD |
1927 | if (!is_root) { |
1928 | return LTTCOMM_NEED_ROOT_SESSIOND; | |
1929 | } else { | |
1930 | return LTTCOMM_KERN_NA; | |
1931 | } | |
f3ed775e | 1932 | } |
33a2b854 | 1933 | |
f3ed775e | 1934 | /* |
54d01ffb | 1935 | * Init tracing by creating trace directory and sending fds kernel consumer. |
f3ed775e | 1936 | */ |
54d01ffb | 1937 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
f3ed775e | 1938 | { |
f40799e8 | 1939 | int ret = 0; |
173af62f DG |
1940 | struct lttng_ht_iter iter; |
1941 | struct consumer_socket *socket; | |
8c0faa1d | 1942 | |
173af62f | 1943 | assert(session); |
d9800920 | 1944 | |
173af62f DG |
1945 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
1946 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
1947 | socket, node.node) { | |
1948 | /* Code flow error */ | |
1949 | assert(socket->fd >= 0); | |
1950 | ||
1951 | pthread_mutex_lock(socket->lock); | |
1952 | ret = kernel_consumer_send_session(socket->fd, session); | |
1953 | pthread_mutex_unlock(socket->lock); | |
1954 | if (ret < 0) { | |
1955 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
1956 | goto error; | |
1957 | } | |
f3ed775e | 1958 | } |
00e2e675 | 1959 | } |
1d4b027a | 1960 | |
00e2e675 DG |
1961 | error: |
1962 | return ret; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Create a socket to the relayd using the URI. | |
1967 | * | |
1968 | * On success, the relayd_sock pointer is set to the created socket. | |
1969 | * Else, it is untouched and an lttcomm error code is returned. | |
1970 | */ | |
1971 | static int create_connect_relayd(struct consumer_output *output, | |
1972 | const char *session_name, struct lttng_uri *uri, | |
1973 | struct lttcomm_sock **relayd_sock) | |
1974 | { | |
1975 | int ret; | |
1976 | struct lttcomm_sock *sock; | |
1977 | ||
1978 | /* Create socket object from URI */ | |
1979 | sock = lttcomm_alloc_sock_from_uri(uri); | |
1980 | if (sock == NULL) { | |
1981 | ret = LTTCOMM_FATAL; | |
1982 | goto error; | |
1983 | } | |
1984 | ||
1985 | ret = lttcomm_create_sock(sock); | |
1986 | if (ret < 0) { | |
1987 | ret = LTTCOMM_FATAL; | |
1988 | goto error; | |
1989 | } | |
1990 | ||
1991 | /* Connect to relayd so we can proceed with a session creation. */ | |
1992 | ret = relayd_connect(sock); | |
1993 | if (ret < 0) { | |
1994 | ERR("Unable to reach lttng-relayd"); | |
1995 | ret = LTTCOMM_RELAYD_SESSION_FAIL; | |
1996 | goto free_sock; | |
f3ed775e | 1997 | } |
1d4b027a | 1998 | |
00e2e675 DG |
1999 | /* Create socket for control stream. */ |
2000 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
2001 | DBG3("Creating relayd stream socket from URI"); | |
2002 | ||
2003 | /* Check relayd version */ | |
0a6b5085 DG |
2004 | ret = relayd_version_check(sock, RELAYD_VERSION_COMM_MAJOR, |
2005 | RELAYD_VERSION_COMM_MINOR); | |
00e2e675 DG |
2006 | if (ret < 0) { |
2007 | ret = LTTCOMM_RELAYD_VERSION_FAIL; | |
2008 | goto close_sock; | |
2009 | } | |
2010 | } else if (uri->stype == LTTNG_STREAM_DATA) { | |
2011 | DBG3("Creating relayd data socket from URI"); | |
2012 | } else { | |
2013 | /* Command is not valid */ | |
2014 | ERR("Relayd invalid stream type: %d", uri->stype); | |
2015 | ret = LTTCOMM_INVALID; | |
2016 | goto close_sock; | |
2017 | } | |
2018 | ||
2019 | *relayd_sock = sock; | |
2020 | ||
2021 | return LTTCOMM_OK; | |
2022 | ||
2023 | close_sock: | |
2024 | if (sock) { | |
2025 | (void) relayd_close(sock); | |
2026 | } | |
2027 | free_sock: | |
2028 | if (sock) { | |
2029 | lttcomm_destroy_sock(sock); | |
2030 | } | |
2031 | error: | |
2032 | return ret; | |
2033 | } | |
2034 | ||
2035 | /* | |
2036 | * Connect to the relayd using URI and send the socket to the right consumer. | |
2037 | */ | |
2038 | static int send_socket_relayd_consumer(int domain, struct ltt_session *session, | |
2039 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, | |
2040 | int consumer_fd) | |
2041 | { | |
2042 | int ret; | |
2043 | struct lttcomm_sock *sock = NULL; | |
2044 | ||
2045 | /* Set the network sequence index if not set. */ | |
2046 | if (consumer->net_seq_index == -1) { | |
2047 | /* | |
2048 | * Increment net_seq_idx because we are about to transfer the | |
2049 | * new relayd socket to the consumer. | |
2050 | */ | |
2051 | uatomic_inc(&relayd_net_seq_idx); | |
2052 | /* Assign unique key so the consumer can match streams */ | |
2053 | consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx); | |
2054 | } | |
2055 | ||
2056 | /* Connect to relayd and make version check if uri is the control. */ | |
2057 | ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock); | |
2058 | if (ret != LTTCOMM_OK) { | |
2059 | goto close_sock; | |
2060 | } | |
2061 | ||
2062 | /* If the control socket is connected, network session is ready */ | |
2063 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
2064 | session->net_handle = 1; | |
2065 | } | |
2066 | ||
37278a1e DG |
2067 | /* Send relayd socket to consumer. */ |
2068 | ret = consumer_send_relayd_socket(consumer_fd, sock, | |
2069 | consumer, relayd_uri->stype); | |
2070 | if (ret < 0) { | |
2071 | ret = LTTCOMM_ENABLE_CONSUMER_FAIL; | |
2072 | goto close_sock; | |
00e2e675 DG |
2073 | } |
2074 | ||
2075 | ret = LTTCOMM_OK; | |
2076 | ||
2077 | /* | |
2078 | * Close socket which was dup on the consumer side. The session daemon does | |
2079 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
2080 | */ | |
2081 | ||
2082 | close_sock: | |
2083 | if (sock) { | |
2084 | (void) relayd_close(sock); | |
2085 | lttcomm_destroy_sock(sock); | |
2086 | } | |
2087 | ||
2088 | return ret; | |
2089 | } | |
2090 | ||
2091 | /* | |
2092 | * Send both relayd sockets to a specific consumer and domain. This is a | |
2093 | * helper function to facilitate sending the information to the consumer for a | |
2094 | * session. | |
2095 | */ | |
2096 | static int send_sockets_relayd_consumer(int domain, | |
2097 | struct ltt_session *session, struct consumer_output *consumer, int fd) | |
2098 | { | |
2099 | int ret; | |
2100 | ||
3f8e211f DG |
2101 | assert(session); |
2102 | assert(consumer); | |
2103 | ||
2104 | /* Don't resend the sockets to the consumer. */ | |
2105 | if (consumer->dst.net.relayd_socks_sent) { | |
2106 | ret = LTTCOMM_OK; | |
2107 | goto error; | |
2108 | } | |
2109 | ||
00e2e675 DG |
2110 | /* Sending control relayd socket. */ |
2111 | ret = send_socket_relayd_consumer(domain, session, | |
2112 | &consumer->dst.net.control, consumer, fd); | |
2113 | if (ret != LTTCOMM_OK) { | |
2114 | goto error; | |
2115 | } | |
2116 | ||
2117 | /* Sending data relayd socket. */ | |
2118 | ret = send_socket_relayd_consumer(domain, session, | |
2119 | &consumer->dst.net.data, consumer, fd); | |
2120 | if (ret != LTTCOMM_OK) { | |
2121 | goto error; | |
2122 | } | |
2123 | ||
3f8e211f DG |
2124 | /* Flag that all relayd sockets were sent to the consumer. */ |
2125 | consumer->dst.net.relayd_socks_sent = 1; | |
2126 | ||
00e2e675 DG |
2127 | error: |
2128 | return ret; | |
2129 | } | |
2130 | ||
2131 | /* | |
2132 | * Setup relayd connections for a tracing session. First creates the socket to | |
2133 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
2134 | * network. | |
2135 | */ | |
2136 | static int setup_relayd(struct ltt_session *session) | |
2137 | { | |
2138 | int ret = LTTCOMM_OK; | |
2139 | struct ltt_ust_session *usess; | |
2140 | struct ltt_kernel_session *ksess; | |
173af62f DG |
2141 | struct consumer_socket *socket; |
2142 | struct lttng_ht_iter iter; | |
00e2e675 DG |
2143 | |
2144 | assert(session); | |
2145 | ||
2146 | usess = session->ust_session; | |
2147 | ksess = session->kernel_session; | |
2148 | ||
2149 | DBG2("Setting relayd for session %s", session->name); | |
2150 | ||
a4b92340 DG |
2151 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
2152 | && usess->consumer->enabled) { | |
173af62f DG |
2153 | /* For each consumer socket, send relayd sockets */ |
2154 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
2155 | socket, node.node) { | |
2156 | /* Code flow error */ | |
2157 | assert(socket->fd >= 0); | |
2158 | ||
2159 | pthread_mutex_lock(socket->lock); | |
00e2e675 | 2160 | send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session, |
173af62f DG |
2161 | usess->consumer, socket->fd); |
2162 | pthread_mutex_unlock(socket->lock); | |
00e2e675 DG |
2163 | if (ret != LTTCOMM_OK) { |
2164 | goto error; | |
2165 | } | |
2166 | } | |
a4b92340 DG |
2167 | } |
2168 | ||
2169 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
2170 | && ksess->consumer->enabled) { | |
173af62f DG |
2171 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
2172 | socket, node.node) { | |
2173 | /* Code flow error */ | |
2174 | assert(socket->fd >= 0); | |
2175 | ||
2176 | pthread_mutex_lock(socket->lock); | |
2177 | send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session, | |
2178 | ksess->consumer, socket->fd); | |
2179 | pthread_mutex_unlock(socket->lock); | |
00e2e675 DG |
2180 | if (ret != LTTCOMM_OK) { |
2181 | goto error; | |
2182 | } | |
2183 | } | |
00e2e675 DG |
2184 | } |
2185 | ||
2186 | error: | |
2187 | return ret; | |
2188 | } | |
2189 | ||
07424f16 DG |
2190 | /* |
2191 | * Set consumer subdirectory using the session name and a generated datetime if | |
2192 | * needed. This is appended to the current subdirectory. | |
2193 | */ | |
2194 | static int set_consumer_subdir(struct consumer_output *consumer, | |
2195 | const char *session_name) | |
2196 | { | |
2197 | int ret = 0; | |
2198 | unsigned int have_default_name = 0; | |
2199 | char datetime[16], tmp_path[PATH_MAX]; | |
2200 | time_t rawtime; | |
2201 | struct tm *timeinfo; | |
2202 | ||
2203 | assert(consumer); | |
2204 | assert(session_name); | |
2205 | ||
2206 | memset(tmp_path, 0, sizeof(tmp_path)); | |
2207 | ||
2208 | /* Flag if we have a default session. */ | |
2209 | if (strncmp(session_name, DEFAULT_SESSION_NAME "-", | |
2210 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) { | |
2211 | have_default_name = 1; | |
2212 | } else { | |
2213 | /* Get date and time for session path */ | |
2214 | time(&rawtime); | |
2215 | timeinfo = localtime(&rawtime); | |
2216 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
2217 | } | |
2218 | ||
2219 | if (have_default_name) { | |
2220 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
2221 | "%s/%s", consumer->subdir, session_name); | |
2222 | } else { | |
2223 | ret = snprintf(tmp_path, sizeof(tmp_path), | |
2224 | "%s/%s-%s/", consumer->subdir, session_name, datetime); | |
2225 | } | |
2226 | if (ret < 0) { | |
2227 | PERROR("snprintf session name date"); | |
2228 | goto error; | |
2229 | } | |
2230 | ||
2231 | strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir)); | |
2232 | DBG2("Consumer subdir set to %s", consumer->subdir); | |
2233 | ||
2234 | error: | |
2235 | return ret; | |
2236 | } | |
2237 | ||
00e2e675 DG |
2238 | /* |
2239 | * Copy consumer output from the tracing session to the domain session. The | |
2240 | * function also applies the right modification on a per domain basis for the | |
2241 | * trace files destination directory. | |
2242 | */ | |
2243 | static int copy_session_consumer(int domain, struct ltt_session *session) | |
2244 | { | |
2245 | int ret; | |
2246 | const char *dir_name; | |
2247 | struct consumer_output *consumer; | |
2248 | ||
a4b92340 DG |
2249 | assert(session); |
2250 | assert(session->consumer); | |
2251 | ||
00e2e675 DG |
2252 | switch (domain) { |
2253 | case LTTNG_DOMAIN_KERNEL: | |
2254 | DBG3("Copying tracing session consumer output in kernel session"); | |
2255 | session->kernel_session->consumer = | |
2256 | consumer_copy_output(session->consumer); | |
2257 | /* Ease our life a bit for the next part */ | |
2258 | consumer = session->kernel_session->consumer; | |
2259 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
2260 | break; | |
2261 | case LTTNG_DOMAIN_UST: | |
2262 | DBG3("Copying tracing session consumer output in UST session"); | |
2263 | session->ust_session->consumer = | |
2264 | consumer_copy_output(session->consumer); | |
2265 | /* Ease our life a bit for the next part */ | |
2266 | consumer = session->ust_session->consumer; | |
2267 | dir_name = DEFAULT_UST_TRACE_DIR; | |
2268 | break; | |
2269 | default: | |
2270 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2271 | goto error; | |
2272 | } | |
2273 | ||
07424f16 DG |
2274 | ret = set_consumer_subdir(session->consumer, session->name); |
2275 | if (ret < 0) { | |
2276 | ret = LTTCOMM_FATAL; | |
2277 | goto error; | |
2278 | } | |
2279 | ||
00e2e675 DG |
2280 | /* Append correct directory to subdir */ |
2281 | strncat(consumer->subdir, dir_name, sizeof(consumer->subdir)); | |
2282 | DBG3("Copy session consumer subdir %s", consumer->subdir); | |
2283 | ||
00e2e675 DG |
2284 | ret = LTTCOMM_OK; |
2285 | ||
1d4b027a DG |
2286 | error: |
2287 | return ret; | |
8c0faa1d DG |
2288 | } |
2289 | ||
0177d773 DG |
2290 | /* |
2291 | * Create an UST session and add it to the session ust list. | |
2292 | */ | |
44d3bd01 | 2293 | static int create_ust_session(struct ltt_session *session, |
6df2e2c9 | 2294 | struct lttng_domain *domain) |
0177d773 | 2295 | { |
6df2e2c9 | 2296 | int ret; |
00e2e675 DG |
2297 | struct ltt_ust_session *lus = NULL; |
2298 | ||
2299 | assert(session); | |
a4b92340 | 2300 | assert(domain); |
00e2e675 | 2301 | assert(session->consumer); |
44d3bd01 DG |
2302 | |
2303 | switch (domain->type) { | |
48842b30 | 2304 | case LTTNG_DOMAIN_UST: |
44d3bd01 DG |
2305 | break; |
2306 | default: | |
00e2e675 | 2307 | ERR("Unknown UST domain on create session %d", domain->type); |
13384287 | 2308 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
44d3bd01 DG |
2309 | goto error; |
2310 | } | |
0177d773 DG |
2311 | |
2312 | DBG("Creating UST session"); | |
2313 | ||
a991f516 | 2314 | lus = trace_ust_create_session(session->path, session->id, domain); |
0177d773 | 2315 | if (lus == NULL) { |
44d3bd01 | 2316 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
2317 | goto error; |
2318 | } | |
2319 | ||
6df2e2c9 MD |
2320 | lus->uid = session->uid; |
2321 | lus->gid = session->gid; | |
f6a9efaa | 2322 | session->ust_session = lus; |
44d3bd01 | 2323 | |
00e2e675 DG |
2324 | /* Copy session output to the newly created UST session */ |
2325 | ret = copy_session_consumer(domain->type, session); | |
2326 | if (ret != LTTCOMM_OK) { | |
2327 | goto error; | |
2328 | } | |
2329 | ||
44d3bd01 | 2330 | return LTTCOMM_OK; |
0177d773 DG |
2331 | |
2332 | error: | |
2333 | free(lus); | |
00e2e675 | 2334 | session->ust_session = NULL; |
0177d773 DG |
2335 | return ret; |
2336 | } | |
2337 | ||
333f285e | 2338 | /* |
d063d709 | 2339 | * Create a kernel tracer session then create the default channel. |
333f285e | 2340 | */ |
6df2e2c9 | 2341 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 2342 | { |
f3ed775e | 2343 | int ret; |
f3ed775e DG |
2344 | |
2345 | DBG("Creating kernel session"); | |
2346 | ||
2347 | ret = kernel_create_session(session, kernel_tracer_fd); | |
2348 | if (ret < 0) { | |
2349 | ret = LTTCOMM_KERN_SESS_FAIL; | |
2350 | goto error; | |
333f285e DG |
2351 | } |
2352 | ||
a4b92340 DG |
2353 | /* Code flow safety */ |
2354 | assert(session->kernel_session); | |
2355 | ||
00e2e675 DG |
2356 | /* Copy session output to the newly created Kernel session */ |
2357 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
2358 | if (ret != LTTCOMM_OK) { | |
2359 | goto error; | |
2360 | } | |
2361 | ||
2362 | /* Create directory(ies) on local filesystem. */ | |
a4b92340 DG |
2363 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && |
2364 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
00e2e675 DG |
2365 | ret = run_as_mkdir_recursive( |
2366 | session->kernel_session->consumer->dst.trace_path, | |
2367 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2368 | if (ret < 0) { | |
2369 | if (ret != -EEXIST) { | |
2370 | ERR("Trace directory creation error"); | |
2371 | goto error; | |
2372 | } | |
7a485870 DG |
2373 | } |
2374 | } | |
00e2e675 | 2375 | |
6df2e2c9 MD |
2376 | session->kernel_session->uid = session->uid; |
2377 | session->kernel_session->gid = session->gid; | |
7a485870 | 2378 | |
00e2e675 DG |
2379 | return LTTCOMM_OK; |
2380 | ||
f3ed775e | 2381 | error: |
00e2e675 DG |
2382 | trace_kernel_destroy_session(session->kernel_session); |
2383 | session->kernel_session = NULL; | |
f3ed775e | 2384 | return ret; |
333f285e DG |
2385 | } |
2386 | ||
8e0af1b4 | 2387 | /* |
c7704d57 DG |
2388 | * Check if the UID or GID match the session. Root user has access to all |
2389 | * sessions. | |
8e0af1b4 | 2390 | */ |
c7704d57 | 2391 | static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid) |
8e0af1b4 | 2392 | { |
c7704d57 | 2393 | if (uid != session->uid && gid != session->gid && uid != 0) { |
8e0af1b4 MD |
2394 | return 0; |
2395 | } else { | |
2396 | return 1; | |
2397 | } | |
2398 | } | |
2399 | ||
00e2e675 DG |
2400 | /* |
2401 | * Count number of session permitted by uid/gid. | |
2402 | */ | |
8e0af1b4 MD |
2403 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) |
2404 | { | |
2405 | unsigned int i = 0; | |
2406 | struct ltt_session *session; | |
2407 | ||
2408 | DBG("Counting number of available session for UID %d GID %d", | |
2409 | uid, gid); | |
2410 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
2411 | /* | |
2412 | * Only list the sessions the user can control. | |
2413 | */ | |
2414 | if (!session_access_ok(session, uid, gid)) { | |
2415 | continue; | |
2416 | } | |
2417 | i++; | |
2418 | } | |
2419 | return i; | |
2420 | } | |
2421 | ||
ad20f474 DG |
2422 | /* |
2423 | * Create a session path used by list_lttng_sessions for the case that the | |
2424 | * session consumer is on the network. | |
2425 | */ | |
2426 | static int build_network_session_path(char *dst, size_t size, | |
2427 | struct ltt_session *session) | |
2428 | { | |
2429 | int ret, kdata_port, udata_port; | |
2430 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
2431 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
2432 | ||
2433 | assert(session); | |
2434 | assert(dst); | |
2435 | ||
2436 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
2437 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
2438 | ||
2439 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
2440 | ||
2441 | if (session->kernel_session && session->kernel_session->consumer) { | |
2442 | kuri = &session->kernel_session->consumer->dst.net.control; | |
2443 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
2444 | } | |
2445 | ||
2446 | if (session->ust_session && session->ust_session->consumer) { | |
2447 | uuri = &session->ust_session->consumer->dst.net.control; | |
2448 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
2449 | } | |
2450 | ||
2451 | if (uuri == NULL && kuri == NULL) { | |
2452 | uri = &session->consumer->dst.net.control; | |
2453 | kdata_port = session->consumer->dst.net.data.port; | |
2454 | } else if (kuri && uuri) { | |
2455 | ret = uri_compare(kuri, uuri); | |
2456 | if (ret) { | |
2457 | /* Not Equal */ | |
2458 | uri = kuri; | |
2459 | /* Build uuri URL string */ | |
2460 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
2461 | if (ret < 0) { | |
2462 | goto error; | |
2463 | } | |
2464 | } else { | |
2465 | uri = kuri; | |
2466 | } | |
2467 | } else if (kuri && uuri == NULL) { | |
2468 | uri = kuri; | |
2469 | } else if (uuri && kuri == NULL) { | |
2470 | uri = uuri; | |
2471 | } | |
2472 | ||
2473 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
2474 | if (ret < 0) { | |
2475 | goto error; | |
2476 | } | |
2477 | ||
2478 | if (strlen(tmp_uurl) > 0) { | |
2479 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", | |
2480 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
2481 | } else { | |
2482 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, kdata_port); | |
2483 | } | |
2484 | ||
2485 | error: | |
2486 | return ret; | |
2487 | } | |
2488 | ||
6c9cc2ab DG |
2489 | /* |
2490 | * Using the session list, filled a lttng_session array to send back to the | |
2491 | * client for session listing. | |
2492 | * | |
2493 | * The session list lock MUST be acquired before calling this function. Use | |
54d01ffb | 2494 | * session_lock_list() and session_unlock_list(). |
6c9cc2ab | 2495 | */ |
c7704d57 DG |
2496 | static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid, |
2497 | gid_t gid) | |
6c9cc2ab | 2498 | { |
ad20f474 | 2499 | int ret; |
8e0af1b4 | 2500 | unsigned int i = 0; |
6c9cc2ab DG |
2501 | struct ltt_session *session; |
2502 | ||
8e0af1b4 MD |
2503 | DBG("Getting all available session for UID %d GID %d", |
2504 | uid, gid); | |
6c9cc2ab DG |
2505 | /* |
2506 | * Iterate over session list and append data after the control struct in | |
2507 | * the buffer. | |
2508 | */ | |
2509 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
8e0af1b4 MD |
2510 | /* |
2511 | * Only list the sessions the user can control. | |
2512 | */ | |
2513 | if (!session_access_ok(session, uid, gid)) { | |
2514 | continue; | |
2515 | } | |
ad20f474 | 2516 | |
6f636768 DG |
2517 | struct ltt_kernel_session *ksess = session->kernel_session; |
2518 | struct ltt_ust_session *usess = session->ust_session; | |
2519 | ||
2520 | if (session->consumer->type == CONSUMER_DST_NET || | |
2521 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
2522 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
ad20f474 DG |
2523 | ret = build_network_session_path(sessions[i].path, |
2524 | sizeof(session[i].path), session); | |
6f636768 DG |
2525 | } else { |
2526 | ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s", | |
2527 | session->consumer->dst.trace_path); | |
ad20f474 DG |
2528 | } |
2529 | if (ret < 0) { | |
2530 | PERROR("snprintf session path"); | |
2531 | continue; | |
2532 | } | |
2533 | ||
6c9cc2ab | 2534 | strncpy(sessions[i].name, session->name, NAME_MAX); |
99497cd0 | 2535 | sessions[i].name[NAME_MAX - 1] = '\0'; |
464dd62d | 2536 | sessions[i].enabled = session->enabled; |
6c9cc2ab DG |
2537 | i++; |
2538 | } | |
2539 | } | |
2540 | ||
9f19cc17 DG |
2541 | /* |
2542 | * Fill lttng_channel array of all channels. | |
2543 | */ | |
b551a063 | 2544 | static void list_lttng_channels(int domain, struct ltt_session *session, |
9f19cc17 DG |
2545 | struct lttng_channel *channels) |
2546 | { | |
2547 | int i = 0; | |
2548 | struct ltt_kernel_channel *kchan; | |
2549 | ||
2550 | DBG("Listing channels for session %s", session->name); | |
2551 | ||
b551a063 DG |
2552 | switch (domain) { |
2553 | case LTTNG_DOMAIN_KERNEL: | |
2554 | /* Kernel channels */ | |
2555 | if (session->kernel_session != NULL) { | |
2556 | cds_list_for_each_entry(kchan, | |
2557 | &session->kernel_session->channel_list.head, list) { | |
2558 | /* Copy lttng_channel struct to array */ | |
2559 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
2560 | channels[i].enabled = kchan->enabled; | |
2561 | i++; | |
2562 | } | |
2563 | } | |
2564 | break; | |
2565 | case LTTNG_DOMAIN_UST: | |
2566 | { | |
bec39940 | 2567 | struct lttng_ht_iter iter; |
b551a063 DG |
2568 | struct ltt_ust_channel *uchan; |
2569 | ||
bec39940 DG |
2570 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
2571 | &iter.iter, uchan, node.node) { | |
b551a063 DG |
2572 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); |
2573 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
2574 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
2575 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
2576 | channels[i].attr.switch_timer_interval = | |
2577 | uchan->attr.switch_timer_interval; | |
2578 | channels[i].attr.read_timer_interval = | |
2579 | uchan->attr.read_timer_interval; | |
aea829b3 | 2580 | channels[i].enabled = uchan->enabled; |
5edd7e09 DG |
2581 | switch (uchan->attr.output) { |
2582 | case LTTNG_UST_MMAP: | |
2583 | default: | |
2584 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
2585 | break; | |
2586 | } | |
befe0905 | 2587 | i++; |
b551a063 DG |
2588 | } |
2589 | break; | |
2590 | } | |
2591 | default: | |
2592 | break; | |
2593 | } | |
2594 | } | |
2595 | ||
2596 | /* | |
2597 | * Create a list of ust global domain events. | |
2598 | */ | |
2599 | static int list_lttng_ust_global_events(char *channel_name, | |
2600 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
2601 | { | |
2602 | int i = 0, ret = 0; | |
2603 | unsigned int nb_event = 0; | |
bec39940 DG |
2604 | struct lttng_ht_iter iter; |
2605 | struct lttng_ht_node_str *node; | |
b551a063 DG |
2606 | struct ltt_ust_channel *uchan; |
2607 | struct ltt_ust_event *uevent; | |
2608 | struct lttng_event *tmp; | |
2609 | ||
2610 | DBG("Listing UST global events for channel %s", channel_name); | |
2611 | ||
2612 | rcu_read_lock(); | |
2613 | ||
bec39940 DG |
2614 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); |
2615 | node = lttng_ht_iter_get_node_str(&iter); | |
d7b75b30 DG |
2616 | if (node == NULL) { |
2617 | ret = -LTTCOMM_UST_CHAN_NOT_FOUND; | |
2618 | goto error; | |
b551a063 DG |
2619 | } |
2620 | ||
bec39940 | 2621 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
d7b75b30 | 2622 | |
bec39940 | 2623 | nb_event += lttng_ht_get_count(uchan->events); |
d7b75b30 | 2624 | |
b551a063 DG |
2625 | if (nb_event == 0) { |
2626 | ret = nb_event; | |
2627 | goto error; | |
2628 | } | |
2629 | ||
2630 | DBG3("Listing UST global %d events", nb_event); | |
2631 | ||
2632 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2633 | if (tmp == NULL) { | |
2634 | ret = -LTTCOMM_FATAL; | |
2635 | goto error; | |
2636 | } | |
2637 | ||
bec39940 | 2638 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
d7b75b30 DG |
2639 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
2640 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2641 | tmp[i].enabled = uevent->enabled; | |
2642 | switch (uevent->attr.instrumentation) { | |
2643 | case LTTNG_UST_TRACEPOINT: | |
2644 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
2645 | break; | |
2646 | case LTTNG_UST_PROBE: | |
2647 | tmp[i].type = LTTNG_EVENT_PROBE; | |
2648 | break; | |
2649 | case LTTNG_UST_FUNCTION: | |
2650 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
2651 | break; | |
0cda4f28 | 2652 | } |
8005f29a | 2653 | tmp[i].loglevel = uevent->attr.loglevel; |
0cda4f28 | 2654 | switch (uevent->attr.loglevel_type) { |
8005f29a | 2655 | case LTTNG_UST_LOGLEVEL_ALL: |
22e25b71 | 2656 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
0cda4f28 | 2657 | break; |
8005f29a | 2658 | case LTTNG_UST_LOGLEVEL_RANGE: |
22e25b71 | 2659 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
8005f29a MD |
2660 | break; |
2661 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
22e25b71 | 2662 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
ac3bd9c0 | 2663 | break; |
9f19cc17 | 2664 | } |
fceb65df MD |
2665 | if (uevent->filter) { |
2666 | tmp[i].filter = 1; | |
2667 | } | |
d7b75b30 | 2668 | i++; |
9f19cc17 DG |
2669 | } |
2670 | ||
b551a063 DG |
2671 | ret = nb_event; |
2672 | *events = tmp; | |
2673 | ||
2674 | error: | |
2675 | rcu_read_unlock(); | |
2676 | return ret; | |
9f19cc17 DG |
2677 | } |
2678 | ||
2679 | /* | |
b551a063 | 2680 | * Fill lttng_event array of all kernel events in the channel. |
9f19cc17 | 2681 | */ |
b551a063 DG |
2682 | static int list_lttng_kernel_events(char *channel_name, |
2683 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
9f19cc17 | 2684 | { |
b551a063 DG |
2685 | int i = 0, ret; |
2686 | unsigned int nb_event; | |
9f19cc17 | 2687 | struct ltt_kernel_event *event; |
b551a063 DG |
2688 | struct ltt_kernel_channel *kchan; |
2689 | ||
2690 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
2691 | if (kchan == NULL) { | |
2692 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2693 | goto error; | |
2694 | } | |
2695 | ||
2696 | nb_event = kchan->event_count; | |
9f19cc17 DG |
2697 | |
2698 | DBG("Listing events for channel %s", kchan->channel->name); | |
2699 | ||
b551a063 DG |
2700 | if (nb_event == 0) { |
2701 | ret = nb_event; | |
2702 | goto error; | |
2703 | } | |
2704 | ||
2705 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2706 | if (*events == NULL) { | |
2707 | ret = LTTCOMM_FATAL; | |
2708 | goto error; | |
2709 | } | |
2710 | ||
9f19cc17 DG |
2711 | /* Kernel channels */ |
2712 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
b551a063 DG |
2713 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
2714 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2715 | (*events)[i].enabled = event->enabled; | |
9f19cc17 DG |
2716 | switch (event->event->instrumentation) { |
2717 | case LTTNG_KERNEL_TRACEPOINT: | |
b551a063 | 2718 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; |
9f19cc17 DG |
2719 | break; |
2720 | case LTTNG_KERNEL_KPROBE: | |
2721 | case LTTNG_KERNEL_KRETPROBE: | |
b551a063 DG |
2722 | (*events)[i].type = LTTNG_EVENT_PROBE; |
2723 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
9f19cc17 DG |
2724 | sizeof(struct lttng_kernel_kprobe)); |
2725 | break; | |
2726 | case LTTNG_KERNEL_FUNCTION: | |
b551a063 DG |
2727 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
2728 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
9f19cc17 DG |
2729 | sizeof(struct lttng_kernel_function)); |
2730 | break; | |
0133c199 | 2731 | case LTTNG_KERNEL_NOOP: |
b551a063 | 2732 | (*events)[i].type = LTTNG_EVENT_NOOP; |
0133c199 | 2733 | break; |
a54bd42d | 2734 | case LTTNG_KERNEL_SYSCALL: |
b551a063 | 2735 | (*events)[i].type = LTTNG_EVENT_SYSCALL; |
0133c199 | 2736 | break; |
7a3d1328 MD |
2737 | case LTTNG_KERNEL_ALL: |
2738 | assert(0); | |
2739 | break; | |
9f19cc17 DG |
2740 | } |
2741 | i++; | |
2742 | } | |
b551a063 DG |
2743 | |
2744 | return nb_event; | |
2745 | ||
2746 | error: | |
2747 | return ret; | |
9f19cc17 DG |
2748 | } |
2749 | ||
a4b92340 DG |
2750 | |
2751 | /* | |
2752 | * Add URI so the consumer output object. Set the correct path depending on the | |
2753 | * domain adding the default trace directory. | |
2754 | */ | |
2755 | static int add_uri_to_consumer(struct consumer_output *consumer, | |
07424f16 | 2756 | struct lttng_uri *uri, int domain, const char *session_name) |
a4b92340 | 2757 | { |
07424f16 | 2758 | int ret = LTTCOMM_OK; |
a4b92340 DG |
2759 | const char *default_trace_dir; |
2760 | ||
2761 | assert(uri); | |
2762 | ||
2763 | if (consumer == NULL) { | |
2764 | DBG("No consumer detected. Don't add URI. Stopping."); | |
2765 | ret = LTTCOMM_NO_CONSUMER; | |
2766 | goto error; | |
2767 | } | |
2768 | ||
2769 | switch (domain) { | |
2770 | case LTTNG_DOMAIN_KERNEL: | |
2771 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; | |
2772 | break; | |
2773 | case LTTNG_DOMAIN_UST: | |
2774 | default_trace_dir = DEFAULT_UST_TRACE_DIR; | |
2775 | break; | |
2776 | default: | |
2777 | /* | |
2778 | * This case is possible is we try to add the URI to the global tracing | |
2779 | * session consumer object which in this case there is no subdir. | |
2780 | */ | |
2781 | default_trace_dir = ""; | |
2782 | } | |
2783 | ||
2784 | switch (uri->dtype) { | |
2785 | case LTTNG_DST_IPV4: | |
2786 | case LTTNG_DST_IPV6: | |
2787 | DBG2("Setting network URI to consumer"); | |
2788 | ||
2789 | /* Set URI into consumer output object */ | |
2790 | ret = consumer_set_network_uri(consumer, uri); | |
2791 | if (ret < 0) { | |
2792 | ret = LTTCOMM_FATAL; | |
2793 | goto error; | |
ad20f474 DG |
2794 | } else if (ret == 1) { |
2795 | /* | |
2796 | * URI was the same in the consumer so we do not append the subdir | |
2797 | * again so to not duplicate output dir. | |
2798 | */ | |
2799 | goto error; | |
a4b92340 DG |
2800 | } |
2801 | ||
07424f16 DG |
2802 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { |
2803 | ret = set_consumer_subdir(consumer, session_name); | |
2804 | if (ret < 0) { | |
2805 | ret = LTTCOMM_FATAL; | |
2806 | goto error; | |
2807 | } | |
2808 | } | |
2809 | ||
2810 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
2811 | /* On a new subdir, reappend the default trace dir. */ | |
2812 | strncat(consumer->subdir, default_trace_dir, sizeof(consumer->subdir)); | |
2813 | DBG3("Append domain trace name to subdir %s", consumer->subdir); | |
a4b92340 DG |
2814 | } |
2815 | ||
2816 | break; | |
2817 | case LTTNG_DST_PATH: | |
2818 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
2819 | memset(consumer->dst.trace_path, 0, | |
2820 | sizeof(consumer->dst.trace_path)); | |
2821 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
2822 | sizeof(consumer->dst.trace_path)); | |
2823 | /* Append default trace dir */ | |
2824 | strncat(consumer->dst.trace_path, default_trace_dir, | |
2825 | sizeof(consumer->dst.trace_path)); | |
2826 | /* Flag consumer as local. */ | |
2827 | consumer->type = CONSUMER_DST_LOCAL; | |
2828 | break; | |
2829 | } | |
2830 | ||
2831 | error: | |
2832 | return ret; | |
2833 | } | |
2834 | ||
fac6795d | 2835 | /* |
54d01ffb | 2836 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
fac6795d | 2837 | */ |
54d01ffb DG |
2838 | static int cmd_disable_channel(struct ltt_session *session, |
2839 | int domain, char *channel_name) | |
fac6795d | 2840 | { |
54d01ffb | 2841 | int ret; |
78f0bacd DG |
2842 | struct ltt_ust_session *usess; |
2843 | ||
2844 | usess = session->ust_session; | |
379473d2 | 2845 | |
54d01ffb | 2846 | switch (domain) { |
78f0bacd DG |
2847 | case LTTNG_DOMAIN_KERNEL: |
2848 | { | |
2849 | ret = channel_kernel_disable(session->kernel_session, | |
2850 | channel_name); | |
2851 | if (ret != LTTCOMM_OK) { | |
2852 | goto error; | |
2853 | } | |
54d01ffb | 2854 | |
78f0bacd DG |
2855 | kernel_wait_quiescent(kernel_tracer_fd); |
2856 | break; | |
2857 | } | |
2858 | case LTTNG_DOMAIN_UST: | |
2859 | { | |
2860 | struct ltt_ust_channel *uchan; | |
bec39940 | 2861 | struct lttng_ht *chan_ht; |
78f0bacd | 2862 | |
7885e399 DG |
2863 | chan_ht = usess->domain_global.channels; |
2864 | ||
2865 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
78f0bacd DG |
2866 | if (uchan == NULL) { |
2867 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
54d01ffb | 2868 | goto error; |
78f0bacd DG |
2869 | } |
2870 | ||
7885e399 DG |
2871 | ret = channel_ust_disable(usess, domain, uchan); |
2872 | if (ret != LTTCOMM_OK) { | |
78f0bacd DG |
2873 | goto error; |
2874 | } | |
78f0bacd DG |
2875 | break; |
2876 | } | |
d78d6610 | 2877 | #if 0 |
78f0bacd DG |
2878 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
2879 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2880 | case LTTNG_DOMAIN_UST_PID: | |
d78d6610 | 2881 | #endif |
78f0bacd DG |
2882 | default: |
2883 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2884 | goto error; | |
20fe2104 DG |
2885 | } |
2886 | ||
54d01ffb | 2887 | ret = LTTCOMM_OK; |
d65106b1 | 2888 | |
54d01ffb DG |
2889 | error: |
2890 | return ret; | |
2891 | } | |
2892 | ||
2893 | /* | |
2894 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
2895 | */ | |
44d3bd01 | 2896 | static int cmd_enable_channel(struct ltt_session *session, |
aea829b3 | 2897 | int domain, struct lttng_channel *attr) |
54d01ffb DG |
2898 | { |
2899 | int ret; | |
f6a9efaa | 2900 | struct ltt_ust_session *usess = session->ust_session; |
bec39940 | 2901 | struct lttng_ht *chan_ht; |
f6a9efaa | 2902 | |
5d56b5aa | 2903 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
d65106b1 | 2904 | |
aea829b3 | 2905 | switch (domain) { |
44d3bd01 DG |
2906 | case LTTNG_DOMAIN_KERNEL: |
2907 | { | |
2908 | struct ltt_kernel_channel *kchan; | |
54d01ffb | 2909 | |
44d3bd01 DG |
2910 | kchan = trace_kernel_get_channel_by_name(attr->name, |
2911 | session->kernel_session); | |
2912 | if (kchan == NULL) { | |
2913 | ret = channel_kernel_create(session->kernel_session, | |
2914 | attr, kernel_poll_pipe[1]); | |
2915 | } else { | |
2916 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
2917 | } | |
2918 | ||
2919 | if (ret != LTTCOMM_OK) { | |
2920 | goto error; | |
2921 | } | |
2922 | ||
2923 | kernel_wait_quiescent(kernel_tracer_fd); | |
2924 | break; | |
2925 | } | |
f6a9efaa DG |
2926 | case LTTNG_DOMAIN_UST: |
2927 | { | |
2928 | struct ltt_ust_channel *uchan; | |
2929 | ||
7885e399 | 2930 | chan_ht = usess->domain_global.channels; |
f6a9efaa | 2931 | |
7885e399 | 2932 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
f6a9efaa | 2933 | if (uchan == NULL) { |
7885e399 | 2934 | ret = channel_ust_create(usess, domain, attr); |
f6a9efaa | 2935 | } else { |
7885e399 | 2936 | ret = channel_ust_enable(usess, domain, uchan); |
48842b30 | 2937 | } |
f6a9efaa DG |
2938 | break; |
2939 | } | |
d78d6610 | 2940 | #if 0 |
78f0bacd DG |
2941 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
2942 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
44d3bd01 | 2943 | case LTTNG_DOMAIN_UST_PID: |
d78d6610 | 2944 | #endif |
44d3bd01 DG |
2945 | default: |
2946 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2947 | goto error; | |
54d01ffb DG |
2948 | } |
2949 | ||
54d01ffb DG |
2950 | error: |
2951 | return ret; | |
2952 | } | |
2953 | ||
2954 | /* | |
2955 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
2956 | */ | |
2957 | static int cmd_disable_event(struct ltt_session *session, int domain, | |
2958 | char *channel_name, char *event_name) | |
2959 | { | |
2960 | int ret; | |
54d01ffb DG |
2961 | |
2962 | switch (domain) { | |
2963 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
2964 | { |
2965 | struct ltt_kernel_channel *kchan; | |
b0a40d28 | 2966 | struct ltt_kernel_session *ksess; |
2bdd86d4 | 2967 | |
b0a40d28 DG |
2968 | ksess = session->kernel_session; |
2969 | ||
2970 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
2971 | if (kchan == NULL) { |
2972 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2973 | goto error; | |
d65106b1 DG |
2974 | } |
2975 | ||
b0a40d28 | 2976 | ret = event_kernel_disable_tracepoint(ksess, kchan, event_name); |
54d01ffb DG |
2977 | if (ret != LTTCOMM_OK) { |
2978 | goto error; | |
2979 | } | |
2980 | ||
2981 | kernel_wait_quiescent(kernel_tracer_fd); | |
d65106b1 | 2982 | break; |
2bdd86d4 MD |
2983 | } |
2984 | case LTTNG_DOMAIN_UST: | |
b0a40d28 | 2985 | { |
b0a40d28 | 2986 | struct ltt_ust_channel *uchan; |
7f79d3a1 | 2987 | struct ltt_ust_session *usess; |
b0a40d28 DG |
2988 | |
2989 | usess = session->ust_session; | |
2990 | ||
2991 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2992 | channel_name); | |
2993 | if (uchan == NULL) { | |
2994 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2995 | goto error; | |
2996 | } | |
2997 | ||
7f79d3a1 DG |
2998 | ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name); |
2999 | if (ret != LTTCOMM_OK) { | |
b0a40d28 DG |
3000 | goto error; |
3001 | } | |
3002 | ||
7f79d3a1 | 3003 | DBG3("Disable UST event %s in channel %s completed", event_name, |
b0a40d28 | 3004 | channel_name); |
b0a40d28 DG |
3005 | break; |
3006 | } | |
d78d6610 | 3007 | #if 0 |
2bdd86d4 MD |
3008 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
3009 | case LTTNG_DOMAIN_UST_PID: | |
3010 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 3011 | #endif |
54d01ffb | 3012 | default: |
1ab1ea0b | 3013 | ret = LTTCOMM_UND; |
54d01ffb | 3014 | goto error; |
d65106b1 | 3015 | } |
26cc6b4e | 3016 | |
54d01ffb DG |
3017 | ret = LTTCOMM_OK; |
3018 | ||
3019 | error: | |
3020 | return ret; | |
3021 | } | |
3022 | ||
3023 | /* | |
3024 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. | |
3025 | */ | |
3026 | static int cmd_disable_event_all(struct ltt_session *session, int domain, | |
3027 | char *channel_name) | |
3028 | { | |
3029 | int ret; | |
54d01ffb DG |
3030 | |
3031 | switch (domain) { | |
3032 | case LTTNG_DOMAIN_KERNEL: | |
9730260e DG |
3033 | { |
3034 | struct ltt_kernel_session *ksess; | |
3035 | struct ltt_kernel_channel *kchan; | |
3036 | ||
3037 | ksess = session->kernel_session; | |
3038 | ||
3039 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
3040 | if (kchan == NULL) { |
3041 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
3042 | goto error; | |
26cc6b4e DG |
3043 | } |
3044 | ||
9730260e | 3045 | ret = event_kernel_disable_all(ksess, kchan); |
54d01ffb | 3046 | if (ret != LTTCOMM_OK) { |
0b97ec54 | 3047 | goto error; |
26cc6b4e DG |
3048 | } |
3049 | ||
54d01ffb | 3050 | kernel_wait_quiescent(kernel_tracer_fd); |
26cc6b4e | 3051 | break; |
9730260e DG |
3052 | } |
3053 | case LTTNG_DOMAIN_UST: | |
3054 | { | |
3055 | struct ltt_ust_session *usess; | |
3056 | struct ltt_ust_channel *uchan; | |
3057 | ||
3058 | usess = session->ust_session; | |
3059 | ||
3060 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
3061 | channel_name); | |
3062 | if (uchan == NULL) { | |
3063 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
3064 | goto error; | |
3065 | } | |
3066 | ||
7f79d3a1 DG |
3067 | ret = event_ust_disable_all_tracepoints(usess, domain, uchan); |
3068 | if (ret != 0) { | |
9730260e DG |
3069 | goto error; |
3070 | } | |
3071 | ||
7f79d3a1 | 3072 | DBG3("Disable all UST events in channel %s completed", channel_name); |
9730260e DG |
3073 | |
3074 | break; | |
3075 | } | |
d78d6610 | 3076 | #if 0 |
9730260e DG |
3077 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
3078 | case LTTNG_DOMAIN_UST_PID: | |
3079 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 3080 | #endif |
54d01ffb | 3081 | default: |
1ab1ea0b | 3082 | ret = LTTCOMM_UND; |
54d01ffb | 3083 | goto error; |
26cc6b4e | 3084 | } |
e953ef25 | 3085 | |
54d01ffb | 3086 | ret = LTTCOMM_OK; |
e953ef25 | 3087 | |
54d01ffb DG |
3088 | error: |
3089 | return ret; | |
3090 | } | |
f5177a38 | 3091 | |
54d01ffb DG |
3092 | /* |
3093 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
3094 | */ | |
3095 | static int cmd_add_context(struct ltt_session *session, int domain, | |
3096 | char *channel_name, char *event_name, struct lttng_event_context *ctx) | |
3097 | { | |
3098 | int ret; | |
f5177a38 | 3099 | |
54d01ffb DG |
3100 | switch (domain) { |
3101 | case LTTNG_DOMAIN_KERNEL: | |
3102 | /* Add kernel context to kernel tracer */ | |
3103 | ret = context_kernel_add(session->kernel_session, ctx, | |
3104 | event_name, channel_name); | |
3105 | if (ret != LTTCOMM_OK) { | |
f5177a38 | 3106 | goto error; |
e953ef25 | 3107 | } |
2bdd86d4 MD |
3108 | break; |
3109 | case LTTNG_DOMAIN_UST: | |
3110 | { | |
55cc08a6 | 3111 | struct ltt_ust_session *usess = session->ust_session; |
e953ef25 | 3112 | |
55cc08a6 DG |
3113 | ret = context_ust_add(usess, domain, ctx, event_name, channel_name); |
3114 | if (ret != LTTCOMM_OK) { | |
3115 | goto error; | |
2bdd86d4 | 3116 | } |
e953ef25 | 3117 | break; |
2bdd86d4 | 3118 | } |
d78d6610 | 3119 | #if 0 |
55cc08a6 DG |
3120 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
3121 | case LTTNG_DOMAIN_UST_PID: | |
3122 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
d78d6610 | 3123 | #endif |
54d01ffb | 3124 | default: |
1ab1ea0b | 3125 | ret = LTTCOMM_UND; |
54d01ffb | 3126 | goto error; |
e953ef25 | 3127 | } |
950131af | 3128 | |
54d01ffb | 3129 | ret = LTTCOMM_OK; |
950131af | 3130 | |
54d01ffb DG |
3131 | error: |
3132 | return ret; | |
3133 | } | |
3134 | ||
53a80697 MD |
3135 | /* |
3136 | * Command LTTNG_SET_FILTER processed by the client thread. | |
3137 | */ | |
3138 | static int cmd_set_filter(struct ltt_session *session, int domain, | |
3139 | char *channel_name, char *event_name, | |
3140 | struct lttng_filter_bytecode *bytecode) | |
3141 | { | |
3142 | int ret; | |
3143 | ||
3144 | switch (domain) { | |
3145 | case LTTNG_DOMAIN_KERNEL: | |
3146 | ret = LTTCOMM_FATAL; | |
3147 | break; | |
3148 | case LTTNG_DOMAIN_UST: | |
3149 | { | |
3150 | struct ltt_ust_session *usess = session->ust_session; | |
3151 | ||
3152 | ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name); | |
3153 | if (ret != LTTCOMM_OK) { | |
3154 | goto error; | |
3155 | } | |
3156 | break; | |
3157 | } | |
3158 | #if 0 | |
3159 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
3160 | case LTTNG_DOMAIN_UST_PID: | |
3161 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
3162 | #endif | |
3163 | default: | |
3164 | ret = LTTCOMM_UND; | |
3165 | goto error; | |
3166 | } | |
3167 | ||
3168 | ret = LTTCOMM_OK; | |
3169 | ||
3170 | error: | |
3171 | return ret; | |
3172 | ||
3173 | } | |
3174 | ||
54d01ffb DG |
3175 | /* |
3176 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
3177 | */ | |
3178 | static int cmd_enable_event(struct ltt_session *session, int domain, | |
3179 | char *channel_name, struct lttng_event *event) | |
3180 | { | |
3181 | int ret; | |
f6cd6b0f | 3182 | struct lttng_channel *attr; |
48842b30 | 3183 | struct ltt_ust_session *usess = session->ust_session; |
54d01ffb DG |
3184 | |
3185 | switch (domain) { | |
3186 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
3187 | { |
3188 | struct ltt_kernel_channel *kchan; | |
3189 | ||
54d01ffb DG |
3190 | kchan = trace_kernel_get_channel_by_name(channel_name, |
3191 | session->kernel_session); | |
3192 | if (kchan == NULL) { | |
f6cd6b0f DG |
3193 | attr = channel_new_default_attr(domain); |
3194 | if (attr == NULL) { | |
3195 | ret = LTTCOMM_FATAL; | |
3196 | goto error; | |
3197 | } | |
3198 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
3199 | ||
54d01ffb | 3200 | /* This call will notify the kernel thread */ |
44d3bd01 | 3201 | ret = channel_kernel_create(session->kernel_session, |
f6cd6b0f | 3202 | attr, kernel_poll_pipe[1]); |
54d01ffb | 3203 | if (ret != LTTCOMM_OK) { |
ff4d74e6 | 3204 | free(attr); |
f5177a38 DG |
3205 | goto error; |
3206 | } | |
ff4d74e6 | 3207 | free(attr); |
54d01ffb | 3208 | } |
950131af | 3209 | |
54d01ffb DG |
3210 | /* Get the newly created kernel channel pointer */ |
3211 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
3212 | session->kernel_session); | |
3213 | if (kchan == NULL) { | |
3214 | /* This sould not happen... */ | |
3215 | ret = LTTCOMM_FATAL; | |
3216 | goto error; | |
3217 | } | |
f5177a38 | 3218 | |
48842b30 DG |
3219 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, |
3220 | event); | |
54d01ffb | 3221 | if (ret != LTTCOMM_OK) { |
f5177a38 | 3222 | goto error; |
950131af DG |
3223 | } |
3224 | ||
54d01ffb | 3225 | kernel_wait_quiescent(kernel_tracer_fd); |
950131af | 3226 | break; |
2bdd86d4 MD |
3227 | } |
3228 | case LTTNG_DOMAIN_UST: | |
3229 | { | |
aea829b3 | 3230 | struct lttng_channel *attr; |
edb67388 | 3231 | struct ltt_ust_channel *uchan; |
2bdd86d4 | 3232 | |
edb67388 | 3233 | /* Get channel from global UST domain */ |
48842b30 DG |
3234 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
3235 | channel_name); | |
3236 | if (uchan == NULL) { | |
aea829b3 DG |
3237 | /* Create default channel */ |
3238 | attr = channel_new_default_attr(domain); | |
3239 | if (attr == NULL) { | |
3240 | ret = LTTCOMM_FATAL; | |
3241 | goto error; | |
3242 |