| 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.h> |
| 9 | #include <common/fd-tracker/utils.h> |
| 10 | #include <common/utils.h> |
| 11 | #include <lttng/constant.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | static |
| 17 | int open_pipe_cloexec(void *data, int *fds) |
| 18 | { |
| 19 | return utils_create_pipe_cloexec(fds); |
| 20 | } |
| 21 | |
| 22 | static |
| 23 | int close_pipe(void *data, int *pipe) |
| 24 | { |
| 25 | utils_close_pipe(pipe); |
| 26 | pipe[0] = pipe[1] = -1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | LTTNG_HIDDEN |
| 31 | int fd_tracker_util_close_fd(void *unused, int *fd) |
| 32 | { |
| 33 | return close(*fd); |
| 34 | } |
| 35 | |
| 36 | LTTNG_HIDDEN |
| 37 | int fd_tracker_util_pipe_open_cloexec( |
| 38 | struct fd_tracker *tracker, const char *name, int *pipe) |
| 39 | { |
| 40 | int ret; |
| 41 | const char *name_prefix; |
| 42 | char *names[2]; |
| 43 | |
| 44 | name_prefix = name ? name : "Unknown pipe"; |
| 45 | ret = asprintf(&names[0], "%s (read end)", name_prefix); |
| 46 | if (ret < 0) { |
| 47 | goto end; |
| 48 | } |
| 49 | ret = asprintf(&names[1], "%s (write end)", name_prefix); |
| 50 | if (ret < 0) { |
| 51 | goto end; |
| 52 | } |
| 53 | |
| 54 | ret = fd_tracker_open_unsuspendable_fd(tracker, pipe, |
| 55 | (const char **) names, 2, open_pipe_cloexec, NULL); |
| 56 | free(names[0]); |
| 57 | free(names[1]); |
| 58 | end: |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | LTTNG_HIDDEN |
| 63 | int fd_tracker_util_pipe_close(struct fd_tracker *tracker, int *pipe) |
| 64 | { |
| 65 | return fd_tracker_close_unsuspendable_fd( |
| 66 | tracker, pipe, 2, close_pipe, NULL); |
| 67 | } |
| 68 | |
| 69 | struct open_directory_handle_args { |
| 70 | const struct lttng_directory_handle *in_handle; |
| 71 | struct lttng_directory_handle *ret_handle; |
| 72 | const char *path; |
| 73 | }; |
| 74 | |
| 75 | static |
| 76 | int open_directory_handle(void *_args, int *out_fds) |
| 77 | { |
| 78 | int ret = 0; |
| 79 | struct open_directory_handle_args *args = _args; |
| 80 | struct lttng_directory_handle *new_handle = NULL; |
| 81 | |
| 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); |
| 86 | if (!new_handle) { |
| 87 | ret = -errno; |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | args->ret_handle = new_handle; |
| 92 | |
| 93 | /* |
| 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. |
| 98 | * |
| 99 | * Calling this a hack is a fair assessment. |
| 100 | */ |
| 101 | if (!lttng_directory_handle_uses_fd(new_handle)) { |
| 102 | ret = ENOTSUP; |
| 103 | } else { |
| 104 | #ifdef COMPAT_DIRFD |
| 105 | *out_fds = new_handle->dirfd; |
| 106 | #else |
| 107 | abort(); |
| 108 | #endif |
| 109 | |
| 110 | } |
| 111 | end: |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | #ifdef COMPAT_DIRFD |
| 116 | static |
| 117 | int fd_close(void *unused, int *in_fds) |
| 118 | { |
| 119 | const int ret = close(in_fds[0]); |
| 120 | |
| 121 | in_fds[0] = -1; |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static |
| 126 | void directory_handle_destroy( |
| 127 | struct lttng_directory_handle *handle, void *data) |
| 128 | { |
| 129 | struct fd_tracker *tracker = data; |
| 130 | const int ret = fd_tracker_close_unsuspendable_fd( |
| 131 | tracker, &handle->dirfd, 1, fd_close, NULL); |
| 132 | |
| 133 | if (ret) { |
| 134 | ERR("Failed to untrack directory handle file descriptor"); |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | LTTNG_HIDDEN |
| 140 | struct lttng_directory_handle *fd_tracker_create_directory_handle( |
| 141 | struct fd_tracker *tracker, const char *path) |
| 142 | { |
| 143 | return fd_tracker_create_directory_handle_from_handle( |
| 144 | tracker, NULL, path); |
| 145 | } |
| 146 | |
| 147 | LTTNG_HIDDEN |
| 148 | struct lttng_directory_handle *fd_tracker_create_directory_handle_from_handle( |
| 149 | struct fd_tracker *tracker, |
| 150 | struct lttng_directory_handle *in_handle, |
| 151 | const char *path) |
| 152 | { |
| 153 | int ret; |
| 154 | int dirfd = -1; |
| 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, |
| 160 | .path = path, |
| 161 | }; |
| 162 | |
| 163 | if (!path) { |
| 164 | if (!getcwd(cwd_path, sizeof(cwd_path))) { |
| 165 | PERROR("Failed to get current working directory to name directory handle"); |
| 166 | goto end; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | ret = asprintf(&handle_name, "Directory handle to %s", |
| 171 | path ? path : cwd_path); |
| 172 | if (ret < 0) { |
| 173 | PERROR("Failed to format directory handle name"); |
| 174 | goto end; |
| 175 | } |
| 176 | |
| 177 | ret = fd_tracker_open_unsuspendable_fd(tracker, &dirfd, |
| 178 | (const char **) &handle_name, 1, open_directory_handle, |
| 179 | &open_args); |
| 180 | if (ret && ret != ENOTSUP) { |
| 181 | ERR("Failed to open directory handle to %s through the fd tracker", path ? path : cwd_path); |
| 182 | } |
| 183 | new_handle = open_args.ret_handle; |
| 184 | |
| 185 | #ifdef COMPAT_DIRFD |
| 186 | new_handle->destroy_cb = directory_handle_destroy; |
| 187 | new_handle->destroy_cb_data = tracker; |
| 188 | #endif |
| 189 | end: |
| 190 | free(handle_name); |
| 191 | return new_handle; |
| 192 | } |