feature: Add syscall wrappers for pidfdopen and pidfdgetfd

Related-To: NEO-12952

Signed-off-by: Slawomir Milczarek <slawomir.milczarek@intel.com>
This commit is contained in:
Slawomir Milczarek
2025-04-17 11:43:14 +00:00
committed by Compute-Runtime-Automation
parent 3596522637
commit a2d25ca31f
4 changed files with 43 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
#include <poll.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
namespace NEO {
namespace SysCalls {
@@ -56,5 +57,7 @@ int closedir(DIR *dir);
off_t lseek(int fd, off_t offset, int whence) noexcept;
long sysconf(int name);
int mkfifo(const char *pathname, mode_t mode);
int pidfdopen(pid_t pid, unsigned int flags);
int pidfdgetfd(int pidfd, int targetfd, unsigned int flags);
} // namespace SysCalls
} // namespace NEO

View File

@@ -18,10 +18,15 @@
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef SYS_pidfd_getfd
#define SYS_pidfd_getfd 438
#endif
namespace NEO {
namespace SysCalls {
@@ -184,6 +189,16 @@ int closedir(DIR *dir) {
return ::closedir(dir);
}
int pidfdopen(pid_t pid, unsigned int flags) {
long retval = ::syscall(SYS_pidfd_open, pid, flags);
return static_cast<int>(retval);
}
int pidfdgetfd(int pidfd, int targetfd, unsigned int flags) {
long retval = ::syscall(SYS_pidfd_getfd, pidfd, targetfd, flags);
return static_cast<int>(retval);
}
off_t lseek(int fd, off_t offset, int whence) noexcept {
return ::lseek(fd, offset, whence);
}

View File

@@ -66,6 +66,8 @@ int flockCalled = 0;
int opendirCalled = 0;
int readdirCalled = 0;
int closedirCalled = 0;
int pidfdopenCalled = 0;
int pidfdgetfdCalled = 0;
int fsyncCalled = 0;
int fsyncArgPassed = 0;
int fsyncRetVal = 0;
@@ -112,6 +114,8 @@ DIR *(*sysCallsOpendir)(const char *name) = nullptr;
struct dirent *(*sysCallsReaddir)(DIR *dir) = nullptr;
int (*sysCallsClosedir)(DIR *dir) = nullptr;
int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize) = nullptr;
int (*sysCallsPidfdOpen)(pid_t pid, unsigned int flags) = nullptr;
int (*sysCallsPidfdGetfd)(int pidfd, int fd, unsigned int flags) = nullptr;
off_t lseekReturn = 4096u;
std::atomic<int> lseekCalledCount(0);
long sysconfReturn = 1ull << 30;
@@ -536,5 +540,21 @@ int mkfifo(const char *pathname, mode_t mode) {
return 0;
}
int pidfdopen(pid_t pid, unsigned int flags) {
pidfdopenCalled++;
if (sysCallsPidfdOpen != nullptr) {
return sysCallsPidfdOpen(pid, flags);
}
return 0;
}
int pidfdgetfd(int pid, int targetfd, unsigned int flags) {
pidfdgetfdCalled++;
if (sysCallsPidfdGetfd != nullptr) {
return sysCallsPidfdGetfd(pid, targetfd, flags);
}
return 0;
}
} // namespace SysCalls
} // namespace NEO

View File

@@ -13,6 +13,7 @@
#include <iostream>
#include <poll.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
namespace NEO {
@@ -49,6 +50,8 @@ extern struct dirent *(*sysCallsReaddir)(DIR *dir);
extern int (*sysCallsClosedir)(DIR *dir);
extern int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize);
extern int (*sysCallsClose)(int fileDescriptor);
extern int (*sysCallsPidfdOpen)(pid_t pid, unsigned int flags);
extern int (*sysCallsPidfdGetfd)(int pidfd, int fd, unsigned int flags);
extern bool allowFakeDevicePath;
extern int flockRetVal;
@@ -83,6 +86,8 @@ extern bool failFcntl;
extern bool failFcntl1;
extern bool failAccess;
extern int setErrno;
extern int pidfdopenCalled;
extern int pidfdgetfdCalled;
extern std::vector<void *> mmapVector;
extern std::vector<void *> mmapCapturedExtendedPointers;