Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
26296c48 | 4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
91d76f53 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
91d76f53 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
18 | */ |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
fac6795d DG |
21 | #include <getopt.h> |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
0bb7724a | 24 | #include <paths.h> |
fac6795d DG |
25 | #include <pthread.h> |
26 | #include <signal.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
331744e3 | 30 | #include <inttypes.h> |
0fdd1e2c | 31 | #include <sys/mman.h> |
b73401da | 32 | #include <sys/mount.h> |
1e307fab | 33 | #include <sys/resource.h> |
fac6795d DG |
34 | #include <sys/socket.h> |
35 | #include <sys/stat.h> | |
36 | #include <sys/types.h> | |
0fdd1e2c | 37 | #include <sys/wait.h> |
5c827ce0 | 38 | #include <urcu/uatomic.h> |
fac6795d | 39 | #include <unistd.h> |
4f0b90ee | 40 | #include <ctype.h> |
fac6795d | 41 | |
990570ed | 42 | #include <common/common.h> |
d27c42b8 | 43 | #include <common/compat/socket.h> |
e8fa9fb0 | 44 | #include <common/compat/getenv.h> |
db758600 DG |
45 | #include <common/defaults.h> |
46 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 47 | #include <common/futex.h> |
00e2e675 | 48 | #include <common/relayd/relayd.h> |
81b86775 | 49 | #include <common/utils.h> |
3ccdf997 | 50 | #include <common/daemonize.h> |
f40ef1d5 | 51 | #include <common/config/session-config.h> |
dcabc190 FD |
52 | #include <common/dynamic-buffer.h> |
53 | #include <lttng/userspace-probe-internal.h> | |
76fcf151 | 54 | #include <lttng/event-internal.h> |
fac6795d | 55 | |
10a8a223 | 56 | #include "lttng-sessiond.h" |
7972aab2 | 57 | #include "buffer-registry.h" |
54d01ffb | 58 | #include "channel.h" |
2f77fc4b | 59 | #include "cmd.h" |
00e2e675 | 60 | #include "consumer.h" |
099e26bd | 61 | #include "context.h" |
54d01ffb | 62 | #include "event.h" |
4771f025 | 63 | #include "kernel.h" |
f1e16794 | 64 | #include "kernel-consumer.h" |
096102bd | 65 | #include "modprobe.h" |
0fdd1e2c | 66 | #include "shm.h" |
1e307fab | 67 | #include "ust-ctl.h" |
00e2e675 | 68 | #include "ust-consumer.h" |
8e68d1c8 | 69 | #include "utils.h" |
4063050c | 70 | #include "fd-limit.h" |
8782cc74 | 71 | #include "health-sessiond.h" |
8ac94142 | 72 | #include "testpoint.h" |
d0b96690 | 73 | #include "ust-thread.h" |
022d91ba | 74 | #include "agent-thread.h" |
fb198a11 | 75 | #include "save.h" |
ef367a93 | 76 | #include "load-session-thread.h" |
b3530820 JG |
77 | #include "notification-thread.h" |
78 | #include "notification-thread-commands.h" | |
db66e574 | 79 | #include "rotation-thread.h" |
0dbc2034 | 80 | #include "lttng-syscall.h" |
7c1d2758 | 81 | #include "agent.h" |
5e97de00 | 82 | #include "ht-cleanup.h" |
e6142f2e | 83 | #include "sessiond-config.h" |
8e319828 | 84 | #include "timer.h" |
ebaeda94 | 85 | |
4fc83d94 PP |
86 | static const char *help_msg = |
87 | #ifdef LTTNG_EMBED_HELP | |
88 | #include <lttng-sessiond.8.h> | |
89 | #else | |
90 | NULL | |
91 | #endif | |
92 | ; | |
93 | ||
fac6795d | 94 | const char *progname; |
c9cb3e7d | 95 | static int lockfile_fd = -1; |
3bd1e081 | 96 | |
0bb7724a DG |
97 | /* Set to 1 when a SIGUSR1 signal is received. */ |
98 | static int recv_child_signal; | |
99 | ||
88076e89 JD |
100 | static struct lttng_kernel_tracer_version kernel_tracer_version; |
101 | static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version; | |
102 | ||
a23ec3a7 DG |
103 | /* |
104 | * Consumer daemon specific control data. Every value not initialized here is | |
105 | * set to 0 by the static definition. | |
106 | */ | |
3bd1e081 MD |
107 | static struct consumer_data kconsumer_data = { |
108 | .type = LTTNG_CONSUMER_KERNEL, | |
03550b58 MD |
109 | .err_sock = -1, |
110 | .cmd_sock = -1, | |
b3530820 | 111 | .channel_monitor_pipe = -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 | 116 | }; |
7753dea8 MD |
117 | static struct consumer_data ustconsumer64_data = { |
118 | .type = LTTNG_CONSUMER64_UST, | |
03550b58 MD |
119 | .err_sock = -1, |
120 | .cmd_sock = -1, | |
b3530820 | 121 | .channel_monitor_pipe = -1, |
173af62f DG |
122 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
123 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
124 | .cond = PTHREAD_COND_INITIALIZER, |
125 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
7753dea8 MD |
126 | }; |
127 | static struct consumer_data ustconsumer32_data = { | |
128 | .type = LTTNG_CONSUMER32_UST, | |
03550b58 MD |
129 | .err_sock = -1, |
130 | .cmd_sock = -1, | |
b3530820 | 131 | .channel_monitor_pipe = -1, |
173af62f DG |
132 | .pid_mutex = PTHREAD_MUTEX_INITIALIZER, |
133 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
a23ec3a7 DG |
134 | .cond = PTHREAD_COND_INITIALIZER, |
135 | .cond_mutex = PTHREAD_MUTEX_INITIALIZER, | |
3bd1e081 MD |
136 | }; |
137 | ||
26296c48 JG |
138 | /* Command line options */ |
139 | static const struct option long_options[] = { | |
0f5ea17c JG |
140 | { "client-sock", required_argument, 0, 'c' }, |
141 | { "apps-sock", required_argument, 0, 'a' }, | |
142 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
143 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
144 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
145 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
146 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
147 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
148 | { "consumerd32-path", required_argument, 0, '\0' }, | |
149 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
150 | { "consumerd64-path", required_argument, 0, '\0' }, | |
151 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
152 | { "daemonize", no_argument, 0, 'd' }, | |
153 | { "background", no_argument, 0, 'b' }, | |
154 | { "sig-parent", no_argument, 0, 'S' }, | |
155 | { "help", no_argument, 0, 'h' }, | |
156 | { "group", required_argument, 0, 'g' }, | |
157 | { "version", no_argument, 0, 'V' }, | |
158 | { "quiet", no_argument, 0, 'q' }, | |
159 | { "verbose", no_argument, 0, 'v' }, | |
160 | { "verbose-consumer", no_argument, 0, '\0' }, | |
161 | { "no-kernel", no_argument, 0, '\0' }, | |
162 | { "pidfile", required_argument, 0, 'p' }, | |
163 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
164 | { "config", required_argument, 0, 'f' }, | |
165 | { "load", required_argument, 0, 'l' }, | |
166 | { "kmod-probes", required_argument, 0, '\0' }, | |
167 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
168 | { NULL, 0, 0, 0 } |
169 | }; | |
170 | ||
171 | /* Command line options to ignore from configuration file */ | |
172 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
173 | ||
26c9d55e | 174 | /* Shared between threads */ |
099e26bd | 175 | static int dispatch_thread_exit; |
fac6795d | 176 | |
1d4b027a | 177 | /* Sockets and FDs */ |
a4b35e07 MD |
178 | static int client_sock = -1; |
179 | static int apps_sock = -1; | |
1d4b027a | 180 | |
099e26bd DG |
181 | /* |
182 | * This pipe is used to inform the thread managing application communication | |
183 | * that a command is queued and ready to be processed. | |
184 | */ | |
76d7553f | 185 | static int apps_cmd_pipe[2] = { -1, -1 }; |
099e26bd | 186 | |
1d4b027a | 187 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 188 | static pthread_t apps_thread; |
d0b96690 | 189 | static pthread_t apps_notify_thread; |
099e26bd | 190 | static pthread_t reg_apps_thread; |
1d4b027a | 191 | static pthread_t client_thread; |
7a485870 | 192 | static pthread_t kernel_thread; |
099e26bd | 193 | static pthread_t dispatch_thread; |
44a5e5eb | 194 | static pthread_t health_thread; |
0b2dc8df | 195 | static pthread_t ht_cleanup_thread; |
022d91ba | 196 | static pthread_t agent_reg_thread; |
ef367a93 | 197 | static pthread_t load_session_thread; |
b3530820 | 198 | static pthread_t notification_thread; |
db66e574 | 199 | static pthread_t rotation_thread; |
d086f507 | 200 | static pthread_t timer_thread; |
5eb91c98 | 201 | |
099e26bd DG |
202 | /* |
203 | * UST registration command queue. This queue is tied with a futex and uses a N | |
204 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
205 | * | |
b22c5da8 DG |
206 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
207 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
208 | * the line new application socket and monitors it for any I/O error or clean | |
209 | * close that triggers an unregistration of the application. | |
099e26bd DG |
210 | */ |
211 | static struct ust_cmd_queue ust_cmd_queue; | |
212 | ||
2f77fc4b DG |
213 | static const char *module_proc_lttng = "/proc/lttng"; |
214 | ||
5c827ce0 DG |
215 | /* |
216 | * Consumer daemon state which is changed when spawning it, killing it or in | |
217 | * case of a fatal error. | |
218 | */ | |
219 | enum consumerd_state { | |
220 | CONSUMER_STARTED = 1, | |
221 | CONSUMER_STOPPED = 2, | |
222 | CONSUMER_ERROR = 3, | |
223 | }; | |
224 | ||
225 | /* | |
226 | * This consumer daemon state is used to validate if a client command will be | |
227 | * able to reach the consumer. If not, the client is informed. For instance, | |
228 | * doing a "lttng start" when the consumer state is set to ERROR will return an | |
229 | * error to the client. | |
230 | * | |
231 | * The following example shows a possible race condition of this scheme: | |
232 | * | |
233 | * consumer thread error happens | |
234 | * client cmd arrives | |
235 | * client cmd checks state -> still OK | |
236 | * consumer thread exit, sets error | |
237 | * client cmd try to talk to consumer | |
238 | * ... | |
239 | * | |
240 | * However, since the consumer is a different daemon, we have no way of making | |
241 | * sure the command will reach it safely even with this state flag. This is why | |
242 | * we consider that up to the state validation during command processing, the | |
243 | * command is safe. After that, we can not guarantee the correctness of the | |
244 | * client request vis-a-vis the consumer. | |
245 | */ | |
246 | static enum consumerd_state ust_consumerd_state; | |
247 | static enum consumerd_state kernel_consumerd_state; | |
248 | ||
ef367a93 | 249 | /* Load session thread information to operate. */ |
a7333da7 | 250 | static struct load_session_thread_data *load_info; |
7c1d2758 | 251 | |
97bc1426 | 252 | /* |
a7333da7 | 253 | * Section name to look for in the daemon configuration file. |
97bc1426 | 254 | */ |
a7333da7 | 255 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 256 | |
a7333da7 JG |
257 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
258 | static int is_root; | |
5eb91c98 | 259 | |
a7333da7 JG |
260 | /* Rotation thread handle. */ |
261 | static struct rotation_thread_handle *rotation_thread_handle; | |
4a15001e | 262 | |
099e26bd DG |
263 | /* |
264 | * Stop all threads by closing the thread quit pipe. | |
265 | */ | |
cf3af59e MD |
266 | static void stop_threads(void) |
267 | { | |
5eb91c98 DG |
268 | int ret; |
269 | ||
cf3af59e MD |
270 | /* Stopping all threads */ |
271 | DBG("Terminating all threads"); | |
a7333da7 | 272 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
273 | if (ret < 0) { |
274 | ERR("write error on thread quit pipe"); | |
275 | } | |
276 | ||
099e26bd | 277 | /* Dispatch thread */ |
26c9d55e | 278 | CMM_STORE_SHARED(dispatch_thread_exit, 1); |
099e26bd | 279 | futex_nto1_wake(&ust_cmd_queue.futex); |
cf3af59e MD |
280 | } |
281 | ||
e975f9f8 DG |
282 | /* |
283 | * Close every consumer sockets. | |
284 | */ | |
285 | static void close_consumer_sockets(void) | |
286 | { | |
287 | int ret; | |
288 | ||
289 | if (kconsumer_data.err_sock >= 0) { | |
290 | ret = close(kconsumer_data.err_sock); | |
291 | if (ret < 0) { | |
292 | PERROR("kernel consumer err_sock close"); | |
293 | } | |
294 | } | |
295 | if (ustconsumer32_data.err_sock >= 0) { | |
296 | ret = close(ustconsumer32_data.err_sock); | |
297 | if (ret < 0) { | |
a76cbd9f | 298 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
299 | } |
300 | } | |
301 | if (ustconsumer64_data.err_sock >= 0) { | |
302 | ret = close(ustconsumer64_data.err_sock); | |
303 | if (ret < 0) { | |
a76cbd9f | 304 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
305 | } |
306 | } | |
307 | if (kconsumer_data.cmd_sock >= 0) { | |
308 | ret = close(kconsumer_data.cmd_sock); | |
309 | if (ret < 0) { | |
310 | PERROR("kernel consumer cmd_sock close"); | |
311 | } | |
312 | } | |
313 | if (ustconsumer32_data.cmd_sock >= 0) { | |
314 | ret = close(ustconsumer32_data.cmd_sock); | |
315 | if (ret < 0) { | |
a76cbd9f | 316 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
317 | } |
318 | } | |
319 | if (ustconsumer64_data.cmd_sock >= 0) { | |
320 | ret = close(ustconsumer64_data.cmd_sock); | |
321 | if (ret < 0) { | |
a76cbd9f | 322 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
323 | } |
324 | } | |
b3530820 JG |
325 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
326 | ret = close(kconsumer_data.channel_monitor_pipe); | |
327 | if (ret < 0) { | |
328 | PERROR("kernel consumer channel monitor pipe close"); | |
329 | } | |
330 | } | |
331 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
332 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
333 | if (ret < 0) { | |
334 | PERROR("UST consumerd32 channel monitor pipe close"); | |
335 | } | |
336 | } | |
337 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
338 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
339 | if (ret < 0) { | |
340 | PERROR("UST consumerd64 channel monitor pipe close"); | |
341 | } | |
342 | } | |
e975f9f8 DG |
343 | } |
344 | ||
4e4714cb JR |
345 | /* |
346 | * Wait on consumer process termination. | |
347 | * | |
348 | * Need to be called with the consumer data lock held or from a context | |
349 | * ensuring no concurrent access to data (e.g: cleanup). | |
350 | */ | |
351 | static void wait_consumer(struct consumer_data *consumer_data) | |
352 | { | |
353 | pid_t ret; | |
354 | int status; | |
355 | ||
356 | if (consumer_data->pid <= 0) { | |
357 | return; | |
358 | } | |
359 | ||
360 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
361 | consumer_data->pid); | |
362 | ret = waitpid(consumer_data->pid, &status, 0); | |
363 | if (ret == -1) { | |
364 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 365 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
366 | ERR("consumerd termination with error: %d", |
367 | WEXITSTATUS(ret)); | |
368 | } | |
369 | consumer_data->pid = 0; | |
370 | } | |
371 | ||
fac6795d | 372 | /* |
4a15001e | 373 | * Cleanup the session daemon's data structures. |
fac6795d | 374 | */ |
4a15001e | 375 | static void sessiond_cleanup(void) |
fac6795d | 376 | { |
ef599319 | 377 | int ret; |
e32d7f27 | 378 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 379 | |
4a15001e | 380 | DBG("Cleanup sessiond"); |
e07ae692 | 381 | |
4e449f3f MD |
382 | /* |
383 | * Close the thread quit pipe. It has already done its job, | |
384 | * since we are now called. | |
385 | */ | |
a7333da7 | 386 | sessiond_close_quit_pipe(); |
2f77fc4b | 387 | |
c9a2957d JG |
388 | ret = remove(config.pid_file_path.value); |
389 | if (ret < 0) { | |
390 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
391 | } |
392 | ||
e6142f2e JG |
393 | DBG("Removing sessiond and consumerd content of directory %s", |
394 | config.rundir.value); | |
8c6c56c2 MD |
395 | |
396 | /* sessiond */ | |
e6142f2e JG |
397 | DBG("Removing %s", config.pid_file_path.value); |
398 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 399 | |
e6142f2e JG |
400 | DBG("Removing %s", config.agent_port_file_path.value); |
401 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 402 | |
8c6c56c2 | 403 | /* kconsumerd */ |
e6142f2e JG |
404 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
405 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
406 | ||
407 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
408 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
409 | |
410 | /* ust consumerd 32 */ | |
e6142f2e JG |
411 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
412 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
413 | ||
414 | DBG("Removing directory %s", config.consumerd32_path.value); | |
415 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
416 | |
417 | /* ust consumerd 64 */ | |
e6142f2e JG |
418 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
419 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
420 | ||
421 | DBG("Removing directory %s", config.consumerd64_path.value); | |
422 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 423 | |
99d688f2 | 424 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 425 | |
4e4714cb JR |
426 | wait_consumer(&kconsumer_data); |
427 | wait_consumer(&ustconsumer64_data); | |
428 | wait_consumer(&ustconsumer32_data); | |
429 | ||
6a4e4039 JG |
430 | DBG("Cleaning up all agent apps"); |
431 | agent_app_ht_clean(); | |
432 | ||
099e26bd | 433 | DBG("Closing all UST sockets"); |
56fff090 | 434 | ust_app_clean_list(); |
7972aab2 | 435 | buffer_reg_destroy_registries(); |
099e26bd | 436 | |
e6142f2e | 437 | if (is_root && !config.no_kernel) { |
4fba7219 | 438 | DBG2("Closing kernel fd"); |
a4b35e07 | 439 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
440 | ret = close(kernel_tracer_fd); |
441 | if (ret) { | |
442 | PERROR("close"); | |
443 | } | |
a4b35e07 | 444 | } |
2f50c8a3 | 445 | DBG("Unloading kernel modules"); |
096102bd | 446 | modprobe_remove_lttng_all(); |
834978fd | 447 | free(syscall_table); |
2f50c8a3 | 448 | } |
2f77fc4b | 449 | |
e975f9f8 DG |
450 | close_consumer_sockets(); |
451 | ||
ef367a93 JG |
452 | if (load_info) { |
453 | load_session_destroy_data(load_info); | |
454 | free(load_info); | |
455 | } | |
456 | ||
c9cb3e7d JG |
457 | /* |
458 | * We do NOT rmdir rundir because there are other processes | |
459 | * using it, for instance lttng-relayd, which can start in | |
460 | * parallel with this teardown. | |
461 | */ | |
4a15001e MD |
462 | } |
463 | ||
464 | /* | |
465 | * Cleanup the daemon's option data structures. | |
466 | */ | |
467 | static void sessiond_cleanup_options(void) | |
468 | { | |
469 | DBG("Cleaning up options"); | |
470 | ||
e6142f2e | 471 | sessiond_config_fini(&config); |
c9cb3e7d | 472 | |
7567352f | 473 | run_as_destroy_worker(); |
fac6795d DG |
474 | } |
475 | ||
e065084a | 476 | /* |
d063d709 | 477 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 478 | * |
d063d709 | 479 | * Return lttcomm error code. |
e065084a DG |
480 | */ |
481 | static int send_unix_sock(int sock, void *buf, size_t len) | |
482 | { | |
483 | /* Check valid length */ | |
c617c0c6 | 484 | if (len == 0) { |
e065084a DG |
485 | return -1; |
486 | } | |
487 | ||
488 | return lttcomm_send_unix_sock(sock, buf, len); | |
489 | } | |
490 | ||
5461b305 | 491 | /* |
d063d709 | 492 | * Free memory of a command context structure. |
5461b305 | 493 | */ |
a2fb29a5 | 494 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 495 | { |
a2fb29a5 DG |
496 | DBG("Clean command context structure"); |
497 | if (*cmd_ctx) { | |
498 | if ((*cmd_ctx)->llm) { | |
499 | free((*cmd_ctx)->llm); | |
5461b305 | 500 | } |
a2fb29a5 DG |
501 | if ((*cmd_ctx)->lsm) { |
502 | free((*cmd_ctx)->lsm); | |
5461b305 | 503 | } |
a2fb29a5 DG |
504 | free(*cmd_ctx); |
505 | *cmd_ctx = NULL; | |
5461b305 DG |
506 | } |
507 | } | |
508 | ||
fac6795d | 509 | /* |
0fdd1e2c | 510 | * Notify UST applications using the shm mmap futex. |
fac6795d | 511 | */ |
0fdd1e2c | 512 | static int notify_ust_apps(int active) |
fac6795d | 513 | { |
0fdd1e2c | 514 | char *wait_shm_mmap; |
fac6795d | 515 | |
0fdd1e2c | 516 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 517 | |
0fdd1e2c | 518 | /* See shm.c for this call implying mmap, shm and futex calls */ |
e6142f2e | 519 | wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root); |
0fdd1e2c | 520 | if (wait_shm_mmap == NULL) { |
fac6795d DG |
521 | goto error; |
522 | } | |
523 | ||
0fdd1e2c DG |
524 | /* Wake waiting process */ |
525 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
526 | ||
527 | /* Apps notified successfully */ | |
528 | return 0; | |
fac6795d DG |
529 | |
530 | error: | |
0fdd1e2c | 531 | return -1; |
fac6795d DG |
532 | } |
533 | ||
e065084a | 534 | /* |
d063d709 DG |
535 | * Setup the outgoing data buffer for the response (llm) by allocating the |
536 | * right amount of memory and copying the original information from the lsm | |
537 | * structure. | |
ca95a216 | 538 | * |
6e10c9b9 | 539 | * Return 0 on success, negative value on error. |
ca95a216 | 540 | */ |
6e10c9b9 PP |
541 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, |
542 | const void *payload_buf, size_t payload_len, | |
543 | const void *cmd_header_buf, size_t cmd_header_len) | |
ca95a216 | 544 | { |
6e10c9b9 PP |
545 | int ret = 0; |
546 | const size_t header_len = sizeof(struct lttcomm_lttng_msg); | |
547 | const size_t cmd_header_offset = header_len; | |
548 | const size_t payload_offset = cmd_header_offset + cmd_header_len; | |
549 | const size_t total_msg_size = header_len + cmd_header_len + payload_len; | |
ca95a216 | 550 | |
6e10c9b9 | 551 | cmd_ctx->llm = zmalloc(total_msg_size); |
5461b305 | 552 | |
5461b305 | 553 | if (cmd_ctx->llm == NULL) { |
76d7553f | 554 | PERROR("zmalloc"); |
5461b305 | 555 | ret = -ENOMEM; |
6e10c9b9 | 556 | goto end; |
ca95a216 DG |
557 | } |
558 | ||
5461b305 DG |
559 | /* Copy common data */ |
560 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 561 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
6e10c9b9 PP |
562 | cmd_ctx->llm->cmd_header_size = cmd_header_len; |
563 | cmd_ctx->llm->data_size = payload_len; | |
564 | cmd_ctx->lttng_msg_size = total_msg_size; | |
5461b305 | 565 | |
6e10c9b9 | 566 | /* Copy command header */ |
b4e3ceb9 PP |
567 | if (cmd_header_len) { |
568 | memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf, | |
569 | cmd_header_len); | |
570 | } | |
5461b305 | 571 | |
6e10c9b9 | 572 | /* Copy payload */ |
b4e3ceb9 PP |
573 | if (payload_len) { |
574 | memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf, | |
575 | payload_len); | |
576 | } | |
ca95a216 | 577 | |
6e10c9b9 | 578 | end: |
ca95a216 DG |
579 | return ret; |
580 | } | |
581 | ||
6e10c9b9 PP |
582 | /* |
583 | * Version of setup_lttng_msg() without command header. | |
584 | */ | |
585 | static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx, | |
586 | void *payload_buf, size_t payload_len) | |
587 | { | |
588 | return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0); | |
589 | } | |
7a485870 | 590 | /* |
5eb91c98 | 591 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 592 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 593 | */ |
5eb91c98 | 594 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 595 | { |
5eb91c98 | 596 | int ret; |
7a485870 | 597 | struct ltt_kernel_channel *channel; |
e32d7f27 JG |
598 | struct ltt_session *session; |
599 | const struct ltt_session_list *session_list = session_get_list(); | |
7a485870 | 600 | |
5eb91c98 | 601 | DBG("Updating kernel poll set"); |
7a485870 | 602 | |
54d01ffb | 603 | session_lock_list(); |
e32d7f27 JG |
604 | cds_list_for_each_entry(session, &session_list->head, list) { |
605 | if (!session_get(session)) { | |
606 | continue; | |
607 | } | |
54d01ffb | 608 | session_lock(session); |
7a485870 | 609 | if (session->kernel_session == NULL) { |
54d01ffb | 610 | session_unlock(session); |
e32d7f27 | 611 | session_put(session); |
7a485870 DG |
612 | continue; |
613 | } | |
7a485870 | 614 | |
54d01ffb DG |
615 | cds_list_for_each_entry(channel, |
616 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
617 | /* Add channel fd to the kernel poll set */ |
618 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
619 | if (ret < 0) { | |
54d01ffb | 620 | session_unlock(session); |
e32d7f27 | 621 | session_put(session); |
5eb91c98 DG |
622 | goto error; |
623 | } | |
624 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 625 | } |
54d01ffb | 626 | session_unlock(session); |
7a485870 | 627 | } |
54d01ffb | 628 | session_unlock_list(); |
7a485870 | 629 | |
5eb91c98 | 630 | return 0; |
7a485870 DG |
631 | |
632 | error: | |
54d01ffb | 633 | session_unlock_list(); |
7a485870 DG |
634 | return -1; |
635 | } | |
636 | ||
637 | /* | |
54d01ffb | 638 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 639 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 640 | * |
d063d709 | 641 | * Useful for CPU hotplug feature. |
7a485870 | 642 | */ |
7f032058 | 643 | static int update_kernel_stream(int fd) |
7a485870 DG |
644 | { |
645 | int ret = 0; | |
646 | struct ltt_session *session; | |
173af62f | 647 | struct ltt_kernel_session *ksess; |
7a485870 | 648 | struct ltt_kernel_channel *channel; |
e32d7f27 | 649 | const struct ltt_session_list *session_list = session_get_list(); |
7a485870 DG |
650 | |
651 | DBG("Updating kernel streams for channel fd %d", fd); | |
652 | ||
54d01ffb | 653 | session_lock_list(); |
e32d7f27 JG |
654 | cds_list_for_each_entry(session, &session_list->head, list) { |
655 | if (!session_get(session)) { | |
656 | continue; | |
657 | } | |
54d01ffb | 658 | session_lock(session); |
7a485870 | 659 | if (session->kernel_session == NULL) { |
54d01ffb | 660 | session_unlock(session); |
e32d7f27 | 661 | session_put(session); |
7a485870 DG |
662 | continue; |
663 | } | |
173af62f | 664 | ksess = session->kernel_session; |
d9800920 | 665 | |
4a15001e MD |
666 | cds_list_for_each_entry(channel, |
667 | &ksess->channel_list.head, list) { | |
668 | struct lttng_ht_iter iter; | |
669 | struct consumer_socket *socket; | |
d9800920 | 670 | |
4a15001e MD |
671 | if (channel->fd != fd) { |
672 | continue; | |
673 | } | |
674 | DBG("Channel found, updating kernel streams"); | |
675 | ret = kernel_open_channel_stream(channel); | |
676 | if (ret < 0) { | |
677 | goto error; | |
678 | } | |
679 | /* Update the stream global counter */ | |
680 | ksess->stream_count_global += ret; | |
681 | ||
682 | /* | |
683 | * Have we already sent fds to the consumer? If yes, it | |
684 | * means that tracing is started so it is safe to send | |
685 | * our updated stream fds. | |
686 | */ | |
687 | if (ksess->consumer_fds_sent != 1 | |
688 | || ksess->consumer == NULL) { | |
689 | ret = -1; | |
690 | goto error; | |
691 | } | |
692 | ||
693 | rcu_read_lock(); | |
694 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, | |
695 | &iter.iter, socket, node.node) { | |
696 | pthread_mutex_lock(socket->lock); | |
1fc1b7c8 | 697 | ret = kernel_consumer_send_channel_streams(socket, |
4a15001e MD |
698 | channel, ksess, |
699 | session->output_traces ? 1 : 0); | |
700 | pthread_mutex_unlock(socket->lock); | |
701 | if (ret < 0) { | |
e7fe706f | 702 | rcu_read_unlock(); |
4a15001e | 703 | goto error; |
7a485870 | 704 | } |
7a485870 | 705 | } |
4a15001e | 706 | rcu_read_unlock(); |
7a485870 | 707 | } |
54d01ffb | 708 | session_unlock(session); |
e32d7f27 | 709 | session_put(session); |
7a485870 | 710 | } |
54d01ffb | 711 | session_unlock_list(); |
b3c750d2 | 712 | return ret; |
7a485870 | 713 | |
b3c750d2 | 714 | error: |
54d01ffb | 715 | session_unlock(session); |
e32d7f27 | 716 | session_put(session); |
54d01ffb | 717 | session_unlock_list(); |
7a485870 DG |
718 | return ret; |
719 | } | |
720 | ||
487cf67c | 721 | /* |
ffe60014 DG |
722 | * For each tracing session, update newly registered apps. The session list |
723 | * lock MUST be acquired before calling this. | |
487cf67c DG |
724 | */ |
725 | static void update_ust_app(int app_sock) | |
726 | { | |
727 | struct ltt_session *sess, *stmp; | |
e32d7f27 | 728 | const struct ltt_session_list *session_list = session_get_list(); |
487cf67c | 729 | |
fdadac08 DG |
730 | /* Consumer is in an ERROR state. Stop any application update. */ |
731 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
732 | /* Stop the update process since the consumer is dead. */ | |
733 | return; | |
734 | } | |
735 | ||
487cf67c | 736 | /* For all tracing session(s) */ |
e32d7f27 | 737 | cds_list_for_each_entry_safe(sess, stmp, &session_list->head, list) { |
a9ad0c8f MD |
738 | struct ust_app *app; |
739 | ||
e32d7f27 JG |
740 | if (!session_get(sess)) { |
741 | continue; | |
742 | } | |
4ee14516 | 743 | session_lock(sess); |
a9ad0c8f MD |
744 | if (!sess->ust_session) { |
745 | goto unlock_session; | |
746 | } | |
747 | ||
748 | rcu_read_lock(); | |
749 | assert(app_sock >= 0); | |
750 | app = ust_app_find_by_sock(app_sock); | |
751 | if (app == NULL) { | |
752 | /* | |
753 | * Application can be unregistered before so | |
754 | * this is possible hence simply stopping the | |
755 | * update. | |
756 | */ | |
757 | DBG3("UST app update failed to find app sock %d", | |
758 | app_sock); | |
759 | goto unlock_rcu; | |
421cb601 | 760 | } |
a9ad0c8f MD |
761 | ust_app_global_update(sess->ust_session, app); |
762 | unlock_rcu: | |
763 | rcu_read_unlock(); | |
764 | unlock_session: | |
4ee14516 | 765 | session_unlock(sess); |
e32d7f27 | 766 | session_put(sess); |
487cf67c DG |
767 | } |
768 | } | |
769 | ||
7a485870 | 770 | /* |
d063d709 | 771 | * This thread manage event coming from the kernel. |
7a485870 | 772 | * |
d063d709 DG |
773 | * Features supported in this thread: |
774 | * -) CPU Hotplug | |
7a485870 DG |
775 | */ |
776 | static void *thread_manage_kernel(void *data) | |
777 | { | |
139ac872 | 778 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
5eb91c98 | 779 | uint32_t revents, nb_fd; |
7a485870 | 780 | char tmp; |
5eb91c98 | 781 | struct lttng_poll_event events; |
7a485870 | 782 | |
6993eeb3 | 783 | DBG("[thread] Thread manage kernel started"); |
7a485870 | 784 | |
6c71277b | 785 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
927ca06a | 786 | |
d5d63bf1 DG |
787 | /* |
788 | * This first step of the while is to clean this structure which could free | |
6d737ce4 | 789 | * non NULL pointers so initialize it before the loop. |
d5d63bf1 | 790 | */ |
6d737ce4 | 791 | lttng_poll_init(&events); |
d5d63bf1 | 792 | |
e547b070 | 793 | if (testpoint(sessiond_thread_manage_kernel)) { |
6993eeb3 CB |
794 | goto error_testpoint; |
795 | } | |
8ac94142 | 796 | |
840cb59c | 797 | health_code_update(); |
44a5e5eb | 798 | |
e547b070 | 799 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
d21b0d71 | 800 | goto error_testpoint; |
6993eeb3 CB |
801 | } |
802 | ||
7a485870 | 803 | while (1) { |
840cb59c | 804 | health_code_update(); |
44a5e5eb | 805 | |
7a485870 | 806 | if (update_poll_flag == 1) { |
d21b0d71 DG |
807 | /* Clean events object. We are about to populate it again. */ |
808 | lttng_poll_clean(&events); | |
809 | ||
d0b96690 | 810 | ret = sessiond_set_thread_pollset(&events, 2); |
d21b0d71 DG |
811 | if (ret < 0) { |
812 | goto error_poll_create; | |
813 | } | |
814 | ||
815 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
816 | if (ret < 0) { | |
817 | goto error; | |
818 | } | |
5f822d0a | 819 | |
d21b0d71 | 820 | /* This will add the available kernel channel if any. */ |
5eb91c98 DG |
821 | ret = update_kernel_poll(&events); |
822 | if (ret < 0) { | |
7a485870 DG |
823 | goto error; |
824 | } | |
825 | update_poll_flag = 0; | |
826 | } | |
827 | ||
7fa2082e | 828 | DBG("Thread kernel polling"); |
7a485870 DG |
829 | |
830 | /* Poll infinite value of time */ | |
88f2b785 | 831 | restart: |
a78af745 | 832 | health_poll_entry(); |
5eb91c98 | 833 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
834 | DBG("Thread kernel return from poll on %d fds", |
835 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 836 | health_poll_exit(); |
7a485870 | 837 | if (ret < 0) { |
88f2b785 MD |
838 | /* |
839 | * Restart interrupted system call. | |
840 | */ | |
841 | if (errno == EINTR) { | |
842 | goto restart; | |
843 | } | |
7a485870 DG |
844 | goto error; |
845 | } else if (ret == 0) { | |
846 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
847 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
848 | "This should not have happened! Continuing..."); | |
7a485870 DG |
849 | continue; |
850 | } | |
851 | ||
0d9c5d77 DG |
852 | nb_fd = ret; |
853 | ||
5eb91c98 DG |
854 | for (i = 0; i < nb_fd; i++) { |
855 | /* Fetch once the poll data */ | |
856 | revents = LTTNG_POLL_GETEV(&events, i); | |
857 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 858 | |
840cb59c | 859 | health_code_update(); |
44a5e5eb | 860 | |
fd20dac9 MD |
861 | if (!revents) { |
862 | /* No activity for this FD (poll implementation). */ | |
863 | continue; | |
864 | } | |
865 | ||
5eb91c98 | 866 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 867 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 868 | if (ret) { |
139ac872 MD |
869 | err = 0; |
870 | goto exit; | |
5eb91c98 | 871 | } |
7a485870 | 872 | |
5eb91c98 | 873 | /* Check for data on kernel pipe */ |
03e43155 MD |
874 | if (revents & LPOLLIN) { |
875 | if (pollfd == kernel_poll_pipe[0]) { | |
876 | (void) lttng_read(kernel_poll_pipe[0], | |
877 | &tmp, 1); | |
878 | /* | |
879 | * Ret value is useless here, if this pipe gets any actions an | |
880 | * update is required anyway. | |
881 | */ | |
882 | update_poll_flag = 1; | |
883 | continue; | |
884 | } else { | |
885 | /* | |
886 | * New CPU detected by the kernel. Adding kernel stream to | |
887 | * kernel session and updating the kernel consumer | |
888 | */ | |
7f032058 | 889 | ret = update_kernel_stream(pollfd); |
5eb91c98 DG |
890 | if (ret < 0) { |
891 | continue; | |
892 | } | |
893 | break; | |
7a485870 | 894 | } |
03e43155 MD |
895 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
896 | update_poll_flag = 1; | |
897 | continue; | |
898 | } else { | |
899 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
900 | goto error; | |
7a485870 DG |
901 | } |
902 | } | |
903 | } | |
904 | ||
139ac872 | 905 | exit: |
7a485870 | 906 | error: |
5eb91c98 | 907 | lttng_poll_clean(&events); |
76d7553f | 908 | error_poll_create: |
6993eeb3 | 909 | error_testpoint: |
6620da75 DG |
910 | utils_close_pipe(kernel_poll_pipe); |
911 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; | |
139ac872 | 912 | if (err) { |
840cb59c | 913 | health_error(); |
139ac872 | 914 | ERR("Health error occurred in %s", __func__); |
6620da75 DG |
915 | WARN("Kernel thread died unexpectedly. " |
916 | "Kernel tracing can continue but CPU hotplug is disabled."); | |
139ac872 | 917 | } |
8782cc74 | 918 | health_unregister(health_sessiond); |
76d7553f | 919 | DBG("Kernel thread dying"); |
7a485870 DG |
920 | return NULL; |
921 | } | |
922 | ||
a23ec3a7 DG |
923 | /* |
924 | * Signal pthread condition of the consumer data that the thread. | |
925 | */ | |
926 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
927 | { | |
928 | pthread_mutex_lock(&data->cond_mutex); | |
929 | ||
930 | /* | |
931 | * The state is set before signaling. It can be any value, it's the waiter | |
932 | * job to correctly interpret this condition variable associated to the | |
933 | * consumer pthread_cond. | |
934 | * | |
935 | * A value of 0 means that the corresponding thread of the consumer data | |
936 | * was not started. 1 indicates that the thread has started and is ready | |
937 | * for action. A negative value means that there was an error during the | |
938 | * thread bootstrap. | |
939 | */ | |
940 | data->consumer_thread_is_ready = state; | |
941 | (void) pthread_cond_signal(&data->cond); | |
942 | ||
943 | pthread_mutex_unlock(&data->cond_mutex); | |
944 | } | |
945 | ||
1d4b027a | 946 | /* |
3bd1e081 | 947 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 948 | */ |
3bd1e081 | 949 | static void *thread_manage_consumer(void *data) |
1d4b027a | 950 | { |
42fc1d0b | 951 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 952 | uint32_t revents, nb_fd; |
1d4b027a | 953 | enum lttcomm_return_code code; |
5eb91c98 | 954 | struct lttng_poll_event events; |
3bd1e081 | 955 | struct consumer_data *consumer_data = data; |
b3530820 | 956 | struct consumer_socket *cmd_socket_wrapper = NULL; |
1d4b027a | 957 | |
3bd1e081 | 958 | DBG("[thread] Manage consumer started"); |
1d4b027a | 959 | |
34c1e15a MD |
960 | rcu_register_thread(); |
961 | rcu_thread_online(); | |
962 | ||
6c71277b | 963 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 964 | |
855060f8 | 965 | health_code_update(); |
9449cc75 | 966 | |
5eb91c98 | 967 | /* |
331744e3 JD |
968 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
969 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 970 | */ |
331744e3 | 971 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 972 | if (ret < 0) { |
76d7553f | 973 | goto error_poll; |
5eb91c98 | 974 | } |
273ea72c | 975 | |
edb8b045 DG |
976 | /* |
977 | * The error socket here is already in a listening state which was done | |
978 | * just before spawning this thread to avoid a race between the consumer | |
979 | * daemon exec trying to connect and the listen() call. | |
980 | */ | |
3bd1e081 | 981 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
982 | if (ret < 0) { |
983 | goto error; | |
984 | } | |
985 | ||
840cb59c | 986 | health_code_update(); |
44a5e5eb | 987 | |
331744e3 | 988 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 989 | restart: |
a78af745 | 990 | health_poll_entry(); |
8ac94142 | 991 | |
e547b070 | 992 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
993 | goto error; |
994 | } | |
8ac94142 | 995 | |
5eb91c98 | 996 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 997 | health_poll_exit(); |
273ea72c | 998 | if (ret < 0) { |
88f2b785 MD |
999 | /* |
1000 | * Restart interrupted system call. | |
1001 | */ | |
1002 | if (errno == EINTR) { | |
1003 | goto restart; | |
1004 | } | |
273ea72c DG |
1005 | goto error; |
1006 | } | |
1007 | ||
0d9c5d77 DG |
1008 | nb_fd = ret; |
1009 | ||
5eb91c98 DG |
1010 | for (i = 0; i < nb_fd; i++) { |
1011 | /* Fetch once the poll data */ | |
1012 | revents = LTTNG_POLL_GETEV(&events, i); | |
1013 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1014 | ||
840cb59c | 1015 | health_code_update(); |
44a5e5eb | 1016 | |
fd20dac9 MD |
1017 | if (!revents) { |
1018 | /* No activity for this FD (poll implementation). */ | |
1019 | continue; | |
1020 | } | |
1021 | ||
5eb91c98 | 1022 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1023 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1024 | if (ret) { |
139ac872 MD |
1025 | err = 0; |
1026 | goto exit; | |
5eb91c98 DG |
1027 | } |
1028 | ||
1029 | /* Event on the registration socket */ | |
3bd1e081 | 1030 | if (pollfd == consumer_data->err_sock) { |
03e43155 MD |
1031 | if (revents & LPOLLIN) { |
1032 | continue; | |
1033 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 1034 | ERR("consumer err socket poll error"); |
5eb91c98 | 1035 | goto error; |
03e43155 MD |
1036 | } else { |
1037 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1038 | goto error; | |
5eb91c98 DG |
1039 | } |
1040 | } | |
273ea72c DG |
1041 | } |
1042 | ||
3bd1e081 | 1043 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
1044 | if (sock < 0) { |
1045 | goto error; | |
1046 | } | |
1047 | ||
b662582b DG |
1048 | /* |
1049 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1050 | * show must go on. | |
1051 | */ | |
1052 | (void) utils_set_fd_cloexec(sock); | |
1053 | ||
840cb59c | 1054 | health_code_update(); |
44a5e5eb | 1055 | |
3bd1e081 | 1056 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 1057 | |
712ea556 | 1058 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
1059 | ret = lttcomm_recv_unix_sock(sock, &code, |
1060 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
1061 | if (ret <= 0) { |
1062 | goto error; | |
1063 | } | |
1064 | ||
840cb59c | 1065 | health_code_update(); |
b3530820 | 1066 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 | 1067 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
1068 | lttcomm_get_readable_code(-code)); |
1069 | goto error; | |
1070 | } | |
1071 | ||
b3530820 JG |
1072 | /* Connect both command and metadata sockets. */ |
1073 | consumer_data->cmd_sock = | |
1074 | lttcomm_connect_unix_sock( | |
1075 | consumer_data->cmd_unix_sock_path); | |
1076 | consumer_data->metadata_fd = | |
1077 | lttcomm_connect_unix_sock( | |
1078 | consumer_data->cmd_unix_sock_path); | |
1079 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { | |
1080 | PERROR("consumer connect cmd socket"); | |
1081 | /* On error, signal condition and quit. */ | |
1082 | signal_consumer_condition(consumer_data, -1); | |
1083 | goto error; | |
1084 | } | |
1085 | ||
1086 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; | |
1087 | ||
1088 | /* Create metadata socket lock. */ | |
1089 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
1090 | if (consumer_data->metadata_sock.lock == NULL) { | |
1091 | PERROR("zmalloc pthread mutex"); | |
1092 | goto error; | |
1093 | } | |
1094 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
1095 | ||
1096 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); | |
1097 | DBG("Consumer metadata socket ready (fd: %d)", | |
1098 | consumer_data->metadata_fd); | |
1099 | ||
1100 | /* | |
1101 | * Remove the consumerd error sock since we've established a connection. | |
1102 | */ | |
3bd1e081 | 1103 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 1104 | if (ret < 0) { |
72079cae DG |
1105 | goto error; |
1106 | } | |
1107 | ||
331744e3 | 1108 | /* Add new accepted error socket. */ |
5eb91c98 DG |
1109 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
1110 | if (ret < 0) { | |
72079cae | 1111 | goto error; |
5eb91c98 DG |
1112 | } |
1113 | ||
331744e3 | 1114 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 1115 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
1116 | LPOLLIN | LPOLLRDHUP); |
1117 | if (ret < 0) { | |
1118 | goto error; | |
1119 | } | |
1120 | ||
840cb59c | 1121 | health_code_update(); |
44a5e5eb | 1122 | |
b3530820 | 1123 | /* |
62c43103 | 1124 | * Transfer the write-end of the channel monitoring and rotate pipe |
92816cc3 | 1125 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command. |
b3530820 JG |
1126 | */ |
1127 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); | |
1128 | if (!cmd_socket_wrapper) { | |
1129 | goto error; | |
1130 | } | |
3e4dc117 | 1131 | cmd_socket_wrapper->lock = &consumer_data->lock; |
b3530820 JG |
1132 | |
1133 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, | |
1134 | consumer_data->channel_monitor_pipe); | |
1135 | if (ret) { | |
1136 | goto error; | |
1137 | } | |
62c43103 | 1138 | |
b3530820 JG |
1139 | /* Discard the socket wrapper as it is no longer needed. */ |
1140 | consumer_destroy_socket(cmd_socket_wrapper); | |
1141 | cmd_socket_wrapper = NULL; | |
1142 | ||
1143 | /* The thread is completely initialized, signal that it is ready. */ | |
1144 | signal_consumer_condition(consumer_data, 1); | |
1145 | ||
331744e3 | 1146 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 1147 | restart_poll: |
331744e3 | 1148 | while (1) { |
42fc1d0b DG |
1149 | health_code_update(); |
1150 | ||
1151 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
1152 | if (should_quit) { | |
1153 | /* Not a health error. */ | |
1154 | err = 0; | |
1155 | goto exit; | |
1156 | } | |
1157 | ||
331744e3 JD |
1158 | health_poll_entry(); |
1159 | ret = lttng_poll_wait(&events, -1); | |
1160 | health_poll_exit(); | |
1161 | if (ret < 0) { | |
1162 | /* | |
1163 | * Restart interrupted system call. | |
1164 | */ | |
1165 | if (errno == EINTR) { | |
1166 | goto restart_poll; | |
1167 | } | |
1168 | goto error; | |
88f2b785 | 1169 | } |
72079cae | 1170 | |
331744e3 | 1171 | nb_fd = ret; |
0d9c5d77 | 1172 | |
331744e3 JD |
1173 | for (i = 0; i < nb_fd; i++) { |
1174 | /* Fetch once the poll data */ | |
1175 | revents = LTTNG_POLL_GETEV(&events, i); | |
1176 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 1177 | |
331744e3 | 1178 | health_code_update(); |
44a5e5eb | 1179 | |
fd20dac9 MD |
1180 | if (!revents) { |
1181 | /* No activity for this FD (poll implementation). */ | |
1182 | continue; | |
1183 | } | |
1184 | ||
42fc1d0b DG |
1185 | /* |
1186 | * Thread quit pipe has been triggered, flag that we should stop | |
1187 | * but continue the current loop to handle potential data from | |
1188 | * consumer. | |
1189 | */ | |
1190 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 1191 | |
331744e3 JD |
1192 | if (pollfd == sock) { |
1193 | /* Event on the consumerd socket */ | |
03e43155 MD |
1194 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
1195 | && !(revents & LPOLLIN)) { | |
331744e3 JD |
1196 | ERR("consumer err socket second poll error"); |
1197 | goto error; | |
1198 | } | |
1199 | health_code_update(); | |
1200 | /* Wait for any kconsumerd error */ | |
1201 | ret = lttcomm_recv_unix_sock(sock, &code, | |
1202 | sizeof(enum lttcomm_return_code)); | |
1203 | if (ret <= 0) { | |
1204 | ERR("consumer closed the command socket"); | |
1205 | goto error; | |
1206 | } | |
1207 | ||
1208 | ERR("consumer return code : %s", | |
1209 | lttcomm_get_readable_code(-code)); | |
1210 | ||
1211 | goto exit; | |
4ce514c4 | 1212 | } else if (pollfd == consumer_data->metadata_fd) { |
03e43155 MD |
1213 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
1214 | && !(revents & LPOLLIN)) { | |
1215 | ERR("consumer err metadata socket second poll error"); | |
1216 | goto error; | |
1217 | } | |
331744e3 JD |
1218 | /* UST metadata requests */ |
1219 | ret = ust_consumer_metadata_request( | |
1220 | &consumer_data->metadata_sock); | |
1221 | if (ret < 0) { | |
1222 | ERR("Handling metadata request"); | |
1223 | goto error; | |
1224 | } | |
5eb91c98 | 1225 | } |
42fc1d0b | 1226 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 1227 | } |
331744e3 | 1228 | health_code_update(); |
5eb91c98 DG |
1229 | } |
1230 | ||
139ac872 | 1231 | exit: |
1d4b027a | 1232 | error: |
fdadac08 DG |
1233 | /* |
1234 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
1235 | * thread might be using them so get exclusive access which will abort all |
1236 | * other consumer command by other threads. | |
fdadac08 DG |
1237 | */ |
1238 | pthread_mutex_lock(&consumer_data->lock); | |
1239 | ||
5c827ce0 DG |
1240 | /* Immediately set the consumerd state to stopped */ |
1241 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
1242 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
1243 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
1244 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
1245 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
1246 | } else { | |
1247 | /* Code flow error... */ | |
1248 | assert(0); | |
1249 | } | |
1250 | ||
76d7553f MD |
1251 | if (consumer_data->err_sock >= 0) { |
1252 | ret = close(consumer_data->err_sock); | |
1253 | if (ret) { | |
1254 | PERROR("close"); | |
1255 | } | |
a76cbd9f | 1256 | consumer_data->err_sock = -1; |
76d7553f MD |
1257 | } |
1258 | if (consumer_data->cmd_sock >= 0) { | |
1259 | ret = close(consumer_data->cmd_sock); | |
1260 | if (ret) { | |
1261 | PERROR("close"); | |
1262 | } | |
a76cbd9f | 1263 | consumer_data->cmd_sock = -1; |
76d7553f | 1264 | } |
96544455 SS |
1265 | if (consumer_data->metadata_sock.fd_ptr && |
1266 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 1267 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
1268 | if (ret) { |
1269 | PERROR("close"); | |
1270 | } | |
1271 | } | |
76d7553f MD |
1272 | if (sock >= 0) { |
1273 | ret = close(sock); | |
1274 | if (ret) { | |
1275 | PERROR("close"); | |
1276 | } | |
1277 | } | |
273ea72c | 1278 | |
3bd1e081 MD |
1279 | unlink(consumer_data->err_unix_sock_path); |
1280 | unlink(consumer_data->cmd_unix_sock_path); | |
fdadac08 | 1281 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 1282 | |
fdadac08 | 1283 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
1284 | if (consumer_data->metadata_sock.lock) { |
1285 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
1286 | free(consumer_data->metadata_sock.lock); | |
1287 | } | |
5eb91c98 | 1288 | lttng_poll_clean(&events); |
b3530820 JG |
1289 | |
1290 | if (cmd_socket_wrapper) { | |
1291 | consumer_destroy_socket(cmd_socket_wrapper); | |
1292 | } | |
76d7553f | 1293 | error_poll: |
139ac872 | 1294 | if (err) { |
840cb59c | 1295 | health_error(); |
139ac872 MD |
1296 | ERR("Health error occurred in %s", __func__); |
1297 | } | |
8782cc74 | 1298 | health_unregister(health_sessiond); |
76d7553f | 1299 | DBG("consumer thread cleanup completed"); |
0177d773 | 1300 | |
34c1e15a MD |
1301 | rcu_thread_offline(); |
1302 | rcu_unregister_thread(); | |
1303 | ||
5eb91c98 | 1304 | return NULL; |
099e26bd DG |
1305 | } |
1306 | ||
099e26bd | 1307 | /* |
81f04d5f JG |
1308 | * This thread receives application command sockets (FDs) on the |
1309 | * apps_cmd_pipe and waits (polls) on them until they are closed | |
1310 | * or an error occurs. | |
1311 | * | |
1312 | * At that point, it flushes the data (tracing and metadata) associated | |
1313 | * with this application and tears down ust app sessions and other | |
1314 | * associated data structures through ust_app_unregister(). | |
1315 | * | |
1316 | * Note that this thread never sends commands to the applications | |
1317 | * through the command sockets; it merely listens for hang-ups | |
1318 | * and errors on those sockets and cleans-up as they occur. | |
1d4b027a DG |
1319 | */ |
1320 | static void *thread_manage_apps(void *data) | |
099e26bd | 1321 | { |
139ac872 | 1322 | int i, ret, pollfd, err = -1; |
6cd525e8 | 1323 | ssize_t size_ret; |
5eb91c98 | 1324 | uint32_t revents, nb_fd; |
5eb91c98 | 1325 | struct lttng_poll_event events; |
099e26bd DG |
1326 | |
1327 | DBG("[thread] Manage application started"); | |
1328 | ||
f6a9efaa DG |
1329 | rcu_register_thread(); |
1330 | rcu_thread_online(); | |
1331 | ||
6c71277b | 1332 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
927ca06a | 1333 | |
e547b070 | 1334 | if (testpoint(sessiond_thread_manage_apps)) { |
6993eeb3 CB |
1335 | goto error_testpoint; |
1336 | } | |
1337 | ||
840cb59c | 1338 | health_code_update(); |
44a5e5eb | 1339 | |
d0b96690 | 1340 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1341 | if (ret < 0) { |
76d7553f | 1342 | goto error_poll_create; |
5eb91c98 | 1343 | } |
099e26bd | 1344 | |
5eb91c98 DG |
1345 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
1346 | if (ret < 0) { | |
1347 | goto error; | |
1348 | } | |
099e26bd | 1349 | |
e547b070 | 1350 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
6993eeb3 CB |
1351 | goto error; |
1352 | } | |
8ac94142 | 1353 | |
840cb59c | 1354 | health_code_update(); |
44a5e5eb | 1355 | |
5eb91c98 | 1356 | while (1) { |
7fa2082e | 1357 | DBG("Apps thread polling"); |
099e26bd DG |
1358 | |
1359 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1360 | restart: |
a78af745 | 1361 | health_poll_entry(); |
5eb91c98 | 1362 | ret = lttng_poll_wait(&events, -1); |
7fa2082e MD |
1363 | DBG("Apps thread return from poll on %d fds", |
1364 | LTTNG_POLL_GETNB(&events)); | |
a78af745 | 1365 | health_poll_exit(); |
099e26bd | 1366 | if (ret < 0) { |
88f2b785 MD |
1367 | /* |
1368 | * Restart interrupted system call. | |
1369 | */ | |
1370 | if (errno == EINTR) { | |
1371 | goto restart; | |
1372 | } | |
099e26bd DG |
1373 | goto error; |
1374 | } | |
1375 | ||
0d9c5d77 DG |
1376 | nb_fd = ret; |
1377 | ||
5eb91c98 DG |
1378 | for (i = 0; i < nb_fd; i++) { |
1379 | /* Fetch once the poll data */ | |
1380 | revents = LTTNG_POLL_GETEV(&events, i); | |
1381 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1382 | ||
840cb59c | 1383 | health_code_update(); |
44a5e5eb | 1384 | |
fd20dac9 MD |
1385 | if (!revents) { |
1386 | /* No activity for this FD (poll implementation). */ | |
1387 | continue; | |
1388 | } | |
1389 | ||
5eb91c98 | 1390 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1391 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1392 | if (ret) { |
139ac872 MD |
1393 | err = 0; |
1394 | goto exit; | |
5eb91c98 | 1395 | } |
099e26bd | 1396 | |
5eb91c98 DG |
1397 | /* Inspect the apps cmd pipe */ |
1398 | if (pollfd == apps_cmd_pipe[0]) { | |
03e43155 | 1399 | if (revents & LPOLLIN) { |
d0b96690 DG |
1400 | int sock; |
1401 | ||
5eb91c98 | 1402 | /* Empty pipe */ |
6cd525e8 MD |
1403 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
1404 | if (size_ret < sizeof(sock)) { | |
76d7553f | 1405 | PERROR("read apps cmd pipe"); |
5eb91c98 DG |
1406 | goto error; |
1407 | } | |
099e26bd | 1408 | |
840cb59c | 1409 | health_code_update(); |
44a5e5eb | 1410 | |
ffe60014 | 1411 | /* |
03e43155 MD |
1412 | * Since this is a command socket (write then read), |
1413 | * we only monitor the error events of the socket. | |
ffe60014 | 1414 | */ |
d0b96690 DG |
1415 | ret = lttng_poll_add(&events, sock, |
1416 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
1417 | if (ret < 0) { | |
5eb91c98 | 1418 | goto error; |
e0c7ec2b | 1419 | } |
acc7b41b | 1420 | |
d0b96690 | 1421 | DBG("Apps with sock %d added to poll set", sock); |
03e43155 MD |
1422 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
1423 | ERR("Apps command pipe error"); | |
1424 | goto error; | |
1425 | } else { | |
1426 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
1427 | goto error; | |
0177d773 | 1428 | } |
5eb91c98 DG |
1429 | } else { |
1430 | /* | |
54d01ffb DG |
1431 | * At this point, we know that a registered application made |
1432 | * the event at poll_wait. | |
5eb91c98 DG |
1433 | */ |
1434 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1435 | /* Removing from the poll set */ | |
1436 | ret = lttng_poll_del(&events, pollfd); | |
1437 | if (ret < 0) { | |
1438 | goto error; | |
1439 | } | |
099e26bd | 1440 | |
b9d9b220 | 1441 | /* Socket closed on remote end. */ |
56fff090 | 1442 | ust_app_unregister(pollfd); |
03e43155 MD |
1443 | } else { |
1444 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1445 | goto error; | |
5eb91c98 | 1446 | } |
099e26bd | 1447 | } |
44a5e5eb | 1448 | |
840cb59c | 1449 | health_code_update(); |
099e26bd | 1450 | } |
099e26bd DG |
1451 | } |
1452 | ||
139ac872 | 1453 | exit: |
099e26bd | 1454 | error: |
5eb91c98 | 1455 | lttng_poll_clean(&events); |
76d7553f | 1456 | error_poll_create: |
6993eeb3 | 1457 | error_testpoint: |
6620da75 DG |
1458 | utils_close_pipe(apps_cmd_pipe); |
1459 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; | |
1460 | ||
1461 | /* | |
1462 | * We don't clean the UST app hash table here since already registered | |
1463 | * applications can still be controlled so let them be until the session | |
1464 | * daemon dies or the applications stop. | |
1465 | */ | |
1466 | ||
139ac872 | 1467 | if (err) { |
840cb59c | 1468 | health_error(); |
139ac872 MD |
1469 | ERR("Health error occurred in %s", __func__); |
1470 | } | |
8782cc74 | 1471 | health_unregister(health_sessiond); |
76d7553f | 1472 | DBG("Application communication apps thread cleanup complete"); |
f6a9efaa DG |
1473 | rcu_thread_offline(); |
1474 | rcu_unregister_thread(); | |
099e26bd DG |
1475 | return NULL; |
1476 | } | |
1477 | ||
d0b96690 | 1478 | /* |
d88aee68 DG |
1479 | * Send a socket to a thread This is called from the dispatch UST registration |
1480 | * thread once all sockets are set for the application. | |
d0b96690 | 1481 | * |
b85dc84c DG |
1482 | * The sock value can be invalid, we don't really care, the thread will handle |
1483 | * it and make the necessary cleanup if so. | |
1484 | * | |
d0b96690 DG |
1485 | * On success, return 0 else a negative value being the errno message of the |
1486 | * write(). | |
1487 | */ | |
d88aee68 | 1488 | static int send_socket_to_thread(int fd, int sock) |
d0b96690 | 1489 | { |
6cd525e8 | 1490 | ssize_t ret; |
d0b96690 | 1491 | |
b85dc84c DG |
1492 | /* |
1493 | * It's possible that the FD is set as invalid with -1 concurrently just | |
1494 | * before calling this function being a shutdown state of the thread. | |
1495 | */ | |
1496 | if (fd < 0) { | |
1497 | ret = -EBADF; | |
1498 | goto error; | |
1499 | } | |
d0b96690 | 1500 | |
6cd525e8 MD |
1501 | ret = lttng_write(fd, &sock, sizeof(sock)); |
1502 | if (ret < sizeof(sock)) { | |
d88aee68 | 1503 | PERROR("write apps pipe %d", fd); |
d0b96690 DG |
1504 | if (ret < 0) { |
1505 | ret = -errno; | |
1506 | } | |
1507 | goto error; | |
1508 | } | |
1509 | ||
1510 | /* All good. Don't send back the write positive ret value. */ | |
1511 | ret = 0; | |
1512 | error: | |
6cd525e8 | 1513 | return (int) ret; |
d0b96690 DG |
1514 | } |
1515 | ||
f45e313d DG |
1516 | /* |
1517 | * Sanitize the wait queue of the dispatch registration thread meaning removing | |
1518 | * invalid nodes from it. This is to avoid memory leaks for the case the UST | |
1519 | * notify socket is never received. | |
1520 | */ | |
1521 | static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) | |
1522 | { | |
1523 | int ret, nb_fd = 0, i; | |
1524 | unsigned int fd_added = 0; | |
1525 | struct lttng_poll_event events; | |
1526 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; | |
1527 | ||
1528 | assert(wait_queue); | |
1529 | ||
1530 | lttng_poll_init(&events); | |
1531 | ||
1532 | /* Just skip everything for an empty queue. */ | |
1533 | if (!wait_queue->count) { | |
1534 | goto end; | |
1535 | } | |
1536 | ||
1537 | ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC); | |
1538 | if (ret < 0) { | |
1539 | goto error_create; | |
1540 | } | |
1541 | ||
1542 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
1543 | &wait_queue->head, head) { | |
1544 | assert(wait_node->app); | |
1545 | ret = lttng_poll_add(&events, wait_node->app->sock, | |
1546 | LPOLLHUP | LPOLLERR); | |
1547 | if (ret < 0) { | |
1548 | goto error; | |
1549 | } | |
1550 | ||
1551 | fd_added = 1; | |
1552 | } | |
1553 | ||
1554 | if (!fd_added) { | |
1555 | goto end; | |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * Poll but don't block so we can quickly identify the faulty events and | |
1560 | * clean them afterwards from the wait queue. | |
1561 | */ | |
1562 | ret = lttng_poll_wait(&events, 0); | |
1563 | if (ret < 0) { | |
1564 | goto error; | |
1565 | } | |
1566 | nb_fd = ret; | |
1567 | ||
1568 | for (i = 0; i < nb_fd; i++) { | |
1569 | /* Get faulty FD. */ | |
1570 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); | |
1571 | int pollfd = LTTNG_POLL_GETFD(&events, i); | |
1572 | ||
fd20dac9 MD |
1573 | if (!revents) { |
1574 | /* No activity for this FD (poll implementation). */ | |
1575 | continue; | |
1576 | } | |
1577 | ||
f45e313d DG |
1578 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
1579 | &wait_queue->head, head) { | |
1580 | if (pollfd == wait_node->app->sock && | |
1581 | (revents & (LPOLLHUP | LPOLLERR))) { | |
1582 | cds_list_del(&wait_node->head); | |
1583 | wait_queue->count--; | |
1584 | ust_app_destroy(wait_node->app); | |
1585 | free(wait_node); | |
48b40bcf JG |
1586 | /* |
1587 | * Silence warning of use-after-free in | |
1588 | * cds_list_for_each_entry_safe which uses | |
1589 | * __typeof__(*wait_node). | |
1590 | */ | |
1591 | wait_node = NULL; | |
f45e313d | 1592 | break; |
03e43155 MD |
1593 | } else { |
1594 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
1595 | goto error; | |
f45e313d DG |
1596 | } |
1597 | } | |
1598 | } | |
1599 | ||
1600 | if (nb_fd > 0) { | |
1601 | DBG("Wait queue sanitized, %d node were cleaned up", nb_fd); | |
1602 | } | |
1603 | ||
1604 | end: | |
1605 | lttng_poll_clean(&events); | |
1606 | return; | |
1607 | ||
1608 | error: | |
1609 | lttng_poll_clean(&events); | |
1610 | error_create: | |
1611 | ERR("Unable to sanitize wait queue"); | |
1612 | return; | |
1613 | } | |
1614 | ||
099e26bd DG |
1615 | /* |
1616 | * Dispatch request from the registration threads to the application | |
1617 | * communication thread. | |
1618 | */ | |
1619 | static void *thread_dispatch_ust_registration(void *data) | |
1620 | { | |
12e2b881 | 1621 | int ret, err = -1; |
8bdee6e2 | 1622 | struct cds_wfcq_node *node; |
099e26bd | 1623 | struct ust_command *ust_cmd = NULL; |
f45e313d DG |
1624 | struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node; |
1625 | struct ust_reg_wait_queue wait_queue = { | |
1626 | .count = 0, | |
1627 | }; | |
d0b96690 | 1628 | |
967e3668 MD |
1629 | rcu_register_thread(); |
1630 | ||
6c71277b | 1631 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH); |
12e2b881 | 1632 | |
9ad42ec1 MD |
1633 | if (testpoint(sessiond_thread_app_reg_dispatch)) { |
1634 | goto error_testpoint; | |
1635 | } | |
1636 | ||
12e2b881 MD |
1637 | health_code_update(); |
1638 | ||
f45e313d | 1639 | CDS_INIT_LIST_HEAD(&wait_queue.head); |
099e26bd DG |
1640 | |
1641 | DBG("[thread] Dispatch UST command started"); | |
1642 | ||
0ed3b1a8 | 1643 | for (;;) { |
12e2b881 MD |
1644 | health_code_update(); |
1645 | ||
099e26bd DG |
1646 | /* Atomically prepare the queue futex */ |
1647 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1648 | ||
0ed3b1a8 MD |
1649 | if (CMM_LOAD_SHARED(dispatch_thread_exit)) { |
1650 | break; | |
1651 | } | |
1652 | ||
099e26bd | 1653 | do { |
d0b96690 | 1654 | struct ust_app *app = NULL; |
7972aab2 | 1655 | ust_cmd = NULL; |
d0b96690 | 1656 | |
f45e313d DG |
1657 | /* |
1658 | * Make sure we don't have node(s) that have hung up before receiving | |
1659 | * the notify socket. This is to clean the list in order to avoid | |
1660 | * memory leaks from notify socket that are never seen. | |
1661 | */ | |
1662 | sanitize_wait_queue(&wait_queue); | |
1663 | ||
12e2b881 | 1664 | health_code_update(); |
099e26bd | 1665 | /* Dequeue command for registration */ |
8bdee6e2 | 1666 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 1667 | if (node == NULL) { |
00a17c97 | 1668 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1669 | /* Continue thread execution */ |
1670 | break; | |
1671 | } | |
1672 | ||
1673 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1674 | ||
2f50c8a3 DG |
1675 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1676 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1677 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1678 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1679 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1680 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
d0b96690 DG |
1681 | |
1682 | if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) { | |
1683 | wait_node = zmalloc(sizeof(*wait_node)); | |
1684 | if (!wait_node) { | |
1685 | PERROR("zmalloc wait_node dispatch"); | |
020d7f60 DG |
1686 | ret = close(ust_cmd->sock); |
1687 | if (ret < 0) { | |
1688 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1689 | } | |
51dec90d | 1690 | lttng_fd_put(LTTNG_FD_APPS, 1); |
7972aab2 | 1691 | free(ust_cmd); |
d0b96690 DG |
1692 | goto error; |
1693 | } | |
1694 | CDS_INIT_LIST_HEAD(&wait_node->head); | |
1695 | ||
1696 | /* Create application object if socket is CMD. */ | |
1697 | wait_node->app = ust_app_create(&ust_cmd->reg_msg, | |
1698 | ust_cmd->sock); | |
1699 | if (!wait_node->app) { | |
1700 | ret = close(ust_cmd->sock); | |
1701 | if (ret < 0) { | |
1702 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
6620da75 | 1703 | } |
51dec90d | 1704 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 1705 | free(wait_node); |
7972aab2 | 1706 | free(ust_cmd); |
d0b96690 DG |
1707 | continue; |
1708 | } | |
1709 | /* | |
1710 | * Add application to the wait queue so we can set the notify | |
1711 | * socket before putting this object in the global ht. | |
1712 | */ | |
f45e313d DG |
1713 | cds_list_add(&wait_node->head, &wait_queue.head); |
1714 | wait_queue.count++; | |
d0b96690 | 1715 | |
7972aab2 | 1716 | free(ust_cmd); |
d0b96690 DG |
1717 | /* |
1718 | * We have to continue here since we don't have the notify | |
1719 | * socket and the application MUST be added to the hash table | |
1720 | * only at that moment. | |
1721 | */ | |
1722 | continue; | |
1723 | } else { | |
1724 | /* | |
1725 | * Look for the application in the local wait queue and set the | |
1726 | * notify socket if found. | |
1727 | */ | |
d88aee68 | 1728 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, |
f45e313d | 1729 | &wait_queue.head, head) { |
12e2b881 | 1730 | health_code_update(); |
d0b96690 DG |
1731 | if (wait_node->app->pid == ust_cmd->reg_msg.pid) { |
1732 | wait_node->app->notify_sock = ust_cmd->sock; | |
1733 | cds_list_del(&wait_node->head); | |
f45e313d | 1734 | wait_queue.count--; |
d0b96690 DG |
1735 | app = wait_node->app; |
1736 | free(wait_node); | |
1737 | DBG3("UST app notify socket %d is set", ust_cmd->sock); | |
1738 | break; | |
1739 | } | |
1740 | } | |
020d7f60 DG |
1741 | |
1742 | /* | |
1743 | * With no application at this stage the received socket is | |
1744 | * basically useless so close it before we free the cmd data | |
1745 | * structure for good. | |
1746 | */ | |
1747 | if (!app) { | |
1748 | ret = close(ust_cmd->sock); | |
1749 | if (ret < 0) { | |
1750 | PERROR("close ust sock dispatch %d", ust_cmd->sock); | |
1751 | } | |
51dec90d | 1752 | lttng_fd_put(LTTNG_FD_APPS, 1); |
020d7f60 | 1753 | } |
7972aab2 | 1754 | free(ust_cmd); |
d0b96690 DG |
1755 | } |
1756 | ||
1757 | if (app) { | |
d0b96690 DG |
1758 | /* |
1759 | * @session_lock_list | |
1760 | * | |
1761 | * Lock the global session list so from the register up to the | |
1762 | * registration done message, no thread can see the application | |
1763 | * and change its state. | |
1764 | */ | |
1765 | session_lock_list(); | |
1766 | rcu_read_lock(); | |
d88aee68 | 1767 | |
d0b96690 DG |
1768 | /* |
1769 | * Add application to the global hash table. This needs to be | |
1770 | * done before the update to the UST registry can locate the | |
1771 | * application. | |
1772 | */ | |
1773 | ust_app_add(app); | |
d88aee68 DG |
1774 | |
1775 | /* Set app version. This call will print an error if needed. */ | |
1776 | (void) ust_app_version(app); | |
1777 | ||
1778 | /* Send notify socket through the notify pipe. */ | |
1779 | ret = send_socket_to_thread(apps_cmd_notify_pipe[1], | |
1780 | app->notify_sock); | |
1781 | if (ret < 0) { | |
1782 | rcu_read_unlock(); | |
1783 | session_unlock_list(); | |
b85dc84c DG |
1784 | /* |
1785 | * No notify thread, stop the UST tracing. However, this is | |
1786 | * not an internal error of the this thread thus setting | |
1787 | * the health error code to a normal exit. | |
1788 | */ | |
1789 | err = 0; | |
d88aee68 | 1790 | goto error; |
6620da75 | 1791 | } |
d88aee68 | 1792 | |
d0b96690 DG |
1793 | /* |
1794 | * Update newly registered application with the tracing | |
1795 | * registry info already enabled information. | |
1796 | */ | |
1797 | update_ust_app(app->sock); | |
d88aee68 DG |
1798 | |
1799 | /* | |
1800 | * Don't care about return value. Let the manage apps threads | |
1801 | * handle app unregistration upon socket close. | |
1802 | */ | |
fb45065e | 1803 | (void) ust_app_register_done(app); |
d88aee68 DG |
1804 | |
1805 | /* | |
1806 | * Even if the application socket has been closed, send the app | |
1807 | * to the thread and unregistration will take place at that | |
1808 | * place. | |
1809 | */ | |
1810 | ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock); | |
d0b96690 | 1811 | if (ret < 0) { |
d88aee68 DG |
1812 | rcu_read_unlock(); |
1813 | session_unlock_list(); | |
b85dc84c DG |
1814 | /* |
1815 | * No apps. thread, stop the UST tracing. However, this is | |
1816 | * not an internal error of the this thread thus setting | |
1817 | * the health error code to a normal exit. | |
1818 | */ | |
1819 | err = 0; | |
d88aee68 | 1820 | goto error; |
d0b96690 | 1821 | } |
d88aee68 | 1822 | |
d0b96690 DG |
1823 | rcu_read_unlock(); |
1824 | session_unlock_list(); | |
099e26bd | 1825 | } |
099e26bd DG |
1826 | } while (node != NULL); |
1827 | ||
12e2b881 | 1828 | health_poll_entry(); |
099e26bd DG |
1829 | /* Futex wait on queue. Blocking call on futex() */ |
1830 | futex_nto1_wait(&ust_cmd_queue.futex); | |
12e2b881 | 1831 | health_poll_exit(); |
099e26bd | 1832 | } |
12e2b881 MD |
1833 | /* Normal exit, no error */ |
1834 | err = 0; | |
099e26bd DG |
1835 | |
1836 | error: | |
d88aee68 DG |
1837 | /* Clean up wait queue. */ |
1838 | cds_list_for_each_entry_safe(wait_node, tmp_wait_node, | |
f45e313d | 1839 | &wait_queue.head, head) { |
d88aee68 | 1840 | cds_list_del(&wait_node->head); |
f45e313d | 1841 | wait_queue.count--; |
d88aee68 DG |
1842 | free(wait_node); |
1843 | } | |
1844 | ||
772b8f4d MD |
1845 | /* Empty command queue. */ |
1846 | for (;;) { | |
1847 | /* Dequeue command for registration */ | |
1848 | node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail); | |
1849 | if (node == NULL) { | |
1850 | break; | |
1851 | } | |
1852 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1853 | ret = close(ust_cmd->sock); | |
1854 | if (ret < 0) { | |
1855 | PERROR("close ust sock exit dispatch %d", ust_cmd->sock); | |
1856 | } | |
1857 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
1858 | free(ust_cmd); | |
1859 | } | |
1860 | ||
9ad42ec1 | 1861 | error_testpoint: |
099e26bd | 1862 | DBG("Dispatch thread dying"); |
12e2b881 MD |
1863 | if (err) { |
1864 | health_error(); | |
1865 | ERR("Health error occurred in %s", __func__); | |
1866 | } | |
8782cc74 | 1867 | health_unregister(health_sessiond); |
967e3668 | 1868 | rcu_unregister_thread(); |
099e26bd DG |
1869 | return NULL; |
1870 | } | |
1871 | ||
1872 | /* | |
1873 | * This thread manage application registration. | |
1874 | */ | |
1875 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1876 | { |
139ac872 | 1877 | int sock = -1, i, ret, pollfd, err = -1; |
5eb91c98 DG |
1878 | uint32_t revents, nb_fd; |
1879 | struct lttng_poll_event events; | |
099e26bd DG |
1880 | /* |
1881 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1882 | * freed in the manage apps thread. | |
1883 | */ | |
1884 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1885 | |
099e26bd | 1886 | DBG("[thread] Manage application registration started"); |
1d4b027a | 1887 | |
6c71277b | 1888 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
927ca06a | 1889 | |
e547b070 | 1890 | if (testpoint(sessiond_thread_registration_apps)) { |
6993eeb3 CB |
1891 | goto error_testpoint; |
1892 | } | |
8ac94142 | 1893 | |
1d4b027a DG |
1894 | ret = lttcomm_listen_unix_sock(apps_sock); |
1895 | if (ret < 0) { | |
76d7553f | 1896 | goto error_listen; |
1d4b027a DG |
1897 | } |
1898 | ||
5eb91c98 DG |
1899 | /* |
1900 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1901 | * more will be added to this poll set. | |
1902 | */ | |
d0b96690 | 1903 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 1904 | if (ret < 0) { |
76d7553f | 1905 | goto error_create_poll; |
5eb91c98 | 1906 | } |
273ea72c | 1907 | |
5eb91c98 DG |
1908 | /* Add the application registration socket */ |
1909 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1910 | if (ret < 0) { | |
76d7553f | 1911 | goto error_poll_add; |
5eb91c98 | 1912 | } |
273ea72c | 1913 | |
1d4b027a | 1914 | /* Notify all applications to register */ |
0fdd1e2c DG |
1915 | ret = notify_ust_apps(1); |
1916 | if (ret < 0) { | |
1917 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1918 | "Execution continues but there might be problem for already\n" |
1919 | "running applications that wishes to register."); | |
0fdd1e2c | 1920 | } |
1d4b027a DG |
1921 | |
1922 | while (1) { | |
1923 | DBG("Accepting application registration"); | |
273ea72c DG |
1924 | |
1925 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 1926 | restart: |
a78af745 | 1927 | health_poll_entry(); |
5eb91c98 | 1928 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 1929 | health_poll_exit(); |
273ea72c | 1930 | if (ret < 0) { |
88f2b785 MD |
1931 | /* |
1932 | * Restart interrupted system call. | |
1933 | */ | |
1934 | if (errno == EINTR) { | |
1935 | goto restart; | |
1936 | } | |
273ea72c DG |
1937 | goto error; |
1938 | } | |
1939 | ||
0d9c5d77 DG |
1940 | nb_fd = ret; |
1941 | ||
5eb91c98 | 1942 | for (i = 0; i < nb_fd; i++) { |
840cb59c | 1943 | health_code_update(); |
139ac872 | 1944 | |
5eb91c98 DG |
1945 | /* Fetch once the poll data */ |
1946 | revents = LTTNG_POLL_GETEV(&events, i); | |
1947 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1948 | |
fd20dac9 MD |
1949 | if (!revents) { |
1950 | /* No activity for this FD (poll implementation). */ | |
1951 | continue; | |
1952 | } | |
1953 | ||
5eb91c98 | 1954 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 1955 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 1956 | if (ret) { |
139ac872 MD |
1957 | err = 0; |
1958 | goto exit; | |
90014c57 | 1959 | } |
1d4b027a | 1960 | |
5eb91c98 DG |
1961 | /* Event on the registration socket */ |
1962 | if (pollfd == apps_sock) { | |
03e43155 | 1963 | if (revents & LPOLLIN) { |
5eb91c98 DG |
1964 | sock = lttcomm_accept_unix_sock(apps_sock); |
1965 | if (sock < 0) { | |
1966 | goto error; | |
1967 | } | |
099e26bd | 1968 | |
16c5c8fa DG |
1969 | /* |
1970 | * Set socket timeout for both receiving and ending. | |
1971 | * app_socket_timeout is in seconds, whereas | |
1972 | * lttcomm_setsockopt_rcv_timeout and | |
1973 | * lttcomm_setsockopt_snd_timeout expect msec as | |
1974 | * parameter. | |
1975 | */ | |
e6142f2e | 1976 | if (config.app_socket_timeout >= 0) { |
28ce0ff2 | 1977 | (void) lttcomm_setsockopt_rcv_timeout(sock, |
e6142f2e | 1978 | config.app_socket_timeout * 1000); |
28ce0ff2 | 1979 | (void) lttcomm_setsockopt_snd_timeout(sock, |
e6142f2e | 1980 | config.app_socket_timeout * 1000); |
28ce0ff2 | 1981 | } |
16c5c8fa | 1982 | |
b662582b DG |
1983 | /* |
1984 | * Set the CLOEXEC flag. Return code is useless because | |
1985 | * either way, the show must go on. | |
1986 | */ | |
1987 | (void) utils_set_fd_cloexec(sock); | |
1988 | ||
5eb91c98 | 1989 | /* Create UST registration command for enqueuing */ |
ba7f0ae5 | 1990 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
5eb91c98 | 1991 | if (ust_cmd == NULL) { |
76d7553f | 1992 | PERROR("ust command zmalloc"); |
41ed8e47 MD |
1993 | ret = close(sock); |
1994 | if (ret) { | |
1995 | PERROR("close"); | |
1996 | } | |
5eb91c98 DG |
1997 | goto error; |
1998 | } | |
1d4b027a | 1999 | |
5eb91c98 DG |
2000 | /* |
2001 | * Using message-based transmissions to ensure we don't | |
2002 | * have to deal with partially received messages. | |
2003 | */ | |
4063050c MD |
2004 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
2005 | if (ret < 0) { | |
2006 | ERR("Exhausted file descriptors allowed for applications."); | |
2007 | free(ust_cmd); | |
2008 | ret = close(sock); | |
2009 | if (ret) { | |
2010 | PERROR("close"); | |
2011 | } | |
2012 | sock = -1; | |
2013 | continue; | |
2014 | } | |
d88aee68 | 2015 | |
840cb59c | 2016 | health_code_update(); |
d0b96690 DG |
2017 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
2018 | if (ret < 0) { | |
5eb91c98 | 2019 | free(ust_cmd); |
d0b96690 | 2020 | /* Close socket of the application. */ |
76d7553f MD |
2021 | ret = close(sock); |
2022 | if (ret) { | |
2023 | PERROR("close"); | |
2024 | } | |
4063050c | 2025 | lttng_fd_put(LTTNG_FD_APPS, 1); |
76d7553f | 2026 | sock = -1; |
5eb91c98 DG |
2027 | continue; |
2028 | } | |
840cb59c | 2029 | health_code_update(); |
099e26bd | 2030 | |
5eb91c98 | 2031 | ust_cmd->sock = sock; |
34a2494f | 2032 | sock = -1; |
099e26bd | 2033 | |
5eb91c98 DG |
2034 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
2035 | " gid:%d sock:%d name:%s (version %d.%d)", | |
2036 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
2037 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
2038 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
2039 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 2040 | |
5eb91c98 DG |
2041 | /* |
2042 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 2043 | * has been taken! This apps will be part of the *system*. |
5eb91c98 | 2044 | */ |
8bdee6e2 | 2045 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
5eb91c98 DG |
2046 | |
2047 | /* | |
2048 | * Wake the registration queue futex. Implicit memory | |
8bdee6e2 | 2049 | * barrier with the exchange in cds_wfcq_enqueue. |
5eb91c98 DG |
2050 | */ |
2051 | futex_nto1_wake(&ust_cmd_queue.futex); | |
03e43155 MD |
2052 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
2053 | ERR("Register apps socket poll error"); | |
2054 | goto error; | |
2055 | } else { | |
2056 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
2057 | goto error; | |
5eb91c98 DG |
2058 | } |
2059 | } | |
90014c57 | 2060 | } |
1d4b027a DG |
2061 | } |
2062 | ||
139ac872 | 2063 | exit: |
1d4b027a | 2064 | error: |
0fdd1e2c DG |
2065 | /* Notify that the registration thread is gone */ |
2066 | notify_ust_apps(0); | |
2067 | ||
a4b35e07 | 2068 | if (apps_sock >= 0) { |
76d7553f MD |
2069 | ret = close(apps_sock); |
2070 | if (ret) { | |
2071 | PERROR("close"); | |
2072 | } | |
a4b35e07 | 2073 | } |
46c3f085 | 2074 | if (sock >= 0) { |
76d7553f MD |
2075 | ret = close(sock); |
2076 | if (ret) { | |
2077 | PERROR("close"); | |
2078 | } | |
4063050c | 2079 | lttng_fd_put(LTTNG_FD_APPS, 1); |
a4b35e07 | 2080 | } |
e6142f2e | 2081 | unlink(config.apps_unix_sock_path.value); |
0fdd1e2c | 2082 | |
76d7553f | 2083 | error_poll_add: |
5eb91c98 | 2084 | lttng_poll_clean(&events); |
76d7553f MD |
2085 | error_listen: |
2086 | error_create_poll: | |
6993eeb3 | 2087 | error_testpoint: |
76d7553f | 2088 | DBG("UST Registration thread cleanup complete"); |
9ad42ec1 MD |
2089 | if (err) { |
2090 | health_error(); | |
2091 | ERR("Health error occurred in %s", __func__); | |
2092 | } | |
8782cc74 | 2093 | health_unregister(health_sessiond); |
5eb91c98 | 2094 | |
1d4b027a DG |
2095 | return NULL; |
2096 | } | |
2097 | ||
8c0faa1d | 2098 | /* |
3bd1e081 | 2099 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 2100 | * exec or it will fails. |
8c0faa1d | 2101 | */ |
3bd1e081 | 2102 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d | 2103 | { |
a23ec3a7 | 2104 | int ret, clock_ret; |
ee0b0061 DG |
2105 | struct timespec timeout; |
2106 | ||
13a7bce3 JG |
2107 | /* |
2108 | * Make sure we set the readiness flag to 0 because we are NOT ready. | |
2109 | * This access to consumer_thread_is_ready does not need to be | |
2110 | * protected by consumer_data.cond_mutex (yet) since the consumer | |
2111 | * management thread has not been started at this point. | |
2112 | */ | |
a23ec3a7 | 2113 | consumer_data->consumer_thread_is_ready = 0; |
8c0faa1d | 2114 | |
a23ec3a7 DG |
2115 | /* Setup pthread condition */ |
2116 | ret = pthread_condattr_init(&consumer_data->condattr); | |
4a15001e | 2117 | if (ret) { |
a23ec3a7 DG |
2118 | errno = ret; |
2119 | PERROR("pthread_condattr_init consumer data"); | |
2120 | goto error; | |
2121 | } | |
2122 | ||
2123 | /* | |
2124 | * Set the monotonic clock in order to make sure we DO NOT jump in time | |
2125 | * between the clock_gettime() call and the timedwait call. See bug #324 | |
2126 | * for a more details and how we noticed it. | |
2127 | */ | |
2128 | ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC); | |
4a15001e | 2129 | if (ret) { |
a23ec3a7 DG |
2130 | errno = ret; |
2131 | PERROR("pthread_condattr_setclock consumer data"); | |
ee0b0061 DG |
2132 | goto error; |
2133 | } | |
8c0faa1d | 2134 | |
a23ec3a7 | 2135 | ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr); |
4a15001e | 2136 | if (ret) { |
a23ec3a7 DG |
2137 | errno = ret; |
2138 | PERROR("pthread_cond_init consumer data"); | |
2139 | goto error; | |
2140 | } | |
2141 | ||
1a1a34b4 MJ |
2142 | ret = pthread_create(&consumer_data->thread, default_pthread_attr(), |
2143 | thread_manage_consumer, consumer_data); | |
4a15001e MD |
2144 | if (ret) { |
2145 | errno = ret; | |
3bd1e081 | 2146 | PERROR("pthread_create consumer"); |
ee0b0061 | 2147 | ret = -1; |
8c0faa1d DG |
2148 | goto error; |
2149 | } | |
2150 | ||
a23ec3a7 DG |
2151 | /* We are about to wait on a pthread condition */ |
2152 | pthread_mutex_lock(&consumer_data->cond_mutex); | |
2153 | ||
ee0b0061 | 2154 | /* Get time for sem_timedwait absolute timeout */ |
389fbf04 | 2155 | clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout); |
a23ec3a7 DG |
2156 | /* |
2157 | * Set the timeout for the condition timed wait even if the clock gettime | |
2158 | * call fails since we might loop on that call and we want to avoid to | |
2159 | * increment the timeout too many times. | |
2160 | */ | |
2161 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
2162 | ||
2163 | /* | |
2164 | * The following loop COULD be skipped in some conditions so this is why we | |
2165 | * set ret to 0 in order to make sure at least one round of the loop is | |
2166 | * done. | |
2167 | */ | |
2168 | ret = 0; | |
2169 | ||
2170 | /* | |
2171 | * Loop until the condition is reached or when a timeout is reached. Note | |
2172 | * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT | |
2173 | * be returned but the pthread_cond(3), from the glibc-doc, says that it is | |
2174 | * possible. This loop does not take any chances and works with both of | |
2175 | * them. | |
2176 | */ | |
2177 | while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) { | |
2178 | if (clock_ret < 0) { | |
2179 | PERROR("clock_gettime spawn consumer"); | |
2180 | /* Infinite wait for the consumerd thread to be ready */ | |
2181 | ret = pthread_cond_wait(&consumer_data->cond, | |
2182 | &consumer_data->cond_mutex); | |
2183 | } else { | |
2184 | ret = pthread_cond_timedwait(&consumer_data->cond, | |
2185 | &consumer_data->cond_mutex, &timeout); | |
2186 | } | |
ee0b0061 | 2187 | } |
8c0faa1d | 2188 | |
a23ec3a7 DG |
2189 | /* Release the pthread condition */ |
2190 | pthread_mutex_unlock(&consumer_data->cond_mutex); | |
2191 | ||
2192 | if (ret != 0) { | |
2193 | errno = ret; | |
2194 | if (ret == ETIMEDOUT) { | |
4282f9a3 DG |
2195 | int pth_ret; |
2196 | ||
ee0b0061 DG |
2197 | /* |
2198 | * Call has timed out so we kill the kconsumerd_thread and return | |
2199 | * an error. | |
2200 | */ | |
a23ec3a7 DG |
2201 | ERR("Condition timed out. The consumer thread was never ready." |
2202 | " Killing it"); | |
4282f9a3 DG |
2203 | pth_ret = pthread_cancel(consumer_data->thread); |
2204 | if (pth_ret < 0) { | |
3bd1e081 | 2205 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
2206 | } |
2207 | } else { | |
a23ec3a7 | 2208 | PERROR("pthread_cond_wait failed consumer thread"); |
ee0b0061 | 2209 | } |
4282f9a3 DG |
2210 | /* Caller is expecting a negative value on failure. */ |
2211 | ret = -1; | |
ee0b0061 DG |
2212 | goto error; |
2213 | } | |
2214 | ||
3bd1e081 MD |
2215 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2216 | if (consumer_data->pid == 0) { | |
a23ec3a7 | 2217 | ERR("Consumerd did not start"); |
3bd1e081 | 2218 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
2219 | goto error; |
2220 | } | |
3bd1e081 | 2221 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 2222 | |
8c0faa1d DG |
2223 | return 0; |
2224 | ||
2225 | error: | |
2226 | return ret; | |
2227 | } | |
2228 | ||
d9800920 | 2229 | /* |
3bd1e081 | 2230 | * Join consumer thread |
d9800920 | 2231 | */ |
3bd1e081 | 2232 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
2233 | { |
2234 | void *status; | |
cf3af59e | 2235 | |
e8209f6b DG |
2236 | /* Consumer pid must be a real one. */ |
2237 | if (consumer_data->pid > 0) { | |
c617c0c6 | 2238 | int ret; |
3bd1e081 | 2239 | ret = kill(consumer_data->pid, SIGTERM); |
cf3af59e | 2240 | if (ret) { |
4a15001e | 2241 | PERROR("Error killing consumer daemon"); |
cf3af59e MD |
2242 | return ret; |
2243 | } | |
3bd1e081 | 2244 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
2245 | } else { |
2246 | return 0; | |
2247 | } | |
2248 | } | |
2249 | ||
8c0faa1d | 2250 | /* |
3bd1e081 | 2251 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 2252 | * |
d063d709 | 2253 | * Return pid if successful else -1. |
8c0faa1d | 2254 | */ |
3bd1e081 | 2255 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
2256 | { |
2257 | int ret; | |
2258 | pid_t pid; | |
94c55f17 | 2259 | const char *consumer_to_use; |
53086306 | 2260 | const char *verbosity; |
94c55f17 | 2261 | struct stat st; |
8c0faa1d | 2262 | |
3bd1e081 | 2263 | DBG("Spawning consumerd"); |
c49dc785 | 2264 | |
8c0faa1d DG |
2265 | pid = fork(); |
2266 | if (pid == 0) { | |
2267 | /* | |
3bd1e081 | 2268 | * Exec consumerd. |
8c0faa1d | 2269 | */ |
e6142f2e | 2270 | if (config.verbose_consumer) { |
53086306 | 2271 | verbosity = "--verbose"; |
4421f712 | 2272 | } else if (lttng_opt_quiet) { |
53086306 | 2273 | verbosity = "--quiet"; |
4421f712 DG |
2274 | } else { |
2275 | verbosity = ""; | |
53086306 | 2276 | } |
4421f712 | 2277 | |
3bd1e081 MD |
2278 | switch (consumer_data->type) { |
2279 | case LTTNG_CONSUMER_KERNEL: | |
94c55f17 | 2280 | /* |
c7704d57 DG |
2281 | * Find out which consumerd to execute. We will first try the |
2282 | * 64-bit path, then the sessiond's installation directory, and | |
2283 | * fallback on the 32-bit one, | |
94c55f17 | 2284 | */ |
63a799e8 | 2285 | DBG3("Looking for a kernel consumer at these locations:"); |
59ee5091 | 2286 | DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL"); |
e6142f2e | 2287 | DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE); |
59ee5091 | 2288 | DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL"); |
e6142f2e | 2289 | if (stat(config.consumerd64_bin_path.value, &st) == 0) { |
63a799e8 | 2290 | DBG3("Found location #1"); |
e6142f2e JG |
2291 | consumer_to_use = config.consumerd64_bin_path.value; |
2292 | } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) { | |
63a799e8 | 2293 | DBG3("Found location #2"); |
e6142f2e | 2294 | consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE; |
a1a9f823 JG |
2295 | } else if (config.consumerd32_bin_path.value && |
2296 | stat(config.consumerd32_bin_path.value, &st) == 0) { | |
63a799e8 | 2297 | DBG3("Found location #3"); |
e6142f2e | 2298 | consumer_to_use = config.consumerd32_bin_path.value; |
94c55f17 | 2299 | } else { |
63a799e8 | 2300 | DBG("Could not find any valid consumerd executable"); |
4282f9a3 | 2301 | ret = -EINVAL; |
3d678709 | 2302 | goto error; |
94c55f17 AM |
2303 | } |
2304 | DBG("Using kernel consumer at: %s", consumer_to_use); | |
6e2cc8d8 | 2305 | (void) execl(consumer_to_use, |
94c55f17 AM |
2306 | "lttng-consumerd", verbosity, "-k", |
2307 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, | |
2308 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2309 | "--group", config.tracing_group_name.value, |
94c55f17 | 2310 | NULL); |
3bd1e081 | 2311 | break; |
7753dea8 MD |
2312 | case LTTNG_CONSUMER64_UST: |
2313 | { | |
46b23495 | 2314 | if (config.consumerd64_lib_dir.value) { |
8f4905da MD |
2315 | char *tmp; |
2316 | size_t tmplen; | |
ddaec4a2 | 2317 | char *tmpnew; |
8f4905da | 2318 | |
e8fa9fb0 | 2319 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2320 | if (!tmp) { |
2321 | tmp = ""; | |
2322 | } | |
d222983e | 2323 | tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp); |
8f4905da MD |
2324 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
2325 | if (!tmpnew) { | |
2326 | ret = -ENOMEM; | |
2327 | goto error; | |
2328 | } | |
e6142f2e | 2329 | strcat(tmpnew, config.consumerd64_lib_dir.value); |
8f4905da MD |
2330 | if (tmp[0] != '\0') { |
2331 | strcat(tmpnew, ":"); | |
2332 | strcat(tmpnew, tmp); | |
2333 | } | |
d222983e | 2334 | ret = setenv("LD_LIBRARY_PATH", tmpnew, 1); |
ddaec4a2 | 2335 | free(tmpnew); |
8f4905da MD |
2336 | if (ret) { |
2337 | ret = -errno; | |
2338 | goto error; | |
2339 | } | |
2340 | } | |
e6142f2e JG |
2341 | DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value); |
2342 | (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u", | |
7753dea8 MD |
2343 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2344 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2345 | "--group", config.tracing_group_name.value, |
7753dea8 | 2346 | NULL); |
3bd1e081 | 2347 | break; |
7753dea8 MD |
2348 | } |
2349 | case LTTNG_CONSUMER32_UST: | |
2350 | { | |
46b23495 | 2351 | if (config.consumerd32_lib_dir.value) { |
8f4905da MD |
2352 | char *tmp; |
2353 | size_t tmplen; | |
ddaec4a2 | 2354 | char *tmpnew; |
8f4905da | 2355 | |
e8fa9fb0 | 2356 | tmp = lttng_secure_getenv("LD_LIBRARY_PATH"); |
8f4905da MD |
2357 | if (!tmp) { |
2358 | tmp = ""; | |
2359 | } | |
d222983e | 2360 | tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp); |
8f4905da MD |
2361 | tmpnew = zmalloc(tmplen + 1 /* \0 */); |
2362 | if (!tmpnew) { | |
2363 | ret = -ENOMEM; | |
2364 | goto error; | |
2365 | } | |
e6142f2e | 2366 | strcat(tmpnew, config.consumerd32_lib_dir.value); |
8f4905da MD |
2367 | if (tmp[0] != '\0') { |
2368 | strcat(tmpnew, ":"); | |
2369 | strcat(tmpnew, tmp); | |
2370 | } | |
d222983e | 2371 | ret = setenv("LD_LIBRARY_PATH", tmpnew, 1); |
ddaec4a2 | 2372 | free(tmpnew); |
8f4905da MD |
2373 | if (ret) { |
2374 | ret = -errno; | |
2375 | goto error; | |
2376 | } | |
2377 | } | |
e6142f2e JG |
2378 | DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value); |
2379 | (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u", | |
7753dea8 MD |
2380 | "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path, |
2381 | "--consumerd-err-sock", consumer_data->err_unix_sock_path, | |
e6142f2e | 2382 | "--group", config.tracing_group_name.value, |
7753dea8 MD |
2383 | NULL); |
2384 | break; | |
2385 | } | |
3bd1e081 | 2386 | default: |
b60e7183 | 2387 | ERR("unknown consumer type"); |
e9d6b496 | 2388 | errno = 0; |
3bd1e081 | 2389 | } |
8c0faa1d | 2390 | if (errno != 0) { |
4282f9a3 | 2391 | PERROR("Consumer execl()"); |
8c0faa1d | 2392 | } |
4282f9a3 | 2393 | /* Reaching this point, we got a failure on our execl(). */ |
8c0faa1d DG |
2394 | exit(EXIT_FAILURE); |
2395 | } else if (pid > 0) { | |
2396 | ret = pid; | |
8c0faa1d | 2397 | } else { |
76d7553f | 2398 | PERROR("start consumer fork"); |
8c0faa1d | 2399 | ret = -errno; |
8c0faa1d | 2400 | } |
8f4905da | 2401 | error: |
8c0faa1d DG |
2402 | return ret; |
2403 | } | |
2404 | ||
693bd40b | 2405 | /* |
3bd1e081 | 2406 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 2407 | */ |
3bd1e081 | 2408 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b | 2409 | { |
c617c0c6 | 2410 | int ret; |
edb8b045 DG |
2411 | |
2412 | /* | |
2413 | * Set the listen() state on the socket since there is a possible race | |
2414 | * between the exec() of the consumer daemon and this call if place in the | |
2415 | * consumer thread. See bug #366 for more details. | |
2416 | */ | |
2417 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); | |
2418 | if (ret < 0) { | |
2419 | goto error; | |
2420 | } | |
693bd40b | 2421 | |
3bd1e081 MD |
2422 | pthread_mutex_lock(&consumer_data->pid_mutex); |
2423 | if (consumer_data->pid != 0) { | |
2424 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
2425 | goto end; |
2426 | } | |
693bd40b | 2427 | |
3bd1e081 | 2428 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 2429 | if (ret < 0) { |
3bd1e081 MD |
2430 | ERR("Spawning consumerd failed"); |
2431 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 2432 | goto error; |
693bd40b | 2433 | } |
c49dc785 | 2434 | |
3bd1e081 MD |
2435 | /* Setting up the consumer_data pid */ |
2436 | consumer_data->pid = ret; | |
48842b30 | 2437 | DBG2("Consumer pid %d", consumer_data->pid); |
3bd1e081 | 2438 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
693bd40b | 2439 | |
3bd1e081 MD |
2440 | DBG2("Spawning consumer control thread"); |
2441 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 2442 | if (ret < 0) { |
3bd1e081 | 2443 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
2444 | goto error; |
2445 | } | |
2446 | ||
c49dc785 | 2447 | end: |
693bd40b DG |
2448 | return 0; |
2449 | ||
2450 | error: | |
331744e3 | 2451 | /* Cleanup already created sockets on error. */ |
edb8b045 | 2452 | if (consumer_data->err_sock >= 0) { |
c617c0c6 MD |
2453 | int err; |
2454 | ||
edb8b045 DG |
2455 | err = close(consumer_data->err_sock); |
2456 | if (err < 0) { | |
2457 | PERROR("close consumer data error socket"); | |
2458 | } | |
2459 | } | |
693bd40b DG |
2460 | return ret; |
2461 | } | |
2462 | ||
b73401da | 2463 | /* |
096102bd | 2464 | * Setup necessary data for kernel tracer action. |
b73401da | 2465 | */ |
096102bd | 2466 | static int init_kernel_tracer(void) |
b73401da DG |
2467 | { |
2468 | int ret; | |
b73401da | 2469 | |
096102bd DG |
2470 | /* Modprobe lttng kernel modules */ |
2471 | ret = modprobe_lttng_control(); | |
b73401da | 2472 | if (ret < 0) { |
b73401da DG |
2473 | goto error; |
2474 | } | |
2475 | ||
096102bd DG |
2476 | /* Open debugfs lttng */ |
2477 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
2478 | if (kernel_tracer_fd < 0) { | |
2479 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 2480 | goto error_open; |
54d01ffb DG |
2481 | } |
2482 | ||
2f77fc4b | 2483 | /* Validate kernel version */ |
88076e89 JD |
2484 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
2485 | &kernel_tracer_abi_version); | |
2f77fc4b DG |
2486 | if (ret < 0) { |
2487 | goto error_version; | |
b551a063 | 2488 | } |
54d01ffb | 2489 | |
2f77fc4b DG |
2490 | ret = modprobe_lttng_data(); |
2491 | if (ret < 0) { | |
2492 | goto error_modules; | |
54d01ffb DG |
2493 | } |
2494 | ||
6e21424e JR |
2495 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
2496 | kernel_tracer_fd); | |
2497 | if (ret < 0) { | |
2498 | goto error_modules; | |
2499 | } | |
2500 | ||
2501 | if (ret < 1) { | |
2502 | WARN("Kernel tracer does not support buffer monitoring. " | |
2503 | "The monitoring timer of channels in the kernel domain " | |
2504 | "will be set to 0 (disabled)."); | |
2505 | } | |
2506 | ||
2f77fc4b DG |
2507 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
2508 | return 0; | |
2509 | ||
2510 | error_version: | |
2511 | modprobe_remove_lttng_control(); | |
2512 | ret = close(kernel_tracer_fd); | |
2513 | if (ret) { | |
2514 | PERROR("close"); | |
b551a063 | 2515 | } |
2f77fc4b | 2516 | kernel_tracer_fd = -1; |
f73fabfd | 2517 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 2518 | |
2f77fc4b DG |
2519 | error_modules: |
2520 | ret = close(kernel_tracer_fd); | |
2521 | if (ret) { | |
2522 | PERROR("close"); | |
b551a063 | 2523 | } |
54d01ffb | 2524 | |
2f77fc4b DG |
2525 | error_open: |
2526 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
2527 | |
2528 | error: | |
2f77fc4b DG |
2529 | WARN("No kernel tracer available"); |
2530 | kernel_tracer_fd = -1; | |
2531 | if (!is_root) { | |
f73fabfd | 2532 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 2533 | } else { |
f73fabfd | 2534 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 2535 | } |
54d01ffb DG |
2536 | } |
2537 | ||
2f77fc4b | 2538 | |
54d01ffb | 2539 | /* |
2f77fc4b DG |
2540 | * Copy consumer output from the tracing session to the domain session. The |
2541 | * function also applies the right modification on a per domain basis for the | |
2542 | * trace files destination directory. | |
36b588ed MD |
2543 | * |
2544 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2545 | */ |
2f77fc4b | 2546 | static int copy_session_consumer(int domain, struct ltt_session *session) |
54d01ffb DG |
2547 | { |
2548 | int ret; | |
2f77fc4b DG |
2549 | const char *dir_name; |
2550 | struct consumer_output *consumer; | |
2551 | ||
2552 | assert(session); | |
2553 | assert(session->consumer); | |
54d01ffb | 2554 | |
b551a063 DG |
2555 | switch (domain) { |
2556 | case LTTNG_DOMAIN_KERNEL: | |
2f77fc4b | 2557 | DBG3("Copying tracing session consumer output in kernel session"); |
09a90bcd DG |
2558 | /* |
2559 | * XXX: We should audit the session creation and what this function | |
2560 | * does "extra" in order to avoid a destroy since this function is used | |
2561 | * in the domain session creation (kernel and ust) only. Same for UST | |
2562 | * domain. | |
2563 | */ | |
2564 | if (session->kernel_session->consumer) { | |
6addfa37 | 2565 | consumer_output_put(session->kernel_session->consumer); |
09a90bcd | 2566 | } |
2f77fc4b DG |
2567 | session->kernel_session->consumer = |
2568 | consumer_copy_output(session->consumer); | |
2569 | /* Ease our life a bit for the next part */ | |
2570 | consumer = session->kernel_session->consumer; | |
2571 | dir_name = DEFAULT_KERNEL_TRACE_DIR; | |
b551a063 | 2572 | break; |
f20baf8e | 2573 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2574 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2575 | case LTTNG_DOMAIN_PYTHON: |
b551a063 | 2576 | case LTTNG_DOMAIN_UST: |
2f77fc4b | 2577 | DBG3("Copying tracing session consumer output in UST session"); |
09a90bcd | 2578 | if (session->ust_session->consumer) { |
6addfa37 | 2579 | consumer_output_put(session->ust_session->consumer); |
09a90bcd | 2580 | } |
2f77fc4b DG |
2581 | session->ust_session->consumer = |
2582 | consumer_copy_output(session->consumer); | |
2583 | /* Ease our life a bit for the next part */ | |
2584 | consumer = session->ust_session->consumer; | |
2585 | dir_name = DEFAULT_UST_TRACE_DIR; | |
b551a063 DG |
2586 | break; |
2587 | default: | |
f73fabfd | 2588 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
54d01ffb DG |
2589 | goto error; |
2590 | } | |
2591 | ||
2f77fc4b | 2592 | /* Append correct directory to subdir */ |
c30ce0b3 CB |
2593 | strncat(consumer->subdir, dir_name, |
2594 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
2595 | DBG3("Copy session consumer subdir %s", consumer->subdir); |
2596 | ||
f73fabfd | 2597 | ret = LTTNG_OK; |
54d01ffb DG |
2598 | |
2599 | error: | |
2600 | return ret; | |
2601 | } | |
2602 | ||
00e2e675 | 2603 | /* |
2f77fc4b | 2604 | * Create an UST session and add it to the session ust list. |
36b588ed MD |
2605 | * |
2606 | * Should *NOT* be called with RCU read-side lock held. | |
00e2e675 | 2607 | */ |
2f77fc4b DG |
2608 | static int create_ust_session(struct ltt_session *session, |
2609 | struct lttng_domain *domain) | |
00e2e675 DG |
2610 | { |
2611 | int ret; | |
2f77fc4b | 2612 | struct ltt_ust_session *lus = NULL; |
00e2e675 | 2613 | |
a4b92340 | 2614 | assert(session); |
2f77fc4b DG |
2615 | assert(domain); |
2616 | assert(session->consumer); | |
a4b92340 | 2617 | |
2f77fc4b | 2618 | switch (domain->type) { |
f20baf8e | 2619 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 2620 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 2621 | case LTTNG_DOMAIN_PYTHON: |
2f77fc4b DG |
2622 | case LTTNG_DOMAIN_UST: |
2623 | break; | |
2624 | default: | |
2625 | ERR("Unknown UST domain on create session %d", domain->type); | |
f73fabfd | 2626 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
00e2e675 DG |
2627 | goto error; |
2628 | } | |
2629 | ||
2f77fc4b DG |
2630 | DBG("Creating UST session"); |
2631 | ||
dec56f6c | 2632 | lus = trace_ust_create_session(session->id); |
2f77fc4b | 2633 | if (lus == NULL) { |
f73fabfd | 2634 | ret = LTTNG_ERR_UST_SESS_FAIL; |
a4b92340 DG |
2635 | goto error; |
2636 | } | |
2637 | ||
2f77fc4b DG |
2638 | lus->uid = session->uid; |
2639 | lus->gid = session->gid; | |
2bba9e53 | 2640 | lus->output_traces = session->output_traces; |
27babd3a | 2641 | lus->snapshot_mode = session->snapshot_mode; |
ecc48a90 | 2642 | lus->live_timer_interval = session->live_timer; |
2f77fc4b | 2643 | session->ust_session = lus; |
d7ba1388 | 2644 | if (session->shm_path[0]) { |
3d071855 MD |
2645 | strncpy(lus->root_shm_path, session->shm_path, |
2646 | sizeof(lus->root_shm_path)); | |
2647 | lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2648 | strncpy(lus->shm_path, session->shm_path, |
2649 | sizeof(lus->shm_path)); | |
2650 | lus->shm_path[sizeof(lus->shm_path) - 1] = '\0'; | |
2651 | strncat(lus->shm_path, "/ust", | |
2652 | sizeof(lus->shm_path) - strlen(lus->shm_path) - 1); | |
2653 | } | |
2f77fc4b DG |
2654 | /* Copy session output to the newly created UST session */ |
2655 | ret = copy_session_consumer(domain->type, session); | |
f73fabfd | 2656 | if (ret != LTTNG_OK) { |
00e2e675 DG |
2657 | goto error; |
2658 | } | |
2659 | ||
f73fabfd | 2660 | return LTTNG_OK; |
00e2e675 DG |
2661 | |
2662 | error: | |
2f77fc4b DG |
2663 | free(lus); |
2664 | session->ust_session = NULL; | |
00e2e675 DG |
2665 | return ret; |
2666 | } | |
2667 | ||
2668 | /* | |
2f77fc4b | 2669 | * Create a kernel tracer session then create the default channel. |
00e2e675 | 2670 | */ |
2f77fc4b | 2671 | static int create_kernel_session(struct ltt_session *session) |
00e2e675 DG |
2672 | { |
2673 | int ret; | |
a4b92340 | 2674 | |
2f77fc4b | 2675 | DBG("Creating kernel session"); |
00e2e675 | 2676 | |
2f77fc4b DG |
2677 | ret = kernel_create_session(session, kernel_tracer_fd); |
2678 | if (ret < 0) { | |
f73fabfd | 2679 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
00e2e675 DG |
2680 | goto error; |
2681 | } | |
2682 | ||
2f77fc4b DG |
2683 | /* Code flow safety */ |
2684 | assert(session->kernel_session); | |
2685 | ||
2686 | /* Copy session output to the newly created Kernel session */ | |
2687 | ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session); | |
f73fabfd | 2688 | if (ret != LTTNG_OK) { |
a4b92340 DG |
2689 | goto error; |
2690 | } | |
2691 | ||
2f77fc4b DG |
2692 | session->kernel_session->uid = session->uid; |
2693 | session->kernel_session->gid = session->gid; | |
2bba9e53 | 2694 | session->kernel_session->output_traces = session->output_traces; |
27babd3a | 2695 | session->kernel_session->snapshot_mode = session->snapshot_mode; |
00e2e675 | 2696 | |
f73fabfd | 2697 | return LTTNG_OK; |
00e2e675 | 2698 | |
2f77fc4b DG |
2699 | error: |
2700 | trace_kernel_destroy_session(session->kernel_session); | |
2701 | session->kernel_session = NULL; | |
2702 | return ret; | |
2703 | } | |
00e2e675 | 2704 | |
2f77fc4b DG |
2705 | /* |
2706 | * Count number of session permitted by uid/gid. | |
2707 | */ | |
2708 | static unsigned int lttng_sessions_count(uid_t uid, gid_t gid) | |
2709 | { | |
2710 | unsigned int i = 0; | |
2711 | struct ltt_session *session; | |
e32d7f27 | 2712 | const struct ltt_session_list *session_list = session_get_list(); |
07424f16 | 2713 | |
2f77fc4b DG |
2714 | DBG("Counting number of available session for UID %d GID %d", |
2715 | uid, gid); | |
e32d7f27 JG |
2716 | cds_list_for_each_entry(session, &session_list->head, list) { |
2717 | if (!session_get(session)) { | |
2f77fc4b DG |
2718 | continue; |
2719 | } | |
e32d7f27 JG |
2720 | session_lock(session); |
2721 | /* Only count the sessions the user can control. */ | |
2722 | if (session_access_ok(session, uid, gid) && | |
2723 | !session->destroyed) { | |
2724 | i++; | |
2725 | } | |
2726 | session_unlock(session); | |
2727 | session_put(session); | |
a4b92340 | 2728 | } |
2f77fc4b | 2729 | return i; |
00e2e675 DG |
2730 | } |
2731 | ||
dcabc190 FD |
2732 | static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock, |
2733 | int *sock_error, struct lttng_event *event) | |
2734 | { | |
2735 | int fd, ret; | |
2736 | struct lttng_userspace_probe_location *probe_location; | |
87597c2c | 2737 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
dcabc190 FD |
2738 | struct lttng_dynamic_buffer probe_location_buffer; |
2739 | struct lttng_buffer_view buffer_view; | |
2740 | ||
2741 | /* | |
2742 | * Create a buffer to store the serialized version of the probe | |
2743 | * location. | |
2744 | */ | |
2745 | lttng_dynamic_buffer_init(&probe_location_buffer); | |
2746 | ret = lttng_dynamic_buffer_set_size(&probe_location_buffer, | |
2747 | cmd_ctx->lsm->u.enable.userspace_probe_location_len); | |
2748 | if (ret) { | |
2749 | ret = LTTNG_ERR_NOMEM; | |
2750 | goto error; | |
2751 | } | |
2752 | ||
2753 | /* | |
2754 | * Receive the probe location. | |
2755 | */ | |
2756 | ret = lttcomm_recv_unix_sock(sock, probe_location_buffer.data, | |
2757 | probe_location_buffer.size); | |
2758 | if (ret <= 0) { | |
2759 | DBG("Nothing recv() from client var len data... continuing"); | |
2760 | *sock_error = 1; | |
2761 | lttng_dynamic_buffer_reset(&probe_location_buffer); | |
2762 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2763 | goto error; | |
2764 | } | |
2765 | ||
2766 | buffer_view = lttng_buffer_view_from_dynamic_buffer( | |
2767 | &probe_location_buffer, 0, probe_location_buffer.size); | |
2768 | ||
2769 | /* | |
2770 | * Extract the probe location from the serialized version. | |
2771 | */ | |
2772 | ret = lttng_userspace_probe_location_create_from_buffer( | |
2773 | &buffer_view, &probe_location); | |
2774 | if (ret < 0) { | |
2775 | WARN("Failed to create a userspace probe location from the received buffer"); | |
2776 | lttng_dynamic_buffer_reset( &probe_location_buffer); | |
2777 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2778 | goto error; | |
2779 | } | |
2780 | ||
2781 | /* | |
2782 | * Receive the file descriptor to the target binary from the client. | |
2783 | */ | |
2784 | DBG("Receiving userspace probe target FD from client ..."); | |
2785 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); | |
2786 | if (ret <= 0) { | |
2787 | DBG("Nothing recv() from client userspace probe fd... continuing"); | |
2788 | *sock_error = 1; | |
2789 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2790 | goto error; | |
2791 | } | |
2792 | ||
2793 | /* | |
2794 | * Set the file descriptor received from the client through the unix | |
2795 | * socket in the probe location. | |
2796 | */ | |
2797 | lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); | |
2798 | if (!lookup) { | |
2799 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2800 | goto error; | |
2801 | } | |
2802 | ||
2803 | /* | |
2804 | * From the kernel tracer's perspective, all userspace probe event types | |
2805 | * are all the same: a file and an offset. | |
2806 | */ | |
2807 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { | |
2808 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: | |
2809 | ret = lttng_userspace_probe_location_function_set_binary_fd( | |
2810 | probe_location, fd); | |
2811 | break; | |
2812 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: | |
2813 | ret = lttng_userspace_probe_location_tracepoint_set_binary_fd( | |
2814 | probe_location, fd); | |
2815 | break; | |
2816 | default: | |
2817 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2818 | goto error; | |
2819 | } | |
2820 | ||
2821 | if (ret) { | |
2822 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2823 | goto error; | |
2824 | } | |
2825 | ||
2826 | /* Attach the probe location to the event. */ | |
2827 | ret = lttng_event_set_userspace_probe_location(event, probe_location); | |
2828 | if (ret) { | |
2829 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2830 | goto error; | |
2831 | } | |
2832 | ||
2833 | lttng_dynamic_buffer_reset(&probe_location_buffer); | |
2834 | error: | |
2835 | return ret; | |
2836 | } | |
2837 | ||
5c408ad8 JD |
2838 | /* |
2839 | * Check if the current kernel tracer supports the session rotation feature. | |
2840 | * Return 1 if it does, 0 otherwise. | |
2841 | */ | |
2842 | static int check_rotate_compatible(void) | |
2843 | { | |
2844 | int ret = 1; | |
2845 | ||
2846 | if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) { | |
2847 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
2848 | ret = 0; | |
2849 | } | |
2850 | ||
2851 | return ret; | |
2852 | } | |
2853 | ||
54d01ffb DG |
2854 | /* |
2855 | * Process the command requested by the lttng client within the command | |
2856 | * context structure. This function make sure that the return structure (llm) | |
2857 | * is set and ready for transmission before returning. | |
2858 | * | |
2859 | * Return any error encountered or 0 for success. | |
53a80697 MD |
2860 | * |
2861 | * "sock" is only used for special-case var. len data. | |
36b588ed MD |
2862 | * |
2863 | * Should *NOT* be called with RCU read-side lock held. | |
54d01ffb | 2864 | */ |
53a80697 MD |
2865 | static int process_client_msg(struct command_ctx *cmd_ctx, int sock, |
2866 | int *sock_error) | |
54d01ffb | 2867 | { |
f73fabfd | 2868 | int ret = LTTNG_OK; |
44d3bd01 | 2869 | int need_tracing_session = 1; |
2e09ba09 | 2870 | int need_domain; |
54d01ffb DG |
2871 | |
2872 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2873 | ||
3745d315 MD |
2874 | assert(!rcu_read_ongoing()); |
2875 | ||
53a80697 MD |
2876 | *sock_error = 0; |
2877 | ||
2e09ba09 MD |
2878 | switch (cmd_ctx->lsm->cmd_type) { |
2879 | case LTTNG_CREATE_SESSION: | |
27babd3a | 2880 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2881 | case LTTNG_CREATE_SESSION_LIVE: |
2e09ba09 MD |
2882 | case LTTNG_DESTROY_SESSION: |
2883 | case LTTNG_LIST_SESSIONS: | |
2884 | case LTTNG_LIST_DOMAINS: | |
2885 | case LTTNG_START_TRACE: | |
2886 | case LTTNG_STOP_TRACE: | |
6d805429 | 2887 | case LTTNG_DATA_PENDING: |
da3c9ec1 DG |
2888 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
2889 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
2890 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
2891 | case LTTNG_SNAPSHOT_RECORD: | |
fb198a11 | 2892 | case LTTNG_SAVE_SESSION: |
d7ba1388 | 2893 | case LTTNG_SET_SESSION_SHM_PATH: |
eded6438 | 2894 | case LTTNG_REGENERATE_METADATA: |
c2561365 | 2895 | case LTTNG_REGENERATE_STATEDUMP: |
b3530820 JG |
2896 | case LTTNG_REGISTER_TRIGGER: |
2897 | case LTTNG_UNREGISTER_TRIGGER: | |
5c408ad8 | 2898 | case LTTNG_ROTATE_SESSION: |
d68c9a04 | 2899 | case LTTNG_ROTATION_GET_INFO: |
259c2674 | 2900 | case LTTNG_ROTATION_SET_SCHEDULE: |
66ea93b1 | 2901 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
2e09ba09 | 2902 | need_domain = 0; |
3aace903 | 2903 | break; |
2e09ba09 MD |
2904 | default: |
2905 | need_domain = 1; | |
2906 | } | |
2907 | ||
e6142f2e | 2908 | if (config.no_kernel && need_domain |
2e09ba09 | 2909 | && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) { |
531d29f9 | 2910 | if (!is_root) { |
f73fabfd | 2911 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
531d29f9 | 2912 | } else { |
f73fabfd | 2913 | ret = LTTNG_ERR_KERN_NA; |
531d29f9 | 2914 | } |
4fba7219 DG |
2915 | goto error; |
2916 | } | |
2917 | ||
8d3113b2 DG |
2918 | /* Deny register consumer if we already have a spawned consumer. */ |
2919 | if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) { | |
2920 | pthread_mutex_lock(&kconsumer_data.pid_mutex); | |
2921 | if (kconsumer_data.pid > 0) { | |
f73fabfd | 2922 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
fa317f24 | 2923 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
8d3113b2 DG |
2924 | goto error; |
2925 | } | |
2926 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
2927 | } | |
2928 | ||
54d01ffb DG |
2929 | /* |
2930 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2931 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2932 | * command. |
2933 | */ | |
2934 | switch(cmd_ctx->lsm->cmd_type) { | |
2935 | case LTTNG_LIST_SESSIONS: | |
2936 | case LTTNG_LIST_TRACEPOINTS: | |
f37d259d | 2937 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
54d01ffb DG |
2938 | case LTTNG_LIST_DOMAINS: |
2939 | case LTTNG_LIST_CHANNELS: | |
2940 | case LTTNG_LIST_EVENTS: | |
834978fd | 2941 | case LTTNG_LIST_SYSCALLS: |
a5dfbb9d | 2942 | case LTTNG_LIST_TRACKER_PIDS: |
5cd0780d | 2943 | case LTTNG_DATA_PENDING: |
5c408ad8 | 2944 | case LTTNG_ROTATE_SESSION: |
d68c9a04 | 2945 | case LTTNG_ROTATION_GET_INFO: |
66ea93b1 | 2946 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
54d01ffb DG |
2947 | break; |
2948 | default: | |
2949 | /* Setup lttng message with no payload */ | |
6e10c9b9 | 2950 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0); |
54d01ffb DG |
2951 | if (ret < 0) { |
2952 | /* This label does not try to unlock the session */ | |
2953 | goto init_setup_error; | |
2954 | } | |
2955 | } | |
2956 | ||
2957 | /* Commands that DO NOT need a session. */ | |
2958 | switch (cmd_ctx->lsm->cmd_type) { | |
54d01ffb | 2959 | case LTTNG_CREATE_SESSION: |
27babd3a | 2960 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
ecc48a90 | 2961 | case LTTNG_CREATE_SESSION_LIVE: |
54d01ffb DG |
2962 | case LTTNG_LIST_SESSIONS: |
2963 | case LTTNG_LIST_TRACEPOINTS: | |
834978fd | 2964 | case LTTNG_LIST_SYSCALLS: |
f37d259d | 2965 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
fb198a11 | 2966 | case LTTNG_SAVE_SESSION: |
b3530820 JG |
2967 | case LTTNG_REGISTER_TRIGGER: |
2968 | case LTTNG_UNREGISTER_TRIGGER: | |
44d3bd01 | 2969 | need_tracing_session = 0; |
54d01ffb DG |
2970 | break; |
2971 | default: | |
2972 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
256a5576 MD |
2973 | /* |
2974 | * We keep the session list lock across _all_ commands | |
2975 | * for now, because the per-session lock does not | |
2976 | * handle teardown properly. | |
2977 | */ | |
74babd95 | 2978 | session_lock_list(); |
54d01ffb DG |
2979 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
2980 | if (cmd_ctx->session == NULL) { | |
bba2d65f | 2981 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
54d01ffb DG |
2982 | goto error; |
2983 | } else { | |
2984 | /* Acquire lock for the session */ | |
2985 | session_lock(cmd_ctx->session); | |
2986 | } | |
2987 | break; | |
2988 | } | |
b389abbe | 2989 | |
5f3ecf22 DG |
2990 | /* |
2991 | * Commands that need a valid session but should NOT create one if none | |
2992 | * exists. Instead of creating one and destroying it when the command is | |
2993 | * handled, process that right before so we save some round trip in useless | |
2994 | * code path. | |
2995 | */ | |
2996 | switch (cmd_ctx->lsm->cmd_type) { | |
2997 | case LTTNG_DISABLE_CHANNEL: | |
2998 | case LTTNG_DISABLE_EVENT: | |
5f3ecf22 DG |
2999 | switch (cmd_ctx->lsm->domain.type) { |
3000 | case LTTNG_DOMAIN_KERNEL: | |
3001 | if (!cmd_ctx->session->kernel_session) { | |
3002 | ret = LTTNG_ERR_NO_CHANNEL; | |
3003 | goto error; | |
3004 | } | |
3005 | break; | |
3006 | case LTTNG_DOMAIN_JUL: | |
5cdb6027 | 3007 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3008 | case LTTNG_DOMAIN_PYTHON: |
5f3ecf22 DG |
3009 | case LTTNG_DOMAIN_UST: |
3010 | if (!cmd_ctx->session->ust_session) { | |
3011 | ret = LTTNG_ERR_NO_CHANNEL; | |
3012 | goto error; | |
3013 | } | |
3014 | break; | |
3015 | default: | |
3016 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3017 | goto error; | |
3018 | } | |
3019 | default: | |
3020 | break; | |
3021 | } | |
3022 | ||
2e09ba09 MD |
3023 | if (!need_domain) { |
3024 | goto skip_domain; | |
3025 | } | |
a4b92340 | 3026 | |
54d01ffb DG |
3027 | /* |
3028 | * Check domain type for specific "pre-action". | |
3029 | */ | |
3030 | switch (cmd_ctx->lsm->domain.type) { | |
3031 | case LTTNG_DOMAIN_KERNEL: | |
d1f1c568 | 3032 | if (!is_root) { |
f73fabfd | 3033 | ret = LTTNG_ERR_NEED_ROOT_SESSIOND; |
d1f1c568 DG |
3034 | goto error; |
3035 | } | |
3036 | ||
54d01ffb | 3037 | /* Kernel tracer check */ |
a4b35e07 | 3038 | if (kernel_tracer_fd == -1) { |
54d01ffb | 3039 | /* Basically, load kernel tracer modules */ |
096102bd DG |
3040 | ret = init_kernel_tracer(); |
3041 | if (ret != 0) { | |
54d01ffb DG |
3042 | goto error; |
3043 | } | |
3044 | } | |
5eb91c98 | 3045 | |
5c827ce0 DG |
3046 | /* Consumer is in an ERROR state. Report back to client */ |
3047 | if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3048 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3049 | goto error; |
3050 | } | |
3051 | ||
54d01ffb | 3052 | /* Need a session for kernel command */ |
44d3bd01 | 3053 | if (need_tracing_session) { |
54d01ffb | 3054 | if (cmd_ctx->session->kernel_session == NULL) { |
6df2e2c9 | 3055 | ret = create_kernel_session(cmd_ctx->session); |
5eb91c98 | 3056 | if (ret < 0) { |
f73fabfd | 3057 | ret = LTTNG_ERR_KERN_SESS_FAIL; |
5eb91c98 DG |
3058 | goto error; |
3059 | } | |
b389abbe | 3060 | } |
7d29a247 | 3061 | |
54d01ffb | 3062 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
3063 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
3064 | if (kconsumer_data.pid == 0 && | |
785d2d0d | 3065 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
3066 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
3067 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 3068 | if (ret < 0) { |
f73fabfd | 3069 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
54d01ffb | 3070 | goto error; |
950131af | 3071 | } |
5c827ce0 | 3072 | uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED); |
3ff2ecac MD |
3073 | } else { |
3074 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); | |
33a2b854 | 3075 | } |
173af62f | 3076 | |
a4b92340 DG |
3077 | /* |
3078 | * The consumer was just spawned so we need to add the socket to | |
3079 | * the consumer output of the session if exist. | |
3080 | */ | |
3081 | ret = consumer_create_socket(&kconsumer_data, | |
3082 | cmd_ctx->session->kernel_session->consumer); | |
3083 | if (ret < 0) { | |
3084 | goto error; | |
173af62f | 3085 | } |
0d0c377a | 3086 | } |
5c827ce0 | 3087 | |
54d01ffb | 3088 | break; |
b9dfb167 | 3089 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3090 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3091 | case LTTNG_DOMAIN_PYTHON: |
2bdd86d4 | 3092 | case LTTNG_DOMAIN_UST: |
44d3bd01 | 3093 | { |
b51ec5b4 MD |
3094 | if (!ust_app_supported()) { |
3095 | ret = LTTNG_ERR_NO_UST; | |
3096 | goto error; | |
3097 | } | |
5c827ce0 DG |
3098 | /* Consumer is in an ERROR state. Report back to client */ |
3099 | if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) { | |
f73fabfd | 3100 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3101 | goto error; |
3102 | } | |
3103 | ||
44d3bd01 | 3104 | if (need_tracing_session) { |
a4b92340 | 3105 | /* Create UST session if none exist. */ |
f6a9efaa | 3106 | if (cmd_ctx->session->ust_session == NULL) { |
44d3bd01 | 3107 | ret = create_ust_session(cmd_ctx->session, |
6df2e2c9 | 3108 | &cmd_ctx->lsm->domain); |
f73fabfd | 3109 | if (ret != LTTNG_OK) { |
44d3bd01 DG |
3110 | goto error; |
3111 | } | |
3112 | } | |
00e2e675 | 3113 | |
7753dea8 MD |
3114 | /* Start the UST consumer daemons */ |
3115 | /* 64-bit */ | |
3116 | pthread_mutex_lock(&ustconsumer64_data.pid_mutex); | |
e6142f2e | 3117 | if (config.consumerd64_bin_path.value && |
7753dea8 | 3118 | ustconsumer64_data.pid == 0 && |
785d2d0d | 3119 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3120 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3121 | ret = start_consumerd(&ustconsumer64_data); | |
2bdd86d4 | 3122 | if (ret < 0) { |
f73fabfd | 3123 | ret = LTTNG_ERR_UST_CONSUMER64_FAIL; |
173af62f | 3124 | uatomic_set(&ust_consumerd64_fd, -EINVAL); |
2bdd86d4 MD |
3125 | goto error; |
3126 | } | |
48842b30 | 3127 | |
173af62f | 3128 | uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock); |
5c827ce0 | 3129 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
3ff2ecac | 3130 | } else { |
7753dea8 MD |
3131 | pthread_mutex_unlock(&ustconsumer64_data.pid_mutex); |
3132 | } | |
173af62f DG |
3133 | |
3134 | /* | |
3135 | * Setup socket for consumer 64 bit. No need for atomic access | |
3136 | * since it was set above and can ONLY be set in this thread. | |
3137 | */ | |
a4b92340 DG |
3138 | ret = consumer_create_socket(&ustconsumer64_data, |
3139 | cmd_ctx->session->ust_session->consumer); | |
3140 | if (ret < 0) { | |
3141 | goto error; | |
173af62f DG |
3142 | } |
3143 | ||
7753dea8 | 3144 | /* 32-bit */ |
385b881b | 3145 | pthread_mutex_lock(&ustconsumer32_data.pid_mutex); |
e6142f2e | 3146 | if (config.consumerd32_bin_path.value && |
7753dea8 | 3147 | ustconsumer32_data.pid == 0 && |
785d2d0d | 3148 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
7753dea8 MD |
3149 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); |
3150 | ret = start_consumerd(&ustconsumer32_data); | |
3151 | if (ret < 0) { | |
f73fabfd | 3152 | ret = LTTNG_ERR_UST_CONSUMER32_FAIL; |
173af62f | 3153 | uatomic_set(&ust_consumerd32_fd, -EINVAL); |
7753dea8 MD |
3154 | goto error; |
3155 | } | |
5c827ce0 | 3156 | |
173af62f | 3157 | uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock); |
5c827ce0 | 3158 | uatomic_set(&ust_consumerd_state, CONSUMER_STARTED); |
7753dea8 MD |
3159 | } else { |
3160 | pthread_mutex_unlock(&ustconsumer32_data.pid_mutex); | |
2bdd86d4 | 3161 | } |
173af62f DG |
3162 | |
3163 | /* | |
92816cc3 | 3164 | * Setup socket for consumer 32 bit. No need for atomic access |
173af62f DG |
3165 | * since it was set above and can ONLY be set in this thread. |
3166 | */ | |
a4b92340 DG |
3167 | ret = consumer_create_socket(&ustconsumer32_data, |
3168 | cmd_ctx->session->ust_session->consumer); | |
3169 | if (ret < 0) { | |
3170 | goto error; | |
173af62f | 3171 | } |
44d3bd01 DG |
3172 | } |
3173 | break; | |
48842b30 | 3174 | } |
54d01ffb | 3175 | default: |
54d01ffb DG |
3176 | break; |
3177 | } | |
2e09ba09 | 3178 | skip_domain: |
33a2b854 | 3179 | |
5c827ce0 DG |
3180 | /* Validate consumer daemon state when start/stop trace command */ |
3181 | if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE || | |
3182 | cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) { | |
3183 | switch (cmd_ctx->lsm->domain.type) { | |
e7619875 JG |
3184 | case LTTNG_DOMAIN_NONE: |
3185 | break; | |
b9dfb167 | 3186 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 3187 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 3188 | case LTTNG_DOMAIN_PYTHON: |
5c827ce0 DG |
3189 | case LTTNG_DOMAIN_UST: |
3190 | if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3191 | ret = LTTNG_ERR_NO_USTCONSUMERD; |
5c827ce0 DG |
3192 | goto error; |
3193 | } | |
3194 | break; | |
3195 | case LTTNG_DOMAIN_KERNEL: | |
3196 | if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) { | |
f73fabfd | 3197 | ret = LTTNG_ERR_NO_KERNCONSUMERD; |
5c827ce0 DG |
3198 | goto error; |
3199 | } | |
3200 | break; | |
825535cc JG |
3201 | default: |
3202 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
3203 | goto error; | |
5c827ce0 DG |
3204 | } |
3205 | } | |
3206 | ||
8e0af1b4 MD |
3207 | /* |
3208 | * Check that the UID or GID match that of the tracing session. | |
3209 | * The root user can interact with all sessions. | |
3210 | */ | |
3211 | if (need_tracing_session) { | |
3212 | if (!session_access_ok(cmd_ctx->session, | |
730389d9 | 3213 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
e32d7f27 JG |
3214 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) || |
3215 | cmd_ctx->session->destroyed) { | |
f73fabfd | 3216 | ret = LTTNG_ERR_EPERM; |
8e0af1b4 MD |
3217 | goto error; |
3218 | } | |
3219 | } | |
3220 | ||
ffe60014 DG |
3221 | /* |
3222 | * Send relayd information to consumer as soon as we have a domain and a | |
3223 | * session defined. | |
3224 | */ | |
3225 | if (cmd_ctx->session && need_domain) { | |
3226 | /* | |
3227 | * Setup relayd if not done yet. If the relayd information was already | |
3228 | * sent to the consumer, this call will gracefully return. | |
3229 | */ | |
3230 | ret = cmd_setup_relayd(cmd_ctx->session); | |
3231 | if (ret != LTTNG_OK) { | |
3232 | goto error; | |
3233 | } | |
3234 | } | |
3235 | ||
54d01ffb DG |
3236 | /* Process by command type */ |
3237 | switch (cmd_ctx->lsm->cmd_type) { | |
3238 | case LTTNG_ADD_CONTEXT: | |
3239 | { | |
2001793c JG |
3240 | /* |
3241 | * An LTTNG_ADD_CONTEXT command might have a supplementary | |
3242 | * payload if the context being added is an application context. | |
3243 | */ | |
3244 | if (cmd_ctx->lsm->u.context.ctx.ctx == | |
3245 | LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
3246 | char *provider_name = NULL, *context_name = NULL; | |
3247 | size_t provider_name_len = | |
3248 | cmd_ctx->lsm->u.context.provider_name_len; | |
3249 | size_t context_name_len = | |
3250 | cmd_ctx->lsm->u.context.context_name_len; | |
3251 | ||
3252 | if (provider_name_len == 0 || context_name_len == 0) { | |
3253 | /* | |
3254 | * Application provider and context names MUST | |
3255 | * be provided. | |
3256 | */ | |
3257 | ret = -LTTNG_ERR_INVALID; | |
3258 | goto error; | |
3259 | } | |
3260 | ||
3261 | provider_name = zmalloc(provider_name_len + 1); | |
3262 | if (!provider_name) { | |
3263 | ret = -LTTNG_ERR_NOMEM; | |
3264 | goto error; | |
3265 | } | |
e0f4d107 JG |
3266 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = |
3267 | provider_name; | |
2001793c JG |
3268 | |
3269 | context_name = zmalloc(context_name_len + 1); | |
3270 | if (!context_name) { | |
3271 | ret = -LTTNG_ERR_NOMEM; | |
3272 | goto error_add_context; | |
3273 | } | |
e0f4d107 JG |
3274 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = |
3275 | context_name; | |
2001793c JG |
3276 | |
3277 | ret = lttcomm_recv_unix_sock(sock, provider_name, | |
3278 | provider_name_len); | |
3279 | if (ret < 0) { | |
3280 | goto error_add_context; | |
3281 | } | |
3282 | ||
3283 | ret = lttcomm_recv_unix_sock(sock, context_name, | |
3284 | context_name_len); | |
3285 | if (ret < 0) { | |
3286 | goto error_add_context; | |
3287 | } | |
2001793c JG |
3288 | } |
3289 | ||
3290 | /* | |
3291 | * cmd_add_context assumes ownership of the provider and context | |
3292 | * names. | |
3293 | */ | |
3294 | ret = cmd_add_context(cmd_ctx->session, | |
3295 | cmd_ctx->lsm->domain.type, | |
54d01ffb | 3296 | cmd_ctx->lsm->u.context.channel_name, |
2001793c JG |
3297 | &cmd_ctx->lsm->u.context.ctx, |
3298 | kernel_poll_pipe[1]); | |
3299 | ||
3300 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL; | |
3301 | cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL; | |
3302 | error_add_context: | |
3303 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name); | |
3304 | free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name); | |
3305 | if (ret < 0) { | |
3306 | goto error; | |
3307 | } | |
54d01ffb DG |
3308 | break; |
3309 | } | |
3310 | case LTTNG_DISABLE_CHANNEL: | |
3311 | { | |
3312 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
3313 | cmd_ctx->lsm->u.disable.channel_name); | |
3314 | break; | |
3315 | } | |
3316 | case LTTNG_DISABLE_EVENT: | |
3317 | { | |
47b7e2f9 JG |
3318 | |
3319 | /* | |
3320 | * FIXME: handle filter; for now we just receive the filter's | |
3321 | * bytecode along with the filter expression which are sent by | |
3322 | * liblttng-ctl and discard them. | |
3323 | * | |
3324 | * This fixes an issue where the client may block while sending | |
3325 | * the filter payload and encounter an error because the session | |
3326 | * daemon closes the socket without ever handling this data. | |
3327 | */ | |
3328 | size_t count = cmd_ctx->lsm->u.disable.expression_len + | |
3329 | cmd_ctx->lsm->u.disable.bytecode_len; | |
3330 | ||
3331 | if (count) { | |
3332 | char data[LTTNG_FILTER_MAX_LEN]; | |
3333 | ||
3334 | DBG("Discarding disable event command payload of size %zu", count); | |
3335 | while (count) { | |
3336 | ret = lttcomm_recv_unix_sock(sock, data, | |
3337 | count > sizeof(data) ? sizeof(data) : count); | |
3338 | if (ret < 0) { | |
3339 | goto error; | |
3340 | } | |
3341 | ||
3342 | count -= (size_t) ret; | |
3343 | } | |
3344 | } | |
6e911cad | 3345 | /* FIXME: passing packed structure to non-packed pointer */ |
54d01ffb DG |
3346 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
3347 | cmd_ctx->lsm->u.disable.channel_name, | |
6e911cad | 3348 | &cmd_ctx->lsm->u.disable.event); |
33a2b854 DG |
3349 | break; |
3350 | } | |
54d01ffb DG |
3351 | case LTTNG_ENABLE_CHANNEL: |
3352 | { | |
b3530820 JG |
3353 | cmd_ctx->lsm->u.channel.chan.attr.extended.ptr = |
3354 | (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended; | |
7972aab2 | 3355 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
b3530820 JG |
3356 | &cmd_ctx->lsm->u.channel.chan, |
3357 | kernel_poll_pipe[1]); | |
54d01ffb DG |
3358 | break; |
3359 | } | |
ccf10263 MD |
3360 | case LTTNG_TRACK_PID: |
3361 | { | |
3362 | ret = cmd_track_pid(cmd_ctx->session, | |
3363 | cmd_ctx->lsm->domain.type, | |
3364 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3365 | break; | |
3366 | } | |
3367 | case LTTNG_UNTRACK_PID: | |
3368 | { | |
3369 | ret = cmd_untrack_pid(cmd_ctx->session, | |
3370 | cmd_ctx->lsm->domain.type, | |
3371 | cmd_ctx->lsm->u.pid_tracker.pid); | |
3372 | break; | |
3373 | } | |
54d01ffb DG |
3374 | case LTTNG_ENABLE_EVENT: |
3375 | { | |
dcabc190 | 3376 | struct lttng_event *ev = NULL; |
882ef835 JI |
3377 | struct lttng_event_exclusion *exclusion = NULL; |
3378 | struct lttng_filter_bytecode *bytecode = NULL; | |
6b453b5e | 3379 | char *filter_expression = NULL; |
882ef835 | 3380 | |
67676bd8 DG |
3381 | /* Handle exclusion events and receive it from the client. */ |
3382 | if (cmd_ctx->lsm->u.enable.exclusion_count > 0) { | |
3383 | size_t count = cmd_ctx->lsm->u.enable.exclusion_count; | |
3384 | ||
3385 | exclusion = zmalloc(sizeof(struct lttng_event_exclusion) + | |
3386 | (count * LTTNG_SYMBOL_NAME_LEN)); | |
3387 | if (!exclusion) { | |
3388 | ret = LTTNG_ERR_EXCLUSION_NOMEM; | |
3389 | goto error; | |
882ef835 | 3390 | } |
882ef835 | 3391 | |
67676bd8 DG |
3392 | DBG("Receiving var len exclusion event list from client ..."); |
3393 | exclusion->count = count; | |
3394 | ret = lttcomm_recv_unix_sock(sock, exclusion->names, | |
3395 | count * LTTNG_SYMBOL_NAME_LEN); | |
3396 | if (ret <= 0) { | |
3397 | DBG("Nothing recv() from client var len data... continuing"); | |
3398 | *sock_error = 1; | |
3399 | free(exclusion); | |
3400 | ret = LTTNG_ERR_EXCLUSION_INVAL; | |
3401 | goto error; | |
3402 | } | |
3403 | } | |
3404 | ||
6b453b5e JG |
3405 | /* Get filter expression from client. */ |
3406 | if (cmd_ctx->lsm->u.enable.expression_len > 0) { | |
3407 | size_t expression_len = | |
3408 | cmd_ctx->lsm->u.enable.expression_len; | |
3409 | ||
3410 | if (expression_len > LTTNG_FILTER_MAX_LEN) { | |
3411 | ret = LTTNG_ERR_FILTER_INVAL; | |
3412 | free(exclusion); | |
3413 | goto error; | |
3414 | } | |
3415 | ||
3416 | filter_expression = zmalloc(expression_len); | |
3417 | if (!filter_expression) { | |
3418 | free(exclusion); | |
3419 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3420 | goto error; | |
3421 | } | |
3422 | ||
3423 | /* Receive var. len. data */ | |
3424 | DBG("Receiving var len filter's expression from client ..."); | |
3425 | ret = lttcomm_recv_unix_sock(sock, filter_expression, | |
3426 | expression_len); | |
3427 | if (ret <= 0) { | |
dcabc190 | 3428 | DBG("Nothing recv() from client var len data... continuing"); |
6b453b5e JG |
3429 | *sock_error = 1; |
3430 | free(filter_expression); | |
3431 | free(exclusion); | |
3432 | ret = LTTNG_ERR_FILTER_INVAL; | |
3433 | goto error; | |
3434 | } | |
3435 | } | |
3436 | ||
67676bd8 DG |
3437 | /* Handle filter and get bytecode from client. */ |
3438 | if (cmd_ctx->lsm->u.enable.bytecode_len > 0) { | |
3439 | size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len; | |
3440 | ||
3441 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
3442 | ret = LTTNG_ERR_FILTER_INVAL; | |
49d21f93 | 3443 | free(filter_expression); |
67676bd8 DG |
3444 | free(exclusion); |
3445 | goto error; | |
3446 | } | |
3447 | ||
3448 | bytecode = zmalloc(bytecode_len); | |
3449 | if (!bytecode) { | |
49d21f93 | 3450 | free(filter_expression); |
67676bd8 DG |
3451 | free(exclusion); |
3452 | ret = LTTNG_ERR_FILTER_NOMEM; | |
3453 | goto error; | |
882ef835 JI |
3454 | } |
3455 | ||
67676bd8 DG |
3456 | /* Receive var. len. data */ |
3457 | DBG("Receiving var len filter's bytecode from client ..."); | |
3458 | ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len); | |
3459 | if (ret <= 0) { | |
dcabc190 | 3460 | DBG("Nothing recv() from client var len data... continuing"); |
67676bd8 | 3461 | *sock_error = 1; |
49d21f93 | 3462 | free(filter_expression); |
67676bd8 DG |
3463 | free(bytecode); |
3464 | free(exclusion); | |
3465 | ret = LTTNG_ERR_FILTER_INVAL; | |
3466 | goto error; | |
3467 | } | |
3468 | ||
3469 | if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) { | |
49d21f93 | 3470 | free(filter_expression); |
67676bd8 DG |
3471 | free(bytecode); |
3472 | free(exclusion); | |
3473 | ret = LTTNG_ERR_FILTER_INVAL; | |
3474 | goto error; | |
3475 | } | |
882ef835 | 3476 | } |
67676bd8 | 3477 | |
dcabc190 FD |
3478 | ev = lttng_event_copy(&cmd_ctx->lsm->u.enable.event); |
3479 | if (!ev) { | |
3480 | DBG("Failed to copy event: %s", | |
3481 | cmd_ctx->lsm->u.enable.event.name); | |
5ae0c80e JG |
3482 | free(filter_expression); |
3483 | free(bytecode); | |
3484 | free(exclusion); | |
dcabc190 FD |
3485 | ret = LTTNG_ERR_NOMEM; |
3486 | goto error; | |
3487 | } | |
3488 | ||
3489 | ||
3490 | if (cmd_ctx->lsm->u.enable.userspace_probe_location_len > 0) { | |
3491 | /* Expect a userspace probe description. */ | |
3492 | ret = receive_userspace_probe(cmd_ctx, sock, sock_error, ev); | |
3493 | if (ret) { | |
5ae0c80e JG |
3494 | free(filter_expression); |
3495 | free(bytecode); | |
3496 | free(exclusion); | |
dcabc190 FD |
3497 | lttng_event_destroy(ev); |
3498 | goto error; | |
3499 | } | |
3500 | } | |
3501 | ||
67676bd8 DG |
3502 | ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain, |
3503 | cmd_ctx->lsm->u.enable.channel_name, | |
dcabc190 | 3504 | ev, |
6b453b5e | 3505 | filter_expression, bytecode, exclusion, |
67676bd8 | 3506 | kernel_poll_pipe[1]); |
dcabc190 | 3507 | lttng_event_destroy(ev); |
54d01ffb DG |
3508 | break; |
3509 | } | |
052da939 | 3510 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 3511 | { |
9f19cc17 | 3512 | struct lttng_event *events; |
54d01ffb | 3513 | ssize_t nb_events; |
052da939 | 3514 | |
0845bbfa | 3515 | session_lock_list(); |
54d01ffb | 3516 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
0845bbfa | 3517 | session_unlock_list(); |
54d01ffb | 3518 | if (nb_events < 0) { |
f73fabfd | 3519 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3520 | ret = -nb_events; |
3521 | goto error; | |
2ef84c95 DG |
3522 | } |
3523 | ||
3524 | /* | |
3525 | * Setup lttng message with payload size set to the event list size in | |
3526 | * bytes and then copy list into the llm payload. | |
3527 | */ | |
6e10c9b9 PP |
3528 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3529 | sizeof(struct lttng_event) * nb_events); | |
3530 | free(events); | |
3531 | ||
2ef84c95 DG |
3532 | if (ret < 0) { |
3533 | goto setup_error; | |
3534 | } | |
3535 | ||
f73fabfd | 3536 | ret = LTTNG_OK; |
2ef84c95 DG |
3537 | break; |
3538 | } | |
f37d259d MD |
3539 | case LTTNG_LIST_TRACEPOINT_FIELDS: |
3540 | { | |
3541 | struct lttng_event_field *fields; | |
3542 | ssize_t nb_fields; | |
3543 | ||
0845bbfa | 3544 | session_lock_list(); |
a4b92340 DG |
3545 | nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, |
3546 | &fields); | |
0845bbfa | 3547 | session_unlock_list(); |
f37d259d | 3548 | if (nb_fields < 0) { |
f73fabfd | 3549 | /* Return value is a negative lttng_error_code. */ |
f37d259d MD |
3550 | ret = -nb_fields; |
3551 | goto error; | |
3552 | } | |
3553 | ||
3554 | /* | |
3555 | * Setup lttng message with payload size set to the event list size in | |
3556 | * bytes and then copy list into the llm payload. | |
3557 | */ | |
6e10c9b9 | 3558 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields, |
a4b92340 | 3559 | sizeof(struct lttng_event_field) * nb_fields); |
6e10c9b9 PP |
3560 | free(fields); |
3561 | ||
f37d259d | 3562 | if (ret < 0) { |
f37d259d MD |
3563 | goto setup_error; |
3564 | } | |
3565 | ||
f73fabfd | 3566 | ret = LTTNG_OK; |
f37d259d MD |
3567 | break; |
3568 | } | |
834978fd DG |
3569 | case LTTNG_LIST_SYSCALLS: |
3570 | { | |
3571 | struct lttng_event *events; | |
3572 | ssize_t nb_events; | |
3573 | ||
3574 | nb_events = cmd_list_syscalls(&events); | |
3575 | if (nb_events < 0) { | |
3576 | /* Return value is a negative lttng_error_code. */ | |
3577 | ret = -nb_events; | |
3578 | goto error; | |
3579 | } | |
3580 | ||
3581 | /* | |
3582 | * Setup lttng message with payload size set to the event list size in | |
3583 | * bytes and then copy list into the llm payload. | |
3584 | */ | |
6e10c9b9 PP |
3585 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events, |
3586 | sizeof(struct lttng_event) * nb_events); | |
3587 | free(events); | |
3588 | ||
834978fd | 3589 | if (ret < 0) { |
834978fd DG |
3590 | goto setup_error; |
3591 | } | |
3592 | ||
834978fd DG |
3593 | ret = LTTNG_OK; |
3594 | break; | |
3595 | } | |
a5dfbb9d MD |
3596 | case LTTNG_LIST_TRACKER_PIDS: |
3597 | { | |
3598 | int32_t *pids = NULL; | |
3599 | ssize_t nr_pids; | |
3600 | ||
3601 | nr_pids = cmd_list_tracker_pids(cmd_ctx->session, | |
3602 | cmd_ctx->lsm->domain.type, &pids); | |
3603 | if (nr_pids < 0) { | |
3604 | /* Return value is a negative lttng_error_code. */ | |
3605 | ret = -nr_pids; | |
3606 | goto error; | |
3607 | } | |
3608 | ||
3609 | /* | |
3610 | * Setup lttng message with payload size set to the event list size in | |
3611 | * bytes and then copy list into the llm payload. | |
3612 | */ | |
6e10c9b9 PP |
3613 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids, |
3614 | sizeof(int32_t) * nr_pids); | |
3615 | free(pids); | |
3616 | ||
a5dfbb9d | 3617 | if (ret < 0) { |
a5dfbb9d MD |
3618 | goto setup_error; |
3619 | } | |
3620 | ||
a5dfbb9d MD |
3621 | ret = LTTNG_OK; |
3622 | break; | |
3623 | } | |
00e2e675 DG |
3624 | case LTTNG_SET_CONSUMER_URI: |
3625 | { | |
a4b92340 DG |
3626 | size_t nb_uri, len; |
3627 | struct lttng_uri *uris; | |
3628 | ||
3629 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3630 | len = nb_uri * sizeof(struct lttng_uri); | |
3631 | ||
3632 | if (nb_uri == 0) { | |
f73fabfd | 3633 | ret = LTTNG_ERR_INVALID; |
a4b92340 DG |
3634 | goto error; |
3635 | } | |
3636 | ||
3637 | uris = zmalloc(len); | |
3638 | if (uris == NULL) { | |
f73fabfd | 3639 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3640 | goto error; |
3641 | } | |
3642 | ||
3643 | /* Receive variable len data */ | |
77c7c900 | 3644 | DBG("Receiving %zu URI(s) from client ...", nb_uri); |
a4b92340 DG |
3645 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3646 | if (ret <= 0) { | |
3647 | DBG("No URIs received from client... continuing"); | |
3648 | *sock_error = 1; | |
f73fabfd | 3649 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3650 | free(uris); |
a4b92340 DG |
3651 | goto error; |
3652 | } | |
3653 | ||
bda32d56 JG |
3654 | ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris); |
3655 | free(uris); | |
f73fabfd | 3656 | if (ret != LTTNG_OK) { |
a4b92340 DG |
3657 | goto error; |
3658 | } | |
3659 | ||
b1d41407 | 3660 | |
00e2e675 DG |
3661 | break; |
3662 | } | |
f3ed775e | 3663 | case LTTNG_START_TRACE: |
8c0faa1d | 3664 | { |
259c2674 JD |
3665 | /* |
3666 | * On the first start, if we have a kernel session and we have | |
3667 | * enabled time or size-based rotations, we have to make sure | |
3668 | * the kernel tracer supports it. | |
3669 | */ | |
3670 | if (!cmd_ctx->session->has_been_started && \ | |
3671 | cmd_ctx->session->kernel_session && \ | |
3672 | (cmd_ctx->session->rotate_timer_period || \ | |
3673 | cmd_ctx->session->rotate_size) && \ | |
3674 | !check_rotate_compatible()) { | |
3675 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
3676 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
3677 | goto error; | |
3678 | } | |
54d01ffb | 3679 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
3680 | break; |
3681 | } | |
f3ed775e | 3682 | case LTTNG_STOP_TRACE: |
8c0faa1d | 3683 | { |
54d01ffb | 3684 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
3685 | break; |
3686 | } | |
5e16da05 MD |
3687 | case LTTNG_CREATE_SESSION: |
3688 | { | |
a4b92340 DG |
3689 | size_t nb_uri, len; |
3690 | struct lttng_uri *uris = NULL; | |
3691 | ||
3692 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3693 | len = nb_uri * sizeof(struct lttng_uri); | |
3694 | ||
3695 | if (nb_uri > 0) { | |
3696 | uris = zmalloc(len); | |
3697 | if (uris == NULL) { | |
f73fabfd | 3698 | ret = LTTNG_ERR_FATAL; |
a4b92340 DG |
3699 | goto error; |
3700 | } | |
3701 | ||
3702 | /* Receive variable len data */ | |
77c7c900 | 3703 | DBG("Waiting for %zu URIs from client ...", nb_uri); |
a4b92340 DG |
3704 | ret = lttcomm_recv_unix_sock(sock, uris, len); |
3705 | if (ret <= 0) { | |
3706 | DBG("No URIs received from client... continuing"); | |
3707 | *sock_error = 1; | |
f73fabfd | 3708 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3709 | free(uris); |
a4b92340 DG |
3710 | goto error; |
3711 | } | |
3712 | ||
3713 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3714 | DBG("Creating session with ONE network URI is a bad call"); | |
f73fabfd | 3715 | ret = LTTNG_ERR_SESSION_FAIL; |
b1d41407 | 3716 | free(uris); |
a4b92340 DG |
3717 | goto error; |
3718 | } | |
3719 | } | |
3720 | ||
3721 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri, | |
ecc48a90 | 3722 | &cmd_ctx->creds, 0); |
a4b92340 | 3723 | |
b1d41407 DG |
3724 | free(uris); |
3725 | ||
00e2e675 DG |
3726 | break; |
3727 | } | |
5e16da05 MD |
3728 | case LTTNG_DESTROY_SESSION: |
3729 | { | |
e32d7f27 | 3730 | ret = cmd_destroy_session(cmd_ctx->session, |
90936dcf | 3731 | notification_thread_handle); |
5461b305 | 3732 | break; |
5e16da05 | 3733 | } |
9f19cc17 | 3734 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 3735 | { |
54d01ffb | 3736 | ssize_t nb_dom; |
fa64dfb4 | 3737 | struct lttng_domain *domains = NULL; |
5461b305 | 3738 | |
54d01ffb DG |
3739 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
3740 | if (nb_dom < 0) { | |
f73fabfd | 3741 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3742 | ret = -nb_dom; |
3743 | goto error; | |
ce3d728c | 3744 | } |
520ff687 | 3745 | |
6e10c9b9 PP |
3746 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains, |
3747 | nb_dom * sizeof(struct lttng_domain)); | |
3748 | free(domains); | |
3749 | ||
5461b305 DG |
3750 | if (ret < 0) { |
3751 | goto setup_error; | |
520ff687 | 3752 | } |
57167058 | 3753 | |
f73fabfd | 3754 | ret = LTTNG_OK; |
5e16da05 MD |
3755 | break; |
3756 | } | |
9f19cc17 | 3757 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 3758 | { |
53e367f9 | 3759 | ssize_t payload_size; |
ade7ce52 | 3760 | struct lttng_channel *channels = NULL; |
54d01ffb | 3761 | |
53e367f9 | 3762 | payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type, |
b551a063 | 3763 | cmd_ctx->session, &channels); |
53e367f9 | 3764 | if (payload_size < 0) { |
f73fabfd | 3765 | /* Return value is a negative lttng_error_code. */ |
53e367f9 | 3766 | ret = -payload_size; |
54d01ffb | 3767 | goto error; |
5461b305 | 3768 | } |
ca95a216 | 3769 | |
6e10c9b9 | 3770 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels, |
53e367f9 | 3771 | payload_size); |
6e10c9b9 PP |
3772 | free(channels); |
3773 | ||
5461b305 DG |
3774 | if (ret < 0) { |
3775 | goto setup_error; | |
3776 | } | |
9f19cc17 | 3777 | |
f73fabfd | 3778 | ret = LTTNG_OK; |
5461b305 | 3779 | break; |
5e16da05 | 3780 | } |
9f19cc17 | 3781 | case LTTNG_LIST_EVENTS: |
5e16da05 | 3782 | { |
b551a063 | 3783 | ssize_t nb_event; |
684d34d2 | 3784 | struct lttng_event *events = NULL; |
b4e3ceb9 PP |
3785 | struct lttcomm_event_command_header cmd_header; |
3786 | size_t total_size; | |
3787 | ||
b2d66287 | 3788 | memset(&cmd_header, 0, sizeof(cmd_header)); |
b4e3ceb9 PP |
3789 | /* Extended infos are included at the end of events */ |
3790 | nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, | |
3791 | cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name, | |
3792 | &events, &total_size); | |
9f19cc17 | 3793 | |
54d01ffb | 3794 | if (nb_event < 0) { |
f73fabfd | 3795 | /* Return value is a negative lttng_error_code. */ |
54d01ffb DG |
3796 | ret = -nb_event; |
3797 | goto error; | |
5461b305 | 3798 | } |
ca95a216 | 3799 | |
b4e3ceb9 PP |
3800 | cmd_header.nb_events = nb_event; |
3801 | ret = setup_lttng_msg(cmd_ctx, events, total_size, | |
3802 | &cmd_header, sizeof(cmd_header)); | |
6e10c9b9 PP |
3803 | free(events); |
3804 | ||
5461b305 DG |
3805 | if (ret < 0) { |
3806 | goto setup_error; | |
3807 | } | |
9f19cc17 | 3808 | |
f73fabfd | 3809 | ret = LTTNG_OK; |
5461b305 | 3810 | break; |
5e16da05 MD |
3811 | } |
3812 | case LTTNG_LIST_SESSIONS: | |
3813 | { | |
8e0af1b4 | 3814 | unsigned int nr_sessions; |
6e10c9b9 PP |
3815 | void *sessions_payload; |
3816 | size_t payload_len; | |
5461b305 | 3817 | |
8e0af1b4 | 3818 | session_lock_list(); |
730389d9 DG |
3819 | nr_sessions = lttng_sessions_count( |
3820 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), | |
3821 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
6e10c9b9 PP |
3822 | payload_len = sizeof(struct lttng_session) * nr_sessions; |
3823 | sessions_payload = zmalloc(payload_len); | |
d32fb093 | 3824 | |
6e10c9b9 | 3825 | if (!sessions_payload) { |
54d01ffb | 3826 | session_unlock_list(); |
6e10c9b9 | 3827 | ret = -ENOMEM; |
5461b305 | 3828 | goto setup_error; |
e065084a | 3829 | } |
5e16da05 | 3830 | |
6e10c9b9 | 3831 | cmd_list_lttng_sessions(sessions_payload, |
730389d9 DG |
3832 | LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), |
3833 | LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); | |
54d01ffb | 3834 | session_unlock_list(); |
5e16da05 | 3835 | |
6e10c9b9 PP |
3836 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, |
3837 | payload_len); | |
3838 | free(sessions_payload); | |
3839 | ||
3840 | if (ret < 0) { | |
3841 | goto setup_error; | |
3842 | } | |
3843 | ||
f73fabfd | 3844 | ret = LTTNG_OK; |
5e16da05 MD |
3845 | break; |
3846 | } | |
d9800920 DG |
3847 | case LTTNG_REGISTER_CONSUMER: |
3848 | { | |
2f77fc4b DG |
3849 | struct consumer_data *cdata; |
3850 | ||
3851 | switch (cmd_ctx->lsm->domain.type) { | |
3852 | case LTTNG_DOMAIN_KERNEL: | |
3853 | cdata = &kconsumer_data; | |
3854 | break; | |
3855 | default: | |
f73fabfd | 3856 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3857 | goto error; |
3858 | } | |
3859 | ||
54d01ffb | 3860 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2f77fc4b | 3861 | cmd_ctx->lsm->u.reg.path, cdata); |
d9800920 DG |
3862 | break; |
3863 | } | |
6d805429 | 3864 | case LTTNG_DATA_PENDING: |
806e2684 | 3865 | { |
f6151c55 | 3866 | int pending_ret; |
6e10c9b9 | 3867 | uint8_t pending_ret_byte; |
f6151c55 JG |
3868 | |
3869 | pending_ret = cmd_data_pending(cmd_ctx->session); | |
6e10c9b9 | 3870 | |
f6151c55 JG |
3871 | /* |
3872 | * FIXME | |
3873 | * | |
3874 | * This function may returns 0 or 1 to indicate whether or not | |
3875 | * there is data pending. In case of error, it should return an | |
3876 | * LTTNG_ERR code. However, some code paths may still return | |
3877 | * a nondescript error code, which we handle by returning an | |
3878 | * "unknown" error. | |
3879 | */ | |
3880 | if (pending_ret == 0 || pending_ret == 1) { | |
6e10c9b9 PP |
3881 | /* |
3882 | * ret will be set to LTTNG_OK at the end of | |
3883 | * this function. | |
3884 | */ | |
f6151c55 JG |
3885 | } else if (pending_ret < 0) { |
3886 | ret = LTTNG_ERR_UNK; | |
3887 | goto setup_error; | |
3888 | } else { | |
3889 | ret = pending_ret; | |
3890 | goto setup_error; | |
3891 | } | |
3892 | ||
6e10c9b9 PP |
3893 | pending_ret_byte = (uint8_t) pending_ret; |
3894 | ||
3895 | /* 1 byte to return whether or not data is pending */ | |
3896 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, | |
3897 | &pending_ret_byte, 1); | |
3898 | ||
3899 | if (ret < 0) { | |
3900 | goto setup_error; | |
3901 | } | |
3902 | ||
3903 | ret = LTTNG_OK; | |
806e2684 DG |
3904 | break; |
3905 | } | |
da3c9ec1 DG |
3906 | case LTTNG_SNAPSHOT_ADD_OUTPUT: |
3907 | { | |
6dc3064a DG |
3908 | struct lttcomm_lttng_output_id reply; |
3909 | ||
3910 | ret = cmd_snapshot_add_output(cmd_ctx->session, | |
3911 | &cmd_ctx->lsm->u.snapshot_output.output, &reply.id); | |
3912 | if (ret != LTTNG_OK) { | |
3913 | goto error; | |
3914 | } | |
3915 | ||
6e10c9b9 PP |
3916 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply, |
3917 | sizeof(reply)); | |
6dc3064a DG |
3918 | if (ret < 0) { |
3919 | goto setup_error; | |
3920 | } | |
3921 | ||
3922 | /* Copy output list into message payload */ | |
6dc3064a | 3923 | ret = LTTNG_OK; |
da3c9ec1 DG |
3924 | break; |
3925 | } | |
3926 | case LTTNG_SNAPSHOT_DEL_OUTPUT: | |
3927 | { | |
6dc3064a DG |
3928 | ret = cmd_snapshot_del_output(cmd_ctx->session, |
3929 | &cmd_ctx->lsm->u.snapshot_output.output); | |
da3c9ec1 DG |
3930 | break; |
3931 | } | |
3932 | case LTTNG_SNAPSHOT_LIST_OUTPUT: | |
3933 | { | |
6dc3064a DG |
3934 | ssize_t nb_output; |
3935 | struct lttng_snapshot_output *outputs = NULL; | |
3936 | ||
3937 | nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs); | |
3938 | if (nb_output < 0) { | |
3939 | ret = -nb_output; | |
3940 | goto error; | |
3941 | } | |
3942 | ||
6e10c9b9 PP |
3943 | assert((nb_output > 0 && outputs) || nb_output == 0); |
3944 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs, | |
6dc3064a | 3945 | nb_output * sizeof(struct lttng_snapshot_output)); |
6e10c9b9 PP |
3946 | free(outputs); |
3947 | ||
6dc3064a | 3948 | if (ret < 0) { |
6dc3064a DG |
3949 | goto setup_error; |
3950 | } | |
3951 | ||
6dc3064a | 3952 | ret = LTTNG_OK; |
da3c9ec1 DG |
3953 | break; |
3954 | } | |
3955 | case LTTNG_SNAPSHOT_RECORD: | |
3956 | { | |
6dc3064a DG |
3957 | ret = cmd_snapshot_record(cmd_ctx->session, |
3958 | &cmd_ctx->lsm->u.snapshot_record.output, | |
3959 | cmd_ctx->lsm->u.snapshot_record.wait); | |
da3c9ec1 DG |
3960 | break; |
3961 | } | |
27babd3a DG |
3962 | case LTTNG_CREATE_SESSION_SNAPSHOT: |
3963 | { | |
3964 | size_t nb_uri, len; | |
3965 | struct lttng_uri *uris = NULL; | |
3966 | ||
3967 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
3968 | len = nb_uri * sizeof(struct lttng_uri); | |
3969 | ||
3970 | if (nb_uri > 0) { | |
3971 | uris = zmalloc(len); | |
3972 | if (uris == NULL) { | |
3973 | ret = LTTNG_ERR_FATAL; | |
3974 | goto error; | |
3975 | } | |
3976 | ||
3977 | /* Receive variable len data */ | |
3978 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
3979 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
3980 | if (ret <= 0) { | |
3981 | DBG("No URIs received from client... continuing"); | |
3982 | *sock_error = 1; | |
3983 | ret = LTTNG_ERR_SESSION_FAIL; | |
3984 | free(uris); | |
3985 | goto error; | |
3986 | } | |
3987 | ||
3988 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
3989 | DBG("Creating session with ONE network URI is a bad call"); | |
3990 | ret = LTTNG_ERR_SESSION_FAIL; | |
3991 | free(uris); | |
3992 | goto error; | |
3993 | } | |
3994 | } | |
3995 | ||
3996 | ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris, | |
3997 | nb_uri, &cmd_ctx->creds); | |
3998 | free(uris); | |
3999 | break; | |
4000 | } | |
ecc48a90 JD |
4001 | case LTTNG_CREATE_SESSION_LIVE: |
4002 | { | |
4003 | size_t nb_uri, len; | |
4004 | struct lttng_uri *uris = NULL; | |
4005 | ||
4006 | nb_uri = cmd_ctx->lsm->u.uri.size; | |
4007 | len = nb_uri * sizeof(struct lttng_uri); | |
4008 | ||
4009 | if (nb_uri > 0) { | |
4010 | uris = zmalloc(len); | |
4011 | if (uris == NULL) { | |
4012 | ret = LTTNG_ERR_FATAL; | |
4013 | goto error; | |
4014 | } | |
4015 | ||
4016 | /* Receive variable len data */ | |
4017 | DBG("Waiting for %zu URIs from client ...", nb_uri); | |
4018 | ret = lttcomm_recv_unix_sock(sock, uris, len); | |
4019 | if (ret <= 0) { | |
4020 | DBG("No URIs received from client... continuing"); | |
4021 | *sock_error = 1; | |
4022 | ret = LTTNG_ERR_SESSION_FAIL; | |
4023 | free(uris); | |
4024 | goto error; | |
4025 | } | |
4026 | ||
4027 | if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) { | |
4028 | DBG("Creating session with ONE network URI is a bad call"); | |
4029 | ret = LTTNG_ERR_SESSION_FAIL; | |
4030 | free(uris); | |
4031 | goto error; | |
4032 | } | |
4033 | } | |
4034 | ||
4035 | ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, | |
4036 | nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval); | |
4037 | free(uris); | |
4038 | break; | |
4039 | } | |
fb198a11 JG |
4040 | case LTTNG_SAVE_SESSION: |
4041 | { | |
4042 | ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr, | |
4043 | &cmd_ctx->creds); | |
4044 | break; | |
4045 | } | |
d7ba1388 MD |
4046 | case LTTNG_SET_SESSION_SHM_PATH: |
4047 | { | |
4048 | ret = cmd_set_session_shm_path(cmd_ctx->session, | |
4049 | cmd_ctx->lsm->u.set_shm_path.shm_path); | |
4050 | break; | |
4051 | } | |
eded6438 | 4052 | case LTTNG_REGENERATE_METADATA: |
93ec662e | 4053 | { |
eded6438 | 4054 | ret = cmd_regenerate_metadata(cmd_ctx->session); |
93ec662e JD |
4055 | break; |
4056 | } | |
c2561365 JD |
4057 | case LTTNG_REGENERATE_STATEDUMP: |
4058 | { | |
4059 | ret = cmd_regenerate_statedump(cmd_ctx->session); | |
4060 | break; | |
4061 | } | |
b3530820 JG |
4062 | case LTTNG_REGISTER_TRIGGER: |
4063 | { | |
4064 | ret = cmd_register_trigger(cmd_ctx, sock, | |
4065 | notification_thread_handle); | |
4066 | break; | |
4067 | } | |
4068 | case LTTNG_UNREGISTER_TRIGGER: | |
4069 | { | |
4070 | ret = cmd_unregister_trigger(cmd_ctx, sock, | |
4071 | notification_thread_handle); | |
4072 | break; | |
4073 | } | |
5c408ad8 JD |
4074 | case LTTNG_ROTATE_SESSION: |
4075 | { | |
4076 | struct lttng_rotate_session_return rotate_return; | |
4077 | ||
4078 | DBG("Client rotate session \"%s\"", cmd_ctx->session->name); | |
4079 | ||
d68c9a04 | 4080 | memset(&rotate_return, 0, sizeof(rotate_return)); |
5c408ad8 JD |
4081 | if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) { |
4082 | DBG("Kernel tracer version is not compatible with the rotation feature"); | |
4083 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
4084 | goto error; | |
4085 | } | |
4086 | ||
4087 | ret = cmd_rotate_session(cmd_ctx->session, &rotate_return); | |
4088 | if (ret < 0) { | |
4089 | ret = -ret; | |
4090 | goto error; | |
4091 | } | |
4092 | ||
4093 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return, | |
4094 | sizeof(rotate_return)); | |
4095 | if (ret < 0) { | |
4096 | ret = -ret; | |
4097 | goto error; | |
4098 | } | |
4099 | ||
4100 | ret = LTTNG_OK; | |
4101 | break; | |
4102 | } | |
d68c9a04 | 4103 | case LTTNG_ROTATION_GET_INFO: |
5c408ad8 | 4104 | { |
d68c9a04 | 4105 | struct lttng_rotation_get_info_return get_info_return; |
5c408ad8 | 4106 | |
d68c9a04 JD |
4107 | memset(&get_info_return, 0, sizeof(get_info_return)); |
4108 | ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return, | |
4109 | cmd_ctx->lsm->u.get_rotation_info.rotation_id); | |
5c408ad8 JD |
4110 | if (ret < 0) { |
4111 | ret = -ret; | |
4112 | goto error; | |
4113 | } | |
4114 | ||
d68c9a04 JD |
4115 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return, |
4116 | sizeof(get_info_return)); | |
5c408ad8 JD |
4117 | if (ret < 0) { |
4118 | ret = -ret; | |
4119 | goto error; | |
4120 | } | |
4121 | ||
4122 | ret = LTTNG_OK; | |
4123 | break; | |
4124 | } | |
259c2674 JD |
4125 | case LTTNG_ROTATION_SET_SCHEDULE: |
4126 | { | |
66ea93b1 JG |
4127 | bool set_schedule; |
4128 | enum lttng_rotation_schedule_type schedule_type; | |
4129 | uint64_t value; | |
4130 | ||
259c2674 JD |
4131 | if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) { |
4132 | DBG("Kernel tracer version does not support session rotations"); | |
4133 | ret = LTTNG_ERR_ROTATION_WRONG_VERSION; | |
4134 | goto error; | |
4135 | } | |
4136 | ||
66ea93b1 JG |
4137 | set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1; |
4138 | schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type; | |
4139 | value = cmd_ctx->lsm->u.rotation_set_schedule.value; | |
4140 | ||
259c2674 | 4141 | ret = cmd_rotation_set_schedule(cmd_ctx->session, |
66ea93b1 JG |
4142 | set_schedule, |
4143 | schedule_type, | |
4144 | value, | |
90936dcf | 4145 | notification_thread_handle); |
a2026546 | 4146 | if (ret != LTTNG_OK) { |
259c2674 JD |
4147 | goto error; |
4148 | } | |
4149 | ||
259c2674 JD |
4150 | break; |
4151 | } | |
66ea93b1 | 4152 | case LTTNG_SESSION_LIST_ROTATION_SCHEDULES: |
329f3443 | 4153 | { |
66ea93b1 JG |
4154 | struct lttng_session_list_schedules_return schedules = { |
4155 | .periodic.set = !!cmd_ctx->session->rotate_timer_period, | |
4156 | .periodic.value = cmd_ctx->session->rotate_timer_period, | |
4157 | .size.set = !!cmd_ctx->session->rotate_size, | |
4158 | .size.value = cmd_ctx->session->rotate_size, | |
4159 | }; | |
4160 | ||
4161 | ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules, | |
4162 | sizeof(schedules)); | |
329f3443 JD |
4163 | if (ret < 0) { |
4164 | ret = -ret; | |
4165 | goto error; | |
4166 | } | |
4167 | ||
4168 | ret = LTTNG_OK; | |
4169 | break; | |
4170 | } | |
5e16da05 | 4171 | default: |
f73fabfd | 4172 | ret = LTTNG_ERR_UND; |
5461b305 | 4173 | break; |
fac6795d DG |
4174 | } |
4175 | ||
5461b305 | 4176 | error: |
5461b305 DG |
4177 | if (cmd_ctx->llm == NULL) { |
4178 | DBG("Missing llm structure. Allocating one."); | |
6e10c9b9 | 4179 | if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) { |
5461b305 DG |
4180 | goto setup_error; |
4181 | } | |
4182 | } | |
54d01ffb | 4183 | /* Set return code */ |
5461b305 | 4184 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 4185 | setup_error: |
b5541356 | 4186 | if (cmd_ctx->session) { |
54d01ffb | 4187 | session_unlock(cmd_ctx->session); |
e32d7f27 | 4188 | session_put(cmd_ctx->session); |
b5541356 | 4189 | } |
256a5576 MD |
4190 | if (need_tracing_session) { |
4191 | session_unlock_list(); | |
4192 | } | |
54d01ffb | 4193 | init_setup_error: |
3745d315 | 4194 | assert(!rcu_read_ongoing()); |
8028d920 | 4195 | return ret; |
fac6795d DG |
4196 | } |
4197 | ||
44a5e5eb DG |
4198 | /* |
4199 | * Thread managing health check socket. | |
4200 | */ | |
4201 | static void *thread_manage_health(void *data) | |
4202 | { | |
eb4a2943 | 4203 | int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; |
44a5e5eb DG |
4204 | uint32_t revents, nb_fd; |
4205 | struct lttng_poll_event events; | |
0c89d795 MD |
4206 | struct health_comm_msg msg; |
4207 | struct health_comm_reply reply; | |
44a5e5eb DG |
4208 | |
4209 | DBG("[thread] Manage health check started"); | |
4210 | ||
4211 | rcu_register_thread(); | |
4212 | ||
6d737ce4 DG |
4213 | /* We might hit an error path before this is created. */ |
4214 | lttng_poll_init(&events); | |
3cc04881 | 4215 | |
44a5e5eb | 4216 | /* Create unix socket */ |
e6142f2e | 4217 | sock = lttcomm_create_unix_sock(config.health_unix_sock_path.value); |
44a5e5eb DG |
4218 | if (sock < 0) { |
4219 | ERR("Unable to create health check Unix socket"); | |
44a5e5eb DG |
4220 | goto error; |
4221 | } | |
4222 | ||
6c71277b MD |
4223 | if (is_root) { |
4224 | /* lttng health client socket path permissions */ | |
e6142f2e JG |
4225 | ret = chown(config.health_unix_sock_path.value, 0, |
4226 | utils_get_group_id(config.tracing_group_name.value)); | |
6c71277b | 4227 | if (ret < 0) { |
e6142f2e | 4228 | ERR("Unable to set group on %s", config.health_unix_sock_path.value); |
6c71277b | 4229 | PERROR("chown"); |
6c71277b MD |
4230 | goto error; |
4231 | } | |
4232 | ||
e6142f2e | 4233 | ret = chmod(config.health_unix_sock_path.value, |
6c71277b MD |
4234 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
4235 | if (ret < 0) { | |
e6142f2e | 4236 | ERR("Unable to set permissions on %s", config.health_unix_sock_path.value); |
6c71277b | 4237 | PERROR("chmod"); |
6c71277b MD |
4238 | goto error; |
4239 | } | |
4240 | } | |
4241 | ||
b662582b DG |
4242 | /* |
4243 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4244 | * show must go on. | |
4245 | */ | |
4246 | (void) utils_set_fd_cloexec(sock); | |
4247 | ||
44a5e5eb DG |
4248 | ret = lttcomm_listen_unix_sock(sock); |
4249 | if (ret < 0) { | |
4250 | goto error; | |
4251 | } | |
4252 | ||
4253 | /* | |
4254 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4255 | * more will be added to this poll set. | |
4256 | */ | |
d0b96690 | 4257 | ret = sessiond_set_thread_pollset(&events, 2); |
44a5e5eb DG |
4258 | if (ret < 0) { |
4259 | goto error; | |
4260 | } | |
4261 | ||
4262 | /* Add the application registration socket */ | |
4263 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI); | |
4264 | if (ret < 0) { | |
4265 | goto error; | |
4266 | } | |
4267 | ||
ef367a93 | 4268 | sessiond_notify_ready(); |
97bc1426 | 4269 | |
44a5e5eb DG |
4270 | while (1) { |
4271 | DBG("Health check ready"); | |
4272 | ||
44a5e5eb DG |
4273 | /* Inifinite blocking call, waiting for transmission */ |
4274 | restart: | |
4275 | ret = lttng_poll_wait(&events, -1); | |
4276 | if (ret < 0) { | |
4277 | /* | |
4278 | * Restart interrupted system call. | |
4279 | */ | |
4280 | if (errno == EINTR) { | |
4281 | goto restart; | |
4282 | } | |
4283 | goto error; | |
4284 | } | |
4285 | ||
0d9c5d77 DG |
4286 | nb_fd = ret; |
4287 | ||
44a5e5eb DG |
4288 | for (i = 0; i < nb_fd; i++) { |
4289 | /* Fetch once the poll data */ | |
4290 | revents = LTTNG_POLL_GETEV(&events, i); | |
4291 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4292 | ||
fd20dac9 MD |
4293 | if (!revents) { |
4294 | /* No activity for this FD (poll implementation). */ | |
4295 | continue; | |
4296 | } | |
4297 | ||
44a5e5eb | 4298 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4299 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
44a5e5eb | 4300 | if (ret) { |
139ac872 MD |
4301 | err = 0; |
4302 | goto exit; | |
44a5e5eb DG |
4303 | } |
4304 | ||
4305 | /* Event on the registration socket */ | |
4306 | if (pollfd == sock) { | |
03e43155 MD |
4307 | if (revents & LPOLLIN) { |
4308 | continue; | |
4309 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
44a5e5eb DG |
4310 | ERR("Health socket poll error"); |
4311 | goto error; | |
03e43155 MD |
4312 | } else { |
4313 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4314 | goto error; | |
44a5e5eb DG |
4315 | } |
4316 | } | |
4317 | } | |
4318 | ||
4319 | new_sock = lttcomm_accept_unix_sock(sock); | |
4320 | if (new_sock < 0) { | |
4321 | goto error; | |
4322 | } | |
4323 | ||
b662582b DG |
4324 | /* |
4325 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4326 | * show must go on. | |
4327 | */ | |
4328 | (void) utils_set_fd_cloexec(new_sock); | |
4329 | ||
44a5e5eb DG |
4330 | DBG("Receiving data from client for health..."); |
4331 | ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); | |
4332 | if (ret <= 0) { | |
4333 | DBG("Nothing recv() from client... continuing"); | |
4334 | ret = close(new_sock); | |
4335 | if (ret) { | |
4336 | PERROR("close"); | |
4337 | } | |
44a5e5eb DG |
4338 | continue; |
4339 | } | |
4340 | ||
4341 | rcu_thread_online(); | |
4342 | ||
53efb85a | 4343 | memset(&reply, 0, sizeof(reply)); |
6c71277b MD |
4344 | for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) { |
4345 | /* | |
4346 | * health_check_state returns 0 if health is | |
4347 | * bad. | |
4348 | */ | |
4349 | if (!health_check_state(health_sessiond, i)) { | |
4350 | reply.ret_code |= 1ULL << i; | |
4351 | } | |
44a5e5eb DG |
4352 | } |
4353 | ||
6c71277b | 4354 | DBG2("Health check return value %" PRIx64, reply.ret_code); |
44a5e5eb DG |
4355 | |
4356 | ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply)); | |
4357 | if (ret < 0) { | |
4358 | ERR("Failed to send health data back to client"); | |
4359 | } | |
4360 | ||
4361 | /* End of transmission */ | |
4362 | ret = close(new_sock); | |
4363 | if (ret) { | |
4364 | PERROR("close"); | |
4365 | } | |
44a5e5eb DG |
4366 | } |
4367 | ||
139ac872 | 4368 | exit: |
44a5e5eb | 4369 | error: |
139ac872 MD |
4370 | if (err) { |
4371 | ERR("Health error occurred in %s", __func__); | |
4372 | } | |
44a5e5eb | 4373 | DBG("Health check thread dying"); |
e6142f2e | 4374 | unlink(config.health_unix_sock_path.value); |
44a5e5eb DG |
4375 | if (sock >= 0) { |
4376 | ret = close(sock); | |
4377 | if (ret) { | |
4378 | PERROR("close"); | |
4379 | } | |
4380 | } | |
44a5e5eb DG |
4381 | |
4382 | lttng_poll_clean(&events); | |
b11d6a7f | 4383 | stop_threads(); |
44a5e5eb DG |
4384 | rcu_unregister_thread(); |
4385 | return NULL; | |
4386 | } | |
4387 | ||
1d4b027a | 4388 | /* |
d063d709 DG |
4389 | * This thread manage all clients request using the unix client socket for |
4390 | * communication. | |
1d4b027a DG |
4391 | */ |
4392 | static void *thread_manage_clients(void *data) | |
4393 | { | |
139ac872 | 4394 | int sock = -1, ret, i, pollfd, err = -1; |
53a80697 | 4395 | int sock_error; |
5eb91c98 | 4396 | uint32_t revents, nb_fd; |
273ea72c | 4397 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 4398 | struct lttng_poll_event events; |
1d4b027a DG |
4399 | |
4400 | DBG("[thread] Manage client started"); | |
4401 | ||
f6a9efaa DG |
4402 | rcu_register_thread(); |
4403 | ||
6c71277b | 4404 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD); |
927ca06a | 4405 | |
840cb59c | 4406 | health_code_update(); |
44a5e5eb | 4407 | |
1d4b027a DG |
4408 | ret = lttcomm_listen_unix_sock(client_sock); |
4409 | if (ret < 0) { | |
35df2755 | 4410 | goto error_listen; |
1d4b027a DG |
4411 | } |
4412 | ||
5eb91c98 DG |
4413 | /* |
4414 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
4415 | * more will be added to this poll set. | |
4416 | */ | |
d0b96690 | 4417 | ret = sessiond_set_thread_pollset(&events, 2); |
5eb91c98 | 4418 | if (ret < 0) { |
35df2755 | 4419 | goto error_create_poll; |
5eb91c98 | 4420 | } |
273ea72c | 4421 | |
5eb91c98 DG |
4422 | /* Add the application registration socket */ |
4423 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
4424 | if (ret < 0) { | |
4425 | goto error; | |
4426 | } | |
273ea72c | 4427 | |
ef367a93 JG |
4428 | ret = sem_post(&load_info->message_thread_ready); |
4429 | if (ret) { | |
4430 | PERROR("sem_post message_thread_ready"); | |
4431 | goto error; | |
4432 | } | |
0bb7724a | 4433 | |
f28f9e44 JG |
4434 | /* |
4435 | * Wait until all support threads are initialized before accepting | |
4436 | * commands. | |
4437 | */ | |
4438 | while (uatomic_read(<tng_sessiond_ready) != 0) { | |
f28f9e44 JG |
4439 | /* |
4440 | * If a support thread failed to launch, it may signal that | |
4441 | * we must exit and the sessiond would never be marked as | |
4442 | * "ready". | |
4443 | * | |
4444 | * The timeout is set to 1ms, which serves as a way to | |
4445 | * pace down this check. | |
4446 | */ | |
a7333da7 JG |
4447 | ret = sessiond_wait_for_quit_pipe(1000); |
4448 | if (ret > 0) { | |
f28f9e44 JG |
4449 | goto exit; |
4450 | } | |
4451 | } | |
8b0d4636 JG |
4452 | /* |
4453 | * This barrier is paired with the one in sessiond_notify_ready() to | |
4454 | * ensure that loads accessing data initialized by the other threads, | |
4455 | * on which this thread was waiting, are not performed before this point. | |
4456 | * | |
4457 | * Note that this could be a 'read' memory barrier, but a full barrier | |
4458 | * is used in case the code changes. The performance implications of | |
4459 | * this choice are minimal since this is a slow path. | |
4460 | */ | |
4461 | cmm_smp_mb(); | |
f28f9e44 | 4462 | |
72453c8c MD |
4463 | /* This testpoint is after we signal readiness to the parent. */ |
4464 | if (testpoint(sessiond_thread_manage_clients)) { | |
4465 | goto error; | |
4466 | } | |
4467 | ||
e547b070 | 4468 | if (testpoint(sessiond_thread_manage_clients_before_loop)) { |
6993eeb3 CB |
4469 | goto error; |
4470 | } | |
8ac94142 | 4471 | |
840cb59c | 4472 | health_code_update(); |
44a5e5eb | 4473 | |
99d688f2 JG |
4474 | /* Set state as running. */ |
4475 | sessiond_set_client_thread_state(true); | |
4476 | ||
1d4b027a | 4477 | while (1) { |
a503e1ef JG |
4478 | const struct cmd_completion_handler *cmd_completion_handler; |
4479 | ||
1d4b027a | 4480 | DBG("Accepting client command ..."); |
273ea72c DG |
4481 | |
4482 | /* Inifinite blocking call, waiting for transmission */ | |
88f2b785 | 4483 | restart: |
a78af745 | 4484 | health_poll_entry(); |
5eb91c98 | 4485 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 4486 | health_poll_exit(); |
273ea72c | 4487 | if (ret < 0) { |
88f2b785 MD |
4488 | /* |
4489 | * Restart interrupted system call. | |
4490 | */ | |
4491 | if (errno == EINTR) { | |
4492 | goto restart; | |
4493 | } | |
273ea72c DG |
4494 | goto error; |
4495 | } | |
4496 | ||
0d9c5d77 DG |
4497 | nb_fd = ret; |
4498 | ||
5eb91c98 DG |
4499 | for (i = 0; i < nb_fd; i++) { |
4500 | /* Fetch once the poll data */ | |
4501 | revents = LTTNG_POLL_GETEV(&events, i); | |
4502 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
4503 | ||
840cb59c | 4504 | health_code_update(); |
44a5e5eb | 4505 | |
fd20dac9 MD |
4506 | if (!revents) { |
4507 | /* No activity for this FD (poll implementation). */ | |
4508 | continue; | |
4509 | } | |
4510 | ||
5eb91c98 | 4511 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 4512 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 4513 | if (ret) { |
139ac872 MD |
4514 | err = 0; |
4515 | goto exit; | |
5eb91c98 DG |
4516 | } |
4517 | ||
4518 | /* Event on the registration socket */ | |
4519 | if (pollfd == client_sock) { | |
03e43155 MD |
4520 | if (revents & LPOLLIN) { |
4521 | continue; | |
4522 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
5eb91c98 DG |
4523 | ERR("Client socket poll error"); |
4524 | goto error; | |
03e43155 MD |
4525 | } else { |
4526 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
4527 | goto error; | |
5eb91c98 DG |
4528 | } |
4529 | } | |
4530 | } | |
4531 | ||
4532 | DBG("Wait for client response"); | |
4533 | ||
840cb59c | 4534 | health_code_update(); |
44a5e5eb | 4535 | |
5eb91c98 DG |
4536 | sock = lttcomm_accept_unix_sock(client_sock); |
4537 | if (sock < 0) { | |
273ea72c | 4538 | goto error; |
273ea72c DG |
4539 | } |
4540 | ||
b662582b DG |
4541 | /* |
4542 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
4543 | * show must go on. | |
4544 | */ | |
4545 | (void) utils_set_fd_cloexec(sock); | |
4546 | ||
be040666 DG |
4547 | /* Set socket option for credentials retrieval */ |
4548 | ret = lttcomm_setsockopt_creds_unix_sock(sock); | |
4549 | if (ret < 0) { | |
4550 | goto error; | |
4551 | } | |
4552 | ||
5eb91c98 | 4553 | /* Allocate context command to process the client request */ |
ba7f0ae5 | 4554 | cmd_ctx = zmalloc(sizeof(struct command_ctx)); |
5eb91c98 | 4555 | if (cmd_ctx == NULL) { |
76d7553f | 4556 | PERROR("zmalloc cmd_ctx"); |
1d4b027a | 4557 | goto error; |
5eb91c98 | 4558 | } |
1d4b027a | 4559 | |
5eb91c98 | 4560 | /* Allocate data buffer for reception */ |
ba7f0ae5 | 4561 | cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); |
5eb91c98 | 4562 | if (cmd_ctx->lsm == NULL) { |
76d7553f | 4563 | PERROR("zmalloc cmd_ctx->lsm"); |
5eb91c98 DG |
4564 | goto error; |
4565 | } | |
1d4b027a | 4566 | |
5eb91c98 DG |
4567 | cmd_ctx->llm = NULL; |
4568 | cmd_ctx->session = NULL; | |
1d4b027a | 4569 | |
840cb59c | 4570 | health_code_update(); |
44a5e5eb | 4571 | |
5eb91c98 DG |
4572 | /* |
4573 | * Data is received from the lttng client. The struct | |
4574 | * lttcomm_session_msg (lsm) contains the command and data request of | |
4575 | * the client. | |
4576 | */ | |
4577 | DBG("Receiving data from client ..."); | |
be040666 DG |
4578 | ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm, |
4579 | sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); | |
5eb91c98 DG |
4580 | if (ret <= 0) { |
4581 | DBG("Nothing recv() from client... continuing"); | |
76d7553f MD |
4582 | ret = close(sock); |
4583 | if (ret) { | |
4584 | PERROR("close"); | |
4585 | } | |
4586 | sock = -1; | |
a2c0da86 | 4587 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4588 | continue; |
4589 | } | |
1d4b027a | 4590 | |
840cb59c | 4591 | health_code_update(); |
44a5e5eb | 4592 | |
5eb91c98 DG |
4593 | // TODO: Validate cmd_ctx including sanity check for |
4594 | // security purpose. | |
f7776ea7 | 4595 | |
f6a9efaa | 4596 | rcu_thread_online(); |
5eb91c98 DG |
4597 | /* |
4598 | * This function dispatch the work to the kernel or userspace tracer | |
4599 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
4600 | * informations for the client. The command context struct contains | |
4601 | * everything this function may needs. | |
4602 | */ | |
53a80697 | 4603 | ret = process_client_msg(cmd_ctx, sock, &sock_error); |
f6a9efaa | 4604 | rcu_thread_offline(); |
5eb91c98 | 4605 | if (ret < 0) { |
36134aa1 DG |
4606 | ret = close(sock); |
4607 | if (ret) { | |
4608 | PERROR("close"); | |
53a80697 | 4609 | } |
36134aa1 | 4610 | sock = -1; |
bbd973c2 | 4611 | /* |
5eb91c98 | 4612 | * TODO: Inform client somehow of the fatal error. At |
ba7f0ae5 | 4613 | * this point, ret < 0 means that a zmalloc failed |
53a80697 MD |
4614 | * (ENOMEM). Error detected but still accept |
4615 | * command, unless a socket error has been | |
4616 | * detected. | |
bbd973c2 | 4617 | */ |
bbd973c2 | 4618 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
4619 | continue; |
4620 | } | |
d6e4fca4 | 4621 | |
a503e1ef JG |
4622 | cmd_completion_handler = cmd_pop_completion_handler(); |
4623 | if (cmd_completion_handler) { | |
4624 | enum lttng_error_code completion_code; | |
4625 | ||
4626 | completion_code = cmd_completion_handler->run( | |
4627 | cmd_completion_handler->data); | |
4628 | if (completion_code != LTTNG_OK) { | |
4629 | clean_command_ctx(&cmd_ctx); | |
4630 | continue; | |
4631 | } | |
4632 | } | |
4633 | ||
840cb59c | 4634 | health_code_update(); |
44a5e5eb | 4635 | |
a1ac8b97 | 4636 | DBG("Sending response (size: %d, retcode: %s (%d))", |
54d01ffb | 4637 | cmd_ctx->lttng_msg_size, |
a1ac8b97 JG |
4638 | lttng_strerror(-cmd_ctx->llm->ret_code), |
4639 | cmd_ctx->llm->ret_code); | |
54d01ffb | 4640 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
4641 | if (ret < 0) { |
4642 | ERR("Failed to send data back to client"); | |
bbd973c2 | 4643 | } |
1d4b027a | 4644 | |
5eb91c98 | 4645 | /* End of transmission */ |
76d7553f MD |
4646 | ret = close(sock); |
4647 | if (ret) { | |
4648 | PERROR("close"); | |
4649 | } | |
4650 | sock = -1; | |
be040666 DG |
4651 | |
4652 | clean_command_ctx(&cmd_ctx); | |
44a5e5eb | 4653 | |
840cb59c | 4654 | health_code_update(); |
273ea72c DG |
4655 | } |
4656 | ||
139ac872 | 4657 | exit: |
5eb91c98 | 4658 | error: |
35df2755 CB |
4659 | if (sock >= 0) { |
4660 | ret = close(sock); | |
4661 | if (ret) { | |
4662 | PERROR("close"); | |
4663 | } | |
139ac872 | 4664 | } |
44a5e5eb | 4665 | |
35df2755 CB |
4666 | lttng_poll_clean(&events); |
4667 | clean_command_ctx(&cmd_ctx); | |
4668 | ||
4669 | error_listen: | |
4670 | error_create_poll: | |
e6142f2e | 4671 | unlink(config.client_unix_sock_path.value); |
76d7553f MD |
4672 | if (client_sock >= 0) { |
4673 | ret = close(client_sock); | |
4674 | if (ret) { | |
4675 | PERROR("close"); | |
4676 | } | |
a4b35e07 | 4677 | } |
35df2755 CB |
4678 | |
4679 | if (err) { | |
840cb59c | 4680 | health_error(); |
35df2755 | 4681 | ERR("Health error occurred in %s", __func__); |
a4b35e07 | 4682 | } |
273ea72c | 4683 | |
8782cc74 | 4684 | health_unregister(health_sessiond); |
35df2755 CB |
4685 | |
4686 | DBG("Client thread dying"); | |
f6a9efaa DG |
4687 | |
4688 | rcu_unregister_thread(); | |
4a15001e MD |
4689 | |
4690 | /* | |
4691 | * Since we are creating the consumer threads, we own them, so we need | |
4692 | * to join them before our thread exits. | |
4693 | */ | |
4694 | ret = join_consumer_thread(&kconsumer_data); | |
4695 | if (ret) { | |
4696 | errno = ret; | |
4697 | PERROR("join_consumer"); | |
4698 | } | |
4699 | ||
4700 | ret = join_consumer_thread(&ustconsumer32_data); | |
4701 | if (ret) { | |
4702 | errno = ret; | |
4703 | PERROR("join_consumer ust32"); | |
4704 | } | |
4705 | ||
4706 | ret = join_consumer_thread(&ustconsumer64_data); | |
4707 | if (ret) { | |
4708 | errno = ret; | |
4709 | PERROR("join_consumer ust64"); | |
4710 | } | |
99d688f2 JG |
4711 | |
4712 | /* Set state as non-running. */ | |
4713 | sessiond_set_client_thread_state(false); | |
1d4b027a DG |
4714 | return NULL; |
4715 | } | |
4716 | ||
c0232ea5 JG |
4717 | static int string_match(const char *str1, const char *str2) |
4718 | { | |
4719 | return (str1 && str2) && !strcmp(str1, str2); | |
4720 | } | |
4721 | ||
fac6795d | 4722 | /* |
26296c48 JG |
4723 | * Take an option from the getopt output and set it in the right variable to be |
4724 | * used later. | |
4725 | * | |
4726 | * Return 0 on success else a negative value. | |
fac6795d | 4727 | */ |
26296c48 | 4728 | static int set_option(int opt, const char *arg, const char *optname) |
fac6795d | 4729 | { |
26296c48 | 4730 | int ret = 0; |
fac6795d | 4731 | |
c0232ea5 | 4732 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
4733 | if (!arg || *arg == '\0') { |
4734 | ret = -EINVAL; | |
4735 | goto end; | |
4736 | } | |
e8fa9fb0 MD |
4737 | if (lttng_is_setuid_setgid()) { |
4738 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4739 | "-c, --client-sock"); | |
4740 | } else { | |
e6142f2e JG |
4741 | config_string_set(&config.client_unix_sock_path, |
4742 | strdup(arg)); | |
4743 | if (!config.client_unix_sock_path.value) { | |
4744 | ret = -ENOMEM; | |
4745 | PERROR("strdup"); | |
4746 | } | |
e8fa9fb0 | 4747 | } |
c0232ea5 | 4748 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
4749 | if (!arg || *arg == '\0') { |
4750 | ret = -EINVAL; | |
4751 | goto end; | |
4752 | } | |
e8fa9fb0 MD |
4753 | if (lttng_is_setuid_setgid()) { |
4754 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4755 | "-a, --apps-sock"); | |
4756 | } else { | |
e6142f2e JG |
4757 | config_string_set(&config.apps_unix_sock_path, |
4758 | strdup(arg)); | |
4759 | if (!config.apps_unix_sock_path.value) { | |
4760 | ret = -ENOMEM; | |
4761 | PERROR("strdup"); | |
4762 | } | |
e8fa9fb0 | 4763 | } |
c0232ea5 | 4764 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 4765 | config.daemonize = true; |
c0232ea5 | 4766 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 4767 | config.background = true; |
c0232ea5 | 4768 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
4769 | if (!arg || *arg == '\0') { |
4770 | ret = -EINVAL; | |
4771 | goto end; | |
4772 | } | |
e8fa9fb0 MD |
4773 | if (lttng_is_setuid_setgid()) { |
4774 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4775 | "-g, --group"); | |
4776 | } else { | |
e6142f2e JG |
4777 | config_string_set(&config.tracing_group_name, |
4778 | strdup(arg)); | |
4779 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 4780 | ret = -ENOMEM; |
e6142f2e | 4781 | PERROR("strdup"); |
e8fa9fb0 | 4782 | } |
db322c4d | 4783 | } |
c0232ea5 | 4784 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 4785 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 4786 | if (ret) { |
4fc83d94 | 4787 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
4788 | perror("exec"); |
4789 | } | |
4790 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 4791 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
4792 | fprintf(stdout, "%s\n", VERSION); |
4793 | exit(EXIT_SUCCESS); | |
c0232ea5 | 4794 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 4795 | config.sig_parent = true; |
c0232ea5 | 4796 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
4797 | if (!arg || *arg == '\0') { |
4798 | ret = -EINVAL; | |
4799 | goto end; | |
4800 | } | |
e8fa9fb0 MD |
4801 | if (lttng_is_setuid_setgid()) { |
4802 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4803 | "--kconsumerd-err-sock"); | |
4804 | } else { | |
e6142f2e JG |
4805 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
4806 | strdup(arg)); | |
4807 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
4808 | ret = -ENOMEM; | |
4809 | PERROR("strdup"); | |
4810 | } | |
e8fa9fb0 | 4811 | } |
c0232ea5 | 4812 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
4813 | if (!arg || *arg == '\0') { |
4814 | ret = -EINVAL; | |
4815 | goto end; | |
4816 | } | |
e8fa9fb0 MD |
4817 | if (lttng_is_setuid_setgid()) { |
4818 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4819 | "--kconsumerd-cmd-sock"); | |
4820 | } else { | |
e6142f2e JG |
4821 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
4822 | strdup(arg)); | |
4823 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
4824 | ret = -ENOMEM; | |
4825 | PERROR("strdup"); | |
4826 | } | |
e8fa9fb0 | 4827 | } |
c0232ea5 | 4828 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
4829 | if (!arg || *arg == '\0') { |
4830 | ret = -EINVAL; | |
4831 | goto end; | |
4832 | } | |
e8fa9fb0 MD |
4833 | if (lttng_is_setuid_setgid()) { |
4834 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4835 | "--ustconsumerd64-err-sock"); | |
4836 | } else { | |
e6142f2e JG |
4837 | config_string_set(&config.consumerd64_err_unix_sock_path, |
4838 | strdup(arg)); | |
4839 | if (!config.consumerd64_err_unix_sock_path.value) { | |
4840 | ret = -ENOMEM; | |
4841 | PERROR("strdup"); | |
4842 | } | |
e8fa9fb0 | 4843 | } |
c0232ea5 | 4844 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
4845 | if (!arg || *arg == '\0') { |
4846 | ret = -EINVAL; | |
4847 | goto end; | |
4848 | } | |
e8fa9fb0 MD |
4849 | if (lttng_is_setuid_setgid()) { |
4850 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4851 | "--ustconsumerd64-cmd-sock"); | |
4852 | } else { | |
e6142f2e JG |
4853 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
4854 | strdup(arg)); | |
4855 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
4856 | ret = -ENOMEM; | |
4857 | PERROR("strdup"); | |
4858 | } | |
e8fa9fb0 | 4859 | } |
c0232ea5 | 4860 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
4861 | if (!arg || *arg == '\0') { |
4862 | ret = -EINVAL; | |
4863 | goto end; | |
4864 | } | |
e8fa9fb0 MD |
4865 | if (lttng_is_setuid_setgid()) { |
4866 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4867 | "--ustconsumerd32-err-sock"); | |
4868 | } else { | |
e6142f2e JG |
4869 | config_string_set(&config.consumerd32_err_unix_sock_path, |
4870 | strdup(arg)); | |
4871 | if (!config.consumerd32_err_unix_sock_path.value) { | |
4872 | ret = -ENOMEM; | |
4873 | PERROR("strdup"); | |
4874 | } | |
e8fa9fb0 | 4875 | } |
c0232ea5 | 4876 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
4877 | if (!arg || *arg == '\0') { |
4878 | ret = -EINVAL; | |
4879 | goto end; | |
4880 | } | |
e8fa9fb0 MD |
4881 | if (lttng_is_setuid_setgid()) { |
4882 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4883 | "--ustconsumerd32-cmd-sock"); | |
4884 | } else { | |
e6142f2e JG |
4885 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
4886 | strdup(arg)); | |
4887 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
4888 | ret = -ENOMEM; | |
4889 | PERROR("strdup"); | |
4890 | } | |
e8fa9fb0 | 4891 | } |
c0232ea5 | 4892 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 4893 | config.no_kernel = true; |
c0232ea5 | 4894 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 4895 | config.quiet = true; |
c0232ea5 | 4896 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
4897 | /* Verbose level can increase using multiple -v */ |
4898 | if (arg) { | |
13755a18 | 4899 | /* Value obtained from config file */ |
e6142f2e | 4900 | config.verbose = config_parse_value(arg); |
26296c48 | 4901 | } else { |
13755a18 | 4902 | /* -v used on command line */ |
e6142f2e | 4903 | config.verbose++; |
26296c48 | 4904 | } |
13755a18 | 4905 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
4906 | config.verbose = config.verbose < 0 ? 0 : |
4907 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 4908 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 4909 | if (arg) { |
e6142f2e | 4910 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 4911 | } else { |
e6142f2e | 4912 | config.verbose_consumer++; |
26296c48 | 4913 | } |
c0232ea5 | 4914 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
4915 | if (!arg || *arg == '\0') { |
4916 | ret = -EINVAL; | |
4917 | goto end; | |
4918 | } | |
e8fa9fb0 MD |
4919 | if (lttng_is_setuid_setgid()) { |
4920 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4921 | "--consumerd32-path"); | |
4922 | } else { | |
e6142f2e JG |
4923 | config_string_set(&config.consumerd32_bin_path, |
4924 | strdup(arg)); | |
4925 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
4926 | PERROR("strdup"); |
4927 | ret = -ENOMEM; | |
4928 | } | |
db322c4d | 4929 | } |
c0232ea5 | 4930 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
4931 | if (!arg || *arg == '\0') { |
4932 | ret = -EINVAL; | |
4933 | goto end; | |
4934 | } | |
e8fa9fb0 MD |
4935 | if (lttng_is_setuid_setgid()) { |
4936 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4937 | "--consumerd32-libdir"); | |
4938 | } else { | |
e6142f2e JG |
4939 | config_string_set(&config.consumerd32_lib_dir, |
4940 | strdup(arg)); | |
4941 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
4942 | PERROR("strdup"); |
4943 | ret = -ENOMEM; | |
4944 | } | |
db322c4d | 4945 | } |
c0232ea5 | 4946 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
4947 | if (!arg || *arg == '\0') { |
4948 | ret = -EINVAL; | |
4949 | goto end; | |
4950 | } | |
e8fa9fb0 MD |
4951 | if (lttng_is_setuid_setgid()) { |
4952 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4953 | "--consumerd64-path"); | |
4954 | } else { | |
e6142f2e JG |
4955 | config_string_set(&config.consumerd64_bin_path, |
4956 | strdup(arg)); | |
4957 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
4958 | PERROR("strdup"); |
4959 | ret = -ENOMEM; | |
4960 | } | |
db322c4d | 4961 | } |
c0232ea5 | 4962 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
4963 | if (!arg || *arg == '\0') { |
4964 | ret = -EINVAL; | |
4965 | goto end; | |
4966 | } | |
e8fa9fb0 MD |
4967 | if (lttng_is_setuid_setgid()) { |
4968 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4969 | "--consumerd64-libdir"); | |
4970 | } else { | |
e6142f2e JG |
4971 | config_string_set(&config.consumerd64_lib_dir, |
4972 | strdup(arg)); | |
4973 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
4974 | PERROR("strdup"); |
4975 | ret = -ENOMEM; | |
4976 | } | |
db322c4d | 4977 | } |
c0232ea5 | 4978 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
4979 | if (!arg || *arg == '\0') { |
4980 | ret = -EINVAL; | |
4981 | goto end; | |
4982 | } | |
e8fa9fb0 MD |
4983 | if (lttng_is_setuid_setgid()) { |
4984 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
4985 | "-p, --pidfile"); | |
4986 | } else { | |
e6142f2e JG |
4987 | config_string_set(&config.pid_file_path, strdup(arg)); |
4988 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
4989 | PERROR("strdup"); |
4990 | ret = -ENOMEM; | |
4991 | } | |
db322c4d | 4992 | } |
c0232ea5 | 4993 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
4994 | if (!arg || *arg == '\0') { |
4995 | ret = -EINVAL; | |
4996 | goto end; | |
4997 | } | |
e8fa9fb0 MD |
4998 | if (lttng_is_setuid_setgid()) { |
4999 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5000 | "--agent-tcp-port"); | |
5001 | } else { | |
5002 | unsigned long v; | |
4d076222 | 5003 | |
e8fa9fb0 MD |
5004 | errno = 0; |
5005 | v = strtoul(arg, NULL, 0); | |
5006 | if (errno != 0 || !isdigit(arg[0])) { | |
5007 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
5008 | return -1; | |
5009 | } | |
5010 | if (v == 0 || v >= 65535) { | |
5011 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
5012 | return -1; | |
5013 | } | |
2288467f JG |
5014 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
5015 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 5016 | } |
c0232ea5 | 5017 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
5018 | if (!arg || *arg == '\0') { |
5019 | ret = -EINVAL; | |
5020 | goto end; | |
5021 | } | |
e8fa9fb0 MD |
5022 | if (lttng_is_setuid_setgid()) { |
5023 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5024 | "-l, --load"); | |
5025 | } else { | |
e6142f2e JG |
5026 | config_string_set(&config.load_session_path, strdup(arg)); |
5027 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
5028 | PERROR("strdup"); |
5029 | ret = -ENOMEM; | |
5030 | } | |
ef367a93 | 5031 | } |
c0232ea5 | 5032 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
5033 | if (!arg || *arg == '\0') { |
5034 | ret = -EINVAL; | |
5035 | goto end; | |
5036 | } | |
e8fa9fb0 MD |
5037 | if (lttng_is_setuid_setgid()) { |
5038 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5039 | "--kmod-probes"); | |
5040 | } else { | |
e6142f2e JG |
5041 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
5042 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
5043 | PERROR("strdup"); |
5044 | ret = -ENOMEM; | |
5045 | } | |
c9d42407 | 5046 | } |
c0232ea5 | 5047 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
5048 | if (!arg || *arg == '\0') { |
5049 | ret = -EINVAL; | |
5050 | goto end; | |
5051 | } | |
e8fa9fb0 MD |
5052 | if (lttng_is_setuid_setgid()) { |
5053 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5054 | "--extra-kmod-probes"); | |
5055 | } else { | |
e6142f2e JG |
5056 | config_string_set(&config.kmod_extra_probes_list, |
5057 | strdup(arg)); | |
5058 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
5059 | PERROR("strdup"); |
5060 | ret = -ENOMEM; | |
5061 | } | |
db322c4d | 5062 | } |
c0232ea5 JG |
5063 | } else if (string_match(optname, "config") || opt == 'f') { |
5064 | /* This is handled in set_options() thus silent skip. */ | |
5065 | goto end; | |
5066 | } else { | |
26296c48 JG |
5067 | /* Unknown option or other error. |
5068 | * Error is printed by getopt, just return */ | |
5069 | ret = -1; | |
5070 | } | |
5071 | ||
f3dd7ce7 | 5072 | end: |
c5d350b2 JG |
5073 | if (ret == -EINVAL) { |
5074 | const char *opt_name = "unknown"; | |
5075 | int i; | |
5076 | ||
5077 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
5078 | i++) { | |
5079 | if (opt == long_options[i].val) { | |
5080 | opt_name = long_options[i].name; | |
5081 | break; | |
5082 | } | |
5083 | } | |
5084 | ||
5085 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
5086 | opt_name); | |
5087 | } | |
f3dd7ce7 | 5088 | |
26296c48 JG |
5089 | return ret; |
5090 | } | |
5091 | ||
5092 | /* | |
5093 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 5094 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
5095 | * return value conventions. |
5096 | */ | |
5097 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
5098 | { | |
5099 | int ret = 0, i; | |
5100 | ||
5101 | if (!entry || !entry->name || !entry->value) { | |
5102 | ret = -EINVAL; | |
5103 | goto end; | |
5104 | } | |
5105 | ||
5106 | /* Check if the option is to be ignored */ | |
5107 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
5108 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
5109 | goto end; | |
5110 | } | |
5111 | } | |
5112 | ||
5113 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
5114 | i++) { | |
5115 | ||
5116 | /* Ignore if not fully matched. */ | |
5117 | if (strcmp(entry->name, long_options[i].name)) { | |
5118 | continue; | |
5119 | } | |
5120 | ||
5121 | /* | |
5122 | * If the option takes no argument on the command line, we have to | |
5123 | * check if the value is "true". We support non-zero numeric values, | |
5124 | * true, on and yes. | |
5125 | */ | |
5126 | if (!long_options[i].has_arg) { | |
5127 | ret = config_parse_value(entry->value); | |
5128 | if (ret <= 0) { | |
5129 | if (ret) { | |
5130 | WARN("Invalid configuration value \"%s\" for option %s", | |
5131 | entry->value, entry->name); | |
5132 | } | |
5133 | /* False, skip boolean config option. */ | |
5134 | goto end; | |
4d076222 | 5135 | } |
26296c48 JG |
5136 | } |
5137 | ||
5138 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
5139 | goto end; | |
5140 | } | |
5141 | ||
5142 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
5143 | ||
5144 | end: | |
5145 | return ret; | |
5146 | } | |
5147 | ||
5148 | /* | |
5149 | * daemon configuration loading and argument parsing | |
5150 | */ | |
5151 | static int set_options(int argc, char **argv) | |
5152 | { | |
5153 | int ret = 0, c = 0, option_index = 0; | |
5154 | int orig_optopt = optopt, orig_optind = optind; | |
5155 | char *optstring; | |
5156 | const char *config_path = NULL; | |
5157 | ||
5158 | optstring = utils_generate_optstring(long_options, | |
5159 | sizeof(long_options) / sizeof(struct option)); | |
5160 | if (!optstring) { | |
5161 | ret = -ENOMEM; | |
5162 | goto end; | |
5163 | } | |
5164 | ||
5165 | /* Check for the --config option */ | |
5166 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
5167 | &option_index)) != -1) { | |
5168 | if (c == '?') { | |
5169 | ret = -EINVAL; | |
5170 | goto end; | |
5171 | } else if (c != 'f') { | |
5172 | /* if not equal to --config option. */ | |
5173 | continue; | |
5174 | } | |
5175 | ||
e8fa9fb0 MD |
5176 | if (lttng_is_setuid_setgid()) { |
5177 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
5178 | "-f, --config"); | |
5179 | } else { | |
5180 | config_path = utils_expand_path(optarg); | |
5181 | if (!config_path) { | |
5182 | ERR("Failed to resolve path: %s", optarg); | |
5183 | } | |
26296c48 JG |
5184 | } |
5185 | } | |
5186 | ||
5187 | ret = config_get_section_entries(config_path, config_section_name, | |
5188 | config_entry_handler, NULL); | |
5189 | if (ret) { | |
5190 | if (ret > 0) { | |
5191 | ERR("Invalid configuration option at line %i", ret); | |
5192 | ret = -1; | |
5193 | } | |
5194 | goto end; | |
5195 | } | |
5196 | ||
5197 | /* Reset getopt's global state */ | |
5198 | optopt = orig_optopt; | |
5199 | optind = orig_optind; | |
5200 | while (1) { | |
c0232ea5 JG |
5201 | option_index = -1; |
5202 | /* | |
5203 | * getopt_long() will not set option_index if it encounters a | |
5204 | * short option. | |
5205 | */ | |
5206 | c = getopt_long(argc, argv, optstring, long_options, | |
5207 | &option_index); | |
26296c48 | 5208 | if (c == -1) { |
4d076222 DG |
5209 | break; |
5210 | } | |
26296c48 | 5211 | |
c0232ea5 JG |
5212 | /* |
5213 | * Pass NULL as the long option name if popt left the index | |
5214 | * unset. | |
5215 | */ | |
5216 | ret = set_option(c, optarg, | |
5217 | option_index < 0 ? NULL : | |
5218 | long_options[option_index].name); | |
26296c48 JG |
5219 | if (ret < 0) { |
5220 | break; | |
fac6795d DG |
5221 | } |
5222 | } | |
5223 | ||
26296c48 JG |
5224 | end: |
5225 | free(optstring); | |
5226 | return ret; | |
fac6795d DG |
5227 | } |
5228 | ||
5229 | /* | |
d063d709 | 5230 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
5231 | * apps_sock - The communication socket for all UST apps. |
5232 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 5233 | */ |
cf3af59e | 5234 | static int init_daemon_socket(void) |
fac6795d DG |
5235 | { |
5236 | int ret = 0; | |
5237 | mode_t old_umask; | |
5238 | ||
5239 | old_umask = umask(0); | |
5240 | ||
5241 | /* Create client tool unix socket */ | |
e6142f2e | 5242 | client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value); |
d6f42150 | 5243 | if (client_sock < 0) { |
e6142f2e | 5244 | ERR("Create unix sock failed: %s", config.client_unix_sock_path.value); |
fac6795d DG |
5245 | ret = -1; |
5246 | goto end; | |
5247 | } | |
5248 | ||
b662582b DG |
5249 | /* Set the cloexec flag */ |
5250 | ret = utils_set_fd_cloexec(client_sock); | |
5251 | if (ret < 0) { | |
5252 | ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). " | |
5253 | "Continuing but note that the consumer daemon will have a " | |
5254 | "reference to this socket on exec()", client_sock); | |
5255 | } | |
5256 | ||
fac6795d | 5257 | /* File permission MUST be 660 */ |
e6142f2e | 5258 | ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
fac6795d | 5259 | if (ret < 0) { |
e6142f2e | 5260 | ERR("Set file permissions failed: %s", config.client_unix_sock_path.value); |
76d7553f | 5261 | PERROR("chmod"); |
fac6795d DG |
5262 | goto end; |
5263 | } | |
5264 | ||
5265 | /* Create the application unix socket */ | |
e6142f2e | 5266 | apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value); |
d6f42150 | 5267 | if (apps_sock < 0) { |
e6142f2e | 5268 | ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value); |
fac6795d DG |
5269 | ret = -1; |
5270 | goto end; | |
5271 | } | |
5272 | ||
b662582b DG |
5273 | /* Set the cloexec flag */ |
5274 | ret = utils_set_fd_cloexec(apps_sock); | |
5275 | if (ret < 0) { | |
5276 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
5277 | "Continuing but note that the consumer daemon will have a " | |
5278 | "reference to this socket on exec()", apps_sock); | |
5279 | } | |
5280 | ||
d6f42150 | 5281 | /* File permission MUST be 666 */ |
e6142f2e | 5282 | ret = chmod(config.apps_unix_sock_path.value, |
54d01ffb | 5283 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
fac6795d | 5284 | if (ret < 0) { |
e6142f2e | 5285 | ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value); |
76d7553f | 5286 | PERROR("chmod"); |
fac6795d DG |
5287 | goto end; |
5288 | } | |
5289 | ||
b662582b DG |
5290 | DBG3("Session daemon client socket %d and application socket %d created", |
5291 | client_sock, apps_sock); | |
5292 | ||
fac6795d DG |
5293 | end: |
5294 | umask(old_umask); | |
5295 | return ret; | |
5296 | } | |
5297 | ||
f472090a JG |
5298 | /* |
5299 | * Create lockfile using the rundir and return its fd. | |
5300 | */ | |
5301 | static int create_lockfile(void) | |
5302 | { | |
5303 | return utils_create_lock_file(config.lock_file_path.value); | |
5304 | } | |
5305 | ||
fac6795d | 5306 | /* |
54d01ffb DG |
5307 | * Check if the global socket is available, and if a daemon is answering at the |
5308 | * other side. If yes, error is returned. | |
f472090a JG |
5309 | * |
5310 | * Also attempts to create and hold the lock file. | |
fac6795d | 5311 | */ |
cf3af59e | 5312 | static int check_existing_daemon(void) |
fac6795d | 5313 | { |
f472090a JG |
5314 | int ret = 0; |
5315 | ||
7d8234d9 | 5316 | /* Is there anybody out there ? */ |
099e26bd | 5317 | if (lttng_session_daemon_alive()) { |
f472090a JG |
5318 | ret = -EEXIST; |
5319 | goto end; | |
099e26bd | 5320 | } |
b09c7c76 | 5321 | |
f472090a JG |
5322 | lockfile_fd = create_lockfile(); |
5323 | if (lockfile_fd < 0) { | |
5324 | ret = -EEXIST; | |
5325 | goto end; | |
5326 | } | |
5327 | end: | |
5328 | return ret; | |
5329 | } | |
5330 | ||
5331 | static void sessiond_cleanup_lock_file(void) | |
5332 | { | |
5333 | int ret; | |
5334 | ||
5335 | /* | |
5336 | * Cleanup lock file by deleting it and finaly closing it which will | |
5337 | * release the file system lock. | |
5338 | */ | |
5339 | if (lockfile_fd >= 0) { | |
5340 | ret = remove(config.lock_file_path.value); | |
5341 | if (ret < 0) { | |
5342 | PERROR("remove lock file"); | |
5343 | } | |
5344 | ret = close(lockfile_fd); | |
5345 | if (ret < 0) { | |
5346 | PERROR("close lock file"); | |
5347 | } | |
5348 | } | |
fac6795d DG |
5349 | } |
5350 | ||
fac6795d | 5351 | /* |
d063d709 | 5352 | * Set the tracing group gid onto the client socket. |
5e16da05 | 5353 | * |
d063d709 | 5354 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 5355 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 5356 | */ |
be040666 | 5357 | static int set_permissions(char *rundir) |
fac6795d DG |
5358 | { |
5359 | int ret; | |
996b65c8 | 5360 | gid_t gid; |
fac6795d | 5361 | |
e6142f2e | 5362 | gid = utils_get_group_id(config.tracing_group_name.value); |
fac6795d | 5363 | |
d6f42150 | 5364 | /* Set lttng run dir */ |
be040666 | 5365 | ret = chown(rundir, 0, gid); |
d6f42150 | 5366 | if (ret < 0) { |
be040666 | 5367 | ERR("Unable to set group on %s", rundir); |
76d7553f | 5368 | PERROR("chown"); |
d6f42150 DG |
5369 | } |
5370 | ||
6c71277b MD |
5371 | /* |
5372 | * Ensure all applications and tracing group can search the run | |
5373 | * dir. Allow everyone to read the directory, since it does not | |
5374 | * buy us anything to hide its content. | |
5375 | */ | |
5376 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
5377 | if (ret < 0) { |
5378 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 5379 | PERROR("chmod"); |
d1613cf5 JN |
5380 | } |
5381 | ||
d6f42150 | 5382 | /* lttng client socket path */ |
e6142f2e | 5383 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 5384 | if (ret < 0) { |
e6142f2e | 5385 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 5386 | PERROR("chown"); |
d6f42150 DG |
5387 | } |
5388 | ||
3bd1e081 | 5389 | /* kconsumer error socket path */ |
6c71277b | 5390 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 5391 | if (ret < 0) { |
3bd1e081 | 5392 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 5393 | PERROR("chown"); |
3bd1e081 MD |
5394 | } |
5395 | ||
7753dea8 | 5396 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 5397 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
5398 | if (ret < 0) { |
5399 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 5400 | PERROR("chown"); |
7753dea8 MD |
5401 | } |
5402 | ||
5403 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 5404 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 5405 | if (ret < 0) { |
7753dea8 | 5406 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 5407 | PERROR("chown"); |
fac6795d DG |
5408 | } |
5409 | ||
d6f42150 | 5410 | DBG("All permissions are set"); |
e07ae692 | 5411 | |
fac6795d DG |
5412 | return ret; |
5413 | } | |
5414 | ||
d6f42150 | 5415 | /* |
d063d709 | 5416 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 5417 | */ |
e6142f2e | 5418 | static int create_lttng_rundir(void) |
d6f42150 DG |
5419 | { |
5420 | int ret; | |
5421 | ||
e6142f2e | 5422 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 5423 | |
e6142f2e | 5424 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 5425 | if (ret < 0) { |
b1f11e69 | 5426 | if (errno != EEXIST) { |
e6142f2e | 5427 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
5428 | goto error; |
5429 | } else { | |
5430 | ret = 0; | |
5431 | } | |
d6f42150 DG |
5432 | } |
5433 | ||
5434 | error: | |
5435 | return ret; | |
5436 | } | |
5437 | ||
5438 | /* | |
e6142f2e | 5439 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 5440 | * session daemon. |
d6f42150 | 5441 | */ |
e6142f2e | 5442 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
5443 | { |
5444 | int ret; | |
e6142f2e | 5445 | char *path = NULL; |
d6f42150 | 5446 | |
6c71277b | 5447 | switch (consumer_data->type) { |
7753dea8 | 5448 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 5449 | path = config.kconsumerd_path.value; |
7753dea8 MD |
5450 | break; |
5451 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 5452 | path = config.consumerd64_path.value; |
7753dea8 MD |
5453 | break; |
5454 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 5455 | path = config.consumerd32_path.value; |
7753dea8 MD |
5456 | break; |
5457 | default: | |
5458 | ERR("Consumer type unknown"); | |
5459 | ret = -EINVAL; | |
5460 | goto error; | |
d6f42150 | 5461 | } |
e6142f2e | 5462 | assert(path); |
d6f42150 | 5463 | |
67e40797 DG |
5464 | DBG2("Creating consumer directory: %s", path); |
5465 | ||
6c71277b | 5466 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
5467 | if (ret < 0 && errno != EEXIST) { |
5468 | PERROR("mkdir"); | |
5469 | ERR("Failed to create %s", path); | |
5470 | goto error; | |
d6f42150 | 5471 | } |
6c71277b | 5472 | if (is_root) { |
e6142f2e | 5473 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
6c71277b MD |
5474 | if (ret < 0) { |
5475 | ERR("Unable to set group on %s", path); | |
5476 | PERROR("chown"); | |
5477 | goto error; | |
5478 | } | |
5479 | } | |
d6f42150 | 5480 | |
e6142f2e | 5481 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
5482 | consumer_data->err_sock = |
5483 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
5484 | if (consumer_data->err_sock < 0) { | |
5485 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
5486 | ret = -1; |
5487 | goto error; | |
5488 | } | |
5489 | ||
a24f05ab MD |
5490 | /* |
5491 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
5492 | * show must go on. | |
5493 | */ | |
5494 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
5495 | if (ret < 0) { | |
5496 | PERROR("utils_set_fd_cloexec"); | |
5497 | /* continue anyway */ | |
5498 | } | |
5499 | ||
d6f42150 | 5500 | /* File permission MUST be 660 */ |
3bd1e081 | 5501 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 5502 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 5503 | if (ret < 0) { |
3bd1e081 | 5504 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 5505 | PERROR("chmod"); |
d6f42150 DG |
5506 | goto error; |
5507 | } | |
5508 | ||
5509 | error: | |
5510 | return ret; | |
5511 | } | |
5512 | ||
fac6795d | 5513 | /* |
d063d709 | 5514 | * Signal handler for the daemon |
cf3af59e | 5515 | * |
54d01ffb DG |
5516 | * Simply stop all worker threads, leaving main() return gracefully after |
5517 | * joining all threads and calling cleanup(). | |
fac6795d DG |
5518 | */ |
5519 | static void sighandler(int sig) | |
5520 | { | |
5521 | switch (sig) { | |
cf3af59e | 5522 | case SIGINT: |
af87c45a | 5523 | DBG("SIGINT caught"); |
cf3af59e MD |
5524 | stop_threads(); |
5525 | break; | |
5526 | case SIGTERM: | |
af87c45a | 5527 | DBG("SIGTERM caught"); |
cf3af59e MD |
5528 | stop_threads(); |
5529 | break; | |
0bb7724a DG |
5530 | case SIGUSR1: |
5531 | CMM_STORE_SHARED(recv_child_signal, 1); | |
5532 | break; | |
cf3af59e MD |
5533 | default: |
5534 | break; | |
fac6795d | 5535 | } |
fac6795d DG |
5536 | } |
5537 | ||
5538 | /* | |
d063d709 | 5539 | * Setup signal handler for : |
1d4b027a | 5540 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 5541 | */ |
1d4b027a | 5542 | static int set_signal_handler(void) |
fac6795d | 5543 | { |
1d4b027a DG |
5544 | int ret = 0; |
5545 | struct sigaction sa; | |
5546 | sigset_t sigset; | |
fac6795d | 5547 | |
1d4b027a | 5548 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 5549 | PERROR("sigemptyset"); |
1d4b027a DG |
5550 | return ret; |
5551 | } | |
d6f42150 | 5552 | |
1d4b027a DG |
5553 | sa.sa_mask = sigset; |
5554 | sa.sa_flags = 0; | |
0072e5e2 MD |
5555 | |
5556 | sa.sa_handler = sighandler; | |
1d4b027a | 5557 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 5558 | PERROR("sigaction"); |
1d4b027a | 5559 | return ret; |
d6f42150 DG |
5560 | } |
5561 | ||
1d4b027a | 5562 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 5563 | PERROR("sigaction"); |
1d4b027a | 5564 | return ret; |
d6f42150 | 5565 | } |
aaf26714 | 5566 | |
0072e5e2 | 5567 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 5568 | PERROR("sigaction"); |
1d4b027a | 5569 | return ret; |
8c0faa1d DG |
5570 | } |
5571 | ||
0072e5e2 MD |
5572 | sa.sa_handler = SIG_IGN; |
5573 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
5574 | PERROR("sigaction"); |
5575 | return ret; | |
5576 | } | |
5577 | ||
5578 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
5579 | |
5580 | return ret; | |
fac6795d DG |
5581 | } |
5582 | ||
f3ed775e | 5583 | /* |
d063d709 | 5584 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 5585 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
5586 | */ |
5587 | static void set_ulimit(void) | |
5588 | { | |
5589 | int ret; | |
5590 | struct rlimit lim; | |
5591 | ||
514bb9f0 | 5592 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
5593 | lim.rlim_cur = 65535; |
5594 | lim.rlim_max = 65535; | |
5595 | ||
5596 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
5597 | if (ret < 0) { | |
76d7553f | 5598 | PERROR("failed to set open files limit"); |
f3ed775e DG |
5599 | } |
5600 | } | |
5601 | ||
4a15001e | 5602 | static int write_pidfile(void) |
35f90c40 | 5603 | { |
e6142f2e | 5604 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
5605 | } |
5606 | ||
e6142f2e JG |
5607 | static int set_clock_plugin_env(void) |
5608 | { | |
5609 | int ret = 0; | |
5610 | char *env_value = NULL; | |
cd9290dd | 5611 | |
e6142f2e JG |
5612 | if (!config.lttng_ust_clock_plugin.value) { |
5613 | goto end; | |
5614 | } | |
5615 | ||
5616 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
5617 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 5618 | if (ret < 0) { |
e6142f2e JG |
5619 | PERROR("asprintf"); |
5620 | goto end; | |
cd9290dd DG |
5621 | } |
5622 | ||
e6142f2e JG |
5623 | ret = putenv(env_value); |
5624 | if (ret) { | |
5625 | free(env_value); | |
5626 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
5627 | goto end; | |
5628 | } | |
cd9290dd | 5629 | |
e6142f2e JG |
5630 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
5631 | config.lttng_ust_clock_plugin.value); | |
5632 | end: | |
4a15001e | 5633 | return ret; |
ef367a93 JG |
5634 | } |
5635 | ||
99d688f2 JG |
5636 | static void destroy_all_sessions_and_wait(void) |
5637 | { | |
5638 | struct ltt_session *session, *tmp; | |
5639 | struct ltt_session_list *session_list; | |
5640 | ||
5641 | session_list = session_get_list(); | |
5642 | DBG("Initiating destruction of all sessions"); | |
5643 | ||
5644 | if (!session_list) { | |
5645 | return; | |
5646 | } | |
5647 | ||
5648 | /* | |
5649 | * Ensure that the client thread is no longer accepting new commands, | |
5650 | * which could cause new sessions to be created. | |
5651 | */ | |
5652 | sessiond_wait_client_thread_stopped(); | |
5653 | ||
5654 | session_lock_list(); | |
5655 | /* Initiate the destruction of all sessions. */ | |
5656 | cds_list_for_each_entry_safe(session, tmp, | |
5657 | &session_list->head, list) { | |
5658 | if (!session_get(session)) { | |
5659 | continue; | |
5660 | } | |
5661 | ||
5662 | session_lock(session); | |
5663 | if (session->destroyed) { | |
5664 | goto unlock_session; | |
5665 | } | |
5666 | (void) cmd_destroy_session(session, | |
5667 | notification_thread_handle); | |
5668 | unlock_session: | |
5669 | session_unlock(session); | |
5670 | session_put(session); | |
5671 | } | |
5672 | session_unlock_list(); | |
5673 | ||
5674 | /* Wait for the destruction of all sessions to complete. */ | |
5675 | DBG("Waiting for the destruction of all sessions to complete"); | |
5676 | session_list_wait_empty(); | |
5677 | DBG("Destruction of all sessions completed"); | |
5678 | } | |
5679 | ||
fac6795d DG |
5680 | /* |
5681 | * main | |
5682 | */ | |
5683 | int main(int argc, char **argv) | |
5684 | { | |
4a15001e | 5685 | int ret = 0, retval = 0; |
fac6795d | 5686 | void *status; |
e6142f2e | 5687 | const char *env_app_timeout; |
b3530820 JG |
5688 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
5689 | *ust64_channel_monitor_pipe = NULL, | |
5690 | *kernel_channel_monitor_pipe = NULL; | |
99611076 JG |
5691 | bool notification_thread_launched = false; |
5692 | bool rotation_thread_launched = false; | |
5693 | bool timer_thread_launched = false; | |
d086f507 JD |
5694 | struct timer_thread_parameters timer_thread_ctx; |
5695 | /* Queue of rotation jobs populated by the sessiond-timer. */ | |
5696 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
90936dcf | 5697 | sem_t notification_thread_ready; |
fac6795d | 5698 | |
335a95b7 MD |
5699 | init_kernel_workarounds(); |
5700 | ||
f6a9efaa DG |
5701 | rcu_register_thread(); |
5702 | ||
4a15001e MD |
5703 | if (set_signal_handler()) { |
5704 | retval = -1; | |
5705 | goto exit_set_signal_handler; | |
0bb7724a DG |
5706 | } |
5707 | ||
92816cc3 | 5708 | if (timer_signal_init()) { |
d086f507 JD |
5709 | retval = -1; |
5710 | goto exit_set_signal_handler; | |
5711 | } | |
5712 | ||
12744796 DG |
5713 | page_size = sysconf(_SC_PAGESIZE); |
5714 | if (page_size < 0) { | |
5715 | PERROR("sysconf _SC_PAGESIZE"); | |
5716 | page_size = LONG_MAX; | |
5717 | WARN("Fallback page size to %ld", page_size); | |
5718 | } | |
5719 | ||
e6142f2e JG |
5720 | ret = sessiond_config_init(&config); |
5721 | if (ret) { | |
5722 | retval = -1; | |
5723 | goto exit_set_signal_handler; | |
5724 | } | |
5725 | ||
2788b494 JR |
5726 | /* |
5727 | * Init config from environment variables. | |
5728 | * Command line option override env configuration per-doc. Do env first. | |
5729 | */ | |
5730 | sessiond_config_apply_env_config(&config); | |
5731 | ||
4a15001e MD |
5732 | /* |
5733 | * Parse arguments and load the daemon configuration file. | |
5734 | * | |
5735 | * We have an exit_options exit path to free memory reserved by | |
5736 | * set_options. This is needed because the rest of sessiond_cleanup() | |
5737 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
5738 | * depends on set_options. | |
5739 | */ | |
fac6795d | 5740 | progname = argv[0]; |
4a15001e MD |
5741 | if (set_options(argc, argv)) { |
5742 | retval = -1; | |
5743 | goto exit_options; | |
fac6795d DG |
5744 | } |
5745 | ||
e6142f2e JG |
5746 | /* |
5747 | * Resolve all paths received as arguments, configuration option, or | |
5748 | * through environment variable as absolute paths. This is necessary | |
5749 | * since daemonizing causes the sessiond's current working directory | |
5750 | * to '/'. | |
5751 | */ | |
5752 | ret = sessiond_config_resolve_paths(&config); | |
5753 | if (ret) { | |
5754 | goto exit_options; | |
5755 | } | |
5756 | ||
5757 | /* Apply config. */ | |
5758 | lttng_opt_verbose = config.verbose; | |
5759 | lttng_opt_quiet = config.quiet; | |
5760 | kconsumer_data.err_unix_sock_path = | |
5761 | config.kconsumerd_err_unix_sock_path.value; | |
5762 | kconsumer_data.cmd_unix_sock_path = | |
5763 | config.kconsumerd_cmd_unix_sock_path.value; | |
5764 | ustconsumer32_data.err_unix_sock_path = | |
5765 | config.consumerd32_err_unix_sock_path.value; | |
5766 | ustconsumer32_data.cmd_unix_sock_path = | |
5767 | config.consumerd32_cmd_unix_sock_path.value; | |
5768 | ustconsumer64_data.err_unix_sock_path = | |
5769 | config.consumerd64_err_unix_sock_path.value; | |
5770 | ustconsumer64_data.cmd_unix_sock_path = | |
5771 | config.consumerd64_cmd_unix_sock_path.value; | |
5772 | set_clock_plugin_env(); | |
5773 | ||
5774 | sessiond_config_log(&config); | |
5775 | ||
f472090a JG |
5776 | if (create_lttng_rundir()) { |
5777 | retval = -1; | |
5778 | goto exit_options; | |
5779 | } | |
5780 | ||
5781 | /* Abort launch if a session daemon is already running. */ | |
5782 | if (check_existing_daemon()) { | |
5783 | ERR("A session daemon is already running."); | |
5784 | retval = -1; | |
5785 | goto exit_options; | |
5786 | } | |
5787 | ||
fac6795d | 5788 | /* Daemonize */ |
e6142f2e | 5789 | if (config.daemonize || config.background) { |
ceed52b5 MD |
5790 | int i; |
5791 | ||
72dd7491 | 5792 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 5793 | !config.background); |
53094c05 | 5794 | if (ret < 0) { |
4a15001e MD |
5795 | retval = -1; |
5796 | goto exit_options; | |
53094c05 | 5797 | } |
0bb7724a | 5798 | |
ceed52b5 | 5799 | /* |
0bb7724a | 5800 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 5801 | * closed, in case we are called with more opened file |
f472090a | 5802 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
5803 | */ |
5804 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
5805 | if (i == lockfile_fd) { |
5806 | continue; | |
5807 | } | |
ceed52b5 MD |
5808 | (void) close(i); |
5809 | } | |
5810 | } | |
5811 | ||
7567352f MD |
5812 | if (run_as_create_worker(argv[0]) < 0) { |
5813 | goto exit_create_run_as_worker_cleanup; | |
5814 | } | |
5815 | ||
4a15001e MD |
5816 | /* |
5817 | * Starting from here, we can create threads. This needs to be after | |
5818 | * lttng_daemonize due to RCU. | |
5819 | */ | |
5820 | ||
5821 | /* | |
5822 | * Initialize the health check subsystem. This call should set the | |
5823 | * appropriate time values. | |
5824 | */ | |
5825 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
5826 | if (!health_sessiond) { | |
5827 | PERROR("health_app_create error"); | |
5828 | retval = -1; | |
5829 | goto exit_health_sessiond_cleanup; | |
5830 | } | |
5831 | ||
4a15001e | 5832 | /* Create thread to clean up RCU hash tables */ |
5e97de00 | 5833 | if (init_ht_cleanup_thread(&ht_cleanup_thread)) { |
4a15001e MD |
5834 | retval = -1; |
5835 | goto exit_ht_cleanup; | |
5836 | } | |
5837 | ||
ceed52b5 | 5838 | /* Create thread quit pipe */ |
a7333da7 | 5839 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e MD |
5840 | retval = -1; |
5841 | goto exit_init_data; | |
fac6795d DG |
5842 | } |
5843 | ||
5844 | /* Check if daemon is UID = 0 */ | |
5845 | is_root = !getuid(); | |
fac6795d | 5846 | if (is_root) { |
67e40797 | 5847 | /* Create global run dir with root access */ |
67e40797 | 5848 | |
b3530820 JG |
5849 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
5850 | if (!kernel_channel_monitor_pipe) { | |
5851 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
5852 | retval = -1; | |
5853 | goto exit_init_data; | |
5854 | } | |
5855 | kconsumer_data.channel_monitor_pipe = | |
5856 | lttng_pipe_release_writefd( | |
5857 | kernel_channel_monitor_pipe); | |
5858 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
5859 | retval = -1; | |
5860 | goto exit_init_data; | |
5861 | } | |
fac6795d DG |
5862 | } |
5863 | ||
5c827ce0 DG |
5864 | /* Set consumer initial state */ |
5865 | kernel_consumerd_state = CONSUMER_STOPPED; | |
5866 | ust_consumerd_state = CONSUMER_STOPPED; | |
5867 | ||
b3530820 JG |
5868 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
5869 | if (!ust32_channel_monitor_pipe) { | |
5870 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
5871 | retval = -1; | |
5872 | goto exit_init_data; | |
5873 | } | |
5874 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
5875 | ust32_channel_monitor_pipe); | |
5876 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
5877 | retval = -1; | |
5878 | goto exit_init_data; | |
5879 | } | |
62c43103 | 5880 | |
d086f507 | 5881 | /* |
92816cc3 JG |
5882 | * The rotation_thread_timer_queue structure is shared between the |
5883 | * sessiond timer thread and the rotation thread. The main thread keeps | |
5884 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 5885 | */ |
92816cc3 | 5886 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
5887 | if (!rotation_timer_queue) { |
5888 | retval = -1; | |
5889 | goto exit_init_data; | |
5890 | } | |
92816cc3 | 5891 | timer_thread_ctx.rotation_thread_job_queue = rotation_timer_queue; |
67e40797 | 5892 | |
b3530820 JG |
5893 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
5894 | if (!ust64_channel_monitor_pipe) { | |
5895 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
5896 | retval = -1; | |
5897 | goto exit_init_data; | |
5898 | } | |
5899 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
5900 | ust64_channel_monitor_pipe); | |
5901 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
5902 | retval = -1; | |
5903 | goto exit_init_data; | |
5904 | } | |
847177cd | 5905 | |
1427f9b2 DG |
5906 | /* |
5907 | * Init UST app hash table. Alloc hash table before this point since | |
5908 | * cleanup() can get called after that point. | |
5909 | */ | |
4a15001e | 5910 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 5911 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
5912 | retval = -1; |
5913 | goto exit_init_data; | |
5914 | } | |
1427f9b2 | 5915 | |
6a4e4039 JG |
5916 | /* |
5917 | * Initialize agent app hash table. We allocate the hash table here | |
5918 | * since cleanup() can get called after this point. | |
5919 | */ | |
5920 | if (agent_app_ht_alloc()) { | |
5921 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
5922 | retval = -1; |
5923 | goto exit_init_data; | |
f20baf8e DG |
5924 | } |
5925 | ||
a88df331 DG |
5926 | /* |
5927 | * These actions must be executed as root. We do that *after* setting up | |
5928 | * the sockets path because we MUST make the check for another daemon using | |
5929 | * those paths *before* trying to set the kernel consumer sockets and init | |
5930 | * kernel tracer. | |
5931 | */ | |
5932 | if (is_root) { | |
e6142f2e | 5933 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e MD |
5934 | retval = -1; |
5935 | goto exit_init_data; | |
7753dea8 MD |
5936 | } |
5937 | ||
a88df331 | 5938 | /* Setup kernel tracer */ |
e6142f2e | 5939 | if (!config.no_kernel) { |
4fba7219 | 5940 | init_kernel_tracer(); |
89588270 DG |
5941 | if (kernel_tracer_fd >= 0) { |
5942 | ret = syscall_init_table(); | |
5943 | if (ret < 0) { | |
4a15001e MD |
5944 | ERR("Unable to populate syscall table. " |
5945 | "Syscall tracing won't work " | |
5946 | "for this session daemon."); | |
89588270 | 5947 | } |
834978fd | 5948 | } |
4fba7219 | 5949 | } |
a88df331 DG |
5950 | |
5951 | /* Set ulimit for open files */ | |
5952 | set_ulimit(); | |
fac6795d | 5953 | } |
4063050c MD |
5954 | /* init lttng_fd tracking must be done after set_ulimit. */ |
5955 | lttng_fd_init(); | |
fac6795d | 5956 | |
e6142f2e | 5957 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e MD |
5958 | retval = -1; |
5959 | goto exit_init_data; | |
67e40797 DG |
5960 | } |
5961 | ||
e6142f2e | 5962 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e MD |
5963 | retval = -1; |
5964 | goto exit_init_data; | |
67e40797 DG |
5965 | } |
5966 | ||
d6f42150 | 5967 | /* Setup the needed unix socket */ |
4a15001e MD |
5968 | if (init_daemon_socket()) { |
5969 | retval = -1; | |
5970 | goto exit_init_data; | |
fac6795d DG |
5971 | } |
5972 | ||
5973 | /* Set credentials to socket */ | |
e6142f2e | 5974 | if (is_root && set_permissions(config.rundir.value)) { |
4a15001e MD |
5975 | retval = -1; |
5976 | goto exit_init_data; | |
fac6795d DG |
5977 | } |
5978 | ||
5b8719f5 | 5979 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 5980 | if (config.sig_parent) { |
5b8719f5 DG |
5981 | ppid = getppid(); |
5982 | } | |
5983 | ||
7a485870 | 5984 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 5985 | if (is_root && !config.no_kernel) { |
4a15001e MD |
5986 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
5987 | retval = -1; | |
5988 | goto exit_init_data; | |
6620da75 | 5989 | } |
7a485870 DG |
5990 | } |
5991 | ||
099e26bd | 5992 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
5993 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
5994 | retval = -1; | |
5995 | goto exit_init_data; | |
099e26bd DG |
5996 | } |
5997 | ||
d0b96690 | 5998 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
5999 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
6000 | retval = -1; | |
6001 | goto exit_init_data; | |
d0b96690 DG |
6002 | } |
6003 | ||
7972aab2 DG |
6004 | /* Initialize global buffer per UID and PID registry. */ |
6005 | buffer_reg_init_uid_registry(); | |
6006 | buffer_reg_init_pid_registry(); | |
6007 | ||
099e26bd | 6008 | /* Init UST command queue. */ |
8bdee6e2 | 6009 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 6010 | |
2f77fc4b | 6011 | cmd_init(); |
00e2e675 | 6012 | |
ae9e45b3 DG |
6013 | /* Check for the application socket timeout env variable. */ |
6014 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
6015 | if (env_app_timeout) { | |
e6142f2e | 6016 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 6017 | } else { |
e6142f2e | 6018 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
6019 | } |
6020 | ||
4a15001e MD |
6021 | ret = write_pidfile(); |
6022 | if (ret) { | |
6023 | ERR("Error in write_pidfile"); | |
6024 | retval = -1; | |
6025 | goto exit_init_data; | |
6026 | } | |
35f90c40 | 6027 | |
554831e7 MD |
6028 | /* Initialize communication library */ |
6029 | lttcomm_init(); | |
4a15001e | 6030 | /* Initialize TCP timeout values */ |
d831c249 | 6031 | lttcomm_inet_init(); |
554831e7 | 6032 | |
ef367a93 | 6033 | if (load_session_init_data(&load_info) < 0) { |
4a15001e MD |
6034 | retval = -1; |
6035 | goto exit_init_data; | |
ef367a93 | 6036 | } |
e6142f2e | 6037 | load_info->path = config.load_session_path.value; |
ef367a93 | 6038 | |
12b4fb37 | 6039 | /* Create health-check thread. */ |
1a1a34b4 | 6040 | ret = pthread_create(&health_thread, default_pthread_attr(), |
44a5e5eb | 6041 | thread_manage_health, (void *) NULL); |
4a15001e MD |
6042 | if (ret) { |
6043 | errno = ret; | |
44a5e5eb | 6044 | PERROR("pthread_create health"); |
4a15001e | 6045 | retval = -1; |
44a5e5eb DG |
6046 | goto exit_health; |
6047 | } | |
6048 | ||
90936dcf JD |
6049 | /* |
6050 | * The rotation thread needs the notification thread to be ready before | |
6051 | * creating the rotate_notification_channel, so we use this semaphore as | |
6052 | * a rendez-vous point. | |
6053 | */ | |
6054 | sem_init(¬ification_thread_ready, 0, 0); | |
6055 | ||
b3530820 JG |
6056 | /* notification_thread_data acquires the pipes' read side. */ |
6057 | notification_thread_handle = notification_thread_handle_create( | |
6058 | ust32_channel_monitor_pipe, | |
6059 | ust64_channel_monitor_pipe, | |
90936dcf JD |
6060 | kernel_channel_monitor_pipe, |
6061 | ¬ification_thread_ready); | |
b3530820 JG |
6062 | if (!notification_thread_handle) { |
6063 | retval = -1; | |
6064 | ERR("Failed to create notification thread shared data"); | |
6065 | stop_threads(); | |
6066 | goto exit_notification; | |
6067 | } | |
6068 | ||
6069 | /* Create notification thread. */ | |
6070 | ret = pthread_create(¬ification_thread, default_pthread_attr(), | |
6071 | thread_notification, notification_thread_handle); | |
6072 | if (ret) { | |
6073 | errno = ret; | |
6074 | PERROR("pthread_create notification"); | |
6075 | retval = -1; | |
6076 | stop_threads(); | |
6077 | goto exit_notification; | |
6078 | } | |
99611076 | 6079 | notification_thread_launched = true; |
b3530820 | 6080 | |
d086f507 JD |
6081 | /* Create timer thread. */ |
6082 | ret = pthread_create(&timer_thread, default_pthread_attr(), | |
92816cc3 | 6083 | timer_thread_func, &timer_thread_ctx); |
d086f507 JD |
6084 | if (ret) { |
6085 | errno = ret; | |
6086 | PERROR("pthread_create timer"); | |
6087 | retval = -1; | |
6088 | stop_threads(); | |
6089 | goto exit_notification; | |
6090 | } | |
99611076 | 6091 | timer_thread_launched = true; |
d086f507 | 6092 | |
db66e574 JD |
6093 | /* rotation_thread_data acquires the pipes' read side. */ |
6094 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf JD |
6095 | rotation_timer_queue, |
6096 | notification_thread_handle, | |
6097 | ¬ification_thread_ready); | |
db66e574 JD |
6098 | if (!rotation_thread_handle) { |
6099 | retval = -1; | |
6100 | ERR("Failed to create rotation thread shared data"); | |
6101 | stop_threads(); | |
6102 | goto exit_rotation; | |
6103 | } | |
db66e574 JD |
6104 | |
6105 | /* Create rotation thread. */ | |
6106 | ret = pthread_create(&rotation_thread, default_pthread_attr(), | |
6107 | thread_rotation, rotation_thread_handle); | |
6108 | if (ret) { | |
6109 | errno = ret; | |
6110 | PERROR("pthread_create rotation"); | |
6111 | retval = -1; | |
6112 | stop_threads(); | |
6113 | goto exit_rotation; | |
6114 | } | |
99611076 | 6115 | rotation_thread_launched = true; |
db66e574 | 6116 | |
cf3af59e | 6117 | /* Create thread to manage the client socket */ |
1a1a34b4 | 6118 | ret = pthread_create(&client_thread, default_pthread_attr(), |
099e26bd | 6119 | thread_manage_clients, (void *) NULL); |
4a15001e MD |
6120 | if (ret) { |
6121 | errno = ret; | |
76d7553f | 6122 | PERROR("pthread_create clients"); |
4a15001e | 6123 | retval = -1; |
b3530820 | 6124 | stop_threads(); |
cf3af59e MD |
6125 | goto exit_client; |
6126 | } | |
fac6795d | 6127 | |
099e26bd | 6128 | /* Create thread to dispatch registration */ |
1a1a34b4 | 6129 | ret = pthread_create(&dispatch_thread, default_pthread_attr(), |
099e26bd | 6130 | thread_dispatch_ust_registration, (void *) NULL); |
4a15001e MD |
6131 | if (ret) { |
6132 | errno = ret; | |
76d7553f | 6133 | PERROR("pthread_create dispatch"); |
4a15001e | 6134 | retval = -1; |
b3530820 | 6135 | stop_threads(); |
099e26bd DG |
6136 | goto exit_dispatch; |
6137 | } | |
6138 | ||
6139 | /* Create thread to manage application registration. */ | |
1a1a34b4 | 6140 | ret = pthread_create(®_apps_thread, default_pthread_attr(), |
099e26bd | 6141 | thread_registration_apps, (void *) NULL); |
4a15001e MD |
6142 | if (ret) { |
6143 | errno = ret; | |
76d7553f | 6144 | PERROR("pthread_create registration"); |
4a15001e | 6145 | retval = -1; |
b3530820 | 6146 | stop_threads(); |
099e26bd DG |
6147 | goto exit_reg_apps; |
6148 | } | |
6149 | ||
cf3af59e | 6150 | /* Create thread to manage application socket */ |
1a1a34b4 | 6151 | ret = pthread_create(&apps_thread, default_pthread_attr(), |
54d01ffb | 6152 | thread_manage_apps, (void *) NULL); |
4a15001e MD |
6153 | if (ret) { |
6154 | errno = ret; | |
d0b96690 | 6155 | PERROR("pthread_create apps"); |
4a15001e | 6156 | retval = -1; |
b3530820 | 6157 | stop_threads(); |
d0b96690 DG |
6158 | goto exit_apps; |
6159 | } | |
6160 | ||
6161 | /* Create thread to manage application notify socket */ | |
1a1a34b4 | 6162 | ret = pthread_create(&apps_notify_thread, default_pthread_attr(), |
d0b96690 | 6163 | ust_thread_manage_notify, (void *) NULL); |
4a15001e MD |
6164 | if (ret) { |
6165 | errno = ret; | |
f5c32ef1 | 6166 | PERROR("pthread_create notify"); |
4a15001e | 6167 | retval = -1; |
b3530820 | 6168 | stop_threads(); |
9563b0ad | 6169 | goto exit_apps_notify; |
cf3af59e | 6170 | } |
fac6795d | 6171 | |
022d91ba | 6172 | /* Create agent registration thread. */ |
1a1a34b4 | 6173 | ret = pthread_create(&agent_reg_thread, default_pthread_attr(), |
022d91ba | 6174 | agent_thread_manage_registration, (void *) NULL); |
4a15001e MD |
6175 | if (ret) { |
6176 | errno = ret; | |
022d91ba | 6177 | PERROR("pthread_create agent"); |
4a15001e | 6178 | retval = -1; |
b3530820 | 6179 | stop_threads(); |
022d91ba | 6180 | goto exit_agent_reg; |
4d076222 DG |
6181 | } |
6182 | ||
6620da75 | 6183 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 6184 | if (is_root && !config.no_kernel) { |
6620da75 | 6185 | /* Create kernel thread to manage kernel event */ |
1a1a34b4 | 6186 | ret = pthread_create(&kernel_thread, default_pthread_attr(), |
6620da75 | 6187 | thread_manage_kernel, (void *) NULL); |
4a15001e MD |
6188 | if (ret) { |
6189 | errno = ret; | |
6620da75 | 6190 | PERROR("pthread_create kernel"); |
4a15001e | 6191 | retval = -1; |
b3530820 | 6192 | stop_threads(); |
6620da75 DG |
6193 | goto exit_kernel; |
6194 | } | |
ef367a93 | 6195 | } |
7a485870 | 6196 | |
4a15001e | 6197 | /* Create session loading thread. */ |
1a1a34b4 MJ |
6198 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
6199 | thread_load_session, load_info); | |
4a15001e MD |
6200 | if (ret) { |
6201 | errno = ret; | |
6202 | PERROR("pthread_create load_session_thread"); | |
6203 | retval = -1; | |
b3530820 | 6204 | stop_threads(); |
4a15001e MD |
6205 | goto exit_load_session; |
6206 | } | |
6207 | ||
6208 | /* | |
6209 | * This is where we start awaiting program completion (e.g. through | |
6210 | * signal that asks threads to teardown). | |
6211 | */ | |
6212 | ||
6213 | ret = pthread_join(load_session_thread, &status); | |
6214 | if (ret) { | |
6215 | errno = ret; | |
6216 | PERROR("pthread_join load_session_thread"); | |
6217 | retval = -1; | |
6218 | } | |
99d688f2 JG |
6219 | |
6220 | /* Initiate teardown once activity occurs on the quit pipe. */ | |
6221 | sessiond_wait_for_quit_pipe(-1U); | |
6222 | destroy_all_sessions_and_wait(); | |
4a15001e | 6223 | exit_load_session: |
ef367a93 | 6224 | |
e6142f2e | 6225 | if (is_root && !config.no_kernel) { |
6620da75 | 6226 | ret = pthread_join(kernel_thread, &status); |
4a15001e MD |
6227 | if (ret) { |
6228 | errno = ret; | |
6620da75 | 6229 | PERROR("pthread_join"); |
4a15001e | 6230 | retval = -1; |
6620da75 | 6231 | } |
fac6795d | 6232 | } |
cf3af59e | 6233 | exit_kernel: |
4a15001e | 6234 | |
022d91ba | 6235 | ret = pthread_join(agent_reg_thread, &status); |
4a15001e MD |
6236 | if (ret) { |
6237 | errno = ret; | |
022d91ba | 6238 | PERROR("pthread_join agent"); |
4a15001e | 6239 | retval = -1; |
4d076222 | 6240 | } |
022d91ba | 6241 | exit_agent_reg: |
4a15001e | 6242 | |
9563b0ad | 6243 | ret = pthread_join(apps_notify_thread, &status); |
4a15001e MD |
6244 | if (ret) { |
6245 | errno = ret; | |
9563b0ad | 6246 | PERROR("pthread_join apps notify"); |
4a15001e | 6247 | retval = -1; |
9563b0ad | 6248 | } |
9563b0ad | 6249 | exit_apps_notify: |
4a15001e | 6250 | |
cf3af59e | 6251 | ret = pthread_join(apps_thread, &status); |
4a15001e MD |
6252 | if (ret) { |
6253 | errno = ret; | |
9563b0ad | 6254 | PERROR("pthread_join apps"); |
4a15001e | 6255 | retval = -1; |
cf3af59e | 6256 | } |
cf3af59e | 6257 | exit_apps: |
4a15001e | 6258 | |
099e26bd | 6259 | ret = pthread_join(reg_apps_thread, &status); |
4a15001e MD |
6260 | if (ret) { |
6261 | errno = ret; | |
76d7553f | 6262 | PERROR("pthread_join"); |
4a15001e | 6263 | retval = -1; |
099e26bd | 6264 | } |
099e26bd | 6265 | exit_reg_apps: |
4a15001e | 6266 | |
772b8f4d MD |
6267 | /* |
6268 | * Join dispatch thread after joining reg_apps_thread to ensure | |
6269 | * we don't leak applications in the queue. | |
6270 | */ | |
099e26bd | 6271 | ret = pthread_join(dispatch_thread, &status); |
4a15001e MD |
6272 | if (ret) { |
6273 | errno = ret; | |
76d7553f | 6274 | PERROR("pthread_join"); |
4a15001e | 6275 | retval = -1; |
099e26bd | 6276 | } |
099e26bd | 6277 | exit_dispatch: |
4a15001e | 6278 | |
cf3af59e | 6279 | ret = pthread_join(client_thread, &status); |
4a15001e MD |
6280 | if (ret) { |
6281 | errno = ret; | |
76d7553f | 6282 | PERROR("pthread_join"); |
4a15001e | 6283 | retval = -1; |
cf3af59e | 6284 | } |
a88df331 | 6285 | |
b3530820 | 6286 | exit_client: |
db66e574 | 6287 | exit_rotation: |
b3530820 | 6288 | exit_notification: |
90936dcf | 6289 | sem_destroy(¬ification_thread_ready); |
4a15001e MD |
6290 | ret = pthread_join(health_thread, &status); |
6291 | if (ret) { | |
6292 | errno = ret; | |
6293 | PERROR("pthread_join health thread"); | |
6294 | retval = -1; | |
06f525de DG |
6295 | } |
6296 | ||
b3530820 | 6297 | exit_health: |
4a15001e | 6298 | exit_init_data: |
4d62fbf8 MD |
6299 | /* |
6300 | * Wait for all pending call_rcu work to complete before tearing | |
6301 | * down data structures. call_rcu worker may be trying to | |
6302 | * perform lookups in those structures. | |
6303 | */ | |
6304 | rcu_barrier(); | |
4a15001e MD |
6305 | /* |
6306 | * sessiond_cleanup() is called when no other thread is running, except | |
6307 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
6308 | */ | |
6309 | rcu_thread_online(); | |
6310 | sessiond_cleanup(); | |
06f525de | 6311 | |
c4d5de9d JG |
6312 | /* |
6313 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
6314 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
6315 | * the queue is empty before shutting down the clean-up thread. | |
6316 | */ | |
6317 | rcu_barrier(); | |
6318 | ||
b3530820 JG |
6319 | /* |
6320 | * The teardown of the notification system is performed after the | |
6321 | * session daemon's teardown in order to allow it to be notified | |
6322 | * of the active session and channels at the moment of the teardown. | |
6323 | */ | |
6324 | if (notification_thread_handle) { | |
99611076 | 6325 | if (notification_thread_launched) { |
6be1a386 JG |
6326 | notification_thread_command_quit( |
6327 | notification_thread_handle); | |
6328 | ret = pthread_join(notification_thread, &status); | |
6329 | if (ret) { | |
6330 | errno = ret; | |
6331 | PERROR("pthread_join notification thread"); | |
6332 | retval = -1; | |
6333 | } | |
3dec304b | 6334 | } |
6be1a386 | 6335 | notification_thread_handle_destroy(notification_thread_handle); |
b3530820 JG |
6336 | } |
6337 | ||
db66e574 | 6338 | if (rotation_thread_handle) { |
99611076 | 6339 | if (rotation_thread_launched) { |
db66e574 JD |
6340 | ret = pthread_join(rotation_thread, &status); |
6341 | if (ret) { | |
6342 | errno = ret; | |
6343 | PERROR("pthread_join rotation thread"); | |
6344 | retval = -1; | |
6345 | } | |
6346 | } | |
6347 | rotation_thread_handle_destroy(rotation_thread_handle); | |
6348 | } | |
6349 | ||
99611076 | 6350 | if (timer_thread_launched) { |
92816cc3 | 6351 | timer_exit(); |
d086f507 JD |
6352 | ret = pthread_join(timer_thread, &status); |
6353 | if (ret) { | |
6354 | errno = ret; | |
6355 | PERROR("pthread_join timer thread"); | |
6356 | retval = -1; | |
6357 | } | |
6358 | } | |
6359 | ||
6360 | /* | |
6361 | * After the rotation and timer thread have quit, we can safely destroy | |
6362 | * the rotation_timer_queue. | |
6363 | */ | |
92816cc3 | 6364 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
d086f507 | 6365 | |
b3530820 JG |
6366 | rcu_thread_offline(); |
6367 | rcu_unregister_thread(); | |
6368 | ||
5e97de00 | 6369 | ret = fini_ht_cleanup_thread(&ht_cleanup_thread); |
4a15001e | 6370 | if (ret) { |
4a15001e | 6371 | retval = -1; |
0b2dc8df | 6372 | } |
b3530820 JG |
6373 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
6374 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
6375 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
0b2dc8df | 6376 | exit_ht_cleanup: |
4a15001e | 6377 | |
4a15001e MD |
6378 | health_app_destroy(health_sessiond); |
6379 | exit_health_sessiond_cleanup: | |
7567352f | 6380 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
6381 | |
6382 | exit_options: | |
f472090a | 6383 | sessiond_cleanup_lock_file(); |
4a15001e MD |
6384 | sessiond_cleanup_options(); |
6385 | ||
6386 | exit_set_signal_handler: | |
6387 | if (!retval) { | |
cf3af59e | 6388 | exit(EXIT_SUCCESS); |
4a15001e MD |
6389 | } else { |
6390 | exit(EXIT_FAILURE); | |
67e40797 | 6391 | } |
fac6795d | 6392 | } |