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