Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fac6795d | 4 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
91d76f53 | 8 | * |
d14d33bf AM |
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 | * |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
fac6795d DG |
20 | #include <getopt.h> |
21 | #include <grp.h> | |
22 | #include <limits.h> | |
23 | #include <pthread.h> | |
24 | #include <signal.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
0fdd1e2c | 28 | #include <sys/mman.h> |
b73401da | 29 | #include <sys/mount.h> |
1e307fab | 30 | #include <sys/resource.h> |
fac6795d DG |
31 | #include <sys/socket.h> |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
0fdd1e2c | 34 | #include <sys/wait.h> |
5c827ce0 | 35 | #include <urcu/uatomic.h> |
fac6795d | 36 | #include <unistd.h> |
3bd1e081 | 37 | #include <config.h> |
fac6795d | 38 | |
990570ed | 39 | #include <common/common.h> |
d27c42b8 | 40 | #include <common/compat/socket.h> |
db758600 DG |
41 | #include <common/defaults.h> |
42 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 43 | #include <common/futex.h> |
00e2e675 | 44 | #include <common/relayd/relayd.h> |
81b86775 | 45 | #include <common/utils.h> |
fac6795d | 46 | |
10a8a223 | 47 | #include "lttng-sessiond.h" |
54d01ffb | 48 | #include "channel.h" |
2f77fc4b | 49 | #include "cmd.h" |
00e2e675 | 50 | #include "consumer.h" |
099e26bd | 51 | #include "context.h" |
54d01ffb | 52 | #include "event.h" |
4771f025 | 53 | #include "kernel.h" |
f1e16794 | 54 | #include "kernel-consumer.h" |
096102bd | 55 | #include "modprobe.h" |
0fdd1e2c | 56 | #include "shm.h" |
1e307fab | 57 | #include "ust-ctl.h" |
00e2e675 | 58 | #include "ust-consumer.h" |
8e68d1c8 | 59 | #include "utils.h" |
4063050c | 60 | #include "fd-limit.h" |
44a5e5eb | 61 | #include "health.h" |
8ac94142 | 62 | #include "testpoint.h" |
d0b96690 | 63 | #include "ust-thread.h" |
fac6795d | 64 | |
ebaeda94 MD |
65 | #define CONSUMERD_FILE "lttng-consumerd" |
66 | ||
75462a81 | 67 | /* Const values */ |
bbccc3d2 | 68 | const char default_tracing_group[] = DEFAULT_TRACING_GROUP; |
686204ab | 69 | |
fac6795d DG |
70 | const char *progname; |
71 | const char *opt_tracing_group; | |
35f90c40 | 72 | static const char *opt_pidfile; |
5b8719f5 | 73 | static int opt_sig_parent; |
97e19046 | 74 | static int opt_verbose_consumer; |
fac6795d | 75 | static int opt_daemon; |
4fba7219 | 76 | static int opt_no_kernel; |
fac6795d | 77 | static int is_root; /* Set to 1 if the daemon is running as root */ |
1d4b027a | 78 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
67e40797 | 79 | static char *rundir; |
3bd1e081 | 80 | |
a23ec3a7 DG |
81 | /* |
82 | * Consumer daemon specific control data. Every value not initialized here is | |
83 | * set to 0 by the static definition. | |
84 | */ | |
3bd1e081 MD |
85 | static struct consumer_data kconsumer_data = { |
86 | .type = LTTNG_CONSUMER_KERNEL, | |
60922cb0 DG |
87 | .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
88 | .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
03550b58 MD |
89 | .err_sock = -1, |
90 | .cmd_sock = -1, | |
173af62f DG |
91 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
92 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
93 | .cond = PTHREAD_COND_INITIALIZER, |
94 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 | 95 | }; |
7753dea8 MD |
96 | static struct consumer_data ustconsumer64_data = { |
97 | .type = LTTNG_CONSUMER64_UST, | |
60922cb0 DG |
98 | .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, |
99 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, | |
03550b58 MD |
100 | .err_sock = -1, |
101 | .cmd_sock = -1, | |
173af62f DG |
102 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
103 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
104 | .cond = PTHREAD_COND_INITIALIZER, |
105 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
106 | }; |
107 | static struct consumer_data ustconsumer32_data = { | |
108 | .type = LTTNG_CONSUMER32_UST, | |
60922cb0 DG |
109 | .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, |
110 | .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, | |
03550b58 MD |
111 | .err_sock = -1, |
112 | .cmd_sock = -1, | |
173af62f DG |
113 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
114 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
115 | .cond = PTHREAD_COND_INITIALIZER, |
116 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
117 | }; |
118 | ||
26c9d55e | 119 | /* Shared between threads */ |
099e26bd | 120 | static int dispatch_thread_exit; |
fac6795d | 121 | |
54d01ffb DG |
122 | /* Global application Unix socket path */ |
123 | static char apps_unix_sock_path[PATH_MAX]; | |
124 | /* Global client Unix socket path */ | |
125 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
126 | /* global wait shm path for UST */ |
127 | static char wait_shm_path[PATH_MAX]; | |
44a5e5eb DG |
128 | /* Global health check unix path */ |
129 | static char health_unix_sock_path[PATH_MAX]; | |
fac6795d | 130 | |
1d4b027a | 131 | /* Sockets and FDs */ |
a4b35e07 MD |
132 | static int client_sock = -1; |
133 | static int apps_sock = -1; | |
2f77fc4b | 134 | int kernel_tracer_fd = -1; |
76d7553f | 135 | static int kernel_poll_pipe[2] = { -1, -1 }; |
1d4b027a | 136 | |
273ea72c DG |
137 | /* |
138 | * Quit pipe for all threads. This permits a single cancellation point | |
139 | * for all threads when receiving an event on the pipe. | |
140 | */ | |
76d7553f | 141 | static int thread_quit_pipe[2] = { -1, -1 }; |
273ea72c | 142 | |
099e26bd DG |
143 | /* |
144 | * This pipe is used to inform the thread managing application communication | |
145 | * that a command is queued and ready to be processed. | |
146 | */ | |
76d7553f | 147 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 148 | |
d0b96690 DG |
149 | int apps_cmd_notify_pipe[2] = { -1, -1 }; |
150 | ||
1d4b027a | 151 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 152 | static pthread_t apps_thread; |
d0b96690 | 153 | static pthread_t apps_notify_thread; |
099e26bd | 154 | static pthread_t reg_apps_thread; |
1d4b027a | 155 | static pthread_t client_thread; |
7a485870 | 156 | static pthread_t kernel_thread; |
099e26bd | 157 | static pthread_t dispatch_thread; |
44a5e5eb | 158 | static pthread_t health_thread; |
5eb91c98 | 159 | |
099e26bd DG |
160 | /* |
161 | * UST registration command queue. This queue is tied with a futex and uses a N | |
162 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
163 | * | |
164 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
165 | * this queue and the wait/wake scheme. | |
166 | */ | |
167 | static struct ust_cmd_queue ust_cmd_queue; | |
168 | ||
b5541356 DG |
169 | /* |
170 | * Pointer initialized before thread creation. | |
171 | * | |
172 | * This points to the tracing session list containing the session count and a | |
173 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
174 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 175 | * |
d063d709 | 176 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 177 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
178 | */ |
179 | static struct ltt_session_list *session_list_ptr; | |
180 | ||
7753dea8 MD |
181 | int ust_consumerd64_fd = -1; |
182 | int ust_consumerd32_fd = -1; | |
183 | ||
fb6f1fa2 YB |
184 | static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN; |
185 | static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN; | |
186 | static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR; | |
187 | static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR; | |
fb09408a | 188 | |
2f77fc4b DG |
189 | static const char *module_proc_lttng = "/proc/lttng"; |
190 | ||
5c827ce0 DG |
191 | /* |
192 | * Consumer daemon state which is changed when spawning it, killing it or in | |
193 | * case of a fatal error. | |
194 | */ | |
195 | enum consumerd_state { | |
196 | CONSUMER_STARTED = 1, | |
197 | CONSUMER_STOPPED = 2, | |
198 | CONSUMER_ERROR = 3, | |
199 | }; | |
200 | ||
201 | /* | |
202 | * This consumer daemon state is used to validate if a client command will be | |
203 | * able to reach the consumer. If not, the client is informed. For instance, | |
204 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
205 | * error to the client. | |
206 | * | |
207 | * The following example shows a possible race condition of this scheme: | |
208 | * | |
209 | * consumer thread error happens | |
210 | * client cmd arrives | |
211 | * client cmd checks state -> still OK | |
212 | * consumer thread exit, sets error | |
213 | * client cmd try to talk to consumer | |
214 | * ... | |
215 | * | |
216 | * However, since the consumer is a different daemon, we have no way of making | |
217 | * sure the command will reach it safely even with this state flag. This is why | |
218 | * we consider that up to the state validation during command processing, the | |
219 | * command is safe. After that, we can not guarantee the correctness of the | |
220 | * client request vis-a-vis the consumer. | |
221 | */ | |
222 | static enum consumerd_state ust_consumerd_state; | |
223 | static enum consumerd_state kernel_consumerd_state; | |
224 | ||
ae9e45b3 DG |
225 | /* |
226 | * Socket timeout for receiving and sending in seconds. | |
227 | */ | |
228 | static int app_socket_timeout; | |
229 | ||
fb09408a | 230 | static |
7753dea8 | 231 | void setup_consumerd_path(void) |
fb09408a | 232 | { |
fc7a59ce | 233 | const char *bin, *libdir; |
fb09408a | 234 | |
7753dea8 MD |
235 | /* |
236 | * Allow INSTALL_BIN_PATH to be used as a target path for the | |
ebaeda94 MD |
237 | * native architecture size consumer if CONFIG_CONSUMER*_PATH |
238 | * has not been defined. | |
7753dea8 | 239 | */ |
ebaeda94 | 240 | #if (CAA_BITS_PER_LONG == 32) |
fc7a59ce AM |
241 | if (!consumerd32_bin[0]) { |
242 | consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
ebaeda94 MD |
243 | } |
244 | if (!consumerd32_libdir[0]) { | |
245 | consumerd32_libdir = INSTALL_LIB_PATH; | |
246 | } | |
247 | #elif (CAA_BITS_PER_LONG == 64) | |
fc7a59ce AM |
248 | if (!consumerd64_bin[0]) { |
249 | consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE; | |
7753dea8 | 250 | } |
ebaeda94 MD |
251 | if (!consumerd64_libdir[0]) { |
252 | consumerd64_libdir = INSTALL_LIB_PATH; | |
7753dea8 MD |
253 | } |
254 | #else | |
255 | #error "Unknown bitness" | |
256 | #endif | |
257 | ||
fb09408a MD |
258 | /* |
259 | * runtime env. var. overrides the build default. | |
260 | */ | |
fc7a59ce AM |
261 | bin = getenv("LTTNG_CONSUMERD32_BIN"); |
262 | if (bin) { | |
263 | consumerd32_bin = bin; | |
7753dea8 | 264 | } |
fc7a59ce AM |
265 | bin = getenv("LTTNG_CONSUMERD64_BIN"); |
266 | if (bin) { | |
267 | consumerd64_bin = bin; | |
ebaeda94 | 268 | } |
72f579ee | 269 | libdir = getenv("LTTNG_CONSUMERD32_LIBDIR"); |
ebaeda94 MD |
270 | if (libdir) { |
271 | consumerd32_libdir = libdir; | |
272 | } | |
72f579ee | 273 | libdir = getenv("LTTNG_CONSUMERD64_LIBDIR"); |
ebaeda94 MD |
274 | if (libdir) { |
275 | consumerd64_libdir = libdir; | |
fb09408a MD |
276 | } |
277 | } | |
278 | ||
5eb91c98 DG |
279 | /* |
280 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
281 | */ | |
d0b96690 | 282 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) |
5eb91c98 DG |
283 | { |
284 | int ret; | |
285 | ||
d0b96690 | 286 | assert(events); |
5eb91c98 DG |
287 | |
288 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
289 | if (ret < 0) { | |
290 | goto error; | |
291 | } | |
292 | ||
293 | /* Add quit pipe */ | |
d0b96690 | 294 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
5eb91c98 DG |
295 | if (ret < 0) { |
296 | goto error; | |
297 | } | |
298 | ||
299 | return 0; | |
300 | ||
301 | error: | |
302 | return ret; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Check if the thread quit pipe was triggered. | |
307 | * | |
308 | * Return 1 if it was triggered else 0; | |
309 | */ | |
d0b96690 | 310 | int sessiond_check_thread_quit_pipe(int fd, uint32_t events) |
5eb91c98 DG |
311 | { |
312 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
313 | return 1; | |
314 | } | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
0fdd1e2c DG |
319 | /* |
320 | * Return group ID of the tracing group or -1 if not found. | |
321 | */ | |
996b65c8 MD |
322 | static gid_t allowed_group(void) |
323 | { | |
324 | struct group *grp; | |
325 | ||
274e143b MD |
326 | if (opt_tracing_group) { |
327 | grp = getgrnam(opt_tracing_group); | |
328 | } else { | |
329 | grp = getgrnam(default_tracing_group); | |
330 | } | |
996b65c8 MD |
331 | if (!grp) { |
332 | return -1; | |
333 | } else { | |
334 | return grp->gr_gid; | |
335 | } | |
336 | } | |
337 | ||
273ea72c | 338 | /* |
5eb91c98 | 339 | * Init thread quit pipe. |
273ea72c DG |
340 | * |
341 | * Return -1 on error or 0 if all pipes are created. | |
342 | */ | |
343 | static int init_thread_quit_pipe(void) | |
344 | { | |
730389d9 | 345 | int ret, i; |
273ea72c | 346 | |
730389d9 | 347 | ret = pipe(thread_quit_pipe); |
273ea72c | 348 | if (ret < 0) { |
730389d9 | 349 | PERROR("thread quit pipe"); |
273ea72c DG |
350 | goto error; |
351 | } | |
352 | ||
730389d9 DG |
353 | for (i = 0; i < 2; i++) { |
354 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
355 | if (ret < 0) { | |
356 | PERROR("fcntl"); | |
357 | goto error; | |
358 | } | |
359 | } | |
360 | ||
273ea72c DG |
361 | error: |
362 | return ret; | |
363 | } | |
364 | ||
099e26bd DG |
365 | /* |
366 | * Stop all threads by closing the thread quit pipe. | |
367 | */ | |
cf3af59e MD |
368 | static void stop_threads(void) |
369 | { | |
5eb91c98 DG |
370 | int ret; |
371 | ||
cf3af59e MD |
372 | /* Stopping all threads */ |
373 | DBG("Terminating all threads"); | |
54d01ffb | 374 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
375 | if (ret < 0) { |
376 | ERR("write error on thread quit pipe"); | |
377 | } | |
378 | ||
099e26bd | 379 | /* Dispatch thread */ |
26c9d55e | 380 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 381 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
382 | } |
383 | ||
fac6795d | 384 | /* |
d063d709 | 385 | * Cleanup the daemon |
fac6795d | 386 | */ |
cf3af59e | 387 | static void cleanup(void) |
fac6795d | 388 | { |
ef599319 | 389 | int ret; |
6620da75 | 390 | char *cmd = NULL; |
af9737e9 | 391 | struct ltt_session *sess, *stmp; |
fac6795d | 392 | |
1d4b027a | 393 | DBG("Cleaning up"); |
e07ae692 | 394 | |
2f77fc4b DG |
395 | /* First thing first, stop all threads */ |
396 | utils_close_pipe(thread_quit_pipe); | |
397 | ||
35f90c40 DG |
398 | /* |
399 | * If opt_pidfile is undefined, the default file will be wiped when | |
400 | * removing the rundir. | |
401 | */ | |
402 | if (opt_pidfile) { | |
403 | ret = remove(opt_pidfile); | |
404 | if (ret < 0) { | |
405 | PERROR("remove pidfile %s", opt_pidfile); | |
406 | } | |
407 | } | |
408 | ||
67e40797 DG |
409 | DBG("Removing %s directory", rundir); |
410 | ret = asprintf(&cmd, "rm -rf %s", rundir); | |
411 | if (ret < 0) { | |
412 | ERR("asprintf failed. Something is really wrong!"); | |
413 | } | |
5461b305 | 414 | |
67e40797 DG |
415 | /* Remove lttng run directory */ |
416 | ret = system(cmd); | |
417 | if (ret < 0) { | |
418 | ERR("Unable to clean %s", rundir); | |
1d4b027a | 419 | } |
67e40797 | 420 | free(cmd); |
bd69add2 | 421 | free(rundir); |
5461b305 | 422 | |
99bab54f | 423 | DBG("Cleaning up all sessions"); |
fac6795d | 424 | |
b5541356 | 425 | /* Destroy session list mutex */ |
273ea72c DG |
426 | if (session_list_ptr != NULL) { |
427 | pthread_mutex_destroy(&session_list_ptr->lock); | |
428 | ||
429 | /* Cleanup ALL session */ | |
54d01ffb DG |
430 | cds_list_for_each_entry_safe(sess, stmp, |
431 | &session_list_ptr->head, list) { | |
2f77fc4b | 432 | cmd_destroy_session(sess, kernel_poll_pipe[1]); |
273ea72c DG |
433 | } |
434 | } | |
435 | ||
099e26bd | 436 | DBG("Closing all UST sockets"); |
56fff090 | 437 | ust_app_clean_list(); |
099e26bd | 438 | |
4fba7219 DG |
439 | if (is_root && !opt_no_kernel) { |
440 | DBG2("Closing kernel fd"); | |
a4b35e07 | 441 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
442 | ret = close(kernel_tracer_fd); |
443 | if (ret) { | |
444 | PERROR("close"); | |
445 | } | |
a4b35e07 | 446 | } |
2f50c8a3 | 447 | DBG("Unloading kernel modules"); |
096102bd | 448 | modprobe_remove_lttng_all(); |
2f50c8a3 | 449 | } |
2f77fc4b | 450 | |
421cb601 | 451 | /* <fun> */ |
f56a39af | 452 | DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" |
421cb601 DG |
453 | "Matthew, BEET driven development works!%c[%dm", |
454 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
455 | /* </fun> */ | |
fac6795d DG |
456 | } |
457 | ||
e065084a | 458 | /* |
d063d709 | 459 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 460 | * |
d063d709 | 461 | * Return lttcomm error code. |
e065084a DG |
462 | */ |
463 | static int send_unix_sock(int sock, void *buf, size_t len) | |
464 | { | |
465 | /* Check valid length */ | |
c617c0c6 | 466 | if (len == 0) { |
e065084a DG |
467 | return -1; |
468 | } | |
469 | ||
470 | return lttcomm_send_unix_sock(sock, buf, len); | |
471 | } | |
472 | ||
5461b305 | 473 | /* |
d063d709 | 474 | * Free memory of a command context structure. |
5461b305 | 475 | */ |
a2fb29a5 | 476 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 477 | { |
a2fb29a5 DG |
478 | DBG("Clean command context structure"); |
479 | if (*cmd_ctx) { | |
480 | if ((*cmd_ctx)->llm) { | |
481 | free((*cmd_ctx)->llm); | |
5461b305 | 482 | } |
a2fb29a5 DG |
483 | if ((*cmd_ctx)->lsm) { |
484 | free((*cmd_ctx)->lsm); | |
5461b305 | 485 | } |
a2fb29a5 DG |
486 | free(*cmd_ctx); |
487 | *cmd_ctx = NULL; | |
5461b305 DG |
488 | } |
489 | } | |
490 | ||
fac6795d | 491 | /* |
0fdd1e2c | 492 | * Notify UST applications using the shm mmap futex. |
fac6795d | 493 | */ |
0fdd1e2c | 494 | static int notify_ust_apps(int active) |
fac6795d | 495 | { |
0fdd1e2c | 496 | char *wait_shm_mmap; |
fac6795d | 497 | |
0fdd1e2c | 498 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 499 | |
0fdd1e2c DG |
500 | /* See shm.c for this call implying mmap, shm and futex calls */ |
501 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
502 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
503 | goto error; |
504 | } | |
505 | ||
0fdd1e2c DG |
506 | /* Wake waiting process */ |
507 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
508 | ||
509 | /* Apps notified successfully */ | |
510 | return 0; | |
fac6795d DG |
511 | |
512 | error: | |
0fdd1e2c | 513 | return -1; |
fac6795d DG |
514 | } |
515 | ||
e065084a | 516 | /* |
d063d709 DG |
517 | * Setup the outgoing data buffer for the response (llm) by allocating the |
518 | * right amount of memory and copying the original information from the lsm | |
519 | * structure. | |
ca95a216 | 520 | * |
d063d709 | 521 | * Return total size of the buffer pointed by buf. |
ca95a216 | 522 | */ |
5461b305 | 523 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 524 | { |
f3ed775e | 525 | int ret, buf_size; |
ca95a216 | 526 | |
f3ed775e | 527 | buf_size = size; |
5461b305 | 528 | |
ba7f0ae5 | 529 | cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); |
5461b305 | 530 | if (cmd_ctx->llm == NULL) { |
76d7553f | 531 | PERROR("zmalloc"); |
5461b305 | 532 | ret = -ENOMEM; |
ca95a216 DG |
533 | goto error; |
534 | } | |
535 | ||
5461b305 DG |
536 | /* Copy common data */ |
537 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 538 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 539 | |
5461b305 DG |
540 | cmd_ctx->llm->data_size = size; |
541 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
542 | ||
ca95a216 DG |
543 | return buf_size; |
544 | ||
545 | error: | |
546 | return ret; | |
547 | } | |
548 | ||
7a485870 | 549 | /* |
5eb91c98 | 550 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 551 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 552 | */ |
5eb91c98 | 553 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 554 | { |
5eb91c98 | 555 | int ret; |
7a485870 DG |
556 | struct ltt_session *session; |
557 | struct ltt_kernel_channel *channel; | |
558 | ||
5eb91c98 | 559 | DBG("Updating kernel poll set"); |
7a485870 | 560 | |
54d01ffb | 561 | session_lock_list(); |
b5541356 | 562 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 563 | session_lock(session); |
7a485870 | 564 | if (session->kernel_session == NULL) { |
54d01ffb | 565 | session_unlock(session); |
7a485870 DG |
566 | continue; |
567 | } | |
7a485870 | 568 | |
54d01ffb DG |
569 | cds_list_for_each_entry(channel, |
570 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
571 | /* Add channel fd to the kernel poll set */ |
572 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
573 | if (ret < 0) { | |
54d01ffb | 574 | session_unlock(session); |
5eb91c98 DG |
575 | goto error; |
576 | } | |
577 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 578 | } |
54d01ffb | 579 | session_unlock(session); |
7a485870 | 580 | } |
54d01ffb | 581 | session_unlock_list(); |
7a485870 | 582 | |
5eb91c98 | 583 | return 0; |
7a485870 DG |
584 | |
585 | error: | |
54d01ffb | 586 | session_unlock_list(); |
7a485870 DG |
587 | return -1; |
588 | } | |
589 | ||
590 | /* | |
54d01ffb | 591 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 592 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 593 | * |
d063d709 | 594 | * Useful for CPU hotplug feature. |
7a485870 | 595 | */ |
2bdd86d4 | 596 | static int update_kernel_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
597 | { |
598 | int ret = 0; | |
599 | struct ltt_session *session; | |
173af62f | 600 | struct ltt_kernel_session *ksess; |
7a485870 DG |
601 | struct ltt_kernel_channel *channel; |
602 | ||
603 | DBG("Updating kernel streams for channel fd %d", fd); | |
604 | ||
54d01ffb | 605 | session_lock_list(); |
b5541356 | 606 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 607 | session_lock(session); |
7a485870 | 608 | if (session->kernel_session == NULL) { |
54d01ffb | 609 | session_unlock(session); |
7a485870 DG |
610 | continue; |
611 | } | |
173af62f | 612 | ksess = session->kernel_session; |
d9800920 | 613 | |
173af62f | 614 | cds_list_for_each_entry(channel, &ksess->channel_list.head, list) { |
7a485870 DG |
615 | if (channel->fd == fd) { |
616 | DBG("Channel found, updating kernel streams"); | |
617 | ret = kernel_open_channel_stream(channel); | |
618 | if (ret < 0) { | |
b3c750d2 | 619 | goto error; |
7a485870 | 620 | } |
d9800920 | 621 | |
7a485870 | 622 | /* |
5eb91c98 DG |
623 | * Have we already sent fds to the consumer? If yes, it means |
624 | * that tracing is started so it is safe to send our updated | |
625 | * stream fds. | |
7a485870 | 626 | */ |
173af62f DG |
627 | if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) { |
628 | struct lttng_ht_iter iter; | |
629 | struct consumer_socket *socket; | |
630 | ||
e7fe706f | 631 | rcu_read_lock(); |
173af62f DG |
632 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
633 | &iter.iter, socket, node.node) { | |
634 | /* Code flow error */ | |
635 | assert(socket->fd >= 0); | |
636 | ||
637 | pthread_mutex_lock(socket->lock); | |
f50f23d9 | 638 | ret = kernel_consumer_send_channel_stream(socket, |
173af62f DG |
639 | channel, ksess); |
640 | pthread_mutex_unlock(socket->lock); | |
641 | if (ret < 0) { | |
e7fe706f | 642 | rcu_read_unlock(); |
173af62f DG |
643 | goto error; |
644 | } | |
7a485870 | 645 | } |
e7fe706f | 646 | rcu_read_unlock(); |
7a485870 | 647 | } |
b3c750d2 | 648 | goto error; |
7a485870 DG |
649 | } |
650 | } | |
54d01ffb | 651 | session_unlock(session); |
7a485870 | 652 | } |
54d01ffb | 653 | session_unlock_list(); |
b3c750d2 | 654 | return ret; |
7a485870 | 655 | |
b3c750d2 | 656 | error: |
54d01ffb DG |
657 | session_unlock(session); |
658 | session_unlock_list(); | |
7a485870 DG |
659 | return ret; |
660 | } | |
661 | ||
487cf67c | 662 | /* |
ffe60014 DG |
663 | * For each tracing session, update newly registered apps. The session list |
664 | * lock MUST be acquired before calling this. | |
487cf67c DG |
665 | */ |
666 | static void update_ust_app(int app_sock) | |
667 | { | |
668 | struct ltt_session *sess, *stmp; | |
669 | ||
670 | /* For all tracing session(s) */ | |
671 | cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) { | |
4ee14516 | 672 | session_lock(sess); |
421cb601 DG |
673 | if (sess->ust_session) { |
674 | ust_app_global_update(sess->ust_session, app_sock); | |
675 | } | |
4ee14516 | 676 | session_unlock(sess); |
487cf67c DG |
677 | } |
678 | } | |
679 | ||
7a485870 | 680 | /* |
d063d709 | 681 | * This thread manage event coming from the kernel. |
7a485870 | 682 | * |
d063d709 DG |
683 | * Features supported in this thread: |
684 | * -) CPU Hotplug | |
7a485870 DG |
685 | */ |
686 | static void *thread_manage_kernel(void *data) | |
687 | { | |
139ac872 | 688 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 689 | uint32_t revents, nb_fd; |
7a485870 | 690 | char tmp; |
5eb91c98 | 691 | struct lttng_poll_event events; |
7a485870 | 692 | |
6993eeb3 | 693 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 694 | |
927ca06a DG |
695 | health_register(HEALTH_TYPE_KERNEL); |
696 | ||
d5d63bf1 DG |
697 | /* |
698 | * This first step of the while is to clean this structure which could free | |
699 | * non NULL pointers so zero it before the loop. | |
700 | */ | |
701 | memset(&events, 0, sizeof(events)); | |
702 | ||
6993eeb3 CB |
703 | if (testpoint(thread_manage_kernel)) { |
704 | goto error_testpoint; | |
705 | } | |
8ac94142 | 706 | |
840cb59c | 707 | health_code_update(); |
44a5e5eb | 708 | |
6993eeb3 | 709 | if (testpoint(thread_manage_kernel_before_loop)) { |
d21b0d71 | 710 | goto error_testpoint; |
6993eeb3 CB |
711 | } |
712 | ||
7a485870 | 713 | while (1) { |
840cb59c | 714 | health_code_update(); |
44a5e5eb | 715 | |
7a485870 | 716 | if (update_poll_flag == 1) { |
d21b0d71 DG |
717 | /* Clean events object. We are about to populate it again. */ |
718 | lttng_poll_clean(&events); | |
719 | ||
d0b96690 | 720 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
721 | if (ret < 0) { |
722 | goto error_poll_create; | |
723 | } | |
724 | ||
725 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
726 | if (ret < 0) { | |
727 | goto error; | |
728 | } | |
5f822d0a | 729 | |
d21b0d71 | 730 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
731 | ret = update_kernel_poll(&events); |
732 | if (ret < 0) { | |
7a485870 DG |
733 | goto error; |
734 | } | |
735 | update_poll_flag = 0; | |
736 | } | |
737 | ||
d21b0d71 | 738 | DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events)); |
7a485870 DG |
739 | |
740 | /* Poll infinite value of time */ | |
88f2b785 | 741 | restart: |
a78af745 | 742 | health_poll_entry(); |
5eb91c98 | 743 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 744 | health_poll_exit(); |
7a485870 | 745 | if (ret < 0) { |
88f2b785 MD |
746 | /* |
747 | * Restart interrupted system call. | |
748 | */ | |
749 | if (errno == EINTR) { | |
750 | goto restart; | |
751 | } | |
7a485870 DG |
752 | goto error; |
753 | } else if (ret == 0) { | |
754 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
755 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
756 | "This should not have happened! Continuing..."); | |
7a485870 DG |
757 | continue; |
758 | } | |
759 | ||
0d9c5d77 DG |
760 | nb_fd = ret; |
761 | ||
5eb91c98 DG |
762 | for (i = 0; i < nb_fd; i++) { |
763 | /* Fetch once the poll data */ | |
764 | revents = LTTNG_POLL_GETEV(&events, i); | |
765 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 766 | |
840cb59c | 767 | health_code_update(); |
44a5e5eb | 768 | |
5eb91c98 | 769 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 770 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 771 | if (ret) { |
139ac872 MD |
772 | err = 0; |
773 | goto exit; | |
5eb91c98 | 774 | } |
7a485870 | 775 | |
5eb91c98 DG |
776 | /* Check for data on kernel pipe */ |
777 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
f921c78f DG |
778 | do { |
779 | ret = read(kernel_poll_pipe[0], &tmp, 1); | |
780 | } while (ret < 0 && errno == EINTR); | |
781 | /* | |
782 | * Ret value is useless here, if this pipe gets any actions an | |
783 | * update is required anyway. | |
784 | */ | |
5eb91c98 DG |
785 | update_poll_flag = 1; |
786 | continue; | |
787 | } else { | |
788 | /* | |
789 | * New CPU detected by the kernel. Adding kernel stream to | |
790 | * kernel session and updating the kernel consumer | |
791 | */ | |
792 | if (revents & LPOLLIN) { | |
2bdd86d4 | 793 | ret = update_kernel_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
794 | if (ret < 0) { |
795 | continue; | |
796 | } | |
797 | break; | |
798 | /* | |
799 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
800 | * and unregister kernel stream at this point. | |
801 | */ | |
7a485870 | 802 | } |
7a485870 DG |
803 | } |
804 | } | |
805 | } | |
806 | ||
139ac872 | 807 | exit: |
7a485870 | 808 | error: |
5eb91c98 | 809 | lttng_poll_clean(&events); |
76d7553f | 810 | error_poll_create: |
6993eeb3 | 811 | error_testpoint: |
6620da75 DG |
812 | utils_close_pipe(kernel_poll_pipe); |
813 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 814 | if (err) { |
840cb59c | 815 | health_error(); |
139ac872 | 816 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
817 | WARN("Kernel thread died unexpectedly. " |
818 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 819 | } |
927ca06a | 820 | health_unregister(); |
76d7553f | 821 | DBG("Kernel thread dying"); |
7a485870 DG |
822 | return NULL; |
823 | } | |
824 | ||
a23ec3a7 DG |
825 | /* |
826 | * Signal pthread condition of the consumer data that the thread. | |
827 | */ | |
828 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
829 | { | |
830 | pthread_mutex_lock(&data->cond_mutex); | |
831 | ||
832 | /* | |
833 | * The state is set before signaling. It can be any value, it's the waiter | |
834 | * job to correctly interpret this condition variable associated to the | |
835 | * consumer pthread_cond. | |
836 | * | |
837 | * A value of 0 means that the corresponding thread of the consumer data | |
838 | * was not started. 1 indicates that the thread has started and is ready | |
839 | * for action. A negative value means that there was an error during the | |
840 | * thread bootstrap. | |
841 | */ | |
842 | data->consumer_thread_is_ready = state; | |
843 | (void) pthread_cond_signal(&data->cond); | |
844 | ||
845 | pthread_mutex_unlock(&data->cond_mutex); | |
846 | } | |
847 | ||
1d4b027a | 848 | /* |
3bd1e081 | 849 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 850 | */ |
3bd1e081 | 851 | static void *thread_manage_consumer(void *data) |
1d4b027a | 852 | { |
139ac872 | 853 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 | 854 | uint32_t revents, nb_fd; |
1d4b027a | 855 | enum lttcomm_return_code code; |
5eb91c98 | 856 | struct lttng_poll_event events; |
3bd1e081 | 857 | struct consumer_data *consumer_data = data; |
1d4b027a | 858 | |
3bd1e081 | 859 | DBG("[thread] Manage consumer started"); |
1d4b027a | 860 | |
927ca06a DG |
861 | health_register(HEALTH_TYPE_CONSUMER); |
862 | ||
855060f8 | 863 | health_code_update(); |
9449cc75 | 864 | |
5eb91c98 DG |
865 | /* |
866 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. | |
867 | * Nothing more will be added to this poll set. | |
868 | */ | |
d0b96690 | 869 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 870 | if (ret < 0) { |
76d7553f | 871 | goto error_poll; |
5eb91c98 | 872 | } |
273ea72c | 873 | |
edb8b045 DG |
874 | /* |
875 | * The error socket here is already in a listening state which was done | |
876 | * just before spawning this thread to avoid a race between the consumer | |
877 | * daemon exec trying to connect and the listen() call. | |
878 | */ | |
3bd1e081 | 879 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
880 | if (ret < 0) { |
881 | goto error; | |
882 | } | |
883 | ||
840cb59c | 884 | health_code_update(); |
44a5e5eb | 885 | |
273ea72c | 886 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 887 | restart: |
a78af745 | 888 | health_poll_entry(); |
8ac94142 | 889 | |
6993eeb3 CB |
890 | if (testpoint(thread_manage_consumer)) { |
891 | goto error; | |
892 | } | |
8ac94142 | 893 | |
5eb91c98 | 894 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 895 | health_poll_exit(); |
273ea72c | 896 | if (ret < 0) { |
88f2b785 MD |
897 | /* |
898 | * Restart interrupted system call. | |
899 | */ | |
900 | if (errno == EINTR) { | |
901 | goto restart; | |
902 | } | |
273ea72c DG |
903 | goto error; |
904 | } | |
905 | ||
0d9c5d77 DG |
906 | nb_fd = ret; |
907 | ||
5eb91c98 DG |
908 | for (i = 0; i < nb_fd; i++) { |
909 | /* Fetch once the poll data */ | |
910 | revents = LTTNG_POLL_GETEV(&events, i); | |
911 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
912 | ||
840cb59c | 913 | health_code_update(); |
44a5e5eb | 914 | |
5eb91c98 | 915 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 916 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 917 | if (ret) { |
139ac872 MD |
918 | err = 0; |
919 | goto exit; | |
5eb91c98 DG |
920 | } |
921 | ||
922 | /* Event on the registration socket */ | |
3bd1e081 | 923 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 924 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 925 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
926 | goto error; |
927 | } | |
928 | } | |
273ea72c DG |
929 | } |
930 | ||
3bd1e081 | 931 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
932 | if (sock < 0) { |
933 | goto error; | |
934 | } | |
935 | ||
b662582b DG |
936 | /* |
937 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
938 | * show must go on. | |
939 | */ | |
940 | (void) utils_set_fd_cloexec(sock); | |
941 | ||
840cb59c | 942 | health_code_update(); |
44a5e5eb | 943 | |
3bd1e081 | 944 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 945 | |
712ea556 | 946 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
947 | ret = lttcomm_recv_unix_sock(sock, &code, |
948 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
949 | if (ret <= 0) { |
950 | goto error; | |
951 | } | |
952 | ||
840cb59c | 953 | health_code_update(); |
44a5e5eb | 954 | |
f73fabfd | 955 | if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 MD |
956 | consumer_data->cmd_sock = |
957 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
958 | if (consumer_data->cmd_sock < 0) { | |
a23ec3a7 DG |
959 | /* On error, signal condition and quit. */ |
960 | signal_consumer_condition(consumer_data, -1); | |
48842b30 | 961 | PERROR("consumer connect"); |
1d4b027a DG |
962 | goto error; |
963 | } | |
a23ec3a7 DG |
964 | signal_consumer_condition(consumer_data, 1); |
965 | DBG("Consumer command socket ready"); | |
1d4b027a | 966 | } else { |
3bd1e081 | 967 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
968 | lttcomm_get_readable_code(-code)); |
969 | goto error; | |
970 | } | |
971 | ||
54d01ffb | 972 | /* Remove the kconsumerd error sock since we've established a connexion */ |
3bd1e081 | 973 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 974 | if (ret < 0) { |
72079cae DG |
975 | goto error; |
976 | } | |
977 | ||
5eb91c98 DG |
978 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
979 | if (ret < 0) { | |
72079cae | 980 | goto error; |
5eb91c98 DG |
981 | } |
982 | ||
840cb59c | 983 | health_code_update(); |
44a5e5eb | 984 | |
5eb91c98 | 985 | /* Inifinite blocking call, waiting for transmission */ |
88f2b785 | 986 | restart_poll: |
a78af745 | 987 | health_poll_entry(); |
5eb91c98 | 988 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 989 | health_poll_exit(); |
5eb91c98 | 990 | if (ret < 0) { |
88f2b785 MD |
991 | /* |
992 | * Restart interrupted system call. | |
993 | */ | |
994 | if (errno == EINTR) { | |
995 | goto restart_poll; | |
996 | } | |
72079cae DG |
997 | goto error; |
998 | } | |
999 | ||
0d9c5d77 DG |
1000 | nb_fd = ret; |
1001 | ||
5eb91c98 DG |
1002 | for (i = 0; i < nb_fd; i++) { |
1003 | /* Fetch once the poll data */ | |
1004 | revents = LTTNG_POLL_GETEV(&events, i); | |
1005 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1006 | ||
840cb59c | 1007 | health_code_update(); |
44a5e5eb | 1008 | |
5eb91c98 | 1009 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1010 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1011 | if (ret) { |
139ac872 MD |
1012 | err = 0; |
1013 | goto exit; | |
5eb91c98 DG |
1014 | } |
1015 | ||
1016 | /* Event on the kconsumerd socket */ | |
1017 | if (pollfd == sock) { | |
1018 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 1019 | ERR("consumer err socket second poll error"); |
5eb91c98 DG |
1020 | goto error; |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | ||
840cb59c | 1025 | health_code_update(); |
44a5e5eb | 1026 | |
712ea556 | 1027 | /* Wait for any kconsumerd error */ |
54d01ffb DG |
1028 | ret = lttcomm_recv_unix_sock(sock, &code, |
1029 | sizeof(enum lttcomm_return_code)); | |
712ea556 | 1030 | if (ret <= 0) { |
3bd1e081 | 1031 | ERR("consumer closed the command socket"); |
712ea556 | 1032 | goto error; |
6f61d021 | 1033 | } |
1d4b027a | 1034 | |
3bd1e081 | 1035 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
712ea556 | 1036 | |
139ac872 | 1037 | exit: |
1d4b027a | 1038 | error: |
5c827ce0 DG |
1039 | /* Immediately set the consumerd state to stopped */ |
1040 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1041 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1042 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1043 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1044 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1045 | } else { | |
1046 | /* Code flow error... */ | |
1047 | assert(0); | |
1048 | } | |
1049 | ||
76d7553f MD |
1050 | if (consumer_data->err_sock >= 0) { |
1051 | ret = close(consumer_data->err_sock); | |
1052 | if (ret) { | |
1053 | PERROR("close"); | |
1054 | } | |
1055 | } | |
1056 | if (consumer_data->cmd_sock >= 0) { | |
1057 | ret = close(consumer_data->cmd_sock); | |
1058 | if (ret) { | |
1059 | PERROR("close"); | |
1060 | } | |
1061 | } | |
1062 | if (sock >= 0) { | |
1063 | ret = close(sock); | |
1064 | if (ret) { | |
1065 | PERROR("close"); | |
1066 | } | |
1067 | } | |
273ea72c | 1068 | |
3bd1e081 MD |
1069 | unlink(consumer_data->err_unix_sock_path); |
1070 | unlink(consumer_data->cmd_unix_sock_path); | |
1071 | consumer_data->pid = 0; | |
1d4b027a | 1072 | |
5eb91c98 | 1073 | lttng_poll_clean(&events); |
76d7553f | 1074 | error_poll: |
139ac872 | 1075 | if (err) { |
840cb59c | 1076 | health_error(); |
139ac872 MD |
1077 | ERR("Health error occurred in %s", __func__); |
1078 | } | |
927ca06a | 1079 | health_unregister(); |
76d7553f | 1080 | DBG("consumer thread cleanup completed"); |
0177d773 | 1081 | |
5eb91c98 | 1082 | return NULL; |
099e26bd DG |
1083 | } |
1084 | ||
099e26bd DG |
1085 | /* |
1086 | * This thread manage application communication. | |
1d4b027a DG |
1087 | */ |
1088 | static void *thread_manage_apps(void *data) | |
099e26bd | 1089 | { |
139ac872 | 1090 | int i, ret, pollfd, err = -1; |
5eb91c98 | 1091 | uint32_t revents, nb_fd; |
5eb91c98 | 1092 | struct lttng_poll_event events; |
099e26bd DG |
1093 | |
1094 | DBG("[thread] Manage application started"); | |
1095 | ||
f6a9efaa DG |
1096 | rcu_register_thread(); |
1097 | rcu_thread_online(); | |
1098 | ||
927ca06a DG |
1099 | health_register(HEALTH_TYPE_APP_MANAGE); |
1100 | ||
6993eeb3 CB |
1101 | if (testpoint(thread_manage_apps)) { |
1102 | goto error_testpoint; | |
1103 | } | |
1104 | ||
840cb59c | 1105 | health_code_update(); |
44a5e5eb | 1106 | |
d0b96690 | 1107 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1108 | if (ret < 0) { |
76d7553f | 1109 | goto error_poll_create; |
5eb91c98 | 1110 | } |
099e26bd | 1111 | |
5eb91c98 DG |
1112 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1113 | if (ret < 0) { | |
1114 | goto error; | |
1115 | } | |
099e26bd | 1116 | |
6993eeb3 CB |
1117 | if (testpoint(thread_manage_apps_before_loop)) { |
1118 | goto error; | |
1119 | } | |
8ac94142 | 1120 | |
840cb59c | 1121 | health_code_update(); |
44a5e5eb | 1122 | |
5eb91c98 | 1123 | while (1) { |
d21b0d71 | 1124 | DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events)); |
099e26bd DG |
1125 | |
1126 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1127 | restart: |
a78af745 | 1128 | health_poll_entry(); |
5eb91c98 | 1129 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1130 | health_poll_exit(); |
099e26bd | 1131 | if (ret < 0) { |
88f2b785 MD |
1132 | /* |
1133 | * Restart interrupted system call. | |
1134 | */ | |
1135 | if (errno == EINTR) { | |
1136 | goto restart; | |
1137 | } | |
099e26bd DG |
1138 | goto error; |
1139 | } | |
1140 | ||
0d9c5d77 DG |
1141 | nb_fd = ret; |
1142 | ||
5eb91c98 DG |
1143 | for (i = 0; i < nb_fd; i++) { |
1144 | /* Fetch once the poll data */ | |
1145 | revents = LTTNG_POLL_GETEV(&events, i); | |
1146 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1147 | ||
840cb59c | 1148 | health_code_update(); |
44a5e5eb | 1149 | |
5eb91c98 | 1150 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1151 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1152 | if (ret) { |
139ac872 MD |
1153 | err = 0; |
1154 | goto exit; | |
5eb91c98 | 1155 | } |
099e26bd | 1156 | |
5eb91c98 DG |
1157 | /* Inspect the apps cmd pipe */ |
1158 | if (pollfd == apps_cmd_pipe[0]) { | |
1159 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1160 | ERR("Apps command pipe error"); | |
0177d773 | 1161 | goto error; |
5eb91c98 | 1162 | } else if (revents & LPOLLIN) { |
d0b96690 DG |
1163 | int sock; |
1164 | ||
5eb91c98 | 1165 | /* Empty pipe */ |
f921c78f | 1166 | do { |
d0b96690 | 1167 | ret = read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
f921c78f | 1168 | } while (ret < 0 && errno == EINTR); |
d0b96690 | 1169 | if (ret < 0 || ret < sizeof(sock)) { |
76d7553f | 1170 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1171 | goto error; |
1172 | } | |
099e26bd | 1173 | |
840cb59c | 1174 | health_code_update(); |
44a5e5eb | 1175 | |
ffe60014 | 1176 | /* |
d0b96690 DG |
1177 | * We only monitor the error events of the socket. This |
1178 | * thread does not handle any incoming data from UST | |
1179 | * (POLLIN). | |
ffe60014 | 1180 | */ |
d0b96690 DG |
1181 | ret = lttng_poll_add(&events, sock, |
1182 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1183 | if (ret < 0) { | |
5eb91c98 | 1184 | goto error; |
e0c7ec2b | 1185 | } |
acc7b41b | 1186 | |
d0b96690 DG |
1187 | /* Set socket timeout for both receiving and ending */ |
1188 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
1189 | app_socket_timeout); | |
1190 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
1191 | app_socket_timeout); | |
ae9e45b3 | 1192 | |
d0b96690 | 1193 | DBG("Apps with sock %d added to poll set", sock); |
487cf67c | 1194 | |
840cb59c | 1195 | health_code_update(); |
44a5e5eb | 1196 | |
5eb91c98 | 1197 | break; |
0177d773 | 1198 | } |
5eb91c98 DG |
1199 | } else { |
1200 | /* | |
54d01ffb DG |
1201 | * At this point, we know that a registered application made |
1202 | * the event at poll_wait. | |
5eb91c98 DG |
1203 | */ |
1204 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1205 | /* Removing from the poll set */ | |
1206 | ret = lttng_poll_del(&events, pollfd); | |
1207 | if (ret < 0) { | |
1208 | goto error; | |
1209 | } | |
099e26bd | 1210 | |
b9d9b220 | 1211 | /* Socket closed on remote end. */ |
56fff090 | 1212 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1213 | break; |
1214 | } | |
099e26bd | 1215 | } |
44a5e5eb | 1216 | |
840cb59c | 1217 | health_code_update(); |
099e26bd | 1218 | } |
099e26bd DG |
1219 | } |
1220 | ||
139ac872 | 1221 | exit: |
099e26bd | 1222 | error: |
5eb91c98 | 1223 | lttng_poll_clean(&events); |
76d7553f | 1224 | error_poll_create: |
6993eeb3 | 1225 | error_testpoint: |
6620da75 DG |
1226 | utils_close_pipe(apps_cmd_pipe); |
1227 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1228 | ||
1229 | /* | |
1230 | * We don't clean the UST app hash table here since already registered | |
1231 | * applications can still be controlled so let them be until the session | |
1232 | * daemon dies or the applications stop. | |
1233 | */ | |
1234 | ||
139ac872 | 1235 | if (err) { |
840cb59c | 1236 | health_error(); |
139ac872 MD |
1237 | ERR("Health error occurred in %s", __func__); |
1238 | } | |
927ca06a | 1239 | health_unregister(); |
76d7553f | 1240 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1241 | rcu_thread_offline(); |
1242 | rcu_unregister_thread(); | |
099e26bd DG |
1243 | return NULL; |
1244 | } | |
1245 | ||
d0b96690 DG |
1246 | /* |
1247 | * Send the application sockets (cmd and notify) to the respective threads. | |
1248 | * This is called from the dispatch UST registration thread once all sockets | |
1249 | * are set for the application. | |
1250 | * | |
1251 | * On success, return 0 else a negative value being the errno message of the | |
1252 | * write(). | |
1253 | */ | |
1254 | static int send_app_sockets_to_threads(struct ust_app *app) | |
1255 | { | |
1256 | int ret; | |
1257 | ||
1258 | assert(app); | |
1259 | /* Sockets MUST be set or else this should not have been called. */ | |
1260 | assert(app->sock >= 0); | |
1261 | assert(app->notify_sock >= 0); | |
1262 | assert(apps_cmd_pipe[1] >= 0); | |
1263 | assert(apps_cmd_notify_pipe[1] >= 0); | |
1264 | ||
1265 | do { | |
1266 | ret = write(apps_cmd_pipe[1], &app->sock, sizeof(app->sock)); | |
1267 | } while (ret < 0 && errno == EINTR); | |
1268 | if (ret < 0 || ret != sizeof(app->sock)) { | |
1269 | PERROR("write apps cmd pipe %d", apps_cmd_pipe[1]); | |
1270 | if (ret < 0) { | |
1271 | ret = -errno; | |
1272 | } | |
1273 | goto error; | |
1274 | } | |
1275 | ||
1276 | do { | |
1277 | ret = write(apps_cmd_notify_pipe[1], &app->notify_sock, | |
1278 | sizeof(app->notify_sock)); | |
1279 | } while (ret < 0 && errno == EINTR); | |
1280 | if (ret < 0 || ret != sizeof(app->notify_sock)) { | |
1281 | PERROR("write apps notify cmd pipe %d", apps_cmd_notify_pipe[1]); | |
1282 | if (ret < 0) { | |
1283 | ret = -errno; | |
1284 | } | |
1285 | goto error; | |
1286 | } | |
1287 | ||
1288 | /* All good. Don't send back the write positive ret value. */ | |
1289 | ret = 0; | |
1290 | error: | |
1291 | return ret; | |
1292 | } | |
1293 | ||
099e26bd DG |
1294 | /* |
1295 | * Dispatch request from the registration threads to the application | |
1296 | * communication thread. | |
1297 | */ | |
1298 | static void *thread_dispatch_ust_registration(void *data) | |
1299 | { | |
1300 | int ret; | |
1301 | struct cds_wfq_node *node; | |
1302 | struct ust_command *ust_cmd = NULL; | |
d0b96690 DG |
1303 | struct { |
1304 | struct ust_app *app; | |
1305 | struct cds_list_head head; | |
1306 | } *wait_node = NULL; | |
1307 | ||
1308 | CDS_LIST_HEAD(wait_queue); | |
099e26bd DG |
1309 | |
1310 | DBG("[thread] Dispatch UST command started"); | |
1311 | ||
26c9d55e | 1312 | while (!CMM_LOAD_SHARED(dispatch_thread_exit)) { |
099e26bd DG |
1313 | /* Atomically prepare the queue futex */ |
1314 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1315 | ||
1316 | do { | |
d0b96690 DG |
1317 | struct ust_app *app = NULL; |
1318 | ||
099e26bd DG |
1319 | /* Dequeue command for registration */ |
1320 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1321 | if (node == NULL) { | |
00a17c97 | 1322 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1323 | /* Continue thread execution */ |
1324 | break; | |
1325 | } | |
1326 | ||
1327 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1328 | ||
2f50c8a3 DG |
1329 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1330 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1331 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1332 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1333 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1334 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1335 | |
1336 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1337 | wait_node = zmalloc(sizeof(*wait_node)); | |
1338 | if (!wait_node) { | |
1339 | PERROR("zmalloc wait_node dispatch"); | |
1340 | goto error; | |
1341 | } | |
1342 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1343 | ||
1344 | /* Create application object if socket is CMD. */ | |
1345 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1346 | ust_cmd->sock); | |
1347 | if (!wait_node->app) { | |
1348 | ret = close(ust_cmd->sock); | |
1349 | if (ret < 0) { | |
1350 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1351 | } |
d0b96690 DG |
1352 | continue; |
1353 | } | |
1354 | /* | |
1355 | * Add application to the wait queue so we can set the notify | |
1356 | * socket before putting this object in the global ht. | |
1357 | */ | |
1358 | cds_list_add(&wait_node->head, &wait_queue); | |
1359 | ||
1360 | /* | |
1361 | * We have to continue here since we don't have the notify | |
1362 | * socket and the application MUST be added to the hash table | |
1363 | * only at that moment. | |
1364 | */ | |
1365 | continue; | |
1366 | } else { | |
1367 | /* | |
1368 | * Look for the application in the local wait queue and set the | |
1369 | * notify socket if found. | |
1370 | */ | |
1371 | cds_list_for_each_entry(wait_node, &wait_queue, head) { | |
1372 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { | |
1373 | wait_node->app->notify_sock = ust_cmd->sock; | |
1374 | cds_list_del(&wait_node->head); | |
1375 | app = wait_node->app; | |
1376 | free(wait_node); | |
1377 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1378 | break; | |
1379 | } | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | if (app) { | |
1384 | ret = send_app_sockets_to_threads(app); | |
1385 | if (ret < 0) { | |
1386 | goto error; | |
1387 | } | |
1388 | /* | |
1389 | * @session_lock_list | |
1390 | * | |
1391 | * Lock the global session list so from the register up to the | |
1392 | * registration done message, no thread can see the application | |
1393 | * and change its state. | |
1394 | */ | |
1395 | session_lock_list(); | |
1396 | rcu_read_lock(); | |
1397 | /* | |
1398 | * Add application to the global hash table. This needs to be | |
1399 | * done before the update to the UST registry can locate the | |
1400 | * application. | |
1401 | */ | |
1402 | ust_app_add(app); | |
1403 | /* | |
1404 | * Get app version. | |
1405 | */ | |
1406 | ret = ust_app_version(app); | |
1407 | if (ret) { | |
1408 | ERR("Unable to get app version"); | |
6620da75 | 1409 | } |
d0b96690 DG |
1410 | /* |
1411 | * Update newly registered application with the tracing | |
1412 | * registry info already enabled information. | |
1413 | */ | |
1414 | update_ust_app(app->sock); | |
1415 | ret = ust_app_register_done(app->sock); | |
1416 | if (ret < 0) { | |
1417 | /* Remove application from the registry. */ | |
1418 | ust_app_unregister(app->sock); | |
1419 | } | |
1420 | rcu_read_unlock(); | |
1421 | session_unlock_list(); | |
6620da75 | 1422 | } else { |
d0b96690 | 1423 | /* Application manager threads are not available. */ |
6620da75 DG |
1424 | ret = close(ust_cmd->sock); |
1425 | if (ret < 0) { | |
1426 | PERROR("close ust_cmd sock"); | |
099e26bd DG |
1427 | } |
1428 | } | |
1429 | free(ust_cmd); | |
1430 | } while (node != NULL); | |
1431 | ||
1432 | /* Futex wait on queue. Blocking call on futex() */ | |
1433 | futex_nto1_wait(&ust_cmd_queue.futex); | |
1434 | } | |
1435 | ||
1436 | error: | |
1437 | DBG("Dispatch thread dying"); | |
1438 | return NULL; | |
1439 | } | |
1440 | ||
1441 | /* | |
1442 | * This thread manage application registration. | |
1443 | */ | |
1444 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1445 | { |
139ac872 | 1446 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1447 | uint32_t revents, nb_fd; |
1448 | struct lttng_poll_event events; | |
099e26bd DG |
1449 | /* |
1450 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1451 | * freed in the manage apps thread. | |
1452 | */ | |
1453 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1454 | |
099e26bd | 1455 | DBG("[thread] Manage application registration started"); |
1d4b027a | 1456 | |
927ca06a DG |
1457 | health_register(HEALTH_TYPE_APP_REG); |
1458 | ||
6993eeb3 CB |
1459 | if (testpoint(thread_registration_apps)) { |
1460 | goto error_testpoint; | |
1461 | } | |
8ac94142 | 1462 | |
1d4b027a DG |
1463 | ret = lttcomm_listen_unix_sock(apps_sock); |
1464 | if (ret < 0) { | |
76d7553f | 1465 | goto error_listen; |
1d4b027a DG |
1466 | } |
1467 | ||
5eb91c98 DG |
1468 | /* |
1469 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1470 | * more will be added to this poll set. | |
1471 | */ | |
d0b96690 | 1472 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1473 | if (ret < 0) { |
76d7553f | 1474 | goto error_create_poll; |
5eb91c98 | 1475 | } |
273ea72c | 1476 | |
5eb91c98 DG |
1477 | /* Add the application registration socket */ |
1478 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1479 | if (ret < 0) { | |
76d7553f | 1480 | goto error_poll_add; |
5eb91c98 | 1481 | } |
273ea72c | 1482 | |
1d4b027a | 1483 | /* Notify all applications to register */ |
0fdd1e2c DG |
1484 | ret = notify_ust_apps(1); |
1485 | if (ret < 0) { | |
1486 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1487 | "Execution continues but there might be problem for already\n" |
1488 | "running applications that wishes to register."); | |
0fdd1e2c | 1489 | } |
1d4b027a DG |
1490 | |
1491 | while (1) { | |
1492 | DBG("Accepting application registration"); | |
273ea72c DG |
1493 | |
1494 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1495 | restart: |
a78af745 | 1496 | health_poll_entry(); |
5eb91c98 | 1497 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1498 | health_poll_exit(); |
273ea72c | 1499 | if (ret < 0) { |
88f2b785 MD |
1500 | /* |
1501 | * Restart interrupted system call. | |
1502 | */ | |
1503 | if (errno == EINTR) { | |
1504 | goto restart; | |
1505 | } | |
273ea72c DG |
1506 | goto error; |
1507 | } | |
1508 | ||
0d9c5d77 DG |
1509 | nb_fd = ret; |
1510 | ||
5eb91c98 | 1511 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 1512 | health_code_update(); |
139ac872 | 1513 | |
5eb91c98 DG |
1514 | /* Fetch once the poll data */ |
1515 | revents = LTTNG_POLL_GETEV(&events, i); | |
1516 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1517 | |
5eb91c98 | 1518 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1519 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1520 | if (ret) { |
139ac872 MD |
1521 | err = 0; |
1522 | goto exit; | |
90014c57 | 1523 | } |
1d4b027a | 1524 | |
5eb91c98 DG |
1525 | /* Event on the registration socket */ |
1526 | if (pollfd == apps_sock) { | |
1527 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1528 | ERR("Register apps socket poll error"); | |
1529 | goto error; | |
1530 | } else if (revents & LPOLLIN) { | |
1531 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1532 | if (sock < 0) { | |
1533 | goto error; | |
1534 | } | |
099e26bd | 1535 | |
b662582b DG |
1536 | /* |
1537 | * Set the CLOEXEC flag. Return code is useless because | |
1538 | * either way, the show must go on. | |
1539 | */ | |
1540 | (void) utils_set_fd_cloexec(sock); | |
1541 | ||
5eb91c98 | 1542 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1543 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1544 | if (ust_cmd == NULL) { |
76d7553f | 1545 | PERROR("ust command zmalloc"); |
5eb91c98 DG |
1546 | goto error; |
1547 | } | |
1d4b027a | 1548 | |
5eb91c98 DG |
1549 | /* |
1550 | * Using message-based transmissions to ensure we don't | |
1551 | * have to deal with partially received messages. | |
1552 | */ | |
4063050c MD |
1553 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
1554 | if (ret < 0) { | |
1555 | ERR("Exhausted file descriptors allowed for applications."); | |
1556 | free(ust_cmd); | |
1557 | ret = close(sock); | |
1558 | if (ret) { | |
1559 | PERROR("close"); | |
1560 | } | |
1561 | sock = -1; | |
1562 | continue; | |
1563 | } | |
840cb59c | 1564 | health_code_update(); |
d0b96690 DG |
1565 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
1566 | if (ret < 0) { | |
5eb91c98 | 1567 | free(ust_cmd); |
d0b96690 | 1568 | /* Close socket of the application. */ |
76d7553f MD |
1569 | ret = close(sock); |
1570 | if (ret) { | |
1571 | PERROR("close"); | |
1572 | } | |
4063050c | 1573 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 1574 | sock = -1; |
5eb91c98 DG |
1575 | continue; |
1576 | } | |
840cb59c | 1577 | health_code_update(); |
099e26bd | 1578 | |
5eb91c98 | 1579 | ust_cmd->sock = sock; |
34a2494f | 1580 | sock = -1; |
099e26bd | 1581 | |
5eb91c98 DG |
1582 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1583 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1584 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1585 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1586 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1587 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1588 | |
5eb91c98 DG |
1589 | /* |
1590 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1591 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1592 | */ |
1593 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1594 | ||
1595 | /* | |
1596 | * Wake the registration queue futex. Implicit memory | |
1597 | * barrier with the exchange in cds_wfq_enqueue. | |
1598 | */ | |
1599 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1600 | } | |
1601 | } | |
90014c57 | 1602 | } |
1d4b027a DG |
1603 | } |
1604 | ||
139ac872 | 1605 | exit: |
1d4b027a | 1606 | error: |
139ac872 | 1607 | if (err) { |
840cb59c | 1608 | health_error(); |
139ac872 MD |
1609 | ERR("Health error occurred in %s", __func__); |
1610 | } | |
139ac872 | 1611 | |
0fdd1e2c DG |
1612 | /* Notify that the registration thread is gone */ |
1613 | notify_ust_apps(0); | |
1614 | ||
a4b35e07 | 1615 | if (apps_sock >= 0) { |
76d7553f MD |
1616 | ret = close(apps_sock); |
1617 | if (ret) { | |
1618 | PERROR("close"); | |
1619 | } | |
a4b35e07 | 1620 | } |
46c3f085 | 1621 | if (sock >= 0) { |
76d7553f MD |
1622 | ret = close(sock); |
1623 | if (ret) { | |
1624 | PERROR("close"); | |
1625 | } | |
4063050c | 1626 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 1627 | } |
273ea72c | 1628 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1629 | |
76d7553f | 1630 | error_poll_add: |
5eb91c98 | 1631 | lttng_poll_clean(&events); |
76d7553f MD |
1632 | error_listen: |
1633 | error_create_poll: | |
6993eeb3 | 1634 | error_testpoint: |
76d7553f | 1635 | DBG("UST Registration thread cleanup complete"); |
927ca06a | 1636 | health_unregister(); |
5eb91c98 | 1637 | |
1d4b027a DG |
1638 | return NULL; |
1639 | } | |
1640 | ||
8c0faa1d | 1641 | /* |
3bd1e081 | 1642 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1643 | * exec or it will fails. |
8c0faa1d | 1644 | */ |
3bd1e081 | 1645 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 1646 | { |
a23ec3a7 | 1647 | int ret, clock_ret; |
ee0b0061 DG |
1648 | struct timespec timeout; |
1649 | ||
a23ec3a7 DG |
1650 | /* Make sure we set the readiness flag to 0 because we are NOT ready */ |
1651 | consumer_data->consumer_thread_is_ready = 0; | |
8c0faa1d | 1652 | |
a23ec3a7 DG |
1653 | /* Setup pthread condition */ |
1654 | ret = pthread_condattr_init(&consumer_data->condattr); | |
1655 | if (ret != 0) { | |
1656 | errno = ret; | |
1657 | PERROR("pthread_condattr_init consumer data"); | |
1658 | goto error; | |
1659 | } | |
1660 | ||
1661 | /* | |
1662 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
1663 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
1664 | * for a more details and how we noticed it. | |
1665 | */ | |
1666 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
1667 | if (ret != 0) { | |
1668 | errno = ret; | |
1669 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
1670 | goto error; |
1671 | } | |
8c0faa1d | 1672 | |
a23ec3a7 DG |
1673 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
1674 | if (ret != 0) { | |
1675 | errno = ret; | |
1676 | PERROR("pthread_cond_init consumer data"); | |
1677 | goto error; | |
1678 | } | |
1679 | ||
1680 | ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer, | |
1681 | consumer_data); | |
8c0faa1d | 1682 | if (ret != 0) { |
3bd1e081 | 1683 | PERROR("pthread_create consumer"); |
ee0b0061 | 1684 | ret = -1; |
8c0faa1d DG |
1685 | goto error; |
1686 | } | |
1687 | ||
a23ec3a7 DG |
1688 | /* We are about to wait on a pthread condition */ |
1689 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
1690 | ||
ee0b0061 | 1691 | /* Get time for sem_timedwait absolute timeout */ |
a23ec3a7 DG |
1692 | clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout); |
1693 | /* | |
1694 | * Set the timeout for the condition timed wait even if the clock gettime | |
1695 | * call fails since we might loop on that call and we want to avoid to | |
1696 | * increment the timeout too many times. | |
1697 | */ | |
1698 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
1699 | ||
1700 | /* | |
1701 | * The following loop COULD be skipped in some conditions so this is why we | |
1702 | * set ret to 0 in order to make sure at least one round of the loop is | |
1703 | * done. | |
1704 | */ | |
1705 | ret = 0; | |
1706 | ||
1707 | /* | |
1708 | * Loop until the condition is reached or when a timeout is reached. Note | |
1709 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
1710 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
1711 | * possible. This loop does not take any chances and works with both of | |
1712 | * them. | |
1713 | */ | |
1714 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
1715 | if (clock_ret < 0) { | |
1716 | PERROR("clock_gettime spawn consumer"); | |
1717 | /* Infinite wait for the consumerd thread to be ready */ | |
1718 | ret = pthread_cond_wait(&consumer_data->cond, | |
1719 | &consumer_data->cond_mutex); | |
1720 | } else { | |
1721 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
1722 | &consumer_data->cond_mutex, &timeout); | |
1723 | } | |
ee0b0061 | 1724 | } |
8c0faa1d | 1725 | |
a23ec3a7 DG |
1726 | /* Release the pthread condition */ |
1727 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
1728 | ||
1729 | if (ret != 0) { | |
1730 | errno = ret; | |
1731 | if (ret == ETIMEDOUT) { | |
ee0b0061 DG |
1732 | /* |
1733 | * Call has timed out so we kill the kconsumerd_thread and return | |
1734 | * an error. | |
1735 | */ | |
a23ec3a7 DG |
1736 | ERR("Condition timed out. The consumer thread was never ready." |
1737 | " Killing it"); | |
3bd1e081 | 1738 | ret = pthread_cancel(consumer_data->thread); |
ee0b0061 | 1739 | if (ret < 0) { |
3bd1e081 | 1740 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
1741 | } |
1742 | } else { | |
a23ec3a7 | 1743 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 DG |
1744 | } |
1745 | goto error; | |
1746 | } | |
1747 | ||
3bd1e081 MD |
1748 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1749 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 1750 | ERR("Consumerd did not start"); |
3bd1e081 | 1751 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
1752 | goto error; |
1753 | } | |
3bd1e081 | 1754 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 1755 | |
8c0faa1d DG |
1756 | return 0; |
1757 | ||
1758 | error: | |
1759 | return ret; | |
1760 | } | |
1761 | ||
d9800920 | 1762 | /* |
3bd1e081 | 1763 | * Join consumer thread |
d9800920 | 1764 | */ |
3bd1e081 | 1765 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
1766 | { |
1767 | void *status; | |
cf3af59e | 1768 | |
e8209f6b DG |
1769 | /* Consumer pid must be a real one. */ |
1770 | if (consumer_data->pid > 0) { | |
c617c0c6 | 1771 | int ret; |
3bd1e081 | 1772 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 1773 | if (ret) { |
3bd1e081 | 1774 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
1775 | return ret; |
1776 | } | |
3bd1e081 | 1777 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
1778 | } else { |
1779 | return 0; | |
1780 | } | |
1781 | } | |
1782 | ||
8c0faa1d | 1783 | /* |
3bd1e081 | 1784 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 1785 | * |
d063d709 | 1786 | * Return pid if successful else -1. |
8c0faa1d | 1787 | */ |
3bd1e081 | 1788 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
1789 | { |
1790 | int ret; | |
1791 | pid_t pid; | |
94c55f17 | 1792 | const char *consumer_to_use; |
53086306 | 1793 | const char *verbosity; |
94c55f17 | 1794 | struct stat st; |
8c0faa1d | 1795 | |
3bd1e081 | 1796 | DBG("Spawning consumerd"); |
c49dc785 | 1797 | |
8c0faa1d DG |
1798 | pid = fork(); |
1799 | if (pid == 0) { | |
1800 | /* | |
3bd1e081 | 1801 | * Exec consumerd. |
8c0faa1d | 1802 | */ |
daee5345 | 1803 | if (opt_verbose_consumer) { |
53086306 DG |
1804 | verbosity = "--verbose"; |
1805 | } else { | |
1806 | verbosity = "--quiet"; | |
1807 | } | |
3bd1e081 MD |
1808 | switch (consumer_data->type) { |
1809 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 1810 | /* |
c7704d57 DG |
1811 | * Find out which consumerd to execute. We will first try the |
1812 | * 64-bit path, then the sessiond's installation directory, and | |
1813 | * fallback on the 32-bit one, | |
94c55f17 | 1814 | */ |
63a799e8 AM |
1815 | DBG3("Looking for a kernel consumer at these locations:"); |
1816 | DBG3(" 1) %s", consumerd64_bin); | |
1817 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE); | |
1818 | DBG3(" 3) %s", consumerd32_bin); | |
94c55f17 | 1819 | if (stat(consumerd64_bin, &st) == 0) { |
63a799e8 | 1820 | DBG3("Found location #1"); |
94c55f17 | 1821 | consumer_to_use = consumerd64_bin; |
94c55f17 | 1822 | } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) { |
63a799e8 | 1823 | DBG3("Found location #2"); |
94c55f17 | 1824 | consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE; |
eb1e0bd4 | 1825 | } else if (stat(consumerd32_bin, &st) == 0) { |
63a799e8 | 1826 | DBG3("Found location #3"); |
eb1e0bd4 | 1827 | consumer_to_use = consumerd32_bin; |
94c55f17 | 1828 | } else { |
63a799e8 | 1829 | DBG("Could not find any valid consumerd executable"); |
94c55f17 AM |
1830 | break; |
1831 | } | |
1832 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
1833 | execl(consumer_to_use, | |
1834 | "lttng-consumerd", verbosity, "-k", | |
1835 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
1836 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1837 | NULL); | |
3bd1e081 | 1838 | break; |
7753dea8 MD |
1839 | case LTTNG_CONSUMER64_UST: |
1840 | { | |
b1e0b6b6 | 1841 | char *tmpnew = NULL; |
8f4905da MD |
1842 | |
1843 | if (consumerd64_libdir[0] != '\0') { | |
1844 | char *tmp; | |
1845 | size_t tmplen; | |
1846 | ||
1847 | tmp = getenv("LD_LIBRARY_PATH"); | |
1848 | if (!tmp) { | |
1849 | tmp = ""; | |
1850 | } | |
1851 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1852 | + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp); | |
1853 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1854 | if (!tmpnew) { | |
1855 | ret = -ENOMEM; | |
1856 | goto error; | |
1857 | } | |
1858 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1859 | strcat(tmpnew, consumerd64_libdir); | |
1860 | if (tmp[0] != '\0') { | |
1861 | strcat(tmpnew, ":"); | |
1862 | strcat(tmpnew, tmp); | |
1863 | } | |
1864 | ret = putenv(tmpnew); | |
1865 | if (ret) { | |
1866 | ret = -errno; | |
1867 | goto error; | |
1868 | } | |
1869 | } | |
94c55f17 | 1870 | DBG("Using 64-bit UST consumer at: %s", consumerd64_bin); |
a5a6aff3 | 1871 | ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1872 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1873 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1874 | NULL); | |
8f4905da MD |
1875 | if (consumerd64_libdir[0] != '\0') { |
1876 | free(tmpnew); | |
1877 | } | |
1878 | if (ret) { | |
1879 | goto error; | |
1880 | } | |
3bd1e081 | 1881 | break; |
7753dea8 MD |
1882 | } |
1883 | case LTTNG_CONSUMER32_UST: | |
1884 | { | |
937dde8e | 1885 | char *tmpnew = NULL; |
8f4905da MD |
1886 | |
1887 | if (consumerd32_libdir[0] != '\0') { | |
1888 | char *tmp; | |
1889 | size_t tmplen; | |
1890 | ||
1891 | tmp = getenv("LD_LIBRARY_PATH"); | |
1892 | if (!tmp) { | |
1893 | tmp = ""; | |
1894 | } | |
1895 | tmplen = strlen("LD_LIBRARY_PATH=") | |
1896 | + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp); | |
1897 | tmpnew = zmalloc(tmplen + 1 /* \0 */); | |
1898 | if (!tmpnew) { | |
1899 | ret = -ENOMEM; | |
1900 | goto error; | |
1901 | } | |
1902 | strcpy(tmpnew, "LD_LIBRARY_PATH="); | |
1903 | strcat(tmpnew, consumerd32_libdir); | |
1904 | if (tmp[0] != '\0') { | |
1905 | strcat(tmpnew, ":"); | |
1906 | strcat(tmpnew, tmp); | |
1907 | } | |
1908 | ret = putenv(tmpnew); | |
1909 | if (ret) { | |
1910 | ret = -errno; | |
1911 | goto error; | |
1912 | } | |
1913 | } | |
94c55f17 | 1914 | DBG("Using 32-bit UST consumer at: %s", consumerd32_bin); |
a5a6aff3 | 1915 | ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u", |
7753dea8 MD |
1916 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
1917 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
1918 | NULL); | |
8f4905da MD |
1919 | if (consumerd32_libdir[0] != '\0') { |
1920 | free(tmpnew); | |
1921 | } | |
1922 | if (ret) { | |
1923 | goto error; | |
1924 | } | |
7753dea8 MD |
1925 | break; |
1926 | } | |
3bd1e081 | 1927 | default: |
76d7553f | 1928 | PERROR("unknown consumer type"); |
3bd1e081 MD |
1929 | exit(EXIT_FAILURE); |
1930 | } | |
8c0faa1d | 1931 | if (errno != 0) { |
76d7553f | 1932 | PERROR("kernel start consumer exec"); |
8c0faa1d DG |
1933 | } |
1934 | exit(EXIT_FAILURE); | |
1935 | } else if (pid > 0) { | |
1936 | ret = pid; | |
8c0faa1d | 1937 | } else { |
76d7553f | 1938 | PERROR("start consumer fork"); |
8c0faa1d | 1939 | ret = -errno; |
8c0faa1d | 1940 | } |
8f4905da | 1941 | error: |
8c0faa1d DG |
1942 | return ret; |
1943 | } | |
1944 | ||
693bd40b | 1945 | /* |
3bd1e081 | 1946 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 1947 | */ |
3bd1e081 | 1948 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 1949 | { |
c617c0c6 | 1950 | int ret; |
edb8b045 DG |
1951 | |
1952 | /* | |
1953 | * Set the listen() state on the socket since there is a possible race | |
1954 | * between the exec() of the consumer daemon and this call if place in the | |
1955 | * consumer thread. See bug #366 for more details. | |
1956 | */ | |
1957 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
1958 | if (ret < 0) { | |
1959 | goto error; | |
1960 | } | |
693bd40b | 1961 | |
3bd1e081 MD |
1962 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1963 | if (consumer_data->pid != 0) { | |
1964 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
1965 | goto end; |
1966 | } | |
693bd40b | 1967 | |
3bd1e081 | 1968 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 1969 | if (ret < 0) { |
3bd1e081 MD |
1970 | ERR("Spawning consumerd failed"); |
1971 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 1972 | goto error; |
693bd40b | 1973 | } |
c49dc785 | 1974 | |
3bd1e081 MD |
1975 | /* Setting up the consumer_data pid */ |
1976 | consumer_data->pid = ret; | |
48842b30 | 1977 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 1978 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 1979 | |
3bd1e081 MD |
1980 | DBG2("Spawning consumer control thread"); |
1981 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 1982 | if (ret < 0) { |
3bd1e081 | 1983 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
1984 | goto error; |
1985 | } | |
1986 | ||
c49dc785 | 1987 | end: |
693bd40b DG |
1988 | return 0; |
1989 | ||
1990 | error: | |
edb8b045 DG |
1991 | /* Cleanup already created socket on error. */ |
1992 | if (consumer_data->err_sock >= 0) { | |
c617c0c6 MD |
1993 | int err; |
1994 | ||
edb8b045 DG |
1995 | err = close(consumer_data->err_sock); |
1996 | if (err < 0) { | |
1997 | PERROR("close consumer data error socket"); | |
1998 | } | |
1999 | } | |
693bd40b DG |
2000 | return ret; |
2001 | } | |
2002 | ||
44a5e5eb | 2003 | /* |
139ac872 MD |
2004 | * Compute health status of each consumer. If one of them is zero (bad |
2005 | * state), we return 0. | |
44a5e5eb DG |
2006 | */ |
2007 | static int check_consumer_health(void) | |
2008 | { | |
2009 | int ret; | |
2010 | ||
927ca06a | 2011 | ret = health_check_state(HEALTH_TYPE_CONSUMER); |
44a5e5eb DG |
2012 | |
2013 | DBG3("Health consumer check %d", ret); | |
2014 | ||
2015 | return ret; | |
2016 | } | |
2017 | ||
b73401da | 2018 | /* |
096102bd | 2019 | * Setup necessary data for kernel tracer action. |
b73401da | 2020 | */ |
096102bd | 2021 | static int init_kernel_tracer(void) |
b73401da DG |
2022 | { |
2023 | int ret; | |
b73401da | 2024 | |
096102bd DG |
2025 | /* Modprobe lttng kernel modules */ |
2026 | ret = modprobe_lttng_control(); | |
b73401da | 2027 | if (ret < 0) { |
b73401da DG |
2028 | goto error; |
2029 | } | |
2030 | ||
096102bd DG |
2031 | /* Open debugfs lttng */ |
2032 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2033 | if (kernel_tracer_fd < 0) { | |
2034 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b DG |
2035 | ret = -1; |
2036 | goto error_open; | |
54d01ffb DG |
2037 | } |
2038 | ||
2f77fc4b DG |
2039 | /* Validate kernel version */ |
2040 | ret = kernel_validate_version(kernel_tracer_fd); | |
2041 | if (ret < 0) { | |
2042 | goto error_version; | |
b551a063 | 2043 | } |
54d01ffb | 2044 | |
2f77fc4b DG |
2045 | ret = modprobe_lttng_data(); |
2046 | if (ret < 0) { | |
2047 | goto error_modules; | |
54d01ffb DG |
2048 | } |
2049 | ||
2f77fc4b DG |
2050 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2051 | return 0; | |
2052 | ||
2053 | error_version: | |
2054 | modprobe_remove_lttng_control(); | |
2055 | ret = close(kernel_tracer_fd); | |
2056 | if (ret) { | |
2057 | PERROR("close"); | |
b551a063 | 2058 | } |
2f77fc4b | 2059 | kernel_tracer_fd = -1; |
f73fabfd | 2060 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2061 | |
2f77fc4b DG |
2062 | error_modules: |
2063 | ret = close(kernel_tracer_fd); | |
2064 | if (ret) { | |
2065 | PERROR("close"); | |
b551a063 | 2066 | } |
54d01ffb | 2067 | |
2f77fc4b DG |
2068 | error_open: |
2069 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2070 | |
2071 | error: | |
2f77fc4b DG |
2072 | WARN("No kernel tracer available"); |
2073 | kernel_tracer_fd = -1; | |
2074 | if (!is_root) { | |
f73fabfd | 2075 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2076 | } else { |
f73fabfd | 2077 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2078 | } |
54d01ffb DG |
2079 | } |
2080 | ||
2f77fc4b | 2081 | |
54d01ffb | 2082 | /* |
2f77fc4b DG |
2083 | * Copy consumer output from the tracing session to the domain session. The |
2084 | * function also applies the right modification on a per domain basis for the | |
2085 | * trace files destination directory. | |
54d01ffb | 2086 | */ |
2f77fc4b | 2087 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2088 | { |
2089 | int ret; | |
2f77fc4b DG |
2090 | const char *dir_name; |
2091 | struct consumer_output *consumer; | |
2092 | ||
2093 | assert(session); | |
2094 | assert(session->consumer); | |
54d01ffb | 2095 | |
b551a063 DG |
2096 | switch (domain) { |
2097 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2098 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2099 | /* |
2100 | * XXX: We should audit the session creation and what this function | |
2101 | * does "extra" in order to avoid a destroy since this function is used | |
2102 | * in the domain session creation (kernel and ust) only. Same for UST | |
2103 | * domain. | |
2104 | */ | |
2105 | if (session->kernel_session->consumer) { | |
2106 | consumer_destroy_output(session->kernel_session->consumer); | |
2107 | } | |
2f77fc4b DG |
2108 | session->kernel_session->consumer = |
2109 | consumer_copy_output(session->consumer); | |
2110 | /* Ease our life a bit for the next part */ | |
2111 | consumer = session->kernel_session->consumer; | |
2112 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 DG |
2113 | break; |
2114 | case LTTNG_DOMAIN_UST: | |
2f77fc4b | 2115 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd DG |
2116 | if (session->ust_session->consumer) { |
2117 | consumer_destroy_output(session->ust_session->consumer); | |
2118 | } | |
2f77fc4b DG |
2119 | session->ust_session->consumer = |
2120 | consumer_copy_output(session->consumer); | |
2121 | /* Ease our life a bit for the next part */ | |
2122 | consumer = session->ust_session->consumer; | |
2123 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2124 | break; |
2125 | default: | |
f73fabfd | 2126 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2127 | goto error; |
2128 | } | |
2129 | ||
2f77fc4b | 2130 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2131 | strncat(consumer->subdir, dir_name, |
2132 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2133 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2134 | ||
f73fabfd | 2135 | ret = LTTNG_OK; |
54d01ffb DG |
2136 | |
2137 | error: | |
2138 | return ret; | |
2139 | } | |
2140 | ||
00e2e675 | 2141 | /* |
2f77fc4b | 2142 | * Create an UST session and add it to the session ust list. |
00e2e675 | 2143 | */ |
2f77fc4b DG |
2144 | static int create_ust_session(struct ltt_session *session, |
2145 | struct lttng_domain *domain) | |
00e2e675 DG |
2146 | { |
2147 | int ret; | |
2f77fc4b | 2148 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2149 | |
a4b92340 | 2150 | assert(session); |
2f77fc4b DG |
2151 | assert(domain); |
2152 | assert(session->consumer); | |
a4b92340 | 2153 | |
2f77fc4b DG |
2154 | switch (domain->type) { |
2155 | case LTTNG_DOMAIN_UST: | |
2156 | break; | |
2157 | default: | |
2158 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2159 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2160 | goto error; |
2161 | } | |
2162 | ||
2f77fc4b DG |
2163 | DBG("Creating UST session"); |
2164 | ||
fdd9eb17 | 2165 | lus = trace_ust_create_session(session->path, session->id); |
2f77fc4b | 2166 | if (lus == NULL) { |
f73fabfd | 2167 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2168 | goto error; |
2169 | } | |
2170 | ||
2f77fc4b DG |
2171 | lus->uid = session->uid; |
2172 | lus->gid = session->gid; | |
2173 | session->ust_session = lus; | |
00e2e675 | 2174 | |
2f77fc4b DG |
2175 | /* Copy session output to the newly created UST session */ |
2176 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2177 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2178 | goto error; |
2179 | } | |
2180 | ||
f73fabfd | 2181 | return LTTNG_OK; |
00e2e675 DG |
2182 | |
2183 | error: | |
2f77fc4b DG |
2184 | free(lus); |
2185 | session->ust_session = NULL; | |
00e2e675 DG |
2186 | return ret; |
2187 | } | |
2188 | ||
2189 | /* | |
2f77fc4b | 2190 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2191 | */ |
2f77fc4b | 2192 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2193 | { |
2194 | int ret; | |
a4b92340 | 2195 | |
2f77fc4b | 2196 | DBG("Creating kernel session"); |
00e2e675 | 2197 | |
2f77fc4b DG |
2198 | ret = kernel_create_session(session, kernel_tracer_fd); |
2199 | if (ret < 0) { | |
f73fabfd | 2200 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2201 | goto error; |
2202 | } | |
2203 | ||
2f77fc4b DG |
2204 | /* Code flow safety */ |
2205 | assert(session->kernel_session); | |
2206 | ||
2207 | /* Copy session output to the newly created Kernel session */ | |
2208 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2209 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2210 | goto error; |
2211 | } | |
2212 | ||
2f77fc4b DG |
2213 | /* Create directory(ies) on local filesystem. */ |
2214 | if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL && | |
2215 | strlen(session->kernel_session->consumer->dst.trace_path) > 0) { | |
2216 | ret = run_as_mkdir_recursive( | |
2217 | session->kernel_session->consumer->dst.trace_path, | |
2218 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2219 | if (ret < 0) { | |
2220 | if (ret != -EEXIST) { | |
2221 | ERR("Trace directory creation error"); | |
00e2e675 DG |
2222 | goto error; |
2223 | } | |
00e2e675 | 2224 | } |
2f77fc4b | 2225 | } |
00e2e675 | 2226 | |
2f77fc4b DG |
2227 | session->kernel_session->uid = session->uid; |
2228 | session->kernel_session->gid = session->gid; | |
00e2e675 | 2229 | |
f73fabfd | 2230 | return LTTNG_OK; |
00e2e675 | 2231 | |
2f77fc4b DG |
2232 | error: |
2233 | trace_kernel_destroy_session(session->kernel_session); | |
2234 | session->kernel_session = NULL; | |
2235 | return ret; | |
2236 | } | |
00e2e675 | 2237 | |
2f77fc4b DG |
2238 | /* |
2239 | * Count number of session permitted by uid/gid. | |
2240 | */ | |
2241 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2242 | { | |
2243 | unsigned int i = 0; | |
2244 | struct ltt_session *session; | |
07424f16 | 2245 | |
2f77fc4b DG |
2246 | DBG("Counting number of available session for UID %d GID %d", |
2247 | uid, gid); | |
2248 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
00e2e675 | 2249 | /* |
2f77fc4b | 2250 | * Only list the sessions the user can control. |
00e2e675 | 2251 | */ |
2f77fc4b DG |
2252 | if (!session_access_ok(session, uid, gid)) { |
2253 | continue; | |
2254 | } | |
2255 | i++; | |
a4b92340 | 2256 | } |
2f77fc4b | 2257 | return i; |
00e2e675 DG |
2258 | } |
2259 | ||
54d01ffb DG |
2260 | /* |
2261 | * Process the command requested by the lttng client within the command | |
2262 | * context structure. This function make sure that the return structure (llm) | |
2263 | * is set and ready for transmission before returning. | |
2264 | * | |
2265 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2266 | * |
2267 | * "sock" is only used for special-case var. len data. | |
54d01ffb | 2268 | */ |
53a80697 MD |
2269 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2270 | int *sock_error) | |
54d01ffb | 2271 | { |
f73fabfd | 2272 | int ret = LTTNG_OK; |
44d3bd01 | 2273 | int need_tracing_session = 1; |
2e09ba09 | 2274 | int need_domain; |
54d01ffb DG |
2275 | |
2276 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2277 | ||
53a80697 MD |
2278 | *sock_error = 0; |
2279 | ||
2e09ba09 MD |
2280 | switch (cmd_ctx->lsm->cmd_type) { |
2281 | case LTTNG_CREATE_SESSION: | |
2282 | case LTTNG_DESTROY_SESSION: | |
2283 | case LTTNG_LIST_SESSIONS: | |
2284 | case LTTNG_LIST_DOMAINS: | |
2285 | case LTTNG_START_TRACE: | |
2286 | case LTTNG_STOP_TRACE: | |
6d805429 | 2287 | case LTTNG_DATA_PENDING: |
2e09ba09 | 2288 | need_domain = 0; |
3aace903 | 2289 | break; |
2e09ba09 MD |
2290 | default: |
2291 | need_domain = 1; | |
2292 | } | |
2293 | ||
2294 | if (opt_no_kernel && need_domain | |
2295 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { | |
531d29f9 | 2296 | if (!is_root) { |
f73fabfd | 2297 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 2298 | } else { |
f73fabfd | 2299 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 2300 | } |
4fba7219 DG |
2301 | goto error; |
2302 | } | |
2303 | ||
8d3113b2 DG |
2304 | /* Deny register consumer if we already have a spawned consumer. */ |
2305 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
2306 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
2307 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 2308 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 2309 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
2310 | goto error; |
2311 | } | |
2312 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
2313 | } | |
2314 | ||
54d01ffb DG |
2315 | /* |
2316 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2317 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2318 | * command. |
2319 | */ | |
2320 | switch(cmd_ctx->lsm->cmd_type) { | |
2321 | case LTTNG_LIST_SESSIONS: | |
2322 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2323 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
2324 | case LTTNG_LIST_DOMAINS: |
2325 | case LTTNG_LIST_CHANNELS: | |
2326 | case LTTNG_LIST_EVENTS: | |
2327 | break; | |
2328 | default: | |
2329 | /* Setup lttng message with no payload */ | |
2330 | ret = setup_lttng_msg(cmd_ctx, 0); | |
2331 | if (ret < 0) { | |
2332 | /* This label does not try to unlock the session */ | |
2333 | goto init_setup_error; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | /* Commands that DO NOT need a session. */ | |
2338 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 2339 | case LTTNG_CREATE_SESSION: |
2e09ba09 | 2340 | case LTTNG_CALIBRATE: |
54d01ffb DG |
2341 | case LTTNG_LIST_SESSIONS: |
2342 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2343 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
44d3bd01 | 2344 | need_tracing_session = 0; |
54d01ffb DG |
2345 | break; |
2346 | default: | |
2347 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
2348 | /* |
2349 | * We keep the session list lock across _all_ commands | |
2350 | * for now, because the per-session lock does not | |
2351 | * handle teardown properly. | |
2352 | */ | |
74babd95 | 2353 | session_lock_list(); |
54d01ffb DG |
2354 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
2355 | if (cmd_ctx->session == NULL) { | |
2356 | if (cmd_ctx->lsm->session.name != NULL) { | |
f73fabfd | 2357 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
2358 | } else { |
2359 | /* If no session name specified */ | |
f73fabfd | 2360 | ret = LTTNG_ERR_SELECT_SESS; |
54d01ffb DG |
2361 | } |
2362 | goto error; | |
2363 | } else { | |
2364 | /* Acquire lock for the session */ | |
2365 | session_lock(cmd_ctx->session); | |
2366 | } | |
2367 | break; | |
2368 | } | |
b389abbe | 2369 | |
2e09ba09 MD |
2370 | if (!need_domain) { |
2371 | goto skip_domain; | |
2372 | } | |
a4b92340 | 2373 | |
54d01ffb DG |
2374 | /* |
2375 | * Check domain type for specific "pre-action". | |
2376 | */ | |
2377 | switch (cmd_ctx->lsm->domain.type) { | |
2378 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 2379 | if (!is_root) { |
f73fabfd | 2380 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
2381 | goto error; |
2382 | } | |
2383 | ||
54d01ffb | 2384 | /* Kernel tracer check */ |
a4b35e07 | 2385 | if (kernel_tracer_fd == -1) { |
54d01ffb | 2386 | /* Basically, load kernel tracer modules */ |
096102bd DG |
2387 | ret = init_kernel_tracer(); |
2388 | if (ret != 0) { | |
54d01ffb DG |
2389 | goto error; |
2390 | } | |
2391 | } | |
5eb91c98 | 2392 | |
5c827ce0 DG |
2393 | /* Consumer is in an ERROR state. Report back to client */ |
2394 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2395 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2396 | goto error; |
2397 | } | |
2398 | ||
54d01ffb | 2399 | /* Need a session for kernel command */ |
44d3bd01 | 2400 | if (need_tracing_session) { |
54d01ffb | 2401 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 2402 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 2403 | if (ret < 0) { |
f73fabfd | 2404 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
2405 | goto error; |
2406 | } | |
b389abbe | 2407 | } |
7d29a247 | 2408 | |
54d01ffb | 2409 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
2410 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
2411 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 2412 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
2413 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
2414 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 2415 | if (ret < 0) { |
f73fabfd | 2416 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 2417 | goto error; |
950131af | 2418 | } |
5c827ce0 | 2419 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
2420 | } else { |
2421 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 2422 | } |
173af62f | 2423 | |
a4b92340 DG |
2424 | /* |
2425 | * The consumer was just spawned so we need to add the socket to | |
2426 | * the consumer output of the session if exist. | |
2427 | */ | |
2428 | ret = consumer_create_socket(&kconsumer_data, | |
2429 | cmd_ctx->session->kernel_session->consumer); | |
2430 | if (ret < 0) { | |
2431 | goto error; | |
173af62f | 2432 | } |
0d0c377a | 2433 | } |
5c827ce0 | 2434 | |
54d01ffb | 2435 | break; |
2bdd86d4 | 2436 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 2437 | { |
5c827ce0 DG |
2438 | /* Consumer is in an ERROR state. Report back to client */ |
2439 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 2440 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2441 | goto error; |
2442 | } | |
2443 | ||
44d3bd01 | 2444 | if (need_tracing_session) { |
a4b92340 | 2445 | /* Create UST session if none exist. */ |
f6a9efaa | 2446 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 2447 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 2448 | &cmd_ctx->lsm->domain); |
f73fabfd | 2449 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
2450 | goto error; |
2451 | } | |
2452 | } | |
00e2e675 | 2453 | |
7753dea8 MD |
2454 | /* Start the UST consumer daemons */ |
2455 | /* 64-bit */ | |
2456 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
fc7a59ce | 2457 | if (consumerd64_bin[0] != '\0' && |
7753dea8 | 2458 | ustconsumer64_data.pid == 0 && |
785d2d0d | 2459 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2460 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2461 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 2462 | if (ret < 0) { |
f73fabfd | 2463 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 2464 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
2465 | goto error; |
2466 | } | |
48842b30 | 2467 | |
173af62f | 2468 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 2469 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 2470 | } else { |
7753dea8 MD |
2471 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
2472 | } | |
173af62f DG |
2473 | |
2474 | /* | |
2475 | * Setup socket for consumer 64 bit. No need for atomic access | |
2476 | * since it was set above and can ONLY be set in this thread. | |
2477 | */ | |
a4b92340 DG |
2478 | ret = consumer_create_socket(&ustconsumer64_data, |
2479 | cmd_ctx->session->ust_session->consumer); | |
2480 | if (ret < 0) { | |
2481 | goto error; | |
173af62f DG |
2482 | } |
2483 | ||
7753dea8 | 2484 | /* 32-bit */ |
fc7a59ce | 2485 | if (consumerd32_bin[0] != '\0' && |
7753dea8 | 2486 | ustconsumer32_data.pid == 0 && |
785d2d0d | 2487 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
2488 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
2489 | ret = start_consumerd(&ustconsumer32_data); | |
2490 | if (ret < 0) { | |
f73fabfd | 2491 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 2492 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
2493 | goto error; |
2494 | } | |
5c827ce0 | 2495 | |
173af62f | 2496 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 2497 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
2498 | } else { |
2499 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 2500 | } |
173af62f DG |
2501 | |
2502 | /* | |
2503 | * Setup socket for consumer 64 bit. No need for atomic access | |
2504 | * since it was set above and can ONLY be set in this thread. | |
2505 | */ | |
a4b92340 DG |
2506 | ret = consumer_create_socket(&ustconsumer32_data, |
2507 | cmd_ctx->session->ust_session->consumer); | |
2508 | if (ret < 0) { | |
2509 | goto error; | |
173af62f | 2510 | } |
44d3bd01 DG |
2511 | } |
2512 | break; | |
48842b30 | 2513 | } |
54d01ffb | 2514 | default: |
54d01ffb DG |
2515 | break; |
2516 | } | |
2e09ba09 | 2517 | skip_domain: |
33a2b854 | 2518 | |
5c827ce0 DG |
2519 | /* Validate consumer daemon state when start/stop trace command */ |
2520 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
2521 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
2522 | switch (cmd_ctx->lsm->domain.type) { | |
2523 | case LTTNG_DOMAIN_UST: | |
2524 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2525 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
2526 | goto error; |
2527 | } | |
2528 | break; | |
2529 | case LTTNG_DOMAIN_KERNEL: | |
2530 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 2531 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
2532 | goto error; |
2533 | } | |
2534 | break; | |
2535 | } | |
2536 | } | |
2537 | ||
8e0af1b4 MD |
2538 | /* |
2539 | * Check that the UID or GID match that of the tracing session. | |
2540 | * The root user can interact with all sessions. | |
2541 | */ | |
2542 | if (need_tracing_session) { | |
2543 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 DG |
2544 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
2545 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { | |
f73fabfd | 2546 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
2547 | goto error; |
2548 | } | |
2549 | } | |
2550 | ||
ffe60014 DG |
2551 | /* |
2552 | * Send relayd information to consumer as soon as we have a domain and a | |
2553 | * session defined. | |
2554 | */ | |
2555 | if (cmd_ctx->session && need_domain) { | |
2556 | /* | |
2557 | * Setup relayd if not done yet. If the relayd information was already | |
2558 | * sent to the consumer, this call will gracefully return. | |
2559 | */ | |
2560 | ret = cmd_setup_relayd(cmd_ctx->session); | |
2561 | if (ret != LTTNG_OK) { | |
2562 | goto error; | |
2563 | } | |
2564 | } | |
2565 | ||
54d01ffb DG |
2566 | /* Process by command type */ |
2567 | switch (cmd_ctx->lsm->cmd_type) { | |
2568 | case LTTNG_ADD_CONTEXT: | |
2569 | { | |
2570 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2571 | cmd_ctx->lsm->u.context.channel_name, | |
979e618e | 2572 | &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]); |
54d01ffb DG |
2573 | break; |
2574 | } | |
2575 | case LTTNG_DISABLE_CHANNEL: | |
2576 | { | |
2577 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2578 | cmd_ctx->lsm->u.disable.channel_name); | |
2579 | break; | |
2580 | } | |
2581 | case LTTNG_DISABLE_EVENT: | |
2582 | { | |
2583 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2584 | cmd_ctx->lsm->u.disable.channel_name, | |
2585 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
2586 | break; |
2587 | } | |
54d01ffb DG |
2588 | case LTTNG_DISABLE_ALL_EVENT: |
2589 | { | |
2bdd86d4 | 2590 | DBG("Disabling all events"); |
54d01ffb DG |
2591 | |
2592 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2593 | cmd_ctx->lsm->u.disable.channel_name); | |
2594 | break; | |
2595 | } | |
2596 | case LTTNG_ENABLE_CHANNEL: | |
2597 | { | |
aea829b3 | 2598 | ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 2599 | &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]); |
54d01ffb DG |
2600 | break; |
2601 | } | |
2602 | case LTTNG_ENABLE_EVENT: | |
2603 | { | |
2604 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2605 | cmd_ctx->lsm->u.enable.channel_name, | |
025faf73 | 2606 | &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]); |
54d01ffb DG |
2607 | break; |
2608 | } | |
2609 | case LTTNG_ENABLE_ALL_EVENT: | |
2610 | { | |
2bdd86d4 | 2611 | DBG("Enabling all events"); |
54d01ffb DG |
2612 | |
2613 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
8c9ae521 | 2614 | cmd_ctx->lsm->u.enable.channel_name, |
025faf73 | 2615 | cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]); |
54d01ffb DG |
2616 | break; |
2617 | } | |
052da939 | 2618 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 2619 | { |
9f19cc17 | 2620 | struct lttng_event *events; |
54d01ffb | 2621 | ssize_t nb_events; |
052da939 | 2622 | |
54d01ffb DG |
2623 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
2624 | if (nb_events < 0) { | |
f73fabfd | 2625 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
2626 | ret = -nb_events; |
2627 | goto error; | |
2ef84c95 DG |
2628 | } |
2629 | ||
2630 | /* | |
2631 | * Setup lttng message with payload size set to the event list size in | |
2632 | * bytes and then copy list into the llm payload. | |
2633 | */ | |
052da939 | 2634 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2635 | if (ret < 0) { |
052da939 | 2636 | free(events); |
2ef84c95 DG |
2637 | goto setup_error; |
2638 | } | |
2639 | ||
2640 | /* Copy event list into message payload */ | |
9f19cc17 | 2641 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 2642 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2643 | |
9f19cc17 | 2644 | free(events); |
2ef84c95 | 2645 | |
f73fabfd | 2646 | ret = LTTNG_OK; |
2ef84c95 DG |
2647 | break; |
2648 | } | |
f37d259d MD |
2649 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
2650 | { | |
2651 | struct lttng_event_field *fields; | |
2652 | ssize_t nb_fields; | |
2653 | ||
a4b92340 DG |
2654 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
2655 | &fields); | |
f37d259d | 2656 | if (nb_fields < 0) { |
f73fabfd | 2657 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
2658 | ret = -nb_fields; |
2659 | goto error; | |
2660 | } | |
2661 | ||
2662 | /* | |
2663 | * Setup lttng message with payload size set to the event list size in | |
2664 | * bytes and then copy list into the llm payload. | |
2665 | */ | |
a4b92340 DG |
2666 | ret = setup_lttng_msg(cmd_ctx, |
2667 | sizeof(struct lttng_event_field) * nb_fields); | |
f37d259d MD |
2668 | if (ret < 0) { |
2669 | free(fields); | |
2670 | goto setup_error; | |
2671 | } | |
2672 | ||
2673 | /* Copy event list into message payload */ | |
2674 | memcpy(cmd_ctx->llm->payload, fields, | |
2675 | sizeof(struct lttng_event_field) * nb_fields); | |
2676 | ||
2677 | free(fields); | |
2678 | ||
f73fabfd | 2679 | ret = LTTNG_OK; |
f37d259d MD |
2680 | break; |
2681 | } | |
00e2e675 DG |
2682 | case LTTNG_SET_CONSUMER_URI: |
2683 | { | |
a4b92340 DG |
2684 | size_t nb_uri, len; |
2685 | struct lttng_uri *uris; | |
2686 | ||
2687 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
2688 | len = nb_uri * sizeof(struct lttng_uri); | |
2689 | ||
2690 | if (nb_uri == 0) { | |
f73fabfd | 2691 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
2692 | goto error; |
2693 | } | |
2694 | ||
2695 | uris = zmalloc(len); | |
2696 | if (uris == NULL) { | |
f73fabfd | 2697 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
2698 | goto error; |
2699 | } | |
2700 | ||
2701 | /* Receive variable len data */ | |
77c7c900 | 2702 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
2703 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
2704 | if (ret <= 0) { | |
2705 | DBG("No URIs received from client... continuing"); | |
2706 | *sock_error = 1; | |
f73fabfd | 2707 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 2708 | free(uris); |
a4b92340 DG |
2709 | goto error; |
2710 | } | |
2711 | ||
00e2e675 | 2712 | ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
a4b92340 | 2713 | nb_uri, uris); |
f73fabfd | 2714 | if (ret != LTTNG_OK) { |
b1d41407 | 2715 | free(uris); |
a4b92340 DG |
2716 | goto error; |
2717 | } | |
2718 | ||
2719 | /* | |
2720 | * XXX: 0 means that this URI should be applied on the session. Should | |
2721 | * be a DOMAIN enuam. | |
2722 | */ | |
2723 | if (cmd_ctx->lsm->domain.type == 0) { | |
2724 | /* Add the URI for the UST session if a consumer is present. */ | |
2725 | if (cmd_ctx->session->ust_session && | |
2726 | cmd_ctx->session->ust_session->consumer) { | |
2727 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session, | |
2728 | nb_uri, uris); | |
2729 | } else if (cmd_ctx->session->kernel_session && | |
2730 | cmd_ctx->session->kernel_session->consumer) { | |
2731 | ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL, | |
2732 | cmd_ctx->session, nb_uri, uris); | |
2733 | } | |
2734 | } | |
2735 | ||
b1d41407 DG |
2736 | free(uris); |
2737 | ||
00e2e675 DG |
2738 | break; |
2739 | } | |
f3ed775e | 2740 | case LTTNG_START_TRACE: |
8c0faa1d | 2741 | { |
54d01ffb | 2742 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
2743 | break; |
2744 | } | |
f3ed775e | 2745 | case LTTNG_STOP_TRACE: |
8c0faa1d | 2746 | { |
54d01ffb | 2747 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
2748 | break; |
2749 | } | |
5e16da05 MD |
2750 | case LTTNG_CREATE_SESSION: |
2751 | { | |
a4b92340 DG |
2752 | size_t nb_uri, len; |
2753 | struct lttng_uri *uris = NULL; | |
2754 | ||
2755 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
2756 | len = nb_uri * sizeof(struct lttng_uri); | |
2757 | ||
2758 | if (nb_uri > 0) { | |
2759 | uris = zmalloc(len); | |
2760 | if (uris == NULL) { | |
f73fabfd | 2761 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
2762 | goto error; |
2763 | } | |
2764 | ||
2765 | /* Receive variable len data */ | |
77c7c900 | 2766 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
2767 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
2768 | if (ret <= 0) { | |
2769 | DBG("No URIs received from client... continuing"); | |
2770 | *sock_error = 1; | |
f73fabfd | 2771 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 2772 | free(uris); |
a4b92340 DG |
2773 | goto error; |
2774 | } | |
2775 | ||
2776 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
2777 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 2778 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 2779 | free(uris); |
a4b92340 DG |
2780 | goto error; |
2781 | } | |
2782 | } | |
2783 | ||
2784 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
2785 | &cmd_ctx->creds); | |
2786 | ||
b1d41407 DG |
2787 | free(uris); |
2788 | ||
00e2e675 DG |
2789 | break; |
2790 | } | |
5e16da05 MD |
2791 | case LTTNG_DESTROY_SESSION: |
2792 | { | |
2f77fc4b | 2793 | ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]); |
a4b92340 DG |
2794 | |
2795 | /* Set session to NULL so we do not unlock it after free. */ | |
256a5576 | 2796 | cmd_ctx->session = NULL; |
5461b305 | 2797 | break; |
5e16da05 | 2798 | } |
9f19cc17 | 2799 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 2800 | { |
54d01ffb DG |
2801 | ssize_t nb_dom; |
2802 | struct lttng_domain *domains; | |
5461b305 | 2803 | |
54d01ffb DG |
2804 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
2805 | if (nb_dom < 0) { | |
f73fabfd | 2806 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
2807 | ret = -nb_dom; |
2808 | goto error; | |
ce3d728c | 2809 | } |
520ff687 | 2810 | |
54d01ffb | 2811 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 DG |
2812 | if (ret < 0) { |
2813 | goto setup_error; | |
520ff687 | 2814 | } |
57167058 | 2815 | |
54d01ffb DG |
2816 | /* Copy event list into message payload */ |
2817 | memcpy(cmd_ctx->llm->payload, domains, | |
2818 | nb_dom * sizeof(struct lttng_domain)); | |
2819 | ||
2820 | free(domains); | |
5e16da05 | 2821 | |
f73fabfd | 2822 | ret = LTTNG_OK; |
5e16da05 MD |
2823 | break; |
2824 | } | |
9f19cc17 | 2825 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 2826 | { |
6775595e | 2827 | int nb_chan; |
54d01ffb DG |
2828 | struct lttng_channel *channels; |
2829 | ||
b551a063 DG |
2830 | nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type, |
2831 | cmd_ctx->session, &channels); | |
54d01ffb | 2832 | if (nb_chan < 0) { |
f73fabfd | 2833 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
2834 | ret = -nb_chan; |
2835 | goto error; | |
5461b305 | 2836 | } |
ca95a216 | 2837 | |
54d01ffb | 2838 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 DG |
2839 | if (ret < 0) { |
2840 | goto setup_error; | |
2841 | } | |
9f19cc17 | 2842 | |
54d01ffb DG |
2843 | /* Copy event list into message payload */ |
2844 | memcpy(cmd_ctx->llm->payload, channels, | |
2845 | nb_chan * sizeof(struct lttng_channel)); | |
2846 | ||
2847 | free(channels); | |
9f19cc17 | 2848 | |
f73fabfd | 2849 | ret = LTTNG_OK; |
5461b305 | 2850 | break; |
5e16da05 | 2851 | } |
9f19cc17 | 2852 | case LTTNG_LIST_EVENTS: |
5e16da05 | 2853 | { |
b551a063 | 2854 | ssize_t nb_event; |
684d34d2 | 2855 | struct lttng_event *events = NULL; |
9f19cc17 | 2856 | |
b551a063 | 2857 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session, |
54d01ffb DG |
2858 | cmd_ctx->lsm->u.list.channel_name, &events); |
2859 | if (nb_event < 0) { | |
f73fabfd | 2860 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
2861 | ret = -nb_event; |
2862 | goto error; | |
5461b305 | 2863 | } |
ca95a216 | 2864 | |
54d01ffb | 2865 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 DG |
2866 | if (ret < 0) { |
2867 | goto setup_error; | |
2868 | } | |
9f19cc17 | 2869 | |
54d01ffb DG |
2870 | /* Copy event list into message payload */ |
2871 | memcpy(cmd_ctx->llm->payload, events, | |
2872 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 2873 | |
54d01ffb | 2874 | free(events); |
9f19cc17 | 2875 | |
f73fabfd | 2876 | ret = LTTNG_OK; |
5461b305 | 2877 | break; |
5e16da05 MD |
2878 | } |
2879 | case LTTNG_LIST_SESSIONS: | |
2880 | { | |
8e0af1b4 | 2881 | unsigned int nr_sessions; |
5461b305 | 2882 | |
8e0af1b4 | 2883 | session_lock_list(); |
730389d9 DG |
2884 | nr_sessions = lttng_sessions_count( |
2885 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
2886 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
d32fb093 | 2887 | |
8e0af1b4 | 2888 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); |
5461b305 | 2889 | if (ret < 0) { |
54d01ffb | 2890 | session_unlock_list(); |
5461b305 | 2891 | goto setup_error; |
e065084a | 2892 | } |
5e16da05 | 2893 | |
6c9cc2ab | 2894 | /* Filled the session array */ |
2f77fc4b | 2895 | cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), |
730389d9 DG |
2896 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
2897 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6c9cc2ab | 2898 | |
54d01ffb | 2899 | session_unlock_list(); |
5e16da05 | 2900 | |
f73fabfd | 2901 | ret = LTTNG_OK; |
5e16da05 MD |
2902 | break; |
2903 | } | |
d0254c7c MD |
2904 | case LTTNG_CALIBRATE: |
2905 | { | |
54d01ffb DG |
2906 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
2907 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
2908 | break; |
2909 | } | |
d9800920 DG |
2910 | case LTTNG_REGISTER_CONSUMER: |
2911 | { | |
2f77fc4b DG |
2912 | struct consumer_data *cdata; |
2913 | ||
2914 | switch (cmd_ctx->lsm->domain.type) { | |
2915 | case LTTNG_DOMAIN_KERNEL: | |
2916 | cdata = &kconsumer_data; | |
2917 | break; | |
2918 | default: | |
f73fabfd | 2919 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2920 | goto error; |
2921 | } | |
2922 | ||
54d01ffb | 2923 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 2924 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
2925 | break; |
2926 | } | |
025faf73 | 2927 | case LTTNG_ENABLE_EVENT_WITH_FILTER: |
53a80697 MD |
2928 | { |
2929 | struct lttng_filter_bytecode *bytecode; | |
2930 | ||
025faf73 | 2931 | if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) { |
f73fabfd | 2932 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
2933 | goto error; |
2934 | } | |
52df2401 MD |
2935 | if (cmd_ctx->lsm->u.enable.bytecode_len == 0) { |
2936 | ret = LTTNG_ERR_FILTER_INVAL; | |
2937 | goto error; | |
2938 | } | |
025faf73 | 2939 | bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len); |
53a80697 | 2940 | if (!bytecode) { |
f73fabfd | 2941 | ret = LTTNG_ERR_FILTER_NOMEM; |
53a80697 MD |
2942 | goto error; |
2943 | } | |
2944 | /* Receive var. len. data */ | |
2945 | DBG("Receiving var len data from client ..."); | |
2946 | ret = lttcomm_recv_unix_sock(sock, bytecode, | |
025faf73 | 2947 | cmd_ctx->lsm->u.enable.bytecode_len); |
53a80697 MD |
2948 | if (ret <= 0) { |
2949 | DBG("Nothing recv() from client var len data... continuing"); | |
2950 | *sock_error = 1; | |
f73fabfd | 2951 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
2952 | goto error; |
2953 | } | |
2954 | ||
2955 | if (bytecode->len + sizeof(*bytecode) | |
025faf73 | 2956 | != cmd_ctx->lsm->u.enable.bytecode_len) { |
53a80697 | 2957 | free(bytecode); |
f73fabfd | 2958 | ret = LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
2959 | goto error; |
2960 | } | |
2961 | ||
025faf73 DG |
2962 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2963 | cmd_ctx->lsm->u.enable.channel_name, | |
2964 | &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]); | |
53a80697 MD |
2965 | break; |
2966 | } | |
6d805429 | 2967 | case LTTNG_DATA_PENDING: |
806e2684 | 2968 | { |
6d805429 | 2969 | ret = cmd_data_pending(cmd_ctx->session); |
806e2684 DG |
2970 | break; |
2971 | } | |
5e16da05 | 2972 | default: |
f73fabfd | 2973 | ret = LTTNG_ERR_UND; |
5461b305 | 2974 | break; |
fac6795d DG |
2975 | } |
2976 | ||
5461b305 | 2977 | error: |
5461b305 DG |
2978 | if (cmd_ctx->llm == NULL) { |
2979 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 2980 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
2981 | goto setup_error; |
2982 | } | |
2983 | } | |
54d01ffb | 2984 | /* Set return code */ |
5461b305 | 2985 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 2986 | setup_error: |
b5541356 | 2987 | if (cmd_ctx->session) { |
54d01ffb | 2988 | session_unlock(cmd_ctx->session); |
b5541356 | 2989 | } |
256a5576 MD |
2990 | if (need_tracing_session) { |
2991 | session_unlock_list(); | |
2992 | } | |
54d01ffb | 2993 | init_setup_error: |
8028d920 | 2994 | return ret; |
fac6795d DG |
2995 | } |
2996 | ||
44a5e5eb DG |
2997 | /* |
2998 | * Thread managing health check socket. | |
2999 | */ | |
3000 | static void *thread_manage_health(void *data) | |
3001 | { | |
eb4a2943 | 3002 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
3003 | uint32_t revents, nb_fd; |
3004 | struct lttng_poll_event events; | |
3005 | struct lttcomm_health_msg msg; | |
3006 | struct lttcomm_health_data reply; | |
3007 | ||
3008 | DBG("[thread] Manage health check started"); | |
3009 | ||
3010 | rcu_register_thread(); | |
3011 | ||
3012 | /* Create unix socket */ | |
3013 | sock = lttcomm_create_unix_sock(health_unix_sock_path); | |
3014 | if (sock < 0) { | |
3015 | ERR("Unable to create health check Unix socket"); | |
3016 | ret = -1; | |
3017 | goto error; | |
3018 | } | |
3019 | ||
b662582b DG |
3020 | /* |
3021 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3022 | * show must go on. | |
3023 | */ | |
3024 | (void) utils_set_fd_cloexec(sock); | |
3025 | ||
44a5e5eb DG |
3026 | ret = lttcomm_listen_unix_sock(sock); |
3027 | if (ret < 0) { | |
3028 | goto error; | |
3029 | } | |
3030 | ||
3031 | /* | |
3032 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3033 | * more will be added to this poll set. | |
3034 | */ | |
d0b96690 | 3035 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
3036 | if (ret < 0) { |
3037 | goto error; | |
3038 | } | |
3039 | ||
3040 | /* Add the application registration socket */ | |
3041 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
3042 | if (ret < 0) { | |
3043 | goto error; | |
3044 | } | |
3045 | ||
3046 | while (1) { | |
3047 | DBG("Health check ready"); | |
3048 | ||
44a5e5eb DG |
3049 | /* Inifinite blocking call, waiting for transmission */ |
3050 | restart: | |
3051 | ret = lttng_poll_wait(&events, -1); | |
3052 | if (ret < 0) { | |
3053 | /* | |
3054 | * Restart interrupted system call. | |
3055 | */ | |
3056 | if (errno == EINTR) { | |
3057 | goto restart; | |
3058 | } | |
3059 | goto error; | |
3060 | } | |
3061 | ||
0d9c5d77 DG |
3062 | nb_fd = ret; |
3063 | ||
44a5e5eb DG |
3064 | for (i = 0; i < nb_fd; i++) { |
3065 | /* Fetch once the poll data */ | |
3066 | revents = LTTNG_POLL_GETEV(&events, i); | |
3067 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3068 | ||
3069 | /* Thread quit pipe has been closed. Killing thread. */ | |
d0b96690 | 3070 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 3071 | if (ret) { |
139ac872 MD |
3072 | err = 0; |
3073 | goto exit; | |
44a5e5eb DG |
3074 | } |
3075 | ||
3076 | /* Event on the registration socket */ | |
3077 | if (pollfd == sock) { | |
3078 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3079 | ERR("Health socket poll error"); | |
3080 | goto error; | |
3081 | } | |
3082 | } | |
3083 | } | |
3084 | ||
3085 | new_sock = lttcomm_accept_unix_sock(sock); | |
3086 | if (new_sock < 0) { | |
3087 | goto error; | |
3088 | } | |
3089 | ||
b662582b DG |
3090 | /* |
3091 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3092 | * show must go on. | |
3093 | */ | |
3094 | (void) utils_set_fd_cloexec(new_sock); | |
3095 | ||
44a5e5eb DG |
3096 | DBG("Receiving data from client for health..."); |
3097 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
3098 | if (ret <= 0) { | |
3099 | DBG("Nothing recv() from client... continuing"); | |
3100 | ret = close(new_sock); | |
3101 | if (ret) { | |
3102 | PERROR("close"); | |
3103 | } | |
3104 | new_sock = -1; | |
3105 | continue; | |
3106 | } | |
3107 | ||
3108 | rcu_thread_online(); | |
3109 | ||
3110 | switch (msg.component) { | |
3111 | case LTTNG_HEALTH_CMD: | |
927ca06a | 3112 | reply.ret_code = health_check_state(HEALTH_TYPE_CMD); |
44a5e5eb | 3113 | break; |
139ac872 | 3114 | case LTTNG_HEALTH_APP_MANAGE: |
927ca06a | 3115 | reply.ret_code = health_check_state(HEALTH_TYPE_APP_MANAGE); |
139ac872 | 3116 | break; |
44a5e5eb | 3117 | case LTTNG_HEALTH_APP_REG: |
927ca06a | 3118 | reply.ret_code = health_check_state(HEALTH_TYPE_APP_REG); |
44a5e5eb DG |
3119 | break; |
3120 | case LTTNG_HEALTH_KERNEL: | |
927ca06a | 3121 | reply.ret_code = health_check_state(HEALTH_TYPE_KERNEL); |
44a5e5eb DG |
3122 | break; |
3123 | case LTTNG_HEALTH_CONSUMER: | |
3124 | reply.ret_code = check_consumer_health(); | |
3125 | break; | |
3126 | case LTTNG_HEALTH_ALL: | |
44a5e5eb | 3127 | reply.ret_code = |
927ca06a DG |
3128 | health_check_state(HEALTH_TYPE_APP_MANAGE) && |
3129 | health_check_state(HEALTH_TYPE_APP_REG) && | |
3130 | health_check_state(HEALTH_TYPE_CMD) && | |
3131 | health_check_state(HEALTH_TYPE_KERNEL) && | |
139ac872 | 3132 | check_consumer_health(); |
44a5e5eb DG |
3133 | break; |
3134 | default: | |
f73fabfd | 3135 | reply.ret_code = LTTNG_ERR_UND; |
44a5e5eb DG |
3136 | break; |
3137 | } | |
3138 | ||
3139 | /* | |
3140 | * Flip ret value since 0 is a success and 1 indicates a bad health for | |
3141 | * the client where in the sessiond it is the opposite. Again, this is | |
3142 | * just to make things easier for us poor developer which enjoy a lot | |
3143 | * lazyness. | |
3144 | */ | |
3145 | if (reply.ret_code == 0 || reply.ret_code == 1) { | |
3146 | reply.ret_code = !reply.ret_code; | |
3147 | } | |
3148 | ||
3149 | DBG2("Health check return value %d", reply.ret_code); | |
3150 | ||
3151 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
3152 | if (ret < 0) { | |
3153 | ERR("Failed to send health data back to client"); | |
3154 | } | |
3155 | ||
3156 | /* End of transmission */ | |
3157 | ret = close(new_sock); | |
3158 | if (ret) { | |
3159 | PERROR("close"); | |
3160 | } | |
3161 | new_sock = -1; | |
3162 | } | |
3163 | ||
139ac872 | 3164 | exit: |
44a5e5eb | 3165 | error: |
139ac872 MD |
3166 | if (err) { |
3167 | ERR("Health error occurred in %s", __func__); | |
3168 | } | |
44a5e5eb DG |
3169 | DBG("Health check thread dying"); |
3170 | unlink(health_unix_sock_path); | |
3171 | if (sock >= 0) { | |
3172 | ret = close(sock); | |
3173 | if (ret) { | |
3174 | PERROR("close"); | |
3175 | } | |
3176 | } | |
3177 | if (new_sock >= 0) { | |
3178 | ret = close(new_sock); | |
3179 | if (ret) { | |
3180 | PERROR("close"); | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | lttng_poll_clean(&events); | |
3185 | ||
3186 | rcu_unregister_thread(); | |
3187 | return NULL; | |
3188 | } | |
3189 | ||
1d4b027a | 3190 | /* |
d063d709 DG |
3191 | * This thread manage all clients request using the unix client socket for |
3192 | * communication. | |
1d4b027a DG |
3193 | */ |
3194 | static void *thread_manage_clients(void *data) | |
3195 | { | |
139ac872 | 3196 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 3197 | int sock_error; |
5eb91c98 | 3198 | uint32_t revents, nb_fd; |
273ea72c | 3199 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 3200 | struct lttng_poll_event events; |
1d4b027a DG |
3201 | |
3202 | DBG("[thread] Manage client started"); | |
3203 | ||
f6a9efaa DG |
3204 | rcu_register_thread(); |
3205 | ||
927ca06a DG |
3206 | health_register(HEALTH_TYPE_CMD); |
3207 | ||
6993eeb3 CB |
3208 | if (testpoint(thread_manage_clients)) { |
3209 | goto error_testpoint; | |
3210 | } | |
3211 | ||
840cb59c | 3212 | health_code_update(); |
44a5e5eb | 3213 | |
1d4b027a DG |
3214 | ret = lttcomm_listen_unix_sock(client_sock); |
3215 | if (ret < 0) { | |
35df2755 | 3216 | goto error_listen; |
1d4b027a DG |
3217 | } |
3218 | ||
5eb91c98 DG |
3219 | /* |
3220 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
3221 | * more will be added to this poll set. | |
3222 | */ | |
d0b96690 | 3223 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 3224 | if (ret < 0) { |
35df2755 | 3225 | goto error_create_poll; |
5eb91c98 | 3226 | } |
273ea72c | 3227 | |
5eb91c98 DG |
3228 | /* Add the application registration socket */ |
3229 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
3230 | if (ret < 0) { | |
3231 | goto error; | |
3232 | } | |
273ea72c | 3233 | |
bbd973c2 DG |
3234 | /* |
3235 | * Notify parent pid that we are ready to accept command for client side. | |
1d4b027a DG |
3236 | */ |
3237 | if (opt_sig_parent) { | |
8db8d1dc | 3238 | kill(ppid, SIGUSR1); |
1d4b027a DG |
3239 | } |
3240 | ||
6993eeb3 CB |
3241 | if (testpoint(thread_manage_clients_before_loop)) { |
3242 | goto error; | |
3243 | } | |
8ac94142 | 3244 | |
840cb59c | 3245 | health_code_update(); |
44a5e5eb | 3246 | |
1d4b027a | 3247 | while (1) { |
1d4b027a | 3248 | DBG("Accepting client command ..."); |
273ea72c DG |
3249 | |
3250 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 3251 | restart: |
a78af745 | 3252 | health_poll_entry(); |
5eb91c98 | 3253 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 3254 | health_poll_exit(); |
273ea72c | 3255 | if (ret < 0) { |
88f2b785 MD |
3256 | /* |
3257 | * Restart interrupted system call. | |
3258 | */ | |
3259 | if (errno == EINTR) { | |
3260 | goto restart; | |
3261 | } | |
273ea72c DG |
3262 | goto error; |
3263 | } | |
3264 | ||
0d9c5d77 DG |
3265 | nb_fd = ret; |
3266 | ||
5eb91c98 DG |
3267 | for (i = 0; i < nb_fd; i++) { |
3268 | /* Fetch once the poll data */ | |
3269 | revents = LTTNG_POLL_GETEV(&events, i); | |
3270 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
3271 | ||
840cb59c | 3272 | health_code_update(); |
44a5e5eb | 3273 | |
5eb91c98 | 3274 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 3275 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 3276 | if (ret) { |
139ac872 MD |
3277 | err = 0; |
3278 | goto exit; | |
5eb91c98 DG |
3279 | } |
3280 | ||
3281 | /* Event on the registration socket */ | |
3282 | if (pollfd == client_sock) { | |
3283 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3284 | ERR("Client socket poll error"); | |
3285 | goto error; | |
3286 | } | |
3287 | } | |
3288 | } | |
3289 | ||
3290 | DBG("Wait for client response"); | |
3291 | ||
840cb59c | 3292 | health_code_update(); |
44a5e5eb | 3293 | |
5eb91c98 DG |
3294 | sock = lttcomm_accept_unix_sock(client_sock); |
3295 | if (sock < 0) { | |
273ea72c | 3296 | goto error; |
273ea72c DG |
3297 | } |
3298 | ||
b662582b DG |
3299 | /* |
3300 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
3301 | * show must go on. | |
3302 | */ | |
3303 | (void) utils_set_fd_cloexec(sock); | |
3304 | ||
be040666 DG |
3305 | /* Set socket option for credentials retrieval */ |
3306 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
3307 | if (ret < 0) { | |
3308 | goto error; | |
3309 | } | |
3310 | ||
5eb91c98 | 3311 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 3312 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 3313 | if (cmd_ctx == NULL) { |
76d7553f | 3314 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 3315 | goto error; |
5eb91c98 | 3316 | } |
1d4b027a | 3317 | |
5eb91c98 | 3318 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 3319 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 3320 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 3321 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
3322 | goto error; |
3323 | } | |
1d4b027a | 3324 | |
5eb91c98 DG |
3325 | cmd_ctx->llm = NULL; |
3326 | cmd_ctx->session = NULL; | |
1d4b027a | 3327 | |
840cb59c | 3328 | health_code_update(); |
44a5e5eb | 3329 | |
5eb91c98 DG |
3330 | /* |
3331 | * Data is received from the lttng client. The struct | |
3332 | * lttcomm_session_msg (lsm) contains the command and data request of | |
3333 | * the client. | |
3334 | */ | |
3335 | DBG("Receiving data from client ..."); | |
be040666 DG |
3336 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
3337 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
3338 | if (ret <= 0) { |
3339 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
3340 | ret = close(sock); |
3341 | if (ret) { | |
3342 | PERROR("close"); | |
3343 | } | |
3344 | sock = -1; | |
a2c0da86 | 3345 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3346 | continue; |
3347 | } | |
1d4b027a | 3348 | |
840cb59c | 3349 | health_code_update(); |
44a5e5eb | 3350 | |
5eb91c98 DG |
3351 | // TODO: Validate cmd_ctx including sanity check for |
3352 | // security purpose. | |
f7776ea7 | 3353 | |
f6a9efaa | 3354 | rcu_thread_online(); |
5eb91c98 DG |
3355 | /* |
3356 | * This function dispatch the work to the kernel or userspace tracer | |
3357 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
3358 | * informations for the client. The command context struct contains | |
3359 | * everything this function may needs. | |
3360 | */ | |
53a80697 | 3361 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 3362 | rcu_thread_offline(); |
5eb91c98 | 3363 | if (ret < 0) { |
53a80697 MD |
3364 | if (sock_error) { |
3365 | ret = close(sock); | |
3366 | if (ret) { | |
3367 | PERROR("close"); | |
3368 | } | |
3369 | sock = -1; | |
3370 | } | |
bbd973c2 | 3371 | /* |
5eb91c98 | 3372 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 3373 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
3374 | * (ENOMEM). Error detected but still accept |
3375 | * command, unless a socket error has been | |
3376 | * detected. | |
bbd973c2 | 3377 | */ |
bbd973c2 | 3378 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3379 | continue; |
3380 | } | |
d6e4fca4 | 3381 | |
840cb59c | 3382 | health_code_update(); |
44a5e5eb | 3383 | |
54d01ffb DG |
3384 | DBG("Sending response (size: %d, retcode: %s)", |
3385 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 3386 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 3387 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
3388 | if (ret < 0) { |
3389 | ERR("Failed to send data back to client"); | |
bbd973c2 | 3390 | } |
1d4b027a | 3391 | |
5eb91c98 | 3392 | /* End of transmission */ |
76d7553f MD |
3393 | ret = close(sock); |
3394 | if (ret) { | |
3395 | PERROR("close"); | |
3396 | } | |
3397 | sock = -1; | |
be040666 DG |
3398 | |
3399 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 3400 | |
840cb59c | 3401 | health_code_update(); |
273ea72c DG |
3402 | } |
3403 | ||
139ac872 | 3404 | exit: |
5eb91c98 | 3405 | error: |
35df2755 CB |
3406 | if (sock >= 0) { |
3407 | ret = close(sock); | |
3408 | if (ret) { | |
3409 | PERROR("close"); | |
3410 | } | |
139ac872 | 3411 | } |
44a5e5eb | 3412 | |
35df2755 CB |
3413 | lttng_poll_clean(&events); |
3414 | clean_command_ctx(&cmd_ctx); | |
3415 | ||
3416 | error_listen: | |
3417 | error_create_poll: | |
6993eeb3 | 3418 | error_testpoint: |
273ea72c | 3419 | unlink(client_unix_sock_path); |
76d7553f MD |
3420 | if (client_sock >= 0) { |
3421 | ret = close(client_sock); | |
3422 | if (ret) { | |
3423 | PERROR("close"); | |
3424 | } | |
a4b35e07 | 3425 | } |
35df2755 CB |
3426 | |
3427 | if (err) { | |
840cb59c | 3428 | health_error(); |
35df2755 | 3429 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 3430 | } |
273ea72c | 3431 | |
927ca06a | 3432 | health_unregister(); |
35df2755 CB |
3433 | |
3434 | DBG("Client thread dying"); | |
f6a9efaa DG |
3435 | |
3436 | rcu_unregister_thread(); | |
1d4b027a DG |
3437 | return NULL; |
3438 | } | |
3439 | ||
3440 | ||
fac6795d DG |
3441 | /* |
3442 | * usage function on stderr | |
3443 | */ | |
3444 | static void usage(void) | |
3445 | { | |
b716ce68 | 3446 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
3447 | fprintf(stderr, " -h, --help Display this usage.\n"); |
3448 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
3449 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
3450 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
3451 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
7753dea8 MD |
3452 | fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n"); |
3453 | fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n"); | |
3454 | fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n"); | |
3455 | fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n"); | |
ebaeda94 MD |
3456 | fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n"); |
3457 | fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n"); | |
3458 | fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); | |
3459 | fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); | |
d6f42150 DG |
3460 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
3461 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
3462 | fprintf(stderr, " -V, --version Show version number.\n"); | |
3463 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
3464 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
3465 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
35f90c40 | 3466 | fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n"); |
3bd1e081 | 3467 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
4fba7219 | 3468 | fprintf(stderr, " --no-kernel Disable kernel tracer\n"); |
fac6795d DG |
3469 | } |
3470 | ||
3471 | /* | |
3472 | * daemon argument parsing | |
3473 | */ | |
3474 | static int parse_args(int argc, char **argv) | |
3475 | { | |
3476 | int c; | |
3477 | ||
3478 | static struct option long_options[] = { | |
3479 | { "client-sock", 1, 0, 'c' }, | |
3480 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
3481 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
3482 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
7753dea8 MD |
3483 | { "ustconsumerd32-cmd-sock", 1, 0, 'G' }, |
3484 | { "ustconsumerd32-err-sock", 1, 0, 'H' }, | |
ebaeda94 MD |
3485 | { "ustconsumerd64-cmd-sock", 1, 0, 'D' }, |
3486 | { "ustconsumerd64-err-sock", 1, 0, 'F' }, | |
3487 | { "consumerd32-path", 1, 0, 'u' }, | |
3488 | { "consumerd32-libdir", 1, 0, 'U' }, | |
3489 | { "consumerd64-path", 1, 0, 't' }, | |
3490 | { "consumerd64-libdir", 1, 0, 'T' }, | |
fac6795d | 3491 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 3492 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
3493 | { "help", 0, 0, 'h' }, |
3494 | { "group", 1, 0, 'g' }, | |
3495 | { "version", 0, 0, 'V' }, | |
75462a81 | 3496 | { "quiet", 0, 0, 'q' }, |
3f9947db | 3497 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 3498 | { "verbose-consumer", 0, 0, 'Z' }, |
4fba7219 | 3499 | { "no-kernel", 0, 0, 'N' }, |
35f90c40 | 3500 | { "pidfile", 1, 0, 'p' }, |
fac6795d DG |
3501 | { NULL, 0, 0, 0 } |
3502 | }; | |
3503 | ||
3504 | while (1) { | |
3505 | int option_index = 0; | |
35f90c40 | 3506 | c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:", |
54d01ffb | 3507 | long_options, &option_index); |
fac6795d DG |
3508 | if (c == -1) { |
3509 | break; | |
3510 | } | |
3511 | ||
3512 | switch (c) { | |
3513 | case 0: | |
3514 | fprintf(stderr, "option %s", long_options[option_index].name); | |
3515 | if (optarg) { | |
3516 | fprintf(stderr, " with arg %s\n", optarg); | |
3517 | } | |
3518 | break; | |
b716ce68 | 3519 | case 'c': |
fac6795d DG |
3520 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
3521 | break; | |
3522 | case 'a': | |
3523 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
3524 | break; | |
3525 | case 'd': | |
3526 | opt_daemon = 1; | |
3527 | break; | |
3528 | case 'g': | |
fb09408a | 3529 | opt_tracing_group = optarg; |
fac6795d DG |
3530 | break; |
3531 | case 'h': | |
3532 | usage(); | |
3533 | exit(EXIT_FAILURE); | |
3534 | case 'V': | |
3535 | fprintf(stdout, "%s\n", VERSION); | |
3536 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
3537 | case 'S': |
3538 | opt_sig_parent = 1; | |
3539 | break; | |
d6f42150 | 3540 | case 'E': |
3bd1e081 | 3541 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
3542 | break; |
3543 | case 'C': | |
3bd1e081 MD |
3544 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
3545 | break; | |
3546 | case 'F': | |
7753dea8 | 3547 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
3bd1e081 MD |
3548 | break; |
3549 | case 'D': | |
7753dea8 MD |
3550 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
3551 | break; | |
3552 | case 'H': | |
3553 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
3554 | break; | |
3555 | case 'G': | |
3556 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 3557 | break; |
4fba7219 DG |
3558 | case 'N': |
3559 | opt_no_kernel = 1; | |
3560 | break; | |
75462a81 | 3561 | case 'q': |
97e19046 | 3562 | lttng_opt_quiet = 1; |
75462a81 | 3563 | break; |
3f9947db | 3564 | case 'v': |
53086306 | 3565 | /* Verbose level can increase using multiple -v */ |
97e19046 | 3566 | lttng_opt_verbose += 1; |
3f9947db | 3567 | break; |
31f73cc9 | 3568 | case 'Z': |
3bd1e081 | 3569 | opt_verbose_consumer += 1; |
31f73cc9 | 3570 | break; |
fb09408a | 3571 | case 'u': |
fc7a59ce | 3572 | consumerd32_bin= optarg; |
ebaeda94 MD |
3573 | break; |
3574 | case 'U': | |
3575 | consumerd32_libdir = optarg; | |
7753dea8 MD |
3576 | break; |
3577 | case 't': | |
fc7a59ce | 3578 | consumerd64_bin = optarg; |
ebaeda94 MD |
3579 | break; |
3580 | case 'T': | |
3581 | consumerd64_libdir = optarg; | |
fb09408a | 3582 | break; |
35f90c40 DG |
3583 | case 'p': |
3584 | opt_pidfile = optarg; | |
3585 | break; | |
fac6795d DG |
3586 | default: |
3587 | /* Unknown option or other error. | |
3588 | * Error is printed by getopt, just return */ | |
3589 | return -1; | |
3590 | } | |
3591 | } | |
3592 | ||
3593 | return 0; | |
3594 | } | |
3595 | ||
3596 | /* | |
d063d709 | 3597 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
3598 | * apps_sock - The communication socket for all UST apps. |
3599 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 3600 | */ |
cf3af59e | 3601 | static int init_daemon_socket(void) |
fac6795d DG |
3602 | { |
3603 | int ret = 0; | |
3604 | mode_t old_umask; | |
3605 | ||
3606 | old_umask = umask(0); | |
3607 | ||
3608 | /* Create client tool unix socket */ | |
d6f42150 DG |
3609 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
3610 | if (client_sock < 0) { | |
3611 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
3612 | ret = -1; |
3613 | goto end; | |
3614 | } | |
3615 | ||
b662582b DG |
3616 | /* Set the cloexec flag */ |
3617 | ret = utils_set_fd_cloexec(client_sock); | |
3618 | if (ret < 0) { | |
3619 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
3620 | "Continuing but note that the consumer daemon will have a " | |
3621 | "reference to this socket on exec()", client_sock); | |
3622 | } | |
3623 | ||
fac6795d DG |
3624 | /* File permission MUST be 660 */ |
3625 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3626 | if (ret < 0) { | |
d6f42150 | 3627 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
76d7553f | 3628 | PERROR("chmod"); |
fac6795d DG |
3629 | goto end; |
3630 | } | |
3631 | ||
3632 | /* Create the application unix socket */ | |
d6f42150 DG |
3633 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
3634 | if (apps_sock < 0) { | |
3635 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
3636 | ret = -1; |
3637 | goto end; | |
3638 | } | |
3639 | ||
b662582b DG |
3640 | /* Set the cloexec flag */ |
3641 | ret = utils_set_fd_cloexec(apps_sock); | |
3642 | if (ret < 0) { | |
3643 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
3644 | "Continuing but note that the consumer daemon will have a " | |
3645 | "reference to this socket on exec()", apps_sock); | |
3646 | } | |
3647 | ||
d6f42150 | 3648 | /* File permission MUST be 666 */ |
54d01ffb DG |
3649 | ret = chmod(apps_unix_sock_path, |
3650 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 3651 | if (ret < 0) { |
d6f42150 | 3652 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
76d7553f | 3653 | PERROR("chmod"); |
fac6795d DG |
3654 | goto end; |
3655 | } | |
3656 | ||
b662582b DG |
3657 | DBG3("Session daemon client socket %d and application socket %d created", |
3658 | client_sock, apps_sock); | |
3659 | ||
fac6795d DG |
3660 | end: |
3661 | umask(old_umask); | |
3662 | return ret; | |
3663 | } | |
3664 | ||
3665 | /* | |
54d01ffb DG |
3666 | * Check if the global socket is available, and if a daemon is answering at the |
3667 | * other side. If yes, error is returned. | |
fac6795d | 3668 | */ |
cf3af59e | 3669 | static int check_existing_daemon(void) |
fac6795d | 3670 | { |
7d8234d9 | 3671 | /* Is there anybody out there ? */ |
099e26bd | 3672 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 3673 | return -EEXIST; |
099e26bd | 3674 | } |
b09c7c76 DG |
3675 | |
3676 | return 0; | |
fac6795d DG |
3677 | } |
3678 | ||
fac6795d | 3679 | /* |
d063d709 | 3680 | * Set the tracing group gid onto the client socket. |
5e16da05 | 3681 | * |
d063d709 | 3682 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 3683 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 3684 | */ |
be040666 | 3685 | static int set_permissions(char *rundir) |
fac6795d DG |
3686 | { |
3687 | int ret; | |
996b65c8 | 3688 | gid_t gid; |
fac6795d | 3689 | |
6775595e DG |
3690 | ret = allowed_group(); |
3691 | if (ret < 0) { | |
be040666 DG |
3692 | WARN("No tracing group detected"); |
3693 | ret = 0; | |
fac6795d DG |
3694 | goto end; |
3695 | } | |
3696 | ||
6775595e DG |
3697 | gid = ret; |
3698 | ||
d6f42150 | 3699 | /* Set lttng run dir */ |
be040666 | 3700 | ret = chown(rundir, 0, gid); |
d6f42150 | 3701 | if (ret < 0) { |
be040666 | 3702 | ERR("Unable to set group on %s", rundir); |
76d7553f | 3703 | PERROR("chown"); |
d6f42150 DG |
3704 | } |
3705 | ||
d1613cf5 | 3706 | /* Ensure tracing group can search the run dir */ |
61076f74 | 3707 | ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH); |
d1613cf5 JN |
3708 | if (ret < 0) { |
3709 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 3710 | PERROR("chmod"); |
d1613cf5 JN |
3711 | } |
3712 | ||
d6f42150 | 3713 | /* lttng client socket path */ |
996b65c8 | 3714 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 3715 | if (ret < 0) { |
d6f42150 | 3716 | ERR("Unable to set group on %s", client_unix_sock_path); |
76d7553f | 3717 | PERROR("chown"); |
d6f42150 DG |
3718 | } |
3719 | ||
3bd1e081 MD |
3720 | /* kconsumer error socket path */ |
3721 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); | |
d6f42150 | 3722 | if (ret < 0) { |
3bd1e081 | 3723 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 3724 | PERROR("chown"); |
3bd1e081 MD |
3725 | } |
3726 | ||
7753dea8 MD |
3727 | /* 64-bit ustconsumer error socket path */ |
3728 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid); | |
3729 | if (ret < 0) { | |
3730 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 3731 | PERROR("chown"); |
7753dea8 MD |
3732 | } |
3733 | ||
3734 | /* 32-bit ustconsumer compat32 error socket path */ | |
3735 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid); | |
3bd1e081 | 3736 | if (ret < 0) { |
7753dea8 | 3737 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 3738 | PERROR("chown"); |
fac6795d DG |
3739 | } |
3740 | ||
d6f42150 | 3741 | DBG("All permissions are set"); |
e07ae692 | 3742 | |
fac6795d DG |
3743 | end: |
3744 | return ret; | |
3745 | } | |
3746 | ||
d6f42150 | 3747 | /* |
d063d709 | 3748 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 3749 | */ |
67e40797 | 3750 | static int create_lttng_rundir(const char *rundir) |
d6f42150 DG |
3751 | { |
3752 | int ret; | |
3753 | ||
67e40797 DG |
3754 | DBG3("Creating LTTng run directory: %s", rundir); |
3755 | ||
d1613cf5 | 3756 | ret = mkdir(rundir, S_IRWXU); |
d6f42150 | 3757 | if (ret < 0) { |
b1f11e69 | 3758 | if (errno != EEXIST) { |
67e40797 | 3759 | ERR("Unable to create %s", rundir); |
b1f11e69 DG |
3760 | goto error; |
3761 | } else { | |
3762 | ret = 0; | |
3763 | } | |
d6f42150 DG |
3764 | } |
3765 | ||
3766 | error: | |
3767 | return ret; | |
3768 | } | |
3769 | ||
3770 | /* | |
d063d709 DG |
3771 | * Setup sockets and directory needed by the kconsumerd communication with the |
3772 | * session daemon. | |
d6f42150 | 3773 | */ |
67e40797 DG |
3774 | static int set_consumer_sockets(struct consumer_data *consumer_data, |
3775 | const char *rundir) | |
d6f42150 DG |
3776 | { |
3777 | int ret; | |
67e40797 | 3778 | char path[PATH_MAX]; |
d6f42150 | 3779 | |
67e40797 | 3780 | switch (consumer_data->type) { |
7753dea8 | 3781 | case LTTNG_CONSUMER_KERNEL: |
60922cb0 | 3782 | snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir); |
7753dea8 MD |
3783 | break; |
3784 | case LTTNG_CONSUMER64_UST: | |
60922cb0 | 3785 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir); |
7753dea8 MD |
3786 | break; |
3787 | case LTTNG_CONSUMER32_UST: | |
60922cb0 | 3788 | snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir); |
7753dea8 MD |
3789 | break; |
3790 | default: | |
3791 | ERR("Consumer type unknown"); | |
3792 | ret = -EINVAL; | |
3793 | goto error; | |
d6f42150 DG |
3794 | } |
3795 | ||
67e40797 DG |
3796 | DBG2("Creating consumer directory: %s", path); |
3797 | ||
d1613cf5 | 3798 | ret = mkdir(path, S_IRWXU); |
d6f42150 | 3799 | if (ret < 0) { |
6beb2242 | 3800 | if (errno != EEXIST) { |
f11e84c2 | 3801 | PERROR("mkdir"); |
3bd1e081 | 3802 | ERR("Failed to create %s", path); |
6beb2242 DG |
3803 | goto error; |
3804 | } | |
f11e84c2 | 3805 | ret = -1; |
d6f42150 DG |
3806 | } |
3807 | ||
3808 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
3809 | consumer_data->err_sock = |
3810 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
3811 | if (consumer_data->err_sock < 0) { | |
3812 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
3813 | ret = -1; |
3814 | goto error; | |
3815 | } | |
3816 | ||
3817 | /* File permission MUST be 660 */ | |
3bd1e081 | 3818 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 3819 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 3820 | if (ret < 0) { |
3bd1e081 | 3821 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 3822 | PERROR("chmod"); |
d6f42150 DG |
3823 | goto error; |
3824 | } | |
3825 | ||
3826 | error: | |
3827 | return ret; | |
3828 | } | |
3829 | ||
fac6795d | 3830 | /* |
d063d709 | 3831 | * Signal handler for the daemon |
cf3af59e | 3832 | * |
54d01ffb DG |
3833 | * Simply stop all worker threads, leaving main() return gracefully after |
3834 | * joining all threads and calling cleanup(). | |
fac6795d DG |
3835 | */ |
3836 | static void sighandler(int sig) | |
3837 | { | |
3838 | switch (sig) { | |
cf3af59e | 3839 | case SIGPIPE: |
af87c45a | 3840 | DBG("SIGPIPE caught"); |
cf3af59e MD |
3841 | return; |
3842 | case SIGINT: | |
af87c45a | 3843 | DBG("SIGINT caught"); |
cf3af59e MD |
3844 | stop_threads(); |
3845 | break; | |
3846 | case SIGTERM: | |
af87c45a | 3847 | DBG("SIGTERM caught"); |
cf3af59e MD |
3848 | stop_threads(); |
3849 | break; | |
3850 | default: | |
3851 | break; | |
fac6795d | 3852 | } |
fac6795d DG |
3853 | } |
3854 | ||
3855 | /* | |
d063d709 | 3856 | * Setup signal handler for : |
1d4b027a | 3857 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 3858 | */ |
1d4b027a | 3859 | static int set_signal_handler(void) |
fac6795d | 3860 | { |
1d4b027a DG |
3861 | int ret = 0; |
3862 | struct sigaction sa; | |
3863 | sigset_t sigset; | |
fac6795d | 3864 | |
1d4b027a | 3865 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 3866 | PERROR("sigemptyset"); |
1d4b027a DG |
3867 | return ret; |
3868 | } | |
d6f42150 | 3869 | |
1d4b027a DG |
3870 | sa.sa_handler = sighandler; |
3871 | sa.sa_mask = sigset; | |
3872 | sa.sa_flags = 0; | |
3873 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
76d7553f | 3874 | PERROR("sigaction"); |
1d4b027a | 3875 | return ret; |
d6f42150 DG |
3876 | } |
3877 | ||
1d4b027a | 3878 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 3879 | PERROR("sigaction"); |
1d4b027a | 3880 | return ret; |
d6f42150 | 3881 | } |
aaf26714 | 3882 | |
1d4b027a | 3883 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
76d7553f | 3884 | PERROR("sigaction"); |
1d4b027a | 3885 | return ret; |
8c0faa1d DG |
3886 | } |
3887 | ||
1d4b027a DG |
3888 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
3889 | ||
3890 | return ret; | |
fac6795d DG |
3891 | } |
3892 | ||
f3ed775e | 3893 | /* |
d063d709 DG |
3894 | * Set open files limit to unlimited. This daemon can open a large number of |
3895 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
3896 | */ |
3897 | static void set_ulimit(void) | |
3898 | { | |
3899 | int ret; | |
3900 | struct rlimit lim; | |
3901 | ||
a88df331 | 3902 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
3903 | lim.rlim_cur = 65535; |
3904 | lim.rlim_max = 65535; | |
3905 | ||
3906 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
3907 | if (ret < 0) { | |
76d7553f | 3908 | PERROR("failed to set open files limit"); |
f3ed775e DG |
3909 | } |
3910 | } | |
3911 | ||
35f90c40 DG |
3912 | /* |
3913 | * Write pidfile using the rundir and opt_pidfile. | |
3914 | */ | |
3915 | static void write_pidfile(void) | |
3916 | { | |
3917 | int ret; | |
3918 | char pidfile_path[PATH_MAX]; | |
3919 | ||
3920 | assert(rundir); | |
3921 | ||
3922 | if (opt_pidfile) { | |
3923 | strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path)); | |
3924 | } else { | |
3925 | /* Build pidfile path from rundir and opt_pidfile. */ | |
3926 | ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/" | |
3927 | DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir); | |
3928 | if (ret < 0) { | |
3929 | PERROR("snprintf pidfile path"); | |
3930 | goto error; | |
3931 | } | |
3932 | } | |
3933 | ||
3934 | /* | |
3935 | * Create pid file in rundir. Return value is of no importance. The | |
3936 | * execution will continue even though we are not able to write the file. | |
3937 | */ | |
3938 | (void) utils_create_pid_file(getpid(), pidfile_path); | |
3939 | ||
3940 | error: | |
3941 | return; | |
3942 | } | |
3943 | ||
fac6795d DG |
3944 | /* |
3945 | * main | |
3946 | */ | |
3947 | int main(int argc, char **argv) | |
3948 | { | |
fac6795d DG |
3949 | int ret = 0; |
3950 | void *status; | |
ae9e45b3 | 3951 | const char *home_path, *env_app_timeout; |
fac6795d | 3952 | |
335a95b7 MD |
3953 | init_kernel_workarounds(); |
3954 | ||
f6a9efaa DG |
3955 | rcu_register_thread(); |
3956 | ||
7753dea8 | 3957 | setup_consumerd_path(); |
fb09408a | 3958 | |
fac6795d DG |
3959 | /* Parse arguments */ |
3960 | progname = argv[0]; | |
c617c0c6 | 3961 | if ((ret = parse_args(argc, argv)) < 0) { |
cf3af59e | 3962 | goto error; |
fac6795d DG |
3963 | } |
3964 | ||
3965 | /* Daemonize */ | |
3966 | if (opt_daemon) { | |
ceed52b5 MD |
3967 | int i; |
3968 | ||
3969 | /* | |
3970 | * fork | |
3971 | * child: setsid, close FD 0, 1, 2, chdir / | |
3972 | * parent: exit (if fork is successful) | |
3973 | */ | |
53094c05 DG |
3974 | ret = daemon(0, 0); |
3975 | if (ret < 0) { | |
76d7553f | 3976 | PERROR("daemon"); |
cf3af59e | 3977 | goto error; |
53094c05 | 3978 | } |
ceed52b5 MD |
3979 | /* |
3980 | * We are in the child. Make sure all other file | |
3981 | * descriptors are closed, in case we are called with | |
3982 | * more opened file descriptors than the standard ones. | |
3983 | */ | |
3984 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
3985 | (void) close(i); | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | /* Create thread quit pipe */ | |
3990 | if ((ret = init_thread_quit_pipe()) < 0) { | |
3991 | goto error; | |
fac6795d DG |
3992 | } |
3993 | ||
3994 | /* Check if daemon is UID = 0 */ | |
3995 | is_root = !getuid(); | |
3996 | ||
fac6795d | 3997 | if (is_root) { |
990570ed | 3998 | rundir = strdup(DEFAULT_LTTNG_RUNDIR); |
67e40797 DG |
3999 | |
4000 | /* Create global run dir with root access */ | |
4001 | ret = create_lttng_rundir(rundir); | |
d6f42150 | 4002 | if (ret < 0) { |
cf3af59e | 4003 | goto error; |
d6f42150 DG |
4004 | } |
4005 | ||
fac6795d | 4006 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
4007 | snprintf(apps_unix_sock_path, PATH_MAX, |
4008 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
4009 | } |
4010 | ||
4011 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
4012 | snprintf(client_unix_sock_path, PATH_MAX, |
4013 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
4014 | } | |
0fdd1e2c DG |
4015 | |
4016 | /* Set global SHM for ust */ | |
4017 | if (strlen(wait_shm_path) == 0) { | |
4018 | snprintf(wait_shm_path, PATH_MAX, | |
4019 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
4020 | } | |
67e40797 | 4021 | |
44a5e5eb DG |
4022 | if (strlen(health_unix_sock_path) == 0) { |
4023 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4024 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK); | |
4025 | } | |
4026 | ||
67e40797 DG |
4027 | /* Setup kernel consumerd path */ |
4028 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4029 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir); |
67e40797 | 4030 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4031 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4032 | |
4033 | DBG2("Kernel consumer err path: %s", | |
4034 | kconsumer_data.err_unix_sock_path); | |
4035 | DBG2("Kernel consumer cmd path: %s", | |
4036 | kconsumer_data.cmd_unix_sock_path); | |
fac6795d | 4037 | } else { |
b082db07 DG |
4038 | home_path = get_home_dir(); |
4039 | if (home_path == NULL) { | |
273ea72c DG |
4040 | /* TODO: Add --socket PATH option */ |
4041 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
4042 | ret = -EPERM; |
4043 | goto error; | |
b082db07 DG |
4044 | } |
4045 | ||
67e40797 DG |
4046 | /* |
4047 | * Create rundir from home path. This will create something like | |
4048 | * $HOME/.lttng | |
4049 | */ | |
990570ed | 4050 | ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path); |
67e40797 DG |
4051 | if (ret < 0) { |
4052 | ret = -ENOMEM; | |
4053 | goto error; | |
4054 | } | |
4055 | ||
4056 | ret = create_lttng_rundir(rundir); | |
4057 | if (ret < 0) { | |
4058 | goto error; | |
4059 | } | |
4060 | ||
fac6795d | 4061 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 4062 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 4063 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
4064 | } |
4065 | ||
4066 | /* Set the cli tool unix socket path */ | |
4067 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 4068 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 4069 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 4070 | } |
0fdd1e2c DG |
4071 | |
4072 | /* Set global SHM for ust */ | |
4073 | if (strlen(wait_shm_path) == 0) { | |
4074 | snprintf(wait_shm_path, PATH_MAX, | |
d0b96690 | 4075 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); |
0fdd1e2c | 4076 | } |
44a5e5eb DG |
4077 | |
4078 | /* Set health check Unix path */ | |
4079 | if (strlen(health_unix_sock_path) == 0) { | |
4080 | snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), | |
4081 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path); | |
4082 | } | |
fac6795d DG |
4083 | } |
4084 | ||
5c827ce0 DG |
4085 | /* Set consumer initial state */ |
4086 | kernel_consumerd_state = CONSUMER_STOPPED; | |
4087 | ust_consumerd_state = CONSUMER_STOPPED; | |
4088 | ||
847177cd DG |
4089 | DBG("Client socket path %s", client_unix_sock_path); |
4090 | DBG("Application socket path %s", apps_unix_sock_path); | |
d0b96690 | 4091 | DBG("Application wait path %s", wait_shm_path); |
67e40797 DG |
4092 | DBG("LTTng run directory path: %s", rundir); |
4093 | ||
4094 | /* 32 bits consumerd path setup */ | |
4095 | snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4096 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir); |
67e40797 | 4097 | snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4098 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4099 | |
4100 | DBG2("UST consumer 32 bits err path: %s", | |
4101 | ustconsumer32_data.err_unix_sock_path); | |
4102 | DBG2("UST consumer 32 bits cmd path: %s", | |
4103 | ustconsumer32_data.cmd_unix_sock_path); | |
4104 | ||
4105 | /* 64 bits consumerd path setup */ | |
4106 | snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, | |
60922cb0 | 4107 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir); |
67e40797 | 4108 | snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, |
60922cb0 | 4109 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir); |
67e40797 DG |
4110 | |
4111 | DBG2("UST consumer 64 bits err path: %s", | |
4112 | ustconsumer64_data.err_unix_sock_path); | |
4113 | DBG2("UST consumer 64 bits cmd path: %s", | |
4114 | ustconsumer64_data.cmd_unix_sock_path); | |
847177cd | 4115 | |
273ea72c | 4116 | /* |
7d8234d9 | 4117 | * See if daemon already exist. |
fac6795d | 4118 | */ |
7d8234d9 | 4119 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 4120 | ERR("Already running daemon.\n"); |
273ea72c | 4121 | /* |
cf3af59e MD |
4122 | * We do not goto exit because we must not cleanup() |
4123 | * because a daemon is already running. | |
ab118b20 | 4124 | */ |
cf3af59e | 4125 | goto error; |
a88df331 DG |
4126 | } |
4127 | ||
1427f9b2 DG |
4128 | /* |
4129 | * Init UST app hash table. Alloc hash table before this point since | |
4130 | * cleanup() can get called after that point. | |
4131 | */ | |
4132 | ust_app_ht_alloc(); | |
4133 | ||
54d01ffb | 4134 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
4135 | |
4136 | /* | |
4137 | * These actions must be executed as root. We do that *after* setting up | |
4138 | * the sockets path because we MUST make the check for another daemon using | |
4139 | * those paths *before* trying to set the kernel consumer sockets and init | |
4140 | * kernel tracer. | |
4141 | */ | |
4142 | if (is_root) { | |
67e40797 | 4143 | ret = set_consumer_sockets(&kconsumer_data, rundir); |
7753dea8 MD |
4144 | if (ret < 0) { |
4145 | goto exit; | |
4146 | } | |
4147 | ||
a88df331 | 4148 | /* Setup kernel tracer */ |
4fba7219 DG |
4149 | if (!opt_no_kernel) { |
4150 | init_kernel_tracer(); | |
4151 | } | |
a88df331 DG |
4152 | |
4153 | /* Set ulimit for open files */ | |
4154 | set_ulimit(); | |
fac6795d | 4155 | } |
4063050c MD |
4156 | /* init lttng_fd tracking must be done after set_ulimit. */ |
4157 | lttng_fd_init(); | |
fac6795d | 4158 | |
67e40797 DG |
4159 | ret = set_consumer_sockets(&ustconsumer64_data, rundir); |
4160 | if (ret < 0) { | |
4161 | goto exit; | |
4162 | } | |
4163 | ||
4164 | ret = set_consumer_sockets(&ustconsumer32_data, rundir); | |
4165 | if (ret < 0) { | |
4166 | goto exit; | |
4167 | } | |
4168 | ||
cf3af59e MD |
4169 | if ((ret = set_signal_handler()) < 0) { |
4170 | goto exit; | |
fac6795d DG |
4171 | } |
4172 | ||
d6f42150 | 4173 | /* Setup the needed unix socket */ |
cf3af59e MD |
4174 | if ((ret = init_daemon_socket()) < 0) { |
4175 | goto exit; | |
fac6795d DG |
4176 | } |
4177 | ||
4178 | /* Set credentials to socket */ | |
be040666 | 4179 | if (is_root && ((ret = set_permissions(rundir)) < 0)) { |
cf3af59e | 4180 | goto exit; |
fac6795d DG |
4181 | } |
4182 | ||
5b8719f5 DG |
4183 | /* Get parent pid if -S, --sig-parent is specified. */ |
4184 | if (opt_sig_parent) { | |
4185 | ppid = getppid(); | |
4186 | } | |
4187 | ||
7a485870 | 4188 | /* Setup the kernel pipe for waking up the kernel thread */ |
6620da75 DG |
4189 | if (is_root && !opt_no_kernel) { |
4190 | if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) { | |
4191 | goto exit; | |
4192 | } | |
7a485870 DG |
4193 | } |
4194 | ||
099e26bd | 4195 | /* Setup the thread apps communication pipe. */ |
ef599319 | 4196 | if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) { |
099e26bd DG |
4197 | goto exit; |
4198 | } | |
4199 | ||
d0b96690 DG |
4200 | /* Setup the thread apps notify communication pipe. */ |
4201 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) { | |
4202 | goto exit; | |
4203 | } | |
4204 | ||
099e26bd DG |
4205 | /* Init UST command queue. */ |
4206 | cds_wfq_init(&ust_cmd_queue.queue); | |
4207 | ||
273ea72c | 4208 | /* |
54d01ffb DG |
4209 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
4210 | * statically declared in session.c | |
273ea72c | 4211 | */ |
54d01ffb | 4212 | session_list_ptr = session_get_list(); |
b5541356 | 4213 | |
5eb91c98 DG |
4214 | /* Set up max poll set size */ |
4215 | lttng_poll_set_max_size(); | |
4216 | ||
2f77fc4b | 4217 | cmd_init(); |
00e2e675 | 4218 | |
ae9e45b3 DG |
4219 | /* Check for the application socket timeout env variable. */ |
4220 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
4221 | if (env_app_timeout) { | |
4222 | app_socket_timeout = atoi(env_app_timeout); | |
4223 | } else { | |
4224 | app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; | |
4225 | } | |
4226 | ||
35f90c40 DG |
4227 | write_pidfile(); |
4228 | ||
44a5e5eb DG |
4229 | /* Create thread to manage the client socket */ |
4230 | ret = pthread_create(&health_thread, NULL, | |
4231 | thread_manage_health, (void *) NULL); | |
4232 | if (ret != 0) { | |
4233 | PERROR("pthread_create health"); | |
4234 | goto exit_health; | |
4235 | } | |
4236 | ||
cf3af59e | 4237 | /* Create thread to manage the client socket */ |
099e26bd DG |
4238 | ret = pthread_create(&client_thread, NULL, |
4239 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 4240 | if (ret != 0) { |
76d7553f | 4241 | PERROR("pthread_create clients"); |
cf3af59e MD |
4242 | goto exit_client; |
4243 | } | |
fac6795d | 4244 | |
099e26bd DG |
4245 | /* Create thread to dispatch registration */ |
4246 | ret = pthread_create(&dispatch_thread, NULL, | |
4247 | thread_dispatch_ust_registration, (void *) NULL); | |
4248 | if (ret != 0) { | |
76d7553f | 4249 | PERROR("pthread_create dispatch"); |
099e26bd DG |
4250 | goto exit_dispatch; |
4251 | } | |
4252 | ||
4253 | /* Create thread to manage application registration. */ | |
4254 | ret = pthread_create(®_apps_thread, NULL, | |
4255 | thread_registration_apps, (void *) NULL); | |
4256 | if (ret != 0) { | |
76d7553f | 4257 | PERROR("pthread_create registration"); |
099e26bd DG |
4258 | goto exit_reg_apps; |
4259 | } | |
4260 | ||
cf3af59e | 4261 | /* Create thread to manage application socket */ |
54d01ffb DG |
4262 | ret = pthread_create(&apps_thread, NULL, |
4263 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 4264 | if (ret != 0) { |
d0b96690 DG |
4265 | PERROR("pthread_create apps"); |
4266 | goto exit_apps; | |
4267 | } | |
4268 | ||
4269 | /* Create thread to manage application notify socket */ | |
4270 | ret = pthread_create(&apps_notify_thread, NULL, | |
4271 | ust_thread_manage_notify, (void *) NULL); | |
4272 | if (ret != 0) { | |
76d7553f | 4273 | PERROR("pthread_create apps"); |
cf3af59e MD |
4274 | goto exit_apps; |
4275 | } | |
fac6795d | 4276 | |
6620da75 DG |
4277 | /* Don't start this thread if kernel tracing is not requested nor root */ |
4278 | if (is_root && !opt_no_kernel) { | |
4279 | /* Create kernel thread to manage kernel event */ | |
4280 | ret = pthread_create(&kernel_thread, NULL, | |
4281 | thread_manage_kernel, (void *) NULL); | |
4282 | if (ret != 0) { | |
4283 | PERROR("pthread_create kernel"); | |
4284 | goto exit_kernel; | |
4285 | } | |
7a485870 | 4286 | |
6620da75 DG |
4287 | ret = pthread_join(kernel_thread, &status); |
4288 | if (ret != 0) { | |
4289 | PERROR("pthread_join"); | |
4290 | goto error; /* join error, exit without cleanup */ | |
4291 | } | |
fac6795d DG |
4292 | } |
4293 | ||
cf3af59e MD |
4294 | exit_kernel: |
4295 | ret = pthread_join(apps_thread, &status); | |
4296 | if (ret != 0) { | |
76d7553f | 4297 | PERROR("pthread_join"); |
cf3af59e MD |
4298 | goto error; /* join error, exit without cleanup */ |
4299 | } | |
fac6795d | 4300 | |
cf3af59e | 4301 | exit_apps: |
099e26bd DG |
4302 | ret = pthread_join(reg_apps_thread, &status); |
4303 | if (ret != 0) { | |
76d7553f | 4304 | PERROR("pthread_join"); |
099e26bd DG |
4305 | goto error; /* join error, exit without cleanup */ |
4306 | } | |
4307 | ||
4308 | exit_reg_apps: | |
4309 | ret = pthread_join(dispatch_thread, &status); | |
4310 | if (ret != 0) { | |
76d7553f | 4311 | PERROR("pthread_join"); |
099e26bd DG |
4312 | goto error; /* join error, exit without cleanup */ |
4313 | } | |
4314 | ||
4315 | exit_dispatch: | |
cf3af59e MD |
4316 | ret = pthread_join(client_thread, &status); |
4317 | if (ret != 0) { | |
76d7553f | 4318 | PERROR("pthread_join"); |
cf3af59e MD |
4319 | goto error; /* join error, exit without cleanup */ |
4320 | } | |
4321 | ||
3bd1e081 | 4322 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 4323 | if (ret != 0) { |
76d7553f | 4324 | PERROR("join_consumer"); |
cf3af59e MD |
4325 | goto error; /* join error, exit without cleanup */ |
4326 | } | |
a88df331 | 4327 | |
06f525de DG |
4328 | ret = join_consumer_thread(&ustconsumer32_data); |
4329 | if (ret != 0) { | |
4330 | PERROR("join_consumer ust32"); | |
4331 | goto error; /* join error, exit without cleanup */ | |
4332 | } | |
4333 | ||
4334 | ret = join_consumer_thread(&ustconsumer64_data); | |
4335 | if (ret != 0) { | |
4336 | PERROR("join_consumer ust64"); | |
4337 | goto error; /* join error, exit without cleanup */ | |
4338 | } | |
4339 | ||
cf3af59e | 4340 | exit_client: |
06f525de DG |
4341 | ret = pthread_join(health_thread, &status); |
4342 | if (ret != 0) { | |
4343 | PERROR("pthread_join health thread"); | |
4344 | goto error; /* join error, exit without cleanup */ | |
4345 | } | |
4346 | ||
44a5e5eb | 4347 | exit_health: |
a88df331 | 4348 | exit: |
cf3af59e MD |
4349 | /* |
4350 | * cleanup() is called when no other thread is running. | |
4351 | */ | |
f6a9efaa | 4352 | rcu_thread_online(); |
cf3af59e | 4353 | cleanup(); |
f6a9efaa DG |
4354 | rcu_thread_offline(); |
4355 | rcu_unregister_thread(); | |
67e40797 | 4356 | if (!ret) { |
cf3af59e | 4357 | exit(EXIT_SUCCESS); |
67e40797 | 4358 | } |
cf3af59e | 4359 | error: |
5e16da05 | 4360 | exit(EXIT_FAILURE); |
fac6795d | 4361 | } |