2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.hpp>
15 #include <sys/types.h>
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()
109 struct lttng_pipe
*p
;
111 p
= zmalloc
<lttng_pipe
>();
113 PERROR("zmalloc pipe create");
116 p
->fd
[0] = p
->fd
[1] = -1;
118 ret
= pthread_mutex_init(&p
->read_mutex
, nullptr);
120 PERROR("pthread_mutex_init read lock pipe create");
123 ret
= pthread_mutex_init(&p
->write_mutex
, nullptr);
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
, int flags
)
219 struct lttng_pipe
*pipe
;
221 pipe
= _pipe_create();
226 ret
= mkfifo(path
, mode
);
232 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
238 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
240 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
246 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
248 ret
= _pipe_set_flags(pipe
, flags
);
256 lttng_pipe_destroy(pipe
);
261 * Close read side of a lttng pipe.
263 * Return 0 on success else a negative value.
265 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
271 /* Handle read side first. */
272 lock_read_side(pipe
);
273 ret
= _pipe_read_close(pipe
);
274 unlock_read_side(pipe
);
280 * Close write side of a lttng pipe.
282 * Return 0 on success else a negative value.
284 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
290 lock_write_side(pipe
);
291 ret
= _pipe_write_close(pipe
);
292 unlock_write_side(pipe
);
298 * Close both read and write side of a lttng pipe.
300 * Return 0 on success else a negative value.
302 int lttng_pipe_close(struct lttng_pipe
*pipe
)
304 int ret
, ret_val
= 0;
308 ret
= lttng_pipe_read_close(pipe
);
313 ret
= lttng_pipe_write_close(pipe
);
322 * Close and destroy a lttng pipe object. Finally, pipe is freed.
324 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
333 * Destroy should *never* be called with a locked mutex. These must always
334 * succeed so we unlock them after the close pipe below.
336 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
338 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
341 /* Close pipes WITHOUT trying to lock the pipes. */
342 (void) _pipe_read_close(pipe
);
343 (void) _pipe_write_close(pipe
);
345 unlock_read_side(pipe
);
346 unlock_write_side(pipe
);
348 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
349 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
355 * Read on a lttng pipe and put the data in buf of at least size count.
357 * Return "count" on success. Return < count on error. errno can be used
358 * 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.
385 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
, size_t count
)
392 lock_write_side(pipe
);
393 if (!lttng_pipe_is_write_open(pipe
)) {
398 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
400 unlock_write_side(pipe
);
405 * Return and release the read end of the pipe.
407 * This call transfers the ownership of the read fd of the underlying pipe
408 * to the caller if it is still open.
410 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
413 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
422 lock_read_side(pipe
);
423 if (!lttng_pipe_is_read_open(pipe
)) {
429 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
431 unlock_read_side(pipe
);
437 * Return and release the write end of the pipe.
439 * This call transfers the ownership of the write fd of the underlying pipe
440 * to the caller if it is still open.
442 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
445 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
454 lock_write_side(pipe
);
455 if (!lttng_pipe_is_write_open(pipe
)) {
461 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
463 unlock_write_side(pipe
);
This page took 0.038057 seconds and 4 git commands to generate.