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