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