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