buffer-view: introduce lttng_buffer_view_contains_string
[lttng-tools.git] / src / common / pipe.c
CommitLineData
9fd92637 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
9fd92637 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
9fd92637 5 *
9fd92637
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
9fd92637
DG
9#include <assert.h>
10#include <fcntl.h>
11#include <unistd.h>
9c2bd8db
JG
12#include <sys/types.h>
13#include <sys/stat.h>
9fd92637
DG
14
15#include <common/common.h>
16
17#include "pipe.h"
18
19/*
20 * Lock read side of a pipe.
21 */
22static void lock_read_side(struct lttng_pipe *pipe)
23{
24 pthread_mutex_lock(&pipe->read_mutex);
25}
26
27/*
28 * Unlock read side of a pipe.
29 */
30static void unlock_read_side(struct lttng_pipe *pipe)
31{
32 pthread_mutex_unlock(&pipe->read_mutex);
33}
34
35/*
36 * Lock write side of a pipe.
37 */
38static void lock_write_side(struct lttng_pipe *pipe)
39{
40 pthread_mutex_lock(&pipe->write_mutex);
41}
42
43/*
44 * Unlock write side of a pipe.
45 */
46static void unlock_write_side(struct lttng_pipe *pipe)
47{
48 pthread_mutex_unlock(&pipe->write_mutex);
49}
50
51/*
52 * Internal function. Close read side of pipe WITHOUT locking the mutex.
53 *
54 * Return 0 on success else a negative errno from close(2).
55 */
56static int _pipe_read_close(struct lttng_pipe *pipe)
57{
58 int ret, ret_val = 0;
59
60 assert(pipe);
61
62 if (!lttng_pipe_is_read_open(pipe)) {
63 goto end;
64 }
65
66 do {
67 ret = close(pipe->fd[0]);
68 } while (ret < 0 && errno == EINTR);
69 if (ret < 0) {
70 PERROR("close lttng read pipe");
71 ret_val = -errno;
72 }
73 pipe->r_state = LTTNG_PIPE_STATE_CLOSED;
74
75end:
76 return ret_val;
77}
78
79/*
80 * Internal function. Close write side of pipe WITHOUT locking the mutex.
81 *
82 * Return 0 on success else a negative errno from close(2).
83 */
84static int _pipe_write_close(struct lttng_pipe *pipe)
85{
86 int ret, ret_val = 0;
87
88 assert(pipe);
89
90 if (!lttng_pipe_is_write_open(pipe)) {
91 goto end;
92 }
93
94 do {
95 ret = close(pipe->fd[1]);
96 } while (ret < 0 && errno == EINTR);
97 if (ret < 0) {
98 PERROR("close lttng write pipe");
99 ret_val = -errno;
100 }
101 pipe->w_state = LTTNG_PIPE_STATE_CLOSED;
102
103end:
104 return ret_val;
105}
106
9c2bd8db
JG
107static struct lttng_pipe *_pipe_create(void)
108{
109 int ret;
110 struct lttng_pipe *p;
111
112 p = zmalloc(sizeof(*p));
113 if (!p) {
114 PERROR("zmalloc pipe create");
115 goto end;
116 }
117 p->fd[0] = p->fd[1] = -1;
118
119 ret = pthread_mutex_init(&p->read_mutex, NULL);
120 if (ret) {
121 PERROR("pthread_mutex_init read lock pipe create");
122 goto error_destroy;
123 }
124 ret = pthread_mutex_init(&p->write_mutex, NULL);
125 if (ret) {
126 PERROR("pthread_mutex_init write lock pipe create");
127 goto error_destroy_rmutex;
128 }
129end:
130 return p;
131error_destroy_rmutex:
132 (void) pthread_mutex_destroy(&p->read_mutex);
133error_destroy:
134 free(p);
135 return NULL;
136}
137
138static int _pipe_set_flags(struct lttng_pipe *pipe, int flags)
139{
140 int i, ret = 0;
141
142 if (!flags) {
143 goto end;
144 }
145
146 for (i = 0; i < 2; i++) {
699f8738
JD
147 if (flags & O_NONBLOCK) {
148 ret = fcntl(pipe->fd[i], F_SETFL, O_NONBLOCK);
149 if (ret < 0) {
150 PERROR("fcntl lttng pipe %d", flags);
151 goto end;
152 }
153 }
154 if (flags & FD_CLOEXEC) {
155 ret = fcntl(pipe->fd[i], F_SETFD, FD_CLOEXEC);
156 if (ret < 0) {
157 PERROR("fcntl lttng pipe %d", flags);
158 goto end;
159 }
160 }
161 /*
162 * We only check for O_NONBLOCK or FD_CLOEXEC, if another flag is
163 * needed, we can add it, but for now just make sure we don't make
164 * mistakes with the parameters we pass.
165 */
166 if (!(flags & O_NONBLOCK) && !(flags & FD_CLOEXEC)) {
167 fprintf(stderr, "Unsupported flag\n");
168 ret = -1;
9c2bd8db
JG
169 goto end;
170 }
171 }
172end:
173 return ret;
174}
9fd92637
DG
175
176/*
177 * Open a new lttng pipe and set flags using fcntl().
178 *
179 * Return a newly allocated lttng pipe on success or else NULL.
180 */
a247c6e0 181LTTNG_HIDDEN
9fd92637
DG
182struct lttng_pipe *lttng_pipe_open(int flags)
183{
184 int ret;
185 struct lttng_pipe *p;
186
9c2bd8db 187 p = _pipe_create();
9fd92637 188 if (!p) {
9fd92637
DG
189 goto error;
190 }
191
192 ret = pipe(p->fd);
193 if (ret < 0) {
194 PERROR("lttng pipe");
195 goto error;
196 }
9c2bd8db
JG
197 p->r_state = LTTNG_PIPE_STATE_OPENED;
198 p->w_state = LTTNG_PIPE_STATE_OPENED;
9fd92637 199
9c2bd8db
JG
200 ret = _pipe_set_flags(p, flags);
201 if (ret) {
202 goto error;
9fd92637
DG
203 }
204
9fd92637
DG
205 p->flags = flags;
206
207 return p;
9fd92637
DG
208error:
209 lttng_pipe_destroy(p);
210 return NULL;
211}
212
9c2bd8db
JG
213/*
214 * Open a new lttng pipe at path and set flags using fcntl().
215 *
216 * Return a newly allocated lttng pipe on success or else NULL.
217 */
218LTTNG_HIDDEN
219struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode,
220 int flags)
221{
222 int ret, fd_r, fd_w;
223 struct lttng_pipe *pipe;
224
225 pipe = _pipe_create();
226 if (!pipe) {
227 goto error;
228 }
229
230 ret = mkfifo(path, mode);
231 if (ret) {
232 PERROR("mkfifo");
233 goto error;
234 }
235
236 fd_r = open(path, O_RDONLY | O_NONBLOCK);
237 if (fd_r < 0) {
238 PERROR("open fifo");
9c2bd8db
JG
239 goto error;
240 }
241 pipe->fd[0] = fd_r;
242 pipe->r_state = LTTNG_PIPE_STATE_OPENED;
243
244 fd_w = open(path, O_WRONLY | O_NONBLOCK);
245 if (fd_w < 0) {
246 PERROR("open fifo");
9c2bd8db
JG
247 goto error;
248 }
249 pipe->fd[1] = fd_w;
250 pipe->w_state = LTTNG_PIPE_STATE_OPENED;
251
252 ret = _pipe_set_flags(pipe, flags);
253 if (ret) {
254 goto error;
255 }
256 pipe->flags = flags;
257
258 return pipe;
259error:
260 lttng_pipe_destroy(pipe);
261 return NULL;
262}
263
9fd92637
DG
264/*
265 * Close read side of a lttng pipe.
266 *
267 * Return 0 on success else a negative value.
268 */
a247c6e0 269LTTNG_HIDDEN
9fd92637
DG
270int lttng_pipe_read_close(struct lttng_pipe *pipe)
271{
272 int ret;
273
274 assert(pipe);
275
276 /* Handle read side first. */
277 lock_read_side(pipe);
278 ret = _pipe_read_close(pipe);
279 unlock_read_side(pipe);
280
281 return ret;
282}
283
284/*
285 * Close write side of a lttng pipe.
286 *
287 * Return 0 on success else a negative value.
288 */
a247c6e0 289LTTNG_HIDDEN
9fd92637
DG
290int lttng_pipe_write_close(struct lttng_pipe *pipe)
291{
292 int ret;
293
294 assert(pipe);
295
296 lock_write_side(pipe);
297 ret = _pipe_write_close(pipe);
298 unlock_write_side(pipe);
299
300 return ret;
301}
302
303/*
304 * Close both read and write side of a lttng pipe.
305 *
306 * Return 0 on success else a negative value.
307 */
a247c6e0 308LTTNG_HIDDEN
9fd92637
DG
309int lttng_pipe_close(struct lttng_pipe *pipe)
310{
311 int ret, ret_val = 0;
312
313 assert(pipe);
314
315 ret = lttng_pipe_read_close(pipe);
316 if (ret < 0) {
317 ret_val = ret;
318 }
319
320 ret = lttng_pipe_write_close(pipe);
321 if (ret < 0) {
322 ret_val = ret;
323 }
324
325 return ret_val;
326}
327
328/*
329 * Close and destroy a lttng pipe object. Finally, pipe is freed.
330 */
a247c6e0 331LTTNG_HIDDEN
9fd92637
DG
332void lttng_pipe_destroy(struct lttng_pipe *pipe)
333{
334 int ret;
335
336 if (!pipe) {
337 return;
338 }
339
340 /*
341 * Destroy should *never* be called with a locked mutex. These must always
342 * succeed so we unlock them after the close pipe below.
343 */
344 ret = pthread_mutex_trylock(&pipe->read_mutex);
345 assert(!ret);
346 ret = pthread_mutex_trylock(&pipe->write_mutex);
347 assert(!ret);
348
349 /* Close pipes WITHOUT trying to lock the pipes. */
350 (void) _pipe_read_close(pipe);
351 (void) _pipe_write_close(pipe);
352
353 unlock_read_side(pipe);
354 unlock_write_side(pipe);
355
356 (void) pthread_mutex_destroy(&pipe->read_mutex);
357 (void) pthread_mutex_destroy(&pipe->write_mutex);
358
359 free(pipe);
360}
361
362/*
363 * Read on a lttng pipe and put the data in buf of at least size count.
364 *
6cd525e8
MD
365 * Return "count" on success. Return < count on error. errno can be used
366 * to check the actual error.
9fd92637 367 */
a247c6e0 368LTTNG_HIDDEN
9fd92637
DG
369ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count)
370{
6cd525e8 371 ssize_t ret;
9fd92637
DG
372
373 assert(pipe);
374 assert(buf);
375
376 lock_read_side(pipe);
9fd92637 377 if (!lttng_pipe_is_read_open(pipe)) {
6cd525e8
MD
378 ret = -1;
379 errno = EBADF;
9fd92637
DG
380 goto error;
381 }
6cd525e8 382 ret = lttng_read(pipe->fd[0], buf, count);
9fd92637
DG
383error:
384 unlock_read_side(pipe);
385 return ret;
386}
387
388/*
389 * Write on a lttng pipe using the data in buf and size of count.
390 *
6cd525e8
MD
391 * Return "count" on success. Return < count on error. errno can be used
392 * to check the actual error.
9fd92637 393 */
a247c6e0 394LTTNG_HIDDEN
9fd92637
DG
395ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf,
396 size_t count)
397{
6cd525e8 398 ssize_t ret;
9fd92637
DG
399
400 assert(pipe);
401 assert(buf);
402
403 lock_write_side(pipe);
9fd92637 404 if (!lttng_pipe_is_write_open(pipe)) {
6cd525e8
MD
405 ret = -1;
406 errno = EBADF;
9fd92637
DG
407 goto error;
408 }
6cd525e8 409 ret = lttng_write(pipe->fd[1], buf, count);
9fd92637
DG
410error:
411 unlock_write_side(pipe);
412 return ret;
413}
55dfb029
JG
414
415/*
416 * Return and release the read end of the pipe.
417 *
418 * This call transfers the ownership of the read fd of the underlying pipe
419 * to the caller if it is still open.
420 *
421 * Returns the fd of the read end of the pipe, or -1 if it was already closed or
422 * released.
423 */
424LTTNG_HIDDEN
425int lttng_pipe_release_readfd(struct lttng_pipe *pipe)
426{
427 int ret;
428
429 if (!pipe) {
430 ret = -1;
431 goto end;
432 }
433
434 lock_read_side(pipe);
435 if (!lttng_pipe_is_read_open(pipe)) {
436 ret = -1;
437 goto end_unlock;
438 }
439 ret = pipe->fd[0];
440 pipe->fd[0] = -1;
441 pipe->r_state = LTTNG_PIPE_STATE_CLOSED;
442end_unlock:
443 unlock_read_side(pipe);
444end:
445 return ret;
446}
447
448/*
449 * Return and release the write end of the pipe.
450 *
451 * This call transfers the ownership of the write fd of the underlying pipe
452 * to the caller if it is still open.
453 *
454 * Returns the fd of the write end of the pipe, or -1 if it was alwritey closed
455 * or released.
456 */
457LTTNG_HIDDEN
458int lttng_pipe_release_writefd(struct lttng_pipe *pipe)
459{
460 int ret;
461
462 if (!pipe) {
463 ret = -1;
464 goto end;
465 }
466
467 lock_write_side(pipe);
468 if (!lttng_pipe_is_write_open(pipe)) {
469 ret = -1;
470 goto end_unlock;
471 }
472 ret = pipe->fd[1];
473 pipe->fd[1] = -1;
474 pipe->w_state = LTTNG_PIPE_STATE_CLOSED;
475end_unlock:
476 unlock_write_side(pipe);
477end:
478 return ret;
479}
This page took 0.063065 seconds and 4 git commands to generate.