Fix: sessiond: leak of application socket on chmod failure
[lttng-tools.git] / src / bin / lttng-sessiond / register.c
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
37 struct thread_notifiers {
38 struct lttng_pipe *quit_pipe;
39 struct ust_cmd_queue *ust_cmd_queue;
40 sem_t ready;
41 bool running;
42 };
43
44 /*
45 * Creates the application socket.
46 */
47 static 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);
75 goto error_close_socket;
76 }
77
78 DBG3("Session daemon application socket created (fd = %d) ", apps_sock);
79 ret = apps_sock;
80 end:
81 umask(old_umask);
82 return ret;
83 error_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;
90 }
91
92 /*
93 * Notify UST applications using the shm mmap futex.
94 */
95 static 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
113 error:
114 return -1;
115 }
116
117 static 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
125 static void set_thread_status(struct thread_notifiers *notifiers, bool running)
126 {
127 DBG("Marking application registration thread's state as %s", running ? "running" : "error");
128 notifiers->running = running;
129 sem_post(&notifiers->ready);
130 }
131
132 static bool wait_thread_status(struct thread_notifiers *notifiers)
133 {
134 DBG("Waiting for application registration thread to be ready");
135 sem_wait(&notifiers->ready);
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
145 static void thread_init_cleanup(void *data)
146 {
147 struct thread_notifiers *notifiers = data;
148
149 set_thread_status(notifiers, false);
150 }
151
152 /*
153 * This thread manage application registration.
154 */
155 static 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
173 pthread_cleanup_push(thread_init_cleanup, NULL);
174 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
175
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
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 }
192
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
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
357 exit:
358 error:
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
377 error_poll_add:
378 lttng_poll_clean(&events);
379 error_listen:
380 error_create_poll:
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
390 static 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
398 struct lttng_thread *launch_application_registration_thread(
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
405 notifiers = zmalloc(sizeof(*notifiers));
406 if (!notifiers) {
407 goto error_alloc;
408 }
409 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
410 if (!quit_pipe) {
411 goto error;
412 }
413 notifiers->quit_pipe = quit_pipe;
414 notifiers->ust_cmd_queue = cmd_queue;
415 sem_init(&notifiers->ready, 0, 0);
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 }
425 if (!wait_thread_status(notifiers)) {
426 lttng_thread_put(thread);
427 thread = NULL;
428 }
429 return thread;
430 error:
431 cleanup_application_registration_thread(notifiers);
432 error_alloc:
433 return NULL;
434 }
This page took 0.037511 seconds and 4 git commands to generate.