syscalls: Add a bunch of missing system calls.

readlink, umask, truncate, ftruncate, mkdir, and getcwd.
This commit is contained in:
Michael Adler
2008-07-23 14:41:33 -07:00
parent 8c4f18f6f5
commit 2cd04fd6da
8 changed files with 121 additions and 20 deletions

View File

@@ -303,6 +303,8 @@ class LiveProcess : public Process
return full + filename;
}
std::string getcwd() const { return cwd; }
virtual void syscall(int64_t callnum, ThreadContext *tc);
virtual SyscallDesc* getDesc(int callnum) = 0;

View File

@@ -239,6 +239,59 @@ gethostnameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
return 0;
}
SyscallReturn
getcwdFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{
int result = 0;
unsigned long size = tc->getSyscallArg(1);
BufferArg buf(tc->getSyscallArg(0), size);
// Is current working directory defined?
string cwd = p->getcwd();
if (!cwd.empty()) {
if (cwd.length() >= size) {
// Buffer too small
return -ERANGE;
}
strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
result = cwd.length();
}
else {
if (getcwd((char *)buf.bufferPtr(), size) != NULL) {
result = strlen((char *)buf.bufferPtr());
}
else {
result = -1;
}
}
buf.copyOut(tc->getMemPort());
return (result == -1) ? -errno : result;
}
SyscallReturn
readlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{
string path;
if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
return (TheISA::IntReg)-EFAULT;
// Adjust path for current working directory
path = p->fullPath(path);
size_t bufsiz = tc->getSyscallArg(2);
BufferArg buf(tc->getSyscallArg(1), bufsiz);
int result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
buf.copyOut(tc->getMemPort());
return (result == -1) ? -errno : result;
}
SyscallReturn
unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{
@@ -254,6 +307,24 @@ unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
return (result == -1) ? -errno : result;
}
SyscallReturn
mkdirFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{
string path;
if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
return (TheISA::IntReg)-EFAULT;
// Adjust path for current working directory
path = p->fullPath(path);
mode_t mode = tc->getSyscallArg(1);
int result = mkdir(path.c_str(), mode);
return (result == -1) ? -errno : result;
}
SyscallReturn
renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{
@@ -306,6 +377,17 @@ ftruncateFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *t
return (result == -1) ? -errno : result;
}
SyscallReturn
umaskFunc(SyscallDesc *desc, int num, LiveProcess *process, ThreadContext *tc)
{
// Letting the simulated program change the simulator's umask seems like
// a bad idea. Compromise by just returning the current umask but not
// changing anything.
mode_t oldMask = umask(0);
umask(oldMask);
return oldMask;
}
SyscallReturn
chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
{

View File

@@ -223,10 +223,22 @@ SyscallReturn munmapFunc(SyscallDesc *desc, int num,
SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target getcwd() handler.
SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target unlink() handler.
SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target unlink() handler.
SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target mkdir() handler.
SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target rename() handler.
SyscallReturn renameFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
@@ -242,6 +254,11 @@ SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target umask() handler.
SyscallReturn umaskFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);
/// Target chown() handler.
SyscallReturn chownFunc(SyscallDesc *desc, int num,
LiveProcess *p, ThreadContext *tc);