xref: /aosp_15_r20/test/dittosuite/src/syscall.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
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 
SchedSetattr(pid_t pid,const SchedAttr__ & attr,unsigned int flags)89 int Syscall::SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) {
90   long ret = syscall(__NR_sched_setattr, pid, &attr, flags);
91   if (ret == -1) {
92     PLOGF("Error calling syscall(__NR_sched_setattr)");
93   }
94   return ret;
95 }
96 
Sync()97 void Syscall::Sync() {
98   return sync();
99 }
100 
Unlink(const std::string & path_name)101 int Syscall::Unlink(const std::string& path_name) {
102   return unlink(path_name.c_str());
103 }
104 
Write(int fd,char * buf,int64_t count,int64_t offset)105 int64_t Syscall::Write(int fd, char* buf, int64_t count, int64_t offset) {
106   return pwrite64(fd, buf, count, offset);
107 }
108 
LockMutex(pthread_mutex_t * mutex)109 int Syscall::LockMutex(pthread_mutex_t* mutex) {
110   return pthread_mutex_lock(mutex);
111 }
112 
UnlockMutex(pthread_mutex_t * mutex)113 int Syscall::UnlockMutex(pthread_mutex_t* mutex) {
114   return pthread_mutex_unlock(mutex);
115 }
116 
to_string(const SchedAttr__ & attr)117 std::string to_string(const SchedAttr__& attr) {
118   std::stringstream ss;
119   ss << "size: " << attr.size << ", policy: " << attr.sched_policy
120      << ", flags: " << attr.sched_flags << ", nice: " << attr.sched_nice
121      << ", priority: " << attr.sched_priority << ", runtime: " << attr.sched_runtime
122      << ", deadline: " << attr.sched_deadline << ", period: " << attr.sched_period;
123   return ss.str();
124 }
125 
126 }  // namespace dittosuite
127