| 1 | /* |
| 2 | * fd-cache.c |
| 3 | * |
| 4 | * Babeltrace - File descriptor cache |
| 5 | * |
| 6 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 7 | * |
| 8 | * Author: Francis Deslauriers <francis.deslauriers@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "FD-CACHE" |
| 30 | #include "logging.h" |
| 31 | |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | #include <glib.h> |
| 37 | |
| 38 | #include "common/assert.h" |
| 39 | #include "fd-cache.h" |
| 40 | |
| 41 | struct file_key { |
| 42 | uint64_t dev; |
| 43 | uint64_t ino; |
| 44 | }; |
| 45 | |
| 46 | struct fd_handle_internal { |
| 47 | struct bt_fd_cache_handle fd_handle; |
| 48 | uint64_t ref_count; |
| 49 | struct file_key *key; |
| 50 | }; |
| 51 | |
| 52 | static |
| 53 | void fd_cache_handle_internal_destroy( |
| 54 | struct fd_handle_internal *internal_fd) |
| 55 | { |
| 56 | if (!internal_fd) { |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | if (internal_fd->fd_handle.fd >= 0) { |
| 61 | close(internal_fd->fd_handle.fd); |
| 62 | internal_fd->fd_handle.fd = -1; |
| 63 | } |
| 64 | |
| 65 | end: |
| 66 | g_free(internal_fd); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Using simple hash algorithm found on stackoverflow: |
| 71 | * https://stackoverflow.com/questions/664014/ |
| 72 | */ |
| 73 | static inline |
| 74 | uint64_t hash_uint64_t(uint64_t x) { |
| 75 | x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); |
| 76 | x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); |
| 77 | x = x ^ (x >> 31); |
| 78 | return x; |
| 79 | } |
| 80 | |
| 81 | static |
| 82 | guint file_key_hash(gconstpointer v) |
| 83 | { |
| 84 | const struct file_key *fk = v; |
| 85 | return hash_uint64_t(fk->dev) ^ hash_uint64_t(fk->ino); |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | gboolean file_key_equal(gconstpointer v1, gconstpointer v2) |
| 90 | { |
| 91 | const struct file_key *fk1 = v1; |
| 92 | const struct file_key *fk2 = v2; |
| 93 | |
| 94 | return (fk1->dev == fk2->dev) && (fk1->ino == fk2->ino); |
| 95 | } |
| 96 | |
| 97 | static |
| 98 | void file_key_destroy(gpointer data) |
| 99 | { |
| 100 | struct file_key *fk = data; |
| 101 | g_free(fk); |
| 102 | } |
| 103 | |
| 104 | BT_HIDDEN |
| 105 | int bt_fd_cache_init(struct bt_fd_cache *fdc) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | fdc->cache = g_hash_table_new_full(file_key_hash, file_key_equal, |
| 110 | file_key_destroy, (GDestroyNotify) fd_cache_handle_internal_destroy); |
| 111 | if (!fdc->cache) { |
| 112 | ret = -1; |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | BT_HIDDEN |
| 119 | void bt_fd_cache_fini(struct bt_fd_cache *fdc) |
| 120 | { |
| 121 | BT_ASSERT(fdc->cache); |
| 122 | /* |
| 123 | * All handle should have been removed for the hashtable at this point. |
| 124 | */ |
| 125 | BT_ASSERT(g_hash_table_size(fdc->cache) == 0); |
| 126 | g_hash_table_destroy(fdc->cache); |
| 127 | |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | BT_HIDDEN |
| 132 | struct bt_fd_cache_handle *bt_fd_cache_get_handle(struct bt_fd_cache *fdc, |
| 133 | const char *path) |
| 134 | { |
| 135 | struct fd_handle_internal *fd_internal = NULL; |
| 136 | struct stat statbuf; |
| 137 | struct file_key fk; |
| 138 | int ret, fd = -1; |
| 139 | |
| 140 | ret = stat(path, &statbuf); |
| 141 | if (ret < 0) { |
| 142 | /* |
| 143 | * This is not necessarily an error as we sometimes try to open |
| 144 | * files to see if they exist. Log the error as DEBUG severity |
| 145 | * level. |
| 146 | */ |
| 147 | BT_LOGD_ERRNO("Failed to stat file", ": path=%s", path); |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Use the device number and inode number to uniquely identify a file. |
| 153 | * Even if the file has the same path, it may have been replaced so we |
| 154 | * must open a new FD for it. This replacement of file is more likely |
| 155 | * to happen with a lttng-live source component. |
| 156 | */ |
| 157 | fk.dev = statbuf.st_dev; |
| 158 | fk.ino = statbuf.st_ino; |
| 159 | |
| 160 | fd_internal = g_hash_table_lookup(fdc->cache, &fk); |
| 161 | if (!fd_internal) { |
| 162 | struct file_key *file_key; |
| 163 | |
| 164 | fd = open(path, O_RDONLY); |
| 165 | if (fd < 0) { |
| 166 | BT_LOGE_ERRNO("Failed to open file", "path=%s", path); |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | fd_internal = g_new0(struct fd_handle_internal, 1); |
| 171 | if (!fd_internal) { |
| 172 | BT_LOGE("Failed to allocate fd internal handle"); |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | file_key = g_new0(struct file_key, 1); |
| 177 | if (!fd_internal) { |
| 178 | BT_LOGE("Failed to allocate file key"); |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | *file_key = fk; |
| 183 | |
| 184 | fd_internal->fd_handle.fd = fd; |
| 185 | fd_internal->ref_count = 0; |
| 186 | fd_internal->key = file_key; |
| 187 | |
| 188 | /* Insert the newly created fd handle. */ |
| 189 | g_hash_table_insert(fdc->cache, fd_internal->key, fd_internal); |
| 190 | } |
| 191 | |
| 192 | fd_internal->ref_count++; |
| 193 | goto end; |
| 194 | |
| 195 | error: |
| 196 | /* |
| 197 | * Close file descriptor if it was open() and we are currently on error |
| 198 | * path. |
| 199 | */ |
| 200 | if (fd != -1) { |
| 201 | ret = close(fd); |
| 202 | if (ret) { |
| 203 | BT_LOGE_ERRNO("Failed to close file descriptor", |
| 204 | ": fd=%i, path=%s", fd, path); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | fd_cache_handle_internal_destroy(fd_internal); |
| 209 | fd_internal = NULL; |
| 210 | end: |
| 211 | return (struct bt_fd_cache_handle *) fd_internal; |
| 212 | } |
| 213 | |
| 214 | BT_HIDDEN |
| 215 | void bt_fd_cache_put_handle(struct bt_fd_cache *fdc, |
| 216 | struct bt_fd_cache_handle *handle) |
| 217 | { |
| 218 | struct fd_handle_internal *fd_internal; |
| 219 | |
| 220 | if (!handle) { |
| 221 | goto end; |
| 222 | } |
| 223 | |
| 224 | fd_internal = (struct fd_handle_internal *) handle; |
| 225 | |
| 226 | BT_ASSERT(fd_internal->ref_count > 0); |
| 227 | |
| 228 | if (fd_internal->ref_count > 1) { |
| 229 | fd_internal->ref_count--; |
| 230 | } else { |
| 231 | gboolean ret; |
| 232 | int close_ret; |
| 233 | |
| 234 | close_ret = close(fd_internal->fd_handle.fd); |
| 235 | if (close_ret == -1) { |
| 236 | BT_LOGW_ERRNO("Failed to close file descriptor", |
| 237 | ": fd=%d", fd_internal->fd_handle.fd); |
| 238 | } |
| 239 | ret = g_hash_table_remove(fdc->cache, fd_internal->key); |
| 240 | BT_ASSERT(ret); |
| 241 | } |
| 242 | |
| 243 | end: |
| 244 | return; |
| 245 | } |