2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.h>
9 #include <common/fd-tracker/utils.h>
10 #include <common/utils.h>
11 #include <lttng/constant.h>
17 int open_pipe_cloexec(void *data
, int *fds
)
19 return utils_create_pipe_cloexec(fds
);
23 int close_pipe(void *data
, int *pipe
)
25 utils_close_pipe(pipe
);
26 pipe
[0] = pipe
[1] = -1;
31 int fd_tracker_util_close_fd(void *unused
, int *fd
)
37 int fd_tracker_util_pipe_open_cloexec(
38 struct fd_tracker
*tracker
, const char *name
, int *pipe
)
41 const char *name_prefix
;
44 name_prefix
= name
? name
: "Unknown pipe";
45 ret
= asprintf(&names
[0], "%s (read end)", name_prefix
);
49 ret
= asprintf(&names
[1], "%s (write end)", name_prefix
);
54 ret
= fd_tracker_open_unsuspendable_fd(tracker
, pipe
,
55 (const char **) names
, 2, open_pipe_cloexec
, NULL
);
63 int fd_tracker_util_pipe_close(struct fd_tracker
*tracker
, int *pipe
)
65 return fd_tracker_close_unsuspendable_fd(
66 tracker
, pipe
, 2, close_pipe
, NULL
);
69 struct open_directory_handle_args
{
70 const struct lttng_directory_handle
*in_handle
;
71 struct lttng_directory_handle
*ret_handle
;
76 int open_directory_handle(void *_args
, int *out_fds
)
79 struct open_directory_handle_args
*args
= _args
;
80 struct lttng_directory_handle
*new_handle
= NULL
;
82 new_handle
= args
->in_handle
?
83 lttng_directory_handle_create_from_handle(
84 args
->path
, args
->in_handle
) :
85 lttng_directory_handle_create(args
->path
);
91 args
->ret_handle
= new_handle
;
94 * Reserved to indicate that the handle does not use a handle; there is
95 * nothing to track. We want to indicate an error to the fd-tracker so
96 * that it doesn't attempt to track the file descriptor, but also want
97 * the caller to retrieve the newly-created handle.
99 * Calling this a hack is a fair assessment.
101 if (!lttng_directory_handle_uses_fd(new_handle
)) {
105 *out_fds
= new_handle
->dirfd
;
117 int fd_close(void *unused
, int *in_fds
)
119 const int ret
= close(in_fds
[0]);
126 void directory_handle_destroy(
127 struct lttng_directory_handle
*handle
, void *data
)
129 struct fd_tracker
*tracker
= data
;
130 const int ret
= fd_tracker_close_unsuspendable_fd(
131 tracker
, &handle
->dirfd
, 1, fd_close
, NULL
);
134 ERR("Failed to untrack directory handle file descriptor");
140 struct lttng_directory_handle
*fd_tracker_create_directory_handle(
141 struct fd_tracker
*tracker
, const char *path
)
143 return fd_tracker_create_directory_handle_from_handle(
144 tracker
, NULL
, path
);
148 struct lttng_directory_handle
*fd_tracker_create_directory_handle_from_handle(
149 struct fd_tracker
*tracker
,
150 struct lttng_directory_handle
*in_handle
,
155 char *handle_name
= NULL
;
156 char cwd_path
[LTTNG_PATH_MAX
] = "working directory";
157 struct lttng_directory_handle
*new_handle
= NULL
;
158 struct open_directory_handle_args open_args
= {
159 .in_handle
= in_handle
,
164 if (!getcwd(cwd_path
, sizeof(cwd_path
))) {
165 PERROR("Failed to get current working directory to name directory handle");
170 ret
= asprintf(&handle_name
, "Directory handle to %s",
171 path
? path
: cwd_path
);
173 PERROR("Failed to format directory handle name");
177 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &dirfd
,
178 (const char **) &handle_name
, 1, open_directory_handle
,
180 if (ret
&& ret
!= ENOTSUP
) {
181 ERR("Failed to open directory handle to %s through the fd tracker", path
? path
: cwd_path
);
183 new_handle
= open_args
.ret_handle
;
186 new_handle
->destroy_cb
= directory_handle_destroy
;
187 new_handle
->destroy_cb_data
= tracker
;