aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/job.c
blob: 15879516ed1a0936e0576c4a9993e23ad718f5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "rc.h"

#include <signal.h>
#include <termios.h>

// -----------------------------------------------------------------------
// exports

Thread *
getjob(int pid, int *index)
{
    int i;
    Thread *job;
    for(i=0,job=shell.jobs; job && job->pid != pid; i++, job=job->link)
        ;

    return job;
}

void
report(Thread *job, int index)
{
    switch(job->wait.status){
        case Pdone:
            print(shell.err, "job %d [%d]: done\n",      index, job->pid);
            break;
        case Pstop:
            print(shell.err, "job %d [%d]: suspended\n", index, job->pid);
            break;
        case Pagain:
            print(shell.err, "job %d [%d]: continued\n", index, job->pid);
            break;
        case Prun:
            print(shell.err, "job %d [%d]: running\n",   index, job->pid);
            break;
        default:
            fatal("bad wait status: %d\n", job->wait.status);
    }
}

void
wakeup(Thread *job)
{
    int i;
    job->wait.status = Prun;
    for(i=0; i < job->wait.len; i++){
        if(job->wait.on[i].status == Pstop)
            job->wait.on[i].status = Prun;
    }

    tcsetpgrp(0, job->pgid);
}

void
foreground(Thread *job, int now)
{
    Thread *caller = job->caller;
    if(now){
        if(kill(-job->pgid, SIGCONT) < 0)
            perror("kill[SIGCONT]");
    }

    waitall(job);
    /*
     * reset state if we have a caller
     * otherwise we will exit anyways
     */
    if(caller && caller->flag.user){
        tcsetpgrp(0, caller->pid);
        job->flag.user = 1;
    }
}

void
addjob(Thread *job)
{
    job->link = shell.jobs;
    shell.jobs = job;
    job->wait.status = Prun;
}

void
deljob(Thread *job)
{
    Thread **jp;

    for(jp = &shell.jobs; *jp && *jp != job; jp = &(*jp)->link)
        ;

    *jp = job->link;
}