|
| 1 | +/* |
| 2 | + * The Shadow Simulator |
| 3 | + * See LICENSE for licensing information |
| 4 | + */ |
| 5 | + |
| 6 | +#include "main/host/syscall/process.h" |
| 7 | + |
| 8 | +#include <errno.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <sys/syscall.h> |
| 11 | + |
| 12 | +#include "main/host/host.h" |
| 13 | +#include "main/host/syscall/protected.h" |
| 14 | +#include "main/host/thread.h" |
| 15 | +#include "main/utility/syscall.h" |
| 16 | +#include "support/logger/logger.h" |
| 17 | + |
| 18 | +/////////////////////////////////////////////////////////// |
| 19 | +// Helpers |
| 20 | +/////////////////////////////////////////////////////////// |
| 21 | + |
| 22 | +static SysCallReturn _syscallhandler_killHelper(SysCallHandler* sys, pid_t pid, pid_t tid, int sig, |
| 23 | + long syscallnum) { |
| 24 | + pid_t my_tid = thread_getNativeTid(sys->thread); |
| 25 | + |
| 26 | + // Return error if trying to stop/continue a process so we don't disrupt our ptracer. |
| 27 | + // NOTE: If we run into signal problems, we could consider only allowing a process to |
| 28 | + // send signals to itself, i.e., disallow inter-process signaling. See Github PR#1075. |
| 29 | + if (sig == SIGSTOP || sig == SIGCONT) { |
| 30 | + return (SysCallReturn){.state = SYSCALL_DONE, .retval.as_i64 = -ENOSYS}; |
| 31 | + } |
| 32 | + |
| 33 | + debug( |
| 34 | + "making syscall %li in native thread %i (pid=%i and tid=%i)", syscallnum, my_tid, pid, tid); |
| 35 | + |
| 36 | + long result = 0; |
| 37 | + |
| 38 | + switch (syscallnum) { |
| 39 | + case SYS_kill: result = thread_nativeSyscall(sys->thread, SYS_kill, pid, sig); break; |
| 40 | + case SYS_tkill: result = thread_nativeSyscall(sys->thread, SYS_tkill, tid, sig); break; |
| 41 | + case SYS_tgkill: |
| 42 | + result = thread_nativeSyscall(sys->thread, SYS_tgkill, pid, tid, sig); |
| 43 | + break; |
| 44 | + default: error("Invalid syscall number %li given", syscallnum); break; |
| 45 | + } |
| 46 | + |
| 47 | + int error = syscall_rawReturnValueToErrno(result); |
| 48 | + |
| 49 | + debug("native syscall returned error code %i", error); |
| 50 | + |
| 51 | + return (SysCallReturn){.state = SYSCALL_DONE, .retval.as_i64 = -error}; |
| 52 | +} |
| 53 | + |
| 54 | +/////////////////////////////////////////////////////////// |
| 55 | +// System Calls |
| 56 | +/////////////////////////////////////////////////////////// |
| 57 | + |
| 58 | +SysCallReturn syscallhandler_kill(SysCallHandler* sys, const SysCallArgs* args) { |
| 59 | + utility_assert(sys && args); |
| 60 | + pid_t pid = args->args[0].as_i64; |
| 61 | + int sig = args->args[1].as_i64; |
| 62 | + |
| 63 | + debug("kill called on pid %i with signal %i", pid, sig); |
| 64 | + |
| 65 | + pid_t native_pid = 0; |
| 66 | + |
| 67 | + if (pid == -1 || pid == 0) { |
| 68 | + // Special pids do not need translation |
| 69 | + native_pid = pid; |
| 70 | + } else { |
| 71 | + // Translate from virtual to native pid |
| 72 | + // Support -pid for kill |
| 73 | + native_pid = (pid > 0) ? host_getNativeTID(sys->host, pid, 0) |
| 74 | + : -host_getNativeTID(sys->host, -pid, 0); |
| 75 | + |
| 76 | + // If there is no such thread, it's an error |
| 77 | + if (native_pid == 0) { |
| 78 | + return (SysCallReturn){.state = SYSCALL_DONE, .retval.as_i64 = -ESRCH}; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + debug("translated virtual pid %i to native pid %i", pid, native_pid); |
| 83 | + return _syscallhandler_killHelper(sys, native_pid, 0, sig, SYS_kill); |
| 84 | +} |
| 85 | + |
| 86 | +SysCallReturn syscallhandler_tgkill(SysCallHandler* sys, const SysCallArgs* args) { |
| 87 | + utility_assert(sys && args); |
| 88 | + |
| 89 | + pid_t tgid = args->args[0].as_i64; |
| 90 | + pid_t tid = args->args[1].as_i64; |
| 91 | + int sig = args->args[2].as_i64; |
| 92 | + |
| 93 | + debug("tgkill called on tgid %i and tid %i with signal %i", tgid, tid, sig); |
| 94 | + |
| 95 | + // Translate from virtual to native tgid and tid |
| 96 | + pid_t native_tgid = host_getNativeTID(sys->host, tgid, 0); |
| 97 | + pid_t native_tid = host_getNativeTID(sys->host, tgid, tid); |
| 98 | + |
| 99 | + // If there is no such threads it's an error |
| 100 | + if (native_tgid == 0 || native_tid == 0) { |
| 101 | + return (SysCallReturn){.state = SYSCALL_DONE, .retval.as_i64 = -ESRCH}; |
| 102 | + } |
| 103 | + |
| 104 | + debug("translated virtual tgid %i to native tgid %i and virtual tid %i to native tid %i", tgid, |
| 105 | + native_tgid, tid, native_tid); |
| 106 | + return _syscallhandler_killHelper(sys, native_tgid, native_tid, sig, SYS_tgkill); |
| 107 | +} |
| 108 | + |
| 109 | +SysCallReturn syscallhandler_tkill(SysCallHandler* sys, const SysCallArgs* args) { |
| 110 | + utility_assert(sys && args); |
| 111 | + pid_t tid = args->args[0].as_i64; |
| 112 | + int sig = args->args[1].as_i64; |
| 113 | + |
| 114 | + debug("tkill called on tid %i with signal %i", tid, sig); |
| 115 | + |
| 116 | + // Translate from virtual to native tid |
| 117 | + pid_t native_tid = host_getNativeTID(sys->host, 0, tid); |
| 118 | + |
| 119 | + // If there is no such thread it's an error |
| 120 | + if (native_tid == 0) { |
| 121 | + return (SysCallReturn){.state = SYSCALL_DONE, .retval.as_i64 = -ESRCH}; |
| 122 | + } |
| 123 | + |
| 124 | + debug("translated virtual tid %i to native tid %i", tid, native_tid); |
| 125 | + return _syscallhandler_killHelper(sys, 0, native_tid, sig, SYS_tkill); |
| 126 | +} |
0 commit comments