2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/types.h>
15 #include <common/common.h>
20 * Lock read side of a pipe.
22 static void lock_read_side(struct lttng_pipe
*pipe
)
24 pthread_mutex_lock(&pipe
->read_mutex
);
28 * Unlock read side of a pipe.
30 static void unlock_read_side(struct lttng_pipe
*pipe
)
32 pthread_mutex_unlock(&pipe
->read_mutex
);
36 * Lock write side of a pipe.
38 static void lock_write_side(struct lttng_pipe
*pipe
)
40 pthread_mutex_lock(&pipe
->write_mutex
);
44 * Unlock write side of a pipe.
46 static void unlock_write_side(struct lttng_pipe
*pipe
)
48 pthread_mutex_unlock(&pipe
->write_mutex
);
52 * Internal function. Close read side of pipe WITHOUT locking the mutex.
54 * Return 0 on success else a negative errno from close(2).
56 static int _pipe_read_close(struct lttng_pipe
*pipe
)
62 if (!lttng_pipe_is_read_open(pipe
)) {
67 ret
= close(pipe
->fd
[0]);
68 } while (ret
< 0 && errno
== EINTR
);
70 PERROR("close lttng read pipe");
73 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
80 * Internal function. Close write side of pipe WITHOUT locking the mutex.
82 * Return 0 on success else a negative errno from close(2).
84 static int _pipe_write_close(struct lttng_pipe
*pipe
)
90 if (!lttng_pipe_is_write_open(pipe
)) {
95 ret
= close(pipe
->fd
[1]);
96 } while (ret
< 0 && errno
== EINTR
);
98 PERROR("close lttng write pipe");
101 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
107 static struct lttng_pipe
*_pipe_create(void)
110 struct lttng_pipe
*p
;
112 p
= zmalloc(sizeof(*p
));
114 PERROR("zmalloc pipe create");
117 p
->fd
[0] = p
->fd
[1] = -1;
119 ret
= pthread_mutex_init(&p
->read_mutex
, NULL
);
121 PERROR("pthread_mutex_init read lock pipe create");
124 ret
= pthread_mutex_init(&p
->write_mutex
, NULL
);
126 PERROR("pthread_mutex_init write lock pipe create");
127 goto error_destroy_rmutex
;
131 error_destroy_rmutex
:
132 (void) pthread_mutex_destroy(&p
->read_mutex
);
138 static int _pipe_set_flags(struct lttng_pipe
*pipe
, int flags
)
146 for (i
= 0; i
< 2; i
++) {
147 if (flags
& O_NONBLOCK
) {
148 ret
= fcntl(pipe
->fd
[i
], F_SETFL
, O_NONBLOCK
);
150 PERROR("fcntl lttng pipe %d", flags
);
154 if (flags
& FD_CLOEXEC
) {
155 ret
= fcntl(pipe
->fd
[i
], F_SETFD
, FD_CLOEXEC
);
157 PERROR("fcntl lttng pipe %d", flags
);
162 * We only check for O_NONBLOCK or FD_CLOEXEC, if another flag is
163 * needed, we can add it, but for now just make sure we don't make
164 * mistakes with the parameters we pass.
166 if (!(flags
& O_NONBLOCK
) && !(flags
& FD_CLOEXEC
)) {
167 fprintf(stderr
, "Unsupported flag\n");
177 * Open a new lttng pipe and set flags using fcntl().
179 * Return a newly allocated lttng pipe on success or else NULL.
182 struct lttng_pipe
*lttng_pipe_open(int flags
)
185 struct lttng_pipe
*p
;
194 PERROR("lttng pipe");
197 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
198 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
200 ret
= _pipe_set_flags(p
, flags
);
209 lttng_pipe_destroy(p
);
214 * Open a new lttng pipe at path and set flags using fcntl().
216 * Return a newly allocated lttng pipe on success or else NULL.
219 struct lttng_pipe
*lttng_pipe_named_open(const char *path
, mode_t mode
,
223 struct lttng_pipe
*pipe
;
225 pipe
= _pipe_create();
230 ret
= mkfifo(path
, mode
);
236 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
242 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
244 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
250 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
252 ret
= _pipe_set_flags(pipe
, flags
);
260 lttng_pipe_destroy(pipe
);
265 * Close read side of a lttng pipe.
267 * Return 0 on success else a negative value.
270 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
276 /* Handle read side first. */
277 lock_read_side(pipe
);
278 ret
= _pipe_read_close(pipe
);
279 unlock_read_side(pipe
);
285 * Close write side of a lttng pipe.
287 * Return 0 on success else a negative value.
290 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
296 lock_write_side(pipe
);
297 ret
= _pipe_write_close(pipe
);
298 unlock_write_side(pipe
);
304 * Close both read and write side of a lttng pipe.
306 * Return 0 on success else a negative value.
309 int lttng_pipe_close(struct lttng_pipe
*pipe
)
311 int ret
, ret_val
= 0;
315 ret
= lttng_pipe_read_close(pipe
);
320 ret
= lttng_pipe_write_close(pipe
);
329 * Close and destroy a lttng pipe object. Finally, pipe is freed.
332 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
341 * Destroy should *never* be called with a locked mutex. These must always
342 * succeed so we unlock them after the close pipe below.
344 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
346 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
349 /* Close pipes WITHOUT trying to lock the pipes. */
350 (void) _pipe_read_close(pipe
);
351 (void) _pipe_write_close(pipe
);
353 unlock_read_side(pipe
);
354 unlock_write_side(pipe
);
356 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
357 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
363 * Read on a lttng pipe and put the data in buf of at least size count.
365 * Return "count" on success. Return < count on error. errno can be used
366 * to check the actual error.
369 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
376 lock_read_side(pipe
);
377 if (!lttng_pipe_is_read_open(pipe
)) {
382 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
384 unlock_read_side(pipe
);
389 * Write on a lttng pipe using the data in buf and size of count.
391 * Return "count" on success. Return < count on error. errno can be used
392 * to check the actual error.
395 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
403 lock_write_side(pipe
);
404 if (!lttng_pipe_is_write_open(pipe
)) {
409 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
411 unlock_write_side(pipe
);
416 * Return and release the read end of the pipe.
418 * This call transfers the ownership of the read fd of the underlying pipe
419 * to the caller if it is still open.
421 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
425 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
434 lock_read_side(pipe
);
435 if (!lttng_pipe_is_read_open(pipe
)) {
441 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
443 unlock_read_side(pipe
);
449 * Return and release the write end of the pipe.
451 * This call transfers the ownership of the write fd of the underlying pipe
452 * to the caller if it is still open.
454 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
458 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
467 lock_write_side(pipe
);
468 if (!lttng_pipe_is_write_open(pipe
)) {
474 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
476 unlock_write_side(pipe
);
This page took 0.038962 seconds and 4 git commands to generate.