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