8
|
1 |
#include <stdlib.h>
|
|
2 |
#include <stdio.h>
|
3
|
3 |
#include <assert.h>
|
|
4 |
#include "msg.h"
|
|
5 |
|
8
|
6 |
static enum
|
|
7 |
{
|
|
8 |
FREE,
|
|
9 |
WAITING
|
|
10 |
} state = FREE;
|
|
11 |
|
3
|
12 |
struct Job
|
|
13 |
{
|
|
14 |
int jobid;
|
|
15 |
char command[CMD_LEN];
|
|
16 |
enum Jobstate state;
|
|
17 |
struct Job *next;
|
|
18 |
};
|
|
19 |
|
|
20 |
/* Globals */
|
|
21 |
static struct Job *firstjob = 0;
|
|
22 |
static jobids = 0;
|
|
23 |
|
5
|
24 |
static void send_list_line(int s, const char * str)
|
|
25 |
{
|
|
26 |
struct msg m;
|
|
27 |
int res;
|
|
28 |
|
|
29 |
m.type = LIST_LINE;
|
|
30 |
|
|
31 |
strcpy(m.u.line, str);
|
|
32 |
|
|
33 |
res = write(s, &m, sizeof(m));
|
|
34 |
if(res == -1)
|
|
35 |
perror("write");
|
|
36 |
}
|
|
37 |
|
3
|
38 |
void s_list(int s)
|
|
39 |
{
|
|
40 |
int i;
|
|
41 |
struct Job *p;
|
5
|
42 |
char buffer[LINE_LEN];
|
3
|
43 |
|
5
|
44 |
sprintf(buffer, "ID\tState\tCommand\n");
|
|
45 |
send_list_line(s,buffer);
|
3
|
46 |
|
|
47 |
p = firstjob;
|
|
48 |
|
|
49 |
while(p != 0)
|
|
50 |
{
|
5
|
51 |
sprintf(buffer, "%i\t%i\t%s\n",
|
|
52 |
p->jobid,
|
|
53 |
p->state,
|
|
54 |
p->command);
|
|
55 |
send_list_line(s,buffer);
|
3
|
56 |
p = p->next;
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
static struct Job * newjobptr()
|
|
61 |
{
|
|
62 |
struct Job *p;
|
|
63 |
|
|
64 |
if (firstjob == 0)
|
|
65 |
{
|
|
66 |
firstjob = (struct Job *) malloc(sizeof(*firstjob));
|
|
67 |
firstjob->next = 0;
|
|
68 |
return firstjob;
|
|
69 |
}
|
|
70 |
|
|
71 |
p = firstjob;
|
|
72 |
while(p->next != 0)
|
|
73 |
p = p->next;
|
|
74 |
|
|
75 |
p->next = (struct Job *) malloc(sizeof(*p));
|
|
76 |
p->next->next = 0;
|
|
77 |
|
|
78 |
return p->next;
|
|
79 |
}
|
|
80 |
|
|
81 |
/* Returns job id */
|
|
82 |
int s_newjob(struct msg *m)
|
|
83 |
{
|
|
84 |
struct Job *p;
|
|
85 |
|
|
86 |
p = newjobptr();
|
|
87 |
|
|
88 |
p->jobid = jobids++;
|
|
89 |
p->state = QUEUED;
|
|
90 |
strcpy(p->command, m->u.command);
|
|
91 |
|
|
92 |
return p->jobid;
|
|
93 |
}
|
|
94 |
|
|
95 |
void s_removejob(int jobid)
|
|
96 |
{
|
|
97 |
struct Job *p;
|
|
98 |
struct Job *newnext;
|
|
99 |
|
|
100 |
if (firstjob->jobid == jobid)
|
|
101 |
{
|
|
102 |
struct Job *newfirst;
|
|
103 |
/* First job is to be removed */
|
|
104 |
newfirst = firstjob->next;
|
|
105 |
free(firstjob);
|
|
106 |
firstjob = newfirst;
|
|
107 |
return;
|
|
108 |
}
|
|
109 |
|
|
110 |
p = firstjob;
|
|
111 |
/* Not first job */
|
|
112 |
while (p->next != 0)
|
|
113 |
{
|
|
114 |
if (p->next->jobid == jobid)
|
|
115 |
break;
|
|
116 |
p = p->next;
|
|
117 |
}
|
|
118 |
assert(p->next != 0);
|
|
119 |
|
|
120 |
newnext = p->next->next;
|
|
121 |
|
|
122 |
free(p->next);
|
|
123 |
p->next = newnext;
|
|
124 |
}
|
8
|
125 |
|
|
126 |
/* -1 if no one should be run. */
|
|
127 |
int next_run_job()
|
|
128 |
{
|
|
129 |
if (state == WAITING)
|
|
130 |
return -1;
|
|
131 |
|
|
132 |
if (firstjob != 0)
|
9
|
133 |
{
|
|
134 |
state = WAITING;
|
8
|
135 |
return firstjob->jobid;
|
9
|
136 |
}
|
8
|
137 |
|
|
138 |
return -1;
|
|
139 |
}
|
|
140 |
|
|
141 |
void job_finished()
|
|
142 |
{
|
|
143 |
struct Job *newfirst;
|
|
144 |
|
|
145 |
assert(state == WAITING);
|
|
146 |
assert(firstjob != 0);
|
|
147 |
|
|
148 |
newfirst = firstjob->next;
|
|
149 |
free(firstjob);
|
|
150 |
firstjob = newfirst;
|
|
151 |
|
|
152 |
state = FREE;
|
|
153 |
}
|