xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 63966)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*63966Seric static char sccsid[] = "@(#)conf.c	8.7 (Berkeley) 07/20/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1758153Seric # include <signal.h>
1836928Sbostic # include <pwd.h>
19404Seric 
20294Seric /*
213309Seric **  CONF.C -- Sendmail Configuration Tables.
22294Seric **
23294Seric **	Defines the configuration of this installation.
24294Seric **
251388Seric **	Configuration Variables:
262897Seric **		HdrInfo -- a table describing well-known header fields.
272897Seric **			Each entry has the field name and some flags,
284147Seric **			which are described in sendmail.h.
294093Seric **
304093Seric **	Notes:
314093Seric **		I have tried to put almost all the reasonable
324093Seric **		configuration information into the configuration
334093Seric **		file read at runtime.  My intent is that anything
344093Seric **		here is a function of the version of UNIX you
354093Seric **		are running, or is really static -- for example
364093Seric **		the headers are a superset of widely used
374093Seric **		protocols.  If you find yourself playing with
384093Seric **		this file too much, you may be making a mistake!
39294Seric */
40294Seric 
41294Seric 
42294Seric 
43294Seric 
444437Seric /*
452897Seric **  Header info table
463057Seric **	Final (null) entry contains the flags used for any other field.
474147Seric **
484147Seric **	Not all of these are actually handled specially by sendmail
494147Seric **	at this time.  They are included as placeholders, to let
504147Seric **	you know that "someday" I intend to have sendmail do
514147Seric **	something with them.
522897Seric */
532897Seric 
542897Seric struct hdrinfo	HdrInfo[] =
552897Seric {
568060Seric 		/* originator fields, most to least significant  */
5711417Seric 	"resent-sender",	H_FROM|H_RESENT,
5811417Seric 	"resent-from",		H_FROM|H_RESENT,
5925686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
609055Seric 	"sender",		H_FROM,
619055Seric 	"from",			H_FROM,
6225686Seric 	"reply-to",		H_FROM,
639055Seric 	"full-name",		H_ACHECK,
6457359Seric 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
6557359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
689055Seric 	"to",			H_RCPT,
6911417Seric 	"resent-to",		H_RCPT|H_RESENT,
709055Seric 	"cc",			H_RCPT,
7111417Seric 	"resent-cc",		H_RCPT|H_RESENT,
729055Seric 	"bcc",			H_RCPT|H_ACHECK,
7311417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7456215Seric 	"apparently-to",	H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
7711417Seric 	"message-id",		0,
7811417Seric 	"resent-message-id",	H_RESENT,
799055Seric 	"message",		H_EOH,
809055Seric 	"text",			H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
8311417Seric 	"date",			0,
8411417Seric 	"resent-date",		H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
879055Seric 	"received",		H_TRACE|H_FORCE,
8859318Seric 	"x400-received",	H_TRACE|H_FORCE,
899055Seric 	"via",			H_TRACE|H_FORCE,
909055Seric 	"mail-from",		H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
9358796Seric 	"comments",		H_FORCE,
9463753Seric 	"return-path",		H_FORCE|H_ACHECK,
9558796Seric 
969055Seric 	NULL,			0,
972897Seric };
984166Seric 
994166Seric 
1004166Seric 
1014282Seric /*
1024282Seric **  Location of system files/databases/etc.
1034282Seric */
1044282Seric 
10540980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
10640980Sbostic char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
10758082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1089039Seric 
1099064Seric 
1109064Seric 
1119039Seric /*
11258082Seric **  Privacy values
11358082Seric */
11458082Seric 
11558082Seric struct prival PrivacyValues[] =
11658082Seric {
11758082Seric 	"public",		PRIV_PUBLIC,
11858082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11958114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
12058082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12158082Seric 	"noexpn",		PRIV_NOEXPN,
12258082Seric 	"novrfy",		PRIV_NOVRFY,
12358249Seric 	"restrictmailq",	PRIV_RESTRMAILQ,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12558082Seric 	"goaway",		PRIV_GOAWAY,
12658789Seric 	NULL,			0,
12758082Seric };
12858082Seric 
12958082Seric 
13058082Seric 
13158082Seric /*
13224943Seric **  Miscellaneous stuff.
1339039Seric */
1349039Seric 
13524943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13624943Seric /*
13724943Seric **  SETDEFAULTS -- set default values
13824943Seric **
13924943Seric **	Because of the way freezing is done, these must be initialized
14024943Seric **	using direct code.
14124943Seric **
14224943Seric **	Parameters:
14358734Seric **		e -- the default envelope.
14424943Seric **
14524943Seric **	Returns:
14624943Seric **		none.
14724943Seric **
14824943Seric **	Side Effects:
14924943Seric **		Initializes a bunch of global variables to their
15024943Seric **		default values.
15124943Seric */
15224943Seric 
15358737Seric #define DAYS		* 24 * 60 * 60
15458737Seric 
15558734Seric setdefaults(e)
15658734Seric 	register ENVELOPE *e;
15724943Seric {
15857438Seric 	SpaceSub = ' ';				/* option B */
15957438Seric 	QueueLA = 8;				/* option x */
16057438Seric 	RefuseLA = 12;				/* option X */
16157438Seric 	WkRecipFact = 30000L;			/* option y */
16257438Seric 	WkClassFact = 1800L;			/* option z */
16357438Seric 	WkTimeFact = 90000L;			/* option Z */
16457438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
16563787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
16657438Seric 						/* option F */
16757438Seric 	DefUid = 1;				/* option u */
16857438Seric 	DefGid = 1;				/* option g */
16957438Seric 	CheckpointInterval = 10;		/* option C */
17057438Seric 	MaxHopCount = 25;			/* option h */
17158734Seric 	e->e_sendmode = SM_FORK;		/* option d */
17258734Seric 	e->e_errormode = EM_PRINT;		/* option e */
17359709Seric 	SevenBit = FALSE;			/* option 7 */
17457438Seric 	MaxMciCache = 1;			/* option k */
17557438Seric 	MciCacheTimeout = 300;			/* option K */
17657438Seric 	LogLevel = 9;				/* option L */
17758112Seric 	settimeouts(NULL);			/* option r */
17858737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
17958737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
18058853Seric 	PrivacyFlags = 0;			/* option p */
18140973Sbostic 	setdefuser();
18253654Seric 	setupmaps();
18357402Seric 	setupmailers();
18424943Seric }
185294Seric 
18640973Sbostic 
1874326Seric /*
18840973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
18940973Sbostic */
19040973Sbostic 
19140973Sbostic setdefuser()
19240973Sbostic {
19340973Sbostic 	struct passwd *defpwent;
19457386Seric 	static char defuserbuf[40];
19540973Sbostic 
19657386Seric 	DefUser = defuserbuf;
19740973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
19857386Seric 		strcpy(defuserbuf, defpwent->pw_name);
19940973Sbostic 	else
20057386Seric 		strcpy(defuserbuf, "nobody");
20140973Sbostic }
20253654Seric /*
20356836Seric **  HOST_MAP_INIT -- initialize host class structures
20456836Seric */
20556836Seric 
20656836Seric bool
20760219Seric host_map_init(map, args)
20856836Seric 	MAP *map;
20956836Seric 	char *args;
21056836Seric {
21156836Seric 	register char *p = args;
21256836Seric 
21356836Seric 	for (;;)
21456836Seric 	{
21558050Seric 		while (isascii(*p) && isspace(*p))
21656836Seric 			p++;
21756836Seric 		if (*p != '-')
21856836Seric 			break;
21956836Seric 		switch (*++p)
22056836Seric 		{
22156836Seric 		  case 'a':
22256836Seric 			map->map_app = ++p;
22356836Seric 			break;
22456836Seric 		}
22558050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
22656836Seric 			p++;
22756836Seric 		if (*p != '\0')
22856836Seric 			*p++ = '\0';
22956836Seric 	}
23056836Seric 	if (map->map_app != NULL)
23156836Seric 		map->map_app = newstr(map->map_app);
23256836Seric 	return TRUE;
23356836Seric }
23457402Seric /*
23557402Seric **  SETUPMAILERS -- initialize default mailers
23657402Seric */
23756836Seric 
23857402Seric setupmailers()
23957402Seric {
24057402Seric 	char buf[100];
24157402Seric 
24257403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
24357403Seric 	makemailer(buf);
24457403Seric 
24559883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
24657402Seric 	makemailer(buf);
24757402Seric 
24857402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
24957402Seric 	makemailer(buf);
25057402Seric }
25156836Seric /*
25260207Seric **  SETUPMAPS -- set up map classes
25360207Seric */
25460207Seric 
25560207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
25660207Seric 	{ \
25760207Seric 		extern bool parse __P((MAP *, char *)); \
25860207Seric 		extern bool open __P((MAP *, int)); \
25960207Seric 		extern void close __P((MAP *)); \
26060207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
26160207Seric 		extern void store __P((MAP *, char *, char *)); \
26260207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
26360207Seric 		s->s_mapclass.map_cname = name; \
26460207Seric 		s->s_mapclass.map_ext = ext; \
26560207Seric 		s->s_mapclass.map_cflags = flags; \
26660207Seric 		s->s_mapclass.map_parse = parse; \
26760207Seric 		s->s_mapclass.map_open = open; \
26860207Seric 		s->s_mapclass.map_close = close; \
26960207Seric 		s->s_mapclass.map_lookup = lookup; \
27060207Seric 		s->s_mapclass.map_store = store; \
27160207Seric 	}
27260207Seric 
27360207Seric setupmaps()
27460207Seric {
27560207Seric 	register STAB *s;
27660207Seric 
27760207Seric #ifdef NEWDB
27860207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
27960207Seric 		map_parseargs, hash_map_open, db_map_close,
28060207Seric 		db_map_lookup, db_map_store);
28160207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
28260207Seric 		map_parseargs, bt_map_open, db_map_close,
28360207Seric 		db_map_lookup, db_map_store);
28460207Seric #endif
28560207Seric 
28660207Seric #ifdef NDBM
28760207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
28860207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
28960207Seric 		ndbm_map_lookup, ndbm_map_store);
29060207Seric #endif
29160207Seric 
29260207Seric #ifdef NIS
29360207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
29460207Seric 		map_parseargs, nis_map_open, nis_map_close,
29560207Seric 		nis_map_lookup, nis_map_store);
29660207Seric #endif
29760207Seric 
29860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
29960207Seric 		map_parseargs, stab_map_open, stab_map_close,
30060207Seric 		stab_map_lookup, stab_map_store);
30160207Seric 
30260207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, impl_map_open, impl_map_close,
30460207Seric 		impl_map_lookup, impl_map_store);
30560207Seric 
30660207Seric 	/* host DNS lookup */
30760207Seric 	MAPDEF("host", NULL, 0,
30860207Seric 		host_map_init, null_map_open, null_map_close,
30960207Seric 		host_map_lookup, null_map_store);
31060207Seric 
31160207Seric 	/* dequote map */
31260207Seric 	MAPDEF("dequote", NULL, 0,
31360207Seric 		dequote_init, null_map_open, null_map_close,
31460207Seric 		dequote_map, null_map_store);
31560207Seric 
31660207Seric #if 0
31760207Seric # ifdef USERDB
31860207Seric 	/* user database */
31960207Seric 	MAPDEF("udb", ".db", 0,
32060207Seric 		udb_map_parse, null_map_open, null_map_close,
32160207Seric 		udb_map_lookup, null_map_store);
32260207Seric # endif
32360207Seric #endif
32460207Seric }
32560207Seric 
32660207Seric #undef MAPDEF
32760207Seric /*
3289369Seric **  USERNAME -- return the user id of the logged in user.
3299369Seric **
3309369Seric **	Parameters:
3319369Seric **		none.
3329369Seric **
3339369Seric **	Returns:
3349369Seric **		The login name of the logged in user.
3359369Seric **
3369369Seric **	Side Effects:
3379369Seric **		none.
3389369Seric **
3399369Seric **	Notes:
3409369Seric **		The return value is statically allocated.
3419369Seric */
3429369Seric 
3439369Seric char *
3449369Seric username()
3459369Seric {
34617469Seric 	static char *myname = NULL;
3479369Seric 	extern char *getlogin();
34819904Smiriam 	register struct passwd *pw;
3499369Seric 
35017469Seric 	/* cache the result */
35117469Seric 	if (myname == NULL)
35217469Seric 	{
35317469Seric 		myname = getlogin();
35417469Seric 		if (myname == NULL || myname[0] == '\0')
35517469Seric 		{
35663787Seric 			pw = getpwuid(RealUid);
35717469Seric 			if (pw != NULL)
35840993Sbostic 				myname = newstr(pw->pw_name);
35917469Seric 		}
36019904Smiriam 		else
36119904Smiriam 		{
36263787Seric 			uid_t uid = RealUid;
36319873Smiriam 
36440993Sbostic 			myname = newstr(myname);
36540993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
36658736Seric 			      (uid != 0 && uid != pw->pw_uid))
36719904Smiriam 			{
36858736Seric 				pw = getpwuid(uid);
36924945Seric 				if (pw != NULL)
37040993Sbostic 					myname = newstr(pw->pw_name);
37119873Smiriam 			}
37219873Smiriam 		}
37317469Seric 		if (myname == NULL || myname[0] == '\0')
37417469Seric 		{
37558151Seric 			syserr("554 Who are you?");
37617469Seric 			myname = "postmaster";
37717469Seric 		}
37817469Seric 	}
37917469Seric 
38017469Seric 	return (myname);
3819369Seric }
3829369Seric /*
3834190Seric **  TTYPATH -- Get the path of the user's tty
384294Seric **
385294Seric **	Returns the pathname of the user's tty.  Returns NULL if
386294Seric **	the user is not logged in or if s/he has write permission
387294Seric **	denied.
388294Seric **
389294Seric **	Parameters:
390294Seric **		none
391294Seric **
392294Seric **	Returns:
393294Seric **		pathname of the user's tty.
394294Seric **		NULL if not logged in or write permission denied.
395294Seric **
396294Seric **	Side Effects:
397294Seric **		none.
398294Seric **
399294Seric **	WARNING:
400294Seric **		Return value is in a local buffer.
401294Seric **
402294Seric **	Called By:
403294Seric **		savemail
404294Seric */
405294Seric 
406294Seric char *
407294Seric ttypath()
408294Seric {
409294Seric 	struct stat stbuf;
410294Seric 	register char *pathn;
411294Seric 	extern char *ttyname();
4124081Seric 	extern char *getlogin();
413294Seric 
414294Seric 	/* compute the pathname of the controlling tty */
4159369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4169369Seric 	    (pathn = ttyname(0)) == NULL)
417294Seric 	{
418294Seric 		errno = 0;
419294Seric 		return (NULL);
420294Seric 	}
421294Seric 
422294Seric 	/* see if we have write permission */
4232967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
424294Seric 	{
425294Seric 		errno = 0;
426294Seric 		return (NULL);
427294Seric 	}
428294Seric 
429294Seric 	/* see if the user is logged in */
430294Seric 	if (getlogin() == NULL)
431294Seric 		return (NULL);
432294Seric 
433294Seric 	/* looks good */
434294Seric 	return (pathn);
435294Seric }
4362967Seric /*
4372967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4382967Seric **
4392967Seric **	This routine can be supplied on a per-installation basis
4402967Seric **	to determine whether a person is allowed to send a message.
4412967Seric **	This allows restriction of certain types of internet
4422967Seric **	forwarding or registration of users.
4432967Seric **
4442967Seric **	If the hosts are found to be incompatible, an error
44557454Seric **	message should be given using "usrerr" and 0 should
4462967Seric **	be returned.
4472967Seric **
4484288Seric **	'NoReturn' can be set to suppress the return-to-sender
4494288Seric **	function; this should be done on huge messages.
4504288Seric **
4512967Seric **	Parameters:
4522967Seric **		to -- the person being sent to.
4532967Seric **
4542967Seric **	Returns:
45557459Seric **		an exit status
4562967Seric **
4572967Seric **	Side Effects:
4582967Seric **		none (unless you include the usrerr stuff)
4592967Seric */
4602967Seric 
46155012Seric checkcompat(to, e)
4622967Seric 	register ADDRESS *to;
46355012Seric 	register ENVELOPE *e;
4642967Seric {
46512133Seric # ifdef lint
46612133Seric 	if (to == NULL)
46712133Seric 		to++;
46863834Seric # endif /* lint */
46910698Seric # ifdef EXAMPLE_CODE
47010698Seric 	/* this code is intended as an example only */
4714437Seric 	register STAB *s;
4724437Seric 
4734437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
47455012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4759369Seric 	    to->q_mailer == s->s_mailer)
4764437Seric 	{
47758151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
47810698Seric 		/* NoReturn = TRUE; to supress return copy */
47957459Seric 		return (EX_UNAVAILABLE);
4804437Seric 	}
48156795Seric # endif /* EXAMPLE_CODE */
48257459Seric 	return (EX_OK);
4832967Seric }
4849369Seric /*
4859369Seric **  HOLDSIGS -- arrange to hold all signals
4869369Seric **
4879369Seric **	Parameters:
4889369Seric **		none.
4899369Seric **
4909369Seric **	Returns:
4919369Seric **		none.
4929369Seric **
4939369Seric **	Side Effects:
4949369Seric **		Arranges that signals are held.
4959369Seric */
4969369Seric 
4979369Seric holdsigs()
4989369Seric {
4999369Seric }
5009369Seric /*
5019369Seric **  RLSESIGS -- arrange to release all signals
5029369Seric **
5039369Seric **	This undoes the effect of holdsigs.
5049369Seric **
5059369Seric **	Parameters:
5069369Seric **		none.
5079369Seric **
5089369Seric **	Returns:
5099369Seric **		none.
5109369Seric **
5119369Seric **	Side Effects:
5129369Seric **		Arranges that signals are released.
5139369Seric */
5149369Seric 
5159369Seric rlsesigs()
5169369Seric {
5179369Seric }
51814872Seric /*
51914872Seric **  GETLA -- get the current load average
52014872Seric **
52114881Seric **	This code stolen from la.c.
52214881Seric **
52314872Seric **	Parameters:
52414872Seric **		none.
52514872Seric **
52614872Seric **	Returns:
52714872Seric **		The current load average as an integer.
52814872Seric **
52914872Seric **	Side Effects:
53014872Seric **		none.
53114872Seric */
53214872Seric 
53351920Seric /* try to guess what style of load average we have */
53451920Seric #define LA_ZERO		1	/* always return load average as zero */
53551920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
53651920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
53751920Seric #define LA_SUBR		4	/* call getloadavg */
53814872Seric 
53963962Seric /* do guesses based on general OS type */
54051920Seric #ifndef LA_TYPE
54163962Seric # define LA_TYPE	LA_ZERO
54251920Seric #endif
54351920Seric 
54451920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
54551920Seric 
54614872Seric #include <nlist.h>
54714872Seric 
54851920Seric #ifndef LA_AVENRUN
54963962Seric # ifdef SYSTEM5
55063962Seric #  define LA_AVENRUN	"avenrun"
55163962Seric # else
55263962Seric #  define LA_AVENRUN	"_avenrun"
55363962Seric # endif
55451920Seric #endif
55551920Seric 
55651920Seric /* _PATH_UNIX should be defined in <paths.h> */
55751920Seric #ifndef _PATH_UNIX
55863962Seric # if defined(SYSTEM5)
55963962Seric #  define _PATH_UNIX	"/unix"
56063962Seric # else
56163962Seric #  define _PATH_UNIX	"/vmunix"
56263962Seric # endif
56351920Seric #endif
56451920Seric 
56514872Seric struct	nlist Nl[] =
56614872Seric {
56751920Seric 	{ LA_AVENRUN },
56814872Seric #define	X_AVENRUN	0
56914872Seric 	{ 0 },
57014872Seric };
57114872Seric 
57260583Seric #ifndef FSHIFT
57360583Seric # if defined(unixpc)
57460583Seric #  define FSHIFT	5
57560583Seric # endif
57657736Seric 
57760583Seric # if defined(__alpha)
57860583Seric #  define FSHIFT	10
57960583Seric # endif
58057977Seric 
58160583Seric # if (LA_TYPE == LA_INT)
58251920Seric #  define FSHIFT	8
58360583Seric # endif
58457736Seric #endif
58560583Seric 
58657736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
58751920Seric #  define FSCALE	(1 << FSHIFT)
58851920Seric #endif
58940930Srick 
59014872Seric getla()
59114872Seric {
59214872Seric 	static int kmem = -1;
59351920Seric #if LA_TYPE == LA_INT
59424943Seric 	long avenrun[3];
59551920Seric #else
59651920Seric 	double avenrun[3];
59751920Seric #endif
59825615Seric 	extern off_t lseek();
59957736Seric 	extern int errno;
60014872Seric 
60114872Seric 	if (kmem < 0)
60214872Seric 	{
60324945Seric 		kmem = open("/dev/kmem", 0, 0);
60414872Seric 		if (kmem < 0)
60557736Seric 		{
60657736Seric 			if (tTd(3, 1))
60757736Seric 				printf("getla: open(/dev/kmem): %s\n",
60857736Seric 					errstring(errno));
60914872Seric 			return (-1);
61057736Seric 		}
61151920Seric 		(void) fcntl(kmem, F_SETFD, 1);
61257736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
61357736Seric 		{
61457736Seric 			if (tTd(3, 1))
61557736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
61657736Seric 					errstring(errno));
61714872Seric 			return (-1);
61857736Seric 		}
61959253Seric 		if (Nl[X_AVENRUN].n_value == 0)
62059253Seric 		{
62159253Seric 			if (tTd(3, 1))
62259253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
62359253Seric 					_PATH_UNIX, LA_AVENRUN);
62459253Seric 			return (-1);
62559253Seric 		}
62614872Seric 	}
62757736Seric 	if (tTd(3, 20))
62857736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
62924945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
63023118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
63119967Seric 	{
63219967Seric 		/* thank you Ian */
63357736Seric 		if (tTd(3, 1))
63457736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
63519967Seric 		return (-1);
63619967Seric 	}
63751920Seric #if LA_TYPE == LA_INT
63857736Seric 	if (tTd(3, 5))
63957736Seric 	{
64057736Seric 		printf("getla: avenrun = %d", avenrun[0]);
64157736Seric 		if (tTd(3, 15))
64257736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
64357736Seric 		printf("\n");
64457736Seric 	}
64557736Seric 	if (tTd(3, 1))
64657736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
64724943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
64851920Seric #else
64957736Seric 	if (tTd(3, 5))
65057736Seric 	{
65157736Seric 		printf("getla: avenrun = %g", avenrun[0]);
65257736Seric 		if (tTd(3, 15))
65357736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
65457736Seric 		printf("\n");
65557736Seric 	}
65657736Seric 	if (tTd(3, 1))
65757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
65851920Seric 	return ((int) (avenrun[0] + 0.5));
65951920Seric #endif
66014872Seric }
66114872Seric 
66251773Seric #else
66351920Seric #if LA_TYPE == LA_SUBR
66451773Seric 
66551773Seric getla()
66651773Seric {
66751920Seric 	double avenrun[3];
66851920Seric 
66951920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
67057736Seric 	{
67157736Seric 		if (tTd(3, 1))
67257736Seric 			perror("getla: getloadavg failed:");
67351920Seric 		return (-1);
67457736Seric 	}
67557736Seric 	if (tTd(3, 1))
67657736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
67751920Seric 	return ((int) (avenrun[0] + 0.5));
67851773Seric }
67951773Seric 
68051773Seric #else
68151773Seric 
68251773Seric getla()
68351773Seric {
68457736Seric 	if (tTd(3, 1))
68557736Seric 		printf("getla: ZERO\n");
68651920Seric 	return (0);
68751773Seric }
68851773Seric 
68951773Seric #endif
69051773Seric #endif
69124943Seric /*
69224943Seric **  SHOULDQUEUE -- should this message be queued or sent?
69324943Seric **
69424943Seric **	Compares the message cost to the load average to decide.
69524943Seric **
69624943Seric **	Parameters:
69724943Seric **		pri -- the priority of the message in question.
69857438Seric **		ctime -- the message creation time.
69924943Seric **
70024943Seric **	Returns:
70124943Seric **		TRUE -- if this message should be queued up for the
70224943Seric **			time being.
70324943Seric **		FALSE -- if the load is low enough to send this message.
70424943Seric **
70524943Seric **	Side Effects:
70624943Seric **		none.
70724943Seric */
70824943Seric 
70924943Seric bool
71057438Seric shouldqueue(pri, ctime)
71124943Seric 	long pri;
71257438Seric 	time_t ctime;
71324943Seric {
71451920Seric 	if (CurrentLA < QueueLA)
71524943Seric 		return (FALSE);
71658132Seric 	if (CurrentLA >= RefuseLA)
71758132Seric 		return (TRUE);
71851920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
71924943Seric }
72024943Seric /*
72153037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
72253037Seric **
72353037Seric **	Parameters:
72453037Seric **		none.
72553037Seric **
72653037Seric **	Returns:
72753037Seric **		TRUE if incoming SMTP connections should be refused
72853037Seric **			(for now).
72953037Seric **		FALSE if we should accept new work.
73053037Seric **
73153037Seric **	Side Effects:
73253037Seric **		none.
73353037Seric */
73453037Seric 
73553037Seric bool
73653037Seric refuseconnections()
73753037Seric {
73859156Seric #ifdef XLA
73959156Seric 	if (!xla_smtp_ok())
74059156Seric 		return TRUE;
74159156Seric #endif
74259156Seric 
74353037Seric 	/* this is probably too simplistic */
74458132Seric 	return (CurrentLA >= RefuseLA);
74553037Seric }
74653037Seric /*
74724943Seric **  SETPROCTITLE -- set process title for ps
74824943Seric **
74924943Seric **	Parameters:
75058674Seric **		fmt -- a printf style format string.
75158674Seric **		a, b, c -- possible parameters to fmt.
75224943Seric **
75324943Seric **	Returns:
75424943Seric **		none.
75524943Seric **
75624943Seric **	Side Effects:
75724943Seric **		Clobbers argv of our main procedure so ps(1) will
75824943Seric **		display the title.
75924943Seric */
76024943Seric 
76158689Seric #ifdef SETPROCTITLE
76258689Seric # ifdef __hpux
76358689Seric #  include <sys/pstat.h>
76458689Seric # endif
76559732Seric # ifdef BSD4_4
76659732Seric #  include <machine/vmparam.h>
76759732Seric #  include <sys/exec.h>
76859992Seric #  ifdef PS_STRINGS
76959992Seric #   define SETPROC_STATIC static
77059992Seric #  endif
77159732Seric # endif
77259732Seric # ifndef SETPROC_STATIC
77359732Seric #  define SETPROC_STATIC
77459732Seric # endif
77558689Seric #endif
77658689Seric 
77724943Seric /*VARARGS1*/
77857642Seric #ifdef __STDC__
77957642Seric setproctitle(char *fmt, ...)
78057642Seric #else
78157642Seric setproctitle(fmt, va_alist)
78224943Seric 	char *fmt;
78357642Seric 	va_dcl
78457642Seric #endif
78524943Seric {
78624943Seric # ifdef SETPROCTITLE
78724943Seric 	register char *p;
78825049Seric 	register int i;
78959732Seric 	SETPROC_STATIC char buf[MAXLINE];
79056852Seric 	VA_LOCAL_DECL
79158689Seric #  ifdef __hpux
79258689Seric 	union pstun pst;
79358689Seric #  endif
79424943Seric 	extern char **Argv;
79524943Seric 	extern char *LastArgv;
79624943Seric 
79758674Seric 	p = buf;
79824943Seric 
79958674Seric 	/* print sendmail: heading for grep */
80058674Seric 	(void) strcpy(p, "sendmail: ");
80158674Seric 	p += strlen(p);
80224943Seric 
80358674Seric 	/* print the argument string */
80458674Seric 	VA_START(fmt);
80558674Seric 	(void) vsprintf(p, fmt, ap);
80656852Seric 	VA_END;
80754996Seric 
80858674Seric 	i = strlen(buf);
80958689Seric 
81058689Seric #  ifdef __hpux
81158689Seric 	pst.pst_command = buf;
81258689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
81358689Seric #  else
81459992Seric #   ifdef PS_STRINGS
81559732Seric 	PS_STRINGS->ps_nargvstr = 1;
81659732Seric 	PS_STRINGS->ps_argvstr = buf;
81759732Seric #   else
81854996Seric 	if (i > LastArgv - Argv[0] - 2)
81925049Seric 	{
82054996Seric 		i = LastArgv - Argv[0] - 2;
82158674Seric 		buf[i] = '\0';
82225049Seric 	}
82358674Seric 	(void) strcpy(Argv[0], buf);
82454997Seric 	p = &Argv[0][i];
82524943Seric 	while (p < LastArgv)
82624943Seric 		*p++ = ' ';
82759732Seric #   endif
82858689Seric #  endif
82956795Seric # endif /* SETPROCTITLE */
83024943Seric }
83125698Seric /*
83225698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
83325698Seric **
83425698Seric **	Parameters:
83525698Seric **		none.
83625698Seric **
83725698Seric **	Returns:
83825698Seric **		none.
83925698Seric **
84025698Seric **	Side Effects:
84125698Seric **		Picks up extant zombies.
84225698Seric */
84325698Seric 
84425698Seric # include <sys/wait.h>
84525698Seric 
84646928Sbostic void
84725698Seric reapchild()
84825698Seric {
849*63966Seric # ifdef HASWAITPID
85060219Seric 	auto int status;
85160560Seric 	int count;
85260560Seric 	int pid;
85360219Seric 
85460560Seric 	count = 0;
85560560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
85660560Seric 	{
85760560Seric 		if (count++ > 1000)
85860560Seric 		{
85960560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
86060560Seric 				pid, status);
86160560Seric 			break;
86260560Seric 		}
86360560Seric 	}
86460219Seric # else
86525698Seric # ifdef WNOHANG
86625698Seric 	union wait status;
86725698Seric 
86863753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
86925698Seric 		continue;
87056795Seric # else /* WNOHANG */
87125698Seric 	auto int status;
87225698Seric 
87360219Seric 	while (wait(&status) > 0)
87425698Seric 		continue;
87556795Seric # endif /* WNOHANG */
87660219Seric # endif
87758061Seric # ifdef SYSTEM5
87858061Seric 	(void) signal(SIGCHLD, reapchild);
87958061Seric # endif
88025698Seric }
88155418Seric /*
88263962Seric **  SETENV -- set environment variable
88363962Seric **
88463962Seric **	Putenv is more modern, but this is a simpler interface
88563962Seric **
88663962Seric **	Parameters:
88763962Seric **		name -- the name of the envariable.
88863962Seric **		value -- the value of that envariable.
88963962Seric **		overwrite -- if set, overwrite existing value
89063962Seric **			(this is assumed to be set).
89163962Seric **
89263962Seric **	Returns:
89363962Seric **		none.
89463962Seric **
89563962Seric **	Side Effects:
89663962Seric **		The environment is updated.
89763962Seric */
89863962Seric 
89963962Seric #ifndef HASSETENV
90063962Seric 
90163962Seric setenv(name, value, overwrite)
90263962Seric 	char *name;
90363962Seric 	char *value;
90463962Seric 	int overwrite;
90563962Seric {
90663962Seric 	register char *p;
90763962Seric 
90863962Seric 	p = xalloc(strlen(name) + strlen(value) + 2);
90963962Seric 	strcpy(p, name);
91063962Seric 	strcat(p, "=");
91163962Seric 	strcat(p, value);
91263962Seric 	putenv(p);
91363962Seric }
91463962Seric 
91563962Seric #endif
91663962Seric /*
91755418Seric **  UNSETENV -- remove a variable from the environment
91855418Seric **
91955418Seric **	Not needed on newer systems.
92055418Seric **
92155418Seric **	Parameters:
92255418Seric **		name -- the string name of the environment variable to be
92355418Seric **			deleted from the current environment.
92455418Seric **
92555418Seric **	Returns:
92655418Seric **		none.
92755418Seric **
92855418Seric **	Globals:
92955418Seric **		environ -- a pointer to the current environment.
93055418Seric **
93155418Seric **	Side Effects:
93255418Seric **		Modifies environ.
93355418Seric */
93455418Seric 
93563962Seric #ifndef HASUNSETENV
93655418Seric 
93755418Seric void
93855418Seric unsetenv(name)
93955418Seric 	char *name;
94055418Seric {
94155418Seric 	extern char **environ;
94255418Seric 	register char **pp;
94355418Seric 	int len = strlen(name);
94455418Seric 
94555418Seric 	for (pp = environ; *pp != NULL; pp++)
94655418Seric 	{
94755418Seric 		if (strncmp(name, *pp, len) == 0 &&
94855418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
94955418Seric 			break;
95055418Seric 	}
95155418Seric 
95255418Seric 	for (; *pp != NULL; pp++)
95355418Seric 		*pp = pp[1];
95455418Seric }
95555418Seric 
95663962Seric #endif
95756215Seric /*
95856215Seric **  GETDTABLESIZE -- return number of file descriptors
95956215Seric **
96056215Seric **	Only on non-BSD systems
96156215Seric **
96256215Seric **	Parameters:
96356215Seric **		none
96456215Seric **
96556215Seric **	Returns:
96656215Seric **		size of file descriptor table
96756215Seric **
96856215Seric **	Side Effects:
96956215Seric **		none
97056215Seric */
97156215Seric 
97263787Seric #ifdef SOLARIS
97363787Seric # include <sys/resource.h>
97463787Seric #endif
97556215Seric 
97656215Seric int
97763787Seric getdtsize()
97856215Seric {
97963787Seric #ifdef RLIMIT_NOFILE
98063787Seric 	struct rlimit rl;
98163787Seric 
98263787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
98363787Seric 		return rl.rlim_cur;
98463787Seric #endif
98563787Seric 
98658689Seric # ifdef _SC_OPEN_MAX
98758689Seric 	return sysconf(_SC_OPEN_MAX);
98858689Seric # else
98963787Seric #  ifdef HASGETDTABLESIZE
99063787Seric 	return getdtablesize();
99163787Seric #  else
99256215Seric 	return NOFILE;
99363787Seric #  endif
99458689Seric # endif
99556215Seric }
99657631Seric /*
99757631Seric **  UNAME -- get the UUCP name of this system.
99857631Seric */
99957631Seric 
100057943Seric #ifndef HASUNAME
100157631Seric 
100257631Seric int
100357631Seric uname(name)
100457631Seric 	struct utsname *name;
100557631Seric {
100657631Seric 	FILE *file;
100757631Seric 	char *n;
100857631Seric 
100957631Seric 	name->nodename[0] = '\0';
101057631Seric 
101157661Seric 	/* try /etc/whoami -- one line with the node name */
101257631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
101357631Seric 	{
101457661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
101557631Seric 		(void) fclose(file);
101657661Seric 		n = strchr(name->nodename, '\n');
101757631Seric 		if (n != NULL)
101857631Seric 			*n = '\0';
101957631Seric 		if (name->nodename[0] != '\0')
102057631Seric 			return (0);
102157631Seric 	}
102257631Seric 
102357661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
102457631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
102557631Seric 	{
102657631Seric 		char buf[MAXLINE];
102757631Seric 
102857631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
102957631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
103057631Seric 					NODE_LENGTH, name->nodename) > 0)
103157631Seric 				break;
103257631Seric 		(void) fclose(file);
103357631Seric 		if (name->nodename[0] != '\0')
103457631Seric 			return (0);
103557631Seric 	}
103657631Seric 
103757631Seric #ifdef TRUST_POPEN
103857631Seric 	/*
103957631Seric 	**  Popen is known to have security holes.
104057631Seric 	*/
104157631Seric 
104257661Seric 	/* try uuname -l to return local name */
104357631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
104457631Seric 	{
104557661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
104657631Seric 		(void) pclose(file);
104757661Seric 		n = strchr(name, '\n');
104857631Seric 		if (n != NULL)
104957631Seric 			*n = '\0';
105057661Seric 		if (name->nodename[0] != '\0')
105157631Seric 			return (0);
105257631Seric 	}
105357631Seric #endif
105457631Seric 
105557631Seric 	return (-1);
105657631Seric }
105757943Seric #endif /* HASUNAME */
105858068Seric /*
105958068Seric **  INITGROUPS -- initialize groups
106058068Seric **
106158068Seric **	Stub implementation for System V style systems
106258068Seric */
106358068Seric 
106458068Seric #ifndef HASINITGROUPS
106559430Seric # if !defined(SYSTEM5) || defined(__hpux)
106658068Seric #  define HASINITGROUPS
106758068Seric # endif
106858068Seric #endif
106958068Seric 
107058068Seric #ifndef HASINITGROUPS
107158068Seric 
107258068Seric initgroups(name, basegid)
107358068Seric 	char *name;
107458068Seric 	int basegid;
107558068Seric {
107658068Seric 	return 0;
107758068Seric }
107858068Seric 
107958068Seric #endif
108058082Seric /*
108159289Seric **  SETSID -- set session id (for non-POSIX systems)
108259289Seric */
108359289Seric 
108459289Seric #ifndef HASSETSID
108559289Seric 
108663753Seric pid_t
108763753Seric setsid __P ((void))
108859289Seric {
108959289Seric # ifdef SYSTEM5
109063753Seric 	return setpgrp();
109163753Seric # else
109263753Seric 	return 0;
109359289Seric # endif
109459289Seric }
109559289Seric 
109659289Seric #endif
109759289Seric /*
109858082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
109958082Seric **
110058082Seric **	Only implemented if you have statfs.
110158082Seric **
110258082Seric **	Parameters:
110358333Seric **		msize -- the size to check against.  If zero, we don't yet
110458333Seric **			know how big the message will be, so just check for
110558333Seric **			a "reasonable" amount.
110658082Seric **
110758082Seric **	Returns:
110858082Seric **		TRUE if there is enough space.
110958082Seric **		FALSE otherwise.
111058082Seric */
111158082Seric 
111258082Seric #ifndef HASSTATFS
111358082Seric # if defined(BSD4_4) || defined(__osf__)
111458082Seric #  define HASSTATFS
111558082Seric # endif
111658082Seric #endif
111758082Seric 
111858082Seric #ifdef HASSTATFS
111958157Seric # undef HASUSTAT
112058157Seric #endif
112158157Seric 
112258157Seric #if defined(HASUSTAT)
112358157Seric # include <ustat.h>
112458157Seric #endif
112558157Seric 
112658157Seric #ifdef HASSTATFS
112758133Seric # if defined(sgi) || defined(apollo)
112858133Seric #  include <sys/statfs.h>
112958133Seric # else
113063962Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE)
113158133Seric #   include <sys/vfs.h>
113258133Seric #  else
113358157Seric #   include <sys/mount.h>
113458133Seric #  endif
113558133Seric # endif
113658082Seric #endif
113758082Seric 
113858082Seric bool
113958333Seric enoughspace(msize)
114058333Seric 	long msize;
114158082Seric {
114258160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
114358157Seric # if defined(HASUSTAT)
114458153Seric 	struct ustat fs;
114558153Seric 	struct stat statbuf;
114658366Seric #  define FSBLOCKSIZE	DEV_BSIZE
114758157Seric #  define f_bavail	f_tfree
114858157Seric # else
114958157Seric #  if defined(ultrix)
115058157Seric 	struct fs_data fs;
115158157Seric #   define f_bavail	fd_bfreen
115258366Seric #   define FSBLOCKSIZE	fs.fd_bsize
115358153Seric #  else
115458082Seric 	struct statfs fs;
115558366Seric #   define FSBLOCKSIZE	fs.f_bsize
115658153Seric #  endif
115758133Seric # endif
115858082Seric 	extern int errno;
115958082Seric 
116058333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
116158082Seric 	{
116258082Seric 		if (tTd(4, 80))
116358133Seric 			printf("enoughspace: no threshold\n");
116458133Seric 		return TRUE;
116558133Seric 	}
116658333Seric 
116758157Seric # if defined(HASUSTAT)
116858157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
116958157Seric # else
117058157Seric #  if defined(sgi) || defined(apollo)
117158133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
117258157Seric #  else
117358157Seric #   if defined(ultrix)
117458133Seric 	if (statfs(QueueDir, &fs) > 0)
117558153Seric #   else
117658133Seric 	if (statfs(QueueDir, &fs) == 0)
117758153Seric #   endif
117858133Seric #  endif
117958133Seric # endif
118058133Seric 	{
118158133Seric 		if (tTd(4, 80))
118258333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
118358333Seric 				fs.f_bavail, msize);
118458333Seric 
118558333Seric 		/* convert msize to block count */
118658366Seric 		msize = msize / FSBLOCKSIZE + 1;
118758333Seric 		if (MinBlocksFree >= 0)
118858333Seric 			msize += MinBlocksFree;
118958333Seric 
119058333Seric 		if (fs.f_bavail < msize)
119158090Seric 		{
119258090Seric #ifdef LOG
119358090Seric 			if (LogLevel > 0)
119458090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
119558333Seric 					QueueDir, fs.f_bavail, msize);
119658090Seric #endif
119758082Seric 			return FALSE;
119858090Seric 		}
119958082Seric 	}
120058082Seric 	else if (tTd(4, 80))
120158333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
120258333Seric 			MinBlocksFree, msize, errstring(errno));
120358082Seric #endif
120458082Seric 	return TRUE;
120558082Seric }
120658542Seric /*
120758542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
120858542Seric **
120958542Seric **	This looks at an errno value and tells if this is likely to
121058542Seric **	go away if retried later.
121158542Seric **
121258542Seric **	Parameters:
121358542Seric **		err -- the errno code to classify.
121458542Seric **
121558542Seric **	Returns:
121658542Seric **		TRUE if this is probably transient.
121758542Seric **		FALSE otherwise.
121858542Seric */
121958542Seric 
122058542Seric bool
122158542Seric transienterror(err)
122258542Seric 	int err;
122358542Seric {
122458542Seric 	switch (err)
122558542Seric 	{
122658542Seric 	  case EIO:			/* I/O error */
122758542Seric 	  case ENXIO:			/* Device not configured */
122858542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
122958542Seric 	  case ENOMEM:			/* Cannot allocate memory */
123058542Seric 	  case ENODEV:			/* Operation not supported by device */
123158542Seric 	  case ENFILE:			/* Too many open files in system */
123258542Seric 	  case EMFILE:			/* Too many open files */
123358542Seric 	  case ENOSPC:			/* No space left on device */
123458542Seric #ifdef ETIMEDOUT
123558542Seric 	  case ETIMEDOUT:		/* Connection timed out */
123658542Seric #endif
123758542Seric #ifdef ESTALE
123858542Seric 	  case ESTALE:			/* Stale NFS file handle */
123958542Seric #endif
124058542Seric #ifdef ENETDOWN
124158542Seric 	  case ENETDOWN:		/* Network is down */
124258542Seric #endif
124358542Seric #ifdef ENETUNREACH
124458542Seric 	  case ENETUNREACH:		/* Network is unreachable */
124558542Seric #endif
124658542Seric #ifdef ENETRESET
124758542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
124858542Seric #endif
124958542Seric #ifdef ECONNABORTED
125058542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
125158542Seric #endif
125258542Seric #ifdef ECONNRESET
125358542Seric 	  case ECONNRESET:		/* Connection reset by peer */
125458542Seric #endif
125558542Seric #ifdef ENOBUFS
125658542Seric 	  case ENOBUFS:			/* No buffer space available */
125758542Seric #endif
125858542Seric #ifdef ESHUTDOWN
125958542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
126058542Seric #endif
126158542Seric #ifdef ECONNREFUSED
126258542Seric 	  case ECONNREFUSED:		/* Connection refused */
126358542Seric #endif
126458542Seric #ifdef EHOSTDOWN
126558542Seric 	  case EHOSTDOWN:		/* Host is down */
126658542Seric #endif
126758542Seric #ifdef EHOSTUNREACH
126858542Seric 	  case EHOSTUNREACH:		/* No route to host */
126958542Seric #endif
127058542Seric #ifdef EDQUOT
127158542Seric 	  case EDQUOT:			/* Disc quota exceeded */
127258542Seric #endif
127358542Seric #ifdef EPROCLIM
127458542Seric 	  case EPROCLIM:		/* Too many processes */
127558542Seric #endif
127658542Seric #ifdef EUSERS
127758542Seric 	  case EUSERS:			/* Too many users */
127858542Seric #endif
127958542Seric #ifdef EDEADLK
128058542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
128158542Seric #endif
128258542Seric #ifdef EISCONN
128358542Seric 	  case EISCONN:			/* Socket already connected */
128458542Seric #endif
128558542Seric #ifdef EINPROGRESS
128658542Seric 	  case EINPROGRESS:		/* Operation now in progress */
128758542Seric #endif
128858542Seric #ifdef EALREADY
128958542Seric 	  case EALREADY:		/* Operation already in progress */
129058542Seric #endif
129158542Seric #ifdef EADDRINUSE
129258542Seric 	  case EADDRINUSE:		/* Address already in use */
129358542Seric #endif
129458542Seric #ifdef EADDRNOTAVAIL
129558542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
129658542Seric #endif
129763834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
129858542Seric 	  case ENOSR:			/* Out of streams resources */
129958542Seric #endif
130058542Seric 		return TRUE;
130158542Seric 	}
130258542Seric 
130358542Seric 	/* nope, must be permanent */
130458542Seric 	return FALSE;
130558542Seric }
130658689Seric /*
130758689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
130858689Seric **
130958689Seric **	Parameters:
131058689Seric **		fd -- the file descriptor of the file.
131158689Seric **		filename -- the file name (for error messages).
131258689Seric **		type -- type of the lock.  Bits can be:
131358689Seric **			LOCK_EX -- exclusive lock.
131458689Seric **			LOCK_NB -- non-blocking.
131558689Seric **
131658689Seric **	Returns:
131758689Seric **		TRUE if the lock was acquired.
131858689Seric **		FALSE otherwise.
131958689Seric */
132058689Seric 
132158689Seric bool
132258689Seric lockfile(fd, filename, type)
132358689Seric 	int fd;
132458689Seric 	char *filename;
132558689Seric 	int type;
132658689Seric {
132758689Seric # ifdef LOCKF
132858689Seric 	int action;
132958689Seric 	struct flock lfd;
133058689Seric 
133159447Seric 	if (bitset(LOCK_UN, type))
133259447Seric 		lfd.l_type = F_UNLCK;
133359447Seric 	else if (bitset(LOCK_EX, type))
133458689Seric 		lfd.l_type = F_WRLCK;
133558689Seric 	else
133658689Seric 		lfd.l_type = F_RDLCK;
133758689Seric 
133858689Seric 	if (bitset(LOCK_NB, type))
133958689Seric 		action = F_SETLK;
134058689Seric 	else
134158689Seric 		action = F_SETLKW;
134258689Seric 
134358689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
134458689Seric 
134558689Seric 	if (fcntl(fd, action, &lfd) >= 0)
134658689Seric 		return TRUE;
134758689Seric 
134858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
134959447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
135058689Seric # else
135158689Seric 	if (flock(fd, type) >= 0)
135258689Seric 		return TRUE;
135358689Seric 
135458689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
135559447Seric 		syserr("cannot flock(%s, %o)", filename, type);
135658689Seric # endif
135758689Seric 	return FALSE;
135858689Seric }
1359