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