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