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