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