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