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. */ | |
584fc280 MD |
59 | ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], |
60 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
d0b96690 DG |
61 | if (ret < 0) { |
62 | goto error; | |
63 | } | |
64 | ||
380e8d6f MD |
65 | health_code_update(); |
66 | ||
d0b96690 | 67 | while (1) { |
65da3cc7 | 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); |
65da3cc7 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 | ||
e0077699 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 | ||
584fc280 MD |
112 | if (revents & LPOLLIN) { |
113 | /* Get socket from dispatch thread. */ | |
114 | size_ret = lttng_read(apps_cmd_notify_pipe[0], | |
115 | &sock, sizeof(sock)); | |
116 | if (size_ret < sizeof(sock)) { | |
117 | PERROR("read apps notify pipe"); | |
118 | goto error; | |
119 | } | |
120 | health_code_update(); | |
121 | ||
122 | ret = lttng_poll_add(&events, sock, | |
123 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
124 | if (ret < 0) { | |
125 | /* | |
126 | * It's possible we've reached the max poll fd allowed. | |
127 | * Let's close the socket but continue normal execution. | |
128 | */ | |
129 | ret = close(sock); | |
130 | if (ret) { | |
131 | PERROR("close notify socket %d", sock); | |
132 | } | |
133 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
134 | continue; | |
135 | } | |
136 | DBG3("UST thread notify added sock %d to pollset", sock); | |
137 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
d0b96690 DG |
138 | ERR("Apps notify command pipe error"); |
139 | goto error; | |
584fc280 MD |
140 | } else { |
141 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
d0b96690 DG |
142 | goto error; |
143 | } | |
d0b96690 DG |
144 | } else { |
145 | /* | |
146 | * At this point, we know that a registered application | |
147 | * triggered the event. | |
148 | */ | |
584fc280 MD |
149 | if (revents & (LPOLLIN | LPOLLPRI)) { |
150 | ret = ust_app_recv_notify(pollfd); | |
151 | if (ret < 0) { | |
152 | /* Removing from the poll set */ | |
153 | ret = lttng_poll_del(&events, pollfd); | |
154 | if (ret < 0) { | |
155 | goto error; | |
156 | } | |
157 | ||
158 | /* The socket is closed after a grace period here. */ | |
159 | ust_app_notify_sock_unregister(pollfd); | |
160 | } | |
161 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
d0b96690 DG |
162 | /* Removing from the poll set */ |
163 | ret = lttng_poll_del(&events, pollfd); | |
164 | if (ret < 0) { | |
165 | goto error; | |
166 | } | |
167 | ||
d88aee68 DG |
168 | /* The socket is closed after a grace period here. */ |
169 | ust_app_notify_sock_unregister(pollfd); | |
d0b96690 | 170 | } else { |
584fc280 MD |
171 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
172 | goto error; | |
d0b96690 | 173 | } |
380e8d6f | 174 | health_code_update(); |
d0b96690 DG |
175 | } |
176 | } | |
177 | } | |
178 | ||
179 | exit: | |
180 | error: | |
181 | lttng_poll_clean(&events); | |
182 | error_poll_create: | |
9ad42ec1 | 183 | error_testpoint: |
d0b96690 DG |
184 | utils_close_pipe(apps_cmd_notify_pipe); |
185 | apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1; | |
186 | DBG("Application notify communication apps thread cleanup complete"); | |
380e8d6f MD |
187 | if (err) { |
188 | health_error(); | |
189 | ERR("Health error occurred in %s", __func__); | |
190 | } | |
8782cc74 | 191 | health_unregister(health_sessiond); |
d0b96690 DG |
192 | rcu_thread_offline(); |
193 | rcu_unregister_thread(); | |
194 | return NULL; | |
195 | } |