summaryrefslogtreecommitdiff
path: root/src/tet3/xresd/xresproc.c
blob: dfadd68b70dec6834ad69f0553dd86f40c2dae75 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/*
 *      SCCS:  @(#)xresproc.c	1.17 (97/10/27) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 * (C) Copyright 1994 UniSoft 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.
 *
 * 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:   	@(#)xresproc.c	1.17 97/10/27 TETware release 3.3
NAME:		xresproc.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	execution results file request processing functions

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., October 1992
	Inform MTCM if result code action is Abort.

	Andrew Dingwall, UniSoft Ltd., November 1992
	Reset per-system xres states and results on TP start.

	Andrew Dingwall, UniSoft Ltd., December 1992
	Corrected result precedence in addresult().

	Denis McConalogue, UniSoft Limited, August 1993
	changed dtet/ to dtet2/ in #include

	Denis McConalogue, UniSoft Limited, September 1993
	added support for OP_XRCLOSE message request

	Andrew Dingwall, UniSoft Ltd., November 1993
	enhancements for FIFO transport interface

	Andrew Dingwall, UniSoft Ltd., November 1994
	perform Abort processing as each result is registered
	rather than just at TP end
	include xres file lines in trace output

	Andrew Dingwall, UniSoft Ltd., August 1996
	use rescode functions in dtet2lib instead of the local ones

	Andrew Dingwall, UniSoft Ltd., September 1996
	moved addresult() from here to tet_addresult() in dtet2lib/rescode.c.

	Andrew Dingwall, UniSoft Ltd., June 1997
	changes to enable parallel remote and distributed test cases
	to work correctly

	Andrew Dingwall, UniSoft Ltd., October 1997
	if a TP doesn't register a result, use NORESULT instead of UNRESOLVED

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

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

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
/* #include <sys/types.h> included by tet_api.h */
#include "dtmac.h"
#include "tet_api.h"
#include "dtmsg.h"
#include "ptab.h"
#include "sptab.h"
#include "xtab.h"
#include "avmsg.h"
#include "valmsg.h"
#include "error.h"
#include "ltoa.h"
#include "xresd.h"
#include "dtetlib.h"
#include "tet_jrnl.h"


/* static function declarations */
static int op_xr2 PROTOLIST((struct ptab *, struct xtab *));
static void xrinfo PROTOLIST((struct xtab *, char *, char *));
static int xrmsg PROTOLIST((struct xtab *, int));
#ifndef NOTRACE
static char *xrstate PROTOLIST((int));
#endif
static int xrwrite PROTOLIST((struct xtab *, struct avmsg *));


/*
**	op_xropen() - open a tet_xres file
**
**	in the request message:
**		AV_XFNAME = tet_xres file to open
**
**	in the reply message (rc = ER_OK):
**		VM_XRID = xrid to use in all subsequent references to the file
*/

void op_xropen(pp)
register struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register struct xtab *xp;

	/* do some sanity checks on the request message */
	if ((int) mp->av_argc != OP_XROPEN_ARGC || !AV_XFNAME(mp) || !*AV_XFNAME(mp)) {
		pp->ptm_rc = ER_INVAL;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	TRACE2(tet_Txresd, 4, "OP_XROPEN: file = \"%s\"", AV_XFNAME(mp));

	/* get a new xtab element for this request */
	if ((xp = xtalloc()) == (struct xtab *) 0) {
		pp->ptm_rc = ER_ERR;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	if ((pp->ptm_rc = op_xr2(pp, xp)) == ER_OK) {
		xp->xt_ptab = pp;
		xtadd(xp);
		pp->ptm_mtype = MT_VALMSG;
		pp->ptm_len = valmsgsz(OP_XROPEN_NVALUE);
	}
	else {
		xtfree(xp);
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
	}
}

/*
**	op_xr2() - extend the op_xropen() processing
**
**	return ER_OK if successful or other ER_* error code on error
*/

static int op_xr2(pp, xp)
register struct ptab *pp;
register struct xtab *xp;
{
	register char *dp = pp->ptm_data;
	static char msg[] = "can't open";

#define mp	((struct avmsg *) dp)

	/* fill in the xtab details */
	if ((xp->xt_xfname = tet_strstore(AV_XFNAME(mp))) == (char *) 0)
		return(ER_ERR);

	/* open the tet_xres files */
	if ((xp->xt_xfp = fopen(xp->xt_xfname, "w")) == NULL) {
		error(errno, msg, xp->xt_xfname);
		return(ER_ERR);
	}

#undef mp
#define rp	((struct valmsg *) dp)

	/* make sure that the message buffer is big enough for the reply */
	if (BUFCHK(&pp->ptm_data, &pp->pt_mdlen, valmsgsz(OP_XROPEN_NVALUE)) < 0)
		return(ER_ERR);
	dp = pp->ptm_data;

	/* all ok so fill in the reply message and return */
	TRACE2(tet_Txresd, 4, "OP_XROPEN: return xrid = %s",
		tet_l2a(xp->xt_xrid));
	VM_XRID(rp) = xp->xt_xrid;
	rp->vm_nvalue = OP_XROPEN_NVALUE;
	return(ER_OK);

#undef rp
}


/*
**	op_xrclose() - close a tet_xres file previously opened by 
**			op_xropen
**
**	in the request message 
**		VM_XRID = xrid of tet_xres file to be closed
*/

void op_xrclose(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	TRACE2(tet_Txresd, 4, "OP_XRCLOSE: xrid = %s", tet_l2a(VM_XRID(mp)));

	/* find the xtab entry */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that the request comes from the process that
		opened the file */
	if (xp->xt_ptab != pp) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* make sure that neither IC nor TP are in progress */
	if (xp->xt_flags & (XF_ICINPROGRESS | XF_TPINPROGRESS)) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* here to close the file and return success */
	TRACE3(tet_Txresd, 4, "OP_XRCLOSE: closed xrid = %s (\"%s\")",
		tet_l2a(VM_XRID(mp)), xp->xt_xfname);
	xtrm(xp);
	xtfree(xp);
	pp->ptm_rc = ER_OK;
}

/*
**	op_xrsys() - receive system name list for tet_xres file
**
**	in the request message:
**		VM_XRID = xrid to use
**		VM_XSYSID(0) .. VM_XSYSID(OP_XRSYS_NSYS - 1) = system names
*/

void op_xrsys(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;
	register struct uxtab *up;
	register int i, j, nsys;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* do a sanity check on the request message */
	if (mp->vm_nvalue < (unsigned short) OP_XRSYS_NVALUE(1)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* calculate the number of systems in the list */
	nsys = OP_XRSYS_NSYS(mp);

#ifndef NOTRACE
	if (tet_Txresd > 0) {
		TRACE2(tet_Txresd, 4, "OP_XRSYS: nsys = %s", tet_i2a(nsys));
		for (i = 0; i < nsys; i++)
			TRACE2(tet_Txresd, 6, "xrsys: sysid = %s",
				tet_l2a(VM_XSYSID(mp, i)));
	}
#endif

	/* do some more sanity checks on the request message -
		make sure that there are no duplicate sysids in the list */
	for (i = 0; i < nsys; i++) {
		if (VM_XSYSID(mp, i) < 0) {
			pp->ptm_rc = ER_INVAL;
			return;
		}
		for (j = 0; j < i; j++)
			if (VM_XSYSID(mp, j) == VM_XSYSID(mp, i)) {
				pp->ptm_rc = ER_DUPS;
				return;
			}
	}

	/* find the xtab entry */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that neither IC nor TP have been started */
	if (xp->xt_flags & (XF_ICINPROGRESS | XF_TPINPROGRESS)) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* allocate storage for the xres per-user details list */
	if (uxtalloc(xp, nsys) < 0) {
		pp->ptm_rc = ER_ERR;
		return;
	}

	/* copy in the per-user details list */
	for (i = 0, up = xp->xt_ud; i < xp->xt_nud; i++, up++)
		up->ux_sysid = VM_XSYSID(mp, i);

	/* here if all is ok */
	pp->ptm_rc = ER_OK;
}

/*
**	op_xrsend() - associate XRID with this TCM
**
**	in the request message:
**		VM_XRID = xrid to use
*/

void op_xrsend(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct sptab *sp = (struct sptab *) pp->pt_sdata;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* do a sanity check on the request message */
	if ((int) mp->vm_nvalue != OP_XRSEND_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Txresd, 4, "OP_XRSEND: xrid = %s", tet_l2a(VM_XRID(mp)));

	/*
	** do some more sanity checks on the request message -
	**	check for invalid XRID and contradicting requests
	*/
	if (VM_XRID(mp) < 0L) {
		pp->ptm_rc = ER_INVAL;
		return;
	}
	if (sp->sp_xrid >= 0L && sp->sp_xrid != VM_XRID(mp)) {
		pp->ptm_rc = ER_DONE;
		return;
	}

	/* all OK so store the XRID and return */
	sp->sp_xrid = VM_XRID(mp);
	pp->ptm_rc = ER_OK;
}

/*
**	op_icstart() - signal IC start
*/

void op_icstart(pp)
struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct uxtab *up;
	register struct ptab *q;
	register struct xtab *xp;
	register int rc, count;
	struct ptab *pte;
	extern struct ptab *tet_ptab;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* make sure requester is MTCM */
	if (pp->ptr_ptype != PT_MTCM) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* make sure that vm_nvalue has a plausible value */
	if ((int) mp->vm_nvalue != OP_ICSTART_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE5(tet_Txresd, 4,
		"OP_ICSTART: xrid = %s, icno = %s, activity = %s, tpcount = %s",
		tet_l2a(VM_XRID(mp)), tet_l2a(VM_ICNO(mp)),
		tet_l2a(VM_ACTIVITY(mp)), tet_l2a(VM_TPCOUNT(mp)));

	/* find the xtab element */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that neither IC nor TP have been started */
	if (xp->xt_flags & (XF_ICINPROGRESS | XF_TPINPROGRESS)) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* make sure that at least one TCM is logged on from each
		participating system;
		make sure that ux_ptab is filled in
		ptab elements are stored LIFO so we search the table
		backwards to find the first TCM logged on from each system */

	for (pte = tet_ptab; pte->pt_next; pte = pte->pt_next)
		;

	count = 0;
	for (up = xp->xt_ud; up < xp->xt_ud + xp->xt_nud; up++) {
		if (!up->ux_ptab) {
			for (q = pte; q; q = q->pt_last) {
				if (q->ptr_sysid == up->ux_sysid &&
					(q->pt_flags & PF_LOGGEDON) &&
					((struct sptab *) q->pt_sdata)->sp_xrid == xp->xt_xrid)
						switch (q->ptr_ptype) {
						case PT_MTCM:
						case PT_STCM:
							up->ux_ptab = q;
							break;
						default:
							continue;
						}
				else
					continue;
				break;
			}
		}
		if (!up->ux_ptab)
			error(0, "no TCM logged on from system",
				tet_i2a(up->ux_sysid));
		else
			count++;
	}
	if (count < xp->xt_nud) {
		pp->ptm_rc = ER_SYSID;
		return;
	}

	/* store the values, emit the ICstart message and return */
	xp->xt_icno = (int) VM_ICNO(mp);
	xp->xt_activity = VM_ACTIVITY(mp);
	xp->xt_tpcount = (int) VM_TPCOUNT(mp);
	xp->xt_flags |= XF_ICINPROGRESS;
	rc = xrmsg(xp, TET_JNL_IC_START);
	xp->xt_tpcount = 0;
	pp->ptm_rc = rc;
}

/*
**	op_icend() - receive notification of IC end
*/

void op_icend(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* make sure requester is MTCM */
	if (pp->ptr_ptype != PT_MTCM) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* make sure that vm_nvalue has a plausible value */
	if ((int) mp->vm_nvalue != OP_ICEND_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Txresd, 4, "OP_ICEND: xrid = %s", tet_l2a(VM_XRID(mp)));

	/* find the xtab element */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that an IC is in progress but that a TP is not */
	if ((xp->xt_flags & XF_ICINPROGRESS) == 0) {
		pp->ptm_rc = ER_DONE;
		return;
	}
	else if (xp->xt_flags & XF_TPINPROGRESS) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* mark the IC as finished, emit the ICend message and return */
	xp->xt_flags &= ~XF_ICINPROGRESS;
	pp->ptm_rc = icend(xp);
}

/*
**	op_tpstart() - receive notification of TP start
*/

void op_tpstart(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;
	register struct uxtab *up;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* make sure requester is MTCM */
	if (pp->ptr_ptype != PT_MTCM) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* make sure that vm_nvalue has a plausible value */
	if ((int) mp->vm_nvalue != OP_TPSTART_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE3(tet_Txresd, 4, "OP_TPSTART: xrid = %s, tpno = %s",
		tet_l2a(VM_XRID(mp)), tet_l2a(VM_TPNO(mp)));

	/* find the xtab element */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure the an IC has been started,
		and that a TP has not already been started */
	if ((xp->xt_flags & XF_ICINPROGRESS) == 0) {
		pp->ptm_rc = ER_DONE;
		return;
	}
	else if (xp->xt_flags & XF_TPINPROGRESS) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* reset the result codes and abort flag */
	xp->xt_result = -1;
	xp->xt_flags &= ~XF_TCABORT;
	for (up = xp->xt_ud; up < xp->xt_ud + xp->xt_nud; up++) {
		up->ux_state = XS_NOTREPORTED;
		up->ux_result = -1;
	}

	/* store the values, emit the TPstart message and return */
	xp->xt_flags |= XF_TPINPROGRESS;
	xp->xt_tpno = (int) VM_TPNO(mp);
	pp->ptm_rc = xrmsg(xp, TET_JNL_TP_START);
}

/*
**	op_tpend() - receive notification of TP end
*/

void op_tpend(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* make sure requester is MTCM */
	if (pp->ptr_ptype != PT_MTCM) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* make sure that vm_nvalue has a plausible value */
	if ((int) mp->vm_nvalue != OP_TPEND_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Txresd, 4, "OP_TPEND: xrid = %s", tet_l2a(VM_XRID(mp)));

	/* find the xtab element */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that both an IC and a TP are in progress */
	if ((xp->xt_flags & (XF_ICINPROGRESS | XF_TPINPROGRESS)) != (XF_ICINPROGRESS | XF_TPINPROGRESS)) {
		pp->ptm_rc = ER_DONE;
		return;
	}

	/* mark the TP as finished and return */
	xp->xt_tpcount++;
	xp->xt_flags &= ~XF_TPINPROGRESS;
	pp->ptm_rc = tpend(xp);
}

/*
**	op_xres() - receive xres lines from clients
*/

void op_xres(pp)
register struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register struct xtab *xp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	TRACE3(tet_Txresd, 4, "OP_XRES: %s xrid = %s",
		tet_r2a(&pp->pt_rid), tet_l2a(AV_XRID(mp)));

	/* find the xtab element for this request */
	if ((xp = xtfind(AV_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* write out the lines to the tet_xres file */
	pp->ptm_rc = xrwrite(xp, mp);
}

/*
**	op_result() - receive a TP result
*/

void op_result(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct xtab *xp;
	register struct uxtab *up;
	register int result;
	int abflag;
	static char fmt[] = "ABORT on result code %d:";
	static char gcerr[] =
		"tet_getresname() returns NULL but sets abflag !!";
	char *text, buf[sizeof fmt + LNUMSZ];

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* do a sanity check on the request */
	if (mp->vm_nvalue != OP_RESULT_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE4(tet_Txresd, 4, "OP_RESULT: %s xrid = %s, result = %s",
		tet_r2a(&pp->pt_rid), tet_l2a(VM_XRID(mp)),
		tet_l2a(VM_RESULT(mp)));

	/* check that the result code is within the range
		allowed by the spec */
	if ((result = (int) VM_RESULT(mp)) < 0 || result > 127) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* find the xtab element for this request */
	if ((xp = xtfind(VM_XRID(mp))) == (struct xtab *) 0) {
		pp->ptm_rc = ER_XRID;
		return;
	}

	/* make sure that a TP is in progress */
	if ((xp->xt_flags & (XF_ICINPROGRESS | XF_TPINPROGRESS)) != (XF_ICINPROGRESS | XF_TPINPROGRESS)) {
		pp->ptm_rc = ER_DONE;
		return;
	}

	/* make sure that this system is participating in the test */
	for (up = xp->xt_ud; up < xp->xt_ud + xp->xt_nud; up++)
		if (up->ux_sysid == pp->ptr_sysid)
			break;
	if (up >= xp->xt_ud + xp->xt_nud) {
		pp->ptm_rc = ER_SYSID;
		return;
	}

	/* register the result */
	switch (up->ux_state) {
	default:
		error(0, "op_result(): internal error: unknown uxtab state",
			tet_i2a(up->ux_state));
		result = tet_addresult(up->ux_result, result);
		/* fall through */
	case XS_NOTREPORTED:
		up->ux_result = result;
		up->ux_state = XS_REPORTED;
		break;
	case XS_DEAD:
	case XS_REPORTED:
		up->ux_result = tet_addresult(up->ux_result, result);
		break;
	}

	/* see if the associated action for a valid result is Abort */
	if (result >= 0) {
		abflag = 0;
		text = tet_getresname(result, &abflag);
		if (abflag && (xp->xt_flags & XF_TCABORT) == 0) {
			xp->xt_flags |= XF_TCABORT;
			sprintf(buf, fmt, result);
			xrinfo(xp, buf, text ? text : gcerr);
		}
	}

	pp->ptm_rc = ER_OK;
}

/*
**	icend() - perform IC end processing
**
**	return ER_OK if successful or other ER_* error code on error
*/

int icend(xp)
register struct xtab *xp;
{
	/* make sure the event was expected */
	if (xp->xt_flags & XF_ICINPROGRESS) {
		xrinfo(xp, "MTCM did not signal IC end", (char *) 0);
		xp->xt_flags &= ~XF_ICINPROGRESS;
	}
	xp->xt_flags |= XF_ICDONE;

	/* emit the IC end message and return */
	return(xrmsg(xp, TET_JNL_IC_END));
}

/*
**	tpend() - perform TP end processing
**
**	return ER_OK if successful or other ER_* error code on error
**
**	as a special case, ER_ABORT is returned if the action for any code
**	that contributes to the consolidated result is to abort all TCMs
*/

int tpend(xp)
register struct xtab *xp;
{
	register struct uxtab *up;
	register int result, rc;

	/* make sure that the event was expected */
	if (xp->xt_flags & XF_TPINPROGRESS) {
		xrinfo(xp, "MTCM did not signal TP end", (char *) 0);
		xp->xt_flags &= ~XF_TPINPROGRESS;
	}
	xp->xt_flags |= XF_TPDONE;

	TRACE4(tet_Txresd, 6, "tpend() called, xrid = %s, icno = %s, tpno = %s",
		tet_l2a(xp->xt_xrid), tet_i2a(xp->xt_icno),
		tet_i2a(xp->xt_tpno));

	/* consolidate the per-system results */
	xp->xt_result = -1;
	for (up = xp->xt_ud; up < xp->xt_ud + xp->xt_nud; up++) {
		switch (up->ux_state) {
		case XS_DEAD:
			if (up->ux_result < 0) {
				result = TET_NORESULT;
				break;
			}
			xrinfo(xp, "TCM exited before TP end, system",
				tet_i2a(up->ux_sysid));
			/* fall through */
		case XS_REPORTED:
			result = up->ux_result;
			break;
		case XS_NOTREPORTED:
			result = TET_NORESULT;
			break;
		}
		xp->xt_result = tet_addresult(xp->xt_result, result);
		TRACE6(tet_Txresd, 6, "sysid = %s, state = %s, sys result = %s, use result = %s, cons result = %s",
			tet_i2a(up->ux_sysid), xrstate(up->ux_state),
			tet_i2a(up->ux_result), tet_i2a(result),
			tet_i2a(xp->xt_result));
	}

	/* emit the TP result message and return */
	rc = xrmsg(xp, TET_JNL_TP_RESULT);
	return(xp->xt_flags & XF_TCABORT ? ER_ABORT : rc);
}

/*
**	xrmsg() - emit an {IC|TP}{start|end} message to the tet_xres file
**
**	return ER_OK if successful or other ER_* error code on error
*/

static char wrfail[] = "write failed on";

static int xrmsg(xp, code)
register struct xtab *xp;
register int code;
{
	time_t now;
	register struct tm *tp;
	register char *text;
	char buf[64];

	/* format the variable part of the message */
	switch (code) {
	case TET_JNL_IC_START:
		sprintf(buf, "%d %d", xp->xt_icno, xp->xt_tpcount);
		text = "IC Start";
		break;
	case TET_JNL_IC_END:
		sprintf(buf, "%d %d", xp->xt_icno, xp->xt_tpcount);
		text = "IC End";
		break;
	case TET_JNL_TP_START:
		sprintf(buf, "%d", xp->xt_tpno);
		text = "TP Start";
		break;
	case TET_JNL_TP_RESULT:
		sprintf(buf, "%d %d", xp->xt_tpno, xp->xt_result);
		if ((text = tet_getresname(xp->xt_result, (int *) 0)) == (char *) 0)
			text = "(NO RESULT NAME)";
		break;
	default:
		error(0, "xrmsg(): unexpected xres code", tet_i2a(code));
		return(ER_ERR);
	}

	/* output the message */
	now = time((time_t *) 0);
	tp = localtime(&now);
	TRACE5(tet_Txresd, 8, "xrmsg(): %s|%s %s <time>|%s",
		tet_i2a(code), tet_l2a(xp->xt_activity), buf, text);
	if (fprintf(xp->xt_xfp, "%d|%ld %s %02d:%02d:%02d|%s\n",
		code, xp->xt_activity, buf,
		tp->tm_hour, tp->tm_min, tp->tm_sec, text) < 0 ||
			fflush(xp->xt_xfp) < 0) {
				error(errno, wrfail, xp->xt_xfname);
				return(ER_ERR);
	}

	/* all ok so return success */
	return(ER_OK);
}

/*
**	xrinfo() - write a TCM/API error message to the xres file
*/

static void xrinfo(xp, s1, s2)
struct xtab *xp;
char *s1, *s2;
{
	TRACE6(tet_Txresd, 1, "xrinfo(): %s|%s|%s%s%s",
		tet_i2a(TET_JNL_TCM_INFO), tet_l2a(xp->xt_activity), s1,
		s2 && *s2 ? " " : "", s2 ? s2 : "");

	if (fprintf(xp->xt_xfp, "%d|%ld|xresd: %s%s%s\n", TET_JNL_TCM_INFO,
		xp->xt_activity, s1, s2 && *s2 ? " " : "", s2 ? s2 : "") < 0 ||
			fflush(xp->xt_xfp) < 0)
				error(errno, wrfail, xp->xt_xfname);
}

/*
**	xrwrite() - write message lines to the xres file
**
**	return ER_OK if successful or other ER_* error code on error
*/

static int xrwrite(xp, mp)
register struct xtab *xp;
register struct avmsg *mp;
{
	register char *s;
	register int n;

	/* write out the lines to the tmp file */
	for (n = 0; n < OP_XRES_NLINE(mp); n++) {
		if ((s = AV_XLINE(mp, n)) == (char *) 0)
			continue;
		TRACE2(tet_Txresd, 8, "xrwrite: line = \"%.40s\"", s);
		if (fprintf(xp->xt_xfp, "%s\n", s ? s : "<null>") < 0) {
			error(errno, wrfail, xp->xt_xfname);
			return(ER_ERR);
		}
	}

	/* flush out the file's stdio buffer */
	if (fflush(xp->xt_xfp) < 0) {
		error(errno, wrfail, xp->xt_xfname);
		return(ER_ERR);
	}

	return(ER_OK);
}

/*
**	xrstate() - return printable representation of system result state
*/

#ifndef NOTRACE
static char *xrstate(state)
int state;
{
	static char text[] = "xres-state ";
	static char msg[sizeof text + LNUMSZ];

	switch (state) {
	case XS_NOTREPORTED:
		return("NOTREPORTED");
	case XS_REPORTED:
		return("REPORTED");
	case XS_DEAD:
		return("DEAD");
	default:
		sprintf(msg, "%s%d", text, state);
		return(msg);
	}
}
#endif /* NOTRACE */