2544f7b45f76265a8d757d540686953047092758
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.
24 #include <common/common.h>
29 * Lock read side of a pipe.
31 static void lock_read_side(struct lttng_pipe
*pipe
)
33 pthread_mutex_lock(&pipe
->read_mutex
);
37 * Unlock read side of a pipe.
39 static void unlock_read_side(struct lttng_pipe
*pipe
)
41 pthread_mutex_unlock(&pipe
->read_mutex
);
45 * Lock write side of a pipe.
47 static void lock_write_side(struct lttng_pipe
*pipe
)
49 pthread_mutex_lock(&pipe
->write_mutex
);
53 * Unlock write side of a pipe.
55 static void unlock_write_side(struct lttng_pipe
*pipe
)
57 pthread_mutex_unlock(&pipe
->write_mutex
);
61 * Internal function. Close read side of pipe WITHOUT locking the mutex.
63 * Return 0 on success else a negative errno from close(2).
65 static int _pipe_read_close(struct lttng_pipe
*pipe
)
71 if (!lttng_pipe_is_read_open(pipe
)) {
76 ret
= close(pipe
->fd
[0]);
77 } while (ret
< 0 && errno
== EINTR
);
79 PERROR("close lttng read pipe");
82 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
89 * Internal function. Close write side of pipe WITHOUT locking the mutex.
91 * Return 0 on success else a negative errno from close(2).
93 static int _pipe_write_close(struct lttng_pipe
*pipe
)
99 if (!lttng_pipe_is_write_open(pipe
)) {
104 ret
= close(pipe
->fd
[1]);
105 } while (ret
< 0 && errno
== EINTR
);
107 PERROR("close lttng write pipe");
110 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
118 * Open a new lttng pipe and set flags using fcntl().
120 * Return a newly allocated lttng pipe on success or else NULL.
123 struct lttng_pipe
*lttng_pipe_open(int flags
)
126 struct lttng_pipe
*p
;
128 p
= zmalloc(sizeof(*p
));
130 PERROR("zmalloc pipe open");
136 PERROR("lttng pipe");
143 for (i
= 0; i
< 2; i
++) {
144 ret
= fcntl(p
->fd
[i
], F_SETFD
, flags
);
146 PERROR("fcntl lttng pipe %d", flags
);
152 pthread_mutex_init(&p
->read_mutex
, NULL
);
153 pthread_mutex_init(&p
->write_mutex
, NULL
);
154 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
155 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
161 lttng_pipe_destroy(p
);
166 * Close read side of a lttng pipe.
168 * Return 0 on success else a negative value.
171 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
177 /* Handle read side first. */
178 lock_read_side(pipe
);
179 ret
= _pipe_read_close(pipe
);
180 unlock_read_side(pipe
);
186 * Close write side of a lttng pipe.
188 * Return 0 on success else a negative value.
191 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
197 lock_write_side(pipe
);
198 ret
= _pipe_write_close(pipe
);
199 unlock_write_side(pipe
);
205 * Close both read and write side of a lttng pipe.
207 * Return 0 on success else a negative value.
210 int lttng_pipe_close(struct lttng_pipe
*pipe
)
212 int ret
, ret_val
= 0;
216 ret
= lttng_pipe_read_close(pipe
);
221 ret
= lttng_pipe_write_close(pipe
);
230 * Close and destroy a lttng pipe object. Finally, pipe is freed.
233 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
242 * Destroy should *never* be called with a locked mutex. These must always
243 * succeed so we unlock them after the close pipe below.
245 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
247 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
250 /* Close pipes WITHOUT trying to lock the pipes. */
251 (void) _pipe_read_close(pipe
);
252 (void) _pipe_write_close(pipe
);
254 unlock_read_side(pipe
);
255 unlock_write_side(pipe
);
257 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
258 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
264 * Read on a lttng pipe and put the data in buf of at least size count.
266 * Return "count" on success. Return < count on error. errno can be used
267 * to check the actual error.
270 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
277 lock_read_side(pipe
);
278 if (!lttng_pipe_is_read_open(pipe
)) {
283 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
285 unlock_read_side(pipe
);
290 * Write on a lttng pipe using the data in buf and size of count.
292 * Return "count" on success. Return < count on error. errno can be used
293 * to check the actual error.
296 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
304 lock_write_side(pipe
);
305 if (!lttng_pipe_is_write_open(pipe
)) {
310 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
312 unlock_write_side(pipe
);
This page took 0.068633 seconds and 5 git commands to generate.