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