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