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