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