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