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