xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64314)
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*64314Seric static char sccsid[] = "@(#)conf.c	8.23 (Berkeley) 08/21/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1736928Sbostic # include <pwd.h>
18404Seric 
19294Seric /*
203309Seric **  CONF.C -- Sendmail Configuration Tables.
21294Seric **
22294Seric **	Defines the configuration of this installation.
23294Seric **
241388Seric **	Configuration Variables:
252897Seric **		HdrInfo -- a table describing well-known header fields.
262897Seric **			Each entry has the field name and some flags,
274147Seric **			which are described in sendmail.h.
284093Seric **
294093Seric **	Notes:
304093Seric **		I have tried to put almost all the reasonable
314093Seric **		configuration information into the configuration
324093Seric **		file read at runtime.  My intent is that anything
334093Seric **		here is a function of the version of UNIX you
344093Seric **		are running, or is really static -- for example
354093Seric **		the headers are a superset of widely used
364093Seric **		protocols.  If you find yourself playing with
374093Seric **		this file too much, you may be making a mistake!
38294Seric */
39294Seric 
40294Seric 
41294Seric 
42294Seric 
434437Seric /*
442897Seric **  Header info table
453057Seric **	Final (null) entry contains the flags used for any other field.
464147Seric **
474147Seric **	Not all of these are actually handled specially by sendmail
484147Seric **	at this time.  They are included as placeholders, to let
494147Seric **	you know that "someday" I intend to have sendmail do
504147Seric **	something with them.
512897Seric */
522897Seric 
532897Seric struct hdrinfo	HdrInfo[] =
542897Seric {
558060Seric 		/* originator fields, most to least significant  */
5611417Seric 	"resent-sender",	H_FROM|H_RESENT,
5711417Seric 	"resent-from",		H_FROM|H_RESENT,
5825686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
599055Seric 	"sender",		H_FROM,
609055Seric 	"from",			H_FROM,
6125686Seric 	"reply-to",		H_FROM,
629055Seric 	"full-name",		H_ACHECK,
6357359Seric 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
6457359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6558796Seric 
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,
7458796Seric 
758060Seric 		/* message identification and control */
7611417Seric 	"message-id",		0,
7711417Seric 	"resent-message-id",	H_RESENT,
789055Seric 	"message",		H_EOH,
799055Seric 	"text",			H_EOH,
8058796Seric 
8111417Seric 		/* date fields */
8211417Seric 	"date",			0,
8311417Seric 	"resent-date",		H_RESENT,
8458796Seric 
858060Seric 		/* trace fields */
869055Seric 	"received",		H_TRACE|H_FORCE,
8759318Seric 	"x400-received",	H_TRACE|H_FORCE,
889055Seric 	"via",			H_TRACE|H_FORCE,
899055Seric 	"mail-from",		H_TRACE|H_FORCE,
908060Seric 
9158796Seric 		/* miscellaneous fields */
9258796Seric 	"comments",		H_FORCE,
9363753Seric 	"return-path",		H_FORCE|H_ACHECK,
9458796Seric 
959055Seric 	NULL,			0,
962897Seric };
974166Seric 
984166Seric 
994166Seric 
1004282Seric /*
1014282Seric **  Location of system files/databases/etc.
1024282Seric */
1034282Seric 
10440980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
10558082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1069039Seric 
1079064Seric 
1089064Seric 
1099039Seric /*
11058082Seric **  Privacy values
11158082Seric */
11258082Seric 
11358082Seric struct prival PrivacyValues[] =
11458082Seric {
11558082Seric 	"public",		PRIV_PUBLIC,
11658082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11758114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11858082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11958082Seric 	"noexpn",		PRIV_NOEXPN,
12058082Seric 	"novrfy",		PRIV_NOVRFY,
12158249Seric 	"restrictmailq",	PRIV_RESTRMAILQ,
12258789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12358082Seric 	"goaway",		PRIV_GOAWAY,
12458789Seric 	NULL,			0,
12558082Seric };
12658082Seric 
12758082Seric 
12858082Seric 
12958082Seric /*
13024943Seric **  Miscellaneous stuff.
1319039Seric */
1329039Seric 
13324943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13424943Seric /*
13524943Seric **  SETDEFAULTS -- set default values
13624943Seric **
13724943Seric **	Because of the way freezing is done, these must be initialized
13824943Seric **	using direct code.
13924943Seric **
14024943Seric **	Parameters:
14158734Seric **		e -- the default envelope.
14224943Seric **
14324943Seric **	Returns:
14424943Seric **		none.
14524943Seric **
14624943Seric **	Side Effects:
14724943Seric **		Initializes a bunch of global variables to their
14824943Seric **		default values.
14924943Seric */
15024943Seric 
15158737Seric #define DAYS		* 24 * 60 * 60
15258737Seric 
15358734Seric setdefaults(e)
15458734Seric 	register ENVELOPE *e;
15524943Seric {
15657438Seric 	SpaceSub = ' ';				/* option B */
15757438Seric 	QueueLA = 8;				/* option x */
15857438Seric 	RefuseLA = 12;				/* option X */
15957438Seric 	WkRecipFact = 30000L;			/* option y */
16057438Seric 	WkClassFact = 1800L;			/* option z */
16157438Seric 	WkTimeFact = 90000L;			/* option Z */
16257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
16363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
16457438Seric 						/* option F */
16557438Seric 	DefUid = 1;				/* option u */
16657438Seric 	DefGid = 1;				/* option g */
16757438Seric 	CheckpointInterval = 10;		/* option C */
16857438Seric 	MaxHopCount = 25;			/* option h */
16958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
17058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
17159709Seric 	SevenBit = FALSE;			/* option 7 */
17257438Seric 	MaxMciCache = 1;			/* option k */
17357438Seric 	MciCacheTimeout = 300;			/* option K */
17457438Seric 	LogLevel = 9;				/* option L */
17558112Seric 	settimeouts(NULL);			/* option r */
17658737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
17758737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
17858853Seric 	PrivacyFlags = 0;			/* option p */
17940973Sbostic 	setdefuser();
18053654Seric 	setupmaps();
18157402Seric 	setupmailers();
18224943Seric }
183294Seric 
18440973Sbostic 
1854326Seric /*
18640973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
18740973Sbostic */
18840973Sbostic 
18940973Sbostic setdefuser()
19040973Sbostic {
19140973Sbostic 	struct passwd *defpwent;
19257386Seric 	static char defuserbuf[40];
19340973Sbostic 
19457386Seric 	DefUser = defuserbuf;
19540973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
19657386Seric 		strcpy(defuserbuf, defpwent->pw_name);
19740973Sbostic 	else
19857386Seric 		strcpy(defuserbuf, "nobody");
19940973Sbostic }
20053654Seric /*
20156836Seric **  HOST_MAP_INIT -- initialize host class structures
20256836Seric */
20356836Seric 
20456836Seric bool
20560219Seric host_map_init(map, args)
20656836Seric 	MAP *map;
20756836Seric 	char *args;
20856836Seric {
20956836Seric 	register char *p = args;
21056836Seric 
21156836Seric 	for (;;)
21256836Seric 	{
21358050Seric 		while (isascii(*p) && isspace(*p))
21456836Seric 			p++;
21556836Seric 		if (*p != '-')
21656836Seric 			break;
21756836Seric 		switch (*++p)
21856836Seric 		{
21956836Seric 		  case 'a':
22056836Seric 			map->map_app = ++p;
22156836Seric 			break;
22256836Seric 		}
22358050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
22456836Seric 			p++;
22556836Seric 		if (*p != '\0')
22656836Seric 			*p++ = '\0';
22756836Seric 	}
22856836Seric 	if (map->map_app != NULL)
22956836Seric 		map->map_app = newstr(map->map_app);
23056836Seric 	return TRUE;
23156836Seric }
23257402Seric /*
23357402Seric **  SETUPMAILERS -- initialize default mailers
23457402Seric */
23556836Seric 
23657402Seric setupmailers()
23757402Seric {
23857402Seric 	char buf[100];
23957402Seric 
24057403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
24157403Seric 	makemailer(buf);
24257403Seric 
24359883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
24457402Seric 	makemailer(buf);
24557402Seric 
24657402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
24757402Seric 	makemailer(buf);
24857402Seric }
24956836Seric /*
25060207Seric **  SETUPMAPS -- set up map classes
25160207Seric */
25260207Seric 
25360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
25460207Seric 	{ \
25560207Seric 		extern bool parse __P((MAP *, char *)); \
25660207Seric 		extern bool open __P((MAP *, int)); \
25760207Seric 		extern void close __P((MAP *)); \
25860207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
25960207Seric 		extern void store __P((MAP *, char *, char *)); \
26060207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
26160207Seric 		s->s_mapclass.map_cname = name; \
26260207Seric 		s->s_mapclass.map_ext = ext; \
26360207Seric 		s->s_mapclass.map_cflags = flags; \
26460207Seric 		s->s_mapclass.map_parse = parse; \
26560207Seric 		s->s_mapclass.map_open = open; \
26660207Seric 		s->s_mapclass.map_close = close; \
26760207Seric 		s->s_mapclass.map_lookup = lookup; \
26860207Seric 		s->s_mapclass.map_store = store; \
26960207Seric 	}
27060207Seric 
27160207Seric setupmaps()
27260207Seric {
27360207Seric 	register STAB *s;
27460207Seric 
27560207Seric #ifdef NEWDB
27660207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
27760207Seric 		map_parseargs, hash_map_open, db_map_close,
27860207Seric 		db_map_lookup, db_map_store);
27960207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
28060207Seric 		map_parseargs, bt_map_open, db_map_close,
28160207Seric 		db_map_lookup, db_map_store);
28260207Seric #endif
28360207Seric 
28460207Seric #ifdef NDBM
28560207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
28660207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
28760207Seric 		ndbm_map_lookup, ndbm_map_store);
28860207Seric #endif
28960207Seric 
29060207Seric #ifdef NIS
29160207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
29260207Seric 		map_parseargs, nis_map_open, nis_map_close,
29360207Seric 		nis_map_lookup, nis_map_store);
29460207Seric #endif
29560207Seric 
29660207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
29760207Seric 		map_parseargs, stab_map_open, stab_map_close,
29860207Seric 		stab_map_lookup, stab_map_store);
29960207Seric 
30060207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
30160207Seric 		map_parseargs, impl_map_open, impl_map_close,
30260207Seric 		impl_map_lookup, impl_map_store);
30360207Seric 
30460207Seric 	/* host DNS lookup */
30560207Seric 	MAPDEF("host", NULL, 0,
30660207Seric 		host_map_init, null_map_open, null_map_close,
30760207Seric 		host_map_lookup, null_map_store);
30860207Seric 
30960207Seric 	/* dequote map */
31060207Seric 	MAPDEF("dequote", NULL, 0,
31160207Seric 		dequote_init, null_map_open, null_map_close,
31260207Seric 		dequote_map, null_map_store);
31360207Seric 
31460207Seric #if 0
31560207Seric # ifdef USERDB
31660207Seric 	/* user database */
31760207Seric 	MAPDEF("udb", ".db", 0,
31860207Seric 		udb_map_parse, null_map_open, null_map_close,
31960207Seric 		udb_map_lookup, null_map_store);
32060207Seric # endif
32160207Seric #endif
32260207Seric }
32360207Seric 
32460207Seric #undef MAPDEF
32560207Seric /*
3269369Seric **  USERNAME -- return the user id of the logged in user.
3279369Seric **
3289369Seric **	Parameters:
3299369Seric **		none.
3309369Seric **
3319369Seric **	Returns:
3329369Seric **		The login name of the logged in user.
3339369Seric **
3349369Seric **	Side Effects:
3359369Seric **		none.
3369369Seric **
3379369Seric **	Notes:
3389369Seric **		The return value is statically allocated.
3399369Seric */
3409369Seric 
3419369Seric char *
3429369Seric username()
3439369Seric {
34417469Seric 	static char *myname = NULL;
3459369Seric 	extern char *getlogin();
34619904Smiriam 	register struct passwd *pw;
3479369Seric 
34817469Seric 	/* cache the result */
34917469Seric 	if (myname == NULL)
35017469Seric 	{
35117469Seric 		myname = getlogin();
35217469Seric 		if (myname == NULL || myname[0] == '\0')
35317469Seric 		{
35463787Seric 			pw = getpwuid(RealUid);
35517469Seric 			if (pw != NULL)
35640993Sbostic 				myname = newstr(pw->pw_name);
35717469Seric 		}
35819904Smiriam 		else
35919904Smiriam 		{
36063787Seric 			uid_t uid = RealUid;
36119873Smiriam 
36240993Sbostic 			myname = newstr(myname);
36340993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
36458736Seric 			      (uid != 0 && uid != pw->pw_uid))
36519904Smiriam 			{
36658736Seric 				pw = getpwuid(uid);
36724945Seric 				if (pw != NULL)
36840993Sbostic 					myname = newstr(pw->pw_name);
36919873Smiriam 			}
37019873Smiriam 		}
37117469Seric 		if (myname == NULL || myname[0] == '\0')
37217469Seric 		{
37358151Seric 			syserr("554 Who are you?");
37417469Seric 			myname = "postmaster";
37517469Seric 		}
37617469Seric 	}
37717469Seric 
37817469Seric 	return (myname);
3799369Seric }
3809369Seric /*
3814190Seric **  TTYPATH -- Get the path of the user's tty
382294Seric **
383294Seric **	Returns the pathname of the user's tty.  Returns NULL if
384294Seric **	the user is not logged in or if s/he has write permission
385294Seric **	denied.
386294Seric **
387294Seric **	Parameters:
388294Seric **		none
389294Seric **
390294Seric **	Returns:
391294Seric **		pathname of the user's tty.
392294Seric **		NULL if not logged in or write permission denied.
393294Seric **
394294Seric **	Side Effects:
395294Seric **		none.
396294Seric **
397294Seric **	WARNING:
398294Seric **		Return value is in a local buffer.
399294Seric **
400294Seric **	Called By:
401294Seric **		savemail
402294Seric */
403294Seric 
404294Seric char *
405294Seric ttypath()
406294Seric {
407294Seric 	struct stat stbuf;
408294Seric 	register char *pathn;
409294Seric 	extern char *ttyname();
4104081Seric 	extern char *getlogin();
411294Seric 
412294Seric 	/* compute the pathname of the controlling tty */
4139369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4149369Seric 	    (pathn = ttyname(0)) == NULL)
415294Seric 	{
416294Seric 		errno = 0;
417294Seric 		return (NULL);
418294Seric 	}
419294Seric 
420294Seric 	/* see if we have write permission */
4212967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
422294Seric 	{
423294Seric 		errno = 0;
424294Seric 		return (NULL);
425294Seric 	}
426294Seric 
427294Seric 	/* see if the user is logged in */
428294Seric 	if (getlogin() == NULL)
429294Seric 		return (NULL);
430294Seric 
431294Seric 	/* looks good */
432294Seric 	return (pathn);
433294Seric }
4342967Seric /*
4352967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4362967Seric **
4372967Seric **	This routine can be supplied on a per-installation basis
4382967Seric **	to determine whether a person is allowed to send a message.
4392967Seric **	This allows restriction of certain types of internet
4402967Seric **	forwarding or registration of users.
4412967Seric **
4422967Seric **	If the hosts are found to be incompatible, an error
44357454Seric **	message should be given using "usrerr" and 0 should
4442967Seric **	be returned.
4452967Seric **
4464288Seric **	'NoReturn' can be set to suppress the return-to-sender
4474288Seric **	function; this should be done on huge messages.
4484288Seric **
4492967Seric **	Parameters:
4502967Seric **		to -- the person being sent to.
4512967Seric **
4522967Seric **	Returns:
45357459Seric **		an exit status
4542967Seric **
4552967Seric **	Side Effects:
4562967Seric **		none (unless you include the usrerr stuff)
4572967Seric */
4582967Seric 
45955012Seric checkcompat(to, e)
4602967Seric 	register ADDRESS *to;
46155012Seric 	register ENVELOPE *e;
4622967Seric {
46312133Seric # ifdef lint
46412133Seric 	if (to == NULL)
46512133Seric 		to++;
46663834Seric # endif /* lint */
46710698Seric # ifdef EXAMPLE_CODE
46810698Seric 	/* this code is intended as an example only */
4694437Seric 	register STAB *s;
4704437Seric 
4714437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
47255012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4739369Seric 	    to->q_mailer == s->s_mailer)
4744437Seric 	{
47558151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
47610698Seric 		/* NoReturn = TRUE; to supress return copy */
47757459Seric 		return (EX_UNAVAILABLE);
4784437Seric 	}
47956795Seric # endif /* EXAMPLE_CODE */
48057459Seric 	return (EX_OK);
4812967Seric }
4829369Seric /*
48364035Seric **  SETSIGNAL -- set a signal handler
48464035Seric **
48564035Seric **	This is essentially old BSD "signal(3)".
48664035Seric */
48764035Seric 
48864035Seric setsig_t
48964035Seric setsignal(sig, handler)
49064035Seric 	int sig;
49164035Seric 	setsig_t handler;
49264035Seric {
49364072Seric #if defined(SYS5SIGNALS) || defined(BSD4_3)
49464035Seric 	return signal(sig, handler);
49564035Seric #else
49664035Seric 	struct sigaction n, o;
49764035Seric 
49864035Seric 	bzero(&n, sizeof n);
49964035Seric 	n.sa_handler = handler;
50064035Seric 	if (sigaction(sig, &n, &o) < 0)
50164035Seric 		return SIG_ERR;
50264035Seric 	return o.sa_handler;
50364035Seric #endif
50464035Seric }
50564035Seric /*
5069369Seric **  HOLDSIGS -- arrange to hold all signals
5079369Seric **
5089369Seric **	Parameters:
5099369Seric **		none.
5109369Seric **
5119369Seric **	Returns:
5129369Seric **		none.
5139369Seric **
5149369Seric **	Side Effects:
5159369Seric **		Arranges that signals are held.
5169369Seric */
5179369Seric 
5189369Seric holdsigs()
5199369Seric {
5209369Seric }
5219369Seric /*
5229369Seric **  RLSESIGS -- arrange to release all signals
5239369Seric **
5249369Seric **	This undoes the effect of holdsigs.
5259369Seric **
5269369Seric **	Parameters:
5279369Seric **		none.
5289369Seric **
5299369Seric **	Returns:
5309369Seric **		none.
5319369Seric **
5329369Seric **	Side Effects:
5339369Seric **		Arranges that signals are released.
5349369Seric */
5359369Seric 
5369369Seric rlsesigs()
5379369Seric {
5389369Seric }
53914872Seric /*
54014872Seric **  GETLA -- get the current load average
54114872Seric **
54214881Seric **	This code stolen from la.c.
54314881Seric **
54414872Seric **	Parameters:
54514872Seric **		none.
54614872Seric **
54714872Seric **	Returns:
54814872Seric **		The current load average as an integer.
54914872Seric **
55014872Seric **	Side Effects:
55114872Seric **		none.
55214872Seric */
55314872Seric 
55451920Seric /* try to guess what style of load average we have */
55551920Seric #define LA_ZERO		1	/* always return load average as zero */
55651920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
55751920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
55851920Seric #define LA_SUBR		4	/* call getloadavg */
55964295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
56014872Seric 
56163962Seric /* do guesses based on general OS type */
56251920Seric #ifndef LA_TYPE
56363962Seric # define LA_TYPE	LA_ZERO
56451920Seric #endif
56551920Seric 
56651920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
56751920Seric 
56814872Seric #include <nlist.h>
56914872Seric 
57051920Seric #ifndef LA_AVENRUN
57163962Seric # ifdef SYSTEM5
57263962Seric #  define LA_AVENRUN	"avenrun"
57363962Seric # else
57463962Seric #  define LA_AVENRUN	"_avenrun"
57563962Seric # endif
57651920Seric #endif
57751920Seric 
57851920Seric /* _PATH_UNIX should be defined in <paths.h> */
57951920Seric #ifndef _PATH_UNIX
58063962Seric # if defined(SYSTEM5)
58163962Seric #  define _PATH_UNIX	"/unix"
58263962Seric # else
58363962Seric #  define _PATH_UNIX	"/vmunix"
58463962Seric # endif
58551920Seric #endif
58651920Seric 
58714872Seric struct	nlist Nl[] =
58814872Seric {
58951920Seric 	{ LA_AVENRUN },
59014872Seric #define	X_AVENRUN	0
59114872Seric 	{ 0 },
59214872Seric };
59314872Seric 
59460583Seric #ifndef FSHIFT
59560583Seric # if defined(unixpc)
59660583Seric #  define FSHIFT	5
59760583Seric # endif
59857736Seric 
59960583Seric # if defined(__alpha)
60060583Seric #  define FSHIFT	10
60160583Seric # endif
60257977Seric 
60360583Seric # if (LA_TYPE == LA_INT)
60451920Seric #  define FSHIFT	8
60560583Seric # endif
60657736Seric #endif
60760583Seric 
60857736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
60951920Seric #  define FSCALE	(1 << FSHIFT)
61051920Seric #endif
61140930Srick 
61214872Seric getla()
61314872Seric {
61414872Seric 	static int kmem = -1;
61551920Seric #if LA_TYPE == LA_INT
61624943Seric 	long avenrun[3];
61751920Seric #else
61851920Seric 	double avenrun[3];
61951920Seric #endif
62025615Seric 	extern off_t lseek();
62157736Seric 	extern int errno;
62214872Seric 
62314872Seric 	if (kmem < 0)
62414872Seric 	{
62524945Seric 		kmem = open("/dev/kmem", 0, 0);
62614872Seric 		if (kmem < 0)
62757736Seric 		{
62857736Seric 			if (tTd(3, 1))
62957736Seric 				printf("getla: open(/dev/kmem): %s\n",
63057736Seric 					errstring(errno));
63114872Seric 			return (-1);
63257736Seric 		}
63351920Seric 		(void) fcntl(kmem, F_SETFD, 1);
63457736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
63557736Seric 		{
63657736Seric 			if (tTd(3, 1))
63757736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
63857736Seric 					errstring(errno));
63914872Seric 			return (-1);
64057736Seric 		}
64159253Seric 		if (Nl[X_AVENRUN].n_value == 0)
64259253Seric 		{
64359253Seric 			if (tTd(3, 1))
64459253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
64559253Seric 					_PATH_UNIX, LA_AVENRUN);
64659253Seric 			return (-1);
64759253Seric 		}
64814872Seric 	}
64957736Seric 	if (tTd(3, 20))
65057736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
65124945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
65223118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
65319967Seric 	{
65419967Seric 		/* thank you Ian */
65557736Seric 		if (tTd(3, 1))
65657736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
65719967Seric 		return (-1);
65819967Seric 	}
65951920Seric #if LA_TYPE == LA_INT
66057736Seric 	if (tTd(3, 5))
66157736Seric 	{
66257736Seric 		printf("getla: avenrun = %d", avenrun[0]);
66357736Seric 		if (tTd(3, 15))
66457736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
66557736Seric 		printf("\n");
66657736Seric 	}
66757736Seric 	if (tTd(3, 1))
66857736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
66924943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
67051920Seric #else
67157736Seric 	if (tTd(3, 5))
67257736Seric 	{
67357736Seric 		printf("getla: avenrun = %g", avenrun[0]);
67457736Seric 		if (tTd(3, 15))
67557736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
67657736Seric 		printf("\n");
67757736Seric 	}
67857736Seric 	if (tTd(3, 1))
67957736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
68051920Seric 	return ((int) (avenrun[0] + 0.5));
68151920Seric #endif
68214872Seric }
68314872Seric 
68451773Seric #else
68551920Seric #if LA_TYPE == LA_SUBR
68651773Seric 
68751773Seric getla()
68851773Seric {
68951920Seric 	double avenrun[3];
69051920Seric 
69151920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
69257736Seric 	{
69357736Seric 		if (tTd(3, 1))
69457736Seric 			perror("getla: getloadavg failed:");
69551920Seric 		return (-1);
69657736Seric 	}
69757736Seric 	if (tTd(3, 1))
69857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
69951920Seric 	return ((int) (avenrun[0] + 0.5));
70051773Seric }
70151773Seric 
70251773Seric #else
70364295Seric #if LA_TYPE == LA_MACH
70451773Seric 
70564295Seric /*
70664295Seric **  This has been tested on NeXT release 2.1.
70764295Seric */
70864295Seric 
70964295Seric #include <mach.h>
71064295Seric 
71151773Seric getla()
71251773Seric {
71364295Seric 	processor_set_t default_set;
71464295Seric 	kern_return_t error;
71564295Seric 	unsigned int info_count;
71664295Seric 	struct processor_set_basic_info info;
71764295Seric 	host_t host;
71864295Seric 
71964295Seric 	error = processor_set_default(host_self(), &default_set);
72064295Seric 	if (error != KERN_SUCCESS)
72164295Seric 		return -1;
72264295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
72364295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
72464295Seric 			       &host, (processor_set_info_t)&info,
72564295Seric 			       &info_count) != KERN_SUCCESS)
72664295Seric 	{
72764295Seric 		return -1;
72864295Seric 	}
72964295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
73064295Seric }
73164295Seric 
73264295Seric 
73364295Seric #else
73464295Seric 
73564295Seric getla()
73664295Seric {
73757736Seric 	if (tTd(3, 1))
73857736Seric 		printf("getla: ZERO\n");
73951920Seric 	return (0);
74051773Seric }
74151773Seric 
74251773Seric #endif
74351773Seric #endif
74464295Seric #endif
74524943Seric /*
74624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
74724943Seric **
74824943Seric **	Compares the message cost to the load average to decide.
74924943Seric **
75024943Seric **	Parameters:
75124943Seric **		pri -- the priority of the message in question.
75257438Seric **		ctime -- the message creation time.
75324943Seric **
75424943Seric **	Returns:
75524943Seric **		TRUE -- if this message should be queued up for the
75624943Seric **			time being.
75724943Seric **		FALSE -- if the load is low enough to send this message.
75824943Seric **
75924943Seric **	Side Effects:
76024943Seric **		none.
76124943Seric */
76224943Seric 
76324943Seric bool
76457438Seric shouldqueue(pri, ctime)
76524943Seric 	long pri;
76657438Seric 	time_t ctime;
76724943Seric {
76851920Seric 	if (CurrentLA < QueueLA)
76924943Seric 		return (FALSE);
77058132Seric 	if (CurrentLA >= RefuseLA)
77158132Seric 		return (TRUE);
77251920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77324943Seric }
77424943Seric /*
77553037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
77653037Seric **
77753037Seric **	Parameters:
77853037Seric **		none.
77953037Seric **
78053037Seric **	Returns:
78153037Seric **		TRUE if incoming SMTP connections should be refused
78253037Seric **			(for now).
78353037Seric **		FALSE if we should accept new work.
78453037Seric **
78553037Seric **	Side Effects:
78653037Seric **		none.
78753037Seric */
78853037Seric 
78953037Seric bool
79053037Seric refuseconnections()
79153037Seric {
79259156Seric #ifdef XLA
79359156Seric 	if (!xla_smtp_ok())
79459156Seric 		return TRUE;
79559156Seric #endif
79659156Seric 
79753037Seric 	/* this is probably too simplistic */
79858132Seric 	return (CurrentLA >= RefuseLA);
79953037Seric }
80053037Seric /*
80124943Seric **  SETPROCTITLE -- set process title for ps
80224943Seric **
80324943Seric **	Parameters:
80458674Seric **		fmt -- a printf style format string.
80558674Seric **		a, b, c -- possible parameters to fmt.
80624943Seric **
80724943Seric **	Returns:
80824943Seric **		none.
80924943Seric **
81024943Seric **	Side Effects:
81124943Seric **		Clobbers argv of our main procedure so ps(1) will
81224943Seric **		display the title.
81324943Seric */
81424943Seric 
81558689Seric #ifdef SETPROCTITLE
81658689Seric # ifdef __hpux
81758689Seric #  include <sys/pstat.h>
81858689Seric # endif
81959732Seric # ifdef BSD4_4
82059732Seric #  include <machine/vmparam.h>
82159732Seric #  include <sys/exec.h>
82264247Seric #  ifdef __bsdi__
82364247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
82464247Seric #  endif
82559992Seric #  ifdef PS_STRINGS
82659992Seric #   define SETPROC_STATIC static
82759992Seric #  endif
82859732Seric # endif
82959732Seric # ifndef SETPROC_STATIC
83059732Seric #  define SETPROC_STATIC
83159732Seric # endif
83258689Seric #endif
83358689Seric 
83424943Seric /*VARARGS1*/
83557642Seric #ifdef __STDC__
83657642Seric setproctitle(char *fmt, ...)
83757642Seric #else
83857642Seric setproctitle(fmt, va_alist)
83924943Seric 	char *fmt;
84057642Seric 	va_dcl
84157642Seric #endif
84224943Seric {
84324943Seric # ifdef SETPROCTITLE
84424943Seric 	register char *p;
84525049Seric 	register int i;
84659732Seric 	SETPROC_STATIC char buf[MAXLINE];
84756852Seric 	VA_LOCAL_DECL
84858689Seric #  ifdef __hpux
84958689Seric 	union pstun pst;
85058689Seric #  endif
85124943Seric 	extern char **Argv;
85224943Seric 	extern char *LastArgv;
85324943Seric 
85458674Seric 	p = buf;
85524943Seric 
85658674Seric 	/* print sendmail: heading for grep */
85758674Seric 	(void) strcpy(p, "sendmail: ");
85858674Seric 	p += strlen(p);
85924943Seric 
86058674Seric 	/* print the argument string */
86158674Seric 	VA_START(fmt);
86258674Seric 	(void) vsprintf(p, fmt, ap);
86356852Seric 	VA_END;
86454996Seric 
86558674Seric 	i = strlen(buf);
86658689Seric 
86758689Seric #  ifdef __hpux
86858689Seric 	pst.pst_command = buf;
86958689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87058689Seric #  else
87159992Seric #   ifdef PS_STRINGS
87259732Seric 	PS_STRINGS->ps_nargvstr = 1;
87359732Seric 	PS_STRINGS->ps_argvstr = buf;
87459732Seric #   else
87554996Seric 	if (i > LastArgv - Argv[0] - 2)
87625049Seric 	{
87754996Seric 		i = LastArgv - Argv[0] - 2;
87858674Seric 		buf[i] = '\0';
87925049Seric 	}
88058674Seric 	(void) strcpy(Argv[0], buf);
88154997Seric 	p = &Argv[0][i];
88224943Seric 	while (p < LastArgv)
88324943Seric 		*p++ = ' ';
88459732Seric #   endif
88558689Seric #  endif
88656795Seric # endif /* SETPROCTITLE */
88724943Seric }
88825698Seric /*
88925698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
89025698Seric **
89125698Seric **	Parameters:
89225698Seric **		none.
89325698Seric **
89425698Seric **	Returns:
89525698Seric **		none.
89625698Seric **
89725698Seric **	Side Effects:
89825698Seric **		Picks up extant zombies.
89925698Seric */
90025698Seric 
90125698Seric # include <sys/wait.h>
90225698Seric 
90346928Sbostic void
90425698Seric reapchild()
90525698Seric {
90663966Seric # ifdef HASWAITPID
90760219Seric 	auto int status;
90860560Seric 	int count;
90960560Seric 	int pid;
91060219Seric 
91160560Seric 	count = 0;
91260560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
91360560Seric 	{
91460560Seric 		if (count++ > 1000)
91560560Seric 		{
91660560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
91760560Seric 				pid, status);
91860560Seric 			break;
91960560Seric 		}
92060560Seric 	}
92160219Seric # else
92225698Seric # ifdef WNOHANG
92325698Seric 	union wait status;
92425698Seric 
92563753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
92625698Seric 		continue;
92756795Seric # else /* WNOHANG */
92825698Seric 	auto int status;
92925698Seric 
93060219Seric 	while (wait(&status) > 0)
93125698Seric 		continue;
93256795Seric # endif /* WNOHANG */
93360219Seric # endif
93464035Seric # ifdef SYS5SIGNALS
93564035Seric 	(void) setsignal(SIGCHLD, reapchild);
93658061Seric # endif
93725698Seric }
93855418Seric /*
93955418Seric **  UNSETENV -- remove a variable from the environment
94055418Seric **
94155418Seric **	Not needed on newer systems.
94255418Seric **
94355418Seric **	Parameters:
94455418Seric **		name -- the string name of the environment variable to be
94555418Seric **			deleted from the current environment.
94655418Seric **
94755418Seric **	Returns:
94855418Seric **		none.
94955418Seric **
95055418Seric **	Globals:
95155418Seric **		environ -- a pointer to the current environment.
95255418Seric **
95355418Seric **	Side Effects:
95455418Seric **		Modifies environ.
95555418Seric */
95655418Seric 
95763962Seric #ifndef HASUNSETENV
95855418Seric 
95955418Seric void
96055418Seric unsetenv(name)
96155418Seric 	char *name;
96255418Seric {
96355418Seric 	extern char **environ;
96455418Seric 	register char **pp;
96555418Seric 	int len = strlen(name);
96655418Seric 
96755418Seric 	for (pp = environ; *pp != NULL; pp++)
96855418Seric 	{
96955418Seric 		if (strncmp(name, *pp, len) == 0 &&
97055418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
97155418Seric 			break;
97255418Seric 	}
97355418Seric 
97455418Seric 	for (; *pp != NULL; pp++)
97555418Seric 		*pp = pp[1];
97655418Seric }
97755418Seric 
97863962Seric #endif
97956215Seric /*
98056215Seric **  GETDTABLESIZE -- return number of file descriptors
98156215Seric **
98256215Seric **	Only on non-BSD systems
98356215Seric **
98456215Seric **	Parameters:
98556215Seric **		none
98656215Seric **
98756215Seric **	Returns:
98856215Seric **		size of file descriptor table
98956215Seric **
99056215Seric **	Side Effects:
99156215Seric **		none
99256215Seric */
99356215Seric 
99463787Seric #ifdef SOLARIS
99563787Seric # include <sys/resource.h>
99663787Seric #endif
99756215Seric 
99856215Seric int
99963787Seric getdtsize()
100056215Seric {
100163787Seric #ifdef RLIMIT_NOFILE
100263787Seric 	struct rlimit rl;
100363787Seric 
100463787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
100563787Seric 		return rl.rlim_cur;
100663787Seric #endif
100763787Seric 
100864031Seric # ifdef HASGETDTABLESIZE
100964031Seric 	return getdtablesize();
101064031Seric # else
101164031Seric #  ifdef _SC_OPEN_MAX
101258689Seric 	return sysconf(_SC_OPEN_MAX);
101363787Seric #  else
101456215Seric 	return NOFILE;
101563787Seric #  endif
101658689Seric # endif
101756215Seric }
101857631Seric /*
101957631Seric **  UNAME -- get the UUCP name of this system.
102057631Seric */
102157631Seric 
102257943Seric #ifndef HASUNAME
102357631Seric 
102457631Seric int
102557631Seric uname(name)
102657631Seric 	struct utsname *name;
102757631Seric {
102857631Seric 	FILE *file;
102957631Seric 	char *n;
103057631Seric 
103157631Seric 	name->nodename[0] = '\0';
103257631Seric 
103357661Seric 	/* try /etc/whoami -- one line with the node name */
103457631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
103557631Seric 	{
103657661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
103757631Seric 		(void) fclose(file);
103857661Seric 		n = strchr(name->nodename, '\n');
103957631Seric 		if (n != NULL)
104057631Seric 			*n = '\0';
104157631Seric 		if (name->nodename[0] != '\0')
104257631Seric 			return (0);
104357631Seric 	}
104457631Seric 
104557661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
104657631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
104757631Seric 	{
104857631Seric 		char buf[MAXLINE];
104957631Seric 
105057631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
105157631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
105257631Seric 					NODE_LENGTH, name->nodename) > 0)
105357631Seric 				break;
105457631Seric 		(void) fclose(file);
105557631Seric 		if (name->nodename[0] != '\0')
105657631Seric 			return (0);
105757631Seric 	}
105857631Seric 
105957631Seric #ifdef TRUST_POPEN
106057631Seric 	/*
106157631Seric 	**  Popen is known to have security holes.
106257631Seric 	*/
106357631Seric 
106457661Seric 	/* try uuname -l to return local name */
106557631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
106657631Seric 	{
106757661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
106857631Seric 		(void) pclose(file);
106957661Seric 		n = strchr(name, '\n');
107057631Seric 		if (n != NULL)
107157631Seric 			*n = '\0';
107257661Seric 		if (name->nodename[0] != '\0')
107357631Seric 			return (0);
107457631Seric 	}
107557631Seric #endif
107657631Seric 
107757631Seric 	return (-1);
107857631Seric }
107957943Seric #endif /* HASUNAME */
108058068Seric /*
108158068Seric **  INITGROUPS -- initialize groups
108258068Seric **
108358068Seric **	Stub implementation for System V style systems
108458068Seric */
108558068Seric 
108658068Seric #ifndef HASINITGROUPS
108758068Seric 
108858068Seric initgroups(name, basegid)
108958068Seric 	char *name;
109058068Seric 	int basegid;
109158068Seric {
109258068Seric 	return 0;
109358068Seric }
109458068Seric 
109558068Seric #endif
109658082Seric /*
109759289Seric **  SETSID -- set session id (for non-POSIX systems)
109859289Seric */
109959289Seric 
110059289Seric #ifndef HASSETSID
110159289Seric 
110263753Seric pid_t
110363753Seric setsid __P ((void))
110459289Seric {
110564155Seric #ifdef TIOCNOTTY
110664155Seric 	int fd;
110764155Seric 
110864155Seric 	fd = open("/dev/tty", 2);
110964155Seric 	if (fd >= 0)
111064155Seric 	{
111164155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
111264155Seric 		(void) close(fd);
111364155Seric 	}
111464155Seric #endif /* TIOCNOTTY */
111559289Seric # ifdef SYSTEM5
111663753Seric 	return setpgrp();
111763753Seric # else
111864155Seric 	return setpgid(0, getpid());
111959289Seric # endif
112059289Seric }
112159289Seric 
112259289Seric #endif
112359289Seric /*
112463969Seric **  GETOPT -- for old systems or systems with bogus implementations
112563969Seric */
112663969Seric 
112763969Seric #ifdef NEEDGETOPT
112863969Seric 
112963969Seric /*
113063969Seric  * Copyright (c) 1985 Regents of the University of California.
113163969Seric  * All rights reserved.  The Berkeley software License Agreement
113263969Seric  * specifies the terms and conditions for redistribution.
113363969Seric  */
113463969Seric 
113563969Seric 
113663969Seric /*
113763969Seric ** this version hacked to add `atend' flag to allow state machine
113863969Seric ** to reset if invoked by the program to scan args for a 2nd time
113963969Seric */
114063969Seric 
114163969Seric #if defined(LIBC_SCCS) && !defined(lint)
114263969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
114364072Seric #endif /* LIBC_SCCS and not lint */
114463969Seric 
114563969Seric #include <stdio.h>
114663969Seric 
114763969Seric /*
114863969Seric  * get option letter from argument vector
114963969Seric  */
115063969Seric int	opterr = 1,		/* if error message should be printed */
115163969Seric 	optind = 1,		/* index into parent argv vector */
115263969Seric 	optopt;			/* character checked for validity */
115363969Seric char	*optarg;		/* argument associated with option */
115463969Seric 
115563969Seric #define BADCH	(int)'?'
115663969Seric #define EMSG	""
115763969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
115863969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
115963969Seric 
116063969Seric getopt(nargc,nargv,ostr)
116163969Seric int	nargc;
116263969Seric char	**nargv,
116363969Seric 	*ostr;
116463969Seric {
116563969Seric 	static char	*place = EMSG;	/* option letter processing */
116663969Seric 	static char	atend = 0;
116763969Seric 	register char	*oli;		/* option letter list index */
116863969Seric 
116963969Seric 	if (atend) {
117063969Seric 		atend = 0;
117163969Seric 		place = EMSG;
117263969Seric 	}
117363969Seric 	if(!*place) {			/* update scanning pointer */
117464136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
117563969Seric 			atend++;
117663969Seric 			return(EOF);
117763969Seric 		}
117863969Seric 		if (*place == '-') {	/* found "--" */
117963969Seric 			++optind;
118063969Seric 			atend++;
118163969Seric 			return(EOF);
118263969Seric 		}
118363969Seric 	}				/* option letter okay? */
118464125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
118564136Seric 		if (!*place) ++optind;
118663969Seric 		tell(": illegal option -- ");
118763969Seric 	}
118863969Seric 	if (*++oli != ':') {		/* don't need argument */
118963969Seric 		optarg = NULL;
119063969Seric 		if (!*place) ++optind;
119163969Seric 	}
119263969Seric 	else {				/* need an argument */
119363969Seric 		if (*place) optarg = place;	/* no white space */
119463969Seric 		else if (nargc <= ++optind) {	/* no arg */
119563969Seric 			place = EMSG;
119663969Seric 			tell(": option requires an argument -- ");
119763969Seric 		}
119863969Seric 	 	else optarg = nargv[optind];	/* white space */
119963969Seric 		place = EMSG;
120063969Seric 		++optind;
120163969Seric 	}
120263969Seric 	return(optopt);			/* dump back option letter */
120363969Seric }
120463969Seric 
120563969Seric #endif
120663969Seric /*
120763969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
120863969Seric */
120963969Seric 
121063969Seric #ifdef NEEDVPRINTF
121163969Seric 
121263969Seric #define MAXARG	16
121363969Seric 
121463969Seric vfprintf(fp, fmt, ap)
121563969Seric 	FILE *	fp;
121663969Seric 	char *	fmt;
121763969Seric 	char **	ap;
121863969Seric {
121963969Seric 	char *	bp[MAXARG];
122063969Seric 	int	i = 0;
122163969Seric 
122263969Seric 	while (*ap && i < MAXARG)
122363969Seric 		bp[i++] = *ap++;
122463969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
122563969Seric 			 bp[4], bp[5], bp[6], bp[7],
122663969Seric 			 bp[8], bp[9], bp[10], bp[11],
122763969Seric 			 bp[12], bp[13], bp[14], bp[15]);
122863969Seric }
122963969Seric 
123063969Seric vsprintf(s, fmt, ap)
123163969Seric 	char *	s;
123263969Seric 	char *	fmt;
123363969Seric 	char **	ap;
123463969Seric {
123563969Seric 	char *	bp[MAXARG];
123663969Seric 	int	i = 0;
123763969Seric 
123863969Seric 	while (*ap && i < MAXARG)
123963969Seric 		bp[i++] = *ap++;
124063969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
124163969Seric 			bp[4], bp[5], bp[6], bp[7],
124263969Seric 			bp[8], bp[9], bp[10], bp[11],
124363969Seric 			bp[12], bp[13], bp[14], bp[15]);
124463969Seric }
124563969Seric 
124663969Seric #endif
124763969Seric /*
124864311Seric **  FREESPACE -- see how much free space is on the queue filesystem
124958082Seric **
125058082Seric **	Only implemented if you have statfs.
125158082Seric **
125258082Seric **	Parameters:
125364311Seric **		dir -- the directory in question.
125464311Seric **		bsize -- a variable into which the filesystem
125564311Seric **			block size is stored.
125658082Seric **
125758082Seric **	Returns:
125864311Seric **		The number of bytes free on the queue filesystem.
125964311Seric **		-1 if the statfs call fails.
126064311Seric **
126164311Seric **	Side effects:
126264311Seric **		Puts the filesystem block size into bsize.
126358082Seric */
126458082Seric 
126558082Seric #ifdef HASSTATFS
126658157Seric # undef HASUSTAT
126758157Seric #endif
126858157Seric 
126958157Seric #if defined(HASUSTAT)
127058157Seric # include <ustat.h>
127158157Seric #endif
127258157Seric 
127358157Seric #ifdef HASSTATFS
1274*64314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_)
127558133Seric #  include <sys/statfs.h>
127658133Seric # else
1277*64314Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT)
127858133Seric #   include <sys/vfs.h>
127958133Seric #  else
128058157Seric #   include <sys/mount.h>
128158133Seric #  endif
128258133Seric # endif
128358082Seric #endif
128458082Seric 
128564311Seric long
128664311Seric freespace(dir, bsize)
128764311Seric 	char *dir;
128864311Seric 	long *bsize;
128958082Seric {
129058160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
129158157Seric # if defined(HASUSTAT)
129258153Seric 	struct ustat fs;
129358153Seric 	struct stat statbuf;
129458366Seric #  define FSBLOCKSIZE	DEV_BSIZE
129558157Seric #  define f_bavail	f_tfree
129658157Seric # else
129758157Seric #  if defined(ultrix)
129858157Seric 	struct fs_data fs;
129958157Seric #   define f_bavail	fd_bfreen
130058366Seric #   define FSBLOCKSIZE	fs.fd_bsize
130158153Seric #  else
130258082Seric 	struct statfs fs;
130358366Seric #   define FSBLOCKSIZE	fs.f_bsize
130464311Seric #   if defined(_SCO_UNIX_)
130563987Seric #    define f_bavail f_bfree
130663987Seric #   endif
130758153Seric #  endif
130858133Seric # endif
130958082Seric 	extern int errno;
131058082Seric 
131158157Seric # if defined(HASUSTAT)
131264311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
131358157Seric # else
1314*64314Seric #  if defined(IRIX) || defined(apollo)
131564311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
131658157Seric #  else
131758157Seric #   if defined(ultrix)
131864311Seric 	if (statfs(dir, &fs) > 0)
131958153Seric #   else
132064311Seric 	if (statfs(dir, &fs) == 0)
132158153Seric #   endif
132258133Seric #  endif
132358133Seric # endif
132458133Seric 	{
132564311Seric 		if (bsize != NULL)
132664311Seric 			*bsize = FSBLOCKSIZE;
132764311Seric 		return (fs.f_bavail);
132864311Seric 	}
132964311Seric 	return (-1);
133064311Seric }
133164311Seric /*
133264311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
133364311Seric **
133464311Seric **	Only implemented if you have statfs.
133564311Seric **
133664311Seric **	Parameters:
133764311Seric **		msize -- the size to check against.  If zero, we don't yet
133864311Seric **		know how big the message will be, so just check for
133964311Seric **		a "reasonable" amount.
134064311Seric **
134164311Seric **	Returns:
134264311Seric **		TRUE if there is enough space.
134364311Seric **		FALSE otherwise.
134464311Seric */
134564311Seric 
134664311Seric bool
134764311Seric enoughspace(msize)
134864311Seric 	long msize;
134964311Seric {
135064311Seric 	long bfree, bsize;
135164311Seric 
135264311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
135364311Seric 	{
135458133Seric 		if (tTd(4, 80))
135564311Seric 			printf("enoughspace: no threshold\n");
135664311Seric 		return TRUE;
135764311Seric 	}
135864311Seric 
135964311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
136064311Seric 	{
136164311Seric 		if (tTd(4, 80))
136258333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
136364311Seric 				bfree, msize);
136458333Seric 
136558333Seric 		/* convert msize to block count */
136664311Seric 		msize = msize / bsize + 1;
136758333Seric 		if (MinBlocksFree >= 0)
136858333Seric 			msize += MinBlocksFree;
136958333Seric 
137064311Seric 		if (bfree < msize)
137158090Seric 		{
137258090Seric #ifdef LOG
137358090Seric 			if (LogLevel > 0)
137464254Seric 				syslog(LOG_ALERT,
137564254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
137664311Seric 					CurEnv->e_id, bfree,
137764254Seric 					CurHostName, msize, QueueDir);
137858090Seric #endif
137958082Seric 			return FALSE;
138058090Seric 		}
138158082Seric 	}
138258082Seric 	else if (tTd(4, 80))
138358333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
138458333Seric 			MinBlocksFree, msize, errstring(errno));
138558082Seric #endif
138658082Seric 	return TRUE;
138758082Seric }
138858542Seric /*
138958542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
139058542Seric **
139158542Seric **	This looks at an errno value and tells if this is likely to
139258542Seric **	go away if retried later.
139358542Seric **
139458542Seric **	Parameters:
139558542Seric **		err -- the errno code to classify.
139658542Seric **
139758542Seric **	Returns:
139858542Seric **		TRUE if this is probably transient.
139958542Seric **		FALSE otherwise.
140058542Seric */
140158542Seric 
140258542Seric bool
140358542Seric transienterror(err)
140458542Seric 	int err;
140558542Seric {
140658542Seric 	switch (err)
140758542Seric 	{
140858542Seric 	  case EIO:			/* I/O error */
140958542Seric 	  case ENXIO:			/* Device not configured */
141058542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
141158542Seric 	  case ENOMEM:			/* Cannot allocate memory */
141258542Seric 	  case ENODEV:			/* Operation not supported by device */
141358542Seric 	  case ENFILE:			/* Too many open files in system */
141458542Seric 	  case EMFILE:			/* Too many open files */
141558542Seric 	  case ENOSPC:			/* No space left on device */
141658542Seric #ifdef ETIMEDOUT
141758542Seric 	  case ETIMEDOUT:		/* Connection timed out */
141858542Seric #endif
141958542Seric #ifdef ESTALE
142058542Seric 	  case ESTALE:			/* Stale NFS file handle */
142158542Seric #endif
142258542Seric #ifdef ENETDOWN
142358542Seric 	  case ENETDOWN:		/* Network is down */
142458542Seric #endif
142558542Seric #ifdef ENETUNREACH
142658542Seric 	  case ENETUNREACH:		/* Network is unreachable */
142758542Seric #endif
142858542Seric #ifdef ENETRESET
142958542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
143058542Seric #endif
143158542Seric #ifdef ECONNABORTED
143258542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
143358542Seric #endif
143458542Seric #ifdef ECONNRESET
143558542Seric 	  case ECONNRESET:		/* Connection reset by peer */
143658542Seric #endif
143758542Seric #ifdef ENOBUFS
143858542Seric 	  case ENOBUFS:			/* No buffer space available */
143958542Seric #endif
144058542Seric #ifdef ESHUTDOWN
144158542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
144258542Seric #endif
144358542Seric #ifdef ECONNREFUSED
144458542Seric 	  case ECONNREFUSED:		/* Connection refused */
144558542Seric #endif
144658542Seric #ifdef EHOSTDOWN
144758542Seric 	  case EHOSTDOWN:		/* Host is down */
144858542Seric #endif
144958542Seric #ifdef EHOSTUNREACH
145058542Seric 	  case EHOSTUNREACH:		/* No route to host */
145158542Seric #endif
145258542Seric #ifdef EDQUOT
145358542Seric 	  case EDQUOT:			/* Disc quota exceeded */
145458542Seric #endif
145558542Seric #ifdef EPROCLIM
145658542Seric 	  case EPROCLIM:		/* Too many processes */
145758542Seric #endif
145858542Seric #ifdef EUSERS
145958542Seric 	  case EUSERS:			/* Too many users */
146058542Seric #endif
146158542Seric #ifdef EDEADLK
146258542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
146358542Seric #endif
146458542Seric #ifdef EISCONN
146558542Seric 	  case EISCONN:			/* Socket already connected */
146658542Seric #endif
146758542Seric #ifdef EINPROGRESS
146858542Seric 	  case EINPROGRESS:		/* Operation now in progress */
146958542Seric #endif
147058542Seric #ifdef EALREADY
147158542Seric 	  case EALREADY:		/* Operation already in progress */
147258542Seric #endif
147358542Seric #ifdef EADDRINUSE
147458542Seric 	  case EADDRINUSE:		/* Address already in use */
147558542Seric #endif
147658542Seric #ifdef EADDRNOTAVAIL
147758542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
147858542Seric #endif
147963834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
148058542Seric 	  case ENOSR:			/* Out of streams resources */
148158542Seric #endif
148258542Seric 		return TRUE;
148358542Seric 	}
148458542Seric 
148558542Seric 	/* nope, must be permanent */
148658542Seric 	return FALSE;
148758542Seric }
148858689Seric /*
148964035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
149058689Seric **
149158689Seric **	Parameters:
149258689Seric **		fd -- the file descriptor of the file.
149358689Seric **		filename -- the file name (for error messages).
149458689Seric **		type -- type of the lock.  Bits can be:
149558689Seric **			LOCK_EX -- exclusive lock.
149658689Seric **			LOCK_NB -- non-blocking.
149758689Seric **
149858689Seric **	Returns:
149958689Seric **		TRUE if the lock was acquired.
150058689Seric **		FALSE otherwise.
150158689Seric */
150258689Seric 
150358689Seric bool
150458689Seric lockfile(fd, filename, type)
150558689Seric 	int fd;
150658689Seric 	char *filename;
150758689Seric 	int type;
150858689Seric {
150964035Seric # ifndef HASFLOCK
151058689Seric 	int action;
151158689Seric 	struct flock lfd;
151264035Seric 
151363983Seric 	bzero(&lfd, sizeof lfd);
151459447Seric 	if (bitset(LOCK_UN, type))
151559447Seric 		lfd.l_type = F_UNLCK;
151659447Seric 	else if (bitset(LOCK_EX, type))
151758689Seric 		lfd.l_type = F_WRLCK;
151858689Seric 	else
151958689Seric 		lfd.l_type = F_RDLCK;
152058689Seric 
152158689Seric 	if (bitset(LOCK_NB, type))
152258689Seric 		action = F_SETLK;
152358689Seric 	else
152458689Seric 		action = F_SETLKW;
152558689Seric 
152664264Seric 	if (tTd(55, 60))
152764264Seric 		printf("lockfile(%s, action=%d, type=%d): ",
152864264Seric 			filename, action, lfd.l_type);
152964264Seric 
153058689Seric 	if (fcntl(fd, action, &lfd) >= 0)
153164264Seric 	{
153264264Seric 		if (tTd(55, 60))
153364264Seric 			printf("SUCCESS\n");
153458689Seric 		return TRUE;
153564264Seric 	}
153658689Seric 
153764264Seric 	if (tTd(55, 60))
153864264Seric 		printf("(%s) ", errstring(errno));
153964264Seric 
154064136Seric 	/*
154164136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
154264136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
154364136Seric 	**  as type "tmp" (that is, served from swap space), the
154464136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
154564136Seric 	**  Since this is fairly common during testing, we will assume
154664136Seric 	**  that this indicates that the lock is successfully grabbed.
154764136Seric 	*/
154864136Seric 
154964136Seric 	if (errno == EINVAL)
155064264Seric 	{
155164264Seric 		if (tTd(55, 60))
155264264Seric 			printf("SUCCESS\n");
155364136Seric 		return TRUE;
155464264Seric 	}
155564136Seric 
155658689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
155759447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
155858689Seric # else
155964264Seric 	if (tTd(55, 60))
156064264Seric 		printf("lockfile(%s, type=%o): ", filename, type);
156164264Seric 
156258689Seric 	if (flock(fd, type) >= 0)
156364264Seric 	{
156464264Seric 		if (tTd(55, 60))
156564264Seric 			printf("SUCCESS\n");
156658689Seric 		return TRUE;
156764264Seric 	}
156858689Seric 
156964264Seric 	if (tTd(55, 60))
157064264Seric 		printf("(%s) ", errstring(errno));
157164264Seric 
157258689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
157359447Seric 		syserr("cannot flock(%s, %o)", filename, type);
157458689Seric # endif
157564264Seric 	if (tTd(55, 60))
157664264Seric 		printf("FAIL\n");
157758689Seric 	return FALSE;
157858689Seric }
1579