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