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 if (flags
& O_NONBLOCK
) {
158 ret
= fcntl(pipe
->fd
[i
], F_SETFL
, O_NONBLOCK
);
160 PERROR("fcntl lttng pipe %d", flags
);
164 if (flags
& FD_CLOEXEC
) {
165 ret
= fcntl(pipe
->fd
[i
], F_SETFD
, FD_CLOEXEC
);
167 PERROR("fcntl lttng pipe %d", flags
);
172 * We only check for O_NONBLOCK or FD_CLOEXEC, if another flag is
173 * needed, we can add it, but for now just make sure we don't make
174 * mistakes with the parameters we pass.
176 if (!(flags
& O_NONBLOCK
) && !(flags
& FD_CLOEXEC
)) {
177 fprintf(stderr
, "Unsupported flag\n");
187 * Open a new lttng pipe and set flags using fcntl().
189 * Return a newly allocated lttng pipe on success or else NULL.
192 struct lttng_pipe
*lttng_pipe_open(int flags
)
195 struct lttng_pipe
*p
;
204 PERROR("lttng pipe");
207 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
208 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
210 ret
= _pipe_set_flags(p
, flags
);
219 lttng_pipe_destroy(p
);
224 * Open a new lttng pipe at path and set flags using fcntl().
226 * Return a newly allocated lttng pipe on success or else NULL.
229 struct lttng_pipe
*lttng_pipe_named_open(const char *path
, mode_t mode
,
233 struct lttng_pipe
*pipe
;
235 pipe
= _pipe_create();
240 ret
= mkfifo(path
, mode
);
246 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
252 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
254 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
260 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
262 ret
= _pipe_set_flags(pipe
, flags
);
270 lttng_pipe_destroy(pipe
);
275 * Close read side of a lttng pipe.
277 * Return 0 on success else a negative value.
280 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
286 /* Handle read side first. */
287 lock_read_side(pipe
);
288 ret
= _pipe_read_close(pipe
);
289 unlock_read_side(pipe
);
295 * Close write side of a lttng pipe.
297 * Return 0 on success else a negative value.
300 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
306 lock_write_side(pipe
);
307 ret
= _pipe_write_close(pipe
);
308 unlock_write_side(pipe
);
314 * Close both read and write side of a lttng pipe.
316 * Return 0 on success else a negative value.
319 int lttng_pipe_close(struct lttng_pipe
*pipe
)
321 int ret
, ret_val
= 0;
325 ret
= lttng_pipe_read_close(pipe
);
330 ret
= lttng_pipe_write_close(pipe
);
339 * Close and destroy a lttng pipe object. Finally, pipe is freed.
342 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
351 * Destroy should *never* be called with a locked mutex. These must always
352 * succeed so we unlock them after the close pipe below.
354 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
356 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
359 /* Close pipes WITHOUT trying to lock the pipes. */
360 (void) _pipe_read_close(pipe
);
361 (void) _pipe_write_close(pipe
);
363 unlock_read_side(pipe
);
364 unlock_write_side(pipe
);
366 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
367 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
373 * Read on a lttng pipe and put the data in buf of at least size count.
375 * Return "count" on success. Return < count on error. errno can be used
376 * to check the actual error.
379 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
386 lock_read_side(pipe
);
387 if (!lttng_pipe_is_read_open(pipe
)) {
392 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
394 unlock_read_side(pipe
);
399 * Write on a lttng pipe using the data in buf and size of count.
401 * Return "count" on success. Return < count on error. errno can be used
402 * to check the actual error.
405 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
413 lock_write_side(pipe
);
414 if (!lttng_pipe_is_write_open(pipe
)) {
419 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
421 unlock_write_side(pipe
);
426 * Return and release the read end of the pipe.
428 * This call transfers the ownership of the read fd of the underlying pipe
429 * to the caller if it is still open.
431 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
435 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
444 lock_read_side(pipe
);
445 if (!lttng_pipe_is_read_open(pipe
)) {
451 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
453 unlock_read_side(pipe
);
459 * Return and release the write end of the pipe.
461 * This call transfers the ownership of the write fd of the underlying pipe
462 * to the caller if it is still open.
464 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
468 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
477 lock_write_side(pipe
);
478 if (!lttng_pipe_is_write_open(pipe
)) {
484 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
486 unlock_write_side(pipe
);
This page took 0.051948 seconds and 4 git commands to generate.