Commit | Line | Data |
---|---|---|
22b80785 JG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #include <stddef.h> | |
21 | #include <stdlib.h> | |
22 | #include <urcu.h> | |
23 | #include <common/futex.h> | |
24 | #include <common/macros.h> | |
25 | #include <common/utils.h> | |
26 | #include <sys/stat.h> | |
27 | ||
28 | #include "register.h" | |
29 | #include "lttng-sessiond.h" | |
30 | #include "testpoint.h" | |
31 | #include "health-sessiond.h" | |
32 | #include "fd-limit.h" | |
33 | #include "shm.h" | |
34 | #include "utils.h" | |
35 | #include "thread.h" | |
36 | ||
37 | struct thread_notifiers { | |
38 | struct lttng_pipe *quit_pipe; | |
39 | struct ust_cmd_queue *ust_cmd_queue; | |
bd0e523e | 40 | sem_t ready; |
2863220c | 41 | bool running; |
22b80785 JG |
42 | }; |
43 | ||
44 | /* | |
45 | * Creates the application socket. | |
46 | */ | |
47 | static int create_application_socket(void) | |
48 | { | |
49 | int ret = 0; | |
50 | int apps_sock; | |
51 | const mode_t old_umask = umask(0); | |
52 | ||
53 | /* Create the application unix socket */ | |
54 | apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value); | |
55 | if (apps_sock < 0) { | |
56 | ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value); | |
57 | ret = -1; | |
58 | goto end; | |
59 | } | |
60 | ||
61 | /* Set the cloexec flag */ | |
62 | ret = utils_set_fd_cloexec(apps_sock); | |
63 | if (ret < 0) { | |
64 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " | |
65 | "Continuing but note that the consumer daemon will have a " | |
66 | "reference to this socket on exec()", apps_sock); | |
67 | } | |
68 | ||
69 | /* File permission MUST be 666 */ | |
70 | ret = chmod(config.apps_unix_sock_path.value, | |
71 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
72 | if (ret < 0) { | |
73 | PERROR("Set file permissions failed on %s", | |
74 | config.apps_unix_sock_path.value); | |
75 | goto end; | |
76 | } | |
77 | ||
78 | DBG3("Session daemon application socket created (fd = %d) ", apps_sock); | |
79 | ret = apps_sock; | |
80 | end: | |
81 | umask(old_umask); | |
82 | return ret; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Notify UST applications using the shm mmap futex. | |
87 | */ | |
88 | static int notify_ust_apps(int active, bool is_root) | |
89 | { | |
90 | char *wait_shm_mmap; | |
91 | ||
92 | DBG("Notifying applications of session daemon state: %d", active); | |
93 | ||
94 | /* See shm.c for this call implying mmap, shm and futex calls */ | |
95 | wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root); | |
96 | if (wait_shm_mmap == NULL) { | |
97 | goto error; | |
98 | } | |
99 | ||
100 | /* Wake waiting process */ | |
101 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
102 | ||
103 | /* Apps notified successfully */ | |
104 | return 0; | |
105 | ||
106 | error: | |
107 | return -1; | |
108 | } | |
109 | ||
110 | static void cleanup_application_registration_thread(void *data) | |
111 | { | |
112 | struct thread_notifiers *notifiers = data; | |
113 | ||
114 | lttng_pipe_destroy(notifiers->quit_pipe); | |
115 | free(notifiers); | |
116 | } | |
117 | ||
2863220c | 118 | static void set_thread_status(struct thread_notifiers *notifiers, bool running) |
bd0e523e | 119 | { |
2863220c JG |
120 | DBG("Marking application registration thread's state as %s", running ? "running" : "error"); |
121 | notifiers->running = running; | |
bd0e523e JG |
122 | sem_post(¬ifiers->ready); |
123 | } | |
124 | ||
2863220c | 125 | static bool wait_thread_status(struct thread_notifiers *notifiers) |
bd0e523e JG |
126 | { |
127 | DBG("Waiting for application registration thread to be ready"); | |
128 | sem_wait(¬ifiers->ready); | |
2863220c JG |
129 | if (notifiers->running) { |
130 | DBG("Application registration thread is ready"); | |
131 | } else { | |
132 | ERR("Initialization of application registration thread failed"); | |
133 | } | |
134 | ||
135 | return notifiers->running; | |
136 | } | |
137 | ||
138 | static void thread_init_cleanup(void *data) | |
139 | { | |
140 | struct thread_notifiers *notifiers = data; | |
141 | ||
142 | set_thread_status(notifiers, false); | |
bd0e523e JG |
143 | } |
144 | ||
22b80785 JG |
145 | /* |
146 | * This thread manage application registration. | |
147 | */ | |
148 | static void *thread_application_registration(void *data) | |
149 | { | |
150 | int sock = -1, i, ret, pollfd, err = -1; | |
151 | int apps_sock = -1; | |
152 | uint32_t revents, nb_fd; | |
153 | struct lttng_poll_event events; | |
154 | /* | |
155 | * Gets allocated in this thread, enqueued to a global queue, dequeued | |
156 | * and freed in the manage apps thread. | |
157 | */ | |
158 | struct ust_command *ust_cmd = NULL; | |
159 | const bool is_root = (getuid() == 0); | |
160 | struct thread_notifiers *notifiers = data; | |
161 | const int quit_pipe_read_fd = lttng_pipe_get_readfd( | |
162 | notifiers->quit_pipe); | |
163 | ||
164 | DBG("[thread] Manage application registration started"); | |
165 | ||
2863220c | 166 | pthread_cleanup_push(thread_init_cleanup, NULL); |
22b80785 JG |
167 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
168 | ||
22b80785 JG |
169 | apps_sock = create_application_socket(); |
170 | if (apps_sock < 0) { | |
171 | goto error_listen; | |
172 | } | |
173 | ||
174 | ret = lttcomm_listen_unix_sock(apps_sock); | |
175 | if (ret < 0) { | |
176 | goto error_listen; | |
177 | } | |
178 | ||
2863220c JG |
179 | set_thread_status(notifiers, true); |
180 | pthread_cleanup_pop(0); | |
181 | ||
182 | if (testpoint(sessiond_thread_registration_apps)) { | |
183 | goto error_create_poll; | |
184 | } | |
bd0e523e | 185 | |
22b80785 JG |
186 | /* |
187 | * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing | |
188 | * more will be added to this poll set. | |
189 | */ | |
190 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
191 | if (ret < 0) { | |
192 | goto error_create_poll; | |
193 | } | |
194 | ||
195 | /* Add the application registration socket */ | |
196 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
197 | if (ret < 0) { | |
198 | goto error_poll_add; | |
199 | } | |
200 | ||
201 | /* Add the application registration socket */ | |
202 | ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLRDHUP); | |
203 | if (ret < 0) { | |
204 | goto error_poll_add; | |
205 | } | |
206 | ||
207 | /* Notify all applications to register */ | |
208 | ret = notify_ust_apps(1, is_root); | |
209 | if (ret < 0) { | |
210 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
211 | "Execution continues but there might be problem for already\n" | |
212 | "running applications that wishes to register."); | |
213 | } | |
214 | ||
215 | while (1) { | |
216 | DBG("Accepting application registration"); | |
217 | ||
218 | /* Inifinite blocking call, waiting for transmission */ | |
219 | restart: | |
220 | health_poll_entry(); | |
221 | ret = lttng_poll_wait(&events, -1); | |
222 | health_poll_exit(); | |
223 | if (ret < 0) { | |
224 | /* | |
225 | * Restart interrupted system call. | |
226 | */ | |
227 | if (errno == EINTR) { | |
228 | goto restart; | |
229 | } | |
230 | goto error; | |
231 | } | |
232 | ||
233 | nb_fd = ret; | |
234 | ||
235 | for (i = 0; i < nb_fd; i++) { | |
236 | health_code_update(); | |
237 | ||
238 | /* Fetch once the poll data */ | |
239 | revents = LTTNG_POLL_GETEV(&events, i); | |
240 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
241 | ||
22b80785 JG |
242 | /* Thread quit pipe has been closed. Killing thread. */ |
243 | if (pollfd == quit_pipe_read_fd) { | |
244 | err = 0; | |
245 | goto exit; | |
246 | } else { | |
247 | /* Event on the registration socket */ | |
248 | if (revents & LPOLLIN) { | |
249 | sock = lttcomm_accept_unix_sock(apps_sock); | |
250 | if (sock < 0) { | |
251 | goto error; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Set socket timeout for both receiving and ending. | |
256 | * app_socket_timeout is in seconds, whereas | |
257 | * lttcomm_setsockopt_rcv_timeout and | |
258 | * lttcomm_setsockopt_snd_timeout expect msec as | |
259 | * parameter. | |
260 | */ | |
261 | if (config.app_socket_timeout >= 0) { | |
262 | (void) lttcomm_setsockopt_rcv_timeout(sock, | |
263 | config.app_socket_timeout * 1000); | |
264 | (void) lttcomm_setsockopt_snd_timeout(sock, | |
265 | config.app_socket_timeout * 1000); | |
266 | } | |
267 | ||
268 | /* | |
269 | * Set the CLOEXEC flag. Return code is useless because | |
270 | * either way, the show must go on. | |
271 | */ | |
272 | (void) utils_set_fd_cloexec(sock); | |
273 | ||
274 | /* Create UST registration command for enqueuing */ | |
275 | ust_cmd = zmalloc(sizeof(struct ust_command)); | |
276 | if (ust_cmd == NULL) { | |
277 | PERROR("ust command zmalloc"); | |
278 | ret = close(sock); | |
279 | if (ret) { | |
280 | PERROR("close"); | |
281 | } | |
282 | goto error; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Using message-based transmissions to ensure we don't | |
287 | * have to deal with partially received messages. | |
288 | */ | |
289 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
290 | if (ret < 0) { | |
291 | ERR("Exhausted file descriptors allowed for applications."); | |
292 | free(ust_cmd); | |
293 | ret = close(sock); | |
294 | if (ret) { | |
295 | PERROR("close"); | |
296 | } | |
297 | sock = -1; | |
298 | continue; | |
299 | } | |
300 | ||
301 | health_code_update(); | |
302 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); | |
303 | if (ret < 0) { | |
304 | free(ust_cmd); | |
305 | /* Close socket of the application. */ | |
306 | ret = close(sock); | |
307 | if (ret) { | |
308 | PERROR("close"); | |
309 | } | |
310 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
311 | sock = -1; | |
312 | continue; | |
313 | } | |
314 | health_code_update(); | |
315 | ||
316 | ust_cmd->sock = sock; | |
317 | sock = -1; | |
318 | ||
319 | DBG("UST registration received with pid:%d ppid:%d uid:%d" | |
320 | " gid:%d sock:%d name:%s (version %d.%d)", | |
321 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
322 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
323 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
324 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
325 | ||
326 | /* | |
327 | * Lock free enqueue the registration request. The red pill | |
328 | * has been taken! This apps will be part of the *system*. | |
329 | */ | |
330 | cds_wfcq_enqueue(¬ifiers->ust_cmd_queue->head, | |
331 | ¬ifiers->ust_cmd_queue->tail, | |
332 | &ust_cmd->node); | |
333 | ||
334 | /* | |
335 | * Wake the registration queue futex. Implicit memory | |
336 | * barrier with the exchange in cds_wfcq_enqueue. | |
337 | */ | |
338 | futex_nto1_wake(¬ifiers->ust_cmd_queue->futex); | |
339 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
340 | ERR("Register apps socket poll error"); | |
341 | goto error; | |
342 | } else { | |
343 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
344 | goto error; | |
345 | } | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | exit: | |
351 | error: | |
352 | /* Notify that the registration thread is gone */ | |
353 | notify_ust_apps(0, is_root); | |
354 | ||
355 | if (apps_sock >= 0) { | |
356 | ret = close(apps_sock); | |
357 | if (ret) { | |
358 | PERROR("close"); | |
359 | } | |
360 | } | |
361 | if (sock >= 0) { | |
362 | ret = close(sock); | |
363 | if (ret) { | |
364 | PERROR("close"); | |
365 | } | |
366 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
367 | } | |
368 | unlink(config.apps_unix_sock_path.value); | |
369 | ||
370 | error_poll_add: | |
371 | lttng_poll_clean(&events); | |
372 | error_listen: | |
373 | error_create_poll: | |
22b80785 JG |
374 | DBG("UST Registration thread cleanup complete"); |
375 | if (err) { | |
376 | health_error(); | |
377 | ERR("Health error occurred in %s", __func__); | |
378 | } | |
379 | health_unregister(health_sessiond); | |
380 | return NULL; | |
381 | } | |
382 | ||
383 | static bool shutdown_application_registration_thread(void *data) | |
384 | { | |
385 | struct thread_notifiers *notifiers = data; | |
386 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); | |
387 | ||
388 | return notify_thread_pipe(write_fd) == 1; | |
389 | } | |
390 | ||
143745a6 | 391 | struct lttng_thread *launch_application_registration_thread( |
22b80785 JG |
392 | struct ust_cmd_queue *cmd_queue) |
393 | { | |
394 | struct lttng_pipe *quit_pipe; | |
395 | struct thread_notifiers *notifiers = NULL; | |
396 | struct lttng_thread *thread; | |
397 | ||
22b80785 JG |
398 | notifiers = zmalloc(sizeof(*notifiers)); |
399 | if (!notifiers) { | |
a7f04300 JG |
400 | goto error_alloc; |
401 | } | |
402 | quit_pipe = lttng_pipe_open(FD_CLOEXEC); | |
403 | if (!quit_pipe) { | |
22b80785 JG |
404 | goto error; |
405 | } | |
406 | notifiers->quit_pipe = quit_pipe; | |
407 | notifiers->ust_cmd_queue = cmd_queue; | |
bd0e523e | 408 | sem_init(¬ifiers->ready, 0, 0); |
22b80785 JG |
409 | |
410 | thread = lttng_thread_create("UST application registration", | |
411 | thread_application_registration, | |
412 | shutdown_application_registration_thread, | |
413 | cleanup_application_registration_thread, | |
414 | notifiers); | |
415 | if (!thread) { | |
416 | goto error; | |
417 | } | |
2863220c JG |
418 | if (!wait_thread_status(notifiers)) { |
419 | lttng_thread_put(thread); | |
420 | thread = NULL; | |
421 | } | |
143745a6 | 422 | return thread; |
22b80785 JG |
423 | error: |
424 | cleanup_application_registration_thread(notifiers); | |
a7f04300 | 425 | error_alloc: |
143745a6 | 426 | return NULL; |
22b80785 | 427 | } |