Fix: test: proper redirection of stderr to stdout
[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
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],
61 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
62 if (ret < 0) {
63 goto error;
64 }
65
66 health_code_update();
67
68 while (1) {
69 DBG3("[ust-thread] Manage notify polling");
70
71 /* Inifinite blocking call, waiting for transmission */
72 restart:
73 health_poll_entry();
74 ret = lttng_poll_wait(&events, -1);
75 DBG3("[ust-thread] Manage notify return from poll on %d fds",
76 LTTNG_POLL_GETNB(&events));
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 health_code_update();
92
93 /* Fetch once the poll data */
94 revents = LTTNG_POLL_GETEV(&events, i);
95 pollfd = LTTNG_POLL_GETFD(&events, i);
96
97 if (!revents) {
98 /* No activity for this FD (poll implementation). */
99 continue;
100 }
101
102 /* Thread quit pipe has been closed. Killing thread. */
103 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
104 if (ret) {
105 err = 0;
106 goto exit;
107 }
108
109 /* Inspect the apps cmd pipe */
110 if (pollfd == apps_cmd_notify_pipe[0]) {
111 int sock;
112
113 if (revents & LPOLLIN) {
114 /* Get socket from dispatch thread. */
115 size_ret = lttng_read(apps_cmd_notify_pipe[0],
116 &sock, sizeof(sock));
117 if (size_ret < sizeof(sock)) {
118 PERROR("read apps notify pipe");
119 goto error;
120 }
121 health_code_update();
122
123 ret = lttng_poll_add(&events, sock,
124 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
125 if (ret < 0) {
126 /*
127 * It's possible we've reached the max poll fd allowed.
128 * Let's close the socket but continue normal execution.
129 */
130 ret = close(sock);
131 if (ret) {
132 PERROR("close notify socket %d", sock);
133 }
134 lttng_fd_put(LTTNG_FD_APPS, 1);
135 continue;
136 }
137 DBG3("UST thread notify added sock %d to pollset", sock);
138 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
139 ERR("Apps notify command pipe error");
140 goto error;
141 } else {
142 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
143 goto error;
144 }
145 } else {
146 /*
147 * At this point, we know that a registered application
148 * triggered the event.
149 */
150 if (revents & (LPOLLIN | LPOLLPRI)) {
151 ret = ust_app_recv_notify(pollfd);
152 if (ret < 0) {
153 /* Removing from the poll set */
154 ret = lttng_poll_del(&events, pollfd);
155 if (ret < 0) {
156 goto error;
157 }
158
159 /* The socket is closed after a grace period here. */
160 ust_app_notify_sock_unregister(pollfd);
161 }
162 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
163 /* Removing from the poll set */
164 ret = lttng_poll_del(&events, pollfd);
165 if (ret < 0) {
166 goto error;
167 }
168
169 /* The socket is closed after a grace period here. */
170 ust_app_notify_sock_unregister(pollfd);
171 } else {
172 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
173 goto error;
174 }
175 health_code_update();
176 }
177 }
178 }
179
180 exit:
181 error:
182 lttng_poll_clean(&events);
183 error_poll_create:
184 error_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.032817 seconds and 4 git commands to generate.