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