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*59883Seric static char sccsid[] = "@(#)conf.c 6.54 (Berkeley) 05/11/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 */ 17359709Seric SevenBit = FALSE; /* option 7 */ 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(); 18259672Seric 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 338*59883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, 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 83359732Seric # ifdef BSD4_4 83459732Seric # include <machine/vmparam.h> 83559732Seric # include <sys/exec.h> 83659732Seric # define SETPROC_STATIC static 83759732Seric # endif 83859732Seric # ifndef SETPROC_STATIC 83959732Seric # define SETPROC_STATIC 84059732Seric # endif 84158689Seric #endif 84258689Seric 84324943Seric /*VARARGS1*/ 84457642Seric #ifdef __STDC__ 84557642Seric setproctitle(char *fmt, ...) 84657642Seric #else 84757642Seric setproctitle(fmt, va_alist) 84824943Seric char *fmt; 84957642Seric va_dcl 85057642Seric #endif 85124943Seric { 85224943Seric # ifdef SETPROCTITLE 85324943Seric register char *p; 85425049Seric register int i; 85559732Seric SETPROC_STATIC char buf[MAXLINE]; 85656852Seric VA_LOCAL_DECL 85758689Seric # ifdef __hpux 85858689Seric union pstun pst; 85958689Seric # endif 86024943Seric extern char **Argv; 86124943Seric extern char *LastArgv; 86224943Seric 86358674Seric p = buf; 86424943Seric 86558674Seric /* print sendmail: heading for grep */ 86658674Seric (void) strcpy(p, "sendmail: "); 86758674Seric p += strlen(p); 86824943Seric 86958674Seric /* print the argument string */ 87058674Seric VA_START(fmt); 87158674Seric (void) vsprintf(p, fmt, ap); 87256852Seric VA_END; 87354996Seric 87458674Seric i = strlen(buf); 87558689Seric 87658689Seric # ifdef __hpux 87758689Seric pst.pst_command = buf; 87858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 87958689Seric # else 88059732Seric # ifdef BSD4_4 88159732Seric PS_STRINGS->ps_nargvstr = 1; 88259732Seric PS_STRINGS->ps_argvstr = buf; 88359732Seric # else 88454996Seric if (i > LastArgv - Argv[0] - 2) 88525049Seric { 88654996Seric i = LastArgv - Argv[0] - 2; 88758674Seric buf[i] = '\0'; 88825049Seric } 88958674Seric (void) strcpy(Argv[0], buf); 89054997Seric p = &Argv[0][i]; 89124943Seric while (p < LastArgv) 89224943Seric *p++ = ' '; 89359732Seric # endif 89458689Seric # endif 89556795Seric # endif /* SETPROCTITLE */ 89624943Seric } 89725698Seric /* 89825698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 89925698Seric ** 90025698Seric ** Parameters: 90125698Seric ** none. 90225698Seric ** 90325698Seric ** Returns: 90425698Seric ** none. 90525698Seric ** 90625698Seric ** Side Effects: 90725698Seric ** Picks up extant zombies. 90825698Seric */ 90925698Seric 91025698Seric # include <sys/wait.h> 91125698Seric 91246928Sbostic void 91325698Seric reapchild() 91425698Seric { 91525698Seric # ifdef WNOHANG 91625698Seric union wait status; 91725698Seric 91846928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 91925698Seric continue; 92056795Seric # else /* WNOHANG */ 92125698Seric auto int status; 92225698Seric 92346928Sbostic while (wait((int *)&status) > 0) 92425698Seric continue; 92556795Seric # endif /* WNOHANG */ 92658061Seric # ifdef SYSTEM5 92758061Seric (void) signal(SIGCHLD, reapchild); 92858061Seric # endif 92925698Seric } 93055418Seric /* 93155418Seric ** UNSETENV -- remove a variable from the environment 93255418Seric ** 93355418Seric ** Not needed on newer systems. 93455418Seric ** 93555418Seric ** Parameters: 93655418Seric ** name -- the string name of the environment variable to be 93755418Seric ** deleted from the current environment. 93855418Seric ** 93955418Seric ** Returns: 94055418Seric ** none. 94155418Seric ** 94255418Seric ** Globals: 94355418Seric ** environ -- a pointer to the current environment. 94455418Seric ** 94555418Seric ** Side Effects: 94655418Seric ** Modifies environ. 94755418Seric */ 94855418Seric 94955418Seric #ifdef UNSETENV 95055418Seric 95155418Seric void 95255418Seric unsetenv(name) 95355418Seric char *name; 95455418Seric { 95555418Seric extern char **environ; 95655418Seric register char **pp; 95755418Seric int len = strlen(name); 95855418Seric 95955418Seric for (pp = environ; *pp != NULL; pp++) 96055418Seric { 96155418Seric if (strncmp(name, *pp, len) == 0 && 96255418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 96355418Seric break; 96455418Seric } 96555418Seric 96655418Seric for (; *pp != NULL; pp++) 96755418Seric *pp = pp[1]; 96855418Seric } 96955418Seric 97055418Seric #endif /* UNSETENV */ 97156215Seric /* 97256215Seric ** GETDTABLESIZE -- return number of file descriptors 97356215Seric ** 97456215Seric ** Only on non-BSD systems 97556215Seric ** 97656215Seric ** Parameters: 97756215Seric ** none 97856215Seric ** 97956215Seric ** Returns: 98056215Seric ** size of file descriptor table 98156215Seric ** 98256215Seric ** Side Effects: 98356215Seric ** none 98456215Seric */ 98556215Seric 98656215Seric #ifdef SYSTEM5 98756215Seric 98856215Seric int 98956215Seric getdtablesize() 99056215Seric { 99158689Seric # ifdef _SC_OPEN_MAX 99258689Seric return sysconf(_SC_OPEN_MAX); 99358689Seric # else 99456215Seric return NOFILE; 99558689Seric # endif 99656215Seric } 99756215Seric 99856215Seric #endif 99957631Seric /* 100057631Seric ** UNAME -- get the UUCP name of this system. 100157631Seric */ 100257631Seric 100357943Seric #ifndef HASUNAME 100457631Seric 100557631Seric int 100657631Seric uname(name) 100757631Seric struct utsname *name; 100857631Seric { 100957631Seric FILE *file; 101057631Seric char *n; 101157631Seric 101257631Seric name->nodename[0] = '\0'; 101357631Seric 101457661Seric /* try /etc/whoami -- one line with the node name */ 101557631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 101657631Seric { 101757661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 101857631Seric (void) fclose(file); 101957661Seric n = strchr(name->nodename, '\n'); 102057631Seric if (n != NULL) 102157631Seric *n = '\0'; 102257631Seric if (name->nodename[0] != '\0') 102357631Seric return (0); 102457631Seric } 102557631Seric 102657661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 102757631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 102857631Seric { 102957631Seric char buf[MAXLINE]; 103057631Seric 103157631Seric while (fgets(buf, MAXLINE, file) != NULL) 103257631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 103357631Seric NODE_LENGTH, name->nodename) > 0) 103457631Seric break; 103557631Seric (void) fclose(file); 103657631Seric if (name->nodename[0] != '\0') 103757631Seric return (0); 103857631Seric } 103957631Seric 104057631Seric #ifdef TRUST_POPEN 104157631Seric /* 104257631Seric ** Popen is known to have security holes. 104357631Seric */ 104457631Seric 104557661Seric /* try uuname -l to return local name */ 104657631Seric if ((file = popen("uuname -l", "r")) != NULL) 104757631Seric { 104857661Seric (void) fgets(name, NODE_LENGTH + 1, file); 104957631Seric (void) pclose(file); 105057661Seric n = strchr(name, '\n'); 105157631Seric if (n != NULL) 105257631Seric *n = '\0'; 105357661Seric if (name->nodename[0] != '\0') 105457631Seric return (0); 105557631Seric } 105657631Seric #endif 105757631Seric 105857631Seric return (-1); 105957631Seric } 106057943Seric #endif /* HASUNAME */ 106158068Seric /* 106258068Seric ** INITGROUPS -- initialize groups 106358068Seric ** 106458068Seric ** Stub implementation for System V style systems 106558068Seric */ 106658068Seric 106758068Seric #ifndef HASINITGROUPS 106859430Seric # if !defined(SYSTEM5) || defined(__hpux) 106958068Seric # define HASINITGROUPS 107058068Seric # endif 107158068Seric #endif 107258068Seric 107358068Seric #ifndef HASINITGROUPS 107458068Seric 107558068Seric initgroups(name, basegid) 107658068Seric char *name; 107758068Seric int basegid; 107858068Seric { 107958068Seric return 0; 108058068Seric } 108158068Seric 108258068Seric #endif 108358082Seric /* 108459289Seric ** SETSID -- set session id (for non-POSIX systems) 108559289Seric */ 108659289Seric 108759289Seric #ifndef HASSETSID 108859289Seric 108959289Seric setsid() 109059289Seric { 109159289Seric # ifdef SYSTEM5 109259289Seric setpgrp(); 109359289Seric # endif 109459289Seric } 109559289Seric 109659289Seric #endif 109759289Seric /* 109858082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 109958082Seric ** 110058082Seric ** Only implemented if you have statfs. 110158082Seric ** 110258082Seric ** Parameters: 110358333Seric ** msize -- the size to check against. If zero, we don't yet 110458333Seric ** know how big the message will be, so just check for 110558333Seric ** a "reasonable" amount. 110658082Seric ** 110758082Seric ** Returns: 110858082Seric ** TRUE if there is enough space. 110958082Seric ** FALSE otherwise. 111058082Seric */ 111158082Seric 111258082Seric #ifndef HASSTATFS 111358082Seric # if defined(BSD4_4) || defined(__osf__) 111458082Seric # define HASSTATFS 111558082Seric # endif 111658082Seric #endif 111758082Seric 111858082Seric #ifdef HASSTATFS 111958157Seric # undef HASUSTAT 112058157Seric #endif 112158157Seric 112258157Seric #if defined(HASUSTAT) 112358157Seric # include <ustat.h> 112458157Seric #endif 112558157Seric 112658157Seric #ifdef HASSTATFS 112758133Seric # if defined(sgi) || defined(apollo) 112858133Seric # include <sys/statfs.h> 112958133Seric # else 113059430Seric # if defined(sun) || defined(__hpux) 113158133Seric # include <sys/vfs.h> 113258133Seric # else 113358157Seric # include <sys/mount.h> 113458133Seric # endif 113558133Seric # endif 113658082Seric #endif 113758082Seric 113858082Seric bool 113958333Seric enoughspace(msize) 114058333Seric long msize; 114158082Seric { 114258160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 114358157Seric # if defined(HASUSTAT) 114458153Seric struct ustat fs; 114558153Seric struct stat statbuf; 114658366Seric # define FSBLOCKSIZE DEV_BSIZE 114758157Seric # define f_bavail f_tfree 114858157Seric # else 114958157Seric # if defined(ultrix) 115058157Seric struct fs_data fs; 115158157Seric # define f_bavail fd_bfreen 115258366Seric # define FSBLOCKSIZE fs.fd_bsize 115358153Seric # else 115458082Seric struct statfs fs; 115558366Seric # define FSBLOCKSIZE fs.f_bsize 115658153Seric # endif 115758133Seric # endif 115858333Seric long blocksneeded; 115958082Seric extern int errno; 116058082Seric extern char *errstring(); 116158082Seric 116258333Seric if (MinBlocksFree <= 0 && msize <= 0) 116358082Seric { 116458082Seric if (tTd(4, 80)) 116558133Seric printf("enoughspace: no threshold\n"); 116658133Seric return TRUE; 116758133Seric } 116858333Seric 116958157Seric # if defined(HASUSTAT) 117058157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 117158157Seric # else 117258157Seric # if defined(sgi) || defined(apollo) 117358133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 117458157Seric # else 117558157Seric # if defined(ultrix) 117658133Seric if (statfs(QueueDir, &fs) > 0) 117758153Seric # else 117858133Seric if (statfs(QueueDir, &fs) == 0) 117958153Seric # endif 118058133Seric # endif 118158133Seric # endif 118258133Seric { 118358133Seric if (tTd(4, 80)) 118458333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 118558333Seric fs.f_bavail, msize); 118658333Seric 118758333Seric /* convert msize to block count */ 118858366Seric msize = msize / FSBLOCKSIZE + 1; 118958333Seric if (MinBlocksFree >= 0) 119058333Seric msize += MinBlocksFree; 119158333Seric 119258333Seric if (fs.f_bavail < msize) 119358090Seric { 119458090Seric #ifdef LOG 119558090Seric if (LogLevel > 0) 119658090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 119758333Seric QueueDir, fs.f_bavail, msize); 119858090Seric #endif 119958082Seric return FALSE; 120058090Seric } 120158082Seric } 120258082Seric else if (tTd(4, 80)) 120358333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 120458333Seric MinBlocksFree, msize, errstring(errno)); 120558082Seric #endif 120658082Seric return TRUE; 120758082Seric } 120858542Seric /* 120958542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 121058542Seric ** 121158542Seric ** This looks at an errno value and tells if this is likely to 121258542Seric ** go away if retried later. 121358542Seric ** 121458542Seric ** Parameters: 121558542Seric ** err -- the errno code to classify. 121658542Seric ** 121758542Seric ** Returns: 121858542Seric ** TRUE if this is probably transient. 121958542Seric ** FALSE otherwise. 122058542Seric */ 122158542Seric 122258542Seric bool 122358542Seric transienterror(err) 122458542Seric int err; 122558542Seric { 122658542Seric switch (err) 122758542Seric { 122858542Seric case EIO: /* I/O error */ 122958542Seric case ENXIO: /* Device not configured */ 123058542Seric case EAGAIN: /* Resource temporarily unavailable */ 123158542Seric case ENOMEM: /* Cannot allocate memory */ 123258542Seric case ENODEV: /* Operation not supported by device */ 123358542Seric case ENFILE: /* Too many open files in system */ 123458542Seric case EMFILE: /* Too many open files */ 123558542Seric case ENOSPC: /* No space left on device */ 123658542Seric #ifdef ETIMEDOUT 123758542Seric case ETIMEDOUT: /* Connection timed out */ 123858542Seric #endif 123958542Seric #ifdef ESTALE 124058542Seric case ESTALE: /* Stale NFS file handle */ 124158542Seric #endif 124258542Seric #ifdef ENETDOWN 124358542Seric case ENETDOWN: /* Network is down */ 124458542Seric #endif 124558542Seric #ifdef ENETUNREACH 124658542Seric case ENETUNREACH: /* Network is unreachable */ 124758542Seric #endif 124858542Seric #ifdef ENETRESET 124958542Seric case ENETRESET: /* Network dropped connection on reset */ 125058542Seric #endif 125158542Seric #ifdef ECONNABORTED 125258542Seric case ECONNABORTED: /* Software caused connection abort */ 125358542Seric #endif 125458542Seric #ifdef ECONNRESET 125558542Seric case ECONNRESET: /* Connection reset by peer */ 125658542Seric #endif 125758542Seric #ifdef ENOBUFS 125858542Seric case ENOBUFS: /* No buffer space available */ 125958542Seric #endif 126058542Seric #ifdef ESHUTDOWN 126158542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 126258542Seric #endif 126358542Seric #ifdef ECONNREFUSED 126458542Seric case ECONNREFUSED: /* Connection refused */ 126558542Seric #endif 126658542Seric #ifdef EHOSTDOWN 126758542Seric case EHOSTDOWN: /* Host is down */ 126858542Seric #endif 126958542Seric #ifdef EHOSTUNREACH 127058542Seric case EHOSTUNREACH: /* No route to host */ 127158542Seric #endif 127258542Seric #ifdef EDQUOT 127358542Seric case EDQUOT: /* Disc quota exceeded */ 127458542Seric #endif 127558542Seric #ifdef EPROCLIM 127658542Seric case EPROCLIM: /* Too many processes */ 127758542Seric #endif 127858542Seric #ifdef EUSERS 127958542Seric case EUSERS: /* Too many users */ 128058542Seric #endif 128158542Seric #ifdef EDEADLK 128258542Seric case EDEADLK: /* Resource deadlock avoided */ 128358542Seric #endif 128458542Seric #ifdef EISCONN 128558542Seric case EISCONN: /* Socket already connected */ 128658542Seric #endif 128758542Seric #ifdef EINPROGRESS 128858542Seric case EINPROGRESS: /* Operation now in progress */ 128958542Seric #endif 129058542Seric #ifdef EALREADY 129158542Seric case EALREADY: /* Operation already in progress */ 129258542Seric #endif 129358542Seric #ifdef EADDRINUSE 129458542Seric case EADDRINUSE: /* Address already in use */ 129558542Seric #endif 129658542Seric #ifdef EADDRNOTAVAIL 129758542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 129858542Seric #endif 129958542Seric #ifdef ENOSR 130058542Seric case ENOSR: /* Out of streams resources */ 130158542Seric #endif 130258542Seric return TRUE; 130358542Seric } 130458542Seric 130558542Seric /* nope, must be permanent */ 130658542Seric return FALSE; 130758542Seric } 130858689Seric /* 130958689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 131058689Seric ** 131158689Seric ** Parameters: 131258689Seric ** fd -- the file descriptor of the file. 131358689Seric ** filename -- the file name (for error messages). 131458689Seric ** type -- type of the lock. Bits can be: 131558689Seric ** LOCK_EX -- exclusive lock. 131658689Seric ** LOCK_NB -- non-blocking. 131758689Seric ** 131858689Seric ** Returns: 131958689Seric ** TRUE if the lock was acquired. 132058689Seric ** FALSE otherwise. 132158689Seric */ 132258689Seric 132358689Seric bool 132458689Seric lockfile(fd, filename, type) 132558689Seric int fd; 132658689Seric char *filename; 132758689Seric int type; 132858689Seric { 132958689Seric # ifdef LOCKF 133058689Seric int action; 133158689Seric struct flock lfd; 133258689Seric 133359447Seric if (bitset(LOCK_UN, type)) 133459447Seric lfd.l_type = F_UNLCK; 133559447Seric else if (bitset(LOCK_EX, type)) 133658689Seric lfd.l_type = F_WRLCK; 133758689Seric else 133858689Seric lfd.l_type = F_RDLCK; 133958689Seric 134058689Seric if (bitset(LOCK_NB, type)) 134158689Seric action = F_SETLK; 134258689Seric else 134358689Seric action = F_SETLKW; 134458689Seric 134558689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 134658689Seric 134758689Seric if (fcntl(fd, action, &lfd) >= 0) 134858689Seric return TRUE; 134958689Seric 135058689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 135159447Seric syserr("cannot lockf(%s, %o)", filename, type); 135258689Seric # else 135358689Seric if (flock(fd, type) >= 0) 135458689Seric return TRUE; 135558689Seric 135658689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 135759447Seric syserr("cannot flock(%s, %o)", filename, type); 135858689Seric # endif 135958689Seric return FALSE; 136058689Seric } 1361