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
|
|
@ -61,6 +61,10 @@ int pciuser_mmio_ok(uint64 phys, uint64 len);
|
||||||
void pciuser_release_all(struct proc*);
|
void pciuser_release_all(struct proc*);
|
||||||
int pciuser_owner_pid(int idx);
|
int pciuser_owner_pid(int idx);
|
||||||
int proc_reincarnate(int status);
|
int proc_reincarnate(int status);
|
||||||
|
int sig_default_stops(int);
|
||||||
|
void proc_stop(int);
|
||||||
|
int sig_attention(struct proc*);
|
||||||
|
int sig_blocking_check(void);
|
||||||
void proc_cputime(uint64 *user, uint64 *sys);
|
void proc_cputime(uint64 *user, uint64 *sys);
|
||||||
// kernel/dev/procfs.c: /proc/strace
|
// kernel/dev/procfs.c: /proc/strace
|
||||||
int strace_wanted(const char *name);
|
int strace_wanted(const char *name);
|
||||||
|
|
|
||||||
|
|
@ -175,6 +175,7 @@ struct procinfo {
|
||||||
char name[16];
|
char name[16];
|
||||||
char exe[64]; // full path, for /proc/<pid>/exe
|
char exe[64]; // full path, for /proc/<pid>/exe
|
||||||
int kthread; // a kernel thread, not a program
|
int kthread; // a kernel thread, not a program
|
||||||
|
int stopped; // stopped by a signal (ps shows T)
|
||||||
};
|
};
|
||||||
|
|
||||||
struct proc {
|
struct proc {
|
||||||
|
|
@ -248,6 +249,7 @@ struct proc {
|
||||||
// support nested signal delivery).
|
// support nested signal delivery).
|
||||||
uint64 sig_handler[32];
|
uint64 sig_handler[32];
|
||||||
uint64 sig_restorer[32];
|
uint64 sig_restorer[32];
|
||||||
|
uint64 sig_flags[32]; // sa_flags; SA_RESTART is the one read
|
||||||
struct trapframe saved_sigtf;
|
struct trapframe saved_sigtf;
|
||||||
int in_signal;
|
int in_signal;
|
||||||
|
|
||||||
|
|
@ -330,6 +332,13 @@ struct proc {
|
||||||
|
|
||||||
// umask(2). Inherited across fork; 022 for the first process.
|
// umask(2). Inherited across fork; 022 for the first process.
|
||||||
uint umask;
|
uint umask;
|
||||||
|
|
||||||
|
// Job control. stopsig is the signal that stopped the process, 0 while it
|
||||||
|
// runs; stop_report and cont_report are what wait(WUNTRACED/WCONTINUED)
|
||||||
|
// has yet to tell the parent.
|
||||||
|
int stopsig;
|
||||||
|
int stop_report;
|
||||||
|
int cont_report;
|
||||||
};
|
};
|
||||||
#define FD_CLOEXEC 1
|
#define FD_CLOEXEC 1
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,9 @@
|
||||||
#define KBD_PGDN (KBD_SPECIAL + 0x07)
|
#define KBD_PGDN (KBD_SPECIAL + 0x07)
|
||||||
#define KBD_INS (KBD_SPECIAL + 0x08)
|
#define KBD_INS (KBD_SPECIAL + 0x08)
|
||||||
#define KBD_DEL (KBD_SPECIAL + 0x09)
|
#define KBD_DEL (KBD_SPECIAL + 0x09)
|
||||||
|
// F1..F12, consecutive: KBD_F1 + n is F(n+1).
|
||||||
|
#define KBD_F1 (KBD_SPECIAL + 0x10)
|
||||||
|
#define KBD_F12 (KBD_SPECIAL + 0x1B)
|
||||||
|
|
||||||
// Switch virtual terminal.
|
// Switch virtual terminal.
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -624,6 +624,21 @@ consoleintr(int (*getc)(void))
|
||||||
case KBD_PGDN: esc = "\033[6~"; break;
|
case KBD_PGDN: esc = "\033[6~"; break;
|
||||||
case KBD_INS: esc = "\033[2~"; break;
|
case KBD_INS: esc = "\033[2~"; break;
|
||||||
case KBD_DEL: esc = "\033[3~"; break;
|
case KBD_DEL: esc = "\033[3~"; break;
|
||||||
|
// The function keys, as the Linux console sends them -- the strings
|
||||||
|
// the `linux` terminfo entry lists. Nothing sent them at all, so mc's
|
||||||
|
// F10 never quit and htop's F-key bar was decoration.
|
||||||
|
case KBD_F1: esc = "\033[[A"; break;
|
||||||
|
case KBD_F1 + 1: esc = "\033[[B"; break;
|
||||||
|
case KBD_F1 + 2: esc = "\033[[C"; break;
|
||||||
|
case KBD_F1 + 3: esc = "\033[[D"; break;
|
||||||
|
case KBD_F1 + 4: esc = "\033[[E"; break;
|
||||||
|
case KBD_F1 + 5: esc = "\033[17~"; break;
|
||||||
|
case KBD_F1 + 6: esc = "\033[18~"; break;
|
||||||
|
case KBD_F1 + 7: esc = "\033[19~"; break;
|
||||||
|
case KBD_F1 + 8: esc = "\033[20~"; break;
|
||||||
|
case KBD_F1 + 9: esc = "\033[21~"; break;
|
||||||
|
case KBD_F1 + 10: esc = "\033[23~"; break;
|
||||||
|
case KBD_F12: esc = "\033[24~"; break;
|
||||||
}
|
}
|
||||||
if (esc) {
|
if (esc) {
|
||||||
// Not echoed. In raw mode -- which is where a line editor lives, and
|
// Not echoed. In raw mode -- which is where a line editor lives, and
|
||||||
|
|
@ -727,6 +742,16 @@ consoleread(struct inode *ip, uint64 off, char *dst, int n)
|
||||||
ilock(ip);
|
ilock(ip);
|
||||||
return -EINTR;
|
return -EINTR;
|
||||||
}
|
}
|
||||||
|
if (sig_attention(myproc())) { // Ctrl-Z, or a handler to run
|
||||||
|
release(&input_lock);
|
||||||
|
int e = sig_blocking_check();
|
||||||
|
if (e) {
|
||||||
|
ilock(ip);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
acquire(&input_lock);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
sleep(&v->in.r, &input_lock);
|
sleep(&v->in.r, &input_lock);
|
||||||
}
|
}
|
||||||
c = v->in.buf[v->in.r++ % VT_INPUT_BUF];
|
c = v->in.buf[v->in.r++ % VT_INPUT_BUF];
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,12 @@
|
||||||
[0xCB] = KBD_LF, [0xCD] = KBD_RT, \
|
[0xCB] = KBD_LF, [0xCD] = KBD_RT, \
|
||||||
[0x97] = KBD_HOME, [0xCF] = KBD_END, \
|
[0x97] = KBD_HOME, [0xCF] = KBD_END, \
|
||||||
[0xD2] = KBD_INS, [0xD3] = KBD_DEL, \
|
[0xD2] = KBD_INS, [0xD3] = KBD_DEL, \
|
||||||
|
[0x3B] = KBD_F1, [0x3C] = KBD_F1 + 1, \
|
||||||
|
[0x3D] = KBD_F1 + 2, [0x3E] = KBD_F1 + 3, \
|
||||||
|
[0x3F] = KBD_F1 + 4, [0x40] = KBD_F1 + 5, \
|
||||||
|
[0x41] = KBD_F1 + 6, [0x42] = KBD_F1 + 7, \
|
||||||
|
[0x43] = KBD_F1 + 8, [0x44] = KBD_F1 + 9, \
|
||||||
|
[0x57] = KBD_F1 + 10, [0x58] = KBD_F12, \
|
||||||
[0x9C] = '\n', /* KP_Enter */ \
|
[0x9C] = '\n', /* KP_Enter */ \
|
||||||
[0xB5] = '/' /* KP_Div */
|
[0xB5] = '/' /* KP_Div */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -458,6 +458,16 @@ pts_read(struct inode *ip, uint64 off, char *dst, int n)
|
||||||
ilock(ip);
|
ilock(ip);
|
||||||
return got ? got : -EINTR;
|
return got ? got : -EINTR;
|
||||||
}
|
}
|
||||||
|
if (sig_attention(myproc())) {
|
||||||
|
release(&ptylock);
|
||||||
|
int e = sig_blocking_check();
|
||||||
|
if (e) {
|
||||||
|
ilock(ip);
|
||||||
|
return got ? got : e;
|
||||||
|
}
|
||||||
|
acquire(&ptylock);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
sleep(&p->ir, &ptylock);
|
sleep(&p->ir, &ptylock);
|
||||||
}
|
}
|
||||||
c = p->in[p->ir++ % PTY_BUF];
|
c = p->in[p->ir++ % PTY_BUF];
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,8 @@ hid_special(uchar usage, uchar mods)
|
||||||
if (usage >= 0x3A && usage < 0x3A + NVT && (mods & HID_MOD_ALT) &&
|
if (usage >= 0x3A && usage < 0x3A + NVT && (mods & HID_MOD_ALT) &&
|
||||||
!(mods & HID_MOD_SHIFT))
|
!(mods & HID_MOD_SHIFT))
|
||||||
return KBD_VT_SWITCH + (usage - 0x3A);
|
return KBD_VT_SWITCH + (usage - 0x3A);
|
||||||
|
if (usage >= 0x3A && usage <= 0x45)
|
||||||
|
return KBD_F1 + (usage - 0x3A);
|
||||||
switch (usage) {
|
switch (usage) {
|
||||||
case 0x49: return KBD_INS;
|
case 0x49: return KBD_INS;
|
||||||
case 0x4A: return KBD_HOME;
|
case 0x4A: return KBD_HOME;
|
||||||
|
|
|
||||||
|
|
@ -183,6 +183,14 @@ piperead(struct pipe *p, char *addr, int n)
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if(sig_attention(myproc())){ // stopped, or a handler wants to run
|
||||||
|
release(&p->lock);
|
||||||
|
int e = sig_blocking_check();
|
||||||
|
if(e)
|
||||||
|
return e;
|
||||||
|
acquire(&p->lock);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
sleep(&p->nread, &p->lock); //DOC: piperead-sleep
|
sleep(&p->nread, &p->lock); //DOC: piperead-sleep
|
||||||
}
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ gen_pid_stat(struct sb *s, const struct procinfo *pi)
|
||||||
sbputs(s, " (");
|
sbputs(s, " (");
|
||||||
sbputs(s, pi->name);
|
sbputs(s, pi->name);
|
||||||
sbputs(s, ") ");
|
sbputs(s, ") ");
|
||||||
sbputc(s, state_char(pi->state));
|
sbputc(s, pi->stopped ? 'T' : state_char(pi->state));
|
||||||
sbputc(s, ' ');
|
sbputc(s, ' ');
|
||||||
sbputi(s, pi->ppid); // 4 ppid
|
sbputi(s, pi->ppid); // 4 ppid
|
||||||
sbputc(s, ' ');
|
sbputc(s, ' ');
|
||||||
|
|
@ -247,7 +247,7 @@ static void
|
||||||
gen_pid_status(struct sb *s, const struct procinfo *pi)
|
gen_pid_status(struct sb *s, const struct procinfo *pi)
|
||||||
{
|
{
|
||||||
sbputs(s, "Name:\t"); sbputs(s, pi->name); sbputc(s, '\n');
|
sbputs(s, "Name:\t"); sbputs(s, pi->name); sbputc(s, '\n');
|
||||||
sbputs(s, "State:\t"); sbputc(s, state_char(pi->state));
|
sbputs(s, "State:\t"); sbputc(s, pi->stopped ? 'T' : state_char(pi->state));
|
||||||
sbputs(s, "\nTgid:\t"); sbputi(s, pi->pid);
|
sbputs(s, "\nTgid:\t"); sbputi(s, pi->pid);
|
||||||
sbputs(s, "\nPid:\t"); sbputi(s, pi->pid);
|
sbputs(s, "\nPid:\t"); sbputi(s, pi->pid);
|
||||||
sbputs(s, "\nPPid:\t"); sbputi(s, pi->ppid);
|
sbputs(s, "\nPPid:\t"); sbputi(s, pi->ppid);
|
||||||
|
|
|
||||||
|
|
@ -318,6 +318,7 @@ found:
|
||||||
p->alarm_at = p->alarm_every = 0; // a child's timers start disarmed
|
p->alarm_at = p->alarm_every = 0; // a child's timers start disarmed
|
||||||
p->utime = p->stime = p->cutime = p->cstime = 0;
|
p->utime = p->stime = p->cutime = p->cstime = 0;
|
||||||
p->strace = 0;
|
p->strace = 0;
|
||||||
|
p->stopsig = p->stop_report = p->cont_report = 0;
|
||||||
p->umask = 022;
|
p->umask = 022;
|
||||||
|
|
||||||
// A recycled slot must not inherit a dead process's place in the queue --
|
// A recycled slot must not inherit a dead process's place in the queue --
|
||||||
|
|
@ -486,6 +487,7 @@ fork(void)
|
||||||
// aren't inherited. allocproc() already zeroed pending_signals/
|
// aren't inherited. allocproc() already zeroed pending_signals/
|
||||||
// in_signal/saved_sigtf for this fresh slot.
|
// in_signal/saved_sigtf for this fresh slot.
|
||||||
memmove(np->sig_handler, myproc()->sig_handler, sizeof(np->sig_handler));
|
memmove(np->sig_handler, myproc()->sig_handler, sizeof(np->sig_handler));
|
||||||
|
memmove(np->sig_flags, myproc()->sig_flags, sizeof(np->sig_flags));
|
||||||
memmove(np->sig_restorer, myproc()->sig_restorer, sizeof(np->sig_restorer));
|
memmove(np->sig_restorer, myproc()->sig_restorer, sizeof(np->sig_restorer));
|
||||||
np->signal_mask = myproc()->signal_mask;
|
np->signal_mask = myproc()->signal_mask;
|
||||||
|
|
||||||
|
|
@ -779,6 +781,25 @@ waitpid(int pid, int *status, int options, uint64 *ut, uint64 *st)
|
||||||
if (status) *status = rstatus;
|
if (status) *status = rstatus;
|
||||||
return rpid;
|
return rpid;
|
||||||
}
|
}
|
||||||
|
// WUNTRACED and WCONTINUED: a stop or a resume, each reported once.
|
||||||
|
if((options & 2) && p->stop_report){
|
||||||
|
int rpid = p->pid, rstatus = (p->stop_report << 8) | 0x7f;
|
||||||
|
p->stop_report = 0;
|
||||||
|
if (ut) *ut = p->utime;
|
||||||
|
if (st) *st = p->stime;
|
||||||
|
release(&ptable.lock);
|
||||||
|
if (status) *status = rstatus;
|
||||||
|
return rpid;
|
||||||
|
}
|
||||||
|
if((options & 8) && p->cont_report){
|
||||||
|
int rpid = p->pid;
|
||||||
|
p->cont_report = 0;
|
||||||
|
if (ut) *ut = p->utime;
|
||||||
|
if (st) *st = p->stime;
|
||||||
|
release(&ptable.lock);
|
||||||
|
if (status) *status = 0xffff;
|
||||||
|
return rpid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Two different failures, and they used to share one bare -1.
|
// Two different failures, and they used to share one bare -1.
|
||||||
|
|
@ -1645,6 +1666,139 @@ alarm_thread_start(void)
|
||||||
cprintf("alarm: no kernel thread; alarm() and setitimer() will not fire\n");
|
cprintf("alarm: no kernel thread; alarm() and setitimer() will not fire\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Stopping and continuing --------------------------------------------
|
||||||
|
//
|
||||||
|
// SIGSTOP, and SIGTSTP/SIGTTIN/SIGTTOU with no handler, stop a process; SIGCONT
|
||||||
|
// resumes it. Neither existed: Ctrl-Z did nothing, and mc -- which stops its
|
||||||
|
// subshell and waits to be told it has stopped -- hung before drawing.
|
||||||
|
//
|
||||||
|
// The stop itself happens on the way back to user mode, in deliver_signals(),
|
||||||
|
// where the process can sleep. SIGCONT acts when it is sent, because a
|
||||||
|
// stopped process will not reach that point by itself.
|
||||||
|
|
||||||
|
int
|
||||||
|
sig_default_stops(int sig)
|
||||||
|
{
|
||||||
|
return sig == SIGSTOP || sig == SIGTSTP || sig == SIGTTIN || sig == SIGTTOU;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define STOP_SIGS ((1ULL << SIGSTOP) | (1ULL << SIGTSTP) | \
|
||||||
|
(1ULL << SIGTTIN) | (1ULL << SIGTTOU))
|
||||||
|
|
||||||
|
// Tell the parent about a stop or a resume: SIGCHLD, and a wake for a wait()
|
||||||
|
// or a sigsuspend() that is waiting to hear it. Called with ptable.lock.
|
||||||
|
static void
|
||||||
|
notify_parent(struct proc *p)
|
||||||
|
{
|
||||||
|
struct proc *pp = p->parent;
|
||||||
|
if(pp == 0)
|
||||||
|
return;
|
||||||
|
pp->pending_signals |= 1ULL << SIGCHLD;
|
||||||
|
if(pp->state == SLEEPING){
|
||||||
|
pp->state = RUNNABLE;
|
||||||
|
sched_enqueue(pp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The side effects of sending sig to p, beyond setting it pending. With
|
||||||
|
// ptable.lock held.
|
||||||
|
static void
|
||||||
|
sig_job_control(struct proc *p, int sig)
|
||||||
|
{
|
||||||
|
if(sig == SIGCONT){
|
||||||
|
// A resume cancels any stop still on its way, and the other way round.
|
||||||
|
p->pending_signals &= ~STOP_SIGS;
|
||||||
|
if(p->stopsig){
|
||||||
|
p->stopsig = 0;
|
||||||
|
p->stop_report = 0;
|
||||||
|
p->cont_report = 1;
|
||||||
|
notify_parent(p);
|
||||||
|
}
|
||||||
|
} else if(sig_default_stops(sig)){
|
||||||
|
p->pending_signals &= ~(1ULL << SIGCONT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the calling process with sig until SIGCONT or SIGKILL.
|
||||||
|
void
|
||||||
|
proc_stop(int sig)
|
||||||
|
{
|
||||||
|
struct proc *p = myproc();
|
||||||
|
|
||||||
|
acquire(&ptable.lock);
|
||||||
|
p->stopsig = sig;
|
||||||
|
p->stop_report = sig;
|
||||||
|
p->cont_report = 0;
|
||||||
|
notify_parent(p);
|
||||||
|
// Other signals wake it too; only a resume or a kill ends the stop.
|
||||||
|
while(p->stopsig && !p->killed)
|
||||||
|
sleep(&p->stopsig, &ptable.lock);
|
||||||
|
p->stopsig = 0;
|
||||||
|
release(&ptable.lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A handler that ends a blocking call. Installed with SA_RESTART -- which
|
||||||
|
// musl's signal() always asks for -- the call goes on waiting instead, and
|
||||||
|
// the handler runs when it returns: Linux restarts the call around the
|
||||||
|
// handler, and a program that asked for that does not check for EINTR.
|
||||||
|
#define SA_RESTART 0x10000000
|
||||||
|
static int
|
||||||
|
sig_interrupts(struct proc *p, int sig)
|
||||||
|
{
|
||||||
|
return p->sig_handler[sig] > 1 && !(p->sig_flags[sig] & SA_RESTART);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Whether a pending signal should end a blocking wait: one that will stop the
|
||||||
|
// process, or one with a handler to run. A lockless look, for the inside of a
|
||||||
|
// sleep loop that holds some other lock; sig_blocking_check() does the work.
|
||||||
|
int
|
||||||
|
sig_attention(struct proc *p)
|
||||||
|
{
|
||||||
|
uint64 d = p->pending_signals & ~p->signal_mask;
|
||||||
|
if(d == 0)
|
||||||
|
return 0;
|
||||||
|
for(int sig = 1; sig < 32; sig++)
|
||||||
|
if((d & (1ULL << sig)) &&
|
||||||
|
(sig == SIGSTOP || sig_interrupts(p, sig) ||
|
||||||
|
(sig_default_stops(sig) && p->sig_handler[sig] == 0)))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For a blocking call that saw sig_attention(), with no locks held. A stop is
|
||||||
|
// taken here, and the call carries on waiting once continued -- which is what
|
||||||
|
// Linux's restart does, and what lets Ctrl-Z stop a `sleep` or a `cat`. A
|
||||||
|
// signal with a handler ends the call with -EINTR, as POSIX has it.
|
||||||
|
int
|
||||||
|
sig_blocking_check(void)
|
||||||
|
{
|
||||||
|
struct proc *p = myproc();
|
||||||
|
|
||||||
|
for(;;){
|
||||||
|
int stop = 0, caught = 0;
|
||||||
|
acquire(&ptable.lock);
|
||||||
|
uint64 d = p->pending_signals & ~p->signal_mask;
|
||||||
|
for(int sig = 1; sig < 32 && !stop; sig++){
|
||||||
|
if(!(d & (1ULL << sig)))
|
||||||
|
continue;
|
||||||
|
if(sig == SIGSTOP || (sig_default_stops(sig) && p->sig_handler[sig] == 0)){
|
||||||
|
p->pending_signals &= ~(1ULL << sig);
|
||||||
|
stop = sig;
|
||||||
|
} else if(sig_interrupts(p, sig)){
|
||||||
|
caught = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
release(&ptable.lock);
|
||||||
|
if(stop){
|
||||||
|
proc_stop(stop);
|
||||||
|
if(p->killed)
|
||||||
|
return -EINTR;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return (caught || p->killed) ? -EINTR : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
sigsend(int pid, int sig)
|
sigsend(int pid, int sig)
|
||||||
{
|
{
|
||||||
|
|
@ -1661,6 +1815,7 @@ sigsend(int pid, int sig)
|
||||||
return 0; // silently dropped, as POSIX requires
|
return 0; // silently dropped, as POSIX requires
|
||||||
}
|
}
|
||||||
p->pending_signals |= 1ULL << sig;
|
p->pending_signals |= 1ULL << sig;
|
||||||
|
sig_job_control(p, sig);
|
||||||
if(sig_kills_now(p, sig))
|
if(sig_kills_now(p, sig))
|
||||||
p->killed = 1;
|
p->killed = 1;
|
||||||
if(sig == SIGKILL)
|
if(sig == SIGKILL)
|
||||||
|
|
@ -1705,6 +1860,7 @@ sigsend_pgrp(int pgid, int sig)
|
||||||
if(init_ignores(p, sig))
|
if(init_ignores(p, sig))
|
||||||
continue; // see init_ignores(): this is the Ctrl-C case
|
continue; // see init_ignores(): this is the Ctrl-C case
|
||||||
p->pending_signals |= 1ULL << sig;
|
p->pending_signals |= 1ULL << sig;
|
||||||
|
sig_job_control(p, sig);
|
||||||
if(sig_kills_now(p, sig)){
|
if(sig_kills_now(p, sig)){
|
||||||
p->killed = 1;
|
p->killed = 1;
|
||||||
if(p->killsig == 0)
|
if(p->killsig == 0)
|
||||||
|
|
@ -1929,6 +2085,7 @@ proc_snapshot(int idx, struct procinfo *out)
|
||||||
safestrcpy(out->name, p->name, sizeof(out->name));
|
safestrcpy(out->name, p->name, sizeof(out->name));
|
||||||
safestrcpy(out->exe, p->exe, sizeof(out->exe));
|
safestrcpy(out->exe, p->exe, sizeof(out->exe));
|
||||||
out->kthread = p->kfn != 0;
|
out->kthread = p->kfn != 0;
|
||||||
|
out->stopped = p->stopsig != 0;
|
||||||
release(&ptable.lock);
|
release(&ptable.lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1653,6 +1653,20 @@ addr_t sys_ioctl(void)
|
||||||
// terminal and nothing to choose. It records the choice now, which is what
|
// 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
|
// /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.
|
// 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) {
|
if (request == TIOCSCTTY) {
|
||||||
struct vt *v = vt_for_minor(f->ip->minor);
|
struct vt *v = vt_for_minor(f->ip->minor);
|
||||||
myproc()->ctty = vt_index(v) + 1;
|
myproc()->ctty = vt_index(v) + 1;
|
||||||
|
|
|
||||||
|
|
@ -79,13 +79,18 @@ sys_sigwait(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// musl's struct sigaction (musl 1.2.5 signal.h) for this target:
|
// The native musl hands over its own struct sigaction (musl 1.2.5 signal.h):
|
||||||
// sa_handler (8 bytes) at offset 0, then a 128-byte sigset_t (musl uses
|
// sa_handler at 0, a 128-byte sigset_t, sa_flags (4 bytes) at 136, then
|
||||||
// 128/sizeof(long) = 16 longs regardless of NSIG), sa_flags (4 bytes) +
|
// sa_restorer at 144.
|
||||||
// padding, then sa_restorer (8 bytes) at offset 144. We only care about
|
//
|
||||||
// those two 8-byte fields.
|
// 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_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
|
// rt_sigaction: store the handler + restorer so deliver_signals() (called
|
||||||
// from syscall()'s tail, kernel/sys/syscall.c) can actually jump to user
|
// from syscall()'s tail, kernel/sys/syscall.c) can actually jump to user
|
||||||
|
|
@ -108,15 +113,30 @@ sys_rt_sigaction(void)
|
||||||
argaddr(2, &oldactp);
|
argaddr(2, &oldactp);
|
||||||
if (sig <= 0 || sig >= 32) return -1;
|
if (sig <= 0 || sig >= 32) return -1;
|
||||||
|
|
||||||
|
struct proc *p = myproc();
|
||||||
if (oldactp) {
|
if (oldactp) {
|
||||||
put_user(myproc()->sig_handler[sig], oldactp + SA_HANDLER_OFF);
|
put_user(p->sig_handler[sig], oldactp + SA_HANDLER_OFF);
|
||||||
put_user(myproc()->sig_restorer[sig], oldactp + SA_RESTORER_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) {
|
if (actp) {
|
||||||
copy_from_user(&myproc()->sig_handler[sig], actp + SA_HANDLER_OFF,
|
uint64 h, r, fl = 0;
|
||||||
sizeof(myproc()->sig_handler[sig]));
|
if (copy_from_user(&h, actp + SA_HANDLER_OFF, sizeof(h)) < 0 ||
|
||||||
copy_from_user(&myproc()->sig_restorer[sig], actp + SA_RESTORER_OFF,
|
copy_from_user(&r, actp + SA_RESTORER_OFF(p), sizeof(r)) < 0 ||
|
||||||
sizeof(myproc()->sig_restorer[sig]));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -195,6 +215,16 @@ deliver_signals(struct trapframe *tf)
|
||||||
// Except for a signal that was blocked when it arrived: sigsend()
|
// Except for a signal that was blocked when it arrived: sigsend()
|
||||||
// left it pending without acting, and now that it is deliverable its
|
// left it pending without acting, and now that it is deliverable its
|
||||||
// default action happens here.
|
// 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) {
|
if (myproc()->sig_handler[sig] <= 1) {
|
||||||
myproc()->pending_signals &= ~(1ULL << sig);
|
myproc()->pending_signals &= ~(1ULL << sig);
|
||||||
if (myproc()->sig_handler[sig] == 0 && sig_default_kills(sig)) {
|
if (myproc()->sig_handler[sig] == 0 && sig_default_kills(sig)) {
|
||||||
|
|
|
||||||
|
|
@ -60,25 +60,67 @@ sys_gettimeofday(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr_t
|
// Sleep for a timespec, in ticks, rounded up.
|
||||||
sys_nanosleep(void)
|
//
|
||||||
|
// 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)
|
||||||
{
|
{
|
||||||
addr_t tsaddr, raw;
|
struct proc *p = myproc();
|
||||||
if (argaddr(0, &tsaddr) < 0 || fetchaddr(tsaddr, &raw) < 0)
|
|
||||||
return -1;
|
|
||||||
int64 sec = (int64)raw;
|
|
||||||
if (sec > 0) {
|
|
||||||
acquire(&tickslock);
|
acquire(&tickslock);
|
||||||
uint t0 = ticks;
|
uint t0 = ticks;
|
||||||
while (ticks - t0 < (uint)(sec * HZ)) {
|
while ((uint64)(uint)(ticks - t0) < want) {
|
||||||
if (myproc()->killed) { release(&tickslock); return -EINTR; }
|
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);
|
sleep(&ticks, &tickslock);
|
||||||
}
|
}
|
||||||
release(&tickslock);
|
release(&tickslock);
|
||||||
}
|
|
||||||
return 0;
|
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, 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
|
addr_t
|
||||||
sys_clock_gettime(void)
|
sys_clock_gettime(void)
|
||||||
{
|
{
|
||||||
|
|
@ -173,20 +215,34 @@ sys_clock_getres(void)
|
||||||
addr_t
|
addr_t
|
||||||
sys_clock_nanosleep(void)
|
sys_clock_nanosleep(void)
|
||||||
{
|
{
|
||||||
// Treat the same as nanosleep: arg2 is the timespec, arg1 is flags.
|
// clock_nanosleep(clockid, flags, rqtp, rmtp). TIMER_ABSTIME (1) makes
|
||||||
// Linux: clock_nanosleep(clockid, flags, rqtp, rmtp)
|
// rqtp a time on that clock rather than an interval.
|
||||||
addr_t tsaddr, raw;
|
int clkid, flags;
|
||||||
if (argaddr(2, &tsaddr) < 0 || fetchaddr(tsaddr, &raw) < 0)
|
addr_t tsaddr, remaddr;
|
||||||
return -1;
|
struct timespec ts;
|
||||||
int64 sec = (int64)raw;
|
uint64 want;
|
||||||
if (sec > 0) {
|
argint(0, &clkid);
|
||||||
acquire(&tickslock);
|
argint(1, &flags);
|
||||||
uint t0 = ticks;
|
argaddr(2, &tsaddr);
|
||||||
while (ticks - t0 < (uint)(sec * HZ)) {
|
argaddr(3, &remaddr);
|
||||||
if (myproc()->killed) { release(&tickslock); return -EINTR; }
|
if (copy_from_user(&ts, tsaddr, sizeof(ts)) < 0)
|
||||||
sleep(&ticks, &tickslock);
|
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
|
||||||
}
|
}
|
||||||
release(&tickslock);
|
int e = timespec_ticks(&ts, &want);
|
||||||
}
|
if (e)
|
||||||
return 0;
|
return e;
|
||||||
|
return sleep_ticks(want, remaddr);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue