common: credentials: hide symbols
[lttng-tools.git] / src / bin / lttng-sessiond / notify-apps.c
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
DG
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"
f9d2ba6a 16#include "notify-apps.h"
8782cc74 17#include "health-sessiond.h"
9ad42ec1 18#include "testpoint.h"
971a61c6
JG
19#include "utils.h"
20#include "thread.h"
21
22struct thread_notifiers {
23 struct lttng_pipe *quit_pipe;
24 int apps_cmd_notify_pipe_read_fd;
25};
d0b96690
DG
26
27/*
28 * This thread manage application notify communication.
29 */
971a61c6 30static void *thread_application_notification(void *data)
d0b96690 31{
380e8d6f 32 int i, ret, pollfd, err = -1;
6cd525e8 33 ssize_t size_ret;
d0b96690
DG
34 uint32_t revents, nb_fd;
35 struct lttng_poll_event events;
971a61c6
JG
36 struct thread_notifiers *notifiers = data;
37 const int quit_pipe_read_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
d0b96690
DG
38
39 DBG("[ust-thread] Manage application notify command");
40
41 rcu_register_thread();
42 rcu_thread_online();
43
6c71277b
MD
44 health_register(health_sessiond,
45 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY);
380e8d6f 46
9ad42ec1
MD
47 if (testpoint(sessiond_thread_app_manage_notify)) {
48 goto error_testpoint;
49 }
50
380e8d6f
MD
51 health_code_update();
52
971a61c6 53 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
d0b96690
DG
54 if (ret < 0) {
55 goto error_poll_create;
56 }
57
58 /* Add notify pipe to the pollset. */
971a61c6 59 ret = lttng_poll_add(&events, notifiers->apps_cmd_notify_pipe_read_fd,
03e43155 60 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
d0b96690
DG
61 if (ret < 0) {
62 goto error;
63 }
64
971a61c6
JG
65 ret = lttng_poll_add(&events, quit_pipe_read_fd,
66 LPOLLIN | LPOLLERR);
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
DG
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. */
971a61c6 103 if (pollfd == quit_pipe_read_fd) {
380e8d6f 104 err = 0;
d0b96690 105 goto exit;
971a61c6
JG
106 } else if (pollfd == notifiers->apps_cmd_notify_pipe_read_fd) {
107 /* Inspect the apps cmd pipe */
d0b96690
DG
108 int sock;
109
03e43155
MD
110 if (revents & LPOLLIN) {
111 /* Get socket from dispatch thread. */
971a61c6 112 size_ret = lttng_read(notifiers->apps_cmd_notify_pipe_read_fd,
03e43155
MD
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)) {
d0b96690
DG
136 ERR("Apps notify command pipe error");
137 goto error;
03e43155
MD
138 } else {
139 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
d0b96690
DG
140 goto error;
141 }
d0b96690
DG
142 } else {
143 /*
144 * At this point, we know that a registered application
145 * triggered the event.
146 */
03e43155
MD
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)) {
d0b96690
DG
160 /* Removing from the poll set */
161 ret = lttng_poll_del(&events, pollfd);
162 if (ret < 0) {
163 goto error;
164 }
165
d88aee68
DG
166 /* The socket is closed after a grace period here. */
167 ust_app_notify_sock_unregister(pollfd);
d0b96690 168 } else {
03e43155
MD
169 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
170 goto error;
d0b96690 171 }
380e8d6f 172 health_code_update();
d0b96690
DG
173 }
174 }
175 }
176
177exit:
178error:
179 lttng_poll_clean(&events);
180error_poll_create:
9ad42ec1 181error_testpoint:
971a61c6 182
d0b96690 183 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
184 if (err) {
185 health_error();
186 ERR("Health error occurred in %s", __func__);
187 }
8782cc74 188 health_unregister(health_sessiond);
d0b96690
DG
189 rcu_thread_offline();
190 rcu_unregister_thread();
191 return NULL;
192}
971a61c6
JG
193
194static 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
202static 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
210bool 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) {
21fa020e 218 goto error_alloc;
971a61c6
JG
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;
238error:
239 cleanup_application_notification_thread(notifiers);
21fa020e 240error_alloc:
971a61c6
JG
241 return false;
242}
This page took 0.068317 seconds and 4 git commands to generate.