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> | |
b73401da | 31 | #include <sys/mount.h> |
fac6795d DG |
32 | #include <sys/shm.h> |
33 | #include <sys/socket.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/types.h> | |
f3ed775e DG |
36 | #include <sys/time.h> |
37 | #include <sys/resource.h> | |
fac6795d DG |
38 | #include <unistd.h> |
39 | ||
40 | #include <urcu/list.h> /* URCU list library (-lurcu) */ | |
5b97ec60 | 41 | #include <lttng/lttng.h> |
fac6795d DG |
42 | |
43 | #include "liblttsessiondcomm.h" | |
44 | #include "ltt-sessiond.h" | |
75462a81 | 45 | #include "lttngerr.h" |
20fe2104 | 46 | #include "kernel-ctl.h" |
9e78d6ae | 47 | #include "ust-ctl.h" |
5b74c7b1 | 48 | #include "session.h" |
91d76f53 | 49 | #include "traceable-app.h" |
5dc18550 | 50 | #include "lttng-kconsumerd.h" |
62d3069f | 51 | #include "libustctl.h" |
8e68d1c8 | 52 | #include "utils.h" |
fac6795d | 53 | |
5e16da05 MD |
54 | /* |
55 | * TODO: | |
56 | * teardown: signal SIGTERM handler -> write into pipe. Threads waits | |
57 | * with epoll on pipe and on other pipes/sockets for commands. Main | |
58 | * simply waits on pthread join. | |
59 | */ | |
60 | ||
75462a81 | 61 | /* Const values */ |
686204ab | 62 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 63 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
64 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
65 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
66 | ||
fac6795d | 67 | /* Variables */ |
1d4b027a DG |
68 | int opt_verbose; /* Not static for lttngerr.h */ |
69 | int opt_quiet; /* Not static for lttngerr.h */ | |
fac6795d DG |
70 | const char *progname; |
71 | const char *opt_tracing_group; | |
5b8719f5 | 72 | static int opt_sig_parent; |
fac6795d DG |
73 | static int opt_daemon; |
74 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
1d4b027a DG |
75 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
76 | static pid_t kconsumerd_pid; | |
fac6795d | 77 | |
d6f42150 DG |
78 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
79 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
80 | static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */ | |
81 | static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */ | |
fac6795d | 82 | |
1d4b027a | 83 | /* Sockets and FDs */ |
d6f42150 DG |
84 | static int client_sock; |
85 | static int apps_sock; | |
86 | static int kconsumerd_err_sock; | |
8c0faa1d | 87 | static int kconsumerd_cmd_sock; |
20fe2104 | 88 | static int kernel_tracer_fd; |
1d4b027a DG |
89 | |
90 | /* Pthread, Mutexes and Semaphores */ | |
8c0faa1d | 91 | static pthread_t kconsumerd_thread; |
1d4b027a DG |
92 | static pthread_t apps_thread; |
93 | static pthread_t client_thread; | |
8c0faa1d DG |
94 | static sem_t kconsumerd_sem; |
95 | ||
1d4b027a | 96 | static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */ |
fac6795d | 97 | |
fac6795d | 98 | /* |
1d4b027a | 99 | * teardown_kernel_session |
fac6795d | 100 | * |
1d4b027a DG |
101 | * Complete teardown of a kernel session. This free all data structure |
102 | * related to a kernel session and update counter. | |
fac6795d | 103 | */ |
1d4b027a | 104 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 105 | { |
1d4b027a DG |
106 | if (session->kernel_session != NULL) { |
107 | DBG("Tearing down kernel session"); | |
c363b55d | 108 | trace_destroy_kernel_session(session->kernel_session); |
1d4b027a DG |
109 | /* Extra precaution */ |
110 | session->kernel_session = NULL; | |
111 | /* Decrement session count */ | |
112 | session->kern_session_count--; | |
fac6795d | 113 | } |
fac6795d DG |
114 | } |
115 | ||
116 | /* | |
1d4b027a | 117 | * cleanup |
fac6795d | 118 | * |
1d4b027a | 119 | * Cleanup the daemon on exit |
fac6795d | 120 | */ |
1d4b027a | 121 | static void cleanup() |
fac6795d | 122 | { |
1d4b027a DG |
123 | int ret; |
124 | char *cmd; | |
125 | struct ltt_session *sess; | |
fac6795d | 126 | |
1d4b027a | 127 | DBG("Cleaning up"); |
e07ae692 | 128 | |
1d4b027a DG |
129 | /* <fun> */ |
130 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); | |
131 | MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); | |
132 | /* </fun> */ | |
fac6795d | 133 | |
1d4b027a DG |
134 | /* Stopping all threads */ |
135 | DBG("Terminating all threads"); | |
136 | pthread_cancel(client_thread); | |
137 | pthread_cancel(apps_thread); | |
138 | if (kconsumerd_pid != 0) { | |
139 | pthread_cancel(kconsumerd_thread); | |
5b8719f5 DG |
140 | } |
141 | ||
1d4b027a DG |
142 | DBG("Unlinking all unix socket"); |
143 | unlink(client_unix_sock_path); | |
144 | unlink(apps_unix_sock_path); | |
145 | unlink(kconsumerd_err_unix_sock_path); | |
fac6795d | 146 | |
1d4b027a DG |
147 | DBG("Removing %s directory", LTTNG_RUNDIR); |
148 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); | |
149 | if (ret < 0) { | |
150 | ERR("asprintf failed. Something is really wrong!"); | |
151 | } | |
5461b305 | 152 | |
1d4b027a DG |
153 | /* Remove lttng run directory */ |
154 | ret = system(cmd); | |
155 | if (ret < 0) { | |
156 | ERR("Unable to clean " LTTNG_RUNDIR); | |
157 | } | |
5461b305 | 158 | |
1d4b027a DG |
159 | DBG("Cleaning up all session"); |
160 | /* Cleanup ALL session */ | |
161 | cds_list_for_each_entry(sess, <t_session_list.head, list) { | |
162 | teardown_kernel_session(sess); | |
163 | // TODO complete session cleanup (including UST) | |
fac6795d DG |
164 | } |
165 | ||
f3ed775e | 166 | DBG("Closing kernel fd"); |
1d4b027a | 167 | close(kernel_tracer_fd); |
fac6795d DG |
168 | } |
169 | ||
e065084a DG |
170 | /* |
171 | * send_unix_sock | |
172 | * | |
173 | * Send data on a unix socket using the liblttsessiondcomm API. | |
174 | * | |
175 | * Return lttcomm error code. | |
176 | */ | |
177 | static int send_unix_sock(int sock, void *buf, size_t len) | |
178 | { | |
179 | /* Check valid length */ | |
180 | if (len <= 0) { | |
181 | return -1; | |
182 | } | |
183 | ||
184 | return lttcomm_send_unix_sock(sock, buf, len); | |
185 | } | |
186 | ||
5461b305 DG |
187 | /* |
188 | * clean_command_ctx | |
189 | * | |
190 | * Free memory of a command context structure. | |
191 | */ | |
192 | static void clean_command_ctx(struct command_ctx *cmd_ctx) | |
193 | { | |
194 | DBG("Clean command context structure %p", cmd_ctx); | |
195 | if (cmd_ctx) { | |
196 | if (cmd_ctx->llm) { | |
197 | free(cmd_ctx->llm); | |
198 | } | |
199 | if (cmd_ctx->lsm) { | |
200 | free(cmd_ctx->lsm); | |
201 | } | |
202 | free(cmd_ctx); | |
203 | cmd_ctx = NULL; | |
204 | } | |
205 | } | |
206 | ||
f3ed775e DG |
207 | /* |
208 | * send_kconsumerd_fds | |
209 | * | |
210 | * Send all stream fds of the kernel session to the consumer. | |
211 | */ | |
212 | static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session) | |
213 | { | |
214 | int ret; | |
215 | size_t nb_fd; | |
216 | struct ltt_kernel_stream *stream; | |
217 | struct ltt_kernel_channel *chan; | |
218 | struct lttcomm_kconsumerd_header lkh; | |
219 | struct lttcomm_kconsumerd_msg lkm; | |
220 | ||
221 | nb_fd = session->stream_count_global; | |
222 | ||
223 | /* Setup header */ | |
224 | lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg); | |
225 | lkh.cmd_type = ADD_STREAM; | |
226 | ||
227 | DBG("Sending kconsumerd header"); | |
228 | ||
229 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); | |
230 | if (ret < 0) { | |
231 | perror("send kconsumerd header"); | |
232 | goto error; | |
233 | } | |
234 | ||
235 | DBG("Sending metadata stream fd"); | |
236 | ||
237 | /* Send metadata stream fd first */ | |
238 | lkm.fd = session->metadata_stream_fd; | |
239 | lkm.state = ACTIVE_FD; | |
240 | lkm.max_sb_size = session->metadata->conf->attr.subbuf_size; | |
241 | strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX); | |
242 | ||
243 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
244 | if (ret < 0) { | |
245 | perror("send kconsumerd fd"); | |
246 | goto error; | |
247 | } | |
248 | ||
249 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
250 | cds_list_for_each_entry(stream, &chan->stream_list.head, list) { | |
251 | lkm.fd = stream->fd; | |
252 | lkm.state = stream->state; | |
253 | lkm.max_sb_size = chan->channel->attr.subbuf_size; | |
254 | strncpy(lkm.path_name, stream->pathname, PATH_MAX); | |
255 | ||
256 | DBG("Sending fd %d to kconsumerd", lkm.fd); | |
257 | ||
258 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
259 | if (ret < 0) { | |
260 | perror("send kconsumerd fd"); | |
261 | goto error; | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
266 | DBG("Kconsumerd fds sent"); | |
267 | ||
268 | return 0; | |
269 | ||
270 | error: | |
271 | return ret; | |
272 | } | |
273 | ||
274 | /* | |
275 | * create_trace_dir | |
276 | * | |
277 | * Create the trace output directory. | |
278 | */ | |
279 | static int create_trace_dir(struct ltt_kernel_session *session) | |
280 | { | |
281 | int ret; | |
282 | struct ltt_kernel_channel *chan; | |
283 | ||
284 | /* Create all channel directories */ | |
285 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
286 | DBG("Creating trace directory at %s", chan->pathname); | |
8e68d1c8 | 287 | ret = mkdir_recursive(chan->pathname, S_IRWXU | S_IRWXG ); |
f3ed775e DG |
288 | if (ret < 0) { |
289 | if (ret != EEXIST) { | |
8e68d1c8 | 290 | ERR("Trace directory creation error"); |
f3ed775e DG |
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 | */ | |
2b1173d8 | 584 | execlp("ltt-kconsumerd", "ltt-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 | ||
b73401da DG |
645 | /* |
646 | * modprobe_kernel_modules | |
647 | */ | |
648 | static int modprobe_kernel_modules(void) | |
649 | { | |
650 | int ret = 0, i = 0; | |
651 | char modprobe[256]; | |
652 | ||
653 | while (kernel_modules_list[i] != NULL) { | |
654 | ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s", | |
655 | kernel_modules_list[i]); | |
656 | if (ret < 0) { | |
657 | perror("snprintf modprobe"); | |
658 | goto error; | |
659 | } | |
660 | ret = system(modprobe); | |
661 | if (ret < 0) { | |
662 | ERR("Unable to load module %s", kernel_modules_list[i]); | |
663 | } | |
664 | DBG("Modprobe successfully %s", kernel_modules_list[i]); | |
665 | i++; | |
666 | } | |
667 | ||
668 | error: | |
669 | return ret; | |
670 | } | |
671 | ||
672 | /* | |
673 | * mount_debugfs | |
674 | */ | |
675 | static int mount_debugfs(char *path) | |
676 | { | |
677 | int ret; | |
678 | char *type = "debugfs"; | |
679 | ||
680 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG); | |
681 | if (ret < 0) { | |
682 | goto error; | |
683 | } | |
684 | ||
685 | ret = mount(type, path, type, 0, NULL); | |
686 | if (ret < 0) { | |
687 | perror("mount debugfs"); | |
688 | goto error; | |
689 | } | |
690 | ||
691 | DBG("Mounted debugfs successfully at %s", path); | |
692 | ||
693 | error: | |
694 | return ret; | |
695 | } | |
696 | ||
8c0faa1d | 697 | /* |
f3ed775e | 698 | * init_kernel_tracer |
8c0faa1d | 699 | * |
f3ed775e | 700 | * Setup necessary data for kernel tracer action. |
8c0faa1d | 701 | */ |
f3ed775e | 702 | static void init_kernel_tracer(void) |
8c0faa1d | 703 | { |
b73401da DG |
704 | int ret; |
705 | char *proc_mounts = "/proc/mounts"; | |
706 | char line[256]; | |
707 | char *debugfs_path = NULL, *lttng_path; | |
708 | FILE *fp; | |
709 | ||
710 | /* Detect debugfs */ | |
711 | fp = fopen(proc_mounts, "r"); | |
712 | if (fp == NULL) { | |
713 | ERR("Unable to probe %s", proc_mounts); | |
714 | goto error; | |
715 | } | |
716 | ||
717 | while (fgets(line, sizeof(line), fp) != NULL) { | |
718 | if (strstr(line, "debugfs") != NULL) { | |
719 | /* Remove first string */ | |
720 | strtok(line, " "); | |
721 | /* Dup string here so we can reuse line later on */ | |
722 | debugfs_path = strdup(strtok(NULL, " ")); | |
723 | DBG("Got debugfs path : %s", debugfs_path); | |
724 | break; | |
725 | } | |
726 | } | |
727 | ||
728 | fclose(fp); | |
729 | ||
730 | /* Mount debugfs if needded */ | |
731 | if (debugfs_path == NULL) { | |
732 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); | |
733 | if (ret < 0) { | |
734 | perror("asprintf debugfs path"); | |
735 | goto error; | |
736 | } | |
737 | ret = mount_debugfs(debugfs_path); | |
738 | if (ret < 0) { | |
739 | goto error; | |
740 | } | |
741 | } | |
742 | ||
743 | /* Modprobe lttng kernel modules */ | |
744 | ret = modprobe_kernel_modules(); | |
745 | if (ret < 0) { | |
746 | goto error; | |
747 | } | |
748 | ||
749 | /* Setup lttng kernel path */ | |
750 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); | |
751 | if (ret < 0) { | |
752 | perror("asprintf lttng path"); | |
753 | goto error; | |
754 | } | |
755 | ||
756 | /* Open debugfs lttng */ | |
757 | kernel_tracer_fd = open(lttng_path, O_RDWR); | |
f3ed775e | 758 | if (kernel_tracer_fd < 0) { |
b73401da DG |
759 | DBG("Failed to open %s", lttng_path); |
760 | goto error; | |
8c0faa1d DG |
761 | } |
762 | ||
b73401da DG |
763 | free(lttng_path); |
764 | free(debugfs_path); | |
f3ed775e | 765 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
b73401da DG |
766 | return; |
767 | ||
768 | error: | |
769 | if (lttng_path) { | |
770 | free(lttng_path); | |
771 | } | |
772 | if (debugfs_path) { | |
773 | free(debugfs_path); | |
774 | } | |
775 | WARN("No kernel tracer available"); | |
776 | kernel_tracer_fd = 0; | |
777 | return; | |
f3ed775e | 778 | } |
33a2b854 | 779 | |
f3ed775e DG |
780 | /* |
781 | * start_kernel_trace | |
782 | * | |
783 | * Start tracing by creating trace directory and sending FDs to the kernel | |
784 | * consumer. | |
785 | */ | |
786 | static int start_kernel_trace(struct ltt_kernel_session *session) | |
787 | { | |
788 | int ret; | |
8c0faa1d | 789 | |
f3ed775e DG |
790 | /* Create trace directory */ |
791 | ret = create_trace_dir(session); | |
1d4b027a | 792 | if (ret < 0) { |
f3ed775e DG |
793 | if (ret == -EEXIST) { |
794 | ret = LTTCOMM_KERN_DIR_EXIST; | |
795 | } else { | |
796 | ret = LTTCOMM_KERN_DIR_FAIL; | |
797 | goto error; | |
33a2b854 DG |
798 | } |
799 | } | |
800 | ||
f3ed775e DG |
801 | if (session->kconsumer_fds_sent == 0) { |
802 | ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session); | |
803 | if (ret < 0) { | |
804 | ERR("Send kconsumerd fds failed"); | |
805 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
806 | goto error; | |
807 | } | |
1d4b027a | 808 | |
f3ed775e DG |
809 | session->kconsumer_fds_sent = 1; |
810 | } | |
1d4b027a DG |
811 | |
812 | error: | |
813 | return ret; | |
8c0faa1d DG |
814 | } |
815 | ||
816 | /* | |
f3ed775e | 817 | * init_default_channel |
8c0faa1d | 818 | * |
f3ed775e | 819 | * Allocate a channel structure and fill it. |
8c0faa1d | 820 | */ |
f3ed775e | 821 | static struct lttng_channel *init_default_channel(void) |
8c0faa1d | 822 | { |
f3ed775e | 823 | struct lttng_channel *chan; |
1d4b027a | 824 | |
f3ed775e DG |
825 | chan = malloc(sizeof(struct lttng_channel)); |
826 | if (chan == NULL) { | |
827 | perror("init channel malloc"); | |
828 | goto error; | |
8c0faa1d | 829 | } |
1d4b027a | 830 | |
f3ed775e DG |
831 | if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) { |
832 | perror("snprintf defautl channel name"); | |
833 | return NULL; | |
834 | } | |
835 | ||
836 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
837 | chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE; | |
838 | chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM; | |
839 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
840 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
7d452e12 | 841 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
1d4b027a DG |
842 | |
843 | error: | |
f3ed775e | 844 | return chan; |
8c0faa1d DG |
845 | } |
846 | ||
333f285e | 847 | /* |
f3ed775e | 848 | * create_kernel_session |
333f285e | 849 | * |
f3ed775e | 850 | * Create a kernel tracer session then create the default channel. |
333f285e | 851 | */ |
f3ed775e | 852 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 853 | { |
f3ed775e DG |
854 | int ret; |
855 | struct lttng_channel *chan; | |
856 | ||
857 | DBG("Creating kernel session"); | |
858 | ||
859 | ret = kernel_create_session(session, kernel_tracer_fd); | |
860 | if (ret < 0) { | |
861 | ret = LTTCOMM_KERN_SESS_FAIL; | |
862 | goto error; | |
333f285e DG |
863 | } |
864 | ||
f3ed775e DG |
865 | chan = init_default_channel(); |
866 | if (chan == NULL) { | |
867 | ret = LTTCOMM_FATAL; | |
868 | goto error; | |
869 | } | |
870 | ||
871 | DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME); | |
872 | ||
b082db07 | 873 | ret = kernel_create_channel(session->kernel_session, chan, session->path); |
f3ed775e DG |
874 | if (ret < 0) { |
875 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
876 | goto error; | |
877 | } | |
878 | ||
879 | error: | |
880 | return ret; | |
333f285e DG |
881 | } |
882 | ||
fac6795d DG |
883 | /* |
884 | * process_client_msg | |
885 | * | |
5461b305 DG |
886 | * Process the command requested by the lttng client within the command |
887 | * context structure. This function make sure that the return structure (llm) | |
888 | * is set and ready for transmission before returning. | |
fac6795d | 889 | * |
e065084a | 890 | * Return any error encountered or 0 for success. |
fac6795d | 891 | */ |
5461b305 | 892 | static int process_client_msg(struct command_ctx *cmd_ctx) |
fac6795d | 893 | { |
5461b305 | 894 | int ret; |
fac6795d | 895 | |
5461b305 | 896 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
fac6795d | 897 | |
f3ed775e | 898 | /* Listing commands don't need a session */ |
5461b305 | 899 | switch (cmd_ctx->lsm->cmd_type) { |
5e16da05 MD |
900 | case LTTNG_CREATE_SESSION: |
901 | case LTTNG_LIST_SESSIONS: | |
f3ed775e DG |
902 | case LTTNG_LIST_EVENTS: |
903 | case LTTNG_KERNEL_LIST_EVENTS: | |
904 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 MD |
905 | break; |
906 | default: | |
f3ed775e DG |
907 | DBG("Getting session %s by name", cmd_ctx->lsm->session_name); |
908 | cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name); | |
5461b305 | 909 | if (cmd_ctx->session == NULL) { |
f3ed775e DG |
910 | /* If session name not found */ |
911 | if (cmd_ctx->lsm->session_name != NULL) { | |
912 | ret = LTTCOMM_SESS_NOT_FOUND; | |
913 | } else { /* If no session name specified */ | |
914 | ret = LTTCOMM_SELECT_SESS; | |
915 | } | |
5461b305 | 916 | goto error; |
379473d2 | 917 | } |
5e16da05 | 918 | break; |
379473d2 DG |
919 | } |
920 | ||
f3ed775e DG |
921 | /* |
922 | * Check kernel command for kernel session. | |
923 | */ | |
20fe2104 | 924 | switch (cmd_ctx->lsm->cmd_type) { |
d65106b1 | 925 | case LTTNG_KERNEL_ADD_CONTEXT: |
f3ed775e DG |
926 | case LTTNG_KERNEL_CREATE_CHANNEL: |
927 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: | |
928 | case LTTNG_KERNEL_DISABLE_CHANNEL: | |
929 | case LTTNG_KERNEL_DISABLE_EVENT: | |
930 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: | |
931 | case LTTNG_KERNEL_ENABLE_CHANNEL: | |
932 | case LTTNG_KERNEL_ENABLE_EVENT: | |
933 | case LTTNG_KERNEL_LIST_EVENTS: | |
333f285e | 934 | /* Kernel tracer check */ |
20fe2104 | 935 | if (kernel_tracer_fd == 0) { |
333f285e DG |
936 | init_kernel_tracer(); |
937 | if (kernel_tracer_fd == 0) { | |
938 | ret = LTTCOMM_KERN_NA; | |
939 | goto error; | |
940 | } | |
20fe2104 | 941 | } |
f3ed775e DG |
942 | |
943 | /* Need a session for kernel command */ | |
944 | if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS && | |
945 | cmd_ctx->session->kernel_session == NULL) { | |
7b395890 | 946 | |
f3ed775e DG |
947 | ret = create_kernel_session(cmd_ctx->session); |
948 | if (ret < 0) { | |
949 | ret = LTTCOMM_KERN_SESS_FAIL; | |
950 | goto error; | |
951 | } | |
952 | ||
7b395890 | 953 | /* Start the kernel consumer daemon */ |
f3ed775e DG |
954 | if (kconsumerd_pid == 0) { |
955 | ret = start_kconsumerd(); | |
956 | if (ret < 0) { | |
957 | goto error; | |
958 | } | |
959 | } | |
960 | } | |
20fe2104 DG |
961 | } |
962 | ||
471d1693 | 963 | /* Connect to ust apps if available pid */ |
5461b305 | 964 | if (cmd_ctx->lsm->pid > 0) { |
471d1693 | 965 | /* Connect to app using ustctl API */ |
5461b305 DG |
966 | cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid); |
967 | if (cmd_ctx->ust_sock < 0) { | |
471d1693 | 968 | ret = LTTCOMM_NO_TRACEABLE; |
5461b305 | 969 | goto error; |
471d1693 DG |
970 | } |
971 | } | |
972 | ||
fac6795d | 973 | /* Process by command type */ |
5461b305 | 974 | switch (cmd_ctx->lsm->cmd_type) { |
d65106b1 DG |
975 | case LTTNG_KERNEL_ADD_CONTEXT: |
976 | { | |
977 | int found = 0, no_event = 0; | |
978 | struct ltt_kernel_channel *chan; | |
979 | struct ltt_kernel_event *event; | |
980 | ||
981 | /* Setup lttng message with no payload */ | |
982 | ret = setup_lttng_msg(cmd_ctx, 0); | |
983 | if (ret < 0) { | |
984 | goto setup_error; | |
985 | } | |
986 | ||
987 | /* Check if event name is given */ | |
988 | if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) { | |
989 | no_event = 1; | |
990 | } | |
991 | ||
992 | if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) { | |
993 | /* Go over all channels */ | |
994 | DBG("Adding context to all channels"); | |
995 | cds_list_for_each_entry(chan, | |
996 | &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
997 | if (no_event) { | |
998 | ret = kernel_add_channel_context(chan, | |
999 | &cmd_ctx->lsm->u.context.ctx); | |
1000 | if (ret < 0) { | |
1001 | continue; | |
1002 | } | |
1003 | } else { | |
1004 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); | |
1005 | if (event != NULL) { | |
1006 | ret = kernel_add_event_context(event, | |
1007 | &cmd_ctx->lsm->u.context.ctx); | |
1008 | if (ret < 0) { | |
1009 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1010 | goto error; | |
1011 | } | |
1012 | found = 1; | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | } else { | |
1018 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name, | |
1019 | cmd_ctx->session->kernel_session); | |
1020 | if (chan == NULL) { | |
1021 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1022 | goto error; | |
1023 | } | |
1024 | ||
1025 | if (no_event) { | |
1026 | ret = kernel_add_channel_context(chan, | |
1027 | &cmd_ctx->lsm->u.context.ctx); | |
1028 | if (ret < 0) { | |
1029 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1030 | goto error; | |
1031 | } | |
1032 | } else { | |
1033 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); | |
1034 | if (event != NULL) { | |
1035 | ret = kernel_add_event_context(event, | |
1036 | &cmd_ctx->lsm->u.context.ctx); | |
1037 | if (ret < 0) { | |
1038 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1039 | goto error; | |
1040 | } | |
1041 | } | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | if (!found && !no_event) { | |
1046 | ret = LTTCOMM_NO_EVENT; | |
1047 | goto error; | |
1048 | } | |
1049 | ||
1050 | ret = LTTCOMM_OK; | |
1051 | break; | |
1052 | } | |
f3ed775e | 1053 | case LTTNG_KERNEL_CREATE_CHANNEL: |
20fe2104 | 1054 | { |
f3ed775e | 1055 | /* Setup lttng message with no payload */ |
20fe2104 DG |
1056 | ret = setup_lttng_msg(cmd_ctx, 0); |
1057 | if (ret < 0) { | |
1058 | goto setup_error; | |
1059 | } | |
1060 | ||
f3ed775e | 1061 | /* Kernel tracer */ |
8c0faa1d DG |
1062 | DBG("Creating kernel channel"); |
1063 | ||
f3ed775e | 1064 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, |
b082db07 | 1065 | &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); |
20fe2104 DG |
1066 | if (ret < 0) { |
1067 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
1068 | goto error; | |
1069 | } | |
1070 | ||
1071 | ret = LTTCOMM_OK; | |
1072 | break; | |
1073 | } | |
26cc6b4e DG |
1074 | case LTTNG_KERNEL_DISABLE_CHANNEL: |
1075 | { | |
1076 | struct ltt_kernel_channel *chan; | |
1077 | ||
1078 | /* Setup lttng message with no payload */ | |
1079 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1080 | if (ret < 0) { | |
1081 | goto setup_error; | |
1082 | } | |
1083 | ||
1084 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, | |
1085 | cmd_ctx->session->kernel_session); | |
1086 | if (chan == NULL) { | |
1087 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1088 | goto error; | |
1089 | } else if (chan->enabled == 1) { | |
1090 | ret = kernel_disable_channel(chan); | |
1091 | if (ret < 0) { | |
1092 | if (ret != EEXIST) { | |
1093 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; | |
1094 | } | |
1095 | goto error; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | kernel_wait_quiescent(kernel_tracer_fd); | |
1100 | ret = LTTCOMM_OK; | |
1101 | break; | |
1102 | } | |
e953ef25 DG |
1103 | case LTTNG_KERNEL_DISABLE_EVENT: |
1104 | { | |
e953ef25 | 1105 | struct ltt_kernel_channel *chan; |
19e70852 | 1106 | struct ltt_kernel_event *ev; |
e953ef25 DG |
1107 | |
1108 | /* Setup lttng message with no payload */ | |
1109 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1110 | if (ret < 0) { | |
1111 | goto setup_error; | |
1112 | } | |
1113 | ||
19e70852 DG |
1114 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, |
1115 | cmd_ctx->session->kernel_session); | |
1116 | if (chan == NULL) { | |
1117 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1118 | goto error; | |
1119 | } | |
e953ef25 | 1120 | |
19e70852 DG |
1121 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan); |
1122 | if (ev != NULL) { | |
1123 | DBG("Disabling kernel event %s for channel %s.", | |
1124 | cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name); | |
1125 | ret = kernel_disable_event(ev); | |
1126 | if (ret < 0) { | |
1127 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
1128 | goto error; | |
e953ef25 DG |
1129 | } |
1130 | } | |
1131 | ||
19e70852 DG |
1132 | kernel_wait_quiescent(kernel_tracer_fd); |
1133 | ret = LTTCOMM_OK; | |
e953ef25 DG |
1134 | break; |
1135 | } | |
950131af DG |
1136 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: |
1137 | { | |
1138 | struct ltt_kernel_channel *chan; | |
1139 | struct ltt_kernel_event *ev; | |
1140 | ||
1141 | /* Setup lttng message with no payload */ | |
1142 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1143 | if (ret < 0) { | |
1144 | goto setup_error; | |
1145 | } | |
1146 | ||
1147 | DBG("Disabling all enabled kernel events"); | |
1148 | ||
1149 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, | |
1150 | cmd_ctx->session->kernel_session); | |
1151 | if (chan == NULL) { | |
1152 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1153 | goto error; | |
1154 | } | |
1155 | ||
1156 | /* For each event in the kernel session */ | |
1157 | cds_list_for_each_entry(ev, &chan->events_list.head, list) { | |
1158 | DBG("Disabling kernel event %s for channel %s.", | |
1159 | ev->event->name, cmd_ctx->lsm->u.disable.channel_name); | |
1160 | ret = kernel_disable_event(ev); | |
1161 | if (ret < 0) { | |
1162 | continue; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | /* Quiescent wait after event disable */ | |
1167 | kernel_wait_quiescent(kernel_tracer_fd); | |
1168 | ret = LTTCOMM_OK; | |
1169 | break; | |
1170 | } | |
d36b8583 DG |
1171 | case LTTNG_KERNEL_ENABLE_CHANNEL: |
1172 | { | |
1173 | struct ltt_kernel_channel *chan; | |
1174 | ||
1175 | /* Setup lttng message with no payload */ | |
1176 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1177 | if (ret < 0) { | |
1178 | goto setup_error; | |
1179 | } | |
1180 | ||
1181 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, | |
1182 | cmd_ctx->session->kernel_session); | |
1183 | if (chan == NULL) { | |
1184 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1185 | goto error; | |
1186 | } else if (chan->enabled == 0) { | |
1187 | ret = kernel_enable_channel(chan); | |
1188 | if (ret < 0) { | |
1189 | if (ret != EEXIST) { | |
1190 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; | |
1191 | } | |
1192 | goto error; | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | kernel_wait_quiescent(kernel_tracer_fd); | |
1197 | ret = LTTCOMM_OK; | |
1198 | break; | |
1199 | } | |
f3ed775e | 1200 | case LTTNG_KERNEL_ENABLE_EVENT: |
894be886 | 1201 | { |
f3ed775e | 1202 | struct ltt_kernel_channel *chan; |
19e70852 | 1203 | struct ltt_kernel_event *ev; |
f3ed775e | 1204 | |
894be886 DG |
1205 | /* Setup lttng message with no payload */ |
1206 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1207 | if (ret < 0) { | |
1208 | goto setup_error; | |
1209 | } | |
1210 | ||
19e70852 DG |
1211 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, |
1212 | cmd_ctx->session->kernel_session); | |
1213 | if (chan == NULL) { | |
1214 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1215 | goto error; | |
f34daff7 DG |
1216 | } |
1217 | ||
19e70852 DG |
1218 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan); |
1219 | if (ev == NULL) { | |
1220 | DBG("Creating kernel event %s for channel %s.", | |
950131af | 1221 | cmd_ctx->lsm->u.enable.event.name, chan->channel->name); |
19e70852 | 1222 | ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan); |
f3ed775e | 1223 | } else { |
19e70852 | 1224 | DBG("Enabling kernel event %s for channel %s.", |
950131af | 1225 | cmd_ctx->lsm->u.enable.event.name, chan->channel->name); |
19e70852 DG |
1226 | ret = kernel_enable_event(ev); |
1227 | } | |
1228 | ||
1229 | if (ret < 0) { | |
1230 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
1231 | goto error; | |
f3ed775e | 1232 | } |
19e70852 DG |
1233 | |
1234 | kernel_wait_quiescent(kernel_tracer_fd); | |
1235 | ret = LTTCOMM_OK; | |
894be886 DG |
1236 | break; |
1237 | } | |
f3ed775e | 1238 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: |
33a2b854 | 1239 | { |
950131af | 1240 | int pos, size; |
33a2b854 | 1241 | char *event_list, *event, *ptr; |
f3ed775e | 1242 | struct ltt_kernel_channel *chan; |
950131af DG |
1243 | struct ltt_kernel_event *ev; |
1244 | struct lttng_event ev_attr; | |
33a2b854 DG |
1245 | |
1246 | /* Setup lttng message with no payload */ | |
1247 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1248 | if (ret < 0) { | |
1249 | goto setup_error; | |
1250 | } | |
1251 | ||
1252 | DBG("Enabling all kernel event"); | |
1253 | ||
950131af DG |
1254 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, |
1255 | cmd_ctx->session->kernel_session); | |
1256 | if (chan == NULL) { | |
1257 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
33a2b854 DG |
1258 | goto error; |
1259 | } | |
1260 | ||
950131af DG |
1261 | /* For each event in the kernel session */ |
1262 | cds_list_for_each_entry(ev, &chan->events_list.head, list) { | |
1263 | DBG("Enabling kernel event %s for channel %s.", | |
1264 | ev->event->name, chan->channel->name); | |
1265 | ret = kernel_enable_event(ev); | |
1266 | if (ret < 0) { | |
1267 | continue; | |
f3ed775e DG |
1268 | } |
1269 | } | |
1270 | ||
950131af DG |
1271 | size = kernel_list_events(kernel_tracer_fd, &event_list); |
1272 | if (size < 0) { | |
1273 | ret = LTTCOMM_KERN_LIST_FAIL; | |
f3ed775e DG |
1274 | goto error; |
1275 | } | |
1276 | ||
33a2b854 DG |
1277 | ptr = event_list; |
1278 | while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { | |
950131af DG |
1279 | ev = get_kernel_event_by_name(event, chan); |
1280 | if (ev == NULL) { | |
1281 | strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN); | |
1282 | /* Default event type for enable all */ | |
1283 | ev_attr.type = LTTNG_EVENT_TRACEPOINTS; | |
1284 | /* Enable each single tracepoint event */ | |
1285 | ret = kernel_create_event(&ev_attr, chan); | |
1286 | if (ret < 0) { | |
1287 | /* Ignore error here and continue */ | |
950131af | 1288 | } |
33a2b854 | 1289 | } |
950131af | 1290 | |
33a2b854 DG |
1291 | /* Move pointer to the next line */ |
1292 | ptr += pos + 1; | |
1293 | free(event); | |
1294 | } | |
1295 | ||
1296 | free(event_list); | |
1297 | ||
f3ed775e DG |
1298 | /* Quiescent wait after event enable */ |
1299 | kernel_wait_quiescent(kernel_tracer_fd); | |
33a2b854 DG |
1300 | ret = LTTCOMM_OK; |
1301 | break; | |
1302 | } | |
f3ed775e | 1303 | case LTTNG_KERNEL_LIST_EVENTS: |
2ef84c95 DG |
1304 | { |
1305 | char *event_list; | |
f3ed775e DG |
1306 | ssize_t size = 0; |
1307 | ||
1308 | DBG("Listing kernel events"); | |
2ef84c95 DG |
1309 | |
1310 | size = kernel_list_events(kernel_tracer_fd, &event_list); | |
1311 | if (size < 0) { | |
1312 | ret = LTTCOMM_KERN_LIST_FAIL; | |
1313 | goto error; | |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | * Setup lttng message with payload size set to the event list size in | |
1318 | * bytes and then copy list into the llm payload. | |
1319 | */ | |
1320 | ret = setup_lttng_msg(cmd_ctx, size); | |
1321 | if (ret < 0) { | |
1322 | goto setup_error; | |
1323 | } | |
1324 | ||
1325 | /* Copy event list into message payload */ | |
1326 | memcpy(cmd_ctx->llm->payload, event_list, size); | |
1327 | ||
1328 | free(event_list); | |
1329 | ||
1330 | ret = LTTCOMM_OK; | |
1331 | break; | |
1332 | } | |
f3ed775e | 1333 | case LTTNG_START_TRACE: |
8c0faa1d DG |
1334 | { |
1335 | struct ltt_kernel_channel *chan; | |
f3ed775e | 1336 | |
8c0faa1d DG |
1337 | /* Setup lttng message with no payload */ |
1338 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1339 | if (ret < 0) { | |
1340 | goto setup_error; | |
1341 | } | |
1342 | ||
f3ed775e DG |
1343 | /* Kernel tracing */ |
1344 | if (cmd_ctx->session->kernel_session != NULL) { | |
1345 | if (cmd_ctx->session->kernel_session->metadata == NULL) { | |
1346 | DBG("Open kernel metadata"); | |
58a97671 DG |
1347 | ret = kernel_open_metadata(cmd_ctx->session->kernel_session, |
1348 | cmd_ctx->session->path); | |
f3ed775e DG |
1349 | if (ret < 0) { |
1350 | ret = LTTCOMM_KERN_META_FAIL; | |
1351 | goto error; | |
1352 | } | |
1353 | } | |
8c0faa1d | 1354 | |
f3ed775e DG |
1355 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { |
1356 | DBG("Opening kernel metadata stream"); | |
1357 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { | |
1358 | ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session); | |
1359 | if (ret < 0) { | |
1360 | ERR("Kernel create metadata stream failed"); | |
1361 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1362 | goto error; | |
1363 | } | |
1364 | } | |
1365 | } | |
8c0faa1d | 1366 | |
f3ed775e DG |
1367 | /* For each channel */ |
1368 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
1369 | if (chan->stream_count == 0) { | |
1370 | ret = kernel_open_channel_stream(chan); | |
1371 | if (ret < 0) { | |
1372 | ERR("Kernel create channel stream failed"); | |
1373 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1374 | goto error; | |
1375 | } | |
1376 | /* Update the stream global counter */ | |
1377 | cmd_ctx->session->kernel_session->stream_count_global += ret; | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | DBG("Start kernel tracing"); | |
1382 | ret = kernel_start_session(cmd_ctx->session->kernel_session); | |
8c0faa1d | 1383 | if (ret < 0) { |
f3ed775e DG |
1384 | ERR("Kernel start session failed"); |
1385 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1386 | goto error; |
1387 | } | |
8c0faa1d | 1388 | |
f3ed775e DG |
1389 | ret = start_kernel_trace(cmd_ctx->session->kernel_session); |
1390 | if (ret < 0) { | |
1391 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1392 | goto error; |
1393 | } | |
8c0faa1d | 1394 | |
f3ed775e DG |
1395 | /* Quiescent wait after starting trace */ |
1396 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1397 | } |
1398 | ||
f3ed775e | 1399 | /* TODO: Start all UST traces */ |
8c0faa1d DG |
1400 | |
1401 | ret = LTTCOMM_OK; | |
1402 | break; | |
1403 | } | |
f3ed775e | 1404 | case LTTNG_STOP_TRACE: |
8c0faa1d | 1405 | { |
f3ed775e | 1406 | struct ltt_kernel_channel *chan; |
8c0faa1d DG |
1407 | /* Setup lttng message with no payload */ |
1408 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1409 | if (ret < 0) { | |
1410 | goto setup_error; | |
1411 | } | |
1412 | ||
f3ed775e DG |
1413 | /* Kernel tracer */ |
1414 | if (cmd_ctx->session->kernel_session != NULL) { | |
1415 | DBG("Stop kernel tracing"); | |
84291629 | 1416 | |
f3ed775e DG |
1417 | ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd); |
1418 | if (ret < 0) { | |
1419 | ERR("Kernel metadata flush failed"); | |
1420 | } | |
8c0faa1d | 1421 | |
f3ed775e DG |
1422 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { |
1423 | ret = kernel_flush_buffer(chan); | |
1424 | if (ret < 0) { | |
1425 | ERR("Kernel flush buffer error"); | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | ret = kernel_stop_session(cmd_ctx->session->kernel_session); | |
1430 | if (ret < 0) { | |
1431 | ERR("Kernel stop session failed"); | |
1432 | ret = LTTCOMM_KERN_STOP_FAIL; | |
1433 | goto error; | |
1434 | } | |
1435 | ||
1436 | /* Quiescent wait after stopping trace */ | |
1437 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1438 | } |
1439 | ||
f3ed775e | 1440 | /* TODO : User-space tracer */ |
8c0faa1d DG |
1441 | |
1442 | ret = LTTCOMM_OK; | |
1443 | break; | |
1444 | } | |
5e16da05 MD |
1445 | case LTTNG_CREATE_SESSION: |
1446 | { | |
5461b305 DG |
1447 | /* Setup lttng message with no payload */ |
1448 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1449 | if (ret < 0) { | |
1450 | goto setup_error; | |
1451 | } | |
1452 | ||
f3ed775e | 1453 | ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path); |
5e16da05 | 1454 | if (ret < 0) { |
f3ed775e | 1455 | if (ret == -EEXIST) { |
5e16da05 MD |
1456 | ret = LTTCOMM_EXIST_SESS; |
1457 | } else { | |
aaf97519 | 1458 | ret = LTTCOMM_FATAL; |
aaf97519 | 1459 | } |
5461b305 | 1460 | goto error; |
8028d920 | 1461 | } |
1657e9bb | 1462 | |
5461b305 | 1463 | ret = LTTCOMM_OK; |
5e16da05 MD |
1464 | break; |
1465 | } | |
1466 | case LTTNG_DESTROY_SESSION: | |
1467 | { | |
5461b305 DG |
1468 | /* Setup lttng message with no payload */ |
1469 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1470 | if (ret < 0) { | |
1471 | goto setup_error; | |
1472 | } | |
1473 | ||
f3ed775e DG |
1474 | /* Clean kernel session teardown */ |
1475 | teardown_kernel_session(cmd_ctx->session); | |
1476 | ||
1477 | ret = destroy_session(cmd_ctx->lsm->session_name); | |
5e16da05 | 1478 | if (ret < 0) { |
f3ed775e | 1479 | ret = LTTCOMM_FATAL; |
5461b305 | 1480 | goto error; |
5e16da05 | 1481 | } |
1657e9bb | 1482 | |
5461b305 DG |
1483 | ret = LTTCOMM_OK; |
1484 | break; | |
5e16da05 | 1485 | } |
f3ed775e | 1486 | /* |
5e16da05 MD |
1487 | case LTTNG_LIST_TRACES: |
1488 | { | |
5461b305 | 1489 | unsigned int trace_count; |
1657e9bb | 1490 | |
5461b305 | 1491 | trace_count = get_trace_count_per_session(cmd_ctx->session); |
5e16da05 MD |
1492 | if (trace_count == 0) { |
1493 | ret = LTTCOMM_NO_TRACE; | |
5461b305 | 1494 | goto error; |
1657e9bb | 1495 | } |
df0da139 | 1496 | |
5461b305 DG |
1497 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count); |
1498 | if (ret < 0) { | |
1499 | goto setup_error; | |
df0da139 | 1500 | } |
ca95a216 | 1501 | |
5461b305 DG |
1502 | get_traces_per_session(cmd_ctx->session, |
1503 | (struct lttng_trace *)(cmd_ctx->llm->payload)); | |
1504 | ||
1505 | ret = LTTCOMM_OK; | |
5e16da05 MD |
1506 | break; |
1507 | } | |
f3ed775e DG |
1508 | */ |
1509 | /* | |
5e16da05 MD |
1510 | case UST_CREATE_TRACE: |
1511 | { | |
5461b305 | 1512 | ret = setup_lttng_msg(cmd_ctx, 0); |
5e16da05 | 1513 | if (ret < 0) { |
5461b305 | 1514 | goto setup_error; |
fac6795d | 1515 | } |
ce3d728c | 1516 | |
5461b305 DG |
1517 | ret = ust_create_trace(cmd_ctx); |
1518 | if (ret < 0) { | |
f3ed775e | 1519 | goto error; |
5461b305 DG |
1520 | } |
1521 | break; | |
5e16da05 | 1522 | } |
f3ed775e DG |
1523 | */ |
1524 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 | 1525 | { |
5461b305 DG |
1526 | unsigned int app_count; |
1527 | ||
1528 | app_count = get_app_count(); | |
1529 | DBG("Traceable application count : %d", app_count); | |
5e16da05 MD |
1530 | if (app_count == 0) { |
1531 | ret = LTTCOMM_NO_APPS; | |
5461b305 | 1532 | goto error; |
ce3d728c | 1533 | } |
520ff687 | 1534 | |
5461b305 DG |
1535 | ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count); |
1536 | if (ret < 0) { | |
1537 | goto setup_error; | |
520ff687 | 1538 | } |
57167058 | 1539 | |
5461b305 | 1540 | get_app_list_pids((pid_t *)(cmd_ctx->llm->payload)); |
5e16da05 | 1541 | |
5461b305 | 1542 | ret = LTTCOMM_OK; |
5e16da05 MD |
1543 | break; |
1544 | } | |
f3ed775e | 1545 | /* |
5e16da05 MD |
1546 | case UST_START_TRACE: |
1547 | { | |
5461b305 DG |
1548 | ret = setup_lttng_msg(cmd_ctx, 0); |
1549 | if (ret < 0) { | |
1550 | goto setup_error; | |
1551 | } | |
ca95a216 | 1552 | |
5461b305 DG |
1553 | ret = ust_start_trace(cmd_ctx); |
1554 | if (ret < 0) { | |
1555 | goto setup_error; | |
1556 | } | |
1557 | break; | |
5e16da05 MD |
1558 | } |
1559 | case UST_STOP_TRACE: | |
1560 | { | |
5461b305 DG |
1561 | ret = setup_lttng_msg(cmd_ctx, 0); |
1562 | if (ret < 0) { | |
1563 | goto setup_error; | |
1564 | } | |
ca95a216 | 1565 | |
5461b305 DG |
1566 | ret = ust_stop_trace(cmd_ctx); |
1567 | if (ret < 0) { | |
1568 | goto setup_error; | |
1569 | } | |
1570 | break; | |
5e16da05 | 1571 | } |
f3ed775e | 1572 | */ |
5e16da05 MD |
1573 | case LTTNG_LIST_SESSIONS: |
1574 | { | |
5461b305 DG |
1575 | unsigned int session_count; |
1576 | ||
1577 | session_count = get_session_count(); | |
5e16da05 | 1578 | if (session_count == 0) { |
f3ed775e | 1579 | ret = LTTCOMM_NO_SESSION; |
5461b305 | 1580 | goto error; |
57167058 | 1581 | } |
5e16da05 | 1582 | |
5461b305 DG |
1583 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count); |
1584 | if (ret < 0) { | |
1585 | goto setup_error; | |
e065084a | 1586 | } |
5e16da05 | 1587 | |
5461b305 | 1588 | get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload)); |
5e16da05 | 1589 | |
5461b305 | 1590 | ret = LTTCOMM_OK; |
5e16da05 MD |
1591 | break; |
1592 | } | |
1593 | default: | |
1594 | /* Undefined command */ | |
5461b305 DG |
1595 | ret = setup_lttng_msg(cmd_ctx, 0); |
1596 | if (ret < 0) { | |
1597 | goto setup_error; | |
1598 | } | |
1599 | ||
5e16da05 | 1600 | ret = LTTCOMM_UND; |
5461b305 | 1601 | break; |
fac6795d DG |
1602 | } |
1603 | ||
5461b305 DG |
1604 | /* Set return code */ |
1605 | cmd_ctx->llm->ret_code = ret; | |
ca95a216 | 1606 | |
e065084a DG |
1607 | return ret; |
1608 | ||
5461b305 | 1609 | error: |
5461b305 DG |
1610 | if (cmd_ctx->llm == NULL) { |
1611 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 1612 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
1613 | goto setup_error; |
1614 | } | |
1615 | } | |
e065084a | 1616 | /* Notify client of error */ |
5461b305 | 1617 | cmd_ctx->llm->ret_code = ret; |
e065084a | 1618 | |
5461b305 | 1619 | setup_error: |
8028d920 | 1620 | return ret; |
fac6795d DG |
1621 | } |
1622 | ||
1d4b027a DG |
1623 | /* |
1624 | * thread_manage_clients | |
1625 | * | |
1626 | * This thread manage all clients request using the unix | |
1627 | * client socket for communication. | |
1628 | */ | |
1629 | static void *thread_manage_clients(void *data) | |
1630 | { | |
1631 | int sock, ret; | |
1632 | struct command_ctx *cmd_ctx; | |
1633 | ||
1634 | DBG("[thread] Manage client started"); | |
1635 | ||
1636 | ret = lttcomm_listen_unix_sock(client_sock); | |
1637 | if (ret < 0) { | |
1638 | goto error; | |
1639 | } | |
1640 | ||
1641 | /* Notify parent pid that we are ready | |
1642 | * to accept command for client side. | |
1643 | */ | |
1644 | if (opt_sig_parent) { | |
1645 | kill(ppid, SIGCHLD); | |
1646 | } | |
1647 | ||
1648 | while (1) { | |
1649 | /* Blocking call, waiting for transmission */ | |
1650 | DBG("Accepting client command ..."); | |
1651 | sock = lttcomm_accept_unix_sock(client_sock); | |
1652 | if (sock < 0) { | |
1653 | goto error; | |
1654 | } | |
1655 | ||
1656 | /* Allocate context command to process the client request */ | |
1657 | cmd_ctx = malloc(sizeof(struct command_ctx)); | |
1658 | ||
1659 | /* Allocate data buffer for reception */ | |
1660 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); | |
1661 | cmd_ctx->llm = NULL; | |
1662 | cmd_ctx->session = NULL; | |
1663 | ||
1664 | /* | |
1665 | * Data is received from the lttng client. The struct | |
1666 | * lttcomm_session_msg (lsm) contains the command and data request of | |
1667 | * the client. | |
1668 | */ | |
1669 | DBG("Receiving data from client ..."); | |
1670 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg)); | |
1671 | if (ret <= 0) { | |
1672 | continue; | |
1673 | } | |
1674 | ||
f7776ea7 DG |
1675 | // TODO: Validate cmd_ctx including sanity check for security purpose. |
1676 | ||
1d4b027a DG |
1677 | /* |
1678 | * This function dispatch the work to the kernel or userspace tracer | |
1679 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
1680 | * informations for the client. The command context struct contains | |
1681 | * everything this function may needs. | |
1682 | */ | |
1683 | ret = process_client_msg(cmd_ctx); | |
1684 | if (ret < 0) { | |
1685 | /* TODO: Inform client somehow of the fatal error. At this point, | |
1686 | * ret < 0 means that a malloc failed (ENOMEM). */ | |
1687 | /* Error detected but still accept command */ | |
1688 | clean_command_ctx(cmd_ctx); | |
1689 | continue; | |
1690 | } | |
1691 | ||
1692 | DBG("Sending response (size: %d, retcode: %d)", | |
1693 | cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code); | |
1694 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); | |
1695 | if (ret < 0) { | |
1696 | ERR("Failed to send data back to client"); | |
1697 | } | |
1698 | ||
1699 | clean_command_ctx(cmd_ctx); | |
d6e4fca4 DG |
1700 | |
1701 | /* End of transmission */ | |
1702 | close(sock); | |
1d4b027a DG |
1703 | } |
1704 | ||
1705 | error: | |
1706 | return NULL; | |
1707 | } | |
1708 | ||
1709 | ||
fac6795d DG |
1710 | /* |
1711 | * usage function on stderr | |
1712 | */ | |
1713 | static void usage(void) | |
1714 | { | |
b716ce68 | 1715 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
1716 | fprintf(stderr, " -h, --help Display this usage.\n"); |
1717 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
1718 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
1719 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
1720 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
1721 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
1722 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
1723 | fprintf(stderr, " -V, --version Show version number.\n"); | |
1724 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
1725 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
1726 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
fac6795d DG |
1727 | } |
1728 | ||
1729 | /* | |
1730 | * daemon argument parsing | |
1731 | */ | |
1732 | static int parse_args(int argc, char **argv) | |
1733 | { | |
1734 | int c; | |
1735 | ||
1736 | static struct option long_options[] = { | |
1737 | { "client-sock", 1, 0, 'c' }, | |
1738 | { "apps-sock", 1, 0, 'a' }, | |
d6f42150 DG |
1739 | { "kconsumerd-cmd-sock", 1, 0, 0 }, |
1740 | { "kconsumerd-err-sock", 1, 0, 0 }, | |
fac6795d | 1741 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 1742 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
1743 | { "help", 0, 0, 'h' }, |
1744 | { "group", 1, 0, 'g' }, | |
1745 | { "version", 0, 0, 'V' }, | |
75462a81 | 1746 | { "quiet", 0, 0, 'q' }, |
3f9947db | 1747 | { "verbose", 0, 0, 'v' }, |
fac6795d DG |
1748 | { NULL, 0, 0, 0 } |
1749 | }; | |
1750 | ||
1751 | while (1) { | |
1752 | int option_index = 0; | |
d6f42150 | 1753 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index); |
fac6795d DG |
1754 | if (c == -1) { |
1755 | break; | |
1756 | } | |
1757 | ||
1758 | switch (c) { | |
1759 | case 0: | |
1760 | fprintf(stderr, "option %s", long_options[option_index].name); | |
1761 | if (optarg) { | |
1762 | fprintf(stderr, " with arg %s\n", optarg); | |
1763 | } | |
1764 | break; | |
b716ce68 | 1765 | case 'c': |
fac6795d DG |
1766 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
1767 | break; | |
1768 | case 'a': | |
1769 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
1770 | break; | |
1771 | case 'd': | |
1772 | opt_daemon = 1; | |
1773 | break; | |
1774 | case 'g': | |
1775 | opt_tracing_group = strdup(optarg); | |
1776 | break; | |
1777 | case 'h': | |
1778 | usage(); | |
1779 | exit(EXIT_FAILURE); | |
1780 | case 'V': | |
1781 | fprintf(stdout, "%s\n", VERSION); | |
1782 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
1783 | case 'S': |
1784 | opt_sig_parent = 1; | |
1785 | break; | |
d6f42150 DG |
1786 | case 'E': |
1787 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg); | |
1788 | break; | |
1789 | case 'C': | |
1790 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
1791 | break; | |
75462a81 DG |
1792 | case 'q': |
1793 | opt_quiet = 1; | |
1794 | break; | |
3f9947db DG |
1795 | case 'v': |
1796 | opt_verbose = 1; | |
1797 | break; | |
fac6795d DG |
1798 | default: |
1799 | /* Unknown option or other error. | |
1800 | * Error is printed by getopt, just return */ | |
1801 | return -1; | |
1802 | } | |
1803 | } | |
1804 | ||
1805 | return 0; | |
1806 | } | |
1807 | ||
1808 | /* | |
1809 | * init_daemon_socket | |
1810 | * | |
1811 | * Creates the two needed socket by the daemon. | |
d6f42150 DG |
1812 | * apps_sock - The communication socket for all UST apps. |
1813 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d DG |
1814 | */ |
1815 | static int init_daemon_socket() | |
1816 | { | |
1817 | int ret = 0; | |
1818 | mode_t old_umask; | |
1819 | ||
1820 | old_umask = umask(0); | |
1821 | ||
1822 | /* Create client tool unix socket */ | |
d6f42150 DG |
1823 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
1824 | if (client_sock < 0) { | |
1825 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
1826 | ret = -1; |
1827 | goto end; | |
1828 | } | |
1829 | ||
1830 | /* File permission MUST be 660 */ | |
1831 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1832 | if (ret < 0) { | |
d6f42150 | 1833 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
1834 | perror("chmod"); |
1835 | goto end; | |
1836 | } | |
1837 | ||
1838 | /* Create the application unix socket */ | |
d6f42150 DG |
1839 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
1840 | if (apps_sock < 0) { | |
1841 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
1842 | ret = -1; |
1843 | goto end; | |
1844 | } | |
1845 | ||
d6f42150 | 1846 | /* File permission MUST be 666 */ |
fac6795d DG |
1847 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
1848 | if (ret < 0) { | |
d6f42150 | 1849 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
1850 | perror("chmod"); |
1851 | goto end; | |
1852 | } | |
1853 | ||
1854 | end: | |
1855 | umask(old_umask); | |
1856 | return ret; | |
1857 | } | |
1858 | ||
1859 | /* | |
1860 | * check_existing_daemon | |
1861 | * | |
1862 | * Check if the global socket is available. | |
62bd06d8 | 1863 | * If yes, error is returned. |
fac6795d DG |
1864 | */ |
1865 | static int check_existing_daemon() | |
1866 | { | |
1867 | int ret; | |
1868 | ||
1869 | ret = access(client_unix_sock_path, F_OK); | |
1870 | if (ret == 0) { | |
1871 | ret = access(apps_unix_sock_path, F_OK); | |
1872 | } | |
1873 | ||
1874 | return ret; | |
1875 | } | |
1876 | ||
fac6795d | 1877 | /* |
d6f42150 | 1878 | * set_permissions |
fac6795d | 1879 | * |
5e16da05 MD |
1880 | * Set the tracing group gid onto the client socket. |
1881 | * | |
1882 | * Race window between mkdir and chown is OK because we are going from | |
a463f419 | 1883 | * more permissive (root.root) to les permissive (root.tracing). |
fac6795d | 1884 | */ |
d6f42150 | 1885 | static int set_permissions(void) |
fac6795d DG |
1886 | { |
1887 | int ret; | |
1888 | struct group *grp; | |
1889 | ||
1890 | /* Decide which group name to use */ | |
1891 | (opt_tracing_group != NULL) ? | |
1892 | (grp = getgrnam(opt_tracing_group)) : | |
1893 | (grp = getgrnam(default_tracing_group)); | |
1894 | ||
1895 | if (grp == NULL) { | |
a463f419 DG |
1896 | if (is_root) { |
1897 | WARN("No tracing group detected"); | |
1898 | ret = 0; | |
1899 | } else { | |
1900 | ERR("Missing tracing group. Aborting execution."); | |
1901 | ret = -1; | |
1902 | } | |
fac6795d DG |
1903 | goto end; |
1904 | } | |
1905 | ||
d6f42150 DG |
1906 | /* Set lttng run dir */ |
1907 | ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); | |
1908 | if (ret < 0) { | |
1909 | ERR("Unable to set group on " LTTNG_RUNDIR); | |
1910 | perror("chown"); | |
1911 | } | |
1912 | ||
1913 | /* lttng client socket path */ | |
fac6795d DG |
1914 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
1915 | if (ret < 0) { | |
d6f42150 DG |
1916 | ERR("Unable to set group on %s", client_unix_sock_path); |
1917 | perror("chown"); | |
1918 | } | |
1919 | ||
1920 | /* kconsumerd error socket path */ | |
1921 | ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); | |
1922 | if (ret < 0) { | |
1923 | ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); | |
fac6795d DG |
1924 | perror("chown"); |
1925 | } | |
1926 | ||
d6f42150 | 1927 | DBG("All permissions are set"); |
e07ae692 | 1928 | |
fac6795d DG |
1929 | end: |
1930 | return ret; | |
1931 | } | |
1932 | ||
d6f42150 DG |
1933 | /* |
1934 | * create_lttng_rundir | |
1935 | * | |
1936 | * Create the lttng run directory needed for all | |
1937 | * global sockets and pipe. | |
1938 | */ | |
1939 | static int create_lttng_rundir(void) | |
1940 | { | |
1941 | int ret; | |
1942 | ||
1943 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); | |
1944 | if (ret < 0) { | |
b1f11e69 DG |
1945 | if (errno != EEXIST) { |
1946 | ERR("Unable to create " LTTNG_RUNDIR); | |
1947 | goto error; | |
1948 | } else { | |
1949 | ret = 0; | |
1950 | } | |
d6f42150 DG |
1951 | } |
1952 | ||
1953 | error: | |
1954 | return ret; | |
1955 | } | |
1956 | ||
1957 | /* | |
1958 | * set_kconsumerd_sockets | |
1959 | * | |
1960 | * Setup sockets and directory needed by the kconsumerd | |
1961 | * communication with the session daemon. | |
1962 | */ | |
1963 | static int set_kconsumerd_sockets(void) | |
1964 | { | |
1965 | int ret; | |
1966 | ||
1967 | if (strlen(kconsumerd_err_unix_sock_path) == 0) { | |
1968 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH); | |
1969 | } | |
1970 | ||
1971 | if (strlen(kconsumerd_cmd_unix_sock_path) == 0) { | |
1972 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH); | |
1973 | } | |
1974 | ||
1975 | ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG); | |
1976 | if (ret < 0) { | |
6beb2242 DG |
1977 | if (errno != EEXIST) { |
1978 | ERR("Failed to create " KCONSUMERD_PATH); | |
1979 | goto error; | |
1980 | } | |
1981 | ret = 0; | |
d6f42150 DG |
1982 | } |
1983 | ||
1984 | /* Create the kconsumerd error unix socket */ | |
1985 | kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path); | |
1986 | if (kconsumerd_err_sock < 0) { | |
1987 | ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path); | |
1988 | ret = -1; | |
1989 | goto error; | |
1990 | } | |
1991 | ||
1992 | /* File permission MUST be 660 */ | |
1993 | ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1994 | if (ret < 0) { | |
1995 | ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path); | |
1996 | perror("chmod"); | |
1997 | goto error; | |
1998 | } | |
1999 | ||
2000 | error: | |
2001 | return ret; | |
2002 | } | |
2003 | ||
fac6795d | 2004 | /* |
62bd06d8 | 2005 | * sighandler |
fac6795d | 2006 | * |
62bd06d8 | 2007 | * Signal handler for the daemon |
fac6795d DG |
2008 | */ |
2009 | static void sighandler(int sig) | |
2010 | { | |
2011 | switch (sig) { | |
2012 | case SIGPIPE: | |
e07ae692 | 2013 | DBG("SIGPIPE catched"); |
87378cf5 | 2014 | return; |
fac6795d | 2015 | case SIGINT: |
e07ae692 DG |
2016 | DBG("SIGINT catched"); |
2017 | cleanup(); | |
2018 | break; | |
fac6795d | 2019 | case SIGTERM: |
e07ae692 | 2020 | DBG("SIGTERM catched"); |
fac6795d | 2021 | cleanup(); |
686204ab | 2022 | break; |
fac6795d DG |
2023 | default: |
2024 | break; | |
2025 | } | |
2026 | ||
2027 | exit(EXIT_SUCCESS); | |
2028 | } | |
2029 | ||
2030 | /* | |
1d4b027a | 2031 | * set_signal_handler |
fac6795d | 2032 | * |
1d4b027a DG |
2033 | * Setup signal handler for : |
2034 | * SIGINT, SIGTERM, SIGPIPE | |
fac6795d | 2035 | */ |
1d4b027a | 2036 | static int set_signal_handler(void) |
fac6795d | 2037 | { |
1d4b027a DG |
2038 | int ret = 0; |
2039 | struct sigaction sa; | |
2040 | sigset_t sigset; | |
fac6795d | 2041 | |
1d4b027a DG |
2042 | if ((ret = sigemptyset(&sigset)) < 0) { |
2043 | perror("sigemptyset"); | |
2044 | return ret; | |
2045 | } | |
d6f42150 | 2046 | |
1d4b027a DG |
2047 | sa.sa_handler = sighandler; |
2048 | sa.sa_mask = sigset; | |
2049 | sa.sa_flags = 0; | |
2050 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
2051 | perror("sigaction"); | |
2052 | return ret; | |
d6f42150 DG |
2053 | } |
2054 | ||
1d4b027a DG |
2055 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
2056 | perror("sigaction"); | |
2057 | return ret; | |
d6f42150 | 2058 | } |
aaf26714 | 2059 | |
1d4b027a DG |
2060 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
2061 | perror("sigaction"); | |
2062 | return ret; | |
8c0faa1d DG |
2063 | } |
2064 | ||
1d4b027a DG |
2065 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
2066 | ||
2067 | return ret; | |
fac6795d DG |
2068 | } |
2069 | ||
f3ed775e DG |
2070 | /* |
2071 | * set_ulimit | |
2072 | * | |
2073 | * Set open files limit to unlimited. This daemon can open a large number of | |
2074 | * file descriptors in order to consumer multiple kernel traces. | |
2075 | */ | |
2076 | static void set_ulimit(void) | |
2077 | { | |
2078 | int ret; | |
2079 | struct rlimit lim; | |
2080 | ||
2081 | lim.rlim_cur = 65535; | |
2082 | lim.rlim_max = 65535; | |
2083 | ||
2084 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
2085 | if (ret < 0) { | |
2086 | perror("failed to set open files limit"); | |
2087 | } | |
2088 | } | |
2089 | ||
fac6795d DG |
2090 | /* |
2091 | * main | |
2092 | */ | |
2093 | int main(int argc, char **argv) | |
2094 | { | |
fac6795d DG |
2095 | int ret = 0; |
2096 | void *status; | |
b082db07 | 2097 | const char *home_path; |
fac6795d DG |
2098 | |
2099 | /* Parse arguments */ | |
2100 | progname = argv[0]; | |
2101 | if ((ret = parse_args(argc, argv) < 0)) { | |
2102 | goto error; | |
2103 | } | |
2104 | ||
2105 | /* Daemonize */ | |
2106 | if (opt_daemon) { | |
53094c05 DG |
2107 | ret = daemon(0, 0); |
2108 | if (ret < 0) { | |
2109 | perror("daemon"); | |
2110 | goto error; | |
2111 | } | |
fac6795d DG |
2112 | } |
2113 | ||
2114 | /* Check if daemon is UID = 0 */ | |
2115 | is_root = !getuid(); | |
2116 | ||
fac6795d | 2117 | if (is_root) { |
d6f42150 DG |
2118 | ret = create_lttng_rundir(); |
2119 | if (ret < 0) { | |
2120 | goto error; | |
2121 | } | |
2122 | ||
fac6795d | 2123 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
2124 | snprintf(apps_unix_sock_path, PATH_MAX, |
2125 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
2126 | } |
2127 | ||
2128 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
2129 | snprintf(client_unix_sock_path, PATH_MAX, |
2130 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
2131 | } | |
2132 | ||
2133 | ret = set_kconsumerd_sockets(); | |
2134 | if (ret < 0) { | |
2135 | goto error; | |
fac6795d | 2136 | } |
20fe2104 DG |
2137 | |
2138 | /* Setup kernel tracer */ | |
2139 | init_kernel_tracer(); | |
f3ed775e DG |
2140 | |
2141 | /* Set ulimit for open files */ | |
2142 | set_ulimit(); | |
fac6795d | 2143 | } else { |
b082db07 DG |
2144 | home_path = get_home_dir(); |
2145 | if (home_path == NULL) { | |
2146 | ERR("Can't get HOME directory for sockets creation.\n \ | |
2147 | Please specify --socket PATH."); | |
2148 | goto error; | |
2149 | } | |
2150 | ||
fac6795d | 2151 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 2152 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 2153 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
2154 | } |
2155 | ||
2156 | /* Set the cli tool unix socket path */ | |
2157 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 2158 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 2159 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d DG |
2160 | } |
2161 | } | |
2162 | ||
847177cd DG |
2163 | DBG("Client socket path %s", client_unix_sock_path); |
2164 | DBG("Application socket path %s", apps_unix_sock_path); | |
2165 | ||
fac6795d DG |
2166 | /* See if daemon already exist. If any of the two |
2167 | * socket needed by the daemon are present, this test fails | |
2168 | */ | |
2169 | if ((ret = check_existing_daemon()) == 0) { | |
75462a81 | 2170 | ERR("Already running daemon.\n"); |
ab118b20 | 2171 | /* We do not goto error because we must not |
d6f42150 | 2172 | * cleanup() because a daemon is already running. |
ab118b20 | 2173 | */ |
5e16da05 | 2174 | exit(EXIT_FAILURE); |
fac6795d DG |
2175 | } |
2176 | ||
2177 | if (set_signal_handler() < 0) { | |
2178 | goto error; | |
2179 | } | |
2180 | ||
d6f42150 | 2181 | /* Setup the needed unix socket */ |
fac6795d DG |
2182 | if (init_daemon_socket() < 0) { |
2183 | goto error; | |
2184 | } | |
2185 | ||
2186 | /* Set credentials to socket */ | |
d6f42150 | 2187 | if (is_root && (set_permissions() < 0)) { |
fac6795d DG |
2188 | goto error; |
2189 | } | |
2190 | ||
5b8719f5 DG |
2191 | /* Get parent pid if -S, --sig-parent is specified. */ |
2192 | if (opt_sig_parent) { | |
2193 | ppid = getppid(); | |
2194 | } | |
2195 | ||
fac6795d DG |
2196 | while (1) { |
2197 | /* Create thread to manage the client socket */ | |
1d4b027a | 2198 | ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); |
fac6795d DG |
2199 | if (ret != 0) { |
2200 | perror("pthread_create"); | |
2201 | goto error; | |
2202 | } | |
2203 | ||
2204 | /* Create thread to manage application socket */ | |
1d4b027a | 2205 | ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); |
fac6795d DG |
2206 | if (ret != 0) { |
2207 | perror("pthread_create"); | |
2208 | goto error; | |
2209 | } | |
2210 | ||
1d4b027a DG |
2211 | ret = pthread_join(client_thread, &status); |
2212 | if (ret != 0) { | |
2213 | perror("pthread_join"); | |
2214 | goto error; | |
fac6795d DG |
2215 | } |
2216 | } | |
2217 | ||
2218 | cleanup(); | |
5e16da05 | 2219 | exit(EXIT_SUCCESS); |
fac6795d DG |
2220 | |
2221 | error: | |
2222 | cleanup(); | |
5e16da05 | 2223 | exit(EXIT_FAILURE); |
fac6795d | 2224 | } |