# Node ID 2f4d3f563e761f6c6329767b1e9c15b7f295aee1 # Parent 885f9c39f4ce5572d3035636a82db99302a27860 diff --git a/src/arch/x86/linux/process.cc b/src/arch/x86/linux/process.cc --- a/src/arch/x86/linux/process.cc +++ b/src/arch/x86/linux/process.cc @@ -598,7 +598,7 @@ /* 52 */ SyscallDesc("umount2", unimplementedFunc), /* 53 */ SyscallDesc("lock", unimplementedFunc), /* 54 */ SyscallDesc("ioctl", ioctlFunc), - /* 55 */ SyscallDesc("fcntl", unimplementedFunc), + /* 55 */ SyscallDesc("fcntl", fcntlFunc), /* 56 */ SyscallDesc("mpx", unimplementedFunc), /* 57 */ SyscallDesc("setpgid", unimplementedFunc), /* 58 */ SyscallDesc("ulimit", unimplementedFunc), diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -220,7 +220,7 @@ SyscallReturn dupFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc); -/// Target fnctl() handler. +/// Target fcntl() handler. SyscallReturn fcntlFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc); diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -646,12 +646,13 @@ return (result == -1) ? -local_errno : process->fds.allocFD(new_fdp); } - SyscallReturn fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) { + int arg; int index = 0; int tgt_fd = p->getSyscallArg(tc, index); + int cmd = p->getSyscallArg(tc, index); FDEntryPtr fdp = p->fds[tgt_fd]; @@ -662,35 +663,33 @@ if (fdp == nullptr || (sim_fd = fdp->getSimFD()) < 0) return -EBADF; - int cmd = p->getSyscallArg(tc, index); + int coe = fdp->getCOE(); + switch (cmd) { - case 0: // F_DUPFD - // if we really wanted to support this, we'd need to do it - // in the target fd space. - warn("fcntl(%d, F_DUPFD) not supported, error returned\n", tgt_fd); - return -EMFILE; + case F_GETFD: + return coe & FD_CLOEXEC; - case 1: // F_GETFD (get close-on-exec flag) - case 2: // F_SETFD (set close-on-exec flag) + case F_SETFD: { + arg = p->getSyscallArg(tc, index); + arg ? fdp->setCOE(true) : fdp->setCOE(false); return 0; + } - case 3: // F_GETFL (get file flags) - case 4: // F_SETFL (set file flags) - // not sure if this is totally valid, but we'll pass it through - // to the underlying OS - warn("fcntl(%d, %d) passed through to host\n", tgt_fd, cmd); - return fcntl(sim_fd, cmd); - // return 0; - - case 7: // F_GETLK (get lock) - case 8: // F_SETLK (set lock) - case 9: // F_SETLKW (set lock and wait) - // don't mess with file locking... just act like it's OK - warn("File lock call (fcntl(%d, %d)) ignored.\n", tgt_fd, cmd); - return 0; + // Rely on the host to maintain the file status flags for this file + // description rather than maintain it ourselves. Admittedly, this + // is suboptimal (and possibly error prone), but it is difficult to + // maintain the flags by tracking them across the different descriptors + // (that refer to this file description) caused by clone, dup, and + // subsequent fcntls. + case F_GETFL: + case F_SETFL: { + arg = p->getSyscallArg(tc, index); + int rv = fcntl(sim_fd, cmd, arg); + return (rv == -1) ? -errno : rv; + } default: - warn("Unknown fcntl command %d\n", cmd); + warn("fcntl: unsupported command %d\n", cmd); return 0; } }