xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 60219)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*60219Seric static char sccsid[] = "@(#)conf.c	6.60 (Berkeley) 05/22/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1314881Seric # include <sys/ioctl.h>
1458082Seric # include <sys/param.h>
1558153Seric # include <signal.h>
1636928Sbostic # include <pwd.h>
173309Seric # include "sendmail.h"
1840980Sbostic # include "pathnames.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,
9458796Seric 	"return-path",		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 */
16557142Seric 	FileMode = (getuid() != 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
207*60219Seric 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 /*
3284326Seric **  GETRUID -- get real user id (V7)
3294326Seric */
3304326Seric 
3314326Seric getruid()
3324326Seric {
3339274Seric 	if (OpMode == MD_DAEMON)
3344536Seric 		return (RealUid);
3354536Seric 	else
3364536Seric 		return (getuid());
3374326Seric }
3384326Seric 
3394326Seric 
3404326Seric /*
3414326Seric **  GETRGID -- get real group id (V7).
3424326Seric */
3434326Seric 
3444326Seric getrgid()
3454326Seric {
3469274Seric 	if (OpMode == MD_DAEMON)
3474536Seric 		return (RealGid);
3484536Seric 	else
3494536Seric 		return (getgid());
3504326Seric }
35153654Seric /*
3529369Seric **  USERNAME -- return the user id of the logged in user.
3539369Seric **
3549369Seric **	Parameters:
3559369Seric **		none.
3569369Seric **
3579369Seric **	Returns:
3589369Seric **		The login name of the logged in user.
3599369Seric **
3609369Seric **	Side Effects:
3619369Seric **		none.
3629369Seric **
3639369Seric **	Notes:
3649369Seric **		The return value is statically allocated.
3659369Seric */
3669369Seric 
3679369Seric char *
3689369Seric username()
3699369Seric {
37017469Seric 	static char *myname = NULL;
3719369Seric 	extern char *getlogin();
37219904Smiriam 	register struct passwd *pw;
3739369Seric 
37417469Seric 	/* cache the result */
37517469Seric 	if (myname == NULL)
37617469Seric 	{
37717469Seric 		myname = getlogin();
37817469Seric 		if (myname == NULL || myname[0] == '\0')
37917469Seric 		{
38017469Seric 			pw = getpwuid(getruid());
38117469Seric 			if (pw != NULL)
38240993Sbostic 				myname = newstr(pw->pw_name);
38317469Seric 		}
38419904Smiriam 		else
38519904Smiriam 		{
38658736Seric 			uid_t uid = getuid();
38719873Smiriam 
38840993Sbostic 			myname = newstr(myname);
38940993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
39058736Seric 			      (uid != 0 && uid != pw->pw_uid))
39119904Smiriam 			{
39258736Seric 				pw = getpwuid(uid);
39324945Seric 				if (pw != NULL)
39440993Sbostic 					myname = newstr(pw->pw_name);
39519873Smiriam 			}
39619873Smiriam 		}
39717469Seric 		if (myname == NULL || myname[0] == '\0')
39817469Seric 		{
39958151Seric 			syserr("554 Who are you?");
40017469Seric 			myname = "postmaster";
40117469Seric 		}
40217469Seric 	}
40317469Seric 
40417469Seric 	return (myname);
4059369Seric }
4069369Seric /*
4074190Seric **  TTYPATH -- Get the path of the user's tty
408294Seric **
409294Seric **	Returns the pathname of the user's tty.  Returns NULL if
410294Seric **	the user is not logged in or if s/he has write permission
411294Seric **	denied.
412294Seric **
413294Seric **	Parameters:
414294Seric **		none
415294Seric **
416294Seric **	Returns:
417294Seric **		pathname of the user's tty.
418294Seric **		NULL if not logged in or write permission denied.
419294Seric **
420294Seric **	Side Effects:
421294Seric **		none.
422294Seric **
423294Seric **	WARNING:
424294Seric **		Return value is in a local buffer.
425294Seric **
426294Seric **	Called By:
427294Seric **		savemail
428294Seric */
429294Seric 
430294Seric char *
431294Seric ttypath()
432294Seric {
433294Seric 	struct stat stbuf;
434294Seric 	register char *pathn;
435294Seric 	extern char *ttyname();
4364081Seric 	extern char *getlogin();
437294Seric 
438294Seric 	/* compute the pathname of the controlling tty */
4399369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4409369Seric 	    (pathn = ttyname(0)) == NULL)
441294Seric 	{
442294Seric 		errno = 0;
443294Seric 		return (NULL);
444294Seric 	}
445294Seric 
446294Seric 	/* see if we have write permission */
4472967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
448294Seric 	{
449294Seric 		errno = 0;
450294Seric 		return (NULL);
451294Seric 	}
452294Seric 
453294Seric 	/* see if the user is logged in */
454294Seric 	if (getlogin() == NULL)
455294Seric 		return (NULL);
456294Seric 
457294Seric 	/* looks good */
458294Seric 	return (pathn);
459294Seric }
4602967Seric /*
4612967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4622967Seric **
4632967Seric **	This routine can be supplied on a per-installation basis
4642967Seric **	to determine whether a person is allowed to send a message.
4652967Seric **	This allows restriction of certain types of internet
4662967Seric **	forwarding or registration of users.
4672967Seric **
4682967Seric **	If the hosts are found to be incompatible, an error
46957454Seric **	message should be given using "usrerr" and 0 should
4702967Seric **	be returned.
4712967Seric **
4724288Seric **	'NoReturn' can be set to suppress the return-to-sender
4734288Seric **	function; this should be done on huge messages.
4744288Seric **
4752967Seric **	Parameters:
4762967Seric **		to -- the person being sent to.
4772967Seric **
4782967Seric **	Returns:
47957459Seric **		an exit status
4802967Seric **
4812967Seric **	Side Effects:
4822967Seric **		none (unless you include the usrerr stuff)
4832967Seric */
4842967Seric 
48555012Seric checkcompat(to, e)
4862967Seric 	register ADDRESS *to;
48755012Seric 	register ENVELOPE *e;
4882967Seric {
48912133Seric # ifdef lint
49012133Seric 	if (to == NULL)
49112133Seric 		to++;
49212133Seric # endif lint
49310698Seric # ifdef EXAMPLE_CODE
49410698Seric 	/* this code is intended as an example only */
4954437Seric 	register STAB *s;
4964437Seric 
4974437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49855012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4999369Seric 	    to->q_mailer == s->s_mailer)
5004437Seric 	{
50158151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50210698Seric 		/* NoReturn = TRUE; to supress return copy */
50357459Seric 		return (EX_UNAVAILABLE);
5044437Seric 	}
50556795Seric # endif /* EXAMPLE_CODE */
50657459Seric 	return (EX_OK);
5072967Seric }
5089369Seric /*
5099369Seric **  HOLDSIGS -- arrange to hold all signals
5109369Seric **
5119369Seric **	Parameters:
5129369Seric **		none.
5139369Seric **
5149369Seric **	Returns:
5159369Seric **		none.
5169369Seric **
5179369Seric **	Side Effects:
5189369Seric **		Arranges that signals are held.
5199369Seric */
5209369Seric 
5219369Seric holdsigs()
5229369Seric {
5239369Seric }
5249369Seric /*
5259369Seric **  RLSESIGS -- arrange to release all signals
5269369Seric **
5279369Seric **	This undoes the effect of holdsigs.
5289369Seric **
5299369Seric **	Parameters:
5309369Seric **		none.
5319369Seric **
5329369Seric **	Returns:
5339369Seric **		none.
5349369Seric **
5359369Seric **	Side Effects:
5369369Seric **		Arranges that signals are released.
5379369Seric */
5389369Seric 
5399369Seric rlsesigs()
5409369Seric {
5419369Seric }
54214872Seric /*
54314872Seric **  GETLA -- get the current load average
54414872Seric **
54514881Seric **	This code stolen from la.c.
54614881Seric **
54714872Seric **	Parameters:
54814872Seric **		none.
54914872Seric **
55014872Seric **	Returns:
55114872Seric **		The current load average as an integer.
55214872Seric **
55314872Seric **	Side Effects:
55414872Seric **		none.
55514872Seric */
55614872Seric 
55751920Seric /* try to guess what style of load average we have */
55851920Seric #define LA_ZERO		1	/* always return load average as zero */
55951920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
56051920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
56151920Seric #define LA_SUBR		4	/* call getloadavg */
56214872Seric 
56351920Seric #ifndef LA_TYPE
56460160Seric #  if defined(sun) && !defined(BSD)
56551920Seric #    define LA_TYPE		LA_INT
56651920Seric #  endif
56757977Seric #  if defined(mips) || defined(__alpha)
56857977Seric      /* Ultrix or OSF/1 or RISC/os */
56951920Seric #    define LA_TYPE		LA_INT
57051920Seric #    define LA_AVENRUN		"avenrun"
57151920Seric #  endif
57259430Seric #  if defined(__hpux)
57351920Seric #    define LA_TYPE		LA_FLOAT
57459253Seric #    define LA_AVENRUN		"avenrun"
57551920Seric #  endif
57651920Seric 
577*60219Seric /* now do the guesses based on general OS type */
57851920Seric #  ifndef LA_TYPE
57957736Seric #   if defined(SYSTEM5)
58057736Seric #    define LA_TYPE		LA_INT
58157736Seric #    define LA_AVENRUN		"avenrun"
58257736Seric #   else
58357736Seric #    if defined(BSD)
58457736Seric #     define LA_TYPE		LA_SUBR
58557736Seric #    else
58657736Seric #     define LA_TYPE		LA_ZERO
58757736Seric #    endif
58857736Seric #   endif
58951920Seric #  endif
59051920Seric #endif
59151920Seric 
59251920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
59351920Seric 
59414872Seric #include <nlist.h>
59514872Seric 
59651920Seric #ifndef LA_AVENRUN
59751920Seric #define LA_AVENRUN	"_avenrun"
59851920Seric #endif
59951920Seric 
60051920Seric /* _PATH_UNIX should be defined in <paths.h> */
60151920Seric #ifndef _PATH_UNIX
60259430Seric #  if defined(__hpux)
60351920Seric #    define _PATH_UNIX		"/hp-ux"
60451920Seric #  endif
60551920Seric #  if defined(mips) && !defined(ultrix)
60651920Seric      /* powerful RISC/os */
60751920Seric #    define _PATH_UNIX		"/unix"
60851920Seric #  endif
60957736Seric #  if defined(SYSTEM5)
61057977Seric #    ifndef _PATH_UNIX
61157977Seric #      define _PATH_UNIX	"/unix"
61257977Seric #    endif
61357736Seric #  endif
61451920Seric #  ifndef _PATH_UNIX
61551920Seric #    define _PATH_UNIX		"/vmunix"
61651920Seric #  endif
61751920Seric #endif
61851920Seric 
61914872Seric struct	nlist Nl[] =
62014872Seric {
62151920Seric 	{ LA_AVENRUN },
62214872Seric #define	X_AVENRUN	0
62314872Seric 	{ 0 },
62414872Seric };
62514872Seric 
62657736Seric #if defined(unixpc)
62757736Seric # define FSHIFT		5
62857736Seric #endif
62957736Seric 
63057977Seric #if defined(__alpha)
63157977Seric # define FSHIFT		10
63257977Seric #endif
63357977Seric 
63451920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
63551920Seric #  define FSHIFT	8
63657736Seric #endif
63757736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
63851920Seric #  define FSCALE	(1 << FSHIFT)
63951920Seric #endif
64040930Srick 
64114872Seric getla()
64214872Seric {
64314872Seric 	static int kmem = -1;
64451920Seric #if LA_TYPE == LA_INT
64524943Seric 	long avenrun[3];
64651920Seric #else
64751920Seric 	double avenrun[3];
64851920Seric #endif
64925615Seric 	extern off_t lseek();
65057736Seric 	extern int errno;
65114872Seric 
65214872Seric 	if (kmem < 0)
65314872Seric 	{
65424945Seric 		kmem = open("/dev/kmem", 0, 0);
65514872Seric 		if (kmem < 0)
65657736Seric 		{
65757736Seric 			if (tTd(3, 1))
65857736Seric 				printf("getla: open(/dev/kmem): %s\n",
65957736Seric 					errstring(errno));
66014872Seric 			return (-1);
66157736Seric 		}
66251920Seric 		(void) fcntl(kmem, F_SETFD, 1);
66357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
66457736Seric 		{
66557736Seric 			if (tTd(3, 1))
66657736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
66757736Seric 					errstring(errno));
66814872Seric 			return (-1);
66957736Seric 		}
67059253Seric 		if (Nl[X_AVENRUN].n_value == 0)
67159253Seric 		{
67259253Seric 			if (tTd(3, 1))
67359253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
67459253Seric 					_PATH_UNIX, LA_AVENRUN);
67559253Seric 			return (-1);
67659253Seric 		}
67714872Seric 	}
67857736Seric 	if (tTd(3, 20))
67957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
68024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
68123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
68219967Seric 	{
68319967Seric 		/* thank you Ian */
68457736Seric 		if (tTd(3, 1))
68557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
68619967Seric 		return (-1);
68719967Seric 	}
68851920Seric #if LA_TYPE == LA_INT
68957736Seric 	if (tTd(3, 5))
69057736Seric 	{
69157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
69257736Seric 		if (tTd(3, 15))
69357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
69457736Seric 		printf("\n");
69557736Seric 	}
69657736Seric 	if (tTd(3, 1))
69757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69951920Seric #else
70057736Seric 	if (tTd(3, 5))
70157736Seric 	{
70257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
70357736Seric 		if (tTd(3, 15))
70457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
70557736Seric 		printf("\n");
70657736Seric 	}
70757736Seric 	if (tTd(3, 1))
70857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
70951920Seric 	return ((int) (avenrun[0] + 0.5));
71051920Seric #endif
71114872Seric }
71214872Seric 
71351773Seric #else
71451920Seric #if LA_TYPE == LA_SUBR
71551773Seric 
71651773Seric getla()
71751773Seric {
71851920Seric 	double avenrun[3];
71951920Seric 
72051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
72157736Seric 	{
72257736Seric 		if (tTd(3, 1))
72357736Seric 			perror("getla: getloadavg failed:");
72451920Seric 		return (-1);
72557736Seric 	}
72657736Seric 	if (tTd(3, 1))
72757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72851920Seric 	return ((int) (avenrun[0] + 0.5));
72951773Seric }
73051773Seric 
73151773Seric #else
73251773Seric 
73351773Seric getla()
73451773Seric {
73557736Seric 	if (tTd(3, 1))
73657736Seric 		printf("getla: ZERO\n");
73751920Seric 	return (0);
73851773Seric }
73951773Seric 
74051773Seric #endif
74151773Seric #endif
74224943Seric /*
74324943Seric **  SHOULDQUEUE -- should this message be queued or sent?
74424943Seric **
74524943Seric **	Compares the message cost to the load average to decide.
74624943Seric **
74724943Seric **	Parameters:
74824943Seric **		pri -- the priority of the message in question.
74957438Seric **		ctime -- the message creation time.
75024943Seric **
75124943Seric **	Returns:
75224943Seric **		TRUE -- if this message should be queued up for the
75324943Seric **			time being.
75424943Seric **		FALSE -- if the load is low enough to send this message.
75524943Seric **
75624943Seric **	Side Effects:
75724943Seric **		none.
75824943Seric */
75924943Seric 
76024943Seric bool
76157438Seric shouldqueue(pri, ctime)
76224943Seric 	long pri;
76357438Seric 	time_t ctime;
76424943Seric {
76551920Seric 	if (CurrentLA < QueueLA)
76624943Seric 		return (FALSE);
76758132Seric 	if (CurrentLA >= RefuseLA)
76858132Seric 		return (TRUE);
76951920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77024943Seric }
77124943Seric /*
77253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
77353037Seric **
77453037Seric **	Parameters:
77553037Seric **		none.
77653037Seric **
77753037Seric **	Returns:
77853037Seric **		TRUE if incoming SMTP connections should be refused
77953037Seric **			(for now).
78053037Seric **		FALSE if we should accept new work.
78153037Seric **
78253037Seric **	Side Effects:
78353037Seric **		none.
78453037Seric */
78553037Seric 
78653037Seric bool
78753037Seric refuseconnections()
78853037Seric {
78959156Seric #ifdef XLA
79059156Seric 	if (!xla_smtp_ok())
79159156Seric 		return TRUE;
79259156Seric #endif
79359156Seric 
79453037Seric 	/* this is probably too simplistic */
79558132Seric 	return (CurrentLA >= RefuseLA);
79653037Seric }
79753037Seric /*
79824943Seric **  SETPROCTITLE -- set process title for ps
79924943Seric **
80024943Seric **	Parameters:
80158674Seric **		fmt -- a printf style format string.
80258674Seric **		a, b, c -- possible parameters to fmt.
80324943Seric **
80424943Seric **	Returns:
80524943Seric **		none.
80624943Seric **
80724943Seric **	Side Effects:
80824943Seric **		Clobbers argv of our main procedure so ps(1) will
80924943Seric **		display the title.
81024943Seric */
81124943Seric 
81258689Seric #ifdef SETPROCTITLE
81358689Seric # ifdef __hpux
81458689Seric #  include <sys/pstat.h>
81558689Seric # endif
81659732Seric # ifdef BSD4_4
81759732Seric #  include <machine/vmparam.h>
81859732Seric #  include <sys/exec.h>
81959992Seric #  ifdef PS_STRINGS
82059992Seric #   define SETPROC_STATIC static
82159992Seric #  endif
82259732Seric # endif
82359732Seric # ifndef SETPROC_STATIC
82459732Seric #  define SETPROC_STATIC
82559732Seric # endif
82658689Seric #endif
82758689Seric 
82824943Seric /*VARARGS1*/
82957642Seric #ifdef __STDC__
83057642Seric setproctitle(char *fmt, ...)
83157642Seric #else
83257642Seric setproctitle(fmt, va_alist)
83324943Seric 	char *fmt;
83457642Seric 	va_dcl
83557642Seric #endif
83624943Seric {
83724943Seric # ifdef SETPROCTITLE
83824943Seric 	register char *p;
83925049Seric 	register int i;
84059732Seric 	SETPROC_STATIC char buf[MAXLINE];
84156852Seric 	VA_LOCAL_DECL
84258689Seric #  ifdef __hpux
84358689Seric 	union pstun pst;
84458689Seric #  endif
84524943Seric 	extern char **Argv;
84624943Seric 	extern char *LastArgv;
84724943Seric 
84858674Seric 	p = buf;
84924943Seric 
85058674Seric 	/* print sendmail: heading for grep */
85158674Seric 	(void) strcpy(p, "sendmail: ");
85258674Seric 	p += strlen(p);
85324943Seric 
85458674Seric 	/* print the argument string */
85558674Seric 	VA_START(fmt);
85658674Seric 	(void) vsprintf(p, fmt, ap);
85756852Seric 	VA_END;
85854996Seric 
85958674Seric 	i = strlen(buf);
86058689Seric 
86158689Seric #  ifdef __hpux
86258689Seric 	pst.pst_command = buf;
86358689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
86458689Seric #  else
86559992Seric #   ifdef PS_STRINGS
86659732Seric 	PS_STRINGS->ps_nargvstr = 1;
86759732Seric 	PS_STRINGS->ps_argvstr = buf;
86859732Seric #   else
86954996Seric 	if (i > LastArgv - Argv[0] - 2)
87025049Seric 	{
87154996Seric 		i = LastArgv - Argv[0] - 2;
87258674Seric 		buf[i] = '\0';
87325049Seric 	}
87458674Seric 	(void) strcpy(Argv[0], buf);
87554997Seric 	p = &Argv[0][i];
87624943Seric 	while (p < LastArgv)
87724943Seric 		*p++ = ' ';
87859732Seric #   endif
87958689Seric #  endif
88056795Seric # endif /* SETPROCTITLE */
88124943Seric }
88225698Seric /*
88325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88425698Seric **
88525698Seric **	Parameters:
88625698Seric **		none.
88725698Seric **
88825698Seric **	Returns:
88925698Seric **		none.
89025698Seric **
89125698Seric **	Side Effects:
89225698Seric **		Picks up extant zombies.
89325698Seric */
89425698Seric 
89525698Seric # include <sys/wait.h>
89625698Seric 
89746928Sbostic void
89825698Seric reapchild()
89925698Seric {
900*60219Seric # ifdef WIFEXITED
901*60219Seric 	auto int status;
902*60219Seric 
903*60219Seric 	while (waitpid(-1, &status, WNOHANG) > 0)
904*60219Seric 		continue;
905*60219Seric # else
90625698Seric # ifdef WNOHANG
90725698Seric 	union wait status;
90825698Seric 
90946928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
91025698Seric 		continue;
91156795Seric # else /* WNOHANG */
91225698Seric 	auto int status;
91325698Seric 
914*60219Seric 	while (wait(&status) > 0)
91525698Seric 		continue;
91656795Seric # endif /* WNOHANG */
917*60219Seric # endif
91858061Seric # ifdef SYSTEM5
91958061Seric 	(void) signal(SIGCHLD, reapchild);
92058061Seric # endif
92125698Seric }
92255418Seric /*
92355418Seric **  UNSETENV -- remove a variable from the environment
92455418Seric **
92555418Seric **	Not needed on newer systems.
92655418Seric **
92755418Seric **	Parameters:
92855418Seric **		name -- the string name of the environment variable to be
92955418Seric **			deleted from the current environment.
93055418Seric **
93155418Seric **	Returns:
93255418Seric **		none.
93355418Seric **
93455418Seric **	Globals:
93555418Seric **		environ -- a pointer to the current environment.
93655418Seric **
93755418Seric **	Side Effects:
93855418Seric **		Modifies environ.
93955418Seric */
94055418Seric 
94155418Seric #ifdef UNSETENV
94255418Seric 
94355418Seric void
94455418Seric unsetenv(name)
94555418Seric 	char *name;
94655418Seric {
94755418Seric 	extern char **environ;
94855418Seric 	register char **pp;
94955418Seric 	int len = strlen(name);
95055418Seric 
95155418Seric 	for (pp = environ; *pp != NULL; pp++)
95255418Seric 	{
95355418Seric 		if (strncmp(name, *pp, len) == 0 &&
95455418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
95555418Seric 			break;
95655418Seric 	}
95755418Seric 
95855418Seric 	for (; *pp != NULL; pp++)
95955418Seric 		*pp = pp[1];
96055418Seric }
96155418Seric 
96255418Seric #endif /* UNSETENV */
96356215Seric /*
96456215Seric **  GETDTABLESIZE -- return number of file descriptors
96556215Seric **
96656215Seric **	Only on non-BSD systems
96756215Seric **
96856215Seric **	Parameters:
96956215Seric **		none
97056215Seric **
97156215Seric **	Returns:
97256215Seric **		size of file descriptor table
97356215Seric **
97456215Seric **	Side Effects:
97556215Seric **		none
97656215Seric */
97756215Seric 
97856215Seric #ifdef SYSTEM5
97956215Seric 
98056215Seric int
98156215Seric getdtablesize()
98256215Seric {
98358689Seric # ifdef _SC_OPEN_MAX
98458689Seric 	return sysconf(_SC_OPEN_MAX);
98558689Seric # else
98656215Seric 	return NOFILE;
98758689Seric # endif
98856215Seric }
98956215Seric 
99056215Seric #endif
99157631Seric /*
99257631Seric **  UNAME -- get the UUCP name of this system.
99357631Seric */
99457631Seric 
99557943Seric #ifndef HASUNAME
99657631Seric 
99757631Seric int
99857631Seric uname(name)
99957631Seric 	struct utsname *name;
100057631Seric {
100157631Seric 	FILE *file;
100257631Seric 	char *n;
100357631Seric 
100457631Seric 	name->nodename[0] = '\0';
100557631Seric 
100657661Seric 	/* try /etc/whoami -- one line with the node name */
100757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
100857631Seric 	{
100957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
101057631Seric 		(void) fclose(file);
101157661Seric 		n = strchr(name->nodename, '\n');
101257631Seric 		if (n != NULL)
101357631Seric 			*n = '\0';
101457631Seric 		if (name->nodename[0] != '\0')
101557631Seric 			return (0);
101657631Seric 	}
101757631Seric 
101857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
101957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
102057631Seric 	{
102157631Seric 		char buf[MAXLINE];
102257631Seric 
102357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
102457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
102557631Seric 					NODE_LENGTH, name->nodename) > 0)
102657631Seric 				break;
102757631Seric 		(void) fclose(file);
102857631Seric 		if (name->nodename[0] != '\0')
102957631Seric 			return (0);
103057631Seric 	}
103157631Seric 
103257631Seric #ifdef TRUST_POPEN
103357631Seric 	/*
103457631Seric 	**  Popen is known to have security holes.
103557631Seric 	*/
103657631Seric 
103757661Seric 	/* try uuname -l to return local name */
103857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
103957631Seric 	{
104057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
104157631Seric 		(void) pclose(file);
104257661Seric 		n = strchr(name, '\n');
104357631Seric 		if (n != NULL)
104457631Seric 			*n = '\0';
104557661Seric 		if (name->nodename[0] != '\0')
104657631Seric 			return (0);
104757631Seric 	}
104857631Seric #endif
104957631Seric 
105057631Seric 	return (-1);
105157631Seric }
105257943Seric #endif /* HASUNAME */
105358068Seric /*
105458068Seric **  INITGROUPS -- initialize groups
105558068Seric **
105658068Seric **	Stub implementation for System V style systems
105758068Seric */
105858068Seric 
105958068Seric #ifndef HASINITGROUPS
106059430Seric # if !defined(SYSTEM5) || defined(__hpux)
106158068Seric #  define HASINITGROUPS
106258068Seric # endif
106358068Seric #endif
106458068Seric 
106558068Seric #ifndef HASINITGROUPS
106658068Seric 
106758068Seric initgroups(name, basegid)
106858068Seric 	char *name;
106958068Seric 	int basegid;
107058068Seric {
107158068Seric 	return 0;
107258068Seric }
107358068Seric 
107458068Seric #endif
107558082Seric /*
107659289Seric **  SETSID -- set session id (for non-POSIX systems)
107759289Seric */
107859289Seric 
107959289Seric #ifndef HASSETSID
108059289Seric 
108159289Seric setsid()
108259289Seric {
108359289Seric # ifdef SYSTEM5
108459289Seric 	setpgrp();
108559289Seric # endif
108659289Seric }
108759289Seric 
108859289Seric #endif
108959289Seric /*
109058082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
109158082Seric **
109258082Seric **	Only implemented if you have statfs.
109358082Seric **
109458082Seric **	Parameters:
109558333Seric **		msize -- the size to check against.  If zero, we don't yet
109658333Seric **			know how big the message will be, so just check for
109758333Seric **			a "reasonable" amount.
109858082Seric **
109958082Seric **	Returns:
110058082Seric **		TRUE if there is enough space.
110158082Seric **		FALSE otherwise.
110258082Seric */
110358082Seric 
110458082Seric #ifndef HASSTATFS
110558082Seric # if defined(BSD4_4) || defined(__osf__)
110658082Seric #  define HASSTATFS
110758082Seric # endif
110858082Seric #endif
110958082Seric 
111058082Seric #ifdef HASSTATFS
111158157Seric # undef HASUSTAT
111258157Seric #endif
111358157Seric 
111458157Seric #if defined(HASUSTAT)
111558157Seric # include <ustat.h>
111658157Seric #endif
111758157Seric 
111858157Seric #ifdef HASSTATFS
111958133Seric # if defined(sgi) || defined(apollo)
112058133Seric #  include <sys/statfs.h>
112158133Seric # else
112260160Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
112358133Seric #   include <sys/vfs.h>
112458133Seric #  else
112558157Seric #   include <sys/mount.h>
112658133Seric #  endif
112758133Seric # endif
112858082Seric #endif
112958082Seric 
113058082Seric bool
113158333Seric enoughspace(msize)
113258333Seric 	long msize;
113358082Seric {
113458160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
113558157Seric # if defined(HASUSTAT)
113658153Seric 	struct ustat fs;
113758153Seric 	struct stat statbuf;
113858366Seric #  define FSBLOCKSIZE	DEV_BSIZE
113958157Seric #  define f_bavail	f_tfree
114058157Seric # else
114158157Seric #  if defined(ultrix)
114258157Seric 	struct fs_data fs;
114358157Seric #   define f_bavail	fd_bfreen
114458366Seric #   define FSBLOCKSIZE	fs.fd_bsize
114558153Seric #  else
114658082Seric 	struct statfs fs;
114758366Seric #   define FSBLOCKSIZE	fs.f_bsize
114858153Seric #  endif
114958133Seric # endif
115058333Seric 	long blocksneeded;
115158082Seric 	extern int errno;
115258082Seric 
115358333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
115458082Seric 	{
115558082Seric 		if (tTd(4, 80))
115658133Seric 			printf("enoughspace: no threshold\n");
115758133Seric 		return TRUE;
115858133Seric 	}
115958333Seric 
116058157Seric # if defined(HASUSTAT)
116158157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
116258157Seric # else
116358157Seric #  if defined(sgi) || defined(apollo)
116458133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
116558157Seric #  else
116658157Seric #   if defined(ultrix)
116758133Seric 	if (statfs(QueueDir, &fs) > 0)
116858153Seric #   else
116958133Seric 	if (statfs(QueueDir, &fs) == 0)
117058153Seric #   endif
117158133Seric #  endif
117258133Seric # endif
117358133Seric 	{
117458133Seric 		if (tTd(4, 80))
117558333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
117658333Seric 				fs.f_bavail, msize);
117758333Seric 
117858333Seric 		/* convert msize to block count */
117958366Seric 		msize = msize / FSBLOCKSIZE + 1;
118058333Seric 		if (MinBlocksFree >= 0)
118158333Seric 			msize += MinBlocksFree;
118258333Seric 
118358333Seric 		if (fs.f_bavail < msize)
118458090Seric 		{
118558090Seric #ifdef LOG
118658090Seric 			if (LogLevel > 0)
118758090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
118858333Seric 					QueueDir, fs.f_bavail, msize);
118958090Seric #endif
119058082Seric 			return FALSE;
119158090Seric 		}
119258082Seric 	}
119358082Seric 	else if (tTd(4, 80))
119458333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
119558333Seric 			MinBlocksFree, msize, errstring(errno));
119658082Seric #endif
119758082Seric 	return TRUE;
119858082Seric }
119958542Seric /*
120058542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
120158542Seric **
120258542Seric **	This looks at an errno value and tells if this is likely to
120358542Seric **	go away if retried later.
120458542Seric **
120558542Seric **	Parameters:
120658542Seric **		err -- the errno code to classify.
120758542Seric **
120858542Seric **	Returns:
120958542Seric **		TRUE if this is probably transient.
121058542Seric **		FALSE otherwise.
121158542Seric */
121258542Seric 
121358542Seric bool
121458542Seric transienterror(err)
121558542Seric 	int err;
121658542Seric {
121758542Seric 	switch (err)
121858542Seric 	{
121958542Seric 	  case EIO:			/* I/O error */
122058542Seric 	  case ENXIO:			/* Device not configured */
122158542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
122258542Seric 	  case ENOMEM:			/* Cannot allocate memory */
122358542Seric 	  case ENODEV:			/* Operation not supported by device */
122458542Seric 	  case ENFILE:			/* Too many open files in system */
122558542Seric 	  case EMFILE:			/* Too many open files */
122658542Seric 	  case ENOSPC:			/* No space left on device */
122758542Seric #ifdef ETIMEDOUT
122858542Seric 	  case ETIMEDOUT:		/* Connection timed out */
122958542Seric #endif
123058542Seric #ifdef ESTALE
123158542Seric 	  case ESTALE:			/* Stale NFS file handle */
123258542Seric #endif
123358542Seric #ifdef ENETDOWN
123458542Seric 	  case ENETDOWN:		/* Network is down */
123558542Seric #endif
123658542Seric #ifdef ENETUNREACH
123758542Seric 	  case ENETUNREACH:		/* Network is unreachable */
123858542Seric #endif
123958542Seric #ifdef ENETRESET
124058542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
124158542Seric #endif
124258542Seric #ifdef ECONNABORTED
124358542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
124458542Seric #endif
124558542Seric #ifdef ECONNRESET
124658542Seric 	  case ECONNRESET:		/* Connection reset by peer */
124758542Seric #endif
124858542Seric #ifdef ENOBUFS
124958542Seric 	  case ENOBUFS:			/* No buffer space available */
125058542Seric #endif
125158542Seric #ifdef ESHUTDOWN
125258542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
125358542Seric #endif
125458542Seric #ifdef ECONNREFUSED
125558542Seric 	  case ECONNREFUSED:		/* Connection refused */
125658542Seric #endif
125758542Seric #ifdef EHOSTDOWN
125858542Seric 	  case EHOSTDOWN:		/* Host is down */
125958542Seric #endif
126058542Seric #ifdef EHOSTUNREACH
126158542Seric 	  case EHOSTUNREACH:		/* No route to host */
126258542Seric #endif
126358542Seric #ifdef EDQUOT
126458542Seric 	  case EDQUOT:			/* Disc quota exceeded */
126558542Seric #endif
126658542Seric #ifdef EPROCLIM
126758542Seric 	  case EPROCLIM:		/* Too many processes */
126858542Seric #endif
126958542Seric #ifdef EUSERS
127058542Seric 	  case EUSERS:			/* Too many users */
127158542Seric #endif
127258542Seric #ifdef EDEADLK
127358542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
127458542Seric #endif
127558542Seric #ifdef EISCONN
127658542Seric 	  case EISCONN:			/* Socket already connected */
127758542Seric #endif
127858542Seric #ifdef EINPROGRESS
127958542Seric 	  case EINPROGRESS:		/* Operation now in progress */
128058542Seric #endif
128158542Seric #ifdef EALREADY
128258542Seric 	  case EALREADY:		/* Operation already in progress */
128358542Seric #endif
128458542Seric #ifdef EADDRINUSE
128558542Seric 	  case EADDRINUSE:		/* Address already in use */
128658542Seric #endif
128758542Seric #ifdef EADDRNOTAVAIL
128858542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
128958542Seric #endif
129058542Seric #ifdef ENOSR
129158542Seric 	  case ENOSR:			/* Out of streams resources */
129258542Seric #endif
129358542Seric 		return TRUE;
129458542Seric 	}
129558542Seric 
129658542Seric 	/* nope, must be permanent */
129758542Seric 	return FALSE;
129858542Seric }
129958689Seric /*
130058689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
130158689Seric **
130258689Seric **	Parameters:
130358689Seric **		fd -- the file descriptor of the file.
130458689Seric **		filename -- the file name (for error messages).
130558689Seric **		type -- type of the lock.  Bits can be:
130658689Seric **			LOCK_EX -- exclusive lock.
130758689Seric **			LOCK_NB -- non-blocking.
130858689Seric **
130958689Seric **	Returns:
131058689Seric **		TRUE if the lock was acquired.
131158689Seric **		FALSE otherwise.
131258689Seric */
131358689Seric 
131458689Seric bool
131558689Seric lockfile(fd, filename, type)
131658689Seric 	int fd;
131758689Seric 	char *filename;
131858689Seric 	int type;
131958689Seric {
132058689Seric # ifdef LOCKF
132158689Seric 	int action;
132258689Seric 	struct flock lfd;
132358689Seric 
132459447Seric 	if (bitset(LOCK_UN, type))
132559447Seric 		lfd.l_type = F_UNLCK;
132659447Seric 	else if (bitset(LOCK_EX, type))
132758689Seric 		lfd.l_type = F_WRLCK;
132858689Seric 	else
132958689Seric 		lfd.l_type = F_RDLCK;
133058689Seric 
133158689Seric 	if (bitset(LOCK_NB, type))
133258689Seric 		action = F_SETLK;
133358689Seric 	else
133458689Seric 		action = F_SETLKW;
133558689Seric 
133658689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
133758689Seric 
133858689Seric 	if (fcntl(fd, action, &lfd) >= 0)
133958689Seric 		return TRUE;
134058689Seric 
134158689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
134259447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
134358689Seric # else
134458689Seric 	if (flock(fd, type) >= 0)
134558689Seric 		return TRUE;
134658689Seric 
134758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
134859447Seric 		syserr("cannot flock(%s, %o)", filename, type);
134958689Seric # endif
135058689Seric 	return FALSE;
135158689Seric }
1352