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