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