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*64556Seric static char sccsid[] = "@(#)conf.c 8.36 (Berkeley) 09/22/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 10458082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1059039Seric 1069064Seric 1079064Seric 1089039Seric /* 10958082Seric ** Privacy values 11058082Seric */ 11158082Seric 11258082Seric struct prival PrivacyValues[] = 11358082Seric { 11458082Seric "public", PRIV_PUBLIC, 11558082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11658114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11758082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11858082Seric "noexpn", PRIV_NOEXPN, 11958082Seric "novrfy", PRIV_NOVRFY, 12064333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12164333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12258789Seric "authwarnings", PRIV_AUTHWARNINGS, 12358082Seric "goaway", PRIV_GOAWAY, 12458789Seric NULL, 0, 12558082Seric }; 12658082Seric 12758082Seric 12858082Seric 12958082Seric /* 13024943Seric ** Miscellaneous stuff. 1319039Seric */ 1329039Seric 13324943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13424943Seric /* 13524943Seric ** SETDEFAULTS -- set default values 13624943Seric ** 13724943Seric ** Because of the way freezing is done, these must be initialized 13824943Seric ** using direct code. 13924943Seric ** 14024943Seric ** Parameters: 14158734Seric ** e -- the default envelope. 14224943Seric ** 14324943Seric ** Returns: 14424943Seric ** none. 14524943Seric ** 14624943Seric ** Side Effects: 14724943Seric ** Initializes a bunch of global variables to their 14824943Seric ** default values. 14924943Seric */ 15024943Seric 15158737Seric #define DAYS * 24 * 60 * 60 15258737Seric 15358734Seric setdefaults(e) 15458734Seric register ENVELOPE *e; 15524943Seric { 15657438Seric SpaceSub = ' '; /* option B */ 15757438Seric QueueLA = 8; /* option x */ 15857438Seric RefuseLA = 12; /* option X */ 15957438Seric WkRecipFact = 30000L; /* option y */ 16057438Seric WkClassFact = 1800L; /* option z */ 16157438Seric WkTimeFact = 90000L; /* option Z */ 16257438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16363787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 16457438Seric /* option F */ 16557438Seric DefUid = 1; /* option u */ 16657438Seric DefGid = 1; /* option g */ 16757438Seric CheckpointInterval = 10; /* option C */ 16857438Seric MaxHopCount = 25; /* option h */ 16958734Seric e->e_sendmode = SM_FORK; /* option d */ 17058734Seric e->e_errormode = EM_PRINT; /* option e */ 17159709Seric SevenBit = FALSE; /* option 7 */ 17257438Seric MaxMciCache = 1; /* option k */ 17357438Seric MciCacheTimeout = 300; /* option K */ 17457438Seric LogLevel = 9; /* option L */ 17558112Seric settimeouts(NULL); /* option r */ 17658737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17758737Seric TimeOuts.to_q_warning = 0; /* option T */ 17858853Seric PrivacyFlags = 0; /* option p */ 17940973Sbostic setdefuser(); 18053654Seric setupmaps(); 18157402Seric setupmailers(); 18224943Seric } 183294Seric 18440973Sbostic 1854326Seric /* 18640973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 18740973Sbostic */ 18840973Sbostic 18940973Sbostic setdefuser() 19040973Sbostic { 19140973Sbostic struct passwd *defpwent; 19257386Seric static char defuserbuf[40]; 19340973Sbostic 19457386Seric DefUser = defuserbuf; 19540973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19657386Seric strcpy(defuserbuf, defpwent->pw_name); 19740973Sbostic else 19857386Seric strcpy(defuserbuf, "nobody"); 19940973Sbostic } 20053654Seric /* 20156836Seric ** HOST_MAP_INIT -- initialize host class structures 20256836Seric */ 20356836Seric 20456836Seric bool 20560219Seric host_map_init(map, args) 20656836Seric MAP *map; 20756836Seric char *args; 20856836Seric { 20956836Seric register char *p = args; 21056836Seric 21156836Seric for (;;) 21256836Seric { 21358050Seric while (isascii(*p) && isspace(*p)) 21456836Seric p++; 21556836Seric if (*p != '-') 21656836Seric break; 21756836Seric switch (*++p) 21856836Seric { 21956836Seric case 'a': 22056836Seric map->map_app = ++p; 22156836Seric break; 22256836Seric } 22358050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 22456836Seric p++; 22556836Seric if (*p != '\0') 22656836Seric *p++ = '\0'; 22756836Seric } 22856836Seric if (map->map_app != NULL) 22956836Seric map->map_app = newstr(map->map_app); 23056836Seric return TRUE; 23156836Seric } 23257402Seric /* 23357402Seric ** SETUPMAILERS -- initialize default mailers 23457402Seric */ 23556836Seric 23657402Seric setupmailers() 23757402Seric { 23857402Seric char buf[100]; 23957402Seric 24057403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 24157403Seric makemailer(buf); 24257403Seric 24359883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 24457402Seric makemailer(buf); 24557402Seric 24657402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 24757402Seric makemailer(buf); 24857402Seric } 24956836Seric /* 25060207Seric ** SETUPMAPS -- set up map classes 25160207Seric */ 25260207Seric 25360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 25460207Seric { \ 25560207Seric extern bool parse __P((MAP *, char *)); \ 25660207Seric extern bool open __P((MAP *, int)); \ 25760207Seric extern void close __P((MAP *)); \ 25860207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 25960207Seric extern void store __P((MAP *, char *, char *)); \ 26060207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 26160207Seric s->s_mapclass.map_cname = name; \ 26260207Seric s->s_mapclass.map_ext = ext; \ 26360207Seric s->s_mapclass.map_cflags = flags; \ 26460207Seric s->s_mapclass.map_parse = parse; \ 26560207Seric s->s_mapclass.map_open = open; \ 26660207Seric s->s_mapclass.map_close = close; \ 26760207Seric s->s_mapclass.map_lookup = lookup; \ 26860207Seric s->s_mapclass.map_store = store; \ 26960207Seric } 27060207Seric 27160207Seric setupmaps() 27260207Seric { 27360207Seric register STAB *s; 27460207Seric 27560207Seric #ifdef NEWDB 27660207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 27760207Seric map_parseargs, hash_map_open, db_map_close, 27860207Seric db_map_lookup, db_map_store); 27960207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 28060207Seric map_parseargs, bt_map_open, db_map_close, 28160207Seric db_map_lookup, db_map_store); 28260207Seric #endif 28360207Seric 28460207Seric #ifdef NDBM 28560207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 28660207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 28760207Seric ndbm_map_lookup, ndbm_map_store); 28860207Seric #endif 28960207Seric 29060207Seric #ifdef NIS 29160207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 29260207Seric map_parseargs, nis_map_open, nis_map_close, 29360207Seric nis_map_lookup, nis_map_store); 29460207Seric #endif 29560207Seric 29660207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 29760207Seric map_parseargs, stab_map_open, stab_map_close, 29860207Seric stab_map_lookup, stab_map_store); 29960207Seric 30060207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 30160207Seric map_parseargs, impl_map_open, impl_map_close, 30260207Seric impl_map_lookup, impl_map_store); 30360207Seric 30460207Seric /* host DNS lookup */ 30560207Seric MAPDEF("host", NULL, 0, 30660207Seric host_map_init, null_map_open, null_map_close, 30760207Seric host_map_lookup, null_map_store); 30860207Seric 30960207Seric /* dequote map */ 31060207Seric MAPDEF("dequote", NULL, 0, 31160207Seric dequote_init, null_map_open, null_map_close, 31260207Seric dequote_map, null_map_store); 31360207Seric 31460207Seric #if 0 31560207Seric # ifdef USERDB 31660207Seric /* user database */ 31760207Seric MAPDEF("udb", ".db", 0, 31860207Seric udb_map_parse, null_map_open, null_map_close, 31960207Seric udb_map_lookup, null_map_store); 32060207Seric # endif 32160207Seric #endif 32260207Seric } 32360207Seric 32460207Seric #undef MAPDEF 32560207Seric /* 3269369Seric ** USERNAME -- return the user id of the logged in user. 3279369Seric ** 3289369Seric ** Parameters: 3299369Seric ** none. 3309369Seric ** 3319369Seric ** Returns: 3329369Seric ** The login name of the logged in user. 3339369Seric ** 3349369Seric ** Side Effects: 3359369Seric ** none. 3369369Seric ** 3379369Seric ** Notes: 3389369Seric ** The return value is statically allocated. 3399369Seric */ 3409369Seric 3419369Seric char * 3429369Seric username() 3439369Seric { 34417469Seric static char *myname = NULL; 3459369Seric extern char *getlogin(); 34619904Smiriam register struct passwd *pw; 3479369Seric 34817469Seric /* cache the result */ 34917469Seric if (myname == NULL) 35017469Seric { 35117469Seric myname = getlogin(); 35217469Seric if (myname == NULL || myname[0] == '\0') 35317469Seric { 35463787Seric pw = getpwuid(RealUid); 35517469Seric if (pw != NULL) 35640993Sbostic myname = newstr(pw->pw_name); 35717469Seric } 35819904Smiriam else 35919904Smiriam { 36063787Seric uid_t uid = RealUid; 36119873Smiriam 36240993Sbostic myname = newstr(myname); 36340993Sbostic if ((pw = getpwnam(myname)) == NULL || 36458736Seric (uid != 0 && uid != pw->pw_uid)) 36519904Smiriam { 36658736Seric pw = getpwuid(uid); 36724945Seric if (pw != NULL) 36840993Sbostic myname = newstr(pw->pw_name); 36919873Smiriam } 37019873Smiriam } 37117469Seric if (myname == NULL || myname[0] == '\0') 37217469Seric { 37358151Seric syserr("554 Who are you?"); 37417469Seric myname = "postmaster"; 37517469Seric } 37617469Seric } 37717469Seric 37817469Seric return (myname); 3799369Seric } 3809369Seric /* 3814190Seric ** TTYPATH -- Get the path of the user's tty 382294Seric ** 383294Seric ** Returns the pathname of the user's tty. Returns NULL if 384294Seric ** the user is not logged in or if s/he has write permission 385294Seric ** denied. 386294Seric ** 387294Seric ** Parameters: 388294Seric ** none 389294Seric ** 390294Seric ** Returns: 391294Seric ** pathname of the user's tty. 392294Seric ** NULL if not logged in or write permission denied. 393294Seric ** 394294Seric ** Side Effects: 395294Seric ** none. 396294Seric ** 397294Seric ** WARNING: 398294Seric ** Return value is in a local buffer. 399294Seric ** 400294Seric ** Called By: 401294Seric ** savemail 402294Seric */ 403294Seric 404294Seric char * 405294Seric ttypath() 406294Seric { 407294Seric struct stat stbuf; 408294Seric register char *pathn; 409294Seric extern char *ttyname(); 4104081Seric extern char *getlogin(); 411294Seric 412294Seric /* compute the pathname of the controlling tty */ 4139369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4149369Seric (pathn = ttyname(0)) == NULL) 415294Seric { 416294Seric errno = 0; 417294Seric return (NULL); 418294Seric } 419294Seric 420294Seric /* see if we have write permission */ 4212967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 422294Seric { 423294Seric errno = 0; 424294Seric return (NULL); 425294Seric } 426294Seric 427294Seric /* see if the user is logged in */ 428294Seric if (getlogin() == NULL) 429294Seric return (NULL); 430294Seric 431294Seric /* looks good */ 432294Seric return (pathn); 433294Seric } 4342967Seric /* 4352967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4362967Seric ** 4372967Seric ** This routine can be supplied on a per-installation basis 4382967Seric ** to determine whether a person is allowed to send a message. 4392967Seric ** This allows restriction of certain types of internet 4402967Seric ** forwarding or registration of users. 4412967Seric ** 4422967Seric ** If the hosts are found to be incompatible, an error 44357454Seric ** message should be given using "usrerr" and 0 should 4442967Seric ** be returned. 4452967Seric ** 4464288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4474288Seric ** function; this should be done on huge messages. 4484288Seric ** 4492967Seric ** Parameters: 4502967Seric ** to -- the person being sent to. 4512967Seric ** 4522967Seric ** Returns: 45357459Seric ** an exit status 4542967Seric ** 4552967Seric ** Side Effects: 4562967Seric ** none (unless you include the usrerr stuff) 4572967Seric */ 4582967Seric 45955012Seric checkcompat(to, e) 4602967Seric register ADDRESS *to; 46155012Seric register ENVELOPE *e; 4622967Seric { 46312133Seric # ifdef lint 46412133Seric if (to == NULL) 46512133Seric to++; 46663834Seric # endif /* lint */ 46764402Seric 46864402Seric if (tTd(49, 1)) 46964402Seric printf("checkcompat(to=%s, from=%s)\n", 47064402Seric to->q_paddr, e->e_from.q_paddr); 47164402Seric 47210698Seric # ifdef EXAMPLE_CODE 47310698Seric /* this code is intended as an example only */ 4744437Seric register STAB *s; 4754437Seric 4764437Seric s = stab("arpa", ST_MAILER, ST_FIND); 47755012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4789369Seric to->q_mailer == s->s_mailer) 4794437Seric { 48058151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 48110698Seric /* NoReturn = TRUE; to supress return copy */ 48257459Seric return (EX_UNAVAILABLE); 4834437Seric } 48456795Seric # endif /* EXAMPLE_CODE */ 48557459Seric return (EX_OK); 4862967Seric } 4879369Seric /* 48864035Seric ** SETSIGNAL -- set a signal handler 48964035Seric ** 49064035Seric ** This is essentially old BSD "signal(3)". 49164035Seric */ 49264035Seric 49364035Seric setsig_t 49464035Seric setsignal(sig, handler) 49564035Seric int sig; 49664035Seric setsig_t handler; 49764035Seric { 49864380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 49964035Seric return signal(sig, handler); 50064035Seric #else 50164035Seric struct sigaction n, o; 50264035Seric 50364035Seric bzero(&n, sizeof n); 50464035Seric n.sa_handler = handler; 50564035Seric if (sigaction(sig, &n, &o) < 0) 50664035Seric return SIG_ERR; 50764035Seric return o.sa_handler; 50864035Seric #endif 50964035Seric } 51064035Seric /* 5119369Seric ** HOLDSIGS -- arrange to hold all signals 5129369Seric ** 5139369Seric ** Parameters: 5149369Seric ** none. 5159369Seric ** 5169369Seric ** Returns: 5179369Seric ** none. 5189369Seric ** 5199369Seric ** Side Effects: 5209369Seric ** Arranges that signals are held. 5219369Seric */ 5229369Seric 5239369Seric holdsigs() 5249369Seric { 5259369Seric } 5269369Seric /* 5279369Seric ** RLSESIGS -- arrange to release all signals 5289369Seric ** 5299369Seric ** This undoes the effect of holdsigs. 5309369Seric ** 5319369Seric ** Parameters: 5329369Seric ** none. 5339369Seric ** 5349369Seric ** Returns: 5359369Seric ** none. 5369369Seric ** 5379369Seric ** Side Effects: 5389369Seric ** Arranges that signals are released. 5399369Seric */ 5409369Seric 5419369Seric rlsesigs() 5429369Seric { 5439369Seric } 54414872Seric /* 54514872Seric ** GETLA -- get the current load average 54614872Seric ** 54714881Seric ** This code stolen from la.c. 54814881Seric ** 54914872Seric ** Parameters: 55014872Seric ** none. 55114872Seric ** 55214872Seric ** Returns: 55314872Seric ** The current load average as an integer. 55414872Seric ** 55514872Seric ** Side Effects: 55614872Seric ** none. 55714872Seric */ 55814872Seric 55951920Seric /* try to guess what style of load average we have */ 56051920Seric #define LA_ZERO 1 /* always return load average as zero */ 56164360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 56251920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 56351920Seric #define LA_SUBR 4 /* call getloadavg */ 56464295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 56564360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 56614872Seric 56763962Seric /* do guesses based on general OS type */ 56851920Seric #ifndef LA_TYPE 56963962Seric # define LA_TYPE LA_ZERO 57051920Seric #endif 57151920Seric 57264360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 57351920Seric 57414872Seric #include <nlist.h> 57514872Seric 57651920Seric #ifndef LA_AVENRUN 57763962Seric # ifdef SYSTEM5 57863962Seric # define LA_AVENRUN "avenrun" 57963962Seric # else 58063962Seric # define LA_AVENRUN "_avenrun" 58163962Seric # endif 58251920Seric #endif 58351920Seric 58451920Seric /* _PATH_UNIX should be defined in <paths.h> */ 58551920Seric #ifndef _PATH_UNIX 58663962Seric # if defined(SYSTEM5) 58763962Seric # define _PATH_UNIX "/unix" 58863962Seric # else 58963962Seric # define _PATH_UNIX "/vmunix" 59063962Seric # endif 59151920Seric #endif 59251920Seric 59314872Seric struct nlist Nl[] = 59414872Seric { 59551920Seric { LA_AVENRUN }, 59614872Seric #define X_AVENRUN 0 59714872Seric { 0 }, 59814872Seric }; 59914872Seric 60060583Seric #ifndef FSHIFT 60160583Seric # if defined(unixpc) 60260583Seric # define FSHIFT 5 60360583Seric # endif 60457736Seric 60560583Seric # if defined(__alpha) 60660583Seric # define FSHIFT 10 60760583Seric # endif 60857977Seric 60964360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 61051920Seric # define FSHIFT 8 61160583Seric # endif 61257736Seric #endif 61360583Seric 61464360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 61551920Seric # define FSCALE (1 << FSHIFT) 61651920Seric #endif 61740930Srick 61814872Seric getla() 61914872Seric { 62014872Seric static int kmem = -1; 62151920Seric #if LA_TYPE == LA_INT 62224943Seric long avenrun[3]; 62351920Seric #else 62464360Seric # if LA_TYPE == LA_SHORT 62564360Seric short avenrun[3]; 62664360Seric # else 62751920Seric double avenrun[3]; 62864360Seric # endif 62951920Seric #endif 63025615Seric extern off_t lseek(); 63157736Seric extern int errno; 63214872Seric 63314872Seric if (kmem < 0) 63414872Seric { 63524945Seric kmem = open("/dev/kmem", 0, 0); 63614872Seric if (kmem < 0) 63757736Seric { 63857736Seric if (tTd(3, 1)) 63957736Seric printf("getla: open(/dev/kmem): %s\n", 64057736Seric errstring(errno)); 64114872Seric return (-1); 64257736Seric } 64351920Seric (void) fcntl(kmem, F_SETFD, 1); 64457736Seric if (nlist(_PATH_UNIX, Nl) < 0) 64557736Seric { 64657736Seric if (tTd(3, 1)) 64757736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 64857736Seric errstring(errno)); 64914872Seric return (-1); 65057736Seric } 65159253Seric if (Nl[X_AVENRUN].n_value == 0) 65259253Seric { 65359253Seric if (tTd(3, 1)) 65459253Seric printf("getla: nlist(%s, %s) ==> 0\n", 65559253Seric _PATH_UNIX, LA_AVENRUN); 65659253Seric return (-1); 65759253Seric } 65814872Seric } 65957736Seric if (tTd(3, 20)) 66057736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 66124945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 66223118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 66319967Seric { 66419967Seric /* thank you Ian */ 66557736Seric if (tTd(3, 1)) 66657736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 66719967Seric return (-1); 66819967Seric } 66964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 67057736Seric if (tTd(3, 5)) 67157736Seric { 67257736Seric printf("getla: avenrun = %d", avenrun[0]); 67357736Seric if (tTd(3, 15)) 67457736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 67557736Seric printf("\n"); 67657736Seric } 67757736Seric if (tTd(3, 1)) 67857736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 67924943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 68051920Seric #else 68157736Seric if (tTd(3, 5)) 68257736Seric { 68357736Seric printf("getla: avenrun = %g", avenrun[0]); 68457736Seric if (tTd(3, 15)) 68557736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 68657736Seric printf("\n"); 68757736Seric } 68857736Seric if (tTd(3, 1)) 68957736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 69051920Seric return ((int) (avenrun[0] + 0.5)); 69151920Seric #endif 69214872Seric } 69314872Seric 69451773Seric #else 69551920Seric #if LA_TYPE == LA_SUBR 69651773Seric 69751773Seric getla() 69851773Seric { 69951920Seric double avenrun[3]; 70051920Seric 70151920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 70257736Seric { 70357736Seric if (tTd(3, 1)) 70457736Seric perror("getla: getloadavg failed:"); 70551920Seric return (-1); 70657736Seric } 70757736Seric if (tTd(3, 1)) 70857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 70951920Seric return ((int) (avenrun[0] + 0.5)); 71051773Seric } 71151773Seric 71251773Seric #else 71364295Seric #if LA_TYPE == LA_MACH 71451773Seric 71564295Seric /* 71664295Seric ** This has been tested on NeXT release 2.1. 71764295Seric */ 71864295Seric 71964295Seric #include <mach.h> 72064295Seric 72151773Seric getla() 72251773Seric { 72364295Seric processor_set_t default_set; 72464295Seric kern_return_t error; 72564295Seric unsigned int info_count; 72664295Seric struct processor_set_basic_info info; 72764295Seric host_t host; 72864295Seric 72964295Seric error = processor_set_default(host_self(), &default_set); 73064295Seric if (error != KERN_SUCCESS) 73164295Seric return -1; 73264295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 73364295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 73464295Seric &host, (processor_set_info_t)&info, 73564295Seric &info_count) != KERN_SUCCESS) 73664295Seric { 73764295Seric return -1; 73864295Seric } 73964295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 74064295Seric } 74164295Seric 74264295Seric 74364295Seric #else 74464295Seric 74564295Seric getla() 74664295Seric { 74757736Seric if (tTd(3, 1)) 74857736Seric printf("getla: ZERO\n"); 74951920Seric return (0); 75051773Seric } 75151773Seric 75251773Seric #endif 75351773Seric #endif 75464295Seric #endif 75524943Seric /* 75624943Seric ** SHOULDQUEUE -- should this message be queued or sent? 75724943Seric ** 75824943Seric ** Compares the message cost to the load average to decide. 75924943Seric ** 76024943Seric ** Parameters: 76124943Seric ** pri -- the priority of the message in question. 76257438Seric ** ctime -- the message creation time. 76324943Seric ** 76424943Seric ** Returns: 76524943Seric ** TRUE -- if this message should be queued up for the 76624943Seric ** time being. 76724943Seric ** FALSE -- if the load is low enough to send this message. 76824943Seric ** 76924943Seric ** Side Effects: 77024943Seric ** none. 77124943Seric */ 77224943Seric 77324943Seric bool 77457438Seric shouldqueue(pri, ctime) 77524943Seric long pri; 77657438Seric time_t ctime; 77724943Seric { 77851920Seric if (CurrentLA < QueueLA) 77924943Seric return (FALSE); 78058132Seric if (CurrentLA >= RefuseLA) 78158132Seric return (TRUE); 78251920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 78324943Seric } 78424943Seric /* 78553037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 78653037Seric ** 78753037Seric ** Parameters: 78853037Seric ** none. 78953037Seric ** 79053037Seric ** Returns: 79153037Seric ** TRUE if incoming SMTP connections should be refused 79253037Seric ** (for now). 79353037Seric ** FALSE if we should accept new work. 79453037Seric ** 79553037Seric ** Side Effects: 79653037Seric ** none. 79753037Seric */ 79853037Seric 79953037Seric bool 80053037Seric refuseconnections() 80153037Seric { 80259156Seric #ifdef XLA 80359156Seric if (!xla_smtp_ok()) 80459156Seric return TRUE; 80559156Seric #endif 80659156Seric 80753037Seric /* this is probably too simplistic */ 80858132Seric return (CurrentLA >= RefuseLA); 80953037Seric } 81053037Seric /* 81124943Seric ** SETPROCTITLE -- set process title for ps 81224943Seric ** 81324943Seric ** Parameters: 81458674Seric ** fmt -- a printf style format string. 81558674Seric ** a, b, c -- possible parameters to fmt. 81624943Seric ** 81724943Seric ** Returns: 81824943Seric ** none. 81924943Seric ** 82024943Seric ** Side Effects: 82124943Seric ** Clobbers argv of our main procedure so ps(1) will 82224943Seric ** display the title. 82324943Seric */ 82424943Seric 82558689Seric #ifdef SETPROCTITLE 82658689Seric # ifdef __hpux 82758689Seric # include <sys/pstat.h> 82858689Seric # endif 82959732Seric # ifdef BSD4_4 83059732Seric # include <machine/vmparam.h> 83159732Seric # include <sys/exec.h> 83264247Seric # ifdef __bsdi__ 83364247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 83464247Seric # endif 83559992Seric # ifdef PS_STRINGS 83659992Seric # define SETPROC_STATIC static 83759992Seric # endif 83859732Seric # endif 83959732Seric # ifndef SETPROC_STATIC 84059732Seric # define SETPROC_STATIC 84159732Seric # endif 84258689Seric #endif 84358689Seric 84424943Seric /*VARARGS1*/ 84557642Seric #ifdef __STDC__ 84657642Seric setproctitle(char *fmt, ...) 84757642Seric #else 84857642Seric setproctitle(fmt, va_alist) 84924943Seric char *fmt; 85057642Seric va_dcl 85157642Seric #endif 85224943Seric { 85324943Seric # ifdef SETPROCTITLE 85424943Seric register char *p; 85525049Seric register int i; 85659732Seric SETPROC_STATIC char buf[MAXLINE]; 85756852Seric VA_LOCAL_DECL 85858689Seric # ifdef __hpux 85958689Seric union pstun pst; 86058689Seric # endif 86124943Seric extern char **Argv; 86224943Seric extern char *LastArgv; 86324943Seric 86458674Seric p = buf; 86524943Seric 86658674Seric /* print sendmail: heading for grep */ 86758674Seric (void) strcpy(p, "sendmail: "); 86858674Seric p += strlen(p); 86924943Seric 87058674Seric /* print the argument string */ 87158674Seric VA_START(fmt); 87258674Seric (void) vsprintf(p, fmt, ap); 87356852Seric VA_END; 87454996Seric 87558674Seric i = strlen(buf); 87658689Seric 87758689Seric # ifdef __hpux 87858689Seric pst.pst_command = buf; 87958689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 88058689Seric # else 88159992Seric # ifdef PS_STRINGS 88259732Seric PS_STRINGS->ps_nargvstr = 1; 88359732Seric PS_STRINGS->ps_argvstr = buf; 88459732Seric # else 88554996Seric if (i > LastArgv - Argv[0] - 2) 88625049Seric { 88754996Seric i = LastArgv - Argv[0] - 2; 88858674Seric buf[i] = '\0'; 88925049Seric } 89058674Seric (void) strcpy(Argv[0], buf); 89154997Seric p = &Argv[0][i]; 89224943Seric while (p < LastArgv) 89324943Seric *p++ = ' '; 89459732Seric # endif 89558689Seric # endif 89656795Seric # endif /* SETPROCTITLE */ 89724943Seric } 89825698Seric /* 89925698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 90025698Seric ** 90125698Seric ** Parameters: 90225698Seric ** none. 90325698Seric ** 90425698Seric ** Returns: 90525698Seric ** none. 90625698Seric ** 90725698Seric ** Side Effects: 90825698Seric ** Picks up extant zombies. 90925698Seric */ 91025698Seric 91146928Sbostic void 91225698Seric reapchild() 91325698Seric { 91463966Seric # ifdef HASWAITPID 91560219Seric auto int status; 91660560Seric int count; 91760560Seric int pid; 91860219Seric 91960560Seric count = 0; 92060560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 92160560Seric { 92260560Seric if (count++ > 1000) 92360560Seric { 92460560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 92560560Seric pid, status); 92660560Seric break; 92760560Seric } 92860560Seric } 92960219Seric # else 93025698Seric # ifdef WNOHANG 93125698Seric union wait status; 93225698Seric 93363753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 93425698Seric continue; 93556795Seric # else /* WNOHANG */ 93625698Seric auto int status; 93725698Seric 93860219Seric while (wait(&status) > 0) 93925698Seric continue; 94056795Seric # endif /* WNOHANG */ 94160219Seric # endif 94264035Seric # ifdef SYS5SIGNALS 94364035Seric (void) setsignal(SIGCHLD, reapchild); 94458061Seric # endif 94525698Seric } 94655418Seric /* 94755418Seric ** UNSETENV -- remove a variable from the environment 94855418Seric ** 94955418Seric ** Not needed on newer systems. 95055418Seric ** 95155418Seric ** Parameters: 95255418Seric ** name -- the string name of the environment variable to be 95355418Seric ** deleted from the current environment. 95455418Seric ** 95555418Seric ** Returns: 95655418Seric ** none. 95755418Seric ** 95855418Seric ** Globals: 95955418Seric ** environ -- a pointer to the current environment. 96055418Seric ** 96155418Seric ** Side Effects: 96255418Seric ** Modifies environ. 96355418Seric */ 96455418Seric 96563962Seric #ifndef HASUNSETENV 96655418Seric 96755418Seric void 96855418Seric unsetenv(name) 96955418Seric char *name; 97055418Seric { 97155418Seric extern char **environ; 97255418Seric register char **pp; 97355418Seric int len = strlen(name); 97455418Seric 97555418Seric for (pp = environ; *pp != NULL; pp++) 97655418Seric { 97755418Seric if (strncmp(name, *pp, len) == 0 && 97855418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 97955418Seric break; 98055418Seric } 98155418Seric 98255418Seric for (; *pp != NULL; pp++) 98355418Seric *pp = pp[1]; 98455418Seric } 98555418Seric 98663962Seric #endif 98756215Seric /* 98856215Seric ** GETDTABLESIZE -- return number of file descriptors 98956215Seric ** 99056215Seric ** Only on non-BSD systems 99156215Seric ** 99256215Seric ** Parameters: 99356215Seric ** none 99456215Seric ** 99556215Seric ** Returns: 99656215Seric ** size of file descriptor table 99756215Seric ** 99856215Seric ** Side Effects: 99956215Seric ** none 100056215Seric */ 100156215Seric 100263787Seric #ifdef SOLARIS 100363787Seric # include <sys/resource.h> 100463787Seric #endif 100556215Seric 100656215Seric int 100763787Seric getdtsize() 100856215Seric { 100963787Seric #ifdef RLIMIT_NOFILE 101063787Seric struct rlimit rl; 101163787Seric 101263787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 101363787Seric return rl.rlim_cur; 101463787Seric #endif 101563787Seric 101664031Seric # ifdef HASGETDTABLESIZE 101764031Seric return getdtablesize(); 101864031Seric # else 101964031Seric # ifdef _SC_OPEN_MAX 102058689Seric return sysconf(_SC_OPEN_MAX); 102163787Seric # else 102256215Seric return NOFILE; 102363787Seric # endif 102458689Seric # endif 102556215Seric } 102657631Seric /* 102757631Seric ** UNAME -- get the UUCP name of this system. 102857631Seric */ 102957631Seric 103057943Seric #ifndef HASUNAME 103157631Seric 103257631Seric int 103357631Seric uname(name) 103457631Seric struct utsname *name; 103557631Seric { 103657631Seric FILE *file; 103757631Seric char *n; 103857631Seric 103957631Seric name->nodename[0] = '\0'; 104057631Seric 104157661Seric /* try /etc/whoami -- one line with the node name */ 104257631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 104357631Seric { 104457661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 104557631Seric (void) fclose(file); 104657661Seric n = strchr(name->nodename, '\n'); 104757631Seric if (n != NULL) 104857631Seric *n = '\0'; 104957631Seric if (name->nodename[0] != '\0') 105057631Seric return (0); 105157631Seric } 105257631Seric 105357661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 105457631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 105557631Seric { 105657631Seric char buf[MAXLINE]; 105757631Seric 105857631Seric while (fgets(buf, MAXLINE, file) != NULL) 105957631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 106057631Seric NODE_LENGTH, name->nodename) > 0) 106157631Seric break; 106257631Seric (void) fclose(file); 106357631Seric if (name->nodename[0] != '\0') 106457631Seric return (0); 106557631Seric } 106657631Seric 106757631Seric #ifdef TRUST_POPEN 106857631Seric /* 106957631Seric ** Popen is known to have security holes. 107057631Seric */ 107157631Seric 107257661Seric /* try uuname -l to return local name */ 107357631Seric if ((file = popen("uuname -l", "r")) != NULL) 107457631Seric { 107557661Seric (void) fgets(name, NODE_LENGTH + 1, file); 107657631Seric (void) pclose(file); 107757661Seric n = strchr(name, '\n'); 107857631Seric if (n != NULL) 107957631Seric *n = '\0'; 108057661Seric if (name->nodename[0] != '\0') 108157631Seric return (0); 108257631Seric } 108357631Seric #endif 108457631Seric 108557631Seric return (-1); 108657631Seric } 108757943Seric #endif /* HASUNAME */ 108858068Seric /* 108958068Seric ** INITGROUPS -- initialize groups 109058068Seric ** 109158068Seric ** Stub implementation for System V style systems 109258068Seric */ 109358068Seric 109458068Seric #ifndef HASINITGROUPS 109558068Seric 109658068Seric initgroups(name, basegid) 109758068Seric char *name; 109858068Seric int basegid; 109958068Seric { 110058068Seric return 0; 110158068Seric } 110258068Seric 110358068Seric #endif 110458082Seric /* 110559289Seric ** SETSID -- set session id (for non-POSIX systems) 110659289Seric */ 110759289Seric 110859289Seric #ifndef HASSETSID 110959289Seric 111063753Seric pid_t 111163753Seric setsid __P ((void)) 111259289Seric { 111364155Seric #ifdef TIOCNOTTY 111464155Seric int fd; 111564155Seric 111664155Seric fd = open("/dev/tty", 2); 111764155Seric if (fd >= 0) 111864155Seric { 111964155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 112064155Seric (void) close(fd); 112164155Seric } 112264155Seric #endif /* TIOCNOTTY */ 112359289Seric # ifdef SYSTEM5 112463753Seric return setpgrp(); 112563753Seric # else 112664155Seric return setpgid(0, getpid()); 112759289Seric # endif 112859289Seric } 112959289Seric 113059289Seric #endif 113159289Seric /* 113263969Seric ** GETOPT -- for old systems or systems with bogus implementations 113363969Seric */ 113463969Seric 113563969Seric #ifdef NEEDGETOPT 113663969Seric 113763969Seric /* 113863969Seric * Copyright (c) 1985 Regents of the University of California. 113963969Seric * All rights reserved. The Berkeley software License Agreement 114063969Seric * specifies the terms and conditions for redistribution. 114163969Seric */ 114263969Seric 114363969Seric 114463969Seric /* 114563969Seric ** this version hacked to add `atend' flag to allow state machine 114663969Seric ** to reset if invoked by the program to scan args for a 2nd time 114763969Seric */ 114863969Seric 114963969Seric #if defined(LIBC_SCCS) && !defined(lint) 115063969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 115164072Seric #endif /* LIBC_SCCS and not lint */ 115263969Seric 115363969Seric #include <stdio.h> 115463969Seric 115563969Seric /* 115663969Seric * get option letter from argument vector 115763969Seric */ 115863969Seric int opterr = 1, /* if error message should be printed */ 115963969Seric optind = 1, /* index into parent argv vector */ 116063969Seric optopt; /* character checked for validity */ 116163969Seric char *optarg; /* argument associated with option */ 116263969Seric 116363969Seric #define BADCH (int)'?' 116463969Seric #define EMSG "" 116563969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 116663969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 116763969Seric 116863969Seric getopt(nargc,nargv,ostr) 116963969Seric int nargc; 117063969Seric char **nargv, 117163969Seric *ostr; 117263969Seric { 117363969Seric static char *place = EMSG; /* option letter processing */ 117463969Seric static char atend = 0; 117563969Seric register char *oli; /* option letter list index */ 117663969Seric 117763969Seric if (atend) { 117863969Seric atend = 0; 117963969Seric place = EMSG; 118063969Seric } 118163969Seric if(!*place) { /* update scanning pointer */ 118264136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 118363969Seric atend++; 118463969Seric return(EOF); 118563969Seric } 118663969Seric if (*place == '-') { /* found "--" */ 118763969Seric ++optind; 118863969Seric atend++; 118963969Seric return(EOF); 119063969Seric } 119163969Seric } /* option letter okay? */ 119264125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 119364136Seric if (!*place) ++optind; 119463969Seric tell(": illegal option -- "); 119563969Seric } 119663969Seric if (*++oli != ':') { /* don't need argument */ 119763969Seric optarg = NULL; 119863969Seric if (!*place) ++optind; 119963969Seric } 120063969Seric else { /* need an argument */ 120163969Seric if (*place) optarg = place; /* no white space */ 120263969Seric else if (nargc <= ++optind) { /* no arg */ 120363969Seric place = EMSG; 120463969Seric tell(": option requires an argument -- "); 120563969Seric } 120663969Seric else optarg = nargv[optind]; /* white space */ 120763969Seric place = EMSG; 120863969Seric ++optind; 120963969Seric } 121063969Seric return(optopt); /* dump back option letter */ 121163969Seric } 121263969Seric 121363969Seric #endif 121463969Seric /* 121563969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 121663969Seric */ 121763969Seric 121863969Seric #ifdef NEEDVPRINTF 121963969Seric 122063969Seric #define MAXARG 16 122163969Seric 122263969Seric vfprintf(fp, fmt, ap) 122363969Seric FILE * fp; 122463969Seric char * fmt; 122563969Seric char ** ap; 122663969Seric { 122763969Seric char * bp[MAXARG]; 122863969Seric int i = 0; 122963969Seric 123063969Seric while (*ap && i < MAXARG) 123163969Seric bp[i++] = *ap++; 123263969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 123363969Seric bp[4], bp[5], bp[6], bp[7], 123463969Seric bp[8], bp[9], bp[10], bp[11], 123563969Seric bp[12], bp[13], bp[14], bp[15]); 123663969Seric } 123763969Seric 123863969Seric vsprintf(s, fmt, ap) 123963969Seric char * s; 124063969Seric char * fmt; 124163969Seric char ** ap; 124263969Seric { 124363969Seric char * bp[MAXARG]; 124463969Seric int i = 0; 124563969Seric 124663969Seric while (*ap && i < MAXARG) 124763969Seric bp[i++] = *ap++; 124863969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 124963969Seric bp[4], bp[5], bp[6], bp[7], 125063969Seric bp[8], bp[9], bp[10], bp[11], 125163969Seric bp[12], bp[13], bp[14], bp[15]); 125263969Seric } 125363969Seric 125463969Seric #endif 125563969Seric /* 125664311Seric ** FREESPACE -- see how much free space is on the queue filesystem 125758082Seric ** 125858082Seric ** Only implemented if you have statfs. 125958082Seric ** 126058082Seric ** Parameters: 126164311Seric ** dir -- the directory in question. 126264311Seric ** bsize -- a variable into which the filesystem 126364311Seric ** block size is stored. 126458082Seric ** 126558082Seric ** Returns: 126664311Seric ** The number of bytes free on the queue filesystem. 126764311Seric ** -1 if the statfs call fails. 126864311Seric ** 126964311Seric ** Side effects: 127064311Seric ** Puts the filesystem block size into bsize. 127158082Seric */ 127258082Seric 127358082Seric #ifdef HASSTATFS 127458157Seric # undef HASUSTAT 127558157Seric #endif 127658157Seric 127758157Seric #if defined(HASUSTAT) 127858157Seric # include <ustat.h> 127958157Seric #endif 128058157Seric 128158157Seric #ifdef HASSTATFS 128264314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) 128358133Seric # include <sys/statfs.h> 128458133Seric # else 128564380Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) 128658133Seric # include <sys/vfs.h> 128758133Seric # else 128858157Seric # include <sys/mount.h> 128958133Seric # endif 129058133Seric # endif 129158082Seric #endif 129258082Seric 129364311Seric long 129464311Seric freespace(dir, bsize) 129564311Seric char *dir; 129664311Seric long *bsize; 129758082Seric { 129858160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 129958157Seric # if defined(HASUSTAT) 130058153Seric struct ustat fs; 130158153Seric struct stat statbuf; 130258366Seric # define FSBLOCKSIZE DEV_BSIZE 130358157Seric # define f_bavail f_tfree 130458157Seric # else 130558157Seric # if defined(ultrix) 130658157Seric struct fs_data fs; 130758157Seric # define f_bavail fd_bfreen 130858366Seric # define FSBLOCKSIZE fs.fd_bsize 130958153Seric # else 131058082Seric struct statfs fs; 131158366Seric # define FSBLOCKSIZE fs.f_bsize 131264336Seric # if defined(_SCO_unix_) || defined(IRIX) 131363987Seric # define f_bavail f_bfree 131463987Seric # endif 131558153Seric # endif 131658133Seric # endif 131758082Seric extern int errno; 131858082Seric 131958157Seric # if defined(HASUSTAT) 132064311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 132158157Seric # else 132264314Seric # if defined(IRIX) || defined(apollo) 132364311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 132458157Seric # else 132558157Seric # if defined(ultrix) 132664311Seric if (statfs(dir, &fs) > 0) 132758153Seric # else 132864311Seric if (statfs(dir, &fs) == 0) 132958153Seric # endif 133058133Seric # endif 133158133Seric # endif 133258133Seric { 133364311Seric if (bsize != NULL) 133464311Seric *bsize = FSBLOCKSIZE; 133564311Seric return (fs.f_bavail); 133664311Seric } 133764337Seric #endif 133864311Seric return (-1); 133964311Seric } 134064311Seric /* 134164311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 134264311Seric ** 134364311Seric ** Only implemented if you have statfs. 134464311Seric ** 134564311Seric ** Parameters: 134664311Seric ** msize -- the size to check against. If zero, we don't yet 134764311Seric ** know how big the message will be, so just check for 134864311Seric ** a "reasonable" amount. 134964311Seric ** 135064311Seric ** Returns: 135164311Seric ** TRUE if there is enough space. 135264311Seric ** FALSE otherwise. 135364311Seric */ 135464311Seric 135564311Seric bool 135664311Seric enoughspace(msize) 135764311Seric long msize; 135864311Seric { 135964311Seric long bfree, bsize; 136064311Seric 136164311Seric if (MinBlocksFree <= 0 && msize <= 0) 136264311Seric { 136358133Seric if (tTd(4, 80)) 136464311Seric printf("enoughspace: no threshold\n"); 136564311Seric return TRUE; 136664311Seric } 136764311Seric 136864311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 136964311Seric { 137064311Seric if (tTd(4, 80)) 137158333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 137264311Seric bfree, msize); 137358333Seric 137458333Seric /* convert msize to block count */ 137564311Seric msize = msize / bsize + 1; 137658333Seric if (MinBlocksFree >= 0) 137758333Seric msize += MinBlocksFree; 137858333Seric 137964311Seric if (bfree < msize) 138058090Seric { 138158090Seric #ifdef LOG 138258090Seric if (LogLevel > 0) 138364254Seric syslog(LOG_ALERT, 138464254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 138564311Seric CurEnv->e_id, bfree, 138664254Seric CurHostName, msize, QueueDir); 138758090Seric #endif 138858082Seric return FALSE; 138958090Seric } 139058082Seric } 139158082Seric else if (tTd(4, 80)) 139258333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 139358333Seric MinBlocksFree, msize, errstring(errno)); 139458082Seric return TRUE; 139558082Seric } 139658542Seric /* 139758542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 139858542Seric ** 139958542Seric ** This looks at an errno value and tells if this is likely to 140058542Seric ** go away if retried later. 140158542Seric ** 140258542Seric ** Parameters: 140358542Seric ** err -- the errno code to classify. 140458542Seric ** 140558542Seric ** Returns: 140658542Seric ** TRUE if this is probably transient. 140758542Seric ** FALSE otherwise. 140858542Seric */ 140958542Seric 141058542Seric bool 141158542Seric transienterror(err) 141258542Seric int err; 141358542Seric { 141458542Seric switch (err) 141558542Seric { 141658542Seric case EIO: /* I/O error */ 141758542Seric case ENXIO: /* Device not configured */ 141858542Seric case EAGAIN: /* Resource temporarily unavailable */ 141958542Seric case ENOMEM: /* Cannot allocate memory */ 142058542Seric case ENODEV: /* Operation not supported by device */ 142158542Seric case ENFILE: /* Too many open files in system */ 142258542Seric case EMFILE: /* Too many open files */ 142358542Seric case ENOSPC: /* No space left on device */ 142458542Seric #ifdef ETIMEDOUT 142558542Seric case ETIMEDOUT: /* Connection timed out */ 142658542Seric #endif 142758542Seric #ifdef ESTALE 142858542Seric case ESTALE: /* Stale NFS file handle */ 142958542Seric #endif 143058542Seric #ifdef ENETDOWN 143158542Seric case ENETDOWN: /* Network is down */ 143258542Seric #endif 143358542Seric #ifdef ENETUNREACH 143458542Seric case ENETUNREACH: /* Network is unreachable */ 143558542Seric #endif 143658542Seric #ifdef ENETRESET 143758542Seric case ENETRESET: /* Network dropped connection on reset */ 143858542Seric #endif 143958542Seric #ifdef ECONNABORTED 144058542Seric case ECONNABORTED: /* Software caused connection abort */ 144158542Seric #endif 144258542Seric #ifdef ECONNRESET 144358542Seric case ECONNRESET: /* Connection reset by peer */ 144458542Seric #endif 144558542Seric #ifdef ENOBUFS 144658542Seric case ENOBUFS: /* No buffer space available */ 144758542Seric #endif 144858542Seric #ifdef ESHUTDOWN 144958542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 145058542Seric #endif 145158542Seric #ifdef ECONNREFUSED 145258542Seric case ECONNREFUSED: /* Connection refused */ 145358542Seric #endif 145458542Seric #ifdef EHOSTDOWN 145558542Seric case EHOSTDOWN: /* Host is down */ 145658542Seric #endif 145758542Seric #ifdef EHOSTUNREACH 145858542Seric case EHOSTUNREACH: /* No route to host */ 145958542Seric #endif 146058542Seric #ifdef EDQUOT 146158542Seric case EDQUOT: /* Disc quota exceeded */ 146258542Seric #endif 146358542Seric #ifdef EPROCLIM 146458542Seric case EPROCLIM: /* Too many processes */ 146558542Seric #endif 146658542Seric #ifdef EUSERS 146758542Seric case EUSERS: /* Too many users */ 146858542Seric #endif 146958542Seric #ifdef EDEADLK 147058542Seric case EDEADLK: /* Resource deadlock avoided */ 147158542Seric #endif 147258542Seric #ifdef EISCONN 147358542Seric case EISCONN: /* Socket already connected */ 147458542Seric #endif 147558542Seric #ifdef EINPROGRESS 147658542Seric case EINPROGRESS: /* Operation now in progress */ 147758542Seric #endif 147858542Seric #ifdef EALREADY 147958542Seric case EALREADY: /* Operation already in progress */ 148058542Seric #endif 148158542Seric #ifdef EADDRINUSE 148258542Seric case EADDRINUSE: /* Address already in use */ 148358542Seric #endif 148458542Seric #ifdef EADDRNOTAVAIL 148558542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 148658542Seric #endif 148763834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 148858542Seric case ENOSR: /* Out of streams resources */ 148958542Seric #endif 149058542Seric return TRUE; 149158542Seric } 149258542Seric 149358542Seric /* nope, must be permanent */ 149458542Seric return FALSE; 149558542Seric } 149658689Seric /* 149764035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 149858689Seric ** 149958689Seric ** Parameters: 150058689Seric ** fd -- the file descriptor of the file. 150158689Seric ** filename -- the file name (for error messages). 150264335Seric ** ext -- the filename extension. 150358689Seric ** type -- type of the lock. Bits can be: 150458689Seric ** LOCK_EX -- exclusive lock. 150558689Seric ** LOCK_NB -- non-blocking. 150658689Seric ** 150758689Seric ** Returns: 150858689Seric ** TRUE if the lock was acquired. 150958689Seric ** FALSE otherwise. 151058689Seric */ 151158689Seric 151258689Seric bool 151364335Seric lockfile(fd, filename, ext, type) 151458689Seric int fd; 151558689Seric char *filename; 151664335Seric char *ext; 151758689Seric int type; 151858689Seric { 151964035Seric # ifndef HASFLOCK 152058689Seric int action; 152158689Seric struct flock lfd; 152264335Seric 152364335Seric if (ext == NULL) 152464335Seric ext = ""; 152564035Seric 152663983Seric bzero(&lfd, sizeof lfd); 152759447Seric if (bitset(LOCK_UN, type)) 152859447Seric lfd.l_type = F_UNLCK; 152959447Seric else if (bitset(LOCK_EX, type)) 153058689Seric lfd.l_type = F_WRLCK; 153158689Seric else 153258689Seric lfd.l_type = F_RDLCK; 153358689Seric 153458689Seric if (bitset(LOCK_NB, type)) 153558689Seric action = F_SETLK; 153658689Seric else 153758689Seric action = F_SETLKW; 153858689Seric 153964264Seric if (tTd(55, 60)) 154064335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 154164335Seric filename, ext, action, lfd.l_type); 154264264Seric 154358689Seric if (fcntl(fd, action, &lfd) >= 0) 154464264Seric { 154564264Seric if (tTd(55, 60)) 154664264Seric printf("SUCCESS\n"); 154758689Seric return TRUE; 154864264Seric } 154958689Seric 155064264Seric if (tTd(55, 60)) 155164264Seric printf("(%s) ", errstring(errno)); 155264264Seric 155364136Seric /* 155464136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 155564136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 155664136Seric ** as type "tmp" (that is, served from swap space), the 155764136Seric ** previous fcntl will fail with "Invalid argument" errors. 155864136Seric ** Since this is fairly common during testing, we will assume 155964136Seric ** that this indicates that the lock is successfully grabbed. 156064136Seric */ 156164136Seric 156264136Seric if (errno == EINVAL) 156364264Seric { 156464264Seric if (tTd(55, 60)) 156564264Seric printf("SUCCESS\n"); 156664136Seric return TRUE; 156764264Seric } 156864136Seric 156958689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 157064378Seric { 157164378Seric int omode = -1; 157264378Seric # ifdef F_GETFL 157364378Seric int oerrno = errno; 157464378Seric 157564378Seric (void) fcntl(fd, F_GETFL, &omode); 157664378Seric errno = oerrno; 157764378Seric # endif 157864378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 157964378Seric filename, ext, fd, type, omode, geteuid()); 158064378Seric } 158158689Seric # else 158264335Seric if (ext == NULL) 158364335Seric ext = ""; 158464335Seric 158564264Seric if (tTd(55, 60)) 158664335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 158764264Seric 158858689Seric if (flock(fd, type) >= 0) 158964264Seric { 159064264Seric if (tTd(55, 60)) 159164264Seric printf("SUCCESS\n"); 159258689Seric return TRUE; 159364264Seric } 159458689Seric 159564264Seric if (tTd(55, 60)) 159664264Seric printf("(%s) ", errstring(errno)); 159764264Seric 159858689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 159964378Seric { 160064378Seric int omode = -1; 160164378Seric # ifdef F_GETFL 160264378Seric int oerrno = errno; 160364378Seric 160464378Seric (void) fcntl(fd, F_GETFL, &omode); 160564378Seric errno = oerrno; 160664378Seric # endif 160764378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 160864378Seric filename, ext, fd, type, omode, geteuid()); 160964386Seric } 161058689Seric # endif 161164264Seric if (tTd(55, 60)) 161264264Seric printf("FAIL\n"); 161358689Seric return FALSE; 161458689Seric } 1615*64556Seric /* 1616*64556Seric ** GETCFNAME -- return the name of the .cf file. 1617*64556Seric ** 1618*64556Seric ** Some systems (e.g., NeXT) determine this dynamically. 1619*64556Seric */ 1620*64556Seric 1621*64556Seric char * 1622*64556Seric getcfname() 1623*64556Seric { 1624*64556Seric if (ConfFile != NULL) 1625*64556Seric return ConfFile; 1626*64556Seric return _PATH_SENDMAILCF; 1627*64556Seric } 1628