1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SANDBOXED_API_EMBED_FILE_H_ 16 #define SANDBOXED_API_EMBED_FILE_H_ 17 18 #include "sandboxed_api/file_toc.h" 19 #include "absl/base/thread_annotations.h" 20 #include "absl/container/flat_hash_map.h" 21 #include "absl/synchronization/mutex.h" 22 #include "sandboxed_api/util/fileops.h" 23 24 namespace sapi { 25 26 class EmbedFileTestPeer; 27 28 // The class provides primitives for converting FileToc structures into 29 // executable files. 30 class EmbedFile { 31 public: 32 EmbedFile(const EmbedFile&) = delete; 33 EmbedFile& operator=(const EmbedFile&) = delete; 34 35 // Returns the pointer to the per-process EmbedFile object. 36 static EmbedFile* instance(); 37 38 // Returns a file-descriptor for a given FileToc. 39 int GetFdForFileToc(const FileToc* toc); 40 41 // Returns a duplicated file-descriptor for a given FileToc. 42 int GetDupFdForFileToc(const FileToc* toc); 43 44 private: 45 friend class EmbedFileTestPeer; // For testing. 46 // Creates an executable file for a given FileToc, and return its 47 // file-descriptors (-1 in case of errors). 48 static int CreateFdForFileToc(const FileToc* toc); 49 50 EmbedFile() = default; 51 52 // List of File TOCs and corresponding file-descriptors. 53 absl::flat_hash_map<const FileToc*, file_util::fileops::FDCloser> file_tocs_ 54 ABSL_GUARDED_BY(file_tocs_mutex_); 55 absl::Mutex file_tocs_mutex_; 56 }; 57 58 } // namespace sapi 59 60 #endif // SANDBOXED_API_EMBED_FILE_H_ 61