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. | |
8 | * | |
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. | |
13 | * | |
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> | |
25 | #include <signal.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/ipc.h> | |
30 | #include <sys/shm.h> | |
31 | #include <sys/socket.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
34 | #include <unistd.h> | |
35 | ||
36 | #include <urcu/list.h> /* URCU list library (-lurcu) */ | |
37 | #include <ust/ustctl.h> /* UST control lib (-lust) */ | |
38 | ||
39 | #include "liblttsessiondcomm.h" | |
40 | #include "ltt-sessiond.h" | |
41 | ||
686204ab MD |
42 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
43 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; | |
44 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; | |
45 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
46 | ||
fac6795d DG |
47 | /* Static functions */ |
48 | static int set_signal_handler(void); | |
49 | static int set_socket_perms(void); | |
50 | static void sighandler(int); | |
51 | static void daemonize(void); | |
52 | static void cleanup(void); | |
e065084a | 53 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm); |
fac6795d DG |
54 | static int check_existing_daemon(void); |
55 | static int notify_apps(const char*); | |
56 | static int connect_app(pid_t); | |
57 | static int init_daemon_socket(void); | |
e065084a DG |
58 | static int process_client_msg(int sock, struct lttcomm_session_msg*); |
59 | static int send_unix_sock(int sock, void *buf, size_t len); | |
fac6795d DG |
60 | |
61 | static void *thread_manage_clients(void *); | |
62 | static void *thread_manage_apps(void *); | |
63 | ||
64 | static int create_session(const char*, uuid_t *); | |
65 | static void destroy_session(uuid_t); | |
66 | ||
67 | static struct ltt_session *find_session(uuid_t); | |
68 | ||
69 | /* Variables */ | |
70 | const char *progname; | |
71 | const char *opt_tracing_group; | |
5b8719f5 | 72 | static int opt_sig_parent; |
fac6795d DG |
73 | static int opt_daemon; |
74 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
5b8719f5 | 75 | static pid_t ppid; |
fac6795d DG |
76 | |
77 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ | |
78 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
79 | ||
80 | static int client_socket; | |
81 | static int apps_socket; | |
82 | ||
83 | static struct ltt_session *current_session; | |
e065084a DG |
84 | |
85 | /* Number of element for the list below. */ | |
86 | static unsigned int session_count; | |
87 | static unsigned int traceable_app_count; | |
fac6795d DG |
88 | |
89 | /* Init session's list */ | |
90 | static struct ltt_session_list ltt_session_list = { | |
91 | .head = CDS_LIST_HEAD_INIT(ltt_session_list.head), | |
92 | }; | |
93 | ||
e065084a | 94 | /* Init ust traceabl application's list */ |
fac6795d DG |
95 | static struct ltt_traceable_app_list ltt_traceable_app_list = { |
96 | .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), | |
97 | }; | |
98 | ||
99 | /* | |
100 | * thread_manage_apps | |
101 | * | |
102 | * This thread manage the application socket communication | |
103 | */ | |
104 | static void *thread_manage_apps(void *data) | |
105 | { | |
106 | int sock, ret; | |
107 | struct ltt_traceable_app *lta; | |
108 | ||
109 | /* TODO: Something more elegant is needed but fine for now */ | |
686204ab | 110 | struct { |
fac6795d | 111 | int reg; /* 1:register, 0:unregister */ |
686204ab MD |
112 | pid_t pid; |
113 | uid_t uid; | |
114 | } reg_msg; | |
fac6795d DG |
115 | |
116 | /* Notify all applications to register */ | |
117 | notify_apps(default_global_apps_pipe); | |
118 | ||
119 | ret = lttcomm_listen_unix_sock(apps_socket); | |
120 | if (ret < 0) { | |
121 | goto error; | |
122 | } | |
123 | ||
124 | while (1) { | |
125 | /* Blocking call, waiting for transmission */ | |
126 | sock = lttcomm_accept_unix_sock(apps_socket); | |
127 | if (sock < 0) { | |
128 | goto error; | |
129 | } | |
130 | ||
131 | /* Basic recv here to handle the very simple data | |
132 | * that the libust send to register (reg_msg). | |
133 | */ | |
134 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); | |
135 | if (ret < 0) { | |
136 | perror("recv"); | |
137 | continue; | |
138 | } | |
139 | ||
140 | /* Add application to the global traceable list */ | |
141 | if (reg_msg.reg == 1) { | |
142 | /* Registering */ | |
143 | lta = malloc(sizeof(struct ltt_traceable_app)); | |
144 | lta->pid = reg_msg.pid; | |
145 | lta->uid = reg_msg.uid; | |
146 | cds_list_add(<a->list, <t_traceable_app_list.head); | |
e065084a | 147 | traceable_app_count++; |
fac6795d DG |
148 | } else { |
149 | /* Unregistering */ | |
150 | lta = NULL; | |
151 | cds_list_for_each_entry(lta, <t_traceable_app_list.head, list) { | |
152 | if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) { | |
153 | cds_list_del(<a->list); | |
e065084a DG |
154 | /* Check to not overflow here */ |
155 | if (traceable_app_count != 0) { | |
156 | traceable_app_count--; | |
157 | } | |
fac6795d DG |
158 | break; |
159 | } | |
160 | } | |
161 | ||
162 | /* If an item was found, free it from memory */ | |
163 | if (lta) { | |
164 | free(lta); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | error: | |
170 | ||
171 | return NULL; | |
172 | } | |
173 | ||
174 | /* | |
175 | * thread_manage_clients | |
176 | * | |
177 | * This thread manage all clients request using the unix | |
178 | * client socket for communication. | |
179 | */ | |
180 | static void *thread_manage_clients(void *data) | |
181 | { | |
182 | int sock, ret; | |
183 | struct lttcomm_session_msg lsm; | |
fac6795d DG |
184 | |
185 | ret = lttcomm_listen_unix_sock(client_socket); | |
186 | if (ret < 0) { | |
187 | goto error; | |
188 | } | |
189 | ||
5b8719f5 DG |
190 | /* Notify parent pid that we are ready |
191 | * to accept command for client side. | |
192 | */ | |
193 | if (opt_sig_parent) { | |
194 | kill(ppid, SIGCHLD); | |
195 | } | |
196 | ||
fac6795d DG |
197 | while (1) { |
198 | /* Blocking call, waiting for transmission */ | |
199 | sock = lttcomm_accept_unix_sock(client_socket); | |
200 | if (sock < 0) { | |
201 | goto error; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Data is received from the lttng client. The struct | |
206 | * lttcomm_session_msg (lsm) contains the command and data | |
207 | * request of the client. | |
208 | */ | |
209 | ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm)); | |
210 | if (ret < 0) { | |
211 | continue; | |
212 | } | |
213 | ||
214 | /* This function dispatch the work to the LTTng or UST libs | |
e065084a DG |
215 | * and then sends back the response to the client. This is needed |
216 | * because there might be more then one lttcomm_lttng_msg to | |
217 | * send out so process_client_msg do both jobs. | |
fac6795d | 218 | */ |
e065084a DG |
219 | ret = process_client_msg(sock, &lsm); |
220 | if (ret < 0) { | |
221 | /* Error detected but still accept command */ | |
222 | continue; | |
fac6795d DG |
223 | } |
224 | } | |
225 | ||
226 | error: | |
227 | return NULL; | |
228 | } | |
229 | ||
e065084a DG |
230 | /* |
231 | * send_unix_sock | |
232 | * | |
233 | * Send data on a unix socket using the liblttsessiondcomm API. | |
234 | * | |
235 | * Return lttcomm error code. | |
236 | */ | |
237 | static int send_unix_sock(int sock, void *buf, size_t len) | |
238 | { | |
239 | /* Check valid length */ | |
240 | if (len <= 0) { | |
241 | return -1; | |
242 | } | |
243 | ||
244 | return lttcomm_send_unix_sock(sock, buf, len); | |
245 | } | |
246 | ||
fac6795d DG |
247 | /* |
248 | * connect_app | |
249 | * | |
250 | * Return a socket connected to the libust communication socket | |
251 | * of the application identified by the pid. | |
252 | */ | |
253 | static int connect_app(pid_t pid) | |
254 | { | |
255 | int sock; | |
256 | ||
257 | sock = ustctl_connect_pid(pid); | |
258 | if (sock < 0) { | |
259 | fprintf(stderr, "Fail connecting to the PID %d\n", pid); | |
260 | } | |
261 | ||
262 | return sock; | |
263 | } | |
264 | ||
265 | /* | |
266 | * notify_apps | |
267 | * | |
268 | * Notify apps by writing 42 to a named pipe using name. | |
269 | * Every applications waiting for a ltt-sessiond will be notified | |
270 | * and re-register automatically to the session daemon. | |
271 | * | |
272 | * Return open or write error value. | |
273 | */ | |
274 | static int notify_apps(const char *name) | |
275 | { | |
276 | int fd; | |
277 | int ret = -1; | |
278 | ||
279 | /* Try opening the global pipe */ | |
280 | fd = open(name, O_WRONLY); | |
281 | if (fd < 0) { | |
282 | goto error; | |
283 | } | |
284 | ||
285 | /* Notify by writing on the pipe */ | |
286 | ret = write(fd, "42", 2); | |
287 | if (ret < 0) { | |
288 | perror("write"); | |
289 | } | |
290 | ||
291 | error: | |
292 | return ret; | |
293 | } | |
294 | ||
295 | /* | |
296 | * find_session | |
297 | * | |
298 | * Return a ltt_session structure ptr that matches the uuid. | |
299 | */ | |
300 | static struct ltt_session *find_session(uuid_t session_id) | |
301 | { | |
302 | struct ltt_session *iter = NULL; | |
303 | ||
304 | /* Sanity check for NULL session_id */ | |
305 | if (uuid_is_null(session_id)) { | |
306 | goto end; | |
307 | } | |
308 | ||
309 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
310 | if (uuid_compare(iter->uuid, session_id)) { | |
311 | break; | |
312 | } | |
313 | } | |
314 | ||
315 | end: | |
316 | return iter; | |
317 | } | |
318 | ||
319 | /* | |
320 | * destroy_session | |
321 | * | |
322 | * Delete session from the global session list | |
323 | * and free the memory. | |
324 | */ | |
325 | static void destroy_session(uuid_t session_id) | |
326 | { | |
327 | struct ltt_session *iter = NULL; | |
328 | ||
329 | cds_list_for_each_entry(iter, <t_session_list.head, list) { | |
330 | if (uuid_compare(iter->uuid, session_id)) { | |
331 | cds_list_del(&iter->list); | |
332 | break; | |
333 | } | |
334 | } | |
335 | ||
336 | if (iter) { | |
337 | free(iter); | |
338 | session_count--; | |
339 | } | |
340 | } | |
341 | ||
342 | /* | |
343 | * create_session | |
344 | * | |
345 | * Create a brand new session, | |
346 | */ | |
347 | static int create_session(const char *name, uuid_t *session_id) | |
348 | { | |
349 | struct ltt_session *new_session; | |
350 | ||
351 | /* Allocate session data structure */ | |
352 | new_session = malloc(sizeof(struct ltt_session)); | |
353 | if (new_session == NULL) { | |
354 | perror("malloc"); | |
355 | goto error; | |
356 | } | |
357 | ||
358 | if (name != NULL) { | |
359 | if (asprintf(&new_session->name, "%s", name) < 0) { | |
360 | goto error; | |
361 | } | |
362 | } else { | |
363 | /* Generate session name based on the session count */ | |
364 | if (asprintf(&new_session->name, "%s%d", "auto", session_count) < 0) { | |
365 | goto error; | |
366 | } | |
367 | } | |
368 | ||
369 | /* UUID generation */ | |
370 | uuid_generate(new_session->uuid); | |
371 | uuid_copy(*session_id, new_session->uuid); | |
372 | ||
373 | /* Set consumer (identifier) to 0. This means that there is | |
374 | * NO consumer attach to that session yet. | |
375 | */ | |
376 | new_session->ust_consumer = 0; | |
377 | new_session->lttng_consumer = 0; | |
378 | ||
379 | /* Init list */ | |
380 | CDS_INIT_LIST_HEAD(&new_session->ust_traces); | |
381 | CDS_INIT_LIST_HEAD(&new_session->lttng_traces); | |
382 | ||
383 | /* Add new session to the global session list */ | |
384 | cds_list_add(&new_session->list, <t_session_list.head); | |
385 | ||
386 | session_count++; | |
387 | ||
388 | return 0; | |
389 | ||
390 | error: | |
391 | return -1; | |
392 | } | |
393 | ||
394 | /* | |
395 | * ust_list_apps | |
396 | * | |
397 | * List traceable user-space application and fill an | |
398 | * array of pids. | |
399 | * | |
400 | * Return size of the array. | |
401 | */ | |
e065084a | 402 | static size_t ust_list_apps(pid_t **pids) |
fac6795d DG |
403 | { |
404 | size_t size = 0; | |
405 | struct ltt_traceable_app *iter = NULL; | |
e065084a | 406 | pid_t *p; |
fac6795d | 407 | |
e065084a DG |
408 | if (traceable_app_count == 0) { |
409 | /* No dynamic allocation is done */ | |
410 | goto end; | |
411 | } | |
412 | ||
413 | p = malloc(sizeof(pid_t) * traceable_app_count); | |
fac6795d | 414 | |
e065084a DG |
415 | /* TODO: Mutex needed to access this list */ |
416 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { | |
417 | p[size] = iter->pid; | |
fac6795d DG |
418 | size++; |
419 | } | |
420 | ||
e065084a DG |
421 | *pids = p; |
422 | ||
423 | end: | |
fac6795d DG |
424 | return size; |
425 | } | |
426 | ||
e065084a DG |
427 | /* |
428 | * copy_common_data | |
429 | * | |
430 | * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg | |
431 | */ | |
432 | static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm) | |
433 | { | |
434 | llm->cmd_type = lsm->cmd_type; | |
435 | llm->pid = lsm->pid; | |
436 | if (!uuid_is_null(lsm->session_id)) { | |
437 | uuid_copy(llm->session_id, lsm->session_id); | |
438 | } | |
439 | strncpy(llm->trace_name, lsm->trace_name, sizeof(llm->trace_name)); | |
440 | } | |
441 | ||
fac6795d DG |
442 | /* |
443 | * process_client_msg | |
444 | * | |
445 | * This takes the lttcomm_session_msg struct and process the command requested | |
e065084a DG |
446 | * by the client. It then creates response(s) and send it back to the |
447 | * given socket (sock). | |
fac6795d | 448 | * |
e065084a | 449 | * Return any error encountered or 0 for success. |
fac6795d | 450 | */ |
e065084a | 451 | static int process_client_msg(int sock, struct lttcomm_session_msg *lsm) |
fac6795d | 452 | { |
e065084a DG |
453 | int ret; |
454 | struct lttcomm_lttng_msg llm; | |
fac6795d DG |
455 | |
456 | /* Copy common data to identify the response | |
457 | * on the lttng client side. | |
458 | */ | |
e065084a | 459 | copy_common_data(&llm, lsm); |
fac6795d DG |
460 | |
461 | /* Default return code. | |
e065084a | 462 | * In our world, everything is OK... right? ;) |
fac6795d | 463 | */ |
e065084a | 464 | llm.ret_code = LTTCOMM_OK; |
fac6795d DG |
465 | |
466 | /* Process by command type */ | |
467 | switch (lsm->cmd_type) { | |
468 | case UST_LIST_APPS: | |
469 | { | |
e065084a DG |
470 | pid_t *pids; |
471 | llm.num_pckt = ust_list_apps(&pids); | |
472 | if (llm.num_pckt == 0) { | |
473 | ret = LTTCOMM_NO_APPS; | |
474 | goto error; | |
475 | } | |
476 | ||
477 | /* Send all packets */ | |
478 | while (llm.num_pckt != 0) { | |
479 | llm.u.list_apps.pid = pids[traceable_app_count - llm.num_pckt]; | |
480 | ret = send_unix_sock(sock, (void*) &llm, sizeof(llm)); | |
481 | if (ret < 0) { | |
482 | goto send_error; | |
483 | } | |
484 | llm.num_pckt--; | |
485 | } | |
486 | /* Allocated array by ust_list_apps() */ | |
487 | free(pids); | |
488 | ||
fac6795d DG |
489 | break; |
490 | } | |
491 | default: | |
e065084a | 492 | { |
fac6795d | 493 | /* Undefined command */ |
e065084a | 494 | ret = LTTCOMM_UND; |
fac6795d | 495 | break; |
e065084a | 496 | } |
fac6795d DG |
497 | } |
498 | ||
e065084a DG |
499 | return 0; |
500 | ||
501 | send_error: | |
502 | return ret; | |
503 | ||
504 | error: | |
505 | /* Notify client of error */ | |
506 | llm.ret_code = ret; | |
507 | send_unix_sock(sock, (void*) &llm, sizeof(llm)); | |
508 | ||
509 | return -1; | |
fac6795d DG |
510 | } |
511 | ||
512 | /* | |
513 | * usage function on stderr | |
514 | */ | |
515 | static void usage(void) | |
516 | { | |
517 | fprintf(stderr, "Usage:\n%s OPTIONS\n\nOptions:\n" | |
518 | "\t-h, --help\t\tDisplay this usage.\n" | |
519 | "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n" | |
520 | "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n" | |
521 | "\t-d, --daemonize\t\tStart as a daemon.\n" | |
522 | "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n" | |
5b8719f5 DG |
523 | "\t-V, --version\t\tShow version number.\n" |
524 | "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n", | |
fac6795d DG |
525 | progname); |
526 | } | |
527 | ||
528 | /* | |
529 | * daemon argument parsing | |
530 | */ | |
531 | static int parse_args(int argc, char **argv) | |
532 | { | |
533 | int c; | |
534 | ||
535 | static struct option long_options[] = { | |
536 | { "client-sock", 1, 0, 'c' }, | |
537 | { "apps-sock", 1, 0, 'a' }, | |
538 | { "daemonize", 0, 0, 'd' }, | |
5b8719f5 | 539 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
540 | { "help", 0, 0, 'h' }, |
541 | { "group", 1, 0, 'g' }, | |
542 | { "version", 0, 0, 'V' }, | |
543 | { NULL, 0, 0, 0 } | |
544 | }; | |
545 | ||
546 | while (1) { | |
547 | int option_index = 0; | |
5b8719f5 | 548 | c = getopt_long(argc, argv, "dhVS" "a:c:g:s:", long_options, &option_index); |
fac6795d DG |
549 | if (c == -1) { |
550 | break; | |
551 | } | |
552 | ||
553 | switch (c) { | |
554 | case 0: | |
555 | fprintf(stderr, "option %s", long_options[option_index].name); | |
556 | if (optarg) { | |
557 | fprintf(stderr, " with arg %s\n", optarg); | |
558 | } | |
559 | break; | |
560 | case 's': | |
561 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); | |
562 | break; | |
563 | case 'a': | |
564 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
565 | break; | |
566 | case 'd': | |
567 | opt_daemon = 1; | |
568 | break; | |
569 | case 'g': | |
570 | opt_tracing_group = strdup(optarg); | |
571 | break; | |
572 | case 'h': | |
573 | usage(); | |
574 | exit(EXIT_FAILURE); | |
575 | case 'V': | |
576 | fprintf(stdout, "%s\n", VERSION); | |
577 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
578 | case 'S': |
579 | opt_sig_parent = 1; | |
580 | break; | |
fac6795d DG |
581 | default: |
582 | /* Unknown option or other error. | |
583 | * Error is printed by getopt, just return */ | |
584 | return -1; | |
585 | } | |
586 | } | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
591 | /* | |
592 | * init_daemon_socket | |
593 | * | |
594 | * Creates the two needed socket by the daemon. | |
595 | * apps_socket - The communication socket for all UST apps. | |
596 | * client_socket - The communication of the cli tool (lttng). | |
597 | */ | |
598 | static int init_daemon_socket() | |
599 | { | |
600 | int ret = 0; | |
601 | mode_t old_umask; | |
602 | ||
603 | old_umask = umask(0); | |
604 | ||
605 | /* Create client tool unix socket */ | |
606 | client_socket = lttcomm_create_unix_sock(client_unix_sock_path); | |
607 | if (client_socket < 0) { | |
608 | ret = -1; | |
609 | goto end; | |
610 | } | |
611 | ||
612 | /* File permission MUST be 660 */ | |
613 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
614 | if (ret < 0) { | |
615 | perror("chmod"); | |
616 | goto end; | |
617 | } | |
618 | ||
619 | /* Create the application unix socket */ | |
620 | apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path); | |
621 | if (apps_socket < 0) { | |
622 | ret = -1; | |
623 | goto end; | |
624 | } | |
625 | ||
626 | /* File permission MUST be 660 */ | |
627 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
628 | if (ret < 0) { | |
629 | perror("chmod"); | |
630 | goto end; | |
631 | } | |
632 | ||
633 | end: | |
634 | umask(old_umask); | |
635 | return ret; | |
636 | } | |
637 | ||
638 | /* | |
639 | * check_existing_daemon | |
640 | * | |
641 | * Check if the global socket is available. | |
642 | * If yes, error is returned. | |
643 | */ | |
644 | static int check_existing_daemon() | |
645 | { | |
646 | int ret; | |
647 | ||
648 | ret = access(client_unix_sock_path, F_OK); | |
649 | if (ret == 0) { | |
650 | ret = access(apps_unix_sock_path, F_OK); | |
651 | } | |
652 | ||
653 | return ret; | |
654 | } | |
655 | ||
656 | /* | |
657 | * get_home_dir | |
658 | * | |
659 | * Return pointer to home directory path using | |
660 | * the env variable HOME. | |
661 | * | |
662 | * Default : /tmp | |
663 | */ | |
664 | static const char *get_home_dir(void) | |
665 | { | |
666 | const char *home_path; | |
667 | ||
686204ab | 668 | if ((home_path = (const char *) getenv("HOME")) == NULL) { |
fac6795d DG |
669 | home_path = default_home_dir; |
670 | } | |
671 | ||
672 | return home_path; | |
673 | } | |
674 | ||
675 | /* | |
676 | * set_socket_perms | |
677 | * | |
678 | * Set the tracing group gid onto the client socket. | |
679 | */ | |
680 | static int set_socket_perms(void) | |
681 | { | |
682 | int ret; | |
683 | struct group *grp; | |
684 | ||
685 | /* Decide which group name to use */ | |
686 | (opt_tracing_group != NULL) ? | |
687 | (grp = getgrnam(opt_tracing_group)) : | |
688 | (grp = getgrnam(default_tracing_group)); | |
689 | ||
690 | if (grp == NULL) { | |
691 | fprintf(stderr, "Missing tracing group. Aborting execution.\n"); | |
692 | ret = -1; | |
693 | goto end; | |
694 | } | |
695 | ||
696 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); | |
697 | if (ret < 0) { | |
698 | perror("chown"); | |
699 | } | |
700 | ||
701 | end: | |
702 | return ret; | |
703 | } | |
704 | ||
705 | /* | |
706 | * daemonize | |
707 | * | |
708 | * Daemonize ltt-sessiond. | |
709 | */ | |
710 | static void daemonize(void) | |
711 | { | |
712 | pid_t pid, sid; | |
713 | const char *home_dir = get_home_dir(); | |
714 | ||
715 | /* Fork off the parent process */ | |
716 | if ((pid = fork()) < 0) { | |
717 | perror("fork"); | |
718 | exit(EXIT_FAILURE); | |
719 | } | |
720 | ||
721 | /* Parent can now exit */ | |
722 | if (pid > 0) { | |
723 | exit(EXIT_SUCCESS); | |
724 | } | |
725 | ||
726 | /* Change the file mode mask */ | |
727 | umask(0); | |
728 | ||
729 | /* Create a new SID for the child process */ | |
730 | if ((sid = setsid()) < 0) { | |
731 | perror("setsid"); | |
732 | exit(EXIT_FAILURE); | |
733 | } | |
734 | ||
735 | /* Change the current working directory */ | |
736 | if ((chdir(home_dir)) < 0) { | |
737 | perror("chdir"); | |
738 | exit(EXIT_FAILURE); | |
739 | } | |
740 | ||
741 | /* Close out the standard file descriptors */ | |
742 | close(STDIN_FILENO); | |
743 | close(STDOUT_FILENO); | |
744 | close(STDERR_FILENO); | |
745 | } | |
746 | ||
747 | /* | |
748 | * set_signal_handler | |
749 | * | |
750 | * Setup signal handler for : | |
751 | * SIGINT, SIGTERM, SIGPIPE | |
752 | */ | |
753 | static int set_signal_handler(void) | |
754 | { | |
755 | int ret = 0; | |
756 | struct sigaction sa; | |
757 | sigset_t sigset; | |
758 | ||
759 | if ((ret = sigemptyset(&sigset)) < 0) { | |
760 | perror("sigemptyset"); | |
761 | return ret; | |
762 | } | |
763 | ||
764 | sa.sa_handler = sighandler; | |
765 | sa.sa_mask = sigset; | |
766 | sa.sa_flags = 0; | |
767 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
768 | perror("sigaction"); | |
769 | return ret; | |
770 | } | |
771 | ||
772 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
773 | perror("sigaction"); | |
774 | return ret; | |
775 | } | |
776 | ||
777 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
778 | perror("sigaction"); | |
779 | return ret; | |
780 | } | |
781 | ||
782 | return ret; | |
783 | } | |
784 | ||
785 | /** | |
786 | * sighandler | |
787 | * | |
788 | * Signal handler for the daemon | |
789 | */ | |
790 | static void sighandler(int sig) | |
791 | { | |
792 | switch (sig) { | |
793 | case SIGPIPE: | |
794 | case SIGINT: | |
795 | case SIGTERM: | |
796 | cleanup(); | |
686204ab | 797 | break; |
fac6795d DG |
798 | default: |
799 | break; | |
800 | } | |
801 | ||
802 | exit(EXIT_SUCCESS); | |
803 | } | |
804 | ||
805 | /* | |
806 | * cleanup | |
807 | * | |
808 | * Cleanup the daemon on exit | |
809 | */ | |
810 | static void cleanup() | |
811 | { | |
812 | /* <fun> */ | |
813 | fprintf(stdout, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); | |
814 | fprintf(stdout, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0); | |
815 | /* </fun> */ | |
816 | ||
817 | unlink(client_unix_sock_path); | |
818 | unlink(apps_unix_sock_path); | |
819 | } | |
820 | ||
821 | /* | |
822 | * main | |
823 | */ | |
824 | int main(int argc, char **argv) | |
825 | { | |
826 | int i; | |
827 | int ret = 0; | |
828 | void *status; | |
829 | pthread_t threads[2]; | |
830 | ||
831 | /* Parse arguments */ | |
832 | progname = argv[0]; | |
833 | if ((ret = parse_args(argc, argv) < 0)) { | |
834 | goto error; | |
835 | } | |
836 | ||
837 | /* Daemonize */ | |
838 | if (opt_daemon) { | |
839 | daemonize(); | |
840 | } | |
841 | ||
842 | /* Check if daemon is UID = 0 */ | |
843 | is_root = !getuid(); | |
844 | ||
845 | /* Set all sockets path */ | |
846 | if (is_root) { | |
847 | if (strlen(apps_unix_sock_path) == 0) { | |
848 | (snprintf(apps_unix_sock_path, PATH_MAX, | |
849 | DEFAULT_GLOBAL_APPS_UNIX_SOCK)); | |
850 | } | |
851 | ||
852 | if (strlen(client_unix_sock_path) == 0) { | |
853 | (snprintf(client_unix_sock_path, PATH_MAX, | |
854 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK)); | |
855 | } | |
856 | } else { | |
857 | if (strlen(apps_unix_sock_path) == 0) { | |
858 | (snprintf(apps_unix_sock_path, PATH_MAX, | |
859 | DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir())); | |
860 | } | |
861 | ||
862 | /* Set the cli tool unix socket path */ | |
863 | if (strlen(client_unix_sock_path) == 0) { | |
864 | (snprintf(client_unix_sock_path, PATH_MAX, | |
865 | DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir())); | |
866 | } | |
867 | } | |
868 | ||
869 | /* See if daemon already exist. If any of the two | |
870 | * socket needed by the daemon are present, this test fails | |
871 | */ | |
872 | if ((ret = check_existing_daemon()) == 0) { | |
873 | fprintf(stderr, "Already running daemon.\n"); | |
874 | goto error; | |
875 | } | |
876 | ||
877 | if (set_signal_handler() < 0) { | |
878 | goto error; | |
879 | } | |
880 | ||
881 | /* Setup the two needed unix socket */ | |
882 | if (init_daemon_socket() < 0) { | |
883 | goto error; | |
884 | } | |
885 | ||
886 | /* Set credentials to socket */ | |
887 | if (is_root && (set_socket_perms() < 0)) { | |
888 | goto error; | |
889 | } | |
890 | ||
5b8719f5 DG |
891 | /* Get parent pid if -S, --sig-parent is specified. */ |
892 | if (opt_sig_parent) { | |
893 | ppid = getppid(); | |
894 | } | |
895 | ||
fac6795d DG |
896 | while (1) { |
897 | /* Create thread to manage the client socket */ | |
898 | ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL); | |
899 | if (ret != 0) { | |
900 | perror("pthread_create"); | |
901 | goto error; | |
902 | } | |
903 | ||
904 | /* Create thread to manage application socket */ | |
905 | ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL); | |
906 | if (ret != 0) { | |
907 | perror("pthread_create"); | |
908 | goto error; | |
909 | } | |
910 | ||
911 | for (i = 0; i < 2; i++) { | |
912 | ret = pthread_join(threads[i], &status); | |
913 | if (ret != 0) { | |
914 | perror("pthread_join"); | |
915 | goto error; | |
916 | } | |
917 | } | |
918 | } | |
919 | ||
920 | cleanup(); | |
921 | return 0; | |
922 | ||
923 | error: | |
924 | cleanup(); | |
925 | ||
926 | return EXIT_FAILURE; | |
927 | } |