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

DESCRIPTION:
	generic server request processing functions

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

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

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

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

#include <stdio.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "error.h"
#include "globals.h"
#include "server.h"
#include "dtetlib.h"

#if TESTING || !defined(NOTRACE)
#include "avmsg.h"
#endif


/* static function declarations */
static void logonfail PROTOLIST((struct ptab *, int));
static void op_logoff PROTOLIST((struct ptab *));
static void op_logon PROTOLIST((struct ptab *));
static void op_null PROTOLIST((struct ptab *));
static void op_trace PROTOLIST((struct ptab *));
#if TESTING
   static void op_print PROTOLIST((struct ptab *));
#endif


/*
**	tet_si_serverproc() - server-independent message processing
*/

void tet_si_serverproc(pp)
register struct ptab *pp;
{
	TRACE3(tet_Tserv, 4, "%s serverproc: request = %s",
		tet_r2a(&pp->pt_rid), tet_ptreqcode(pp->ptm_req));

	/* ensure process is logged on exactly once */
	if ((pp->ptm_req != OP_LOGON && (pp->pt_flags & PF_LOGGEDON) == 0) ||
		(pp->ptm_req == OP_LOGON && (pp->pt_flags & PF_LOGGEDON))) {
			pp->ptm_rc = ER_LOGON;
			pp->ptm_mtype = MT_NODATA;
			pp->ptm_len = 0;
			pp->pt_state = PS_SNDMSG;
			pp->pt_flags |= PF_ATTENTION;
			return;
	}

	/* set a message type and return code in case the processing routine
		forgets to set them */
	pp->ptm_mtype = MT_UNKNOWN;
	pp->ptm_rc = ER_INTERN;

	/* process the request */
	switch (pp->ptm_req) {
	case OP_LOGON:
		op_logon(pp);
		break;
	case OP_LOGOFF:
		op_logoff(pp);
		break;
	case OP_TRACE:
		op_trace(pp);
		break;
	case OP_NULL:
		op_null(pp);
		break;
#if TESTING
	case OP_PRINT:
		op_print(pp);
		break;
#endif
	default:
		tet_ss_serverproc(pp);
		break;
	}
}

/*
**	op_logon() - process a logon request
*/

static void op_logon(pp)
register struct ptab *pp;
{
	register struct ptab *q;
	register int errflag, rc;
	struct remid rid;

	/* see if process is already connected via another ptab entry -
		"can't happen" */
	if ((q = tet_getptbysyspid(pp->ptm_sysid, pp->ptm_pid)) != (struct ptab *) 0 && q != pp) {
		error(0, "process already connected!", tet_r2a(&q->pt_rid));
		logonfail(pp, ER_LOGON);
		return;
	}

	/* do some sanity checking on the remid */
	rid.re_sysid = pp->ptm_sysid;
	rid.re_pid = pp->ptm_pid;
	rid.re_ptype = pp->ptm_ptype;
	errflag = 0;
	if (pp->ptm_sysid < 0 || pp->ptm_pid <= 0) {
		errflag = 1;
	}
	else switch (pp->ptm_ptype) {
		case PT_MTCC:
		case PT_STCC:
		case PT_MTCM:
		case PT_STCM:
		case PT_XRESD:
		case PT_SYNCD:
			if (pp->ptm_ptype != tet_myptype)
				break;
			/* else fall through */
		default:
			errflag = 1;
			break;
	}

	if (errflag) {
		error(0, "bad remid in logon request:", tet_r2a(&rid));
		logonfail(pp, ER_INVAL);
		return;
	}

	/* call server-specific logon routine */
	pp->pt_rid = rid;
	if ((rc = tet_ss_logon(pp)) != ER_OK) {
		logonfail(pp, rc);
		return;
	}

	/* all ok so show process as logged on */
	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags = (pp->pt_flags & ~PF_LOGGEDOFF) | PF_LOGGEDON | PF_ATTENTION;
}

/*
**	logonfail() - send an error message after failed logon
*/

static void logonfail(pp, rc)
register struct ptab *pp;
int rc;
{
	pp->ptm_rc = rc;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	tet_si_servwait(pp, LONGDELAY);
	pp->pt_state = PS_DEAD;
	pp->pt_flags |= PF_ATTENTION;
}

/*
**	op_logoff() - process a logoff request
*/

static void op_logoff(pp)
register struct ptab *pp;
{
	/* call the server-specific logoff routine */
	tet_ss_logoff(pp);

	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags = (pp->pt_flags & ~PF_LOGGEDON) | PF_LOGGEDOFF | PF_ATTENTION;
}

/*
**	op_trace() - process a trace request
*/

static void op_trace(pp)
register struct ptab *pp;
{
#ifdef NOTRACE
	pp->ptm_rc = ER_TRACE;
#else
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;

	tet_traceinit((int) mp->av_argc + 1, mp->av_argv - 1);
	pp->ptm_rc = ER_OK;
#endif

	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags |= PF_ATTENTION;
}

/*
**	op_null() - process a null request
*/

static void op_null(pp)
register struct ptab *pp;
{
	/* do nothing successfully */
	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags |= PF_ATTENTION;
}

/*
**	op_print() - print some lines on standard output
**
**	for testing only - could block!!
*/

#if TESTING

static void op_print(pp)
register struct ptab *pp;
{

	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register int n;

	printf("%s: call to op_print(): argc = %d\n",
		tet_progname, mp->av_argc);
	for (n = 0; n < (int) mp->av_argc; n++)
		printf("%s\n", mp->av_argv[n]);
	fflush(stdout);

	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	pp->pt_state = PS_SNDMSG;
	pp->pt_flags |= PF_ATTENTION;
}

#endif /* TESTING */