summaryrefslogtreecommitdiff
path: root/src/tet3/tccd/stcc.c
blob: a3b91f88bdb94bd3c19322f1b215fbe52fb5db31 (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
/*
 *      SCCS:  @(#)stcc.c	1.16 (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:   	@(#)stcc.c	1.16 98/09/01 TETware release 3.3
NAME:		stcc.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	May 1992

DESCRIPTION:
	MTCC request processing routines

MODIFICATIONS:
	Denis McConalogue, UniSoft Limited, August 1993
	added op_rcopy() routine.

	Denis McConalogue, UniSoft Limited, September 1993
	don't use errno after tet_fcopy().		

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

	Andrew Dingwall, UniSoft Ltd., August 1996
	changes for TETware

	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 <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#  include <unistd.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "error.h"
#include "ltoa.h"
#include "avmsg.h"
#include "valmsg.h"
#include "servlib.h"
#include "dtetlib.h"
#include "tccd.h"
#include "tcclib.h"

/* mode for created directories */
#define DIRMODE	((mode_t) (S_IRWXU | S_IRWXG | S_IRWXO))


/*
**	op_putenv() - put strings in the environment
**
**	strings put in the environment by OP_PUTENV are stored in memory
**	obtained from malloc()
**	if an OP_PUTENV request overwrites an existing environment string that
**	is itself stored in malloc'd memory, there is no provision for freeing
**	that memory
**
**	this is not usually a problem, however, for the typical use of this
**	request to set up an environment to be passed to a STCM via exec()
*/

void op_putenv(pp)
struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register char *p1, *p2;
	register int n;
	char buf[128];

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

	/* do a sanity check on the environment strings */
	for (n = 0; n < OP_PUTENV_NLINE(mp); n++) {
		if ((p1 = AV_ENVAR(mp, n)) == (char *) 0 || !*p1) {
			pp->ptm_rc = ER_INVAL;
			return;
		}
		TRACE2(tet_Ttccd, 4, "receive env string = \"%s\"", p1);
		if (!tet_equindex(p1)) {
			pp->ptm_rc = ER_INVAL;
			return;
		}
	}

	/*
	** examine each string:
	**
	**	remember a value for TET_ROOT
	**
	**	if this variable is already in the environment with the
	**	same value, skip it;
	**	otherwise, put the string in the environment
	*/
	for (n = 0; n < OP_PUTENV_NLINE(mp); n++) {
		p1 = tet_equindex(AV_ENVAR(mp, n));
		ASSERT(p1);
		(void) sprintf(buf, "%.*s",
			TET_MIN((int) (p1 - AV_ENVAR(mp, n)), (int) sizeof buf - 1),
			AV_ENVAR(mp, n));
		if (!strcmp(buf, "TET_ROOT") && tetrootset(p1 + 1) < 0)
			break;
		if ((p2 = getenv(buf)) != (char *) 0 && !strcmp(p1 + 1, p2))
			continue;
		if ((p1 = tet_strstore(AV_ENVAR(mp, n))) == (char *) 0 ||
			tet_putenv(p1) < 0)
				break;
	}

	pp->ptm_rc = (n == OP_PUTENV_NLINE(mp)) ? ER_OK : ER_ERR;
}

/*
**	op_access() - determine the accessibility of a file wrt euid and egid
*/

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

	/* 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->av_argc != OP_ACCESS_ARGC || !AV_PATH(mp) || !*AV_PATH(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_ACCESS: path = \"%s\"", AV_PATH(mp));

	/* check the access permissions */
	if (tet_eaccess(AV_PATH(mp), (int) AV_MODE(mp)) < 0) {
		pp->ptm_rc = tet_maperrno(errno);
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_mkdir() - make a directory
*/

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

	/* 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->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_MKDIR: dir = \"%s\"", AV_DIR(mp));

	/* make the directory */
	if (tet_mkdir(AV_DIR(mp), DIRMODE) < 0) {
		pp->ptm_rc = tet_maperrno(errno);
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_mkalldirs() - make directories recursively
*/

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

	/* 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->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_MKALLDIRS: dir = \"%s\"", AV_DIR(mp));

	/* make the directory */
	if (tet_mkalldirs(AV_DIR(mp)) < 0) {
		pp->ptm_rc = tet_maperrno(errno);
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_rmdir() - remove a directory
*/

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

	/* 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->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_RMDIR: dir = \"%s\"", AV_DIR(mp));

	/* remove the directory */
	if (tet_rmdir(AV_DIR(mp)) < 0) {
		pp->ptm_rc = tet_maperrno(errno);
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_rmalldirs() - remove a directory subtree
*/

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

	/* 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->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_RMALLDIRS: dir = \"%s\"", AV_DIR(mp));

	/* remove the directory subtree */
	if (tcf_rmrf(AV_DIR(mp)) < 0) {
		pp->ptm_rc = tet_maperrno(errno);
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_rcopy() - recursively copy files
*/

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

	/* 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->av_argc != OP_RCOPY_ARGC || !AV_XFROM(mp) || !*AV_XFROM(mp)
					 || !AV_XTO(mp)   || !*AV_XTO(mp))  {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE3(tet_Ttccd, 4, "OP_RCOPY: from = \"%s\", to = \"%s\"",
		AV_XFROM(mp), AV_XTO(mp));

	/* do the copy*/
	if (tet_fcopy(AV_XFROM(mp), AV_XTO(mp)) < 0) {
		pp->ptm_rc = ER_ERR;
		return;
	}

	pp->ptm_rc = ER_OK;
}
/*
**	op_chdir() - change directory
*/

void op_chdir(pp)
struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register int errsave;

	/* 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->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* change directory */
	TRACE2(tet_Ttccd, 4, "OP_CHDIR: chdir to \"%s\"", AV_DIR(mp));
	if (CHDIR(AV_DIR(mp)) < 0) {
		errsave = errno;
		if ((pp->ptm_rc = tet_maperrno(errsave)) == ER_ERR)
			error(errsave, "can't chdir to", AV_DIR(mp));
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_unlink() - unlink a file
*/

void op_unlink(pp)
struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register int errsave;

	/* 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->av_argc != OP_UNLINK_ARGC || !AV_FNAME(mp) || !*AV_FNAME(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_UNLINK: file = \"%s\"", AV_FNAME(mp));

	/* unlink the file */
	if (UNLINK(AV_FNAME(mp)) < 0) {
		errsave = errno;
		if ((pp->ptm_rc = tet_maperrno(errsave)) == ER_ERR)
			error(errsave, "can't unlink", AV_FNAME(mp));
		return;
	}

	pp->ptm_rc = ER_OK;
}

/*
**	op_time() - return system time
*/

void op_time(pp)
struct ptab *pp;
{
	register struct valmsg *mp;

	TRACE1(tet_Ttccd, 4, "OP_TIME");

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

	/* all OK so fill in the reply message and return */
	VM_TIME(mp) = time((time_t *) 0);
	pp->ptm_rc = ER_OK;
	pp->ptm_mtype = MT_VALMSG;
	pp->ptm_len = valmsgsz(OP_TIME_NVALUE);
}

/*
**	op_lockfile() - create a lock file (exclusive lock)
**
**	a zero timeout means return immediately if the operation fails
**	a -ve timeout means try indefinately until a lock is obtained or an
**	error occurs
*/

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

	/* 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->av_argc != OP_LOCKFILE_ARGC || !AV_FNAME(mp) || !*AV_FNAME(mp)) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	TRACE3(tet_Ttccd, 4, "OP_LOCKFILE: file = %s, timeout = %s",
		AV_FNAME(mp), tet_l2a(AV_TIMEOUT(mp)));

	/* call the tcc action function to create the lock file */
	pp->ptm_rc = tcf_lockfile(AV_FNAME(mp), AV_TIMEOUT(mp));
}

/*
**	op_sharelock() - create a file in a lock directory (shared lock)
**
**	a zero timeout means return immediately if the operation fails
**	a -ve timeout means try indefinately until a lock is obtained or an
**	error occurs
*/

void op_sharelock(pp)
struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	char *fname;

	/* do a sanity check on the request message */
	if (mp->av_argc != OP_SHARELOCK_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	TRACE3(tet_Ttccd, 4, "OP_SHARELOCK: dir = %s, timeout = %s",
		AV_DIR(mp), tet_l2a(AV_TIMEOUT(mp)));

	/* call the tcc action function to create the shared lock file */
	if ((pp->ptm_rc = tcf_sharelock(AV_DIR(mp), pp->ptr_pid, AV_TIMEOUT(mp), &fname)) == ER_OK) {
		TRACE2(tet_Ttccd, 4, "OP_SHARELOCK: return \"%s\"", fname);
		AV_FNAME(mp) = fname;
		mp->av_argc = OP_SHARELOCK_ARGC;
		pp->ptm_mtype = MT_AVMSG;
		pp->ptm_len = avmsgsz(OP_SHARELOCK_ARGC);
	}
	else {
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
	}
}

/*
**	op_mktmpdir() - make a unique temporary directory
*/

void op_mktmpdir(pp)
struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	char *dir;

	/* do a sanity check on the request message */
	if (mp->av_argc != OP_DIR_ARGC || !AV_DIR(mp) || !*AV_DIR(mp)) {
		pp->ptm_rc = ER_INVAL;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	TRACE2(tet_Ttccd, 4, "OP_MKTMPDIR: dir = \"%s\"", AV_DIR(mp));

	/* call the tcc action function to make the directory */
	if ((pp->ptm_rc = tcf_mktmpdir(AV_DIR(mp), &dir)) == ER_OK) {
		AV_DIR(mp) = dir;
		mp->av_argc = OP_DIR_ARGC;
		pp->ptm_mtype = MT_AVMSG;
		pp->ptm_len = avmsgsz(OP_DIR_ARGC);
	}
	else {
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
	}
}

/*
**	op_rxfile() - transfer a file to the master system
*/

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

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

	/* perform a sanity check on the request message */
	if (mp->av_argc != OP_RXFILE_ARGC ||
		!AV_XFROM(mp) || !*AV_XFROM(mp) ||
		!AV_XTO(mp) || !*AV_XTO(mp)) {
			pp->ptm_rc = ER_INVAL;
			return;
	}

	TRACE3(tet_Ttccd, 4, "OP_RXFILE: from = \"%s\", to = \"%s\"",
		AV_XFROM(mp), AV_XTO(mp));

	/* log on to the xresd if necessary */
	ASSERT(tet_xdptab);
	if ((tet_xdptab->pt_flags & PF_LOGGEDON) == 0 && tet_xdlogon() < 0) {
		pp->ptm_rc = ER_ERR;
		return;
	}

	pp->ptm_rc = (tet_xdxfile(AV_XFROM(mp), AV_XTO(mp)) < 0) ? ER_ERR : ER_OK;
}