Standardize quit pipes behavior
[lttng-tools.git] / src / bin / lttng-sessiond / notify-apps.cpp
CommitLineData
d0b96690 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
d0b96690 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
d0b96690 5 *
d0b96690 6 */
890d8fe4 7
6c1c0768 8#define _LGPL_SOURCE
d0b96690 9
c9e313bc
SM
10#include <common/common.hpp>
11#include <common/utils.hpp>
d0b96690 12
c9e313bc
SM
13#include "fd-limit.hpp"
14#include "lttng-sessiond.hpp"
15#include "notify-apps.hpp"
16#include "health-sessiond.hpp"
17#include "testpoint.hpp"
18#include "utils.hpp"
19#include "thread.hpp"
971a61c6 20
f1494934 21namespace {
971a61c6
JG
22struct thread_notifiers {
23 struct lttng_pipe *quit_pipe;
24 int apps_cmd_notify_pipe_read_fd;
25};
f1494934 26} /* namespace */
d0b96690
DG
27
28/*
29 * This thread manage application notify communication.
30 */
971a61c6 31static void *thread_application_notification(void *data)
d0b96690 32{
8a00688e 33 int i, ret, err = -1;
6cd525e8 34 ssize_t size_ret;
8a00688e 35 uint32_t nb_fd;
d0b96690 36 struct lttng_poll_event events;
7966af57 37 struct thread_notifiers *notifiers = (thread_notifiers *) data;
8a00688e 38 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
d0b96690
DG
39
40 DBG("[ust-thread] Manage application notify command");
41
42 rcu_register_thread();
43 rcu_thread_online();
44
412d7227
SM
45 health_register(the_health_sessiond,
46 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY);
380e8d6f 47
9ad42ec1
MD
48 if (testpoint(sessiond_thread_app_manage_notify)) {
49 goto error_testpoint;
50 }
51
380e8d6f
MD
52 health_code_update();
53
971a61c6 54 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
d0b96690
DG
55 if (ret < 0) {
56 goto error_poll_create;
57 }
58
59 /* Add notify pipe to the pollset. */
971a61c6 60 ret = lttng_poll_add(&events, notifiers->apps_cmd_notify_pipe_read_fd,
03e43155 61 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
d0b96690
DG
62 if (ret < 0) {
63 goto error;
64 }
65
8a00688e 66 ret = lttng_poll_add(&events, thread_quit_pipe_fd,
971a61c6
JG
67 LPOLLIN | LPOLLERR);
68 if (ret < 0) {
69 goto error;
70 }
71
380e8d6f
MD
72 health_code_update();
73
d0b96690 74 while (1) {
7fa2082e 75 DBG3("[ust-thread] Manage notify polling");
d0b96690
DG
76
77 /* Inifinite blocking call, waiting for transmission */
78restart:
380e8d6f 79 health_poll_entry();
d0b96690 80 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
81 DBG3("[ust-thread] Manage notify return from poll on %d fds",
82 LTTNG_POLL_GETNB(&events));
380e8d6f 83 health_poll_exit();
d0b96690
DG
84 if (ret < 0) {
85 /*
86 * Restart interrupted system call.
87 */
88 if (errno == EINTR) {
89 goto restart;
90 }
91 goto error;
92 }
93
94 nb_fd = ret;
95
96 for (i = 0; i < nb_fd; i++) {
380e8d6f
MD
97 health_code_update();
98
d0b96690 99 /* Fetch once the poll data */
8a00688e
MJ
100 const auto revents = LTTNG_POLL_GETEV(&events, i);
101 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
d0b96690 102
8a00688e
MJ
103 /* Activity on thread quit pipe, exiting. */
104 if (pollfd == thread_quit_pipe_fd) {
105 DBG("Activity on thread quit pipe");
380e8d6f 106 err = 0;
d0b96690 107 goto exit;
8a00688e
MJ
108 }
109
110 if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) {
971a61c6 111 /* Inspect the apps cmd pipe */
d0b96690
DG
112 int sock;
113
03e43155
MD
114 if (revents & LPOLLIN) {
115 /* Get socket from dispatch thread. */
971a61c6 116 size_ret = lttng_read(notifiers->apps_cmd_notify_pipe_read_fd,
03e43155
MD
117 &sock, sizeof(sock));
118 if (size_ret < sizeof(sock)) {
119 PERROR("read apps notify pipe");
120 goto error;
121 }
122 health_code_update();
123
124 ret = lttng_poll_add(&events, sock,
125 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
126 if (ret < 0) {
127 /*
128 * It's possible we've reached the max poll fd allowed.
129 * Let's close the socket but continue normal execution.
130 */
131 ret = close(sock);
132 if (ret) {
133 PERROR("close notify socket %d", sock);
134 }
135 lttng_fd_put(LTTNG_FD_APPS, 1);
136 continue;
137 }
138 DBG3("UST thread notify added sock %d to pollset", sock);
139 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
140 ERR("Apps notify command pipe error");
141 goto error;
03e43155
MD
142 } else {
143 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
d0b96690
DG
144 goto error;
145 }
d0b96690
DG
146 } else {
147 /*
148 * At this point, we know that a registered application
149 * triggered the event.
150 */
03e43155
MD
151 if (revents & (LPOLLIN | LPOLLPRI)) {
152 ret = ust_app_recv_notify(pollfd);
153 if (ret < 0) {
154 /* Removing from the poll set */
155 ret = lttng_poll_del(&events, pollfd);
156 if (ret < 0) {
157 goto error;
158 }
159
160 /* The socket is closed after a grace period here. */
161 ust_app_notify_sock_unregister(pollfd);
162 }
163 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
164 /* Removing from the poll set */
165 ret = lttng_poll_del(&events, pollfd);
166 if (ret < 0) {
167 goto error;
168 }
169
d88aee68
DG
170 /* The socket is closed after a grace period here. */
171 ust_app_notify_sock_unregister(pollfd);
d0b96690 172 } else {
03e43155
MD
173 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
174 goto error;
d0b96690 175 }
380e8d6f 176 health_code_update();
d0b96690
DG
177 }
178 }
179 }
180
181exit:
182error:
183 lttng_poll_clean(&events);
184error_poll_create:
9ad42ec1 185error_testpoint:
971a61c6 186
d0b96690 187 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
188 if (err) {
189 health_error();
190 ERR("Health error occurred in %s", __func__);
191 }
412d7227 192 health_unregister(the_health_sessiond);
d0b96690
DG
193 rcu_thread_offline();
194 rcu_unregister_thread();
195 return NULL;
196}
971a61c6
JG
197
198static bool shutdown_application_notification_thread(void *data)
199{
7966af57 200 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
201 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
202
203 return notify_thread_pipe(write_fd) == 1;
204}
205
206static void cleanup_application_notification_thread(void *data)
207{
7966af57 208 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
209
210 lttng_pipe_destroy(notifiers->quit_pipe);
211 free(notifiers);
212}
213
214bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd)
215{
216 struct lttng_thread *thread;
217 struct thread_notifiers *notifiers;
218 struct lttng_pipe *quit_pipe;
219
64803277 220 notifiers = zmalloc<thread_notifiers>();
971a61c6 221 if (!notifiers) {
21fa020e 222 goto error_alloc;
971a61c6
JG
223 }
224 notifiers->apps_cmd_notify_pipe_read_fd = apps_cmd_notify_pipe_read_fd;
225
226 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
227 if (!quit_pipe) {
228 goto error;
229 }
230 notifiers->quit_pipe = quit_pipe;
231
232 thread = lttng_thread_create("Application notification",
233 thread_application_notification,
234 shutdown_application_notification_thread,
235 cleanup_application_notification_thread,
236 notifiers);
237 if (!thread) {
238 goto error;
239 }
240 lttng_thread_put(thread);
241 return true;
242error:
243 cleanup_application_notification_thread(notifiers);
21fa020e 244error_alloc:
971a61c6
JG
245 return false;
246}
This page took 0.082598 seconds and 4 git commands to generate.