mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 19:45:08 +00:00

This is dirty but pretty cool! If we have a pending, unmasked signal for a process that's blocked inside the kernel, we set up alternate stacks for that process and unblock it to execute the signal handler. A slightly different return trampoline is used here: since we need to get back into the kernel, a dedicated syscall is used (sys$sigreturn.) This restores the TSS contents of the process to the state it was in while we were originally blocking in the kernel. NOTE: There's currently only one "kernel resume TSS" so signal nesting definitely won't work.
47 lines
1 KiB
C++
47 lines
1 KiB
C++
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <AK/String.h>
|
|
|
|
static unsigned parseUInt(const String& str, bool& ok)
|
|
{
|
|
unsigned value = 0;
|
|
for (size_t i = 0; i < str.length(); ++i) {
|
|
if (str[i] < '0' || str[i] > '9') {
|
|
ok = false;
|
|
return 0;
|
|
}
|
|
value = value * 10;
|
|
value += str[i] - '0';
|
|
}
|
|
ok = true;
|
|
return value;
|
|
}
|
|
|
|
void handle_sigint(int)
|
|
{
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc != 2) {
|
|
printf("usage: sleep <seconds>\n");
|
|
return 1;
|
|
}
|
|
bool ok;
|
|
unsigned secs = parseUInt(argv[1], ok);
|
|
if (!ok) {
|
|
fprintf(stderr, "Not a valid number of seconds: \"%s\"\n", argv[1]);
|
|
return 1;
|
|
}
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(struct sigaction));
|
|
sa.sa_handler = handle_sigint;
|
|
sigaction(SIGINT, &sa, nullptr);
|
|
unsigned remaining = sleep(secs);
|
|
if (remaining) {
|
|
printf("Sleep interrupted with %u seconds remaining.\n", remaining);
|
|
}
|
|
return 0;
|
|
}
|
|
|