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