1 // Copyright (C) 2021 The Android Open Source Project 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 // http://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 #pragma once 16 17 #include <dirent.h> 18 #include <fcntl.h> 19 #include <sys/param.h> 20 #include <sys/stat.h> 21 #include <unistd.h> 22 23 #include <cstdint> 24 #include <string> 25 26 namespace dittosuite { 27 28 struct SchedAttr__ { 29 uint32_t size; /* Size of this structure */ 30 uint32_t sched_policy; /* Policy (SCHED_*) */ 31 uint64_t sched_flags; /* Flags */ 32 33 int32_t sched_nice; /* Nice value (SCHED_OTHER, 34 SCHED_BATCH) */ 35 uint32_t sched_priority; /* Static priority (SCHED_FIFO, 36 SCHED_RR) */ 37 /* Remaining fields are for SCHED_DEADLINE */ 38 uint64_t sched_runtime; 39 uint64_t sched_deadline; 40 uint64_t sched_period; 41 }; 42 43 std::string to_string(const SchedAttr__& attr); 44 45 class SyscallInterface { 46 public: ~SyscallInterface()47 virtual ~SyscallInterface() {} 48 49 virtual int Access(const std::string& path_name, int mode) = 0; 50 virtual int Close(int fd) = 0; 51 virtual int CloseDir(DIR* dirp) = 0; 52 virtual int FAdvise(int fd, int64_t offset, int64_t len, int advice) = 0; 53 virtual int FAllocate(int fd, int mode, int64_t offset, int64_t len) = 0; 54 virtual int FTruncate(int fd, int64_t length) = 0; 55 virtual int FStat(int filedes, struct stat64* buf) = 0; 56 virtual int FSync(int fd) = 0; 57 virtual pid_t GetTid() = 0; 58 virtual int Open(const std::string& path_name, int flags, int mode) = 0; 59 virtual DIR* OpenDir(const std::string& name) = 0; 60 virtual int64_t Read(int fd, char* buf, int64_t count, int64_t offset) = 0; 61 virtual int SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) = 0; 62 virtual struct dirent* ReadDir(DIR* dirp) = 0; 63 virtual int64_t ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) = 0; 64 virtual void Sync() = 0; 65 virtual int Unlink(const std::string& path_name) = 0; 66 virtual int64_t Write(int fd, char* buf, int64_t count, int64_t offset) = 0; 67 virtual int LockMutex(pthread_mutex_t* mutex) = 0; 68 virtual int UnlockMutex(pthread_mutex_t* mutex) = 0; 69 }; 70 71 class Syscall : public SyscallInterface { 72 public: 73 Syscall(Syscall& other) = delete; 74 void operator=(const Syscall&) = delete; 75 76 static Syscall& GetSyscall(); 77 78 int Access(const std::string& path_name, int mode) override; 79 int Close(int fd) override; 80 int CloseDir(DIR* dirp) override; 81 int FAdvise(int fd, int64_t offset, int64_t len, int advice) override; 82 int FAllocate(int fd, int mode, int64_t offset, int64_t len) override; 83 int FTruncate(int fd, int64_t length) override; 84 int FStat(int filedes, struct stat64* buf) override; 85 int FSync(int fd) override; 86 pid_t GetTid() override; 87 int Open(const std::string& path_name, int flags, int mode) override; 88 DIR* OpenDir(const std::string& name) override; 89 int64_t Read(int fd, char* buf, int64_t count, int64_t offset) override; 90 int SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) override; 91 struct dirent* ReadDir(DIR* dirp) override; 92 int64_t ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) override; 93 void Sync() override; 94 int Unlink(const std::string& path_name) override; 95 int64_t Write(int fd, char* buf, int64_t count, int64_t offset) override; 96 int LockMutex(pthread_mutex_t* mutex) override; 97 int UnlockMutex(pthread_mutex_t* mutex) override; 98 99 private: Syscall()100 Syscall(){}; 101 }; 102 103 } // namespace dittosuite 104