summaryrefslogtreecommitdiff
path: root/src/tet3/tcc/proctab.c
blob: 8464ae16fb952c3b085a89db6f4b7da9e5711fad (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 *	SCCS: @(#)proctab.c	1.5 (98/09/01)
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1996 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 * A copy of the end-user licence agreement is contained in the file
 * Licence which accompanies this distribution.
 * 
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)proctab.c	1.5 98/09/01 TETware release 3.3
NAME:		proctab.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	August 1996

DESCRIPTION:
	tcc process table management functions

MODIFICATIONS:

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include "dtmac.h"
#include "bstring.h"
#include "error.h"
#include "ftoa.h"
#include "ltoa.h"
#include "scentab.h"
#include "proctab.h"
#include "tcc.h"

/* the execution engine's run queue */
struct proctab *runq;


/*
**	pralloc(), prfree() - functions to allocate and free a
**		tcc process table element
*/

struct proctab *pralloc()
{
	register struct proctab *prp;

	errno = 0;
	if ((prp = (struct proctab *) malloc(sizeof *prp)) == (struct proctab *) 0)
		fatal(errno, "can't allocate process table element",
			(char *) 0);

	TRACE2(tet_Tbuf, 6, "allocate proctab element = %s", tet_i2x(prp));

	bzero((char *) prp, sizeof *prp);
	prp->pr_magic = PR_MAGIC;
	prp->pr_state = PRS_IDLE;
	prp->pr_toolstate = PTS_IDLE;
	prp->pr_activity = -1;
#ifndef TET_LITE	/* -START-LITE-CUT- */
	prp->pr_snid = -1L;
	prp->pr_xrid = -1L;
#endif /* !TET_LITE */	/* -END-LITE-CUT- */

	return(prp);
}

void prfree(prp)
struct proctab *prp;
{
	TRACE2(tet_Tbuf, 6, "free proctab element = %s", tet_i2x(prp));

	if (prp) {
		ASSERT(prp->pr_magic == PR_MAGIC);
		ASSERT(prp->pr_srclock == (char *) 0);
		ASSERT(prp->pr_execlock == (char *) 0);
		ASSERT(prp->pr_tmpdir == (char *) 0);
		ASSERT(prp->pr_outfile == (char *) 0);
		ASSERT(prp->pr_tetxres == (char *) 0);
		ASSERT(prp->pr_tcedir == (char *) 0);
		prcfree(prp);
		bzero((char *) prp, sizeof *prp);
		free((char *) prp);
	}
}

/*
**	prcfree() - free all of this proctab's children
*/

void prcfree(prp)
struct proctab *prp;
{
	register struct proctab *child, *lforw;

	for (child = prp->pr_child; child; child = lforw) {
		ASSERT(child->pr_magic == PR_MAGIC);
		lforw = child->pr_lforw;
		prfree(child);
	}

	prp->pr_child = (struct proctab *) 0;
}

/*
**	runqadd() - add a proctab element to the end of the run queue
*/

void runqadd(prp)
register struct proctab *prp;
{
	register struct proctab *q;

	TRACE4(tet_Texec, 10,
		"runqadd(): add proctab %s to runq (%s), flags = %s",
		tet_i2x(prp), tet_i2x(runq), prpflags(prp->pr_flags));

	ASSERT(prp->pr_magic == PR_MAGIC);

#ifdef TET_LITE	/* -LITE-CUT-LINE- */
	ASSERT((prp->pr_flags & PRF_RUNQ) == 0);
#else	/* -START-LITE-CUT- */
	ASSERT((prp->pr_flags & (PRF_RUNQ | PRF_TC_CHILD)) == 0);
#endif /* TET_LITE */	/* -END-LITE-CUT- */

	if (!runq)
		runq = prp;
	else {
		for (q = runq; q->pr_rqforw; q = q->pr_rqforw)
			ASSERT(q->pr_magic == PR_MAGIC);
		q->pr_rqforw = prp;
		prp->pr_rqback = q;
	}

	prp->pr_flags |= PRF_RUNQ;
}

/*
**	runqrm() - remove a proctab element from the run queue
*/

void runqrm(prp)
register struct proctab *prp;
{
	TRACE3(tet_Texec, 10, "runqrm(): remove proctab %s from runq (%s)",
		tet_i2x(prp), tet_i2x(runq));

	ASSERT(prp->pr_magic == PR_MAGIC);
	ASSERT(prp->pr_flags & PRF_RUNQ);

	if (prp->pr_rqforw) {
		ASSERT(prp->pr_rqforw->pr_magic == PR_MAGIC);
		ASSERT(prp->pr_rqforw->pr_rqback == prp);
		prp->pr_rqforw->pr_rqback = prp->pr_rqback;
	}

	if (prp->pr_rqback) {
		ASSERT(prp->pr_rqback->pr_magic == PR_MAGIC);
		ASSERT(prp->pr_rqback->pr_rqforw == prp);
		prp->pr_rqback->pr_rqforw = prp->pr_rqforw;
	}
	else {
		ASSERT(prp == runq);
		runq = prp->pr_rqforw;
	}

	prp->pr_rqforw = prp->pr_rqback = (struct proctab *) 0;
	prp->pr_flags &= ~PRF_RUNQ;

	TRACE1(tet_Texec, 10, "runqrm() RETURN");
}

/*
**	prpstate() - return a printable representation of a proctab
**		pr_state value
*/

char *prpstate(state)
int state;
{
	static char text[] = "proctab-state ";
	static char msg[sizeof text + LNUMSZ];

	switch (state) {
	case PRS_IDLE:
		return("IDLE");
	case PRS_PROCESS:
		return("PROCESS");
	case PRS_NEXT:
		return("NEXT");
	case PRS_SLEEP:
		return("SLEEP");
	case PRS_WAIT:
		return("WAIT");
	default:
		(void) sprintf(msg, "%s%d", text, state);
		return(msg);
	}
}

/*
**	prtcstate() - return a printable representation of a proctab
**		pr_tcstate value
*/

char *prtcstate(state)
int state;
{
	static char text[] = "testcase-state ";
	static char msg[sizeof text + LNUMSZ];

	switch (state) {
	case TCS_START:
		return("START");
	case TCS_LOCK:
		return("LOCK");
	case TCS_UNLOCK:
		return("UNLOCK");
	case TCS_COPY:
		return("COPY");
	case TCS_PREBUILD:
		return("PREBUILD");
	case TCS_BUILD:
		return("BUILD");
	case TCS_BUILDFAIL:
		return("BUILDFAIL");
	case TCS_EXEC:
		return("EXEC");
	case TCS_CLEAN:
		return("CLEAN");
	case TCS_JOURNAL:
		return("JOURNAL");
	case TCS_SAVE:
		return("SAVE");
	case TCS_END:
		return("END");
	default:
		(void) sprintf(msg, "%s%d", text, state);
		return(msg);
	}
}

/*
**	prpflags() - return a printable representation of a proctab
**		pr_flags value
*/

char *prpflags(fval)
int fval;
{
	static struct flags flags[] = {
		{ PRF_ATTENTION,	"ATTENTION" },
		{ PRF_RUNQ,		"RUNQ" },
		{ PRF_STEP,		"STEP" },
		{ PRF_SHLOCK,		"SHLOCK" },
		{ PRF_AUTORESULT,	"AUTORESULT" },
#ifndef TET_LITE	/* -START-LITE-CUT- */
		{ PRF_TC_CHILD,		"TC_CHILD" },
		{ PRF_JNL_CHILD,	"JNL_CHILD" }
#endif /* !TET_LITE */	/* -END-LITE-CUT- */
	};

	return(tet_f2a(fval, flags, sizeof flags / sizeof flags[0]));
}

/*
**	prtoolstate() - retur a printable representation of a tool's state
*/

char *prtoolstate(state)
int state;
{
	static char text[] = "tool-state ";
	static char msg[sizeof text + LNUMSZ];

	switch (state) {
	case PTS_IDLE:
		return("IDLE");
	case PTS_RUNNING:
		return("RUNNING");
	case PTS_EXITED:
		return("EXITED");
	case PTS_ABORT:
		return("ABORT");
	case PTS_SIGTERM:
		return("SIGTERM");
	case PTS_SIGKILL:
		return("SIGKILL");
	default:
		(void) sprintf(msg, "%s%d", text, state);
		return(msg);
	}
}