Fix: agent port file is o+w when launching as root
[lttng-tools.git] / src / bin / lttng-sessiond / register.c
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
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
27 struct thread_state {
28 struct lttng_pipe *quit_pipe;
29 struct ust_cmd_queue *ust_cmd_queue;
30 sem_t ready;
31 bool running;
32 int application_socket;
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
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);
65 goto error_close_socket;
66 }
67
68 DBG3("Session daemon application socket created (fd = %d) ", apps_sock);
69 ret = apps_sock;
70 end:
71 return ret;
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;
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 {
108 struct thread_state *thread_state = data;
109
110 if (!data) {
111 return;
112 }
113
114 lttng_pipe_destroy(thread_state->quit_pipe);
115 free(thread_state);
116 }
117
118 static void set_thread_status(struct thread_state *thread_state, bool running)
119 {
120 DBG("Marking application registration thread's state as %s", running ? "running" : "error");
121 thread_state->running = running;
122 sem_post(&thread_state->ready);
123 }
124
125 static bool wait_thread_status(struct thread_state *thread_state)
126 {
127 DBG("Waiting for application registration thread to be ready");
128 sem_wait(&thread_state->ready);
129 if (thread_state->running) {
130 DBG("Application registration thread is ready");
131 } else {
132 ERR("Initialization of application registration thread failed");
133 }
134
135 return thread_state->running;
136 }
137
138 static void thread_init_cleanup(void *data)
139 {
140 struct thread_state *thread_state = data;
141
142 set_thread_status(thread_state, false);
143 }
144
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;
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);
159 struct thread_state *thread_state = data;
160 const int application_socket = thread_state->application_socket;
161 const int quit_pipe_read_fd = lttng_pipe_get_readfd(
162 thread_state->quit_pipe);
163
164 DBG("[thread] Manage application registration started");
165
166 pthread_cleanup_push(thread_init_cleanup, thread_state);
167 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
168
169 ret = lttcomm_listen_unix_sock(application_socket);
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 */
184 ret = lttng_poll_add(&events, application_socket, LPOLLIN | LPOLLRDHUP);
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
195 set_thread_status(thread_state, true);
196 pthread_cleanup_pop(0);
197
198 if (testpoint(sessiond_thread_registration_apps)) {
199 goto error_poll_add;
200 }
201
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
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) {
236 sock = lttcomm_accept_unix_sock(application_socket);
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 }
269 sock = -1;
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 */
318 cds_wfcq_enqueue(&thread_state->ust_cmd_queue->head,
319 &thread_state->ust_cmd_queue->tail,
320 &ust_cmd->node);
321
322 /*
323 * Wake the registration queue futex. Implicit memory
324 * barrier with the exchange in cds_wfcq_enqueue.
325 */
326 futex_nto1_wake(&thread_state->ust_cmd_queue->futex);
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
343 ret = close(application_socket);
344 if (ret) {
345 PERROR("Failed to close application registration socket");
346 }
347 if (sock >= 0) {
348 ret = close(sock);
349 if (ret) {
350 PERROR("Failed to close application socket");
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:
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 {
371 struct thread_state *thread_state = data;
372 const int write_fd = lttng_pipe_get_writefd(thread_state->quit_pipe);
373
374 return notify_thread_pipe(write_fd) == 1;
375 }
376
377 struct lttng_thread *launch_application_registration_thread(
378 struct ust_cmd_queue *cmd_queue)
379 {
380 int ret;
381 struct lttng_pipe *quit_pipe;
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;
386
387 thread_state = zmalloc(sizeof(*thread_state));
388 if (!thread_state) {
389 goto error_alloc;
390 }
391 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
392 if (!quit_pipe) {
393 goto error;
394 }
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);
403
404 thread = lttng_thread_create("UST application registration",
405 thread_application_registration,
406 shutdown_application_registration_thread,
407 cleanup_application_registration_thread,
408 thread_state);
409 if (!thread) {
410 goto error;
411 }
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;
421 }
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
431 return thread;
432 error:
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 }
440 error_alloc:
441 return NULL;
442 }
This page took 0.04664 seconds and 4 git commands to generate.