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.
121 struct lttng_pipe
*lttng_pipe_open(int flags
)
124 struct lttng_pipe
*p
;
126 p
= zmalloc(sizeof(*p
));
128 PERROR("zmalloc pipe open");
134 PERROR("lttng pipe");
141 for (i
= 0; i
< 2; i
++) {
142 ret
= fcntl(p
->fd
[i
], F_SETFD
, flags
);
144 PERROR("fcntl lttng pipe %d", flags
);
150 pthread_mutex_init(&p
->read_mutex
, NULL
);
151 pthread_mutex_init(&p
->write_mutex
, NULL
);
152 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
153 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
159 lttng_pipe_destroy(p
);
164 * Close read side of a lttng pipe.
166 * Return 0 on success else a negative value.
168 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
174 /* Handle read side first. */
175 lock_read_side(pipe
);
176 ret
= _pipe_read_close(pipe
);
177 unlock_read_side(pipe
);
183 * Close write side of a lttng pipe.
185 * Return 0 on success else a negative value.
187 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
193 lock_write_side(pipe
);
194 ret
= _pipe_write_close(pipe
);
195 unlock_write_side(pipe
);
201 * Close both read and write side of a lttng pipe.
203 * Return 0 on success else a negative value.
205 int lttng_pipe_close(struct lttng_pipe
*pipe
)
207 int ret
, ret_val
= 0;
211 ret
= lttng_pipe_read_close(pipe
);
216 ret
= lttng_pipe_write_close(pipe
);
225 * Close and destroy a lttng pipe object. Finally, pipe is freed.
227 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
236 * Destroy should *never* be called with a locked mutex. These must always
237 * succeed so we unlock them after the close pipe below.
239 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
241 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
244 /* Close pipes WITHOUT trying to lock the pipes. */
245 (void) _pipe_read_close(pipe
);
246 (void) _pipe_write_close(pipe
);
248 unlock_read_side(pipe
);
249 unlock_write_side(pipe
);
251 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
252 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
258 * Read on a lttng pipe and put the data in buf of at least size count.
260 * Return 0 on success or else a negative errno message from read(2).
262 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
264 ssize_t ret
, read_len
, read_left
, index
;
269 lock_read_side(pipe
);
271 if (!lttng_pipe_is_read_open(pipe
)) {
279 read_len
= read(pipe
->fd
[0], buf
+ index
, read_left
);
282 if (errno
== EINTR
) {
285 } else if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
287 * Return the number of bytes read up to this point if any.
294 PERROR("lttng pipe read");
298 read_left
-= read_len
;
300 } while (read_left
> 0);
302 /* Everything went fine. */
306 unlock_read_side(pipe
);
311 * Write on a lttng pipe using the data in buf and size of count.
313 * Return 0 on success or else a negative errno message from write(2).
315 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
318 ssize_t ret
, write_len
, write_left
, index
;
323 lock_write_side(pipe
);
325 if (!lttng_pipe_is_write_open(pipe
)) {
333 write_len
= write(pipe
->fd
[1], buf
+ index
, write_left
);
336 if (errno
== EINTR
) {
339 } else if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
341 * Return the number of bytes read up to this point if any.
348 PERROR("lttng pipe write");
352 write_left
-= write_len
;
354 } while (write_left
> 0);
356 /* Everything went fine. */
360 unlock_write_side(pipe
);
This page took 0.047749 seconds and 5 git commands to generate.