Commit | Line | Data |
---|---|---|
9fd92637 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
9fd92637 DG |
20 | #include <assert.h> |
21 | #include <fcntl.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | #include <common/common.h> | |
25 | ||
26 | #include "pipe.h" | |
27 | ||
28 | /* | |
29 | * Lock read side of a pipe. | |
30 | */ | |
31 | static void lock_read_side(struct lttng_pipe *pipe) | |
32 | { | |
33 | pthread_mutex_lock(&pipe->read_mutex); | |
34 | } | |
35 | ||
36 | /* | |
37 | * Unlock read side of a pipe. | |
38 | */ | |
39 | static void unlock_read_side(struct lttng_pipe *pipe) | |
40 | { | |
41 | pthread_mutex_unlock(&pipe->read_mutex); | |
42 | } | |
43 | ||
44 | /* | |
45 | * Lock write side of a pipe. | |
46 | */ | |
47 | static void lock_write_side(struct lttng_pipe *pipe) | |
48 | { | |
49 | pthread_mutex_lock(&pipe->write_mutex); | |
50 | } | |
51 | ||
52 | /* | |
53 | * Unlock write side of a pipe. | |
54 | */ | |
55 | static void unlock_write_side(struct lttng_pipe *pipe) | |
56 | { | |
57 | pthread_mutex_unlock(&pipe->write_mutex); | |
58 | } | |
59 | ||
60 | /* | |
61 | * Internal function. Close read side of pipe WITHOUT locking the mutex. | |
62 | * | |
63 | * Return 0 on success else a negative errno from close(2). | |
64 | */ | |
65 | static int _pipe_read_close(struct lttng_pipe *pipe) | |
66 | { | |
67 | int ret, ret_val = 0; | |
68 | ||
69 | assert(pipe); | |
70 | ||
71 | if (!lttng_pipe_is_read_open(pipe)) { | |
72 | goto end; | |
73 | } | |
74 | ||
75 | do { | |
76 | ret = close(pipe->fd[0]); | |
77 | } while (ret < 0 && errno == EINTR); | |
78 | if (ret < 0) { | |
79 | PERROR("close lttng read pipe"); | |
80 | ret_val = -errno; | |
81 | } | |
82 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; | |
83 | ||
84 | end: | |
85 | return ret_val; | |
86 | } | |
87 | ||
88 | /* | |
89 | * Internal function. Close write side of pipe WITHOUT locking the mutex. | |
90 | * | |
91 | * Return 0 on success else a negative errno from close(2). | |
92 | */ | |
93 | static int _pipe_write_close(struct lttng_pipe *pipe) | |
94 | { | |
95 | int ret, ret_val = 0; | |
96 | ||
97 | assert(pipe); | |
98 | ||
99 | if (!lttng_pipe_is_write_open(pipe)) { | |
100 | goto end; | |
101 | } | |
102 | ||
103 | do { | |
104 | ret = close(pipe->fd[1]); | |
105 | } while (ret < 0 && errno == EINTR); | |
106 | if (ret < 0) { | |
107 | PERROR("close lttng write pipe"); | |
108 | ret_val = -errno; | |
109 | } | |
110 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; | |
111 | ||
112 | end: | |
113 | return ret_val; | |
114 | } | |
115 | ||
116 | ||
117 | /* | |
118 | * Open a new lttng pipe and set flags using fcntl(). | |
119 | * | |
120 | * Return a newly allocated lttng pipe on success or else NULL. | |
121 | */ | |
122 | struct lttng_pipe *lttng_pipe_open(int flags) | |
123 | { | |
124 | int ret; | |
125 | struct lttng_pipe *p; | |
126 | ||
127 | p = zmalloc(sizeof(*p)); | |
128 | if (!p) { | |
129 | PERROR("zmalloc pipe open"); | |
130 | goto error; | |
131 | } | |
132 | ||
133 | ret = pipe(p->fd); | |
134 | if (ret < 0) { | |
135 | PERROR("lttng pipe"); | |
136 | goto error; | |
137 | } | |
138 | ||
139 | if (flags) { | |
140 | int i; | |
141 | ||
142 | for (i = 0; i < 2; i++) { | |
143 | ret = fcntl(p->fd[i], F_SETFD, flags); | |
144 | if (ret < 0) { | |
145 | PERROR("fcntl lttng pipe %d", flags); | |
146 | goto error; | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | pthread_mutex_init(&p->read_mutex, NULL); | |
152 | pthread_mutex_init(&p->write_mutex, NULL); | |
153 | p->r_state = LTTNG_PIPE_STATE_OPENED; | |
154 | p->w_state = LTTNG_PIPE_STATE_OPENED; | |
155 | p->flags = flags; | |
156 | ||
157 | return p; | |
158 | ||
159 | error: | |
160 | lttng_pipe_destroy(p); | |
161 | return NULL; | |
162 | } | |
163 | ||
164 | /* | |
165 | * Close read side of a lttng pipe. | |
166 | * | |
167 | * Return 0 on success else a negative value. | |
168 | */ | |
169 | int lttng_pipe_read_close(struct lttng_pipe *pipe) | |
170 | { | |
171 | int ret; | |
172 | ||
173 | assert(pipe); | |
174 | ||
175 | /* Handle read side first. */ | |
176 | lock_read_side(pipe); | |
177 | ret = _pipe_read_close(pipe); | |
178 | unlock_read_side(pipe); | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
183 | /* | |
184 | * Close write side of a lttng pipe. | |
185 | * | |
186 | * Return 0 on success else a negative value. | |
187 | */ | |
188 | int lttng_pipe_write_close(struct lttng_pipe *pipe) | |
189 | { | |
190 | int ret; | |
191 | ||
192 | assert(pipe); | |
193 | ||
194 | lock_write_side(pipe); | |
195 | ret = _pipe_write_close(pipe); | |
196 | unlock_write_side(pipe); | |
197 | ||
198 | return ret; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Close both read and write side of a lttng pipe. | |
203 | * | |
204 | * Return 0 on success else a negative value. | |
205 | */ | |
206 | int lttng_pipe_close(struct lttng_pipe *pipe) | |
207 | { | |
208 | int ret, ret_val = 0; | |
209 | ||
210 | assert(pipe); | |
211 | ||
212 | ret = lttng_pipe_read_close(pipe); | |
213 | if (ret < 0) { | |
214 | ret_val = ret; | |
215 | } | |
216 | ||
217 | ret = lttng_pipe_write_close(pipe); | |
218 | if (ret < 0) { | |
219 | ret_val = ret; | |
220 | } | |
221 | ||
222 | return ret_val; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Close and destroy a lttng pipe object. Finally, pipe is freed. | |
227 | */ | |
228 | void lttng_pipe_destroy(struct lttng_pipe *pipe) | |
229 | { | |
230 | int ret; | |
231 | ||
232 | if (!pipe) { | |
233 | return; | |
234 | } | |
235 | ||
236 | /* | |
237 | * Destroy should *never* be called with a locked mutex. These must always | |
238 | * succeed so we unlock them after the close pipe below. | |
239 | */ | |
240 | ret = pthread_mutex_trylock(&pipe->read_mutex); | |
241 | assert(!ret); | |
242 | ret = pthread_mutex_trylock(&pipe->write_mutex); | |
243 | assert(!ret); | |
244 | ||
245 | /* Close pipes WITHOUT trying to lock the pipes. */ | |
246 | (void) _pipe_read_close(pipe); | |
247 | (void) _pipe_write_close(pipe); | |
248 | ||
249 | unlock_read_side(pipe); | |
250 | unlock_write_side(pipe); | |
251 | ||
252 | (void) pthread_mutex_destroy(&pipe->read_mutex); | |
253 | (void) pthread_mutex_destroy(&pipe->write_mutex); | |
254 | ||
255 | free(pipe); | |
256 | } | |
257 | ||
258 | /* | |
259 | * Read on a lttng pipe and put the data in buf of at least size count. | |
260 | * | |
6cd525e8 MD |
261 | * Return "count" on success. Return < count on error. errno can be used |
262 | * to check the actual error. | |
9fd92637 DG |
263 | */ |
264 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count) | |
265 | { | |
6cd525e8 | 266 | ssize_t ret; |
9fd92637 DG |
267 | |
268 | assert(pipe); | |
269 | assert(buf); | |
270 | ||
271 | lock_read_side(pipe); | |
9fd92637 | 272 | if (!lttng_pipe_is_read_open(pipe)) { |
6cd525e8 MD |
273 | ret = -1; |
274 | errno = EBADF; | |
9fd92637 DG |
275 | goto error; |
276 | } | |
6cd525e8 | 277 | ret = lttng_read(pipe->fd[0], buf, count); |
9fd92637 DG |
278 | error: |
279 | unlock_read_side(pipe); | |
280 | return ret; | |
281 | } | |
282 | ||
283 | /* | |
284 | * Write on a lttng pipe using the data in buf and size of count. | |
285 | * | |
6cd525e8 MD |
286 | * Return "count" on success. Return < count on error. errno can be used |
287 | * to check the actual error. | |
9fd92637 DG |
288 | */ |
289 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, | |
290 | size_t count) | |
291 | { | |
6cd525e8 | 292 | ssize_t ret; |
9fd92637 DG |
293 | |
294 | assert(pipe); | |
295 | assert(buf); | |
296 | ||
297 | lock_write_side(pipe); | |
9fd92637 | 298 | if (!lttng_pipe_is_write_open(pipe)) { |
6cd525e8 MD |
299 | ret = -1; |
300 | errno = EBADF; | |
9fd92637 DG |
301 | goto error; |
302 | } | |
6cd525e8 | 303 | ret = lttng_write(pipe->fd[1], buf, count); |
9fd92637 DG |
304 | error: |
305 | unlock_write_side(pipe); | |
306 | return ret; | |
307 | } |