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 };
68 
69 class Syscall : public SyscallInterface {
70  public:
71   Syscall(Syscall& other) = delete;
72   void operator=(const Syscall&) = delete;
73 
74   static Syscall& GetSyscall();
75 
76   int Access(const std::string& path_name, int mode) override;
77   int Close(int fd) override;
78   int CloseDir(DIR* dirp) override;
79   int FAdvise(int fd, int64_t offset, int64_t len, int advice) override;
80   int FAllocate(int fd, int mode, int64_t offset, int64_t len) override;
81   int FTruncate(int fd, int64_t length) override;
82   int FStat(int filedes, struct stat64* buf) override;
83   int FSync(int fd) override;
84   pid_t GetTid() override;
85   int Open(const std::string& path_name, int flags, int mode) override;
86   DIR* OpenDir(const std::string& name) override;
87   int64_t Read(int fd, char* buf, int64_t count, int64_t offset) override;
88   int SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) override;
89   struct dirent* ReadDir(DIR* dirp) override;
90   int64_t ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) override;
91   void Sync() override;
92   int Unlink(const std::string& path_name) override;
93   int64_t Write(int fd, char* buf, int64_t count, int64_t offset) override;
94 
95  private:
Syscall()96   Syscall(){};
97 };
98 
99 }  // namespace dittosuite
100