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 | 52 | #include <common/dynamic-buffer.h> |
76fcf151 | 53 | #include <lttng/event-internal.h> |
fac6795d | 54 | |
10a8a223 | 55 | #include "lttng-sessiond.h" |
7972aab2 | 56 | #include "buffer-registry.h" |
54d01ffb | 57 | #include "channel.h" |
2f77fc4b | 58 | #include "cmd.h" |
00e2e675 | 59 | #include "consumer.h" |
099e26bd | 60 | #include "context.h" |
54d01ffb | 61 | #include "event.h" |
4771f025 | 62 | #include "kernel.h" |
f1e16794 | 63 | #include "kernel-consumer.h" |
096102bd | 64 | #include "modprobe.h" |
0fdd1e2c | 65 | #include "shm.h" |
1e307fab | 66 | #include "ust-ctl.h" |
00e2e675 | 67 | #include "ust-consumer.h" |
8e68d1c8 | 68 | #include "utils.h" |
4063050c | 69 | #include "fd-limit.h" |
8782cc74 | 70 | #include "health-sessiond.h" |
8ac94142 | 71 | #include "testpoint.h" |
d0b96690 | 72 | #include "ust-thread.h" |
022d91ba | 73 | #include "agent-thread.h" |
fb198a11 | 74 | #include "save.h" |
b3530820 JG |
75 | #include "notification-thread.h" |
76 | #include "notification-thread-commands.h" | |
db66e574 | 77 | #include "rotation-thread.h" |
0dbc2034 | 78 | #include "lttng-syscall.h" |
7c1d2758 | 79 | #include "agent.h" |
5e97de00 | 80 | #include "ht-cleanup.h" |
e6142f2e | 81 | #include "sessiond-config.h" |
8e319828 | 82 | #include "timer.h" |
a3707772 | 83 | #include "thread.h" |
917a718d | 84 | #include "client.h" |
5d1b0219 | 85 | #include "dispatch.h" |
1785d7f2 | 86 | #include "register.h" |
7649924e | 87 | #include "manage-apps.h" |
5b093681 | 88 | #include "manage-kernel.h" |
ebaeda94 | 89 | |
4fc83d94 PP |
90 | static const char *help_msg = |
91 | #ifdef LTTNG_EMBED_HELP | |
92 | #include <lttng-sessiond.8.h> | |
93 | #else | |
94 | NULL | |
95 | #endif | |
96 | ; | |
97 | ||
fac6795d | 98 | const char *progname; |
c9cb3e7d | 99 | static int lockfile_fd = -1; |
3bd1e081 | 100 | |
0bb7724a DG |
101 | /* Set to 1 when a SIGUSR1 signal is received. */ |
102 | static int recv_child_signal; | |
103 | ||
26296c48 JG |
104 | /* Command line options */ |
105 | static const struct option long_options[] = { | |
0f5ea17c JG |
106 | { "client-sock", required_argument, 0, 'c' }, |
107 | { "apps-sock", required_argument, 0, 'a' }, | |
108 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
109 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
110 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
111 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
112 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
113 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
114 | { "consumerd32-path", required_argument, 0, '\0' }, | |
115 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
116 | { "consumerd64-path", required_argument, 0, '\0' }, | |
117 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
118 | { "daemonize", no_argument, 0, 'd' }, | |
119 | { "background", no_argument, 0, 'b' }, | |
120 | { "sig-parent", no_argument, 0, 'S' }, | |
121 | { "help", no_argument, 0, 'h' }, | |
122 | { "group", required_argument, 0, 'g' }, | |
123 | { "version", no_argument, 0, 'V' }, | |
124 | { "quiet", no_argument, 0, 'q' }, | |
125 | { "verbose", no_argument, 0, 'v' }, | |
126 | { "verbose-consumer", no_argument, 0, '\0' }, | |
127 | { "no-kernel", no_argument, 0, '\0' }, | |
128 | { "pidfile", required_argument, 0, 'p' }, | |
129 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
130 | { "config", required_argument, 0, 'f' }, | |
131 | { "load", required_argument, 0, 'l' }, | |
132 | { "kmod-probes", required_argument, 0, '\0' }, | |
133 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
134 | { NULL, 0, 0, 0 } |
135 | }; | |
136 | ||
137 | /* Command line options to ignore from configuration file */ | |
138 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
1d4b027a | 139 | |
099e26bd DG |
140 | /* |
141 | * This pipe is used to inform the thread managing application communication | |
142 | * that a command is queued and ready to be processed. | |
143 | */ | |
76d7553f | 144 | static int apps_cmd_pipe[2] = { -1, -1 }; |
971a61c6 | 145 | static int apps_cmd_notify_pipe[2] = { -1, -1 }; |
099e26bd | 146 | |
099e26bd DG |
147 | /* |
148 | * UST registration command queue. This queue is tied with a futex and uses a N | |
149 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
150 | * | |
b22c5da8 DG |
151 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
152 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
153 | * the line new application socket and monitors it for any I/O error or clean | |
154 | * close that triggers an unregistration of the application. | |
099e26bd DG |
155 | */ |
156 | static struct ust_cmd_queue ust_cmd_queue; | |
157 | ||
2f77fc4b DG |
158 | static const char *module_proc_lttng = "/proc/lttng"; |
159 | ||
97bc1426 | 160 | /* |
a7333da7 | 161 | * Section name to look for in the daemon configuration file. |
97bc1426 | 162 | */ |
a7333da7 | 163 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 164 | |
a7333da7 JG |
165 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
166 | static int is_root; | |
5eb91c98 | 167 | |
099e26bd DG |
168 | /* |
169 | * Stop all threads by closing the thread quit pipe. | |
170 | */ | |
cf3af59e MD |
171 | static void stop_threads(void) |
172 | { | |
5eb91c98 DG |
173 | int ret; |
174 | ||
cf3af59e MD |
175 | /* Stopping all threads */ |
176 | DBG("Terminating all threads"); | |
a7333da7 | 177 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
178 | if (ret < 0) { |
179 | ERR("write error on thread quit pipe"); | |
180 | } | |
cf3af59e MD |
181 | } |
182 | ||
e975f9f8 DG |
183 | /* |
184 | * Close every consumer sockets. | |
185 | */ | |
186 | static void close_consumer_sockets(void) | |
187 | { | |
188 | int ret; | |
189 | ||
190 | if (kconsumer_data.err_sock >= 0) { | |
191 | ret = close(kconsumer_data.err_sock); | |
192 | if (ret < 0) { | |
193 | PERROR("kernel consumer err_sock close"); | |
194 | } | |
195 | } | |
196 | if (ustconsumer32_data.err_sock >= 0) { | |
197 | ret = close(ustconsumer32_data.err_sock); | |
198 | if (ret < 0) { | |
a76cbd9f | 199 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
200 | } |
201 | } | |
202 | if (ustconsumer64_data.err_sock >= 0) { | |
203 | ret = close(ustconsumer64_data.err_sock); | |
204 | if (ret < 0) { | |
a76cbd9f | 205 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
206 | } |
207 | } | |
208 | if (kconsumer_data.cmd_sock >= 0) { | |
209 | ret = close(kconsumer_data.cmd_sock); | |
210 | if (ret < 0) { | |
211 | PERROR("kernel consumer cmd_sock close"); | |
212 | } | |
213 | } | |
214 | if (ustconsumer32_data.cmd_sock >= 0) { | |
215 | ret = close(ustconsumer32_data.cmd_sock); | |
216 | if (ret < 0) { | |
a76cbd9f | 217 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
218 | } |
219 | } | |
220 | if (ustconsumer64_data.cmd_sock >= 0) { | |
221 | ret = close(ustconsumer64_data.cmd_sock); | |
222 | if (ret < 0) { | |
a76cbd9f | 223 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
224 | } |
225 | } | |
b3530820 JG |
226 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
227 | ret = close(kconsumer_data.channel_monitor_pipe); | |
228 | if (ret < 0) { | |
229 | PERROR("kernel consumer channel monitor pipe close"); | |
230 | } | |
231 | } | |
232 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
233 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
234 | if (ret < 0) { | |
235 | PERROR("UST consumerd32 channel monitor pipe close"); | |
236 | } | |
237 | } | |
238 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
239 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
240 | if (ret < 0) { | |
241 | PERROR("UST consumerd64 channel monitor pipe close"); | |
242 | } | |
243 | } | |
e975f9f8 DG |
244 | } |
245 | ||
4e4714cb JR |
246 | /* |
247 | * Wait on consumer process termination. | |
248 | * | |
249 | * Need to be called with the consumer data lock held or from a context | |
250 | * ensuring no concurrent access to data (e.g: cleanup). | |
251 | */ | |
252 | static void wait_consumer(struct consumer_data *consumer_data) | |
253 | { | |
254 | pid_t ret; | |
255 | int status; | |
256 | ||
257 | if (consumer_data->pid <= 0) { | |
258 | return; | |
259 | } | |
260 | ||
261 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
262 | consumer_data->pid); | |
263 | ret = waitpid(consumer_data->pid, &status, 0); | |
264 | if (ret == -1) { | |
265 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 266 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
267 | ERR("consumerd termination with error: %d", |
268 | WEXITSTATUS(ret)); | |
269 | } | |
270 | consumer_data->pid = 0; | |
271 | } | |
272 | ||
fac6795d | 273 | /* |
4a15001e | 274 | * Cleanup the session daemon's data structures. |
fac6795d | 275 | */ |
4a15001e | 276 | static void sessiond_cleanup(void) |
fac6795d | 277 | { |
ef599319 | 278 | int ret; |
e32d7f27 | 279 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 280 | |
4a15001e | 281 | DBG("Cleanup sessiond"); |
e07ae692 | 282 | |
4e449f3f MD |
283 | /* |
284 | * Close the thread quit pipe. It has already done its job, | |
285 | * since we are now called. | |
286 | */ | |
a7333da7 | 287 | sessiond_close_quit_pipe(); |
971a61c6 JG |
288 | utils_close_pipe(apps_cmd_pipe); |
289 | utils_close_pipe(apps_cmd_notify_pipe); | |
5b093681 | 290 | utils_close_pipe(kernel_poll_pipe); |
2f77fc4b | 291 | |
c9a2957d JG |
292 | ret = remove(config.pid_file_path.value); |
293 | if (ret < 0) { | |
294 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
295 | } |
296 | ||
e6142f2e JG |
297 | DBG("Removing sessiond and consumerd content of directory %s", |
298 | config.rundir.value); | |
8c6c56c2 MD |
299 | |
300 | /* sessiond */ | |
e6142f2e JG |
301 | DBG("Removing %s", config.pid_file_path.value); |
302 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 303 | |
e6142f2e JG |
304 | DBG("Removing %s", config.agent_port_file_path.value); |
305 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 306 | |
8c6c56c2 | 307 | /* kconsumerd */ |
e6142f2e JG |
308 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
309 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
310 | ||
311 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
312 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
313 | |
314 | /* ust consumerd 32 */ | |
e6142f2e JG |
315 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
316 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
317 | ||
318 | DBG("Removing directory %s", config.consumerd32_path.value); | |
319 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
320 | |
321 | /* ust consumerd 64 */ | |
e6142f2e JG |
322 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
323 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
324 | ||
325 | DBG("Removing directory %s", config.consumerd64_path.value); | |
326 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 327 | |
99d688f2 | 328 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 329 | |
4e4714cb JR |
330 | wait_consumer(&kconsumer_data); |
331 | wait_consumer(&ustconsumer64_data); | |
332 | wait_consumer(&ustconsumer32_data); | |
333 | ||
6a4e4039 JG |
334 | DBG("Cleaning up all agent apps"); |
335 | agent_app_ht_clean(); | |
336 | ||
099e26bd | 337 | DBG("Closing all UST sockets"); |
56fff090 | 338 | ust_app_clean_list(); |
7972aab2 | 339 | buffer_reg_destroy_registries(); |
099e26bd | 340 | |
e6142f2e | 341 | if (is_root && !config.no_kernel) { |
4fba7219 | 342 | DBG2("Closing kernel fd"); |
a4b35e07 | 343 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
344 | ret = close(kernel_tracer_fd); |
345 | if (ret) { | |
346 | PERROR("close"); | |
347 | } | |
a4b35e07 | 348 | } |
2f50c8a3 | 349 | DBG("Unloading kernel modules"); |
096102bd | 350 | modprobe_remove_lttng_all(); |
834978fd | 351 | free(syscall_table); |
2f50c8a3 | 352 | } |
2f77fc4b | 353 | |
e975f9f8 DG |
354 | close_consumer_sockets(); |
355 | ||
c9cb3e7d JG |
356 | /* |
357 | * We do NOT rmdir rundir because there are other processes | |
358 | * using it, for instance lttng-relayd, which can start in | |
359 | * parallel with this teardown. | |
360 | */ | |
4a15001e MD |
361 | } |
362 | ||
363 | /* | |
364 | * Cleanup the daemon's option data structures. | |
365 | */ | |
366 | static void sessiond_cleanup_options(void) | |
367 | { | |
368 | DBG("Cleaning up options"); | |
369 | ||
e6142f2e | 370 | sessiond_config_fini(&config); |
c9cb3e7d | 371 | |
7567352f | 372 | run_as_destroy_worker(); |
fac6795d DG |
373 | } |
374 | ||
a23ec3a7 DG |
375 | /* |
376 | * Signal pthread condition of the consumer data that the thread. | |
377 | */ | |
378 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
379 | { | |
380 | pthread_mutex_lock(&data->cond_mutex); | |
381 | ||
382 | /* | |
383 | * The state is set before signaling. It can be any value, it's the waiter | |
384 | * job to correctly interpret this condition variable associated to the | |
385 | * consumer pthread_cond. | |
386 | * | |
387 | * A value of 0 means that the corresponding thread of the consumer data | |
388 | * was not started. 1 indicates that the thread has started and is ready | |
389 | * for action. A negative value means that there was an error during the | |
390 | * thread bootstrap. | |
391 | */ | |
392 | data->consumer_thread_is_ready = state; | |
393 | (void) pthread_cond_signal(&data->cond); | |
394 | ||
395 | pthread_mutex_unlock(&data->cond_mutex); | |
396 | } | |
397 | ||
1d4b027a | 398 | /* |
3bd1e081 | 399 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 400 | */ |
917a718d | 401 | void *thread_manage_consumer(void *data) |
1d4b027a | 402 | { |
42fc1d0b | 403 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 404 | uint32_t revents, nb_fd; |
1d4b027a | 405 | enum lttcomm_return_code code; |
5eb91c98 | 406 | struct lttng_poll_event events; |
3bd1e081 | 407 | struct consumer_data *consumer_data = data; |
b3530820 | 408 | struct consumer_socket *cmd_socket_wrapper = NULL; |
1d4b027a | 409 | |
3bd1e081 | 410 | DBG("[thread] Manage consumer started"); |
1d4b027a | 411 | |
34c1e15a MD |
412 | rcu_register_thread(); |
413 | rcu_thread_online(); | |
414 | ||
6c71277b | 415 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 416 | |
855060f8 | 417 | health_code_update(); |
9449cc75 | 418 | |
5eb91c98 | 419 | /* |
331744e3 JD |
420 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
421 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 422 | */ |
331744e3 | 423 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 424 | if (ret < 0) { |
76d7553f | 425 | goto error_poll; |
5eb91c98 | 426 | } |
273ea72c | 427 | |
edb8b045 DG |
428 | /* |
429 | * The error socket here is already in a listening state which was done | |
430 | * just before spawning this thread to avoid a race between the consumer | |
431 | * daemon exec trying to connect and the listen() call. | |
432 | */ | |
3bd1e081 | 433 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
434 | if (ret < 0) { |
435 | goto error; | |
436 | } | |
437 | ||
840cb59c | 438 | health_code_update(); |
44a5e5eb | 439 | |
331744e3 | 440 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 441 | restart: |
a78af745 | 442 | health_poll_entry(); |
8ac94142 | 443 | |
e547b070 | 444 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
445 | goto error; |
446 | } | |
8ac94142 | 447 | |
5eb91c98 | 448 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 449 | health_poll_exit(); |
273ea72c | 450 | if (ret < 0) { |
88f2b785 MD |
451 | /* |
452 | * Restart interrupted system call. | |
453 | */ | |
454 | if (errno == EINTR) { | |
455 | goto restart; | |
456 | } | |
273ea72c DG |
457 | goto error; |
458 | } | |
459 | ||
0d9c5d77 DG |
460 | nb_fd = ret; |
461 | ||
5eb91c98 DG |
462 | for (i = 0; i < nb_fd; i++) { |
463 | /* Fetch once the poll data */ | |
464 | revents = LTTNG_POLL_GETEV(&events, i); | |
465 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
466 | ||
840cb59c | 467 | health_code_update(); |
44a5e5eb | 468 | |
fd20dac9 MD |
469 | if (!revents) { |
470 | /* No activity for this FD (poll implementation). */ | |
471 | continue; | |
472 | } | |
473 | ||
5eb91c98 | 474 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 475 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 476 | if (ret) { |
139ac872 MD |
477 | err = 0; |
478 | goto exit; | |
5eb91c98 DG |
479 | } |
480 | ||
481 | /* Event on the registration socket */ | |
3bd1e081 | 482 | if (pollfd == consumer_data->err_sock) { |
03e43155 MD |
483 | if (revents & LPOLLIN) { |
484 | continue; | |
485 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 486 | ERR("consumer err socket poll error"); |
5eb91c98 | 487 | goto error; |
03e43155 MD |
488 | } else { |
489 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
490 | goto error; | |
5eb91c98 DG |
491 | } |
492 | } | |
273ea72c DG |
493 | } |
494 | ||
3bd1e081 | 495 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
496 | if (sock < 0) { |
497 | goto error; | |
498 | } | |
499 | ||
b662582b DG |
500 | /* |
501 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
502 | * show must go on. | |
503 | */ | |
504 | (void) utils_set_fd_cloexec(sock); | |
505 | ||
840cb59c | 506 | health_code_update(); |
44a5e5eb | 507 | |
3bd1e081 | 508 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 509 | |
712ea556 | 510 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
511 | ret = lttcomm_recv_unix_sock(sock, &code, |
512 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
513 | if (ret <= 0) { |
514 | goto error; | |
515 | } | |
516 | ||
840cb59c | 517 | health_code_update(); |
b3530820 | 518 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 | 519 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
520 | lttcomm_get_readable_code(-code)); |
521 | goto error; | |
522 | } | |
523 | ||
b3530820 JG |
524 | /* Connect both command and metadata sockets. */ |
525 | consumer_data->cmd_sock = | |
526 | lttcomm_connect_unix_sock( | |
527 | consumer_data->cmd_unix_sock_path); | |
528 | consumer_data->metadata_fd = | |
529 | lttcomm_connect_unix_sock( | |
530 | consumer_data->cmd_unix_sock_path); | |
531 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { | |
532 | PERROR("consumer connect cmd socket"); | |
533 | /* On error, signal condition and quit. */ | |
534 | signal_consumer_condition(consumer_data, -1); | |
535 | goto error; | |
536 | } | |
537 | ||
538 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; | |
539 | ||
540 | /* Create metadata socket lock. */ | |
541 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
542 | if (consumer_data->metadata_sock.lock == NULL) { | |
543 | PERROR("zmalloc pthread mutex"); | |
544 | goto error; | |
545 | } | |
546 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
547 | ||
548 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); | |
549 | DBG("Consumer metadata socket ready (fd: %d)", | |
550 | consumer_data->metadata_fd); | |
551 | ||
552 | /* | |
553 | * Remove the consumerd error sock since we've established a connection. | |
554 | */ | |
3bd1e081 | 555 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 556 | if (ret < 0) { |
72079cae DG |
557 | goto error; |
558 | } | |
559 | ||
331744e3 | 560 | /* Add new accepted error socket. */ |
5eb91c98 DG |
561 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
562 | if (ret < 0) { | |
72079cae | 563 | goto error; |
5eb91c98 DG |
564 | } |
565 | ||
331744e3 | 566 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 567 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
568 | LPOLLIN | LPOLLRDHUP); |
569 | if (ret < 0) { | |
570 | goto error; | |
571 | } | |
572 | ||
840cb59c | 573 | health_code_update(); |
44a5e5eb | 574 | |
b3530820 | 575 | /* |
62c43103 | 576 | * Transfer the write-end of the channel monitoring and rotate pipe |
92816cc3 | 577 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command. |
b3530820 JG |
578 | */ |
579 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); | |
580 | if (!cmd_socket_wrapper) { | |
581 | goto error; | |
582 | } | |
3e4dc117 | 583 | cmd_socket_wrapper->lock = &consumer_data->lock; |
b3530820 JG |
584 | |
585 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, | |
586 | consumer_data->channel_monitor_pipe); | |
587 | if (ret) { | |
588 | goto error; | |
589 | } | |
62c43103 | 590 | |
b3530820 JG |
591 | /* Discard the socket wrapper as it is no longer needed. */ |
592 | consumer_destroy_socket(cmd_socket_wrapper); | |
593 | cmd_socket_wrapper = NULL; | |
594 | ||
595 | /* The thread is completely initialized, signal that it is ready. */ | |
596 | signal_consumer_condition(consumer_data, 1); | |
597 | ||
331744e3 | 598 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 599 | restart_poll: |
331744e3 | 600 | while (1) { |
42fc1d0b DG |
601 | health_code_update(); |
602 | ||
603 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
604 | if (should_quit) { | |
605 | /* Not a health error. */ | |
606 | err = 0; | |
607 | goto exit; | |
608 | } | |
609 | ||
331744e3 JD |
610 | health_poll_entry(); |
611 | ret = lttng_poll_wait(&events, -1); | |
612 | health_poll_exit(); | |
613 | if (ret < 0) { | |
614 | /* | |
615 | * Restart interrupted system call. | |
616 | */ | |
617 | if (errno == EINTR) { | |
618 | goto restart_poll; | |
619 | } | |
620 | goto error; | |
88f2b785 | 621 | } |
72079cae | 622 | |
331744e3 | 623 | nb_fd = ret; |
0d9c5d77 | 624 | |
331744e3 JD |
625 | for (i = 0; i < nb_fd; i++) { |
626 | /* Fetch once the poll data */ | |
627 | revents = LTTNG_POLL_GETEV(&events, i); | |
628 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 629 | |
331744e3 | 630 | health_code_update(); |
44a5e5eb | 631 | |
fd20dac9 MD |
632 | if (!revents) { |
633 | /* No activity for this FD (poll implementation). */ | |
634 | continue; | |
635 | } | |
636 | ||
42fc1d0b DG |
637 | /* |
638 | * Thread quit pipe has been triggered, flag that we should stop | |
639 | * but continue the current loop to handle potential data from | |
640 | * consumer. | |
641 | */ | |
642 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 643 | |
331744e3 JD |
644 | if (pollfd == sock) { |
645 | /* Event on the consumerd socket */ | |
03e43155 MD |
646 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
647 | && !(revents & LPOLLIN)) { | |
331744e3 JD |
648 | ERR("consumer err socket second poll error"); |
649 | goto error; | |
650 | } | |
651 | health_code_update(); | |
652 | /* Wait for any kconsumerd error */ | |
653 | ret = lttcomm_recv_unix_sock(sock, &code, | |
654 | sizeof(enum lttcomm_return_code)); | |
655 | if (ret <= 0) { | |
656 | ERR("consumer closed the command socket"); | |
657 | goto error; | |
658 | } | |
659 | ||
660 | ERR("consumer return code : %s", | |
661 | lttcomm_get_readable_code(-code)); | |
662 | ||
663 | goto exit; | |
4ce514c4 | 664 | } else if (pollfd == consumer_data->metadata_fd) { |
03e43155 MD |
665 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
666 | && !(revents & LPOLLIN)) { | |
667 | ERR("consumer err metadata socket second poll error"); | |
668 | goto error; | |
669 | } | |
331744e3 JD |
670 | /* UST metadata requests */ |
671 | ret = ust_consumer_metadata_request( | |
672 | &consumer_data->metadata_sock); | |
673 | if (ret < 0) { | |
674 | ERR("Handling metadata request"); | |
675 | goto error; | |
676 | } | |
5eb91c98 | 677 | } |
42fc1d0b | 678 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 679 | } |
331744e3 | 680 | health_code_update(); |
5eb91c98 DG |
681 | } |
682 | ||
139ac872 | 683 | exit: |
1d4b027a | 684 | error: |
fdadac08 DG |
685 | /* |
686 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
687 | * thread might be using them so get exclusive access which will abort all |
688 | * other consumer command by other threads. | |
fdadac08 DG |
689 | */ |
690 | pthread_mutex_lock(&consumer_data->lock); | |
691 | ||
5c827ce0 DG |
692 | /* Immediately set the consumerd state to stopped */ |
693 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
694 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
695 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
696 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
697 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
698 | } else { | |
699 | /* Code flow error... */ | |
700 | assert(0); | |
701 | } | |
702 | ||
76d7553f MD |
703 | if (consumer_data->err_sock >= 0) { |
704 | ret = close(consumer_data->err_sock); | |
705 | if (ret) { | |
706 | PERROR("close"); | |
707 | } | |
a76cbd9f | 708 | consumer_data->err_sock = -1; |
76d7553f MD |
709 | } |
710 | if (consumer_data->cmd_sock >= 0) { | |
711 | ret = close(consumer_data->cmd_sock); | |
712 | if (ret) { | |
713 | PERROR("close"); | |
714 | } | |
a76cbd9f | 715 | consumer_data->cmd_sock = -1; |
76d7553f | 716 | } |
96544455 SS |
717 | if (consumer_data->metadata_sock.fd_ptr && |
718 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 719 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
720 | if (ret) { |
721 | PERROR("close"); | |
722 | } | |
723 | } | |
76d7553f MD |
724 | if (sock >= 0) { |
725 | ret = close(sock); | |
726 | if (ret) { | |
727 | PERROR("close"); | |
728 | } | |
729 | } | |
273ea72c | 730 | |
3bd1e081 MD |
731 | unlink(consumer_data->err_unix_sock_path); |
732 | unlink(consumer_data->cmd_unix_sock_path); | |
fdadac08 | 733 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 734 | |
fdadac08 | 735 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
736 | if (consumer_data->metadata_sock.lock) { |
737 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
738 | free(consumer_data->metadata_sock.lock); | |
739 | } | |
5eb91c98 | 740 | lttng_poll_clean(&events); |
b3530820 JG |
741 | |
742 | if (cmd_socket_wrapper) { | |
743 | consumer_destroy_socket(cmd_socket_wrapper); | |
744 | } | |
76d7553f | 745 | error_poll: |
139ac872 | 746 | if (err) { |
840cb59c | 747 | health_error(); |
139ac872 MD |
748 | ERR("Health error occurred in %s", __func__); |
749 | } | |
8782cc74 | 750 | health_unregister(health_sessiond); |
76d7553f | 751 | DBG("consumer thread cleanup completed"); |
0177d773 | 752 | |
34c1e15a MD |
753 | rcu_thread_offline(); |
754 | rcu_unregister_thread(); | |
755 | ||
5eb91c98 | 756 | return NULL; |
099e26bd DG |
757 | } |
758 | ||
b73401da | 759 | /* |
096102bd | 760 | * Setup necessary data for kernel tracer action. |
b73401da | 761 | */ |
096102bd | 762 | static int init_kernel_tracer(void) |
b73401da DG |
763 | { |
764 | int ret; | |
b73401da | 765 | |
096102bd DG |
766 | /* Modprobe lttng kernel modules */ |
767 | ret = modprobe_lttng_control(); | |
b73401da | 768 | if (ret < 0) { |
b73401da DG |
769 | goto error; |
770 | } | |
771 | ||
096102bd DG |
772 | /* Open debugfs lttng */ |
773 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
774 | if (kernel_tracer_fd < 0) { | |
775 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 776 | goto error_open; |
54d01ffb DG |
777 | } |
778 | ||
2f77fc4b | 779 | /* Validate kernel version */ |
88076e89 JD |
780 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
781 | &kernel_tracer_abi_version); | |
2f77fc4b DG |
782 | if (ret < 0) { |
783 | goto error_version; | |
b551a063 | 784 | } |
54d01ffb | 785 | |
2f77fc4b DG |
786 | ret = modprobe_lttng_data(); |
787 | if (ret < 0) { | |
788 | goto error_modules; | |
54d01ffb DG |
789 | } |
790 | ||
6e21424e JR |
791 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
792 | kernel_tracer_fd); | |
793 | if (ret < 0) { | |
794 | goto error_modules; | |
795 | } | |
796 | ||
797 | if (ret < 1) { | |
798 | WARN("Kernel tracer does not support buffer monitoring. " | |
799 | "The monitoring timer of channels in the kernel domain " | |
800 | "will be set to 0 (disabled)."); | |
801 | } | |
802 | ||
2f77fc4b DG |
803 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
804 | return 0; | |
805 | ||
806 | error_version: | |
807 | modprobe_remove_lttng_control(); | |
808 | ret = close(kernel_tracer_fd); | |
809 | if (ret) { | |
810 | PERROR("close"); | |
b551a063 | 811 | } |
2f77fc4b | 812 | kernel_tracer_fd = -1; |
f73fabfd | 813 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 814 | |
2f77fc4b DG |
815 | error_modules: |
816 | ret = close(kernel_tracer_fd); | |
817 | if (ret) { | |
818 | PERROR("close"); | |
b551a063 | 819 | } |
54d01ffb | 820 | |
2f77fc4b DG |
821 | error_open: |
822 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
823 | |
824 | error: | |
2f77fc4b DG |
825 | WARN("No kernel tracer available"); |
826 | kernel_tracer_fd = -1; | |
827 | if (!is_root) { | |
f73fabfd | 828 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 829 | } else { |
f73fabfd | 830 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 831 | } |
54d01ffb DG |
832 | } |
833 | ||
917a718d | 834 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 835 | { |
917a718d | 836 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
837 | } |
838 | ||
00e2e675 | 839 | /* |
917a718d JG |
840 | * Take an option from the getopt output and set it in the right variable to be |
841 | * used later. | |
36b588ed | 842 | * |
917a718d | 843 | * Return 0 on success else a negative value. |
00e2e675 | 844 | */ |
917a718d | 845 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 846 | { |
917a718d | 847 | int ret = 0; |
fac6795d | 848 | |
c0232ea5 | 849 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
850 | if (!arg || *arg == '\0') { |
851 | ret = -EINVAL; | |
852 | goto end; | |
853 | } | |
e8fa9fb0 MD |
854 | if (lttng_is_setuid_setgid()) { |
855 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
856 | "-c, --client-sock"); | |
857 | } else { | |
e6142f2e JG |
858 | config_string_set(&config.client_unix_sock_path, |
859 | strdup(arg)); | |
860 | if (!config.client_unix_sock_path.value) { | |
861 | ret = -ENOMEM; | |
862 | PERROR("strdup"); | |
863 | } | |
e8fa9fb0 | 864 | } |
c0232ea5 | 865 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
866 | if (!arg || *arg == '\0') { |
867 | ret = -EINVAL; | |
868 | goto end; | |
869 | } | |
e8fa9fb0 MD |
870 | if (lttng_is_setuid_setgid()) { |
871 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
872 | "-a, --apps-sock"); | |
873 | } else { | |
e6142f2e JG |
874 | config_string_set(&config.apps_unix_sock_path, |
875 | strdup(arg)); | |
876 | if (!config.apps_unix_sock_path.value) { | |
877 | ret = -ENOMEM; | |
878 | PERROR("strdup"); | |
879 | } | |
e8fa9fb0 | 880 | } |
c0232ea5 | 881 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 882 | config.daemonize = true; |
c0232ea5 | 883 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 884 | config.background = true; |
c0232ea5 | 885 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
886 | if (!arg || *arg == '\0') { |
887 | ret = -EINVAL; | |
888 | goto end; | |
889 | } | |
e8fa9fb0 MD |
890 | if (lttng_is_setuid_setgid()) { |
891 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
892 | "-g, --group"); | |
893 | } else { | |
e6142f2e JG |
894 | config_string_set(&config.tracing_group_name, |
895 | strdup(arg)); | |
896 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 897 | ret = -ENOMEM; |
e6142f2e | 898 | PERROR("strdup"); |
e8fa9fb0 | 899 | } |
db322c4d | 900 | } |
c0232ea5 | 901 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 902 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 903 | if (ret) { |
4fc83d94 | 904 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
905 | perror("exec"); |
906 | } | |
907 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 908 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
909 | fprintf(stdout, "%s\n", VERSION); |
910 | exit(EXIT_SUCCESS); | |
c0232ea5 | 911 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 912 | config.sig_parent = true; |
c0232ea5 | 913 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
914 | if (!arg || *arg == '\0') { |
915 | ret = -EINVAL; | |
916 | goto end; | |
917 | } | |
e8fa9fb0 MD |
918 | if (lttng_is_setuid_setgid()) { |
919 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
920 | "--kconsumerd-err-sock"); | |
921 | } else { | |
e6142f2e JG |
922 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
923 | strdup(arg)); | |
924 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
925 | ret = -ENOMEM; | |
926 | PERROR("strdup"); | |
927 | } | |
e8fa9fb0 | 928 | } |
c0232ea5 | 929 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
930 | if (!arg || *arg == '\0') { |
931 | ret = -EINVAL; | |
932 | goto end; | |
933 | } | |
e8fa9fb0 MD |
934 | if (lttng_is_setuid_setgid()) { |
935 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
936 | "--kconsumerd-cmd-sock"); | |
937 | } else { | |
e6142f2e JG |
938 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
939 | strdup(arg)); | |
940 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
941 | ret = -ENOMEM; | |
942 | PERROR("strdup"); | |
943 | } | |
e8fa9fb0 | 944 | } |
c0232ea5 | 945 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
946 | if (!arg || *arg == '\0') { |
947 | ret = -EINVAL; | |
948 | goto end; | |
949 | } | |
e8fa9fb0 MD |
950 | if (lttng_is_setuid_setgid()) { |
951 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
952 | "--ustconsumerd64-err-sock"); | |
953 | } else { | |
e6142f2e JG |
954 | config_string_set(&config.consumerd64_err_unix_sock_path, |
955 | strdup(arg)); | |
956 | if (!config.consumerd64_err_unix_sock_path.value) { | |
957 | ret = -ENOMEM; | |
958 | PERROR("strdup"); | |
959 | } | |
e8fa9fb0 | 960 | } |
c0232ea5 | 961 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
962 | if (!arg || *arg == '\0') { |
963 | ret = -EINVAL; | |
964 | goto end; | |
965 | } | |
e8fa9fb0 MD |
966 | if (lttng_is_setuid_setgid()) { |
967 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
968 | "--ustconsumerd64-cmd-sock"); | |
969 | } else { | |
e6142f2e JG |
970 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
971 | strdup(arg)); | |
972 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
973 | ret = -ENOMEM; | |
974 | PERROR("strdup"); | |
975 | } | |
e8fa9fb0 | 976 | } |
c0232ea5 | 977 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
978 | if (!arg || *arg == '\0') { |
979 | ret = -EINVAL; | |
980 | goto end; | |
981 | } | |
e8fa9fb0 MD |
982 | if (lttng_is_setuid_setgid()) { |
983 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
984 | "--ustconsumerd32-err-sock"); | |
985 | } else { | |
e6142f2e JG |
986 | config_string_set(&config.consumerd32_err_unix_sock_path, |
987 | strdup(arg)); | |
988 | if (!config.consumerd32_err_unix_sock_path.value) { | |
989 | ret = -ENOMEM; | |
990 | PERROR("strdup"); | |
991 | } | |
e8fa9fb0 | 992 | } |
c0232ea5 | 993 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
994 | if (!arg || *arg == '\0') { |
995 | ret = -EINVAL; | |
996 | goto end; | |
997 | } | |
e8fa9fb0 MD |
998 | if (lttng_is_setuid_setgid()) { |
999 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1000 | "--ustconsumerd32-cmd-sock"); | |
1001 | } else { | |
e6142f2e JG |
1002 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
1003 | strdup(arg)); | |
1004 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
1005 | ret = -ENOMEM; | |
1006 | PERROR("strdup"); | |
1007 | } | |
e8fa9fb0 | 1008 | } |
c0232ea5 | 1009 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 1010 | config.no_kernel = true; |
c0232ea5 | 1011 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 1012 | config.quiet = true; |
c0232ea5 | 1013 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
1014 | /* Verbose level can increase using multiple -v */ |
1015 | if (arg) { | |
13755a18 | 1016 | /* Value obtained from config file */ |
e6142f2e | 1017 | config.verbose = config_parse_value(arg); |
26296c48 | 1018 | } else { |
13755a18 | 1019 | /* -v used on command line */ |
e6142f2e | 1020 | config.verbose++; |
26296c48 | 1021 | } |
13755a18 | 1022 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
1023 | config.verbose = config.verbose < 0 ? 0 : |
1024 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 1025 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 1026 | if (arg) { |
e6142f2e | 1027 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 1028 | } else { |
e6142f2e | 1029 | config.verbose_consumer++; |
26296c48 | 1030 | } |
c0232ea5 | 1031 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
1032 | if (!arg || *arg == '\0') { |
1033 | ret = -EINVAL; | |
1034 | goto end; | |
1035 | } | |
e8fa9fb0 MD |
1036 | if (lttng_is_setuid_setgid()) { |
1037 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1038 | "--consumerd32-path"); | |
1039 | } else { | |
e6142f2e JG |
1040 | config_string_set(&config.consumerd32_bin_path, |
1041 | strdup(arg)); | |
1042 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
1043 | PERROR("strdup"); |
1044 | ret = -ENOMEM; | |
1045 | } | |
db322c4d | 1046 | } |
c0232ea5 | 1047 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
1048 | if (!arg || *arg == '\0') { |
1049 | ret = -EINVAL; | |
1050 | goto end; | |
1051 | } | |
e8fa9fb0 MD |
1052 | if (lttng_is_setuid_setgid()) { |
1053 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1054 | "--consumerd32-libdir"); | |
1055 | } else { | |
e6142f2e JG |
1056 | config_string_set(&config.consumerd32_lib_dir, |
1057 | strdup(arg)); | |
1058 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
1059 | PERROR("strdup"); |
1060 | ret = -ENOMEM; | |
1061 | } | |
db322c4d | 1062 | } |
c0232ea5 | 1063 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
1064 | if (!arg || *arg == '\0') { |
1065 | ret = -EINVAL; | |
1066 | goto end; | |
1067 | } | |
e8fa9fb0 MD |
1068 | if (lttng_is_setuid_setgid()) { |
1069 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1070 | "--consumerd64-path"); | |
1071 | } else { | |
e6142f2e JG |
1072 | config_string_set(&config.consumerd64_bin_path, |
1073 | strdup(arg)); | |
1074 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
1075 | PERROR("strdup"); |
1076 | ret = -ENOMEM; | |
1077 | } | |
db322c4d | 1078 | } |
c0232ea5 | 1079 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
1080 | if (!arg || *arg == '\0') { |
1081 | ret = -EINVAL; | |
1082 | goto end; | |
1083 | } | |
e8fa9fb0 MD |
1084 | if (lttng_is_setuid_setgid()) { |
1085 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1086 | "--consumerd64-libdir"); | |
1087 | } else { | |
e6142f2e JG |
1088 | config_string_set(&config.consumerd64_lib_dir, |
1089 | strdup(arg)); | |
1090 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
1091 | PERROR("strdup"); |
1092 | ret = -ENOMEM; | |
1093 | } | |
db322c4d | 1094 | } |
c0232ea5 | 1095 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
1096 | if (!arg || *arg == '\0') { |
1097 | ret = -EINVAL; | |
1098 | goto end; | |
1099 | } | |
e8fa9fb0 MD |
1100 | if (lttng_is_setuid_setgid()) { |
1101 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1102 | "-p, --pidfile"); | |
1103 | } else { | |
e6142f2e JG |
1104 | config_string_set(&config.pid_file_path, strdup(arg)); |
1105 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
1106 | PERROR("strdup"); |
1107 | ret = -ENOMEM; | |
1108 | } | |
db322c4d | 1109 | } |
c0232ea5 | 1110 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
1111 | if (!arg || *arg == '\0') { |
1112 | ret = -EINVAL; | |
1113 | goto end; | |
1114 | } | |
e8fa9fb0 MD |
1115 | if (lttng_is_setuid_setgid()) { |
1116 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1117 | "--agent-tcp-port"); | |
1118 | } else { | |
1119 | unsigned long v; | |
4d076222 | 1120 | |
e8fa9fb0 MD |
1121 | errno = 0; |
1122 | v = strtoul(arg, NULL, 0); | |
1123 | if (errno != 0 || !isdigit(arg[0])) { | |
1124 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
1125 | return -1; | |
1126 | } | |
1127 | if (v == 0 || v >= 65535) { | |
1128 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
1129 | return -1; | |
1130 | } | |
2288467f JG |
1131 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
1132 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 1133 | } |
c0232ea5 | 1134 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
1135 | if (!arg || *arg == '\0') { |
1136 | ret = -EINVAL; | |
1137 | goto end; | |
1138 | } | |
e8fa9fb0 MD |
1139 | if (lttng_is_setuid_setgid()) { |
1140 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1141 | "-l, --load"); | |
1142 | } else { | |
e6142f2e JG |
1143 | config_string_set(&config.load_session_path, strdup(arg)); |
1144 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
1145 | PERROR("strdup"); |
1146 | ret = -ENOMEM; | |
1147 | } | |
ef367a93 | 1148 | } |
c0232ea5 | 1149 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
1150 | if (!arg || *arg == '\0') { |
1151 | ret = -EINVAL; | |
1152 | goto end; | |
1153 | } | |
e8fa9fb0 MD |
1154 | if (lttng_is_setuid_setgid()) { |
1155 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1156 | "--kmod-probes"); | |
1157 | } else { | |
e6142f2e JG |
1158 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
1159 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
1160 | PERROR("strdup"); |
1161 | ret = -ENOMEM; | |
1162 | } | |
c9d42407 | 1163 | } |
c0232ea5 | 1164 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
1165 | if (!arg || *arg == '\0') { |
1166 | ret = -EINVAL; | |
1167 | goto end; | |
1168 | } | |
e8fa9fb0 MD |
1169 | if (lttng_is_setuid_setgid()) { |
1170 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1171 | "--extra-kmod-probes"); | |
1172 | } else { | |
e6142f2e JG |
1173 | config_string_set(&config.kmod_extra_probes_list, |
1174 | strdup(arg)); | |
1175 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
1176 | PERROR("strdup"); |
1177 | ret = -ENOMEM; | |
1178 | } | |
db322c4d | 1179 | } |
c0232ea5 JG |
1180 | } else if (string_match(optname, "config") || opt == 'f') { |
1181 | /* This is handled in set_options() thus silent skip. */ | |
1182 | goto end; | |
1183 | } else { | |
26296c48 JG |
1184 | /* Unknown option or other error. |
1185 | * Error is printed by getopt, just return */ | |
1186 | ret = -1; | |
1187 | } | |
1188 | ||
f3dd7ce7 | 1189 | end: |
c5d350b2 JG |
1190 | if (ret == -EINVAL) { |
1191 | const char *opt_name = "unknown"; | |
1192 | int i; | |
1193 | ||
1194 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
1195 | i++) { | |
1196 | if (opt == long_options[i].val) { | |
1197 | opt_name = long_options[i].name; | |
1198 | break; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
1203 | opt_name); | |
1204 | } | |
f3dd7ce7 | 1205 | |
26296c48 JG |
1206 | return ret; |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 1211 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
1212 | * return value conventions. |
1213 | */ | |
1214 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
1215 | { | |
1216 | int ret = 0, i; | |
1217 | ||
1218 | if (!entry || !entry->name || !entry->value) { | |
1219 | ret = -EINVAL; | |
1220 | goto end; | |
1221 | } | |
1222 | ||
1223 | /* Check if the option is to be ignored */ | |
1224 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
1225 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
1226 | goto end; | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
1231 | i++) { | |
1232 | ||
1233 | /* Ignore if not fully matched. */ | |
1234 | if (strcmp(entry->name, long_options[i].name)) { | |
1235 | continue; | |
1236 | } | |
1237 | ||
1238 | /* | |
1239 | * If the option takes no argument on the command line, we have to | |
1240 | * check if the value is "true". We support non-zero numeric values, | |
1241 | * true, on and yes. | |
1242 | */ | |
1243 | if (!long_options[i].has_arg) { | |
1244 | ret = config_parse_value(entry->value); | |
1245 | if (ret <= 0) { | |
1246 | if (ret) { | |
1247 | WARN("Invalid configuration value \"%s\" for option %s", | |
1248 | entry->value, entry->name); | |
1249 | } | |
1250 | /* False, skip boolean config option. */ | |
1251 | goto end; | |
4d076222 | 1252 | } |
26296c48 JG |
1253 | } |
1254 | ||
1255 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
1256 | goto end; | |
1257 | } | |
1258 | ||
1259 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
1260 | ||
1261 | end: | |
1262 | return ret; | |
1263 | } | |
1264 | ||
1265 | /* | |
1266 | * daemon configuration loading and argument parsing | |
1267 | */ | |
1268 | static int set_options(int argc, char **argv) | |
1269 | { | |
1270 | int ret = 0, c = 0, option_index = 0; | |
1271 | int orig_optopt = optopt, orig_optind = optind; | |
1272 | char *optstring; | |
1273 | const char *config_path = NULL; | |
1274 | ||
1275 | optstring = utils_generate_optstring(long_options, | |
1276 | sizeof(long_options) / sizeof(struct option)); | |
1277 | if (!optstring) { | |
1278 | ret = -ENOMEM; | |
1279 | goto end; | |
1280 | } | |
1281 | ||
1282 | /* Check for the --config option */ | |
1283 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
1284 | &option_index)) != -1) { | |
1285 | if (c == '?') { | |
1286 | ret = -EINVAL; | |
1287 | goto end; | |
1288 | } else if (c != 'f') { | |
1289 | /* if not equal to --config option. */ | |
1290 | continue; | |
1291 | } | |
1292 | ||
e8fa9fb0 MD |
1293 | if (lttng_is_setuid_setgid()) { |
1294 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1295 | "-f, --config"); | |
1296 | } else { | |
1297 | config_path = utils_expand_path(optarg); | |
1298 | if (!config_path) { | |
1299 | ERR("Failed to resolve path: %s", optarg); | |
1300 | } | |
26296c48 JG |
1301 | } |
1302 | } | |
1303 | ||
1304 | ret = config_get_section_entries(config_path, config_section_name, | |
1305 | config_entry_handler, NULL); | |
1306 | if (ret) { | |
1307 | if (ret > 0) { | |
1308 | ERR("Invalid configuration option at line %i", ret); | |
1309 | ret = -1; | |
1310 | } | |
1311 | goto end; | |
1312 | } | |
1313 | ||
1314 | /* Reset getopt's global state */ | |
1315 | optopt = orig_optopt; | |
1316 | optind = orig_optind; | |
1317 | while (1) { | |
c0232ea5 JG |
1318 | option_index = -1; |
1319 | /* | |
1320 | * getopt_long() will not set option_index if it encounters a | |
1321 | * short option. | |
1322 | */ | |
1323 | c = getopt_long(argc, argv, optstring, long_options, | |
1324 | &option_index); | |
26296c48 | 1325 | if (c == -1) { |
4d076222 DG |
1326 | break; |
1327 | } | |
26296c48 | 1328 | |
c0232ea5 JG |
1329 | /* |
1330 | * Pass NULL as the long option name if popt left the index | |
1331 | * unset. | |
1332 | */ | |
1333 | ret = set_option(c, optarg, | |
1334 | option_index < 0 ? NULL : | |
1335 | long_options[option_index].name); | |
26296c48 JG |
1336 | if (ret < 0) { |
1337 | break; | |
fac6795d DG |
1338 | } |
1339 | } | |
1340 | ||
26296c48 JG |
1341 | end: |
1342 | free(optstring); | |
1343 | return ret; | |
fac6795d DG |
1344 | } |
1345 | ||
f472090a JG |
1346 | /* |
1347 | * Create lockfile using the rundir and return its fd. | |
1348 | */ | |
1349 | static int create_lockfile(void) | |
1350 | { | |
1351 | return utils_create_lock_file(config.lock_file_path.value); | |
1352 | } | |
1353 | ||
fac6795d | 1354 | /* |
54d01ffb DG |
1355 | * Check if the global socket is available, and if a daemon is answering at the |
1356 | * other side. If yes, error is returned. | |
f472090a JG |
1357 | * |
1358 | * Also attempts to create and hold the lock file. | |
fac6795d | 1359 | */ |
cf3af59e | 1360 | static int check_existing_daemon(void) |
fac6795d | 1361 | { |
f472090a JG |
1362 | int ret = 0; |
1363 | ||
7d8234d9 | 1364 | /* Is there anybody out there ? */ |
099e26bd | 1365 | if (lttng_session_daemon_alive()) { |
f472090a JG |
1366 | ret = -EEXIST; |
1367 | goto end; | |
099e26bd | 1368 | } |
b09c7c76 | 1369 | |
f472090a JG |
1370 | lockfile_fd = create_lockfile(); |
1371 | if (lockfile_fd < 0) { | |
1372 | ret = -EEXIST; | |
1373 | goto end; | |
1374 | } | |
1375 | end: | |
1376 | return ret; | |
1377 | } | |
1378 | ||
1379 | static void sessiond_cleanup_lock_file(void) | |
1380 | { | |
1381 | int ret; | |
1382 | ||
1383 | /* | |
1384 | * Cleanup lock file by deleting it and finaly closing it which will | |
1385 | * release the file system lock. | |
1386 | */ | |
1387 | if (lockfile_fd >= 0) { | |
1388 | ret = remove(config.lock_file_path.value); | |
1389 | if (ret < 0) { | |
1390 | PERROR("remove lock file"); | |
1391 | } | |
1392 | ret = close(lockfile_fd); | |
1393 | if (ret < 0) { | |
1394 | PERROR("close lock file"); | |
1395 | } | |
1396 | } | |
fac6795d DG |
1397 | } |
1398 | ||
fac6795d | 1399 | /* |
d063d709 | 1400 | * Set the tracing group gid onto the client socket. |
5e16da05 | 1401 | * |
d063d709 | 1402 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 1403 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 1404 | */ |
be040666 | 1405 | static int set_permissions(char *rundir) |
fac6795d DG |
1406 | { |
1407 | int ret; | |
996b65c8 | 1408 | gid_t gid; |
fac6795d | 1409 | |
e6142f2e | 1410 | gid = utils_get_group_id(config.tracing_group_name.value); |
fac6795d | 1411 | |
d6f42150 | 1412 | /* Set lttng run dir */ |
be040666 | 1413 | ret = chown(rundir, 0, gid); |
d6f42150 | 1414 | if (ret < 0) { |
be040666 | 1415 | ERR("Unable to set group on %s", rundir); |
76d7553f | 1416 | PERROR("chown"); |
d6f42150 DG |
1417 | } |
1418 | ||
6c71277b MD |
1419 | /* |
1420 | * Ensure all applications and tracing group can search the run | |
1421 | * dir. Allow everyone to read the directory, since it does not | |
1422 | * buy us anything to hide its content. | |
1423 | */ | |
1424 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
1425 | if (ret < 0) { |
1426 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 1427 | PERROR("chmod"); |
d1613cf5 JN |
1428 | } |
1429 | ||
d6f42150 | 1430 | /* lttng client socket path */ |
e6142f2e | 1431 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 1432 | if (ret < 0) { |
e6142f2e | 1433 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 1434 | PERROR("chown"); |
d6f42150 DG |
1435 | } |
1436 | ||
3bd1e081 | 1437 | /* kconsumer error socket path */ |
6c71277b | 1438 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 1439 | if (ret < 0) { |
3bd1e081 | 1440 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 1441 | PERROR("chown"); |
3bd1e081 MD |
1442 | } |
1443 | ||
7753dea8 | 1444 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 1445 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
1446 | if (ret < 0) { |
1447 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 1448 | PERROR("chown"); |
7753dea8 MD |
1449 | } |
1450 | ||
1451 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 1452 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 1453 | if (ret < 0) { |
7753dea8 | 1454 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 1455 | PERROR("chown"); |
fac6795d DG |
1456 | } |
1457 | ||
d6f42150 | 1458 | DBG("All permissions are set"); |
e07ae692 | 1459 | |
fac6795d DG |
1460 | return ret; |
1461 | } | |
1462 | ||
d6f42150 | 1463 | /* |
d063d709 | 1464 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 1465 | */ |
e6142f2e | 1466 | static int create_lttng_rundir(void) |
d6f42150 DG |
1467 | { |
1468 | int ret; | |
1469 | ||
e6142f2e | 1470 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 1471 | |
e6142f2e | 1472 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 1473 | if (ret < 0) { |
b1f11e69 | 1474 | if (errno != EEXIST) { |
e6142f2e | 1475 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
1476 | goto error; |
1477 | } else { | |
1478 | ret = 0; | |
1479 | } | |
d6f42150 DG |
1480 | } |
1481 | ||
1482 | error: | |
1483 | return ret; | |
1484 | } | |
1485 | ||
1486 | /* | |
e6142f2e | 1487 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 1488 | * session daemon. |
d6f42150 | 1489 | */ |
e6142f2e | 1490 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
1491 | { |
1492 | int ret; | |
e6142f2e | 1493 | char *path = NULL; |
d6f42150 | 1494 | |
6c71277b | 1495 | switch (consumer_data->type) { |
7753dea8 | 1496 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 1497 | path = config.kconsumerd_path.value; |
7753dea8 MD |
1498 | break; |
1499 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 1500 | path = config.consumerd64_path.value; |
7753dea8 MD |
1501 | break; |
1502 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 1503 | path = config.consumerd32_path.value; |
7753dea8 MD |
1504 | break; |
1505 | default: | |
1506 | ERR("Consumer type unknown"); | |
1507 | ret = -EINVAL; | |
1508 | goto error; | |
d6f42150 | 1509 | } |
e6142f2e | 1510 | assert(path); |
d6f42150 | 1511 | |
67e40797 DG |
1512 | DBG2("Creating consumer directory: %s", path); |
1513 | ||
6c71277b | 1514 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
1515 | if (ret < 0 && errno != EEXIST) { |
1516 | PERROR("mkdir"); | |
1517 | ERR("Failed to create %s", path); | |
1518 | goto error; | |
d6f42150 | 1519 | } |
6c71277b | 1520 | if (is_root) { |
e6142f2e | 1521 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
6c71277b MD |
1522 | if (ret < 0) { |
1523 | ERR("Unable to set group on %s", path); | |
1524 | PERROR("chown"); | |
1525 | goto error; | |
1526 | } | |
1527 | } | |
d6f42150 | 1528 | |
e6142f2e | 1529 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
1530 | consumer_data->err_sock = |
1531 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
1532 | if (consumer_data->err_sock < 0) { | |
1533 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
1534 | ret = -1; |
1535 | goto error; | |
1536 | } | |
1537 | ||
a24f05ab MD |
1538 | /* |
1539 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1540 | * show must go on. | |
1541 | */ | |
1542 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
1543 | if (ret < 0) { | |
1544 | PERROR("utils_set_fd_cloexec"); | |
1545 | /* continue anyway */ | |
1546 | } | |
1547 | ||
d6f42150 | 1548 | /* File permission MUST be 660 */ |
3bd1e081 | 1549 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 1550 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 1551 | if (ret < 0) { |
3bd1e081 | 1552 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 1553 | PERROR("chmod"); |
d6f42150 DG |
1554 | goto error; |
1555 | } | |
1556 | ||
1557 | error: | |
1558 | return ret; | |
1559 | } | |
1560 | ||
fac6795d | 1561 | /* |
d063d709 | 1562 | * Signal handler for the daemon |
cf3af59e | 1563 | * |
54d01ffb DG |
1564 | * Simply stop all worker threads, leaving main() return gracefully after |
1565 | * joining all threads and calling cleanup(). | |
fac6795d DG |
1566 | */ |
1567 | static void sighandler(int sig) | |
1568 | { | |
1569 | switch (sig) { | |
cf3af59e | 1570 | case SIGINT: |
af87c45a | 1571 | DBG("SIGINT caught"); |
cf3af59e MD |
1572 | stop_threads(); |
1573 | break; | |
1574 | case SIGTERM: | |
af87c45a | 1575 | DBG("SIGTERM caught"); |
cf3af59e MD |
1576 | stop_threads(); |
1577 | break; | |
0bb7724a DG |
1578 | case SIGUSR1: |
1579 | CMM_STORE_SHARED(recv_child_signal, 1); | |
1580 | break; | |
cf3af59e MD |
1581 | default: |
1582 | break; | |
fac6795d | 1583 | } |
fac6795d DG |
1584 | } |
1585 | ||
1586 | /* | |
d063d709 | 1587 | * Setup signal handler for : |
1d4b027a | 1588 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 1589 | */ |
1d4b027a | 1590 | static int set_signal_handler(void) |
fac6795d | 1591 | { |
1d4b027a DG |
1592 | int ret = 0; |
1593 | struct sigaction sa; | |
1594 | sigset_t sigset; | |
fac6795d | 1595 | |
1d4b027a | 1596 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 1597 | PERROR("sigemptyset"); |
1d4b027a DG |
1598 | return ret; |
1599 | } | |
d6f42150 | 1600 | |
1d4b027a DG |
1601 | sa.sa_mask = sigset; |
1602 | sa.sa_flags = 0; | |
0072e5e2 MD |
1603 | |
1604 | sa.sa_handler = sighandler; | |
1d4b027a | 1605 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 1606 | PERROR("sigaction"); |
1d4b027a | 1607 | return ret; |
d6f42150 DG |
1608 | } |
1609 | ||
1d4b027a | 1610 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 1611 | PERROR("sigaction"); |
1d4b027a | 1612 | return ret; |
d6f42150 | 1613 | } |
aaf26714 | 1614 | |
0072e5e2 | 1615 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 1616 | PERROR("sigaction"); |
1d4b027a | 1617 | return ret; |
8c0faa1d DG |
1618 | } |
1619 | ||
0072e5e2 MD |
1620 | sa.sa_handler = SIG_IGN; |
1621 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
1622 | PERROR("sigaction"); |
1623 | return ret; | |
1624 | } | |
1625 | ||
1626 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
1627 | |
1628 | return ret; | |
fac6795d DG |
1629 | } |
1630 | ||
f3ed775e | 1631 | /* |
d063d709 | 1632 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 1633 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
1634 | */ |
1635 | static void set_ulimit(void) | |
1636 | { | |
1637 | int ret; | |
1638 | struct rlimit lim; | |
1639 | ||
514bb9f0 | 1640 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
1641 | lim.rlim_cur = 65535; |
1642 | lim.rlim_max = 65535; | |
1643 | ||
1644 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
1645 | if (ret < 0) { | |
76d7553f | 1646 | PERROR("failed to set open files limit"); |
f3ed775e DG |
1647 | } |
1648 | } | |
1649 | ||
4a15001e | 1650 | static int write_pidfile(void) |
35f90c40 | 1651 | { |
e6142f2e | 1652 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
1653 | } |
1654 | ||
e6142f2e JG |
1655 | static int set_clock_plugin_env(void) |
1656 | { | |
1657 | int ret = 0; | |
1658 | char *env_value = NULL; | |
cd9290dd | 1659 | |
e6142f2e JG |
1660 | if (!config.lttng_ust_clock_plugin.value) { |
1661 | goto end; | |
1662 | } | |
1663 | ||
1664 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
1665 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 1666 | if (ret < 0) { |
e6142f2e JG |
1667 | PERROR("asprintf"); |
1668 | goto end; | |
cd9290dd DG |
1669 | } |
1670 | ||
e6142f2e JG |
1671 | ret = putenv(env_value); |
1672 | if (ret) { | |
1673 | free(env_value); | |
1674 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
1675 | goto end; | |
1676 | } | |
cd9290dd | 1677 | |
e6142f2e JG |
1678 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
1679 | config.lttng_ust_clock_plugin.value); | |
1680 | end: | |
4a15001e | 1681 | return ret; |
ef367a93 JG |
1682 | } |
1683 | ||
99d688f2 JG |
1684 | static void destroy_all_sessions_and_wait(void) |
1685 | { | |
1686 | struct ltt_session *session, *tmp; | |
1687 | struct ltt_session_list *session_list; | |
1688 | ||
1689 | session_list = session_get_list(); | |
1690 | DBG("Initiating destruction of all sessions"); | |
1691 | ||
1692 | if (!session_list) { | |
1693 | return; | |
1694 | } | |
1695 | ||
99d688f2 JG |
1696 | session_lock_list(); |
1697 | /* Initiate the destruction of all sessions. */ | |
1698 | cds_list_for_each_entry_safe(session, tmp, | |
1699 | &session_list->head, list) { | |
1700 | if (!session_get(session)) { | |
1701 | continue; | |
1702 | } | |
1703 | ||
1704 | session_lock(session); | |
1705 | if (session->destroyed) { | |
1706 | goto unlock_session; | |
1707 | } | |
63763958 | 1708 | (void) cmd_stop_trace(session); |
99d688f2 JG |
1709 | (void) cmd_destroy_session(session, |
1710 | notification_thread_handle); | |
1711 | unlock_session: | |
1712 | session_unlock(session); | |
1713 | session_put(session); | |
1714 | } | |
1715 | session_unlock_list(); | |
1716 | ||
1717 | /* Wait for the destruction of all sessions to complete. */ | |
1718 | DBG("Waiting for the destruction of all sessions to complete"); | |
1719 | session_list_wait_empty(); | |
1720 | DBG("Destruction of all sessions completed"); | |
1721 | } | |
1722 | ||
fac6795d DG |
1723 | /* |
1724 | * main | |
1725 | */ | |
1726 | int main(int argc, char **argv) | |
1727 | { | |
4a15001e | 1728 | int ret = 0, retval = 0; |
e6142f2e | 1729 | const char *env_app_timeout; |
b3530820 JG |
1730 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
1731 | *ust64_channel_monitor_pipe = NULL, | |
1732 | *kernel_channel_monitor_pipe = NULL; | |
a3707772 | 1733 | struct lttng_thread *ht_cleanup_thread = NULL; |
bc26e826 | 1734 | struct timer_thread_parameters timer_thread_parameters; |
64d9b072 JG |
1735 | /* Rotation thread handle. */ |
1736 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
1737 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
1738 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
917a718d | 1739 | struct lttng_thread *client_thread = NULL; |
fac6795d | 1740 | |
335a95b7 MD |
1741 | init_kernel_workarounds(); |
1742 | ||
f6a9efaa DG |
1743 | rcu_register_thread(); |
1744 | ||
4a15001e MD |
1745 | if (set_signal_handler()) { |
1746 | retval = -1; | |
1747 | goto exit_set_signal_handler; | |
0bb7724a DG |
1748 | } |
1749 | ||
92816cc3 | 1750 | if (timer_signal_init()) { |
d086f507 JD |
1751 | retval = -1; |
1752 | goto exit_set_signal_handler; | |
1753 | } | |
1754 | ||
12744796 DG |
1755 | page_size = sysconf(_SC_PAGESIZE); |
1756 | if (page_size < 0) { | |
1757 | PERROR("sysconf _SC_PAGESIZE"); | |
1758 | page_size = LONG_MAX; | |
1759 | WARN("Fallback page size to %ld", page_size); | |
1760 | } | |
1761 | ||
e6142f2e JG |
1762 | ret = sessiond_config_init(&config); |
1763 | if (ret) { | |
1764 | retval = -1; | |
1765 | goto exit_set_signal_handler; | |
1766 | } | |
1767 | ||
2788b494 JR |
1768 | /* |
1769 | * Init config from environment variables. | |
1770 | * Command line option override env configuration per-doc. Do env first. | |
1771 | */ | |
1772 | sessiond_config_apply_env_config(&config); | |
1773 | ||
4a15001e MD |
1774 | /* |
1775 | * Parse arguments and load the daemon configuration file. | |
1776 | * | |
1777 | * We have an exit_options exit path to free memory reserved by | |
1778 | * set_options. This is needed because the rest of sessiond_cleanup() | |
1779 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
1780 | * depends on set_options. | |
1781 | */ | |
fac6795d | 1782 | progname = argv[0]; |
4a15001e MD |
1783 | if (set_options(argc, argv)) { |
1784 | retval = -1; | |
1785 | goto exit_options; | |
fac6795d DG |
1786 | } |
1787 | ||
e6142f2e JG |
1788 | /* |
1789 | * Resolve all paths received as arguments, configuration option, or | |
1790 | * through environment variable as absolute paths. This is necessary | |
1791 | * since daemonizing causes the sessiond's current working directory | |
1792 | * to '/'. | |
1793 | */ | |
1794 | ret = sessiond_config_resolve_paths(&config); | |
1795 | if (ret) { | |
1796 | goto exit_options; | |
1797 | } | |
1798 | ||
1799 | /* Apply config. */ | |
1800 | lttng_opt_verbose = config.verbose; | |
1801 | lttng_opt_quiet = config.quiet; | |
1802 | kconsumer_data.err_unix_sock_path = | |
1803 | config.kconsumerd_err_unix_sock_path.value; | |
1804 | kconsumer_data.cmd_unix_sock_path = | |
1805 | config.kconsumerd_cmd_unix_sock_path.value; | |
1806 | ustconsumer32_data.err_unix_sock_path = | |
1807 | config.consumerd32_err_unix_sock_path.value; | |
1808 | ustconsumer32_data.cmd_unix_sock_path = | |
1809 | config.consumerd32_cmd_unix_sock_path.value; | |
1810 | ustconsumer64_data.err_unix_sock_path = | |
1811 | config.consumerd64_err_unix_sock_path.value; | |
1812 | ustconsumer64_data.cmd_unix_sock_path = | |
1813 | config.consumerd64_cmd_unix_sock_path.value; | |
1814 | set_clock_plugin_env(); | |
1815 | ||
1816 | sessiond_config_log(&config); | |
1817 | ||
f472090a JG |
1818 | if (create_lttng_rundir()) { |
1819 | retval = -1; | |
1820 | goto exit_options; | |
1821 | } | |
1822 | ||
1823 | /* Abort launch if a session daemon is already running. */ | |
1824 | if (check_existing_daemon()) { | |
1825 | ERR("A session daemon is already running."); | |
1826 | retval = -1; | |
1827 | goto exit_options; | |
1828 | } | |
1829 | ||
fac6795d | 1830 | /* Daemonize */ |
e6142f2e | 1831 | if (config.daemonize || config.background) { |
ceed52b5 MD |
1832 | int i; |
1833 | ||
72dd7491 | 1834 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 1835 | !config.background); |
53094c05 | 1836 | if (ret < 0) { |
4a15001e MD |
1837 | retval = -1; |
1838 | goto exit_options; | |
53094c05 | 1839 | } |
0bb7724a | 1840 | |
ceed52b5 | 1841 | /* |
0bb7724a | 1842 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 1843 | * closed, in case we are called with more opened file |
f472090a | 1844 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
1845 | */ |
1846 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
1847 | if (i == lockfile_fd) { |
1848 | continue; | |
1849 | } | |
ceed52b5 MD |
1850 | (void) close(i); |
1851 | } | |
1852 | } | |
1853 | ||
7567352f MD |
1854 | if (run_as_create_worker(argv[0]) < 0) { |
1855 | goto exit_create_run_as_worker_cleanup; | |
1856 | } | |
1857 | ||
4a15001e MD |
1858 | /* |
1859 | * Starting from here, we can create threads. This needs to be after | |
1860 | * lttng_daemonize due to RCU. | |
1861 | */ | |
1862 | ||
1863 | /* | |
1864 | * Initialize the health check subsystem. This call should set the | |
1865 | * appropriate time values. | |
1866 | */ | |
1867 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
1868 | if (!health_sessiond) { | |
1869 | PERROR("health_app_create error"); | |
1870 | retval = -1; | |
1871 | goto exit_health_sessiond_cleanup; | |
1872 | } | |
1873 | ||
4a15001e | 1874 | /* Create thread to clean up RCU hash tables */ |
a3707772 JG |
1875 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
1876 | if (!ht_cleanup_thread) { | |
4a15001e MD |
1877 | retval = -1; |
1878 | goto exit_ht_cleanup; | |
1879 | } | |
1880 | ||
ceed52b5 | 1881 | /* Create thread quit pipe */ |
a7333da7 | 1882 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e MD |
1883 | retval = -1; |
1884 | goto exit_init_data; | |
fac6795d DG |
1885 | } |
1886 | ||
1887 | /* Check if daemon is UID = 0 */ | |
1888 | is_root = !getuid(); | |
fac6795d | 1889 | if (is_root) { |
67e40797 | 1890 | /* Create global run dir with root access */ |
67e40797 | 1891 | |
b3530820 JG |
1892 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
1893 | if (!kernel_channel_monitor_pipe) { | |
1894 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
1895 | retval = -1; | |
1896 | goto exit_init_data; | |
1897 | } | |
1898 | kconsumer_data.channel_monitor_pipe = | |
1899 | lttng_pipe_release_writefd( | |
1900 | kernel_channel_monitor_pipe); | |
1901 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
1902 | retval = -1; | |
1903 | goto exit_init_data; | |
1904 | } | |
fac6795d DG |
1905 | } |
1906 | ||
5c827ce0 DG |
1907 | /* Set consumer initial state */ |
1908 | kernel_consumerd_state = CONSUMER_STOPPED; | |
1909 | ust_consumerd_state = CONSUMER_STOPPED; | |
1910 | ||
b3530820 JG |
1911 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
1912 | if (!ust32_channel_monitor_pipe) { | |
1913 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
1914 | retval = -1; | |
1915 | goto exit_init_data; | |
1916 | } | |
1917 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1918 | ust32_channel_monitor_pipe); | |
1919 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
1920 | retval = -1; | |
1921 | goto exit_init_data; | |
1922 | } | |
62c43103 | 1923 | |
d086f507 | 1924 | /* |
92816cc3 JG |
1925 | * The rotation_thread_timer_queue structure is shared between the |
1926 | * sessiond timer thread and the rotation thread. The main thread keeps | |
1927 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 1928 | */ |
92816cc3 | 1929 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
1930 | if (!rotation_timer_queue) { |
1931 | retval = -1; | |
1932 | goto exit_init_data; | |
1933 | } | |
bc26e826 JG |
1934 | timer_thread_parameters.rotation_thread_job_queue = |
1935 | rotation_timer_queue; | |
67e40797 | 1936 | |
b3530820 JG |
1937 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
1938 | if (!ust64_channel_monitor_pipe) { | |
1939 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
1940 | retval = -1; | |
1941 | goto exit_init_data; | |
1942 | } | |
1943 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1944 | ust64_channel_monitor_pipe); | |
1945 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
1946 | retval = -1; | |
1947 | goto exit_init_data; | |
1948 | } | |
847177cd | 1949 | |
1427f9b2 DG |
1950 | /* |
1951 | * Init UST app hash table. Alloc hash table before this point since | |
1952 | * cleanup() can get called after that point. | |
1953 | */ | |
4a15001e | 1954 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 1955 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
1956 | retval = -1; |
1957 | goto exit_init_data; | |
1958 | } | |
1427f9b2 | 1959 | |
6a4e4039 JG |
1960 | /* |
1961 | * Initialize agent app hash table. We allocate the hash table here | |
1962 | * since cleanup() can get called after this point. | |
1963 | */ | |
1964 | if (agent_app_ht_alloc()) { | |
1965 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
1966 | retval = -1; |
1967 | goto exit_init_data; | |
f20baf8e DG |
1968 | } |
1969 | ||
a88df331 DG |
1970 | /* |
1971 | * These actions must be executed as root. We do that *after* setting up | |
1972 | * the sockets path because we MUST make the check for another daemon using | |
1973 | * those paths *before* trying to set the kernel consumer sockets and init | |
1974 | * kernel tracer. | |
1975 | */ | |
1976 | if (is_root) { | |
e6142f2e | 1977 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e MD |
1978 | retval = -1; |
1979 | goto exit_init_data; | |
7753dea8 MD |
1980 | } |
1981 | ||
a88df331 | 1982 | /* Setup kernel tracer */ |
e6142f2e | 1983 | if (!config.no_kernel) { |
4fba7219 | 1984 | init_kernel_tracer(); |
89588270 DG |
1985 | if (kernel_tracer_fd >= 0) { |
1986 | ret = syscall_init_table(); | |
1987 | if (ret < 0) { | |
4a15001e MD |
1988 | ERR("Unable to populate syscall table. " |
1989 | "Syscall tracing won't work " | |
1990 | "for this session daemon."); | |
89588270 | 1991 | } |
834978fd | 1992 | } |
4fba7219 | 1993 | } |
a88df331 DG |
1994 | |
1995 | /* Set ulimit for open files */ | |
1996 | set_ulimit(); | |
fac6795d | 1997 | } |
4063050c MD |
1998 | /* init lttng_fd tracking must be done after set_ulimit. */ |
1999 | lttng_fd_init(); | |
fac6795d | 2000 | |
e6142f2e | 2001 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e MD |
2002 | retval = -1; |
2003 | goto exit_init_data; | |
67e40797 DG |
2004 | } |
2005 | ||
e6142f2e | 2006 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e MD |
2007 | retval = -1; |
2008 | goto exit_init_data; | |
67e40797 DG |
2009 | } |
2010 | ||
fac6795d | 2011 | /* Set credentials to socket */ |
e6142f2e | 2012 | if (is_root && set_permissions(config.rundir.value)) { |
4a15001e MD |
2013 | retval = -1; |
2014 | goto exit_init_data; | |
fac6795d DG |
2015 | } |
2016 | ||
5b8719f5 | 2017 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 2018 | if (config.sig_parent) { |
5b8719f5 DG |
2019 | ppid = getppid(); |
2020 | } | |
2021 | ||
7a485870 | 2022 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 2023 | if (is_root && !config.no_kernel) { |
4a15001e MD |
2024 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
2025 | retval = -1; | |
2026 | goto exit_init_data; | |
6620da75 | 2027 | } |
7a485870 DG |
2028 | } |
2029 | ||
099e26bd | 2030 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
2031 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
2032 | retval = -1; | |
2033 | goto exit_init_data; | |
099e26bd DG |
2034 | } |
2035 | ||
d0b96690 | 2036 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
2037 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
2038 | retval = -1; | |
2039 | goto exit_init_data; | |
d0b96690 DG |
2040 | } |
2041 | ||
7972aab2 DG |
2042 | /* Initialize global buffer per UID and PID registry. */ |
2043 | buffer_reg_init_uid_registry(); | |
2044 | buffer_reg_init_pid_registry(); | |
2045 | ||
099e26bd | 2046 | /* Init UST command queue. */ |
8bdee6e2 | 2047 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 2048 | |
2f77fc4b | 2049 | cmd_init(); |
00e2e675 | 2050 | |
ae9e45b3 DG |
2051 | /* Check for the application socket timeout env variable. */ |
2052 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
2053 | if (env_app_timeout) { | |
e6142f2e | 2054 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 2055 | } else { |
e6142f2e | 2056 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
2057 | } |
2058 | ||
4a15001e MD |
2059 | ret = write_pidfile(); |
2060 | if (ret) { | |
2061 | ERR("Error in write_pidfile"); | |
2062 | retval = -1; | |
2063 | goto exit_init_data; | |
2064 | } | |
35f90c40 | 2065 | |
554831e7 MD |
2066 | /* Initialize communication library */ |
2067 | lttcomm_init(); | |
4a15001e | 2068 | /* Initialize TCP timeout values */ |
d831c249 | 2069 | lttcomm_inet_init(); |
554831e7 | 2070 | |
12b4fb37 | 2071 | /* Create health-check thread. */ |
173fca4f | 2072 | if (!launch_health_management_thread()) { |
4a15001e | 2073 | retval = -1; |
44a5e5eb DG |
2074 | goto exit_health; |
2075 | } | |
2076 | ||
b3530820 JG |
2077 | /* notification_thread_data acquires the pipes' read side. */ |
2078 | notification_thread_handle = notification_thread_handle_create( | |
2079 | ust32_channel_monitor_pipe, | |
2080 | ust64_channel_monitor_pipe, | |
c8a9de5a | 2081 | kernel_channel_monitor_pipe); |
b3530820 JG |
2082 | if (!notification_thread_handle) { |
2083 | retval = -1; | |
2084 | ERR("Failed to create notification thread shared data"); | |
b3530820 JG |
2085 | goto exit_notification; |
2086 | } | |
2087 | ||
2088 | /* Create notification thread. */ | |
c8a9de5a | 2089 | if (!launch_notification_thread(notification_thread_handle)) { |
b3530820 | 2090 | retval = -1; |
b3530820 JG |
2091 | goto exit_notification; |
2092 | } | |
2093 | ||
d086f507 | 2094 | /* Create timer thread. */ |
bc26e826 | 2095 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 2096 | retval = -1; |
d086f507 JD |
2097 | goto exit_notification; |
2098 | } | |
d086f507 | 2099 | |
db66e574 JD |
2100 | /* rotation_thread_data acquires the pipes' read side. */ |
2101 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf | 2102 | rotation_timer_queue, |
c8a9de5a | 2103 | notification_thread_handle); |
db66e574 JD |
2104 | if (!rotation_thread_handle) { |
2105 | retval = -1; | |
2106 | ERR("Failed to create rotation thread shared data"); | |
2107 | stop_threads(); | |
2108 | goto exit_rotation; | |
2109 | } | |
db66e574 JD |
2110 | |
2111 | /* Create rotation thread. */ | |
64d9b072 | 2112 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 2113 | retval = -1; |
db66e574 JD |
2114 | goto exit_rotation; |
2115 | } | |
2116 | ||
cf3af59e | 2117 | /* Create thread to manage the client socket */ |
917a718d JG |
2118 | client_thread = launch_client_thread(); |
2119 | if (!client_thread) { | |
4a15001e | 2120 | retval = -1; |
cf3af59e MD |
2121 | goto exit_client; |
2122 | } | |
fac6795d | 2123 | |
5d1b0219 JG |
2124 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
2125 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 2126 | retval = -1; |
099e26bd DG |
2127 | goto exit_dispatch; |
2128 | } | |
2129 | ||
2130 | /* Create thread to manage application registration. */ | |
1785d7f2 | 2131 | if (!launch_application_registration_thread(&ust_cmd_queue)) { |
4a15001e | 2132 | retval = -1; |
099e26bd DG |
2133 | goto exit_reg_apps; |
2134 | } | |
2135 | ||
cf3af59e | 2136 | /* Create thread to manage application socket */ |
7649924e | 2137 | if (!launch_application_management_thread(apps_cmd_pipe[0])) { |
4a15001e | 2138 | retval = -1; |
d0b96690 DG |
2139 | goto exit_apps; |
2140 | } | |
2141 | ||
2142 | /* Create thread to manage application notify socket */ | |
971a61c6 | 2143 | if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) { |
4a15001e | 2144 | retval = -1; |
9563b0ad | 2145 | goto exit_apps_notify; |
cf3af59e | 2146 | } |
fac6795d | 2147 | |
8a7e4590 JG |
2148 | /* Create agent management thread. */ |
2149 | if (!launch_agent_management_thread()) { | |
4a15001e | 2150 | retval = -1; |
022d91ba | 2151 | goto exit_agent_reg; |
4d076222 DG |
2152 | } |
2153 | ||
6620da75 | 2154 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 2155 | if (is_root && !config.no_kernel) { |
6620da75 | 2156 | /* Create kernel thread to manage kernel event */ |
5b093681 | 2157 | if (!launch_kernel_management_thread(kernel_poll_pipe[0])) { |
4a15001e | 2158 | retval = -1; |
6620da75 DG |
2159 | goto exit_kernel; |
2160 | } | |
ef367a93 | 2161 | } |
7a485870 | 2162 | |
3316b9d6 JG |
2163 | /* Load sessions. */ |
2164 | ret = config_load_session(config.load_session_path.value, | |
2165 | NULL, 1, 1, NULL); | |
4a15001e | 2166 | if (ret) { |
3316b9d6 | 2167 | ERR("Session load failed: %s", error_get_str(ret)); |
4a15001e MD |
2168 | retval = -1; |
2169 | goto exit_load_session; | |
2170 | } | |
2171 | ||
3316b9d6 JG |
2172 | /* Initialization completed. */ |
2173 | sessiond_signal_parents(); | |
2174 | ||
4a15001e MD |
2175 | /* |
2176 | * This is where we start awaiting program completion (e.g. through | |
2177 | * signal that asks threads to teardown). | |
2178 | */ | |
2179 | ||
99d688f2 JG |
2180 | /* Initiate teardown once activity occurs on the quit pipe. */ |
2181 | sessiond_wait_for_quit_pipe(-1U); | |
917a718d JG |
2182 | |
2183 | /* | |
2184 | * Ensure that the client thread is no longer accepting new commands, | |
2185 | * which could cause new sessions to be created. | |
2186 | */ | |
2187 | if (!lttng_thread_shutdown(client_thread)) { | |
2188 | ERR("Failed to shutdown the client thread, continuing teardown"); | |
2189 | lttng_thread_put(client_thread); | |
2190 | client_thread = NULL; | |
2191 | } | |
2192 | ||
99d688f2 | 2193 | destroy_all_sessions_and_wait(); |
4a15001e | 2194 | exit_load_session: |
cf3af59e | 2195 | exit_kernel: |
022d91ba | 2196 | exit_agent_reg: |
9563b0ad | 2197 | exit_apps_notify: |
cf3af59e | 2198 | exit_apps: |
099e26bd | 2199 | exit_reg_apps: |
099e26bd | 2200 | exit_dispatch: |
b3530820 | 2201 | exit_client: |
db66e574 | 2202 | exit_rotation: |
b3530820 | 2203 | exit_notification: |
a3707772 | 2204 | lttng_thread_list_shutdown_orphans(); |
b3530820 | 2205 | exit_health: |
4a15001e | 2206 | exit_init_data: |
917a718d JG |
2207 | if (client_thread) { |
2208 | lttng_thread_put(client_thread); | |
2209 | } | |
2210 | ||
4d62fbf8 MD |
2211 | /* |
2212 | * Wait for all pending call_rcu work to complete before tearing | |
2213 | * down data structures. call_rcu worker may be trying to | |
2214 | * perform lookups in those structures. | |
2215 | */ | |
2216 | rcu_barrier(); | |
4a15001e MD |
2217 | /* |
2218 | * sessiond_cleanup() is called when no other thread is running, except | |
2219 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
2220 | */ | |
2221 | rcu_thread_online(); | |
2222 | sessiond_cleanup(); | |
06f525de | 2223 | |
c4d5de9d JG |
2224 | /* |
2225 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
2226 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
2227 | * the queue is empty before shutting down the clean-up thread. | |
2228 | */ | |
2229 | rcu_barrier(); | |
2230 | ||
a3707772 JG |
2231 | if (ht_cleanup_thread) { |
2232 | lttng_thread_shutdown(ht_cleanup_thread); | |
2233 | lttng_thread_put(ht_cleanup_thread); | |
2234 | } | |
2235 | ||
64d9b072 JG |
2236 | rcu_thread_offline(); |
2237 | rcu_unregister_thread(); | |
2238 | ||
2239 | if (rotation_thread_handle) { | |
2240 | rotation_thread_handle_destroy(rotation_thread_handle); | |
2241 | } | |
2242 | ||
d086f507 JD |
2243 | /* |
2244 | * After the rotation and timer thread have quit, we can safely destroy | |
2245 | * the rotation_timer_queue. | |
2246 | */ | |
92816cc3 | 2247 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
c8a9de5a JG |
2248 | /* |
2249 | * The teardown of the notification system is performed after the | |
2250 | * session daemon's teardown in order to allow it to be notified | |
2251 | * of the active session and channels at the moment of the teardown. | |
2252 | */ | |
2253 | if (notification_thread_handle) { | |
2254 | notification_thread_handle_destroy(notification_thread_handle); | |
2255 | } | |
b3530820 JG |
2256 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
2257 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
2258 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
0b2dc8df | 2259 | exit_ht_cleanup: |
4a15001e | 2260 | |
4a15001e MD |
2261 | health_app_destroy(health_sessiond); |
2262 | exit_health_sessiond_cleanup: | |
7567352f | 2263 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
2264 | |
2265 | exit_options: | |
f472090a | 2266 | sessiond_cleanup_lock_file(); |
4a15001e MD |
2267 | sessiond_cleanup_options(); |
2268 | ||
2269 | exit_set_signal_handler: | |
2270 | if (!retval) { | |
cf3af59e | 2271 | exit(EXIT_SUCCESS); |
4a15001e MD |
2272 | } else { |
2273 | exit(EXIT_FAILURE); | |
67e40797 | 2274 | } |
fac6795d | 2275 | } |