Commit | Line | Data |
---|---|---|
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" |
9ad42ec1 | 27 | #include "testpoint.h" |
d0b96690 DG |
28 | |
29 | /* | |
30 | * This thread manage application notify communication. | |
31 | */ | |
32 | void *ust_thread_manage_notify(void *data) | |
33 | { | |
380e8d6f | 34 | int i, ret, pollfd, err = -1; |
6cd525e8 | 35 | ssize_t size_ret; |
d0b96690 DG |
36 | uint32_t revents, nb_fd; |
37 | struct lttng_poll_event events; | |
38 | ||
39 | DBG("[ust-thread] Manage application notify command"); | |
40 | ||
41 | rcu_register_thread(); | |
42 | rcu_thread_online(); | |
43 | ||
6c71277b MD |
44 | health_register(health_sessiond, |
45 | HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY); | |
380e8d6f | 46 | |
9ad42ec1 MD |
47 | if (testpoint(sessiond_thread_app_manage_notify)) { |
48 | goto error_testpoint; | |
49 | } | |
50 | ||
380e8d6f MD |
51 | health_code_update(); |
52 | ||
d0b96690 DG |
53 | ret = sessiond_set_thread_pollset(&events, 2); |
54 | if (ret < 0) { | |
55 | goto error_poll_create; | |
56 | } | |
57 | ||
58 | /* Add notify pipe to the pollset. */ | |
59 | ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR); | |
60 | if (ret < 0) { | |
61 | goto error; | |
62 | } | |
63 | ||
380e8d6f MD |
64 | health_code_update(); |
65 | ||
d0b96690 | 66 | while (1) { |
3628d5bd | 67 | DBG3("[ust-thread] Manage notify polling"); |
d0b96690 DG |
68 | |
69 | /* Inifinite blocking call, waiting for transmission */ | |
70 | restart: | |
380e8d6f | 71 | health_poll_entry(); |
d0b96690 | 72 | ret = lttng_poll_wait(&events, -1); |
3628d5bd MD |
73 | DBG3("[ust-thread] Manage notify return from poll on %d fds", |
74 | LTTNG_POLL_GETNB(&events)); | |
380e8d6f | 75 | health_poll_exit(); |
d0b96690 DG |
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++) { | |
380e8d6f MD |
89 | health_code_update(); |
90 | ||
d0b96690 DG |
91 | /* Fetch once the poll data */ |
92 | revents = LTTNG_POLL_GETEV(&events, i); | |
93 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
94 | ||
6550503d MD |
95 | if (!revents) { |
96 | /* No activity for this FD (poll implementation). */ | |
97 | continue; | |
98 | } | |
99 | ||
d0b96690 DG |
100 | /* Thread quit pipe has been closed. Killing thread. */ |
101 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
102 | if (ret) { | |
380e8d6f | 103 | err = 0; |
d0b96690 DG |
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 | ||
6cd525e8 MD |
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)) { | |
d0b96690 DG |
124 | PERROR("read apps notify pipe"); |
125 | goto error; | |
126 | } | |
380e8d6f | 127 | health_code_update(); |
d0b96690 DG |
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 | ||
d88aee68 DG |
156 | /* The socket is closed after a grace period here. */ |
157 | ust_app_notify_sock_unregister(pollfd); | |
d0b96690 DG |
158 | } else if (revents & (LPOLLIN | LPOLLPRI)) { |
159 | ret = ust_app_recv_notify(pollfd); | |
160 | if (ret < 0) { | |
d88aee68 DG |
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; | |
d0b96690 DG |
170 | } |
171 | } else { | |
172 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
173 | continue; | |
174 | } | |
380e8d6f | 175 | health_code_update(); |
d0b96690 DG |
176 | } |
177 | } | |
178 | } | |
179 | ||
180 | exit: | |
181 | error: | |
182 | lttng_poll_clean(&events); | |
183 | error_poll_create: | |
9ad42ec1 | 184 | error_testpoint: |
d0b96690 DG |
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"); | |
380e8d6f MD |
188 | if (err) { |
189 | health_error(); | |
190 | ERR("Health error occurred in %s", __func__); | |
191 | } | |
8782cc74 | 192 | health_unregister(health_sessiond); |
d0b96690 DG |
193 | rcu_thread_offline(); |
194 | rcu_unregister_thread(); | |
195 | return NULL; | |
196 | } |