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*64402Seric static char sccsid[] = "@(#)conf.c 8.34 (Berkeley) 09/04/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 */ 10558082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1069039Seric 1079064Seric 1089064Seric 1099039Seric /* 11058082Seric ** Privacy values 11158082Seric */ 11258082Seric 11358082Seric struct prival PrivacyValues[] = 11458082Seric { 11558082Seric "public", PRIV_PUBLIC, 11658082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11758114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11858082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11958082Seric "noexpn", PRIV_NOEXPN, 12058082Seric "novrfy", PRIV_NOVRFY, 12164333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12264333Seric "restrictqrun", PRIV_RESTRICTQRUN, 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 */ 468*64402Seric 469*64402Seric if (tTd(49, 1)) 470*64402Seric printf("checkcompat(to=%s, from=%s)\n", 471*64402Seric to->q_paddr, e->e_from.q_paddr); 472*64402Seric 47310698Seric # ifdef EXAMPLE_CODE 47410698Seric /* this code is intended as an example only */ 4754437Seric register STAB *s; 4764437Seric 4774437Seric s = stab("arpa", ST_MAILER, ST_FIND); 47855012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4799369Seric to->q_mailer == s->s_mailer) 4804437Seric { 48158151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 48210698Seric /* NoReturn = TRUE; to supress return copy */ 48357459Seric return (EX_UNAVAILABLE); 4844437Seric } 48556795Seric # endif /* EXAMPLE_CODE */ 48657459Seric return (EX_OK); 4872967Seric } 4889369Seric /* 48964035Seric ** SETSIGNAL -- set a signal handler 49064035Seric ** 49164035Seric ** This is essentially old BSD "signal(3)". 49264035Seric */ 49364035Seric 49464035Seric setsig_t 49564035Seric setsignal(sig, handler) 49664035Seric int sig; 49764035Seric setsig_t handler; 49864035Seric { 49964380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 50064035Seric return signal(sig, handler); 50164035Seric #else 50264035Seric struct sigaction n, o; 50364035Seric 50464035Seric bzero(&n, sizeof n); 50564035Seric n.sa_handler = handler; 50664035Seric if (sigaction(sig, &n, &o) < 0) 50764035Seric return SIG_ERR; 50864035Seric return o.sa_handler; 50964035Seric #endif 51064035Seric } 51164035Seric /* 5129369Seric ** HOLDSIGS -- arrange to hold all signals 5139369Seric ** 5149369Seric ** Parameters: 5159369Seric ** none. 5169369Seric ** 5179369Seric ** Returns: 5189369Seric ** none. 5199369Seric ** 5209369Seric ** Side Effects: 5219369Seric ** Arranges that signals are held. 5229369Seric */ 5239369Seric 5249369Seric holdsigs() 5259369Seric { 5269369Seric } 5279369Seric /* 5289369Seric ** RLSESIGS -- arrange to release all signals 5299369Seric ** 5309369Seric ** This undoes the effect of holdsigs. 5319369Seric ** 5329369Seric ** Parameters: 5339369Seric ** none. 5349369Seric ** 5359369Seric ** Returns: 5369369Seric ** none. 5379369Seric ** 5389369Seric ** Side Effects: 5399369Seric ** Arranges that signals are released. 5409369Seric */ 5419369Seric 5429369Seric rlsesigs() 5439369Seric { 5449369Seric } 54514872Seric /* 54614872Seric ** GETLA -- get the current load average 54714872Seric ** 54814881Seric ** This code stolen from la.c. 54914881Seric ** 55014872Seric ** Parameters: 55114872Seric ** none. 55214872Seric ** 55314872Seric ** Returns: 55414872Seric ** The current load average as an integer. 55514872Seric ** 55614872Seric ** Side Effects: 55714872Seric ** none. 55814872Seric */ 55914872Seric 56051920Seric /* try to guess what style of load average we have */ 56151920Seric #define LA_ZERO 1 /* always return load average as zero */ 56264360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 56351920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 56451920Seric #define LA_SUBR 4 /* call getloadavg */ 56564295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 56664360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 56714872Seric 56863962Seric /* do guesses based on general OS type */ 56951920Seric #ifndef LA_TYPE 57063962Seric # define LA_TYPE LA_ZERO 57151920Seric #endif 57251920Seric 57364360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 57451920Seric 57514872Seric #include <nlist.h> 57614872Seric 57751920Seric #ifndef LA_AVENRUN 57863962Seric # ifdef SYSTEM5 57963962Seric # define LA_AVENRUN "avenrun" 58063962Seric # else 58163962Seric # define LA_AVENRUN "_avenrun" 58263962Seric # endif 58351920Seric #endif 58451920Seric 58551920Seric /* _PATH_UNIX should be defined in <paths.h> */ 58651920Seric #ifndef _PATH_UNIX 58763962Seric # if defined(SYSTEM5) 58863962Seric # define _PATH_UNIX "/unix" 58963962Seric # else 59063962Seric # define _PATH_UNIX "/vmunix" 59163962Seric # endif 59251920Seric #endif 59351920Seric 59414872Seric struct nlist Nl[] = 59514872Seric { 59651920Seric { LA_AVENRUN }, 59714872Seric #define X_AVENRUN 0 59814872Seric { 0 }, 59914872Seric }; 60014872Seric 60160583Seric #ifndef FSHIFT 60260583Seric # if defined(unixpc) 60360583Seric # define FSHIFT 5 60460583Seric # endif 60557736Seric 60660583Seric # if defined(__alpha) 60760583Seric # define FSHIFT 10 60860583Seric # endif 60957977Seric 61064360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 61151920Seric # define FSHIFT 8 61260583Seric # endif 61357736Seric #endif 61460583Seric 61564360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 61651920Seric # define FSCALE (1 << FSHIFT) 61751920Seric #endif 61840930Srick 61914872Seric getla() 62014872Seric { 62114872Seric static int kmem = -1; 62251920Seric #if LA_TYPE == LA_INT 62324943Seric long avenrun[3]; 62451920Seric #else 62564360Seric # if LA_TYPE == LA_SHORT 62664360Seric short avenrun[3]; 62764360Seric # else 62851920Seric double avenrun[3]; 62964360Seric # endif 63051920Seric #endif 63125615Seric extern off_t lseek(); 63257736Seric extern int errno; 63314872Seric 63414872Seric if (kmem < 0) 63514872Seric { 63624945Seric kmem = open("/dev/kmem", 0, 0); 63714872Seric if (kmem < 0) 63857736Seric { 63957736Seric if (tTd(3, 1)) 64057736Seric printf("getla: open(/dev/kmem): %s\n", 64157736Seric errstring(errno)); 64214872Seric return (-1); 64357736Seric } 64451920Seric (void) fcntl(kmem, F_SETFD, 1); 64557736Seric if (nlist(_PATH_UNIX, Nl) < 0) 64657736Seric { 64757736Seric if (tTd(3, 1)) 64857736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 64957736Seric errstring(errno)); 65014872Seric return (-1); 65157736Seric } 65259253Seric if (Nl[X_AVENRUN].n_value == 0) 65359253Seric { 65459253Seric if (tTd(3, 1)) 65559253Seric printf("getla: nlist(%s, %s) ==> 0\n", 65659253Seric _PATH_UNIX, LA_AVENRUN); 65759253Seric return (-1); 65859253Seric } 65914872Seric } 66057736Seric if (tTd(3, 20)) 66157736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 66224945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 66323118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 66419967Seric { 66519967Seric /* thank you Ian */ 66657736Seric if (tTd(3, 1)) 66757736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 66819967Seric return (-1); 66919967Seric } 67064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 67157736Seric if (tTd(3, 5)) 67257736Seric { 67357736Seric printf("getla: avenrun = %d", avenrun[0]); 67457736Seric if (tTd(3, 15)) 67557736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 67657736Seric printf("\n"); 67757736Seric } 67857736Seric if (tTd(3, 1)) 67957736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 68024943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 68151920Seric #else 68257736Seric if (tTd(3, 5)) 68357736Seric { 68457736Seric printf("getla: avenrun = %g", avenrun[0]); 68557736Seric if (tTd(3, 15)) 68657736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 68757736Seric printf("\n"); 68857736Seric } 68957736Seric if (tTd(3, 1)) 69057736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 69151920Seric return ((int) (avenrun[0] + 0.5)); 69251920Seric #endif 69314872Seric } 69414872Seric 69551773Seric #else 69651920Seric #if LA_TYPE == LA_SUBR 69751773Seric 69851773Seric getla() 69951773Seric { 70051920Seric double avenrun[3]; 70151920Seric 70251920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 70357736Seric { 70457736Seric if (tTd(3, 1)) 70557736Seric perror("getla: getloadavg failed:"); 70651920Seric return (-1); 70757736Seric } 70857736Seric if (tTd(3, 1)) 70957736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 71051920Seric return ((int) (avenrun[0] + 0.5)); 71151773Seric } 71251773Seric 71351773Seric #else 71464295Seric #if LA_TYPE == LA_MACH 71551773Seric 71664295Seric /* 71764295Seric ** This has been tested on NeXT release 2.1. 71864295Seric */ 71964295Seric 72064295Seric #include <mach.h> 72164295Seric 72251773Seric getla() 72351773Seric { 72464295Seric processor_set_t default_set; 72564295Seric kern_return_t error; 72664295Seric unsigned int info_count; 72764295Seric struct processor_set_basic_info info; 72864295Seric host_t host; 72964295Seric 73064295Seric error = processor_set_default(host_self(), &default_set); 73164295Seric if (error != KERN_SUCCESS) 73264295Seric return -1; 73364295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 73464295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 73564295Seric &host, (processor_set_info_t)&info, 73664295Seric &info_count) != KERN_SUCCESS) 73764295Seric { 73864295Seric return -1; 73964295Seric } 74064295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 74164295Seric } 74264295Seric 74364295Seric 74464295Seric #else 74564295Seric 74664295Seric getla() 74764295Seric { 74857736Seric if (tTd(3, 1)) 74957736Seric printf("getla: ZERO\n"); 75051920Seric return (0); 75151773Seric } 75251773Seric 75351773Seric #endif 75451773Seric #endif 75564295Seric #endif 75624943Seric /* 75724943Seric ** SHOULDQUEUE -- should this message be queued or sent? 75824943Seric ** 75924943Seric ** Compares the message cost to the load average to decide. 76024943Seric ** 76124943Seric ** Parameters: 76224943Seric ** pri -- the priority of the message in question. 76357438Seric ** ctime -- the message creation time. 76424943Seric ** 76524943Seric ** Returns: 76624943Seric ** TRUE -- if this message should be queued up for the 76724943Seric ** time being. 76824943Seric ** FALSE -- if the load is low enough to send this message. 76924943Seric ** 77024943Seric ** Side Effects: 77124943Seric ** none. 77224943Seric */ 77324943Seric 77424943Seric bool 77557438Seric shouldqueue(pri, ctime) 77624943Seric long pri; 77757438Seric time_t ctime; 77824943Seric { 77951920Seric if (CurrentLA < QueueLA) 78024943Seric return (FALSE); 78158132Seric if (CurrentLA >= RefuseLA) 78258132Seric return (TRUE); 78351920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 78424943Seric } 78524943Seric /* 78653037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 78753037Seric ** 78853037Seric ** Parameters: 78953037Seric ** none. 79053037Seric ** 79153037Seric ** Returns: 79253037Seric ** TRUE if incoming SMTP connections should be refused 79353037Seric ** (for now). 79453037Seric ** FALSE if we should accept new work. 79553037Seric ** 79653037Seric ** Side Effects: 79753037Seric ** none. 79853037Seric */ 79953037Seric 80053037Seric bool 80153037Seric refuseconnections() 80253037Seric { 80359156Seric #ifdef XLA 80459156Seric if (!xla_smtp_ok()) 80559156Seric return TRUE; 80659156Seric #endif 80759156Seric 80853037Seric /* this is probably too simplistic */ 80958132Seric return (CurrentLA >= RefuseLA); 81053037Seric } 81153037Seric /* 81224943Seric ** SETPROCTITLE -- set process title for ps 81324943Seric ** 81424943Seric ** Parameters: 81558674Seric ** fmt -- a printf style format string. 81658674Seric ** a, b, c -- possible parameters to fmt. 81724943Seric ** 81824943Seric ** Returns: 81924943Seric ** none. 82024943Seric ** 82124943Seric ** Side Effects: 82224943Seric ** Clobbers argv of our main procedure so ps(1) will 82324943Seric ** display the title. 82424943Seric */ 82524943Seric 82658689Seric #ifdef SETPROCTITLE 82758689Seric # ifdef __hpux 82858689Seric # include <sys/pstat.h> 82958689Seric # endif 83059732Seric # ifdef BSD4_4 83159732Seric # include <machine/vmparam.h> 83259732Seric # include <sys/exec.h> 83364247Seric # ifdef __bsdi__ 83464247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 83564247Seric # endif 83659992Seric # ifdef PS_STRINGS 83759992Seric # define SETPROC_STATIC static 83859992Seric # endif 83959732Seric # endif 84059732Seric # ifndef SETPROC_STATIC 84159732Seric # define SETPROC_STATIC 84259732Seric # endif 84358689Seric #endif 84458689Seric 84524943Seric /*VARARGS1*/ 84657642Seric #ifdef __STDC__ 84757642Seric setproctitle(char *fmt, ...) 84857642Seric #else 84957642Seric setproctitle(fmt, va_alist) 85024943Seric char *fmt; 85157642Seric va_dcl 85257642Seric #endif 85324943Seric { 85424943Seric # ifdef SETPROCTITLE 85524943Seric register char *p; 85625049Seric register int i; 85759732Seric SETPROC_STATIC char buf[MAXLINE]; 85856852Seric VA_LOCAL_DECL 85958689Seric # ifdef __hpux 86058689Seric union pstun pst; 86158689Seric # endif 86224943Seric extern char **Argv; 86324943Seric extern char *LastArgv; 86424943Seric 86558674Seric p = buf; 86624943Seric 86758674Seric /* print sendmail: heading for grep */ 86858674Seric (void) strcpy(p, "sendmail: "); 86958674Seric p += strlen(p); 87024943Seric 87158674Seric /* print the argument string */ 87258674Seric VA_START(fmt); 87358674Seric (void) vsprintf(p, fmt, ap); 87456852Seric VA_END; 87554996Seric 87658674Seric i = strlen(buf); 87758689Seric 87858689Seric # ifdef __hpux 87958689Seric pst.pst_command = buf; 88058689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 88158689Seric # else 88259992Seric # ifdef PS_STRINGS 88359732Seric PS_STRINGS->ps_nargvstr = 1; 88459732Seric PS_STRINGS->ps_argvstr = buf; 88559732Seric # else 88654996Seric if (i > LastArgv - Argv[0] - 2) 88725049Seric { 88854996Seric i = LastArgv - Argv[0] - 2; 88958674Seric buf[i] = '\0'; 89025049Seric } 89158674Seric (void) strcpy(Argv[0], buf); 89254997Seric p = &Argv[0][i]; 89324943Seric while (p < LastArgv) 89424943Seric *p++ = ' '; 89559732Seric # endif 89658689Seric # endif 89756795Seric # endif /* SETPROCTITLE */ 89824943Seric } 89925698Seric /* 90025698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 90125698Seric ** 90225698Seric ** Parameters: 90325698Seric ** none. 90425698Seric ** 90525698Seric ** Returns: 90625698Seric ** none. 90725698Seric ** 90825698Seric ** Side Effects: 90925698Seric ** Picks up extant zombies. 91025698Seric */ 91125698Seric 91225698Seric # include <sys/wait.h> 91325698Seric 91446928Sbostic void 91525698Seric reapchild() 91625698Seric { 91763966Seric # ifdef HASWAITPID 91860219Seric auto int status; 91960560Seric int count; 92060560Seric int pid; 92160219Seric 92260560Seric count = 0; 92360560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 92460560Seric { 92560560Seric if (count++ > 1000) 92660560Seric { 92760560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 92860560Seric pid, status); 92960560Seric break; 93060560Seric } 93160560Seric } 93260219Seric # else 93325698Seric # ifdef WNOHANG 93425698Seric union wait status; 93525698Seric 93663753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 93725698Seric continue; 93856795Seric # else /* WNOHANG */ 93925698Seric auto int status; 94025698Seric 94160219Seric while (wait(&status) > 0) 94225698Seric continue; 94356795Seric # endif /* WNOHANG */ 94460219Seric # endif 94564035Seric # ifdef SYS5SIGNALS 94664035Seric (void) setsignal(SIGCHLD, reapchild); 94758061Seric # endif 94825698Seric } 94955418Seric /* 95055418Seric ** UNSETENV -- remove a variable from the environment 95155418Seric ** 95255418Seric ** Not needed on newer systems. 95355418Seric ** 95455418Seric ** Parameters: 95555418Seric ** name -- the string name of the environment variable to be 95655418Seric ** deleted from the current environment. 95755418Seric ** 95855418Seric ** Returns: 95955418Seric ** none. 96055418Seric ** 96155418Seric ** Globals: 96255418Seric ** environ -- a pointer to the current environment. 96355418Seric ** 96455418Seric ** Side Effects: 96555418Seric ** Modifies environ. 96655418Seric */ 96755418Seric 96863962Seric #ifndef HASUNSETENV 96955418Seric 97055418Seric void 97155418Seric unsetenv(name) 97255418Seric char *name; 97355418Seric { 97455418Seric extern char **environ; 97555418Seric register char **pp; 97655418Seric int len = strlen(name); 97755418Seric 97855418Seric for (pp = environ; *pp != NULL; pp++) 97955418Seric { 98055418Seric if (strncmp(name, *pp, len) == 0 && 98155418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 98255418Seric break; 98355418Seric } 98455418Seric 98555418Seric for (; *pp != NULL; pp++) 98655418Seric *pp = pp[1]; 98755418Seric } 98855418Seric 98963962Seric #endif 99056215Seric /* 99156215Seric ** GETDTABLESIZE -- return number of file descriptors 99256215Seric ** 99356215Seric ** Only on non-BSD systems 99456215Seric ** 99556215Seric ** Parameters: 99656215Seric ** none 99756215Seric ** 99856215Seric ** Returns: 99956215Seric ** size of file descriptor table 100056215Seric ** 100156215Seric ** Side Effects: 100256215Seric ** none 100356215Seric */ 100456215Seric 100563787Seric #ifdef SOLARIS 100663787Seric # include <sys/resource.h> 100763787Seric #endif 100856215Seric 100956215Seric int 101063787Seric getdtsize() 101156215Seric { 101263787Seric #ifdef RLIMIT_NOFILE 101363787Seric struct rlimit rl; 101463787Seric 101563787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 101663787Seric return rl.rlim_cur; 101763787Seric #endif 101863787Seric 101964031Seric # ifdef HASGETDTABLESIZE 102064031Seric return getdtablesize(); 102164031Seric # else 102264031Seric # ifdef _SC_OPEN_MAX 102358689Seric return sysconf(_SC_OPEN_MAX); 102463787Seric # else 102556215Seric return NOFILE; 102663787Seric # endif 102758689Seric # endif 102856215Seric } 102957631Seric /* 103057631Seric ** UNAME -- get the UUCP name of this system. 103157631Seric */ 103257631Seric 103357943Seric #ifndef HASUNAME 103457631Seric 103557631Seric int 103657631Seric uname(name) 103757631Seric struct utsname *name; 103857631Seric { 103957631Seric FILE *file; 104057631Seric char *n; 104157631Seric 104257631Seric name->nodename[0] = '\0'; 104357631Seric 104457661Seric /* try /etc/whoami -- one line with the node name */ 104557631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 104657631Seric { 104757661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 104857631Seric (void) fclose(file); 104957661Seric n = strchr(name->nodename, '\n'); 105057631Seric if (n != NULL) 105157631Seric *n = '\0'; 105257631Seric if (name->nodename[0] != '\0') 105357631Seric return (0); 105457631Seric } 105557631Seric 105657661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 105757631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 105857631Seric { 105957631Seric char buf[MAXLINE]; 106057631Seric 106157631Seric while (fgets(buf, MAXLINE, file) != NULL) 106257631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 106357631Seric NODE_LENGTH, name->nodename) > 0) 106457631Seric break; 106557631Seric (void) fclose(file); 106657631Seric if (name->nodename[0] != '\0') 106757631Seric return (0); 106857631Seric } 106957631Seric 107057631Seric #ifdef TRUST_POPEN 107157631Seric /* 107257631Seric ** Popen is known to have security holes. 107357631Seric */ 107457631Seric 107557661Seric /* try uuname -l to return local name */ 107657631Seric if ((file = popen("uuname -l", "r")) != NULL) 107757631Seric { 107857661Seric (void) fgets(name, NODE_LENGTH + 1, file); 107957631Seric (void) pclose(file); 108057661Seric n = strchr(name, '\n'); 108157631Seric if (n != NULL) 108257631Seric *n = '\0'; 108357661Seric if (name->nodename[0] != '\0') 108457631Seric return (0); 108557631Seric } 108657631Seric #endif 108757631Seric 108857631Seric return (-1); 108957631Seric } 109057943Seric #endif /* HASUNAME */ 109158068Seric /* 109258068Seric ** INITGROUPS -- initialize groups 109358068Seric ** 109458068Seric ** Stub implementation for System V style systems 109558068Seric */ 109658068Seric 109758068Seric #ifndef HASINITGROUPS 109858068Seric 109958068Seric initgroups(name, basegid) 110058068Seric char *name; 110158068Seric int basegid; 110258068Seric { 110358068Seric return 0; 110458068Seric } 110558068Seric 110658068Seric #endif 110758082Seric /* 110859289Seric ** SETSID -- set session id (for non-POSIX systems) 110959289Seric */ 111059289Seric 111159289Seric #ifndef HASSETSID 111259289Seric 111363753Seric pid_t 111463753Seric setsid __P ((void)) 111559289Seric { 111664155Seric #ifdef TIOCNOTTY 111764155Seric int fd; 111864155Seric 111964155Seric fd = open("/dev/tty", 2); 112064155Seric if (fd >= 0) 112164155Seric { 112264155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 112364155Seric (void) close(fd); 112464155Seric } 112564155Seric #endif /* TIOCNOTTY */ 112659289Seric # ifdef SYSTEM5 112763753Seric return setpgrp(); 112863753Seric # else 112964155Seric return setpgid(0, getpid()); 113059289Seric # endif 113159289Seric } 113259289Seric 113359289Seric #endif 113459289Seric /* 113563969Seric ** GETOPT -- for old systems or systems with bogus implementations 113663969Seric */ 113763969Seric 113863969Seric #ifdef NEEDGETOPT 113963969Seric 114063969Seric /* 114163969Seric * Copyright (c) 1985 Regents of the University of California. 114263969Seric * All rights reserved. The Berkeley software License Agreement 114363969Seric * specifies the terms and conditions for redistribution. 114463969Seric */ 114563969Seric 114663969Seric 114763969Seric /* 114863969Seric ** this version hacked to add `atend' flag to allow state machine 114963969Seric ** to reset if invoked by the program to scan args for a 2nd time 115063969Seric */ 115163969Seric 115263969Seric #if defined(LIBC_SCCS) && !defined(lint) 115363969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 115464072Seric #endif /* LIBC_SCCS and not lint */ 115563969Seric 115663969Seric #include <stdio.h> 115763969Seric 115863969Seric /* 115963969Seric * get option letter from argument vector 116063969Seric */ 116163969Seric int opterr = 1, /* if error message should be printed */ 116263969Seric optind = 1, /* index into parent argv vector */ 116363969Seric optopt; /* character checked for validity */ 116463969Seric char *optarg; /* argument associated with option */ 116563969Seric 116663969Seric #define BADCH (int)'?' 116763969Seric #define EMSG "" 116863969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 116963969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 117063969Seric 117163969Seric getopt(nargc,nargv,ostr) 117263969Seric int nargc; 117363969Seric char **nargv, 117463969Seric *ostr; 117563969Seric { 117663969Seric static char *place = EMSG; /* option letter processing */ 117763969Seric static char atend = 0; 117863969Seric register char *oli; /* option letter list index */ 117963969Seric 118063969Seric if (atend) { 118163969Seric atend = 0; 118263969Seric place = EMSG; 118363969Seric } 118463969Seric if(!*place) { /* update scanning pointer */ 118564136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 118663969Seric atend++; 118763969Seric return(EOF); 118863969Seric } 118963969Seric if (*place == '-') { /* found "--" */ 119063969Seric ++optind; 119163969Seric atend++; 119263969Seric return(EOF); 119363969Seric } 119463969Seric } /* option letter okay? */ 119564125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 119664136Seric if (!*place) ++optind; 119763969Seric tell(": illegal option -- "); 119863969Seric } 119963969Seric if (*++oli != ':') { /* don't need argument */ 120063969Seric optarg = NULL; 120163969Seric if (!*place) ++optind; 120263969Seric } 120363969Seric else { /* need an argument */ 120463969Seric if (*place) optarg = place; /* no white space */ 120563969Seric else if (nargc <= ++optind) { /* no arg */ 120663969Seric place = EMSG; 120763969Seric tell(": option requires an argument -- "); 120863969Seric } 120963969Seric else optarg = nargv[optind]; /* white space */ 121063969Seric place = EMSG; 121163969Seric ++optind; 121263969Seric } 121363969Seric return(optopt); /* dump back option letter */ 121463969Seric } 121563969Seric 121663969Seric #endif 121763969Seric /* 121863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 121963969Seric */ 122063969Seric 122163969Seric #ifdef NEEDVPRINTF 122263969Seric 122363969Seric #define MAXARG 16 122463969Seric 122563969Seric vfprintf(fp, fmt, ap) 122663969Seric FILE * fp; 122763969Seric char * fmt; 122863969Seric char ** ap; 122963969Seric { 123063969Seric char * bp[MAXARG]; 123163969Seric int i = 0; 123263969Seric 123363969Seric while (*ap && i < MAXARG) 123463969Seric bp[i++] = *ap++; 123563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 123663969Seric bp[4], bp[5], bp[6], bp[7], 123763969Seric bp[8], bp[9], bp[10], bp[11], 123863969Seric bp[12], bp[13], bp[14], bp[15]); 123963969Seric } 124063969Seric 124163969Seric vsprintf(s, fmt, ap) 124263969Seric char * s; 124363969Seric char * fmt; 124463969Seric char ** ap; 124563969Seric { 124663969Seric char * bp[MAXARG]; 124763969Seric int i = 0; 124863969Seric 124963969Seric while (*ap && i < MAXARG) 125063969Seric bp[i++] = *ap++; 125163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 125263969Seric bp[4], bp[5], bp[6], bp[7], 125363969Seric bp[8], bp[9], bp[10], bp[11], 125463969Seric bp[12], bp[13], bp[14], bp[15]); 125563969Seric } 125663969Seric 125763969Seric #endif 125863969Seric /* 125964311Seric ** FREESPACE -- see how much free space is on the queue filesystem 126058082Seric ** 126158082Seric ** Only implemented if you have statfs. 126258082Seric ** 126358082Seric ** Parameters: 126464311Seric ** dir -- the directory in question. 126564311Seric ** bsize -- a variable into which the filesystem 126664311Seric ** block size is stored. 126758082Seric ** 126858082Seric ** Returns: 126964311Seric ** The number of bytes free on the queue filesystem. 127064311Seric ** -1 if the statfs call fails. 127164311Seric ** 127264311Seric ** Side effects: 127364311Seric ** Puts the filesystem block size into bsize. 127458082Seric */ 127558082Seric 127658082Seric #ifdef HASSTATFS 127758157Seric # undef HASUSTAT 127858157Seric #endif 127958157Seric 128058157Seric #if defined(HASUSTAT) 128158157Seric # include <ustat.h> 128258157Seric #endif 128358157Seric 128458157Seric #ifdef HASSTATFS 128564314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) 128658133Seric # include <sys/statfs.h> 128758133Seric # else 128864380Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) 128958133Seric # include <sys/vfs.h> 129058133Seric # else 129158157Seric # include <sys/mount.h> 129258133Seric # endif 129358133Seric # endif 129458082Seric #endif 129558082Seric 129664311Seric long 129764311Seric freespace(dir, bsize) 129864311Seric char *dir; 129964311Seric long *bsize; 130058082Seric { 130158160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 130258157Seric # if defined(HASUSTAT) 130358153Seric struct ustat fs; 130458153Seric struct stat statbuf; 130558366Seric # define FSBLOCKSIZE DEV_BSIZE 130658157Seric # define f_bavail f_tfree 130758157Seric # else 130858157Seric # if defined(ultrix) 130958157Seric struct fs_data fs; 131058157Seric # define f_bavail fd_bfreen 131158366Seric # define FSBLOCKSIZE fs.fd_bsize 131258153Seric # else 131358082Seric struct statfs fs; 131458366Seric # define FSBLOCKSIZE fs.f_bsize 131564336Seric # if defined(_SCO_unix_) || defined(IRIX) 131663987Seric # define f_bavail f_bfree 131763987Seric # endif 131858153Seric # endif 131958133Seric # endif 132058082Seric extern int errno; 132158082Seric 132258157Seric # if defined(HASUSTAT) 132364311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 132458157Seric # else 132564314Seric # if defined(IRIX) || defined(apollo) 132664311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 132758157Seric # else 132858157Seric # if defined(ultrix) 132964311Seric if (statfs(dir, &fs) > 0) 133058153Seric # else 133164311Seric if (statfs(dir, &fs) == 0) 133258153Seric # endif 133358133Seric # endif 133458133Seric # endif 133558133Seric { 133664311Seric if (bsize != NULL) 133764311Seric *bsize = FSBLOCKSIZE; 133864311Seric return (fs.f_bavail); 133964311Seric } 134064337Seric #endif 134164311Seric return (-1); 134264311Seric } 134364311Seric /* 134464311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 134564311Seric ** 134664311Seric ** Only implemented if you have statfs. 134764311Seric ** 134864311Seric ** Parameters: 134964311Seric ** msize -- the size to check against. If zero, we don't yet 135064311Seric ** know how big the message will be, so just check for 135164311Seric ** a "reasonable" amount. 135264311Seric ** 135364311Seric ** Returns: 135464311Seric ** TRUE if there is enough space. 135564311Seric ** FALSE otherwise. 135664311Seric */ 135764311Seric 135864311Seric bool 135964311Seric enoughspace(msize) 136064311Seric long msize; 136164311Seric { 136264311Seric long bfree, bsize; 136364311Seric 136464311Seric if (MinBlocksFree <= 0 && msize <= 0) 136564311Seric { 136658133Seric if (tTd(4, 80)) 136764311Seric printf("enoughspace: no threshold\n"); 136864311Seric return TRUE; 136964311Seric } 137064311Seric 137164311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 137264311Seric { 137364311Seric if (tTd(4, 80)) 137458333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 137564311Seric bfree, msize); 137658333Seric 137758333Seric /* convert msize to block count */ 137864311Seric msize = msize / bsize + 1; 137958333Seric if (MinBlocksFree >= 0) 138058333Seric msize += MinBlocksFree; 138158333Seric 138264311Seric if (bfree < msize) 138358090Seric { 138458090Seric #ifdef LOG 138558090Seric if (LogLevel > 0) 138664254Seric syslog(LOG_ALERT, 138764254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 138864311Seric CurEnv->e_id, bfree, 138964254Seric CurHostName, msize, QueueDir); 139058090Seric #endif 139158082Seric return FALSE; 139258090Seric } 139358082Seric } 139458082Seric else if (tTd(4, 80)) 139558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 139658333Seric MinBlocksFree, msize, errstring(errno)); 139758082Seric return TRUE; 139858082Seric } 139958542Seric /* 140058542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 140158542Seric ** 140258542Seric ** This looks at an errno value and tells if this is likely to 140358542Seric ** go away if retried later. 140458542Seric ** 140558542Seric ** Parameters: 140658542Seric ** err -- the errno code to classify. 140758542Seric ** 140858542Seric ** Returns: 140958542Seric ** TRUE if this is probably transient. 141058542Seric ** FALSE otherwise. 141158542Seric */ 141258542Seric 141358542Seric bool 141458542Seric transienterror(err) 141558542Seric int err; 141658542Seric { 141758542Seric switch (err) 141858542Seric { 141958542Seric case EIO: /* I/O error */ 142058542Seric case ENXIO: /* Device not configured */ 142158542Seric case EAGAIN: /* Resource temporarily unavailable */ 142258542Seric case ENOMEM: /* Cannot allocate memory */ 142358542Seric case ENODEV: /* Operation not supported by device */ 142458542Seric case ENFILE: /* Too many open files in system */ 142558542Seric case EMFILE: /* Too many open files */ 142658542Seric case ENOSPC: /* No space left on device */ 142758542Seric #ifdef ETIMEDOUT 142858542Seric case ETIMEDOUT: /* Connection timed out */ 142958542Seric #endif 143058542Seric #ifdef ESTALE 143158542Seric case ESTALE: /* Stale NFS file handle */ 143258542Seric #endif 143358542Seric #ifdef ENETDOWN 143458542Seric case ENETDOWN: /* Network is down */ 143558542Seric #endif 143658542Seric #ifdef ENETUNREACH 143758542Seric case ENETUNREACH: /* Network is unreachable */ 143858542Seric #endif 143958542Seric #ifdef ENETRESET 144058542Seric case ENETRESET: /* Network dropped connection on reset */ 144158542Seric #endif 144258542Seric #ifdef ECONNABORTED 144358542Seric case ECONNABORTED: /* Software caused connection abort */ 144458542Seric #endif 144558542Seric #ifdef ECONNRESET 144658542Seric case ECONNRESET: /* Connection reset by peer */ 144758542Seric #endif 144858542Seric #ifdef ENOBUFS 144958542Seric case ENOBUFS: /* No buffer space available */ 145058542Seric #endif 145158542Seric #ifdef ESHUTDOWN 145258542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 145358542Seric #endif 145458542Seric #ifdef ECONNREFUSED 145558542Seric case ECONNREFUSED: /* Connection refused */ 145658542Seric #endif 145758542Seric #ifdef EHOSTDOWN 145858542Seric case EHOSTDOWN: /* Host is down */ 145958542Seric #endif 146058542Seric #ifdef EHOSTUNREACH 146158542Seric case EHOSTUNREACH: /* No route to host */ 146258542Seric #endif 146358542Seric #ifdef EDQUOT 146458542Seric case EDQUOT: /* Disc quota exceeded */ 146558542Seric #endif 146658542Seric #ifdef EPROCLIM 146758542Seric case EPROCLIM: /* Too many processes */ 146858542Seric #endif 146958542Seric #ifdef EUSERS 147058542Seric case EUSERS: /* Too many users */ 147158542Seric #endif 147258542Seric #ifdef EDEADLK 147358542Seric case EDEADLK: /* Resource deadlock avoided */ 147458542Seric #endif 147558542Seric #ifdef EISCONN 147658542Seric case EISCONN: /* Socket already connected */ 147758542Seric #endif 147858542Seric #ifdef EINPROGRESS 147958542Seric case EINPROGRESS: /* Operation now in progress */ 148058542Seric #endif 148158542Seric #ifdef EALREADY 148258542Seric case EALREADY: /* Operation already in progress */ 148358542Seric #endif 148458542Seric #ifdef EADDRINUSE 148558542Seric case EADDRINUSE: /* Address already in use */ 148658542Seric #endif 148758542Seric #ifdef EADDRNOTAVAIL 148858542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 148958542Seric #endif 149063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 149158542Seric case ENOSR: /* Out of streams resources */ 149258542Seric #endif 149358542Seric return TRUE; 149458542Seric } 149558542Seric 149658542Seric /* nope, must be permanent */ 149758542Seric return FALSE; 149858542Seric } 149958689Seric /* 150064035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 150158689Seric ** 150258689Seric ** Parameters: 150358689Seric ** fd -- the file descriptor of the file. 150458689Seric ** filename -- the file name (for error messages). 150564335Seric ** ext -- the filename extension. 150658689Seric ** type -- type of the lock. Bits can be: 150758689Seric ** LOCK_EX -- exclusive lock. 150858689Seric ** LOCK_NB -- non-blocking. 150958689Seric ** 151058689Seric ** Returns: 151158689Seric ** TRUE if the lock was acquired. 151258689Seric ** FALSE otherwise. 151358689Seric */ 151458689Seric 151558689Seric bool 151664335Seric lockfile(fd, filename, ext, type) 151758689Seric int fd; 151858689Seric char *filename; 151964335Seric char *ext; 152058689Seric int type; 152158689Seric { 152264035Seric # ifndef HASFLOCK 152358689Seric int action; 152458689Seric struct flock lfd; 152564335Seric 152664335Seric if (ext == NULL) 152764335Seric ext = ""; 152864035Seric 152963983Seric bzero(&lfd, sizeof lfd); 153059447Seric if (bitset(LOCK_UN, type)) 153159447Seric lfd.l_type = F_UNLCK; 153259447Seric else if (bitset(LOCK_EX, type)) 153358689Seric lfd.l_type = F_WRLCK; 153458689Seric else 153558689Seric lfd.l_type = F_RDLCK; 153658689Seric 153758689Seric if (bitset(LOCK_NB, type)) 153858689Seric action = F_SETLK; 153958689Seric else 154058689Seric action = F_SETLKW; 154158689Seric 154264264Seric if (tTd(55, 60)) 154364335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 154464335Seric filename, ext, action, lfd.l_type); 154564264Seric 154658689Seric if (fcntl(fd, action, &lfd) >= 0) 154764264Seric { 154864264Seric if (tTd(55, 60)) 154964264Seric printf("SUCCESS\n"); 155058689Seric return TRUE; 155164264Seric } 155258689Seric 155364264Seric if (tTd(55, 60)) 155464264Seric printf("(%s) ", errstring(errno)); 155564264Seric 155664136Seric /* 155764136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 155864136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 155964136Seric ** as type "tmp" (that is, served from swap space), the 156064136Seric ** previous fcntl will fail with "Invalid argument" errors. 156164136Seric ** Since this is fairly common during testing, we will assume 156264136Seric ** that this indicates that the lock is successfully grabbed. 156364136Seric */ 156464136Seric 156564136Seric if (errno == EINVAL) 156664264Seric { 156764264Seric if (tTd(55, 60)) 156864264Seric printf("SUCCESS\n"); 156964136Seric return TRUE; 157064264Seric } 157164136Seric 157258689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 157364378Seric { 157464378Seric int omode = -1; 157564378Seric # ifdef F_GETFL 157664378Seric int oerrno = errno; 157764378Seric 157864378Seric (void) fcntl(fd, F_GETFL, &omode); 157964378Seric errno = oerrno; 158064378Seric # endif 158164378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 158264378Seric filename, ext, fd, type, omode, geteuid()); 158364378Seric } 158458689Seric # else 158564335Seric if (ext == NULL) 158664335Seric ext = ""; 158764335Seric 158864264Seric if (tTd(55, 60)) 158964335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 159064264Seric 159158689Seric if (flock(fd, type) >= 0) 159264264Seric { 159364264Seric if (tTd(55, 60)) 159464264Seric printf("SUCCESS\n"); 159558689Seric return TRUE; 159664264Seric } 159758689Seric 159864264Seric if (tTd(55, 60)) 159964264Seric printf("(%s) ", errstring(errno)); 160064264Seric 160158689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 160264378Seric { 160364378Seric int omode = -1; 160464378Seric # ifdef F_GETFL 160564378Seric int oerrno = errno; 160664378Seric 160764378Seric (void) fcntl(fd, F_GETFL, &omode); 160864378Seric errno = oerrno; 160964378Seric # endif 161064378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 161164378Seric filename, ext, fd, type, omode, geteuid()); 161264386Seric } 161358689Seric # endif 161464264Seric if (tTd(55, 60)) 161564264Seric printf("FAIL\n"); 161658689Seric return FALSE; 161758689Seric } 1618