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