X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fdirectory-handle.c;h=e29d6540b56c648242afafc5c065e1ca0fe7ec9c;hp=d9d1e233757bdb76bbabdc51436ac13b02388574;hb=refs%2Fheads%2Fsow-2020-0002-rev1;hpb=cbf53d23cecaca9c6ec71582663c4a8254a9f285 diff --git a/src/common/compat/directory-handle.c b/src/common/compat/directory-handle.c index d9d1e2337..e29d6540b 100644 --- a/src/common/compat/directory-handle.c +++ b/src/common/compat/directory-handle.c @@ -1,18 +1,8 @@ /* - * Copyright (C) 2019 - Jérémie Galarneau + * Copyright (C) 2019 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include @@ -36,9 +26,6 @@ * of the internal API below. */ static -int lttng_directory_handle_stat(const struct lttng_directory_handle *handle, - const char *path, struct stat *st); -static int lttng_directory_handle_mkdir( const struct lttng_directory_handle *handle, const char *path, mode_t mode); @@ -93,6 +80,26 @@ void lttng_directory_handle_release(struct urcu_ref *ref); #ifdef COMPAT_DIRFD +/* + * Special inode number reserved to represent the "current working directory". + * ino_t is spec'ed as being an unsigned integral type. + */ +#define RESERVED_AT_FDCWD_INO \ + ({ \ + uint64_t reserved_val; \ + switch (sizeof(ino_t)) { \ + case 4: \ + reserved_val = UINT32_MAX; \ + break; \ + case 8: \ + reserved_val = UINT64_MAX; \ + break; \ + default: \ + abort(); \ + } \ + (ino_t) reserved_val; \ + }) + LTTNG_HIDDEN struct lttng_directory_handle *lttng_directory_handle_create(const char *path) { @@ -144,11 +151,25 @@ LTTNG_HIDDEN struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd( int dirfd) { + int ret; struct lttng_directory_handle *handle = zmalloc(sizeof(*handle)); + struct stat stat_buf; if (!handle) { goto end; } + + if (dirfd != AT_FDCWD) { + ret = fstat(dirfd, &stat_buf); + if (ret) { + PERROR("Failed to fstat directory file descriptor %i", dirfd); + lttng_directory_handle_release(&handle->ref); + handle = NULL; + goto end; + } + } else { + handle->directory_inode = RESERVED_AT_FDCWD_INO; + } handle->dirfd = dirfd; urcu_ref_init(&handle->ref); end: @@ -162,6 +183,10 @@ void lttng_directory_handle_release(struct urcu_ref *ref) struct lttng_directory_handle *handle = container_of(ref, struct lttng_directory_handle, ref); + if (handle->destroy_cb) { + handle->destroy_cb(handle, handle->destroy_cb_data); + } + if (handle->dirfd == AT_FDCWD || handle->dirfd == -1) { goto end; } @@ -199,19 +224,33 @@ end: return new_handle; } +LTTNG_HIDDEN +bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs, + const struct lttng_directory_handle *rhs) +{ + return lhs->directory_inode == rhs->directory_inode; +} + static void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle) { handle->dirfd = -1; } -static +LTTNG_HIDDEN int lttng_directory_handle_stat(const struct lttng_directory_handle *handle, const char *path, struct stat *st) { return fstatat(handle->dirfd, path, st, 0); } +LTTNG_HIDDEN +bool lttng_directory_handle_uses_fd( + const struct lttng_directory_handle *handle) +{ + return handle->dirfd != AT_FDCWD; +} + static int lttng_directory_handle_mkdir( const struct lttng_directory_handle *handle, @@ -316,7 +355,12 @@ static int lttng_directory_handle_rmdir( const struct lttng_directory_handle *handle, const char *name) { - return unlinkat(handle->dirfd, name, AT_REMOVEDIR); + int ret = unlinkat(handle->dirfd, name, AT_REMOVEDIR); + if (ret) { + PERROR("Failed to remove directory `%s`", name); + } + + return ret; } static @@ -550,13 +594,20 @@ end: return new_handle; } +LTTNG_HIDDEN +bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs, + const struct lttng_directory_handle *rhs) +{ + return strcmp(lhs->base_path, rhs->base_path) == 0; +} + static void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle) { handle->base_path = NULL; } -static +LTTNG_HIDDEN int lttng_directory_handle_stat(const struct lttng_directory_handle *handle, const char *subdirectory, struct stat *st) { @@ -574,6 +625,13 @@ end: return ret; } +LTTNG_HIDDEN +bool lttng_directory_handle_uses_fd( + const struct lttng_directory_handle *handle) +{ + return false; +} + static int lttng_directory_handle_mkdir(const struct lttng_directory_handle *handle, const char *subdirectory, mode_t mode) @@ -961,8 +1019,9 @@ int lttng_directory_handle_create_subdirectory_as_user( ret = create_directory_check_exists(handle, subdirectory, mode); } else { - ret = _run_as_mkdir(handle, subdirectory, - mode, creds->uid, creds->gid); + ret = _run_as_mkdir(handle, subdirectory, mode, + lttng_credentials_get_uid(creds), + lttng_credentials_get_gid(creds)); } return ret; @@ -982,7 +1041,7 @@ int lttng_directory_handle_create_subdirectory_recursive_as_user( subdirectory_path, mode); } else { ret = _run_as_mkdir_recursive(handle, subdirectory_path, - mode, creds->uid, creds->gid); + mode, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds)); } return ret; @@ -1023,7 +1082,7 @@ int lttng_directory_handle_open_file_as_user( mode); } else { ret = _run_as_open(handle, filename, flags, mode, - creds->uid, creds->gid); + lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds)); } return ret; } @@ -1050,7 +1109,7 @@ int lttng_directory_handle_unlink_file_as_user( /* Run as current user. */ ret = lttng_directory_handle_unlink(handle, filename); } else { - ret = _run_as_unlink(handle, filename, creds->uid, creds->gid); + ret = _run_as_unlink(handle, filename, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds)); } return ret; } @@ -1091,7 +1150,7 @@ int lttng_directory_handle_rename_as_user( old_name, new_handle, new_name); } else { ret = _run_as_rename(old_handle, old_name, new_handle, - new_name, creds->uid, creds->gid); + new_name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds)); } return ret; } @@ -1117,7 +1176,7 @@ int lttng_directory_handle_remove_subdirectory_as_user( /* Run as current user. */ ret = lttng_directory_handle_rmdir(handle, name); } else { - ret = _run_as_rmdir(handle, name, creds->uid, creds->gid); + ret = _run_as_rmdir(handle, name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds)); } return ret; } @@ -1351,8 +1410,8 @@ int lttng_directory_handle_remove_subdirectory_recursive_as_user( /* Run as current user. */ ret = remove_directory_recursive(handle, name, flags); } else { - ret = _run_as_rmdir_recursive(handle, name, creds->uid, - creds->gid, flags); + ret = _run_as_rmdir_recursive(handle, name, lttng_credentials_get_uid(creds), + lttng_credentials_get_gid(creds), flags); } return ret; }