Job control, function keys, and real sleeps
- SIGSTOP/SIGTSTP/SIGTTIN/SIGTTOU stop, SIGCONT resumes; the parent gets SIGCHLD and wait4 reports WUNTRACED/WCONTINUED; ps shows T. Ctrl-Z works, and mc no longer hangs waiting for its subshell to stop. - Blocking reads (console, pty, pipe) and sleeps take a pending stop and carry on after SIGCONT; a caught signal without SA_RESTART ends them with EINTR. - nanosleep reads tv_nsec (sleep 0.5 used to return at once), writes the remainder, and clock_nanosleep understands TIMER_ABSTIME. - rt_sigaction uses the k_sigaction layout for Linux-ABI processes: the old action's restorer was written 112 bytes past the caller's struct. Flags are kept; SIGKILL and SIGSTOP cannot be caught. - F1..F12 reach programs, as the Linux console sends them (PS/2, USB, I2C). - TIOCLINUX subcode 6 answers "no modifiers" instead of echoing the 6 back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
parent
fd9d392416
commit
2372806862
13 changed files with 366 additions and 42 deletions
|
|
@ -1653,6 +1653,20 @@ addr_t sys_ioctl(void)
|
|||
// terminal and nothing to choose. It records the choice now, which is what
|
||||
// /dev/tty resolves through and what a session leader on tty3 needs so
|
||||
// that its /dev/tty is tty3 and not whatever is on screen.
|
||||
// TIOCLINUX. Only subcode 6, the modifier state, is answered -- with
|
||||
// "none held", which is true often enough. mc asks it after every key on
|
||||
// a Linux console, and the ioctl used to succeed without writing: the 6
|
||||
// it passed in came back as Ctrl+AltGr held, and Enter arrived as an
|
||||
// unknown chord printed as a dot.
|
||||
if (request == 0x541C) {
|
||||
uchar sub;
|
||||
if (copy_from_user(&sub, argp, 1) < 0)
|
||||
return -EFAULT;
|
||||
if (sub != 6)
|
||||
return -EINVAL;
|
||||
sub = 0;
|
||||
return copy_to_user(argp, &sub, 1) < 0 ? -EFAULT : 0;
|
||||
}
|
||||
if (request == TIOCSCTTY) {
|
||||
struct vt *v = vt_for_minor(f->ip->minor);
|
||||
myproc()->ctty = vt_index(v) + 1;
|
||||
|
|
|
|||
|
|
@ -79,13 +79,18 @@ sys_sigwait(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// musl's struct sigaction (musl 1.2.5 signal.h) for this target:
|
||||
// sa_handler (8 bytes) at offset 0, then a 128-byte sigset_t (musl uses
|
||||
// 128/sizeof(long) = 16 longs regardless of NSIG), sa_flags (4 bytes) +
|
||||
// padding, then sa_restorer (8 bytes) at offset 144. We only care about
|
||||
// those two 8-byte fields.
|
||||
// The native musl hands over its own struct sigaction (musl 1.2.5 signal.h):
|
||||
// sa_handler at 0, a 128-byte sigset_t, sa_flags (4 bytes) at 136, then
|
||||
// sa_restorer at 144.
|
||||
//
|
||||
// A Linux-ABI process does not. Its musl converts to the kernel's
|
||||
// k_sigaction first -- handler at 0, flags at 8, restorer at 16, mask at 24,
|
||||
// thirty-two bytes in all -- and this used the native offsets for both, so
|
||||
// every Alpine program that asked for the old action had 8 bytes written 112
|
||||
// bytes past the end of the struct, into whatever its stack held there.
|
||||
#define SA_HANDLER_OFF 0
|
||||
#define SA_RESTORER_OFF 144
|
||||
#define SA_FLAGS_OFF(p) ((p)->syscall_map ? 8 : 136)
|
||||
#define SA_RESTORER_OFF(p) ((p)->syscall_map ? 16 : 144)
|
||||
|
||||
// rt_sigaction: store the handler + restorer so deliver_signals() (called
|
||||
// from syscall()'s tail, kernel/sys/syscall.c) can actually jump to user
|
||||
|
|
@ -108,15 +113,30 @@ sys_rt_sigaction(void)
|
|||
argaddr(2, &oldactp);
|
||||
if (sig <= 0 || sig >= 32) return -1;
|
||||
|
||||
struct proc *p = myproc();
|
||||
if (oldactp) {
|
||||
put_user(myproc()->sig_handler[sig], oldactp + SA_HANDLER_OFF);
|
||||
put_user(myproc()->sig_restorer[sig], oldactp + SA_RESTORER_OFF);
|
||||
put_user(p->sig_handler[sig], oldactp + SA_HANDLER_OFF);
|
||||
put_user(p->sig_restorer[sig], oldactp + SA_RESTORER_OFF(p));
|
||||
if (p->syscall_map) {
|
||||
put_user(p->sig_flags[sig], oldactp + SA_FLAGS_OFF(p));
|
||||
} else {
|
||||
uint32 fl = (uint32)p->sig_flags[sig];
|
||||
put_user(fl, oldactp + SA_FLAGS_OFF(p));
|
||||
}
|
||||
}
|
||||
if (actp) {
|
||||
copy_from_user(&myproc()->sig_handler[sig], actp + SA_HANDLER_OFF,
|
||||
sizeof(myproc()->sig_handler[sig]));
|
||||
copy_from_user(&myproc()->sig_restorer[sig], actp + SA_RESTORER_OFF,
|
||||
sizeof(myproc()->sig_restorer[sig]));
|
||||
uint64 h, r, fl = 0;
|
||||
if (copy_from_user(&h, actp + SA_HANDLER_OFF, sizeof(h)) < 0 ||
|
||||
copy_from_user(&r, actp + SA_RESTORER_OFF(p), sizeof(r)) < 0 ||
|
||||
copy_from_user(&fl, actp + SA_FLAGS_OFF(p),
|
||||
p->syscall_map ? 8 : 4) < 0)
|
||||
return -EFAULT;
|
||||
// SIGKILL and SIGSTOP cannot be caught or ignored.
|
||||
if (sig == SIGKILL || sig == SIGSTOP)
|
||||
return -EINVAL;
|
||||
p->sig_handler[sig] = h;
|
||||
p->sig_restorer[sig] = r;
|
||||
p->sig_flags[sig] = fl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -195,6 +215,16 @@ deliver_signals(struct trapframe *tf)
|
|||
// Except for a signal that was blocked when it arrived: sigsend()
|
||||
// left it pending without acting, and now that it is deliverable its
|
||||
// default action happens here.
|
||||
// SIGSTOP cannot be caught or ignored; the other three stop only by
|
||||
// default. Stopped here and resumed here, by SIGCONT or SIGKILL.
|
||||
if (sig == SIGSTOP ||
|
||||
(sig_default_stops(sig) && myproc()->sig_handler[sig] == 0)) {
|
||||
myproc()->pending_signals &= ~(1ULL << sig);
|
||||
proc_stop(sig);
|
||||
if (myproc()->killed)
|
||||
exit(W_EXITCODE(0, myproc()->killsig ? myproc()->killsig : SIGKILL));
|
||||
continue;
|
||||
}
|
||||
if (myproc()->sig_handler[sig] <= 1) {
|
||||
myproc()->pending_signals &= ~(1ULL << sig);
|
||||
if (myproc()->sig_handler[sig] == 0 && sig_default_kills(sig)) {
|
||||
|
|
|
|||
|
|
@ -60,23 +60,65 @@ sys_gettimeofday(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Sleep for a timespec, in ticks, rounded up.
|
||||
//
|
||||
// Only tv_sec was ever read: `sleep 0.5` returned at once and every
|
||||
// sub-second sleep -- cmatrix's frame delay, a game loop's -- was no sleep at
|
||||
// all. And a signal could not end one: Ctrl-Z on `sleep 100` did nothing.
|
||||
static addr_t
|
||||
sleep_ticks(uint64 want, addr_t remaddr)
|
||||
{
|
||||
struct proc *p = myproc();
|
||||
acquire(&tickslock);
|
||||
uint t0 = ticks;
|
||||
while ((uint64)(uint)(ticks - t0) < want) {
|
||||
if (p->killed || sig_attention(p)) {
|
||||
release(&tickslock);
|
||||
int e = p->killed ? -EINTR : sig_blocking_check();
|
||||
if (e) {
|
||||
if (remaddr) {
|
||||
uint64 left = want - (uint)(ticks - t0);
|
||||
struct timespec r = { (long)(left / HZ),
|
||||
(long)((left % HZ) * (1000000000 / HZ)) };
|
||||
copy_to_user(remaddr, &r, sizeof(r));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
acquire(&tickslock);
|
||||
continue;
|
||||
}
|
||||
sleep(&ticks, &tickslock);
|
||||
}
|
||||
release(&tickslock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
timespec_ticks(const struct timespec *ts, uint64 *out)
|
||||
{
|
||||
if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
|
||||
return -EINVAL;
|
||||
uint64 sec = (uint64)ts->tv_sec;
|
||||
if (sec > 0xFFFFFFFFULL / HZ)
|
||||
sec = 0xFFFFFFFFULL / HZ; // a few years: forever enough
|
||||
*out = sec * HZ + ((uint64)ts->tv_nsec * HZ + 999999999) / 1000000000;
|
||||
return 0;
|
||||
}
|
||||
|
||||
addr_t
|
||||
sys_nanosleep(void)
|
||||
{
|
||||
addr_t tsaddr, raw;
|
||||
if (argaddr(0, &tsaddr) < 0 || fetchaddr(tsaddr, &raw) < 0)
|
||||
return -1;
|
||||
int64 sec = (int64)raw;
|
||||
if (sec > 0) {
|
||||
acquire(&tickslock);
|
||||
uint t0 = ticks;
|
||||
while (ticks - t0 < (uint)(sec * HZ)) {
|
||||
if (myproc()->killed) { release(&tickslock); return -EINTR; }
|
||||
sleep(&ticks, &tickslock);
|
||||
}
|
||||
release(&tickslock);
|
||||
}
|
||||
return 0;
|
||||
addr_t tsaddr, remaddr;
|
||||
struct timespec ts;
|
||||
uint64 want;
|
||||
argaddr(0, &tsaddr);
|
||||
argaddr(1, &remaddr);
|
||||
if (copy_from_user(&ts, tsaddr, sizeof(ts)) < 0)
|
||||
return -EFAULT;
|
||||
int e = timespec_ticks(&ts, &want);
|
||||
if (e)
|
||||
return e;
|
||||
return sleep_ticks(want, remaddr);
|
||||
}
|
||||
|
||||
addr_t
|
||||
|
|
@ -173,20 +215,34 @@ sys_clock_getres(void)
|
|||
addr_t
|
||||
sys_clock_nanosleep(void)
|
||||
{
|
||||
// Treat the same as nanosleep: arg2 is the timespec, arg1 is flags.
|
||||
// Linux: clock_nanosleep(clockid, flags, rqtp, rmtp)
|
||||
addr_t tsaddr, raw;
|
||||
if (argaddr(2, &tsaddr) < 0 || fetchaddr(tsaddr, &raw) < 0)
|
||||
return -1;
|
||||
int64 sec = (int64)raw;
|
||||
if (sec > 0) {
|
||||
acquire(&tickslock);
|
||||
uint t0 = ticks;
|
||||
while (ticks - t0 < (uint)(sec * HZ)) {
|
||||
if (myproc()->killed) { release(&tickslock); return -EINTR; }
|
||||
sleep(&ticks, &tickslock);
|
||||
}
|
||||
release(&tickslock);
|
||||
// clock_nanosleep(clockid, flags, rqtp, rmtp). TIMER_ABSTIME (1) makes
|
||||
// rqtp a time on that clock rather than an interval.
|
||||
int clkid, flags;
|
||||
addr_t tsaddr, remaddr;
|
||||
struct timespec ts;
|
||||
uint64 want;
|
||||
argint(0, &clkid);
|
||||
argint(1, &flags);
|
||||
argaddr(2, &tsaddr);
|
||||
argaddr(3, &remaddr);
|
||||
if (copy_from_user(&ts, tsaddr, sizeof(ts)) < 0)
|
||||
return -EFAULT;
|
||||
if (flags & 1) {
|
||||
uint64 sec, usec;
|
||||
wallclock(&sec, &usec);
|
||||
if (clkid == 1)
|
||||
sec -= boot_epoch;
|
||||
long dsec = ts.tv_sec - (long)sec;
|
||||
long dns = ts.tv_nsec - (long)(usec * 1000);
|
||||
if (dns < 0) { dns += 1000000000; dsec--; }
|
||||
if (dsec < 0)
|
||||
return 0; // already past
|
||||
ts.tv_sec = dsec;
|
||||
ts.tv_nsec = dns;
|
||||
remaddr = 0; // never written for an absolute sleep
|
||||
}
|
||||
return 0;
|
||||
int e = timespec_ticks(&ts, &want);
|
||||
if (e)
|
||||
return e;
|
||||
return sleep_ticks(want, remaddr);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue