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