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> | |
8c0faa1d | 25 | #include <semaphore.h> |
fac6795d DG |
26 | #include <signal.h> |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <sys/ipc.h> | |
31 | #include <sys/shm.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
f3ed775e DG |
35 | #include <sys/time.h> |
36 | #include <sys/resource.h> | |
fac6795d DG |
37 | #include <unistd.h> |
38 | ||
39 | #include <urcu/list.h> /* URCU list library (-lurcu) */ | |
5b97ec60 | 40 | #include <lttng/lttng.h> |
fac6795d DG |
41 | |
42 | #include "liblttsessiondcomm.h" | |
43 | #include "ltt-sessiond.h" | |
75462a81 | 44 | #include "lttngerr.h" |
20fe2104 | 45 | #include "kernel-ctl.h" |
9e78d6ae | 46 | #include "ust-ctl.h" |
5b74c7b1 | 47 | #include "session.h" |
91d76f53 | 48 | #include "traceable-app.h" |
5dc18550 | 49 | #include "lttng-kconsumerd.h" |
62d3069f | 50 | #include "libustctl.h" |
fac6795d | 51 | |
5e16da05 MD |
52 | /* |
53 | * TODO: | |
54 | * teardown: signal SIGTERM handler -> write into pipe. Threads waits | |
55 | * with epoll on pipe and on other pipes/sockets for commands. Main | |
56 | * simply waits on pthread join. | |
57 | */ | |
58 | ||
75462a81 | 59 | /* Const values */ |
686204ab | 60 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 61 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
62 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
63 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
64 | ||
fac6795d | 65 | /* Variables */ |
1d4b027a DG |
66 | int opt_verbose; /* Not static for lttngerr.h */ |
67 | int opt_quiet; /* Not static for lttngerr.h */ | |
fac6795d DG |
68 | const char *progname; |
69 | const char *opt_tracing_group; | |
5b8719f5 | 70 | static int opt_sig_parent; |
fac6795d DG |
71 | static int opt_daemon; |
72 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
1d4b027a DG |
73 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
74 | static pid_t kconsumerd_pid; | |
fac6795d | 75 | |
d6f42150 DG |
76 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
77 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
78 | static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */ | |
79 | static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */ | |
fac6795d | 80 | |
1d4b027a | 81 | /* Sockets and FDs */ |
d6f42150 DG |
82 | static int client_sock; |
83 | static int apps_sock; | |
84 | static int kconsumerd_err_sock; | |
8c0faa1d | 85 | static int kconsumerd_cmd_sock; |
20fe2104 | 86 | static int kernel_tracer_fd; |
1d4b027a DG |
87 | |
88 | /* Pthread, Mutexes and Semaphores */ | |
8c0faa1d | 89 | static pthread_t kconsumerd_thread; |
1d4b027a DG |
90 | static pthread_t apps_thread; |
91 | static pthread_t client_thread; | |
8c0faa1d DG |
92 | static sem_t kconsumerd_sem; |
93 | ||
1d4b027a | 94 | static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */ |
fac6795d | 95 | |
fac6795d | 96 | /* |
1d4b027a | 97 | * teardown_kernel_session |
fac6795d | 98 | * |
1d4b027a DG |
99 | * Complete teardown of a kernel session. This free all data structure |
100 | * related to a kernel session and update counter. | |
fac6795d | 101 | */ |
1d4b027a | 102 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 103 | { |
1d4b027a DG |
104 | if (session->kernel_session != NULL) { |
105 | DBG("Tearing down kernel session"); | |
c363b55d | 106 | trace_destroy_kernel_session(session->kernel_session); |
1d4b027a DG |
107 | /* Extra precaution */ |
108 | session->kernel_session = NULL; | |
109 | /* Decrement session count */ | |
110 | session->kern_session_count--; | |
fac6795d | 111 | } |
fac6795d DG |
112 | } |
113 | ||
114 | /* | |
1d4b027a | 115 | * cleanup |
fac6795d | 116 | * |
1d4b027a | 117 | * Cleanup the daemon on exit |
fac6795d | 118 | */ |
1d4b027a | 119 | static void cleanup() |
fac6795d | 120 | { |
1d4b027a DG |
121 | int ret; |
122 | char *cmd; | |
123 | struct ltt_session *sess; | |
fac6795d | 124 | |
1d4b027a | 125 | DBG("Cleaning up"); |
e07ae692 | 126 | |
1d4b027a DG |
127 | /* <fun> */ |
128 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); | |
129 | MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); | |
130 | /* </fun> */ | |
fac6795d | 131 | |
1d4b027a DG |
132 | /* Stopping all threads */ |
133 | DBG("Terminating all threads"); | |
134 | pthread_cancel(client_thread); | |
135 | pthread_cancel(apps_thread); | |
136 | if (kconsumerd_pid != 0) { | |
137 | pthread_cancel(kconsumerd_thread); | |
5b8719f5 DG |
138 | } |
139 | ||
1d4b027a DG |
140 | DBG("Unlinking all unix socket"); |
141 | unlink(client_unix_sock_path); | |
142 | unlink(apps_unix_sock_path); | |
143 | unlink(kconsumerd_err_unix_sock_path); | |
fac6795d | 144 | |
1d4b027a DG |
145 | DBG("Removing %s directory", LTTNG_RUNDIR); |
146 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); | |
147 | if (ret < 0) { | |
148 | ERR("asprintf failed. Something is really wrong!"); | |
149 | } | |
5461b305 | 150 | |
1d4b027a DG |
151 | /* Remove lttng run directory */ |
152 | ret = system(cmd); | |
153 | if (ret < 0) { | |
154 | ERR("Unable to clean " LTTNG_RUNDIR); | |
155 | } | |
5461b305 | 156 | |
1d4b027a DG |
157 | DBG("Cleaning up all session"); |
158 | /* Cleanup ALL session */ | |
159 | cds_list_for_each_entry(sess, <t_session_list.head, list) { | |
160 | teardown_kernel_session(sess); | |
161 | // TODO complete session cleanup (including UST) | |
fac6795d DG |
162 | } |
163 | ||
f3ed775e | 164 | DBG("Closing kernel fd"); |
1d4b027a | 165 | close(kernel_tracer_fd); |
fac6795d DG |
166 | } |
167 | ||
e065084a DG |
168 | /* |
169 | * send_unix_sock | |
170 | * | |
171 | * Send data on a unix socket using the liblttsessiondcomm API. | |
172 | * | |
173 | * Return lttcomm error code. | |
174 | */ | |
175 | static int send_unix_sock(int sock, void *buf, size_t len) | |
176 | { | |
177 | /* Check valid length */ | |
178 | if (len <= 0) { | |
179 | return -1; | |
180 | } | |
181 | ||
182 | return lttcomm_send_unix_sock(sock, buf, len); | |
183 | } | |
184 | ||
5461b305 DG |
185 | /* |
186 | * clean_command_ctx | |
187 | * | |
188 | * Free memory of a command context structure. | |
189 | */ | |
190 | static void clean_command_ctx(struct command_ctx *cmd_ctx) | |
191 | { | |
192 | DBG("Clean command context structure %p", cmd_ctx); | |
193 | if (cmd_ctx) { | |
194 | if (cmd_ctx->llm) { | |
195 | free(cmd_ctx->llm); | |
196 | } | |
197 | if (cmd_ctx->lsm) { | |
198 | free(cmd_ctx->lsm); | |
199 | } | |
200 | free(cmd_ctx); | |
201 | cmd_ctx = NULL; | |
202 | } | |
203 | } | |
204 | ||
f3ed775e DG |
205 | /* |
206 | * send_kconsumerd_fds | |
207 | * | |
208 | * Send all stream fds of the kernel session to the consumer. | |
209 | */ | |
210 | static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session) | |
211 | { | |
212 | int ret; | |
213 | size_t nb_fd; | |
214 | struct ltt_kernel_stream *stream; | |
215 | struct ltt_kernel_channel *chan; | |
216 | struct lttcomm_kconsumerd_header lkh; | |
217 | struct lttcomm_kconsumerd_msg lkm; | |
218 | ||
219 | nb_fd = session->stream_count_global; | |
220 | ||
221 | /* Setup header */ | |
222 | lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg); | |
223 | lkh.cmd_type = ADD_STREAM; | |
224 | ||
225 | DBG("Sending kconsumerd header"); | |
226 | ||
227 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); | |
228 | if (ret < 0) { | |
229 | perror("send kconsumerd header"); | |
230 | goto error; | |
231 | } | |
232 | ||
233 | DBG("Sending metadata stream fd"); | |
234 | ||
235 | /* Send metadata stream fd first */ | |
236 | lkm.fd = session->metadata_stream_fd; | |
237 | lkm.state = ACTIVE_FD; | |
238 | lkm.max_sb_size = session->metadata->conf->attr.subbuf_size; | |
239 | strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX); | |
240 | ||
241 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
242 | if (ret < 0) { | |
243 | perror("send kconsumerd fd"); | |
244 | goto error; | |
245 | } | |
246 | ||
247 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
248 | cds_list_for_each_entry(stream, &chan->stream_list.head, list) { | |
249 | lkm.fd = stream->fd; | |
250 | lkm.state = stream->state; | |
251 | lkm.max_sb_size = chan->channel->attr.subbuf_size; | |
252 | strncpy(lkm.path_name, stream->pathname, PATH_MAX); | |
253 | ||
254 | DBG("Sending fd %d to kconsumerd", lkm.fd); | |
255 | ||
256 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
257 | if (ret < 0) { | |
258 | perror("send kconsumerd fd"); | |
259 | goto error; | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | DBG("Kconsumerd fds sent"); | |
265 | ||
266 | return 0; | |
267 | ||
268 | error: | |
269 | return ret; | |
270 | } | |
271 | ||
272 | /* | |
273 | * create_trace_dir | |
274 | * | |
275 | * Create the trace output directory. | |
276 | */ | |
277 | static int create_trace_dir(struct ltt_kernel_session *session) | |
278 | { | |
279 | int ret; | |
280 | struct ltt_kernel_channel *chan; | |
281 | ||
282 | /* Create all channel directories */ | |
283 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
284 | DBG("Creating trace directory at %s", chan->pathname); | |
285 | // TODO: recursive create dir | |
286 | ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG ); | |
287 | if (ret < 0) { | |
288 | if (ret != EEXIST) { | |
289 | perror("mkdir trace path"); | |
290 | ret = -errno; | |
291 | goto error; | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | return 0; | |
297 | ||
298 | error: | |
299 | return ret; | |
300 | } | |
301 | ||
fac6795d | 302 | /* |
471d1693 | 303 | * ust_connect_app |
fac6795d DG |
304 | * |
305 | * Return a socket connected to the libust communication socket | |
306 | * of the application identified by the pid. | |
379473d2 DG |
307 | * |
308 | * If the pid is not found in the traceable list, | |
309 | * return -1 to indicate error. | |
fac6795d | 310 | */ |
471d1693 | 311 | static int ust_connect_app(pid_t pid) |
fac6795d | 312 | { |
91d76f53 DG |
313 | int sock; |
314 | struct ltt_traceable_app *lta; | |
379473d2 | 315 | |
e07ae692 DG |
316 | DBG("Connect to application pid %d", pid); |
317 | ||
91d76f53 DG |
318 | lta = find_app_by_pid(pid); |
319 | if (lta == NULL) { | |
320 | /* App not found */ | |
7442b2ba | 321 | DBG("Application pid %d not found", pid); |
379473d2 DG |
322 | return -1; |
323 | } | |
fac6795d | 324 | |
91d76f53 | 325 | sock = ustctl_connect_pid(lta->pid); |
fac6795d | 326 | if (sock < 0) { |
62d3069f | 327 | ERR("Fail connecting to the PID %d", pid); |
fac6795d DG |
328 | } |
329 | ||
330 | return sock; | |
331 | } | |
332 | ||
333 | /* | |
334 | * notify_apps | |
335 | * | |
336 | * Notify apps by writing 42 to a named pipe using name. | |
337 | * Every applications waiting for a ltt-sessiond will be notified | |
338 | * and re-register automatically to the session daemon. | |
339 | * | |
340 | * Return open or write error value. | |
341 | */ | |
342 | static int notify_apps(const char *name) | |
343 | { | |
344 | int fd; | |
345 | int ret = -1; | |
346 | ||
e07ae692 DG |
347 | DBG("Notify the global application pipe"); |
348 | ||
fac6795d DG |
349 | /* Try opening the global pipe */ |
350 | fd = open(name, O_WRONLY); | |
351 | if (fd < 0) { | |
352 | goto error; | |
353 | } | |
354 | ||
355 | /* Notify by writing on the pipe */ | |
356 | ret = write(fd, "42", 2); | |
357 | if (ret < 0) { | |
358 | perror("write"); | |
359 | } | |
360 | ||
361 | error: | |
362 | return ret; | |
363 | } | |
364 | ||
e065084a | 365 | /* |
5461b305 | 366 | * setup_lttng_msg |
ca95a216 | 367 | * |
5461b305 DG |
368 | * Setup the outgoing data buffer for the response (llm) by allocating the |
369 | * right amount of memory and copying the original information from the lsm | |
370 | * structure. | |
ca95a216 DG |
371 | * |
372 | * Return total size of the buffer pointed by buf. | |
373 | */ | |
5461b305 | 374 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 375 | { |
f3ed775e | 376 | int ret, buf_size; |
ca95a216 | 377 | |
f3ed775e | 378 | buf_size = size; |
5461b305 DG |
379 | |
380 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); | |
381 | if (cmd_ctx->llm == NULL) { | |
ca95a216 | 382 | perror("malloc"); |
5461b305 | 383 | ret = -ENOMEM; |
ca95a216 DG |
384 | goto error; |
385 | } | |
386 | ||
5461b305 DG |
387 | /* Copy common data */ |
388 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
389 | cmd_ctx->llm->pid = cmd_ctx->lsm->pid; | |
5461b305 | 390 | |
5461b305 DG |
391 | cmd_ctx->llm->data_size = size; |
392 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
393 | ||
ca95a216 DG |
394 | return buf_size; |
395 | ||
396 | error: | |
397 | return ret; | |
398 | } | |
399 | ||
1d4b027a DG |
400 | /* |
401 | * thread_manage_kconsumerd | |
402 | * | |
403 | * This thread manage the kconsumerd error sent | |
404 | * back to the session daemon. | |
405 | */ | |
406 | static void *thread_manage_kconsumerd(void *data) | |
407 | { | |
408 | int sock, ret; | |
409 | enum lttcomm_return_code code; | |
410 | ||
411 | DBG("[thread] Manage kconsumerd started"); | |
412 | ||
413 | ret = lttcomm_listen_unix_sock(kconsumerd_err_sock); | |
414 | if (ret < 0) { | |
415 | goto error; | |
416 | } | |
417 | ||
418 | sock = lttcomm_accept_unix_sock(kconsumerd_err_sock); | |
419 | if (sock < 0) { | |
420 | goto error; | |
421 | } | |
422 | ||
712ea556 | 423 | /* Getting status code from kconsumerd */ |
1d4b027a DG |
424 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); |
425 | if (ret <= 0) { | |
426 | goto error; | |
427 | } | |
428 | ||
429 | if (code == KCONSUMERD_COMMAND_SOCK_READY) { | |
430 | kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path); | |
431 | if (kconsumerd_cmd_sock < 0) { | |
712ea556 | 432 | sem_post(&kconsumerd_sem); |
1d4b027a DG |
433 | perror("kconsumerd connect"); |
434 | goto error; | |
435 | } | |
436 | /* Signal condition to tell that the kconsumerd is ready */ | |
437 | sem_post(&kconsumerd_sem); | |
438 | DBG("Kconsumerd command socket ready"); | |
439 | } else { | |
6f61d021 | 440 | DBG("Kconsumerd error when waiting for SOCK_READY : %s", |
1d4b027a DG |
441 | lttcomm_get_readable_code(-code)); |
442 | goto error; | |
443 | } | |
444 | ||
712ea556 DG |
445 | /* Wait for any kconsumerd error */ |
446 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); | |
447 | if (ret <= 0) { | |
448 | ERR("Kconsumerd closed the command socket"); | |
449 | goto error; | |
6f61d021 | 450 | } |
1d4b027a | 451 | |
712ea556 DG |
452 | ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code)); |
453 | ||
1d4b027a DG |
454 | error: |
455 | kconsumerd_pid = 0; | |
6f61d021 | 456 | DBG("Kconsumerd thread dying"); |
1d4b027a DG |
457 | return NULL; |
458 | } | |
459 | ||
460 | /* | |
461 | * thread_manage_apps | |
462 | * | |
463 | * This thread manage the application socket communication | |
464 | */ | |
465 | static void *thread_manage_apps(void *data) | |
466 | { | |
467 | int sock, ret; | |
468 | ||
469 | /* TODO: Something more elegant is needed but fine for now */ | |
470 | /* FIXME: change all types to either uint8_t, uint32_t, uint64_t | |
471 | * for 32-bit vs 64-bit compat processes. */ | |
472 | /* replicate in ust with version number */ | |
473 | struct { | |
474 | int reg; /* 1:register, 0:unregister */ | |
475 | pid_t pid; | |
476 | uid_t uid; | |
477 | } reg_msg; | |
478 | ||
479 | DBG("[thread] Manage apps started"); | |
480 | ||
481 | ret = lttcomm_listen_unix_sock(apps_sock); | |
482 | if (ret < 0) { | |
483 | goto error; | |
484 | } | |
485 | ||
486 | /* Notify all applications to register */ | |
487 | notify_apps(default_global_apps_pipe); | |
488 | ||
489 | while (1) { | |
490 | DBG("Accepting application registration"); | |
491 | /* Blocking call, waiting for transmission */ | |
492 | sock = lttcomm_accept_unix_sock(apps_sock); | |
493 | if (sock < 0) { | |
494 | goto error; | |
495 | } | |
496 | ||
497 | /* Basic recv here to handle the very simple data | |
498 | * that the libust send to register (reg_msg). | |
499 | */ | |
500 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); | |
501 | if (ret < 0) { | |
502 | perror("recv"); | |
503 | continue; | |
504 | } | |
505 | ||
506 | /* Add application to the global traceable list */ | |
507 | if (reg_msg.reg == 1) { | |
508 | /* Registering */ | |
509 | ret = register_traceable_app(reg_msg.pid, reg_msg.uid); | |
510 | if (ret < 0) { | |
511 | /* register_traceable_app only return an error with | |
512 | * ENOMEM. At this point, we better stop everything. | |
513 | */ | |
514 | goto error; | |
515 | } | |
516 | } else { | |
517 | /* Unregistering */ | |
518 | unregister_traceable_app(reg_msg.pid); | |
519 | } | |
520 | } | |
521 | ||
522 | error: | |
523 | ||
524 | return NULL; | |
525 | } | |
526 | ||
8c0faa1d | 527 | /* |
693bd40b | 528 | * spawn_kconsumerd_thread |
8c0faa1d DG |
529 | * |
530 | * Start the thread_manage_kconsumerd. This must be done after a kconsumerd | |
531 | * exec or it will fails. | |
532 | */ | |
693bd40b | 533 | static int spawn_kconsumerd_thread(void) |
8c0faa1d DG |
534 | { |
535 | int ret; | |
536 | ||
537 | /* Setup semaphore */ | |
538 | sem_init(&kconsumerd_sem, 0, 0); | |
539 | ||
1d4b027a | 540 | ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL); |
8c0faa1d DG |
541 | if (ret != 0) { |
542 | perror("pthread_create kconsumerd"); | |
543 | goto error; | |
544 | } | |
545 | ||
693bd40b | 546 | /* Wait for the kconsumerd thread to be ready */ |
8c0faa1d DG |
547 | sem_wait(&kconsumerd_sem); |
548 | ||
712ea556 DG |
549 | if (kconsumerd_pid == 0) { |
550 | ERR("Kconsumerd did not start"); | |
551 | goto error; | |
552 | } | |
553 | ||
8c0faa1d DG |
554 | return 0; |
555 | ||
556 | error: | |
712ea556 | 557 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
8c0faa1d DG |
558 | return ret; |
559 | } | |
560 | ||
561 | /* | |
693bd40b | 562 | * spawn_kconsumerd |
8c0faa1d | 563 | * |
693bd40b DG |
564 | * Fork and exec a kernel consumer daemon (kconsumerd). |
565 | * | |
566 | * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel | |
567 | * file descriptor using the libkernelctl or kernel-ctl functions. So, a | |
568 | * kernel consumer MUST only be spawned before creating a kernel session. | |
8c0faa1d DG |
569 | * |
570 | * Return pid if successful else -1. | |
571 | */ | |
693bd40b | 572 | static pid_t spawn_kconsumerd(void) |
8c0faa1d DG |
573 | { |
574 | int ret; | |
575 | pid_t pid; | |
576 | ||
c49dc785 DG |
577 | DBG("Spawning kconsumerd"); |
578 | ||
8c0faa1d DG |
579 | pid = fork(); |
580 | if (pid == 0) { | |
581 | /* | |
582 | * Exec kconsumerd. | |
583 | */ | |
f57244de | 584 | execlp("kconsumerd", "kconsumerd", "--verbose", NULL); |
8c0faa1d DG |
585 | if (errno != 0) { |
586 | perror("kernel start consumer exec"); | |
587 | } | |
588 | exit(EXIT_FAILURE); | |
589 | } else if (pid > 0) { | |
590 | ret = pid; | |
591 | goto error; | |
592 | } else { | |
593 | perror("kernel start consumer fork"); | |
594 | ret = -errno; | |
595 | goto error; | |
596 | } | |
597 | ||
598 | error: | |
599 | return ret; | |
600 | } | |
601 | ||
693bd40b DG |
602 | /* |
603 | * start_kconsumerd | |
604 | * | |
605 | * Spawn the kconsumerd daemon and session daemon thread. | |
606 | */ | |
607 | static int start_kconsumerd(void) | |
608 | { | |
609 | int ret; | |
610 | ||
693bd40b | 611 | pthread_mutex_lock(&kconsumerd_pid_mutex); |
c49dc785 DG |
612 | if (kconsumerd_pid != 0) { |
613 | goto end; | |
614 | } | |
693bd40b | 615 | |
c49dc785 DG |
616 | ret = spawn_kconsumerd(); |
617 | if (ret < 0) { | |
618 | ERR("Spawning kconsumerd failed"); | |
619 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
620 | pthread_mutex_unlock(&kconsumerd_pid_mutex); | |
621 | goto error; | |
693bd40b | 622 | } |
c49dc785 DG |
623 | |
624 | /* Setting up the global kconsumerd_pid */ | |
625 | kconsumerd_pid = ret; | |
693bd40b DG |
626 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
627 | ||
6f61d021 DG |
628 | DBG("Kconsumerd pid %d", ret); |
629 | ||
693bd40b | 630 | DBG("Spawning kconsumerd thread"); |
693bd40b DG |
631 | ret = spawn_kconsumerd_thread(); |
632 | if (ret < 0) { | |
633 | ERR("Fatal error spawning kconsumerd thread"); | |
693bd40b DG |
634 | goto error; |
635 | } | |
636 | ||
c49dc785 DG |
637 | end: |
638 | pthread_mutex_unlock(&kconsumerd_pid_mutex); | |
693bd40b DG |
639 | return 0; |
640 | ||
641 | error: | |
642 | return ret; | |
643 | } | |
644 | ||
8c0faa1d | 645 | /* |
f3ed775e | 646 | * init_kernel_tracer |
8c0faa1d | 647 | * |
f3ed775e | 648 | * Setup necessary data for kernel tracer action. |
8c0faa1d | 649 | */ |
f3ed775e | 650 | static void init_kernel_tracer(void) |
8c0faa1d | 651 | { |
f3ed775e DG |
652 | /* Set the global kernel tracer fd */ |
653 | kernel_tracer_fd = open(DEFAULT_KERNEL_TRACER_PATH, O_RDWR); | |
654 | if (kernel_tracer_fd < 0) { | |
655 | WARN("No kernel tracer available"); | |
656 | kernel_tracer_fd = 0; | |
8c0faa1d DG |
657 | } |
658 | ||
f3ed775e DG |
659 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
660 | } | |
33a2b854 | 661 | |
f3ed775e DG |
662 | /* |
663 | * start_kernel_trace | |
664 | * | |
665 | * Start tracing by creating trace directory and sending FDs to the kernel | |
666 | * consumer. | |
667 | */ | |
668 | static int start_kernel_trace(struct ltt_kernel_session *session) | |
669 | { | |
670 | int ret; | |
8c0faa1d | 671 | |
f3ed775e DG |
672 | /* Create trace directory */ |
673 | ret = create_trace_dir(session); | |
1d4b027a | 674 | if (ret < 0) { |
f3ed775e DG |
675 | if (ret == -EEXIST) { |
676 | ret = LTTCOMM_KERN_DIR_EXIST; | |
677 | } else { | |
678 | ret = LTTCOMM_KERN_DIR_FAIL; | |
679 | goto error; | |
33a2b854 DG |
680 | } |
681 | } | |
682 | ||
f3ed775e DG |
683 | if (session->kconsumer_fds_sent == 0) { |
684 | ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session); | |
685 | if (ret < 0) { | |
686 | ERR("Send kconsumerd fds failed"); | |
687 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
688 | goto error; | |
689 | } | |
1d4b027a | 690 | |
f3ed775e DG |
691 | session->kconsumer_fds_sent = 1; |
692 | } | |
1d4b027a DG |
693 | |
694 | error: | |
695 | return ret; | |
8c0faa1d DG |
696 | } |
697 | ||
698 | /* | |
f3ed775e | 699 | * init_default_channel |
8c0faa1d | 700 | * |
f3ed775e | 701 | * Allocate a channel structure and fill it. |
8c0faa1d | 702 | */ |
f3ed775e | 703 | static struct lttng_channel *init_default_channel(void) |
8c0faa1d | 704 | { |
f3ed775e | 705 | struct lttng_channel *chan; |
1d4b027a | 706 | |
f3ed775e DG |
707 | chan = malloc(sizeof(struct lttng_channel)); |
708 | if (chan == NULL) { | |
709 | perror("init channel malloc"); | |
710 | goto error; | |
8c0faa1d | 711 | } |
1d4b027a | 712 | |
f3ed775e DG |
713 | if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) { |
714 | perror("snprintf defautl channel name"); | |
715 | return NULL; | |
716 | } | |
717 | ||
718 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
719 | chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE; | |
720 | chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM; | |
721 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
722 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
1d4b027a DG |
723 | |
724 | error: | |
f3ed775e | 725 | return chan; |
8c0faa1d DG |
726 | } |
727 | ||
333f285e | 728 | /* |
f3ed775e | 729 | * create_kernel_session |
333f285e | 730 | * |
f3ed775e | 731 | * Create a kernel tracer session then create the default channel. |
333f285e | 732 | */ |
f3ed775e | 733 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 734 | { |
f3ed775e DG |
735 | int ret; |
736 | struct lttng_channel *chan; | |
737 | ||
738 | DBG("Creating kernel session"); | |
739 | ||
740 | ret = kernel_create_session(session, kernel_tracer_fd); | |
741 | if (ret < 0) { | |
742 | ret = LTTCOMM_KERN_SESS_FAIL; | |
743 | goto error; | |
333f285e DG |
744 | } |
745 | ||
f3ed775e DG |
746 | chan = init_default_channel(); |
747 | if (chan == NULL) { | |
748 | ret = LTTCOMM_FATAL; | |
749 | goto error; | |
750 | } | |
751 | ||
752 | DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME); | |
753 | ||
754 | ret = kernel_create_channel(session->kernel_session, chan); | |
755 | if (ret < 0) { | |
756 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
757 | goto error; | |
758 | } | |
759 | ||
760 | error: | |
761 | return ret; | |
333f285e DG |
762 | } |
763 | ||
fac6795d DG |
764 | /* |
765 | * process_client_msg | |
766 | * | |
5461b305 DG |
767 | * Process the command requested by the lttng client within the command |
768 | * context structure. This function make sure that the return structure (llm) | |
769 | * is set and ready for transmission before returning. | |
fac6795d | 770 | * |
e065084a | 771 | * Return any error encountered or 0 for success. |
fac6795d | 772 | */ |
5461b305 | 773 | static int process_client_msg(struct command_ctx *cmd_ctx) |
fac6795d | 774 | { |
5461b305 | 775 | int ret; |
fac6795d | 776 | |
5461b305 | 777 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
fac6795d | 778 | |
f3ed775e | 779 | /* Listing commands don't need a session */ |
5461b305 | 780 | switch (cmd_ctx->lsm->cmd_type) { |
5e16da05 MD |
781 | case LTTNG_CREATE_SESSION: |
782 | case LTTNG_LIST_SESSIONS: | |
f3ed775e DG |
783 | case LTTNG_LIST_EVENTS: |
784 | case LTTNG_KERNEL_LIST_EVENTS: | |
785 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 MD |
786 | break; |
787 | default: | |
f3ed775e DG |
788 | DBG("Getting session %s by name", cmd_ctx->lsm->session_name); |
789 | cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name); | |
5461b305 | 790 | if (cmd_ctx->session == NULL) { |
f3ed775e DG |
791 | /* If session name not found */ |
792 | if (cmd_ctx->lsm->session_name != NULL) { | |
793 | ret = LTTCOMM_SESS_NOT_FOUND; | |
794 | } else { /* If no session name specified */ | |
795 | ret = LTTCOMM_SELECT_SESS; | |
796 | } | |
5461b305 | 797 | goto error; |
379473d2 | 798 | } |
5e16da05 | 799 | break; |
379473d2 DG |
800 | } |
801 | ||
f3ed775e DG |
802 | /* |
803 | * Check kernel command for kernel session. | |
804 | */ | |
20fe2104 | 805 | switch (cmd_ctx->lsm->cmd_type) { |
f3ed775e DG |
806 | case LTTNG_KERNEL_CREATE_CHANNEL: |
807 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: | |
808 | case LTTNG_KERNEL_DISABLE_CHANNEL: | |
809 | case LTTNG_KERNEL_DISABLE_EVENT: | |
810 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: | |
811 | case LTTNG_KERNEL_ENABLE_CHANNEL: | |
812 | case LTTNG_KERNEL_ENABLE_EVENT: | |
813 | case LTTNG_KERNEL_LIST_EVENTS: | |
333f285e | 814 | /* Kernel tracer check */ |
20fe2104 | 815 | if (kernel_tracer_fd == 0) { |
333f285e DG |
816 | init_kernel_tracer(); |
817 | if (kernel_tracer_fd == 0) { | |
818 | ret = LTTCOMM_KERN_NA; | |
819 | goto error; | |
820 | } | |
20fe2104 | 821 | } |
f3ed775e DG |
822 | |
823 | /* Need a session for kernel command */ | |
824 | if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS && | |
825 | cmd_ctx->session->kernel_session == NULL) { | |
7b395890 | 826 | |
f3ed775e DG |
827 | ret = create_kernel_session(cmd_ctx->session); |
828 | if (ret < 0) { | |
829 | ret = LTTCOMM_KERN_SESS_FAIL; | |
830 | goto error; | |
831 | } | |
832 | ||
7b395890 | 833 | /* Start the kernel consumer daemon */ |
f3ed775e DG |
834 | if (kconsumerd_pid == 0) { |
835 | ret = start_kconsumerd(); | |
836 | if (ret < 0) { | |
837 | goto error; | |
838 | } | |
839 | } | |
840 | } | |
20fe2104 DG |
841 | } |
842 | ||
471d1693 | 843 | /* Connect to ust apps if available pid */ |
5461b305 | 844 | if (cmd_ctx->lsm->pid > 0) { |
471d1693 | 845 | /* Connect to app using ustctl API */ |
5461b305 DG |
846 | cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid); |
847 | if (cmd_ctx->ust_sock < 0) { | |
471d1693 | 848 | ret = LTTCOMM_NO_TRACEABLE; |
5461b305 | 849 | goto error; |
471d1693 DG |
850 | } |
851 | } | |
852 | ||
fac6795d | 853 | /* Process by command type */ |
5461b305 | 854 | switch (cmd_ctx->lsm->cmd_type) { |
f3ed775e | 855 | case LTTNG_KERNEL_CREATE_CHANNEL: |
20fe2104 | 856 | { |
f3ed775e | 857 | /* Setup lttng message with no payload */ |
20fe2104 DG |
858 | ret = setup_lttng_msg(cmd_ctx, 0); |
859 | if (ret < 0) { | |
860 | goto setup_error; | |
861 | } | |
862 | ||
f3ed775e | 863 | /* Kernel tracer */ |
8c0faa1d DG |
864 | DBG("Creating kernel channel"); |
865 | ||
f3ed775e DG |
866 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, |
867 | &cmd_ctx->lsm->u.channel.chan); | |
20fe2104 DG |
868 | if (ret < 0) { |
869 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
870 | goto error; | |
871 | } | |
872 | ||
873 | ret = LTTCOMM_OK; | |
874 | break; | |
875 | } | |
e953ef25 DG |
876 | case LTTNG_KERNEL_DISABLE_EVENT: |
877 | { | |
878 | int found = 0; | |
879 | struct ltt_kernel_channel *chan; | |
880 | ||
881 | /* Setup lttng message with no payload */ | |
882 | ret = setup_lttng_msg(cmd_ctx, 0); | |
883 | if (ret < 0) { | |
884 | goto setup_error; | |
885 | } | |
886 | ||
887 | /* Get channel by name and create event for that channel */ | |
888 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
889 | if (strcmp(cmd_ctx->lsm->u.disable.channel_name, chan->channel->name) == 0) { | |
890 | DBG("Disabling kernel event %s for channel %s.", | |
891 | cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name); | |
892 | ||
893 | ret = kernel_disable_event(cmd_ctx->lsm->u.disable.name, chan); | |
894 | if (ret < 0) { | |
895 | ret = LTTCOMM_KERN_DISABLE_FAIL; | |
896 | goto error; | |
897 | } | |
898 | found = 1; | |
899 | break; | |
900 | } | |
901 | } | |
902 | ||
903 | if (!found) { | |
904 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
905 | } else { | |
906 | kernel_wait_quiescent(kernel_tracer_fd); | |
907 | ret = LTTCOMM_OK; | |
908 | } | |
909 | break; | |
910 | } | |
f3ed775e | 911 | case LTTNG_KERNEL_ENABLE_EVENT: |
894be886 | 912 | { |
f3ed775e DG |
913 | int found = 0; |
914 | struct ltt_kernel_channel *chan; | |
915 | ||
894be886 DG |
916 | /* Setup lttng message with no payload */ |
917 | ret = setup_lttng_msg(cmd_ctx, 0); | |
918 | if (ret < 0) { | |
919 | goto setup_error; | |
920 | } | |
921 | ||
f3ed775e DG |
922 | /* Get channel by name and create event for that channel */ |
923 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
924 | if (strcmp(cmd_ctx->lsm->u.enable.channel_name, chan->channel->name) == 0) { | |
925 | DBG("Creating kernel event %s for channel %s.", | |
926 | cmd_ctx->lsm->u.enable.event.name, cmd_ctx->lsm->u.enable.channel_name); | |
927 | ||
928 | ret = kernel_create_event(chan, &cmd_ctx->lsm->u.enable.event); | |
929 | if (ret < 0) { | |
930 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
931 | goto error; | |
932 | } | |
933 | found = 1; | |
934 | break; | |
935 | } | |
f34daff7 DG |
936 | } |
937 | ||
f3ed775e DG |
938 | if (!found) { |
939 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
940 | } else { | |
941 | kernel_wait_quiescent(kernel_tracer_fd); | |
942 | ret = LTTCOMM_OK; | |
943 | } | |
894be886 DG |
944 | break; |
945 | } | |
f3ed775e | 946 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: |
33a2b854 | 947 | { |
f3ed775e | 948 | int pos, size, found; |
33a2b854 | 949 | char *event_list, *event, *ptr; |
f3ed775e DG |
950 | struct ltt_kernel_channel *chan; |
951 | struct lttng_event ev; | |
33a2b854 DG |
952 | |
953 | /* Setup lttng message with no payload */ | |
954 | ret = setup_lttng_msg(cmd_ctx, 0); | |
955 | if (ret < 0) { | |
956 | goto setup_error; | |
957 | } | |
958 | ||
959 | DBG("Enabling all kernel event"); | |
960 | ||
961 | size = kernel_list_events(kernel_tracer_fd, &event_list); | |
962 | if (size < 0) { | |
963 | ret = LTTCOMM_KERN_LIST_FAIL; | |
964 | goto error; | |
965 | } | |
966 | ||
f3ed775e DG |
967 | /* Get channel by name and create event for that channel */ |
968 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
969 | if (strcmp(cmd_ctx->lsm->u.enable.channel_name, chan->channel->name) == 0) { | |
970 | found = 1; | |
971 | break; | |
972 | } | |
973 | } | |
974 | ||
975 | if (!found) { | |
976 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
977 | goto error; | |
978 | } | |
979 | ||
33a2b854 DG |
980 | ptr = event_list; |
981 | while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { | |
f3ed775e DG |
982 | strncpy(ev.name, event, LTTNG_SYM_NAME_LEN); |
983 | /* Default event type for enable all */ | |
984 | ev.type = LTTNG_EVENT_TRACEPOINTS; | |
985 | /* Enable each single tracepoint event */ | |
986 | ret = kernel_create_event(chan, &ev); | |
33a2b854 DG |
987 | if (ret < 0) { |
988 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
989 | goto error; | |
990 | } | |
991 | /* Move pointer to the next line */ | |
992 | ptr += pos + 1; | |
993 | free(event); | |
994 | } | |
995 | ||
996 | free(event_list); | |
997 | ||
f3ed775e DG |
998 | /* Quiescent wait after event enable */ |
999 | kernel_wait_quiescent(kernel_tracer_fd); | |
33a2b854 DG |
1000 | ret = LTTCOMM_OK; |
1001 | break; | |
1002 | } | |
f3ed775e | 1003 | case LTTNG_KERNEL_LIST_EVENTS: |
2ef84c95 DG |
1004 | { |
1005 | char *event_list; | |
f3ed775e DG |
1006 | ssize_t size = 0; |
1007 | ||
1008 | DBG("Listing kernel events"); | |
2ef84c95 DG |
1009 | |
1010 | size = kernel_list_events(kernel_tracer_fd, &event_list); | |
1011 | if (size < 0) { | |
1012 | ret = LTTCOMM_KERN_LIST_FAIL; | |
1013 | goto error; | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Setup lttng message with payload size set to the event list size in | |
1018 | * bytes and then copy list into the llm payload. | |
1019 | */ | |
1020 | ret = setup_lttng_msg(cmd_ctx, size); | |
1021 | if (ret < 0) { | |
1022 | goto setup_error; | |
1023 | } | |
1024 | ||
1025 | /* Copy event list into message payload */ | |
1026 | memcpy(cmd_ctx->llm->payload, event_list, size); | |
1027 | ||
1028 | free(event_list); | |
1029 | ||
1030 | ret = LTTCOMM_OK; | |
1031 | break; | |
1032 | } | |
f3ed775e | 1033 | case LTTNG_START_TRACE: |
8c0faa1d DG |
1034 | { |
1035 | struct ltt_kernel_channel *chan; | |
f3ed775e | 1036 | |
8c0faa1d DG |
1037 | /* Setup lttng message with no payload */ |
1038 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1039 | if (ret < 0) { | |
1040 | goto setup_error; | |
1041 | } | |
1042 | ||
f3ed775e DG |
1043 | /* Kernel tracing */ |
1044 | if (cmd_ctx->session->kernel_session != NULL) { | |
1045 | if (cmd_ctx->session->kernel_session->metadata == NULL) { | |
1046 | DBG("Open kernel metadata"); | |
1047 | ret = kernel_open_metadata(cmd_ctx->session->kernel_session); | |
1048 | if (ret < 0) { | |
1049 | ret = LTTCOMM_KERN_META_FAIL; | |
1050 | goto error; | |
1051 | } | |
1052 | } | |
8c0faa1d | 1053 | |
f3ed775e DG |
1054 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { |
1055 | DBG("Opening kernel metadata stream"); | |
1056 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { | |
1057 | ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session); | |
1058 | if (ret < 0) { | |
1059 | ERR("Kernel create metadata stream failed"); | |
1060 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1061 | goto error; | |
1062 | } | |
1063 | } | |
1064 | } | |
8c0faa1d | 1065 | |
f3ed775e DG |
1066 | /* For each channel */ |
1067 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
1068 | if (chan->stream_count == 0) { | |
1069 | ret = kernel_open_channel_stream(chan); | |
1070 | if (ret < 0) { | |
1071 | ERR("Kernel create channel stream failed"); | |
1072 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1073 | goto error; | |
1074 | } | |
1075 | /* Update the stream global counter */ | |
1076 | cmd_ctx->session->kernel_session->stream_count_global += ret; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | DBG("Start kernel tracing"); | |
1081 | ret = kernel_start_session(cmd_ctx->session->kernel_session); | |
8c0faa1d | 1082 | if (ret < 0) { |
f3ed775e DG |
1083 | ERR("Kernel start session failed"); |
1084 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1085 | goto error; |
1086 | } | |
8c0faa1d | 1087 | |
f3ed775e DG |
1088 | ret = start_kernel_trace(cmd_ctx->session->kernel_session); |
1089 | if (ret < 0) { | |
1090 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1091 | goto error; |
1092 | } | |
8c0faa1d | 1093 | |
f3ed775e DG |
1094 | /* Quiescent wait after starting trace */ |
1095 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1096 | } |
1097 | ||
f3ed775e | 1098 | /* TODO: Start all UST traces */ |
8c0faa1d DG |
1099 | |
1100 | ret = LTTCOMM_OK; | |
1101 | break; | |
1102 | } | |
f3ed775e | 1103 | case LTTNG_STOP_TRACE: |
8c0faa1d | 1104 | { |
f3ed775e | 1105 | struct ltt_kernel_channel *chan; |
8c0faa1d DG |
1106 | /* Setup lttng message with no payload */ |
1107 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1108 | if (ret < 0) { | |
1109 | goto setup_error; | |
1110 | } | |
1111 | ||
f3ed775e DG |
1112 | /* Kernel tracer */ |
1113 | if (cmd_ctx->session->kernel_session != NULL) { | |
1114 | DBG("Stop kernel tracing"); | |
84291629 | 1115 | |
f3ed775e DG |
1116 | ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd); |
1117 | if (ret < 0) { | |
1118 | ERR("Kernel metadata flush failed"); | |
1119 | } | |
8c0faa1d | 1120 | |
f3ed775e DG |
1121 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { |
1122 | ret = kernel_flush_buffer(chan); | |
1123 | if (ret < 0) { | |
1124 | ERR("Kernel flush buffer error"); | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | ret = kernel_stop_session(cmd_ctx->session->kernel_session); | |
1129 | if (ret < 0) { | |
1130 | ERR("Kernel stop session failed"); | |
1131 | ret = LTTCOMM_KERN_STOP_FAIL; | |
1132 | goto error; | |
1133 | } | |
1134 | ||
1135 | /* Quiescent wait after stopping trace */ | |
1136 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1137 | } |
1138 | ||
f3ed775e | 1139 | /* TODO : User-space tracer */ |
8c0faa1d DG |
1140 | |
1141 | ret = LTTCOMM_OK; | |
1142 | break; | |
1143 | } | |
5e16da05 MD |
1144 | case LTTNG_CREATE_SESSION: |
1145 | { | |
5461b305 DG |
1146 | /* Setup lttng message with no payload */ |
1147 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1148 | if (ret < 0) { | |
1149 | goto setup_error; | |
1150 | } | |
1151 | ||
f3ed775e | 1152 | ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path); |
5e16da05 | 1153 | if (ret < 0) { |
f3ed775e | 1154 | if (ret == -EEXIST) { |
5e16da05 MD |
1155 | ret = LTTCOMM_EXIST_SESS; |
1156 | } else { | |
aaf97519 | 1157 | ret = LTTCOMM_FATAL; |
aaf97519 | 1158 | } |
5461b305 | 1159 | goto error; |
8028d920 | 1160 | } |
1657e9bb | 1161 | |
5461b305 | 1162 | ret = LTTCOMM_OK; |
5e16da05 MD |
1163 | break; |
1164 | } | |
1165 | case LTTNG_DESTROY_SESSION: | |
1166 | { | |
5461b305 DG |
1167 | /* Setup lttng message with no payload */ |
1168 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1169 | if (ret < 0) { | |
1170 | goto setup_error; | |
1171 | } | |
1172 | ||
f3ed775e DG |
1173 | /* Clean kernel session teardown */ |
1174 | teardown_kernel_session(cmd_ctx->session); | |
1175 | ||
1176 | ret = destroy_session(cmd_ctx->lsm->session_name); | |
5e16da05 | 1177 | if (ret < 0) { |
f3ed775e | 1178 | ret = LTTCOMM_FATAL; |
5461b305 | 1179 | goto error; |
5e16da05 | 1180 | } |
1657e9bb | 1181 | |
5461b305 DG |
1182 | ret = LTTCOMM_OK; |
1183 | break; | |
5e16da05 | 1184 | } |
f3ed775e | 1185 | /* |
5e16da05 MD |
1186 | case LTTNG_LIST_TRACES: |
1187 | { | |
5461b305 | 1188 | unsigned int trace_count; |
1657e9bb | 1189 | |
5461b305 | 1190 | trace_count = get_trace_count_per_session(cmd_ctx->session); |
5e16da05 MD |
1191 | if (trace_count == 0) { |
1192 | ret = LTTCOMM_NO_TRACE; | |
5461b305 | 1193 | goto error; |
1657e9bb | 1194 | } |
df0da139 | 1195 | |
5461b305 DG |
1196 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count); |
1197 | if (ret < 0) { | |
1198 | goto setup_error; | |
df0da139 | 1199 | } |
ca95a216 | 1200 | |
5461b305 DG |
1201 | get_traces_per_session(cmd_ctx->session, |
1202 | (struct lttng_trace *)(cmd_ctx->llm->payload)); | |
1203 | ||
1204 | ret = LTTCOMM_OK; | |
5e16da05 MD |
1205 | break; |
1206 | } | |
f3ed775e DG |
1207 | */ |
1208 | /* | |
5e16da05 MD |
1209 | case UST_CREATE_TRACE: |
1210 | { | |
5461b305 | 1211 | ret = setup_lttng_msg(cmd_ctx, 0); |
5e16da05 | 1212 | if (ret < 0) { |
5461b305 | 1213 | goto setup_error; |
fac6795d | 1214 | } |
ce3d728c | 1215 | |
5461b305 DG |
1216 | ret = ust_create_trace(cmd_ctx); |
1217 | if (ret < 0) { | |
f3ed775e | 1218 | goto error; |
5461b305 DG |
1219 | } |
1220 | break; | |
5e16da05 | 1221 | } |
f3ed775e DG |
1222 | */ |
1223 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 | 1224 | { |
5461b305 DG |
1225 | unsigned int app_count; |
1226 | ||
1227 | app_count = get_app_count(); | |
1228 | DBG("Traceable application count : %d", app_count); | |
5e16da05 MD |
1229 | if (app_count == 0) { |
1230 | ret = LTTCOMM_NO_APPS; | |
5461b305 | 1231 | goto error; |
ce3d728c | 1232 | } |
520ff687 | 1233 | |
5461b305 DG |
1234 | ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count); |
1235 | if (ret < 0) { | |
1236 | goto setup_error; | |
520ff687 | 1237 | } |
57167058 | 1238 | |
5461b305 | 1239 | get_app_list_pids((pid_t *)(cmd_ctx->llm->payload)); |
5e16da05 | 1240 | |
5461b305 | 1241 | ret = LTTCOMM_OK; |
5e16da05 MD |
1242 | break; |
1243 | } | |
f3ed775e | 1244 | /* |
5e16da05 MD |
1245 | case UST_START_TRACE: |
1246 | { | |
5461b305 DG |
1247 | ret = setup_lttng_msg(cmd_ctx, 0); |
1248 | if (ret < 0) { | |
1249 | goto setup_error; | |
1250 | } | |
ca95a216 | 1251 | |
5461b305 DG |
1252 | ret = ust_start_trace(cmd_ctx); |
1253 | if (ret < 0) { | |
1254 | goto setup_error; | |
1255 | } | |
1256 | break; | |
5e16da05 MD |
1257 | } |
1258 | case UST_STOP_TRACE: | |
1259 | { | |
5461b305 DG |
1260 | ret = setup_lttng_msg(cmd_ctx, 0); |
1261 | if (ret < 0) { | |
1262 | goto setup_error; | |
1263 | } | |
ca95a216 | 1264 | |
5461b305 DG |
1265 | ret = ust_stop_trace(cmd_ctx); |
1266 | if (ret < 0) { | |
1267 | goto setup_error; | |
1268 | } | |
1269 | break; | |
5e16da05 | 1270 | } |
f3ed775e | 1271 | */ |
5e16da05 MD |
1272 | case LTTNG_LIST_SESSIONS: |
1273 | { | |
5461b305 DG |
1274 | unsigned int session_count; |
1275 | ||
1276 | session_count = get_session_count(); | |
5e16da05 | 1277 | if (session_count == 0) { |
f3ed775e | 1278 | ret = LTTCOMM_NO_SESSION; |
5461b305 | 1279 | goto error; |
57167058 | 1280 | } |
5e16da05 | 1281 | |
5461b305 DG |
1282 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count); |
1283 | if (ret < 0) { | |
1284 | goto setup_error; | |
e065084a | 1285 | } |
5e16da05 | 1286 | |
5461b305 | 1287 | get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload)); |
5e16da05 | 1288 | |
5461b305 | 1289 | ret = LTTCOMM_OK; |
5e16da05 MD |
1290 | break; |
1291 | } | |
1292 | default: | |
1293 | /* Undefined command */ | |
5461b305 DG |
1294 | ret = setup_lttng_msg(cmd_ctx, 0); |
1295 | if (ret < 0) { | |
1296 | goto setup_error; | |
1297 | } | |
1298 | ||
5e16da05 | 1299 | ret = LTTCOMM_UND; |
5461b305 | 1300 | break; |
fac6795d DG |
1301 | } |
1302 | ||
5461b305 DG |
1303 | /* Set return code */ |
1304 | cmd_ctx->llm->ret_code = ret; | |
ca95a216 | 1305 | |
e065084a DG |
1306 | return ret; |
1307 | ||
5461b305 | 1308 | error: |
5461b305 DG |
1309 | if (cmd_ctx->llm == NULL) { |
1310 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 1311 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
1312 | goto setup_error; |
1313 | } | |
1314 | } | |
e065084a | 1315 | /* Notify client of error */ |
5461b305 | 1316 | cmd_ctx->llm->ret_code = ret; |
e065084a | 1317 | |
5461b305 | 1318 | setup_error: |
8028d920 | 1319 | return ret; |
fac6795d DG |
1320 | } |
1321 | ||
1d4b027a DG |
1322 | /* |
1323 | * thread_manage_clients | |
1324 | * | |
1325 | * This thread manage all clients request using the unix | |
1326 | * client socket for communication. | |
1327 | */ | |
1328 | static void *thread_manage_clients(void *data) | |
1329 | { | |
1330 | int sock, ret; | |
1331 | struct command_ctx *cmd_ctx; | |
1332 | ||
1333 | DBG("[thread] Manage client started"); | |
1334 | ||
1335 | ret = lttcomm_listen_unix_sock(client_sock); | |
1336 | if (ret < 0) { | |
1337 | goto error; | |
1338 | } | |
1339 | ||
1340 | /* Notify parent pid that we are ready | |
1341 | * to accept command for client side. | |
1342 | */ | |
1343 | if (opt_sig_parent) { | |
1344 | kill(ppid, SIGCHLD); | |
1345 | } | |
1346 | ||
1347 | while (1) { | |
1348 | /* Blocking call, waiting for transmission */ | |
1349 | DBG("Accepting client command ..."); | |
1350 | sock = lttcomm_accept_unix_sock(client_sock); | |
1351 | if (sock < 0) { | |
1352 | goto error; | |
1353 | } | |
1354 | ||
1355 | /* Allocate context command to process the client request */ | |
1356 | cmd_ctx = malloc(sizeof(struct command_ctx)); | |
1357 | ||
1358 | /* Allocate data buffer for reception */ | |
1359 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); | |
1360 | cmd_ctx->llm = NULL; | |
1361 | cmd_ctx->session = NULL; | |
1362 | ||
1363 | /* | |
1364 | * Data is received from the lttng client. The struct | |
1365 | * lttcomm_session_msg (lsm) contains the command and data request of | |
1366 | * the client. | |
1367 | */ | |
1368 | DBG("Receiving data from client ..."); | |
1369 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg)); | |
1370 | if (ret <= 0) { | |
1371 | continue; | |
1372 | } | |
1373 | ||
f7776ea7 DG |
1374 | // TODO: Validate cmd_ctx including sanity check for security purpose. |
1375 | ||
1d4b027a DG |
1376 | /* |
1377 | * This function dispatch the work to the kernel or userspace tracer | |
1378 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
1379 | * informations for the client. The command context struct contains | |
1380 | * everything this function may needs. | |
1381 | */ | |
1382 | ret = process_client_msg(cmd_ctx); | |
1383 | if (ret < 0) { | |
1384 | /* TODO: Inform client somehow of the fatal error. At this point, | |
1385 | * ret < 0 means that a malloc failed (ENOMEM). */ | |
1386 | /* Error detected but still accept command */ | |
1387 | clean_command_ctx(cmd_ctx); | |
1388 | continue; | |
1389 | } | |
1390 | ||
1391 | DBG("Sending response (size: %d, retcode: %d)", | |
1392 | cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code); | |
1393 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); | |
1394 | if (ret < 0) { | |
1395 | ERR("Failed to send data back to client"); | |
1396 | } | |
1397 | ||
1398 | clean_command_ctx(cmd_ctx); | |
d6e4fca4 DG |
1399 | |
1400 | /* End of transmission */ | |
1401 | close(sock); | |
1d4b027a DG |
1402 | } |
1403 | ||
1404 | error: | |
1405 | return NULL; | |
1406 | } | |
1407 | ||
1408 | ||
fac6795d DG |
1409 | /* |
1410 | * usage function on stderr | |
1411 | */ | |
1412 | static void usage(void) | |
1413 | { | |
b716ce68 | 1414 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
1415 | fprintf(stderr, " -h, --help Display this usage.\n"); |
1416 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
1417 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
1418 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
1419 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
1420 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
1421 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
1422 | fprintf(stderr, " -V, --version Show version number.\n"); | |
1423 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
1424 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
1425 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
fac6795d DG |
1426 | } |
1427 | ||
1428 | /* | |
1429 | * daemon argument parsing | |
1430 | */ | |
1431 | static int parse_args(int argc, char **argv) | |
1432 | { | |
1433 | int c; | |
1434 | ||
1435 | static struct option long_options[] = { | |
1436 | { "client-sock", 1, 0, 'c' }, | |
1437 | { "apps-sock", 1, 0, 'a' }, | |
d6f42150 DG |
1438 | { "kconsumerd-cmd-sock", 1, 0, 0 }, |
1439 | { "kconsumerd-err-sock", 1, 0, 0 }, | |
fac6795d | 1440 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 1441 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
1442 | { "help", 0, 0, 'h' }, |
1443 | { "group", 1, 0, 'g' }, | |
1444 | { "version", 0, 0, 'V' }, | |
75462a81 | 1445 | { "quiet", 0, 0, 'q' }, |
3f9947db | 1446 | { "verbose", 0, 0, 'v' }, |
fac6795d DG |
1447 | { NULL, 0, 0, 0 } |
1448 | }; | |
1449 | ||
1450 | while (1) { | |
1451 | int option_index = 0; | |
d6f42150 | 1452 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index); |
fac6795d DG |
1453 | if (c == -1) { |
1454 | break; | |
1455 | } | |
1456 | ||
1457 | switch (c) { | |
1458 | case 0: | |
1459 | fprintf(stderr, "option %s", long_options[option_index].name); | |
1460 | if (optarg) { | |
1461 | fprintf(stderr, " with arg %s\n", optarg); | |
1462 | } | |
1463 | break; | |
b716ce68 | 1464 | case 'c': |
fac6795d DG |
1465 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
1466 | break; | |
1467 | case 'a': | |
1468 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
1469 | break; | |
1470 | case 'd': | |
1471 | opt_daemon = 1; | |
1472 | break; | |
1473 | case 'g': | |
1474 | opt_tracing_group = strdup(optarg); | |
1475 | break; | |
1476 | case 'h': | |
1477 | usage(); | |
1478 | exit(EXIT_FAILURE); | |
1479 | case 'V': | |
1480 | fprintf(stdout, "%s\n", VERSION); | |
1481 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
1482 | case 'S': |
1483 | opt_sig_parent = 1; | |
1484 | break; | |
d6f42150 DG |
1485 | case 'E': |
1486 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg); | |
1487 | break; | |
1488 | case 'C': | |
1489 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
1490 | break; | |
75462a81 DG |
1491 | case 'q': |
1492 | opt_quiet = 1; | |
1493 | break; | |
3f9947db DG |
1494 | case 'v': |
1495 | opt_verbose = 1; | |
1496 | break; | |
fac6795d DG |
1497 | default: |
1498 | /* Unknown option or other error. | |
1499 | * Error is printed by getopt, just return */ | |
1500 | return -1; | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | return 0; | |
1505 | } | |
1506 | ||
1507 | /* | |
1508 | * init_daemon_socket | |
1509 | * | |
1510 | * Creates the two needed socket by the daemon. | |
d6f42150 DG |
1511 | * apps_sock - The communication socket for all UST apps. |
1512 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d DG |
1513 | */ |
1514 | static int init_daemon_socket() | |
1515 | { | |
1516 | int ret = 0; | |
1517 | mode_t old_umask; | |
1518 | ||
1519 | old_umask = umask(0); | |
1520 | ||
1521 | /* Create client tool unix socket */ | |
d6f42150 DG |
1522 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
1523 | if (client_sock < 0) { | |
1524 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
1525 | ret = -1; |
1526 | goto end; | |
1527 | } | |
1528 | ||
1529 | /* File permission MUST be 660 */ | |
1530 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1531 | if (ret < 0) { | |
d6f42150 | 1532 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
1533 | perror("chmod"); |
1534 | goto end; | |
1535 | } | |
1536 | ||
1537 | /* Create the application unix socket */ | |
d6f42150 DG |
1538 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
1539 | if (apps_sock < 0) { | |
1540 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
1541 | ret = -1; |
1542 | goto end; | |
1543 | } | |
1544 | ||
d6f42150 | 1545 | /* File permission MUST be 666 */ |
fac6795d DG |
1546 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
1547 | if (ret < 0) { | |
d6f42150 | 1548 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
1549 | perror("chmod"); |
1550 | goto end; | |
1551 | } | |
1552 | ||
1553 | end: | |
1554 | umask(old_umask); | |
1555 | return ret; | |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * check_existing_daemon | |
1560 | * | |
1561 | * Check if the global socket is available. | |
62bd06d8 | 1562 | * If yes, error is returned. |
fac6795d DG |
1563 | */ |
1564 | static int check_existing_daemon() | |
1565 | { | |
1566 | int ret; | |
1567 | ||
1568 | ret = access(client_unix_sock_path, F_OK); | |
1569 | if (ret == 0) { | |
1570 | ret = access(apps_unix_sock_path, F_OK); | |
1571 | } | |
1572 | ||
1573 | return ret; | |
1574 | } | |
1575 | ||
1576 | /* | |
62bd06d8 | 1577 | * get_home_dir |
fac6795d | 1578 | * |
62bd06d8 DG |
1579 | * Return pointer to home directory path using |
1580 | * the env variable HOME. | |
fac6795d | 1581 | * |
62bd06d8 | 1582 | * Default : /tmp |
fac6795d DG |
1583 | */ |
1584 | static const char *get_home_dir(void) | |
1585 | { | |
1586 | const char *home_path; | |
1587 | ||
686204ab | 1588 | if ((home_path = (const char *) getenv("HOME")) == NULL) { |
fac6795d DG |
1589 | home_path = default_home_dir; |
1590 | } | |
1591 | ||
1592 | return home_path; | |
1593 | } | |
1594 | ||
1595 | /* | |
d6f42150 | 1596 | * set_permissions |
fac6795d | 1597 | * |
5e16da05 MD |
1598 | * Set the tracing group gid onto the client socket. |
1599 | * | |
1600 | * Race window between mkdir and chown is OK because we are going from | |
1601 | * less permissive (root.root) to more permissive (root.tracing). | |
fac6795d | 1602 | */ |
d6f42150 | 1603 | static int set_permissions(void) |
fac6795d DG |
1604 | { |
1605 | int ret; | |
1606 | struct group *grp; | |
1607 | ||
1608 | /* Decide which group name to use */ | |
1609 | (opt_tracing_group != NULL) ? | |
1610 | (grp = getgrnam(opt_tracing_group)) : | |
1611 | (grp = getgrnam(default_tracing_group)); | |
1612 | ||
1613 | if (grp == NULL) { | |
75462a81 | 1614 | ERR("Missing tracing group. Aborting execution.\n"); |
fac6795d DG |
1615 | ret = -1; |
1616 | goto end; | |
1617 | } | |
1618 | ||
d6f42150 DG |
1619 | /* Set lttng run dir */ |
1620 | ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); | |
1621 | if (ret < 0) { | |
1622 | ERR("Unable to set group on " LTTNG_RUNDIR); | |
1623 | perror("chown"); | |
1624 | } | |
1625 | ||
1626 | /* lttng client socket path */ | |
fac6795d DG |
1627 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
1628 | if (ret < 0) { | |
d6f42150 DG |
1629 | ERR("Unable to set group on %s", client_unix_sock_path); |
1630 | perror("chown"); | |
1631 | } | |
1632 | ||
1633 | /* kconsumerd error socket path */ | |
1634 | ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); | |
1635 | if (ret < 0) { | |
1636 | ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); | |
fac6795d DG |
1637 | perror("chown"); |
1638 | } | |
1639 | ||
d6f42150 | 1640 | DBG("All permissions are set"); |
e07ae692 | 1641 | |
fac6795d DG |
1642 | end: |
1643 | return ret; | |
1644 | } | |
1645 | ||
d6f42150 DG |
1646 | /* |
1647 | * create_lttng_rundir | |
1648 | * | |
1649 | * Create the lttng run directory needed for all | |
1650 | * global sockets and pipe. | |
1651 | */ | |
1652 | static int create_lttng_rundir(void) | |
1653 | { | |
1654 | int ret; | |
1655 | ||
1656 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); | |
1657 | if (ret < 0) { | |
b1f11e69 DG |
1658 | if (errno != EEXIST) { |
1659 | ERR("Unable to create " LTTNG_RUNDIR); | |
1660 | goto error; | |
1661 | } else { | |
1662 | ret = 0; | |
1663 | } | |
d6f42150 DG |
1664 | } |
1665 | ||
1666 | error: | |
1667 | return ret; | |
1668 | } | |
1669 | ||
1670 | /* | |
1671 | * set_kconsumerd_sockets | |
1672 | * | |
1673 | * Setup sockets and directory needed by the kconsumerd | |
1674 | * communication with the session daemon. | |
1675 | */ | |
1676 | static int set_kconsumerd_sockets(void) | |
1677 | { | |
1678 | int ret; | |
1679 | ||
1680 | if (strlen(kconsumerd_err_unix_sock_path) == 0) { | |
1681 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH); | |
1682 | } | |
1683 | ||
1684 | if (strlen(kconsumerd_cmd_unix_sock_path) == 0) { | |
1685 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH); | |
1686 | } | |
1687 | ||
1688 | ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG); | |
1689 | if (ret < 0) { | |
6beb2242 DG |
1690 | if (errno != EEXIST) { |
1691 | ERR("Failed to create " KCONSUMERD_PATH); | |
1692 | goto error; | |
1693 | } | |
1694 | ret = 0; | |
d6f42150 DG |
1695 | } |
1696 | ||
1697 | /* Create the kconsumerd error unix socket */ | |
1698 | kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path); | |
1699 | if (kconsumerd_err_sock < 0) { | |
1700 | ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path); | |
1701 | ret = -1; | |
1702 | goto error; | |
1703 | } | |
1704 | ||
1705 | /* File permission MUST be 660 */ | |
1706 | ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1707 | if (ret < 0) { | |
1708 | ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path); | |
1709 | perror("chmod"); | |
1710 | goto error; | |
1711 | } | |
1712 | ||
1713 | error: | |
1714 | return ret; | |
1715 | } | |
1716 | ||
fac6795d | 1717 | /* |
62bd06d8 | 1718 | * sighandler |
fac6795d | 1719 | * |
62bd06d8 | 1720 | * Signal handler for the daemon |
fac6795d DG |
1721 | */ |
1722 | static void sighandler(int sig) | |
1723 | { | |
1724 | switch (sig) { | |
1725 | case SIGPIPE: | |
e07ae692 | 1726 | DBG("SIGPIPE catched"); |
87378cf5 | 1727 | return; |
fac6795d | 1728 | case SIGINT: |
e07ae692 DG |
1729 | DBG("SIGINT catched"); |
1730 | cleanup(); | |
1731 | break; | |
fac6795d | 1732 | case SIGTERM: |
e07ae692 | 1733 | DBG("SIGTERM catched"); |
fac6795d | 1734 | cleanup(); |
686204ab | 1735 | break; |
fac6795d DG |
1736 | default: |
1737 | break; | |
1738 | } | |
1739 | ||
1740 | exit(EXIT_SUCCESS); | |
1741 | } | |
1742 | ||
1743 | /* | |
1d4b027a | 1744 | * set_signal_handler |
fac6795d | 1745 | * |
1d4b027a DG |
1746 | * Setup signal handler for : |
1747 | * SIGINT, SIGTERM, SIGPIPE | |
fac6795d | 1748 | */ |
1d4b027a | 1749 | static int set_signal_handler(void) |
fac6795d | 1750 | { |
1d4b027a DG |
1751 | int ret = 0; |
1752 | struct sigaction sa; | |
1753 | sigset_t sigset; | |
fac6795d | 1754 | |
1d4b027a DG |
1755 | if ((ret = sigemptyset(&sigset)) < 0) { |
1756 | perror("sigemptyset"); | |
1757 | return ret; | |
1758 | } | |
d6f42150 | 1759 | |
1d4b027a DG |
1760 | sa.sa_handler = sighandler; |
1761 | sa.sa_mask = sigset; | |
1762 | sa.sa_flags = 0; | |
1763 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
1764 | perror("sigaction"); | |
1765 | return ret; | |
d6f42150 DG |
1766 | } |
1767 | ||
1d4b027a DG |
1768 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
1769 | perror("sigaction"); | |
1770 | return ret; | |
d6f42150 | 1771 | } |
aaf26714 | 1772 | |
1d4b027a DG |
1773 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
1774 | perror("sigaction"); | |
1775 | return ret; | |
8c0faa1d DG |
1776 | } |
1777 | ||
1d4b027a DG |
1778 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
1779 | ||
1780 | return ret; | |
fac6795d DG |
1781 | } |
1782 | ||
f3ed775e DG |
1783 | /* |
1784 | * set_ulimit | |
1785 | * | |
1786 | * Set open files limit to unlimited. This daemon can open a large number of | |
1787 | * file descriptors in order to consumer multiple kernel traces. | |
1788 | */ | |
1789 | static void set_ulimit(void) | |
1790 | { | |
1791 | int ret; | |
1792 | struct rlimit lim; | |
1793 | ||
1794 | lim.rlim_cur = 65535; | |
1795 | lim.rlim_max = 65535; | |
1796 | ||
1797 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
1798 | if (ret < 0) { | |
1799 | perror("failed to set open files limit"); | |
1800 | } | |
1801 | } | |
1802 | ||
fac6795d DG |
1803 | /* |
1804 | * main | |
1805 | */ | |
1806 | int main(int argc, char **argv) | |
1807 | { | |
fac6795d DG |
1808 | int ret = 0; |
1809 | void *status; | |
fac6795d DG |
1810 | |
1811 | /* Parse arguments */ | |
1812 | progname = argv[0]; | |
1813 | if ((ret = parse_args(argc, argv) < 0)) { | |
1814 | goto error; | |
1815 | } | |
1816 | ||
1817 | /* Daemonize */ | |
1818 | if (opt_daemon) { | |
53094c05 DG |
1819 | ret = daemon(0, 0); |
1820 | if (ret < 0) { | |
1821 | perror("daemon"); | |
1822 | goto error; | |
1823 | } | |
fac6795d DG |
1824 | } |
1825 | ||
1826 | /* Check if daemon is UID = 0 */ | |
1827 | is_root = !getuid(); | |
1828 | ||
1829 | /* Set all sockets path */ | |
1830 | if (is_root) { | |
d6f42150 DG |
1831 | ret = create_lttng_rundir(); |
1832 | if (ret < 0) { | |
1833 | goto error; | |
1834 | } | |
1835 | ||
fac6795d | 1836 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
1837 | snprintf(apps_unix_sock_path, PATH_MAX, |
1838 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
1839 | } |
1840 | ||
1841 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
1842 | snprintf(client_unix_sock_path, PATH_MAX, |
1843 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
1844 | } | |
1845 | ||
1846 | ret = set_kconsumerd_sockets(); | |
1847 | if (ret < 0) { | |
1848 | goto error; | |
fac6795d | 1849 | } |
20fe2104 DG |
1850 | |
1851 | /* Setup kernel tracer */ | |
1852 | init_kernel_tracer(); | |
f3ed775e DG |
1853 | |
1854 | /* Set ulimit for open files */ | |
1855 | set_ulimit(); | |
fac6795d DG |
1856 | } else { |
1857 | if (strlen(apps_unix_sock_path) == 0) { | |
d6f42150 DG |
1858 | snprintf(apps_unix_sock_path, PATH_MAX, |
1859 | DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir()); | |
fac6795d DG |
1860 | } |
1861 | ||
1862 | /* Set the cli tool unix socket path */ | |
1863 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
1864 | snprintf(client_unix_sock_path, PATH_MAX, |
1865 | DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir()); | |
fac6795d DG |
1866 | } |
1867 | } | |
1868 | ||
847177cd DG |
1869 | DBG("Client socket path %s", client_unix_sock_path); |
1870 | DBG("Application socket path %s", apps_unix_sock_path); | |
1871 | ||
fac6795d DG |
1872 | /* See if daemon already exist. If any of the two |
1873 | * socket needed by the daemon are present, this test fails | |
1874 | */ | |
1875 | if ((ret = check_existing_daemon()) == 0) { | |
75462a81 | 1876 | ERR("Already running daemon.\n"); |
ab118b20 | 1877 | /* We do not goto error because we must not |
d6f42150 | 1878 | * cleanup() because a daemon is already running. |
ab118b20 | 1879 | */ |
5e16da05 | 1880 | exit(EXIT_FAILURE); |
fac6795d DG |
1881 | } |
1882 | ||
1883 | if (set_signal_handler() < 0) { | |
1884 | goto error; | |
1885 | } | |
1886 | ||
d6f42150 | 1887 | /* Setup the needed unix socket */ |
fac6795d DG |
1888 | if (init_daemon_socket() < 0) { |
1889 | goto error; | |
1890 | } | |
1891 | ||
1892 | /* Set credentials to socket */ | |
d6f42150 | 1893 | if (is_root && (set_permissions() < 0)) { |
fac6795d DG |
1894 | goto error; |
1895 | } | |
1896 | ||
5b8719f5 DG |
1897 | /* Get parent pid if -S, --sig-parent is specified. */ |
1898 | if (opt_sig_parent) { | |
1899 | ppid = getppid(); | |
1900 | } | |
1901 | ||
fac6795d DG |
1902 | while (1) { |
1903 | /* Create thread to manage the client socket */ | |
1d4b027a | 1904 | ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); |
fac6795d DG |
1905 | if (ret != 0) { |
1906 | perror("pthread_create"); | |
1907 | goto error; | |
1908 | } | |
1909 | ||
1910 | /* Create thread to manage application socket */ | |
1d4b027a | 1911 | ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); |
fac6795d DG |
1912 | if (ret != 0) { |
1913 | perror("pthread_create"); | |
1914 | goto error; | |
1915 | } | |
1916 | ||
1d4b027a DG |
1917 | ret = pthread_join(client_thread, &status); |
1918 | if (ret != 0) { | |
1919 | perror("pthread_join"); | |
1920 | goto error; | |
fac6795d DG |
1921 | } |
1922 | } | |
1923 | ||
1924 | cleanup(); | |
5e16da05 | 1925 | exit(EXIT_SUCCESS); |
fac6795d DG |
1926 | |
1927 | error: | |
1928 | cleanup(); | |
5e16da05 | 1929 | exit(EXIT_FAILURE); |
fac6795d | 1930 | } |