summaryrefslogtreecommitdiff
path: root/comm/OLD/portserv.cc
blob: 4017b25d46ff1c56040968a6c5c58d002371a692 (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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Port server
 *
 *	$Id$
 *	$CurLog$
 */

#include "PortServerReq.h"
#include "PortServer.h"
#include "error.h"
#include "Multiplexer.h"
#include "Datagram.h"
#include "SignalHandler.h"
#include "ccu/List.h"
#include "ccu/RegExp.h"
#include "ccu/String.h"
#include "ccu/Time.h"

#include <stdlib.h>
#include <sys/file.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#ifdef __osf__
extern "C" {
#endif
#include <netdb.h>
#ifdef __osf__
}
#endif
#ifdef __osf__
extern "C" {
	int setsockopt (int, int, int, char*, int);
}
#endif

//---------------- globals
//	if SaveTime > 0, the state is saved to SaveFile
//		SaveTime seconds after a modification
//	if SaveTime < 0, saving is immediate
//	if SaveTime is 0, no save
//	if SaveFileIsFd >= 0, it is taken as output file descr instead of SaveFile
const char* SaveFile = 0;
int	SaveFileIsFd = -1;
int	SaveTime = 0;
int	Mod = 0;
int	NbEntries = 0;
bool	Trace = false;
UchMultiplexer* Mpx = 0;
const char* LoadFile;

#ifndef DEFAGENTLIB
#define DEFAGENTLIB "/usr/local/lib/agents"
#endif
char	DefLoadFile [256];

#ifndef DEFAGENTBIN
#define DEFAGENTBIN "/usr/local/lib/agents/bin"
#endif
const char*	BinDir = 0;

//---------------- constants
// if an auto-launch server has not registered for REG_TIME seconds
// and somebody inquires its address, it is killed and restarted.
#define REG_TIME	60000
#define REG_STR		"minute"

// if an auto-launch server has been run within the last ALR_TIME seconds
// and has not yet registered and somebody inquires its address,
// is is _not_ run.
#define ALR_TIME	30000
#define ALR_STR		"30 seconds"

struct Service {
	CcuString	Key;
	lword	Host;
	sword	Port;
	int	Ident;
	// auto-launch stuff
	bool	AutoLaunch;
	bool	Running;
	Millisecond	LastRun;
	Millisecond	LastRegistered;
	char*	CmdLine;	// the line
	char**	RunCmd;		// array to pointers to CmdLine (for execvp)
};

#ifndef CPLUS_BUG19
CcuListOf <Service> ServList;
#else
CcuList ServList;
#endif

//---------------- auto-launch stuff
char**
ParseConfigLine (char* line)
{
	enum WHERE { IN_SPACE, IN_WORD, IN_QUOTE };
	char quote = '\0';
	register char*	s = line;
	register char*	word = line;
	register WHERE	where = IN_SPACE;
	char* words [256];
	int nw = 0;
	
	// split a line into words
	// a word is a sequence of non blank characters,
	// or a quoted string (' or ").
	// '(', ')' and ',' are also ignored to allow for function like notation
	for (; *s; s++) {
		switch (where) {
		case IN_WORD:
			if (strchr (" \t(),", *s)) {
				*s = '\0';
				where = IN_SPACE;
				if (nw < 255)
					words [nw++] = word;
				else
					return 0;
			}
			break;
		case IN_QUOTE:
			if (*s == quote) {
				*s = '\0';
				where = IN_SPACE;
				if (nw < 255)
					words [nw++] = word;
				else
					return 0;
			}
			break;
		case IN_SPACE:
			if (*s == '\'' || *s == '\"') {
				quote = *s;
				*s = '\0';
				where = IN_QUOTE;
				word = s+1;
			} else
			if (strchr (" \t(),", *s)) {
				*s = '\0';
			} else {
				where = IN_WORD;
				word = s;
			}
			break;
		}
	}
	if (where != IN_SPACE)
		if (nw < 255)
			words [nw++] = word;
		else
			return 0;
	if (where == IN_QUOTE)
		return 0;
	
	// if no argv specified, argv[0] = executable
	if (nw == 3)
		words [nw++] = words [2];
	// expected format: key dir exec argv
	if (nw < 4)
		return 0;
	// null terminated array for execvp
	words [nw++] = 0;
	
	// return a freshly allocated array
	char** nwords = new char* [nw];
	for (int i = 0; i < nw; i++)
		nwords [i] = words [i];
	return nwords;
}

void
AutoLaunch (Service* s)
{
	// s->RunCmd [0]	service name
	// s->RunCmd [1]	directory
	// s->RunCmd [2]	executable
	// s->RunCmd [3 ... ]	argv [0 ...]

	CcuTimeStamp now;
	if (s->LastRun + ALR_TIME > now) {
//		Log ("AutoLaunch", "%s already launched in the past %s", s->RunCmd [0], ALR_STR);
		return;
	}
//	Log ("AutoLaunch", "running %s dir=%s, exec=%s", s->RunCmd[0], s->RunCmd[1], s->RunCmd[2]);
	if (! fork ()) {
		// close everything but stdio
		for (int i = 3; i < NFILE; i++)
			close (i);
		// detach from controlling terminal
		setsid ();
		// change directory. "-" is default
		const char* dir = s->RunCmd [1];
		if (strcmp (dir, "-") == 0)
			dir = BinDir;
		// change directory
		if (chdir (dir) < 0)
			SysError (ErrFatal, dir);
		// exec server
		if (execvp (s->RunCmd [2], s->RunCmd + 3) < 0)
			SysError (ErrFatal, s->RunCmd [2]);
	}
	s->LastRun = now;
}

void
LoadConfigFile ()
{
	FILE* f = fopen (LoadFile, "r");
	if (!f)
		SysError (ErrFatal, LoadFile);
	char line [2048];
	while (fgets (line, sizeof (line), f)) {
		// empty line or comment
		if (! line [0] || line [0] == '#')
			continue;
		
		// expected format: servername dir exec argv0 ...
		
		// remove trailing newline and parse
		line [strlen (line) -1] = 0;
		char* nline = new char [strlen (line) + 1];
		strcpy (nline, line);
		char** words = ParseConfigLine (nline);
		if (! words) {
			::Error (ErrWarn, "LoadConfigFile", "incorrect line ignored in file");
			delete nline;
			continue;
		}
		Service* s = new Service;
		char key [256];	// agent:audio
		sprintf (key, "agent:%s", words [0]);
		s->Key = key;
		s->Host = 0;
		s->Port = 0;
		s->Ident = 0;
		s->AutoLaunch = true;
		s->Running = false;
		s->LastRun= 0;
		s->LastRegistered = 0;
		s->CmdLine = nline;
		s->RunCmd = words;
		ServList.Append (s);
//		Log ("LoadConfigFile", "defined auto-launch server %s dir=%s exec=%s",
//			s->RunCmd [0], s->RunCmd [1], s->RunCmd [2]);
	}
	fclose (f);
}

void
KillProcesses (bool all)
{
	// kill all processes that have been autolaunched,
	// assuming that their id is their pid.
	Service* s;
#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
	while (s = *++iter) {
#else
	CcuListIter iter (ServList);
	while (s = (Service*) *++iter) {
#endif
		if ((all || s->AutoLaunch) && s->Ident) {
//			Log ("KillProcesses", "killing %s pid=%d", s->RunCmd [2], (void*) s->Ident);
			kill (s->Ident, SIGTERM);
		}
	}
}

Service*
RegisterService (const char* key, lword host, sword port, int id)
{
	Service* s = 0;

#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
	while (s = *++iter) {
#else
	CcuListIter iter (ServList);
	while (s = (Service*) *++iter) {
#endif
		if (strcmp (key, s->Key) == 0)
			break;
	}
	if (! s) {
		s = new Service;
		s->Key = key;
		s->AutoLaunch = false;
		s->LastRun = 0;
		s->CmdLine = 0;
		s->RunCmd = 0;
		ServList.Append (s);
	}
	s->Host = host;
	s->Port = port;
	s->Ident = id;
	CcuTimeStamp now;
	s->LastRegistered = now;
	s->Running = true;
	return s;
}

bool
RemoveService (const char* key, lword host, sword port, int id)
{
	Service* s;
#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
	CcuListIterOf <Service> trailing_iter (ServList);
#else
	CcuListIter iter (ServList);
	CcuListIter trailing_iter (ServList);
#endif
	while (++iter) {
#ifndef CPLUS_BUG19
		s = *iter;
#else
		s = (Service*) *iter;
#endif
		if (host == s->Host && port == s->Port && id == s->Ident && strcmp (key, s->Key) == 0) {
			if (s->AutoLaunch) {
				s->Running = false;
				s->LastRun = 0;
				s->LastRegistered = 0;
				s->Host = 0;
				s->Port = 0;
				s->Ident = 0;
			} else {
				s->Key = 0;
				delete s;
				ServList.RemoveAfter (trailing_iter);
			}
			return true;
		}
		++trailing_iter;
	}
	::Error (ErrWarn, key, "not found for remove");
	return false;
}

Service*
InquireService (const char* key)
{
	Service* s;
#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
#else
	CcuListIter iter (ServList);
#endif
	while (++iter) {
#ifndef CPLUS_BUG19
		s = *iter;
#else
		s = (Service*) *iter;
#endif

		if (strcmp (key, s->Key) != 0)
			continue;

		if (!s->AutoLaunch)
			return s;

		if (s->Running) {
			CcuTimeStamp now;
			if (s->LastRegistered + REG_TIME < now) {
//				Log ("Inquire", "%s not registered for the last %s: autolaunching it", s->RunCmd [0], REG_STR);
				if (s->Ident)
					kill (s->Ident, SIGTERM);
				s->Host = 0;
				s->Port = 0;
				s->Ident = 0;
				AutoLaunch (s);
			}
		} else
			AutoLaunch (s);
		return s;
	}
	::Error (ErrWarn, key, "not found");
	return 0;
}


int
Dump ()
{
	Service* s;
#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
#else
	CcuListIter iter (ServList);
#endif
	while (++iter) {
#ifndef CPLUS_BUG19
		s = *iter;
#else
		s = (Service*) *iter;
#endif
		printf ("<%s> 0x%lx:%d (%d)\n", (const char*) s->Key, s->Host, s->Port, s->Ident);
	}
	return 0;
}

int
DumpToFile ()
{
if (Trace) printf ("dumping\n");
	int fd = SaveFileIsFd;
	if (fd < 0 && (fd = creat (SaveFile, 0600)) < 0) {
		SysError (ErrWarn, SaveFile);
		return 0;
	}
	UchFilDes Fd (fd);
	char buf [256];
	Service* s;
#ifndef CPLUS_BUG19
	CcuListIterOf <Service> iter (ServList);
#else
	CcuListIter iter (ServList);
#endif
	while (++iter) {
#ifndef CPLUS_BUG19
		s = *iter;
#else
		s = (Service*) *iter;
#endif
		sprintf (buf, "0x%lx:%d %s\n", s->Host, s->Port, (const char*) s->Key);
		Fd.Write ((byte*) buf, strlen (buf));
	}
	Mod = 0;
	return 1;
}

//	for signal handlers
//
void
SigDump (int, int)
{
	Dump ();
}

void
SigDumpFile (int, int)
{
	DumpToFile ();
}

class RegistrationChannel : public UchDatagram {
protected:
	void	Match (const char*);
public:
		RegistrationChannel (sword);
	void	HandleRead ();
};

RegistrationChannel :: RegistrationChannel (sword port)
: UchDatagram (new UchInetAddress (ANYADDR, port), 0)
{
}

void
Modified ()
{
	if (! Mod && SaveTime > 0) {
		Mod++;
		alarm (SaveTime);
		return;
	}
	if (SaveTime < 0) {
		Mod++;
		DumpToFile ();
	}
}

void
RegistrationChannel :: HandleRead ()
{
	UchMsgBuffer buf (256);
	UchPortServerReq req;

	if (Trace) printf ("HandleRead\n");
	buf.Flush ();
	int n = Receive (buf);
	if (n <= 0) {
		SysError (ErrFatal, "Receive", EINTR);
		return;
	}

	if (! buf.ReadMsg (req)) {
		::Error (ErrWarn, "Receive", "could not read message");
		return;
	}
		
	// respond
	switch (req.Type) {
	case PortServRegister:
		if (Trace) printf ("register\n");
		RegisterService (req.Key, req.Host, req.Port, int (req.Ident));
		Modified ();
		break;
			
	case PortServRemove:
		if (Trace) printf ("remove\n");
		RemoveService (req.Key, req.Host, req.Port, int (req.Ident));
		Modified ();
		break;
				
	case PortServInquire:
		{
		if (Trace) printf ("inquire\n");
		UchPortServerReq ans;
		Service* inq = InquireService (req.Key);
		if (inq) {
			ans.Type = PortServAnswer;
			ans.Host = inq->Host;
			ans.Port = inq->Port;
		} else {
			ans.Type = PortServFail;
			ans.Host = 0;
			ans.Port = 0;
		}
		buf.Flush ();
		buf.WriteMsg (ans);
		if (Reply (buf) < 0)
			SysError (ErrWarn, "reply");
		}
		break;
			
	case PortServMatch:
		if (Trace) printf ("match\n");
		Match (req.Key);
		break;
				
	case PortServDump:
		if (Trace) printf ("dump\n");
		Dump ();
		break;
			
	case PortServQuit:
		if (Trace) printf ("quit\n");
		if (SaveTime)
			DumpToFile ();
		KillProcesses (true);
		Mpx->Close ();
		break;
			
	default:
if (Trace) printf ("unknown\n");
		::Error (ErrWarn, "Receive", "unknown request");

	}
}

void
RegistrationChannel :: Match (const char* key)
{
	UchMsgBuffer buf (256);
	UchPortServerReq ans (PortServMatch);
	int nb = 0;

	CcuRegExp re (key);
	if (re.Compile ()) {
		Service* s;
#ifndef CPLUS_BUG19
		CcuListIterOf <Service> iter (ServList);
		while (++iter) {
			s = *iter;
#else
		CcuListIter iter (ServList);
		while (++iter) {
			s = (Service*) *iter;
#endif
			if (re.Match (s->Key)) {
if (Trace) printf ("Match: %s\n", (const char*) s->Key);
				ans.Host = s->Host;
				ans.Port = s->Port;
				ans.SetKey (s->Key);
				buf.WriteMsg (ans);
				Reply (buf);
				nb++;
			}
		}
	} else
		nb = -1;
	ans.Type = PortServEndMatch;
	ans.Ident = nb;
	ans.SetKey (0);
	buf.WriteMsg (ans);
	Reply (buf);
}

//---------------- BroadcastChannel

class BroadcastChannel : public UchDatagram {
protected:
	sword	p;
	pUchAddress bcast;
	UchMsgBuffer	registerBuf;
	UchMsgBuffer	removeBuf;
	UchMsgBuffer	answerBuf;

static	lword	BroadCastAddress ();
	void	Answer ();

public:
		BroadcastChannel (sword, UchInetAddress*);
	bool SetUp ();
	void HandleRead ();
	void RegisterMe ()	{ printf ("RegisterMe: %d\n", Send (registerBuf, *bcast, true /*peek*/)); }
	void RemoveMe ()	{ Send (removeBuf, *bcast, true /*peek*/); printf ("sending remove\n"); }
};

lword
BroadcastChannel :: BroadCastAddress ()
{
// *** can't get broadcast to work on DECstation !!!
	char* bcast = getenv ("BroadcastChannel");
	unsigned int b1, b2, b3, b4;
	sscanf (bcast, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
	printf ("bacst = %d.%d.%d.%d\n", b1, b2, b3, b4);
//	return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
	return (((b1 * 256U) + b2) * 256U + b3) * 256U + b4;
}

BroadcastChannel :: BroadcastChannel (sword port, UchInetAddress* reg_addr)
: UchDatagram (new UchInetAddress (ANYADDR, port), 0)
{
	lword broadcast = BroadCastAddress ();
	bcast = new UchInetAddress (broadcast, port);

	const char* l = getlogin ();
	if (! l)
		l = cuserid (0);
	if (! l)
		::Error (ErrFatal, "BroadcastChannel", "getlogin failed");

	/* prepare answerBuf for future requests */
	char key [32];
	sprintf (key, "BroadcastChannel:%s:", l);
	lword reg_host = reg_addr->Host ();
	sword reg_port = reg_addr->Port ();
	int pid = getpid ();
	UchPortServerReq reg (PortServRegister, reg_host, reg_port, pid, key);
	registerBuf.WriteMsg (reg);
	UchPortServerReq rem (PortServRemove, reg_host, reg_port, pid, key);
	removeBuf.WriteMsg (rem);
	UchPortServerReq ans (PortServAnswer, reg_host, reg_port, pid, key);
	answerBuf.WriteMsg (ans);
}

bool
BroadcastChannel :: SetUp ()
{
	int on = 1;
	if (! UchDatagram::Setup ())
		return false;
	if (setsockopt (FilDes (), SOL_SOCKET, SO_BROADCAST, (char*) &on, sizeof (on)) < 0)
		return false;
	return true;
}

void
BroadcastChannel :: Answer ()
{
	int res = Send (answerBuf, *bcast, true /*peek*/);
	printf ("AnswerMe: %d\n", res);
}

void
BroadcastChannel :: HandleRead ()
{
	// handle requests
	//
	UchMsgBuffer buf (256);
	UchPortServerReq req;

	buf.Flush ();
	int n = Receive (buf);
	if (n <= 0) {
		SysError (ErrFatal, "BroadcastChannel", EINTR);
		return;
	}
	if (! buf.ReadMsg (req)) {
		::Error (ErrWarn, "BroadcastChannel", "could not read message");
		return;
	}
	
	// respond
	switch (req.Type) {
	case PortServRegister:
		RegisterService (req.Key, req.Host, req.Port, int (req.Ident));
		Modified ();
		printf ("received registration %s 0x%x %d %d\n", (const char*) req.Key, req.Host, req.Port, req.Ident);
		// when somebody else registers, we send our address
		Answer ();
		break;
		
	case PortServInquire:
		printf ("received inquiry\n");
		Answer ();
		break;
		
	case PortServAnswer:
		RegisterService (req.Key, req.Host, req.Port, int (req.Ident));
		Modified ();
		printf ("received answer %s 0x%x %d %d\n", (const char*) req.Key, req.Host, req.Port, req.Ident);
		break;
		
	case PortServRemove:
		RemoveService (req.Key, req.Host, req.Port, int (req.Ident));
		Modified ();
		break;
			
	default:
		::Error (ErrWarn, "main", "unknown request");
	}
}

// main
//

sword
GetPortNumber (const char* servname, int dflt)
{
	/* first try services database */
	struct servent* service = getservbyname (servname, 0);
	if (service)
		return ntohs (service->s_port);

	/* then, environment variable */
	char* portno = getenv (servname);
	if (portno) {
		int servport = atoi (portno);
		return servport;
	}

	/* then default value (may be zero)*/
	return dflt;
}

void
Usage ()
{
	::Error (ErrUsage, "portserv", "[-t] [-s service] [-quit] [-save file [time]] [-load file]");
}

main (int argc, const char** argv)
{
	const char* servname = "portserv";
	int	running = 1;
	bool	qflag = false;
	
	ProgramName (argv [0]);
	LoadFile = getenv ("AGENTMGRCF");
	while (--argc) {
		++argv;
		if (strcmp (*argv, "-quit") == 0) {
			qflag = true;
		} else
		if (strcmp (*argv, "-t") == 0) {
			Trace = true;
		} else
		if (strcmp (*argv, "-s") == 0) {
			if (--argc < 1)
				Usage ();
			servname = *++argv;
		} else
		if (strcmp (*argv, "-save") == 0) {
			if (--argc < 1)
				Usage ();
			SaveFile = *++argv;
			if (strcmp (SaveFile, "-") == 0)
				SaveFileIsFd = 1;
			else if (strcmp (SaveFile, "--") == 0)
				SaveFileIsFd = 2;
			
			if (argc > 1 && argv [1][0] != '-') {
				SaveTime = atoi (*++argv);
				--argc;
			} else
				SaveTime = -1;
		} else if (strcmp (*argv, "-load") == 0 || strcmp (*argv, "-l") == 0) {
			if (--argc < 1)
				Usage ();
			LoadFile = *++argv;
		} else
			Usage ();
	}

	// special case: portService -quit
	// send a PortServQuit message to
	// the portserver that is running
	if (qflag) {
		if (SaveFile || LoadFile)
			::Error (ErrFatal, "-quit", "incompatible with -save or -load");
		UchPortServer ps (servname, 0);
		ps.Quit ();
		exit (0);
	}

	if (! LoadFile) {
		const char* agentlib = getenv ("AGENTLIB");
		if (!agentlib)
			agentlib = DEFAGENTLIB;
		strcpy (DefLoadFile, agentlib);
		strcat (DefLoadFile, "/agentman.cf");
		if (access (DefLoadFile, R_OK) == 0)
			LoadFile = DefLoadFile;
	}

	// load config file
	//
	if (! BinDir) {
		BinDir = getenv ("AGENTBIN");
		if (! BinDir)
			BinDir = DEFAGENTBIN;
	}
	if (LoadFile)
		LoadConfigFile ();


	
	UchMultiplexer mpx;
	Mpx = &mpx;


	// get port
	//
	sword reg_port = GetPortNumber (servname, 3003);
	if (! reg_port)
		::Error (ErrFatal, servname, "service not available");

	// open dgram socket
	//
	if (Trace) printf ("opening\n");
	RegistrationChannel reg_chan (reg_port);
	if (! reg_chan.Setup ())
		SysError (ErrFatal, "main, creating portserv");
	reg_chan.SetMode (IORead);
	mpx.Add (&reg_chan);

	// open dgram broadcast socket for communicating between port servers
	//
	sword bc_port = GetPortNumber ("superman", 0);
	BroadcastChannel* bc_chan = 0;
	if (bc_port) {
		bc_chan = new BroadcastChannel (bc_port, (UchInetAddress*) reg_chan.BoundTo ());
		if (! bc_chan->Setup ()) {
			SysError (ErrWarn, "main, creating broadcast channel");
			delete bc_chan;
			bc_chan = 0;
		} else {
			::Error (ErrLog, "main", "created broadcast channel");
			bc_chan->SetMode (IORead);
			bc_chan->RegisterMe ();
			mpx.Add (bc_chan);
		}
	}

	// auto-dump stuff
	//
	UchSignalHandler h (mpx, SigHup, SigDump);
	if (SaveTime > 0)
		new UchSignalHandler (mpx, SigAlrm, SigDumpFile);
	
	// run the multiplexer
	//
	mpx.LoopScan ();

	// when quitting, notify other agent mgrs
	//
	if (bc_chan)
		bc_chan->RemoveMe ();

	return 0;
}