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*64035Seric static char sccsid[] = "@(#)conf.c 8.12 (Berkeley) 07/26/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 /* 484*64035Seric ** SETSIGNAL -- set a signal handler 485*64035Seric ** 486*64035Seric ** This is essentially old BSD "signal(3)". 487*64035Seric */ 488*64035Seric 489*64035Seric setsig_t 490*64035Seric setsignal(sig, handler) 491*64035Seric int sig; 492*64035Seric setsig_t handler; 493*64035Seric { 494*64035Seric #ifdef SYS5SIGNALS 495*64035Seric return signal(sig, handler); 496*64035Seric #else 497*64035Seric struct sigaction n, o; 498*64035Seric 499*64035Seric bzero(&n, sizeof n); 500*64035Seric n.sa_handler = handler; 501*64035Seric if (sigaction(sig, &n, &o) < 0) 502*64035Seric return SIG_ERR; 503*64035Seric return o.sa_handler; 504*64035Seric #endif 505*64035Seric } 506*64035Seric /* 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 899*64035Seric # ifdef SYS5SIGNALS 900*64035Seric (void) setsignal(SIGCHLD, reapchild); 90158061Seric # endif 90225698Seric } 90355418Seric /* 90463962Seric ** SETENV -- set environment variable 90563962Seric ** 90663962Seric ** Putenv is more modern, but this is a simpler interface 90763962Seric ** 90863962Seric ** Parameters: 90963962Seric ** name -- the name of the envariable. 91063962Seric ** value -- the value of that envariable. 91163962Seric ** overwrite -- if set, overwrite existing value 91263962Seric ** (this is assumed to be set). 91363962Seric ** 91463962Seric ** Returns: 91563962Seric ** none. 91663962Seric ** 91763962Seric ** Side Effects: 91863962Seric ** The environment is updated. 91963962Seric */ 92063962Seric 92163962Seric #ifndef HASSETENV 92263962Seric 92363962Seric setenv(name, value, overwrite) 92463962Seric char *name; 92563962Seric char *value; 92663962Seric int overwrite; 92763962Seric { 92863962Seric register char *p; 92963962Seric 93063962Seric p = xalloc(strlen(name) + strlen(value) + 2); 93163962Seric strcpy(p, name); 93263962Seric strcat(p, "="); 93363962Seric strcat(p, value); 93463962Seric putenv(p); 93563962Seric } 93663962Seric 93763962Seric #endif 93863962Seric /* 93955418Seric ** UNSETENV -- remove a variable from the environment 94055418Seric ** 94155418Seric ** Not needed on newer systems. 94255418Seric ** 94355418Seric ** Parameters: 94455418Seric ** name -- the string name of the environment variable to be 94555418Seric ** deleted from the current environment. 94655418Seric ** 94755418Seric ** Returns: 94855418Seric ** none. 94955418Seric ** 95055418Seric ** Globals: 95155418Seric ** environ -- a pointer to the current environment. 95255418Seric ** 95355418Seric ** Side Effects: 95455418Seric ** Modifies environ. 95555418Seric */ 95655418Seric 95763962Seric #ifndef HASUNSETENV 95855418Seric 95955418Seric void 96055418Seric unsetenv(name) 96155418Seric char *name; 96255418Seric { 96355418Seric extern char **environ; 96455418Seric register char **pp; 96555418Seric int len = strlen(name); 96655418Seric 96755418Seric for (pp = environ; *pp != NULL; pp++) 96855418Seric { 96955418Seric if (strncmp(name, *pp, len) == 0 && 97055418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 97155418Seric break; 97255418Seric } 97355418Seric 97455418Seric for (; *pp != NULL; pp++) 97555418Seric *pp = pp[1]; 97655418Seric } 97755418Seric 97863962Seric #endif 97956215Seric /* 98056215Seric ** GETDTABLESIZE -- return number of file descriptors 98156215Seric ** 98256215Seric ** Only on non-BSD systems 98356215Seric ** 98456215Seric ** Parameters: 98556215Seric ** none 98656215Seric ** 98756215Seric ** Returns: 98856215Seric ** size of file descriptor table 98956215Seric ** 99056215Seric ** Side Effects: 99156215Seric ** none 99256215Seric */ 99356215Seric 99463787Seric #ifdef SOLARIS 99563787Seric # include <sys/resource.h> 99663787Seric #endif 99756215Seric 99856215Seric int 99963787Seric getdtsize() 100056215Seric { 100163787Seric #ifdef RLIMIT_NOFILE 100263787Seric struct rlimit rl; 100363787Seric 100463787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 100563787Seric return rl.rlim_cur; 100663787Seric #endif 100763787Seric 100864031Seric # ifdef HASGETDTABLESIZE 100964031Seric return getdtablesize(); 101064031Seric # else 101164031Seric # ifdef _SC_OPEN_MAX 101258689Seric return sysconf(_SC_OPEN_MAX); 101363787Seric # else 101456215Seric return NOFILE; 101563787Seric # endif 101658689Seric # endif 101756215Seric } 101857631Seric /* 101957631Seric ** UNAME -- get the UUCP name of this system. 102057631Seric */ 102157631Seric 102257943Seric #ifndef HASUNAME 102357631Seric 102457631Seric int 102557631Seric uname(name) 102657631Seric struct utsname *name; 102757631Seric { 102857631Seric FILE *file; 102957631Seric char *n; 103057631Seric 103157631Seric name->nodename[0] = '\0'; 103257631Seric 103357661Seric /* try /etc/whoami -- one line with the node name */ 103457631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 103557631Seric { 103657661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 103757631Seric (void) fclose(file); 103857661Seric n = strchr(name->nodename, '\n'); 103957631Seric if (n != NULL) 104057631Seric *n = '\0'; 104157631Seric if (name->nodename[0] != '\0') 104257631Seric return (0); 104357631Seric } 104457631Seric 104557661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 104657631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 104757631Seric { 104857631Seric char buf[MAXLINE]; 104957631Seric 105057631Seric while (fgets(buf, MAXLINE, file) != NULL) 105157631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 105257631Seric NODE_LENGTH, name->nodename) > 0) 105357631Seric break; 105457631Seric (void) fclose(file); 105557631Seric if (name->nodename[0] != '\0') 105657631Seric return (0); 105757631Seric } 105857631Seric 105957631Seric #ifdef TRUST_POPEN 106057631Seric /* 106157631Seric ** Popen is known to have security holes. 106257631Seric */ 106357631Seric 106457661Seric /* try uuname -l to return local name */ 106557631Seric if ((file = popen("uuname -l", "r")) != NULL) 106657631Seric { 106757661Seric (void) fgets(name, NODE_LENGTH + 1, file); 106857631Seric (void) pclose(file); 106957661Seric n = strchr(name, '\n'); 107057631Seric if (n != NULL) 107157631Seric *n = '\0'; 107257661Seric if (name->nodename[0] != '\0') 107357631Seric return (0); 107457631Seric } 107557631Seric #endif 107657631Seric 107757631Seric return (-1); 107857631Seric } 107957943Seric #endif /* HASUNAME */ 108058068Seric /* 108158068Seric ** INITGROUPS -- initialize groups 108258068Seric ** 108358068Seric ** Stub implementation for System V style systems 108458068Seric */ 108558068Seric 108658068Seric #ifndef HASINITGROUPS 108758068Seric 108858068Seric initgroups(name, basegid) 108958068Seric char *name; 109058068Seric int basegid; 109158068Seric { 109258068Seric return 0; 109358068Seric } 109458068Seric 109558068Seric #endif 109658082Seric /* 109759289Seric ** SETSID -- set session id (for non-POSIX systems) 109859289Seric */ 109959289Seric 110059289Seric #ifndef HASSETSID 110159289Seric 110263753Seric pid_t 110363753Seric setsid __P ((void)) 110459289Seric { 110559289Seric # ifdef SYSTEM5 110663753Seric return setpgrp(); 110763753Seric # else 110863753Seric return 0; 110959289Seric # endif 111059289Seric } 111159289Seric 111259289Seric #endif 111359289Seric /* 111463969Seric ** GETOPT -- for old systems or systems with bogus implementations 111563969Seric */ 111663969Seric 111763969Seric #ifdef NEEDGETOPT 111863969Seric 111963969Seric /* 112063969Seric * Copyright (c) 1985 Regents of the University of California. 112163969Seric * All rights reserved. The Berkeley software License Agreement 112263969Seric * specifies the terms and conditions for redistribution. 112363969Seric */ 112463969Seric 112563969Seric 112663969Seric /* 112763969Seric ** this version hacked to add `atend' flag to allow state machine 112863969Seric ** to reset if invoked by the program to scan args for a 2nd time 112963969Seric */ 113063969Seric 113163969Seric #if defined(LIBC_SCCS) && !defined(lint) 113263969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 113363969Seric #endif LIBC_SCCS and not lint 113463969Seric 113563969Seric #include <stdio.h> 113663969Seric 113763969Seric /* 113863969Seric * get option letter from argument vector 113963969Seric */ 114063969Seric int opterr = 1, /* if error message should be printed */ 114163969Seric optind = 1, /* index into parent argv vector */ 114263969Seric optopt; /* character checked for validity */ 114363969Seric char *optarg; /* argument associated with option */ 114463969Seric 114563969Seric #define BADCH (int)'?' 114663969Seric #define EMSG "" 114763969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 114863969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 114963969Seric 115063969Seric getopt(nargc,nargv,ostr) 115163969Seric int nargc; 115263969Seric char **nargv, 115363969Seric *ostr; 115463969Seric { 115563969Seric static char *place = EMSG; /* option letter processing */ 115663969Seric static char atend = 0; 115763969Seric register char *oli; /* option letter list index */ 115863969Seric char *index(); 115963969Seric 116063969Seric if (atend) { 116163969Seric atend = 0; 116263969Seric place = EMSG; 116363969Seric } 116463969Seric if(!*place) { /* update scanning pointer */ 116563969Seric if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 116663969Seric atend++; 116763969Seric return(EOF); 116863969Seric } 116963969Seric if (*place == '-') { /* found "--" */ 117063969Seric ++optind; 117163969Seric atend++; 117263969Seric return(EOF); 117363969Seric } 117463969Seric } /* option letter okay? */ 117563969Seric if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) { 117663969Seric if(!*place) ++optind; 117763969Seric tell(": illegal option -- "); 117863969Seric } 117963969Seric if (*++oli != ':') { /* don't need argument */ 118063969Seric optarg = NULL; 118163969Seric if (!*place) ++optind; 118263969Seric } 118363969Seric else { /* need an argument */ 118463969Seric if (*place) optarg = place; /* no white space */ 118563969Seric else if (nargc <= ++optind) { /* no arg */ 118663969Seric place = EMSG; 118763969Seric tell(": option requires an argument -- "); 118863969Seric } 118963969Seric else optarg = nargv[optind]; /* white space */ 119063969Seric place = EMSG; 119163969Seric ++optind; 119263969Seric } 119363969Seric return(optopt); /* dump back option letter */ 119463969Seric } 119563969Seric 119663969Seric #endif 119763969Seric /* 119863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 119963969Seric */ 120063969Seric 120163969Seric #ifdef NEEDVPRINTF 120263969Seric 120363969Seric #define MAXARG 16 120463969Seric 120563969Seric vfprintf(fp, fmt, ap) 120663969Seric FILE * fp; 120763969Seric char * fmt; 120863969Seric char ** ap; 120963969Seric { 121063969Seric char * bp[MAXARG]; 121163969Seric int i = 0; 121263969Seric 121363969Seric while (*ap && i < MAXARG) 121463969Seric bp[i++] = *ap++; 121563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 121663969Seric bp[4], bp[5], bp[6], bp[7], 121763969Seric bp[8], bp[9], bp[10], bp[11], 121863969Seric bp[12], bp[13], bp[14], bp[15]); 121963969Seric } 122063969Seric 122163969Seric vsprintf(s, fmt, ap) 122263969Seric char * s; 122363969Seric char * fmt; 122463969Seric char ** ap; 122563969Seric { 122663969Seric char * bp[MAXARG]; 122763969Seric int i = 0; 122863969Seric 122963969Seric while (*ap && i < MAXARG) 123063969Seric bp[i++] = *ap++; 123163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 123263969Seric bp[4], bp[5], bp[6], bp[7], 123363969Seric bp[8], bp[9], bp[10], bp[11], 123463969Seric bp[12], bp[13], bp[14], bp[15]); 123563969Seric } 123663969Seric 123763969Seric #endif 123863969Seric /* 123958082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 124058082Seric ** 124158082Seric ** Only implemented if you have statfs. 124258082Seric ** 124358082Seric ** Parameters: 124458333Seric ** msize -- the size to check against. If zero, we don't yet 124558333Seric ** know how big the message will be, so just check for 124658333Seric ** a "reasonable" amount. 124758082Seric ** 124858082Seric ** Returns: 124958082Seric ** TRUE if there is enough space. 125058082Seric ** FALSE otherwise. 125158082Seric */ 125258082Seric 125358082Seric #ifndef HASSTATFS 125458082Seric # if defined(BSD4_4) || defined(__osf__) 125558082Seric # define HASSTATFS 125658082Seric # endif 125758082Seric #endif 125858082Seric 125958082Seric #ifdef HASSTATFS 126058157Seric # undef HASUSTAT 126158157Seric #endif 126258157Seric 126358157Seric #if defined(HASUSTAT) 126458157Seric # include <ustat.h> 126558157Seric #endif 126658157Seric 126758157Seric #ifdef HASSTATFS 126863987Seric # if defined(sgi) || defined(apollo) || defined(_SCO_unix_) 126958133Seric # include <sys/statfs.h> 127058133Seric # else 127163962Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) 127258133Seric # include <sys/vfs.h> 127358133Seric # else 127458157Seric # include <sys/mount.h> 127558133Seric # endif 127658133Seric # endif 127758082Seric #endif 127858082Seric 127958082Seric bool 128058333Seric enoughspace(msize) 128158333Seric long msize; 128258082Seric { 128358160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 128458157Seric # if defined(HASUSTAT) 128558153Seric struct ustat fs; 128658153Seric struct stat statbuf; 128758366Seric # define FSBLOCKSIZE DEV_BSIZE 128858157Seric # define f_bavail f_tfree 128958157Seric # else 129058157Seric # if defined(ultrix) 129158157Seric struct fs_data fs; 129258157Seric # define f_bavail fd_bfreen 129358366Seric # define FSBLOCKSIZE fs.fd_bsize 129458153Seric # else 129558082Seric struct statfs fs; 129658366Seric # define FSBLOCKSIZE fs.f_bsize 129763987Seric # if defined(_SCO_UNIX_) 129863987Seric # define f_bavail f_bfree 129963987Seric # endif 130058153Seric # endif 130158133Seric # endif 130258082Seric extern int errno; 130358082Seric 130458333Seric if (MinBlocksFree <= 0 && msize <= 0) 130558082Seric { 130658082Seric if (tTd(4, 80)) 130758133Seric printf("enoughspace: no threshold\n"); 130858133Seric return TRUE; 130958133Seric } 131058333Seric 131158157Seric # if defined(HASUSTAT) 131258157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 131358157Seric # else 131458157Seric # if defined(sgi) || defined(apollo) 131558133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 131658157Seric # else 131758157Seric # if defined(ultrix) 131858133Seric if (statfs(QueueDir, &fs) > 0) 131958153Seric # else 132058133Seric if (statfs(QueueDir, &fs) == 0) 132158153Seric # endif 132258133Seric # endif 132358133Seric # endif 132458133Seric { 132558133Seric if (tTd(4, 80)) 132658333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 132758333Seric fs.f_bavail, msize); 132858333Seric 132958333Seric /* convert msize to block count */ 133058366Seric msize = msize / FSBLOCKSIZE + 1; 133158333Seric if (MinBlocksFree >= 0) 133258333Seric msize += MinBlocksFree; 133358333Seric 133458333Seric if (fs.f_bavail < msize) 133558090Seric { 133658090Seric #ifdef LOG 133758090Seric if (LogLevel > 0) 133858090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 133958333Seric QueueDir, fs.f_bavail, msize); 134058090Seric #endif 134158082Seric return FALSE; 134258090Seric } 134358082Seric } 134458082Seric else if (tTd(4, 80)) 134558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 134658333Seric MinBlocksFree, msize, errstring(errno)); 134758082Seric #endif 134858082Seric return TRUE; 134958082Seric } 135058542Seric /* 135158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 135258542Seric ** 135358542Seric ** This looks at an errno value and tells if this is likely to 135458542Seric ** go away if retried later. 135558542Seric ** 135658542Seric ** Parameters: 135758542Seric ** err -- the errno code to classify. 135858542Seric ** 135958542Seric ** Returns: 136058542Seric ** TRUE if this is probably transient. 136158542Seric ** FALSE otherwise. 136258542Seric */ 136358542Seric 136458542Seric bool 136558542Seric transienterror(err) 136658542Seric int err; 136758542Seric { 136858542Seric switch (err) 136958542Seric { 137058542Seric case EIO: /* I/O error */ 137158542Seric case ENXIO: /* Device not configured */ 137258542Seric case EAGAIN: /* Resource temporarily unavailable */ 137358542Seric case ENOMEM: /* Cannot allocate memory */ 137458542Seric case ENODEV: /* Operation not supported by device */ 137558542Seric case ENFILE: /* Too many open files in system */ 137658542Seric case EMFILE: /* Too many open files */ 137758542Seric case ENOSPC: /* No space left on device */ 137858542Seric #ifdef ETIMEDOUT 137958542Seric case ETIMEDOUT: /* Connection timed out */ 138058542Seric #endif 138158542Seric #ifdef ESTALE 138258542Seric case ESTALE: /* Stale NFS file handle */ 138358542Seric #endif 138458542Seric #ifdef ENETDOWN 138558542Seric case ENETDOWN: /* Network is down */ 138658542Seric #endif 138758542Seric #ifdef ENETUNREACH 138858542Seric case ENETUNREACH: /* Network is unreachable */ 138958542Seric #endif 139058542Seric #ifdef ENETRESET 139158542Seric case ENETRESET: /* Network dropped connection on reset */ 139258542Seric #endif 139358542Seric #ifdef ECONNABORTED 139458542Seric case ECONNABORTED: /* Software caused connection abort */ 139558542Seric #endif 139658542Seric #ifdef ECONNRESET 139758542Seric case ECONNRESET: /* Connection reset by peer */ 139858542Seric #endif 139958542Seric #ifdef ENOBUFS 140058542Seric case ENOBUFS: /* No buffer space available */ 140158542Seric #endif 140258542Seric #ifdef ESHUTDOWN 140358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 140458542Seric #endif 140558542Seric #ifdef ECONNREFUSED 140658542Seric case ECONNREFUSED: /* Connection refused */ 140758542Seric #endif 140858542Seric #ifdef EHOSTDOWN 140958542Seric case EHOSTDOWN: /* Host is down */ 141058542Seric #endif 141158542Seric #ifdef EHOSTUNREACH 141258542Seric case EHOSTUNREACH: /* No route to host */ 141358542Seric #endif 141458542Seric #ifdef EDQUOT 141558542Seric case EDQUOT: /* Disc quota exceeded */ 141658542Seric #endif 141758542Seric #ifdef EPROCLIM 141858542Seric case EPROCLIM: /* Too many processes */ 141958542Seric #endif 142058542Seric #ifdef EUSERS 142158542Seric case EUSERS: /* Too many users */ 142258542Seric #endif 142358542Seric #ifdef EDEADLK 142458542Seric case EDEADLK: /* Resource deadlock avoided */ 142558542Seric #endif 142658542Seric #ifdef EISCONN 142758542Seric case EISCONN: /* Socket already connected */ 142858542Seric #endif 142958542Seric #ifdef EINPROGRESS 143058542Seric case EINPROGRESS: /* Operation now in progress */ 143158542Seric #endif 143258542Seric #ifdef EALREADY 143358542Seric case EALREADY: /* Operation already in progress */ 143458542Seric #endif 143558542Seric #ifdef EADDRINUSE 143658542Seric case EADDRINUSE: /* Address already in use */ 143758542Seric #endif 143858542Seric #ifdef EADDRNOTAVAIL 143958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 144058542Seric #endif 144163834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 144258542Seric case ENOSR: /* Out of streams resources */ 144358542Seric #endif 144458542Seric return TRUE; 144558542Seric } 144658542Seric 144758542Seric /* nope, must be permanent */ 144858542Seric return FALSE; 144958542Seric } 145058689Seric /* 1451*64035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 145258689Seric ** 145358689Seric ** Parameters: 145458689Seric ** fd -- the file descriptor of the file. 145558689Seric ** filename -- the file name (for error messages). 145658689Seric ** type -- type of the lock. Bits can be: 145758689Seric ** LOCK_EX -- exclusive lock. 145858689Seric ** LOCK_NB -- non-blocking. 145958689Seric ** 146058689Seric ** Returns: 146158689Seric ** TRUE if the lock was acquired. 146258689Seric ** FALSE otherwise. 146358689Seric */ 146458689Seric 146558689Seric bool 146658689Seric lockfile(fd, filename, type) 146758689Seric int fd; 146858689Seric char *filename; 146958689Seric int type; 147058689Seric { 1471*64035Seric # ifndef HASFLOCK 147258689Seric int action; 147358689Seric struct flock lfd; 1474*64035Seric 147563983Seric bzero(&lfd, sizeof lfd); 147659447Seric if (bitset(LOCK_UN, type)) 147759447Seric lfd.l_type = F_UNLCK; 147859447Seric else if (bitset(LOCK_EX, type)) 147958689Seric lfd.l_type = F_WRLCK; 148058689Seric else 148158689Seric lfd.l_type = F_RDLCK; 148258689Seric 148358689Seric if (bitset(LOCK_NB, type)) 148458689Seric action = F_SETLK; 148558689Seric else 148658689Seric action = F_SETLKW; 148758689Seric 148858689Seric if (fcntl(fd, action, &lfd) >= 0) 148958689Seric return TRUE; 149058689Seric 149158689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 149259447Seric syserr("cannot lockf(%s, %o)", filename, type); 149358689Seric # else 149458689Seric if (flock(fd, type) >= 0) 149558689Seric return TRUE; 149658689Seric 149758689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 149859447Seric syserr("cannot flock(%s, %o)", filename, type); 149958689Seric # endif 150058689Seric return FALSE; 150158689Seric } 1502