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