summaryrefslogtreecommitdiff
path: root/src/tet3/xresd/xtab.c
blob: 207b1324248fd2f2036906b977ba05415d46b1bb (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
/*
 *      SCCS:  @(#)xtab.c	1.8 (97/06/02) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 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.
 *
 * 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:   	@(#)xtab.c	1.8 97/06/02 TETware release 3.3
NAME:		xtab.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	execution results table administration functions

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., September 1996
	changes to enable TCC to own an xres id (instead of MTCM)

	Andrew Dingwall, UniSoft Ltd., June 1997
	prevent XRID wrapping around through zero

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

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

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "xtab.h"
#include "valmsg.h"
#include "error.h"
#include "ltoa.h"
#include "llist.h"
#include "bstring.h"
#include "dtetlib.h"
#include "xresd.h"

static struct xtab *xtab;		/* ptr to head of xres table */


/*
**	xtalloc() - allocate an xres table element and return a pointer thereto
**
**	return (struct xtab *) 0 on error
*/

struct xtab *xtalloc()
{
	register struct xtab *xp;
	static long xrid;

	/* allocate a new XRES ID */
	if (++xrid < 0L) {
		error(0, "too many XRES IDs", (char *) 0);
		return((struct xtab *) 0);
	}

	/* allocate memory for the xtab element and fill it in */
	errno = 0;
	if ((xp = (struct xtab *) malloc(sizeof *xp)) == (struct xtab *) 0) {
		error(errno, "can't allocate xtab element", (char *) 0);
		return((struct xtab *) 0);
	}
	TRACE2(tet_Tbuf, 6, "allocate xtab = %s", tet_i2x(xp));
	bzero((char *) xp, sizeof *xp);

	xp->xt_xrid = xrid;

	return(xp);
}

/*
**	xtfree() - close files and free an xres table element
*/

void xtfree(xp)
struct xtab *xp;
{
	TRACE2(tet_Tbuf, 6, "free xtab = %s", tet_i2x(xp));

	if (xp) {
		if (xp->xt_xfp)
			(void) fclose(xp->xt_xfp);
		if (xp->xt_xfname) {
			TRACE2(tet_Tbuf, 6, "free xtab xfname = %s",
				tet_i2x(xp->xt_xfname));
			free(xp->xt_xfname);
		}
		if (xp->xt_ud) {
			TRACE2(tet_Tbuf, 6, "free uxtab = %s",
				tet_i2x(xp->xt_ud));
			free((char *) xp->xt_ud);
		}
		free((char *) xp);
	}
}

/*
**	uxtalloc() - allocate an xtab per-user details element or grow an
**		existing one
**
**	return 0 if successful or -1 on error
*/

int uxtalloc(xp, nud)
struct xtab *xp;
int nud;
{
	register int needlen;

	ASSERT(xp);
	needlen = nud * sizeof *xp->xt_ud;

	if (BUFCHK((char **) &xp->xt_ud, &xp->xt_udlen, needlen) < 0) {
		if (xp->xt_udlen == 0)
			xp->xt_nud = 0;
		return(-1);
	}
	bzero((char *) xp->xt_ud, needlen);

	xp->xt_nud = nud;
	return(0);
}

/*
**	xtadd() - insert an element in the xtab list
*/

void xtadd(xp)
struct xtab *xp;
{
	tet_listinsert((struct llist **) &xtab, (struct llist *) xp);
}

/*
**	xtrm() - remove an element from the xtab list
*/

void xtrm(xp)
struct xtab *xp;
{
	if (xp->xt_flags & XF_TPINPROGRESS)
		(void) tpend(xp);
	if (xp->xt_flags & XF_ICINPROGRESS)
		(void) icend(xp);

	tet_listremove((struct llist **) &xtab, (struct llist *) xp);
}

/*
**	xtfind() - find an xtab element matching xrid and return a pointer
**		thereto
**
**	return (struct xtab *) 0 if none can be found
*/

struct xtab *xtfind(xrid)
register long xrid;
{
	register struct xtab *xp;

	for (xp = xtab; xp; xp = xp->xt_next)
		if (xp->xt_xrid == xrid)
			break;

	return(xp);
}

/*
**	xtdead() - xtab processing when a connection closes
*/

void xtdead(pp)
register struct ptab *pp;
{
	register struct xtab *xp;
	register struct uxtab *up;
	register int count, done;

	/* find related xres table entries and update them
		remove an entry that is no longer required */
	do {
		done = 1;
		for (xp = xtab; xp; xp = xp->xt_next) {
			count = 0;
			if (xp->xt_ptab == pp)
				xp->xt_ptab = (struct ptab *) 0;
			for (up = xp->xt_ud; up < xp->xt_ud + xp->xt_nud; up++)
				if (up->ux_ptab == pp) {
					up->ux_state = XS_DEAD;
					up->ux_ptab = (struct ptab *) 0;
					count++;
				}
				else if (up->ux_state == XS_DEAD)
					count++;
			if (count == xp->xt_nud) {
				if (xp->xt_flags & XF_TPINPROGRESS)
					(void) tpend(xp);
				if (xp->xt_flags & XF_ICINPROGRESS)
					(void) icend(xp);
				if (!xp->xt_ptab) {
					xtrm(xp);
					xtfree(xp);
					done = 0;
					break;
				}
			}
		}
	} while (!done);
}