Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2021 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <TargetConditionals.h> #if !TARGET_OS_EXCLAVEKIT #include <fcntl.h> #include <unistd.h> #include <sys/attr.h> #include <sys/stat.h> #include <sys/param.h> #include <sys/ucred.h> #include <sys/mount.h> #include <System/sys/fsgetpath.h> #include "FileManager.h" namespace dyld4 { using lsl::UUID; using lsl::UniquePtr; using lsl::Allocator; using lsl::OrderedMap; FileManager::FileManager(Allocator& allocator, const SyscallDelegate* syscall) : _syscall(syscall), _allocator(&allocator), _fsUUIDMap(_allocator->makeUnique<OrderedMap<uint64_t,UUID>>(*_allocator)) {} void FileManager::swap(FileManager& other) { using std::swap; std::swap(_allocator, other._allocator); std::swap(_fsUUIDMap, other._fsUUIDMap); } FileRecord FileManager::fileRecordForPath(Allocator& allocator, const char* filePath) { const char* str = nullptr; if ( filePath ) str = allocator.strdup(filePath); return FileRecord(*this, UniquePtr<const char>(str)); } FileRecord FileManager::fileRecordForStat(const struct stat& sb) { return FileRecord(*this, sb); } FileRecord FileManager::fileRecordForVolumeUUIDAndObjID(const UUID& VID, uint64_t objectID) { return FileRecord(*this, VID, objectID); } FileRecord FileManager::fileRecordForVolumeDevIDAndObjID(uint64_t device, uint64_t objectID) { // auto volume = uuidForFileSystem(device); // char uuidString[64]; // volume.dumpStr(&uuidString[0]); // fprintf(stderr, "device: %llu\n", device); // fprintf(stderr, "objectID: %llu\n", objectID); // fprintf(stderr, "VolumeUUID: %s\n", uuidString); return FileRecord(*this, objectID, device, 0); } FileRecord FileManager::fileRecordForFileID(const FileID& fileID) { return FileRecord(*this, fileID.inode(), fileID.device(), fileID.mtime()); } void FileManager::reloadFSInfos() const { struct VolAttrBuf { u_int32_t length; dev_t dev; fsid_t fsid; vol_capabilities_attr_t volAttrs; uuid_t volUUID; } __attribute__((aligned(4), packed)); typedef struct VolAttrBuf VolAttrBuf; while (1) { int fsCount = getfsstat(nullptr, 0, MNT_NOWAIT); if (fsCount == -1) { // getfsstat failed, stop scanning for file systems, compact info will use full paths break; } int fsInfoSize = fsCount*sizeof(struct statfs); auto fsInfos = (struct statfs *)_allocator->malloc(fsInfoSize); if (this->getfsstat(fsInfos, fsInfoSize, MNT_NOWAIT) != fsCount) { // Retry _allocator->free((void*)fsInfos); continue; } for (auto i = 0; i < fsCount; ++i) { // On darwin the lower 32 bits of a fsid_t are the same as an the dev_t // it comes from, excluding some special circumstances related to volume // groups that are not relevent here. uint64_t f_fsid = (*((uint64_t*)&fsInfos[i].f_fsid)) & 0x00ffffffff; if (_fsUUIDMap->find(f_fsid) != _fsUUIDMap->end()) { continue; } // getattrlist() can upcall when used against a non-root volume which results in a deadlock. if ((fsInfos[i].f_flags & MNT_ROOTFS) == 0) { _fsUUIDMap->insert({f_fsid, UUID()}); continue; } int err; attrlist attrList; VolAttrBuf attrBuf; memset(&attrList, 0, sizeof(attrList)); attrList.bitmapcount = ATTR_BIT_MAP_COUNT; attrList.commonattr = ATTR_CMN_FSID | ATTR_CMN_DEVID; attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES | ATTR_VOL_UUID; err = this->getattrlist(fsInfos[i].f_mntonname, &attrList, &attrBuf, sizeof(attrBuf), 0); if (err == 0 && (attrBuf.volAttrs.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_PERSISTENTOBJECTIDS)) { _fsUUIDMap->insert({f_fsid, attrBuf.volUUID}); } else { _fsUUIDMap->insert({f_fsid, UUID()}); } } _allocator->free((void*)fsInfos); break; } } const UUID FileManager::uuidForFileSystem(uint64_t fsid) const { return withFSInfoLock([&]{ fsid &= 0x00ffffffff; // Mask off high bits that are an fs type tag auto i = _fsUUIDMap->find(fsid); if (i == _fsUUIDMap->end()) { // Maybe a new filesystem was loaded, try scanning reloadFSInfos(); i = _fsUUIDMap->find(fsid); } if (i == _fsUUIDMap->end()) { // Still nothing, add a sentinel _fsUUIDMap->insert({fsid, UUID()}); i = _fsUUIDMap->find(fsid); } assert (i != _fsUUIDMap->end()); return i->second; }); } uint64_t FileManager::fsidForUUID(const UUID& uuid) const { return withFSInfoLock([&]{ for (const auto& i : *_fsUUIDMap) { if (i.second == uuid) { return i.first; } } // Maybe a new filesystem was loaded, try scanning // This is inefficient, but the only time it can happen is in libdyld reconsituting a compact info // after a volume is gone, so it is not worth the memory to make a reverse mapping table reloadFSInfos(); for (const auto& i : *_fsUUIDMap) { if (i.second == uuid) { return i.first; } } return 0ULL; }); } UniquePtr<char> FileManager::getPath(const UUID& VID, uint64_t OID) { if (!VID) { return nullptr; } uint64_t fsid = fsidForUUID(VID); return getPath(fsid, OID); } UniquePtr<char> FileManager::getPath(uint64_t fsid, uint64_t OID) { if ((fsid == 0) || (OID == 0)) { return nullptr; } char path[PATH_MAX]; ssize_t result = this->fsgetpath(&path[0], PATH_MAX, fsid, OID); #if !__LP64__ //FIXME: Workaround for missing stat high bit on 32 bit platforms if (result == -1) { OID = 0x0fffffff00000000ULL | OID; result = this->fsgetpath(&path[0], PATH_MAX, fsid, OID); } #endif if (result == -1) { return nullptr; } return UniquePtr<char>((char*)_allocator->strdup(path)); } ssize_t FileManager::fsgetpath(char result[], size_t resultBufferSize, uint64_t fsID, uint64_t objID) const { #if BUILDING_DYLD return _syscall->fsgetpath(result, resultBufferSize, fsID, objID); #else fsid_t fsid = *reinterpret_cast<fsid_t*>(&fsID); return ::fsgetpath(result, resultBufferSize, &fsid, objID); #endif } int FileManager::getfsstat(struct statfs *buf, int bufsize, int flags) const { #if BUILDING_DYLD return _syscall->getfsstat(buf, bufsize, flags); #else return ::getfsstat(buf, bufsize, flags); #endif } int FileManager::getattrlist(const char* path, struct attrlist * attrList, void * attrBuf, size_t attrBufSize, uint32_t options) const { #if BUILDING_DYLD return _syscall->getattrlist(path, attrList, attrBuf, attrBufSize, options); #else return ::getattrlist(path, attrList, attrBuf, attrBufSize, options); #endif } #pragma mark - #pragma mark FileRecord FileRecord::FileRecord(FileManager& fileManager, const UUID& VID, uint64_t objectID) : _fileManager(&fileManager), _objectID(objectID), _volume(VID) {} FileRecord::FileRecord(FileManager& fileManager, UniquePtr<const char>&& FP) : _fileManager(&fileManager), _path(std::move(FP)) {} FileRecord::FileRecord(FileManager& fileManager, uint64_t objectID, uint64_t device, uint64_t mtime) : _fileManager(&fileManager), _objectID(objectID), _device(device), _volume(_fileManager->uuidForFileSystem(_device)), _mtime(mtime) { if (_objectID && _device && _mtime) { _statResult = 0; } } FileRecord::FileRecord(FileManager& fileManager, const struct stat& sb) : _fileManager(&fileManager), _objectID(sb.st_ino), _device(sb.st_dev), _volume(_fileManager->uuidForFileSystem(_device)), _mtime(sb.st_mtime), _statResult(0) {} FileRecord::FileRecord(const FileRecord& other) : _fileManager(other._fileManager), _objectID(other._objectID), _volume(other._volume), _path(UniquePtr<char>(_fileManager->_allocator->strdup(&*other._path))), _size(other._size), _mtime(other._mtime), _fd(other._fd), _statResult(other._statResult), _mode(other._mode), _valid(other._valid) {} FileRecord::FileRecord(FileRecord&& other) { swap(other); } FileRecord& FileRecord::operator=(const FileRecord& other) { auto tmp = other; swap(tmp); return *this; } FileRecord& FileRecord::operator=(FileRecord&& other) { swap(other); return *this; } FileRecord::~FileRecord() { close(); } void FileRecord::swap(FileRecord& other) { std::swap(_volume, other._volume); std::swap(_objectID, other._objectID); std::swap(_device, other._device); std::swap(_path, other._path); std::swap(_fileManager, other._fileManager); std::swap(_size, other._size); std::swap(_mtime, other._mtime); std::swap(_fd, other._fd); std::swap(_statResult, other._statResult); std::swap(_mode, other._mode); std::swap(_valid, other._valid); } int FileRecord::open(int flags) { assert(_fd == -1); uint64_t fsid = 0; if (_volume) { fsid = _fileManager->fsidForUUID(_volume); } if (fsid && _objectID) { _fd = openbyid_np((fsid_t*)&fsid, (fsobj_id_t*)_objectID, flags); } if (_fd == -1) { _fd = ::open(getPath(), flags); } return _fd; } void FileRecord::close() { if (_fd != -1) { ::close(_fd); _fd = -1; } } bool FileRecord::exists() const { stat(); return (_statResult == 0); } uint64_t FileRecord::objectID() const { if (_objectID == 0) { stat(); } return _objectID; } uint64_t FileRecord::mtime() const { if (_mtime == 0) { stat(); } return _mtime; } size_t FileRecord::size() const { if (_size == 0) { stat(); } return _size; } void FileRecord::stat() const { if (_statResult != 1) { return; } struct stat stat_buf; if (_fd != -1) { _statResult = fstat(_fd, &stat_buf); } else { _statResult = ::stat(getPath(), &stat_buf); } if (_statResult != 0) { return; } _size = (size_t)stat_buf.st_size; _mtime = stat_buf.st_mtime; _mode = stat_buf.st_mode; if ((_objectID == 0) && !_volume) { #if __LP64__ _objectID = stat_buf.st_ino; #else _objectID = stat_buf.st_ino & 0xFFFFFFFF; // HACK, work around inode randomly getting high bit set, making them uncomparable. #endif _volume = _fileManager->uuidForFileSystem(stat_buf.st_dev); } } const char* FileRecord::getPath() const { if (!_path) { if (_device) { _path = _fileManager->getPath(_device, _objectID); } else { _path = _fileManager->getPath(_volume, _objectID); } } return _path.withUnsafe([](const char * path){ return path; }); }; FileManager& FileRecord::fileManager() const { return *_fileManager; } bool FileRecord::persistent() const { return (_volume && _objectID); } const UUID& FileRecord::volume() const { return _volume; } }; /* namedpace dyld4 */ #endif // !TARGET_OS_EXCLAVEKIT |