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*59992Seric static char sccsid[] = "@(#)conf.c 6.55 (Berkeley) 05/13/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 33859883Seric 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> 836*59992Seric # ifdef PS_STRINGS 837*59992Seric # define SETPROC_STATIC static 838*59992Seric # 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 882*59992Seric # 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 { 91725698Seric # ifdef WNOHANG 91825698Seric union wait status; 91925698Seric 92046928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 92125698Seric continue; 92256795Seric # else /* WNOHANG */ 92325698Seric auto int status; 92425698Seric 92546928Sbostic while (wait((int *)&status) > 0) 92625698Seric continue; 92756795Seric # endif /* WNOHANG */ 92858061Seric # ifdef SYSTEM5 92958061Seric (void) signal(SIGCHLD, reapchild); 93058061Seric # endif 93125698Seric } 93255418Seric /* 93355418Seric ** UNSETENV -- remove a variable from the environment 93455418Seric ** 93555418Seric ** Not needed on newer systems. 93655418Seric ** 93755418Seric ** Parameters: 93855418Seric ** name -- the string name of the environment variable to be 93955418Seric ** deleted from the current environment. 94055418Seric ** 94155418Seric ** Returns: 94255418Seric ** none. 94355418Seric ** 94455418Seric ** Globals: 94555418Seric ** environ -- a pointer to the current environment. 94655418Seric ** 94755418Seric ** Side Effects: 94855418Seric ** Modifies environ. 94955418Seric */ 95055418Seric 95155418Seric #ifdef UNSETENV 95255418Seric 95355418Seric void 95455418Seric unsetenv(name) 95555418Seric char *name; 95655418Seric { 95755418Seric extern char **environ; 95855418Seric register char **pp; 95955418Seric int len = strlen(name); 96055418Seric 96155418Seric for (pp = environ; *pp != NULL; pp++) 96255418Seric { 96355418Seric if (strncmp(name, *pp, len) == 0 && 96455418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 96555418Seric break; 96655418Seric } 96755418Seric 96855418Seric for (; *pp != NULL; pp++) 96955418Seric *pp = pp[1]; 97055418Seric } 97155418Seric 97255418Seric #endif /* UNSETENV */ 97356215Seric /* 97456215Seric ** GETDTABLESIZE -- return number of file descriptors 97556215Seric ** 97656215Seric ** Only on non-BSD systems 97756215Seric ** 97856215Seric ** Parameters: 97956215Seric ** none 98056215Seric ** 98156215Seric ** Returns: 98256215Seric ** size of file descriptor table 98356215Seric ** 98456215Seric ** Side Effects: 98556215Seric ** none 98656215Seric */ 98756215Seric 98856215Seric #ifdef SYSTEM5 98956215Seric 99056215Seric int 99156215Seric getdtablesize() 99256215Seric { 99358689Seric # ifdef _SC_OPEN_MAX 99458689Seric return sysconf(_SC_OPEN_MAX); 99558689Seric # else 99656215Seric return NOFILE; 99758689Seric # endif 99856215Seric } 99956215Seric 100056215Seric #endif 100157631Seric /* 100257631Seric ** UNAME -- get the UUCP name of this system. 100357631Seric */ 100457631Seric 100557943Seric #ifndef HASUNAME 100657631Seric 100757631Seric int 100857631Seric uname(name) 100957631Seric struct utsname *name; 101057631Seric { 101157631Seric FILE *file; 101257631Seric char *n; 101357631Seric 101457631Seric name->nodename[0] = '\0'; 101557631Seric 101657661Seric /* try /etc/whoami -- one line with the node name */ 101757631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 101857631Seric { 101957661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 102057631Seric (void) fclose(file); 102157661Seric n = strchr(name->nodename, '\n'); 102257631Seric if (n != NULL) 102357631Seric *n = '\0'; 102457631Seric if (name->nodename[0] != '\0') 102557631Seric return (0); 102657631Seric } 102757631Seric 102857661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 102957631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 103057631Seric { 103157631Seric char buf[MAXLINE]; 103257631Seric 103357631Seric while (fgets(buf, MAXLINE, file) != NULL) 103457631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 103557631Seric NODE_LENGTH, name->nodename) > 0) 103657631Seric break; 103757631Seric (void) fclose(file); 103857631Seric if (name->nodename[0] != '\0') 103957631Seric return (0); 104057631Seric } 104157631Seric 104257631Seric #ifdef TRUST_POPEN 104357631Seric /* 104457631Seric ** Popen is known to have security holes. 104557631Seric */ 104657631Seric 104757661Seric /* try uuname -l to return local name */ 104857631Seric if ((file = popen("uuname -l", "r")) != NULL) 104957631Seric { 105057661Seric (void) fgets(name, NODE_LENGTH + 1, file); 105157631Seric (void) pclose(file); 105257661Seric n = strchr(name, '\n'); 105357631Seric if (n != NULL) 105457631Seric *n = '\0'; 105557661Seric if (name->nodename[0] != '\0') 105657631Seric return (0); 105757631Seric } 105857631Seric #endif 105957631Seric 106057631Seric return (-1); 106157631Seric } 106257943Seric #endif /* HASUNAME */ 106358068Seric /* 106458068Seric ** INITGROUPS -- initialize groups 106558068Seric ** 106658068Seric ** Stub implementation for System V style systems 106758068Seric */ 106858068Seric 106958068Seric #ifndef HASINITGROUPS 107059430Seric # if !defined(SYSTEM5) || defined(__hpux) 107158068Seric # define HASINITGROUPS 107258068Seric # endif 107358068Seric #endif 107458068Seric 107558068Seric #ifndef HASINITGROUPS 107658068Seric 107758068Seric initgroups(name, basegid) 107858068Seric char *name; 107958068Seric int basegid; 108058068Seric { 108158068Seric return 0; 108258068Seric } 108358068Seric 108458068Seric #endif 108558082Seric /* 108659289Seric ** SETSID -- set session id (for non-POSIX systems) 108759289Seric */ 108859289Seric 108959289Seric #ifndef HASSETSID 109059289Seric 109159289Seric setsid() 109259289Seric { 109359289Seric # ifdef SYSTEM5 109459289Seric setpgrp(); 109559289Seric # endif 109659289Seric } 109759289Seric 109859289Seric #endif 109959289Seric /* 110058082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 110158082Seric ** 110258082Seric ** Only implemented if you have statfs. 110358082Seric ** 110458082Seric ** Parameters: 110558333Seric ** msize -- the size to check against. If zero, we don't yet 110658333Seric ** know how big the message will be, so just check for 110758333Seric ** a "reasonable" amount. 110858082Seric ** 110958082Seric ** Returns: 111058082Seric ** TRUE if there is enough space. 111158082Seric ** FALSE otherwise. 111258082Seric */ 111358082Seric 111458082Seric #ifndef HASSTATFS 111558082Seric # if defined(BSD4_4) || defined(__osf__) 111658082Seric # define HASSTATFS 111758082Seric # endif 111858082Seric #endif 111958082Seric 112058082Seric #ifdef HASSTATFS 112158157Seric # undef HASUSTAT 112258157Seric #endif 112358157Seric 112458157Seric #if defined(HASUSTAT) 112558157Seric # include <ustat.h> 112658157Seric #endif 112758157Seric 112858157Seric #ifdef HASSTATFS 112958133Seric # if defined(sgi) || defined(apollo) 113058133Seric # include <sys/statfs.h> 113158133Seric # else 113259430Seric # if defined(sun) || defined(__hpux) 113358133Seric # include <sys/vfs.h> 113458133Seric # else 113558157Seric # include <sys/mount.h> 113658133Seric # endif 113758133Seric # endif 113858082Seric #endif 113958082Seric 114058082Seric bool 114158333Seric enoughspace(msize) 114258333Seric long msize; 114358082Seric { 114458160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 114558157Seric # if defined(HASUSTAT) 114658153Seric struct ustat fs; 114758153Seric struct stat statbuf; 114858366Seric # define FSBLOCKSIZE DEV_BSIZE 114958157Seric # define f_bavail f_tfree 115058157Seric # else 115158157Seric # if defined(ultrix) 115258157Seric struct fs_data fs; 115358157Seric # define f_bavail fd_bfreen 115458366Seric # define FSBLOCKSIZE fs.fd_bsize 115558153Seric # else 115658082Seric struct statfs fs; 115758366Seric # define FSBLOCKSIZE fs.f_bsize 115858153Seric # endif 115958133Seric # endif 116058333Seric long blocksneeded; 116158082Seric extern int errno; 116258082Seric extern char *errstring(); 116358082Seric 116458333Seric if (MinBlocksFree <= 0 && msize <= 0) 116558082Seric { 116658082Seric if (tTd(4, 80)) 116758133Seric printf("enoughspace: no threshold\n"); 116858133Seric return TRUE; 116958133Seric } 117058333Seric 117158157Seric # if defined(HASUSTAT) 117258157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 117358157Seric # else 117458157Seric # if defined(sgi) || defined(apollo) 117558133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 117658157Seric # else 117758157Seric # if defined(ultrix) 117858133Seric if (statfs(QueueDir, &fs) > 0) 117958153Seric # else 118058133Seric if (statfs(QueueDir, &fs) == 0) 118158153Seric # endif 118258133Seric # endif 118358133Seric # endif 118458133Seric { 118558133Seric if (tTd(4, 80)) 118658333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 118758333Seric fs.f_bavail, msize); 118858333Seric 118958333Seric /* convert msize to block count */ 119058366Seric msize = msize / FSBLOCKSIZE + 1; 119158333Seric if (MinBlocksFree >= 0) 119258333Seric msize += MinBlocksFree; 119358333Seric 119458333Seric if (fs.f_bavail < msize) 119558090Seric { 119658090Seric #ifdef LOG 119758090Seric if (LogLevel > 0) 119858090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 119958333Seric QueueDir, fs.f_bavail, msize); 120058090Seric #endif 120158082Seric return FALSE; 120258090Seric } 120358082Seric } 120458082Seric else if (tTd(4, 80)) 120558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 120658333Seric MinBlocksFree, msize, errstring(errno)); 120758082Seric #endif 120858082Seric return TRUE; 120958082Seric } 121058542Seric /* 121158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 121258542Seric ** 121358542Seric ** This looks at an errno value and tells if this is likely to 121458542Seric ** go away if retried later. 121558542Seric ** 121658542Seric ** Parameters: 121758542Seric ** err -- the errno code to classify. 121858542Seric ** 121958542Seric ** Returns: 122058542Seric ** TRUE if this is probably transient. 122158542Seric ** FALSE otherwise. 122258542Seric */ 122358542Seric 122458542Seric bool 122558542Seric transienterror(err) 122658542Seric int err; 122758542Seric { 122858542Seric switch (err) 122958542Seric { 123058542Seric case EIO: /* I/O error */ 123158542Seric case ENXIO: /* Device not configured */ 123258542Seric case EAGAIN: /* Resource temporarily unavailable */ 123358542Seric case ENOMEM: /* Cannot allocate memory */ 123458542Seric case ENODEV: /* Operation not supported by device */ 123558542Seric case ENFILE: /* Too many open files in system */ 123658542Seric case EMFILE: /* Too many open files */ 123758542Seric case ENOSPC: /* No space left on device */ 123858542Seric #ifdef ETIMEDOUT 123958542Seric case ETIMEDOUT: /* Connection timed out */ 124058542Seric #endif 124158542Seric #ifdef ESTALE 124258542Seric case ESTALE: /* Stale NFS file handle */ 124358542Seric #endif 124458542Seric #ifdef ENETDOWN 124558542Seric case ENETDOWN: /* Network is down */ 124658542Seric #endif 124758542Seric #ifdef ENETUNREACH 124858542Seric case ENETUNREACH: /* Network is unreachable */ 124958542Seric #endif 125058542Seric #ifdef ENETRESET 125158542Seric case ENETRESET: /* Network dropped connection on reset */ 125258542Seric #endif 125358542Seric #ifdef ECONNABORTED 125458542Seric case ECONNABORTED: /* Software caused connection abort */ 125558542Seric #endif 125658542Seric #ifdef ECONNRESET 125758542Seric case ECONNRESET: /* Connection reset by peer */ 125858542Seric #endif 125958542Seric #ifdef ENOBUFS 126058542Seric case ENOBUFS: /* No buffer space available */ 126158542Seric #endif 126258542Seric #ifdef ESHUTDOWN 126358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 126458542Seric #endif 126558542Seric #ifdef ECONNREFUSED 126658542Seric case ECONNREFUSED: /* Connection refused */ 126758542Seric #endif 126858542Seric #ifdef EHOSTDOWN 126958542Seric case EHOSTDOWN: /* Host is down */ 127058542Seric #endif 127158542Seric #ifdef EHOSTUNREACH 127258542Seric case EHOSTUNREACH: /* No route to host */ 127358542Seric #endif 127458542Seric #ifdef EDQUOT 127558542Seric case EDQUOT: /* Disc quota exceeded */ 127658542Seric #endif 127758542Seric #ifdef EPROCLIM 127858542Seric case EPROCLIM: /* Too many processes */ 127958542Seric #endif 128058542Seric #ifdef EUSERS 128158542Seric case EUSERS: /* Too many users */ 128258542Seric #endif 128358542Seric #ifdef EDEADLK 128458542Seric case EDEADLK: /* Resource deadlock avoided */ 128558542Seric #endif 128658542Seric #ifdef EISCONN 128758542Seric case EISCONN: /* Socket already connected */ 128858542Seric #endif 128958542Seric #ifdef EINPROGRESS 129058542Seric case EINPROGRESS: /* Operation now in progress */ 129158542Seric #endif 129258542Seric #ifdef EALREADY 129358542Seric case EALREADY: /* Operation already in progress */ 129458542Seric #endif 129558542Seric #ifdef EADDRINUSE 129658542Seric case EADDRINUSE: /* Address already in use */ 129758542Seric #endif 129858542Seric #ifdef EADDRNOTAVAIL 129958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 130058542Seric #endif 130158542Seric #ifdef ENOSR 130258542Seric case ENOSR: /* Out of streams resources */ 130358542Seric #endif 130458542Seric return TRUE; 130558542Seric } 130658542Seric 130758542Seric /* nope, must be permanent */ 130858542Seric return FALSE; 130958542Seric } 131058689Seric /* 131158689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 131258689Seric ** 131358689Seric ** Parameters: 131458689Seric ** fd -- the file descriptor of the file. 131558689Seric ** filename -- the file name (for error messages). 131658689Seric ** type -- type of the lock. Bits can be: 131758689Seric ** LOCK_EX -- exclusive lock. 131858689Seric ** LOCK_NB -- non-blocking. 131958689Seric ** 132058689Seric ** Returns: 132158689Seric ** TRUE if the lock was acquired. 132258689Seric ** FALSE otherwise. 132358689Seric */ 132458689Seric 132558689Seric bool 132658689Seric lockfile(fd, filename, type) 132758689Seric int fd; 132858689Seric char *filename; 132958689Seric int type; 133058689Seric { 133158689Seric # ifdef LOCKF 133258689Seric int action; 133358689Seric struct flock lfd; 133458689Seric 133559447Seric if (bitset(LOCK_UN, type)) 133659447Seric lfd.l_type = F_UNLCK; 133759447Seric else if (bitset(LOCK_EX, type)) 133858689Seric lfd.l_type = F_WRLCK; 133958689Seric else 134058689Seric lfd.l_type = F_RDLCK; 134158689Seric 134258689Seric if (bitset(LOCK_NB, type)) 134358689Seric action = F_SETLK; 134458689Seric else 134558689Seric action = F_SETLKW; 134658689Seric 134758689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 134858689Seric 134958689Seric if (fcntl(fd, action, &lfd) >= 0) 135058689Seric return TRUE; 135158689Seric 135258689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 135359447Seric syserr("cannot lockf(%s, %o)", filename, type); 135458689Seric # else 135558689Seric if (flock(fd, type) >= 0) 135658689Seric return TRUE; 135758689Seric 135858689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 135959447Seric syserr("cannot flock(%s, %o)", filename, type); 136058689Seric # endif 136158689Seric return FALSE; 136258689Seric } 1363