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