122698Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333728Sbostic * Copyright (c) 1988 Regents of the University of California. 433728Sbostic * All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822698Sdist 922698Sdist #ifndef lint 10*59672Seric static char sccsid[] = "@(#)conf.c 6.51 (Berkeley) 05/03/93"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1314881Seric # include <sys/ioctl.h> 1458082Seric # include <sys/param.h> 1558153Seric # include <signal.h> 1636928Sbostic # include <pwd.h> 173309Seric # include "sendmail.h" 1840980Sbostic # include "pathnames.h" 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6457359Seric "return-receipt-to", H_FROM /* |H_RECEIPTTO */, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 689055Seric "to", H_RCPT, 6911417Seric "resent-to", H_RCPT|H_RESENT, 709055Seric "cc", H_RCPT, 7111417Seric "resent-cc", H_RCPT|H_RESENT, 729055Seric "bcc", H_RCPT|H_ACHECK, 7311417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7456215Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 8859318Seric "x400-received", H_TRACE|H_FORCE, 899055Seric "via", H_TRACE|H_FORCE, 909055Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9358796Seric "comments", H_FORCE, 9458796Seric "return-path", H_ACHECK, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10540980Sbostic char *ConfFile = _PATH_SENDMAILCF; /* runtime configuration */ 10640980Sbostic char *FreezeFile = _PATH_SENDMAILFC; /* frozen version of above */ 10758082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1089039Seric 1099064Seric 1109064Seric 1119039Seric /* 11258082Seric ** Privacy values 11358082Seric */ 11458082Seric 11558082Seric struct prival PrivacyValues[] = 11658082Seric { 11758082Seric "public", PRIV_PUBLIC, 11858082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11958114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 12058082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12158082Seric "noexpn", PRIV_NOEXPN, 12258082Seric "novrfy", PRIV_NOVRFY, 12358249Seric "restrictmailq", PRIV_RESTRMAILQ, 12458789Seric "authwarnings", PRIV_AUTHWARNINGS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13624943Seric /* 13724943Seric ** SETDEFAULTS -- set default values 13824943Seric ** 13924943Seric ** Because of the way freezing is done, these must be initialized 14024943Seric ** using direct code. 14124943Seric ** 14224943Seric ** Parameters: 14358734Seric ** e -- the default envelope. 14424943Seric ** 14524943Seric ** Returns: 14624943Seric ** none. 14724943Seric ** 14824943Seric ** Side Effects: 14924943Seric ** Initializes a bunch of global variables to their 15024943Seric ** default values. 15124943Seric */ 15224943Seric 15358737Seric #define DAYS * 24 * 60 * 60 15458737Seric 15558734Seric setdefaults(e) 15658734Seric register ENVELOPE *e; 15724943Seric { 15857438Seric SpaceSub = ' '; /* option B */ 15957438Seric QueueLA = 8; /* option x */ 16057438Seric RefuseLA = 12; /* option X */ 16157438Seric WkRecipFact = 30000L; /* option y */ 16257438Seric WkClassFact = 1800L; /* option z */ 16357438Seric WkTimeFact = 90000L; /* option Z */ 16457438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16557142Seric FileMode = (getuid() != geteuid()) ? 0644 : 0600; 16657438Seric /* option F */ 16757438Seric DefUid = 1; /* option u */ 16857438Seric DefGid = 1; /* option g */ 16957438Seric CheckpointInterval = 10; /* option C */ 17057438Seric MaxHopCount = 25; /* option h */ 17158734Seric e->e_sendmode = SM_FORK; /* option d */ 17258734Seric e->e_errormode = EM_PRINT; /* option e */ 17357438Seric EightBit = FALSE; /* option 8 */ 17457438Seric MaxMciCache = 1; /* option k */ 17557438Seric MciCacheTimeout = 300; /* option K */ 17657438Seric LogLevel = 9; /* option L */ 17758112Seric settimeouts(NULL); /* option r */ 17858737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17958737Seric TimeOuts.to_q_warning = 0; /* option T */ 18058853Seric PrivacyFlags = 0; /* option p */ 18140973Sbostic setdefuser(); 182*59672Seric setupaliases(); 18353654Seric setupmaps(); 18457402Seric setupmailers(); 18524943Seric } 186294Seric 18740973Sbostic 1884326Seric /* 18940973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 19040973Sbostic */ 19140973Sbostic 19240973Sbostic setdefuser() 19340973Sbostic { 19440973Sbostic struct passwd *defpwent; 19557386Seric static char defuserbuf[40]; 19640973Sbostic 19757386Seric DefUser = defuserbuf; 19840973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19957386Seric strcpy(defuserbuf, defpwent->pw_name); 20040973Sbostic else 20157386Seric strcpy(defuserbuf, "nobody"); 20240973Sbostic } 20353654Seric /* 20453654Seric ** SETUPMAPS -- set up map classes 20553654Seric ** 20653654Seric ** Since these are compiled in, they cannot be in the config file. 20753654Seric ** 20853654Seric */ 20940973Sbostic 21053654Seric setupmaps() 21153654Seric { 21253654Seric register STAB *s; 21340973Sbostic 21458802Seric /* host name lookup map */ 21558802Seric { 21658802Seric extern bool host_map_init(); 21758802Seric extern char *maphostname(); 21853654Seric 21958802Seric s = stab("host", ST_MAPCLASS, ST_ENTER); 22058802Seric s->s_mapclass.map_init = host_map_init; 22158802Seric s->s_mapclass.map_lookup = maphostname; 22258802Seric } 22353654Seric 22458802Seric /* dequote map */ 22558802Seric { 22658802Seric extern bool dequote_init(); 22758802Seric extern char *dequote_map(); 22858802Seric 22958802Seric s = stab("dequote", ST_MAPCLASS, ST_ENTER); 23058802Seric s->s_mapclass.map_init = dequote_init; 23158802Seric s->s_mapclass.map_lookup = dequote_map; 23258802Seric } 23358802Seric 23453654Seric # ifdef DBM_MAP 23553654Seric /* dbm file access */ 23653654Seric { 23756836Seric extern bool dbm_map_init(); 23853654Seric extern char *dbm_map_lookup(); 23953654Seric 24053654Seric s = stab("dbm", ST_MAPCLASS, ST_ENTER); 24153654Seric s->s_mapclass.map_init = dbm_map_init; 24253654Seric s->s_mapclass.map_lookup = dbm_map_lookup; 24353654Seric } 24453654Seric # endif 24553654Seric 24653654Seric # ifdef BTREE_MAP 24753654Seric /* new database file access -- btree files */ 24853654Seric { 24956823Seric extern bool bt_map_init(); 25056823Seric extern char *db_map_lookup(); 25153654Seric 25253654Seric s = stab("btree", ST_MAPCLASS, ST_ENTER); 25353654Seric s->s_mapclass.map_init = bt_map_init; 25456823Seric s->s_mapclass.map_lookup = db_map_lookup; 25553654Seric } 25653654Seric # endif 25753654Seric 25853654Seric # ifdef HASH_MAP 25953654Seric /* new database file access -- hash files */ 26053654Seric { 26156823Seric extern bool hash_map_init(); 26256823Seric extern char *db_map_lookup(); 26353654Seric 26453654Seric s = stab("hash", ST_MAPCLASS, ST_ENTER); 26553654Seric s->s_mapclass.map_init = hash_map_init; 26656823Seric s->s_mapclass.map_lookup = db_map_lookup; 26753654Seric } 26853654Seric # endif 26953654Seric 27057208Seric # ifdef NIS_MAP 27157208Seric /* NIS map access */ 27257208Seric { 27357208Seric extern bool nis_map_init(); 27457208Seric extern char *nis_map_lookup(); 27557208Seric 27657208Seric s = stab("nis", ST_MAPCLASS, ST_ENTER); 27757208Seric s->s_mapclass.map_init = nis_map_init; 27857208Seric s->s_mapclass.map_lookup = nis_map_lookup; 27957208Seric } 28057208Seric # endif 28157208Seric 28253654Seric # ifdef USERDB_MAP 28353654Seric /* user database */ 28453654Seric { 28556836Seric extern bool udb_map_init(); 28653654Seric extern char *udb_map_lookup(); 28753654Seric 28853654Seric s = stab("udb", ST_MAPCLASS, ST_ENTER); 28953654Seric s->s_mapclass.map_init = udb_map_init; 29053654Seric s->s_mapclass.map_lookup = udb_map_lookup; 29153654Seric } 29253654Seric # endif 29353654Seric } 29453654Seric /* 29556836Seric ** HOST_MAP_INIT -- initialize host class structures 29656836Seric */ 29756836Seric 29856836Seric bool 29956836Seric host_map_init(map, mapname, args) 30056836Seric MAP *map; 30156836Seric char *mapname; 30256836Seric char *args; 30356836Seric { 30456836Seric register char *p = args; 30556836Seric 30656836Seric for (;;) 30756836Seric { 30858050Seric while (isascii(*p) && isspace(*p)) 30956836Seric p++; 31056836Seric if (*p != '-') 31156836Seric break; 31256836Seric switch (*++p) 31356836Seric { 31456836Seric case 'a': 31556836Seric map->map_app = ++p; 31656836Seric break; 31756836Seric } 31858050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 31956836Seric p++; 32056836Seric if (*p != '\0') 32156836Seric *p++ = '\0'; 32256836Seric } 32356836Seric if (map->map_app != NULL) 32456836Seric map->map_app = newstr(map->map_app); 32556836Seric return TRUE; 32656836Seric } 32757402Seric /* 32857402Seric ** SETUPMAILERS -- initialize default mailers 32957402Seric */ 33056836Seric 33157402Seric setupmailers() 33257402Seric { 33357402Seric char buf[100]; 33457402Seric 33557403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 33657403Seric makemailer(buf); 33757403Seric 33857402Seric strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE"); 33957402Seric makemailer(buf); 34057402Seric 34157402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 34257402Seric makemailer(buf); 34357402Seric } 34456836Seric /* 3454326Seric ** GETRUID -- get real user id (V7) 3464326Seric */ 3474326Seric 3484326Seric getruid() 3494326Seric { 3509274Seric if (OpMode == MD_DAEMON) 3514536Seric return (RealUid); 3524536Seric else 3534536Seric return (getuid()); 3544326Seric } 3554326Seric 3564326Seric 3574326Seric /* 3584326Seric ** GETRGID -- get real group id (V7). 3594326Seric */ 3604326Seric 3614326Seric getrgid() 3624326Seric { 3639274Seric if (OpMode == MD_DAEMON) 3644536Seric return (RealGid); 3654536Seric else 3664536Seric return (getgid()); 3674326Seric } 36853654Seric /* 3699369Seric ** USERNAME -- return the user id of the logged in user. 3709369Seric ** 3719369Seric ** Parameters: 3729369Seric ** none. 3739369Seric ** 3749369Seric ** Returns: 3759369Seric ** The login name of the logged in user. 3769369Seric ** 3779369Seric ** Side Effects: 3789369Seric ** none. 3799369Seric ** 3809369Seric ** Notes: 3819369Seric ** The return value is statically allocated. 3829369Seric */ 3839369Seric 3849369Seric char * 3859369Seric username() 3869369Seric { 38717469Seric static char *myname = NULL; 3889369Seric extern char *getlogin(); 38919904Smiriam register struct passwd *pw; 3909369Seric 39117469Seric /* cache the result */ 39217469Seric if (myname == NULL) 39317469Seric { 39417469Seric myname = getlogin(); 39517469Seric if (myname == NULL || myname[0] == '\0') 39617469Seric { 39717469Seric pw = getpwuid(getruid()); 39817469Seric if (pw != NULL) 39940993Sbostic myname = newstr(pw->pw_name); 40017469Seric } 40119904Smiriam else 40219904Smiriam { 40358736Seric uid_t uid = getuid(); 40419873Smiriam 40540993Sbostic myname = newstr(myname); 40640993Sbostic if ((pw = getpwnam(myname)) == NULL || 40758736Seric (uid != 0 && uid != pw->pw_uid)) 40819904Smiriam { 40958736Seric pw = getpwuid(uid); 41024945Seric if (pw != NULL) 41140993Sbostic myname = newstr(pw->pw_name); 41219873Smiriam } 41319873Smiriam } 41417469Seric if (myname == NULL || myname[0] == '\0') 41517469Seric { 41658151Seric syserr("554 Who are you?"); 41717469Seric myname = "postmaster"; 41817469Seric } 41917469Seric } 42017469Seric 42117469Seric return (myname); 4229369Seric } 4239369Seric /* 4244190Seric ** TTYPATH -- Get the path of the user's tty 425294Seric ** 426294Seric ** Returns the pathname of the user's tty. Returns NULL if 427294Seric ** the user is not logged in or if s/he has write permission 428294Seric ** denied. 429294Seric ** 430294Seric ** Parameters: 431294Seric ** none 432294Seric ** 433294Seric ** Returns: 434294Seric ** pathname of the user's tty. 435294Seric ** NULL if not logged in or write permission denied. 436294Seric ** 437294Seric ** Side Effects: 438294Seric ** none. 439294Seric ** 440294Seric ** WARNING: 441294Seric ** Return value is in a local buffer. 442294Seric ** 443294Seric ** Called By: 444294Seric ** savemail 445294Seric */ 446294Seric 447294Seric char * 448294Seric ttypath() 449294Seric { 450294Seric struct stat stbuf; 451294Seric register char *pathn; 452294Seric extern char *ttyname(); 4534081Seric extern char *getlogin(); 454294Seric 455294Seric /* compute the pathname of the controlling tty */ 4569369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4579369Seric (pathn = ttyname(0)) == NULL) 458294Seric { 459294Seric errno = 0; 460294Seric return (NULL); 461294Seric } 462294Seric 463294Seric /* see if we have write permission */ 4642967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 465294Seric { 466294Seric errno = 0; 467294Seric return (NULL); 468294Seric } 469294Seric 470294Seric /* see if the user is logged in */ 471294Seric if (getlogin() == NULL) 472294Seric return (NULL); 473294Seric 474294Seric /* looks good */ 475294Seric return (pathn); 476294Seric } 4772967Seric /* 4782967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4792967Seric ** 4802967Seric ** This routine can be supplied on a per-installation basis 4812967Seric ** to determine whether a person is allowed to send a message. 4822967Seric ** This allows restriction of certain types of internet 4832967Seric ** forwarding or registration of users. 4842967Seric ** 4852967Seric ** If the hosts are found to be incompatible, an error 48657454Seric ** message should be given using "usrerr" and 0 should 4872967Seric ** be returned. 4882967Seric ** 4894288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4904288Seric ** function; this should be done on huge messages. 4914288Seric ** 4922967Seric ** Parameters: 4932967Seric ** to -- the person being sent to. 4942967Seric ** 4952967Seric ** Returns: 49657459Seric ** an exit status 4972967Seric ** 4982967Seric ** Side Effects: 4992967Seric ** none (unless you include the usrerr stuff) 5002967Seric */ 5012967Seric 50255012Seric checkcompat(to, e) 5032967Seric register ADDRESS *to; 50455012Seric register ENVELOPE *e; 5052967Seric { 50612133Seric # ifdef lint 50712133Seric if (to == NULL) 50812133Seric to++; 50912133Seric # endif lint 51010698Seric # ifdef EXAMPLE_CODE 51110698Seric /* this code is intended as an example only */ 5124437Seric register STAB *s; 5134437Seric 5144437Seric s = stab("arpa", ST_MAILER, ST_FIND); 51555012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 5169369Seric to->q_mailer == s->s_mailer) 5174437Seric { 51858151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 51910698Seric /* NoReturn = TRUE; to supress return copy */ 52057459Seric return (EX_UNAVAILABLE); 5214437Seric } 52256795Seric # endif /* EXAMPLE_CODE */ 52357459Seric return (EX_OK); 5242967Seric } 5259369Seric /* 5269369Seric ** HOLDSIGS -- arrange to hold all signals 5279369Seric ** 5289369Seric ** Parameters: 5299369Seric ** none. 5309369Seric ** 5319369Seric ** Returns: 5329369Seric ** none. 5339369Seric ** 5349369Seric ** Side Effects: 5359369Seric ** Arranges that signals are held. 5369369Seric */ 5379369Seric 5389369Seric holdsigs() 5399369Seric { 5409369Seric } 5419369Seric /* 5429369Seric ** RLSESIGS -- arrange to release all signals 5439369Seric ** 5449369Seric ** This undoes the effect of holdsigs. 5459369Seric ** 5469369Seric ** Parameters: 5479369Seric ** none. 5489369Seric ** 5499369Seric ** Returns: 5509369Seric ** none. 5519369Seric ** 5529369Seric ** Side Effects: 5539369Seric ** Arranges that signals are released. 5549369Seric */ 5559369Seric 5569369Seric rlsesigs() 5579369Seric { 5589369Seric } 55914872Seric /* 56014872Seric ** GETLA -- get the current load average 56114872Seric ** 56214881Seric ** This code stolen from la.c. 56314881Seric ** 56414872Seric ** Parameters: 56514872Seric ** none. 56614872Seric ** 56714872Seric ** Returns: 56814872Seric ** The current load average as an integer. 56914872Seric ** 57014872Seric ** Side Effects: 57114872Seric ** none. 57214872Seric */ 57314872Seric 57451920Seric /* try to guess what style of load average we have */ 57551920Seric #define LA_ZERO 1 /* always return load average as zero */ 57651920Seric #define LA_INT 2 /* read kmem for avenrun; interpret as int */ 57751920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 57851920Seric #define LA_SUBR 4 /* call getloadavg */ 57914872Seric 58051920Seric #ifndef LA_TYPE 58151920Seric # if defined(sun) 58251920Seric # define LA_TYPE LA_INT 58351920Seric # endif 58457977Seric # if defined(mips) || defined(__alpha) 58557977Seric /* Ultrix or OSF/1 or RISC/os */ 58651920Seric # define LA_TYPE LA_INT 58751920Seric # define LA_AVENRUN "avenrun" 58851920Seric # endif 58959430Seric # if defined(__hpux) 59051920Seric # define LA_TYPE LA_FLOAT 59159253Seric # define LA_AVENRUN "avenrun" 59251920Seric # endif 59351920Seric 59451920Seric # ifndef LA_TYPE 59557736Seric # if defined(SYSTEM5) 59657736Seric # define LA_TYPE LA_INT 59757736Seric # define LA_AVENRUN "avenrun" 59857736Seric # else 59957736Seric # if defined(BSD) 60057736Seric # define LA_TYPE LA_SUBR 60157736Seric # else 60257736Seric # define LA_TYPE LA_ZERO 60357736Seric # endif 60457736Seric # endif 60551920Seric # endif 60651920Seric #endif 60751920Seric 60851920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) 60951920Seric 61014872Seric #include <nlist.h> 61114872Seric 61251920Seric #ifndef LA_AVENRUN 61351920Seric #define LA_AVENRUN "_avenrun" 61451920Seric #endif 61551920Seric 61651920Seric /* _PATH_UNIX should be defined in <paths.h> */ 61751920Seric #ifndef _PATH_UNIX 61859430Seric # if defined(__hpux) 61951920Seric # define _PATH_UNIX "/hp-ux" 62051920Seric # endif 62151920Seric # if defined(mips) && !defined(ultrix) 62251920Seric /* powerful RISC/os */ 62351920Seric # define _PATH_UNIX "/unix" 62451920Seric # endif 62557736Seric # if defined(SYSTEM5) 62657977Seric # ifndef _PATH_UNIX 62757977Seric # define _PATH_UNIX "/unix" 62857977Seric # endif 62957736Seric # endif 63051920Seric # ifndef _PATH_UNIX 63151920Seric # define _PATH_UNIX "/vmunix" 63251920Seric # endif 63351920Seric #endif 63451920Seric 63514872Seric struct nlist Nl[] = 63614872Seric { 63751920Seric { LA_AVENRUN }, 63814872Seric #define X_AVENRUN 0 63914872Seric { 0 }, 64014872Seric }; 64114872Seric 64257736Seric #if defined(unixpc) 64357736Seric # define FSHIFT 5 64457736Seric #endif 64557736Seric 64657977Seric #if defined(__alpha) 64757977Seric # define FSHIFT 10 64857977Seric #endif 64957977Seric 65051920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT) 65151920Seric # define FSHIFT 8 65257736Seric #endif 65357736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE) 65451920Seric # define FSCALE (1 << FSHIFT) 65551920Seric #endif 65640930Srick 65714872Seric getla() 65814872Seric { 65914872Seric static int kmem = -1; 66051920Seric #if LA_TYPE == LA_INT 66124943Seric long avenrun[3]; 66251920Seric #else 66351920Seric double avenrun[3]; 66451920Seric #endif 66525615Seric extern off_t lseek(); 66657736Seric extern char *errstring(); 66757736Seric extern int errno; 66814872Seric 66914872Seric if (kmem < 0) 67014872Seric { 67124945Seric kmem = open("/dev/kmem", 0, 0); 67214872Seric if (kmem < 0) 67357736Seric { 67457736Seric if (tTd(3, 1)) 67557736Seric printf("getla: open(/dev/kmem): %s\n", 67657736Seric errstring(errno)); 67714872Seric return (-1); 67857736Seric } 67951920Seric (void) fcntl(kmem, F_SETFD, 1); 68057736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68157736Seric { 68257736Seric if (tTd(3, 1)) 68357736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68457736Seric errstring(errno)); 68514872Seric return (-1); 68657736Seric } 68759253Seric if (Nl[X_AVENRUN].n_value == 0) 68859253Seric { 68959253Seric if (tTd(3, 1)) 69059253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69159253Seric _PATH_UNIX, LA_AVENRUN); 69259253Seric return (-1); 69359253Seric } 69414872Seric } 69557736Seric if (tTd(3, 20)) 69657736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 69724945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 69823118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 69919967Seric { 70019967Seric /* thank you Ian */ 70157736Seric if (tTd(3, 1)) 70257736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70319967Seric return (-1); 70419967Seric } 70551920Seric #if LA_TYPE == LA_INT 70657736Seric if (tTd(3, 5)) 70757736Seric { 70857736Seric printf("getla: avenrun = %d", avenrun[0]); 70957736Seric if (tTd(3, 15)) 71057736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71157736Seric printf("\n"); 71257736Seric } 71357736Seric if (tTd(3, 1)) 71457736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71524943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71651920Seric #else 71757736Seric if (tTd(3, 5)) 71857736Seric { 71957736Seric printf("getla: avenrun = %g", avenrun[0]); 72057736Seric if (tTd(3, 15)) 72157736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72257736Seric printf("\n"); 72357736Seric } 72457736Seric if (tTd(3, 1)) 72557736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 72651920Seric return ((int) (avenrun[0] + 0.5)); 72751920Seric #endif 72814872Seric } 72914872Seric 73051773Seric #else 73151920Seric #if LA_TYPE == LA_SUBR 73251773Seric 73351773Seric getla() 73451773Seric { 73551920Seric double avenrun[3]; 73651920Seric 73751920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 73857736Seric { 73957736Seric if (tTd(3, 1)) 74057736Seric perror("getla: getloadavg failed:"); 74151920Seric return (-1); 74257736Seric } 74357736Seric if (tTd(3, 1)) 74457736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 74551920Seric return ((int) (avenrun[0] + 0.5)); 74651773Seric } 74751773Seric 74851773Seric #else 74951773Seric 75051773Seric getla() 75151773Seric { 75257736Seric if (tTd(3, 1)) 75357736Seric printf("getla: ZERO\n"); 75451920Seric return (0); 75551773Seric } 75651773Seric 75751773Seric #endif 75851773Seric #endif 75924943Seric /* 76024943Seric ** SHOULDQUEUE -- should this message be queued or sent? 76124943Seric ** 76224943Seric ** Compares the message cost to the load average to decide. 76324943Seric ** 76424943Seric ** Parameters: 76524943Seric ** pri -- the priority of the message in question. 76657438Seric ** ctime -- the message creation time. 76724943Seric ** 76824943Seric ** Returns: 76924943Seric ** TRUE -- if this message should be queued up for the 77024943Seric ** time being. 77124943Seric ** FALSE -- if the load is low enough to send this message. 77224943Seric ** 77324943Seric ** Side Effects: 77424943Seric ** none. 77524943Seric */ 77624943Seric 77724943Seric bool 77857438Seric shouldqueue(pri, ctime) 77924943Seric long pri; 78057438Seric time_t ctime; 78124943Seric { 78251920Seric if (CurrentLA < QueueLA) 78324943Seric return (FALSE); 78458132Seric if (CurrentLA >= RefuseLA) 78558132Seric return (TRUE); 78651920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 78724943Seric } 78824943Seric /* 78953037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 79053037Seric ** 79153037Seric ** Parameters: 79253037Seric ** none. 79353037Seric ** 79453037Seric ** Returns: 79553037Seric ** TRUE if incoming SMTP connections should be refused 79653037Seric ** (for now). 79753037Seric ** FALSE if we should accept new work. 79853037Seric ** 79953037Seric ** Side Effects: 80053037Seric ** none. 80153037Seric */ 80253037Seric 80353037Seric bool 80453037Seric refuseconnections() 80553037Seric { 80659156Seric #ifdef XLA 80759156Seric if (!xla_smtp_ok()) 80859156Seric return TRUE; 80959156Seric #endif 81059156Seric 81153037Seric /* this is probably too simplistic */ 81258132Seric return (CurrentLA >= RefuseLA); 81353037Seric } 81453037Seric /* 81524943Seric ** SETPROCTITLE -- set process title for ps 81624943Seric ** 81724943Seric ** Parameters: 81858674Seric ** fmt -- a printf style format string. 81958674Seric ** a, b, c -- possible parameters to fmt. 82024943Seric ** 82124943Seric ** Returns: 82224943Seric ** none. 82324943Seric ** 82424943Seric ** Side Effects: 82524943Seric ** Clobbers argv of our main procedure so ps(1) will 82624943Seric ** display the title. 82724943Seric */ 82824943Seric 82958689Seric #ifdef SETPROCTITLE 83058689Seric # ifdef __hpux 83158689Seric # include <sys/pstat.h> 83258689Seric # endif 83358689Seric #endif 83458689Seric 83524943Seric /*VARARGS1*/ 83657642Seric #ifdef __STDC__ 83757642Seric setproctitle(char *fmt, ...) 83857642Seric #else 83957642Seric setproctitle(fmt, va_alist) 84024943Seric char *fmt; 84157642Seric va_dcl 84257642Seric #endif 84324943Seric { 84424943Seric # ifdef SETPROCTITLE 84524943Seric register char *p; 84625049Seric register int i; 84758674Seric char buf[MAXLINE]; 84856852Seric VA_LOCAL_DECL 84958689Seric # ifdef __hpux 85058689Seric union pstun pst; 85158689Seric # endif 85224943Seric extern char **Argv; 85324943Seric extern char *LastArgv; 85424943Seric 85558674Seric p = buf; 85624943Seric 85758674Seric /* print sendmail: heading for grep */ 85858674Seric (void) strcpy(p, "sendmail: "); 85958674Seric p += strlen(p); 86024943Seric 86158674Seric /* print the argument string */ 86258674Seric VA_START(fmt); 86358674Seric (void) vsprintf(p, fmt, ap); 86456852Seric VA_END; 86554996Seric 86658674Seric i = strlen(buf); 86758689Seric 86858689Seric # ifdef __hpux 86958689Seric pst.pst_command = buf; 87058689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 87158689Seric # else 87258689Seric 87354996Seric if (i > LastArgv - Argv[0] - 2) 87425049Seric { 87554996Seric i = LastArgv - Argv[0] - 2; 87658674Seric buf[i] = '\0'; 87725049Seric } 87858674Seric (void) strcpy(Argv[0], buf); 87954997Seric p = &Argv[0][i]; 88024943Seric while (p < LastArgv) 88124943Seric *p++ = ' '; 88258689Seric # endif 88356795Seric # endif /* SETPROCTITLE */ 88424943Seric } 88525698Seric /* 88625698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 88725698Seric ** 88825698Seric ** Parameters: 88925698Seric ** none. 89025698Seric ** 89125698Seric ** Returns: 89225698Seric ** none. 89325698Seric ** 89425698Seric ** Side Effects: 89525698Seric ** Picks up extant zombies. 89625698Seric */ 89725698Seric 89825698Seric # include <sys/wait.h> 89925698Seric 90046928Sbostic void 90125698Seric reapchild() 90225698Seric { 90325698Seric # ifdef WNOHANG 90425698Seric union wait status; 90525698Seric 90646928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 90725698Seric continue; 90856795Seric # else /* WNOHANG */ 90925698Seric auto int status; 91025698Seric 91146928Sbostic while (wait((int *)&status) > 0) 91225698Seric continue; 91356795Seric # endif /* WNOHANG */ 91458061Seric # ifdef SYSTEM5 91558061Seric (void) signal(SIGCHLD, reapchild); 91658061Seric # endif 91725698Seric } 91855418Seric /* 91955418Seric ** UNSETENV -- remove a variable from the environment 92055418Seric ** 92155418Seric ** Not needed on newer systems. 92255418Seric ** 92355418Seric ** Parameters: 92455418Seric ** name -- the string name of the environment variable to be 92555418Seric ** deleted from the current environment. 92655418Seric ** 92755418Seric ** Returns: 92855418Seric ** none. 92955418Seric ** 93055418Seric ** Globals: 93155418Seric ** environ -- a pointer to the current environment. 93255418Seric ** 93355418Seric ** Side Effects: 93455418Seric ** Modifies environ. 93555418Seric */ 93655418Seric 93755418Seric #ifdef UNSETENV 93855418Seric 93955418Seric void 94055418Seric unsetenv(name) 94155418Seric char *name; 94255418Seric { 94355418Seric extern char **environ; 94455418Seric register char **pp; 94555418Seric int len = strlen(name); 94655418Seric 94755418Seric for (pp = environ; *pp != NULL; pp++) 94855418Seric { 94955418Seric if (strncmp(name, *pp, len) == 0 && 95055418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 95155418Seric break; 95255418Seric } 95355418Seric 95455418Seric for (; *pp != NULL; pp++) 95555418Seric *pp = pp[1]; 95655418Seric } 95755418Seric 95855418Seric #endif /* UNSETENV */ 95956215Seric /* 96056215Seric ** GETDTABLESIZE -- return number of file descriptors 96156215Seric ** 96256215Seric ** Only on non-BSD systems 96356215Seric ** 96456215Seric ** Parameters: 96556215Seric ** none 96656215Seric ** 96756215Seric ** Returns: 96856215Seric ** size of file descriptor table 96956215Seric ** 97056215Seric ** Side Effects: 97156215Seric ** none 97256215Seric */ 97356215Seric 97456215Seric #ifdef SYSTEM5 97556215Seric 97656215Seric int 97756215Seric getdtablesize() 97856215Seric { 97958689Seric # ifdef _SC_OPEN_MAX 98058689Seric return sysconf(_SC_OPEN_MAX); 98158689Seric # else 98256215Seric return NOFILE; 98358689Seric # endif 98456215Seric } 98556215Seric 98656215Seric #endif 98757631Seric /* 98857631Seric ** UNAME -- get the UUCP name of this system. 98957631Seric */ 99057631Seric 99157943Seric #ifndef HASUNAME 99257631Seric 99357631Seric int 99457631Seric uname(name) 99557631Seric struct utsname *name; 99657631Seric { 99757631Seric FILE *file; 99857631Seric char *n; 99957631Seric 100057631Seric name->nodename[0] = '\0'; 100157631Seric 100257661Seric /* try /etc/whoami -- one line with the node name */ 100357631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 100457631Seric { 100557661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 100657631Seric (void) fclose(file); 100757661Seric n = strchr(name->nodename, '\n'); 100857631Seric if (n != NULL) 100957631Seric *n = '\0'; 101057631Seric if (name->nodename[0] != '\0') 101157631Seric return (0); 101257631Seric } 101357631Seric 101457661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 101557631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 101657631Seric { 101757631Seric char buf[MAXLINE]; 101857631Seric 101957631Seric while (fgets(buf, MAXLINE, file) != NULL) 102057631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 102157631Seric NODE_LENGTH, name->nodename) > 0) 102257631Seric break; 102357631Seric (void) fclose(file); 102457631Seric if (name->nodename[0] != '\0') 102557631Seric return (0); 102657631Seric } 102757631Seric 102857631Seric #ifdef TRUST_POPEN 102957631Seric /* 103057631Seric ** Popen is known to have security holes. 103157631Seric */ 103257631Seric 103357661Seric /* try uuname -l to return local name */ 103457631Seric if ((file = popen("uuname -l", "r")) != NULL) 103557631Seric { 103657661Seric (void) fgets(name, NODE_LENGTH + 1, file); 103757631Seric (void) pclose(file); 103857661Seric n = strchr(name, '\n'); 103957631Seric if (n != NULL) 104057631Seric *n = '\0'; 104157661Seric if (name->nodename[0] != '\0') 104257631Seric return (0); 104357631Seric } 104457631Seric #endif 104557631Seric 104657631Seric return (-1); 104757631Seric } 104857943Seric #endif /* HASUNAME */ 104958068Seric /* 105058068Seric ** INITGROUPS -- initialize groups 105158068Seric ** 105258068Seric ** Stub implementation for System V style systems 105358068Seric */ 105458068Seric 105558068Seric #ifndef HASINITGROUPS 105659430Seric # if !defined(SYSTEM5) || defined(__hpux) 105758068Seric # define HASINITGROUPS 105858068Seric # endif 105958068Seric #endif 106058068Seric 106158068Seric #ifndef HASINITGROUPS 106258068Seric 106358068Seric initgroups(name, basegid) 106458068Seric char *name; 106558068Seric int basegid; 106658068Seric { 106758068Seric return 0; 106858068Seric } 106958068Seric 107058068Seric #endif 107158082Seric /* 107259289Seric ** SETSID -- set session id (for non-POSIX systems) 107359289Seric */ 107459289Seric 107559289Seric #ifndef HASSETSID 107659289Seric 107759289Seric setsid() 107859289Seric { 107959289Seric # ifdef SYSTEM5 108059289Seric setpgrp(); 108159289Seric # endif 108259289Seric } 108359289Seric 108459289Seric #endif 108559289Seric /* 108658082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 108758082Seric ** 108858082Seric ** Only implemented if you have statfs. 108958082Seric ** 109058082Seric ** Parameters: 109158333Seric ** msize -- the size to check against. If zero, we don't yet 109258333Seric ** know how big the message will be, so just check for 109358333Seric ** a "reasonable" amount. 109458082Seric ** 109558082Seric ** Returns: 109658082Seric ** TRUE if there is enough space. 109758082Seric ** FALSE otherwise. 109858082Seric */ 109958082Seric 110058082Seric #ifndef HASSTATFS 110158082Seric # if defined(BSD4_4) || defined(__osf__) 110258082Seric # define HASSTATFS 110358082Seric # endif 110458082Seric #endif 110558082Seric 110658082Seric #ifdef HASSTATFS 110758157Seric # undef HASUSTAT 110858157Seric #endif 110958157Seric 111058157Seric #if defined(HASUSTAT) 111158157Seric # include <ustat.h> 111258157Seric #endif 111358157Seric 111458157Seric #ifdef HASSTATFS 111558133Seric # if defined(sgi) || defined(apollo) 111658133Seric # include <sys/statfs.h> 111758133Seric # else 111859430Seric # if defined(sun) || defined(__hpux) 111958133Seric # include <sys/vfs.h> 112058133Seric # else 112158157Seric # include <sys/mount.h> 112258133Seric # endif 112358133Seric # endif 112458082Seric #endif 112558082Seric 112658082Seric bool 112758333Seric enoughspace(msize) 112858333Seric long msize; 112958082Seric { 113058160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 113158157Seric # if defined(HASUSTAT) 113258153Seric struct ustat fs; 113358153Seric struct stat statbuf; 113458366Seric # define FSBLOCKSIZE DEV_BSIZE 113558157Seric # define f_bavail f_tfree 113658157Seric # else 113758157Seric # if defined(ultrix) 113858157Seric struct fs_data fs; 113958157Seric # define f_bavail fd_bfreen 114058366Seric # define FSBLOCKSIZE fs.fd_bsize 114158153Seric # else 114258082Seric struct statfs fs; 114358366Seric # define FSBLOCKSIZE fs.f_bsize 114458153Seric # endif 114558133Seric # endif 114658333Seric long blocksneeded; 114758082Seric extern int errno; 114858082Seric extern char *errstring(); 114958082Seric 115058333Seric if (MinBlocksFree <= 0 && msize <= 0) 115158082Seric { 115258082Seric if (tTd(4, 80)) 115358133Seric printf("enoughspace: no threshold\n"); 115458133Seric return TRUE; 115558133Seric } 115658333Seric 115758157Seric # if defined(HASUSTAT) 115858157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 115958157Seric # else 116058157Seric # if defined(sgi) || defined(apollo) 116158133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 116258157Seric # else 116358157Seric # if defined(ultrix) 116458133Seric if (statfs(QueueDir, &fs) > 0) 116558153Seric # else 116658133Seric if (statfs(QueueDir, &fs) == 0) 116758153Seric # endif 116858133Seric # endif 116958133Seric # endif 117058133Seric { 117158133Seric if (tTd(4, 80)) 117258333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 117358333Seric fs.f_bavail, msize); 117458333Seric 117558333Seric /* convert msize to block count */ 117658366Seric msize = msize / FSBLOCKSIZE + 1; 117758333Seric if (MinBlocksFree >= 0) 117858333Seric msize += MinBlocksFree; 117958333Seric 118058333Seric if (fs.f_bavail < msize) 118158090Seric { 118258090Seric #ifdef LOG 118358090Seric if (LogLevel > 0) 118458090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 118558333Seric QueueDir, fs.f_bavail, msize); 118658090Seric #endif 118758082Seric return FALSE; 118858090Seric } 118958082Seric } 119058082Seric else if (tTd(4, 80)) 119158333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 119258333Seric MinBlocksFree, msize, errstring(errno)); 119358082Seric #endif 119458082Seric return TRUE; 119558082Seric } 119658542Seric /* 119758542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 119858542Seric ** 119958542Seric ** This looks at an errno value and tells if this is likely to 120058542Seric ** go away if retried later. 120158542Seric ** 120258542Seric ** Parameters: 120358542Seric ** err -- the errno code to classify. 120458542Seric ** 120558542Seric ** Returns: 120658542Seric ** TRUE if this is probably transient. 120758542Seric ** FALSE otherwise. 120858542Seric */ 120958542Seric 121058542Seric bool 121158542Seric transienterror(err) 121258542Seric int err; 121358542Seric { 121458542Seric switch (err) 121558542Seric { 121658542Seric case EIO: /* I/O error */ 121758542Seric case ENXIO: /* Device not configured */ 121858542Seric case EAGAIN: /* Resource temporarily unavailable */ 121958542Seric case ENOMEM: /* Cannot allocate memory */ 122058542Seric case ENODEV: /* Operation not supported by device */ 122158542Seric case ENFILE: /* Too many open files in system */ 122258542Seric case EMFILE: /* Too many open files */ 122358542Seric case ENOSPC: /* No space left on device */ 122458542Seric #ifdef ETIMEDOUT 122558542Seric case ETIMEDOUT: /* Connection timed out */ 122658542Seric #endif 122758542Seric #ifdef ESTALE 122858542Seric case ESTALE: /* Stale NFS file handle */ 122958542Seric #endif 123058542Seric #ifdef ENETDOWN 123158542Seric case ENETDOWN: /* Network is down */ 123258542Seric #endif 123358542Seric #ifdef ENETUNREACH 123458542Seric case ENETUNREACH: /* Network is unreachable */ 123558542Seric #endif 123658542Seric #ifdef ENETRESET 123758542Seric case ENETRESET: /* Network dropped connection on reset */ 123858542Seric #endif 123958542Seric #ifdef ECONNABORTED 124058542Seric case ECONNABORTED: /* Software caused connection abort */ 124158542Seric #endif 124258542Seric #ifdef ECONNRESET 124358542Seric case ECONNRESET: /* Connection reset by peer */ 124458542Seric #endif 124558542Seric #ifdef ENOBUFS 124658542Seric case ENOBUFS: /* No buffer space available */ 124758542Seric #endif 124858542Seric #ifdef ESHUTDOWN 124958542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 125058542Seric #endif 125158542Seric #ifdef ECONNREFUSED 125258542Seric case ECONNREFUSED: /* Connection refused */ 125358542Seric #endif 125458542Seric #ifdef EHOSTDOWN 125558542Seric case EHOSTDOWN: /* Host is down */ 125658542Seric #endif 125758542Seric #ifdef EHOSTUNREACH 125858542Seric case EHOSTUNREACH: /* No route to host */ 125958542Seric #endif 126058542Seric #ifdef EDQUOT 126158542Seric case EDQUOT: /* Disc quota exceeded */ 126258542Seric #endif 126358542Seric #ifdef EPROCLIM 126458542Seric case EPROCLIM: /* Too many processes */ 126558542Seric #endif 126658542Seric #ifdef EUSERS 126758542Seric case EUSERS: /* Too many users */ 126858542Seric #endif 126958542Seric #ifdef EDEADLK 127058542Seric case EDEADLK: /* Resource deadlock avoided */ 127158542Seric #endif 127258542Seric #ifdef EISCONN 127358542Seric case EISCONN: /* Socket already connected */ 127458542Seric #endif 127558542Seric #ifdef EINPROGRESS 127658542Seric case EINPROGRESS: /* Operation now in progress */ 127758542Seric #endif 127858542Seric #ifdef EALREADY 127958542Seric case EALREADY: /* Operation already in progress */ 128058542Seric #endif 128158542Seric #ifdef EADDRINUSE 128258542Seric case EADDRINUSE: /* Address already in use */ 128358542Seric #endif 128458542Seric #ifdef EADDRNOTAVAIL 128558542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 128658542Seric #endif 128758542Seric #ifdef ENOSR 128858542Seric case ENOSR: /* Out of streams resources */ 128958542Seric #endif 129058542Seric return TRUE; 129158542Seric } 129258542Seric 129358542Seric /* nope, must be permanent */ 129458542Seric return FALSE; 129558542Seric } 129658689Seric /* 129758689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 129858689Seric ** 129958689Seric ** Parameters: 130058689Seric ** fd -- the file descriptor of the file. 130158689Seric ** filename -- the file name (for error messages). 130258689Seric ** type -- type of the lock. Bits can be: 130358689Seric ** LOCK_EX -- exclusive lock. 130458689Seric ** LOCK_NB -- non-blocking. 130558689Seric ** 130658689Seric ** Returns: 130758689Seric ** TRUE if the lock was acquired. 130858689Seric ** FALSE otherwise. 130958689Seric */ 131058689Seric 131158689Seric bool 131258689Seric lockfile(fd, filename, type) 131358689Seric int fd; 131458689Seric char *filename; 131558689Seric int type; 131658689Seric { 131758689Seric # ifdef LOCKF 131858689Seric int action; 131958689Seric struct flock lfd; 132058689Seric 132159447Seric if (bitset(LOCK_UN, type)) 132259447Seric lfd.l_type = F_UNLCK; 132359447Seric else if (bitset(LOCK_EX, type)) 132458689Seric lfd.l_type = F_WRLCK; 132558689Seric else 132658689Seric lfd.l_type = F_RDLCK; 132758689Seric 132858689Seric if (bitset(LOCK_NB, type)) 132958689Seric action = F_SETLK; 133058689Seric else 133158689Seric action = F_SETLKW; 133258689Seric 133358689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 133458689Seric 133558689Seric if (fcntl(fd, action, &lfd) >= 0) 133658689Seric return TRUE; 133758689Seric 133858689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 133959447Seric syserr("cannot lockf(%s, %o)", filename, type); 134058689Seric # else 134158689Seric if (flock(fd, type) >= 0) 134258689Seric return TRUE; 134358689Seric 134458689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 134559447Seric syserr("cannot flock(%s, %o)", filename, type); 134658689Seric # endif 134758689Seric return FALSE; 134858689Seric } 1349