Fix: compat poll: add missing empty revents checks
[lttng-tools.git] / src / bin / lttng-sessiond / ht-cleanup.c
1 /*
2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <assert.h>
21
22 #include <common/hashtable/hashtable.h>
23 #include <common/common.h>
24 #include <common/utils.h>
25
26 #include "lttng-sessiond.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
29
30 void *thread_ht_cleanup(void *data)
31 {
32 int ret, i, pollfd, err = -1;
33 ssize_t size_ret;
34 uint32_t revents, nb_fd;
35 struct lttng_poll_event events;
36
37 DBG("[ht-thread] startup.");
38
39 rcu_register_thread();
40 rcu_thread_online();
41
42 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP);
43
44 if (testpoint(sessiond_thread_ht_cleanup)) {
45 DBG("[ht-thread] testpoint.");
46 goto error_testpoint;
47 }
48
49 health_code_update();
50
51 ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);
52 if (ret < 0) {
53 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret);
54 goto error_poll_create;
55 }
56
57 /* Add pipe to the pollset. */
58 ret = lttng_poll_add(&events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR);
59 if (ret < 0) {
60 DBG("[ht-thread] lttng_poll_add error %d.", ret);
61 goto error;
62 }
63
64 health_code_update();
65
66 while (1) {
67 int handled_event;
68
69 DBG3("[ht-thread] Polling on %d fds.",
70 LTTNG_POLL_GETNB(&events));
71
72 /* Inifinite blocking call, waiting for transmission */
73 restart:
74 handled_event = 0;
75 health_poll_entry();
76 ret = lttng_poll_wait(&events, -1);
77 health_poll_exit();
78 if (ret < 0) {
79 /*
80 * Restart interrupted system call.
81 */
82 if (errno == EINTR) {
83 goto restart;
84 }
85 goto error;
86 }
87
88 nb_fd = ret;
89
90 for (i = 0; i < nb_fd; i++) {
91 struct lttng_ht *ht;
92
93 health_code_update();
94
95 /* Fetch once the poll data */
96 revents = LTTNG_POLL_GETEV(&events, i);
97 pollfd = LTTNG_POLL_GETFD(&events, i);
98
99 if (!revents) {
100 /* No activity for this FD (poll implementation). */
101 continue;
102 }
103
104 if (pollfd != ht_cleanup_pipe[0]) {
105 continue;
106 }
107
108 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
109 ERR("ht cleanup pipe error");
110 goto error;
111 } else if (!(revents & LPOLLIN)) {
112 /* No POLLIN and not a catched error, stop the thread. */
113 ERR("ht cleanup failed. revent: %u", revents);
114 goto error;
115 }
116
117 /* Get socket from dispatch thread. */
118 size_ret = lttng_read(ht_cleanup_pipe[0], &ht,
119 sizeof(ht));
120 if (size_ret < sizeof(ht)) {
121 PERROR("ht cleanup notify pipe");
122 goto error;
123 }
124 health_code_update();
125 /*
126 * The whole point of this thread is to call
127 * lttng_ht_destroy from a context that is NOT:
128 * 1) a read-side RCU lock,
129 * 2) a call_rcu thread.
130 */
131 lttng_ht_destroy(ht);
132
133 health_code_update();
134 }
135
136 /* Only check cleanup quit when no more work to do. */
137 if (handled_event) {
138 continue;
139 }
140
141 for (i = 0; i < nb_fd; i++) {
142 health_code_update();
143
144 /* Fetch once the poll data */
145 revents = LTTNG_POLL_GETEV(&events, i);
146 pollfd = LTTNG_POLL_GETFD(&events, i);
147
148 if (!revents) {
149 /* No activity for this FD (poll implementation). */
150 continue;
151 }
152
153 if (pollfd == ht_cleanup_pipe[0]) {
154 continue;
155 }
156
157 /* Thread quit pipe has been closed. Killing thread. */
158 ret = sessiond_check_ht_cleanup_quit(pollfd, revents);
159 if (ret) {
160 err = 0;
161 DBG("[ht-cleanup] quit.");
162 goto exit;
163 }
164 }
165 }
166
167 exit:
168 error:
169 lttng_poll_clean(&events);
170 error_poll_create:
171 error_testpoint:
172 DBG("[ht-cleanup] Thread terminates.");
173 if (err) {
174 health_error();
175 ERR("Health error occurred in %s", __func__);
176 }
177 health_unregister(health_sessiond);
178 rcu_thread_offline();
179 rcu_unregister_thread();
180 return NULL;
181 }
This page took 0.033759 seconds and 5 git commands to generate.