Fix: call lttng_poll_set_max_size before start threads
[lttng-tools.git] / src / bin / lttng-sessiond / ust-thread.c
... / ...
CommitLineData
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 */
33void *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 */
72restart:
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 if (!revents) {
96 /* No activity for this FD (poll implementation). */
97 continue;
98 }
99
100 /* Thread quit pipe has been closed. Killing thread. */
101 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
102 if (ret) {
103 err = 0;
104 goto exit;
105 }
106
107 /* Inspect the apps cmd pipe */
108 if (pollfd == apps_cmd_notify_pipe[0]) {
109 int sock;
110
111 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
112 ERR("Apps notify command pipe error");
113 goto error;
114 } else if (!(revents & LPOLLIN)) {
115 /* No POLLIN and not a catched error, stop the thread. */
116 ERR("Notify command pipe failed. revent: %u", revents);
117 goto error;
118 }
119
120 /* Get socket from dispatch thread. */
121 size_ret = lttng_read(apps_cmd_notify_pipe[0],
122 &sock, sizeof(sock));
123 if (size_ret < sizeof(sock)) {
124 PERROR("read apps notify pipe");
125 goto error;
126 }
127 health_code_update();
128
129 ret = lttng_poll_add(&events, sock,
130 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
131 if (ret < 0) {
132 /*
133 * It's possible we've reached the max poll fd allowed.
134 * Let's close the socket but continue normal execution.
135 */
136 ret = close(sock);
137 if (ret) {
138 PERROR("close notify socket %d", sock);
139 }
140 lttng_fd_put(LTTNG_FD_APPS, 1);
141 continue;
142 }
143 DBG3("UST thread notify added sock %d to pollset", sock);
144 } else {
145 /*
146 * At this point, we know that a registered application
147 * triggered the event.
148 */
149 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
150 /* Removing from the poll set */
151 ret = lttng_poll_del(&events, pollfd);
152 if (ret < 0) {
153 goto error;
154 }
155
156 /* The socket is closed after a grace period here. */
157 ust_app_notify_sock_unregister(pollfd);
158 } else if (revents & (LPOLLIN | LPOLLPRI)) {
159 ret = ust_app_recv_notify(pollfd);
160 if (ret < 0) {
161 /*
162 * If the notification failed either the application is
163 * dead or an internal error happened. In both cases,
164 * we can only continue here. If the application is
165 * dead, an unregistration will follow or else the
166 * application will notice that we are not responding
167 * on that socket and will close it.
168 */
169 continue;
170 }
171 } else {
172 ERR("Unknown poll events %u for sock %d", revents, pollfd);
173 continue;
174 }
175 health_code_update();
176 }
177 }
178 }
179
180exit:
181error:
182 lttng_poll_clean(&events);
183error_poll_create:
184error_testpoint:
185 utils_close_pipe(apps_cmd_notify_pipe);
186 apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1;
187 DBG("Application notify communication apps thread cleanup complete");
188 if (err) {
189 health_error();
190 ERR("Health error occurred in %s", __func__);
191 }
192 health_unregister(health_sessiond);
193 rcu_thread_offline();
194 rcu_unregister_thread();
195 return NULL;
196}
This page took 0.024641 seconds and 5 git commands to generate.