Rename uprobe files to userspace-probe
[lttng-tools.git] / src / common / pipe.c
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14
15 #include <common/common.h>
16
17 #include "pipe.h"
18
19 /*
20 * Lock read side of a pipe.
21 */
22 static 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 */
30 static 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 */
38 static 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 */
46 static 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 */
56 static 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
75 end:
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 */
84 static 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
103 end:
104 return ret_val;
105 }
106
107 static 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 }
129 end:
130 return p;
131 error_destroy_rmutex:
132 (void) pthread_mutex_destroy(&p->read_mutex);
133 error_destroy:
134 free(p);
135 return NULL;
136 }
137
138 static 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++) {
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;
169 goto end;
170 }
171 }
172 end:
173 return ret;
174 }
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 */
181 LTTNG_HIDDEN
182 struct lttng_pipe *lttng_pipe_open(int flags)
183 {
184 int ret;
185 struct lttng_pipe *p;
186
187 p = _pipe_create();
188 if (!p) {
189 goto error;
190 }
191
192 ret = pipe(p->fd);
193 if (ret < 0) {
194 PERROR("lttng pipe");
195 goto error;
196 }
197 p->r_state = LTTNG_PIPE_STATE_OPENED;
198 p->w_state = LTTNG_PIPE_STATE_OPENED;
199
200 ret = _pipe_set_flags(p, flags);
201 if (ret) {
202 goto error;
203 }
204
205 p->flags = flags;
206
207 return p;
208 error:
209 lttng_pipe_destroy(p);
210 return NULL;
211 }
212
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 */
218 LTTNG_HIDDEN
219 struct 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");
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");
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;
259 error:
260 lttng_pipe_destroy(pipe);
261 return NULL;
262 }
263
264 /*
265 * Close read side of a lttng pipe.
266 *
267 * Return 0 on success else a negative value.
268 */
269 LTTNG_HIDDEN
270 int 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 */
289 LTTNG_HIDDEN
290 int 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 */
308 LTTNG_HIDDEN
309 int 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 */
331 LTTNG_HIDDEN
332 void 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 *
365 * Return "count" on success. Return < count on error. errno can be used
366 * to check the actual error.
367 */
368 LTTNG_HIDDEN
369 ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count)
370 {
371 ssize_t ret;
372
373 assert(pipe);
374 assert(buf);
375
376 lock_read_side(pipe);
377 if (!lttng_pipe_is_read_open(pipe)) {
378 ret = -1;
379 errno = EBADF;
380 goto error;
381 }
382 ret = lttng_read(pipe->fd[0], buf, count);
383 error:
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 *
391 * Return "count" on success. Return < count on error. errno can be used
392 * to check the actual error.
393 */
394 LTTNG_HIDDEN
395 ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf,
396 size_t count)
397 {
398 ssize_t ret;
399
400 assert(pipe);
401 assert(buf);
402
403 lock_write_side(pipe);
404 if (!lttng_pipe_is_write_open(pipe)) {
405 ret = -1;
406 errno = EBADF;
407 goto error;
408 }
409 ret = lttng_write(pipe->fd[1], buf, count);
410 error:
411 unlock_write_side(pipe);
412 return ret;
413 }
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 */
424 LTTNG_HIDDEN
425 int 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;
442 end_unlock:
443 unlock_read_side(pipe);
444 end:
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 */
457 LTTNG_HIDDEN
458 int 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;
475 end_unlock:
476 unlock_write_side(pipe);
477 end:
478 return ret;
479 }
This page took 0.053854 seconds and 4 git commands to generate.