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