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