Build fix: missing stdio.h include in signal-helper.hpp
[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,
1524f98c 61 LPOLLIN | LPOLLRDHUP);
d0b96690
DG
62 if (ret < 0) {
63 goto error;
64 }
65
1524f98c 66 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
971a61c6
JG
67 if (ret < 0) {
68 goto error;
69 }
70
380e8d6f
MD
71 health_code_update();
72
d0b96690 73 while (1) {
7fa2082e 74 DBG3("[ust-thread] Manage notify polling");
d0b96690
DG
75
76 /* Inifinite blocking call, waiting for transmission */
77restart:
380e8d6f 78 health_poll_entry();
d0b96690 79 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
80 DBG3("[ust-thread] Manage notify return from poll on %d fds",
81 LTTNG_POLL_GETNB(&events));
380e8d6f 82 health_poll_exit();
d0b96690
DG
83 if (ret < 0) {
84 /*
85 * Restart interrupted system call.
86 */
87 if (errno == EINTR) {
88 goto restart;
89 }
90 goto error;
91 }
92
93 nb_fd = ret;
94
95 for (i = 0; i < nb_fd; i++) {
380e8d6f
MD
96 health_code_update();
97
d0b96690 98 /* Fetch once the poll data */
8a00688e
MJ
99 const auto revents = LTTNG_POLL_GETEV(&events, i);
100 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
d0b96690 101
8a00688e
MJ
102 /* Activity on thread quit pipe, exiting. */
103 if (pollfd == thread_quit_pipe_fd) {
104 DBG("Activity on thread quit pipe");
380e8d6f 105 err = 0;
d0b96690 106 goto exit;
8a00688e
MJ
107 }
108
109 if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) {
971a61c6 110 /* Inspect the apps cmd pipe */
d0b96690
DG
111 int sock;
112
03e43155
MD
113 if (revents & LPOLLIN) {
114 /* Get socket from dispatch thread. */
971a61c6 115 size_ret = lttng_read(notifiers->apps_cmd_notify_pipe_read_fd,
03e43155
MD
116 &sock, sizeof(sock));
117 if (size_ret < sizeof(sock)) {
118 PERROR("read apps notify pipe");
119 goto error;
120 }
121 health_code_update();
122
1524f98c 123 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
03e43155
MD
124 if (ret < 0) {
125 /*
126 * It's possible we've reached the max poll fd allowed.
127 * Let's close the socket but continue normal execution.
128 */
129 ret = close(sock);
130 if (ret) {
131 PERROR("close notify socket %d", sock);
132 }
133 lttng_fd_put(LTTNG_FD_APPS, 1);
134 continue;
135 }
136 DBG3("UST thread notify added sock %d to pollset", sock);
137 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
138 ERR("Apps notify command pipe error");
139 goto error;
03e43155
MD
140 } else {
141 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
d0b96690
DG
142 goto error;
143 }
d0b96690
DG
144 } else {
145 /*
146 * At this point, we know that a registered application
147 * triggered the event.
148 */
03e43155
MD
149 if (revents & (LPOLLIN | LPOLLPRI)) {
150 ret = ust_app_recv_notify(pollfd);
151 if (ret < 0) {
152 /* Removing from the poll set */
153 ret = lttng_poll_del(&events, pollfd);
154 if (ret < 0) {
155 goto error;
156 }
157
158 /* The socket is closed after a grace period here. */
159 ust_app_notify_sock_unregister(pollfd);
160 }
161 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
162 /* Removing from the poll set */
163 ret = lttng_poll_del(&events, pollfd);
164 if (ret < 0) {
165 goto error;
166 }
167
d88aee68
DG
168 /* The socket is closed after a grace period here. */
169 ust_app_notify_sock_unregister(pollfd);
d0b96690 170 } else {
03e43155
MD
171 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
172 goto error;
d0b96690 173 }
380e8d6f 174 health_code_update();
d0b96690
DG
175 }
176 }
177 }
178
179exit:
180error:
181 lttng_poll_clean(&events);
182error_poll_create:
9ad42ec1 183error_testpoint:
971a61c6 184
d0b96690 185 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
186 if (err) {
187 health_error();
188 ERR("Health error occurred in %s", __func__);
189 }
412d7227 190 health_unregister(the_health_sessiond);
d0b96690
DG
191 rcu_thread_offline();
192 rcu_unregister_thread();
193 return NULL;
194}
971a61c6
JG
195
196static bool shutdown_application_notification_thread(void *data)
197{
7966af57 198 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
199 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
200
201 return notify_thread_pipe(write_fd) == 1;
202}
203
204static void cleanup_application_notification_thread(void *data)
205{
7966af57 206 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
207
208 lttng_pipe_destroy(notifiers->quit_pipe);
209 free(notifiers);
210}
211
212bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd)
213{
214 struct lttng_thread *thread;
215 struct thread_notifiers *notifiers;
216 struct lttng_pipe *quit_pipe;
217
64803277 218 notifiers = zmalloc<thread_notifiers>();
971a61c6 219 if (!notifiers) {
21fa020e 220 goto error_alloc;
971a61c6
JG
221 }
222 notifiers->apps_cmd_notify_pipe_read_fd = apps_cmd_notify_pipe_read_fd;
223
224 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
225 if (!quit_pipe) {
226 goto error;
227 }
228 notifiers->quit_pipe = quit_pipe;
229
230 thread = lttng_thread_create("Application notification",
231 thread_application_notification,
232 shutdown_application_notification_thread,
233 cleanup_application_notification_thread,
234 notifiers);
235 if (!thread) {
236 goto error;
237 }
238 lttng_thread_put(thread);
239 return true;
240error:
241 cleanup_application_notification_thread(notifiers);
21fa020e 242error_alloc:
971a61c6
JG
243 return false;
244}
This page took 0.081203 seconds and 4 git commands to generate.