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 #include <sys/syscall.h>
16 
17 #include <sstream>
18 
19 #include <ditto/logger.h>
20 #include <ditto/syscall.h>
21 
22 namespace dittosuite {
23 
GetSyscall()24 Syscall& Syscall::GetSyscall() {
25   static Syscall syscall;
26   return syscall;
27 }
28 
Access(const std::string & path_name,int mode)29 int Syscall::Access(const std::string& path_name, int mode) {
30   return access(path_name.c_str(), mode);
31 }
32 
Close(int fd)33 int Syscall::Close(int fd) {
34   return close(fd);
35 }
36 
CloseDir(DIR * dirp)37 int Syscall::CloseDir(DIR* dirp) {
38   return closedir(dirp);
39 }
40 
FAdvise(int fd,int64_t offset,int64_t len,int advice)41 int Syscall::FAdvise(int fd, int64_t offset, int64_t len, int advice) {
42   return posix_fadvise64(fd, offset, len, advice);
43 }
44 
FAllocate(int fd,int mode,int64_t offset,int64_t len)45 int Syscall::FAllocate(int fd, int mode, int64_t offset, int64_t len) {
46   return fallocate64(fd, mode, offset, len);
47 }
48 
FTruncate(int fd,int64_t length)49 int Syscall::FTruncate(int fd, int64_t length) {
50   return ftruncate64(fd, length);
51 }
52 
FStat(int filedes,struct stat64 * buf)53 int Syscall::FStat(int filedes, struct stat64* buf) {
54   return fstat64(filedes, buf);
55 }
56 
FSync(int fd)57 int Syscall::FSync(int fd) {
58   return fsync(fd);
59 }
60 
GetTid()61 pid_t Syscall::GetTid() {
62   long ret = syscall(SYS_gettid);
63   if (ret == -1) {
64     PLOGF("Error calling syscall(SYS_gettid)");
65   }
66   return ret;
67 }
68 
Open(const std::string & path_name,int flags,int mode)69 int Syscall::Open(const std::string& path_name, int flags, int mode) {
70   return open(path_name.c_str(), flags, mode);
71 }
72 
OpenDir(const std::string & name)73 DIR* Syscall::OpenDir(const std::string& name) {
74   return opendir(name.c_str());
75 }
76 
Read(int fd,char * buf,int64_t count,int64_t offset)77 int64_t Syscall::Read(int fd, char* buf, int64_t count, int64_t offset) {
78   return pread64(fd, buf, count, offset);
79 }
80 
ReadDir(DIR * dirp)81 struct dirent* Syscall::ReadDir(DIR* dirp) {
82   return readdir(dirp);
83 }
84 
ReadLink(const std::string & path_name,char * buf,int64_t bufsiz)85 int64_t Syscall::ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) {
86   return readlink(path_name.c_str(), buf, bufsiz);
87 }
88 
89 #ifndef __NR_sched_setattr
90 
91 /* Define all the __NR_sched_setattr syscall numbers for every architecture */
92 
93 #ifdef __x86_64__
94 #define __NR_sched_setattr 314
95 #endif
96 
97 #ifdef __i386__
98 #define __NR_sched_setattr 351
99 #endif
100 
101 #ifdef __arm__
102 #define __NR_sched_setattr 380
103 #endif
104 
105 /* If none of the architecture above have been matched, then use the
106  * asm-generic/unistd.h definition 274, which also matches the aarch64
107  * definition of __NR_sched_setattr. */
108 #ifndef __NR_sched_setattr
109 #define __NR_sched_setattr 274
110 #endif
111 
112 #else /* __NR_sched_setattr */
113 
114 /* Make sure the __NR_sched_setattr syscall numbers are consistent with the
115 Linux implementation */
116 
117 #if ((defined(__x86_64__) && __NR_sched_setattr != 314) || \
118      (defined(__i386__) && __NR_sched_setattr != 351) ||   \
119      (defined(__arm__) && __NR_sched_setattr != 380)) &&   \
120     __NR_sched_setattr != 274 /* aarch64 and asm-generic/unistd.h */
121 #error "Wrong definition of __NR_sched_setattr"
122 #endif
123 
124 #endif /* __NR_sched_setattr */
125 
SchedSetattr(pid_t pid,const SchedAttr__ & attr,unsigned int flags)126 int Syscall::SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) {
127   long ret = syscall(__NR_sched_setattr, pid, &attr, flags);
128   if (ret == -1) {
129     PLOGF("Error calling syscall(__NR_sched_setattr)");
130   }
131   return ret;
132 }
133 
Sync()134 void Syscall::Sync() {
135   return sync();
136 }
137 
Unlink(const std::string & path_name)138 int Syscall::Unlink(const std::string& path_name) {
139   return unlink(path_name.c_str());
140 }
141 
Write(int fd,char * buf,int64_t count,int64_t offset)142 int64_t Syscall::Write(int fd, char* buf, int64_t count, int64_t offset) {
143   return pwrite64(fd, buf, count, offset);
144 }
145 
to_string(const SchedAttr__ & attr)146 std::string to_string(const SchedAttr__& attr) {
147   std::stringstream ss;
148   ss << "size: " << attr.size << ", policy: " << attr.sched_policy
149      << ", flags: " << attr.sched_flags << ", nice: " << attr.sched_nice
150      << ", priority: " << attr.sched_priority << ", runtime: " << attr.sched_runtime
151      << ", deadline: " << attr.sched_deadline << ", period: " << attr.sched_period;
152   return ss.str();
153 }
154 
155 }  // namespace dittosuite
156