408c5afc65756fcb92172c16db0b810dcc514cf7
[lttng-tools.git] / src / common / fd-tracker / utils.cpp
1 /*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/error.hpp>
9 #include <common/fd-tracker/utils.hpp>
10 #include <common/utils.hpp>
11
12 #include <lttng/constant.h>
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 static int open_pipe_cloexec(void *data __attribute__((unused)), int *fds)
19 {
20 return utils_create_pipe_cloexec(fds);
21 }
22
23 static int close_pipe(void *data __attribute__((unused)), int *pipe)
24 {
25 utils_close_pipe(pipe);
26 pipe[0] = pipe[1] = -1;
27 return 0;
28 }
29
30 int fd_tracker_util_close_fd(void *unused __attribute__((unused)), int *fd)
31 {
32 return close(*fd);
33 }
34
35 int fd_tracker_util_pipe_open_cloexec(struct fd_tracker *tracker, const char *name, int *pipe)
36 {
37 int ret;
38 const char *name_prefix;
39 char *names[2];
40
41 name_prefix = name ? name : "Unknown pipe";
42 ret = asprintf(&names[0], "%s (read end)", name_prefix);
43 if (ret < 0) {
44 goto end;
45 }
46 ret = asprintf(&names[1], "%s (write end)", name_prefix);
47 if (ret < 0) {
48 goto end;
49 }
50
51 ret = fd_tracker_open_unsuspendable_fd(
52 tracker, pipe, (const char **) names, 2, open_pipe_cloexec, nullptr);
53 free(names[0]);
54 free(names[1]);
55 end:
56 return ret;
57 }
58
59 int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe)
60 {
61 return fd_tracker_close_unsuspendable_fd(tracker, pipe, 2, close_pipe, nullptr);
62 }
63
64 namespace {
65 struct open_directory_handle_args {
66 const struct lttng_directory_handle *in_handle;
67 struct lttng_directory_handle *ret_handle;
68 const char *path;
69 };
70 } /* namespace */
71
72 static int open_directory_handle(void *_args, int *out_fds)
73 {
74 int ret = 0;
75 struct open_directory_handle_args *args = (open_directory_handle_args *) _args;
76 struct lttng_directory_handle *new_handle = nullptr;
77
78 new_handle = args->in_handle ?
79 lttng_directory_handle_create_from_handle(args->path, args->in_handle) :
80 lttng_directory_handle_create(args->path);
81 if (!new_handle) {
82 ret = -errno;
83 goto end;
84 }
85
86 args->ret_handle = new_handle;
87
88 /*
89 * Reserved to indicate that the handle does not use a handle; there is
90 * nothing to track. We want to indicate an error to the fd-tracker so
91 * that it doesn't attempt to track the file descriptor, but also want
92 * the caller to retrieve the newly-created handle.
93 *
94 * Calling this a hack is a fair assessment.
95 */
96 if (!lttng_directory_handle_uses_fd(new_handle)) {
97 ret = ENOTSUP;
98 } else {
99 #ifdef HAVE_DIRFD
100 *out_fds = new_handle->dirfd;
101 #else
102 abort();
103 #endif
104 }
105 end:
106 return ret;
107 }
108
109 #ifdef HAVE_DIRFD
110 static int fd_close(void *unused __attribute__((unused)), int *in_fds)
111 {
112 const int ret = close(in_fds[0]);
113
114 in_fds[0] = -1;
115 return ret;
116 }
117
118 static void directory_handle_destroy(struct lttng_directory_handle *handle, void *data)
119 {
120 struct fd_tracker *tracker = (fd_tracker *) data;
121 const int ret =
122 fd_tracker_close_unsuspendable_fd(tracker, &handle->dirfd, 1, fd_close, nullptr);
123
124 if (ret) {
125 ERR("Failed to untrack directory handle file descriptor");
126 }
127 }
128 #endif
129
130 struct lttng_directory_handle *fd_tracker_create_directory_handle(struct fd_tracker *tracker,
131 const char *path)
132 {
133 return fd_tracker_create_directory_handle_from_handle(tracker, nullptr, path);
134 }
135
136 struct lttng_directory_handle *fd_tracker_create_directory_handle_from_handle(
137 struct fd_tracker *tracker, struct lttng_directory_handle *in_handle, const char *path)
138 {
139 int ret;
140 int dirfd = -1;
141 char *handle_name = nullptr;
142 char cwd_path[LTTNG_PATH_MAX] = "working directory";
143 struct lttng_directory_handle *new_handle = nullptr;
144 open_directory_handle_args open_args{};
145
146 open_args.in_handle = in_handle;
147 open_args.path = path;
148
149 if (!path) {
150 if (!getcwd(cwd_path, sizeof(cwd_path))) {
151 PERROR("Failed to get current working directory to name directory handle");
152 goto end;
153 }
154 }
155
156 ret = asprintf(&handle_name, "Directory handle to %s", path ? path : cwd_path);
157 if (ret < 0) {
158 PERROR("Failed to format directory handle name");
159 goto end;
160 }
161
162 ret = fd_tracker_open_unsuspendable_fd(
163 tracker, &dirfd, (const char **) &handle_name, 1, open_directory_handle, &open_args);
164 if (ret && ret != ENOTSUP) {
165 ERR("Failed to open directory handle to %s through the fd tracker",
166 path ? path : cwd_path);
167 }
168 new_handle = open_args.ret_handle;
169
170 #ifdef HAVE_DIRFD
171 new_handle->destroy_cb = directory_handle_destroy;
172 new_handle->destroy_cb_data = tracker;
173 #endif
174 end:
175 free(handle_name);
176 return new_handle;
177 }
This page took 0.031811 seconds and 4 git commands to generate.