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.
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.
169 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
175 /* Handle read side first. */
176 lock_read_side(pipe
);
177 ret
= _pipe_read_close(pipe
);
178 unlock_read_side(pipe
);
184 * Close write side of a lttng pipe.
186 * Return 0 on success else a negative value.
188 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
194 lock_write_side(pipe
);
195 ret
= _pipe_write_close(pipe
);
196 unlock_write_side(pipe
);
202 * Close both read and write side of a lttng pipe.
204 * Return 0 on success else a negative value.
206 int lttng_pipe_close(struct lttng_pipe
*pipe
)
208 int ret
, ret_val
= 0;
212 ret
= lttng_pipe_read_close(pipe
);
217 ret
= lttng_pipe_write_close(pipe
);
226 * Close and destroy a lttng pipe object. Finally, pipe is freed.
228 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
237 * Destroy should *never* be called with a locked mutex. These must always
238 * succeed so we unlock them after the close pipe below.
240 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
242 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
245 /* Close pipes WITHOUT trying to lock the pipes. */
246 (void) _pipe_read_close(pipe
);
247 (void) _pipe_write_close(pipe
);
249 unlock_read_side(pipe
);
250 unlock_write_side(pipe
);
252 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
253 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
259 * Read on a lttng pipe and put the data in buf of at least size count.
261 * Return "count" on success. Return < count on error. errno can be used
262 * to check the actual error.
264 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
271 lock_read_side(pipe
);
272 if (!lttng_pipe_is_read_open(pipe
)) {
277 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
279 unlock_read_side(pipe
);
284 * Write on a lttng pipe using the data in buf and size of count.
286 * Return "count" on success. Return < count on error. errno can be used
287 * to check the actual error.
289 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
297 lock_write_side(pipe
);
298 if (!lttng_pipe_is_write_open(pipe
)) {
303 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
305 unlock_write_side(pipe
);
This page took 0.036627 seconds and 5 git commands to generate.