Fix: define _LGPL_SOURCE in C files
[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 goto error_testpoint;
46 }
47
48 health_code_update();
49
50 ret = sessiond_set_thread_pollset(&events, 2);
51 if (ret < 0) {
52 goto error_poll_create;
53 }
54
55 /* Add pipe to the pollset. */
56 ret = lttng_poll_add(&events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR);
57 if (ret < 0) {
58 goto error;
59 }
60
61 health_code_update();
62
63 while (1) {
64 DBG3("[ht-thread] Polling on %d fds.",
65 LTTNG_POLL_GETNB(&events));
66
67 /* Inifinite blocking call, waiting for transmission */
68 restart:
69 health_poll_entry();
70 ret = lttng_poll_wait(&events, -1);
71 health_poll_exit();
72 if (ret < 0) {
73 /*
74 * Restart interrupted system call.
75 */
76 if (errno == EINTR) {
77 goto restart;
78 }
79 goto error;
80 }
81
82 nb_fd = ret;
83
84 for (i = 0; i < nb_fd; i++) {
85 struct lttng_ht *ht;
86
87 health_code_update();
88
89 /* Fetch once the poll data */
90 revents = LTTNG_POLL_GETEV(&events, i);
91 pollfd = LTTNG_POLL_GETFD(&events, i);
92
93 /* Thread quit pipe has been closed. Killing thread. */
94 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
95 if (ret) {
96 err = 0;
97 goto exit;
98 }
99 assert(pollfd == ht_cleanup_pipe[0]);
100
101 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
102 ERR("ht cleanup pipe error");
103 goto error;
104 } else if (!(revents & LPOLLIN)) {
105 /* No POLLIN and not a catched error, stop the thread. */
106 ERR("ht cleanup failed. revent: %u", revents);
107 goto error;
108 }
109
110 /* Get socket from dispatch thread. */
111 size_ret = lttng_read(ht_cleanup_pipe[0], &ht,
112 sizeof(ht));
113 if (size_ret < sizeof(ht)) {
114 PERROR("ht cleanup notify pipe");
115 goto error;
116 }
117 health_code_update();
118 /*
119 * The whole point of this thread is to call
120 * lttng_ht_destroy from a context that is NOT:
121 * 1) a read-side RCU lock,
122 * 2) a call_rcu thread.
123 */
124 lttng_ht_destroy(ht);
125
126 health_code_update();
127 }
128 }
129
130 exit:
131 error:
132 lttng_poll_clean(&events);
133 error_poll_create:
134 error_testpoint:
135 utils_close_pipe(ht_cleanup_pipe);
136 ht_cleanup_pipe[0] = ht_cleanup_pipe[1] = -1;
137 DBG("[ust-thread] cleanup complete.");
138 if (err) {
139 health_error();
140 ERR("Health error occurred in %s", __func__);
141 }
142 health_unregister(health_sessiond);
143 rcu_thread_offline();
144 rcu_unregister_thread();
145 return NULL;
146 }
This page took 0.032642 seconds and 5 git commands to generate.