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