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 "count" on success. Return < count on error. errno can be used
261 * to check the actual error.
263 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
270 lock_read_side(pipe
);
271 if (!lttng_pipe_is_read_open(pipe
)) {
276 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
278 unlock_read_side(pipe
);
283 * Write on a lttng pipe using the data in buf and size of count.
285 * Return "count" on success. Return < count on error. errno can be used
286 * to check the actual error.
288 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
296 lock_write_side(pipe
);
297 if (!lttng_pipe_is_write_open(pipe
)) {
302 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
304 unlock_write_side(pipe
);
This page took 0.03497 seconds and 4 git commands to generate.