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 23 namespace sapi { 24 25 // The class provides primitives for converting FileToc structures into 26 // executable files. 27 class EmbedFile { 28 public: 29 EmbedFile(const EmbedFile&) = delete; 30 EmbedFile& operator=(const EmbedFile&) = delete; 31 32 // Returns the pointer to the per-process EmbedFile object. 33 static EmbedFile* instance(); 34 35 // Returns a file-descriptor for a given FileToc. 36 int GetFdForFileToc(const FileToc* toc); 37 38 // Returns a duplicated file-descriptor for a given FileToc. 39 int GetDupFdForFileToc(const FileToc* toc); 40 41 private: 42 // Creates an executable file for a given FileToc, and return its 43 // file-descriptors (-1 in case of errors). 44 static int CreateFdForFileToc(const FileToc* toc); 45 46 EmbedFile() = default; 47 48 // List of File TOCs and corresponding file-descriptors. 49 absl::flat_hash_map<const FileToc*, int> file_tocs_ 50 ABSL_GUARDED_BY(file_tocs_mutex_); 51 absl::Mutex file_tocs_mutex_; 52 }; 53 54 } // namespace sapi 55 56 #endif // SANDBOXED_API_EMBED_FILE_H_ 57