From 31d829d388824c6795009afa55610ea5f5a22b0c Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Thu, 16 Nov 2006 12:43:11 -0800 Subject: Implement current working directory for LiveProcesses --HG-- extra : convert_revision : a2d3cf29ab65c61af27d82a8c421a41a19fd5aeb --- src/sim/process.cc | 18 +++++++++++------- src/sim/process.hh | 17 +++++++++++++++++ src/sim/syscall_emul.cc | 13 +++++++++++++ src/sim/syscall_emul.hh | 22 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) (limited to 'src/sim') diff --git a/src/sim/process.cc b/src/sim/process.cc index f3e289d41..b43fa7d00 100644 --- a/src/sim/process.cc +++ b/src/sim/process.cc @@ -314,11 +314,12 @@ LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, System *_system, int stdin_fd, int stdout_fd, int stderr_fd, vector &_argv, vector &_envp, + const string &_cwd, uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, uint64_t _pid, uint64_t _ppid) : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), - objFile(_objFile), argv(_argv), envp(_envp) + objFile(_objFile), argv(_argv), envp(_envp), cwd(_cwd) { __uid = _uid; __euid = _euid; @@ -427,6 +428,7 @@ LiveProcess::create(const std::string &nm, System *system, int stdin_fd, int stdout_fd, int stderr_fd, std::string executable, std::vector &argv, std::vector &envp, + const std::string &cwd, uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, uint64_t _pid, uint64_t _ppid) @@ -445,14 +447,14 @@ LiveProcess::create(const std::string &nm, System *system, int stdin_fd, case ObjectFile::Tru64: process = new AlphaTru64Process(nm, objFile, system, stdin_fd, stdout_fd, stderr_fd, - argv, envp, + argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid); break; case ObjectFile::Linux: process = new AlphaLinuxProcess(nm, objFile, system, stdin_fd, stdout_fd, stderr_fd, - argv, envp, + argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid); break; @@ -466,7 +468,7 @@ LiveProcess::create(const std::string &nm, System *system, int stdin_fd, case ObjectFile::Linux: process = new SparcLinuxProcess(nm, objFile, system, stdin_fd, stdout_fd, stderr_fd, - argv, envp, + argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid); break; @@ -474,7 +476,7 @@ LiveProcess::create(const std::string &nm, System *system, int stdin_fd, case ObjectFile::Solaris: process = new SparcSolarisProcess(nm, objFile, system, stdin_fd, stdout_fd, stderr_fd, - argv, envp, + argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid); break; default: @@ -487,7 +489,7 @@ LiveProcess::create(const std::string &nm, System *system, int stdin_fd, case ObjectFile::Linux: process = new MipsLinuxProcess(nm, objFile, system, stdin_fd, stdout_fd, stderr_fd, - argv, envp, + argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid); break; @@ -512,6 +514,7 @@ BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) Param input; Param output; VectorParam env; + Param cwd; SimObjectParam system; Param uid; Param euid; @@ -530,6 +533,7 @@ BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), INIT_PARAM(env, "environment settings"), + INIT_PARAM(cwd, "current working directory"), INIT_PARAM(system, "system"), INIT_PARAM(uid, "user id"), INIT_PARAM(euid, "effective user id"), @@ -566,7 +570,7 @@ CREATE_SIM_OBJECT(LiveProcess) return LiveProcess::create(getInstanceName(), system, stdin_fd, stdout_fd, stderr_fd, (string)executable == "" ? cmd[0] : executable, - cmd, env, + cmd, env, cwd, uid, euid, gid, egid, pid, ppid); } diff --git a/src/sim/process.hh b/src/sim/process.hh index 5c37f725e..616c02c00 100644 --- a/src/sim/process.hh +++ b/src/sim/process.hh @@ -177,11 +177,13 @@ class LiveProcess : public Process ObjectFile *objFile; std::vector argv; std::vector envp; + std::string cwd; LiveProcess(const std::string &nm, ObjectFile *objFile, System *_system, int stdin_fd, int stdout_fd, int stderr_fd, std::vector &argv, std::vector &envp, + const std::string &cwd, uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, uint64_t _pid, uint64_t _ppid); @@ -207,6 +209,20 @@ class LiveProcess : public Process inline uint64_t pid() {return __pid;} inline uint64_t ppid() {return __ppid;} + std::string + fullPath(const std::string &filename) + { + if (filename[0] == '/' || cwd.empty()) + return filename; + + std::string full = cwd; + + if (cwd[cwd.size() - 1] != '/') + full += '/'; + + return full + filename; + } + virtual void syscall(int64_t callnum, ThreadContext *tc); virtual SyscallDesc* getDesc(int callnum) = 0; @@ -220,6 +236,7 @@ class LiveProcess : public Process std::string executable, std::vector &argv, std::vector &envp, + const std::string &cwd, uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, uint64_t _pid, uint64_t _ppid); diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index 9028d590b..ab44c0a35 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -214,6 +214,9 @@ unlinkFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) return (TheISA::IntReg)-EFAULT; + // Adjust path for current working directory + path = p->fullPath(path); + int result = unlink(path.c_str()); return (result == -1) ? -errno : result; } @@ -231,6 +234,10 @@ renameFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1))) return -EFAULT; + // Adjust path for current working directory + old_name = p->fullPath(old_name); + new_name = p->fullPath(new_name); + int64_t result = rename(old_name.c_str(), new_name.c_str()); return (result == -1) ? -errno : result; } @@ -245,6 +252,9 @@ truncateFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) off_t length = tc->getSyscallArg(1); + // Adjust path for current working directory + path = p->fullPath(path); + int result = truncate(path.c_str(), length); return (result == -1) ? -errno : result; } @@ -277,6 +287,9 @@ chownFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) uint32_t group = tc->getSyscallArg(2); gid_t hostGroup = group; + // Adjust path for current working directory + path = p->fullPath(path); + int result = chown(path.c_str(), hostOwner, hostGroup); return (result == -1) ? -errno : result; } diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh index e79712a19..07689ef06 100644 --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -500,6 +500,9 @@ openFunc(SyscallDesc *desc, int callnum, LiveProcess *process, hostFlags |= O_BINARY; #endif + // Adjust path for current working directory + path = process->fullPath(path); + DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); // open the file @@ -526,6 +529,9 @@ chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, // XXX translate mode flags via OS::something??? hostMode = mode; + // Adjust path for current working directory + path = process->fullPath(path); + // do the chmod int result = chmod(path.c_str(), hostMode); if (result < 0) @@ -573,6 +579,9 @@ statFunc(SyscallDesc *desc, int callnum, LiveProcess *process, if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) return -EFAULT; + // Adjust path for current working directory + path = process->fullPath(path); + struct stat hostBuf; int result = stat(path.c_str(), &hostBuf); @@ -626,6 +635,9 @@ lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) return -EFAULT; + // Adjust path for current working directory + path = process->fullPath(path); + struct stat hostBuf; int result = lstat(path.c_str(), &hostBuf); @@ -648,6 +660,9 @@ lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) return -EFAULT; + // Adjust path for current working directory + path = process->fullPath(path); + #if NO_STAT64 struct stat hostBuf; int result = lstat(path.c_str(), &hostBuf); @@ -701,6 +716,9 @@ statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) return -EFAULT; + // Adjust path for current working directory + path = process->fullPath(path); + struct statfs hostBuf; int result = statfs(path.c_str(), &hostBuf); @@ -896,6 +914,10 @@ utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); } + + // Adjust path for current working directory + path = process->fullPath(path); + int result = utimes(path.c_str(), hostTimeval); if (result < 0) -- cgit v1.2.3