c2dce78816f0fad0e41a935d49371b20f5bbcd0d
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <sys/types.h>
14 #include <common/common.hpp>
19 * Lock read side of a pipe.
21 static void lock_read_side(struct lttng_pipe
*pipe
)
23 pthread_mutex_lock(&pipe
->read_mutex
);
27 * Unlock read side of a pipe.
29 static void unlock_read_side(struct lttng_pipe
*pipe
)
31 pthread_mutex_unlock(&pipe
->read_mutex
);
35 * Lock write side of a pipe.
37 static void lock_write_side(struct lttng_pipe
*pipe
)
39 pthread_mutex_lock(&pipe
->write_mutex
);
43 * Unlock write side of a pipe.
45 static void unlock_write_side(struct lttng_pipe
*pipe
)
47 pthread_mutex_unlock(&pipe
->write_mutex
);
51 * Internal function. Close read side of pipe WITHOUT locking the mutex.
53 * Return 0 on success else a negative errno from close(2).
55 static int _pipe_read_close(struct lttng_pipe
*pipe
)
61 if (!lttng_pipe_is_read_open(pipe
)) {
66 ret
= close(pipe
->fd
[0]);
67 } while (ret
< 0 && errno
== EINTR
);
69 PERROR("close lttng read pipe");
72 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
79 * Internal function. Close write side of pipe WITHOUT locking the mutex.
81 * Return 0 on success else a negative errno from close(2).
83 static int _pipe_write_close(struct lttng_pipe
*pipe
)
89 if (!lttng_pipe_is_write_open(pipe
)) {
94 ret
= close(pipe
->fd
[1]);
95 } while (ret
< 0 && errno
== EINTR
);
97 PERROR("close lttng write pipe");
100 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
106 static struct lttng_pipe
*_pipe_create(void)
109 struct lttng_pipe
*p
;
111 p
= (lttng_pipe
*) zmalloc(sizeof(*p
));
113 PERROR("zmalloc pipe create");
116 p
->fd
[0] = p
->fd
[1] = -1;
118 ret
= pthread_mutex_init(&p
->read_mutex
, NULL
);
120 PERROR("pthread_mutex_init read lock pipe create");
123 ret
= pthread_mutex_init(&p
->write_mutex
, NULL
);
125 PERROR("pthread_mutex_init write lock pipe create");
126 goto error_destroy_rmutex
;
130 error_destroy_rmutex
:
131 (void) pthread_mutex_destroy(&p
->read_mutex
);
137 static int _pipe_set_flags(struct lttng_pipe
*pipe
, int flags
)
145 for (i
= 0; i
< 2; i
++) {
146 if (flags
& O_NONBLOCK
) {
147 ret
= fcntl(pipe
->fd
[i
], F_SETFL
, O_NONBLOCK
);
149 PERROR("fcntl lttng pipe %d", flags
);
153 if (flags
& FD_CLOEXEC
) {
154 ret
= fcntl(pipe
->fd
[i
], F_SETFD
, FD_CLOEXEC
);
156 PERROR("fcntl lttng pipe %d", flags
);
161 * We only check for O_NONBLOCK or FD_CLOEXEC, if another flag is
162 * needed, we can add it, but for now just make sure we don't make
163 * mistakes with the parameters we pass.
165 if (!(flags
& O_NONBLOCK
) && !(flags
& FD_CLOEXEC
)) {
166 fprintf(stderr
, "Unsupported flag\n");
176 * Open a new lttng pipe and set flags using fcntl().
178 * Return a newly allocated lttng pipe on success or else NULL.
180 struct lttng_pipe
*lttng_pipe_open(int flags
)
183 struct lttng_pipe
*p
;
192 PERROR("lttng pipe");
195 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
196 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
198 ret
= _pipe_set_flags(p
, flags
);
207 lttng_pipe_destroy(p
);
212 * Open a new lttng pipe at path and set flags using fcntl().
214 * Return a newly allocated lttng pipe on success or else NULL.
216 struct lttng_pipe
*lttng_pipe_named_open(const char *path
, mode_t mode
,
220 struct lttng_pipe
*pipe
;
222 pipe
= _pipe_create();
227 ret
= mkfifo(path
, mode
);
233 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
239 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
241 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
247 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
249 ret
= _pipe_set_flags(pipe
, flags
);
257 lttng_pipe_destroy(pipe
);
262 * Close read side of a lttng pipe.
264 * Return 0 on success else a negative value.
266 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
272 /* Handle read side first. */
273 lock_read_side(pipe
);
274 ret
= _pipe_read_close(pipe
);
275 unlock_read_side(pipe
);
281 * Close write side of a lttng pipe.
283 * Return 0 on success else a negative value.
285 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
291 lock_write_side(pipe
);
292 ret
= _pipe_write_close(pipe
);
293 unlock_write_side(pipe
);
299 * Close both read and write side of a lttng pipe.
301 * Return 0 on success else a negative value.
303 int lttng_pipe_close(struct lttng_pipe
*pipe
)
305 int ret
, ret_val
= 0;
309 ret
= lttng_pipe_read_close(pipe
);
314 ret
= lttng_pipe_write_close(pipe
);
323 * Close and destroy a lttng pipe object. Finally, pipe is freed.
325 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
334 * Destroy should *never* be called with a locked mutex. These must always
335 * succeed so we unlock them after the close pipe below.
337 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
339 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
342 /* Close pipes WITHOUT trying to lock the pipes. */
343 (void) _pipe_read_close(pipe
);
344 (void) _pipe_write_close(pipe
);
346 unlock_read_side(pipe
);
347 unlock_write_side(pipe
);
349 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
350 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
356 * Read on a lttng pipe and put the data in buf of at least size count.
358 * Return "count" on success. Return < count on error. errno can be used
359 * to check the actual error.
361 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
368 lock_read_side(pipe
);
369 if (!lttng_pipe_is_read_open(pipe
)) {
374 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
376 unlock_read_side(pipe
);
381 * Write on a lttng pipe using the data in buf and size of count.
383 * Return "count" on success. Return < count on error. errno can be used
384 * 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
415 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
424 lock_read_side(pipe
);
425 if (!lttng_pipe_is_read_open(pipe
)) {
431 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
433 unlock_read_side(pipe
);
439 * Return and release the write end of the pipe.
441 * This call transfers the ownership of the write fd of the underlying pipe
442 * to the caller if it is still open.
444 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
447 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
456 lock_write_side(pipe
);
457 if (!lttng_pipe_is_write_open(pipe
)) {
463 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
465 unlock_write_side(pipe
);
This page took 0.038829 seconds and 4 git commands to generate.