Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / common / fd-tracker / utils.c
1 /*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/fd-tracker/utils.h>
9 #include <common/utils.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13
14 static int open_pipe_cloexec(void *data, int *fds)
15 {
16 return utils_create_pipe_cloexec(fds);
17 }
18
19 static int close_pipe(void *data, int *pipe)
20 {
21 utils_close_pipe(pipe);
22 pipe[0] = pipe[1] = -1;
23 return 0;
24 }
25
26 int fd_tracker_util_close_fd(void *unused, int *fd)
27 {
28 return close(*fd);
29 }
30
31 int fd_tracker_util_pipe_open_cloexec(
32 struct fd_tracker *tracker, const char *name, int *pipe)
33 {
34 int ret;
35 const char *name_prefix;
36 char *names[2];
37
38 name_prefix = name ? name : "Unknown pipe";
39 ret = asprintf(&names[0], "%s (read end)", name_prefix);
40 if (ret < 0) {
41 goto end;
42 }
43 ret = asprintf(&names[1], "%s (write end)", name_prefix);
44 if (ret < 0) {
45 goto end;
46 }
47
48 ret = fd_tracker_open_unsuspendable_fd(tracker, pipe,
49 (const char **) names, 2, open_pipe_cloexec, NULL);
50 free(names[0]);
51 free(names[1]);
52 end:
53 return ret;
54 }
55
56 int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe)
57 {
58 return fd_tracker_close_unsuspendable_fd(
59 tracker, pipe, 2, close_pipe, NULL);
60 }
This page took 0.030098 seconds and 4 git commands to generate.