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