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> | |
35 | #include <unistd.h> | |
36 | ||
37 | #include <urcu/list.h> /* URCU list library (-lurcu) */ | |
38 | #include <ust/ustctl.h> /* UST control lib (-lust) */ | |
5b97ec60 | 39 | #include <lttng/lttng.h> |
fac6795d DG |
40 | |
41 | #include "liblttsessiondcomm.h" | |
42 | #include "ltt-sessiond.h" | |
75462a81 | 43 | #include "lttngerr.h" |
20fe2104 | 44 | #include "kernel-ctl.h" |
9e78d6ae | 45 | #include "ust-ctl.h" |
5b74c7b1 | 46 | #include "session.h" |
91d76f53 | 47 | #include "traceable-app.h" |
fac6795d | 48 | |
5e16da05 MD |
49 | /* |
50 | * TODO: | |
51 | * teardown: signal SIGTERM handler -> write into pipe. Threads waits | |
52 | * with epoll on pipe and on other pipes/sockets for commands. Main | |
53 | * simply waits on pthread join. | |
54 | */ | |
55 | ||
75462a81 | 56 | /* Const values */ |
686204ab | 57 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 58 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
59 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
60 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
61 | ||
fac6795d | 62 | /* Variables */ |
1d4b027a DG |
63 | int opt_verbose; /* Not static for lttngerr.h */ |
64 | int opt_quiet; /* Not static for lttngerr.h */ | |
fac6795d DG |
65 | const char *progname; |
66 | const char *opt_tracing_group; | |
5b8719f5 | 67 | static int opt_sig_parent; |
fac6795d DG |
68 | static int opt_daemon; |
69 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
1d4b027a DG |
70 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
71 | static pid_t kconsumerd_pid; | |
fac6795d | 72 | |
d6f42150 DG |
73 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
74 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
75 | static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */ | |
76 | static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */ | |
fac6795d | 77 | |
1d4b027a | 78 | /* Sockets and FDs */ |
d6f42150 DG |
79 | static int client_sock; |
80 | static int apps_sock; | |
81 | static int kconsumerd_err_sock; | |
8c0faa1d | 82 | static int kconsumerd_cmd_sock; |
20fe2104 | 83 | static int kernel_tracer_fd; |
1d4b027a DG |
84 | |
85 | /* Pthread, Mutexes and Semaphores */ | |
8c0faa1d | 86 | static pthread_t kconsumerd_thread; |
1d4b027a DG |
87 | static pthread_t apps_thread; |
88 | static pthread_t client_thread; | |
8c0faa1d DG |
89 | static sem_t kconsumerd_sem; |
90 | ||
1d4b027a | 91 | static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */ |
fac6795d | 92 | |
fac6795d | 93 | /* |
1d4b027a | 94 | * teardown_kernel_session |
fac6795d | 95 | * |
1d4b027a DG |
96 | * Complete teardown of a kernel session. This free all data structure |
97 | * related to a kernel session and update counter. | |
fac6795d | 98 | */ |
1d4b027a | 99 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 100 | { |
1d4b027a DG |
101 | if (session->kernel_session != NULL) { |
102 | DBG("Tearing down kernel session"); | |
c363b55d | 103 | trace_destroy_kernel_session(session->kernel_session); |
1d4b027a DG |
104 | /* Extra precaution */ |
105 | session->kernel_session = NULL; | |
106 | /* Decrement session count */ | |
107 | session->kern_session_count--; | |
fac6795d | 108 | } |
fac6795d DG |
109 | } |
110 | ||
111 | /* | |
1d4b027a | 112 | * cleanup |
fac6795d | 113 | * |
1d4b027a | 114 | * Cleanup the daemon on exit |
fac6795d | 115 | */ |
1d4b027a | 116 | static void cleanup() |
fac6795d | 117 | { |
1d4b027a DG |
118 | int ret; |
119 | char *cmd; | |
120 | struct ltt_session *sess; | |
fac6795d | 121 | |
1d4b027a | 122 | DBG("Cleaning up"); |
e07ae692 | 123 | |
1d4b027a DG |
124 | /* <fun> */ |
125 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); | |
126 | MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); | |
127 | /* </fun> */ | |
fac6795d | 128 | |
1d4b027a DG |
129 | /* Stopping all threads */ |
130 | DBG("Terminating all threads"); | |
131 | pthread_cancel(client_thread); | |
132 | pthread_cancel(apps_thread); | |
133 | if (kconsumerd_pid != 0) { | |
134 | pthread_cancel(kconsumerd_thread); | |
5b8719f5 DG |
135 | } |
136 | ||
1d4b027a DG |
137 | DBG("Unlinking all unix socket"); |
138 | unlink(client_unix_sock_path); | |
139 | unlink(apps_unix_sock_path); | |
140 | unlink(kconsumerd_err_unix_sock_path); | |
fac6795d | 141 | |
1d4b027a DG |
142 | DBG("Removing %s directory", LTTNG_RUNDIR); |
143 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); | |
144 | if (ret < 0) { | |
145 | ERR("asprintf failed. Something is really wrong!"); | |
146 | } | |
5461b305 | 147 | |
1d4b027a DG |
148 | /* Remove lttng run directory */ |
149 | ret = system(cmd); | |
150 | if (ret < 0) { | |
151 | ERR("Unable to clean " LTTNG_RUNDIR); | |
152 | } | |
5461b305 | 153 | |
1d4b027a DG |
154 | DBG("Cleaning up all session"); |
155 | /* Cleanup ALL session */ | |
156 | cds_list_for_each_entry(sess, <t_session_list.head, list) { | |
157 | teardown_kernel_session(sess); | |
158 | // TODO complete session cleanup (including UST) | |
fac6795d DG |
159 | } |
160 | ||
1d4b027a | 161 | close(kernel_tracer_fd); |
fac6795d DG |
162 | } |
163 | ||
e065084a DG |
164 | /* |
165 | * send_unix_sock | |
166 | * | |
167 | * Send data on a unix socket using the liblttsessiondcomm API. | |
168 | * | |
169 | * Return lttcomm error code. | |
170 | */ | |
171 | static int send_unix_sock(int sock, void *buf, size_t len) | |
172 | { | |
173 | /* Check valid length */ | |
174 | if (len <= 0) { | |
175 | return -1; | |
176 | } | |
177 | ||
178 | return lttcomm_send_unix_sock(sock, buf, len); | |
179 | } | |
180 | ||
5461b305 DG |
181 | /* |
182 | * clean_command_ctx | |
183 | * | |
184 | * Free memory of a command context structure. | |
185 | */ | |
186 | static void clean_command_ctx(struct command_ctx *cmd_ctx) | |
187 | { | |
188 | DBG("Clean command context structure %p", cmd_ctx); | |
189 | if (cmd_ctx) { | |
190 | if (cmd_ctx->llm) { | |
191 | free(cmd_ctx->llm); | |
192 | } | |
193 | if (cmd_ctx->lsm) { | |
194 | free(cmd_ctx->lsm); | |
195 | } | |
196 | free(cmd_ctx); | |
197 | cmd_ctx = NULL; | |
198 | } | |
199 | } | |
200 | ||
fac6795d | 201 | /* |
471d1693 | 202 | * ust_connect_app |
fac6795d DG |
203 | * |
204 | * Return a socket connected to the libust communication socket | |
205 | * of the application identified by the pid. | |
379473d2 DG |
206 | * |
207 | * If the pid is not found in the traceable list, | |
208 | * return -1 to indicate error. | |
fac6795d | 209 | */ |
471d1693 | 210 | static int ust_connect_app(pid_t pid) |
fac6795d | 211 | { |
91d76f53 DG |
212 | int sock; |
213 | struct ltt_traceable_app *lta; | |
379473d2 | 214 | |
e07ae692 DG |
215 | DBG("Connect to application pid %d", pid); |
216 | ||
91d76f53 DG |
217 | lta = find_app_by_pid(pid); |
218 | if (lta == NULL) { | |
219 | /* App not found */ | |
7442b2ba | 220 | DBG("Application pid %d not found", pid); |
379473d2 DG |
221 | return -1; |
222 | } | |
fac6795d | 223 | |
91d76f53 | 224 | sock = ustctl_connect_pid(lta->pid); |
fac6795d | 225 | if (sock < 0) { |
75462a81 | 226 | ERR("Fail connecting to the PID %d\n", pid); |
fac6795d DG |
227 | } |
228 | ||
229 | return sock; | |
230 | } | |
231 | ||
232 | /* | |
233 | * notify_apps | |
234 | * | |
235 | * Notify apps by writing 42 to a named pipe using name. | |
236 | * Every applications waiting for a ltt-sessiond will be notified | |
237 | * and re-register automatically to the session daemon. | |
238 | * | |
239 | * Return open or write error value. | |
240 | */ | |
241 | static int notify_apps(const char *name) | |
242 | { | |
243 | int fd; | |
244 | int ret = -1; | |
245 | ||
e07ae692 DG |
246 | DBG("Notify the global application pipe"); |
247 | ||
fac6795d DG |
248 | /* Try opening the global pipe */ |
249 | fd = open(name, O_WRONLY); | |
250 | if (fd < 0) { | |
251 | goto error; | |
252 | } | |
253 | ||
254 | /* Notify by writing on the pipe */ | |
255 | ret = write(fd, "42", 2); | |
256 | if (ret < 0) { | |
257 | perror("write"); | |
258 | } | |
259 | ||
260 | error: | |
261 | return ret; | |
262 | } | |
263 | ||
e065084a | 264 | /* |
5461b305 | 265 | * setup_lttng_msg |
ca95a216 | 266 | * |
5461b305 DG |
267 | * Setup the outgoing data buffer for the response (llm) by allocating the |
268 | * right amount of memory and copying the original information from the lsm | |
269 | * structure. | |
ca95a216 DG |
270 | * |
271 | * Return total size of the buffer pointed by buf. | |
272 | */ | |
5461b305 | 273 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 274 | { |
5461b305 | 275 | int ret, buf_size, trace_name_size; |
ca95a216 | 276 | |
5461b305 DG |
277 | /* |
278 | * Check for the trace_name. If defined, it's part of the payload data of | |
279 | * the llm structure. | |
280 | */ | |
281 | trace_name_size = strlen(cmd_ctx->lsm->trace_name); | |
282 | buf_size = trace_name_size + size; | |
283 | ||
284 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); | |
285 | if (cmd_ctx->llm == NULL) { | |
ca95a216 | 286 | perror("malloc"); |
5461b305 | 287 | ret = -ENOMEM; |
ca95a216 DG |
288 | goto error; |
289 | } | |
290 | ||
5461b305 DG |
291 | /* Copy common data */ |
292 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
293 | cmd_ctx->llm->pid = cmd_ctx->lsm->pid; | |
294 | if (!uuid_is_null(cmd_ctx->lsm->session_uuid)) { | |
295 | uuid_copy(cmd_ctx->llm->session_uuid, cmd_ctx->lsm->session_uuid); | |
296 | } | |
297 | ||
298 | cmd_ctx->llm->trace_name_offset = trace_name_size; | |
299 | cmd_ctx->llm->data_size = size; | |
300 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
301 | ||
302 | /* Copy trace name to the llm structure. Begining of the payload. */ | |
303 | memcpy(cmd_ctx->llm->payload, cmd_ctx->lsm->trace_name, trace_name_size); | |
ca95a216 DG |
304 | |
305 | return buf_size; | |
306 | ||
307 | error: | |
308 | return ret; | |
309 | } | |
310 | ||
1d4b027a DG |
311 | /* |
312 | * thread_manage_kconsumerd | |
313 | * | |
314 | * This thread manage the kconsumerd error sent | |
315 | * back to the session daemon. | |
316 | */ | |
317 | static void *thread_manage_kconsumerd(void *data) | |
318 | { | |
319 | int sock, ret; | |
320 | enum lttcomm_return_code code; | |
321 | ||
322 | DBG("[thread] Manage kconsumerd started"); | |
323 | ||
324 | ret = lttcomm_listen_unix_sock(kconsumerd_err_sock); | |
325 | if (ret < 0) { | |
326 | goto error; | |
327 | } | |
328 | ||
329 | sock = lttcomm_accept_unix_sock(kconsumerd_err_sock); | |
330 | if (sock < 0) { | |
331 | goto error; | |
332 | } | |
333 | ||
334 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); | |
335 | if (ret <= 0) { | |
336 | goto error; | |
337 | } | |
338 | ||
339 | if (code == KCONSUMERD_COMMAND_SOCK_READY) { | |
340 | kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path); | |
341 | if (kconsumerd_cmd_sock < 0) { | |
342 | perror("kconsumerd connect"); | |
343 | goto error; | |
344 | } | |
345 | /* Signal condition to tell that the kconsumerd is ready */ | |
346 | sem_post(&kconsumerd_sem); | |
347 | DBG("Kconsumerd command socket ready"); | |
348 | } else { | |
6f61d021 | 349 | DBG("Kconsumerd error when waiting for SOCK_READY : %s", |
1d4b027a DG |
350 | lttcomm_get_readable_code(-code)); |
351 | goto error; | |
352 | } | |
353 | ||
6f61d021 DG |
354 | while (1) { |
355 | /* Wait for any kconsumerd error */ | |
356 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); | |
357 | if (ret <= 0) { | |
358 | ERR("Kconsumerd closed the command socket"); | |
359 | goto error; | |
360 | } | |
1d4b027a | 361 | |
6f61d021 DG |
362 | ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code)); |
363 | if (code != KCONSUMERD_POLL_HUP) { | |
364 | goto error; | |
365 | } | |
366 | } | |
1d4b027a DG |
367 | |
368 | error: | |
369 | kconsumerd_pid = 0; | |
6f61d021 | 370 | DBG("Kconsumerd thread dying"); |
1d4b027a DG |
371 | return NULL; |
372 | } | |
373 | ||
374 | /* | |
375 | * thread_manage_apps | |
376 | * | |
377 | * This thread manage the application socket communication | |
378 | */ | |
379 | static void *thread_manage_apps(void *data) | |
380 | { | |
381 | int sock, ret; | |
382 | ||
383 | /* TODO: Something more elegant is needed but fine for now */ | |
384 | /* FIXME: change all types to either uint8_t, uint32_t, uint64_t | |
385 | * for 32-bit vs 64-bit compat processes. */ | |
386 | /* replicate in ust with version number */ | |
387 | struct { | |
388 | int reg; /* 1:register, 0:unregister */ | |
389 | pid_t pid; | |
390 | uid_t uid; | |
391 | } reg_msg; | |
392 | ||
393 | DBG("[thread] Manage apps started"); | |
394 | ||
395 | ret = lttcomm_listen_unix_sock(apps_sock); | |
396 | if (ret < 0) { | |
397 | goto error; | |
398 | } | |
399 | ||
400 | /* Notify all applications to register */ | |
401 | notify_apps(default_global_apps_pipe); | |
402 | ||
403 | while (1) { | |
404 | DBG("Accepting application registration"); | |
405 | /* Blocking call, waiting for transmission */ | |
406 | sock = lttcomm_accept_unix_sock(apps_sock); | |
407 | if (sock < 0) { | |
408 | goto error; | |
409 | } | |
410 | ||
411 | /* Basic recv here to handle the very simple data | |
412 | * that the libust send to register (reg_msg). | |
413 | */ | |
414 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); | |
415 | if (ret < 0) { | |
416 | perror("recv"); | |
417 | continue; | |
418 | } | |
419 | ||
420 | /* Add application to the global traceable list */ | |
421 | if (reg_msg.reg == 1) { | |
422 | /* Registering */ | |
423 | ret = register_traceable_app(reg_msg.pid, reg_msg.uid); | |
424 | if (ret < 0) { | |
425 | /* register_traceable_app only return an error with | |
426 | * ENOMEM. At this point, we better stop everything. | |
427 | */ | |
428 | goto error; | |
429 | } | |
430 | } else { | |
431 | /* Unregistering */ | |
432 | unregister_traceable_app(reg_msg.pid); | |
433 | } | |
434 | } | |
435 | ||
436 | error: | |
437 | ||
438 | return NULL; | |
439 | } | |
440 | ||
8c0faa1d | 441 | /* |
693bd40b | 442 | * spawn_kconsumerd_thread |
8c0faa1d DG |
443 | * |
444 | * Start the thread_manage_kconsumerd. This must be done after a kconsumerd | |
445 | * exec or it will fails. | |
446 | */ | |
693bd40b | 447 | static int spawn_kconsumerd_thread(void) |
8c0faa1d DG |
448 | { |
449 | int ret; | |
450 | ||
451 | /* Setup semaphore */ | |
452 | sem_init(&kconsumerd_sem, 0, 0); | |
453 | ||
1d4b027a | 454 | ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL); |
8c0faa1d DG |
455 | if (ret != 0) { |
456 | perror("pthread_create kconsumerd"); | |
457 | goto error; | |
458 | } | |
459 | ||
693bd40b | 460 | /* Wait for the kconsumerd thread to be ready */ |
8c0faa1d DG |
461 | sem_wait(&kconsumerd_sem); |
462 | ||
463 | return 0; | |
464 | ||
465 | error: | |
466 | return ret; | |
467 | } | |
468 | ||
469 | /* | |
693bd40b | 470 | * spawn_kconsumerd |
8c0faa1d | 471 | * |
693bd40b DG |
472 | * Fork and exec a kernel consumer daemon (kconsumerd). |
473 | * | |
474 | * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel | |
475 | * file descriptor using the libkernelctl or kernel-ctl functions. So, a | |
476 | * kernel consumer MUST only be spawned before creating a kernel session. | |
8c0faa1d DG |
477 | * |
478 | * Return pid if successful else -1. | |
479 | */ | |
693bd40b | 480 | static pid_t spawn_kconsumerd(void) |
8c0faa1d DG |
481 | { |
482 | int ret; | |
483 | pid_t pid; | |
484 | ||
485 | pid = fork(); | |
486 | if (pid == 0) { | |
487 | /* | |
488 | * Exec kconsumerd. | |
489 | */ | |
490 | execlp("kconsumerd", "kconsumerd", "--daemonize", NULL); | |
491 | if (errno != 0) { | |
492 | perror("kernel start consumer exec"); | |
493 | } | |
494 | exit(EXIT_FAILURE); | |
495 | } else if (pid > 0) { | |
496 | ret = pid; | |
497 | goto error; | |
498 | } else { | |
499 | perror("kernel start consumer fork"); | |
500 | ret = -errno; | |
501 | goto error; | |
502 | } | |
503 | ||
504 | error: | |
505 | return ret; | |
506 | } | |
507 | ||
693bd40b DG |
508 | /* |
509 | * start_kconsumerd | |
510 | * | |
511 | * Spawn the kconsumerd daemon and session daemon thread. | |
512 | */ | |
513 | static int start_kconsumerd(void) | |
514 | { | |
515 | int ret; | |
516 | ||
517 | DBG("Spawning kconsumerd"); | |
518 | ||
519 | pthread_mutex_lock(&kconsumerd_pid_mutex); | |
520 | if (kconsumerd_pid == 0) { | |
521 | ret = spawn_kconsumerd(); | |
522 | if (ret < 0) { | |
523 | ERR("Spawning kconsumerd failed"); | |
524 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
525 | pthread_mutex_unlock(&kconsumerd_pid_mutex); | |
526 | goto error; | |
527 | } | |
528 | ||
529 | /* Setting up the global kconsumerd_pid */ | |
530 | kconsumerd_pid = ret; | |
531 | } | |
532 | pthread_mutex_unlock(&kconsumerd_pid_mutex); | |
533 | ||
6f61d021 DG |
534 | DBG("Kconsumerd pid %d", ret); |
535 | ||
693bd40b DG |
536 | DBG("Spawning kconsumerd thread"); |
537 | ||
538 | ret = spawn_kconsumerd_thread(); | |
539 | if (ret < 0) { | |
540 | ERR("Fatal error spawning kconsumerd thread"); | |
541 | ret = LTTCOMM_FATAL; | |
542 | goto error; | |
543 | } | |
544 | ||
545 | return 0; | |
546 | ||
547 | error: | |
548 | return ret; | |
549 | } | |
550 | ||
8c0faa1d DG |
551 | /* |
552 | * send_kconsumerd_fds | |
553 | * | |
554 | * Send all stream fds of the kernel session to the consumer. | |
555 | */ | |
556 | static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session) | |
557 | { | |
558 | int ret, i = 0; | |
559 | /* Plus one here for the metadata fd */ | |
560 | size_t nb_fd = session->stream_count_global + 1; | |
561 | int fds[nb_fd]; | |
562 | struct ltt_kernel_stream *stream; | |
563 | struct ltt_kernel_channel *chan; | |
564 | struct lttcomm_kconsumerd_header lkh; | |
565 | struct lttcomm_kconsumerd_msg buf[nb_fd]; | |
566 | ||
567 | /* Add metadata data */ | |
568 | fds[i] = session->metadata_stream_fd; | |
569 | buf[i].fd = fds[i]; | |
570 | buf[i].state = ACTIVE_FD; | |
571 | buf[i].max_sb_size = session->metadata->conf->subbuf_size; | |
572 | strncpy(buf[i].path_name, session->metadata->pathname, PATH_MAX); | |
573 | ||
574 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
575 | cds_list_for_each_entry(stream, &chan->stream_list.head, list) { | |
576 | i++; | |
577 | fds[i] = stream->fd; | |
578 | buf[i].fd = stream->fd; | |
579 | buf[i].state = stream->state; | |
580 | buf[i].max_sb_size = chan->channel->subbuf_size; | |
581 | strncpy(buf[i].path_name, stream->pathname, PATH_MAX); | |
582 | } | |
583 | } | |
584 | ||
585 | /* Setup header */ | |
586 | lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg); | |
587 | lkh.cmd_type = LTTCOMM_ADD_STREAM; | |
588 | ||
589 | DBG("Sending kconsumerd header"); | |
1d4b027a DG |
590 | |
591 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); | |
592 | if (ret < 0) { | |
593 | perror("send kconsumerd header"); | |
594 | goto error; | |
8c0faa1d DG |
595 | } |
596 | ||
1d4b027a | 597 | DBG("Sending all fds to kconsumerd"); |
8c0faa1d | 598 | |
1d4b027a DG |
599 | ret = lttcomm_send_fds_unix_sock(sock, buf, fds, nb_fd, lkh.payload_size); |
600 | if (ret < 0) { | |
601 | perror("send kconsumerd fds"); | |
602 | goto error; | |
603 | } | |
604 | ||
605 | DBG("Kconsumerd fds sent"); | |
606 | ||
607 | return 0; | |
608 | ||
609 | error: | |
610 | return ret; | |
8c0faa1d DG |
611 | } |
612 | ||
613 | /* | |
1d4b027a | 614 | * create_trace_dir |
8c0faa1d | 615 | * |
1d4b027a | 616 | * Create the trace output directory. |
8c0faa1d | 617 | */ |
1d4b027a | 618 | static int create_trace_dir(struct ltt_kernel_session *session) |
8c0faa1d | 619 | { |
1d4b027a DG |
620 | int ret; |
621 | struct ltt_kernel_channel *chan; | |
622 | ||
623 | /* Create all channel directories */ | |
624 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
1b6083d9 | 625 | DBG("Creating trace directory at %s", chan->pathname); |
1d4b027a DG |
626 | ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG ); |
627 | if (ret < 0) { | |
628 | perror("mkdir trace path"); | |
629 | ret = -errno; | |
630 | goto error; | |
631 | } | |
8c0faa1d | 632 | } |
1d4b027a DG |
633 | |
634 | return 0; | |
635 | ||
636 | error: | |
637 | return ret; | |
8c0faa1d DG |
638 | } |
639 | ||
fac6795d DG |
640 | /* |
641 | * process_client_msg | |
642 | * | |
5461b305 DG |
643 | * Process the command requested by the lttng client within the command |
644 | * context structure. This function make sure that the return structure (llm) | |
645 | * is set and ready for transmission before returning. | |
fac6795d | 646 | * |
e065084a | 647 | * Return any error encountered or 0 for success. |
fac6795d | 648 | */ |
5461b305 | 649 | static int process_client_msg(struct command_ctx *cmd_ctx) |
fac6795d | 650 | { |
5461b305 | 651 | int ret; |
fac6795d | 652 | |
5461b305 | 653 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
fac6795d | 654 | |
379473d2 | 655 | /* Check command that needs a session */ |
5461b305 | 656 | switch (cmd_ctx->lsm->cmd_type) { |
5e16da05 MD |
657 | case LTTNG_CREATE_SESSION: |
658 | case LTTNG_LIST_SESSIONS: | |
659 | case UST_LIST_APPS: | |
660 | break; | |
661 | default: | |
5461b305 DG |
662 | cmd_ctx->session = find_session_by_uuid(cmd_ctx->lsm->session_uuid); |
663 | if (cmd_ctx->session == NULL) { | |
379473d2 | 664 | ret = LTTCOMM_SELECT_SESS; |
5461b305 | 665 | goto error; |
379473d2 | 666 | } |
5e16da05 | 667 | break; |
379473d2 DG |
668 | } |
669 | ||
20fe2104 DG |
670 | /* Check command for kernel tracing */ |
671 | switch (cmd_ctx->lsm->cmd_type) { | |
672 | case KERNEL_CREATE_SESSION: | |
673 | case KERNEL_CREATE_CHANNEL: | |
8c0faa1d | 674 | case KERNEL_CREATE_STREAM: |
aaf26714 DG |
675 | case KERNEL_DISABLE_EVENT: |
676 | case KERNEL_ENABLE_EVENT: | |
677 | case KERNEL_OPEN_METADATA: | |
678 | case KERNEL_START_TRACE: | |
679 | case KERNEL_STOP_TRACE: | |
680 | /* TODO: reconnect to kernel tracer to check if | |
681 | * it's loadded */ | |
20fe2104 DG |
682 | if (kernel_tracer_fd == 0) { |
683 | ret = LTTCOMM_KERN_NA; | |
684 | goto error; | |
685 | } | |
686 | break; | |
687 | } | |
688 | ||
471d1693 | 689 | /* Connect to ust apps if available pid */ |
5461b305 | 690 | if (cmd_ctx->lsm->pid > 0) { |
471d1693 | 691 | /* Connect to app using ustctl API */ |
5461b305 DG |
692 | cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid); |
693 | if (cmd_ctx->ust_sock < 0) { | |
471d1693 | 694 | ret = LTTCOMM_NO_TRACEABLE; |
5461b305 | 695 | goto error; |
471d1693 DG |
696 | } |
697 | } | |
698 | ||
fac6795d | 699 | /* Process by command type */ |
5461b305 | 700 | switch (cmd_ctx->lsm->cmd_type) { |
20fe2104 DG |
701 | case KERNEL_CREATE_SESSION: |
702 | { | |
703 | ret = setup_lttng_msg(cmd_ctx, 0); | |
704 | if (ret < 0) { | |
705 | goto setup_error; | |
706 | } | |
707 | ||
693bd40b | 708 | ret = start_kconsumerd(); |
8c0faa1d | 709 | if (ret < 0) { |
8c0faa1d DG |
710 | goto error; |
711 | } | |
712 | ||
20fe2104 DG |
713 | DBG("Creating kernel session"); |
714 | ||
8c0faa1d | 715 | ret = kernel_create_session(cmd_ctx->session, kernel_tracer_fd); |
20fe2104 DG |
716 | if (ret < 0) { |
717 | ret = LTTCOMM_KERN_SESS_FAIL; | |
718 | goto error; | |
719 | } | |
720 | ||
721 | ret = LTTCOMM_OK; | |
722 | break; | |
723 | } | |
724 | case KERNEL_CREATE_CHANNEL: | |
725 | { | |
726 | ret = setup_lttng_msg(cmd_ctx, 0); | |
727 | if (ret < 0) { | |
728 | goto setup_error; | |
729 | } | |
730 | ||
8c0faa1d DG |
731 | DBG("Creating kernel channel"); |
732 | ||
733 | ret = kernel_create_channel(cmd_ctx->session->kernel_session); | |
20fe2104 | 734 | |
20fe2104 DG |
735 | if (ret < 0) { |
736 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
737 | goto error; | |
738 | } | |
739 | ||
740 | ret = LTTCOMM_OK; | |
741 | break; | |
742 | } | |
894be886 DG |
743 | case KERNEL_ENABLE_EVENT: |
744 | { | |
745 | /* Setup lttng message with no payload */ | |
746 | ret = setup_lttng_msg(cmd_ctx, 0); | |
747 | if (ret < 0) { | |
748 | goto setup_error; | |
749 | } | |
750 | ||
751 | DBG("Enabling kernel event %s", cmd_ctx->lsm->u.event.event_name); | |
752 | ||
8c0faa1d | 753 | ret = kernel_enable_event(cmd_ctx->session->kernel_session, cmd_ctx->lsm->u.event.event_name); |
f34daff7 DG |
754 | if (ret < 0) { |
755 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
756 | goto error; | |
757 | } | |
758 | ||
894be886 DG |
759 | ret = LTTCOMM_OK; |
760 | break; | |
761 | } | |
aaf26714 DG |
762 | case KERNEL_OPEN_METADATA: |
763 | { | |
764 | /* Setup lttng message with no payload */ | |
765 | ret = setup_lttng_msg(cmd_ctx, 0); | |
766 | if (ret < 0) { | |
767 | goto setup_error; | |
768 | } | |
769 | ||
770 | DBG("Open kernel metadata"); | |
771 | ||
772 | ret = kernel_open_metadata(cmd_ctx->session->kernel_session); | |
773 | if (ret < 0) { | |
774 | ret = LTTCOMM_KERN_META_FAIL; | |
775 | goto error; | |
776 | } | |
777 | ||
778 | ret = LTTCOMM_OK; | |
779 | break; | |
780 | } | |
8c0faa1d DG |
781 | case KERNEL_CREATE_STREAM: |
782 | { | |
783 | struct ltt_kernel_channel *chan; | |
784 | /* Setup lttng message with no payload */ | |
785 | ret = setup_lttng_msg(cmd_ctx, 0); | |
786 | if (ret < 0) { | |
787 | goto setup_error; | |
788 | } | |
789 | ||
790 | DBG("Creating kernel stream"); | |
791 | ||
792 | ret = kernel_create_metadata_stream(cmd_ctx->session->kernel_session); | |
793 | if (ret < 0) { | |
794 | ERR("Kernel create metadata stream failed"); | |
795 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
796 | goto error; | |
797 | } | |
798 | ||
799 | /* For each channel */ | |
800 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
801 | ret = kernel_create_channel_stream(chan); | |
802 | if (ret < 0) { | |
803 | ERR("Kernel create channel stream failed"); | |
804 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
805 | goto error; | |
806 | } | |
807 | /* Update the stream global counter */ | |
808 | cmd_ctx->session->kernel_session->stream_count_global += ret; | |
809 | } | |
810 | ||
811 | ret = LTTCOMM_OK; | |
812 | break; | |
813 | } | |
814 | case KERNEL_START_TRACE: | |
815 | { | |
816 | /* Setup lttng message with no payload */ | |
817 | ret = setup_lttng_msg(cmd_ctx, 0); | |
818 | if (ret < 0) { | |
819 | goto setup_error; | |
820 | } | |
821 | ||
822 | DBG("Start kernel tracing"); | |
823 | ||
824 | ret = create_trace_dir(cmd_ctx->session->kernel_session); | |
825 | if (ret < 0) { | |
826 | if (ret == -EEXIST) { | |
827 | ret = LTTCOMM_KERN_DIR_EXIST; | |
828 | } else { | |
829 | ret = LTTCOMM_KERN_DIR_FAIL; | |
830 | goto error; | |
831 | } | |
832 | } | |
833 | ||
834 | ret = kernel_start_session(cmd_ctx->session->kernel_session); | |
835 | if (ret < 0) { | |
836 | ERR("Kernel start session failed"); | |
837 | ret = LTTCOMM_KERN_START_FAIL; | |
838 | goto error; | |
839 | } | |
840 | ||
841 | ret = send_kconsumerd_fds(kconsumerd_cmd_sock, cmd_ctx->session->kernel_session); | |
842 | if (ret < 0) { | |
843 | ERR("Send kconsumerd fds failed"); | |
844 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
845 | goto error; | |
846 | } | |
847 | ||
848 | ret = LTTCOMM_OK; | |
849 | break; | |
850 | } | |
851 | case KERNEL_STOP_TRACE: | |
852 | { | |
853 | /* Setup lttng message with no payload */ | |
854 | ret = setup_lttng_msg(cmd_ctx, 0); | |
855 | if (ret < 0) { | |
856 | goto setup_error; | |
857 | } | |
858 | ||
84291629 DG |
859 | if (cmd_ctx->session->kernel_session == NULL) { |
860 | ret = LTTCOMM_KERN_NO_SESSION; | |
861 | goto error; | |
862 | } | |
863 | ||
8c0faa1d DG |
864 | DBG("Stop kernel tracing"); |
865 | ||
866 | ret = kernel_stop_session(cmd_ctx->session->kernel_session); | |
867 | if (ret < 0) { | |
868 | ERR("Kernel stop session failed"); | |
869 | ret = LTTCOMM_KERN_STOP_FAIL; | |
870 | goto error; | |
871 | } | |
872 | ||
873 | /* Clean kernel session teardown */ | |
874 | teardown_kernel_session(cmd_ctx->session); | |
875 | ||
876 | ret = LTTCOMM_OK; | |
877 | break; | |
878 | } | |
5e16da05 MD |
879 | case LTTNG_CREATE_SESSION: |
880 | { | |
5461b305 DG |
881 | /* Setup lttng message with no payload */ |
882 | ret = setup_lttng_msg(cmd_ctx, 0); | |
883 | if (ret < 0) { | |
884 | goto setup_error; | |
885 | } | |
886 | ||
887 | ret = create_session(cmd_ctx->lsm->session_name, &cmd_ctx->llm->session_uuid); | |
5e16da05 MD |
888 | if (ret < 0) { |
889 | if (ret == -1) { | |
890 | ret = LTTCOMM_EXIST_SESS; | |
891 | } else { | |
aaf97519 | 892 | ret = LTTCOMM_FATAL; |
aaf97519 | 893 | } |
5461b305 | 894 | goto error; |
8028d920 | 895 | } |
1657e9bb | 896 | |
5461b305 | 897 | ret = LTTCOMM_OK; |
5e16da05 MD |
898 | break; |
899 | } | |
900 | case LTTNG_DESTROY_SESSION: | |
901 | { | |
5461b305 DG |
902 | /* Setup lttng message with no payload */ |
903 | ret = setup_lttng_msg(cmd_ctx, 0); | |
904 | if (ret < 0) { | |
905 | goto setup_error; | |
906 | } | |
907 | ||
908 | ret = destroy_session(&cmd_ctx->lsm->session_uuid); | |
5e16da05 MD |
909 | if (ret < 0) { |
910 | ret = LTTCOMM_NO_SESS; | |
5461b305 | 911 | goto error; |
5e16da05 | 912 | } |
1657e9bb | 913 | |
5461b305 DG |
914 | ret = LTTCOMM_OK; |
915 | break; | |
5e16da05 MD |
916 | } |
917 | case LTTNG_LIST_TRACES: | |
918 | { | |
5461b305 | 919 | unsigned int trace_count; |
1657e9bb | 920 | |
5461b305 | 921 | trace_count = get_trace_count_per_session(cmd_ctx->session); |
5e16da05 MD |
922 | if (trace_count == 0) { |
923 | ret = LTTCOMM_NO_TRACE; | |
5461b305 | 924 | goto error; |
1657e9bb | 925 | } |
df0da139 | 926 | |
5461b305 DG |
927 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count); |
928 | if (ret < 0) { | |
929 | goto setup_error; | |
df0da139 | 930 | } |
ca95a216 | 931 | |
5461b305 DG |
932 | get_traces_per_session(cmd_ctx->session, |
933 | (struct lttng_trace *)(cmd_ctx->llm->payload)); | |
934 | ||
935 | ret = LTTCOMM_OK; | |
5e16da05 MD |
936 | break; |
937 | } | |
938 | case UST_CREATE_TRACE: | |
939 | { | |
5461b305 DG |
940 | /* Setup lttng message with no payload */ |
941 | ret = setup_lttng_msg(cmd_ctx, 0); | |
5e16da05 | 942 | if (ret < 0) { |
5461b305 | 943 | goto setup_error; |
fac6795d | 944 | } |
ce3d728c | 945 | |
5461b305 DG |
946 | ret = ust_create_trace(cmd_ctx); |
947 | if (ret < 0) { | |
948 | goto setup_error; | |
949 | } | |
950 | break; | |
5e16da05 MD |
951 | } |
952 | case UST_LIST_APPS: | |
953 | { | |
5461b305 DG |
954 | unsigned int app_count; |
955 | ||
956 | app_count = get_app_count(); | |
957 | DBG("Traceable application count : %d", app_count); | |
5e16da05 MD |
958 | if (app_count == 0) { |
959 | ret = LTTCOMM_NO_APPS; | |
5461b305 | 960 | goto error; |
ce3d728c | 961 | } |
520ff687 | 962 | |
5461b305 DG |
963 | ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count); |
964 | if (ret < 0) { | |
965 | goto setup_error; | |
520ff687 | 966 | } |
57167058 | 967 | |
5461b305 | 968 | get_app_list_pids((pid_t *)(cmd_ctx->llm->payload)); |
5e16da05 | 969 | |
5461b305 | 970 | ret = LTTCOMM_OK; |
5e16da05 MD |
971 | break; |
972 | } | |
973 | case UST_START_TRACE: | |
974 | { | |
5461b305 DG |
975 | /* Setup lttng message with no payload */ |
976 | ret = setup_lttng_msg(cmd_ctx, 0); | |
977 | if (ret < 0) { | |
978 | goto setup_error; | |
979 | } | |
ca95a216 | 980 | |
5461b305 DG |
981 | ret = ust_start_trace(cmd_ctx); |
982 | if (ret < 0) { | |
983 | goto setup_error; | |
984 | } | |
985 | break; | |
5e16da05 MD |
986 | } |
987 | case UST_STOP_TRACE: | |
988 | { | |
5461b305 DG |
989 | /* Setup lttng message with no payload */ |
990 | ret = setup_lttng_msg(cmd_ctx, 0); | |
991 | if (ret < 0) { | |
992 | goto setup_error; | |
993 | } | |
ca95a216 | 994 | |
5461b305 DG |
995 | ret = ust_stop_trace(cmd_ctx); |
996 | if (ret < 0) { | |
997 | goto setup_error; | |
998 | } | |
999 | break; | |
5e16da05 MD |
1000 | } |
1001 | case LTTNG_LIST_SESSIONS: | |
1002 | { | |
5461b305 DG |
1003 | unsigned int session_count; |
1004 | ||
1005 | session_count = get_session_count(); | |
5e16da05 MD |
1006 | if (session_count == 0) { |
1007 | ret = LTTCOMM_NO_SESS; | |
5461b305 | 1008 | goto error; |
57167058 | 1009 | } |
5e16da05 | 1010 | |
5461b305 DG |
1011 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count); |
1012 | if (ret < 0) { | |
1013 | goto setup_error; | |
e065084a | 1014 | } |
5e16da05 | 1015 | |
5461b305 | 1016 | get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload)); |
5e16da05 | 1017 | |
5461b305 | 1018 | ret = LTTCOMM_OK; |
5e16da05 MD |
1019 | break; |
1020 | } | |
1021 | default: | |
1022 | /* Undefined command */ | |
5461b305 DG |
1023 | ret = setup_lttng_msg(cmd_ctx, 0); |
1024 | if (ret < 0) { | |
1025 | goto setup_error; | |
1026 | } | |
1027 | ||
5e16da05 | 1028 | ret = LTTCOMM_UND; |
5461b305 | 1029 | break; |
fac6795d DG |
1030 | } |
1031 | ||
5461b305 DG |
1032 | /* Set return code */ |
1033 | cmd_ctx->llm->ret_code = ret; | |
ca95a216 | 1034 | |
e065084a DG |
1035 | return ret; |
1036 | ||
5461b305 | 1037 | error: |
5461b305 DG |
1038 | if (cmd_ctx->llm == NULL) { |
1039 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 1040 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
1041 | goto setup_error; |
1042 | } | |
1043 | } | |
e065084a | 1044 | /* Notify client of error */ |
5461b305 | 1045 | cmd_ctx->llm->ret_code = ret; |
e065084a | 1046 | |
5461b305 | 1047 | setup_error: |
8028d920 | 1048 | return ret; |
fac6795d DG |
1049 | } |
1050 | ||
1d4b027a DG |
1051 | /* |
1052 | * thread_manage_clients | |
1053 | * | |
1054 | * This thread manage all clients request using the unix | |
1055 | * client socket for communication. | |
1056 | */ | |
1057 | static void *thread_manage_clients(void *data) | |
1058 | { | |
1059 | int sock, ret; | |
1060 | struct command_ctx *cmd_ctx; | |
1061 | ||
1062 | DBG("[thread] Manage client started"); | |
1063 | ||
1064 | ret = lttcomm_listen_unix_sock(client_sock); | |
1065 | if (ret < 0) { | |
1066 | goto error; | |
1067 | } | |
1068 | ||
1069 | /* Notify parent pid that we are ready | |
1070 | * to accept command for client side. | |
1071 | */ | |
1072 | if (opt_sig_parent) { | |
1073 | kill(ppid, SIGCHLD); | |
1074 | } | |
1075 | ||
1076 | while (1) { | |
1077 | /* Blocking call, waiting for transmission */ | |
1078 | DBG("Accepting client command ..."); | |
1079 | sock = lttcomm_accept_unix_sock(client_sock); | |
1080 | if (sock < 0) { | |
1081 | goto error; | |
1082 | } | |
1083 | ||
1084 | /* Allocate context command to process the client request */ | |
1085 | cmd_ctx = malloc(sizeof(struct command_ctx)); | |
1086 | ||
1087 | /* Allocate data buffer for reception */ | |
1088 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); | |
1089 | cmd_ctx->llm = NULL; | |
1090 | cmd_ctx->session = NULL; | |
1091 | ||
1092 | /* | |
1093 | * Data is received from the lttng client. The struct | |
1094 | * lttcomm_session_msg (lsm) contains the command and data request of | |
1095 | * the client. | |
1096 | */ | |
1097 | DBG("Receiving data from client ..."); | |
1098 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg)); | |
1099 | if (ret <= 0) { | |
1100 | continue; | |
1101 | } | |
1102 | ||
1103 | /* | |
1104 | * This function dispatch the work to the kernel or userspace tracer | |
1105 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
1106 | * informations for the client. The command context struct contains | |
1107 | * everything this function may needs. | |
1108 | */ | |
1109 | ret = process_client_msg(cmd_ctx); | |
1110 | if (ret < 0) { | |
1111 | /* TODO: Inform client somehow of the fatal error. At this point, | |
1112 | * ret < 0 means that a malloc failed (ENOMEM). */ | |
1113 | /* Error detected but still accept command */ | |
1114 | clean_command_ctx(cmd_ctx); | |
1115 | continue; | |
1116 | } | |
1117 | ||
1118 | DBG("Sending response (size: %d, retcode: %d)", | |
1119 | cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code); | |
1120 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); | |
1121 | if (ret < 0) { | |
1122 | ERR("Failed to send data back to client"); | |
1123 | } | |
1124 | ||
1125 | clean_command_ctx(cmd_ctx); | |
1126 | } | |
1127 | ||
1128 | error: | |
1129 | return NULL; | |
1130 | } | |
1131 | ||
1132 | ||
fac6795d DG |
1133 | /* |
1134 | * usage function on stderr | |
1135 | */ | |
1136 | static void usage(void) | |
1137 | { | |
b716ce68 | 1138 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
1139 | fprintf(stderr, " -h, --help Display this usage.\n"); |
1140 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
1141 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
1142 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
1143 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
1144 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
1145 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
1146 | fprintf(stderr, " -V, --version Show version number.\n"); | |
1147 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
1148 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
1149 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
fac6795d DG |
1150 | } |
1151 | ||
1152 | /* | |
1153 | * daemon argument parsing | |
1154 | */ | |
1155 | static int parse_args(int argc, char **argv) | |
1156 | { | |
1157 | int c; | |
1158 | ||
1159 | static struct option long_options[] = { | |
1160 | { "client-sock", 1, 0, 'c' }, | |
1161 | { "apps-sock", 1, 0, 'a' }, | |
d6f42150 DG |
1162 | { "kconsumerd-cmd-sock", 1, 0, 0 }, |
1163 | { "kconsumerd-err-sock", 1, 0, 0 }, | |
fac6795d | 1164 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 1165 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
1166 | { "help", 0, 0, 'h' }, |
1167 | { "group", 1, 0, 'g' }, | |
1168 | { "version", 0, 0, 'V' }, | |
75462a81 | 1169 | { "quiet", 0, 0, 'q' }, |
3f9947db | 1170 | { "verbose", 0, 0, 'v' }, |
fac6795d DG |
1171 | { NULL, 0, 0, 0 } |
1172 | }; | |
1173 | ||
1174 | while (1) { | |
1175 | int option_index = 0; | |
d6f42150 | 1176 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index); |
fac6795d DG |
1177 | if (c == -1) { |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | switch (c) { | |
1182 | case 0: | |
1183 | fprintf(stderr, "option %s", long_options[option_index].name); | |
1184 | if (optarg) { | |
1185 | fprintf(stderr, " with arg %s\n", optarg); | |
1186 | } | |
1187 | break; | |
b716ce68 | 1188 | case 'c': |
fac6795d DG |
1189 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
1190 | break; | |
1191 | case 'a': | |
1192 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
1193 | break; | |
1194 | case 'd': | |
1195 | opt_daemon = 1; | |
1196 | break; | |
1197 | case 'g': | |
1198 | opt_tracing_group = strdup(optarg); | |
1199 | break; | |
1200 | case 'h': | |
1201 | usage(); | |
1202 | exit(EXIT_FAILURE); | |
1203 | case 'V': | |
1204 | fprintf(stdout, "%s\n", VERSION); | |
1205 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
1206 | case 'S': |
1207 | opt_sig_parent = 1; | |
1208 | break; | |
d6f42150 DG |
1209 | case 'E': |
1210 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg); | |
1211 | break; | |
1212 | case 'C': | |
1213 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
1214 | break; | |
75462a81 DG |
1215 | case 'q': |
1216 | opt_quiet = 1; | |
1217 | break; | |
3f9947db DG |
1218 | case 'v': |
1219 | opt_verbose = 1; | |
1220 | break; | |
fac6795d DG |
1221 | default: |
1222 | /* Unknown option or other error. | |
1223 | * Error is printed by getopt, just return */ | |
1224 | return -1; | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | /* | |
1232 | * init_daemon_socket | |
1233 | * | |
1234 | * Creates the two needed socket by the daemon. | |
d6f42150 DG |
1235 | * apps_sock - The communication socket for all UST apps. |
1236 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d DG |
1237 | */ |
1238 | static int init_daemon_socket() | |
1239 | { | |
1240 | int ret = 0; | |
1241 | mode_t old_umask; | |
1242 | ||
1243 | old_umask = umask(0); | |
1244 | ||
1245 | /* Create client tool unix socket */ | |
d6f42150 DG |
1246 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
1247 | if (client_sock < 0) { | |
1248 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
1249 | ret = -1; |
1250 | goto end; | |
1251 | } | |
1252 | ||
1253 | /* File permission MUST be 660 */ | |
1254 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1255 | if (ret < 0) { | |
d6f42150 | 1256 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
1257 | perror("chmod"); |
1258 | goto end; | |
1259 | } | |
1260 | ||
1261 | /* Create the application unix socket */ | |
d6f42150 DG |
1262 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
1263 | if (apps_sock < 0) { | |
1264 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
1265 | ret = -1; |
1266 | goto end; | |
1267 | } | |
1268 | ||
d6f42150 | 1269 | /* File permission MUST be 666 */ |
fac6795d DG |
1270 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
1271 | if (ret < 0) { | |
d6f42150 | 1272 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
1273 | perror("chmod"); |
1274 | goto end; | |
1275 | } | |
1276 | ||
1277 | end: | |
1278 | umask(old_umask); | |
1279 | return ret; | |
1280 | } | |
1281 | ||
1282 | /* | |
1283 | * check_existing_daemon | |
1284 | * | |
1285 | * Check if the global socket is available. | |
62bd06d8 | 1286 | * If yes, error is returned. |
fac6795d DG |
1287 | */ |
1288 | static int check_existing_daemon() | |
1289 | { | |
1290 | int ret; | |
1291 | ||
1292 | ret = access(client_unix_sock_path, F_OK); | |
1293 | if (ret == 0) { | |
1294 | ret = access(apps_unix_sock_path, F_OK); | |
1295 | } | |
1296 | ||
1297 | return ret; | |
1298 | } | |
1299 | ||
1300 | /* | |
62bd06d8 | 1301 | * get_home_dir |
fac6795d | 1302 | * |
62bd06d8 DG |
1303 | * Return pointer to home directory path using |
1304 | * the env variable HOME. | |
fac6795d | 1305 | * |
62bd06d8 | 1306 | * Default : /tmp |
fac6795d DG |
1307 | */ |
1308 | static const char *get_home_dir(void) | |
1309 | { | |
1310 | const char *home_path; | |
1311 | ||
686204ab | 1312 | if ((home_path = (const char *) getenv("HOME")) == NULL) { |
fac6795d DG |
1313 | home_path = default_home_dir; |
1314 | } | |
1315 | ||
1316 | return home_path; | |
1317 | } | |
1318 | ||
1319 | /* | |
d6f42150 | 1320 | * set_permissions |
fac6795d | 1321 | * |
5e16da05 MD |
1322 | * Set the tracing group gid onto the client socket. |
1323 | * | |
1324 | * Race window between mkdir and chown is OK because we are going from | |
1325 | * less permissive (root.root) to more permissive (root.tracing). | |
fac6795d | 1326 | */ |
d6f42150 | 1327 | static int set_permissions(void) |
fac6795d DG |
1328 | { |
1329 | int ret; | |
1330 | struct group *grp; | |
1331 | ||
1332 | /* Decide which group name to use */ | |
1333 | (opt_tracing_group != NULL) ? | |
1334 | (grp = getgrnam(opt_tracing_group)) : | |
1335 | (grp = getgrnam(default_tracing_group)); | |
1336 | ||
1337 | if (grp == NULL) { | |
75462a81 | 1338 | ERR("Missing tracing group. Aborting execution.\n"); |
fac6795d DG |
1339 | ret = -1; |
1340 | goto end; | |
1341 | } | |
1342 | ||
d6f42150 DG |
1343 | /* Set lttng run dir */ |
1344 | ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); | |
1345 | if (ret < 0) { | |
1346 | ERR("Unable to set group on " LTTNG_RUNDIR); | |
1347 | perror("chown"); | |
1348 | } | |
1349 | ||
1350 | /* lttng client socket path */ | |
fac6795d DG |
1351 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
1352 | if (ret < 0) { | |
d6f42150 DG |
1353 | ERR("Unable to set group on %s", client_unix_sock_path); |
1354 | perror("chown"); | |
1355 | } | |
1356 | ||
1357 | /* kconsumerd error socket path */ | |
1358 | ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); | |
1359 | if (ret < 0) { | |
1360 | ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); | |
fac6795d DG |
1361 | perror("chown"); |
1362 | } | |
1363 | ||
d6f42150 | 1364 | DBG("All permissions are set"); |
e07ae692 | 1365 | |
fac6795d DG |
1366 | end: |
1367 | return ret; | |
1368 | } | |
1369 | ||
d6f42150 DG |
1370 | /* |
1371 | * create_lttng_rundir | |
1372 | * | |
1373 | * Create the lttng run directory needed for all | |
1374 | * global sockets and pipe. | |
1375 | */ | |
1376 | static int create_lttng_rundir(void) | |
1377 | { | |
1378 | int ret; | |
1379 | ||
1380 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); | |
1381 | if (ret < 0) { | |
1382 | ERR("Unable to create " LTTNG_RUNDIR); | |
1383 | goto error; | |
1384 | } | |
1385 | ||
1386 | error: | |
1387 | return ret; | |
1388 | } | |
1389 | ||
20fe2104 DG |
1390 | /* |
1391 | * init_kernel_tracer | |
1392 | * | |
1393 | * Setup necessary data for kernel tracer action. | |
1394 | */ | |
1395 | static void init_kernel_tracer(void) | |
1396 | { | |
1397 | /* Set the global kernel tracer fd */ | |
1398 | kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR); | |
1399 | if (kernel_tracer_fd < 0) { | |
1400 | WARN("No kernel tracer available"); | |
1401 | kernel_tracer_fd = 0; | |
1402 | } | |
8c0faa1d DG |
1403 | |
1404 | DBG("Kernel tracer fd %d", kernel_tracer_fd); | |
20fe2104 DG |
1405 | } |
1406 | ||
d6f42150 DG |
1407 | /* |
1408 | * set_kconsumerd_sockets | |
1409 | * | |
1410 | * Setup sockets and directory needed by the kconsumerd | |
1411 | * communication with the session daemon. | |
1412 | */ | |
1413 | static int set_kconsumerd_sockets(void) | |
1414 | { | |
1415 | int ret; | |
1416 | ||
1417 | if (strlen(kconsumerd_err_unix_sock_path) == 0) { | |
1418 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH); | |
1419 | } | |
1420 | ||
1421 | if (strlen(kconsumerd_cmd_unix_sock_path) == 0) { | |
1422 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH); | |
1423 | } | |
1424 | ||
1425 | ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG); | |
1426 | if (ret < 0) { | |
1427 | ERR("Failed to create " KCONSUMERD_PATH); | |
1428 | goto error; | |
1429 | } | |
1430 | ||
1431 | /* Create the kconsumerd error unix socket */ | |
1432 | kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path); | |
1433 | if (kconsumerd_err_sock < 0) { | |
1434 | ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path); | |
1435 | ret = -1; | |
1436 | goto error; | |
1437 | } | |
1438 | ||
1439 | /* File permission MUST be 660 */ | |
1440 | ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1441 | if (ret < 0) { | |
1442 | ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path); | |
1443 | perror("chmod"); | |
1444 | goto error; | |
1445 | } | |
1446 | ||
1447 | error: | |
1448 | return ret; | |
1449 | } | |
1450 | ||
fac6795d | 1451 | /* |
62bd06d8 | 1452 | * sighandler |
fac6795d | 1453 | * |
62bd06d8 | 1454 | * Signal handler for the daemon |
fac6795d DG |
1455 | */ |
1456 | static void sighandler(int sig) | |
1457 | { | |
1458 | switch (sig) { | |
1459 | case SIGPIPE: | |
e07ae692 | 1460 | DBG("SIGPIPE catched"); |
87378cf5 | 1461 | return; |
fac6795d | 1462 | case SIGINT: |
e07ae692 DG |
1463 | DBG("SIGINT catched"); |
1464 | cleanup(); | |
1465 | break; | |
fac6795d | 1466 | case SIGTERM: |
e07ae692 | 1467 | DBG("SIGTERM catched"); |
fac6795d | 1468 | cleanup(); |
686204ab | 1469 | break; |
fac6795d DG |
1470 | default: |
1471 | break; | |
1472 | } | |
1473 | ||
1474 | exit(EXIT_SUCCESS); | |
1475 | } | |
1476 | ||
1477 | /* | |
1d4b027a | 1478 | * set_signal_handler |
fac6795d | 1479 | * |
1d4b027a DG |
1480 | * Setup signal handler for : |
1481 | * SIGINT, SIGTERM, SIGPIPE | |
fac6795d | 1482 | */ |
1d4b027a | 1483 | static int set_signal_handler(void) |
fac6795d | 1484 | { |
1d4b027a DG |
1485 | int ret = 0; |
1486 | struct sigaction sa; | |
1487 | sigset_t sigset; | |
fac6795d | 1488 | |
1d4b027a DG |
1489 | if ((ret = sigemptyset(&sigset)) < 0) { |
1490 | perror("sigemptyset"); | |
1491 | return ret; | |
1492 | } | |
d6f42150 | 1493 | |
1d4b027a DG |
1494 | sa.sa_handler = sighandler; |
1495 | sa.sa_mask = sigset; | |
1496 | sa.sa_flags = 0; | |
1497 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
1498 | perror("sigaction"); | |
1499 | return ret; | |
d6f42150 DG |
1500 | } |
1501 | ||
1d4b027a DG |
1502 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
1503 | perror("sigaction"); | |
1504 | return ret; | |
d6f42150 | 1505 | } |
aaf26714 | 1506 | |
1d4b027a DG |
1507 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
1508 | perror("sigaction"); | |
1509 | return ret; | |
8c0faa1d DG |
1510 | } |
1511 | ||
1d4b027a DG |
1512 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
1513 | ||
1514 | return ret; | |
fac6795d DG |
1515 | } |
1516 | ||
1517 | /* | |
1518 | * main | |
1519 | */ | |
1520 | int main(int argc, char **argv) | |
1521 | { | |
fac6795d DG |
1522 | int ret = 0; |
1523 | void *status; | |
fac6795d DG |
1524 | |
1525 | /* Parse arguments */ | |
1526 | progname = argv[0]; | |
1527 | if ((ret = parse_args(argc, argv) < 0)) { | |
1528 | goto error; | |
1529 | } | |
1530 | ||
1531 | /* Daemonize */ | |
1532 | if (opt_daemon) { | |
53094c05 DG |
1533 | ret = daemon(0, 0); |
1534 | if (ret < 0) { | |
1535 | perror("daemon"); | |
1536 | goto error; | |
1537 | } | |
fac6795d DG |
1538 | } |
1539 | ||
1540 | /* Check if daemon is UID = 0 */ | |
1541 | is_root = !getuid(); | |
1542 | ||
1543 | /* Set all sockets path */ | |
1544 | if (is_root) { | |
d6f42150 DG |
1545 | ret = create_lttng_rundir(); |
1546 | if (ret < 0) { | |
1547 | goto error; | |
1548 | } | |
1549 | ||
fac6795d | 1550 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
1551 | snprintf(apps_unix_sock_path, PATH_MAX, |
1552 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
1553 | } |
1554 | ||
1555 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
1556 | snprintf(client_unix_sock_path, PATH_MAX, |
1557 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
1558 | } | |
1559 | ||
1560 | ret = set_kconsumerd_sockets(); | |
1561 | if (ret < 0) { | |
1562 | goto error; | |
fac6795d | 1563 | } |
20fe2104 DG |
1564 | |
1565 | /* Setup kernel tracer */ | |
1566 | init_kernel_tracer(); | |
fac6795d DG |
1567 | } else { |
1568 | if (strlen(apps_unix_sock_path) == 0) { | |
d6f42150 DG |
1569 | snprintf(apps_unix_sock_path, PATH_MAX, |
1570 | DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir()); | |
fac6795d DG |
1571 | } |
1572 | ||
1573 | /* Set the cli tool unix socket path */ | |
1574 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
1575 | snprintf(client_unix_sock_path, PATH_MAX, |
1576 | DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir()); | |
fac6795d DG |
1577 | } |
1578 | } | |
1579 | ||
847177cd DG |
1580 | DBG("Client socket path %s", client_unix_sock_path); |
1581 | DBG("Application socket path %s", apps_unix_sock_path); | |
1582 | ||
fac6795d DG |
1583 | /* See if daemon already exist. If any of the two |
1584 | * socket needed by the daemon are present, this test fails | |
1585 | */ | |
1586 | if ((ret = check_existing_daemon()) == 0) { | |
75462a81 | 1587 | ERR("Already running daemon.\n"); |
ab118b20 | 1588 | /* We do not goto error because we must not |
d6f42150 | 1589 | * cleanup() because a daemon is already running. |
ab118b20 | 1590 | */ |
5e16da05 | 1591 | exit(EXIT_FAILURE); |
fac6795d DG |
1592 | } |
1593 | ||
1594 | if (set_signal_handler() < 0) { | |
1595 | goto error; | |
1596 | } | |
1597 | ||
d6f42150 | 1598 | /* Setup the needed unix socket */ |
fac6795d DG |
1599 | if (init_daemon_socket() < 0) { |
1600 | goto error; | |
1601 | } | |
1602 | ||
1603 | /* Set credentials to socket */ | |
d6f42150 | 1604 | if (is_root && (set_permissions() < 0)) { |
fac6795d DG |
1605 | goto error; |
1606 | } | |
1607 | ||
5b8719f5 DG |
1608 | /* Get parent pid if -S, --sig-parent is specified. */ |
1609 | if (opt_sig_parent) { | |
1610 | ppid = getppid(); | |
1611 | } | |
1612 | ||
fac6795d DG |
1613 | while (1) { |
1614 | /* Create thread to manage the client socket */ | |
1d4b027a | 1615 | ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); |
fac6795d DG |
1616 | if (ret != 0) { |
1617 | perror("pthread_create"); | |
1618 | goto error; | |
1619 | } | |
1620 | ||
1621 | /* Create thread to manage application socket */ | |
1d4b027a | 1622 | ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); |
fac6795d DG |
1623 | if (ret != 0) { |
1624 | perror("pthread_create"); | |
1625 | goto error; | |
1626 | } | |
1627 | ||
1d4b027a DG |
1628 | ret = pthread_join(client_thread, &status); |
1629 | if (ret != 0) { | |
1630 | perror("pthread_join"); | |
1631 | goto error; | |
fac6795d DG |
1632 | } |
1633 | } | |
1634 | ||
1635 | cleanup(); | |
5e16da05 | 1636 | exit(EXIT_SUCCESS); |
fac6795d DG |
1637 | |
1638 | error: | |
1639 | cleanup(); | |
5e16da05 | 1640 | exit(EXIT_FAILURE); |
fac6795d | 1641 | } |