Commit | Line | Data |
---|---|---|
9fd92637 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
9fd92637 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
9fd92637 | 5 | * |
9fd92637 DG |
6 | */ |
7 | ||
8 | #ifndef LTTNG_PIPE_H | |
9 | #define LTTNG_PIPE_H | |
10 | ||
c9e313bc | 11 | #include <common/macros.hpp> |
0038180d JG |
12 | #include <common/make-unique-wrapper.hpp> |
13 | ||
14 | #include <pthread.h> | |
40dde31f | 15 | #include <sys/types.h> |
9fd92637 DG |
16 | |
17 | enum lttng_pipe_state { | |
18 | LTTNG_PIPE_STATE_OPENED = 1, | |
19 | LTTNG_PIPE_STATE_CLOSED = 2, | |
20 | }; | |
21 | ||
0038180d JG |
22 | /* Close both side of pipe. */ |
23 | int lttng_pipe_close(struct lttng_pipe *pipe); | |
24 | ||
9fd92637 | 25 | struct lttng_pipe { |
0038180d JG |
26 | static void _lttng_pipe_close_wrapper(lttng_pipe *pipe) |
27 | { | |
28 | lttng_pipe_close(pipe); | |
29 | } | |
30 | ||
31 | using uptr = std::unique_ptr< | |
32 | lttng_pipe, | |
f053d40c | 33 | lttng::memory::create_deleter_class<lttng_pipe, _lttng_pipe_close_wrapper>::deleter>; |
0038180d | 34 | |
9fd92637 DG |
35 | /* Read: 0, Write: 1. */ |
36 | int fd[2]; | |
37 | /* | |
38 | * Flags of the pipe once opened. pipe(2) specifies either O_NONBLOCK or | |
39 | * O_CLOEXEC can be used. Flags are set using fcntl(2) call. | |
40 | */ | |
41 | int flags; | |
42 | ||
43 | /* | |
44 | * These states are protected by the operation mutex below. | |
45 | */ | |
46 | enum lttng_pipe_state r_state; | |
47 | enum lttng_pipe_state w_state; | |
48 | ||
49 | /* Held for each read(2) operation. */ | |
50 | pthread_mutex_t read_mutex; | |
51 | /* Held for each write(2) operation. */ | |
52 | pthread_mutex_t write_mutex; | |
53 | }; | |
54 | ||
55 | /* | |
56 | * Return 1 if read side is open else 0. | |
57 | */ | |
40b66b26 | 58 | static inline int lttng_pipe_is_read_open(const struct lttng_pipe *pipe) |
9fd92637 DG |
59 | { |
60 | return pipe->r_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Return 1 if write side is open else 0. | |
65 | */ | |
40b66b26 | 66 | static inline int lttng_pipe_is_write_open(const struct lttng_pipe *pipe) |
9fd92637 DG |
67 | { |
68 | return pipe->w_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; | |
69 | } | |
70 | ||
40b66b26 | 71 | static inline int lttng_pipe_get_readfd(const struct lttng_pipe *pipe) |
9fd92637 DG |
72 | { |
73 | return pipe->fd[0]; | |
74 | } | |
75 | ||
40b66b26 | 76 | static inline int lttng_pipe_get_writefd(const struct lttng_pipe *pipe) |
9fd92637 DG |
77 | { |
78 | return pipe->fd[1]; | |
79 | } | |
80 | ||
81 | struct lttng_pipe *lttng_pipe_open(int flags); | |
28f23191 | 82 | struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode, int flags); |
9fd92637 DG |
83 | int lttng_pipe_write_close(struct lttng_pipe *pipe); |
84 | int lttng_pipe_read_close(struct lttng_pipe *pipe); | |
9fd92637 DG |
85 | void lttng_pipe_destroy(struct lttng_pipe *pipe); |
86 | ||
87 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count); | |
28f23191 | 88 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, size_t count); |
55dfb029 | 89 | /* Returns and releases the read end of the pipe. */ |
55dfb029 JG |
90 | int lttng_pipe_release_readfd(struct lttng_pipe *pipe); |
91 | /* Returns and releases the write end of the pipe. */ | |
55dfb029 | 92 | int lttng_pipe_release_writefd(struct lttng_pipe *pipe); |
9fd92637 DG |
93 | |
94 | #endif /* LTTNG_PIPE_H */ |