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*63969Seric static char sccsid[] = "@(#)conf.c 8.8 (Berkeley) 07/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> 1758153Seric # include <signal.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6457359Seric "return-receipt-to", H_FROM /* |H_RECEIPTTO */, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 689055Seric "to", H_RCPT, 6911417Seric "resent-to", H_RCPT|H_RESENT, 709055Seric "cc", H_RCPT, 7111417Seric "resent-cc", H_RCPT|H_RESENT, 729055Seric "bcc", H_RCPT|H_ACHECK, 7311417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7456215Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 8859318Seric "x400-received", H_TRACE|H_FORCE, 899055Seric "via", H_TRACE|H_FORCE, 909055Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9358796Seric "comments", H_FORCE, 9463753Seric "return-path", H_FORCE|H_ACHECK, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10540980Sbostic char *ConfFile = _PATH_SENDMAILCF; /* runtime configuration */ 10640980Sbostic char *FreezeFile = _PATH_SENDMAILFC; /* frozen version of above */ 10758082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1089039Seric 1099064Seric 1109064Seric 1119039Seric /* 11258082Seric ** Privacy values 11358082Seric */ 11458082Seric 11558082Seric struct prival PrivacyValues[] = 11658082Seric { 11758082Seric "public", PRIV_PUBLIC, 11858082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11958114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 12058082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12158082Seric "noexpn", PRIV_NOEXPN, 12258082Seric "novrfy", PRIV_NOVRFY, 12358249Seric "restrictmailq", PRIV_RESTRMAILQ, 12458789Seric "authwarnings", PRIV_AUTHWARNINGS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13624943Seric /* 13724943Seric ** SETDEFAULTS -- set default values 13824943Seric ** 13924943Seric ** Because of the way freezing is done, these must be initialized 14024943Seric ** using direct code. 14124943Seric ** 14224943Seric ** Parameters: 14358734Seric ** e -- the default envelope. 14424943Seric ** 14524943Seric ** Returns: 14624943Seric ** none. 14724943Seric ** 14824943Seric ** Side Effects: 14924943Seric ** Initializes a bunch of global variables to their 15024943Seric ** default values. 15124943Seric */ 15224943Seric 15358737Seric #define DAYS * 24 * 60 * 60 15458737Seric 15558734Seric setdefaults(e) 15658734Seric register ENVELOPE *e; 15724943Seric { 15857438Seric SpaceSub = ' '; /* option B */ 15957438Seric QueueLA = 8; /* option x */ 16057438Seric RefuseLA = 12; /* option X */ 16157438Seric WkRecipFact = 30000L; /* option y */ 16257438Seric WkClassFact = 1800L; /* option z */ 16357438Seric WkTimeFact = 90000L; /* option Z */ 16457438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16563787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 16657438Seric /* option F */ 16757438Seric DefUid = 1; /* option u */ 16857438Seric DefGid = 1; /* option g */ 16957438Seric CheckpointInterval = 10; /* option C */ 17057438Seric MaxHopCount = 25; /* option h */ 17158734Seric e->e_sendmode = SM_FORK; /* option d */ 17258734Seric e->e_errormode = EM_PRINT; /* option e */ 17359709Seric SevenBit = FALSE; /* option 7 */ 17457438Seric MaxMciCache = 1; /* option k */ 17557438Seric MciCacheTimeout = 300; /* option K */ 17657438Seric LogLevel = 9; /* option L */ 17758112Seric settimeouts(NULL); /* option r */ 17858737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17958737Seric TimeOuts.to_q_warning = 0; /* option T */ 18058853Seric PrivacyFlags = 0; /* option p */ 18140973Sbostic setdefuser(); 18253654Seric setupmaps(); 18357402Seric setupmailers(); 18424943Seric } 185294Seric 18640973Sbostic 1874326Seric /* 18840973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 18940973Sbostic */ 19040973Sbostic 19140973Sbostic setdefuser() 19240973Sbostic { 19340973Sbostic struct passwd *defpwent; 19457386Seric static char defuserbuf[40]; 19540973Sbostic 19657386Seric DefUser = defuserbuf; 19740973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19857386Seric strcpy(defuserbuf, defpwent->pw_name); 19940973Sbostic else 20057386Seric strcpy(defuserbuf, "nobody"); 20140973Sbostic } 20253654Seric /* 20356836Seric ** HOST_MAP_INIT -- initialize host class structures 20456836Seric */ 20556836Seric 20656836Seric bool 20760219Seric host_map_init(map, args) 20856836Seric MAP *map; 20956836Seric char *args; 21056836Seric { 21156836Seric register char *p = args; 21256836Seric 21356836Seric for (;;) 21456836Seric { 21558050Seric while (isascii(*p) && isspace(*p)) 21656836Seric p++; 21756836Seric if (*p != '-') 21856836Seric break; 21956836Seric switch (*++p) 22056836Seric { 22156836Seric case 'a': 22256836Seric map->map_app = ++p; 22356836Seric break; 22456836Seric } 22558050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 22656836Seric p++; 22756836Seric if (*p != '\0') 22856836Seric *p++ = '\0'; 22956836Seric } 23056836Seric if (map->map_app != NULL) 23156836Seric map->map_app = newstr(map->map_app); 23256836Seric return TRUE; 23356836Seric } 23457402Seric /* 23557402Seric ** SETUPMAILERS -- initialize default mailers 23657402Seric */ 23756836Seric 23857402Seric setupmailers() 23957402Seric { 24057402Seric char buf[100]; 24157402Seric 24257403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 24357403Seric makemailer(buf); 24457403Seric 24559883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 24657402Seric makemailer(buf); 24757402Seric 24857402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 24957402Seric makemailer(buf); 25057402Seric } 25156836Seric /* 25260207Seric ** SETUPMAPS -- set up map classes 25360207Seric */ 25460207Seric 25560207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 25660207Seric { \ 25760207Seric extern bool parse __P((MAP *, char *)); \ 25860207Seric extern bool open __P((MAP *, int)); \ 25960207Seric extern void close __P((MAP *)); \ 26060207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 26160207Seric extern void store __P((MAP *, char *, char *)); \ 26260207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 26360207Seric s->s_mapclass.map_cname = name; \ 26460207Seric s->s_mapclass.map_ext = ext; \ 26560207Seric s->s_mapclass.map_cflags = flags; \ 26660207Seric s->s_mapclass.map_parse = parse; \ 26760207Seric s->s_mapclass.map_open = open; \ 26860207Seric s->s_mapclass.map_close = close; \ 26960207Seric s->s_mapclass.map_lookup = lookup; \ 27060207Seric s->s_mapclass.map_store = store; \ 27160207Seric } 27260207Seric 27360207Seric setupmaps() 27460207Seric { 27560207Seric register STAB *s; 27660207Seric 27760207Seric #ifdef NEWDB 27860207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 27960207Seric map_parseargs, hash_map_open, db_map_close, 28060207Seric db_map_lookup, db_map_store); 28160207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 28260207Seric map_parseargs, bt_map_open, db_map_close, 28360207Seric db_map_lookup, db_map_store); 28460207Seric #endif 28560207Seric 28660207Seric #ifdef NDBM 28760207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 28860207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 28960207Seric ndbm_map_lookup, ndbm_map_store); 29060207Seric #endif 29160207Seric 29260207Seric #ifdef NIS 29360207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 29460207Seric map_parseargs, nis_map_open, nis_map_close, 29560207Seric nis_map_lookup, nis_map_store); 29660207Seric #endif 29760207Seric 29860207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 29960207Seric map_parseargs, stab_map_open, stab_map_close, 30060207Seric stab_map_lookup, stab_map_store); 30160207Seric 30260207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 30360207Seric map_parseargs, impl_map_open, impl_map_close, 30460207Seric impl_map_lookup, impl_map_store); 30560207Seric 30660207Seric /* host DNS lookup */ 30760207Seric MAPDEF("host", NULL, 0, 30860207Seric host_map_init, null_map_open, null_map_close, 30960207Seric host_map_lookup, null_map_store); 31060207Seric 31160207Seric /* dequote map */ 31260207Seric MAPDEF("dequote", NULL, 0, 31360207Seric dequote_init, null_map_open, null_map_close, 31460207Seric dequote_map, null_map_store); 31560207Seric 31660207Seric #if 0 31760207Seric # ifdef USERDB 31860207Seric /* user database */ 31960207Seric MAPDEF("udb", ".db", 0, 32060207Seric udb_map_parse, null_map_open, null_map_close, 32160207Seric udb_map_lookup, null_map_store); 32260207Seric # endif 32360207Seric #endif 32460207Seric } 32560207Seric 32660207Seric #undef MAPDEF 32760207Seric /* 3289369Seric ** USERNAME -- return the user id of the logged in user. 3299369Seric ** 3309369Seric ** Parameters: 3319369Seric ** none. 3329369Seric ** 3339369Seric ** Returns: 3349369Seric ** The login name of the logged in user. 3359369Seric ** 3369369Seric ** Side Effects: 3379369Seric ** none. 3389369Seric ** 3399369Seric ** Notes: 3409369Seric ** The return value is statically allocated. 3419369Seric */ 3429369Seric 3439369Seric char * 3449369Seric username() 3459369Seric { 34617469Seric static char *myname = NULL; 3479369Seric extern char *getlogin(); 34819904Smiriam register struct passwd *pw; 3499369Seric 35017469Seric /* cache the result */ 35117469Seric if (myname == NULL) 35217469Seric { 35317469Seric myname = getlogin(); 35417469Seric if (myname == NULL || myname[0] == '\0') 35517469Seric { 35663787Seric pw = getpwuid(RealUid); 35717469Seric if (pw != NULL) 35840993Sbostic myname = newstr(pw->pw_name); 35917469Seric } 36019904Smiriam else 36119904Smiriam { 36263787Seric uid_t uid = RealUid; 36319873Smiriam 36440993Sbostic myname = newstr(myname); 36540993Sbostic if ((pw = getpwnam(myname)) == NULL || 36658736Seric (uid != 0 && uid != pw->pw_uid)) 36719904Smiriam { 36858736Seric pw = getpwuid(uid); 36924945Seric if (pw != NULL) 37040993Sbostic myname = newstr(pw->pw_name); 37119873Smiriam } 37219873Smiriam } 37317469Seric if (myname == NULL || myname[0] == '\0') 37417469Seric { 37558151Seric syserr("554 Who are you?"); 37617469Seric myname = "postmaster"; 37717469Seric } 37817469Seric } 37917469Seric 38017469Seric return (myname); 3819369Seric } 3829369Seric /* 3834190Seric ** TTYPATH -- Get the path of the user's tty 384294Seric ** 385294Seric ** Returns the pathname of the user's tty. Returns NULL if 386294Seric ** the user is not logged in or if s/he has write permission 387294Seric ** denied. 388294Seric ** 389294Seric ** Parameters: 390294Seric ** none 391294Seric ** 392294Seric ** Returns: 393294Seric ** pathname of the user's tty. 394294Seric ** NULL if not logged in or write permission denied. 395294Seric ** 396294Seric ** Side Effects: 397294Seric ** none. 398294Seric ** 399294Seric ** WARNING: 400294Seric ** Return value is in a local buffer. 401294Seric ** 402294Seric ** Called By: 403294Seric ** savemail 404294Seric */ 405294Seric 406294Seric char * 407294Seric ttypath() 408294Seric { 409294Seric struct stat stbuf; 410294Seric register char *pathn; 411294Seric extern char *ttyname(); 4124081Seric extern char *getlogin(); 413294Seric 414294Seric /* compute the pathname of the controlling tty */ 4159369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4169369Seric (pathn = ttyname(0)) == NULL) 417294Seric { 418294Seric errno = 0; 419294Seric return (NULL); 420294Seric } 421294Seric 422294Seric /* see if we have write permission */ 4232967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 424294Seric { 425294Seric errno = 0; 426294Seric return (NULL); 427294Seric } 428294Seric 429294Seric /* see if the user is logged in */ 430294Seric if (getlogin() == NULL) 431294Seric return (NULL); 432294Seric 433294Seric /* looks good */ 434294Seric return (pathn); 435294Seric } 4362967Seric /* 4372967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4382967Seric ** 4392967Seric ** This routine can be supplied on a per-installation basis 4402967Seric ** to determine whether a person is allowed to send a message. 4412967Seric ** This allows restriction of certain types of internet 4422967Seric ** forwarding or registration of users. 4432967Seric ** 4442967Seric ** If the hosts are found to be incompatible, an error 44557454Seric ** message should be given using "usrerr" and 0 should 4462967Seric ** be returned. 4472967Seric ** 4484288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4494288Seric ** function; this should be done on huge messages. 4504288Seric ** 4512967Seric ** Parameters: 4522967Seric ** to -- the person being sent to. 4532967Seric ** 4542967Seric ** Returns: 45557459Seric ** an exit status 4562967Seric ** 4572967Seric ** Side Effects: 4582967Seric ** none (unless you include the usrerr stuff) 4592967Seric */ 4602967Seric 46155012Seric checkcompat(to, e) 4622967Seric register ADDRESS *to; 46355012Seric register ENVELOPE *e; 4642967Seric { 46512133Seric # ifdef lint 46612133Seric if (to == NULL) 46712133Seric to++; 46863834Seric # endif /* lint */ 46910698Seric # ifdef EXAMPLE_CODE 47010698Seric /* this code is intended as an example only */ 4714437Seric register STAB *s; 4724437Seric 4734437Seric s = stab("arpa", ST_MAILER, ST_FIND); 47455012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4759369Seric to->q_mailer == s->s_mailer) 4764437Seric { 47758151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 47810698Seric /* NoReturn = TRUE; to supress return copy */ 47957459Seric return (EX_UNAVAILABLE); 4804437Seric } 48156795Seric # endif /* EXAMPLE_CODE */ 48257459Seric return (EX_OK); 4832967Seric } 4849369Seric /* 4859369Seric ** HOLDSIGS -- arrange to hold all signals 4869369Seric ** 4879369Seric ** Parameters: 4889369Seric ** none. 4899369Seric ** 4909369Seric ** Returns: 4919369Seric ** none. 4929369Seric ** 4939369Seric ** Side Effects: 4949369Seric ** Arranges that signals are held. 4959369Seric */ 4969369Seric 4979369Seric holdsigs() 4989369Seric { 4999369Seric } 5009369Seric /* 5019369Seric ** RLSESIGS -- arrange to release all signals 5029369Seric ** 5039369Seric ** This undoes the effect of holdsigs. 5049369Seric ** 5059369Seric ** Parameters: 5069369Seric ** none. 5079369Seric ** 5089369Seric ** Returns: 5099369Seric ** none. 5109369Seric ** 5119369Seric ** Side Effects: 5129369Seric ** Arranges that signals are released. 5139369Seric */ 5149369Seric 5159369Seric rlsesigs() 5169369Seric { 5179369Seric } 51814872Seric /* 51914872Seric ** GETLA -- get the current load average 52014872Seric ** 52114881Seric ** This code stolen from la.c. 52214881Seric ** 52314872Seric ** Parameters: 52414872Seric ** none. 52514872Seric ** 52614872Seric ** Returns: 52714872Seric ** The current load average as an integer. 52814872Seric ** 52914872Seric ** Side Effects: 53014872Seric ** none. 53114872Seric */ 53214872Seric 53351920Seric /* try to guess what style of load average we have */ 53451920Seric #define LA_ZERO 1 /* always return load average as zero */ 53551920Seric #define LA_INT 2 /* read kmem for avenrun; interpret as int */ 53651920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 53751920Seric #define LA_SUBR 4 /* call getloadavg */ 53814872Seric 53963962Seric /* do guesses based on general OS type */ 54051920Seric #ifndef LA_TYPE 54163962Seric # define LA_TYPE LA_ZERO 54251920Seric #endif 54351920Seric 54451920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) 54551920Seric 54614872Seric #include <nlist.h> 54714872Seric 54851920Seric #ifndef LA_AVENRUN 54963962Seric # ifdef SYSTEM5 55063962Seric # define LA_AVENRUN "avenrun" 55163962Seric # else 55263962Seric # define LA_AVENRUN "_avenrun" 55363962Seric # endif 55451920Seric #endif 55551920Seric 55651920Seric /* _PATH_UNIX should be defined in <paths.h> */ 55751920Seric #ifndef _PATH_UNIX 55863962Seric # if defined(SYSTEM5) 55963962Seric # define _PATH_UNIX "/unix" 56063962Seric # else 56163962Seric # define _PATH_UNIX "/vmunix" 56263962Seric # endif 56351920Seric #endif 56451920Seric 56514872Seric struct nlist Nl[] = 56614872Seric { 56751920Seric { LA_AVENRUN }, 56814872Seric #define X_AVENRUN 0 56914872Seric { 0 }, 57014872Seric }; 57114872Seric 57260583Seric #ifndef FSHIFT 57360583Seric # if defined(unixpc) 57460583Seric # define FSHIFT 5 57560583Seric # endif 57657736Seric 57760583Seric # if defined(__alpha) 57860583Seric # define FSHIFT 10 57960583Seric # endif 58057977Seric 58160583Seric # if (LA_TYPE == LA_INT) 58251920Seric # define FSHIFT 8 58360583Seric # endif 58457736Seric #endif 58560583Seric 58657736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE) 58751920Seric # define FSCALE (1 << FSHIFT) 58851920Seric #endif 58940930Srick 59014872Seric getla() 59114872Seric { 59214872Seric static int kmem = -1; 59351920Seric #if LA_TYPE == LA_INT 59424943Seric long avenrun[3]; 59551920Seric #else 59651920Seric double avenrun[3]; 59751920Seric #endif 59825615Seric extern off_t lseek(); 59957736Seric extern int errno; 60014872Seric 60114872Seric if (kmem < 0) 60214872Seric { 60324945Seric kmem = open("/dev/kmem", 0, 0); 60414872Seric if (kmem < 0) 60557736Seric { 60657736Seric if (tTd(3, 1)) 60757736Seric printf("getla: open(/dev/kmem): %s\n", 60857736Seric errstring(errno)); 60914872Seric return (-1); 61057736Seric } 61151920Seric (void) fcntl(kmem, F_SETFD, 1); 61257736Seric if (nlist(_PATH_UNIX, Nl) < 0) 61357736Seric { 61457736Seric if (tTd(3, 1)) 61557736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 61657736Seric errstring(errno)); 61714872Seric return (-1); 61857736Seric } 61959253Seric if (Nl[X_AVENRUN].n_value == 0) 62059253Seric { 62159253Seric if (tTd(3, 1)) 62259253Seric printf("getla: nlist(%s, %s) ==> 0\n", 62359253Seric _PATH_UNIX, LA_AVENRUN); 62459253Seric return (-1); 62559253Seric } 62614872Seric } 62757736Seric if (tTd(3, 20)) 62857736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 62924945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 63023118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 63119967Seric { 63219967Seric /* thank you Ian */ 63357736Seric if (tTd(3, 1)) 63457736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 63519967Seric return (-1); 63619967Seric } 63751920Seric #if LA_TYPE == LA_INT 63857736Seric if (tTd(3, 5)) 63957736Seric { 64057736Seric printf("getla: avenrun = %d", avenrun[0]); 64157736Seric if (tTd(3, 15)) 64257736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 64357736Seric printf("\n"); 64457736Seric } 64557736Seric if (tTd(3, 1)) 64657736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 64724943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 64851920Seric #else 64957736Seric if (tTd(3, 5)) 65057736Seric { 65157736Seric printf("getla: avenrun = %g", avenrun[0]); 65257736Seric if (tTd(3, 15)) 65357736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 65457736Seric printf("\n"); 65557736Seric } 65657736Seric if (tTd(3, 1)) 65757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 65851920Seric return ((int) (avenrun[0] + 0.5)); 65951920Seric #endif 66014872Seric } 66114872Seric 66251773Seric #else 66351920Seric #if LA_TYPE == LA_SUBR 66451773Seric 66551773Seric getla() 66651773Seric { 66751920Seric double avenrun[3]; 66851920Seric 66951920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 67057736Seric { 67157736Seric if (tTd(3, 1)) 67257736Seric perror("getla: getloadavg failed:"); 67351920Seric return (-1); 67457736Seric } 67557736Seric if (tTd(3, 1)) 67657736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 67751920Seric return ((int) (avenrun[0] + 0.5)); 67851773Seric } 67951773Seric 68051773Seric #else 68151773Seric 68251773Seric getla() 68351773Seric { 68457736Seric if (tTd(3, 1)) 68557736Seric printf("getla: ZERO\n"); 68651920Seric return (0); 68751773Seric } 68851773Seric 68951773Seric #endif 69051773Seric #endif 69124943Seric /* 69224943Seric ** SHOULDQUEUE -- should this message be queued or sent? 69324943Seric ** 69424943Seric ** Compares the message cost to the load average to decide. 69524943Seric ** 69624943Seric ** Parameters: 69724943Seric ** pri -- the priority of the message in question. 69857438Seric ** ctime -- the message creation time. 69924943Seric ** 70024943Seric ** Returns: 70124943Seric ** TRUE -- if this message should be queued up for the 70224943Seric ** time being. 70324943Seric ** FALSE -- if the load is low enough to send this message. 70424943Seric ** 70524943Seric ** Side Effects: 70624943Seric ** none. 70724943Seric */ 70824943Seric 70924943Seric bool 71057438Seric shouldqueue(pri, ctime) 71124943Seric long pri; 71257438Seric time_t ctime; 71324943Seric { 71451920Seric if (CurrentLA < QueueLA) 71524943Seric return (FALSE); 71658132Seric if (CurrentLA >= RefuseLA) 71758132Seric return (TRUE); 71851920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 71924943Seric } 72024943Seric /* 72153037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 72253037Seric ** 72353037Seric ** Parameters: 72453037Seric ** none. 72553037Seric ** 72653037Seric ** Returns: 72753037Seric ** TRUE if incoming SMTP connections should be refused 72853037Seric ** (for now). 72953037Seric ** FALSE if we should accept new work. 73053037Seric ** 73153037Seric ** Side Effects: 73253037Seric ** none. 73353037Seric */ 73453037Seric 73553037Seric bool 73653037Seric refuseconnections() 73753037Seric { 73859156Seric #ifdef XLA 73959156Seric if (!xla_smtp_ok()) 74059156Seric return TRUE; 74159156Seric #endif 74259156Seric 74353037Seric /* this is probably too simplistic */ 74458132Seric return (CurrentLA >= RefuseLA); 74553037Seric } 74653037Seric /* 74724943Seric ** SETPROCTITLE -- set process title for ps 74824943Seric ** 74924943Seric ** Parameters: 75058674Seric ** fmt -- a printf style format string. 75158674Seric ** a, b, c -- possible parameters to fmt. 75224943Seric ** 75324943Seric ** Returns: 75424943Seric ** none. 75524943Seric ** 75624943Seric ** Side Effects: 75724943Seric ** Clobbers argv of our main procedure so ps(1) will 75824943Seric ** display the title. 75924943Seric */ 76024943Seric 76158689Seric #ifdef SETPROCTITLE 76258689Seric # ifdef __hpux 76358689Seric # include <sys/pstat.h> 76458689Seric # endif 76559732Seric # ifdef BSD4_4 76659732Seric # include <machine/vmparam.h> 76759732Seric # include <sys/exec.h> 76859992Seric # ifdef PS_STRINGS 76959992Seric # define SETPROC_STATIC static 77059992Seric # endif 77159732Seric # endif 77259732Seric # ifndef SETPROC_STATIC 77359732Seric # define SETPROC_STATIC 77459732Seric # endif 77558689Seric #endif 77658689Seric 77724943Seric /*VARARGS1*/ 77857642Seric #ifdef __STDC__ 77957642Seric setproctitle(char *fmt, ...) 78057642Seric #else 78157642Seric setproctitle(fmt, va_alist) 78224943Seric char *fmt; 78357642Seric va_dcl 78457642Seric #endif 78524943Seric { 78624943Seric # ifdef SETPROCTITLE 78724943Seric register char *p; 78825049Seric register int i; 78959732Seric SETPROC_STATIC char buf[MAXLINE]; 79056852Seric VA_LOCAL_DECL 79158689Seric # ifdef __hpux 79258689Seric union pstun pst; 79358689Seric # endif 79424943Seric extern char **Argv; 79524943Seric extern char *LastArgv; 79624943Seric 79758674Seric p = buf; 79824943Seric 79958674Seric /* print sendmail: heading for grep */ 80058674Seric (void) strcpy(p, "sendmail: "); 80158674Seric p += strlen(p); 80224943Seric 80358674Seric /* print the argument string */ 80458674Seric VA_START(fmt); 80558674Seric (void) vsprintf(p, fmt, ap); 80656852Seric VA_END; 80754996Seric 80858674Seric i = strlen(buf); 80958689Seric 81058689Seric # ifdef __hpux 81158689Seric pst.pst_command = buf; 81258689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 81358689Seric # else 81459992Seric # ifdef PS_STRINGS 81559732Seric PS_STRINGS->ps_nargvstr = 1; 81659732Seric PS_STRINGS->ps_argvstr = buf; 81759732Seric # else 81854996Seric if (i > LastArgv - Argv[0] - 2) 81925049Seric { 82054996Seric i = LastArgv - Argv[0] - 2; 82158674Seric buf[i] = '\0'; 82225049Seric } 82358674Seric (void) strcpy(Argv[0], buf); 82454997Seric p = &Argv[0][i]; 82524943Seric while (p < LastArgv) 82624943Seric *p++ = ' '; 82759732Seric # endif 82858689Seric # endif 82956795Seric # endif /* SETPROCTITLE */ 83024943Seric } 83125698Seric /* 83225698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 83325698Seric ** 83425698Seric ** Parameters: 83525698Seric ** none. 83625698Seric ** 83725698Seric ** Returns: 83825698Seric ** none. 83925698Seric ** 84025698Seric ** Side Effects: 84125698Seric ** Picks up extant zombies. 84225698Seric */ 84325698Seric 84425698Seric # include <sys/wait.h> 84525698Seric 84646928Sbostic void 84725698Seric reapchild() 84825698Seric { 84963966Seric # ifdef HASWAITPID 85060219Seric auto int status; 85160560Seric int count; 85260560Seric int pid; 85360219Seric 85460560Seric count = 0; 85560560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 85660560Seric { 85760560Seric if (count++ > 1000) 85860560Seric { 85960560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 86060560Seric pid, status); 86160560Seric break; 86260560Seric } 86360560Seric } 86460219Seric # else 86525698Seric # ifdef WNOHANG 86625698Seric union wait status; 86725698Seric 86863753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 86925698Seric continue; 87056795Seric # else /* WNOHANG */ 87125698Seric auto int status; 87225698Seric 87360219Seric while (wait(&status) > 0) 87425698Seric continue; 87556795Seric # endif /* WNOHANG */ 87660219Seric # endif 87758061Seric # ifdef SYSTEM5 87858061Seric (void) signal(SIGCHLD, reapchild); 87958061Seric # endif 88025698Seric } 88155418Seric /* 88263962Seric ** SETENV -- set environment variable 88363962Seric ** 88463962Seric ** Putenv is more modern, but this is a simpler interface 88563962Seric ** 88663962Seric ** Parameters: 88763962Seric ** name -- the name of the envariable. 88863962Seric ** value -- the value of that envariable. 88963962Seric ** overwrite -- if set, overwrite existing value 89063962Seric ** (this is assumed to be set). 89163962Seric ** 89263962Seric ** Returns: 89363962Seric ** none. 89463962Seric ** 89563962Seric ** Side Effects: 89663962Seric ** The environment is updated. 89763962Seric */ 89863962Seric 89963962Seric #ifndef HASSETENV 90063962Seric 90163962Seric setenv(name, value, overwrite) 90263962Seric char *name; 90363962Seric char *value; 90463962Seric int overwrite; 90563962Seric { 90663962Seric register char *p; 90763962Seric 90863962Seric p = xalloc(strlen(name) + strlen(value) + 2); 90963962Seric strcpy(p, name); 91063962Seric strcat(p, "="); 91163962Seric strcat(p, value); 91263962Seric putenv(p); 91363962Seric } 91463962Seric 91563962Seric #endif 91663962Seric /* 91755418Seric ** UNSETENV -- remove a variable from the environment 91855418Seric ** 91955418Seric ** Not needed on newer systems. 92055418Seric ** 92155418Seric ** Parameters: 92255418Seric ** name -- the string name of the environment variable to be 92355418Seric ** deleted from the current environment. 92455418Seric ** 92555418Seric ** Returns: 92655418Seric ** none. 92755418Seric ** 92855418Seric ** Globals: 92955418Seric ** environ -- a pointer to the current environment. 93055418Seric ** 93155418Seric ** Side Effects: 93255418Seric ** Modifies environ. 93355418Seric */ 93455418Seric 93563962Seric #ifndef HASUNSETENV 93655418Seric 93755418Seric void 93855418Seric unsetenv(name) 93955418Seric char *name; 94055418Seric { 94155418Seric extern char **environ; 94255418Seric register char **pp; 94355418Seric int len = strlen(name); 94455418Seric 94555418Seric for (pp = environ; *pp != NULL; pp++) 94655418Seric { 94755418Seric if (strncmp(name, *pp, len) == 0 && 94855418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 94955418Seric break; 95055418Seric } 95155418Seric 95255418Seric for (; *pp != NULL; pp++) 95355418Seric *pp = pp[1]; 95455418Seric } 95555418Seric 95663962Seric #endif 95756215Seric /* 95856215Seric ** GETDTABLESIZE -- return number of file descriptors 95956215Seric ** 96056215Seric ** Only on non-BSD systems 96156215Seric ** 96256215Seric ** Parameters: 96356215Seric ** none 96456215Seric ** 96556215Seric ** Returns: 96656215Seric ** size of file descriptor table 96756215Seric ** 96856215Seric ** Side Effects: 96956215Seric ** none 97056215Seric */ 97156215Seric 97263787Seric #ifdef SOLARIS 97363787Seric # include <sys/resource.h> 97463787Seric #endif 97556215Seric 97656215Seric int 97763787Seric getdtsize() 97856215Seric { 97963787Seric #ifdef RLIMIT_NOFILE 98063787Seric struct rlimit rl; 98163787Seric 98263787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 98363787Seric return rl.rlim_cur; 98463787Seric #endif 98563787Seric 98658689Seric # ifdef _SC_OPEN_MAX 98758689Seric return sysconf(_SC_OPEN_MAX); 98858689Seric # else 98963787Seric # ifdef HASGETDTABLESIZE 99063787Seric return getdtablesize(); 99163787Seric # else 99256215Seric return NOFILE; 99363787Seric # endif 99458689Seric # endif 99556215Seric } 99657631Seric /* 99757631Seric ** UNAME -- get the UUCP name of this system. 99857631Seric */ 99957631Seric 100057943Seric #ifndef HASUNAME 100157631Seric 100257631Seric int 100357631Seric uname(name) 100457631Seric struct utsname *name; 100557631Seric { 100657631Seric FILE *file; 100757631Seric char *n; 100857631Seric 100957631Seric name->nodename[0] = '\0'; 101057631Seric 101157661Seric /* try /etc/whoami -- one line with the node name */ 101257631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 101357631Seric { 101457661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 101557631Seric (void) fclose(file); 101657661Seric n = strchr(name->nodename, '\n'); 101757631Seric if (n != NULL) 101857631Seric *n = '\0'; 101957631Seric if (name->nodename[0] != '\0') 102057631Seric return (0); 102157631Seric } 102257631Seric 102357661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 102457631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 102557631Seric { 102657631Seric char buf[MAXLINE]; 102757631Seric 102857631Seric while (fgets(buf, MAXLINE, file) != NULL) 102957631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 103057631Seric NODE_LENGTH, name->nodename) > 0) 103157631Seric break; 103257631Seric (void) fclose(file); 103357631Seric if (name->nodename[0] != '\0') 103457631Seric return (0); 103557631Seric } 103657631Seric 103757631Seric #ifdef TRUST_POPEN 103857631Seric /* 103957631Seric ** Popen is known to have security holes. 104057631Seric */ 104157631Seric 104257661Seric /* try uuname -l to return local name */ 104357631Seric if ((file = popen("uuname -l", "r")) != NULL) 104457631Seric { 104557661Seric (void) fgets(name, NODE_LENGTH + 1, file); 104657631Seric (void) pclose(file); 104757661Seric n = strchr(name, '\n'); 104857631Seric if (n != NULL) 104957631Seric *n = '\0'; 105057661Seric if (name->nodename[0] != '\0') 105157631Seric return (0); 105257631Seric } 105357631Seric #endif 105457631Seric 105557631Seric return (-1); 105657631Seric } 105757943Seric #endif /* HASUNAME */ 105858068Seric /* 105958068Seric ** INITGROUPS -- initialize groups 106058068Seric ** 106158068Seric ** Stub implementation for System V style systems 106258068Seric */ 106358068Seric 106458068Seric #ifndef HASINITGROUPS 106559430Seric # if !defined(SYSTEM5) || defined(__hpux) 106658068Seric # define HASINITGROUPS 106758068Seric # endif 106858068Seric #endif 106958068Seric 107058068Seric #ifndef HASINITGROUPS 107158068Seric 107258068Seric initgroups(name, basegid) 107358068Seric char *name; 107458068Seric int basegid; 107558068Seric { 107658068Seric return 0; 107758068Seric } 107858068Seric 107958068Seric #endif 108058082Seric /* 108159289Seric ** SETSID -- set session id (for non-POSIX systems) 108259289Seric */ 108359289Seric 108459289Seric #ifndef HASSETSID 108559289Seric 108663753Seric pid_t 108763753Seric setsid __P ((void)) 108859289Seric { 108959289Seric # ifdef SYSTEM5 109063753Seric return setpgrp(); 109163753Seric # else 109263753Seric return 0; 109359289Seric # endif 109459289Seric } 109559289Seric 109659289Seric #endif 109759289Seric /* 1098*63969Seric ** GETOPT -- for old systems or systems with bogus implementations 1099*63969Seric */ 1100*63969Seric 1101*63969Seric #ifdef NEEDGETOPT 1102*63969Seric 1103*63969Seric /* 1104*63969Seric * Copyright (c) 1985 Regents of the University of California. 1105*63969Seric * All rights reserved. The Berkeley software License Agreement 1106*63969Seric * specifies the terms and conditions for redistribution. 1107*63969Seric */ 1108*63969Seric 1109*63969Seric 1110*63969Seric /* 1111*63969Seric ** this version hacked to add `atend' flag to allow state machine 1112*63969Seric ** to reset if invoked by the program to scan args for a 2nd time 1113*63969Seric */ 1114*63969Seric 1115*63969Seric #if defined(LIBC_SCCS) && !defined(lint) 1116*63969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 1117*63969Seric #endif LIBC_SCCS and not lint 1118*63969Seric 1119*63969Seric #include <stdio.h> 1120*63969Seric 1121*63969Seric /* 1122*63969Seric * get option letter from argument vector 1123*63969Seric */ 1124*63969Seric int opterr = 1, /* if error message should be printed */ 1125*63969Seric optind = 1, /* index into parent argv vector */ 1126*63969Seric optopt; /* character checked for validity */ 1127*63969Seric char *optarg; /* argument associated with option */ 1128*63969Seric 1129*63969Seric #define BADCH (int)'?' 1130*63969Seric #define EMSG "" 1131*63969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 1132*63969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 1133*63969Seric 1134*63969Seric getopt(nargc,nargv,ostr) 1135*63969Seric int nargc; 1136*63969Seric char **nargv, 1137*63969Seric *ostr; 1138*63969Seric { 1139*63969Seric static char *place = EMSG; /* option letter processing */ 1140*63969Seric static char atend = 0; 1141*63969Seric register char *oli; /* option letter list index */ 1142*63969Seric char *index(); 1143*63969Seric 1144*63969Seric if (atend) { 1145*63969Seric atend = 0; 1146*63969Seric place = EMSG; 1147*63969Seric } 1148*63969Seric if(!*place) { /* update scanning pointer */ 1149*63969Seric if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 1150*63969Seric atend++; 1151*63969Seric return(EOF); 1152*63969Seric } 1153*63969Seric if (*place == '-') { /* found "--" */ 1154*63969Seric ++optind; 1155*63969Seric atend++; 1156*63969Seric return(EOF); 1157*63969Seric } 1158*63969Seric } /* option letter okay? */ 1159*63969Seric if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) { 1160*63969Seric if(!*place) ++optind; 1161*63969Seric tell(": illegal option -- "); 1162*63969Seric } 1163*63969Seric if (*++oli != ':') { /* don't need argument */ 1164*63969Seric optarg = NULL; 1165*63969Seric if (!*place) ++optind; 1166*63969Seric } 1167*63969Seric else { /* need an argument */ 1168*63969Seric if (*place) optarg = place; /* no white space */ 1169*63969Seric else if (nargc <= ++optind) { /* no arg */ 1170*63969Seric place = EMSG; 1171*63969Seric tell(": option requires an argument -- "); 1172*63969Seric } 1173*63969Seric else optarg = nargv[optind]; /* white space */ 1174*63969Seric place = EMSG; 1175*63969Seric ++optind; 1176*63969Seric } 1177*63969Seric return(optopt); /* dump back option letter */ 1178*63969Seric } 1179*63969Seric 1180*63969Seric #endif 1181*63969Seric /* 1182*63969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 1183*63969Seric */ 1184*63969Seric 1185*63969Seric #ifdef NEEDVPRINTF 1186*63969Seric 1187*63969Seric #define MAXARG 16 1188*63969Seric 1189*63969Seric vfprintf(fp, fmt, ap) 1190*63969Seric FILE * fp; 1191*63969Seric char * fmt; 1192*63969Seric char ** ap; 1193*63969Seric { 1194*63969Seric char * bp[MAXARG]; 1195*63969Seric int i = 0; 1196*63969Seric 1197*63969Seric while (*ap && i < MAXARG) 1198*63969Seric bp[i++] = *ap++; 1199*63969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 1200*63969Seric bp[4], bp[5], bp[6], bp[7], 1201*63969Seric bp[8], bp[9], bp[10], bp[11], 1202*63969Seric bp[12], bp[13], bp[14], bp[15]); 1203*63969Seric } 1204*63969Seric 1205*63969Seric vsprintf(s, fmt, ap) 1206*63969Seric char * s; 1207*63969Seric char * fmt; 1208*63969Seric char ** ap; 1209*63969Seric { 1210*63969Seric char * bp[MAXARG]; 1211*63969Seric int i = 0; 1212*63969Seric 1213*63969Seric while (*ap && i < MAXARG) 1214*63969Seric bp[i++] = *ap++; 1215*63969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 1216*63969Seric bp[4], bp[5], bp[6], bp[7], 1217*63969Seric bp[8], bp[9], bp[10], bp[11], 1218*63969Seric bp[12], bp[13], bp[14], bp[15]); 1219*63969Seric } 1220*63969Seric 1221*63969Seric #endif 1222*63969Seric /* 122358082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 122458082Seric ** 122558082Seric ** Only implemented if you have statfs. 122658082Seric ** 122758082Seric ** Parameters: 122858333Seric ** msize -- the size to check against. If zero, we don't yet 122958333Seric ** know how big the message will be, so just check for 123058333Seric ** a "reasonable" amount. 123158082Seric ** 123258082Seric ** Returns: 123358082Seric ** TRUE if there is enough space. 123458082Seric ** FALSE otherwise. 123558082Seric */ 123658082Seric 123758082Seric #ifndef HASSTATFS 123858082Seric # if defined(BSD4_4) || defined(__osf__) 123958082Seric # define HASSTATFS 124058082Seric # endif 124158082Seric #endif 124258082Seric 124358082Seric #ifdef HASSTATFS 124458157Seric # undef HASUSTAT 124558157Seric #endif 124658157Seric 124758157Seric #if defined(HASUSTAT) 124858157Seric # include <ustat.h> 124958157Seric #endif 125058157Seric 125158157Seric #ifdef HASSTATFS 125258133Seric # if defined(sgi) || defined(apollo) 125358133Seric # include <sys/statfs.h> 125458133Seric # else 125563962Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) 125658133Seric # include <sys/vfs.h> 125758133Seric # else 125858157Seric # include <sys/mount.h> 125958133Seric # endif 126058133Seric # endif 126158082Seric #endif 126258082Seric 126358082Seric bool 126458333Seric enoughspace(msize) 126558333Seric long msize; 126658082Seric { 126758160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 126858157Seric # if defined(HASUSTAT) 126958153Seric struct ustat fs; 127058153Seric struct stat statbuf; 127158366Seric # define FSBLOCKSIZE DEV_BSIZE 127258157Seric # define f_bavail f_tfree 127358157Seric # else 127458157Seric # if defined(ultrix) 127558157Seric struct fs_data fs; 127658157Seric # define f_bavail fd_bfreen 127758366Seric # define FSBLOCKSIZE fs.fd_bsize 127858153Seric # else 127958082Seric struct statfs fs; 128058366Seric # define FSBLOCKSIZE fs.f_bsize 128158153Seric # endif 128258133Seric # endif 128358082Seric extern int errno; 128458082Seric 128558333Seric if (MinBlocksFree <= 0 && msize <= 0) 128658082Seric { 128758082Seric if (tTd(4, 80)) 128858133Seric printf("enoughspace: no threshold\n"); 128958133Seric return TRUE; 129058133Seric } 129158333Seric 129258157Seric # if defined(HASUSTAT) 129358157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 129458157Seric # else 129558157Seric # if defined(sgi) || defined(apollo) 129658133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 129758157Seric # else 129858157Seric # if defined(ultrix) 129958133Seric if (statfs(QueueDir, &fs) > 0) 130058153Seric # else 130158133Seric if (statfs(QueueDir, &fs) == 0) 130258153Seric # endif 130358133Seric # endif 130458133Seric # endif 130558133Seric { 130658133Seric if (tTd(4, 80)) 130758333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 130858333Seric fs.f_bavail, msize); 130958333Seric 131058333Seric /* convert msize to block count */ 131158366Seric msize = msize / FSBLOCKSIZE + 1; 131258333Seric if (MinBlocksFree >= 0) 131358333Seric msize += MinBlocksFree; 131458333Seric 131558333Seric if (fs.f_bavail < msize) 131658090Seric { 131758090Seric #ifdef LOG 131858090Seric if (LogLevel > 0) 131958090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 132058333Seric QueueDir, fs.f_bavail, msize); 132158090Seric #endif 132258082Seric return FALSE; 132358090Seric } 132458082Seric } 132558082Seric else if (tTd(4, 80)) 132658333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 132758333Seric MinBlocksFree, msize, errstring(errno)); 132858082Seric #endif 132958082Seric return TRUE; 133058082Seric } 133158542Seric /* 133258542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 133358542Seric ** 133458542Seric ** This looks at an errno value and tells if this is likely to 133558542Seric ** go away if retried later. 133658542Seric ** 133758542Seric ** Parameters: 133858542Seric ** err -- the errno code to classify. 133958542Seric ** 134058542Seric ** Returns: 134158542Seric ** TRUE if this is probably transient. 134258542Seric ** FALSE otherwise. 134358542Seric */ 134458542Seric 134558542Seric bool 134658542Seric transienterror(err) 134758542Seric int err; 134858542Seric { 134958542Seric switch (err) 135058542Seric { 135158542Seric case EIO: /* I/O error */ 135258542Seric case ENXIO: /* Device not configured */ 135358542Seric case EAGAIN: /* Resource temporarily unavailable */ 135458542Seric case ENOMEM: /* Cannot allocate memory */ 135558542Seric case ENODEV: /* Operation not supported by device */ 135658542Seric case ENFILE: /* Too many open files in system */ 135758542Seric case EMFILE: /* Too many open files */ 135858542Seric case ENOSPC: /* No space left on device */ 135958542Seric #ifdef ETIMEDOUT 136058542Seric case ETIMEDOUT: /* Connection timed out */ 136158542Seric #endif 136258542Seric #ifdef ESTALE 136358542Seric case ESTALE: /* Stale NFS file handle */ 136458542Seric #endif 136558542Seric #ifdef ENETDOWN 136658542Seric case ENETDOWN: /* Network is down */ 136758542Seric #endif 136858542Seric #ifdef ENETUNREACH 136958542Seric case ENETUNREACH: /* Network is unreachable */ 137058542Seric #endif 137158542Seric #ifdef ENETRESET 137258542Seric case ENETRESET: /* Network dropped connection on reset */ 137358542Seric #endif 137458542Seric #ifdef ECONNABORTED 137558542Seric case ECONNABORTED: /* Software caused connection abort */ 137658542Seric #endif 137758542Seric #ifdef ECONNRESET 137858542Seric case ECONNRESET: /* Connection reset by peer */ 137958542Seric #endif 138058542Seric #ifdef ENOBUFS 138158542Seric case ENOBUFS: /* No buffer space available */ 138258542Seric #endif 138358542Seric #ifdef ESHUTDOWN 138458542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 138558542Seric #endif 138658542Seric #ifdef ECONNREFUSED 138758542Seric case ECONNREFUSED: /* Connection refused */ 138858542Seric #endif 138958542Seric #ifdef EHOSTDOWN 139058542Seric case EHOSTDOWN: /* Host is down */ 139158542Seric #endif 139258542Seric #ifdef EHOSTUNREACH 139358542Seric case EHOSTUNREACH: /* No route to host */ 139458542Seric #endif 139558542Seric #ifdef EDQUOT 139658542Seric case EDQUOT: /* Disc quota exceeded */ 139758542Seric #endif 139858542Seric #ifdef EPROCLIM 139958542Seric case EPROCLIM: /* Too many processes */ 140058542Seric #endif 140158542Seric #ifdef EUSERS 140258542Seric case EUSERS: /* Too many users */ 140358542Seric #endif 140458542Seric #ifdef EDEADLK 140558542Seric case EDEADLK: /* Resource deadlock avoided */ 140658542Seric #endif 140758542Seric #ifdef EISCONN 140858542Seric case EISCONN: /* Socket already connected */ 140958542Seric #endif 141058542Seric #ifdef EINPROGRESS 141158542Seric case EINPROGRESS: /* Operation now in progress */ 141258542Seric #endif 141358542Seric #ifdef EALREADY 141458542Seric case EALREADY: /* Operation already in progress */ 141558542Seric #endif 141658542Seric #ifdef EADDRINUSE 141758542Seric case EADDRINUSE: /* Address already in use */ 141858542Seric #endif 141958542Seric #ifdef EADDRNOTAVAIL 142058542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 142158542Seric #endif 142263834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 142358542Seric case ENOSR: /* Out of streams resources */ 142458542Seric #endif 142558542Seric return TRUE; 142658542Seric } 142758542Seric 142858542Seric /* nope, must be permanent */ 142958542Seric return FALSE; 143058542Seric } 143158689Seric /* 143258689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 143358689Seric ** 143458689Seric ** Parameters: 143558689Seric ** fd -- the file descriptor of the file. 143658689Seric ** filename -- the file name (for error messages). 143758689Seric ** type -- type of the lock. Bits can be: 143858689Seric ** LOCK_EX -- exclusive lock. 143958689Seric ** LOCK_NB -- non-blocking. 144058689Seric ** 144158689Seric ** Returns: 144258689Seric ** TRUE if the lock was acquired. 144358689Seric ** FALSE otherwise. 144458689Seric */ 144558689Seric 144658689Seric bool 144758689Seric lockfile(fd, filename, type) 144858689Seric int fd; 144958689Seric char *filename; 145058689Seric int type; 145158689Seric { 145258689Seric # ifdef LOCKF 145358689Seric int action; 145458689Seric struct flock lfd; 145558689Seric 145659447Seric if (bitset(LOCK_UN, type)) 145759447Seric lfd.l_type = F_UNLCK; 145859447Seric else if (bitset(LOCK_EX, type)) 145958689Seric lfd.l_type = F_WRLCK; 146058689Seric else 146158689Seric lfd.l_type = F_RDLCK; 146258689Seric 146358689Seric if (bitset(LOCK_NB, type)) 146458689Seric action = F_SETLK; 146558689Seric else 146658689Seric action = F_SETLKW; 146758689Seric 146858689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 146958689Seric 147058689Seric if (fcntl(fd, action, &lfd) >= 0) 147158689Seric return TRUE; 147258689Seric 147358689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 147459447Seric syserr("cannot lockf(%s, %o)", filename, type); 147558689Seric # else 147658689Seric if (flock(fd, type) >= 0) 147758689Seric return TRUE; 147858689Seric 147958689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 148059447Seric syserr("cannot flock(%s, %o)", filename, type); 148158689Seric # endif 148258689Seric return FALSE; 148358689Seric } 1484