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