Teardown the notification thread after the sessiond clean-up
[lttng-tools.git] / src / bin / lttng-sessiond / thread-utils.c
CommitLineData
a7333da7
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 "lttng-sessiond.h"
21#include "utils.h"
22#include <common/utils.h>
99d688f2
JG
23#include <pthread.h>
24
25#define USEC_PER_SEC 1000000
a7333da7
JG
26
27/*
28 * Quit pipe for all threads. This permits a single cancellation point
29 * for all threads when receiving an event on the pipe.
30 */
31static int thread_quit_pipe[2] = { -1, -1 };
32
33/*
34 * Init thread quit pipe.
35 *
36 * Return -1 on error or 0 if all pipes are created.
37 */
38static int __init_thread_quit_pipe(int *a_pipe)
39{
40 int ret, i;
41
42 ret = pipe(a_pipe);
43 if (ret < 0) {
44 PERROR("thread quit pipe");
45 goto error;
46 }
47
48 for (i = 0; i < 2; i++) {
49 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
50 if (ret < 0) {
51 PERROR("fcntl");
52 goto error;
53 }
54 }
55
56error:
57 return ret;
58}
59
60int sessiond_init_thread_quit_pipe(void)
61{
62 return __init_thread_quit_pipe(thread_quit_pipe);
63}
64
65int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
66{
67 return (fd == thread_quit_pipe[0] && (events & LPOLLIN));
68}
69
70/*
71 * Wait for a notification on the quit pipe (with a timeout).
72 *
99d688f2
JG
73 * A timeout value of -1U means no timeout.
74 *
a7333da7
JG
75 * Returns 1 if the caller should quit, 0 if the timeout was reached, and
76 * -1 if an error was encountered.
77 */
78int sessiond_wait_for_quit_pipe(unsigned int timeout_us)
79{
80 int ret;
81 fd_set read_fds;
82 struct timeval timeout;
83
84 FD_ZERO(&read_fds);
85 FD_SET(thread_quit_pipe[0], &read_fds);
86 memset(&timeout, 0, sizeof(timeout));
99d688f2
JG
87 timeout.tv_sec = timeout_us / USEC_PER_SEC;
88 timeout.tv_usec = timeout_us % USEC_PER_SEC;
a7333da7
JG
89
90 while (true) {
91 ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL,
99d688f2 92 timeout_us != -1U ? &timeout : NULL);
a7333da7
JG
93 if (ret < 0 && errno == EINTR) {
94 /* Retry on interrupt. */
95 continue;
96 } else {
97 break;
98 }
99 }
100
101 if (ret > 0) {
102 /* Should quit. */
103 ret = 1;
104 } else if (ret < 0 && errno != EINTR) {
105 /* Unknown error. */
106 PERROR("Failed to select() thread quit pipe");
107 ret = -1;
108 } else {
109 /* Timeout reached. */
110 ret = 0;
111 }
112
113 return ret;
114}
115
116int sessiond_notify_quit_pipe(void)
117{
118 return notify_thread_pipe(thread_quit_pipe[1]);
119}
120
121void sessiond_close_quit_pipe(void)
122{
123 utils_close_pipe(thread_quit_pipe);
124}
125
126static
127int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
128 int *a_pipe)
129{
130 int ret;
131
132 assert(events);
133
134 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
135 if (ret < 0) {
136 goto error;
137 }
138
139 /* Add quit pipe */
140 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
141 if (ret < 0) {
142 goto error;
143 }
144
145 return 0;
146
147error:
148 return ret;
149}
150
151/*
152 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
153 */
154int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
155{
156 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
157}
This page took 0.028484 seconds and 4 git commands to generate.