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