Commit | Line | Data |
---|---|---|
a7333da7 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa MJ |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
a7333da7 | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
a7333da7 | 7 | * |
a7333da7 JG |
8 | */ |
9 | ||
c9e313bc SM |
10 | #include "lttng-sessiond.hpp" |
11 | #include "utils.hpp" | |
28ab034a | 12 | |
c9e313bc | 13 | #include <common/utils.hpp> |
28ab034a | 14 | |
99d688f2 JG |
15 | #include <pthread.h> |
16 | ||
a7333da7 | 17 | /* |
f90a04ed MJ |
18 | * Quit pipe for the main thread. This is used by signal handlers to start the |
19 | * shutdown sequence of the main thread which will tear down the other threads | |
20 | * in the appropriate order. | |
a7333da7 | 21 | */ |
f90a04ed | 22 | static int main_quit_pipe[2] = { -1, -1 }; |
a7333da7 JG |
23 | |
24 | /* | |
f90a04ed | 25 | * Init main quit pipe. |
a7333da7 JG |
26 | * |
27 | * Return -1 on error or 0 if all pipes are created. | |
28 | */ | |
cd9adb8b | 29 | int sessiond_init_main_quit_pipe() |
a7333da7 JG |
30 | { |
31 | int ret, i; | |
32 | ||
f90a04ed | 33 | ret = pipe(main_quit_pipe); |
a7333da7 | 34 | if (ret < 0) { |
f90a04ed | 35 | PERROR("main quit pipe"); |
a7333da7 JG |
36 | goto error; |
37 | } | |
38 | ||
39 | for (i = 0; i < 2; i++) { | |
f90a04ed | 40 | ret = fcntl(main_quit_pipe[i], F_SETFD, FD_CLOEXEC); |
a7333da7 | 41 | if (ret < 0) { |
f90a04ed | 42 | PERROR("fcntl main_quit_pipe"); |
a7333da7 JG |
43 | goto error; |
44 | } | |
45 | } | |
46 | ||
47 | error: | |
48 | return ret; | |
49 | } | |
50 | ||
a7333da7 | 51 | /* |
f90a04ed | 52 | * Wait for a notification on the main quit pipe (with a timeout). |
a7333da7 | 53 | * |
99d688f2 JG |
54 | * A timeout value of -1U means no timeout. |
55 | * | |
a7333da7 JG |
56 | * Returns 1 if the caller should quit, 0 if the timeout was reached, and |
57 | * -1 if an error was encountered. | |
58 | */ | |
f90a04ed | 59 | int sessiond_wait_for_main_quit_pipe(int timeout_ms) |
a7333da7 JG |
60 | { |
61 | int ret; | |
2d54bfb6 | 62 | struct lttng_poll_event events; |
a7333da7 | 63 | |
2d54bfb6 MD |
64 | ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC); |
65 | if (ret < 0) { | |
66 | PERROR("Failed to initialize poll/epoll set"); | |
67 | ret = -1; | |
68 | goto end; | |
69 | } | |
1524f98c | 70 | ret = lttng_poll_add(&events, main_quit_pipe[0], LPOLLIN); |
2d54bfb6 MD |
71 | if (ret < 0) { |
72 | PERROR("Failed to add file descriptor to poll/epoll set"); | |
73 | ret = -1; | |
74 | goto end_clean_poll; | |
75 | } | |
76 | ret = lttng_poll_wait(&events, timeout_ms); | |
a7333da7 JG |
77 | if (ret > 0) { |
78 | /* Should quit. */ | |
79 | ret = 1; | |
80 | } else if (ret < 0 && errno != EINTR) { | |
81 | /* Unknown error. */ | |
f90a04ed | 82 | PERROR("Failed to epoll()/poll() main quit pipe"); |
a7333da7 JG |
83 | ret = -1; |
84 | } else { | |
85 | /* Timeout reached. */ | |
86 | ret = 0; | |
87 | } | |
2d54bfb6 MD |
88 | end_clean_poll: |
89 | lttng_poll_clean(&events); | |
90 | end: | |
a7333da7 JG |
91 | return ret; |
92 | } | |
93 | ||
cd9adb8b | 94 | int sessiond_notify_main_quit_pipe() |
a7333da7 | 95 | { |
f90a04ed | 96 | return notify_thread_pipe(main_quit_pipe[1]); |
a7333da7 JG |
97 | } |
98 | ||
cd9adb8b | 99 | void sessiond_close_main_quit_pipe() |
a7333da7 | 100 | { |
f90a04ed | 101 | utils_close_pipe(main_quit_pipe); |
a7333da7 JG |
102 | } |
103 | ||
f90a04ed MJ |
104 | /* |
105 | * Create a poll set with O_CLOEXEC and add the main quit pipe to the set. | |
106 | */ | |
107 | int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size) | |
a7333da7 JG |
108 | { |
109 | int ret; | |
110 | ||
a0377dfe | 111 | LTTNG_ASSERT(events); |
a7333da7 JG |
112 | |
113 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
114 | if (ret < 0) { | |
115 | goto error; | |
116 | } | |
117 | ||
f90a04ed | 118 | /* Add main quit pipe */ |
1524f98c | 119 | ret = lttng_poll_add(events, main_quit_pipe[0], LPOLLIN); |
a7333da7 JG |
120 | if (ret < 0) { |
121 | goto error; | |
122 | } | |
123 | ||
124 | return 0; | |
125 | ||
126 | error: | |
127 | return ret; | |
128 | } |