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