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 | * |
54d01ffb DG |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
91d76f53 | 8 | * |
54d01ffb DG |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
91d76f53 | 13 | * |
54d01ffb DG |
14 | * You should have received a copy of the GNU General Public License along with |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <fcntl.h> | |
21 | #include <getopt.h> | |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
24 | #include <pthread.h> | |
8c0faa1d | 25 | #include <semaphore.h> |
fac6795d DG |
26 | #include <signal.h> |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
0fdd1e2c | 30 | #include <sys/mman.h> |
b73401da | 31 | #include <sys/mount.h> |
1e307fab | 32 | #include <sys/resource.h> |
fac6795d DG |
33 | #include <sys/socket.h> |
34 | #include <sys/stat.h> | |
35 | #include <sys/types.h> | |
0fdd1e2c DG |
36 | #include <sys/wait.h> |
37 | #include <urcu/futex.h> | |
fac6795d | 38 | #include <unistd.h> |
3bd1e081 | 39 | #include <config.h> |
fac6795d | 40 | |
3bd1e081 | 41 | #include <lttng-consumerd.h> |
e88129fc | 42 | #include <lttng-sessiond-comm.h> |
3bd1e081 | 43 | #include <lttng/lttng-consumer.h> |
2bdd86d4 | 44 | |
1e307fab | 45 | #include <lttngerr.h> |
fac6795d | 46 | |
54d01ffb | 47 | #include "channel.h" |
5eb91c98 | 48 | #include "compat/poll.h" |
099e26bd | 49 | #include "context.h" |
54d01ffb | 50 | #include "event.h" |
099e26bd | 51 | #include "futex.h" |
f6a9efaa | 52 | #include "hashtable.h" |
4771f025 | 53 | #include "kernel.h" |
32258573 | 54 | #include "lttng-sessiond.h" |
0fdd1e2c | 55 | #include "shm.h" |
56fff090 | 56 | #include "ust-app.h" |
1e307fab | 57 | #include "ust-ctl.h" |
8e68d1c8 | 58 | #include "utils.h" |
fac6795d | 59 | |
ebaeda94 MD |
60 | #define CONSUMERD_FILE "lttng-consumerd" |
61 | ||
3bd1e081 MD |
62 | struct consumer_data { |
63 | enum lttng_consumer_type type; | |
64 | ||
65 | pthread_t thread; /* Worker thread interacting with the consumer */ | |
66 | sem_t sem; | |
67 | ||
68 | /* Mutex to control consumerd pid assignation */ | |
69 | pthread_mutex_t pid_mutex; | |
70 | pid_t pid; | |
71 | ||
72 | int err_sock; | |
73 | int cmd_sock; | |
74 | ||
75 | /* consumer error and command Unix socket path */ | |
76 | char err_unix_sock_path[PATH_MAX]; | |
77 | char cmd_unix_sock_path[PATH_MAX]; | |
78 | }; | |
79 | ||
75462a81 | 80 | /* Const values */ |
686204ab | 81 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 82 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
83 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
84 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
85 | ||
fac6795d | 86 | /* Variables */ |
1d4b027a | 87 | int opt_verbose; /* Not static for lttngerr.h */ |
3bd1e081 | 88 | int opt_verbose_consumer; /* Not static for lttngerr.h */ |
1d4b027a | 89 | int opt_quiet; /* Not static for lttngerr.h */ |
d063d709 | 90 | |
fac6795d DG |
91 | const char *progname; |
92 | const char *opt_tracing_group; | |
5b8719f5 | 93 | static int opt_sig_parent; |
fac6795d | 94 | static int opt_daemon; |
4fba7219 | 95 | static int opt_no_kernel; |
fac6795d | 96 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 97 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
67e40797 | 98 | static char *rundir; |
3bd1e081 MD |
99 | |
100 | /* Consumer daemon specific control data */ | |
101 | static struct consumer_data kconsumer_data = { | |
102 | .type = LTTNG_CONSUMER_KERNEL, | |
7753dea8 MD |
103 | .err_unix_sock_path = KCONSUMERD_ERR_SOCK_PATH, |
104 | .cmd_unix_sock_path = KCONSUMERD_CMD_SOCK_PATH, | |
3bd1e081 | 105 | }; |
7753dea8 MD |
106 | static struct consumer_data ustconsumer64_data = { |
107 | .type = LTTNG_CONSUMER64_UST, | |
108 | .err_unix_sock_path = USTCONSUMERD64_ERR_SOCK_PATH, | |
109 | .cmd_unix_sock_path = USTCONSUMERD64_CMD_SOCK_PATH, | |
110 | }; | |
111 | static struct consumer_data ustconsumer32_data = { | |
112 | .type = LTTNG_CONSUMER32_UST, | |
113 | .err_unix_sock_path = USTCONSUMERD32_ERR_SOCK_PATH, | |
114 | .cmd_unix_sock_path = USTCONSUMERD32_CMD_SOCK_PATH, | |
3bd1e081 MD |
115 | }; |
116 | ||
099e26bd | 117 | static int dispatch_thread_exit; |
fac6795d | 118 | |
54d01ffb DG |
119 | /* Global application Unix socket path */ |
120 | static char apps_unix_sock_path[PATH_MAX]; | |
121 | /* Global client Unix socket path */ | |
122 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
123 | /* global wait shm path for UST */ |
124 | static char wait_shm_path[PATH_MAX]; | |
fac6795d | 125 | |
1d4b027a | 126 | /* Sockets and FDs */ |
d6f42150 DG |
127 | static int client_sock; |
128 | static int apps_sock; | |
20fe2104 | 129 | static int kernel_tracer_fd; |
7a485870 | 130 | static int kernel_poll_pipe[2]; |
1d4b027a | 131 | |
273ea72c DG |
132 | /* |
133 | * Quit pipe for all threads. This permits a single cancellation point | |
134 | * for all threads when receiving an event on the pipe. | |
135 | */ | |
136 | static int thread_quit_pipe[2]; | |
137 | ||
099e26bd DG |
138 | /* |
139 | * This pipe is used to inform the thread managing application communication | |
140 | * that a command is queued and ready to be processed. | |
141 | */ | |
142 | static int apps_cmd_pipe[2]; | |
143 | ||
1d4b027a | 144 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 145 | static pthread_t apps_thread; |
099e26bd | 146 | static pthread_t reg_apps_thread; |
1d4b027a | 147 | static pthread_t client_thread; |
7a485870 | 148 | static pthread_t kernel_thread; |
099e26bd | 149 | static pthread_t dispatch_thread; |
5eb91c98 | 150 | |
fac6795d | 151 | |
099e26bd DG |
152 | /* |
153 | * UST registration command queue. This queue is tied with a futex and uses a N | |
154 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
155 | * | |
156 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
157 | * this queue and the wait/wake scheme. | |
158 | */ | |
159 | static struct ust_cmd_queue ust_cmd_queue; | |
160 | ||
b5541356 DG |
161 | /* |
162 | * Pointer initialized before thread creation. | |
163 | * | |
164 | * This points to the tracing session list containing the session count and a | |
165 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
166 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 167 | * |
d063d709 | 168 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 169 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
170 | */ |
171 | static struct ltt_session_list *session_list_ptr; | |
172 | ||
7753dea8 MD |
173 | int ust_consumerd64_fd = -1; |
174 | int ust_consumerd32_fd = -1; | |
175 | ||
fc7a59ce AM |
176 | static const char *consumerd32_bin = |
177 | __stringify(CONFIG_CONSUMERD32_BIN); | |
178 | static const char *consumerd64_bin = | |
179 | __stringify(CONFIG_CONSUMERD64_BIN); | |
ebaeda94 MD |
180 | static const char *consumerd32_libdir = |
181 | __stringify(CONFIG_CONSUMERD32_LIBDIR); | |
182 | static const char *consumerd64_libdir = | |
183 | __stringify(CONFIG_CONSUMERD64_LIBDIR); | |
fb09408a MD |
184 | |
185 | static | |
7753dea8 | 186 | void setup_consumerd_path(void) |
fb09408a | 187 | { |
fc7a59ce | 188 | const char *bin, *libdir; |
fb09408a | 189 | |
7753dea8 MD |
190 | /* |
191 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
192 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
193 | * has not been defined. | |
7753dea8 | 194 | */ |
ebaeda94 | 195 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
196 | if (!consumerd32_bin[0]) { |
197 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
198 | } |
199 | if (!consumerd32_libdir[0]) { | |
200 | consumerd32_libdir = INSTALL_LIB_PATH; | |
201 | } | |
202 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
203 | if (!consumerd64_bin[0]) { |
204 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 205 | } |
ebaeda94 MD |
206 | if (!consumerd64_libdir[0]) { |
207 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
208 | } |
209 | #else | |
210 | #error "Unknown bitness" | |
211 | #endif | |
212 | ||
fb09408a MD |
213 | /* |
214 | * runtime env. var. overrides the build default. | |
215 | */ | |
fc7a59ce AM |
216 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
217 | if (bin) { | |
218 | consumerd32_bin = bin; | |
7753dea8 | 219 | } |
fc7a59ce AM |
220 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
221 | if (bin) { | |
222 | consumerd64_bin = bin; | |
ebaeda94 MD |
223 | } |
224 | libdir = getenv("LTTNG_TOOLS_CONSUMERD32_LIBDIR"); | |
225 | if (libdir) { | |
226 | consumerd32_libdir = libdir; | |
227 | } | |
228 | libdir = getenv("LTTNG_TOOLS_CONSUMERD64_LIBDIR"); | |
229 | if (libdir) { | |
230 | consumerd64_libdir = libdir; | |
fb09408a MD |
231 | } |
232 | } | |
233 | ||
5eb91c98 DG |
234 | /* |
235 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
236 | */ | |
237 | static int create_thread_poll_set(struct lttng_poll_event *events, | |
238 | unsigned int size) | |
239 | { | |
240 | int ret; | |
241 | ||
242 | if (events == NULL || size == 0) { | |
243 | ret = -1; | |
244 | goto error; | |
245 | } | |
246 | ||
247 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
248 | if (ret < 0) { | |
249 | goto error; | |
250 | } | |
251 | ||
252 | /* Add quit pipe */ | |
253 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); | |
254 | if (ret < 0) { | |
255 | goto error; | |
256 | } | |
257 | ||
258 | return 0; | |
259 | ||
260 | error: | |
261 | return ret; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Check if the thread quit pipe was triggered. | |
266 | * | |
267 | * Return 1 if it was triggered else 0; | |
268 | */ | |
269 | static int check_thread_quit_pipe(int fd, uint32_t events) | |
270 | { | |
271 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
272 | return 1; | |
273 | } | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
0fdd1e2c DG |
278 | /* |
279 | * Remove modules in reverse load order. | |
280 | */ | |
281 | static int modprobe_remove_kernel_modules(void) | |
282 | { | |
283 | int ret = 0, i; | |
284 | char modprobe[256]; | |
285 | ||
286 | for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) { | |
287 | ret = snprintf(modprobe, sizeof(modprobe), | |
24b487b8 | 288 | "/sbin/modprobe -r -q %s", |
0fdd1e2c DG |
289 | kernel_modules_list[i].name); |
290 | if (ret < 0) { | |
24b487b8 | 291 | perror("snprintf modprobe -r"); |
0fdd1e2c DG |
292 | goto error; |
293 | } | |
294 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
295 | ret = system(modprobe); | |
296 | if (ret == -1) { | |
24b487b8 | 297 | ERR("Unable to launch modprobe -r for module %s", |
0fdd1e2c DG |
298 | kernel_modules_list[i].name); |
299 | } else if (kernel_modules_list[i].required | |
300 | && WEXITSTATUS(ret) != 0) { | |
301 | ERR("Unable to remove module %s", | |
302 | kernel_modules_list[i].name); | |
303 | } else { | |
304 | DBG("Modprobe removal successful %s", | |
305 | kernel_modules_list[i].name); | |
306 | } | |
307 | } | |
308 | ||
309 | error: | |
310 | return ret; | |
311 | } | |
312 | ||
313 | /* | |
314 | * Return group ID of the tracing group or -1 if not found. | |
315 | */ | |
996b65c8 MD |
316 | static gid_t allowed_group(void) |
317 | { | |
318 | struct group *grp; | |
319 | ||
274e143b MD |
320 | if (opt_tracing_group) { |
321 | grp = getgrnam(opt_tracing_group); | |
322 | } else { | |
323 | grp = getgrnam(default_tracing_group); | |
324 | } | |
996b65c8 MD |
325 | if (!grp) { |
326 | return -1; | |
327 | } else { | |
328 | return grp->gr_gid; | |
329 | } | |
330 | } | |
331 | ||
273ea72c | 332 | /* |
5eb91c98 | 333 | * Init thread quit pipe. |
273ea72c DG |
334 | * |
335 | * Return -1 on error or 0 if all pipes are created. | |
336 | */ | |
337 | static int init_thread_quit_pipe(void) | |
338 | { | |
339 | int ret; | |
340 | ||
341 | ret = pipe2(thread_quit_pipe, O_CLOEXEC); | |
342 | if (ret < 0) { | |
343 | perror("thread quit pipe"); | |
344 | goto error; | |
345 | } | |
346 | ||
347 | error: | |
348 | return ret; | |
349 | } | |
350 | ||
fac6795d | 351 | /* |
d063d709 DG |
352 | * Complete teardown of a kernel session. This free all data structure related |
353 | * to a kernel session and update counter. | |
fac6795d | 354 | */ |
1d4b027a | 355 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 356 | { |
8f7a84dd DG |
357 | if (!session->kernel_session) { |
358 | DBG3("No kernel session when tearingdown session"); | |
84ea9c99 | 359 | return; |
8f7a84dd DG |
360 | } |
361 | ||
84ea9c99 | 362 | DBG("Tearing down kernel session"); |
d9800920 | 363 | |
84ea9c99 MD |
364 | /* |
365 | * If a custom kernel consumer was registered, close the socket before | |
366 | * tearing down the complete kernel session structure | |
367 | */ | |
368 | if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) { | |
369 | lttcomm_close_unix_sock(session->kernel_session->consumer_fd); | |
fac6795d | 370 | } |
84ea9c99 MD |
371 | |
372 | trace_kernel_destroy_session(session->kernel_session); | |
fac6795d DG |
373 | } |
374 | ||
f6a9efaa DG |
375 | /* |
376 | * Complete teardown of all UST sessions. This will free everything on his path | |
377 | * and destroy the core essence of all ust sessions :) | |
378 | */ | |
379 | static void teardown_ust_session(struct ltt_session *session) | |
380 | { | |
84cd17c6 MD |
381 | int ret; |
382 | ||
8f7a84dd DG |
383 | if (!session->ust_session) { |
384 | DBG3("No UST session when tearingdown session"); | |
f470a390 | 385 | return; |
8f7a84dd DG |
386 | } |
387 | ||
84ea9c99 | 388 | DBG("Tearing down UST session(s)"); |
8f7a84dd | 389 | |
84cd17c6 MD |
390 | ret = ust_app_destroy_trace_all(session->ust_session); |
391 | if (ret) { | |
392 | ERR("Error in ust_app_destroy_trace_all"); | |
393 | } | |
8f7a84dd | 394 | |
f6a9efaa DG |
395 | trace_ust_destroy_session(session->ust_session); |
396 | } | |
397 | ||
099e26bd DG |
398 | /* |
399 | * Stop all threads by closing the thread quit pipe. | |
400 | */ | |
cf3af59e MD |
401 | static void stop_threads(void) |
402 | { | |
5eb91c98 DG |
403 | int ret; |
404 | ||
cf3af59e MD |
405 | /* Stopping all threads */ |
406 | DBG("Terminating all threads"); | |
54d01ffb | 407 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
408 | if (ret < 0) { |
409 | ERR("write error on thread quit pipe"); | |
410 | } | |
411 | ||
099e26bd DG |
412 | /* Dispatch thread */ |
413 | dispatch_thread_exit = 1; | |
414 | futex_nto1_wake(&ust_cmd_queue.futex); | |
cf3af59e MD |
415 | } |
416 | ||
fac6795d | 417 | /* |
d063d709 | 418 | * Cleanup the daemon |
fac6795d | 419 | */ |
cf3af59e | 420 | static void cleanup(void) |
fac6795d | 421 | { |
1d4b027a DG |
422 | int ret; |
423 | char *cmd; | |
af9737e9 | 424 | struct ltt_session *sess, *stmp; |
fac6795d | 425 | |
1d4b027a | 426 | DBG("Cleaning up"); |
e07ae692 | 427 | |
67e40797 DG |
428 | DBG("Removing %s directory", rundir); |
429 | ret = asprintf(&cmd, "rm -rf %s", rundir); | |
430 | if (ret < 0) { | |
431 | ERR("asprintf failed. Something is really wrong!"); | |
432 | } | |
5461b305 | 433 | |
67e40797 DG |
434 | /* Remove lttng run directory */ |
435 | ret = system(cmd); | |
436 | if (ret < 0) { | |
437 | ERR("Unable to clean %s", rundir); | |
1d4b027a | 438 | } |
67e40797 | 439 | free(cmd); |
5461b305 | 440 | |
1d4b027a | 441 | DBG("Cleaning up all session"); |
fac6795d | 442 | |
b5541356 | 443 | /* Destroy session list mutex */ |
273ea72c DG |
444 | if (session_list_ptr != NULL) { |
445 | pthread_mutex_destroy(&session_list_ptr->lock); | |
446 | ||
447 | /* Cleanup ALL session */ | |
54d01ffb DG |
448 | cds_list_for_each_entry_safe(sess, stmp, |
449 | &session_list_ptr->head, list) { | |
273ea72c | 450 | teardown_kernel_session(sess); |
f6a9efaa DG |
451 | teardown_ust_session(sess); |
452 | free(sess); | |
273ea72c DG |
453 | } |
454 | } | |
455 | ||
099e26bd | 456 | DBG("Closing all UST sockets"); |
56fff090 | 457 | ust_app_clean_list(); |
099e26bd | 458 | |
3bd1e081 | 459 | pthread_mutex_destroy(&kconsumer_data.pid_mutex); |
b5541356 | 460 | |
4fba7219 DG |
461 | if (is_root && !opt_no_kernel) { |
462 | DBG2("Closing kernel fd"); | |
463 | close(kernel_tracer_fd); | |
2f50c8a3 DG |
464 | DBG("Unloading kernel modules"); |
465 | modprobe_remove_kernel_modules(); | |
466 | } | |
5eb91c98 DG |
467 | |
468 | close(thread_quit_pipe[0]); | |
469 | close(thread_quit_pipe[1]); | |
421cb601 DG |
470 | |
471 | /* <fun> */ | |
f56a39af | 472 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
473 | "Matthew, BEET driven development works!%c[%dm", |
474 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
475 | /* </fun> */ | |
fac6795d DG |
476 | } |
477 | ||
e065084a | 478 | /* |
d063d709 | 479 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 480 | * |
d063d709 | 481 | * Return lttcomm error code. |
e065084a DG |
482 | */ |
483 | static int send_unix_sock(int sock, void *buf, size_t len) | |
484 | { | |
485 | /* Check valid length */ | |
486 | if (len <= 0) { | |
487 | return -1; | |
488 | } | |
489 | ||
490 | return lttcomm_send_unix_sock(sock, buf, len); | |
491 | } | |
492 | ||
5461b305 | 493 | /* |
d063d709 | 494 | * Free memory of a command context structure. |
5461b305 | 495 | */ |
a2fb29a5 | 496 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 497 | { |
a2fb29a5 DG |
498 | DBG("Clean command context structure"); |
499 | if (*cmd_ctx) { | |
500 | if ((*cmd_ctx)->llm) { | |
501 | free((*cmd_ctx)->llm); | |
5461b305 | 502 | } |
a2fb29a5 DG |
503 | if ((*cmd_ctx)->lsm) { |
504 | free((*cmd_ctx)->lsm); | |
5461b305 | 505 | } |
a2fb29a5 DG |
506 | free(*cmd_ctx); |
507 | *cmd_ctx = NULL; | |
5461b305 DG |
508 | } |
509 | } | |
510 | ||
f3ed775e | 511 | /* |
d063d709 | 512 | * Send all stream fds of kernel channel to the consumer. |
f3ed775e | 513 | */ |
2bdd86d4 | 514 | static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, |
6df2e2c9 MD |
515 | int sock, struct ltt_kernel_channel *channel, |
516 | uid_t uid, gid_t gid) | |
f3ed775e DG |
517 | { |
518 | int ret; | |
f3ed775e | 519 | struct ltt_kernel_stream *stream; |
3bd1e081 | 520 | struct lttcomm_consumer_msg lkm; |
f3ed775e | 521 | |
3bd1e081 | 522 | DBG("Sending streams of channel %s to kernel consumer", |
54d01ffb | 523 | channel->channel->name); |
f3ed775e | 524 | |
3bd1e081 MD |
525 | /* Send channel */ |
526 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
527 | lkm.u.channel.channel_key = channel->fd; | |
528 | lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size; | |
529 | lkm.u.channel.mmap_len = 0; /* for kernel */ | |
2bdd86d4 | 530 | DBG("Sending channel %d to consumer", lkm.u.channel.channel_key); |
3bd1e081 | 531 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); |
f3ed775e | 532 | if (ret < 0) { |
3bd1e081 | 533 | perror("send consumer channel"); |
f3ed775e DG |
534 | goto error; |
535 | } | |
536 | ||
3bd1e081 | 537 | /* Send streams */ |
7a485870 | 538 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
3bd1e081 MD |
539 | if (!stream->fd) { |
540 | continue; | |
541 | } | |
542 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; | |
543 | lkm.u.stream.channel_key = channel->fd; | |
544 | lkm.u.stream.stream_key = stream->fd; | |
545 | lkm.u.stream.state = stream->state; | |
546 | lkm.u.stream.output = channel->channel->attr.output; | |
547 | lkm.u.stream.mmap_len = 0; /* for kernel */ | |
6df2e2c9 MD |
548 | lkm.u.stream.uid = uid; |
549 | lkm.u.stream.gid = gid; | |
3bd1e081 MD |
550 | strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1); |
551 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
552 | DBG("Sending stream %d to consumer", lkm.u.stream.stream_key); | |
553 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
554 | if (ret < 0) { | |
555 | perror("send consumer stream"); | |
556 | goto error; | |
557 | } | |
558 | ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1); | |
559 | if (ret < 0) { | |
560 | perror("send consumer stream ancillary data"); | |
561 | goto error; | |
f3ed775e DG |
562 | } |
563 | } | |
564 | ||
3bd1e081 | 565 | DBG("consumer channel streams sent"); |
f3ed775e DG |
566 | |
567 | return 0; | |
568 | ||
569 | error: | |
570 | return ret; | |
571 | } | |
572 | ||
573 | /* | |
d063d709 | 574 | * Send all stream fds of the kernel session to the consumer. |
f3ed775e | 575 | */ |
2bdd86d4 | 576 | static int send_kconsumer_session_streams(struct consumer_data *consumer_data, |
3bd1e081 | 577 | struct ltt_kernel_session *session) |
f3ed775e DG |
578 | { |
579 | int ret; | |
580 | struct ltt_kernel_channel *chan; | |
3bd1e081 MD |
581 | struct lttcomm_consumer_msg lkm; |
582 | int sock = session->consumer_fd; | |
7a485870 DG |
583 | |
584 | DBG("Sending metadata stream fd"); | |
585 | ||
2bdd86d4 | 586 | /* Extra protection. It's NOT supposed to be set to 0 at this point */ |
d9800920 | 587 | if (session->consumer_fd == 0) { |
3bd1e081 | 588 | session->consumer_fd = consumer_data->cmd_sock; |
d9800920 DG |
589 | } |
590 | ||
7a485870 | 591 | if (session->metadata_stream_fd != 0) { |
3bd1e081 MD |
592 | /* Send metadata channel fd */ |
593 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
594 | lkm.u.channel.channel_key = session->metadata->fd; | |
595 | lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size; | |
596 | lkm.u.channel.mmap_len = 0; /* for kernel */ | |
597 | DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key); | |
598 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
599 | if (ret < 0) { | |
600 | perror("send consumer channel"); | |
601 | goto error; | |
602 | } | |
603 | ||
604 | /* Send metadata stream fd */ | |
605 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; | |
606 | lkm.u.stream.channel_key = session->metadata->fd; | |
607 | lkm.u.stream.stream_key = session->metadata_stream_fd; | |
608 | lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; | |
609 | lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
610 | lkm.u.stream.mmap_len = 0; /* for kernel */ | |
6df2e2c9 MD |
611 | lkm.u.stream.uid = session->uid; |
612 | lkm.u.stream.gid = session->gid; | |
3bd1e081 MD |
613 | strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1); |
614 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
615 | DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key); | |
616 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
617 | if (ret < 0) { | |
618 | perror("send consumer stream"); | |
619 | goto error; | |
620 | } | |
621 | ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1); | |
7a485870 | 622 | if (ret < 0) { |
3bd1e081 | 623 | perror("send consumer stream"); |
7a485870 DG |
624 | goto error; |
625 | } | |
626 | } | |
f3ed775e | 627 | |
f3ed775e | 628 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
6df2e2c9 MD |
629 | ret = send_kconsumer_channel_streams(consumer_data, sock, chan, |
630 | session->uid, session->gid); | |
2bdd86d4 MD |
631 | if (ret < 0) { |
632 | goto error; | |
633 | } | |
634 | } | |
635 | ||
636 | DBG("consumer fds (metadata and channel streams) sent"); | |
637 | ||
638 | return 0; | |
639 | ||
640 | error: | |
641 | return ret; | |
642 | } | |
643 | ||
fac6795d | 644 | /* |
0fdd1e2c | 645 | * Notify UST applications using the shm mmap futex. |
fac6795d | 646 | */ |
0fdd1e2c | 647 | static int notify_ust_apps(int active) |
fac6795d | 648 | { |
0fdd1e2c | 649 | char *wait_shm_mmap; |
fac6795d | 650 | |
0fdd1e2c | 651 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 652 | |
0fdd1e2c DG |
653 | /* See shm.c for this call implying mmap, shm and futex calls */ |
654 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
655 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
656 | goto error; |
657 | } | |
658 | ||
0fdd1e2c DG |
659 | /* Wake waiting process */ |
660 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
661 | ||
662 | /* Apps notified successfully */ | |
663 | return 0; | |
fac6795d DG |
664 | |
665 | error: | |
0fdd1e2c | 666 | return -1; |
fac6795d DG |
667 | } |
668 | ||
e065084a | 669 | /* |
d063d709 DG |
670 | * Setup the outgoing data buffer for the response (llm) by allocating the |
671 | * right amount of memory and copying the original information from the lsm | |
672 | * structure. | |
ca95a216 | 673 | * |
d063d709 | 674 | * Return total size of the buffer pointed by buf. |
ca95a216 | 675 | */ |
5461b305 | 676 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 677 | { |
f3ed775e | 678 | int ret, buf_size; |
ca95a216 | 679 | |
f3ed775e | 680 | buf_size = size; |
5461b305 | 681 | |
ba7f0ae5 | 682 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 683 | if (cmd_ctx->llm == NULL) { |
ba7f0ae5 | 684 | perror("zmalloc"); |
5461b305 | 685 | ret = -ENOMEM; |
ca95a216 DG |
686 | goto error; |
687 | } | |
688 | ||
5461b305 DG |
689 | /* Copy common data */ |
690 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 691 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 692 | |
5461b305 DG |
693 | cmd_ctx->llm->data_size = size; |
694 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
695 | ||
ca95a216 DG |
696 | return buf_size; |
697 | ||
698 | error: | |
699 | return ret; | |
700 | } | |
701 | ||
7a485870 | 702 | /* |
5eb91c98 | 703 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 704 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 705 | */ |
5eb91c98 | 706 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 707 | { |
5eb91c98 | 708 | int ret; |
7a485870 DG |
709 | struct ltt_session *session; |
710 | struct ltt_kernel_channel *channel; | |
711 | ||
5eb91c98 | 712 | DBG("Updating kernel poll set"); |
7a485870 | 713 | |
54d01ffb | 714 | session_lock_list(); |
b5541356 | 715 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 716 | session_lock(session); |
7a485870 | 717 | if (session->kernel_session == NULL) { |
54d01ffb | 718 | session_unlock(session); |
7a485870 DG |
719 | continue; |
720 | } | |
7a485870 | 721 | |
54d01ffb DG |
722 | cds_list_for_each_entry(channel, |
723 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
724 | /* Add channel fd to the kernel poll set */ |
725 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
726 | if (ret < 0) { | |
54d01ffb | 727 | session_unlock(session); |
5eb91c98 DG |
728 | goto error; |
729 | } | |
730 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 731 | } |
54d01ffb | 732 | session_unlock(session); |
7a485870 | 733 | } |
54d01ffb | 734 | session_unlock_list(); |
7a485870 | 735 | |
5eb91c98 | 736 | return 0; |
7a485870 DG |
737 | |
738 | error: | |
54d01ffb | 739 | session_unlock_list(); |
7a485870 DG |
740 | return -1; |
741 | } | |
742 | ||
743 | /* | |
54d01ffb | 744 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 745 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 746 | * |
d063d709 | 747 | * Useful for CPU hotplug feature. |
7a485870 | 748 | */ |
2bdd86d4 | 749 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
750 | { |
751 | int ret = 0; | |
752 | struct ltt_session *session; | |
753 | struct ltt_kernel_channel *channel; | |
754 | ||
755 | DBG("Updating kernel streams for channel fd %d", fd); | |
756 | ||
54d01ffb | 757 | session_lock_list(); |
b5541356 | 758 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 759 | session_lock(session); |
7a485870 | 760 | if (session->kernel_session == NULL) { |
54d01ffb | 761 | session_unlock(session); |
7a485870 DG |
762 | continue; |
763 | } | |
d9800920 DG |
764 | |
765 | /* This is not suppose to be 0 but this is an extra security check */ | |
766 | if (session->kernel_session->consumer_fd == 0) { | |
3bd1e081 | 767 | session->kernel_session->consumer_fd = consumer_data->cmd_sock; |
d9800920 DG |
768 | } |
769 | ||
5eb91c98 DG |
770 | cds_list_for_each_entry(channel, |
771 | &session->kernel_session->channel_list.head, list) { | |
7a485870 DG |
772 | if (channel->fd == fd) { |
773 | DBG("Channel found, updating kernel streams"); | |
774 | ret = kernel_open_channel_stream(channel); | |
775 | if (ret < 0) { | |
b3c750d2 | 776 | goto error; |
7a485870 | 777 | } |
d9800920 | 778 | |
7a485870 | 779 | /* |
5eb91c98 DG |
780 | * Have we already sent fds to the consumer? If yes, it means |
781 | * that tracing is started so it is safe to send our updated | |
782 | * stream fds. | |
7a485870 | 783 | */ |
3bd1e081 | 784 | if (session->kernel_session->consumer_fds_sent == 1) { |
2bdd86d4 | 785 | ret = send_kconsumer_channel_streams(consumer_data, |
6df2e2c9 MD |
786 | session->kernel_session->consumer_fd, channel, |
787 | session->uid, session->gid); | |
7a485870 | 788 | if (ret < 0) { |
b3c750d2 | 789 | goto error; |
7a485870 DG |
790 | } |
791 | } | |
b3c750d2 | 792 | goto error; |
7a485870 DG |
793 | } |
794 | } | |
54d01ffb | 795 | session_unlock(session); |
7a485870 | 796 | } |
54d01ffb | 797 | session_unlock_list(); |
b3c750d2 | 798 | return ret; |
7a485870 | 799 | |
b3c750d2 | 800 | error: |
54d01ffb DG |
801 | session_unlock(session); |
802 | session_unlock_list(); | |
7a485870 DG |
803 | return ret; |
804 | } | |
805 | ||
487cf67c DG |
806 | /* |
807 | * For each tracing session, update newly registered apps. | |
808 | */ | |
809 | static void update_ust_app(int app_sock) | |
810 | { | |
811 | struct ltt_session *sess, *stmp; | |
812 | ||
813 | /* For all tracing session(s) */ | |
814 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
421cb601 DG |
815 | if (sess->ust_session) { |
816 | ust_app_global_update(sess->ust_session, app_sock); | |
817 | } | |
487cf67c DG |
818 | } |
819 | } | |
820 | ||
7a485870 | 821 | /* |
d063d709 | 822 | * This thread manage event coming from the kernel. |
7a485870 | 823 | * |
d063d709 DG |
824 | * Features supported in this thread: |
825 | * -) CPU Hotplug | |
7a485870 DG |
826 | */ |
827 | static void *thread_manage_kernel(void *data) | |
828 | { | |
5eb91c98 DG |
829 | int ret, i, pollfd, update_poll_flag = 1; |
830 | uint32_t revents, nb_fd; | |
7a485870 | 831 | char tmp; |
5eb91c98 | 832 | struct lttng_poll_event events; |
7a485870 DG |
833 | |
834 | DBG("Thread manage kernel started"); | |
835 | ||
5eb91c98 DG |
836 | ret = create_thread_poll_set(&events, 2); |
837 | if (ret < 0) { | |
838 | goto error; | |
839 | } | |
840 | ||
841 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
842 | if (ret < 0) { | |
843 | goto error; | |
844 | } | |
845 | ||
7a485870 DG |
846 | while (1) { |
847 | if (update_poll_flag == 1) { | |
5f822d0a DG |
848 | /* |
849 | * Reset number of fd in the poll set. Always 2 since there is the thread | |
850 | * quit pipe and the kernel pipe. | |
851 | */ | |
852 | events.nb_fd = 2; | |
853 | ||
5eb91c98 DG |
854 | ret = update_kernel_poll(&events); |
855 | if (ret < 0) { | |
7a485870 DG |
856 | goto error; |
857 | } | |
858 | update_poll_flag = 0; | |
859 | } | |
860 | ||
5eb91c98 DG |
861 | nb_fd = LTTNG_POLL_GETNB(&events); |
862 | ||
863 | DBG("Thread kernel polling on %d fds", nb_fd); | |
864 | ||
865 | /* Zeroed the poll events */ | |
866 | lttng_poll_reset(&events); | |
7a485870 DG |
867 | |
868 | /* Poll infinite value of time */ | |
5eb91c98 | 869 | ret = lttng_poll_wait(&events, -1); |
7a485870 | 870 | if (ret < 0) { |
7a485870 DG |
871 | goto error; |
872 | } else if (ret == 0) { | |
873 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
874 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
875 | "This should not have happened! Continuing..."); | |
7a485870 DG |
876 | continue; |
877 | } | |
878 | ||
5eb91c98 DG |
879 | for (i = 0; i < nb_fd; i++) { |
880 | /* Fetch once the poll data */ | |
881 | revents = LTTNG_POLL_GETEV(&events, i); | |
882 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 883 | |
5eb91c98 DG |
884 | /* Thread quit pipe has been closed. Killing thread. */ |
885 | ret = check_thread_quit_pipe(pollfd, revents); | |
886 | if (ret) { | |
887 | goto error; | |
888 | } | |
7a485870 | 889 | |
5eb91c98 DG |
890 | /* Check for data on kernel pipe */ |
891 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
892 | ret = read(kernel_poll_pipe[0], &tmp, 1); | |
893 | update_poll_flag = 1; | |
894 | continue; | |
895 | } else { | |
896 | /* | |
897 | * New CPU detected by the kernel. Adding kernel stream to | |
898 | * kernel session and updating the kernel consumer | |
899 | */ | |
900 | if (revents & LPOLLIN) { | |
2bdd86d4 | 901 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
902 | if (ret < 0) { |
903 | continue; | |
904 | } | |
905 | break; | |
906 | /* | |
907 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
908 | * and unregister kernel stream at this point. | |
909 | */ | |
7a485870 | 910 | } |
7a485870 DG |
911 | } |
912 | } | |
913 | } | |
914 | ||
915 | error: | |
916 | DBG("Kernel thread dying"); | |
273ea72c DG |
917 | close(kernel_poll_pipe[0]); |
918 | close(kernel_poll_pipe[1]); | |
5eb91c98 DG |
919 | |
920 | lttng_poll_clean(&events); | |
921 | ||
7a485870 DG |
922 | return NULL; |
923 | } | |
924 | ||
1d4b027a | 925 | /* |
3bd1e081 | 926 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 927 | */ |
3bd1e081 | 928 | static void *thread_manage_consumer(void *data) |
1d4b027a | 929 | { |
5eb91c98 DG |
930 | int sock = 0, i, ret, pollfd; |
931 | uint32_t revents, nb_fd; | |
1d4b027a | 932 | enum lttcomm_return_code code; |
5eb91c98 | 933 | struct lttng_poll_event events; |
3bd1e081 | 934 | struct consumer_data *consumer_data = data; |
1d4b027a | 935 | |
3bd1e081 | 936 | DBG("[thread] Manage consumer started"); |
1d4b027a | 937 | |
3bd1e081 | 938 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
939 | if (ret < 0) { |
940 | goto error; | |
941 | } | |
942 | ||
5eb91c98 DG |
943 | /* |
944 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. | |
945 | * Nothing more will be added to this poll set. | |
946 | */ | |
947 | ret = create_thread_poll_set(&events, 2); | |
948 | if (ret < 0) { | |
949 | goto error; | |
950 | } | |
273ea72c | 951 | |
3bd1e081 | 952 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
953 | if (ret < 0) { |
954 | goto error; | |
955 | } | |
956 | ||
957 | nb_fd = LTTNG_POLL_GETNB(&events); | |
273ea72c DG |
958 | |
959 | /* Inifinite blocking call, waiting for transmission */ | |
5eb91c98 | 960 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 961 | if (ret < 0) { |
273ea72c DG |
962 | goto error; |
963 | } | |
964 | ||
5eb91c98 DG |
965 | for (i = 0; i < nb_fd; i++) { |
966 | /* Fetch once the poll data */ | |
967 | revents = LTTNG_POLL_GETEV(&events, i); | |
968 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
969 | ||
970 | /* Thread quit pipe has been closed. Killing thread. */ | |
971 | ret = check_thread_quit_pipe(pollfd, revents); | |
972 | if (ret) { | |
973 | goto error; | |
974 | } | |
975 | ||
976 | /* Event on the registration socket */ | |
3bd1e081 | 977 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 978 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 979 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
980 | goto error; |
981 | } | |
982 | } | |
273ea72c DG |
983 | } |
984 | ||
3bd1e081 | 985 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
986 | if (sock < 0) { |
987 | goto error; | |
988 | } | |
989 | ||
3bd1e081 | 990 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 991 | |
712ea556 | 992 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
993 | ret = lttcomm_recv_unix_sock(sock, &code, |
994 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
995 | if (ret <= 0) { |
996 | goto error; | |
997 | } | |
998 | ||
3bd1e081 MD |
999 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
1000 | consumer_data->cmd_sock = | |
1001 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
1002 | if (consumer_data->cmd_sock < 0) { | |
1003 | sem_post(&consumer_data->sem); | |
48842b30 | 1004 | PERROR("consumer connect"); |
1d4b027a DG |
1005 | goto error; |
1006 | } | |
1007 | /* Signal condition to tell that the kconsumerd is ready */ | |
3bd1e081 MD |
1008 | sem_post(&consumer_data->sem); |
1009 | DBG("consumer command socket ready"); | |
1d4b027a | 1010 | } else { |
3bd1e081 | 1011 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1012 | lttcomm_get_readable_code(-code)); |
1013 | goto error; | |
1014 | } | |
1015 | ||
54d01ffb | 1016 | /* Remove the kconsumerd error sock since we've established a connexion */ |
3bd1e081 | 1017 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1018 | if (ret < 0) { |
72079cae DG |
1019 | goto error; |
1020 | } | |
1021 | ||
5eb91c98 DG |
1022 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1023 | if (ret < 0) { | |
72079cae | 1024 | goto error; |
5eb91c98 DG |
1025 | } |
1026 | ||
1027 | /* Update number of fd */ | |
1028 | nb_fd = LTTNG_POLL_GETNB(&events); | |
1029 | ||
1030 | /* Inifinite blocking call, waiting for transmission */ | |
1031 | ret = lttng_poll_wait(&events, -1); | |
1032 | if (ret < 0) { | |
72079cae DG |
1033 | goto error; |
1034 | } | |
1035 | ||
5eb91c98 DG |
1036 | for (i = 0; i < nb_fd; i++) { |
1037 | /* Fetch once the poll data */ | |
1038 | revents = LTTNG_POLL_GETEV(&events, i); | |
1039 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1040 | ||
1041 | /* Thread quit pipe has been closed. Killing thread. */ | |
1042 | ret = check_thread_quit_pipe(pollfd, revents); | |
1043 | if (ret) { | |
1044 | goto error; | |
1045 | } | |
1046 | ||
1047 | /* Event on the kconsumerd socket */ | |
1048 | if (pollfd == sock) { | |
1049 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 1050 | ERR("consumer err socket second poll error"); |
5eb91c98 DG |
1051 | goto error; |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | ||
712ea556 | 1056 | /* Wait for any kconsumerd error */ |
54d01ffb DG |
1057 | ret = lttcomm_recv_unix_sock(sock, &code, |
1058 | sizeof(enum lttcomm_return_code)); | |
712ea556 | 1059 | if (ret <= 0) { |
3bd1e081 | 1060 | ERR("consumer closed the command socket"); |
712ea556 | 1061 | goto error; |
6f61d021 | 1062 | } |
1d4b027a | 1063 | |
3bd1e081 | 1064 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
712ea556 | 1065 | |
1d4b027a | 1066 | error: |
3bd1e081 MD |
1067 | DBG("consumer thread dying"); |
1068 | close(consumer_data->err_sock); | |
1069 | close(consumer_data->cmd_sock); | |
5eb91c98 | 1070 | close(sock); |
273ea72c | 1071 | |
3bd1e081 MD |
1072 | unlink(consumer_data->err_unix_sock_path); |
1073 | unlink(consumer_data->cmd_unix_sock_path); | |
1074 | consumer_data->pid = 0; | |
1d4b027a | 1075 | |
5eb91c98 | 1076 | lttng_poll_clean(&events); |
0177d773 | 1077 | |
5eb91c98 | 1078 | return NULL; |
099e26bd DG |
1079 | } |
1080 | ||
099e26bd DG |
1081 | /* |
1082 | * This thread manage application communication. | |
1d4b027a DG |
1083 | */ |
1084 | static void *thread_manage_apps(void *data) | |
099e26bd | 1085 | { |
5eb91c98 DG |
1086 | int i, ret, pollfd; |
1087 | uint32_t revents, nb_fd; | |
099e26bd | 1088 | struct ust_command ust_cmd; |
5eb91c98 | 1089 | struct lttng_poll_event events; |
099e26bd DG |
1090 | |
1091 | DBG("[thread] Manage application started"); | |
1092 | ||
f6a9efaa DG |
1093 | rcu_register_thread(); |
1094 | rcu_thread_online(); | |
1095 | ||
5eb91c98 DG |
1096 | ret = create_thread_poll_set(&events, 2); |
1097 | if (ret < 0) { | |
1098 | goto error; | |
1099 | } | |
099e26bd | 1100 | |
5eb91c98 DG |
1101 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1102 | if (ret < 0) { | |
1103 | goto error; | |
1104 | } | |
099e26bd | 1105 | |
5eb91c98 DG |
1106 | while (1) { |
1107 | /* Zeroed the events structure */ | |
1108 | lttng_poll_reset(&events); | |
0177d773 | 1109 | |
5eb91c98 | 1110 | nb_fd = LTTNG_POLL_GETNB(&events); |
099e26bd DG |
1111 | |
1112 | DBG("Apps thread polling on %d fds", nb_fd); | |
1113 | ||
1114 | /* Inifinite blocking call, waiting for transmission */ | |
5eb91c98 | 1115 | ret = lttng_poll_wait(&events, -1); |
099e26bd | 1116 | if (ret < 0) { |
099e26bd DG |
1117 | goto error; |
1118 | } | |
1119 | ||
5eb91c98 DG |
1120 | for (i = 0; i < nb_fd; i++) { |
1121 | /* Fetch once the poll data */ | |
1122 | revents = LTTNG_POLL_GETEV(&events, i); | |
1123 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1124 | ||
1125 | /* Thread quit pipe has been closed. Killing thread. */ | |
1126 | ret = check_thread_quit_pipe(pollfd, revents); | |
1127 | if (ret) { | |
099e26bd | 1128 | goto error; |
5eb91c98 | 1129 | } |
099e26bd | 1130 | |
5eb91c98 DG |
1131 | /* Inspect the apps cmd pipe */ |
1132 | if (pollfd == apps_cmd_pipe[0]) { | |
1133 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1134 | ERR("Apps command pipe error"); | |
0177d773 | 1135 | goto error; |
5eb91c98 DG |
1136 | } else if (revents & LPOLLIN) { |
1137 | /* Empty pipe */ | |
1138 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); | |
1139 | if (ret < 0 || ret < sizeof(ust_cmd)) { | |
1140 | perror("read apps cmd pipe"); | |
1141 | goto error; | |
1142 | } | |
099e26bd | 1143 | |
5eb91c98 | 1144 | /* Register applicaton to the session daemon */ |
56fff090 | 1145 | ret = ust_app_register(&ust_cmd.reg_msg, |
54d01ffb | 1146 | ust_cmd.sock); |
88ff5b7f | 1147 | if (ret == -ENOMEM) { |
5eb91c98 | 1148 | goto error; |
88ff5b7f MD |
1149 | } else if (ret < 0) { |
1150 | break; | |
5eb91c98 DG |
1151 | } |
1152 | ||
acc7b41b MD |
1153 | /* |
1154 | * Add channel(s) and event(s) to newly registered apps | |
1155 | * from lttng global UST domain. | |
1156 | */ | |
1157 | update_ust_app(ust_cmd.sock); | |
1158 | ||
5eb91c98 DG |
1159 | ret = ustctl_register_done(ust_cmd.sock); |
1160 | if (ret < 0) { | |
1161 | /* | |
1162 | * If the registration is not possible, we simply | |
1163 | * unregister the apps and continue | |
1164 | */ | |
56fff090 | 1165 | ust_app_unregister(ust_cmd.sock); |
5eb91c98 DG |
1166 | } else { |
1167 | /* | |
1168 | * We just need here to monitor the close of the UST | |
1169 | * socket and poll set monitor those by default. | |
1170 | */ | |
1171 | ret = lttng_poll_add(&events, ust_cmd.sock, 0); | |
1172 | if (ret < 0) { | |
1173 | goto error; | |
1174 | } | |
1175 | ||
54d01ffb DG |
1176 | DBG("Apps with sock %d added to poll set", |
1177 | ust_cmd.sock); | |
5eb91c98 | 1178 | } |
487cf67c | 1179 | |
5eb91c98 | 1180 | break; |
0177d773 | 1181 | } |
5eb91c98 DG |
1182 | } else { |
1183 | /* | |
54d01ffb DG |
1184 | * At this point, we know that a registered application made |
1185 | * the event at poll_wait. | |
5eb91c98 DG |
1186 | */ |
1187 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1188 | /* Removing from the poll set */ | |
1189 | ret = lttng_poll_del(&events, pollfd); | |
1190 | if (ret < 0) { | |
1191 | goto error; | |
1192 | } | |
099e26bd | 1193 | |
b9d9b220 | 1194 | /* Socket closed on remote end. */ |
56fff090 | 1195 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1196 | break; |
1197 | } | |
099e26bd DG |
1198 | } |
1199 | } | |
099e26bd DG |
1200 | } |
1201 | ||
1202 | error: | |
1203 | DBG("Application communication apps dying"); | |
1204 | close(apps_cmd_pipe[0]); | |
1205 | close(apps_cmd_pipe[1]); | |
1206 | ||
5eb91c98 | 1207 | lttng_poll_clean(&events); |
099e26bd | 1208 | |
f6a9efaa DG |
1209 | rcu_thread_offline(); |
1210 | rcu_unregister_thread(); | |
099e26bd DG |
1211 | return NULL; |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Dispatch request from the registration threads to the application | |
1216 | * communication thread. | |
1217 | */ | |
1218 | static void *thread_dispatch_ust_registration(void *data) | |
1219 | { | |
1220 | int ret; | |
1221 | struct cds_wfq_node *node; | |
1222 | struct ust_command *ust_cmd = NULL; | |
1223 | ||
1224 | DBG("[thread] Dispatch UST command started"); | |
1225 | ||
1226 | while (!dispatch_thread_exit) { | |
1227 | /* Atomically prepare the queue futex */ | |
1228 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1229 | ||
1230 | do { | |
1231 | /* Dequeue command for registration */ | |
1232 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1233 | if (node == NULL) { | |
00a17c97 | 1234 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1235 | /* Continue thread execution */ |
1236 | break; | |
1237 | } | |
1238 | ||
1239 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1240 | ||
2f50c8a3 DG |
1241 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1242 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1243 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1244 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1245 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1246 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
099e26bd DG |
1247 | /* |
1248 | * Inform apps thread of the new application registration. This | |
1249 | * call is blocking so we can be assured that the data will be read | |
1250 | * at some point in time or wait to the end of the world :) | |
1251 | */ | |
1252 | ret = write(apps_cmd_pipe[1], ust_cmd, | |
1253 | sizeof(struct ust_command)); | |
1254 | if (ret < 0) { | |
1255 | perror("write apps cmd pipe"); | |
1256 | if (errno == EBADF) { | |
1257 | /* | |
1258 | * We can't inform the application thread to process | |
1259 | * registration. We will exit or else application | |
1260 | * registration will not occur and tracing will never | |
1261 | * start. | |
1262 | */ | |
1263 | goto error; | |
1264 | } | |
1265 | } | |
1266 | free(ust_cmd); | |
1267 | } while (node != NULL); | |
1268 | ||
1269 | /* Futex wait on queue. Blocking call on futex() */ | |
1270 | futex_nto1_wait(&ust_cmd_queue.futex); | |
1271 | } | |
1272 | ||
1273 | error: | |
1274 | DBG("Dispatch thread dying"); | |
1275 | return NULL; | |
1276 | } | |
1277 | ||
1278 | /* | |
1279 | * This thread manage application registration. | |
1280 | */ | |
1281 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1282 | { |
5eb91c98 DG |
1283 | int sock = 0, i, ret, pollfd; |
1284 | uint32_t revents, nb_fd; | |
1285 | struct lttng_poll_event events; | |
099e26bd DG |
1286 | /* |
1287 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1288 | * freed in the manage apps thread. | |
1289 | */ | |
1290 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1291 | |
099e26bd | 1292 | DBG("[thread] Manage application registration started"); |
1d4b027a DG |
1293 | |
1294 | ret = lttcomm_listen_unix_sock(apps_sock); | |
1295 | if (ret < 0) { | |
1296 | goto error; | |
1297 | } | |
1298 | ||
5eb91c98 DG |
1299 | /* |
1300 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1301 | * more will be added to this poll set. | |
1302 | */ | |
1303 | ret = create_thread_poll_set(&events, 2); | |
1304 | if (ret < 0) { | |
1305 | goto error; | |
1306 | } | |
273ea72c | 1307 | |
5eb91c98 DG |
1308 | /* Add the application registration socket */ |
1309 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1310 | if (ret < 0) { | |
1311 | goto error; | |
1312 | } | |
273ea72c | 1313 | |
1d4b027a | 1314 | /* Notify all applications to register */ |
0fdd1e2c DG |
1315 | ret = notify_ust_apps(1); |
1316 | if (ret < 0) { | |
1317 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1318 | "Execution continues but there might be problem for already\n" |
1319 | "running applications that wishes to register."); | |
0fdd1e2c | 1320 | } |
1d4b027a DG |
1321 | |
1322 | while (1) { | |
1323 | DBG("Accepting application registration"); | |
273ea72c | 1324 | |
5eb91c98 DG |
1325 | nb_fd = LTTNG_POLL_GETNB(&events); |
1326 | ||
273ea72c | 1327 | /* Inifinite blocking call, waiting for transmission */ |
5eb91c98 | 1328 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 1329 | if (ret < 0) { |
273ea72c DG |
1330 | goto error; |
1331 | } | |
1332 | ||
5eb91c98 DG |
1333 | for (i = 0; i < nb_fd; i++) { |
1334 | /* Fetch once the poll data */ | |
1335 | revents = LTTNG_POLL_GETEV(&events, i); | |
1336 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1337 | |
5eb91c98 DG |
1338 | /* Thread quit pipe has been closed. Killing thread. */ |
1339 | ret = check_thread_quit_pipe(pollfd, revents); | |
1340 | if (ret) { | |
90014c57 DG |
1341 | goto error; |
1342 | } | |
1d4b027a | 1343 | |
5eb91c98 DG |
1344 | /* Event on the registration socket */ |
1345 | if (pollfd == apps_sock) { | |
1346 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1347 | ERR("Register apps socket poll error"); | |
1348 | goto error; | |
1349 | } else if (revents & LPOLLIN) { | |
1350 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1351 | if (sock < 0) { | |
1352 | goto error; | |
1353 | } | |
099e26bd | 1354 | |
5eb91c98 | 1355 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1356 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1357 | if (ust_cmd == NULL) { |
ba7f0ae5 | 1358 | perror("ust command zmalloc"); |
5eb91c98 DG |
1359 | goto error; |
1360 | } | |
1d4b027a | 1361 | |
5eb91c98 DG |
1362 | /* |
1363 | * Using message-based transmissions to ensure we don't | |
1364 | * have to deal with partially received messages. | |
1365 | */ | |
1366 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, | |
1367 | sizeof(struct ust_register_msg)); | |
1368 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { | |
1369 | if (ret < 0) { | |
1370 | perror("lttcomm_recv_unix_sock register apps"); | |
1371 | } else { | |
1372 | ERR("Wrong size received on apps register"); | |
1373 | } | |
1374 | free(ust_cmd); | |
1375 | close(sock); | |
1376 | continue; | |
1377 | } | |
099e26bd | 1378 | |
5eb91c98 | 1379 | ust_cmd->sock = sock; |
099e26bd | 1380 | |
5eb91c98 DG |
1381 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1382 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1383 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1384 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1385 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1386 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1387 | |
5eb91c98 DG |
1388 | /* |
1389 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1390 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1391 | */ |
1392 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1393 | ||
1394 | /* | |
1395 | * Wake the registration queue futex. Implicit memory | |
1396 | * barrier with the exchange in cds_wfq_enqueue. | |
1397 | */ | |
1398 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1399 | } | |
1400 | } | |
90014c57 | 1401 | } |
1d4b027a DG |
1402 | } |
1403 | ||
1404 | error: | |
0fdd1e2c DG |
1405 | DBG("UST Registration thread dying"); |
1406 | ||
1407 | /* Notify that the registration thread is gone */ | |
1408 | notify_ust_apps(0); | |
1409 | ||
1410 | close(apps_sock); | |
1411 | close(sock); | |
273ea72c | 1412 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1413 | |
5eb91c98 DG |
1414 | lttng_poll_clean(&events); |
1415 | ||
1d4b027a DG |
1416 | return NULL; |
1417 | } | |
1418 | ||
8c0faa1d | 1419 | /* |
3bd1e081 | 1420 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1421 | * exec or it will fails. |
8c0faa1d | 1422 | */ |
3bd1e081 | 1423 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d DG |
1424 | { |
1425 | int ret; | |
ee0b0061 DG |
1426 | struct timespec timeout; |
1427 | ||
1428 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; | |
1429 | timeout.tv_nsec = 0; | |
8c0faa1d DG |
1430 | |
1431 | /* Setup semaphore */ | |
3bd1e081 | 1432 | ret = sem_init(&consumer_data->sem, 0, 0); |
ee0b0061 | 1433 | if (ret < 0) { |
3bd1e081 | 1434 | PERROR("sem_init consumer semaphore"); |
ee0b0061 DG |
1435 | goto error; |
1436 | } | |
8c0faa1d | 1437 | |
3bd1e081 MD |
1438 | ret = pthread_create(&consumer_data->thread, NULL, |
1439 | thread_manage_consumer, consumer_data); | |
8c0faa1d | 1440 | if (ret != 0) { |
3bd1e081 | 1441 | PERROR("pthread_create consumer"); |
ee0b0061 | 1442 | ret = -1; |
8c0faa1d DG |
1443 | goto error; |
1444 | } | |
1445 | ||
ee0b0061 DG |
1446 | /* Get time for sem_timedwait absolute timeout */ |
1447 | ret = clock_gettime(CLOCK_REALTIME, &timeout); | |
1448 | if (ret < 0) { | |
3bd1e081 | 1449 | PERROR("clock_gettime spawn consumer"); |
ee0b0061 | 1450 | /* Infinite wait for the kconsumerd thread to be ready */ |
3bd1e081 | 1451 | ret = sem_wait(&consumer_data->sem); |
ee0b0061 DG |
1452 | } else { |
1453 | /* Normal timeout if the gettime was successful */ | |
1454 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
3bd1e081 | 1455 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
ee0b0061 | 1456 | } |
8c0faa1d | 1457 | |
ee0b0061 DG |
1458 | if (ret < 0) { |
1459 | if (errno == ETIMEDOUT) { | |
1460 | /* | |
1461 | * Call has timed out so we kill the kconsumerd_thread and return | |
1462 | * an error. | |
1463 | */ | |
3bd1e081 MD |
1464 | ERR("The consumer thread was never ready. Killing it"); |
1465 | ret = pthread_cancel(consumer_data->thread); | |
ee0b0061 | 1466 | if (ret < 0) { |
3bd1e081 | 1467 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
1468 | } |
1469 | } else { | |
3bd1e081 | 1470 | PERROR("semaphore wait failed consumer thread"); |
ee0b0061 DG |
1471 | } |
1472 | goto error; | |
1473 | } | |
1474 | ||
3bd1e081 MD |
1475 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1476 | if (consumer_data->pid == 0) { | |
712ea556 | 1477 | ERR("Kconsumerd did not start"); |
3bd1e081 | 1478 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
1479 | goto error; |
1480 | } | |
3bd1e081 | 1481 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 1482 | |
8c0faa1d DG |
1483 | return 0; |
1484 | ||
1485 | error: | |
1486 | return ret; | |
1487 | } | |
1488 | ||
d9800920 | 1489 | /* |
3bd1e081 | 1490 | * Join consumer thread |
d9800920 | 1491 | */ |
3bd1e081 | 1492 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
1493 | { |
1494 | void *status; | |
1495 | int ret; | |
1496 | ||
3bd1e081 MD |
1497 | if (consumer_data->pid != 0) { |
1498 | ret = kill(consumer_data->pid, SIGTERM); | |
cf3af59e | 1499 | if (ret) { |
3bd1e081 | 1500 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
1501 | return ret; |
1502 | } | |
3bd1e081 | 1503 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
1504 | } else { |
1505 | return 0; | |
1506 | } | |
1507 | } | |
1508 | ||
8c0faa1d | 1509 | /* |
3bd1e081 | 1510 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 1511 | * |
d063d709 | 1512 | * Return pid if successful else -1. |
8c0faa1d | 1513 | */ |
3bd1e081 | 1514 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
1515 | { |
1516 | int ret; | |
1517 | pid_t pid; | |
94c55f17 | 1518 | const char *consumer_to_use; |
53086306 | 1519 | const char *verbosity; |
94c55f17 | 1520 | struct stat st; |
8c0faa1d | 1521 | |
3bd1e081 | 1522 | DBG("Spawning consumerd"); |
c49dc785 | 1523 | |
8c0faa1d DG |
1524 | pid = fork(); |
1525 | if (pid == 0) { | |
1526 | /* | |
3bd1e081 | 1527 | * Exec consumerd. |
8c0faa1d | 1528 | */ |
3bd1e081 | 1529 | if (opt_verbose > 1 || opt_verbose_consumer) { |
53086306 DG |
1530 | verbosity = "--verbose"; |
1531 | } else { | |
1532 | verbosity = "--quiet"; | |
1533 | } | |
3bd1e081 MD |
1534 | switch (consumer_data->type) { |
1535 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 1536 | /* |
f806231f MD |
1537 | * Find out which consumerd to execute. We will |
1538 | * first try the 64-bit path, then the | |
1539 | * sessiond's installation directory, and | |
1540 | * fallback on the 32-bit one, | |
94c55f17 AM |
1541 | */ |
1542 | if (stat(consumerd64_bin, &st) == 0) { | |
1543 | consumer_to_use = consumerd64_bin; | |
94c55f17 AM |
1544 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
1545 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
eb1e0bd4 MD |
1546 | } else if (stat(consumerd32_bin, &st) == 0) { |
1547 | consumer_to_use = consumerd32_bin; | |
94c55f17 AM |
1548 | } else { |
1549 | break; | |
1550 | } | |
1551 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
1552 | execl(consumer_to_use, | |
1553 | "lttng-consumerd", verbosity, "-k", | |
1554 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
1555 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1556 | NULL); | |
3bd1e081 | 1557 | break; |
7753dea8 MD |
1558 | case LTTNG_CONSUMER64_UST: |
1559 | { | |
b1e0b6b6 | 1560 | char *tmpnew = NULL; |
8f4905da MD |
1561 | |
1562 | if (consumerd64_libdir[0] != '\0') { | |
1563 | char *tmp; | |
1564 | size_t tmplen; | |
1565 | ||
1566 | tmp = getenv("LD_LIBRARY_PATH"); | |
1567 | if (!tmp) { | |
1568 | tmp = ""; | |
1569 | } | |
1570 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1571 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
1572 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1573 | if (!tmpnew) { | |
1574 | ret = -ENOMEM; | |
1575 | goto error; | |
1576 | } | |
1577 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1578 | strcat(tmpnew, consumerd64_libdir); | |
1579 | if (tmp[0] != '\0') { | |
1580 | strcat(tmpnew, ":"); | |
1581 | strcat(tmpnew, tmp); | |
1582 | } | |
1583 | ret = putenv(tmpnew); | |
1584 | if (ret) { | |
1585 | ret = -errno; | |
1586 | goto error; | |
1587 | } | |
1588 | } | |
94c55f17 | 1589 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 1590 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1591 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1592 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1593 | NULL); | |
8f4905da MD |
1594 | if (consumerd64_libdir[0] != '\0') { |
1595 | free(tmpnew); | |
1596 | } | |
1597 | if (ret) { | |
1598 | goto error; | |
1599 | } | |
3bd1e081 | 1600 | break; |
7753dea8 MD |
1601 | } |
1602 | case LTTNG_CONSUMER32_UST: | |
1603 | { | |
937dde8e | 1604 | char *tmpnew = NULL; |
8f4905da MD |
1605 | |
1606 | if (consumerd32_libdir[0] != '\0') { | |
1607 | char *tmp; | |
1608 | size_t tmplen; | |
1609 | ||
1610 | tmp = getenv("LD_LIBRARY_PATH"); | |
1611 | if (!tmp) { | |
1612 | tmp = ""; | |
1613 | } | |
1614 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1615 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
1616 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1617 | if (!tmpnew) { | |
1618 | ret = -ENOMEM; | |
1619 | goto error; | |
1620 | } | |
1621 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1622 | strcat(tmpnew, consumerd32_libdir); | |
1623 | if (tmp[0] != '\0') { | |
1624 | strcat(tmpnew, ":"); | |
1625 | strcat(tmpnew, tmp); | |
1626 | } | |
1627 | ret = putenv(tmpnew); | |
1628 | if (ret) { | |
1629 | ret = -errno; | |
1630 | goto error; | |
1631 | } | |
1632 | } | |
94c55f17 | 1633 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 1634 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1635 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1636 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1637 | NULL); | |
8f4905da MD |
1638 | if (consumerd32_libdir[0] != '\0') { |
1639 | free(tmpnew); | |
1640 | } | |
1641 | if (ret) { | |
1642 | goto error; | |
1643 | } | |
7753dea8 MD |
1644 | break; |
1645 | } | |
3bd1e081 MD |
1646 | default: |
1647 | perror("unknown consumer type"); | |
1648 | exit(EXIT_FAILURE); | |
1649 | } | |
8c0faa1d DG |
1650 | if (errno != 0) { |
1651 | perror("kernel start consumer exec"); | |
1652 | } | |
1653 | exit(EXIT_FAILURE); | |
1654 | } else if (pid > 0) { | |
1655 | ret = pid; | |
8c0faa1d | 1656 | } else { |
3bd1e081 | 1657 | perror("start consumer fork"); |
8c0faa1d | 1658 | ret = -errno; |
8c0faa1d | 1659 | } |
8f4905da | 1660 | error: |
8c0faa1d DG |
1661 | return ret; |
1662 | } | |
1663 | ||
693bd40b | 1664 | /* |
3bd1e081 | 1665 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 1666 | */ |
3bd1e081 | 1667 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b DG |
1668 | { |
1669 | int ret; | |
1670 | ||
3bd1e081 MD |
1671 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1672 | if (consumer_data->pid != 0) { | |
1673 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
1674 | goto end; |
1675 | } | |
693bd40b | 1676 | |
3bd1e081 | 1677 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 1678 | if (ret < 0) { |
3bd1e081 MD |
1679 | ERR("Spawning consumerd failed"); |
1680 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 1681 | goto error; |
693bd40b | 1682 | } |
c49dc785 | 1683 | |
3bd1e081 MD |
1684 | /* Setting up the consumer_data pid */ |
1685 | consumer_data->pid = ret; | |
48842b30 | 1686 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 1687 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 1688 | |
3bd1e081 MD |
1689 | DBG2("Spawning consumer control thread"); |
1690 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 1691 | if (ret < 0) { |
3bd1e081 | 1692 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
1693 | goto error; |
1694 | } | |
1695 | ||
c49dc785 | 1696 | end: |
693bd40b DG |
1697 | return 0; |
1698 | ||
1699 | error: | |
1700 | return ret; | |
1701 | } | |
1702 | ||
b73401da | 1703 | /* |
d063d709 | 1704 | * modprobe_kernel_modules |
b73401da DG |
1705 | */ |
1706 | static int modprobe_kernel_modules(void) | |
1707 | { | |
ab147185 | 1708 | int ret = 0, i; |
b73401da DG |
1709 | char modprobe[256]; |
1710 | ||
ab147185 MD |
1711 | for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) { |
1712 | ret = snprintf(modprobe, sizeof(modprobe), | |
1713 | "/sbin/modprobe %s%s", | |
24b487b8 | 1714 | kernel_modules_list[i].required ? "" : "-q ", |
ab147185 | 1715 | kernel_modules_list[i].name); |
b73401da DG |
1716 | if (ret < 0) { |
1717 | perror("snprintf modprobe"); | |
1718 | goto error; | |
1719 | } | |
ab147185 | 1720 | modprobe[sizeof(modprobe) - 1] = '\0'; |
b73401da | 1721 | ret = system(modprobe); |
ab147185 MD |
1722 | if (ret == -1) { |
1723 | ERR("Unable to launch modprobe for module %s", | |
1724 | kernel_modules_list[i].name); | |
1725 | } else if (kernel_modules_list[i].required | |
d9800920 | 1726 | && WEXITSTATUS(ret) != 0) { |
ab147185 MD |
1727 | ERR("Unable to load module %s", |
1728 | kernel_modules_list[i].name); | |
1729 | } else { | |
1730 | DBG("Modprobe successfully %s", | |
1731 | kernel_modules_list[i].name); | |
1732 | } | |
1733 | } | |
1734 | ||
1735 | error: | |
1736 | return ret; | |
1737 | } | |
1738 | ||
b73401da | 1739 | /* |
d063d709 | 1740 | * mount_debugfs |
b73401da DG |
1741 | */ |
1742 | static int mount_debugfs(char *path) | |
1743 | { | |
1744 | int ret; | |
1745 | char *type = "debugfs"; | |
1746 | ||
996b65c8 | 1747 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); |
b73401da | 1748 | if (ret < 0) { |
7272acf5 | 1749 | PERROR("Cannot create debugfs path"); |
b73401da DG |
1750 | goto error; |
1751 | } | |
1752 | ||
1753 | ret = mount(type, path, type, 0, NULL); | |
1754 | if (ret < 0) { | |
7272acf5 | 1755 | PERROR("Cannot mount debugfs"); |
b73401da DG |
1756 | goto error; |
1757 | } | |
1758 | ||
1759 | DBG("Mounted debugfs successfully at %s", path); | |
1760 | ||
1761 | error: | |
1762 | return ret; | |
1763 | } | |
1764 | ||
8c0faa1d | 1765 | /* |
d063d709 | 1766 | * Setup necessary data for kernel tracer action. |
8c0faa1d | 1767 | */ |
f3ed775e | 1768 | static void init_kernel_tracer(void) |
8c0faa1d | 1769 | { |
b73401da DG |
1770 | int ret; |
1771 | char *proc_mounts = "/proc/mounts"; | |
1772 | char line[256]; | |
684d34d2 | 1773 | char *debugfs_path = NULL, *lttng_path = NULL; |
b73401da DG |
1774 | FILE *fp; |
1775 | ||
1776 | /* Detect debugfs */ | |
1777 | fp = fopen(proc_mounts, "r"); | |
1778 | if (fp == NULL) { | |
1779 | ERR("Unable to probe %s", proc_mounts); | |
1780 | goto error; | |
1781 | } | |
1782 | ||
1783 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1784 | if (strstr(line, "debugfs") != NULL) { | |
1785 | /* Remove first string */ | |
1786 | strtok(line, " "); | |
1787 | /* Dup string here so we can reuse line later on */ | |
1788 | debugfs_path = strdup(strtok(NULL, " ")); | |
1789 | DBG("Got debugfs path : %s", debugfs_path); | |
1790 | break; | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | fclose(fp); | |
1795 | ||
1796 | /* Mount debugfs if needded */ | |
1797 | if (debugfs_path == NULL) { | |
1798 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); | |
1799 | if (ret < 0) { | |
1800 | perror("asprintf debugfs path"); | |
1801 | goto error; | |
1802 | } | |
1803 | ret = mount_debugfs(debugfs_path); | |
1804 | if (ret < 0) { | |
7272acf5 | 1805 | perror("Cannot mount debugfs"); |
b73401da DG |
1806 | goto error; |
1807 | } | |
1808 | } | |
1809 | ||
1810 | /* Modprobe lttng kernel modules */ | |
1811 | ret = modprobe_kernel_modules(); | |
1812 | if (ret < 0) { | |
1813 | goto error; | |
1814 | } | |
1815 | ||
1816 | /* Setup lttng kernel path */ | |
1817 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); | |
1818 | if (ret < 0) { | |
1819 | perror("asprintf lttng path"); | |
1820 | goto error; | |
1821 | } | |
1822 | ||
1823 | /* Open debugfs lttng */ | |
1824 | kernel_tracer_fd = open(lttng_path, O_RDWR); | |
f3ed775e | 1825 | if (kernel_tracer_fd < 0) { |
b73401da DG |
1826 | DBG("Failed to open %s", lttng_path); |
1827 | goto error; | |
8c0faa1d DG |
1828 | } |
1829 | ||
b73401da DG |
1830 | free(lttng_path); |
1831 | free(debugfs_path); | |
f3ed775e | 1832 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
b73401da DG |
1833 | return; |
1834 | ||
1835 | error: | |
1836 | if (lttng_path) { | |
1837 | free(lttng_path); | |
1838 | } | |
1839 | if (debugfs_path) { | |
1840 | free(debugfs_path); | |
1841 | } | |
1842 | WARN("No kernel tracer available"); | |
1843 | kernel_tracer_fd = 0; | |
1844 | return; | |
f3ed775e | 1845 | } |
33a2b854 | 1846 | |
f3ed775e | 1847 | /* |
54d01ffb | 1848 | * Init tracing by creating trace directory and sending fds kernel consumer. |
f3ed775e | 1849 | */ |
54d01ffb | 1850 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
f3ed775e | 1851 | { |
f40799e8 | 1852 | int ret = 0; |
8c0faa1d | 1853 | |
3bd1e081 | 1854 | if (session->consumer_fds_sent == 0) { |
d9800920 | 1855 | /* |
54d01ffb DG |
1856 | * Assign default kernel consumer socket if no consumer assigned to the |
1857 | * kernel session. At this point, it's NOT suppose to be 0 but this is | |
1858 | * an extra security check. | |
d9800920 DG |
1859 | */ |
1860 | if (session->consumer_fd == 0) { | |
3bd1e081 | 1861 | session->consumer_fd = kconsumer_data.cmd_sock; |
d9800920 DG |
1862 | } |
1863 | ||
2bdd86d4 | 1864 | ret = send_kconsumer_session_streams(&kconsumer_data, session); |
f3ed775e | 1865 | if (ret < 0) { |
f3ed775e DG |
1866 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
1867 | goto error; | |
1868 | } | |
1d4b027a | 1869 | |
3bd1e081 | 1870 | session->consumer_fds_sent = 1; |
f3ed775e | 1871 | } |
1d4b027a DG |
1872 | |
1873 | error: | |
1874 | return ret; | |
8c0faa1d DG |
1875 | } |
1876 | ||
0177d773 DG |
1877 | /* |
1878 | * Create an UST session and add it to the session ust list. | |
1879 | */ | |
44d3bd01 | 1880 | static int create_ust_session(struct ltt_session *session, |
6df2e2c9 | 1881 | struct lttng_domain *domain) |
0177d773 | 1882 | { |
13384287 | 1883 | struct ltt_ust_session *lus = NULL; |
6df2e2c9 | 1884 | int ret; |
44d3bd01 DG |
1885 | |
1886 | switch (domain->type) { | |
48842b30 | 1887 | case LTTNG_DOMAIN_UST: |
44d3bd01 DG |
1888 | break; |
1889 | default: | |
13384287 | 1890 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
44d3bd01 DG |
1891 | goto error; |
1892 | } | |
0177d773 DG |
1893 | |
1894 | DBG("Creating UST session"); | |
1895 | ||
a991f516 | 1896 | lus = trace_ust_create_session(session->path, session->id, domain); |
0177d773 | 1897 | if (lus == NULL) { |
44d3bd01 | 1898 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
1899 | goto error; |
1900 | } | |
1901 | ||
6df2e2c9 MD |
1902 | ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG, |
1903 | session->uid, session->gid); | |
0177d773 DG |
1904 | if (ret < 0) { |
1905 | if (ret != -EEXIST) { | |
1906 | ERR("Trace directory creation error"); | |
44d3bd01 | 1907 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
1908 | goto error; |
1909 | } | |
1910 | } | |
1911 | ||
f6a9efaa DG |
1912 | /* The domain type dictate different actions on session creation */ |
1913 | switch (domain->type) { | |
48842b30 DG |
1914 | case LTTNG_DOMAIN_UST: |
1915 | /* No ustctl for the global UST domain */ | |
1916 | break; | |
1917 | default: | |
36dc12cc | 1918 | ERR("Unknown UST domain on create session %d", domain->type); |
48842b30 | 1919 | goto error; |
0177d773 | 1920 | } |
6df2e2c9 MD |
1921 | lus->uid = session->uid; |
1922 | lus->gid = session->gid; | |
f6a9efaa | 1923 | session->ust_session = lus; |
44d3bd01 DG |
1924 | |
1925 | return LTTCOMM_OK; | |
0177d773 DG |
1926 | |
1927 | error: | |
1928 | free(lus); | |
1929 | return ret; | |
1930 | } | |
1931 | ||
333f285e | 1932 | /* |
d063d709 | 1933 | * Create a kernel tracer session then create the default channel. |
333f285e | 1934 | */ |
6df2e2c9 | 1935 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 1936 | { |
f3ed775e | 1937 | int ret; |
f3ed775e DG |
1938 | |
1939 | DBG("Creating kernel session"); | |
1940 | ||
1941 | ret = kernel_create_session(session, kernel_tracer_fd); | |
1942 | if (ret < 0) { | |
1943 | ret = LTTCOMM_KERN_SESS_FAIL; | |
1944 | goto error; | |
333f285e DG |
1945 | } |
1946 | ||
d9800920 | 1947 | /* Set kernel consumer socket fd */ |
3bd1e081 MD |
1948 | if (kconsumer_data.cmd_sock) { |
1949 | session->kernel_session->consumer_fd = kconsumer_data.cmd_sock; | |
d9800920 DG |
1950 | } |
1951 | ||
63053e7c | 1952 | ret = mkdir_recursive(session->kernel_session->trace_path, |
6df2e2c9 | 1953 | S_IRWXU | S_IRWXG, session->uid, session->gid); |
7a485870 | 1954 | if (ret < 0) { |
996b65c8 | 1955 | if (ret != -EEXIST) { |
7a485870 DG |
1956 | ERR("Trace directory creation error"); |
1957 | goto error; | |
1958 | } | |
1959 | } | |
6df2e2c9 MD |
1960 | session->kernel_session->uid = session->uid; |
1961 | session->kernel_session->gid = session->gid; | |
7a485870 | 1962 | |
f3ed775e DG |
1963 | error: |
1964 | return ret; | |
333f285e DG |
1965 | } |
1966 | ||
6c9cc2ab DG |
1967 | /* |
1968 | * Using the session list, filled a lttng_session array to send back to the | |
1969 | * client for session listing. | |
1970 | * | |
1971 | * The session list lock MUST be acquired before calling this function. Use | |
54d01ffb | 1972 | * session_lock_list() and session_unlock_list(). |
6c9cc2ab DG |
1973 | */ |
1974 | static void list_lttng_sessions(struct lttng_session *sessions) | |
1975 | { | |
1976 | int i = 0; | |
1977 | struct ltt_session *session; | |
1978 | ||
1979 | DBG("Getting all available session"); | |
1980 | /* | |
1981 | * Iterate over session list and append data after the control struct in | |
1982 | * the buffer. | |
1983 | */ | |
1984 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
1985 | strncpy(sessions[i].path, session->path, PATH_MAX); | |
99497cd0 | 1986 | sessions[i].path[PATH_MAX - 1] = '\0'; |
6c9cc2ab | 1987 | strncpy(sessions[i].name, session->name, NAME_MAX); |
99497cd0 | 1988 | sessions[i].name[NAME_MAX - 1] = '\0'; |
464dd62d | 1989 | sessions[i].enabled = session->enabled; |
6c9cc2ab DG |
1990 | i++; |
1991 | } | |
1992 | } | |
1993 | ||
9f19cc17 DG |
1994 | /* |
1995 | * Fill lttng_channel array of all channels. | |
1996 | */ | |
b551a063 | 1997 | static void list_lttng_channels(int domain, struct ltt_session *session, |
9f19cc17 DG |
1998 | struct lttng_channel *channels) |
1999 | { | |
2000 | int i = 0; | |
2001 | struct ltt_kernel_channel *kchan; | |
2002 | ||
2003 | DBG("Listing channels for session %s", session->name); | |
2004 | ||
b551a063 DG |
2005 | switch (domain) { |
2006 | case LTTNG_DOMAIN_KERNEL: | |
2007 | /* Kernel channels */ | |
2008 | if (session->kernel_session != NULL) { | |
2009 | cds_list_for_each_entry(kchan, | |
2010 | &session->kernel_session->channel_list.head, list) { | |
2011 | /* Copy lttng_channel struct to array */ | |
2012 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
2013 | channels[i].enabled = kchan->enabled; | |
2014 | i++; | |
2015 | } | |
2016 | } | |
2017 | break; | |
2018 | case LTTNG_DOMAIN_UST: | |
2019 | { | |
2020 | struct cds_lfht_iter iter; | |
2021 | struct ltt_ust_channel *uchan; | |
2022 | ||
2023 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels, | |
2024 | &iter, uchan, node) { | |
2025 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); | |
2026 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
2027 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
2028 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
2029 | channels[i].attr.switch_timer_interval = | |
2030 | uchan->attr.switch_timer_interval; | |
2031 | channels[i].attr.read_timer_interval = | |
2032 | uchan->attr.read_timer_interval; | |
aea829b3 | 2033 | channels[i].enabled = uchan->enabled; |
5edd7e09 DG |
2034 | switch (uchan->attr.output) { |
2035 | case LTTNG_UST_MMAP: | |
2036 | default: | |
2037 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
2038 | break; | |
2039 | } | |
befe0905 | 2040 | i++; |
b551a063 DG |
2041 | } |
2042 | break; | |
2043 | } | |
2044 | default: | |
2045 | break; | |
2046 | } | |
2047 | } | |
2048 | ||
2049 | /* | |
2050 | * Create a list of ust global domain events. | |
2051 | */ | |
2052 | static int list_lttng_ust_global_events(char *channel_name, | |
2053 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
2054 | { | |
2055 | int i = 0, ret = 0; | |
2056 | unsigned int nb_event = 0; | |
2057 | struct cds_lfht_iter iter; | |
d7b75b30 | 2058 | struct cds_lfht_node *node; |
b551a063 DG |
2059 | struct ltt_ust_channel *uchan; |
2060 | struct ltt_ust_event *uevent; | |
2061 | struct lttng_event *tmp; | |
2062 | ||
2063 | DBG("Listing UST global events for channel %s", channel_name); | |
2064 | ||
2065 | rcu_read_lock(); | |
2066 | ||
d7b75b30 DG |
2067 | node = hashtable_lookup(ust_global->channels, (void *) channel_name, |
2068 | strlen(channel_name), &iter); | |
2069 | if (node == NULL) { | |
2070 | ret = -LTTCOMM_UST_CHAN_NOT_FOUND; | |
2071 | goto error; | |
b551a063 DG |
2072 | } |
2073 | ||
d7b75b30 DG |
2074 | uchan = caa_container_of(node, struct ltt_ust_channel, node); |
2075 | ||
2076 | nb_event += hashtable_get_count(uchan->events); | |
2077 | ||
b551a063 DG |
2078 | if (nb_event == 0) { |
2079 | ret = nb_event; | |
2080 | goto error; | |
2081 | } | |
2082 | ||
2083 | DBG3("Listing UST global %d events", nb_event); | |
2084 | ||
2085 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2086 | if (tmp == NULL) { | |
2087 | ret = -LTTCOMM_FATAL; | |
2088 | goto error; | |
2089 | } | |
2090 | ||
d7b75b30 DG |
2091 | cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) { |
2092 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); | |
2093 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2094 | tmp[i].enabled = uevent->enabled; | |
2095 | switch (uevent->attr.instrumentation) { | |
2096 | case LTTNG_UST_TRACEPOINT: | |
2097 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
2098 | break; | |
2099 | case LTTNG_UST_PROBE: | |
2100 | tmp[i].type = LTTNG_EVENT_PROBE; | |
2101 | break; | |
2102 | case LTTNG_UST_FUNCTION: | |
2103 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
2104 | break; | |
ac3bd9c0 | 2105 | case LTTNG_UST_TRACEPOINT_LOGLEVEL: |
e4baff1e | 2106 | tmp[i].type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL; |
ac3bd9c0 | 2107 | break; |
9f19cc17 | 2108 | } |
d7b75b30 | 2109 | i++; |
9f19cc17 DG |
2110 | } |
2111 | ||
b551a063 DG |
2112 | ret = nb_event; |
2113 | *events = tmp; | |
2114 | ||
2115 | error: | |
2116 | rcu_read_unlock(); | |
2117 | return ret; | |
9f19cc17 DG |
2118 | } |
2119 | ||
2120 | /* | |
b551a063 | 2121 | * Fill lttng_event array of all kernel events in the channel. |
9f19cc17 | 2122 | */ |
b551a063 DG |
2123 | static int list_lttng_kernel_events(char *channel_name, |
2124 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
9f19cc17 | 2125 | { |
b551a063 DG |
2126 | int i = 0, ret; |
2127 | unsigned int nb_event; | |
9f19cc17 | 2128 | struct ltt_kernel_event *event; |
b551a063 DG |
2129 | struct ltt_kernel_channel *kchan; |
2130 | ||
2131 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
2132 | if (kchan == NULL) { | |
2133 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2134 | goto error; | |
2135 | } | |
2136 | ||
2137 | nb_event = kchan->event_count; | |
9f19cc17 DG |
2138 | |
2139 | DBG("Listing events for channel %s", kchan->channel->name); | |
2140 | ||
b551a063 DG |
2141 | if (nb_event == 0) { |
2142 | ret = nb_event; | |
2143 | goto error; | |
2144 | } | |
2145 | ||
2146 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
2147 | if (*events == NULL) { | |
2148 | ret = LTTCOMM_FATAL; | |
2149 | goto error; | |
2150 | } | |
2151 | ||
9f19cc17 DG |
2152 | /* Kernel channels */ |
2153 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
b551a063 DG |
2154 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
2155 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
2156 | (*events)[i].enabled = event->enabled; | |
9f19cc17 DG |
2157 | switch (event->event->instrumentation) { |
2158 | case LTTNG_KERNEL_TRACEPOINT: | |
b551a063 | 2159 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; |
9f19cc17 DG |
2160 | break; |
2161 | case LTTNG_KERNEL_KPROBE: | |
2162 | case LTTNG_KERNEL_KRETPROBE: | |
b551a063 DG |
2163 | (*events)[i].type = LTTNG_EVENT_PROBE; |
2164 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
9f19cc17 DG |
2165 | sizeof(struct lttng_kernel_kprobe)); |
2166 | break; | |
2167 | case LTTNG_KERNEL_FUNCTION: | |
b551a063 DG |
2168 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
2169 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
9f19cc17 DG |
2170 | sizeof(struct lttng_kernel_function)); |
2171 | break; | |
0133c199 | 2172 | case LTTNG_KERNEL_NOOP: |
b551a063 | 2173 | (*events)[i].type = LTTNG_EVENT_NOOP; |
0133c199 | 2174 | break; |
a54bd42d | 2175 | case LTTNG_KERNEL_SYSCALL: |
b551a063 | 2176 | (*events)[i].type = LTTNG_EVENT_SYSCALL; |
0133c199 | 2177 | break; |
7a3d1328 MD |
2178 | case LTTNG_KERNEL_ALL: |
2179 | assert(0); | |
2180 | break; | |
9f19cc17 DG |
2181 | } |
2182 | i++; | |
2183 | } | |
b551a063 DG |
2184 | |
2185 | return nb_event; | |
2186 | ||
2187 | error: | |
2188 | return ret; | |
9f19cc17 DG |
2189 | } |
2190 | ||
fac6795d | 2191 | /* |
54d01ffb | 2192 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
fac6795d | 2193 | */ |
54d01ffb DG |
2194 | static int cmd_disable_channel(struct ltt_session *session, |
2195 | int domain, char *channel_name) | |
fac6795d | 2196 | { |
54d01ffb | 2197 | int ret; |
78f0bacd DG |
2198 | struct ltt_ust_session *usess; |
2199 | ||
2200 | usess = session->ust_session; | |
379473d2 | 2201 | |
54d01ffb | 2202 | switch (domain) { |
78f0bacd DG |
2203 | case LTTNG_DOMAIN_KERNEL: |
2204 | { | |
2205 | ret = channel_kernel_disable(session->kernel_session, | |
2206 | channel_name); | |
2207 | if (ret != LTTCOMM_OK) { | |
2208 | goto error; | |
2209 | } | |
54d01ffb | 2210 | |
78f0bacd DG |
2211 | kernel_wait_quiescent(kernel_tracer_fd); |
2212 | break; | |
2213 | } | |
2214 | case LTTNG_DOMAIN_UST: | |
2215 | { | |
2216 | struct ltt_ust_channel *uchan; | |
7885e399 | 2217 | struct cds_lfht *chan_ht; |
78f0bacd | 2218 | |
7885e399 DG |
2219 | chan_ht = usess->domain_global.channels; |
2220 | ||
2221 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
78f0bacd DG |
2222 | if (uchan == NULL) { |
2223 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
54d01ffb | 2224 | goto error; |
78f0bacd DG |
2225 | } |
2226 | ||
7885e399 DG |
2227 | ret = channel_ust_disable(usess, domain, uchan); |
2228 | if (ret != LTTCOMM_OK) { | |
78f0bacd DG |
2229 | goto error; |
2230 | } | |
78f0bacd DG |
2231 | break; |
2232 | } | |
2233 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
2234 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2235 | case LTTNG_DOMAIN_UST_PID: | |
2236 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2237 | goto error; | |
2238 | default: | |
2239 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2240 | goto error; | |
20fe2104 DG |
2241 | } |
2242 | ||
54d01ffb | 2243 | ret = LTTCOMM_OK; |
d65106b1 | 2244 | |
54d01ffb DG |
2245 | error: |
2246 | return ret; | |
2247 | } | |
2248 | ||
2249 | /* | |
2250 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
2251 | */ | |
44d3bd01 | 2252 | static int cmd_enable_channel(struct ltt_session *session, |
aea829b3 | 2253 | int domain, struct lttng_channel *attr) |
54d01ffb DG |
2254 | { |
2255 | int ret; | |
f6a9efaa | 2256 | struct ltt_ust_session *usess = session->ust_session; |
7885e399 | 2257 | struct cds_lfht *chan_ht; |
f6a9efaa | 2258 | |
5d56b5aa | 2259 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
d65106b1 | 2260 | |
aea829b3 | 2261 | switch (domain) { |
44d3bd01 DG |
2262 | case LTTNG_DOMAIN_KERNEL: |
2263 | { | |
2264 | struct ltt_kernel_channel *kchan; | |
54d01ffb | 2265 | |
44d3bd01 DG |
2266 | kchan = trace_kernel_get_channel_by_name(attr->name, |
2267 | session->kernel_session); | |
2268 | if (kchan == NULL) { | |
2269 | ret = channel_kernel_create(session->kernel_session, | |
2270 | attr, kernel_poll_pipe[1]); | |
2271 | } else { | |
2272 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
2273 | } | |
2274 | ||
2275 | if (ret != LTTCOMM_OK) { | |
2276 | goto error; | |
2277 | } | |
2278 | ||
2279 | kernel_wait_quiescent(kernel_tracer_fd); | |
2280 | break; | |
2281 | } | |
f6a9efaa DG |
2282 | case LTTNG_DOMAIN_UST: |
2283 | { | |
2284 | struct ltt_ust_channel *uchan; | |
2285 | ||
7885e399 | 2286 | chan_ht = usess->domain_global.channels; |
f6a9efaa | 2287 | |
7885e399 | 2288 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
f6a9efaa | 2289 | if (uchan == NULL) { |
7885e399 | 2290 | ret = channel_ust_create(usess, domain, attr); |
f6a9efaa | 2291 | } else { |
7885e399 | 2292 | ret = channel_ust_enable(usess, domain, uchan); |
48842b30 | 2293 | } |
f6a9efaa DG |
2294 | break; |
2295 | } | |
78f0bacd DG |
2296 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
2297 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
44d3bd01 | 2298 | case LTTNG_DOMAIN_UST_PID: |
f6a9efaa DG |
2299 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2300 | goto error; | |
44d3bd01 DG |
2301 | default: |
2302 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
2303 | goto error; | |
54d01ffb DG |
2304 | } |
2305 | ||
54d01ffb DG |
2306 | error: |
2307 | return ret; | |
2308 | } | |
2309 | ||
2310 | /* | |
2311 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
2312 | */ | |
2313 | static int cmd_disable_event(struct ltt_session *session, int domain, | |
2314 | char *channel_name, char *event_name) | |
2315 | { | |
2316 | int ret; | |
54d01ffb DG |
2317 | |
2318 | switch (domain) { | |
2319 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
2320 | { |
2321 | struct ltt_kernel_channel *kchan; | |
b0a40d28 | 2322 | struct ltt_kernel_session *ksess; |
2bdd86d4 | 2323 | |
b0a40d28 DG |
2324 | ksess = session->kernel_session; |
2325 | ||
2326 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
2327 | if (kchan == NULL) { |
2328 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2329 | goto error; | |
d65106b1 DG |
2330 | } |
2331 | ||
b0a40d28 | 2332 | ret = event_kernel_disable_tracepoint(ksess, kchan, event_name); |
54d01ffb DG |
2333 | if (ret != LTTCOMM_OK) { |
2334 | goto error; | |
2335 | } | |
2336 | ||
2337 | kernel_wait_quiescent(kernel_tracer_fd); | |
d65106b1 | 2338 | break; |
2bdd86d4 MD |
2339 | } |
2340 | case LTTNG_DOMAIN_UST: | |
b0a40d28 | 2341 | { |
b0a40d28 | 2342 | struct ltt_ust_channel *uchan; |
7f79d3a1 | 2343 | struct ltt_ust_session *usess; |
b0a40d28 DG |
2344 | |
2345 | usess = session->ust_session; | |
2346 | ||
2347 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2348 | channel_name); | |
2349 | if (uchan == NULL) { | |
2350 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2351 | goto error; | |
2352 | } | |
2353 | ||
7f79d3a1 DG |
2354 | ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name); |
2355 | if (ret != LTTCOMM_OK) { | |
b0a40d28 DG |
2356 | goto error; |
2357 | } | |
2358 | ||
7f79d3a1 | 2359 | DBG3("Disable UST event %s in channel %s completed", event_name, |
b0a40d28 | 2360 | channel_name); |
b0a40d28 DG |
2361 | break; |
2362 | } | |
2bdd86d4 MD |
2363 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2364 | case LTTNG_DOMAIN_UST_PID: | |
2365 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
54d01ffb | 2366 | default: |
54d01ffb DG |
2367 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2368 | goto error; | |
d65106b1 | 2369 | } |
26cc6b4e | 2370 | |
54d01ffb DG |
2371 | ret = LTTCOMM_OK; |
2372 | ||
2373 | error: | |
2374 | return ret; | |
2375 | } | |
2376 | ||
2377 | /* | |
2378 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. | |
2379 | */ | |
2380 | static int cmd_disable_event_all(struct ltt_session *session, int domain, | |
2381 | char *channel_name) | |
2382 | { | |
2383 | int ret; | |
54d01ffb DG |
2384 | |
2385 | switch (domain) { | |
2386 | case LTTNG_DOMAIN_KERNEL: | |
9730260e DG |
2387 | { |
2388 | struct ltt_kernel_session *ksess; | |
2389 | struct ltt_kernel_channel *kchan; | |
2390 | ||
2391 | ksess = session->kernel_session; | |
2392 | ||
2393 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
54d01ffb DG |
2394 | if (kchan == NULL) { |
2395 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2396 | goto error; | |
26cc6b4e DG |
2397 | } |
2398 | ||
9730260e | 2399 | ret = event_kernel_disable_all(ksess, kchan); |
54d01ffb | 2400 | if (ret != LTTCOMM_OK) { |
0b97ec54 | 2401 | goto error; |
26cc6b4e DG |
2402 | } |
2403 | ||
54d01ffb | 2404 | kernel_wait_quiescent(kernel_tracer_fd); |
26cc6b4e | 2405 | break; |
9730260e DG |
2406 | } |
2407 | case LTTNG_DOMAIN_UST: | |
2408 | { | |
2409 | struct ltt_ust_session *usess; | |
2410 | struct ltt_ust_channel *uchan; | |
2411 | ||
2412 | usess = session->ust_session; | |
2413 | ||
2414 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2415 | channel_name); | |
2416 | if (uchan == NULL) { | |
2417 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2418 | goto error; | |
2419 | } | |
2420 | ||
7f79d3a1 DG |
2421 | ret = event_ust_disable_all_tracepoints(usess, domain, uchan); |
2422 | if (ret != 0) { | |
9730260e DG |
2423 | goto error; |
2424 | } | |
2425 | ||
7f79d3a1 | 2426 | DBG3("Disable all UST events in channel %s completed", channel_name); |
9730260e DG |
2427 | |
2428 | break; | |
2429 | } | |
2430 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2431 | case LTTNG_DOMAIN_UST_PID: | |
2432 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
54d01ffb | 2433 | default: |
54d01ffb DG |
2434 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2435 | goto error; | |
26cc6b4e | 2436 | } |
e953ef25 | 2437 | |
54d01ffb | 2438 | ret = LTTCOMM_OK; |
e953ef25 | 2439 | |
54d01ffb DG |
2440 | error: |
2441 | return ret; | |
2442 | } | |
f5177a38 | 2443 | |
54d01ffb DG |
2444 | /* |
2445 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
2446 | */ | |
2447 | static int cmd_add_context(struct ltt_session *session, int domain, | |
2448 | char *channel_name, char *event_name, struct lttng_event_context *ctx) | |
2449 | { | |
2450 | int ret; | |
f5177a38 | 2451 | |
54d01ffb DG |
2452 | switch (domain) { |
2453 | case LTTNG_DOMAIN_KERNEL: | |
2454 | /* Add kernel context to kernel tracer */ | |
2455 | ret = context_kernel_add(session->kernel_session, ctx, | |
2456 | event_name, channel_name); | |
2457 | if (ret != LTTCOMM_OK) { | |
f5177a38 | 2458 | goto error; |
e953ef25 | 2459 | } |
2bdd86d4 MD |
2460 | break; |
2461 | case LTTNG_DOMAIN_UST: | |
2462 | { | |
55cc08a6 | 2463 | struct ltt_ust_session *usess = session->ust_session; |
e953ef25 | 2464 | |
55cc08a6 DG |
2465 | ret = context_ust_add(usess, domain, ctx, event_name, channel_name); |
2466 | if (ret != LTTCOMM_OK) { | |
2467 | goto error; | |
2bdd86d4 | 2468 | } |
e953ef25 | 2469 | break; |
2bdd86d4 | 2470 | } |
55cc08a6 DG |
2471 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
2472 | case LTTNG_DOMAIN_UST_PID: | |
2473 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
54d01ffb | 2474 | default: |
54d01ffb DG |
2475 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2476 | goto error; | |
e953ef25 | 2477 | } |
950131af | 2478 | |
54d01ffb | 2479 | ret = LTTCOMM_OK; |
950131af | 2480 | |
54d01ffb DG |
2481 | error: |
2482 | return ret; | |
2483 | } | |
2484 | ||
2485 | /* | |
2486 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
13dce3b7 MD |
2487 | * |
2488 | * TODO: currently, both events and loglevels are kept within the same | |
2489 | * namespace for UST global registry/app registery, so if an event | |
2490 | * happen to have the same name as the loglevel (very unlikely though), | |
2491 | * and an attempt is made to enable/disable both in the same session, | |
2492 | * the first to be created will be the only one allowed to exist. | |
54d01ffb DG |
2493 | */ |
2494 | static int cmd_enable_event(struct ltt_session *session, int domain, | |
2495 | char *channel_name, struct lttng_event *event) | |
2496 | { | |
2497 | int ret; | |
f6cd6b0f | 2498 | struct lttng_channel *attr; |
48842b30 | 2499 | struct ltt_ust_session *usess = session->ust_session; |
54d01ffb DG |
2500 | |
2501 | switch (domain) { | |
2502 | case LTTNG_DOMAIN_KERNEL: | |
2bdd86d4 MD |
2503 | { |
2504 | struct ltt_kernel_channel *kchan; | |
2505 | ||
54d01ffb DG |
2506 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2507 | session->kernel_session); | |
2508 | if (kchan == NULL) { | |
f6cd6b0f DG |
2509 | attr = channel_new_default_attr(domain); |
2510 | if (attr == NULL) { | |
2511 | ret = LTTCOMM_FATAL; | |
2512 | goto error; | |
2513 | } | |
2514 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
2515 | ||
54d01ffb | 2516 | /* This call will notify the kernel thread */ |
44d3bd01 | 2517 | ret = channel_kernel_create(session->kernel_session, |
f6cd6b0f | 2518 | attr, kernel_poll_pipe[1]); |
54d01ffb | 2519 | if (ret != LTTCOMM_OK) { |
ff4d74e6 | 2520 | free(attr); |
f5177a38 DG |
2521 | goto error; |
2522 | } | |
ff4d74e6 | 2523 | free(attr); |
54d01ffb | 2524 | } |
950131af | 2525 | |
54d01ffb DG |
2526 | /* Get the newly created kernel channel pointer */ |
2527 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2528 | session->kernel_session); | |
2529 | if (kchan == NULL) { | |
2530 | /* This sould not happen... */ | |
2531 | ret = LTTCOMM_FATAL; | |
2532 | goto error; | |
2533 | } | |
f5177a38 | 2534 | |
48842b30 DG |
2535 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, |
2536 | event); | |
54d01ffb | 2537 | if (ret != LTTCOMM_OK) { |
f5177a38 | 2538 | goto error; |
950131af DG |
2539 | } |
2540 | ||
54d01ffb | 2541 | kernel_wait_quiescent(kernel_tracer_fd); |
950131af | 2542 | break; |
2bdd86d4 MD |
2543 | } |
2544 | case LTTNG_DOMAIN_UST: | |
2545 | { | |
aea829b3 | 2546 | struct lttng_channel *attr; |
edb67388 | 2547 | struct ltt_ust_channel *uchan; |
2bdd86d4 | 2548 | |
edb67388 | 2549 | /* Get channel from global UST domain */ |
48842b30 DG |
2550 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
2551 | channel_name); | |
2552 | if (uchan == NULL) { | |
aea829b3 DG |
2553 | /* Create default channel */ |
2554 | attr = channel_new_default_attr(domain); | |
2555 | if (attr == NULL) { | |
2556 | ret = LTTCOMM_FATAL; | |
2557 | goto error; | |
2558 | } | |
596219f7 | 2559 | snprintf(attr->name, NAME_MAX, "%s", channel_name); |
edb67388 | 2560 | attr->name[NAME_MAX - 1] = '\0'; |
aea829b3 | 2561 | |
7885e399 | 2562 | ret = channel_ust_create(usess, domain, attr); |
edb67388 | 2563 | if (ret != LTTCOMM_OK) { |
ff4d74e6 | 2564 | free(attr); |
aea829b3 DG |
2565 | goto error; |
2566 | } | |
aea829b3 DG |
2567 | free(attr); |
2568 | ||
2569 | /* Get the newly created channel reference back */ | |
2570 | uchan = trace_ust_find_channel_by_name( | |
2571 | usess->domain_global.channels, channel_name); | |
2572 | if (uchan == NULL) { | |
2573 | /* Something is really wrong */ | |
2574 | ret = LTTCOMM_FATAL; | |
2575 | goto error; | |
2576 | } | |
48842b30 | 2577 | } |
2bdd86d4 | 2578 | |
edb67388 | 2579 | /* At this point, the session and channel exist on the tracer */ |
d5cd1f34 | 2580 | |
edb67388 DG |
2581 | ret = event_ust_enable_tracepoint(usess, domain, uchan, event); |
2582 | if (ret != LTTCOMM_OK) { | |
48842b30 | 2583 | goto error; |
2bdd86d4 MD |
2584 | } |
2585 | break; | |
2586 | } | |
2587 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2588 | case LTTNG_DOMAIN_UST_PID: | |
2589 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
54d01ffb | 2590 | default: |
54d01ffb DG |
2591 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2592 | goto error; | |
950131af | 2593 | } |
d36b8583 | 2594 | |
54d01ffb | 2595 | ret = LTTCOMM_OK; |
d36b8583 | 2596 | |
54d01ffb DG |
2597 | error: |
2598 | return ret; | |
2599 | } | |
7d29a247 | 2600 | |
54d01ffb DG |
2601 | /* |
2602 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. | |
2603 | */ | |
2604 | static int cmd_enable_event_all(struct ltt_session *session, int domain, | |
8c9ae521 | 2605 | char *channel_name, int event_type) |
54d01ffb DG |
2606 | { |
2607 | int ret; | |
2608 | struct ltt_kernel_channel *kchan; | |
7d29a247 | 2609 | |
54d01ffb DG |
2610 | switch (domain) { |
2611 | case LTTNG_DOMAIN_KERNEL: | |
2612 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2613 | session->kernel_session); | |
2614 | if (kchan == NULL) { | |
2615 | /* This call will notify the kernel thread */ | |
44d3bd01 DG |
2616 | ret = channel_kernel_create(session->kernel_session, NULL, |
2617 | kernel_poll_pipe[1]); | |
54d01ffb DG |
2618 | if (ret != LTTCOMM_OK) { |
2619 | goto error; | |
d36b8583 | 2620 | } |
0d0c377a | 2621 | |
8f69e5eb DG |
2622 | /* Get the newly created kernel channel pointer */ |
2623 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2624 | session->kernel_session); | |
2625 | if (kchan == NULL) { | |
2626 | /* This sould not happen... */ | |
2627 | ret = LTTCOMM_FATAL; | |
2628 | goto error; | |
2629 | } | |
2630 | ||
54d01ffb | 2631 | } |
0fdd1e2c | 2632 | |
7a3d1328 | 2633 | switch (event_type) { |
76d45b40 | 2634 | case LTTNG_EVENT_SYSCALL: |
7a3d1328 | 2635 | ret = event_kernel_enable_all_syscalls(session->kernel_session, |
8c9ae521 | 2636 | kchan, kernel_tracer_fd); |
7a3d1328 | 2637 | break; |
76d45b40 | 2638 | case LTTNG_EVENT_TRACEPOINT: |
8c9ae521 | 2639 | /* |
7a3d1328 MD |
2640 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
2641 | * events already registered to the channel. | |
8c9ae521 | 2642 | */ |
7a3d1328 MD |
2643 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, |
2644 | kchan, kernel_tracer_fd); | |
2645 | break; | |
76d45b40 | 2646 | case LTTNG_EVENT_ALL: |
7a3d1328 | 2647 | /* Enable syscalls and tracepoints */ |
8c9ae521 DG |
2648 | ret = event_kernel_enable_all(session->kernel_session, |
2649 | kchan, kernel_tracer_fd); | |
7a3d1328 MD |
2650 | break; |
2651 | default: | |
2652 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
2653 | goto error; | |
8c9ae521 | 2654 | } |
8f69e5eb DG |
2655 | |
2656 | /* Manage return value */ | |
54d01ffb | 2657 | if (ret != LTTCOMM_OK) { |
0d0c377a | 2658 | goto error; |
d36b8583 DG |
2659 | } |
2660 | ||
54d01ffb | 2661 | kernel_wait_quiescent(kernel_tracer_fd); |
d36b8583 | 2662 | break; |
76d45b40 DG |
2663 | case LTTNG_DOMAIN_UST: |
2664 | { | |
2665 | struct lttng_channel *attr; | |
2666 | struct ltt_ust_channel *uchan; | |
2667 | struct ltt_ust_session *usess = session->ust_session; | |
2668 | ||
2669 | /* Get channel from global UST domain */ | |
2670 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2671 | channel_name); | |
2672 | if (uchan == NULL) { | |
2673 | /* Create default channel */ | |
2674 | attr = channel_new_default_attr(domain); | |
2675 | if (attr == NULL) { | |
2676 | ret = LTTCOMM_FATAL; | |
2677 | goto error; | |
2678 | } | |
2679 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
2680 | attr->name[NAME_MAX - 1] = '\0'; | |
2681 | ||
2682 | /* Use the internal command enable channel */ | |
7885e399 | 2683 | ret = channel_ust_create(usess, domain, attr); |
76d45b40 DG |
2684 | if (ret != LTTCOMM_OK) { |
2685 | free(attr); | |
2686 | goto error; | |
2687 | } | |
2688 | free(attr); | |
2689 | ||
2690 | /* Get the newly created channel reference back */ | |
2691 | uchan = trace_ust_find_channel_by_name( | |
2692 | usess->domain_global.channels, channel_name); | |
2693 | if (uchan == NULL) { | |
2694 | /* Something is really wrong */ | |
2695 | ret = LTTCOMM_FATAL; | |
2696 | goto error; | |
2697 | } | |
2698 | } | |
2699 | ||
2700 | /* At this point, the session and channel exist on the tracer */ | |
2701 | ||
2702 | switch (event_type) { | |
2703 | case LTTNG_EVENT_ALL: | |
2704 | case LTTNG_EVENT_TRACEPOINT: | |
2705 | ret = event_ust_enable_all_tracepoints(usess, domain, uchan); | |
2706 | if (ret != LTTCOMM_OK) { | |
2707 | goto error; | |
2708 | } | |
2709 | break; | |
2710 | default: | |
2711 | ret = LTTCOMM_UST_ENABLE_FAIL; | |
2712 | goto error; | |
2713 | } | |
2714 | ||
2715 | /* Manage return value */ | |
2716 | if (ret != LTTCOMM_OK) { | |
2717 | goto error; | |
2718 | } | |
2719 | ||
2720 | break; | |
2721 | } | |
2722 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
2723 | case LTTNG_DOMAIN_UST_PID: | |
2724 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
54d01ffb | 2725 | default: |
54d01ffb DG |
2726 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2727 | goto error; | |
d36b8583 | 2728 | } |
f3ed775e | 2729 | |
54d01ffb DG |
2730 | ret = LTTCOMM_OK; |
2731 | ||
2732 | error: | |
2733 | return ret; | |
2734 | } | |
2735 | ||
2736 | /* | |
2737 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2738 | */ | |
2739 | static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) | |
2740 | { | |
2741 | int ret; | |
2742 | ssize_t nb_events = 0; | |
2743 | ||
2744 | switch (domain) { | |
2745 | case LTTNG_DOMAIN_KERNEL: | |
2746 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
2747 | if (nb_events < 0) { | |
2748 | ret = LTTCOMM_KERN_LIST_FAIL; | |
2749 | goto error; | |
894be886 | 2750 | } |
54d01ffb | 2751 | break; |
b551a063 DG |
2752 | case LTTNG_DOMAIN_UST: |
2753 | nb_events = ust_app_list_events(events); | |
2754 | if (nb_events < 0) { | |
2755 | ret = LTTCOMM_UST_LIST_FAIL; | |
2756 | goto error; | |
2757 | } | |
2758 | break; | |
54d01ffb | 2759 | default: |
54d01ffb DG |
2760 | ret = LTTCOMM_NOT_IMPLEMENTED; |
2761 | goto error; | |
2762 | } | |
894be886 | 2763 | |
54d01ffb | 2764 | return nb_events; |
7d29a247 | 2765 | |
54d01ffb DG |
2766 | error: |
2767 | /* Return negative value to differentiate return code */ | |
2768 | return -ret; | |
2769 | } | |
b389abbe | 2770 | |
54d01ffb DG |
2771 | /* |
2772 | * Command LTTNG_START_TRACE processed by the client thread. | |
2773 | */ | |
2774 | static int cmd_start_trace(struct ltt_session *session) | |
2775 | { | |
2776 | int ret; | |
54d01ffb | 2777 | struct ltt_kernel_session *ksession; |
b9d9b220 | 2778 | struct ltt_ust_session *usess; |
b389abbe | 2779 | |
54d01ffb DG |
2780 | /* Short cut */ |
2781 | ksession = session->kernel_session; | |
b9d9b220 | 2782 | usess = session->ust_session; |
5eb91c98 | 2783 | |
b9ef1c83 DG |
2784 | if (session->enabled) { |
2785 | ret = LTTCOMM_UST_START_FAIL; | |
2786 | goto error; | |
2787 | } | |
2788 | ||
464dd62d MD |
2789 | session->enabled = 1; |
2790 | ||
54d01ffb DG |
2791 | /* Kernel tracing */ |
2792 | if (ksession != NULL) { | |
2bdd86d4 MD |
2793 | struct ltt_kernel_channel *kchan; |
2794 | ||
54d01ffb DG |
2795 | /* Open kernel metadata */ |
2796 | if (ksession->metadata == NULL) { | |
2797 | ret = kernel_open_metadata(ksession, ksession->trace_path); | |
2798 | if (ret < 0) { | |
2799 | ret = LTTCOMM_KERN_META_FAIL; | |
2800 | goto error; | |
b389abbe | 2801 | } |
54d01ffb | 2802 | } |
7d29a247 | 2803 | |
54d01ffb DG |
2804 | /* Open kernel metadata stream */ |
2805 | if (ksession->metadata_stream_fd == 0) { | |
2806 | ret = kernel_open_metadata_stream(ksession); | |
2807 | if (ret < 0) { | |
2808 | ERR("Kernel create metadata stream failed"); | |
2809 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
2810 | goto error; | |
2811 | } | |
2812 | } | |
2813 | ||
2814 | /* For each channel */ | |
2815 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
2816 | if (kchan->stream_count == 0) { | |
2817 | ret = kernel_open_channel_stream(kchan); | |
2818 | if (ret < 0) { | |
2819 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
7d29a247 DG |
2820 | goto error; |
2821 | } | |
54d01ffb DG |
2822 | /* Update the stream global counter */ |
2823 | ksession->stream_count_global += ret; | |
7d29a247 | 2824 | } |
54d01ffb DG |
2825 | } |
2826 | ||
2827 | /* Setup kernel consumer socket and send fds to it */ | |
2828 | ret = init_kernel_tracing(ksession); | |
2829 | if (ret < 0) { | |
2830 | ret = LTTCOMM_KERN_START_FAIL; | |
2831 | goto error; | |
2832 | } | |
2833 | ||
2834 | /* This start the kernel tracing */ | |
2835 | ret = kernel_start_session(ksession); | |
2836 | if (ret < 0) { | |
2837 | ret = LTTCOMM_KERN_START_FAIL; | |
2838 | goto error; | |
2839 | } | |
2840 | ||
2841 | /* Quiescent wait after starting trace */ | |
2842 | kernel_wait_quiescent(kernel_tracer_fd); | |
2843 | } | |
2844 | ||
36dc12cc | 2845 | /* Flag session that trace should start automatically */ |
b9d9b220 DG |
2846 | if (usess) { |
2847 | usess->start_trace = 1; | |
36dc12cc | 2848 | |
b9d9b220 DG |
2849 | ret = ust_app_start_trace_all(usess); |
2850 | if (ret < 0) { | |
2851 | ret = LTTCOMM_UST_START_FAIL; | |
2852 | goto error; | |
2853 | } | |
2bdd86d4 | 2854 | } |
54d01ffb DG |
2855 | |
2856 | ret = LTTCOMM_OK; | |
2857 | ||
2858 | error: | |
2859 | return ret; | |
2860 | } | |
2861 | ||
2862 | /* | |
2863 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2864 | */ | |
2865 | static int cmd_stop_trace(struct ltt_session *session) | |
2866 | { | |
2867 | int ret; | |
2868 | struct ltt_kernel_channel *kchan; | |
2869 | struct ltt_kernel_session *ksession; | |
8be98f9a | 2870 | struct ltt_ust_session *usess; |
54d01ffb DG |
2871 | |
2872 | /* Short cut */ | |
2873 | ksession = session->kernel_session; | |
8be98f9a | 2874 | usess = session->ust_session; |
54d01ffb | 2875 | |
b9ef1c83 DG |
2876 | if (!session->enabled) { |
2877 | ret = LTTCOMM_UST_START_FAIL; | |
2878 | goto error; | |
2879 | } | |
2880 | ||
464dd62d MD |
2881 | session->enabled = 0; |
2882 | ||
54d01ffb DG |
2883 | /* Kernel tracer */ |
2884 | if (ksession != NULL) { | |
2885 | DBG("Stop kernel tracing"); | |
2886 | ||
2887 | /* Flush all buffers before stopping */ | |
2888 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2889 | if (ret < 0) { | |
2890 | ERR("Kernel metadata flush failed"); | |
2891 | } | |
f34daff7 | 2892 | |
54d01ffb DG |
2893 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
2894 | ret = kernel_flush_buffer(kchan); | |
0d0c377a | 2895 | if (ret < 0) { |
54d01ffb | 2896 | ERR("Kernel flush buffer error"); |
7d29a247 | 2897 | } |
54d01ffb | 2898 | } |
19e70852 | 2899 | |
54d01ffb DG |
2900 | ret = kernel_stop_session(ksession); |
2901 | if (ret < 0) { | |
2902 | ret = LTTCOMM_KERN_STOP_FAIL; | |
19e70852 | 2903 | goto error; |
f3ed775e | 2904 | } |
54d01ffb DG |
2905 | |
2906 | kernel_wait_quiescent(kernel_tracer_fd); | |
894be886 | 2907 | } |
54d01ffb | 2908 | |
8be98f9a MD |
2909 | if (usess) { |
2910 | usess->start_trace = 0; | |
2bdd86d4 | 2911 | |
8be98f9a | 2912 | ret = ust_app_stop_trace_all(usess); |
2bdd86d4 | 2913 | if (ret < 0) { |
8be98f9a | 2914 | ret = LTTCOMM_UST_START_FAIL; |
2bdd86d4 MD |
2915 | goto error; |
2916 | } | |
2bdd86d4 | 2917 | } |
54d01ffb DG |
2918 | |
2919 | ret = LTTCOMM_OK; | |
2920 | ||
2921 | error: | |
2922 | return ret; | |
2923 | } | |
2924 | ||
2925 | /* | |
2926 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
2927 | */ | |
6df2e2c9 | 2928 | static int cmd_create_session(char *name, char *path, struct ucred *creds) |
54d01ffb DG |
2929 | { |
2930 | int ret; | |
2931 | ||
6df2e2c9 | 2932 | ret = session_create(name, path, creds->uid, creds->gid); |
54d01ffb DG |
2933 | if (ret != LTTCOMM_OK) { |
2934 | goto error; | |
2935 | } | |
2936 | ||
2937 | ret = LTTCOMM_OK; | |
2938 | ||
2939 | error: | |
2940 | return ret; | |
2941 | } | |
2942 | ||
2943 | /* | |
2944 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
2945 | */ | |
2946 | static int cmd_destroy_session(struct ltt_session *session, char *name) | |
2947 | { | |
2948 | int ret; | |
2949 | ||
2950 | /* Clean kernel session teardown */ | |
2951 | teardown_kernel_session(session); | |
84cd17c6 MD |
2952 | /* UST session teardown */ |
2953 | teardown_ust_session(session); | |
54d01ffb DG |
2954 | |
2955 | /* | |
2956 | * Must notify the kernel thread here to update it's poll setin order | |
2957 | * to remove the channel(s)' fd just destroyed. | |
2958 | */ | |
2959 | ret = notify_thread_pipe(kernel_poll_pipe[1]); | |
2960 | if (ret < 0) { | |
2961 | perror("write kernel poll pipe"); | |
2962 | } | |
2963 | ||
271933a4 | 2964 | ret = session_destroy(session); |
54d01ffb DG |
2965 | |
2966 | return ret; | |
2967 | } | |
2968 | ||
2969 | /* | |
2970 | * Command LTTNG_CALIBRATE processed by the client thread. | |
2971 | */ | |
2972 | static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) | |
2973 | { | |
2974 | int ret; | |
2975 | ||
2976 | switch (domain) { | |
2977 | case LTTNG_DOMAIN_KERNEL: | |
33a2b854 | 2978 | { |
54d01ffb | 2979 | struct lttng_kernel_calibrate kcalibrate; |
33a2b854 | 2980 | |
54d01ffb DG |
2981 | kcalibrate.type = calibrate->type; |
2982 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); | |
33a2b854 | 2983 | if (ret < 0) { |
54d01ffb DG |
2984 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
2985 | goto error; | |
33a2b854 | 2986 | } |
54d01ffb DG |
2987 | break; |
2988 | } | |
2989 | default: | |
2990 | /* TODO: Userspace tracing */ | |
2991 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2992 | goto error; | |
2993 | } | |
33a2b854 | 2994 | |
54d01ffb | 2995 | ret = LTTCOMM_OK; |
33a2b854 | 2996 | |
54d01ffb DG |
2997 | error: |
2998 | return ret; | |
2999 | } | |
7d29a247 | 3000 | |
54d01ffb DG |
3001 | /* |
3002 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3003 | */ | |
3004 | static int cmd_register_consumer(struct ltt_session *session, int domain, | |
3005 | char *sock_path) | |
3006 | { | |
3007 | int ret, sock; | |
b389abbe | 3008 | |
54d01ffb DG |
3009 | switch (domain) { |
3010 | case LTTNG_DOMAIN_KERNEL: | |
3011 | /* Can't register a consumer if there is already one */ | |
48c4f28c | 3012 | if (session->kernel_session->consumer_fds_sent != 0) { |
54d01ffb DG |
3013 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
3014 | goto error; | |
3015 | } | |
3016 | ||
3017 | sock = lttcomm_connect_unix_sock(sock_path); | |
3018 | if (sock < 0) { | |
3019 | ret = LTTCOMM_CONNECT_FAIL; | |
3020 | goto error; | |
3021 | } | |
3022 | ||
3023 | session->kernel_session->consumer_fd = sock; | |
3024 | break; | |
3025 | default: | |
3026 | /* TODO: Userspace tracing */ | |
3027 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
3028 | goto error; | |
3029 | } | |
3030 | ||
3031 | ret = LTTCOMM_OK; | |
3032 | ||
3033 | error: | |
3034 | return ret; | |
3035 | } | |
3036 | ||
3037 | /* | |
3038 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3039 | */ | |
3040 | static ssize_t cmd_list_domains(struct ltt_session *session, | |
3041 | struct lttng_domain **domains) | |
3042 | { | |
b551a063 | 3043 | int ret, index = 0; |
54d01ffb DG |
3044 | ssize_t nb_dom = 0; |
3045 | ||
3046 | if (session->kernel_session != NULL) { | |
b551a063 | 3047 | DBG3("Listing domains found kernel domain"); |
54d01ffb DG |
3048 | nb_dom++; |
3049 | } | |
3050 | ||
b551a063 DG |
3051 | if (session->ust_session != NULL) { |
3052 | DBG3("Listing domains found UST global domain"); | |
3053 | nb_dom++; | |
3054 | } | |
54d01ffb | 3055 | |
b551a063 | 3056 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
54d01ffb DG |
3057 | if (*domains == NULL) { |
3058 | ret = -LTTCOMM_FATAL; | |
3059 | goto error; | |
3060 | } | |
3061 | ||
b551a063 DG |
3062 | if (session->kernel_session != NULL) { |
3063 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
3064 | index++; | |
3065 | } | |
3066 | ||
3067 | if (session->ust_session != NULL) { | |
3068 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
3069 | index++; | |
3070 | } | |
54d01ffb | 3071 | |
54d01ffb DG |
3072 | return nb_dom; |
3073 | ||
3074 | error: | |
3075 | return ret; | |
3076 | } | |
3077 | ||
3078 | /* | |
3079 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3080 | */ | |
b551a063 | 3081 | static ssize_t cmd_list_channels(int domain, struct ltt_session *session, |
54d01ffb DG |
3082 | struct lttng_channel **channels) |
3083 | { | |
3084 | int ret; | |
3085 | ssize_t nb_chan = 0; | |
3086 | ||
b551a063 DG |
3087 | switch (domain) { |
3088 | case LTTNG_DOMAIN_KERNEL: | |
3089 | if (session->kernel_session != NULL) { | |
3090 | nb_chan = session->kernel_session->channel_count; | |
3091 | } | |
0df502fd | 3092 | DBG3("Number of kernel channels %zd", nb_chan); |
b551a063 DG |
3093 | break; |
3094 | case LTTNG_DOMAIN_UST: | |
3095 | if (session->ust_session != NULL) { | |
3096 | nb_chan = hashtable_get_count( | |
3097 | session->ust_session->domain_global.channels); | |
3098 | } | |
0df502fd | 3099 | DBG3("Number of UST global channels %zd", nb_chan); |
b551a063 DG |
3100 | break; |
3101 | default: | |
3102 | *channels = NULL; | |
3103 | ret = -LTTCOMM_NOT_IMPLEMENTED; | |
54d01ffb DG |
3104 | goto error; |
3105 | } | |
3106 | ||
b551a063 DG |
3107 | if (nb_chan > 0) { |
3108 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); | |
3109 | if (*channels == NULL) { | |
3110 | ret = -LTTCOMM_FATAL; | |
3111 | goto error; | |
3112 | } | |
54d01ffb | 3113 | |
b551a063 DG |
3114 | list_lttng_channels(domain, session, *channels); |
3115 | } else { | |
3116 | *channels = NULL; | |
3117 | } | |
2bdd86d4 | 3118 | |
54d01ffb DG |
3119 | return nb_chan; |
3120 | ||
3121 | error: | |
3122 | return ret; | |
3123 | } | |
3124 | ||
3125 | /* | |
3126 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3127 | */ | |
b551a063 | 3128 | static ssize_t cmd_list_events(int domain, struct ltt_session *session, |
54d01ffb DG |
3129 | char *channel_name, struct lttng_event **events) |
3130 | { | |
b551a063 | 3131 | int ret = 0; |
54d01ffb | 3132 | ssize_t nb_event = 0; |
54d01ffb | 3133 | |
b551a063 DG |
3134 | switch (domain) { |
3135 | case LTTNG_DOMAIN_KERNEL: | |
3136 | if (session->kernel_session != NULL) { | |
3137 | nb_event = list_lttng_kernel_events(channel_name, | |
3138 | session->kernel_session, events); | |
54d01ffb | 3139 | } |
b551a063 DG |
3140 | break; |
3141 | case LTTNG_DOMAIN_UST: | |
3142 | { | |
3143 | if (session->ust_session != NULL) { | |
3144 | nb_event = list_lttng_ust_global_events(channel_name, | |
3145 | &session->ust_session->domain_global, events); | |
3146 | } | |
3147 | break; | |
54d01ffb | 3148 | } |
b551a063 DG |
3149 | default: |
3150 | ret = -LTTCOMM_NOT_IMPLEMENTED; | |
54d01ffb DG |
3151 | goto error; |
3152 | } | |
3153 | ||
b551a063 | 3154 | ret = nb_event; |
54d01ffb DG |
3155 | |
3156 | error: | |
3157 | return ret; | |
3158 | } | |
3159 | ||
3160 | /* | |
3161 | * Process the command requested by the lttng client within the command | |
3162 | * context structure. This function make sure that the return structure (llm) | |
3163 | * is set and ready for transmission before returning. | |
3164 | * | |
3165 | * Return any error encountered or 0 for success. | |
3166 | */ | |
3167 | static int process_client_msg(struct command_ctx *cmd_ctx) | |
3168 | { | |
3169 | int ret = LTTCOMM_OK; | |
44d3bd01 | 3170 | int need_tracing_session = 1; |
54d01ffb DG |
3171 | |
3172 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
3173 | ||
4fba7219 DG |
3174 | if (opt_no_kernel && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { |
3175 | ret = LTTCOMM_KERN_NA; | |
3176 | goto error; | |
3177 | } | |
3178 | ||
54d01ffb DG |
3179 | /* |
3180 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 3181 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
3182 | * command. |
3183 | */ | |
3184 | switch(cmd_ctx->lsm->cmd_type) { | |
3185 | case LTTNG_LIST_SESSIONS: | |
3186 | case LTTNG_LIST_TRACEPOINTS: | |
3187 | case LTTNG_LIST_DOMAINS: | |
3188 | case LTTNG_LIST_CHANNELS: | |
3189 | case LTTNG_LIST_EVENTS: | |
3190 | break; | |
3191 | default: | |
3192 | /* Setup lttng message with no payload */ | |
3193 | ret = setup_lttng_msg(cmd_ctx, 0); | |
3194 | if (ret < 0) { | |
3195 | /* This label does not try to unlock the session */ | |
3196 | goto init_setup_error; | |
3197 | } | |
3198 | } | |
3199 | ||
3200 | /* Commands that DO NOT need a session. */ | |
3201 | switch (cmd_ctx->lsm->cmd_type) { | |
3202 | case LTTNG_CALIBRATE: | |
3203 | case LTTNG_CREATE_SESSION: | |
3204 | case LTTNG_LIST_SESSIONS: | |
3205 | case LTTNG_LIST_TRACEPOINTS: | |
44d3bd01 | 3206 | need_tracing_session = 0; |
54d01ffb DG |
3207 | break; |
3208 | default: | |
3209 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
74babd95 | 3210 | session_lock_list(); |
54d01ffb | 3211 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
74babd95 | 3212 | session_unlock_list(); |
54d01ffb DG |
3213 | if (cmd_ctx->session == NULL) { |
3214 | if (cmd_ctx->lsm->session.name != NULL) { | |
3215 | ret = LTTCOMM_SESS_NOT_FOUND; | |
3216 | } else { | |
3217 | /* If no session name specified */ | |
3218 | ret = LTTCOMM_SELECT_SESS; | |
3219 | } | |
3220 | goto error; | |
3221 | } else { | |
3222 | /* Acquire lock for the session */ | |
3223 | session_lock(cmd_ctx->session); | |
3224 | } | |
3225 | break; | |
3226 | } | |
b389abbe | 3227 | |
54d01ffb DG |
3228 | /* |
3229 | * Check domain type for specific "pre-action". | |
3230 | */ | |
3231 | switch (cmd_ctx->lsm->domain.type) { | |
3232 | case LTTNG_DOMAIN_KERNEL: | |
3233 | /* Kernel tracer check */ | |
3234 | if (kernel_tracer_fd == 0) { | |
3235 | /* Basically, load kernel tracer modules */ | |
3236 | init_kernel_tracer(); | |
3237 | if (kernel_tracer_fd == 0) { | |
3238 | ret = LTTCOMM_KERN_NA; | |
3239 | goto error; | |
3240 | } | |
3241 | } | |
5eb91c98 | 3242 | |
54d01ffb | 3243 | /* Need a session for kernel command */ |
44d3bd01 | 3244 | if (need_tracing_session) { |
54d01ffb | 3245 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 3246 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 3247 | if (ret < 0) { |
54d01ffb | 3248 | ret = LTTCOMM_KERN_SESS_FAIL; |
5eb91c98 DG |
3249 | goto error; |
3250 | } | |
b389abbe | 3251 | } |
7d29a247 | 3252 | |
54d01ffb | 3253 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
3254 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
3255 | if (kconsumer_data.pid == 0 && | |
54d01ffb | 3256 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
3257 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
3258 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 3259 | if (ret < 0) { |
54d01ffb DG |
3260 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
3261 | goto error; | |
950131af | 3262 | } |
3ff2ecac MD |
3263 | } else { |
3264 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 3265 | } |
0d0c377a | 3266 | } |
54d01ffb | 3267 | break; |
2bdd86d4 | 3268 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 3269 | { |
44d3bd01 | 3270 | if (need_tracing_session) { |
f6a9efaa | 3271 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 3272 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 3273 | &cmd_ctx->lsm->domain); |
44d3bd01 DG |
3274 | if (ret != LTTCOMM_OK) { |
3275 | goto error; | |
3276 | } | |
3277 | } | |
7753dea8 MD |
3278 | /* Start the UST consumer daemons */ |
3279 | /* 64-bit */ | |
3280 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 3281 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 3282 | ustconsumer64_data.pid == 0 && |
2bdd86d4 | 3283 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3284 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3285 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 3286 | if (ret < 0) { |
7753dea8 MD |
3287 | ret = LTTCOMM_UST_CONSUMER64_FAIL; |
3288 | ust_consumerd64_fd = -EINVAL; | |
2bdd86d4 MD |
3289 | goto error; |
3290 | } | |
48842b30 | 3291 | |
7753dea8 | 3292 | ust_consumerd64_fd = ustconsumer64_data.cmd_sock; |
3ff2ecac | 3293 | } else { |
7753dea8 MD |
3294 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3295 | } | |
3296 | /* 32-bit */ | |
fc7a59ce | 3297 | if (consumerd32_bin[0] != '\0' && |
7753dea8 MD |
3298 | ustconsumer32_data.pid == 0 && |
3299 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { | |
3300 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
3301 | ret = start_consumerd(&ustconsumer32_data); | |
3302 | if (ret < 0) { | |
3303 | ret = LTTCOMM_UST_CONSUMER32_FAIL; | |
3304 | ust_consumerd32_fd = -EINVAL; | |
3305 | goto error; | |
3306 | } | |
3307 | ust_consumerd32_fd = ustconsumer32_data.cmd_sock; | |
3308 | } else { | |
3309 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 3310 | } |
44d3bd01 DG |
3311 | } |
3312 | break; | |
48842b30 | 3313 | } |
54d01ffb | 3314 | default: |
54d01ffb DG |
3315 | break; |
3316 | } | |
33a2b854 | 3317 | |
54d01ffb DG |
3318 | /* Process by command type */ |
3319 | switch (cmd_ctx->lsm->cmd_type) { | |
3320 | case LTTNG_ADD_CONTEXT: | |
3321 | { | |
3322 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3323 | cmd_ctx->lsm->u.context.channel_name, | |
3324 | cmd_ctx->lsm->u.context.event_name, | |
3325 | &cmd_ctx->lsm->u.context.ctx); | |
3326 | break; | |
3327 | } | |
3328 | case LTTNG_DISABLE_CHANNEL: | |
3329 | { | |
3330 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3331 | cmd_ctx->lsm->u.disable.channel_name); | |
3332 | break; | |
3333 | } | |
3334 | case LTTNG_DISABLE_EVENT: | |
3335 | { | |
3336 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3337 | cmd_ctx->lsm->u.disable.channel_name, | |
3338 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
3339 | ret = LTTCOMM_OK; |
3340 | break; | |
3341 | } | |
54d01ffb DG |
3342 | case LTTNG_DISABLE_ALL_EVENT: |
3343 | { | |
2bdd86d4 | 3344 | DBG("Disabling all events"); |
54d01ffb DG |
3345 | |
3346 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3347 | cmd_ctx->lsm->u.disable.channel_name); | |
3348 | break; | |
3349 | } | |
3350 | case LTTNG_ENABLE_CHANNEL: | |
3351 | { | |
aea829b3 | 3352 | ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
54d01ffb DG |
3353 | &cmd_ctx->lsm->u.channel.chan); |
3354 | break; | |
3355 | } | |
3356 | case LTTNG_ENABLE_EVENT: | |
3357 | { | |
3358 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3359 | cmd_ctx->lsm->u.enable.channel_name, | |
3360 | &cmd_ctx->lsm->u.enable.event); | |
3361 | break; | |
3362 | } | |
3363 | case LTTNG_ENABLE_ALL_EVENT: | |
3364 | { | |
2bdd86d4 | 3365 | DBG("Enabling all events"); |
54d01ffb DG |
3366 | |
3367 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
8c9ae521 DG |
3368 | cmd_ctx->lsm->u.enable.channel_name, |
3369 | cmd_ctx->lsm->u.enable.event.type); | |
54d01ffb DG |
3370 | break; |
3371 | } | |
052da939 | 3372 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 3373 | { |
9f19cc17 | 3374 | struct lttng_event *events; |
54d01ffb | 3375 | ssize_t nb_events; |
052da939 | 3376 | |
54d01ffb DG |
3377 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
3378 | if (nb_events < 0) { | |
3379 | ret = -nb_events; | |
3380 | goto error; | |
2ef84c95 DG |
3381 | } |
3382 | ||
3383 | /* | |
3384 | * Setup lttng message with payload size set to the event list size in | |
3385 | * bytes and then copy list into the llm payload. | |
3386 | */ | |
052da939 | 3387 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 3388 | if (ret < 0) { |
052da939 | 3389 | free(events); |
2ef84c95 DG |
3390 | goto setup_error; |
3391 | } | |
3392 | ||
3393 | /* Copy event list into message payload */ | |
9f19cc17 | 3394 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 3395 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 3396 | |
9f19cc17 | 3397 | free(events); |
2ef84c95 DG |
3398 | |
3399 | ret = LTTCOMM_OK; | |
3400 | break; | |
3401 | } | |
f3ed775e | 3402 | case LTTNG_START_TRACE: |
8c0faa1d | 3403 | { |
54d01ffb | 3404 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3405 | break; |
3406 | } | |
f3ed775e | 3407 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3408 | { |
54d01ffb | 3409 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3410 | break; |
3411 | } | |
5e16da05 MD |
3412 | case LTTNG_CREATE_SESSION: |
3413 | { | |
54d01ffb | 3414 | ret = cmd_create_session(cmd_ctx->lsm->session.name, |
6df2e2c9 | 3415 | cmd_ctx->lsm->session.path, &cmd_ctx->creds); |
5e16da05 MD |
3416 | break; |
3417 | } | |
3418 | case LTTNG_DESTROY_SESSION: | |
3419 | { | |
54d01ffb DG |
3420 | ret = cmd_destroy_session(cmd_ctx->session, |
3421 | cmd_ctx->lsm->session.name); | |
5461b305 | 3422 | break; |
5e16da05 | 3423 | } |
9f19cc17 | 3424 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3425 | { |
54d01ffb DG |
3426 | ssize_t nb_dom; |
3427 | struct lttng_domain *domains; | |
5461b305 | 3428 | |
54d01ffb DG |
3429 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3430 | if (nb_dom < 0) { | |
3431 | ret = -nb_dom; | |
3432 | goto error; | |
ce3d728c | 3433 | } |
520ff687 | 3434 | |
54d01ffb | 3435 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 DG |
3436 | if (ret < 0) { |
3437 | goto setup_error; | |
520ff687 | 3438 | } |
57167058 | 3439 | |
54d01ffb DG |
3440 | /* Copy event list into message payload */ |
3441 | memcpy(cmd_ctx->llm->payload, domains, | |
3442 | nb_dom * sizeof(struct lttng_domain)); | |
3443 | ||
3444 | free(domains); | |
5e16da05 | 3445 | |
5461b305 | 3446 | ret = LTTCOMM_OK; |
5e16da05 MD |
3447 | break; |
3448 | } | |
9f19cc17 | 3449 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3450 | { |
54d01ffb DG |
3451 | size_t nb_chan; |
3452 | struct lttng_channel *channels; | |
3453 | ||
b551a063 DG |
3454 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
3455 | cmd_ctx->session, &channels); | |
54d01ffb DG |
3456 | if (nb_chan < 0) { |
3457 | ret = -nb_chan; | |
3458 | goto error; | |
5461b305 | 3459 | } |
ca95a216 | 3460 | |
54d01ffb | 3461 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 DG |
3462 | if (ret < 0) { |
3463 | goto setup_error; | |
3464 | } | |
9f19cc17 | 3465 | |
54d01ffb DG |
3466 | /* Copy event list into message payload */ |
3467 | memcpy(cmd_ctx->llm->payload, channels, | |
3468 | nb_chan * sizeof(struct lttng_channel)); | |
3469 | ||
3470 | free(channels); | |
9f19cc17 DG |
3471 | |
3472 | ret = LTTCOMM_OK; | |
5461b305 | 3473 | break; |
5e16da05 | 3474 | } |
9f19cc17 | 3475 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3476 | { |
b551a063 | 3477 | ssize_t nb_event; |
684d34d2 | 3478 | struct lttng_event *events = NULL; |
9f19cc17 | 3479 | |
b551a063 | 3480 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
54d01ffb DG |
3481 | cmd_ctx->lsm->u.list.channel_name, &events); |
3482 | if (nb_event < 0) { | |
3483 | ret = -nb_event; | |
3484 | goto error; | |
5461b305 | 3485 | } |
ca95a216 | 3486 | |
54d01ffb | 3487 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 DG |
3488 | if (ret < 0) { |
3489 | goto setup_error; | |
3490 | } | |
9f19cc17 | 3491 | |
54d01ffb DG |
3492 | /* Copy event list into message payload */ |
3493 | memcpy(cmd_ctx->llm->payload, events, | |
3494 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 3495 | |
54d01ffb | 3496 | free(events); |
9f19cc17 DG |
3497 | |
3498 | ret = LTTCOMM_OK; | |
5461b305 | 3499 | break; |
5e16da05 MD |
3500 | } |
3501 | case LTTNG_LIST_SESSIONS: | |
3502 | { | |
54d01ffb | 3503 | session_lock_list(); |
5461b305 | 3504 | |
6c9cc2ab | 3505 | if (session_list_ptr->count == 0) { |
f3ed775e | 3506 | ret = LTTCOMM_NO_SESSION; |
54d01ffb | 3507 | session_unlock_list(); |
5461b305 | 3508 | goto error; |
57167058 | 3509 | } |
5e16da05 | 3510 | |
6c9cc2ab DG |
3511 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * |
3512 | session_list_ptr->count); | |
5461b305 | 3513 | if (ret < 0) { |
54d01ffb | 3514 | session_unlock_list(); |
5461b305 | 3515 | goto setup_error; |
e065084a | 3516 | } |
5e16da05 | 3517 | |
6c9cc2ab DG |
3518 | /* Filled the session array */ |
3519 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload)); | |
3520 | ||
54d01ffb | 3521 | session_unlock_list(); |
5e16da05 | 3522 | |
5461b305 | 3523 | ret = LTTCOMM_OK; |
5e16da05 MD |
3524 | break; |
3525 | } | |
d0254c7c MD |
3526 | case LTTNG_CALIBRATE: |
3527 | { | |
54d01ffb DG |
3528 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
3529 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
3530 | break; |
3531 | } | |
d9800920 DG |
3532 | case LTTNG_REGISTER_CONSUMER: |
3533 | { | |
54d01ffb DG |
3534 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
3535 | cmd_ctx->lsm->u.reg.path); | |
d9800920 DG |
3536 | break; |
3537 | } | |
5e16da05 | 3538 | default: |
5e16da05 | 3539 | ret = LTTCOMM_UND; |
5461b305 | 3540 | break; |
fac6795d DG |
3541 | } |
3542 | ||
5461b305 | 3543 | error: |
5461b305 DG |
3544 | if (cmd_ctx->llm == NULL) { |
3545 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 3546 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
3547 | goto setup_error; |
3548 | } | |
3549 | } | |
54d01ffb | 3550 | /* Set return code */ |
5461b305 | 3551 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 3552 | setup_error: |
b5541356 | 3553 | if (cmd_ctx->session) { |
54d01ffb | 3554 | session_unlock(cmd_ctx->session); |
b5541356 | 3555 | } |
54d01ffb | 3556 | init_setup_error: |
8028d920 | 3557 | return ret; |
fac6795d DG |
3558 | } |
3559 | ||
1d4b027a | 3560 | /* |
d063d709 DG |
3561 | * This thread manage all clients request using the unix client socket for |
3562 | * communication. | |
1d4b027a DG |
3563 | */ |
3564 | static void *thread_manage_clients(void *data) | |
3565 | { | |
5eb91c98 DG |
3566 | int sock = 0, ret, i, pollfd; |
3567 | uint32_t revents, nb_fd; | |
273ea72c | 3568 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 3569 | struct lttng_poll_event events; |
1d4b027a DG |
3570 | |
3571 | DBG("[thread] Manage client started"); | |
3572 | ||
f6a9efaa DG |
3573 | rcu_register_thread(); |
3574 | ||
1d4b027a DG |
3575 | ret = lttcomm_listen_unix_sock(client_sock); |
3576 | if (ret < 0) { | |
3577 | goto error; | |
3578 | } | |
3579 | ||
5eb91c98 DG |
3580 | /* |
3581 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3582 | * more will be added to this poll set. | |
3583 | */ | |
3584 | ret = create_thread_poll_set(&events, 2); | |
3585 | if (ret < 0) { | |
3586 | goto error; | |
3587 | } | |
273ea72c | 3588 | |
5eb91c98 DG |
3589 | /* Add the application registration socket */ |
3590 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
3591 | if (ret < 0) { | |
3592 | goto error; | |
3593 | } | |
273ea72c | 3594 | |
bbd973c2 DG |
3595 | /* |
3596 | * Notify parent pid that we are ready to accept command for client side. | |
1d4b027a DG |
3597 | */ |
3598 | if (opt_sig_parent) { | |
3599 | kill(ppid, SIGCHLD); | |
3600 | } | |
3601 | ||
3602 | while (1) { | |
1d4b027a | 3603 | DBG("Accepting client command ..."); |
273ea72c | 3604 | |
5eb91c98 DG |
3605 | nb_fd = LTTNG_POLL_GETNB(&events); |
3606 | ||
273ea72c | 3607 | /* Inifinite blocking call, waiting for transmission */ |
5eb91c98 | 3608 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 3609 | if (ret < 0) { |
273ea72c DG |
3610 | goto error; |
3611 | } | |
3612 | ||
5eb91c98 DG |
3613 | for (i = 0; i < nb_fd; i++) { |
3614 | /* Fetch once the poll data */ | |
3615 | revents = LTTNG_POLL_GETEV(&events, i); | |
3616 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3617 | ||
3618 | /* Thread quit pipe has been closed. Killing thread. */ | |
3619 | ret = check_thread_quit_pipe(pollfd, revents); | |
3620 | if (ret) { | |
3621 | goto error; | |
3622 | } | |
3623 | ||
3624 | /* Event on the registration socket */ | |
3625 | if (pollfd == client_sock) { | |
3626 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3627 | ERR("Client socket poll error"); | |
3628 | goto error; | |
3629 | } | |
3630 | } | |
3631 | } | |
3632 | ||
3633 | DBG("Wait for client response"); | |
3634 | ||
3635 | sock = lttcomm_accept_unix_sock(client_sock); | |
3636 | if (sock < 0) { | |
273ea72c | 3637 | goto error; |
273ea72c DG |
3638 | } |
3639 | ||
be040666 DG |
3640 | /* Set socket option for credentials retrieval */ |
3641 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
3642 | if (ret < 0) { | |
3643 | goto error; | |
3644 | } | |
3645 | ||
5eb91c98 | 3646 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 3647 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 3648 | if (cmd_ctx == NULL) { |
ba7f0ae5 | 3649 | perror("zmalloc cmd_ctx"); |
1d4b027a | 3650 | goto error; |
5eb91c98 | 3651 | } |
1d4b027a | 3652 | |
5eb91c98 | 3653 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 3654 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 3655 | if (cmd_ctx->lsm == NULL) { |
ba7f0ae5 | 3656 | perror("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
3657 | goto error; |
3658 | } | |
1d4b027a | 3659 | |
5eb91c98 DG |
3660 | cmd_ctx->llm = NULL; |
3661 | cmd_ctx->session = NULL; | |
1d4b027a | 3662 | |
5eb91c98 DG |
3663 | /* |
3664 | * Data is received from the lttng client. The struct | |
3665 | * lttcomm_session_msg (lsm) contains the command and data request of | |
3666 | * the client. | |
3667 | */ | |
3668 | DBG("Receiving data from client ..."); | |
be040666 DG |
3669 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
3670 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
3671 | if (ret <= 0) { |
3672 | DBG("Nothing recv() from client... continuing"); | |
3673 | close(sock); | |
3674 | free(cmd_ctx); | |
3675 | continue; | |
3676 | } | |
1d4b027a | 3677 | |
5eb91c98 DG |
3678 | // TODO: Validate cmd_ctx including sanity check for |
3679 | // security purpose. | |
f7776ea7 | 3680 | |
f6a9efaa | 3681 | rcu_thread_online(); |
5eb91c98 DG |
3682 | /* |
3683 | * This function dispatch the work to the kernel or userspace tracer | |
3684 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
3685 | * informations for the client. The command context struct contains | |
3686 | * everything this function may needs. | |
3687 | */ | |
3688 | ret = process_client_msg(cmd_ctx); | |
f6a9efaa | 3689 | rcu_thread_offline(); |
5eb91c98 | 3690 | if (ret < 0) { |
bbd973c2 | 3691 | /* |
5eb91c98 | 3692 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 3693 | * this point, ret < 0 means that a zmalloc failed |
5eb91c98 | 3694 | * (ENOMEM). Error detected but still accept command. |
bbd973c2 | 3695 | */ |
bbd973c2 | 3696 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3697 | continue; |
3698 | } | |
d6e4fca4 | 3699 | |
54d01ffb DG |
3700 | DBG("Sending response (size: %d, retcode: %s)", |
3701 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 3702 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 3703 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
3704 | if (ret < 0) { |
3705 | ERR("Failed to send data back to client"); | |
bbd973c2 | 3706 | } |
1d4b027a | 3707 | |
5eb91c98 | 3708 | /* End of transmission */ |
273ea72c | 3709 | close(sock); |
be040666 DG |
3710 | |
3711 | clean_command_ctx(&cmd_ctx); | |
273ea72c DG |
3712 | } |
3713 | ||
5eb91c98 DG |
3714 | error: |
3715 | DBG("Client thread dying"); | |
273ea72c | 3716 | unlink(client_unix_sock_path); |
5eb91c98 DG |
3717 | close(client_sock); |
3718 | close(sock); | |
273ea72c | 3719 | |
5eb91c98 | 3720 | lttng_poll_clean(&events); |
a2fb29a5 | 3721 | clean_command_ctx(&cmd_ctx); |
f6a9efaa DG |
3722 | |
3723 | rcu_unregister_thread(); | |
1d4b027a DG |
3724 | return NULL; |
3725 | } | |
3726 | ||
3727 | ||
fac6795d DG |
3728 | /* |
3729 | * usage function on stderr | |
3730 | */ | |
3731 | static void usage(void) | |
3732 | { | |
b716ce68 | 3733 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
3734 | fprintf(stderr, " -h, --help Display this usage.\n"); |
3735 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
3736 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
3737 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
3738 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
7753dea8 MD |
3739 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
3740 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); | |
3741 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); | |
3742 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); | |
ebaeda94 MD |
3743 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
3744 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); | |
3745 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); | |
3746 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); | |
d6f42150 DG |
3747 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
3748 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
3749 | fprintf(stderr, " -V, --version Show version number.\n"); | |
3750 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
3751 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
3752 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
3bd1e081 | 3753 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
4fba7219 | 3754 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
fac6795d DG |
3755 | } |
3756 | ||
3757 | /* | |
3758 | * daemon argument parsing | |
3759 | */ | |
3760 | static int parse_args(int argc, char **argv) | |
3761 | { | |
3762 | int c; | |
3763 | ||
3764 | static struct option long_options[] = { | |
3765 | { "client-sock", 1, 0, 'c' }, | |
3766 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
3767 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
3768 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
7753dea8 MD |
3769 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
3770 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
ebaeda94 MD |
3771 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
3772 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
3773 | { "consumerd32-path", 1, 0, 'u' }, | |
3774 | { "consumerd32-libdir", 1, 0, 'U' }, | |
3775 | { "consumerd64-path", 1, 0, 't' }, | |
3776 | { "consumerd64-libdir", 1, 0, 'T' }, | |
fac6795d | 3777 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 3778 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
3779 | { "help", 0, 0, 'h' }, |
3780 | { "group", 1, 0, 'g' }, | |
3781 | { "version", 0, 0, 'V' }, | |
75462a81 | 3782 | { "quiet", 0, 0, 'q' }, |
3f9947db | 3783 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 3784 | { "verbose-consumer", 0, 0, 'Z' }, |
4fba7219 | 3785 | { "no-kernel", 0, 0, 'N' }, |
fac6795d DG |
3786 | { NULL, 0, 0, 0 } |
3787 | }; | |
3788 | ||
3789 | while (1) { | |
3790 | int option_index = 0; | |
4fba7219 | 3791 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t", |
54d01ffb | 3792 | long_options, &option_index); |
fac6795d DG |
3793 | if (c == -1) { |
3794 | break; | |
3795 | } | |
3796 | ||
3797 | switch (c) { | |
3798 | case 0: | |
3799 | fprintf(stderr, "option %s", long_options[option_index].name); | |
3800 | if (optarg) { | |
3801 | fprintf(stderr, " with arg %s\n", optarg); | |
3802 | } | |
3803 | break; | |
b716ce68 | 3804 | case 'c': |
fac6795d DG |
3805 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
3806 | break; | |
3807 | case 'a': | |
3808 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
3809 | break; | |
3810 | case 'd': | |
3811 | opt_daemon = 1; | |
3812 | break; | |
3813 | case 'g': | |
fb09408a | 3814 | opt_tracing_group = optarg; |
fac6795d DG |
3815 | break; |
3816 | case 'h': | |
3817 | usage(); | |
3818 | exit(EXIT_FAILURE); | |
3819 | case 'V': | |
3820 | fprintf(stdout, "%s\n", VERSION); | |
3821 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
3822 | case 'S': |
3823 | opt_sig_parent = 1; | |
3824 | break; | |
d6f42150 | 3825 | case 'E': |
3bd1e081 | 3826 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
3827 | break; |
3828 | case 'C': | |
3bd1e081 MD |
3829 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
3830 | break; | |
3831 | case 'F': | |
7753dea8 | 3832 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
3bd1e081 MD |
3833 | break; |
3834 | case 'D': | |
7753dea8 MD |
3835 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
3836 | break; | |
3837 | case 'H': | |
3838 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
3839 | break; | |
3840 | case 'G': | |
3841 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 3842 | break; |
4fba7219 DG |
3843 | case 'N': |
3844 | opt_no_kernel = 1; | |
3845 | break; | |
75462a81 DG |
3846 | case 'q': |
3847 | opt_quiet = 1; | |
3848 | break; | |
3f9947db | 3849 | case 'v': |
53086306 DG |
3850 | /* Verbose level can increase using multiple -v */ |
3851 | opt_verbose += 1; | |
3f9947db | 3852 | break; |
31f73cc9 | 3853 | case 'Z': |
3bd1e081 | 3854 | opt_verbose_consumer += 1; |
31f73cc9 | 3855 | break; |
fb09408a | 3856 | case 'u': |
fc7a59ce | 3857 | consumerd32_bin= optarg; |
ebaeda94 MD |
3858 | break; |
3859 | case 'U': | |
3860 | consumerd32_libdir = optarg; | |
7753dea8 MD |
3861 | break; |
3862 | case 't': | |
fc7a59ce | 3863 | consumerd64_bin = optarg; |
ebaeda94 MD |
3864 | break; |
3865 | case 'T': | |
3866 | consumerd64_libdir = optarg; | |
fb09408a | 3867 | break; |
fac6795d DG |
3868 | default: |
3869 | /* Unknown option or other error. | |
3870 | * Error is printed by getopt, just return */ | |
3871 | return -1; | |
3872 | } | |
3873 | } | |
3874 | ||
3875 | return 0; | |
3876 | } | |
3877 | ||
3878 | /* | |
d063d709 | 3879 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
3880 | * apps_sock - The communication socket for all UST apps. |
3881 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 3882 | */ |
cf3af59e | 3883 | static int init_daemon_socket(void) |
fac6795d DG |
3884 | { |
3885 | int ret = 0; | |
3886 | mode_t old_umask; | |
3887 | ||
3888 | old_umask = umask(0); | |
3889 | ||
3890 | /* Create client tool unix socket */ | |
d6f42150 DG |
3891 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
3892 | if (client_sock < 0) { | |
3893 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
3894 | ret = -1; |
3895 | goto end; | |
3896 | } | |
3897 | ||
3898 | /* File permission MUST be 660 */ | |
3899 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3900 | if (ret < 0) { | |
d6f42150 | 3901 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
3902 | perror("chmod"); |
3903 | goto end; | |
3904 | } | |
3905 | ||
3906 | /* Create the application unix socket */ | |
d6f42150 DG |
3907 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
3908 | if (apps_sock < 0) { | |
3909 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
3910 | ret = -1; |
3911 | goto end; | |
3912 | } | |
3913 | ||
d6f42150 | 3914 | /* File permission MUST be 666 */ |
54d01ffb DG |
3915 | ret = chmod(apps_unix_sock_path, |
3916 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 3917 | if (ret < 0) { |
d6f42150 | 3918 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
3919 | perror("chmod"); |
3920 | goto end; | |
3921 | } | |
3922 | ||
3923 | end: | |
3924 | umask(old_umask); | |
3925 | return ret; | |
3926 | } | |
3927 | ||
3928 | /* | |
54d01ffb DG |
3929 | * Check if the global socket is available, and if a daemon is answering at the |
3930 | * other side. If yes, error is returned. | |
fac6795d | 3931 | */ |
cf3af59e | 3932 | static int check_existing_daemon(void) |
fac6795d | 3933 | { |
7d8234d9 | 3934 | if (access(client_unix_sock_path, F_OK) < 0 && |
099e26bd | 3935 | access(apps_unix_sock_path, F_OK) < 0) { |
7d8234d9 | 3936 | return 0; |
099e26bd | 3937 | } |
54d01ffb | 3938 | |
7d8234d9 | 3939 | /* Is there anybody out there ? */ |
099e26bd | 3940 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 3941 | return -EEXIST; |
099e26bd | 3942 | } else { |
7d8234d9 | 3943 | return 0; |
099e26bd | 3944 | } |
fac6795d DG |
3945 | } |
3946 | ||
fac6795d | 3947 | /* |
d063d709 | 3948 | * Set the tracing group gid onto the client socket. |
5e16da05 | 3949 | * |
d063d709 DG |
3950 | * Race window between mkdir and chown is OK because we are going from more |
3951 | * permissive (root.root) to les permissive (root.tracing). | |
fac6795d | 3952 | */ |
be040666 | 3953 | static int set_permissions(char *rundir) |
fac6795d DG |
3954 | { |
3955 | int ret; | |
996b65c8 | 3956 | gid_t gid; |
fac6795d | 3957 | |
996b65c8 MD |
3958 | gid = allowed_group(); |
3959 | if (gid < 0) { | |
be040666 DG |
3960 | WARN("No tracing group detected"); |
3961 | ret = 0; | |
fac6795d DG |
3962 | goto end; |
3963 | } | |
3964 | ||
d6f42150 | 3965 | /* Set lttng run dir */ |
be040666 | 3966 | ret = chown(rundir, 0, gid); |
d6f42150 | 3967 | if (ret < 0) { |
be040666 | 3968 | ERR("Unable to set group on %s", rundir); |
d6f42150 DG |
3969 | perror("chown"); |
3970 | } | |
3971 | ||
3972 | /* lttng client socket path */ | |
996b65c8 | 3973 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 3974 | if (ret < 0) { |
d6f42150 DG |
3975 | ERR("Unable to set group on %s", client_unix_sock_path); |
3976 | perror("chown"); | |
3977 | } | |
3978 | ||
3bd1e081 MD |
3979 | /* kconsumer error socket path */ |
3980 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); | |
d6f42150 | 3981 | if (ret < 0) { |
3bd1e081 MD |
3982 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
3983 | perror("chown"); | |
3984 | } | |
3985 | ||
7753dea8 MD |
3986 | /* 64-bit ustconsumer error socket path */ |
3987 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid); | |
3988 | if (ret < 0) { | |
3989 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
3990 | perror("chown"); | |
3991 | } | |
3992 | ||
3993 | /* 32-bit ustconsumer compat32 error socket path */ | |
3994 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid); | |
3bd1e081 | 3995 | if (ret < 0) { |
7753dea8 | 3996 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
fac6795d DG |
3997 | perror("chown"); |
3998 | } | |
3999 | ||
d6f42150 | 4000 | DBG("All permissions are set"); |
e07ae692 | 4001 | |
fac6795d DG |
4002 | end: |
4003 | return ret; | |
4004 | } | |
4005 | ||
7a485870 | 4006 | /* |
d063d709 | 4007 | * Create the pipe used to wake up the kernel thread. |
7a485870 DG |
4008 | */ |
4009 | static int create_kernel_poll_pipe(void) | |
4010 | { | |
4011 | return pipe2(kernel_poll_pipe, O_CLOEXEC); | |
4012 | } | |
4013 | ||
099e26bd DG |
4014 | /* |
4015 | * Create the application command pipe to wake thread_manage_apps. | |
4016 | */ | |
4017 | static int create_apps_cmd_pipe(void) | |
4018 | { | |
4019 | return pipe2(apps_cmd_pipe, O_CLOEXEC); | |
4020 | } | |
4021 | ||
d6f42150 | 4022 | /* |
d063d709 | 4023 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 4024 | */ |
67e40797 | 4025 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
4026 | { |
4027 | int ret; | |
4028 | ||
67e40797 DG |
4029 | DBG3("Creating LTTng run directory: %s", rundir); |
4030 | ||
4031 | ret = mkdir(rundir, S_IRWXU | S_IRWXG ); | |
d6f42150 | 4032 | if (ret < 0) { |
b1f11e69 | 4033 | if (errno != EEXIST) { |
67e40797 | 4034 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
4035 | goto error; |
4036 | } else { | |
4037 | ret = 0; | |
4038 | } | |
d6f42150 DG |
4039 | } |
4040 | ||
4041 | error: | |
4042 | return ret; | |
4043 | } | |
4044 | ||
4045 | /* | |
d063d709 DG |
4046 | * Setup sockets and directory needed by the kconsumerd communication with the |
4047 | * session daemon. | |
d6f42150 | 4048 | */ |
67e40797 DG |
4049 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
4050 | const char *rundir) | |
d6f42150 DG |
4051 | { |
4052 | int ret; | |
67e40797 | 4053 | char path[PATH_MAX]; |
d6f42150 | 4054 | |
67e40797 | 4055 | switch (consumer_data->type) { |
7753dea8 | 4056 | case LTTNG_CONSUMER_KERNEL: |
67e40797 | 4057 | snprintf(path, PATH_MAX, KCONSUMERD_PATH, rundir); |
7753dea8 MD |
4058 | break; |
4059 | case LTTNG_CONSUMER64_UST: | |
67e40797 | 4060 | snprintf(path, PATH_MAX, USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
4061 | break; |
4062 | case LTTNG_CONSUMER32_UST: | |
67e40797 | 4063 | snprintf(path, PATH_MAX, USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
4064 | break; |
4065 | default: | |
4066 | ERR("Consumer type unknown"); | |
4067 | ret = -EINVAL; | |
4068 | goto error; | |
d6f42150 DG |
4069 | } |
4070 | ||
67e40797 DG |
4071 | DBG2("Creating consumer directory: %s", path); |
4072 | ||
3bd1e081 | 4073 | ret = mkdir(path, S_IRWXU | S_IRWXG); |
d6f42150 | 4074 | if (ret < 0) { |
6beb2242 | 4075 | if (errno != EEXIST) { |
3bd1e081 | 4076 | ERR("Failed to create %s", path); |
6beb2242 DG |
4077 | goto error; |
4078 | } | |
4079 | ret = 0; | |
d6f42150 DG |
4080 | } |
4081 | ||
4082 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
4083 | consumer_data->err_sock = |
4084 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
4085 | if (consumer_data->err_sock < 0) { | |
4086 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
4087 | ret = -1; |
4088 | goto error; | |
4089 | } | |
4090 | ||
4091 | /* File permission MUST be 660 */ | |
3bd1e081 | 4092 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 4093 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 4094 | if (ret < 0) { |
3bd1e081 | 4095 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 4096 | PERROR("chmod"); |
d6f42150 DG |
4097 | goto error; |
4098 | } | |
4099 | ||
4100 | error: | |
4101 | return ret; | |
4102 | } | |
4103 | ||
fac6795d | 4104 | /* |
d063d709 | 4105 | * Signal handler for the daemon |
cf3af59e | 4106 | * |
54d01ffb DG |
4107 | * Simply stop all worker threads, leaving main() return gracefully after |
4108 | * joining all threads and calling cleanup(). | |
fac6795d DG |
4109 | */ |
4110 | static void sighandler(int sig) | |
4111 | { | |
4112 | switch (sig) { | |
cf3af59e MD |
4113 | case SIGPIPE: |
4114 | DBG("SIGPIPE catched"); | |
4115 | return; | |
4116 | case SIGINT: | |
4117 | DBG("SIGINT catched"); | |
4118 | stop_threads(); | |
4119 | break; | |
4120 | case SIGTERM: | |
4121 | DBG("SIGTERM catched"); | |
4122 | stop_threads(); | |
4123 | break; | |
4124 | default: | |
4125 | break; | |
fac6795d | 4126 | } |
fac6795d DG |
4127 | } |
4128 | ||
4129 | /* | |
d063d709 | 4130 | * Setup signal handler for : |
1d4b027a | 4131 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 4132 | */ |
1d4b027a | 4133 | static int set_signal_handler(void) |
fac6795d | 4134 | { |
1d4b027a DG |
4135 | int ret = 0; |
4136 | struct sigaction sa; | |
4137 | sigset_t sigset; | |
fac6795d | 4138 | |
1d4b027a DG |
4139 | if ((ret = sigemptyset(&sigset)) < 0) { |
4140 | perror("sigemptyset"); | |
4141 | return ret; | |
4142 | } | |
d6f42150 | 4143 | |
1d4b027a DG |
4144 | sa.sa_handler = sighandler; |
4145 | sa.sa_mask = sigset; | |
4146 | sa.sa_flags = 0; | |
4147 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
4148 | perror("sigaction"); | |
4149 | return ret; | |
d6f42150 DG |
4150 | } |
4151 | ||
1d4b027a DG |
4152 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
4153 | perror("sigaction"); | |
4154 | return ret; | |
d6f42150 | 4155 | } |
aaf26714 | 4156 | |
1d4b027a DG |
4157 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
4158 | perror("sigaction"); | |
4159 | return ret; | |
8c0faa1d DG |
4160 | } |
4161 | ||
1d4b027a DG |
4162 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
4163 | ||
4164 | return ret; | |
fac6795d DG |
4165 | } |
4166 | ||
f3ed775e | 4167 | /* |
d063d709 DG |
4168 | * Set open files limit to unlimited. This daemon can open a large number of |
4169 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
4170 | */ |
4171 | static void set_ulimit(void) | |
4172 | { | |
4173 | int ret; | |
4174 | struct rlimit lim; | |
4175 | ||
a88df331 | 4176 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
4177 | lim.rlim_cur = 65535; |
4178 | lim.rlim_max = 65535; | |
4179 | ||
4180 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
4181 | if (ret < 0) { | |
4182 | perror("failed to set open files limit"); | |
4183 | } | |
4184 | } | |
4185 | ||
fac6795d DG |
4186 | /* |
4187 | * main | |
4188 | */ | |
4189 | int main(int argc, char **argv) | |
4190 | { | |
fac6795d DG |
4191 | int ret = 0; |
4192 | void *status; | |
b082db07 | 4193 | const char *home_path; |
fac6795d | 4194 | |
f6a9efaa DG |
4195 | rcu_register_thread(); |
4196 | ||
273ea72c | 4197 | /* Create thread quit pipe */ |
cf3af59e MD |
4198 | if ((ret = init_thread_quit_pipe()) < 0) { |
4199 | goto error; | |
273ea72c DG |
4200 | } |
4201 | ||
7753dea8 | 4202 | setup_consumerd_path(); |
fb09408a | 4203 | |
fac6795d DG |
4204 | /* Parse arguments */ |
4205 | progname = argv[0]; | |
4206 | if ((ret = parse_args(argc, argv) < 0)) { | |
cf3af59e | 4207 | goto error; |
fac6795d DG |
4208 | } |
4209 | ||
4210 | /* Daemonize */ | |
4211 | if (opt_daemon) { | |
53094c05 DG |
4212 | ret = daemon(0, 0); |
4213 | if (ret < 0) { | |
4214 | perror("daemon"); | |
cf3af59e | 4215 | goto error; |
53094c05 | 4216 | } |
fac6795d DG |
4217 | } |
4218 | ||
4219 | /* Check if daemon is UID = 0 */ | |
4220 | is_root = !getuid(); | |
4221 | ||
fac6795d | 4222 | if (is_root) { |
67e40797 DG |
4223 | rundir = strdup(LTTNG_RUNDIR); |
4224 | ||
4225 | /* Create global run dir with root access */ | |
4226 | ret = create_lttng_rundir(rundir); | |
d6f42150 | 4227 | if (ret < 0) { |
cf3af59e | 4228 | goto error; |
d6f42150 DG |
4229 | } |
4230 | ||
fac6795d | 4231 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
4232 | snprintf(apps_unix_sock_path, PATH_MAX, |
4233 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
4234 | } |
4235 | ||
4236 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
4237 | snprintf(client_unix_sock_path, PATH_MAX, |
4238 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
4239 | } | |
0fdd1e2c DG |
4240 | |
4241 | /* Set global SHM for ust */ | |
4242 | if (strlen(wait_shm_path) == 0) { | |
4243 | snprintf(wait_shm_path, PATH_MAX, | |
4244 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
4245 | } | |
67e40797 DG |
4246 | |
4247 | /* Setup kernel consumerd path */ | |
4248 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, | |
4249 | KCONSUMERD_ERR_SOCK_PATH, rundir); | |
4250 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, | |
4251 | KCONSUMERD_CMD_SOCK_PATH, rundir); | |
4252 | ||
4253 | DBG2("Kernel consumer err path: %s", | |
4254 | kconsumer_data.err_unix_sock_path); | |
4255 | DBG2("Kernel consumer cmd path: %s", | |
4256 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 4257 | } else { |
b082db07 DG |
4258 | home_path = get_home_dir(); |
4259 | if (home_path == NULL) { | |
273ea72c DG |
4260 | /* TODO: Add --socket PATH option */ |
4261 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
4262 | ret = -EPERM; |
4263 | goto error; | |
b082db07 DG |
4264 | } |
4265 | ||
67e40797 DG |
4266 | /* |
4267 | * Create rundir from home path. This will create something like | |
4268 | * $HOME/.lttng | |
4269 | */ | |
4270 | ret = asprintf(&rundir, LTTNG_HOME_RUNDIR, home_path); | |
4271 | if (ret < 0) { | |
4272 | ret = -ENOMEM; | |
4273 | goto error; | |
4274 | } | |
4275 | ||
4276 | ret = create_lttng_rundir(rundir); | |
4277 | if (ret < 0) { | |
4278 | goto error; | |
4279 | } | |
4280 | ||
fac6795d | 4281 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 4282 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 4283 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
4284 | } |
4285 | ||
4286 | /* Set the cli tool unix socket path */ | |
4287 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 4288 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 4289 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 4290 | } |
0fdd1e2c DG |
4291 | |
4292 | /* Set global SHM for ust */ | |
4293 | if (strlen(wait_shm_path) == 0) { | |
4294 | snprintf(wait_shm_path, PATH_MAX, | |
4295 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid()); | |
4296 | } | |
fac6795d DG |
4297 | } |
4298 | ||
847177cd DG |
4299 | DBG("Client socket path %s", client_unix_sock_path); |
4300 | DBG("Application socket path %s", apps_unix_sock_path); | |
67e40797 DG |
4301 | DBG("LTTng run directory path: %s", rundir); |
4302 | ||
4303 | /* 32 bits consumerd path setup */ | |
4304 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, | |
4305 | USTCONSUMERD32_ERR_SOCK_PATH, rundir); | |
4306 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, | |
4307 | USTCONSUMERD32_CMD_SOCK_PATH, rundir); | |
4308 | ||
4309 | DBG2("UST consumer 32 bits err path: %s", | |
4310 | ustconsumer32_data.err_unix_sock_path); | |
4311 | DBG2("UST consumer 32 bits cmd path: %s", | |
4312 | ustconsumer32_data.cmd_unix_sock_path); | |
4313 | ||
4314 | /* 64 bits consumerd path setup */ | |
4315 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, | |
4316 | USTCONSUMERD64_ERR_SOCK_PATH, rundir); | |
4317 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, | |
4318 | USTCONSUMERD64_CMD_SOCK_PATH, rundir); | |
4319 | ||
4320 | DBG2("UST consumer 64 bits err path: %s", | |
4321 | ustconsumer64_data.err_unix_sock_path); | |
4322 | DBG2("UST consumer 64 bits cmd path: %s", | |
4323 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 4324 | |
273ea72c | 4325 | /* |
7d8234d9 | 4326 | * See if daemon already exist. |
fac6795d | 4327 | */ |
7d8234d9 | 4328 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 4329 | ERR("Already running daemon.\n"); |
273ea72c | 4330 | /* |
cf3af59e MD |
4331 | * We do not goto exit because we must not cleanup() |
4332 | * because a daemon is already running. | |
ab118b20 | 4333 | */ |
cf3af59e | 4334 | goto error; |
a88df331 DG |
4335 | } |
4336 | ||
54d01ffb | 4337 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
4338 | |
4339 | /* | |
4340 | * These actions must be executed as root. We do that *after* setting up | |
4341 | * the sockets path because we MUST make the check for another daemon using | |
4342 | * those paths *before* trying to set the kernel consumer sockets and init | |
4343 | * kernel tracer. | |
4344 | */ | |
4345 | if (is_root) { | |
67e40797 | 4346 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
7753dea8 MD |
4347 | if (ret < 0) { |
4348 | goto exit; | |
4349 | } | |
4350 | ||
a88df331 | 4351 | /* Setup kernel tracer */ |
4fba7219 DG |
4352 | if (!opt_no_kernel) { |
4353 | init_kernel_tracer(); | |
4354 | } | |
a88df331 DG |
4355 | |
4356 | /* Set ulimit for open files */ | |
4357 | set_ulimit(); | |
fac6795d DG |
4358 | } |
4359 | ||
67e40797 DG |
4360 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
4361 | if (ret < 0) { | |
4362 | goto exit; | |
4363 | } | |
4364 | ||
4365 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); | |
4366 | if (ret < 0) { | |
4367 | goto exit; | |
4368 | } | |
4369 | ||
cf3af59e MD |
4370 | if ((ret = set_signal_handler()) < 0) { |
4371 | goto exit; | |
fac6795d DG |
4372 | } |
4373 | ||
d6f42150 | 4374 | /* Setup the needed unix socket */ |
cf3af59e MD |
4375 | if ((ret = init_daemon_socket()) < 0) { |
4376 | goto exit; | |
fac6795d DG |
4377 | } |
4378 | ||
4379 | /* Set credentials to socket */ | |
be040666 | 4380 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
cf3af59e | 4381 | goto exit; |
fac6795d DG |
4382 | } |
4383 | ||
5b8719f5 DG |
4384 | /* Get parent pid if -S, --sig-parent is specified. */ |
4385 | if (opt_sig_parent) { | |
4386 | ppid = getppid(); | |
4387 | } | |
4388 | ||
7a485870 | 4389 | /* Setup the kernel pipe for waking up the kernel thread */ |
cf3af59e MD |
4390 | if ((ret = create_kernel_poll_pipe()) < 0) { |
4391 | goto exit; | |
7a485870 DG |
4392 | } |
4393 | ||
099e26bd DG |
4394 | /* Setup the thread apps communication pipe. */ |
4395 | if ((ret = create_apps_cmd_pipe()) < 0) { | |
4396 | goto exit; | |
4397 | } | |
4398 | ||
4399 | /* Init UST command queue. */ | |
4400 | cds_wfq_init(&ust_cmd_queue.queue); | |
4401 | ||
f6a9efaa DG |
4402 | /* Init UST app hash table */ |
4403 | ust_app_ht_alloc(); | |
4404 | ||
273ea72c | 4405 | /* |
54d01ffb DG |
4406 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
4407 | * statically declared in session.c | |
273ea72c | 4408 | */ |
54d01ffb | 4409 | session_list_ptr = session_get_list(); |
b5541356 | 4410 | |
5eb91c98 DG |
4411 | /* Set up max poll set size */ |
4412 | lttng_poll_set_max_size(); | |
4413 | ||
cf3af59e | 4414 | /* Create thread to manage the client socket */ |
099e26bd DG |
4415 | ret = pthread_create(&client_thread, NULL, |
4416 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 4417 | if (ret != 0) { |
099e26bd | 4418 | perror("pthread_create clients"); |
cf3af59e MD |
4419 | goto exit_client; |
4420 | } | |
fac6795d | 4421 | |
099e26bd DG |
4422 | /* Create thread to dispatch registration */ |
4423 | ret = pthread_create(&dispatch_thread, NULL, | |
4424 | thread_dispatch_ust_registration, (void *) NULL); | |
4425 | if (ret != 0) { | |
4426 | perror("pthread_create dispatch"); | |
4427 | goto exit_dispatch; | |
4428 | } | |
4429 | ||
4430 | /* Create thread to manage application registration. */ | |
4431 | ret = pthread_create(®_apps_thread, NULL, | |
4432 | thread_registration_apps, (void *) NULL); | |
4433 | if (ret != 0) { | |
4434 | perror("pthread_create registration"); | |
4435 | goto exit_reg_apps; | |
4436 | } | |
4437 | ||
cf3af59e | 4438 | /* Create thread to manage application socket */ |
54d01ffb DG |
4439 | ret = pthread_create(&apps_thread, NULL, |
4440 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 4441 | if (ret != 0) { |
099e26bd | 4442 | perror("pthread_create apps"); |
cf3af59e MD |
4443 | goto exit_apps; |
4444 | } | |
fac6795d | 4445 | |
cf3af59e | 4446 | /* Create kernel thread to manage kernel event */ |
54d01ffb DG |
4447 | ret = pthread_create(&kernel_thread, NULL, |
4448 | thread_manage_kernel, (void *) NULL); | |
cf3af59e | 4449 | if (ret != 0) { |
099e26bd | 4450 | perror("pthread_create kernel"); |
cf3af59e MD |
4451 | goto exit_kernel; |
4452 | } | |
7a485870 | 4453 | |
cf3af59e MD |
4454 | ret = pthread_join(kernel_thread, &status); |
4455 | if (ret != 0) { | |
4456 | perror("pthread_join"); | |
4457 | goto error; /* join error, exit without cleanup */ | |
fac6795d DG |
4458 | } |
4459 | ||
cf3af59e MD |
4460 | exit_kernel: |
4461 | ret = pthread_join(apps_thread, &status); | |
4462 | if (ret != 0) { | |
4463 | perror("pthread_join"); | |
4464 | goto error; /* join error, exit without cleanup */ | |
4465 | } | |
fac6795d | 4466 | |
cf3af59e | 4467 | exit_apps: |
099e26bd DG |
4468 | ret = pthread_join(reg_apps_thread, &status); |
4469 | if (ret != 0) { | |
4470 | perror("pthread_join"); | |
4471 | goto error; /* join error, exit without cleanup */ | |
4472 | } | |
4473 | ||
4474 | exit_reg_apps: | |
4475 | ret = pthread_join(dispatch_thread, &status); | |
4476 | if (ret != 0) { | |
4477 | perror("pthread_join"); | |
4478 | goto error; /* join error, exit without cleanup */ | |
4479 | } | |
4480 | ||
4481 | exit_dispatch: | |
cf3af59e MD |
4482 | ret = pthread_join(client_thread, &status); |
4483 | if (ret != 0) { | |
4484 | perror("pthread_join"); | |
4485 | goto error; /* join error, exit without cleanup */ | |
4486 | } | |
4487 | ||
3bd1e081 | 4488 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 4489 | if (ret != 0) { |
3bd1e081 | 4490 | perror("join_consumer"); |
cf3af59e MD |
4491 | goto error; /* join error, exit without cleanup */ |
4492 | } | |
a88df331 | 4493 | |
cf3af59e | 4494 | exit_client: |
a88df331 | 4495 | exit: |
cf3af59e MD |
4496 | /* |
4497 | * cleanup() is called when no other thread is running. | |
4498 | */ | |
f6a9efaa | 4499 | rcu_thread_online(); |
cf3af59e | 4500 | cleanup(); |
f6a9efaa DG |
4501 | rcu_thread_offline(); |
4502 | rcu_unregister_thread(); | |
67e40797 | 4503 | if (!ret) { |
cf3af59e | 4504 | exit(EXIT_SUCCESS); |
67e40797 | 4505 | } |
cf3af59e | 4506 | error: |
5e16da05 | 4507 | exit(EXIT_FAILURE); |
fac6795d | 4508 | } |