2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <common/common.h>
28 * Lock read side of a pipe.
30 static void lock_read_side(struct lttng_pipe
*pipe
)
32 pthread_mutex_lock(&pipe
->read_mutex
);
36 * Unlock read side of a pipe.
38 static void unlock_read_side(struct lttng_pipe
*pipe
)
40 pthread_mutex_unlock(&pipe
->read_mutex
);
44 * Lock write side of a pipe.
46 static void lock_write_side(struct lttng_pipe
*pipe
)
48 pthread_mutex_lock(&pipe
->write_mutex
);
52 * Unlock write side of a pipe.
54 static void unlock_write_side(struct lttng_pipe
*pipe
)
56 pthread_mutex_unlock(&pipe
->write_mutex
);
60 * Internal function. Close read side of pipe WITHOUT locking the mutex.
62 * Return 0 on success else a negative errno from close(2).
64 static int _pipe_read_close(struct lttng_pipe
*pipe
)
70 if (!lttng_pipe_is_read_open(pipe
)) {
75 ret
= close(pipe
->fd
[0]);
76 } while (ret
< 0 && errno
== EINTR
);
78 PERROR("close lttng read pipe");
81 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
88 * Internal function. Close write side of pipe WITHOUT locking the mutex.
90 * Return 0 on success else a negative errno from close(2).
92 static int _pipe_write_close(struct lttng_pipe
*pipe
)
98 if (!lttng_pipe_is_write_open(pipe
)) {
103 ret
= close(pipe
->fd
[1]);
104 } while (ret
< 0 && errno
== EINTR
);
106 PERROR("close lttng write pipe");
109 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
117 * Open a new lttng pipe and set flags using fcntl().
119 * Return a newly allocated lttng pipe on success or else NULL.
122 struct lttng_pipe
*lttng_pipe_open(int flags
)
125 struct lttng_pipe
*p
;
127 p
= zmalloc(sizeof(*p
));
129 PERROR("zmalloc pipe open");
135 PERROR("lttng pipe");
142 for (i
= 0; i
< 2; i
++) {
143 ret
= fcntl(p
->fd
[i
], F_SETFD
, flags
);
145 PERROR("fcntl lttng pipe %d", flags
);
151 pthread_mutex_init(&p
->read_mutex
, NULL
);
152 pthread_mutex_init(&p
->write_mutex
, NULL
);
153 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
154 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
160 lttng_pipe_destroy(p
);
165 * Close read side of a lttng pipe.
167 * Return 0 on success else a negative value.
170 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
176 /* Handle read side first. */
177 lock_read_side(pipe
);
178 ret
= _pipe_read_close(pipe
);
179 unlock_read_side(pipe
);
185 * Close write side of a lttng pipe.
187 * Return 0 on success else a negative value.
190 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
196 lock_write_side(pipe
);
197 ret
= _pipe_write_close(pipe
);
198 unlock_write_side(pipe
);
204 * Close both read and write side of a lttng pipe.
206 * Return 0 on success else a negative value.
209 int lttng_pipe_close(struct lttng_pipe
*pipe
)
211 int ret
, ret_val
= 0;
215 ret
= lttng_pipe_read_close(pipe
);
220 ret
= lttng_pipe_write_close(pipe
);
229 * Close and destroy a lttng pipe object. Finally, pipe is freed.
232 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
241 * Destroy should *never* be called with a locked mutex. These must always
242 * succeed so we unlock them after the close pipe below.
244 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
246 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
249 /* Close pipes WITHOUT trying to lock the pipes. */
250 (void) _pipe_read_close(pipe
);
251 (void) _pipe_write_close(pipe
);
253 unlock_read_side(pipe
);
254 unlock_write_side(pipe
);
256 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
257 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
263 * Read on a lttng pipe and put the data in buf of at least size count.
265 * Return "count" on success. Return < count on error. errno can be used
266 * to check the actual error.
269 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
276 lock_read_side(pipe
);
277 if (!lttng_pipe_is_read_open(pipe
)) {
282 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
284 unlock_read_side(pipe
);
289 * Write on a lttng pipe using the data in buf and size of count.
291 * Return "count" on success. Return < count on error. errno can be used
292 * to check the actual error.
295 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
303 lock_write_side(pipe
);
304 if (!lttng_pipe_is_write_open(pipe
)) {
309 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
311 unlock_write_side(pipe
);
This page took 0.043782 seconds and 5 git commands to generate.