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