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