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