2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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.
22 #include <sys/types.h>
25 #include <common/common.h>
30 * Lock read side of a pipe.
32 static void lock_read_side(struct lttng_pipe
*pipe
)
34 pthread_mutex_lock(&pipe
->read_mutex
);
38 * Unlock read side of a pipe.
40 static void unlock_read_side(struct lttng_pipe
*pipe
)
42 pthread_mutex_unlock(&pipe
->read_mutex
);
46 * Lock write side of a pipe.
48 static void lock_write_side(struct lttng_pipe
*pipe
)
50 pthread_mutex_lock(&pipe
->write_mutex
);
54 * Unlock write side of a pipe.
56 static void unlock_write_side(struct lttng_pipe
*pipe
)
58 pthread_mutex_unlock(&pipe
->write_mutex
);
62 * Internal function. Close read side of pipe WITHOUT locking the mutex.
64 * Return 0 on success else a negative errno from close(2).
66 static int _pipe_read_close(struct lttng_pipe
*pipe
)
72 if (!lttng_pipe_is_read_open(pipe
)) {
77 ret
= close(pipe
->fd
[0]);
78 } while (ret
< 0 && errno
== EINTR
);
80 PERROR("close lttng read pipe");
83 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
90 * Internal function. Close write side of pipe WITHOUT locking the mutex.
92 * Return 0 on success else a negative errno from close(2).
94 static int _pipe_write_close(struct lttng_pipe
*pipe
)
100 if (!lttng_pipe_is_write_open(pipe
)) {
105 ret
= close(pipe
->fd
[1]);
106 } while (ret
< 0 && errno
== EINTR
);
108 PERROR("close lttng write pipe");
111 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
117 static struct lttng_pipe
*_pipe_create(void)
120 struct lttng_pipe
*p
;
122 p
= zmalloc(sizeof(*p
));
124 PERROR("zmalloc pipe create");
127 p
->fd
[0] = p
->fd
[1] = -1;
129 ret
= pthread_mutex_init(&p
->read_mutex
, NULL
);
131 PERROR("pthread_mutex_init read lock pipe create");
134 ret
= pthread_mutex_init(&p
->write_mutex
, NULL
);
136 PERROR("pthread_mutex_init write lock pipe create");
137 goto error_destroy_rmutex
;
141 error_destroy_rmutex
:
142 (void) pthread_mutex_destroy(&p
->read_mutex
);
148 static int _pipe_set_flags(struct lttng_pipe
*pipe
, int flags
)
156 for (i
= 0; i
< 2; i
++) {
157 ret
= fcntl(pipe
->fd
[i
], F_SETFD
, flags
);
159 PERROR("fcntl lttng pipe %d", flags
);
168 * Open a new lttng pipe and set flags using fcntl().
170 * Return a newly allocated lttng pipe on success or else NULL.
173 struct lttng_pipe
*lttng_pipe_open(int flags
)
176 struct lttng_pipe
*p
;
185 PERROR("lttng pipe");
188 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
189 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
191 ret
= _pipe_set_flags(p
, flags
);
200 lttng_pipe_destroy(p
);
205 * Open a new lttng pipe at path and set flags using fcntl().
207 * Return a newly allocated lttng pipe on success or else NULL.
210 struct lttng_pipe
*lttng_pipe_named_open(const char *path
, mode_t mode
,
214 struct lttng_pipe
*pipe
;
216 pipe
= _pipe_create();
221 ret
= mkfifo(path
, mode
);
227 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
233 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
235 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
241 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
243 ret
= _pipe_set_flags(pipe
, flags
);
251 lttng_pipe_destroy(pipe
);
256 * Close read side of a lttng pipe.
258 * Return 0 on success else a negative value.
261 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
267 /* Handle read side first. */
268 lock_read_side(pipe
);
269 ret
= _pipe_read_close(pipe
);
270 unlock_read_side(pipe
);
276 * Close write side of a lttng pipe.
278 * Return 0 on success else a negative value.
281 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
287 lock_write_side(pipe
);
288 ret
= _pipe_write_close(pipe
);
289 unlock_write_side(pipe
);
295 * Close both read and write side of a lttng pipe.
297 * Return 0 on success else a negative value.
300 int lttng_pipe_close(struct lttng_pipe
*pipe
)
302 int ret
, ret_val
= 0;
306 ret
= lttng_pipe_read_close(pipe
);
311 ret
= lttng_pipe_write_close(pipe
);
320 * Close and destroy a lttng pipe object. Finally, pipe is freed.
323 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
332 * Destroy should *never* be called with a locked mutex. These must always
333 * succeed so we unlock them after the close pipe below.
335 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
337 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
340 /* Close pipes WITHOUT trying to lock the pipes. */
341 (void) _pipe_read_close(pipe
);
342 (void) _pipe_write_close(pipe
);
344 unlock_read_side(pipe
);
345 unlock_write_side(pipe
);
347 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
348 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
354 * Read on a lttng pipe and put the data in buf of at least size count.
356 * Return "count" on success. Return < count on error. errno can be used
357 * to check the actual error.
360 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
367 lock_read_side(pipe
);
368 if (!lttng_pipe_is_read_open(pipe
)) {
373 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
375 unlock_read_side(pipe
);
380 * Write on a lttng pipe using the data in buf and size of count.
382 * Return "count" on success. Return < count on error. errno can be used
383 * to check the actual error.
386 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
394 lock_write_side(pipe
);
395 if (!lttng_pipe_is_write_open(pipe
)) {
400 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
402 unlock_write_side(pipe
);
407 * Return and release the read end of the pipe.
409 * This call transfers the ownership of the read fd of the underlying pipe
410 * to the caller if it is still open.
412 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
416 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
425 lock_read_side(pipe
);
426 if (!lttng_pipe_is_read_open(pipe
)) {
432 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
434 unlock_read_side(pipe
);
440 * Return and release the write end of the pipe.
442 * This call transfers the ownership of the write fd of the underlying pipe
443 * to the caller if it is still open.
445 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
449 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
458 lock_write_side(pipe
);
459 if (!lttng_pipe_is_write_open(pipe
)) {
465 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
467 unlock_write_side(pipe
);
This page took 0.04979 seconds and 4 git commands to generate.