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*64136Seric static char sccsid[] = "@(#)conf.c 8.15 (Berkeley) 08/07/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> 79059992Seric # ifdef PS_STRINGS 79159992Seric # define SETPROC_STATIC static 79259992Seric # endif 79359732Seric # endif 79459732Seric # ifndef SETPROC_STATIC 79559732Seric # define SETPROC_STATIC 79659732Seric # endif 79758689Seric #endif 79858689Seric 79924943Seric /*VARARGS1*/ 80057642Seric #ifdef __STDC__ 80157642Seric setproctitle(char *fmt, ...) 80257642Seric #else 80357642Seric setproctitle(fmt, va_alist) 80424943Seric char *fmt; 80557642Seric va_dcl 80657642Seric #endif 80724943Seric { 80824943Seric # ifdef SETPROCTITLE 80924943Seric register char *p; 81025049Seric register int i; 81159732Seric SETPROC_STATIC char buf[MAXLINE]; 81256852Seric VA_LOCAL_DECL 81358689Seric # ifdef __hpux 81458689Seric union pstun pst; 81558689Seric # endif 81624943Seric extern char **Argv; 81724943Seric extern char *LastArgv; 81824943Seric 81958674Seric p = buf; 82024943Seric 82158674Seric /* print sendmail: heading for grep */ 82258674Seric (void) strcpy(p, "sendmail: "); 82358674Seric p += strlen(p); 82424943Seric 82558674Seric /* print the argument string */ 82658674Seric VA_START(fmt); 82758674Seric (void) vsprintf(p, fmt, ap); 82856852Seric VA_END; 82954996Seric 83058674Seric i = strlen(buf); 83158689Seric 83258689Seric # ifdef __hpux 83358689Seric pst.pst_command = buf; 83458689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 83558689Seric # else 83659992Seric # ifdef PS_STRINGS 83759732Seric PS_STRINGS->ps_nargvstr = 1; 83859732Seric PS_STRINGS->ps_argvstr = buf; 83959732Seric # else 84054996Seric if (i > LastArgv - Argv[0] - 2) 84125049Seric { 84254996Seric i = LastArgv - Argv[0] - 2; 84358674Seric buf[i] = '\0'; 84425049Seric } 84558674Seric (void) strcpy(Argv[0], buf); 84654997Seric p = &Argv[0][i]; 84724943Seric while (p < LastArgv) 84824943Seric *p++ = ' '; 84959732Seric # endif 85058689Seric # endif 85156795Seric # endif /* SETPROCTITLE */ 85224943Seric } 85325698Seric /* 85425698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 85525698Seric ** 85625698Seric ** Parameters: 85725698Seric ** none. 85825698Seric ** 85925698Seric ** Returns: 86025698Seric ** none. 86125698Seric ** 86225698Seric ** Side Effects: 86325698Seric ** Picks up extant zombies. 86425698Seric */ 86525698Seric 86625698Seric # include <sys/wait.h> 86725698Seric 86846928Sbostic void 86925698Seric reapchild() 87025698Seric { 87163966Seric # ifdef HASWAITPID 87260219Seric auto int status; 87360560Seric int count; 87460560Seric int pid; 87560219Seric 87660560Seric count = 0; 87760560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 87860560Seric { 87960560Seric if (count++ > 1000) 88060560Seric { 88160560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 88260560Seric pid, status); 88360560Seric break; 88460560Seric } 88560560Seric } 88660219Seric # else 88725698Seric # ifdef WNOHANG 88825698Seric union wait status; 88925698Seric 89063753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 89125698Seric continue; 89256795Seric # else /* WNOHANG */ 89325698Seric auto int status; 89425698Seric 89560219Seric while (wait(&status) > 0) 89625698Seric continue; 89756795Seric # endif /* WNOHANG */ 89860219Seric # endif 89964035Seric # ifdef SYS5SIGNALS 90064035Seric (void) setsignal(SIGCHLD, reapchild); 90158061Seric # endif 90225698Seric } 90355418Seric /* 90455418Seric ** UNSETENV -- remove a variable from the environment 90555418Seric ** 90655418Seric ** Not needed on newer systems. 90755418Seric ** 90855418Seric ** Parameters: 90955418Seric ** name -- the string name of the environment variable to be 91055418Seric ** deleted from the current environment. 91155418Seric ** 91255418Seric ** Returns: 91355418Seric ** none. 91455418Seric ** 91555418Seric ** Globals: 91655418Seric ** environ -- a pointer to the current environment. 91755418Seric ** 91855418Seric ** Side Effects: 91955418Seric ** Modifies environ. 92055418Seric */ 92155418Seric 92263962Seric #ifndef HASUNSETENV 92355418Seric 92455418Seric void 92555418Seric unsetenv(name) 92655418Seric char *name; 92755418Seric { 92855418Seric extern char **environ; 92955418Seric register char **pp; 93055418Seric int len = strlen(name); 93155418Seric 93255418Seric for (pp = environ; *pp != NULL; pp++) 93355418Seric { 93455418Seric if (strncmp(name, *pp, len) == 0 && 93555418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 93655418Seric break; 93755418Seric } 93855418Seric 93955418Seric for (; *pp != NULL; pp++) 94055418Seric *pp = pp[1]; 94155418Seric } 94255418Seric 94363962Seric #endif 94456215Seric /* 94556215Seric ** GETDTABLESIZE -- return number of file descriptors 94656215Seric ** 94756215Seric ** Only on non-BSD systems 94856215Seric ** 94956215Seric ** Parameters: 95056215Seric ** none 95156215Seric ** 95256215Seric ** Returns: 95356215Seric ** size of file descriptor table 95456215Seric ** 95556215Seric ** Side Effects: 95656215Seric ** none 95756215Seric */ 95856215Seric 95963787Seric #ifdef SOLARIS 96063787Seric # include <sys/resource.h> 96163787Seric #endif 96256215Seric 96356215Seric int 96463787Seric getdtsize() 96556215Seric { 96663787Seric #ifdef RLIMIT_NOFILE 96763787Seric struct rlimit rl; 96863787Seric 96963787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 97063787Seric return rl.rlim_cur; 97163787Seric #endif 97263787Seric 97364031Seric # ifdef HASGETDTABLESIZE 97464031Seric return getdtablesize(); 97564031Seric # else 97664031Seric # ifdef _SC_OPEN_MAX 97758689Seric return sysconf(_SC_OPEN_MAX); 97863787Seric # else 97956215Seric return NOFILE; 98063787Seric # endif 98158689Seric # endif 98256215Seric } 98357631Seric /* 98457631Seric ** UNAME -- get the UUCP name of this system. 98557631Seric */ 98657631Seric 98757943Seric #ifndef HASUNAME 98857631Seric 98957631Seric int 99057631Seric uname(name) 99157631Seric struct utsname *name; 99257631Seric { 99357631Seric FILE *file; 99457631Seric char *n; 99557631Seric 99657631Seric name->nodename[0] = '\0'; 99757631Seric 99857661Seric /* try /etc/whoami -- one line with the node name */ 99957631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 100057631Seric { 100157661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 100257631Seric (void) fclose(file); 100357661Seric n = strchr(name->nodename, '\n'); 100457631Seric if (n != NULL) 100557631Seric *n = '\0'; 100657631Seric if (name->nodename[0] != '\0') 100757631Seric return (0); 100857631Seric } 100957631Seric 101057661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 101157631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 101257631Seric { 101357631Seric char buf[MAXLINE]; 101457631Seric 101557631Seric while (fgets(buf, MAXLINE, file) != NULL) 101657631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 101757631Seric NODE_LENGTH, name->nodename) > 0) 101857631Seric break; 101957631Seric (void) fclose(file); 102057631Seric if (name->nodename[0] != '\0') 102157631Seric return (0); 102257631Seric } 102357631Seric 102457631Seric #ifdef TRUST_POPEN 102557631Seric /* 102657631Seric ** Popen is known to have security holes. 102757631Seric */ 102857631Seric 102957661Seric /* try uuname -l to return local name */ 103057631Seric if ((file = popen("uuname -l", "r")) != NULL) 103157631Seric { 103257661Seric (void) fgets(name, NODE_LENGTH + 1, file); 103357631Seric (void) pclose(file); 103457661Seric n = strchr(name, '\n'); 103557631Seric if (n != NULL) 103657631Seric *n = '\0'; 103757661Seric if (name->nodename[0] != '\0') 103857631Seric return (0); 103957631Seric } 104057631Seric #endif 104157631Seric 104257631Seric return (-1); 104357631Seric } 104457943Seric #endif /* HASUNAME */ 104558068Seric /* 104658068Seric ** INITGROUPS -- initialize groups 104758068Seric ** 104858068Seric ** Stub implementation for System V style systems 104958068Seric */ 105058068Seric 105158068Seric #ifndef HASINITGROUPS 105258068Seric 105358068Seric initgroups(name, basegid) 105458068Seric char *name; 105558068Seric int basegid; 105658068Seric { 105758068Seric return 0; 105858068Seric } 105958068Seric 106058068Seric #endif 106158082Seric /* 106259289Seric ** SETSID -- set session id (for non-POSIX systems) 106359289Seric */ 106459289Seric 106559289Seric #ifndef HASSETSID 106659289Seric 106763753Seric pid_t 106863753Seric setsid __P ((void)) 106959289Seric { 107059289Seric # ifdef SYSTEM5 107163753Seric return setpgrp(); 107263753Seric # else 107363753Seric return 0; 107459289Seric # endif 107559289Seric } 107659289Seric 107759289Seric #endif 107859289Seric /* 107963969Seric ** GETOPT -- for old systems or systems with bogus implementations 108063969Seric */ 108163969Seric 108263969Seric #ifdef NEEDGETOPT 108363969Seric 108463969Seric /* 108563969Seric * Copyright (c) 1985 Regents of the University of California. 108663969Seric * All rights reserved. The Berkeley software License Agreement 108763969Seric * specifies the terms and conditions for redistribution. 108863969Seric */ 108963969Seric 109063969Seric 109163969Seric /* 109263969Seric ** this version hacked to add `atend' flag to allow state machine 109363969Seric ** to reset if invoked by the program to scan args for a 2nd time 109463969Seric */ 109563969Seric 109663969Seric #if defined(LIBC_SCCS) && !defined(lint) 109763969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 109864072Seric #endif /* LIBC_SCCS and not lint */ 109963969Seric 110063969Seric #include <stdio.h> 110163969Seric 110263969Seric /* 110363969Seric * get option letter from argument vector 110463969Seric */ 110563969Seric int opterr = 1, /* if error message should be printed */ 110663969Seric optind = 1, /* index into parent argv vector */ 110763969Seric optopt; /* character checked for validity */ 110863969Seric char *optarg; /* argument associated with option */ 110963969Seric 111063969Seric #define BADCH (int)'?' 111163969Seric #define EMSG "" 111263969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 111363969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 111463969Seric 111563969Seric getopt(nargc,nargv,ostr) 111663969Seric int nargc; 111763969Seric char **nargv, 111863969Seric *ostr; 111963969Seric { 112063969Seric static char *place = EMSG; /* option letter processing */ 112163969Seric static char atend = 0; 112263969Seric register char *oli; /* option letter list index */ 112363969Seric 112463969Seric if (atend) { 112563969Seric atend = 0; 112663969Seric place = EMSG; 112763969Seric } 112863969Seric if(!*place) { /* update scanning pointer */ 1129*64136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 113063969Seric atend++; 113163969Seric return(EOF); 113263969Seric } 113363969Seric if (*place == '-') { /* found "--" */ 113463969Seric ++optind; 113563969Seric atend++; 113663969Seric return(EOF); 113763969Seric } 113863969Seric } /* option letter okay? */ 113964125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 1140*64136Seric if (!*place) ++optind; 114163969Seric tell(": illegal option -- "); 114263969Seric } 114363969Seric if (*++oli != ':') { /* don't need argument */ 114463969Seric optarg = NULL; 114563969Seric if (!*place) ++optind; 114663969Seric } 114763969Seric else { /* need an argument */ 114863969Seric if (*place) optarg = place; /* no white space */ 114963969Seric else if (nargc <= ++optind) { /* no arg */ 115063969Seric place = EMSG; 115163969Seric tell(": option requires an argument -- "); 115263969Seric } 115363969Seric else optarg = nargv[optind]; /* white space */ 115463969Seric place = EMSG; 115563969Seric ++optind; 115663969Seric } 115763969Seric return(optopt); /* dump back option letter */ 115863969Seric } 115963969Seric 116063969Seric #endif 116163969Seric /* 116263969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 116363969Seric */ 116463969Seric 116563969Seric #ifdef NEEDVPRINTF 116663969Seric 116763969Seric #define MAXARG 16 116863969Seric 116963969Seric vfprintf(fp, fmt, ap) 117063969Seric FILE * fp; 117163969Seric char * fmt; 117263969Seric char ** ap; 117363969Seric { 117463969Seric char * bp[MAXARG]; 117563969Seric int i = 0; 117663969Seric 117763969Seric while (*ap && i < MAXARG) 117863969Seric bp[i++] = *ap++; 117963969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 118063969Seric bp[4], bp[5], bp[6], bp[7], 118163969Seric bp[8], bp[9], bp[10], bp[11], 118263969Seric bp[12], bp[13], bp[14], bp[15]); 118363969Seric } 118463969Seric 118563969Seric vsprintf(s, fmt, ap) 118663969Seric char * s; 118763969Seric char * fmt; 118863969Seric char ** ap; 118963969Seric { 119063969Seric char * bp[MAXARG]; 119163969Seric int i = 0; 119263969Seric 119363969Seric while (*ap && i < MAXARG) 119463969Seric bp[i++] = *ap++; 119563969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 119663969Seric bp[4], bp[5], bp[6], bp[7], 119763969Seric bp[8], bp[9], bp[10], bp[11], 119863969Seric bp[12], bp[13], bp[14], bp[15]); 119963969Seric } 120063969Seric 120163969Seric #endif 120263969Seric /* 120358082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 120458082Seric ** 120558082Seric ** Only implemented if you have statfs. 120658082Seric ** 120758082Seric ** Parameters: 120858333Seric ** msize -- the size to check against. If zero, we don't yet 120958333Seric ** know how big the message will be, so just check for 121058333Seric ** a "reasonable" amount. 121158082Seric ** 121258082Seric ** Returns: 121358082Seric ** TRUE if there is enough space. 121458082Seric ** FALSE otherwise. 121558082Seric */ 121658082Seric 121758082Seric #ifndef HASSTATFS 121858082Seric # if defined(BSD4_4) || defined(__osf__) 121958082Seric # define HASSTATFS 122058082Seric # endif 122158082Seric #endif 122258082Seric 122358082Seric #ifdef HASSTATFS 122458157Seric # undef HASUSTAT 122558157Seric #endif 122658157Seric 122758157Seric #if defined(HASUSTAT) 122858157Seric # include <ustat.h> 122958157Seric #endif 123058157Seric 123158157Seric #ifdef HASSTATFS 123263987Seric # if defined(sgi) || defined(apollo) || defined(_SCO_unix_) 123358133Seric # include <sys/statfs.h> 123458133Seric # else 123563962Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) 123658133Seric # include <sys/vfs.h> 123758133Seric # else 123858157Seric # include <sys/mount.h> 123958133Seric # endif 124058133Seric # endif 124158082Seric #endif 124258082Seric 124358082Seric bool 124458333Seric enoughspace(msize) 124558333Seric long msize; 124658082Seric { 124758160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 124858157Seric # if defined(HASUSTAT) 124958153Seric struct ustat fs; 125058153Seric struct stat statbuf; 125158366Seric # define FSBLOCKSIZE DEV_BSIZE 125258157Seric # define f_bavail f_tfree 125358157Seric # else 125458157Seric # if defined(ultrix) 125558157Seric struct fs_data fs; 125658157Seric # define f_bavail fd_bfreen 125758366Seric # define FSBLOCKSIZE fs.fd_bsize 125858153Seric # else 125958082Seric struct statfs fs; 126058366Seric # define FSBLOCKSIZE fs.f_bsize 126164072Seric # if defined(_SCO_unix_) 126263987Seric # define f_bavail f_bfree 126363987Seric # endif 126458153Seric # endif 126558133Seric # endif 126658082Seric extern int errno; 126758082Seric 126858333Seric if (MinBlocksFree <= 0 && msize <= 0) 126958082Seric { 127058082Seric if (tTd(4, 80)) 127158133Seric printf("enoughspace: no threshold\n"); 127258133Seric return TRUE; 127358133Seric } 127458333Seric 127558157Seric # if defined(HASUSTAT) 127658157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 127758157Seric # else 127858157Seric # if defined(sgi) || defined(apollo) 127958133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 128058157Seric # else 128158157Seric # if defined(ultrix) 128258133Seric if (statfs(QueueDir, &fs) > 0) 128358153Seric # else 128458133Seric if (statfs(QueueDir, &fs) == 0) 128558153Seric # endif 128658133Seric # endif 128758133Seric # endif 128858133Seric { 128958133Seric if (tTd(4, 80)) 129058333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 129158333Seric fs.f_bavail, msize); 129258333Seric 129358333Seric /* convert msize to block count */ 129458366Seric msize = msize / FSBLOCKSIZE + 1; 129558333Seric if (MinBlocksFree >= 0) 129658333Seric msize += MinBlocksFree; 129758333Seric 129858333Seric if (fs.f_bavail < msize) 129958090Seric { 130058090Seric #ifdef LOG 130158090Seric if (LogLevel > 0) 130258090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 130358333Seric QueueDir, fs.f_bavail, msize); 130458090Seric #endif 130558082Seric return FALSE; 130658090Seric } 130758082Seric } 130858082Seric else if (tTd(4, 80)) 130958333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 131058333Seric MinBlocksFree, msize, errstring(errno)); 131158082Seric #endif 131258082Seric return TRUE; 131358082Seric } 131458542Seric /* 131558542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 131658542Seric ** 131758542Seric ** This looks at an errno value and tells if this is likely to 131858542Seric ** go away if retried later. 131958542Seric ** 132058542Seric ** Parameters: 132158542Seric ** err -- the errno code to classify. 132258542Seric ** 132358542Seric ** Returns: 132458542Seric ** TRUE if this is probably transient. 132558542Seric ** FALSE otherwise. 132658542Seric */ 132758542Seric 132858542Seric bool 132958542Seric transienterror(err) 133058542Seric int err; 133158542Seric { 133258542Seric switch (err) 133358542Seric { 133458542Seric case EIO: /* I/O error */ 133558542Seric case ENXIO: /* Device not configured */ 133658542Seric case EAGAIN: /* Resource temporarily unavailable */ 133758542Seric case ENOMEM: /* Cannot allocate memory */ 133858542Seric case ENODEV: /* Operation not supported by device */ 133958542Seric case ENFILE: /* Too many open files in system */ 134058542Seric case EMFILE: /* Too many open files */ 134158542Seric case ENOSPC: /* No space left on device */ 134258542Seric #ifdef ETIMEDOUT 134358542Seric case ETIMEDOUT: /* Connection timed out */ 134458542Seric #endif 134558542Seric #ifdef ESTALE 134658542Seric case ESTALE: /* Stale NFS file handle */ 134758542Seric #endif 134858542Seric #ifdef ENETDOWN 134958542Seric case ENETDOWN: /* Network is down */ 135058542Seric #endif 135158542Seric #ifdef ENETUNREACH 135258542Seric case ENETUNREACH: /* Network is unreachable */ 135358542Seric #endif 135458542Seric #ifdef ENETRESET 135558542Seric case ENETRESET: /* Network dropped connection on reset */ 135658542Seric #endif 135758542Seric #ifdef ECONNABORTED 135858542Seric case ECONNABORTED: /* Software caused connection abort */ 135958542Seric #endif 136058542Seric #ifdef ECONNRESET 136158542Seric case ECONNRESET: /* Connection reset by peer */ 136258542Seric #endif 136358542Seric #ifdef ENOBUFS 136458542Seric case ENOBUFS: /* No buffer space available */ 136558542Seric #endif 136658542Seric #ifdef ESHUTDOWN 136758542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 136858542Seric #endif 136958542Seric #ifdef ECONNREFUSED 137058542Seric case ECONNREFUSED: /* Connection refused */ 137158542Seric #endif 137258542Seric #ifdef EHOSTDOWN 137358542Seric case EHOSTDOWN: /* Host is down */ 137458542Seric #endif 137558542Seric #ifdef EHOSTUNREACH 137658542Seric case EHOSTUNREACH: /* No route to host */ 137758542Seric #endif 137858542Seric #ifdef EDQUOT 137958542Seric case EDQUOT: /* Disc quota exceeded */ 138058542Seric #endif 138158542Seric #ifdef EPROCLIM 138258542Seric case EPROCLIM: /* Too many processes */ 138358542Seric #endif 138458542Seric #ifdef EUSERS 138558542Seric case EUSERS: /* Too many users */ 138658542Seric #endif 138758542Seric #ifdef EDEADLK 138858542Seric case EDEADLK: /* Resource deadlock avoided */ 138958542Seric #endif 139058542Seric #ifdef EISCONN 139158542Seric case EISCONN: /* Socket already connected */ 139258542Seric #endif 139358542Seric #ifdef EINPROGRESS 139458542Seric case EINPROGRESS: /* Operation now in progress */ 139558542Seric #endif 139658542Seric #ifdef EALREADY 139758542Seric case EALREADY: /* Operation already in progress */ 139858542Seric #endif 139958542Seric #ifdef EADDRINUSE 140058542Seric case EADDRINUSE: /* Address already in use */ 140158542Seric #endif 140258542Seric #ifdef EADDRNOTAVAIL 140358542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 140458542Seric #endif 140563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 140658542Seric case ENOSR: /* Out of streams resources */ 140758542Seric #endif 140858542Seric return TRUE; 140958542Seric } 141058542Seric 141158542Seric /* nope, must be permanent */ 141258542Seric return FALSE; 141358542Seric } 141458689Seric /* 141564035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 141658689Seric ** 141758689Seric ** Parameters: 141858689Seric ** fd -- the file descriptor of the file. 141958689Seric ** filename -- the file name (for error messages). 142058689Seric ** type -- type of the lock. Bits can be: 142158689Seric ** LOCK_EX -- exclusive lock. 142258689Seric ** LOCK_NB -- non-blocking. 142358689Seric ** 142458689Seric ** Returns: 142558689Seric ** TRUE if the lock was acquired. 142658689Seric ** FALSE otherwise. 142758689Seric */ 142858689Seric 142958689Seric bool 143058689Seric lockfile(fd, filename, type) 143158689Seric int fd; 143258689Seric char *filename; 143358689Seric int type; 143458689Seric { 143564035Seric # ifndef HASFLOCK 143658689Seric int action; 143758689Seric struct flock lfd; 143864035Seric 143963983Seric bzero(&lfd, sizeof lfd); 144059447Seric if (bitset(LOCK_UN, type)) 144159447Seric lfd.l_type = F_UNLCK; 144259447Seric else if (bitset(LOCK_EX, type)) 144358689Seric lfd.l_type = F_WRLCK; 144458689Seric else 144558689Seric lfd.l_type = F_RDLCK; 144658689Seric 144758689Seric if (bitset(LOCK_NB, type)) 144858689Seric action = F_SETLK; 144958689Seric else 145058689Seric action = F_SETLKW; 145158689Seric 145258689Seric if (fcntl(fd, action, &lfd) >= 0) 145358689Seric return TRUE; 145458689Seric 1455*64136Seric /* 1456*64136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 1457*64136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 1458*64136Seric ** as type "tmp" (that is, served from swap space), the 1459*64136Seric ** previous fcntl will fail with "Invalid argument" errors. 1460*64136Seric ** Since this is fairly common during testing, we will assume 1461*64136Seric ** that this indicates that the lock is successfully grabbed. 1462*64136Seric */ 1463*64136Seric 1464*64136Seric if (errno == EINVAL) 1465*64136Seric return TRUE; 1466*64136Seric 146758689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 146859447Seric syserr("cannot lockf(%s, %o)", filename, type); 146958689Seric # else 147058689Seric if (flock(fd, type) >= 0) 147158689Seric return TRUE; 147258689Seric 147358689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 147459447Seric syserr("cannot flock(%s, %o)", filename, type); 147558689Seric # endif 147658689Seric return FALSE; 147758689Seric } 1478