xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 58737)
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*58737Seric static char sccsid[] = "@(#)conf.c	6.40 (Berkeley) 03/19/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,
668060Seric 		/* destination fields */
679055Seric 	"to",			H_RCPT,
6811417Seric 	"resent-to",		H_RCPT|H_RESENT,
699055Seric 	"cc",			H_RCPT,
7011417Seric 	"resent-cc",		H_RCPT|H_RESENT,
719055Seric 	"bcc",			H_RCPT|H_ACHECK,
7211417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7356215Seric 	"apparently-to",	H_RCPT,
748060Seric 		/* message identification and control */
7511417Seric 	"message-id",		0,
7611417Seric 	"resent-message-id",	H_RESENT,
779055Seric 	"message",		H_EOH,
789055Seric 	"text",			H_EOH,
7911417Seric 		/* date fields */
8011417Seric 	"date",			0,
8111417Seric 	"resent-date",		H_RESENT,
828060Seric 		/* trace fields */
839055Seric 	"received",		H_TRACE|H_FORCE,
849055Seric 	"via",			H_TRACE|H_FORCE,
859055Seric 	"mail-from",		H_TRACE|H_FORCE,
868060Seric 
879055Seric 	NULL,			0,
882897Seric };
894166Seric 
904166Seric 
914166Seric 
924282Seric /*
934282Seric **  Location of system files/databases/etc.
944282Seric */
954282Seric 
9640980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
9740980Sbostic char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
9858082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
999039Seric 
1009064Seric 
1019064Seric 
1029039Seric /*
10358082Seric **  Privacy values
10458082Seric */
10558082Seric 
10658082Seric struct prival PrivacyValues[] =
10758082Seric {
10858082Seric 	"public",		PRIV_PUBLIC,
10958082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11058114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11158082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11258082Seric 	"noexpn",		PRIV_NOEXPN,
11358082Seric 	"novrfy",		PRIV_NOVRFY,
11458249Seric 	"restrictmailq",	PRIV_RESTRMAILQ,
11558082Seric 	"goaway",		PRIV_GOAWAY,
11658082Seric 	NULL,			PRIV_PUBLIC,
11758082Seric };
11858082Seric 
11958082Seric 
12058082Seric 
12158082Seric /*
12224943Seric **  Miscellaneous stuff.
1239039Seric */
1249039Seric 
12524943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
12624943Seric /*
12724943Seric **  SETDEFAULTS -- set default values
12824943Seric **
12924943Seric **	Because of the way freezing is done, these must be initialized
13024943Seric **	using direct code.
13124943Seric **
13224943Seric **	Parameters:
13358734Seric **		e -- the default envelope.
13424943Seric **
13524943Seric **	Returns:
13624943Seric **		none.
13724943Seric **
13824943Seric **	Side Effects:
13924943Seric **		Initializes a bunch of global variables to their
14024943Seric **		default values.
14124943Seric */
14224943Seric 
143*58737Seric #define DAYS		* 24 * 60 * 60
144*58737Seric 
14558734Seric setdefaults(e)
14658734Seric 	register ENVELOPE *e;
14724943Seric {
14857438Seric 	SpaceSub = ' ';				/* option B */
14957438Seric 	QueueLA = 8;				/* option x */
15057438Seric 	RefuseLA = 12;				/* option X */
15157438Seric 	WkRecipFact = 30000L;			/* option y */
15257438Seric 	WkClassFact = 1800L;			/* option z */
15357438Seric 	WkTimeFact = 90000L;			/* option Z */
15457438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
15557142Seric 	FileMode = (getuid() != geteuid()) ? 0644 : 0600;
15657438Seric 						/* option F */
15757438Seric 	DefUid = 1;				/* option u */
15857438Seric 	DefGid = 1;				/* option g */
15957438Seric 	CheckpointInterval = 10;		/* option C */
16057438Seric 	MaxHopCount = 25;			/* option h */
16158734Seric 	e->e_sendmode = SM_FORK;		/* option d */
16258734Seric 	e->e_errormode = EM_PRINT;		/* option e */
16357438Seric 	EightBit = FALSE;			/* option 8 */
16457438Seric 	MaxMciCache = 1;			/* option k */
16557438Seric 	MciCacheTimeout = 300;			/* option K */
16657438Seric 	LogLevel = 9;				/* option L */
16758112Seric 	settimeouts(NULL);			/* option r */
168*58737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
169*58737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
17040973Sbostic 	setdefuser();
17153654Seric 	setupmaps();
17257402Seric 	setupmailers();
17324943Seric }
174294Seric 
17540973Sbostic 
1764326Seric /*
17740973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
17840973Sbostic */
17940973Sbostic 
18040973Sbostic setdefuser()
18140973Sbostic {
18240973Sbostic 	struct passwd *defpwent;
18357386Seric 	static char defuserbuf[40];
18440973Sbostic 
18557386Seric 	DefUser = defuserbuf;
18640973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
18757386Seric 		strcpy(defuserbuf, defpwent->pw_name);
18840973Sbostic 	else
18957386Seric 		strcpy(defuserbuf, "nobody");
19040973Sbostic }
19153654Seric /*
19253654Seric **  SETUPMAPS -- set up map classes
19353654Seric **
19453654Seric **	Since these are compiled in, they cannot be in the config file.
19553654Seric **
19653654Seric */
19740973Sbostic 
19853654Seric setupmaps()
19953654Seric {
20053654Seric 	register STAB *s;
20156836Seric 	extern bool host_map_init();
20253654Seric 	extern char *maphostname();
20340973Sbostic 
20453654Seric 	/* set up host name lookup map */
20553654Seric 	s = stab("host", ST_MAPCLASS, ST_ENTER);
20656836Seric 	s->s_mapclass.map_init = host_map_init;
20753654Seric 	s->s_mapclass.map_lookup = maphostname;
20853654Seric 
20953654Seric 	/*
21053654Seric 	**  Set up other map classes.
21153654Seric 	*/
21253654Seric 
21353654Seric # ifdef DBM_MAP
21453654Seric 	/* dbm file access */
21553654Seric 	{
21656836Seric 		extern bool dbm_map_init();
21753654Seric 		extern char *dbm_map_lookup();
21853654Seric 
21953654Seric 		s = stab("dbm", ST_MAPCLASS, ST_ENTER);
22053654Seric 		s->s_mapclass.map_init = dbm_map_init;
22153654Seric 		s->s_mapclass.map_lookup = dbm_map_lookup;
22253654Seric 	}
22353654Seric # endif
22453654Seric 
22553654Seric # ifdef BTREE_MAP
22653654Seric 	/* new database file access -- btree files */
22753654Seric 	{
22856823Seric 		extern bool bt_map_init();
22956823Seric 		extern char *db_map_lookup();
23053654Seric 
23153654Seric 		s = stab("btree", ST_MAPCLASS, ST_ENTER);
23253654Seric 		s->s_mapclass.map_init = bt_map_init;
23356823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
23453654Seric 	}
23553654Seric # endif
23653654Seric 
23753654Seric # ifdef HASH_MAP
23853654Seric 	/* new database file access -- hash files */
23953654Seric 	{
24056823Seric 		extern bool hash_map_init();
24156823Seric 		extern char *db_map_lookup();
24253654Seric 
24353654Seric 		s = stab("hash", ST_MAPCLASS, ST_ENTER);
24453654Seric 		s->s_mapclass.map_init = hash_map_init;
24556823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
24653654Seric 	}
24753654Seric # endif
24853654Seric 
24957208Seric # ifdef NIS_MAP
25057208Seric 	/* NIS map access */
25157208Seric 	{
25257208Seric 		extern bool nis_map_init();
25357208Seric 		extern char *nis_map_lookup();
25457208Seric 
25557208Seric 		s = stab("nis", ST_MAPCLASS, ST_ENTER);
25657208Seric 		s->s_mapclass.map_init = nis_map_init;
25757208Seric 		s->s_mapclass.map_lookup = nis_map_lookup;
25857208Seric 	}
25957208Seric # endif
26057208Seric 
26153654Seric # ifdef USERDB_MAP
26253654Seric 	/* user database */
26353654Seric 	{
26456836Seric 		extern bool udb_map_init();
26553654Seric 		extern char *udb_map_lookup();
26653654Seric 
26753654Seric 		s = stab("udb", ST_MAPCLASS, ST_ENTER);
26853654Seric 		s->s_mapclass.map_init = udb_map_init;
26953654Seric 		s->s_mapclass.map_lookup = udb_map_lookup;
27053654Seric 	}
27153654Seric # endif
27253654Seric }
27353654Seric /*
27456836Seric **  HOST_MAP_INIT -- initialize host class structures
27556836Seric */
27656836Seric 
27756836Seric bool
27856836Seric host_map_init(map, mapname, args)
27956836Seric 	MAP *map;
28056836Seric 	char *mapname;
28156836Seric 	char *args;
28256836Seric {
28356836Seric 	register char *p = args;
28456836Seric 
28556836Seric 	for (;;)
28656836Seric 	{
28758050Seric 		while (isascii(*p) && isspace(*p))
28856836Seric 			p++;
28956836Seric 		if (*p != '-')
29056836Seric 			break;
29156836Seric 		switch (*++p)
29256836Seric 		{
29356836Seric 		  case 'a':
29456836Seric 			map->map_app = ++p;
29556836Seric 			break;
29656836Seric 		}
29758050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
29856836Seric 			p++;
29956836Seric 		if (*p != '\0')
30056836Seric 			*p++ = '\0';
30156836Seric 	}
30256836Seric 	if (map->map_app != NULL)
30356836Seric 		map->map_app = newstr(map->map_app);
30456836Seric 	return TRUE;
30556836Seric }
30657402Seric /*
30757402Seric **  SETUPMAILERS -- initialize default mailers
30857402Seric */
30956836Seric 
31057402Seric setupmailers()
31157402Seric {
31257402Seric 	char buf[100];
31357402Seric 
31457403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
31557403Seric 	makemailer(buf);
31657403Seric 
31757402Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE");
31857402Seric 	makemailer(buf);
31957402Seric 
32057402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
32157402Seric 	makemailer(buf);
32257402Seric }
32356836Seric /*
3244326Seric **  GETRUID -- get real user id (V7)
3254326Seric */
3264326Seric 
3274326Seric getruid()
3284326Seric {
3299274Seric 	if (OpMode == MD_DAEMON)
3304536Seric 		return (RealUid);
3314536Seric 	else
3324536Seric 		return (getuid());
3334326Seric }
3344326Seric 
3354326Seric 
3364326Seric /*
3374326Seric **  GETRGID -- get real group id (V7).
3384326Seric */
3394326Seric 
3404326Seric getrgid()
3414326Seric {
3429274Seric 	if (OpMode == MD_DAEMON)
3434536Seric 		return (RealGid);
3444536Seric 	else
3454536Seric 		return (getgid());
3464326Seric }
34753654Seric /*
3489369Seric **  USERNAME -- return the user id of the logged in user.
3499369Seric **
3509369Seric **	Parameters:
3519369Seric **		none.
3529369Seric **
3539369Seric **	Returns:
3549369Seric **		The login name of the logged in user.
3559369Seric **
3569369Seric **	Side Effects:
3579369Seric **		none.
3589369Seric **
3599369Seric **	Notes:
3609369Seric **		The return value is statically allocated.
3619369Seric */
3629369Seric 
3639369Seric char *
3649369Seric username()
3659369Seric {
36617469Seric 	static char *myname = NULL;
3679369Seric 	extern char *getlogin();
36819904Smiriam 	register struct passwd *pw;
3699369Seric 
37017469Seric 	/* cache the result */
37117469Seric 	if (myname == NULL)
37217469Seric 	{
37317469Seric 		myname = getlogin();
37417469Seric 		if (myname == NULL || myname[0] == '\0')
37517469Seric 		{
37617469Seric 			pw = getpwuid(getruid());
37717469Seric 			if (pw != NULL)
37840993Sbostic 				myname = newstr(pw->pw_name);
37917469Seric 		}
38019904Smiriam 		else
38119904Smiriam 		{
38258736Seric 			uid_t uid = getuid();
38319873Smiriam 
38440993Sbostic 			myname = newstr(myname);
38540993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
38658736Seric 			      (uid != 0 && uid != pw->pw_uid))
38719904Smiriam 			{
38858736Seric 				pw = getpwuid(uid);
38924945Seric 				if (pw != NULL)
39040993Sbostic 					myname = newstr(pw->pw_name);
39119873Smiriam 			}
39219873Smiriam 		}
39317469Seric 		if (myname == NULL || myname[0] == '\0')
39417469Seric 		{
39558151Seric 			syserr("554 Who are you?");
39617469Seric 			myname = "postmaster";
39717469Seric 		}
39817469Seric 	}
39917469Seric 
40017469Seric 	return (myname);
4019369Seric }
4029369Seric /*
4034190Seric **  TTYPATH -- Get the path of the user's tty
404294Seric **
405294Seric **	Returns the pathname of the user's tty.  Returns NULL if
406294Seric **	the user is not logged in or if s/he has write permission
407294Seric **	denied.
408294Seric **
409294Seric **	Parameters:
410294Seric **		none
411294Seric **
412294Seric **	Returns:
413294Seric **		pathname of the user's tty.
414294Seric **		NULL if not logged in or write permission denied.
415294Seric **
416294Seric **	Side Effects:
417294Seric **		none.
418294Seric **
419294Seric **	WARNING:
420294Seric **		Return value is in a local buffer.
421294Seric **
422294Seric **	Called By:
423294Seric **		savemail
424294Seric */
425294Seric 
426294Seric # include <sys/stat.h>
427294Seric 
428294Seric char *
429294Seric ttypath()
430294Seric {
431294Seric 	struct stat stbuf;
432294Seric 	register char *pathn;
433294Seric 	extern char *ttyname();
4344081Seric 	extern char *getlogin();
435294Seric 
436294Seric 	/* compute the pathname of the controlling tty */
4379369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4389369Seric 	    (pathn = ttyname(0)) == NULL)
439294Seric 	{
440294Seric 		errno = 0;
441294Seric 		return (NULL);
442294Seric 	}
443294Seric 
444294Seric 	/* see if we have write permission */
4452967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
446294Seric 	{
447294Seric 		errno = 0;
448294Seric 		return (NULL);
449294Seric 	}
450294Seric 
451294Seric 	/* see if the user is logged in */
452294Seric 	if (getlogin() == NULL)
453294Seric 		return (NULL);
454294Seric 
455294Seric 	/* looks good */
456294Seric 	return (pathn);
457294Seric }
4582967Seric /*
4592967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4602967Seric **
4612967Seric **	This routine can be supplied on a per-installation basis
4622967Seric **	to determine whether a person is allowed to send a message.
4632967Seric **	This allows restriction of certain types of internet
4642967Seric **	forwarding or registration of users.
4652967Seric **
4662967Seric **	If the hosts are found to be incompatible, an error
46757454Seric **	message should be given using "usrerr" and 0 should
4682967Seric **	be returned.
4692967Seric **
4704288Seric **	'NoReturn' can be set to suppress the return-to-sender
4714288Seric **	function; this should be done on huge messages.
4724288Seric **
4732967Seric **	Parameters:
4742967Seric **		to -- the person being sent to.
4752967Seric **
4762967Seric **	Returns:
47757459Seric **		an exit status
4782967Seric **
4792967Seric **	Side Effects:
4802967Seric **		none (unless you include the usrerr stuff)
4812967Seric */
4822967Seric 
48355012Seric checkcompat(to, e)
4842967Seric 	register ADDRESS *to;
48555012Seric 	register ENVELOPE *e;
4862967Seric {
48712133Seric # ifdef lint
48812133Seric 	if (to == NULL)
48912133Seric 		to++;
49012133Seric # endif lint
49110698Seric # ifdef EXAMPLE_CODE
49210698Seric 	/* this code is intended as an example only */
4934437Seric 	register STAB *s;
4944437Seric 
4954437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49655012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4979369Seric 	    to->q_mailer == s->s_mailer)
4984437Seric 	{
49958151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50010698Seric 		/* NoReturn = TRUE; to supress return copy */
50157459Seric 		return (EX_UNAVAILABLE);
5024437Seric 	}
50356795Seric # endif /* EXAMPLE_CODE */
50457459Seric 	return (EX_OK);
5052967Seric }
5069369Seric /*
5079369Seric **  HOLDSIGS -- arrange to hold all signals
5089369Seric **
5099369Seric **	Parameters:
5109369Seric **		none.
5119369Seric **
5129369Seric **	Returns:
5139369Seric **		none.
5149369Seric **
5159369Seric **	Side Effects:
5169369Seric **		Arranges that signals are held.
5179369Seric */
5189369Seric 
5199369Seric holdsigs()
5209369Seric {
5219369Seric }
5229369Seric /*
5239369Seric **  RLSESIGS -- arrange to release all signals
5249369Seric **
5259369Seric **	This undoes the effect of holdsigs.
5269369Seric **
5279369Seric **	Parameters:
5289369Seric **		none.
5299369Seric **
5309369Seric **	Returns:
5319369Seric **		none.
5329369Seric **
5339369Seric **	Side Effects:
5349369Seric **		Arranges that signals are released.
5359369Seric */
5369369Seric 
5379369Seric rlsesigs()
5389369Seric {
5399369Seric }
54014872Seric /*
54114872Seric **  GETLA -- get the current load average
54214872Seric **
54314881Seric **	This code stolen from la.c.
54414881Seric **
54514872Seric **	Parameters:
54614872Seric **		none.
54714872Seric **
54814872Seric **	Returns:
54914872Seric **		The current load average as an integer.
55014872Seric **
55114872Seric **	Side Effects:
55214872Seric **		none.
55314872Seric */
55414872Seric 
55551920Seric /* try to guess what style of load average we have */
55651920Seric #define LA_ZERO		1	/* always return load average as zero */
55751920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
55851920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
55951920Seric #define LA_SUBR		4	/* call getloadavg */
56014872Seric 
56151920Seric #ifndef LA_TYPE
56251920Seric #  if defined(sun)
56351920Seric #    define LA_TYPE		LA_INT
56451920Seric #  endif
56557977Seric #  if defined(mips) || defined(__alpha)
56657977Seric      /* Ultrix or OSF/1 or RISC/os */
56751920Seric #    define LA_TYPE		LA_INT
56851920Seric #    define LA_AVENRUN		"avenrun"
56951920Seric #  endif
57051920Seric #  if defined(hpux)
57151920Seric #    define LA_TYPE		LA_FLOAT
57251920Seric #  endif
57351920Seric 
57451920Seric #  ifndef LA_TYPE
57557736Seric #   if defined(SYSTEM5)
57657736Seric #    define LA_TYPE		LA_INT
57757736Seric #    define LA_AVENRUN		"avenrun"
57857736Seric #   else
57957736Seric #    if defined(BSD)
58057736Seric #     define LA_TYPE		LA_SUBR
58157736Seric #    else
58257736Seric #     define LA_TYPE		LA_ZERO
58357736Seric #    endif
58457736Seric #   endif
58551920Seric #  endif
58651920Seric #endif
58751920Seric 
58851920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
58951920Seric 
59014872Seric #include <nlist.h>
59114872Seric 
59251920Seric #ifndef LA_AVENRUN
59351920Seric #define LA_AVENRUN	"_avenrun"
59451920Seric #endif
59551920Seric 
59651920Seric /* _PATH_UNIX should be defined in <paths.h> */
59751920Seric #ifndef _PATH_UNIX
59851920Seric #  if defined(hpux)
59951920Seric #    define _PATH_UNIX		"/hp-ux"
60051920Seric #  endif
60151920Seric #  if defined(mips) && !defined(ultrix)
60251920Seric      /* powerful RISC/os */
60351920Seric #    define _PATH_UNIX		"/unix"
60451920Seric #  endif
60557736Seric #  if defined(SYSTEM5)
60657977Seric #    ifndef _PATH_UNIX
60757977Seric #      define _PATH_UNIX	"/unix"
60857977Seric #    endif
60957736Seric #  endif
61051920Seric #  ifndef _PATH_UNIX
61151920Seric #    define _PATH_UNIX		"/vmunix"
61251920Seric #  endif
61351920Seric #endif
61451920Seric 
61514872Seric struct	nlist Nl[] =
61614872Seric {
61751920Seric 	{ LA_AVENRUN },
61814872Seric #define	X_AVENRUN	0
61914872Seric 	{ 0 },
62014872Seric };
62114872Seric 
62257736Seric #if defined(unixpc)
62357736Seric # define FSHIFT		5
62457736Seric #endif
62557736Seric 
62657977Seric #if defined(__alpha)
62757977Seric # define FSHIFT		10
62857977Seric #endif
62957977Seric 
63051920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
63151920Seric #  define FSHIFT	8
63257736Seric #endif
63357736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
63451920Seric #  define FSCALE	(1 << FSHIFT)
63551920Seric #endif
63640930Srick 
63714872Seric getla()
63814872Seric {
63914872Seric 	static int kmem = -1;
64051920Seric #if LA_TYPE == LA_INT
64124943Seric 	long avenrun[3];
64251920Seric #else
64351920Seric 	double avenrun[3];
64451920Seric #endif
64525615Seric 	extern off_t lseek();
64657736Seric 	extern char *errstring();
64757736Seric 	extern int errno;
64814872Seric 
64914872Seric 	if (kmem < 0)
65014872Seric 	{
65124945Seric 		kmem = open("/dev/kmem", 0, 0);
65214872Seric 		if (kmem < 0)
65357736Seric 		{
65457736Seric 			if (tTd(3, 1))
65557736Seric 				printf("getla: open(/dev/kmem): %s\n",
65657736Seric 					errstring(errno));
65714872Seric 			return (-1);
65857736Seric 		}
65951920Seric 		(void) fcntl(kmem, F_SETFD, 1);
66057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
66157736Seric 		{
66257736Seric 			if (tTd(3, 1))
66357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
66457736Seric 					errstring(errno));
66514872Seric 			return (-1);
66657736Seric 		}
66714872Seric 	}
66857736Seric 	if (tTd(3, 20))
66957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
67024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
67123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
67219967Seric 	{
67319967Seric 		/* thank you Ian */
67457736Seric 		if (tTd(3, 1))
67557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
67619967Seric 		return (-1);
67719967Seric 	}
67851920Seric #if LA_TYPE == LA_INT
67957736Seric 	if (tTd(3, 5))
68057736Seric 	{
68157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
68257736Seric 		if (tTd(3, 15))
68357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
68457736Seric 		printf("\n");
68557736Seric 	}
68657736Seric 	if (tTd(3, 1))
68757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
68824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
68951920Seric #else
69057736Seric 	if (tTd(3, 5))
69157736Seric 	{
69257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
69357736Seric 		if (tTd(3, 15))
69457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
69557736Seric 		printf("\n");
69657736Seric 	}
69757736Seric 	if (tTd(3, 1))
69857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
69951920Seric 	return ((int) (avenrun[0] + 0.5));
70051920Seric #endif
70114872Seric }
70214872Seric 
70351773Seric #else
70451920Seric #if LA_TYPE == LA_SUBR
70551773Seric 
70651773Seric getla()
70751773Seric {
70851920Seric 	double avenrun[3];
70951920Seric 
71051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
71157736Seric 	{
71257736Seric 		if (tTd(3, 1))
71357736Seric 			perror("getla: getloadavg failed:");
71451920Seric 		return (-1);
71557736Seric 	}
71657736Seric 	if (tTd(3, 1))
71757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
71851920Seric 	return ((int) (avenrun[0] + 0.5));
71951773Seric }
72051773Seric 
72151773Seric #else
72251773Seric 
72351773Seric getla()
72451773Seric {
72557736Seric 	if (tTd(3, 1))
72657736Seric 		printf("getla: ZERO\n");
72751920Seric 	return (0);
72851773Seric }
72951773Seric 
73051773Seric #endif
73151773Seric #endif
73224943Seric /*
73324943Seric **  SHOULDQUEUE -- should this message be queued or sent?
73424943Seric **
73524943Seric **	Compares the message cost to the load average to decide.
73624943Seric **
73724943Seric **	Parameters:
73824943Seric **		pri -- the priority of the message in question.
73957438Seric **		ctime -- the message creation time.
74024943Seric **
74124943Seric **	Returns:
74224943Seric **		TRUE -- if this message should be queued up for the
74324943Seric **			time being.
74424943Seric **		FALSE -- if the load is low enough to send this message.
74524943Seric **
74624943Seric **	Side Effects:
74724943Seric **		none.
74824943Seric */
74924943Seric 
75024943Seric bool
75157438Seric shouldqueue(pri, ctime)
75224943Seric 	long pri;
75357438Seric 	time_t ctime;
75424943Seric {
75551920Seric 	if (CurrentLA < QueueLA)
75624943Seric 		return (FALSE);
75758132Seric 	if (CurrentLA >= RefuseLA)
75858132Seric 		return (TRUE);
75951920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
76024943Seric }
76124943Seric /*
76253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
76353037Seric **
76453037Seric **	Parameters:
76553037Seric **		none.
76653037Seric **
76753037Seric **	Returns:
76853037Seric **		TRUE if incoming SMTP connections should be refused
76953037Seric **			(for now).
77053037Seric **		FALSE if we should accept new work.
77153037Seric **
77253037Seric **	Side Effects:
77353037Seric **		none.
77453037Seric */
77553037Seric 
77653037Seric bool
77753037Seric refuseconnections()
77853037Seric {
77953037Seric 	/* this is probably too simplistic */
78058132Seric 	return (CurrentLA >= RefuseLA);
78153037Seric }
78253037Seric /*
78324943Seric **  SETPROCTITLE -- set process title for ps
78424943Seric **
78524943Seric **	Parameters:
78658674Seric **		fmt -- a printf style format string.
78758674Seric **		a, b, c -- possible parameters to fmt.
78824943Seric **
78924943Seric **	Returns:
79024943Seric **		none.
79124943Seric **
79224943Seric **	Side Effects:
79324943Seric **		Clobbers argv of our main procedure so ps(1) will
79424943Seric **		display the title.
79524943Seric */
79624943Seric 
79758689Seric #ifdef SETPROCTITLE
79858689Seric # ifdef __hpux
79958689Seric #  include <sys/pstat.h>
80058689Seric # endif
80158689Seric #endif
80258689Seric 
80324943Seric /*VARARGS1*/
80457642Seric #ifdef __STDC__
80557642Seric setproctitle(char *fmt, ...)
80657642Seric #else
80757642Seric setproctitle(fmt, va_alist)
80824943Seric 	char *fmt;
80957642Seric 	va_dcl
81057642Seric #endif
81124943Seric {
81224943Seric # ifdef SETPROCTITLE
81324943Seric 	register char *p;
81425049Seric 	register int i;
81558674Seric 	char buf[MAXLINE];
81656852Seric 	VA_LOCAL_DECL
81758689Seric #  ifdef __hpux
81858689Seric 	union pstun pst;
81958689Seric #  endif
82024943Seric 	extern char **Argv;
82124943Seric 	extern char *LastArgv;
82224943Seric 
82358674Seric 	p = buf;
82424943Seric 
82558674Seric 	/* print sendmail: heading for grep */
82658674Seric 	(void) strcpy(p, "sendmail: ");
82758674Seric 	p += strlen(p);
82824943Seric 
82958674Seric 	/* print the argument string */
83058674Seric 	VA_START(fmt);
83158674Seric 	(void) vsprintf(p, fmt, ap);
83256852Seric 	VA_END;
83354996Seric 
83458674Seric 	i = strlen(buf);
83558689Seric 
83658689Seric #  ifdef __hpux
83758689Seric 	pst.pst_command = buf;
83858689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
83958689Seric #  else
84058689Seric 
84154996Seric 	if (i > LastArgv - Argv[0] - 2)
84225049Seric 	{
84354996Seric 		i = LastArgv - Argv[0] - 2;
84458674Seric 		buf[i] = '\0';
84525049Seric 	}
84658674Seric 	(void) strcpy(Argv[0], buf);
84754997Seric 	p = &Argv[0][i];
84824943Seric 	while (p < LastArgv)
84924943Seric 		*p++ = ' ';
85058689Seric #  endif
85156795Seric # endif /* SETPROCTITLE */
85224943Seric }
85325698Seric /*
85425698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
85525698Seric **
85625698Seric **	Parameters:
85725698Seric **		none.
85825698Seric **
85925698Seric **	Returns:
86025698Seric **		none.
86125698Seric **
86225698Seric **	Side Effects:
86325698Seric **		Picks up extant zombies.
86425698Seric */
86525698Seric 
86625698Seric # include <sys/wait.h>
86725698Seric 
86846928Sbostic void
86925698Seric reapchild()
87025698Seric {
87125698Seric # ifdef WNOHANG
87225698Seric 	union wait status;
87325698Seric 
87446928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
87525698Seric 		continue;
87656795Seric # else /* WNOHANG */
87725698Seric 	auto int status;
87825698Seric 
87946928Sbostic 	while (wait((int *)&status) > 0)
88025698Seric 		continue;
88156795Seric # endif /* WNOHANG */
88258061Seric # ifdef SYSTEM5
88358061Seric 	(void) signal(SIGCHLD, reapchild);
88458061Seric # endif
88525698Seric }
88655418Seric /*
88755418Seric **  UNSETENV -- remove a variable from the environment
88855418Seric **
88955418Seric **	Not needed on newer systems.
89055418Seric **
89155418Seric **	Parameters:
89255418Seric **		name -- the string name of the environment variable to be
89355418Seric **			deleted from the current environment.
89455418Seric **
89555418Seric **	Returns:
89655418Seric **		none.
89755418Seric **
89855418Seric **	Globals:
89955418Seric **		environ -- a pointer to the current environment.
90055418Seric **
90155418Seric **	Side Effects:
90255418Seric **		Modifies environ.
90355418Seric */
90455418Seric 
90555418Seric #ifdef UNSETENV
90655418Seric 
90755418Seric void
90855418Seric unsetenv(name)
90955418Seric 	char *name;
91055418Seric {
91155418Seric 	extern char **environ;
91255418Seric 	register char **pp;
91355418Seric 	int len = strlen(name);
91455418Seric 
91555418Seric 	for (pp = environ; *pp != NULL; pp++)
91655418Seric 	{
91755418Seric 		if (strncmp(name, *pp, len) == 0 &&
91855418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
91955418Seric 			break;
92055418Seric 	}
92155418Seric 
92255418Seric 	for (; *pp != NULL; pp++)
92355418Seric 		*pp = pp[1];
92455418Seric }
92555418Seric 
92655418Seric #endif /* UNSETENV */
92756215Seric /*
92856215Seric **  GETDTABLESIZE -- return number of file descriptors
92956215Seric **
93056215Seric **	Only on non-BSD systems
93156215Seric **
93256215Seric **	Parameters:
93356215Seric **		none
93456215Seric **
93556215Seric **	Returns:
93656215Seric **		size of file descriptor table
93756215Seric **
93856215Seric **	Side Effects:
93956215Seric **		none
94056215Seric */
94156215Seric 
94256215Seric #ifdef SYSTEM5
94356215Seric 
94456215Seric int
94556215Seric getdtablesize()
94656215Seric {
94758689Seric # ifdef _SC_OPEN_MAX
94858689Seric 	return sysconf(_SC_OPEN_MAX);
94958689Seric # else
95056215Seric 	return NOFILE;
95158689Seric # endif
95256215Seric }
95356215Seric 
95456215Seric #endif
95557631Seric /*
95657631Seric **  UNAME -- get the UUCP name of this system.
95757631Seric */
95857631Seric 
95957943Seric #ifndef HASUNAME
96057631Seric 
96157631Seric int
96257631Seric uname(name)
96357631Seric 	struct utsname *name;
96457631Seric {
96557631Seric 	FILE *file;
96657631Seric 	char *n;
96757631Seric 
96857631Seric 	name->nodename[0] = '\0';
96957631Seric 
97057661Seric 	/* try /etc/whoami -- one line with the node name */
97157631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
97257631Seric 	{
97357661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
97457631Seric 		(void) fclose(file);
97557661Seric 		n = strchr(name->nodename, '\n');
97657631Seric 		if (n != NULL)
97757631Seric 			*n = '\0';
97857631Seric 		if (name->nodename[0] != '\0')
97957631Seric 			return (0);
98057631Seric 	}
98157631Seric 
98257661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
98357631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
98457631Seric 	{
98557631Seric 		char buf[MAXLINE];
98657631Seric 
98757631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
98857631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
98957631Seric 					NODE_LENGTH, name->nodename) > 0)
99057631Seric 				break;
99157631Seric 		(void) fclose(file);
99257631Seric 		if (name->nodename[0] != '\0')
99357631Seric 			return (0);
99457631Seric 	}
99557631Seric 
99657631Seric #ifdef TRUST_POPEN
99757631Seric 	/*
99857631Seric 	**  Popen is known to have security holes.
99957631Seric 	*/
100057631Seric 
100157661Seric 	/* try uuname -l to return local name */
100257631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
100357631Seric 	{
100457661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
100557631Seric 		(void) pclose(file);
100657661Seric 		n = strchr(name, '\n');
100757631Seric 		if (n != NULL)
100857631Seric 			*n = '\0';
100957661Seric 		if (name->nodename[0] != '\0')
101057631Seric 			return (0);
101157631Seric 	}
101257631Seric #endif
101357631Seric 
101457631Seric 	return (-1);
101557631Seric }
101657943Seric #endif /* HASUNAME */
101758068Seric /*
101858068Seric **  INITGROUPS -- initialize groups
101958068Seric **
102058068Seric **	Stub implementation for System V style systems
102158068Seric */
102258068Seric 
102358068Seric #ifndef HASINITGROUPS
102458068Seric # if !defined(SYSTEM5) || defined(hpux)
102558068Seric #  define HASINITGROUPS
102658068Seric # endif
102758068Seric #endif
102858068Seric 
102958068Seric #ifndef HASINITGROUPS
103058068Seric 
103158068Seric initgroups(name, basegid)
103258068Seric 	char *name;
103358068Seric 	int basegid;
103458068Seric {
103558068Seric 	return 0;
103658068Seric }
103758068Seric 
103858068Seric #endif
103958082Seric /*
104058082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
104158082Seric **
104258082Seric **	Only implemented if you have statfs.
104358082Seric **
104458082Seric **	Parameters:
104558333Seric **		msize -- the size to check against.  If zero, we don't yet
104658333Seric **			know how big the message will be, so just check for
104758333Seric **			a "reasonable" amount.
104858082Seric **
104958082Seric **	Returns:
105058082Seric **		TRUE if there is enough space.
105158082Seric **		FALSE otherwise.
105258082Seric */
105358082Seric 
105458082Seric #ifndef HASSTATFS
105558082Seric # if defined(BSD4_4) || defined(__osf__)
105658082Seric #  define HASSTATFS
105758082Seric # endif
105858082Seric #endif
105958082Seric 
106058082Seric #ifdef HASSTATFS
106158157Seric # undef HASUSTAT
106258157Seric #endif
106358157Seric 
106458157Seric #if defined(HASUSTAT)
106558157Seric # include <sys/stat.h>
106658157Seric # include <ustat.h>
106758157Seric #endif
106858157Seric 
106958157Seric #ifdef HASSTATFS
107058133Seric # if defined(sgi) || defined(apollo)
107158133Seric #  include <sys/statfs.h>
107258133Seric # else
107358133Seric #  if defined(sun) || defined(hpux)
107458133Seric #   include <sys/vfs.h>
107558133Seric #  else
107658157Seric #   include <sys/mount.h>
107758133Seric #  endif
107858133Seric # endif
107958082Seric #endif
108058082Seric 
108158082Seric bool
108258333Seric enoughspace(msize)
108358333Seric 	long msize;
108458082Seric {
108558160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
108658157Seric # if defined(HASUSTAT)
108758153Seric 	struct ustat fs;
108858153Seric 	struct stat statbuf;
108958366Seric #  define FSBLOCKSIZE	DEV_BSIZE
109058157Seric #  define f_bavail	f_tfree
109158157Seric # else
109258157Seric #  if defined(ultrix)
109358157Seric 	struct fs_data fs;
109458157Seric #   define f_bavail	fd_bfreen
109558366Seric #   define FSBLOCKSIZE	fs.fd_bsize
109658153Seric #  else
109758082Seric 	struct statfs fs;
109858366Seric #   define FSBLOCKSIZE	fs.f_bsize
109958153Seric #  endif
110058133Seric # endif
110158333Seric 	long blocksneeded;
110258082Seric 	extern int errno;
110358082Seric 	extern char *errstring();
110458082Seric 
110558333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
110658082Seric 	{
110758082Seric 		if (tTd(4, 80))
110858133Seric 			printf("enoughspace: no threshold\n");
110958133Seric 		return TRUE;
111058133Seric 	}
111158333Seric 
111258157Seric # if defined(HASUSTAT)
111358157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
111458157Seric # else
111558157Seric #  if defined(sgi) || defined(apollo)
111658133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
111758157Seric #  else
111858157Seric #   if defined(ultrix)
111958133Seric 	if (statfs(QueueDir, &fs) > 0)
112058153Seric #   else
112158133Seric 	if (statfs(QueueDir, &fs) == 0)
112258153Seric #   endif
112358133Seric #  endif
112458133Seric # endif
112558133Seric 	{
112658133Seric 		if (tTd(4, 80))
112758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
112858333Seric 				fs.f_bavail, msize);
112958333Seric 
113058333Seric 		/* convert msize to block count */
113158366Seric 		msize = msize / FSBLOCKSIZE + 1;
113258333Seric 		if (MinBlocksFree >= 0)
113358333Seric 			msize += MinBlocksFree;
113458333Seric 
113558333Seric 		if (fs.f_bavail < msize)
113658090Seric 		{
113758090Seric #ifdef LOG
113858090Seric 			if (LogLevel > 0)
113958090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
114058333Seric 					QueueDir, fs.f_bavail, msize);
114158090Seric #endif
114258082Seric 			return FALSE;
114358090Seric 		}
114458082Seric 	}
114558082Seric 	else if (tTd(4, 80))
114658333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
114758333Seric 			MinBlocksFree, msize, errstring(errno));
114858082Seric #endif
114958082Seric 	return TRUE;
115058082Seric }
115158542Seric /*
115258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
115358542Seric **
115458542Seric **	This looks at an errno value and tells if this is likely to
115558542Seric **	go away if retried later.
115658542Seric **
115758542Seric **	Parameters:
115858542Seric **		err -- the errno code to classify.
115958542Seric **
116058542Seric **	Returns:
116158542Seric **		TRUE if this is probably transient.
116258542Seric **		FALSE otherwise.
116358542Seric */
116458542Seric 
116558542Seric bool
116658542Seric transienterror(err)
116758542Seric 	int err;
116858542Seric {
116958542Seric 	switch (err)
117058542Seric 	{
117158542Seric 	  case EIO:			/* I/O error */
117258542Seric 	  case ENXIO:			/* Device not configured */
117358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
117458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
117558542Seric 	  case ENODEV:			/* Operation not supported by device */
117658542Seric 	  case ENFILE:			/* Too many open files in system */
117758542Seric 	  case EMFILE:			/* Too many open files */
117858542Seric 	  case ENOSPC:			/* No space left on device */
117958542Seric #ifdef ETIMEDOUT
118058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
118158542Seric #endif
118258542Seric #ifdef ESTALE
118358542Seric 	  case ESTALE:			/* Stale NFS file handle */
118458542Seric #endif
118558542Seric #ifdef ENETDOWN
118658542Seric 	  case ENETDOWN:		/* Network is down */
118758542Seric #endif
118858542Seric #ifdef ENETUNREACH
118958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
119058542Seric #endif
119158542Seric #ifdef ENETRESET
119258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
119358542Seric #endif
119458542Seric #ifdef ECONNABORTED
119558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
119658542Seric #endif
119758542Seric #ifdef ECONNRESET
119858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
119958542Seric #endif
120058542Seric #ifdef ENOBUFS
120158542Seric 	  case ENOBUFS:			/* No buffer space available */
120258542Seric #endif
120358542Seric #ifdef ESHUTDOWN
120458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
120558542Seric #endif
120658542Seric #ifdef ECONNREFUSED
120758542Seric 	  case ECONNREFUSED:		/* Connection refused */
120858542Seric #endif
120958542Seric #ifdef EHOSTDOWN
121058542Seric 	  case EHOSTDOWN:		/* Host is down */
121158542Seric #endif
121258542Seric #ifdef EHOSTUNREACH
121358542Seric 	  case EHOSTUNREACH:		/* No route to host */
121458542Seric #endif
121558542Seric #ifdef EDQUOT
121658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
121758542Seric #endif
121858542Seric #ifdef EPROCLIM
121958542Seric 	  case EPROCLIM:		/* Too many processes */
122058542Seric #endif
122158542Seric #ifdef EUSERS
122258542Seric 	  case EUSERS:			/* Too many users */
122358542Seric #endif
122458542Seric #ifdef EDEADLK
122558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
122658542Seric #endif
122758542Seric #ifdef EISCONN
122858542Seric 	  case EISCONN:			/* Socket already connected */
122958542Seric #endif
123058542Seric #ifdef EINPROGRESS
123158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
123258542Seric #endif
123358542Seric #ifdef EALREADY
123458542Seric 	  case EALREADY:		/* Operation already in progress */
123558542Seric #endif
123658542Seric #ifdef EADDRINUSE
123758542Seric 	  case EADDRINUSE:		/* Address already in use */
123858542Seric #endif
123958542Seric #ifdef EADDRNOTAVAIL
124058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
124158542Seric #endif
124258542Seric #ifdef ENOSR
124358542Seric 	  case ENOSR:			/* Out of streams resources */
124458542Seric #endif
124558542Seric 		return TRUE;
124658542Seric 	}
124758542Seric 
124858542Seric 	/* nope, must be permanent */
124958542Seric 	return FALSE;
125058542Seric }
125158689Seric /*
125258689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
125358689Seric **
125458689Seric **	Parameters:
125558689Seric **		fd -- the file descriptor of the file.
125658689Seric **		filename -- the file name (for error messages).
125758689Seric **		type -- type of the lock.  Bits can be:
125858689Seric **			LOCK_EX -- exclusive lock.
125958689Seric **			LOCK_NB -- non-blocking.
126058689Seric **
126158689Seric **	Returns:
126258689Seric **		TRUE if the lock was acquired.
126358689Seric **		FALSE otherwise.
126458689Seric */
126558689Seric 
126658689Seric bool
126758689Seric lockfile(fd, filename, type)
126858689Seric 	int fd;
126958689Seric 	char *filename;
127058689Seric 	int type;
127158689Seric {
127258689Seric # ifdef LOCKF
127358689Seric 	int action;
127458689Seric 	struct flock lfd;
127558689Seric 
127658701Seric 	if (bitset(LOCK_EX, type))
127758689Seric 		lfd.l_type = F_WRLCK;
127858689Seric 	else
127958689Seric 		lfd.l_type = F_RDLCK;
128058689Seric 
128158689Seric 	if (bitset(LOCK_NB, type))
128258689Seric 		action = F_SETLK;
128358689Seric 	else
128458689Seric 		action = F_SETLKW;
128558689Seric 
128658689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
128758689Seric 
128858689Seric 	if (fcntl(fd, action, &lfd) >= 0)
128958689Seric 		return TRUE;
129058689Seric 
129158689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
129258689Seric 		syserr("cannot lockf(%s)", filename);
129358689Seric # else
129458689Seric 	if (flock(fd, type) >= 0)
129558689Seric 		return TRUE;
129658689Seric 
129758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
129858689Seric 		syserr("cannot flock(%s)", filename);
129958689Seric # endif
130058689Seric 	return FALSE;
130158689Seric }
1302