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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
9fd92637 DG |
19 | #include <assert.h> |
20 | #include <fcntl.h> | |
21 | #include <unistd.h> | |
9c2bd8db JG |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
9fd92637 DG |
24 | |
25 | #include <common/common.h> | |
26 | ||
27 | #include "pipe.h" | |
28 | ||
29 | /* | |
30 | * Lock read side of a pipe. | |
31 | */ | |
32 | static void lock_read_side(struct lttng_pipe *pipe) | |
33 | { | |
34 | pthread_mutex_lock(&pipe->read_mutex); | |
35 | } | |
36 | ||
37 | /* | |
38 | * Unlock read side of a pipe. | |
39 | */ | |
40 | static void unlock_read_side(struct lttng_pipe *pipe) | |
41 | { | |
42 | pthread_mutex_unlock(&pipe->read_mutex); | |
43 | } | |
44 | ||
45 | /* | |
46 | * Lock write side of a pipe. | |
47 | */ | |
48 | static void lock_write_side(struct lttng_pipe *pipe) | |
49 | { | |
50 | pthread_mutex_lock(&pipe->write_mutex); | |
51 | } | |
52 | ||
53 | /* | |
54 | * Unlock write side of a pipe. | |
55 | */ | |
56 | static void unlock_write_side(struct lttng_pipe *pipe) | |
57 | { | |
58 | pthread_mutex_unlock(&pipe->write_mutex); | |
59 | } | |
60 | ||
61 | /* | |
62 | * Internal function. Close read side of pipe WITHOUT locking the mutex. | |
63 | * | |
64 | * Return 0 on success else a negative errno from close(2). | |
65 | */ | |
66 | static int _pipe_read_close(struct lttng_pipe *pipe) | |
67 | { | |
68 | int ret, ret_val = 0; | |
69 | ||
70 | assert(pipe); | |
71 | ||
72 | if (!lttng_pipe_is_read_open(pipe)) { | |
73 | goto end; | |
74 | } | |
75 | ||
76 | do { | |
77 | ret = close(pipe->fd[0]); | |
78 | } while (ret < 0 && errno == EINTR); | |
79 | if (ret < 0) { | |
80 | PERROR("close lttng read pipe"); | |
81 | ret_val = -errno; | |
82 | } | |
83 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; | |
84 | ||
85 | end: | |
86 | return ret_val; | |
87 | } | |
88 | ||
89 | /* | |
90 | * Internal function. Close write side of pipe WITHOUT locking the mutex. | |
91 | * | |
92 | * Return 0 on success else a negative errno from close(2). | |
93 | */ | |
94 | static int _pipe_write_close(struct lttng_pipe *pipe) | |
95 | { | |
96 | int ret, ret_val = 0; | |
97 | ||
98 | assert(pipe); | |
99 | ||
100 | if (!lttng_pipe_is_write_open(pipe)) { | |
101 | goto end; | |
102 | } | |
103 | ||
104 | do { | |
105 | ret = close(pipe->fd[1]); | |
106 | } while (ret < 0 && errno == EINTR); | |
107 | if (ret < 0) { | |
108 | PERROR("close lttng write pipe"); | |
109 | ret_val = -errno; | |
110 | } | |
111 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; | |
112 | ||
113 | end: | |
114 | return ret_val; | |
115 | } | |
116 | ||
9c2bd8db JG |
117 | static struct lttng_pipe *_pipe_create(void) |
118 | { | |
119 | int ret; | |
120 | struct lttng_pipe *p; | |
121 | ||
122 | p = zmalloc(sizeof(*p)); | |
123 | if (!p) { | |
124 | PERROR("zmalloc pipe create"); | |
125 | goto end; | |
126 | } | |
127 | p->fd[0] = p->fd[1] = -1; | |
128 | ||
129 | ret = pthread_mutex_init(&p->read_mutex, NULL); | |
130 | if (ret) { | |
131 | PERROR("pthread_mutex_init read lock pipe create"); | |
132 | goto error_destroy; | |
133 | } | |
134 | ret = pthread_mutex_init(&p->write_mutex, NULL); | |
135 | if (ret) { | |
136 | PERROR("pthread_mutex_init write lock pipe create"); | |
137 | goto error_destroy_rmutex; | |
138 | } | |
139 | end: | |
140 | return p; | |
141 | error_destroy_rmutex: | |
142 | (void) pthread_mutex_destroy(&p->read_mutex); | |
143 | error_destroy: | |
144 | free(p); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | static int _pipe_set_flags(struct lttng_pipe *pipe, int flags) | |
149 | { | |
150 | int i, ret = 0; | |
151 | ||
152 | if (!flags) { | |
153 | goto end; | |
154 | } | |
155 | ||
156 | for (i = 0; i < 2; i++) { | |
157 | ret = fcntl(pipe->fd[i], F_SETFD, flags); | |
158 | if (ret < 0) { | |
159 | PERROR("fcntl lttng pipe %d", flags); | |
160 | goto end; | |
161 | } | |
162 | } | |
163 | end: | |
164 | return ret; | |
165 | } | |
9fd92637 DG |
166 | |
167 | /* | |
168 | * Open a new lttng pipe and set flags using fcntl(). | |
169 | * | |
170 | * Return a newly allocated lttng pipe on success or else NULL. | |
171 | */ | |
a247c6e0 | 172 | LTTNG_HIDDEN |
9fd92637 DG |
173 | struct lttng_pipe *lttng_pipe_open(int flags) |
174 | { | |
175 | int ret; | |
176 | struct lttng_pipe *p; | |
177 | ||
9c2bd8db | 178 | p = _pipe_create(); |
9fd92637 | 179 | if (!p) { |
9fd92637 DG |
180 | goto error; |
181 | } | |
182 | ||
183 | ret = pipe(p->fd); | |
184 | if (ret < 0) { | |
185 | PERROR("lttng pipe"); | |
186 | goto error; | |
187 | } | |
9c2bd8db JG |
188 | p->r_state = LTTNG_PIPE_STATE_OPENED; |
189 | p->w_state = LTTNG_PIPE_STATE_OPENED; | |
9fd92637 | 190 | |
9c2bd8db JG |
191 | ret = _pipe_set_flags(p, flags); |
192 | if (ret) { | |
193 | goto error; | |
9fd92637 DG |
194 | } |
195 | ||
9fd92637 DG |
196 | p->flags = flags; |
197 | ||
198 | return p; | |
9fd92637 DG |
199 | error: |
200 | lttng_pipe_destroy(p); | |
201 | return NULL; | |
202 | } | |
203 | ||
9c2bd8db JG |
204 | /* |
205 | * Open a new lttng pipe at path and set flags using fcntl(). | |
206 | * | |
207 | * Return a newly allocated lttng pipe on success or else NULL. | |
208 | */ | |
209 | LTTNG_HIDDEN | |
210 | struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode, | |
211 | int flags) | |
212 | { | |
213 | int ret, fd_r, fd_w; | |
214 | struct lttng_pipe *pipe; | |
215 | ||
216 | pipe = _pipe_create(); | |
217 | if (!pipe) { | |
218 | goto error; | |
219 | } | |
220 | ||
221 | ret = mkfifo(path, mode); | |
222 | if (ret) { | |
223 | PERROR("mkfifo"); | |
224 | goto error; | |
225 | } | |
226 | ||
227 | fd_r = open(path, O_RDONLY | O_NONBLOCK); | |
228 | if (fd_r < 0) { | |
229 | PERROR("open fifo"); | |
230 | ret = fd_r; | |
231 | goto error; | |
232 | } | |
233 | pipe->fd[0] = fd_r; | |
234 | pipe->r_state = LTTNG_PIPE_STATE_OPENED; | |
235 | ||
236 | fd_w = open(path, O_WRONLY | O_NONBLOCK); | |
237 | if (fd_w < 0) { | |
238 | PERROR("open fifo"); | |
239 | ret = fd_w; | |
240 | goto error; | |
241 | } | |
242 | pipe->fd[1] = fd_w; | |
243 | pipe->w_state = LTTNG_PIPE_STATE_OPENED; | |
244 | ||
245 | ret = _pipe_set_flags(pipe, flags); | |
246 | if (ret) { | |
247 | goto error; | |
248 | } | |
249 | pipe->flags = flags; | |
250 | ||
251 | return pipe; | |
252 | error: | |
253 | lttng_pipe_destroy(pipe); | |
254 | return NULL; | |
255 | } | |
256 | ||
9fd92637 DG |
257 | /* |
258 | * Close read side of a lttng pipe. | |
259 | * | |
260 | * Return 0 on success else a negative value. | |
261 | */ | |
a247c6e0 | 262 | LTTNG_HIDDEN |
9fd92637 DG |
263 | int lttng_pipe_read_close(struct lttng_pipe *pipe) |
264 | { | |
265 | int ret; | |
266 | ||
267 | assert(pipe); | |
268 | ||
269 | /* Handle read side first. */ | |
270 | lock_read_side(pipe); | |
271 | ret = _pipe_read_close(pipe); | |
272 | unlock_read_side(pipe); | |
273 | ||
274 | return ret; | |
275 | } | |
276 | ||
277 | /* | |
278 | * Close write side of a lttng pipe. | |
279 | * | |
280 | * Return 0 on success else a negative value. | |
281 | */ | |
a247c6e0 | 282 | LTTNG_HIDDEN |
9fd92637 DG |
283 | int lttng_pipe_write_close(struct lttng_pipe *pipe) |
284 | { | |
285 | int ret; | |
286 | ||
287 | assert(pipe); | |
288 | ||
289 | lock_write_side(pipe); | |
290 | ret = _pipe_write_close(pipe); | |
291 | unlock_write_side(pipe); | |
292 | ||
293 | return ret; | |
294 | } | |
295 | ||
296 | /* | |
297 | * Close both read and write side of a lttng pipe. | |
298 | * | |
299 | * Return 0 on success else a negative value. | |
300 | */ | |
a247c6e0 | 301 | LTTNG_HIDDEN |
9fd92637 DG |
302 | int lttng_pipe_close(struct lttng_pipe *pipe) |
303 | { | |
304 | int ret, ret_val = 0; | |
305 | ||
306 | assert(pipe); | |
307 | ||
308 | ret = lttng_pipe_read_close(pipe); | |
309 | if (ret < 0) { | |
310 | ret_val = ret; | |
311 | } | |
312 | ||
313 | ret = lttng_pipe_write_close(pipe); | |
314 | if (ret < 0) { | |
315 | ret_val = ret; | |
316 | } | |
317 | ||
318 | return ret_val; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Close and destroy a lttng pipe object. Finally, pipe is freed. | |
323 | */ | |
a247c6e0 | 324 | LTTNG_HIDDEN |
9fd92637 DG |
325 | void lttng_pipe_destroy(struct lttng_pipe *pipe) |
326 | { | |
327 | int ret; | |
328 | ||
329 | if (!pipe) { | |
330 | return; | |
331 | } | |
332 | ||
333 | /* | |
334 | * Destroy should *never* be called with a locked mutex. These must always | |
335 | * succeed so we unlock them after the close pipe below. | |
336 | */ | |
337 | ret = pthread_mutex_trylock(&pipe->read_mutex); | |
338 | assert(!ret); | |
339 | ret = pthread_mutex_trylock(&pipe->write_mutex); | |
340 | assert(!ret); | |
341 | ||
342 | /* Close pipes WITHOUT trying to lock the pipes. */ | |
343 | (void) _pipe_read_close(pipe); | |
344 | (void) _pipe_write_close(pipe); | |
345 | ||
346 | unlock_read_side(pipe); | |
347 | unlock_write_side(pipe); | |
348 | ||
349 | (void) pthread_mutex_destroy(&pipe->read_mutex); | |
350 | (void) pthread_mutex_destroy(&pipe->write_mutex); | |
351 | ||
352 | free(pipe); | |
353 | } | |
354 | ||
355 | /* | |
356 | * Read on a lttng pipe and put the data in buf of at least size count. | |
357 | * | |
6cd525e8 MD |
358 | * Return "count" on success. Return < count on error. errno can be used |
359 | * to check the actual error. | |
9fd92637 | 360 | */ |
a247c6e0 | 361 | LTTNG_HIDDEN |
9fd92637 DG |
362 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count) |
363 | { | |
6cd525e8 | 364 | ssize_t ret; |
9fd92637 DG |
365 | |
366 | assert(pipe); | |
367 | assert(buf); | |
368 | ||
369 | lock_read_side(pipe); | |
9fd92637 | 370 | if (!lttng_pipe_is_read_open(pipe)) { |
6cd525e8 MD |
371 | ret = -1; |
372 | errno = EBADF; | |
9fd92637 DG |
373 | goto error; |
374 | } | |
6cd525e8 | 375 | ret = lttng_read(pipe->fd[0], buf, count); |
9fd92637 DG |
376 | error: |
377 | unlock_read_side(pipe); | |
378 | return ret; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Write on a lttng pipe using the data in buf and size of count. | |
383 | * | |
6cd525e8 MD |
384 | * Return "count" on success. Return < count on error. errno can be used |
385 | * to check the actual error. | |
9fd92637 | 386 | */ |
a247c6e0 | 387 | LTTNG_HIDDEN |
9fd92637 DG |
388 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, |
389 | size_t count) | |
390 | { | |
6cd525e8 | 391 | ssize_t ret; |
9fd92637 DG |
392 | |
393 | assert(pipe); | |
394 | assert(buf); | |
395 | ||
396 | lock_write_side(pipe); | |
9fd92637 | 397 | if (!lttng_pipe_is_write_open(pipe)) { |
6cd525e8 MD |
398 | ret = -1; |
399 | errno = EBADF; | |
9fd92637 DG |
400 | goto error; |
401 | } | |
6cd525e8 | 402 | ret = lttng_write(pipe->fd[1], buf, count); |
9fd92637 DG |
403 | error: |
404 | unlock_write_side(pipe); | |
405 | return ret; | |
406 | } | |
55dfb029 JG |
407 | |
408 | /* | |
409 | * Return and release the read end of the pipe. | |
410 | * | |
411 | * This call transfers the ownership of the read fd of the underlying pipe | |
412 | * to the caller if it is still open. | |
413 | * | |
414 | * Returns the fd of the read end of the pipe, or -1 if it was already closed or | |
415 | * released. | |
416 | */ | |
417 | LTTNG_HIDDEN | |
418 | int lttng_pipe_release_readfd(struct lttng_pipe *pipe) | |
419 | { | |
420 | int ret; | |
421 | ||
422 | if (!pipe) { | |
423 | ret = -1; | |
424 | goto end; | |
425 | } | |
426 | ||
427 | lock_read_side(pipe); | |
428 | if (!lttng_pipe_is_read_open(pipe)) { | |
429 | ret = -1; | |
430 | goto end_unlock; | |
431 | } | |
432 | ret = pipe->fd[0]; | |
433 | pipe->fd[0] = -1; | |
434 | pipe->r_state = LTTNG_PIPE_STATE_CLOSED; | |
435 | end_unlock: | |
436 | unlock_read_side(pipe); | |
437 | end: | |
438 | return ret; | |
439 | } | |
440 | ||
441 | /* | |
442 | * Return and release the write end of the pipe. | |
443 | * | |
444 | * This call transfers the ownership of the write fd of the underlying pipe | |
445 | * to the caller if it is still open. | |
446 | * | |
447 | * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed | |
448 | * or released. | |
449 | */ | |
450 | LTTNG_HIDDEN | |
451 | int lttng_pipe_release_writefd(struct lttng_pipe *pipe) | |
452 | { | |
453 | int ret; | |
454 | ||
455 | if (!pipe) { | |
456 | ret = -1; | |
457 | goto end; | |
458 | } | |
459 | ||
460 | lock_write_side(pipe); | |
461 | if (!lttng_pipe_is_write_open(pipe)) { | |
462 | ret = -1; | |
463 | goto end_unlock; | |
464 | } | |
465 | ret = pipe->fd[1]; | |
466 | pipe->fd[1] = -1; | |
467 | pipe->w_state = LTTNG_PIPE_STATE_CLOSED; | |
468 | end_unlock: | |
469 | unlock_write_side(pipe); | |
470 | end: | |
471 | return ret; | |
472 | } |