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.
22 #include <sys/types.h>
25 #include <common/common.h>
30 * Lock read side of a pipe.
32 static void lock_read_side(struct lttng_pipe
*pipe
)
34 pthread_mutex_lock(&pipe
->read_mutex
);
38 * Unlock read side of a pipe.
40 static void unlock_read_side(struct lttng_pipe
*pipe
)
42 pthread_mutex_unlock(&pipe
->read_mutex
);
46 * Lock write side of a pipe.
48 static void lock_write_side(struct lttng_pipe
*pipe
)
50 pthread_mutex_lock(&pipe
->write_mutex
);
54 * Unlock write side of a pipe.
56 static void unlock_write_side(struct lttng_pipe
*pipe
)
58 pthread_mutex_unlock(&pipe
->write_mutex
);
62 * Internal function. Close read side of pipe WITHOUT locking the mutex.
64 * Return 0 on success else a negative errno from close(2).
66 static int _pipe_read_close(struct lttng_pipe
*pipe
)
72 if (!lttng_pipe_is_read_open(pipe
)) {
77 ret
= close(pipe
->fd
[0]);
78 } while (ret
< 0 && errno
== EINTR
);
80 PERROR("close lttng read pipe");
83 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
90 * Internal function. Close write side of pipe WITHOUT locking the mutex.
92 * Return 0 on success else a negative errno from close(2).
94 static int _pipe_write_close(struct lttng_pipe
*pipe
)
100 if (!lttng_pipe_is_write_open(pipe
)) {
105 ret
= close(pipe
->fd
[1]);
106 } while (ret
< 0 && errno
== EINTR
);
108 PERROR("close lttng write pipe");
111 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
117 static struct lttng_pipe
*_pipe_create(void)
120 struct lttng_pipe
*p
;
122 p
= zmalloc(sizeof(*p
));
124 PERROR("zmalloc pipe create");
127 p
->fd
[0] = p
->fd
[1] = -1;
129 ret
= pthread_mutex_init(&p
->read_mutex
, NULL
);
131 PERROR("pthread_mutex_init read lock pipe create");
134 ret
= pthread_mutex_init(&p
->write_mutex
, NULL
);
136 PERROR("pthread_mutex_init write lock pipe create");
137 goto error_destroy_rmutex
;
141 error_destroy_rmutex
:
142 (void) pthread_mutex_destroy(&p
->read_mutex
);
148 static int _pipe_set_flags(struct lttng_pipe
*pipe
, int flags
)
156 for (i
= 0; i
< 2; i
++) {
157 ret
= fcntl(pipe
->fd
[i
], F_SETFD
, flags
);
159 PERROR("fcntl lttng pipe %d", flags
);
168 * Open a new lttng pipe and set flags using fcntl().
170 * Return a newly allocated lttng pipe on success or else NULL.
173 struct lttng_pipe
*lttng_pipe_open(int flags
)
176 struct lttng_pipe
*p
;
185 PERROR("lttng pipe");
188 p
->r_state
= LTTNG_PIPE_STATE_OPENED
;
189 p
->w_state
= LTTNG_PIPE_STATE_OPENED
;
191 ret
= _pipe_set_flags(p
, flags
);
200 lttng_pipe_destroy(p
);
205 * Open a new lttng pipe at path and set flags using fcntl().
207 * Return a newly allocated lttng pipe on success or else NULL.
210 struct lttng_pipe
*lttng_pipe_named_open(const char *path
, mode_t mode
,
214 struct lttng_pipe
*pipe
;
216 pipe
= _pipe_create();
221 ret
= mkfifo(path
, mode
);
227 fd_r
= open(path
, O_RDONLY
| O_NONBLOCK
);
234 pipe
->r_state
= LTTNG_PIPE_STATE_OPENED
;
236 fd_w
= open(path
, O_WRONLY
| O_NONBLOCK
);
243 pipe
->w_state
= LTTNG_PIPE_STATE_OPENED
;
245 ret
= _pipe_set_flags(pipe
, flags
);
253 lttng_pipe_destroy(pipe
);
258 * Close read side of a lttng pipe.
260 * Return 0 on success else a negative value.
263 int lttng_pipe_read_close(struct lttng_pipe
*pipe
)
269 /* Handle read side first. */
270 lock_read_side(pipe
);
271 ret
= _pipe_read_close(pipe
);
272 unlock_read_side(pipe
);
278 * Close write side of a lttng pipe.
280 * Return 0 on success else a negative value.
283 int lttng_pipe_write_close(struct lttng_pipe
*pipe
)
289 lock_write_side(pipe
);
290 ret
= _pipe_write_close(pipe
);
291 unlock_write_side(pipe
);
297 * Close both read and write side of a lttng pipe.
299 * Return 0 on success else a negative value.
302 int lttng_pipe_close(struct lttng_pipe
*pipe
)
304 int ret
, ret_val
= 0;
308 ret
= lttng_pipe_read_close(pipe
);
313 ret
= lttng_pipe_write_close(pipe
);
322 * Close and destroy a lttng pipe object. Finally, pipe is freed.
325 void lttng_pipe_destroy(struct lttng_pipe
*pipe
)
334 * Destroy should *never* be called with a locked mutex. These must always
335 * succeed so we unlock them after the close pipe below.
337 ret
= pthread_mutex_trylock(&pipe
->read_mutex
);
339 ret
= pthread_mutex_trylock(&pipe
->write_mutex
);
342 /* Close pipes WITHOUT trying to lock the pipes. */
343 (void) _pipe_read_close(pipe
);
344 (void) _pipe_write_close(pipe
);
346 unlock_read_side(pipe
);
347 unlock_write_side(pipe
);
349 (void) pthread_mutex_destroy(&pipe
->read_mutex
);
350 (void) pthread_mutex_destroy(&pipe
->write_mutex
);
356 * Read on a lttng pipe and put the data in buf of at least size count.
358 * Return "count" on success. Return < count on error. errno can be used
359 * to check the actual error.
362 ssize_t
lttng_pipe_read(struct lttng_pipe
*pipe
, void *buf
, size_t count
)
369 lock_read_side(pipe
);
370 if (!lttng_pipe_is_read_open(pipe
)) {
375 ret
= lttng_read(pipe
->fd
[0], buf
, count
);
377 unlock_read_side(pipe
);
382 * Write on a lttng pipe using the data in buf and size of count.
384 * Return "count" on success. Return < count on error. errno can be used
385 * to check the actual error.
388 ssize_t
lttng_pipe_write(struct lttng_pipe
*pipe
, const void *buf
,
396 lock_write_side(pipe
);
397 if (!lttng_pipe_is_write_open(pipe
)) {
402 ret
= lttng_write(pipe
->fd
[1], buf
, count
);
404 unlock_write_side(pipe
);
409 * Return and release the read end of the pipe.
411 * This call transfers the ownership of the read fd of the underlying pipe
412 * to the caller if it is still open.
414 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
418 int lttng_pipe_release_readfd(struct lttng_pipe
*pipe
)
427 lock_read_side(pipe
);
428 if (!lttng_pipe_is_read_open(pipe
)) {
434 pipe
->r_state
= LTTNG_PIPE_STATE_CLOSED
;
436 unlock_read_side(pipe
);
442 * Return and release the write end of the pipe.
444 * This call transfers the ownership of the write fd of the underlying pipe
445 * to the caller if it is still open.
447 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
451 int lttng_pipe_release_writefd(struct lttng_pipe
*pipe
)
460 lock_write_side(pipe
);
461 if (!lttng_pipe_is_write_open(pipe
)) {
467 pipe
->w_state
= LTTNG_PIPE_STATE_CLOSED
;
469 unlock_write_side(pipe
);
This page took 0.039059 seconds and 4 git commands to generate.