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