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