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