Fix: empty indexes_ht before destroying it.
[lttng-tools.git] / src / bin / lttng-sessiond / ust-thread.c
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 #define _LGPL_SOURCE
19 #include <assert.h>
20
21 #include <common/common.h>
22 #include <common/utils.h>
23
24 #include "fd-limit.h"
25 #include "lttng-sessiond.h"
26 #include "ust-thread.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
29
30 /*
31 * This thread manage application notify communication.
32 */
33 void *ust_thread_manage_notify(void *data)
34 {
35 int i, ret, pollfd, err = -1;
36 ssize_t size_ret;
37 uint32_t revents, nb_fd;
38 struct lttng_poll_event events;
39
40 DBG("[ust-thread] Manage application notify command");
41
42 rcu_register_thread();
43 rcu_thread_online();
44
45 health_register(health_sessiond,
46 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY);
47
48 if (testpoint(sessiond_thread_app_manage_notify)) {
49 goto error_testpoint;
50 }
51
52 health_code_update();
53
54 ret = sessiond_set_thread_pollset(&events, 2);
55 if (ret < 0) {
56 goto error_poll_create;
57 }
58
59 /* Add notify pipe to the pollset. */
60 ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR);
61 if (ret < 0) {
62 goto error;
63 }
64
65 health_code_update();
66
67 while (1) {
68 DBG3("[ust-thread] Manage notify polling on %d fds",
69 LTTNG_POLL_GETNB(&events));
70
71 /* Inifinite blocking call, waiting for transmission */
72 restart:
73 health_poll_entry();
74 ret = lttng_poll_wait(&events, -1);
75 health_poll_exit();
76 if (ret < 0) {
77 /*
78 * Restart interrupted system call.
79 */
80 if (errno == EINTR) {
81 goto restart;
82 }
83 goto error;
84 }
85
86 nb_fd = ret;
87
88 for (i = 0; i < nb_fd; i++) {
89 health_code_update();
90
91 /* Fetch once the poll data */
92 revents = LTTNG_POLL_GETEV(&events, i);
93 pollfd = LTTNG_POLL_GETFD(&events, i);
94
95 /* Thread quit pipe has been closed. Killing thread. */
96 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
97 if (ret) {
98 err = 0;
99 goto exit;
100 }
101
102 /* Inspect the apps cmd pipe */
103 if (pollfd == apps_cmd_notify_pipe[0]) {
104 int sock;
105
106 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
107 ERR("Apps notify command pipe error");
108 goto error;
109 } else if (!(revents & LPOLLIN)) {
110 /* No POLLIN and not a catched error, stop the thread. */
111 ERR("Notify command pipe failed. revent: %u", revents);
112 goto error;
113 }
114
115 /* Get socket from dispatch thread. */
116 size_ret = lttng_read(apps_cmd_notify_pipe[0],
117 &sock, sizeof(sock));
118 if (size_ret < sizeof(sock)) {
119 PERROR("read apps notify pipe");
120 goto error;
121 }
122 health_code_update();
123
124 ret = lttng_poll_add(&events, sock,
125 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
126 if (ret < 0) {
127 /*
128 * It's possible we've reached the max poll fd allowed.
129 * Let's close the socket but continue normal execution.
130 */
131 ret = close(sock);
132 if (ret) {
133 PERROR("close notify socket %d", sock);
134 }
135 lttng_fd_put(LTTNG_FD_APPS, 1);
136 continue;
137 }
138 DBG3("UST thread notify added sock %d to pollset", sock);
139 } else {
140 /*
141 * At this point, we know that a registered application
142 * triggered the event.
143 */
144 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
145 /* Removing from the poll set */
146 ret = lttng_poll_del(&events, pollfd);
147 if (ret < 0) {
148 goto error;
149 }
150
151 /* The socket is closed after a grace period here. */
152 ust_app_notify_sock_unregister(pollfd);
153 } else if (revents & (LPOLLIN | LPOLLPRI)) {
154 ret = ust_app_recv_notify(pollfd);
155 if (ret < 0) {
156 /*
157 * If the notification failed either the application is
158 * dead or an internal error happened. In both cases,
159 * we can only continue here. If the application is
160 * dead, an unregistration will follow or else the
161 * application will notice that we are not responding
162 * on that socket and will close it.
163 */
164 continue;
165 }
166 } else {
167 ERR("Unknown poll events %u for sock %d", revents, pollfd);
168 continue;
169 }
170 health_code_update();
171 }
172 }
173 }
174
175 exit:
176 error:
177 lttng_poll_clean(&events);
178 error_poll_create:
179 error_testpoint:
180 utils_close_pipe(apps_cmd_notify_pipe);
181 apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1;
182 DBG("Application notify communication apps thread cleanup complete");
183 if (err) {
184 health_error();
185 ERR("Health error occurred in %s", __func__);
186 }
187 health_unregister(health_sessiond);
188 rcu_thread_offline();
189 rcu_unregister_thread();
190 return NULL;
191 }
This page took 0.039867 seconds and 4 git commands to generate.