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