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