summaryrefslogtreecommitdiff
path: root/src/tet3/syncd/synproc.c
blob: 0de1fa38d7276f19fbd30891be478178622fbeeb (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
/*
 *      SCCS:  @(#)synproc.c	1.11 (98/09/01) 
 *
 *	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:   	@(#)synproc.c	1.11 98/09/01 TETware release 3.3
NAME:		synproc.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	sync event request processing functions

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., November 1992
	End all related user sync events when a process performs an autosync.

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

	Andrew Dingwall, UniSoft Ltd., October 1994
	added support for sync message data

	Andrew Dingwall, UniSoft Ltd., June 1997
	changes to enable parallel distributed test cases to work correctly;
	disallow a -ve syncpoint number;
	determine the TCM process type correctly when none of the
	systems is sysid 0

	Andrew Dingwall, UniSoft Ltd., July 1998
	Always decode ASYNC request IC/TP numbers in trace messages.

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

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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "synreq.h"
#include "stab.h"
#include "valmsg.h"
#include "bstring.h"
#include "syncd.h"
#include "dtetlib.h"

#ifndef NOTRACE
#include "ltoa.h"
#endif


/* static function declarations */
static void op_s2 PROTOLIST((struct ptab *, struct stab *, struct valmsg *));
static void procmsdata PROTOLIST((struct stab *, struct ustab *,
	struct valmsg *));


/*
**	op_snget() - return a snid for use in auto sync events
*/

void op_snget(pp)
struct ptab *pp;
{
	register struct valmsg *rp;
	register struct stab *sp;

	/* get a new stab element */
	if ((sp = stalloc()) == (struct stab *) 0) {
		pp->ptm_rc = ER_ERR;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	/* check that the message buffer is big enough for the reply */
	if (BUFCHK(&pp->ptm_data, &pp->pt_mdlen, valmsgsz(OP_SNGET_NVALUE)) < 0) {
		stfree(sp);
		pp->ptm_rc = ER_ERR;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}
	rp = (struct valmsg *) pp->ptm_data;

	/* fill the new element in a bit and add it to the sync event table */
	sp->st_ptab = pp;
	stadd(sp);

	/* set up the reply and return */
	TRACE2(tet_Tsyncd, 4, "OP_SNGET: return snid = %s",
		tet_l2a(sp->st_snid));
	VM_SNID(rp) = sp->st_snid;
	rp->vm_nvalue = OP_SNGET_NVALUE;
	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_VALMSG;
	pp->ptm_len = valmsgsz(OP_SNGET_NVALUE);
}

/*
**	op_snsys() - receive a system name list for auto syncs
**
**	in the request message:
**		VM_SNID = snid to use
**		VM_SSYSID(0) .. VM_SSYSID(OP_SNSYS_NSYS - 1) = system names
*/

void op_snsys(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct stab *sp;
	register struct ustab *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_SNSYS_NVALUE(1)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* calculate the number of system names in the list */
	nsys = OP_SNSYS_NSYS(mp);

#ifndef NOTRACE
	if (tet_Tsyncd > 0) {
		TRACE3(tet_Tsyncd, 4, "OP_SNSYS: snid = %s, nsys = %s",
			tet_l2a(VM_SNID(mp)), tet_i2a(nsys));
		for (i = 0; i < nsys; i++)
			TRACE2(tet_Tsyncd, 6, "OP_SNSYS: sysid = %s",
				tet_l2a(VM_SSYSID(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_SSYSID(mp, i) < 0L) {
			pp->ptm_rc = ER_INVAL;
			return;
		}
		for (j = 0; j < i; j++)
			if (VM_SSYSID(mp, j) == VM_SSYSID(mp, i)) {
				pp->ptm_rc = ER_DUPS;
				return;
			}
	}

	/* find the stab element */
	if ((sp = stafind(VM_SNID(mp))) == (struct stab *) 0) {
		pp->ptm_rc = ER_SNID;
		return;
	}

	/* make sure that the event is not in progress */
	if (sp->st_flags & SF_INPROGRESS) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

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

	/* copy in the user details */
	for (i = 0, up = sp->st_ud; i < sp->st_nud; i++, up++) {
		up->us_sysid = (int) VM_SSYSID(mp, i);
		up->us_ptype = (i == 0) ? PT_MTCM : PT_STCM;
		up->us_state = SS_NOTSYNCED;
	}

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

/*
**	op_snrm() - remove a snid (i.e., an auto-sync sequence)
*/

void op_snrm(pp)
struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct stab *sp;
	register struct ustab *up;

	/* 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_SNRM_NVALUE) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Tsyncd, 4, "OP_SNRM: snid = %s", tet_l2a(VM_SNID(mp)));

	/* find the stab element */
	if ((sp = stafind(VM_SNID(mp))) == (struct stab *) 0) {
		pp->ptm_rc = ER_SNID;
		return;
	}

	/* ensure that the element belongs to this client */
	if (sp->st_ptab != pp) {
		pp->ptm_rc = ER_PERM;
		return;
	}

	/* ensure that no sync event is in progress */
	if (sp->st_flags & SF_INPROGRESS) {
		pp->ptm_rc = ER_INPROGRESS;
		return;
	}

	/* ensure that none of the participants are still logged on */
	for (up = sp->st_ud; up < sp->st_ud + sp->st_nud; up++)
		switch (up->us_state) {
		case SS_DEAD:
			break;
		case SS_NOTSYNCED:
			if (!up->us_ptab)
				break;
			/* else fall through */
		default:
			pp->ptm_rc = ER_INPROGRESS;
			return;
		}

	/* all OK so remove the stab element and return success */
	strm(sp);
	stfree(sp);
	pp->ptm_rc = ER_OK;
}

/*
**	op_async() - process an automatic sync request
*/

void op_async(pp)
struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct stab *sp;

	/* all error reply messages (from here) have no data -
		a successful request does not send a reply message yet */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

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

#ifndef NOTRACE
	if (tet_Tsyncd > 0) {
		register long spno = VM_SPNO(mp);
		TRACE6(tet_Tsyncd, 4, "OP_ASYNC: snid = %s, xrid = %s, spno = %s, vote = %s, timeout = %s",
			tet_l2a(VM_SNID(mp)), tet_l2a(VM_XRID(mp)),
			tet_l2a(spno), tet_ptsvote((int) VM_SVOTE(mp)),
			tet_l2a(VM_STIMEOUT(mp)));
		TRACE4(tet_Tsyncd, 4, "OP_ASYNC: decode spno: ICno = %s, TPno = %s, flag = %s",
			tet_i2a(EX_ICNO(spno)), tet_i2a(EX_TPNO(spno)),
			EX_FLAG(spno) == S_TPEND ? "END" : "START");
	}
#endif

	/* check for a -ve spno */
	if (VM_SPNO(mp) < 0L) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* find the stab element */
	if ((sp = stafind(VM_SNID(mp))) == (struct stab *) 0) {
		pp->ptm_rc = ER_SNID;
		return;
	}

	/* perform common sync processing */
	op_s2(pp, sp, mp);

	/* force end of any user syncs involving this system */
	stuend(sp->st_snid, pp->ptr_sysid);

	/* update the xrid field if necessary */
	if (pp->pt_state == PS_WAITSYNC && sp->st_xrid < 0L && VM_XRID(mp) > 0L)
		sp->st_xrid = VM_XRID(mp);
}

/*
**	op_usync() - process a user sync request
*/

void op_usync(pp)
struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct stab *sp;
	register struct ustab *up;
	register int i, j, nsys;
	struct stab tmp;

	/* all error reply messages (from here) have no data -
		a successful request does not send a reply message yet */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

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

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

#ifndef NOTRACE
	if (tet_Tsyncd > 0) {
		TRACE6(tet_Tsyncd, 4, "OP_USYNC: snid = %s, xrid = %s, spno = %s, vote = %s, timeout = %s",
			tet_l2a(VM_SNID(mp)), tet_l2a(VM_XRID(mp)),
			tet_l2a(VM_SPNO(mp)), tet_ptsvote((int) VM_SVOTE(mp)),
			tet_l2a(VM_STIMEOUT(mp)));
		TRACE3(tet_Tsyncd, 4,
			"OP_USYNC: sync data len = %s, smflags = %s",
			tet_l2a(VM_MSDLEN(mp)), smflags((int) VM_MSFLAGS(mp)));
		for (i = 0; i < nsys; i++)
			TRACE3(tet_Tsyncd, 6,
				"OP_USYNC: sysid = %s, ptype = %s",
				tet_l2a(VM_SSYSID(mp, i)),
				tet_ptptype((int) VM_SPTYPE(mp, i)));
	}
#endif

	/* check for a -ve spno */
	if (VM_SPNO(mp) < 0L) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* make sure that there are no duplicate sysids in the list */
	for (i = 0; i < nsys; i++) {
		if ((int) VM_SSYSID(mp, i) < 0) {
			pp->ptm_rc = ER_INVAL;
			return;
		}
		switch ((int) VM_SPTYPE(mp, i)) {
		case PT_MTCC:
		case PT_STCC:
		case PT_MTCM:
		case PT_STCM:
		case PT_XRESD:
			break;
		default:
			pp->ptm_rc = ER_INVAL;
			return;
		}
		for (j = 0; j < i; j++)
			if (VM_SSYSID(mp, j) == VM_SSYSID(mp, i) &&
				VM_SPTYPE(mp, j) == VM_SPTYPE(mp, i)) {
					pp->ptm_rc = ER_DUPS;
					return;
			}
	}

	/* allocate a scratchpad per-user details area and fill it in */
	bzero((char *) &tmp, sizeof tmp);
	tmp.st_snid = VM_SNID(mp);
	tmp.st_xrid = VM_XRID(mp);
	tmp.st_flags = SF_USYNC;
	tmp.st_smsysid = -1;
	tmp.st_smspno = -1L;
	if (ustalloc(&tmp, nsys + 1) < 0) {
		pp->ptm_rc = ER_ERR;
		return;
	}
	up = tmp.st_ud;
	up->us_sysid = pp->ptr_sysid;
	up->us_ptype = pp->ptr_ptype;
	up++;
	for (i = 0; i < nsys; i++) {
		up->us_sysid = (int) VM_SSYSID(mp, i);
		up->us_ptype = (int) VM_SPTYPE(mp, i);
		up->us_state = SS_NOTSYNCED;
		up++;
	}

	/* see if an element already exists for this event -
		if not, create one and fill it in */
	if ((sp = stufind(VM_XRID(mp), tmp.st_ud, tmp.st_nud)) == (struct stab *) 0)
		if ((sp = stalloc()) == (struct stab *) 0) {
			TRACE2(tet_Tbuf, 6, "free tmp ustab (1) = %s",
				tet_i2x(tmp.st_ud));
			free((char *) tmp.st_ud);
			pp->ptm_rc = ER_ERR;
			return;
		}
		else {
			*sp = tmp;
			stadd(sp);
		}
	else {
		TRACE2(tet_Tbuf, 6, "free tmp ustab (2) = %s",
			tet_i2x(tmp.st_ud));
		free((char *) tmp.st_ud);
	}

	/* perform common sync processing */
	op_s2(pp, sp, mp);
}

/*
**	op_s2() - common sync processing
**
**	on return:
**		if successful, process state is set to PS_WAITSYNC; this
**		inhibits the sending of a reply message
**		otherwise, process state is unchanged and the reason for
**		failure is in the message return code
*/

static void op_s2(pp, sp, mp)
register struct ptab *pp;
register struct stab *sp;
struct valmsg *mp;
{
	register struct sptab *stp = (struct sptab *) pp->pt_sdata;
	register struct ustab *up, *myup;
	register time_t now = time((time_t *) 0);
	struct ptab *upp;
	struct sptab *ustp;

	/* find per-user details for this process */
	myup = (struct ustab *) 0;
	for (up = sp->st_ud; up < sp->st_ud + sp->st_nud; up++)
		if (
			up->us_sysid == pp->ptr_sysid && (
				((sp->st_flags & SF_USYNC) == 0) ||
				up->us_ptype == pp->ptr_ptype
			)
		) {
			myup = up;
			break;
		}
	if (!myup) {
		pp->ptm_rc = ER_SYSID;
		return;
	}

	/* see if the event has already timed out */
	if (myup->us_ntimedout > 0)
		if (VM_SPNO(mp) <= sp->st_lastspno) {
			myup->us_ntimedout--;
			pp->ptm_rc = ER_DONE;
			return;
		}
		else
			myup->us_ntimedout = 0;

	/* update per-user details for this process */
	myup->us_spno = VM_SPNO(mp);
	myup->us_state = (VM_SVOTE(mp) == SV_YES) ? SS_SYNCYES : SS_SYNCNO;
	myup->us_ptab = pp;
	myup->us_smflags = VM_MSFLAGS(mp);

	/* process sync message data if there is any */
	if (VM_MSFLAGS(mp) & SM_SNDMSG)
		procmsdata(sp, myup, mp);

	/* update other participants timeouts */
	for (up = sp->st_ud; up < sp->st_ud + sp->st_nud; up++)
		if (up != myup && (upp = up->us_ptab) != (struct ptab *) 0 &&
			upp->pt_state == PS_WAITSYNC) {
				ustp = (struct sptab *) upp->pt_sdata;
				if (ustp->sp_timeout >= 0L)
					upp->pt_timeout = ustp->sp_timeout + now;
		}

	/* update our ptab details */
	stp->sp_stab = sp;
	if ((stp->sp_timeout = VM_STIMEOUT(mp)) >= 0L)
		pp->pt_timeout = stp->sp_timeout + now;
	if (stp->sp_xrid < 0L && VM_XRID(mp) > 0L)
		stp->sp_xrid = VM_XRID(mp);
	pp->pt_state = PS_WAITSYNC;

	/* see if the event has happened yet */
	sp->st_flags |= SF_INPROGRESS;
	stcheck(sp);
}

/*
**	procmsdata() - process sync message data from a sending system
*/

static void procmsdata(sp, up, mp)
register struct stab *sp;
register struct ustab *up;
register struct valmsg *mp;
{
	/* see if another system has already claimed to send us message data;
		if it has:
			set a flag
			return if the other system sent at least one byte,
			or we are sending zero bytes
	*/
	if (sp->st_smsysid >= 0) {
		sp->st_smflags |= SM_DUP;
		if (sp->st_smlen > 0 || (int) VM_MSDLEN(mp) <= 0)
			return;
	}

	/* here if this is the first system to send us at least one byte of
		message data - grow the data buffer if necessary */
	if (BUFCHK(&sp->st_smdata, &sp->st_smdlen, (int) VM_MSDLEN(mp)) < 0) {
		if (sp->st_smdlen <= 0) {
			sp->st_smlen = 0;
			sp->st_smflags |= SM_TRUNC;
		}
		return;
	}

	/* copy in the message data and mark it as ours */
	if ((sp->st_smlen = (int) VM_MSDLEN(mp)) > 0)
		bcopy(VM_MSDATA(mp), sp->st_smdata, sp->st_smlen);
	sp->st_smflags |= (up->us_smflags & SM_TRUNC);
	sp->st_smsysid = up->us_sysid;
	sp->st_smspno = up->us_spno;
}