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