Fix: HT must not be destroyed with a rcu_read_lock held
[lttng-tools.git] / src / bin / lttng-sessiond / ust-thread.c
CommitLineData
d0b96690
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17#define _GNU_SOURCE
18#include <assert.h>
19
20#include <common/common.h>
21#include <common/utils.h>
22
23#include "fd-limit.h"
24#include "lttng-sessiond.h"
25#include "ust-thread.h"
8782cc74 26#include "health-sessiond.h"
9ad42ec1 27#include "testpoint.h"
d0b96690
DG
28
29/*
30 * This thread manage application notify communication.
31 */
32void *ust_thread_manage_notify(void *data)
33{
380e8d6f 34 int i, ret, pollfd, err = -1;
6cd525e8 35 ssize_t size_ret;
d0b96690
DG
36 uint32_t revents, nb_fd;
37 struct lttng_poll_event events;
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
d0b96690
DG
53 ret = sessiond_set_thread_pollset(&events, 2);
54 if (ret < 0) {
55 goto error_poll_create;
56 }
57
58 /* Add notify pipe to the pollset. */
59 ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR);
60 if (ret < 0) {
61 goto error;
62 }
63
380e8d6f
MD
64 health_code_update();
65
d0b96690
DG
66 while (1) {
67 DBG3("[ust-thread] Manage notify polling on %d fds",
68 LTTNG_POLL_GETNB(&events));
69
70 /* Inifinite blocking call, waiting for transmission */
71restart:
380e8d6f 72 health_poll_entry();
d0b96690 73 ret = lttng_poll_wait(&events, -1);
380e8d6f 74 health_poll_exit();
d0b96690
DG
75 if (ret < 0) {
76 /*
77 * Restart interrupted system call.
78 */
79 if (errno == EINTR) {
80 goto restart;
81 }
82 goto error;
83 }
84
85 nb_fd = ret;
86
87 for (i = 0; i < nb_fd; i++) {
380e8d6f
MD
88 health_code_update();
89
d0b96690
DG
90 /* Fetch once the poll data */
91 revents = LTTNG_POLL_GETEV(&events, i);
92 pollfd = LTTNG_POLL_GETFD(&events, i);
93
94 /* Thread quit pipe has been closed. Killing thread. */
95 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
96 if (ret) {
380e8d6f 97 err = 0;
d0b96690
DG
98 goto exit;
99 }
100
101 /* Inspect the apps cmd pipe */
102 if (pollfd == apps_cmd_notify_pipe[0]) {
103 int sock;
104
105 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
106 ERR("Apps notify command pipe error");
107 goto error;
108 } else if (!(revents & LPOLLIN)) {
109 /* No POLLIN and not a catched error, stop the thread. */
110 ERR("Notify command pipe failed. revent: %u", revents);
111 goto error;
112 }
113
6cd525e8
MD
114 /* Get socket from dispatch thread. */
115 size_ret = lttng_read(apps_cmd_notify_pipe[0],
116 &sock, sizeof(sock));
117 if (size_ret < sizeof(sock)) {
d0b96690
DG
118 PERROR("read apps notify pipe");
119 goto error;
120 }
380e8d6f 121 health_code_update();
d0b96690
DG
122
123 ret = lttng_poll_add(&events, sock,
124 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
125 if (ret < 0) {
126 /*
127 * It's possible we've reached the max poll fd allowed.
128 * Let's close the socket but continue normal execution.
129 */
130 ret = close(sock);
131 if (ret) {
132 PERROR("close notify socket %d", sock);
133 }
134 lttng_fd_put(LTTNG_FD_APPS, 1);
135 continue;
136 }
137 DBG3("UST thread notify added sock %d to pollset", sock);
138 } else {
139 /*
140 * At this point, we know that a registered application
141 * triggered the event.
142 */
143 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
144 /* Removing from the poll set */
145 ret = lttng_poll_del(&events, pollfd);
146 if (ret < 0) {
147 goto error;
148 }
149
d88aee68
DG
150 /* The socket is closed after a grace period here. */
151 ust_app_notify_sock_unregister(pollfd);
d0b96690
DG
152 } else if (revents & (LPOLLIN | LPOLLPRI)) {
153 ret = ust_app_recv_notify(pollfd);
154 if (ret < 0) {
d88aee68
DG
155 /*
156 * If the notification failed either the application is
157 * dead or an internal error happened. In both cases,
158 * we can only continue here. If the application is
159 * dead, an unregistration will follow or else the
160 * application will notice that we are not responding
161 * on that socket and will close it.
162 */
163 continue;
d0b96690
DG
164 }
165 } else {
166 ERR("Unknown poll events %u for sock %d", revents, pollfd);
167 continue;
168 }
380e8d6f 169 health_code_update();
d0b96690
DG
170 }
171 }
172 }
173
174exit:
175error:
176 lttng_poll_clean(&events);
177error_poll_create:
9ad42ec1 178error_testpoint:
d0b96690
DG
179 utils_close_pipe(apps_cmd_notify_pipe);
180 apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1;
181 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
182 if (err) {
183 health_error();
184 ERR("Health error occurred in %s", __func__);
185 }
8782cc74 186 health_unregister(health_sessiond);
d0b96690
DG
187 rcu_thread_offline();
188 rcu_unregister_thread();
189 return NULL;
190}
This page took 0.04319 seconds and 5 git commands to generate.