summaryrefslogtreecommitdiff
path: root/src/tet3/xresd/xresd.c
blob: bf4d472b3de7fb3537787a30b991451d5d4df579 (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
/*
 *      SCCS:  @(#)xresd.c	1.12 (98/09/01) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992, 1996 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:   	@(#)xresd.c	1.12 98/09/01 TETware release 3.3
NAME:		xresd.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	server-specific routines for xresd server

MODIFICATIONS:
	Denis McConalogue, UniSoft Limited, June 1993
	XTI enhancements - fill in name of transport provider
			   interface from -P command line option.

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

	Denis McConalogue, UniSoft Limited, September 1993
	tet_ss_logon() - allow more than one MTCC to log on

	Denis McConalogue, UniSoft Limited, September 1993
	added ss_disconnect() function

	Andrew Dingwall, UniSoft Ltd., November 1993
	enhancements for FIFO transport interface
	moved XTI-specific stuff to xresd_xt.c
	removed disconnect stuff

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

	Andrew Dingwall, UniSoft Ltd., July 1998
	Added support for shared API libraries.
 

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

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "bstring.h"
#include "ptab.h"
#include "sptab.h"
#include "xtab.h"
#include "error.h"
#include "globals.h"
#include "ltoa.h"
#include "xresd.h"
#include "server.h"
#include "servlib.h"
#include "dtetlib.h"
#include "tslib.h"

char *Tet_savedir;			/* saved files directory */


/************************************************************************
**									*
**	MAIN ROUTINES							*
**									*
************************************************************************/

int main(argc, argv)
int argc;
char **argv;
{
	/* must be first */
	tet_init_globals("tetxresd", PT_XRESD, 0, tet_generror, tet_genfatal);
	tet_root[0] = '\0';

	return(tet_si_main(argc, argv, 1));
}

/*
**	tet_ss_argproc() - xresd command-line argument processing
**
**	return 0 if only first arg was used or 1 if both args were used
*/

int tet_ss_argproc(firstarg, nextarg)
char *firstarg, *nextarg;
{
	register int rc;

	switch (*(firstarg + 1)) {
	case 's':
		if (*(firstarg + 2)) {
			Tet_savedir = firstarg + 2;
			rc = 0;
		}
		else {
			Tet_savedir = nextarg;
			rc = 1;
		}
		break;
	default:
		rc = ss_tsargproc(firstarg, nextarg);
		break;
	}

	return(rc);
}

/*
**	tet_ss_initdaemon() - xresd daemon initialisation
*/

void tet_ss_initdaemon()
{
	/* make sure that we have a Tet_savedir from the command line */
	if (!Tet_savedir)
		fatal(0, "must specify a savefiles directory", (char *) 0);

	/* perform xresd transport-specific initialisation */
	ss_tsinitb4fork();

	/* start the daemon */
	tet_si_forkdaemon();

	/* detach from the control terminal */
	tet_tiocnotty();
}

/*
**	tet_ss_serverloop() - xresd main processing loop
**
**	return 1 to be recalled, 0 otherwise
*/

int tet_ss_serverloop()
{
	extern struct ptab *tet_ptab;

	/* perform the generic server loop */
	tet_si_serverloop();

	/* exit if there are no more connected processes,
		otherwise arrange to come back */
	return(tet_ptab ? 1 : 0);
}

/************************************************************************
**									*
**	SERVER-SPECIFIC PARTS OF GENERIC SERVICE ROUTINES		*
**									*
************************************************************************/

/*
**	tet_ss_dead() - server-specific routine to handle a dead process
**
**	server logoff routines should not come here
*/

void tet_ss_dead(pp)
register struct ptab *pp;
{
	static char fmt[] = "%s connection closed";
	static char cl[] = "client";
	static char se[] = "server";
	char msg[sizeof fmt + sizeof cl];

	/* emit a diagnostic message if this is unexpected */
	if ((pp->pt_flags & PF_LOGGEDOFF) == 0) {
		(void) sprintf(msg, fmt, (pp->pt_flags & PF_SERVER) ? se : cl);
		error(0, msg, tet_r2a(&pp->pt_rid));
	}

	pp->pt_flags = (pp->pt_flags & ~PF_LOGGEDON) | PF_LOGGEDOFF;

	/* for clients, remove the related table entries
		and exit if the process table is empty */
	if ((pp->pt_flags & PF_SERVER) == 0) {
		xtdead(pp);
		tfdead(pp);
		tet_so_dead(pp);
	}
}

/************************************************************************
**									*
**	FULL REQUEST PROCESSING ROUTINES				*
**									*
************************************************************************/

/*
**	tet_ss_process() - request processing routine
*/

void tet_ss_process(pp)
struct ptab *pp;
{
	/* a server ptab might get here via tet_si_servwait() */
	if ((pp->pt_flags & PF_SERVER) == 0)
		tet_si_serverproc(pp);
}

/*
**	tet_ss_serverproc() - request processing as a server
*/

void tet_ss_serverproc(pp)
register struct ptab *pp;
{
	switch (pp->ptm_req) {
	case OP_XROPEN:
		op_xropen(pp);
		break;
	case OP_XRCLOSE:
		op_xrclose(pp);
		break;
	case OP_XRSEND:
		op_xrsend(pp);
		break;
	case OP_XRSYS:
		op_xrsys(pp);
		break;
	case OP_ICSTART:
		op_icstart(pp);
		break;
	case OP_TPSTART:
		op_tpstart(pp);
		break;
	case OP_ICEND:
		op_icend(pp);
		break;
	case OP_TPEND:
		op_tpend(pp);
		break;
	case OP_XRES:
		op_xres(pp);
		break;
	case OP_RESULT:
		op_result(pp);
		break;
	case OP_TFOPEN:
		op_tfopen(pp);
		break;
	case OP_TFCLOSE:
		op_tfclose(pp);
		break;
	case OP_TFWRITE:
		op_tfwrite(pp);
		break;
	case OP_FOPEN:
		tet_op_fopen(pp);
		break;
	case OP_FCLOSE:
		tet_op_fclose(pp);
		break;
	case OP_GETS:
		tet_op_gets(pp);
		break;
	case OP_CFNAME:
		op_cfname(pp);
		break;
	case OP_CODESF:
		op_codesf(pp);
		break;
	case OP_RCFNAME:
		op_rcfname(pp);
		break;
	default:
		pp->ptm_rc = ER_REQ;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		break;
	}

	/* here to send a reply message */
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags = (pp->pt_flags & ~PF_IODONE) | PF_ATTENTION;
}

/************************************************************************
**									*
**	SERVER-SPECIFIC PARTS OF GENERIC REQUEST PROCESSING		*
**									*
************************************************************************/

/*
**	tet_ss_logon() - server-specific logon processing routine
**
**	return ER_OK if successful or other ER_* error code on error
*/

/* ARGSUSED */
int tet_ss_logon(pp)
/* register */ struct ptab *pp;
{
	register struct ptab *pp1, *pp2;
	register int count;
	extern struct ptab *tet_ptab;

	/* make sure that we only have one MTCC logged on */
	switch (pp->ptr_ptype) {
	case PT_MTCC:
		pp1 = tet_ptab;
		count = 0;
		while ((pp2 = tet_getnextptbyptype(pp->ptr_ptype, pp1)) != (struct ptab *) 0) {
			if (pp2 != pp)
				count++;
			if ((pp1 = pp2->pt_next) == (struct ptab *) 0)
				break;
		}
		if (count > 0) {
			error(0, "client of this type already logged on",
				tet_r2a(&pp->pt_rid));
			return(ER_LOGON);
		}
		break;
	}

	return(ER_OK);
}

/*
**	tet_ss_logoff() - server-specific logoff processing
*/

void tet_ss_logoff(pp)
struct ptab *pp;
{
	xtdead(pp);
	tfdead(pp);
}

/*
**	tet_ss_cleanup() - clean up and exit
*/

void tet_ss_cleanup()
{
	tet_ts_cleanup();
	exit(0);
}

/************************************************************************
**									*
**	PUBLIC SUBROUTINES						*
**									*
************************************************************************/

/*
**	tet_ss_ptalloc() - allocate server-specific data element in a ptab
**		structure
**
**	return 0 if successful or -1 on error
*/

int tet_ss_ptalloc(pp)
struct ptab *pp;
{
	register struct sptab *sp;

	errno = 0;
	if ((sp = (struct sptab *) malloc(sizeof *sp)) == (struct sptab *) 0) {
		error(errno, "can't get memory for ss data", (char *) 0);
		pp->pt_sdata = (char *) 0;
		return(-1);
	}
	TRACE2(tet_Tbuf, 6, "allocate sptab = %s", tet_i2x(sp));
	bzero((char *) sp, sizeof *sp);

	sp->sp_xrid = -1L;

	pp->pt_sdata = (char *) sp;

	return(0);
}

/*
**	tet_ss_ptfree() - free server-specific data element in a ptab structure
*/

void tet_ss_ptfree(pp)
struct ptab *pp;
{
	register struct sptab *sp = (struct sptab *) pp->pt_sdata;

	TRACE2(tet_Tbuf, 6, "free sptab = %s", tet_i2x(sp));

	if (sp) {
		free((char *) sp);
		pp->pt_sdata = (char *) 0;
	}
}

/*
**	tet_ss_newptab() - server-specific new ptab entry handler
*/

void tet_ss_newptab(pp)
struct ptab *pp;
{
	/* add the entry to the process table */
	tet_ptadd(pp);
}