Fix: sessiond: ODR violation results in memory corruption
[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{
380e8d6f 33 int i, ret, pollfd, err = -1;
6cd525e8 34 ssize_t size_ret;
d0b96690
DG
35 uint32_t revents, nb_fd;
36 struct lttng_poll_event events;
7966af57 37 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6 38 const int quit_pipe_read_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
971a61c6
JG
66 ret = lttng_poll_add(&events, quit_pipe_read_fd,
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
DG
99 /* Fetch once the poll data */
100 revents = LTTNG_POLL_GETEV(&events, i);
101 pollfd = LTTNG_POLL_GETFD(&events, i);
102
103 /* Thread quit pipe has been closed. Killing thread. */
971a61c6 104 if (pollfd == quit_pipe_read_fd) {
380e8d6f 105 err = 0;
d0b96690 106 goto exit;
971a61c6
JG
107 } else if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) {
108 /* Inspect the apps cmd pipe */
d0b96690
DG
109 int sock;
110
03e43155
MD
111 if (revents & LPOLLIN) {
112 /* Get socket from dispatch thread. */
971a61c6 113 size_ret = lttng_read(notifiers->apps_cmd_notify_pipe_read_fd,
03e43155
MD
114 &sock, sizeof(sock));
115 if (size_ret < sizeof(sock)) {
116 PERROR("read apps notify pipe");
117 goto error;
118 }
119 health_code_update();
120
121 ret = lttng_poll_add(&events, sock,
122 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
123 if (ret < 0) {
124 /*
125 * It's possible we've reached the max poll fd allowed.
126 * Let's close the socket but continue normal execution.
127 */
128 ret = close(sock);
129 if (ret) {
130 PERROR("close notify socket %d", sock);
131 }
132 lttng_fd_put(LTTNG_FD_APPS, 1);
133 continue;
134 }
135 DBG3("UST thread notify added sock %d to pollset", sock);
136 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
137 ERR("Apps notify command pipe error");
138 goto error;
03e43155
MD
139 } else {
140 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
d0b96690
DG
141 goto error;
142 }
d0b96690
DG
143 } else {
144 /*
145 * At this point, we know that a registered application
146 * triggered the event.
147 */
03e43155
MD
148 if (revents & (LPOLLIN | LPOLLPRI)) {
149 ret = ust_app_recv_notify(pollfd);
150 if (ret < 0) {
151 /* Removing from the poll set */
152 ret = lttng_poll_del(&events, pollfd);
153 if (ret < 0) {
154 goto error;
155 }
156
157 /* The socket is closed after a grace period here. */
158 ust_app_notify_sock_unregister(pollfd);
159 }
160 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
d0b96690
DG
161 /* Removing from the poll set */
162 ret = lttng_poll_del(&events, pollfd);
163 if (ret < 0) {
164 goto error;
165 }
166
d88aee68
DG
167 /* The socket is closed after a grace period here. */
168 ust_app_notify_sock_unregister(pollfd);
d0b96690 169 } else {
03e43155
MD
170 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
171 goto error;
d0b96690 172 }
380e8d6f 173 health_code_update();
d0b96690
DG
174 }
175 }
176 }
177
178exit:
179error:
180 lttng_poll_clean(&events);
181error_poll_create:
9ad42ec1 182error_testpoint:
971a61c6 183
d0b96690 184 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
185 if (err) {
186 health_error();
187 ERR("Health error occurred in %s", __func__);
188 }
412d7227 189 health_unregister(the_health_sessiond);
d0b96690
DG
190 rcu_thread_offline();
191 rcu_unregister_thread();
192 return NULL;
193}
971a61c6
JG
194
195static bool shutdown_application_notification_thread(void *data)
196{
7966af57 197 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
198 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
199
200 return notify_thread_pipe(write_fd) == 1;
201}
202
203static void cleanup_application_notification_thread(void *data)
204{
7966af57 205 struct thread_notifiers *notifiers = (thread_notifiers *) data;
971a61c6
JG
206
207 lttng_pipe_destroy(notifiers->quit_pipe);
208 free(notifiers);
209}
210
211bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd)
212{
213 struct lttng_thread *thread;
214 struct thread_notifiers *notifiers;
215 struct lttng_pipe *quit_pipe;
216
64803277 217 notifiers = zmalloc<thread_notifiers>();
971a61c6 218 if (!notifiers) {
21fa020e 219 goto error_alloc;
971a61c6
JG
220 }
221 notifiers->apps_cmd_notify_pipe_read_fd = apps_cmd_notify_pipe_read_fd;
222
223 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
224 if (!quit_pipe) {
225 goto error;
226 }
227 notifiers->quit_pipe = quit_pipe;
228
229 thread = lttng_thread_create("Application notification",
230 thread_application_notification,
231 shutdown_application_notification_thread,
232 cleanup_application_notification_thread,
233 notifiers);
234 if (!thread) {
235 goto error;
236 }
237 lttng_thread_put(thread);
238 return true;
239error:
240 cleanup_application_notification_thread(notifiers);
21fa020e 241error_alloc:
971a61c6
JG
242 return false;
243}
This page took 0.075696 seconds and 4 git commands to generate.