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