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*64264Seric static char sccsid[] = "@(#)conf.c 8.19 (Berkeley) 08/15/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 */ 10540980Sbostic char *FreezeFile = _PATH_SENDMAILFC; /* frozen version of above */ 10658082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1079039Seric 1089064Seric 1099064Seric 1109039Seric /* 11158082Seric ** Privacy values 11258082Seric */ 11358082Seric 11458082Seric struct prival PrivacyValues[] = 11558082Seric { 11658082Seric "public", PRIV_PUBLIC, 11758082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11858114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11958082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12058082Seric "noexpn", PRIV_NOEXPN, 12158082Seric "novrfy", PRIV_NOVRFY, 12258249Seric "restrictmailq", PRIV_RESTRMAILQ, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12458082Seric "goaway", PRIV_GOAWAY, 12558789Seric NULL, 0, 12658082Seric }; 12758082Seric 12858082Seric 12958082Seric 13058082Seric /* 13124943Seric ** Miscellaneous stuff. 1329039Seric */ 1339039Seric 13424943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13524943Seric /* 13624943Seric ** SETDEFAULTS -- set default values 13724943Seric ** 13824943Seric ** Because of the way freezing is done, these must be initialized 13924943Seric ** using direct code. 14024943Seric ** 14124943Seric ** Parameters: 14258734Seric ** e -- the default envelope. 14324943Seric ** 14424943Seric ** Returns: 14524943Seric ** none. 14624943Seric ** 14724943Seric ** Side Effects: 14824943Seric ** Initializes a bunch of global variables to their 14924943Seric ** default values. 15024943Seric */ 15124943Seric 15258737Seric #define DAYS * 24 * 60 * 60 15358737Seric 15458734Seric setdefaults(e) 15558734Seric register ENVELOPE *e; 15624943Seric { 15757438Seric SpaceSub = ' '; /* option B */ 15857438Seric QueueLA = 8; /* option x */ 15957438Seric RefuseLA = 12; /* option X */ 16057438Seric WkRecipFact = 30000L; /* option y */ 16157438Seric WkClassFact = 1800L; /* option z */ 16257438Seric WkTimeFact = 90000L; /* option Z */ 16357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16463787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 16557438Seric /* option F */ 16657438Seric DefUid = 1; /* option u */ 16757438Seric DefGid = 1; /* option g */ 16857438Seric CheckpointInterval = 10; /* option C */ 16957438Seric MaxHopCount = 25; /* option h */ 17058734Seric e->e_sendmode = SM_FORK; /* option d */ 17158734Seric e->e_errormode = EM_PRINT; /* option e */ 17259709Seric SevenBit = FALSE; /* option 7 */ 17357438Seric MaxMciCache = 1; /* option k */ 17457438Seric MciCacheTimeout = 300; /* option K */ 17557438Seric LogLevel = 9; /* option L */ 17658112Seric settimeouts(NULL); /* option r */ 17758737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17858737Seric TimeOuts.to_q_warning = 0; /* option T */ 17958853Seric PrivacyFlags = 0; /* option p */ 18040973Sbostic setdefuser(); 18153654Seric setupmaps(); 18257402Seric setupmailers(); 18324943Seric } 184294Seric 18540973Sbostic 1864326Seric /* 18740973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 18840973Sbostic */ 18940973Sbostic 19040973Sbostic setdefuser() 19140973Sbostic { 19240973Sbostic struct passwd *defpwent; 19357386Seric static char defuserbuf[40]; 19440973Sbostic 19557386Seric DefUser = defuserbuf; 19640973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19757386Seric strcpy(defuserbuf, defpwent->pw_name); 19840973Sbostic else 19957386Seric strcpy(defuserbuf, "nobody"); 20040973Sbostic } 20153654Seric /* 20256836Seric ** HOST_MAP_INIT -- initialize host class structures 20356836Seric */ 20456836Seric 20556836Seric bool 20660219Seric host_map_init(map, args) 20756836Seric MAP *map; 20856836Seric char *args; 20956836Seric { 21056836Seric register char *p = args; 21156836Seric 21256836Seric for (;;) 21356836Seric { 21458050Seric while (isascii(*p) && isspace(*p)) 21556836Seric p++; 21656836Seric if (*p != '-') 21756836Seric break; 21856836Seric switch (*++p) 21956836Seric { 22056836Seric case 'a': 22156836Seric map->map_app = ++p; 22256836Seric break; 22356836Seric } 22458050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 22556836Seric p++; 22656836Seric if (*p != '\0') 22756836Seric *p++ = '\0'; 22856836Seric } 22956836Seric if (map->map_app != NULL) 23056836Seric map->map_app = newstr(map->map_app); 23156836Seric return TRUE; 23256836Seric } 23357402Seric /* 23457402Seric ** SETUPMAILERS -- initialize default mailers 23557402Seric */ 23656836Seric 23757402Seric setupmailers() 23857402Seric { 23957402Seric char buf[100]; 24057402Seric 24157403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 24257403Seric makemailer(buf); 24357403Seric 24459883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 24557402Seric makemailer(buf); 24657402Seric 24757402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 24857402Seric makemailer(buf); 24957402Seric } 25056836Seric /* 25160207Seric ** SETUPMAPS -- set up map classes 25260207Seric */ 25360207Seric 25460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 25560207Seric { \ 25660207Seric extern bool parse __P((MAP *, char *)); \ 25760207Seric extern bool open __P((MAP *, int)); \ 25860207Seric extern void close __P((MAP *)); \ 25960207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 26060207Seric extern void store __P((MAP *, char *, char *)); \ 26160207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 26260207Seric s->s_mapclass.map_cname = name; \ 26360207Seric s->s_mapclass.map_ext = ext; \ 26460207Seric s->s_mapclass.map_cflags = flags; \ 26560207Seric s->s_mapclass.map_parse = parse; \ 26660207Seric s->s_mapclass.map_open = open; \ 26760207Seric s->s_mapclass.map_close = close; \ 26860207Seric s->s_mapclass.map_lookup = lookup; \ 26960207Seric s->s_mapclass.map_store = store; \ 27060207Seric } 27160207Seric 27260207Seric setupmaps() 27360207Seric { 27460207Seric register STAB *s; 27560207Seric 27660207Seric #ifdef NEWDB 27760207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 27860207Seric map_parseargs, hash_map_open, db_map_close, 27960207Seric db_map_lookup, db_map_store); 28060207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 28160207Seric map_parseargs, bt_map_open, db_map_close, 28260207Seric db_map_lookup, db_map_store); 28360207Seric #endif 28460207Seric 28560207Seric #ifdef NDBM 28660207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 28760207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 28860207Seric ndbm_map_lookup, ndbm_map_store); 28960207Seric #endif 29060207Seric 29160207Seric #ifdef NIS 29260207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 29360207Seric map_parseargs, nis_map_open, nis_map_close, 29460207Seric nis_map_lookup, nis_map_store); 29560207Seric #endif 29660207Seric 29760207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 29860207Seric map_parseargs, stab_map_open, stab_map_close, 29960207Seric stab_map_lookup, stab_map_store); 30060207Seric 30160207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 30260207Seric map_parseargs, impl_map_open, impl_map_close, 30360207Seric impl_map_lookup, impl_map_store); 30460207Seric 30560207Seric /* host DNS lookup */ 30660207Seric MAPDEF("host", NULL, 0, 30760207Seric host_map_init, null_map_open, null_map_close, 30860207Seric host_map_lookup, null_map_store); 30960207Seric 31060207Seric /* dequote map */ 31160207Seric MAPDEF("dequote", NULL, 0, 31260207Seric dequote_init, null_map_open, null_map_close, 31360207Seric dequote_map, null_map_store); 31460207Seric 31560207Seric #if 0 31660207Seric # ifdef USERDB 31760207Seric /* user database */ 31860207Seric MAPDEF("udb", ".db", 0, 31960207Seric udb_map_parse, null_map_open, null_map_close, 32060207Seric udb_map_lookup, null_map_store); 32160207Seric # endif 32260207Seric #endif 32360207Seric } 32460207Seric 32560207Seric #undef MAPDEF 32660207Seric /* 3279369Seric ** USERNAME -- return the user id of the logged in user. 3289369Seric ** 3299369Seric ** Parameters: 3309369Seric ** none. 3319369Seric ** 3329369Seric ** Returns: 3339369Seric ** The login name of the logged in user. 3349369Seric ** 3359369Seric ** Side Effects: 3369369Seric ** none. 3379369Seric ** 3389369Seric ** Notes: 3399369Seric ** The return value is statically allocated. 3409369Seric */ 3419369Seric 3429369Seric char * 3439369Seric username() 3449369Seric { 34517469Seric static char *myname = NULL; 3469369Seric extern char *getlogin(); 34719904Smiriam register struct passwd *pw; 3489369Seric 34917469Seric /* cache the result */ 35017469Seric if (myname == NULL) 35117469Seric { 35217469Seric myname = getlogin(); 35317469Seric if (myname == NULL || myname[0] == '\0') 35417469Seric { 35563787Seric pw = getpwuid(RealUid); 35617469Seric if (pw != NULL) 35740993Sbostic myname = newstr(pw->pw_name); 35817469Seric } 35919904Smiriam else 36019904Smiriam { 36163787Seric uid_t uid = RealUid; 36219873Smiriam 36340993Sbostic myname = newstr(myname); 36440993Sbostic if ((pw = getpwnam(myname)) == NULL || 36558736Seric (uid != 0 && uid != pw->pw_uid)) 36619904Smiriam { 36758736Seric pw = getpwuid(uid); 36824945Seric if (pw != NULL) 36940993Sbostic myname = newstr(pw->pw_name); 37019873Smiriam } 37119873Smiriam } 37217469Seric if (myname == NULL || myname[0] == '\0') 37317469Seric { 37458151Seric syserr("554 Who are you?"); 37517469Seric myname = "postmaster"; 37617469Seric } 37717469Seric } 37817469Seric 37917469Seric return (myname); 3809369Seric } 3819369Seric /* 3824190Seric ** TTYPATH -- Get the path of the user's tty 383294Seric ** 384294Seric ** Returns the pathname of the user's tty. Returns NULL if 385294Seric ** the user is not logged in or if s/he has write permission 386294Seric ** denied. 387294Seric ** 388294Seric ** Parameters: 389294Seric ** none 390294Seric ** 391294Seric ** Returns: 392294Seric ** pathname of the user's tty. 393294Seric ** NULL if not logged in or write permission denied. 394294Seric ** 395294Seric ** Side Effects: 396294Seric ** none. 397294Seric ** 398294Seric ** WARNING: 399294Seric ** Return value is in a local buffer. 400294Seric ** 401294Seric ** Called By: 402294Seric ** savemail 403294Seric */ 404294Seric 405294Seric char * 406294Seric ttypath() 407294Seric { 408294Seric struct stat stbuf; 409294Seric register char *pathn; 410294Seric extern char *ttyname(); 4114081Seric extern char *getlogin(); 412294Seric 413294Seric /* compute the pathname of the controlling tty */ 4149369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4159369Seric (pathn = ttyname(0)) == NULL) 416294Seric { 417294Seric errno = 0; 418294Seric return (NULL); 419294Seric } 420294Seric 421294Seric /* see if we have write permission */ 4222967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 423294Seric { 424294Seric errno = 0; 425294Seric return (NULL); 426294Seric } 427294Seric 428294Seric /* see if the user is logged in */ 429294Seric if (getlogin() == NULL) 430294Seric return (NULL); 431294Seric 432294Seric /* looks good */ 433294Seric return (pathn); 434294Seric } 4352967Seric /* 4362967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4372967Seric ** 4382967Seric ** This routine can be supplied on a per-installation basis 4392967Seric ** to determine whether a person is allowed to send a message. 4402967Seric ** This allows restriction of certain types of internet 4412967Seric ** forwarding or registration of users. 4422967Seric ** 4432967Seric ** If the hosts are found to be incompatible, an error 44457454Seric ** message should be given using "usrerr" and 0 should 4452967Seric ** be returned. 4462967Seric ** 4474288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4484288Seric ** function; this should be done on huge messages. 4494288Seric ** 4502967Seric ** Parameters: 4512967Seric ** to -- the person being sent to. 4522967Seric ** 4532967Seric ** Returns: 45457459Seric ** an exit status 4552967Seric ** 4562967Seric ** Side Effects: 4572967Seric ** none (unless you include the usrerr stuff) 4582967Seric */ 4592967Seric 46055012Seric checkcompat(to, e) 4612967Seric register ADDRESS *to; 46255012Seric register ENVELOPE *e; 4632967Seric { 46412133Seric # ifdef lint 46512133Seric if (to == NULL) 46612133Seric to++; 46763834Seric # endif /* lint */ 46810698Seric # ifdef EXAMPLE_CODE 46910698Seric /* this code is intended as an example only */ 4704437Seric register STAB *s; 4714437Seric 4724437Seric s = stab("arpa", ST_MAILER, ST_FIND); 47355012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4749369Seric to->q_mailer == s->s_mailer) 4754437Seric { 47658151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 47710698Seric /* NoReturn = TRUE; to supress return copy */ 47857459Seric return (EX_UNAVAILABLE); 4794437Seric } 48056795Seric # endif /* EXAMPLE_CODE */ 48157459Seric return (EX_OK); 4822967Seric } 4839369Seric /* 48464035Seric ** SETSIGNAL -- set a signal handler 48564035Seric ** 48664035Seric ** This is essentially old BSD "signal(3)". 48764035Seric */ 48864035Seric 48964035Seric setsig_t 49064035Seric setsignal(sig, handler) 49164035Seric int sig; 49264035Seric setsig_t handler; 49364035Seric { 49464072Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) 49564035Seric return signal(sig, handler); 49664035Seric #else 49764035Seric struct sigaction n, o; 49864035Seric 49964035Seric bzero(&n, sizeof n); 50064035Seric n.sa_handler = handler; 50164035Seric if (sigaction(sig, &n, &o) < 0) 50264035Seric return SIG_ERR; 50364035Seric return o.sa_handler; 50464035Seric #endif 50564035Seric } 50664035Seric /* 5079369Seric ** HOLDSIGS -- arrange to hold all signals 5089369Seric ** 5099369Seric ** Parameters: 5109369Seric ** none. 5119369Seric ** 5129369Seric ** Returns: 5139369Seric ** none. 5149369Seric ** 5159369Seric ** Side Effects: 5169369Seric ** Arranges that signals are held. 5179369Seric */ 5189369Seric 5199369Seric holdsigs() 5209369Seric { 5219369Seric } 5229369Seric /* 5239369Seric ** RLSESIGS -- arrange to release all signals 5249369Seric ** 5259369Seric ** This undoes the effect of holdsigs. 5269369Seric ** 5279369Seric ** Parameters: 5289369Seric ** none. 5299369Seric ** 5309369Seric ** Returns: 5319369Seric ** none. 5329369Seric ** 5339369Seric ** Side Effects: 5349369Seric ** Arranges that signals are released. 5359369Seric */ 5369369Seric 5379369Seric rlsesigs() 5389369Seric { 5399369Seric } 54014872Seric /* 54114872Seric ** GETLA -- get the current load average 54214872Seric ** 54314881Seric ** This code stolen from la.c. 54414881Seric ** 54514872Seric ** Parameters: 54614872Seric ** none. 54714872Seric ** 54814872Seric ** Returns: 54914872Seric ** The current load average as an integer. 55014872Seric ** 55114872Seric ** Side Effects: 55214872Seric ** none. 55314872Seric */ 55414872Seric 55551920Seric /* try to guess what style of load average we have */ 55651920Seric #define LA_ZERO 1 /* always return load average as zero */ 55751920Seric #define LA_INT 2 /* read kmem for avenrun; interpret as int */ 55851920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 55951920Seric #define LA_SUBR 4 /* call getloadavg */ 56014872Seric 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 70351773Seric 70451773Seric getla() 70551773Seric { 70657736Seric if (tTd(3, 1)) 70757736Seric printf("getla: ZERO\n"); 70851920Seric return (0); 70951773Seric } 71051773Seric 71151773Seric #endif 71251773Seric #endif 71324943Seric /* 71424943Seric ** SHOULDQUEUE -- should this message be queued or sent? 71524943Seric ** 71624943Seric ** Compares the message cost to the load average to decide. 71724943Seric ** 71824943Seric ** Parameters: 71924943Seric ** pri -- the priority of the message in question. 72057438Seric ** ctime -- the message creation time. 72124943Seric ** 72224943Seric ** Returns: 72324943Seric ** TRUE -- if this message should be queued up for the 72424943Seric ** time being. 72524943Seric ** FALSE -- if the load is low enough to send this message. 72624943Seric ** 72724943Seric ** Side Effects: 72824943Seric ** none. 72924943Seric */ 73024943Seric 73124943Seric bool 73257438Seric shouldqueue(pri, ctime) 73324943Seric long pri; 73457438Seric time_t ctime; 73524943Seric { 73651920Seric if (CurrentLA < QueueLA) 73724943Seric return (FALSE); 73858132Seric if (CurrentLA >= RefuseLA) 73958132Seric return (TRUE); 74051920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 74124943Seric } 74224943Seric /* 74353037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 74453037Seric ** 74553037Seric ** Parameters: 74653037Seric ** none. 74753037Seric ** 74853037Seric ** Returns: 74953037Seric ** TRUE if incoming SMTP connections should be refused 75053037Seric ** (for now). 75153037Seric ** FALSE if we should accept new work. 75253037Seric ** 75353037Seric ** Side Effects: 75453037Seric ** none. 75553037Seric */ 75653037Seric 75753037Seric bool 75853037Seric refuseconnections() 75953037Seric { 76059156Seric #ifdef XLA 76159156Seric if (!xla_smtp_ok()) 76259156Seric return TRUE; 76359156Seric #endif 76459156Seric 76553037Seric /* this is probably too simplistic */ 76658132Seric return (CurrentLA >= RefuseLA); 76753037Seric } 76853037Seric /* 76924943Seric ** SETPROCTITLE -- set process title for ps 77024943Seric ** 77124943Seric ** Parameters: 77258674Seric ** fmt -- a printf style format string. 77358674Seric ** a, b, c -- possible parameters to fmt. 77424943Seric ** 77524943Seric ** Returns: 77624943Seric ** none. 77724943Seric ** 77824943Seric ** Side Effects: 77924943Seric ** Clobbers argv of our main procedure so ps(1) will 78024943Seric ** display the title. 78124943Seric */ 78224943Seric 78358689Seric #ifdef SETPROCTITLE 78458689Seric # ifdef __hpux 78558689Seric # include <sys/pstat.h> 78658689Seric # endif 78759732Seric # ifdef BSD4_4 78859732Seric # include <machine/vmparam.h> 78959732Seric # include <sys/exec.h> 79064247Seric # ifdef __bsdi__ 79164247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 79264247Seric # endif 79359992Seric # ifdef PS_STRINGS 79459992Seric # define SETPROC_STATIC static 79559992Seric # endif 79659732Seric # endif 79759732Seric # ifndef SETPROC_STATIC 79859732Seric # define SETPROC_STATIC 79959732Seric # endif 80058689Seric #endif 80158689Seric 80224943Seric /*VARARGS1*/ 80357642Seric #ifdef __STDC__ 80457642Seric setproctitle(char *fmt, ...) 80557642Seric #else 80657642Seric setproctitle(fmt, va_alist) 80724943Seric char *fmt; 80857642Seric va_dcl 80957642Seric #endif 81024943Seric { 81124943Seric # ifdef SETPROCTITLE 81224943Seric register char *p; 81325049Seric register int i; 81459732Seric SETPROC_STATIC char buf[MAXLINE]; 81556852Seric VA_LOCAL_DECL 81658689Seric # ifdef __hpux 81758689Seric union pstun pst; 81858689Seric # endif 81924943Seric extern char **Argv; 82024943Seric extern char *LastArgv; 82124943Seric 82258674Seric p = buf; 82324943Seric 82458674Seric /* print sendmail: heading for grep */ 82558674Seric (void) strcpy(p, "sendmail: "); 82658674Seric p += strlen(p); 82724943Seric 82858674Seric /* print the argument string */ 82958674Seric VA_START(fmt); 83058674Seric (void) vsprintf(p, fmt, ap); 83156852Seric VA_END; 83254996Seric 83358674Seric i = strlen(buf); 83458689Seric 83558689Seric # ifdef __hpux 83658689Seric pst.pst_command = buf; 83758689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 83858689Seric # else 83959992Seric # ifdef PS_STRINGS 84059732Seric PS_STRINGS->ps_nargvstr = 1; 84159732Seric PS_STRINGS->ps_argvstr = buf; 84259732Seric # else 84354996Seric if (i > LastArgv - Argv[0] - 2) 84425049Seric { 84554996Seric i = LastArgv - Argv[0] - 2; 84658674Seric buf[i] = '\0'; 84725049Seric } 84858674Seric (void) strcpy(Argv[0], buf); 84954997Seric p = &Argv[0][i]; 85024943Seric while (p < LastArgv) 85124943Seric *p++ = ' '; 85259732Seric # endif 85358689Seric # endif 85456795Seric # endif /* SETPROCTITLE */ 85524943Seric } 85625698Seric /* 85725698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 85825698Seric ** 85925698Seric ** Parameters: 86025698Seric ** none. 86125698Seric ** 86225698Seric ** Returns: 86325698Seric ** none. 86425698Seric ** 86525698Seric ** Side Effects: 86625698Seric ** Picks up extant zombies. 86725698Seric */ 86825698Seric 86925698Seric # include <sys/wait.h> 87025698Seric 87146928Sbostic void 87225698Seric reapchild() 87325698Seric { 87463966Seric # ifdef HASWAITPID 87560219Seric auto int status; 87660560Seric int count; 87760560Seric int pid; 87860219Seric 87960560Seric count = 0; 88060560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 88160560Seric { 88260560Seric if (count++ > 1000) 88360560Seric { 88460560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 88560560Seric pid, status); 88660560Seric break; 88760560Seric } 88860560Seric } 88960219Seric # else 89025698Seric # ifdef WNOHANG 89125698Seric union wait status; 89225698Seric 89363753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 89425698Seric continue; 89556795Seric # else /* WNOHANG */ 89625698Seric auto int status; 89725698Seric 89860219Seric while (wait(&status) > 0) 89925698Seric continue; 90056795Seric # endif /* WNOHANG */ 90160219Seric # endif 90264035Seric # ifdef SYS5SIGNALS 90364035Seric (void) setsignal(SIGCHLD, reapchild); 90458061Seric # endif 90525698Seric } 90655418Seric /* 90755418Seric ** UNSETENV -- remove a variable from the environment 90855418Seric ** 90955418Seric ** Not needed on newer systems. 91055418Seric ** 91155418Seric ** Parameters: 91255418Seric ** name -- the string name of the environment variable to be 91355418Seric ** deleted from the current environment. 91455418Seric ** 91555418Seric ** Returns: 91655418Seric ** none. 91755418Seric ** 91855418Seric ** Globals: 91955418Seric ** environ -- a pointer to the current environment. 92055418Seric ** 92155418Seric ** Side Effects: 92255418Seric ** Modifies environ. 92355418Seric */ 92455418Seric 92563962Seric #ifndef HASUNSETENV 92655418Seric 92755418Seric void 92855418Seric unsetenv(name) 92955418Seric char *name; 93055418Seric { 93155418Seric extern char **environ; 93255418Seric register char **pp; 93355418Seric int len = strlen(name); 93455418Seric 93555418Seric for (pp = environ; *pp != NULL; pp++) 93655418Seric { 93755418Seric if (strncmp(name, *pp, len) == 0 && 93855418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 93955418Seric break; 94055418Seric } 94155418Seric 94255418Seric for (; *pp != NULL; pp++) 94355418Seric *pp = pp[1]; 94455418Seric } 94555418Seric 94663962Seric #endif 94756215Seric /* 94856215Seric ** GETDTABLESIZE -- return number of file descriptors 94956215Seric ** 95056215Seric ** Only on non-BSD systems 95156215Seric ** 95256215Seric ** Parameters: 95356215Seric ** none 95456215Seric ** 95556215Seric ** Returns: 95656215Seric ** size of file descriptor table 95756215Seric ** 95856215Seric ** Side Effects: 95956215Seric ** none 96056215Seric */ 96156215Seric 96263787Seric #ifdef SOLARIS 96363787Seric # include <sys/resource.h> 96463787Seric #endif 96556215Seric 96656215Seric int 96763787Seric getdtsize() 96856215Seric { 96963787Seric #ifdef RLIMIT_NOFILE 97063787Seric struct rlimit rl; 97163787Seric 97263787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 97363787Seric return rl.rlim_cur; 97463787Seric #endif 97563787Seric 97664031Seric # ifdef HASGETDTABLESIZE 97764031Seric return getdtablesize(); 97864031Seric # else 97964031Seric # ifdef _SC_OPEN_MAX 98058689Seric return sysconf(_SC_OPEN_MAX); 98163787Seric # else 98256215Seric return NOFILE; 98363787Seric # endif 98458689Seric # endif 98556215Seric } 98657631Seric /* 98757631Seric ** UNAME -- get the UUCP name of this system. 98857631Seric */ 98957631Seric 99057943Seric #ifndef HASUNAME 99157631Seric 99257631Seric int 99357631Seric uname(name) 99457631Seric struct utsname *name; 99557631Seric { 99657631Seric FILE *file; 99757631Seric char *n; 99857631Seric 99957631Seric name->nodename[0] = '\0'; 100057631Seric 100157661Seric /* try /etc/whoami -- one line with the node name */ 100257631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 100357631Seric { 100457661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 100557631Seric (void) fclose(file); 100657661Seric n = strchr(name->nodename, '\n'); 100757631Seric if (n != NULL) 100857631Seric *n = '\0'; 100957631Seric if (name->nodename[0] != '\0') 101057631Seric return (0); 101157631Seric } 101257631Seric 101357661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 101457631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 101557631Seric { 101657631Seric char buf[MAXLINE]; 101757631Seric 101857631Seric while (fgets(buf, MAXLINE, file) != NULL) 101957631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 102057631Seric NODE_LENGTH, name->nodename) > 0) 102157631Seric break; 102257631Seric (void) fclose(file); 102357631Seric if (name->nodename[0] != '\0') 102457631Seric return (0); 102557631Seric } 102657631Seric 102757631Seric #ifdef TRUST_POPEN 102857631Seric /* 102957631Seric ** Popen is known to have security holes. 103057631Seric */ 103157631Seric 103257661Seric /* try uuname -l to return local name */ 103357631Seric if ((file = popen("uuname -l", "r")) != NULL) 103457631Seric { 103557661Seric (void) fgets(name, NODE_LENGTH + 1, file); 103657631Seric (void) pclose(file); 103757661Seric n = strchr(name, '\n'); 103857631Seric if (n != NULL) 103957631Seric *n = '\0'; 104057661Seric if (name->nodename[0] != '\0') 104157631Seric return (0); 104257631Seric } 104357631Seric #endif 104457631Seric 104557631Seric return (-1); 104657631Seric } 104757943Seric #endif /* HASUNAME */ 104858068Seric /* 104958068Seric ** INITGROUPS -- initialize groups 105058068Seric ** 105158068Seric ** Stub implementation for System V style systems 105258068Seric */ 105358068Seric 105458068Seric #ifndef HASINITGROUPS 105558068Seric 105658068Seric initgroups(name, basegid) 105758068Seric char *name; 105858068Seric int basegid; 105958068Seric { 106058068Seric return 0; 106158068Seric } 106258068Seric 106358068Seric #endif 106458082Seric /* 106559289Seric ** SETSID -- set session id (for non-POSIX systems) 106659289Seric */ 106759289Seric 106859289Seric #ifndef HASSETSID 106959289Seric 107063753Seric pid_t 107163753Seric setsid __P ((void)) 107259289Seric { 107364155Seric #ifdef TIOCNOTTY 107464155Seric int fd; 107564155Seric 107664155Seric fd = open("/dev/tty", 2); 107764155Seric if (fd >= 0) 107864155Seric { 107964155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 108064155Seric (void) close(fd); 108164155Seric } 108264155Seric #endif /* TIOCNOTTY */ 108359289Seric # ifdef SYSTEM5 108463753Seric return setpgrp(); 108563753Seric # else 108664155Seric return setpgid(0, getpid()); 108759289Seric # endif 108859289Seric } 108959289Seric 109059289Seric #endif 109159289Seric /* 109263969Seric ** GETOPT -- for old systems or systems with bogus implementations 109363969Seric */ 109463969Seric 109563969Seric #ifdef NEEDGETOPT 109663969Seric 109763969Seric /* 109863969Seric * Copyright (c) 1985 Regents of the University of California. 109963969Seric * All rights reserved. The Berkeley software License Agreement 110063969Seric * specifies the terms and conditions for redistribution. 110163969Seric */ 110263969Seric 110363969Seric 110463969Seric /* 110563969Seric ** this version hacked to add `atend' flag to allow state machine 110663969Seric ** to reset if invoked by the program to scan args for a 2nd time 110763969Seric */ 110863969Seric 110963969Seric #if defined(LIBC_SCCS) && !defined(lint) 111063969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 111164072Seric #endif /* LIBC_SCCS and not lint */ 111263969Seric 111363969Seric #include <stdio.h> 111463969Seric 111563969Seric /* 111663969Seric * get option letter from argument vector 111763969Seric */ 111863969Seric int opterr = 1, /* if error message should be printed */ 111963969Seric optind = 1, /* index into parent argv vector */ 112063969Seric optopt; /* character checked for validity */ 112163969Seric char *optarg; /* argument associated with option */ 112263969Seric 112363969Seric #define BADCH (int)'?' 112463969Seric #define EMSG "" 112563969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 112663969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 112763969Seric 112863969Seric getopt(nargc,nargv,ostr) 112963969Seric int nargc; 113063969Seric char **nargv, 113163969Seric *ostr; 113263969Seric { 113363969Seric static char *place = EMSG; /* option letter processing */ 113463969Seric static char atend = 0; 113563969Seric register char *oli; /* option letter list index */ 113663969Seric 113763969Seric if (atend) { 113863969Seric atend = 0; 113963969Seric place = EMSG; 114063969Seric } 114163969Seric if(!*place) { /* update scanning pointer */ 114264136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 114363969Seric atend++; 114463969Seric return(EOF); 114563969Seric } 114663969Seric if (*place == '-') { /* found "--" */ 114763969Seric ++optind; 114863969Seric atend++; 114963969Seric return(EOF); 115063969Seric } 115163969Seric } /* option letter okay? */ 115264125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 115364136Seric if (!*place) ++optind; 115463969Seric tell(": illegal option -- "); 115563969Seric } 115663969Seric if (*++oli != ':') { /* don't need argument */ 115763969Seric optarg = NULL; 115863969Seric if (!*place) ++optind; 115963969Seric } 116063969Seric else { /* need an argument */ 116163969Seric if (*place) optarg = place; /* no white space */ 116263969Seric else if (nargc <= ++optind) { /* no arg */ 116363969Seric place = EMSG; 116463969Seric tell(": option requires an argument -- "); 116563969Seric } 116663969Seric else optarg = nargv[optind]; /* white space */ 116763969Seric place = EMSG; 116863969Seric ++optind; 116963969Seric } 117063969Seric return(optopt); /* dump back option letter */ 117163969Seric } 117263969Seric 117363969Seric #endif 117463969Seric /* 117563969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 117663969Seric */ 117763969Seric 117863969Seric #ifdef NEEDVPRINTF 117963969Seric 118063969Seric #define MAXARG 16 118163969Seric 118263969Seric vfprintf(fp, fmt, ap) 118363969Seric FILE * fp; 118463969Seric char * fmt; 118563969Seric char ** ap; 118663969Seric { 118763969Seric char * bp[MAXARG]; 118863969Seric int i = 0; 118963969Seric 119063969Seric while (*ap && i < MAXARG) 119163969Seric bp[i++] = *ap++; 119263969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 119363969Seric bp[4], bp[5], bp[6], bp[7], 119463969Seric bp[8], bp[9], bp[10], bp[11], 119563969Seric bp[12], bp[13], bp[14], bp[15]); 119663969Seric } 119763969Seric 119863969Seric vsprintf(s, fmt, ap) 119963969Seric char * s; 120063969Seric char * fmt; 120163969Seric char ** ap; 120263969Seric { 120363969Seric char * bp[MAXARG]; 120463969Seric int i = 0; 120563969Seric 120663969Seric while (*ap && i < MAXARG) 120763969Seric bp[i++] = *ap++; 120863969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 120963969Seric bp[4], bp[5], bp[6], bp[7], 121063969Seric bp[8], bp[9], bp[10], bp[11], 121163969Seric bp[12], bp[13], bp[14], bp[15]); 121263969Seric } 121363969Seric 121463969Seric #endif 121563969Seric /* 121658082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 121758082Seric ** 121858082Seric ** Only implemented if you have statfs. 121958082Seric ** 122058082Seric ** Parameters: 122158333Seric ** msize -- the size to check against. If zero, we don't yet 122258333Seric ** know how big the message will be, so just check for 122358333Seric ** a "reasonable" amount. 122458082Seric ** 122558082Seric ** Returns: 122658082Seric ** TRUE if there is enough space. 122758082Seric ** FALSE otherwise. 122858082Seric */ 122958082Seric 123058082Seric #ifndef HASSTATFS 123158082Seric # if defined(BSD4_4) || defined(__osf__) 123258082Seric # define HASSTATFS 123358082Seric # endif 123458082Seric #endif 123558082Seric 123658082Seric #ifdef HASSTATFS 123758157Seric # undef HASUSTAT 123858157Seric #endif 123958157Seric 124058157Seric #if defined(HASUSTAT) 124158157Seric # include <ustat.h> 124258157Seric #endif 124358157Seric 124458157Seric #ifdef HASSTATFS 124563987Seric # if defined(sgi) || defined(apollo) || defined(_SCO_unix_) 124658133Seric # include <sys/statfs.h> 124758133Seric # else 124863962Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) 124958133Seric # include <sys/vfs.h> 125058133Seric # else 125158157Seric # include <sys/mount.h> 125258133Seric # endif 125358133Seric # endif 125458082Seric #endif 125558082Seric 125658082Seric bool 125758333Seric enoughspace(msize) 125858333Seric long msize; 125958082Seric { 126058160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 126158157Seric # if defined(HASUSTAT) 126258153Seric struct ustat fs; 126358153Seric struct stat statbuf; 126458366Seric # define FSBLOCKSIZE DEV_BSIZE 126558157Seric # define f_bavail f_tfree 126658157Seric # else 126758157Seric # if defined(ultrix) 126858157Seric struct fs_data fs; 126958157Seric # define f_bavail fd_bfreen 127058366Seric # define FSBLOCKSIZE fs.fd_bsize 127158153Seric # else 127258082Seric struct statfs fs; 127358366Seric # define FSBLOCKSIZE fs.f_bsize 127464072Seric # if defined(_SCO_unix_) 127563987Seric # define f_bavail f_bfree 127663987Seric # endif 127758153Seric # endif 127858133Seric # endif 127958082Seric extern int errno; 128058082Seric 128158333Seric if (MinBlocksFree <= 0 && msize <= 0) 128258082Seric { 128358082Seric if (tTd(4, 80)) 128458133Seric printf("enoughspace: no threshold\n"); 128558133Seric return TRUE; 128658133Seric } 128758333Seric 128864254Seric if (msize < 0) 128964254Seric msize = 0; 129064254Seric 129158157Seric # if defined(HASUSTAT) 129258157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 129358157Seric # else 129458157Seric # if defined(sgi) || defined(apollo) 129558133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 129658157Seric # else 129758157Seric # if defined(ultrix) 129858133Seric if (statfs(QueueDir, &fs) > 0) 129958153Seric # else 130058133Seric if (statfs(QueueDir, &fs) == 0) 130158153Seric # endif 130258133Seric # endif 130358133Seric # endif 130458133Seric { 130558133Seric if (tTd(4, 80)) 130658333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 130758333Seric fs.f_bavail, msize); 130858333Seric 130958333Seric /* convert msize to block count */ 131058366Seric msize = msize / FSBLOCKSIZE + 1; 131158333Seric if (MinBlocksFree >= 0) 131258333Seric msize += MinBlocksFree; 131358333Seric 131458333Seric if (fs.f_bavail < msize) 131558090Seric { 131658090Seric #ifdef LOG 131758090Seric if (LogLevel > 0) 131864254Seric syslog(LOG_ALERT, 131964254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 132064254Seric CurEnv->e_id, fs.f_bavail, 132164254Seric CurHostName, msize, QueueDir); 132258090Seric #endif 132358082Seric return FALSE; 132458090Seric } 132558082Seric } 132658082Seric else if (tTd(4, 80)) 132758333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 132858333Seric MinBlocksFree, msize, errstring(errno)); 132958082Seric #endif 133058082Seric return TRUE; 133158082Seric } 133258542Seric /* 133358542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 133458542Seric ** 133558542Seric ** This looks at an errno value and tells if this is likely to 133658542Seric ** go away if retried later. 133758542Seric ** 133858542Seric ** Parameters: 133958542Seric ** err -- the errno code to classify. 134058542Seric ** 134158542Seric ** Returns: 134258542Seric ** TRUE if this is probably transient. 134358542Seric ** FALSE otherwise. 134458542Seric */ 134558542Seric 134658542Seric bool 134758542Seric transienterror(err) 134858542Seric int err; 134958542Seric { 135058542Seric switch (err) 135158542Seric { 135258542Seric case EIO: /* I/O error */ 135358542Seric case ENXIO: /* Device not configured */ 135458542Seric case EAGAIN: /* Resource temporarily unavailable */ 135558542Seric case ENOMEM: /* Cannot allocate memory */ 135658542Seric case ENODEV: /* Operation not supported by device */ 135758542Seric case ENFILE: /* Too many open files in system */ 135858542Seric case EMFILE: /* Too many open files */ 135958542Seric case ENOSPC: /* No space left on device */ 136058542Seric #ifdef ETIMEDOUT 136158542Seric case ETIMEDOUT: /* Connection timed out */ 136258542Seric #endif 136358542Seric #ifdef ESTALE 136458542Seric case ESTALE: /* Stale NFS file handle */ 136558542Seric #endif 136658542Seric #ifdef ENETDOWN 136758542Seric case ENETDOWN: /* Network is down */ 136858542Seric #endif 136958542Seric #ifdef ENETUNREACH 137058542Seric case ENETUNREACH: /* Network is unreachable */ 137158542Seric #endif 137258542Seric #ifdef ENETRESET 137358542Seric case ENETRESET: /* Network dropped connection on reset */ 137458542Seric #endif 137558542Seric #ifdef ECONNABORTED 137658542Seric case ECONNABORTED: /* Software caused connection abort */ 137758542Seric #endif 137858542Seric #ifdef ECONNRESET 137958542Seric case ECONNRESET: /* Connection reset by peer */ 138058542Seric #endif 138158542Seric #ifdef ENOBUFS 138258542Seric case ENOBUFS: /* No buffer space available */ 138358542Seric #endif 138458542Seric #ifdef ESHUTDOWN 138558542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 138658542Seric #endif 138758542Seric #ifdef ECONNREFUSED 138858542Seric case ECONNREFUSED: /* Connection refused */ 138958542Seric #endif 139058542Seric #ifdef EHOSTDOWN 139158542Seric case EHOSTDOWN: /* Host is down */ 139258542Seric #endif 139358542Seric #ifdef EHOSTUNREACH 139458542Seric case EHOSTUNREACH: /* No route to host */ 139558542Seric #endif 139658542Seric #ifdef EDQUOT 139758542Seric case EDQUOT: /* Disc quota exceeded */ 139858542Seric #endif 139958542Seric #ifdef EPROCLIM 140058542Seric case EPROCLIM: /* Too many processes */ 140158542Seric #endif 140258542Seric #ifdef EUSERS 140358542Seric case EUSERS: /* Too many users */ 140458542Seric #endif 140558542Seric #ifdef EDEADLK 140658542Seric case EDEADLK: /* Resource deadlock avoided */ 140758542Seric #endif 140858542Seric #ifdef EISCONN 140958542Seric case EISCONN: /* Socket already connected */ 141058542Seric #endif 141158542Seric #ifdef EINPROGRESS 141258542Seric case EINPROGRESS: /* Operation now in progress */ 141358542Seric #endif 141458542Seric #ifdef EALREADY 141558542Seric case EALREADY: /* Operation already in progress */ 141658542Seric #endif 141758542Seric #ifdef EADDRINUSE 141858542Seric case EADDRINUSE: /* Address already in use */ 141958542Seric #endif 142058542Seric #ifdef EADDRNOTAVAIL 142158542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 142258542Seric #endif 142363834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 142458542Seric case ENOSR: /* Out of streams resources */ 142558542Seric #endif 142658542Seric return TRUE; 142758542Seric } 142858542Seric 142958542Seric /* nope, must be permanent */ 143058542Seric return FALSE; 143158542Seric } 143258689Seric /* 143364035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 143458689Seric ** 143558689Seric ** Parameters: 143658689Seric ** fd -- the file descriptor of the file. 143758689Seric ** filename -- the file name (for error messages). 143858689Seric ** type -- type of the lock. Bits can be: 143958689Seric ** LOCK_EX -- exclusive lock. 144058689Seric ** LOCK_NB -- non-blocking. 144158689Seric ** 144258689Seric ** Returns: 144358689Seric ** TRUE if the lock was acquired. 144458689Seric ** FALSE otherwise. 144558689Seric */ 144658689Seric 144758689Seric bool 144858689Seric lockfile(fd, filename, type) 144958689Seric int fd; 145058689Seric char *filename; 145158689Seric int type; 145258689Seric { 145364035Seric # ifndef HASFLOCK 145458689Seric int action; 145558689Seric struct flock lfd; 145664035Seric 145763983Seric bzero(&lfd, sizeof lfd); 145859447Seric if (bitset(LOCK_UN, type)) 145959447Seric lfd.l_type = F_UNLCK; 146059447Seric else if (bitset(LOCK_EX, type)) 146158689Seric lfd.l_type = F_WRLCK; 146258689Seric else 146358689Seric lfd.l_type = F_RDLCK; 146458689Seric 146558689Seric if (bitset(LOCK_NB, type)) 146658689Seric action = F_SETLK; 146758689Seric else 146858689Seric action = F_SETLKW; 146958689Seric 1470*64264Seric if (tTd(55, 60)) 1471*64264Seric printf("lockfile(%s, action=%d, type=%d): ", 1472*64264Seric filename, action, lfd.l_type); 1473*64264Seric 147458689Seric if (fcntl(fd, action, &lfd) >= 0) 1475*64264Seric { 1476*64264Seric if (tTd(55, 60)) 1477*64264Seric printf("SUCCESS\n"); 147858689Seric return TRUE; 1479*64264Seric } 148058689Seric 1481*64264Seric if (tTd(55, 60)) 1482*64264Seric printf("(%s) ", errstring(errno)); 1483*64264Seric 148464136Seric /* 148564136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 148664136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 148764136Seric ** as type "tmp" (that is, served from swap space), the 148864136Seric ** previous fcntl will fail with "Invalid argument" errors. 148964136Seric ** Since this is fairly common during testing, we will assume 149064136Seric ** that this indicates that the lock is successfully grabbed. 149164136Seric */ 149264136Seric 149364136Seric if (errno == EINVAL) 1494*64264Seric { 1495*64264Seric if (tTd(55, 60)) 1496*64264Seric printf("SUCCESS\n"); 149764136Seric return TRUE; 1498*64264Seric } 149964136Seric 150058689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 150159447Seric syserr("cannot lockf(%s, %o)", filename, type); 150258689Seric # else 1503*64264Seric if (tTd(55, 60)) 1504*64264Seric printf("lockfile(%s, type=%o): ", filename, type); 1505*64264Seric 150658689Seric if (flock(fd, type) >= 0) 1507*64264Seric { 1508*64264Seric if (tTd(55, 60)) 1509*64264Seric printf("SUCCESS\n"); 151058689Seric return TRUE; 1511*64264Seric } 151258689Seric 1513*64264Seric if (tTd(55, 60)) 1514*64264Seric printf("(%s) ", errstring(errno)); 1515*64264Seric 151658689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 151759447Seric syserr("cannot flock(%s, %o)", filename, type); 151858689Seric # endif 1519*64264Seric if (tTd(55, 60)) 1520*64264Seric printf("FAIL\n"); 152158689Seric return FALSE; 152258689Seric } 1523