Clean-up: apply clang-format to the newly added fd-tracker
[lttng-tools.git] / src / common / fd-tracker / utils.c
1 /*
2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #include <common/fd-tracker/utils.h>
19 #include <common/utils.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 static int open_pipe_cloexec(void *data, int *fds)
25 {
26 return utils_create_pipe_cloexec(fds);
27 }
28
29 static int close_pipe(void *data, int *pipe)
30 {
31 utils_close_pipe(pipe);
32 pipe[0] = pipe[1] = -1;
33 return 0;
34 }
35
36 int fd_tracker_util_close_fd(void *unused, int *fd)
37 {
38 return close(*fd);
39 }
40
41 int fd_tracker_util_pipe_open_cloexec(
42 struct fd_tracker *tracker, const char *name, int *pipe)
43 {
44 int ret;
45 const char *name_prefix;
46 char *names[2];
47
48 name_prefix = name ? name : "Unknown pipe";
49 ret = asprintf(&names[0], "%s (read end)", name_prefix);
50 if (ret < 0) {
51 goto end;
52 }
53 ret = asprintf(&names[1], "%s (write end)", name_prefix);
54 if (ret < 0) {
55 goto end;
56 }
57
58 ret = fd_tracker_open_unsuspendable_fd(tracker, pipe,
59 (const char **) names, 2, open_pipe_cloexec, NULL);
60 free(names[0]);
61 free(names[1]);
62 end:
63 return ret;
64 }
65
66 int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe)
67 {
68 return fd_tracker_close_unsuspendable_fd(
69 tracker, pipe, 2, close_pipe, NULL);
70 }
This page took 0.032227 seconds and 5 git commands to generate.