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