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