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