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