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*58802Seric static char sccsid[] = "@(#)conf.c 6.43 (Berkeley) 03/25/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, 889055Seric "via", H_TRACE|H_FORCE, 899055Seric "mail-from", H_TRACE|H_FORCE, 908060Seric 9158796Seric /* miscellaneous fields */ 9258796Seric "comments", H_FORCE, 9358796Seric "return-path", H_ACHECK, 9458796Seric 959055Seric NULL, 0, 962897Seric }; 974166Seric 984166Seric 994166Seric 1004282Seric /* 1014282Seric ** Location of system files/databases/etc. 1024282Seric */ 1034282Seric 10440980Sbostic char *ConfFile = _PATH_SENDMAILCF; /* runtime configuration */ 10540980Sbostic char *FreezeFile = _PATH_SENDMAILFC; /* frozen version of above */ 10658082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1079039Seric 1089064Seric 1099064Seric 1109039Seric /* 11158082Seric ** Privacy values 11258082Seric */ 11358082Seric 11458082Seric struct prival PrivacyValues[] = 11558082Seric { 11658082Seric "public", PRIV_PUBLIC, 11758082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11858114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11958082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12058082Seric "noexpn", PRIV_NOEXPN, 12158082Seric "novrfy", PRIV_NOVRFY, 12258249Seric "restrictmailq", PRIV_RESTRMAILQ, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12458082Seric "goaway", PRIV_GOAWAY, 12558789Seric NULL, 0, 12658082Seric }; 12758082Seric 12858082Seric 12958082Seric 13058082Seric /* 13124943Seric ** Miscellaneous stuff. 1329039Seric */ 1339039Seric 13424943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13524943Seric /* 13624943Seric ** SETDEFAULTS -- set default values 13724943Seric ** 13824943Seric ** Because of the way freezing is done, these must be initialized 13924943Seric ** using direct code. 14024943Seric ** 14124943Seric ** Parameters: 14258734Seric ** e -- the default envelope. 14324943Seric ** 14424943Seric ** Returns: 14524943Seric ** none. 14624943Seric ** 14724943Seric ** Side Effects: 14824943Seric ** Initializes a bunch of global variables to their 14924943Seric ** default values. 15024943Seric */ 15124943Seric 15258737Seric #define DAYS * 24 * 60 * 60 15358737Seric 15458734Seric setdefaults(e) 15558734Seric register ENVELOPE *e; 15624943Seric { 15757438Seric SpaceSub = ' '; /* option B */ 15857438Seric QueueLA = 8; /* option x */ 15957438Seric RefuseLA = 12; /* option X */ 16057438Seric WkRecipFact = 30000L; /* option y */ 16157438Seric WkClassFact = 1800L; /* option z */ 16257438Seric WkTimeFact = 90000L; /* option Z */ 16357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16457142Seric FileMode = (getuid() != geteuid()) ? 0644 : 0600; 16557438Seric /* option F */ 16657438Seric DefUid = 1; /* option u */ 16757438Seric DefGid = 1; /* option g */ 16857438Seric CheckpointInterval = 10; /* option C */ 16957438Seric MaxHopCount = 25; /* option h */ 17058734Seric e->e_sendmode = SM_FORK; /* option d */ 17158734Seric e->e_errormode = EM_PRINT; /* option e */ 17257438Seric EightBit = FALSE; /* option 8 */ 17357438Seric MaxMciCache = 1; /* option k */ 17457438Seric MciCacheTimeout = 300; /* option K */ 17557438Seric LogLevel = 9; /* option L */ 17658112Seric settimeouts(NULL); /* option r */ 17758737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17858737Seric TimeOuts.to_q_warning = 0; /* option T */ 17958789Seric PrivacyFlags = PRIV_AUTHWARNINGS; /* option p */ 18040973Sbostic setdefuser(); 18153654Seric setupmaps(); 18257402Seric setupmailers(); 18324943Seric } 184294Seric 18540973Sbostic 1864326Seric /* 18740973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 18840973Sbostic */ 18940973Sbostic 19040973Sbostic setdefuser() 19140973Sbostic { 19240973Sbostic struct passwd *defpwent; 19357386Seric static char defuserbuf[40]; 19440973Sbostic 19557386Seric DefUser = defuserbuf; 19640973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19757386Seric strcpy(defuserbuf, defpwent->pw_name); 19840973Sbostic else 19957386Seric strcpy(defuserbuf, "nobody"); 20040973Sbostic } 20153654Seric /* 20253654Seric ** SETUPMAPS -- set up map classes 20353654Seric ** 20453654Seric ** Since these are compiled in, they cannot be in the config file. 20553654Seric ** 20653654Seric */ 20740973Sbostic 20853654Seric setupmaps() 20953654Seric { 21053654Seric register STAB *s; 21140973Sbostic 212*58802Seric /* host name lookup map */ 213*58802Seric { 214*58802Seric extern bool host_map_init(); 215*58802Seric extern char *maphostname(); 21653654Seric 217*58802Seric s = stab("host", ST_MAPCLASS, ST_ENTER); 218*58802Seric s->s_mapclass.map_init = host_map_init; 219*58802Seric s->s_mapclass.map_lookup = maphostname; 220*58802Seric } 22153654Seric 222*58802Seric /* dequote map */ 223*58802Seric { 224*58802Seric extern bool dequote_init(); 225*58802Seric extern char *dequote_map(); 226*58802Seric 227*58802Seric s = stab("dequote", ST_MAPCLASS, ST_ENTER); 228*58802Seric s->s_mapclass.map_init = dequote_init; 229*58802Seric s->s_mapclass.map_lookup = dequote_map; 230*58802Seric } 231*58802Seric 23253654Seric # ifdef DBM_MAP 23353654Seric /* dbm file access */ 23453654Seric { 23556836Seric extern bool dbm_map_init(); 23653654Seric extern char *dbm_map_lookup(); 23753654Seric 23853654Seric s = stab("dbm", ST_MAPCLASS, ST_ENTER); 23953654Seric s->s_mapclass.map_init = dbm_map_init; 24053654Seric s->s_mapclass.map_lookup = dbm_map_lookup; 24153654Seric } 24253654Seric # endif 24353654Seric 24453654Seric # ifdef BTREE_MAP 24553654Seric /* new database file access -- btree files */ 24653654Seric { 24756823Seric extern bool bt_map_init(); 24856823Seric extern char *db_map_lookup(); 24953654Seric 25053654Seric s = stab("btree", ST_MAPCLASS, ST_ENTER); 25153654Seric s->s_mapclass.map_init = bt_map_init; 25256823Seric s->s_mapclass.map_lookup = db_map_lookup; 25353654Seric } 25453654Seric # endif 25553654Seric 25653654Seric # ifdef HASH_MAP 25753654Seric /* new database file access -- hash files */ 25853654Seric { 25956823Seric extern bool hash_map_init(); 26056823Seric extern char *db_map_lookup(); 26153654Seric 26253654Seric s = stab("hash", ST_MAPCLASS, ST_ENTER); 26353654Seric s->s_mapclass.map_init = hash_map_init; 26456823Seric s->s_mapclass.map_lookup = db_map_lookup; 26553654Seric } 26653654Seric # endif 26753654Seric 26857208Seric # ifdef NIS_MAP 26957208Seric /* NIS map access */ 27057208Seric { 27157208Seric extern bool nis_map_init(); 27257208Seric extern char *nis_map_lookup(); 27357208Seric 27457208Seric s = stab("nis", ST_MAPCLASS, ST_ENTER); 27557208Seric s->s_mapclass.map_init = nis_map_init; 27657208Seric s->s_mapclass.map_lookup = nis_map_lookup; 27757208Seric } 27857208Seric # endif 27957208Seric 28053654Seric # ifdef USERDB_MAP 28153654Seric /* user database */ 28253654Seric { 28356836Seric extern bool udb_map_init(); 28453654Seric extern char *udb_map_lookup(); 28553654Seric 28653654Seric s = stab("udb", ST_MAPCLASS, ST_ENTER); 28753654Seric s->s_mapclass.map_init = udb_map_init; 28853654Seric s->s_mapclass.map_lookup = udb_map_lookup; 28953654Seric } 29053654Seric # endif 29153654Seric } 29253654Seric /* 29356836Seric ** HOST_MAP_INIT -- initialize host class structures 29456836Seric */ 29556836Seric 29656836Seric bool 29756836Seric host_map_init(map, mapname, args) 29856836Seric MAP *map; 29956836Seric char *mapname; 30056836Seric char *args; 30156836Seric { 30256836Seric register char *p = args; 30356836Seric 30456836Seric for (;;) 30556836Seric { 30658050Seric while (isascii(*p) && isspace(*p)) 30756836Seric p++; 30856836Seric if (*p != '-') 30956836Seric break; 31056836Seric switch (*++p) 31156836Seric { 31256836Seric case 'a': 31356836Seric map->map_app = ++p; 31456836Seric break; 31556836Seric } 31658050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 31756836Seric p++; 31856836Seric if (*p != '\0') 31956836Seric *p++ = '\0'; 32056836Seric } 32156836Seric if (map->map_app != NULL) 32256836Seric map->map_app = newstr(map->map_app); 32356836Seric return TRUE; 32456836Seric } 32557402Seric /* 32657402Seric ** SETUPMAILERS -- initialize default mailers 32757402Seric */ 32856836Seric 32957402Seric setupmailers() 33057402Seric { 33157402Seric char buf[100]; 33257402Seric 33357403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 33457403Seric makemailer(buf); 33557403Seric 33657402Seric strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE"); 33757402Seric makemailer(buf); 33857402Seric 33957402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 34057402Seric makemailer(buf); 34157402Seric } 34256836Seric /* 3434326Seric ** GETRUID -- get real user id (V7) 3444326Seric */ 3454326Seric 3464326Seric getruid() 3474326Seric { 3489274Seric if (OpMode == MD_DAEMON) 3494536Seric return (RealUid); 3504536Seric else 3514536Seric return (getuid()); 3524326Seric } 3534326Seric 3544326Seric 3554326Seric /* 3564326Seric ** GETRGID -- get real group id (V7). 3574326Seric */ 3584326Seric 3594326Seric getrgid() 3604326Seric { 3619274Seric if (OpMode == MD_DAEMON) 3624536Seric return (RealGid); 3634536Seric else 3644536Seric return (getgid()); 3654326Seric } 36653654Seric /* 3679369Seric ** USERNAME -- return the user id of the logged in user. 3689369Seric ** 3699369Seric ** Parameters: 3709369Seric ** none. 3719369Seric ** 3729369Seric ** Returns: 3739369Seric ** The login name of the logged in user. 3749369Seric ** 3759369Seric ** Side Effects: 3769369Seric ** none. 3779369Seric ** 3789369Seric ** Notes: 3799369Seric ** The return value is statically allocated. 3809369Seric */ 3819369Seric 3829369Seric char * 3839369Seric username() 3849369Seric { 38517469Seric static char *myname = NULL; 3869369Seric extern char *getlogin(); 38719904Smiriam register struct passwd *pw; 3889369Seric 38917469Seric /* cache the result */ 39017469Seric if (myname == NULL) 39117469Seric { 39217469Seric myname = getlogin(); 39317469Seric if (myname == NULL || myname[0] == '\0') 39417469Seric { 39517469Seric pw = getpwuid(getruid()); 39617469Seric if (pw != NULL) 39740993Sbostic myname = newstr(pw->pw_name); 39817469Seric } 39919904Smiriam else 40019904Smiriam { 40158736Seric uid_t uid = getuid(); 40219873Smiriam 40340993Sbostic myname = newstr(myname); 40440993Sbostic if ((pw = getpwnam(myname)) == NULL || 40558736Seric (uid != 0 && uid != pw->pw_uid)) 40619904Smiriam { 40758736Seric pw = getpwuid(uid); 40824945Seric if (pw != NULL) 40940993Sbostic myname = newstr(pw->pw_name); 41019873Smiriam } 41119873Smiriam } 41217469Seric if (myname == NULL || myname[0] == '\0') 41317469Seric { 41458151Seric syserr("554 Who are you?"); 41517469Seric myname = "postmaster"; 41617469Seric } 41717469Seric } 41817469Seric 41917469Seric return (myname); 4209369Seric } 4219369Seric /* 4224190Seric ** TTYPATH -- Get the path of the user's tty 423294Seric ** 424294Seric ** Returns the pathname of the user's tty. Returns NULL if 425294Seric ** the user is not logged in or if s/he has write permission 426294Seric ** denied. 427294Seric ** 428294Seric ** Parameters: 429294Seric ** none 430294Seric ** 431294Seric ** Returns: 432294Seric ** pathname of the user's tty. 433294Seric ** NULL if not logged in or write permission denied. 434294Seric ** 435294Seric ** Side Effects: 436294Seric ** none. 437294Seric ** 438294Seric ** WARNING: 439294Seric ** Return value is in a local buffer. 440294Seric ** 441294Seric ** Called By: 442294Seric ** savemail 443294Seric */ 444294Seric 445294Seric # include <sys/stat.h> 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 58951920Seric # if defined(hpux) 59051920Seric # define LA_TYPE LA_FLOAT 59151920Seric # endif 59251920Seric 59351920Seric # ifndef LA_TYPE 59457736Seric # if defined(SYSTEM5) 59557736Seric # define LA_TYPE LA_INT 59657736Seric # define LA_AVENRUN "avenrun" 59757736Seric # else 59857736Seric # if defined(BSD) 59957736Seric # define LA_TYPE LA_SUBR 60057736Seric # else 60157736Seric # define LA_TYPE LA_ZERO 60257736Seric # endif 60357736Seric # endif 60451920Seric # endif 60551920Seric #endif 60651920Seric 60751920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) 60851920Seric 60914872Seric #include <nlist.h> 61014872Seric 61151920Seric #ifndef LA_AVENRUN 61251920Seric #define LA_AVENRUN "_avenrun" 61351920Seric #endif 61451920Seric 61551920Seric /* _PATH_UNIX should be defined in <paths.h> */ 61651920Seric #ifndef _PATH_UNIX 61751920Seric # if defined(hpux) 61851920Seric # define _PATH_UNIX "/hp-ux" 61951920Seric # endif 62051920Seric # if defined(mips) && !defined(ultrix) 62151920Seric /* powerful RISC/os */ 62251920Seric # define _PATH_UNIX "/unix" 62351920Seric # endif 62457736Seric # if defined(SYSTEM5) 62557977Seric # ifndef _PATH_UNIX 62657977Seric # define _PATH_UNIX "/unix" 62757977Seric # endif 62857736Seric # endif 62951920Seric # ifndef _PATH_UNIX 63051920Seric # define _PATH_UNIX "/vmunix" 63151920Seric # endif 63251920Seric #endif 63351920Seric 63414872Seric struct nlist Nl[] = 63514872Seric { 63651920Seric { LA_AVENRUN }, 63714872Seric #define X_AVENRUN 0 63814872Seric { 0 }, 63914872Seric }; 64014872Seric 64157736Seric #if defined(unixpc) 64257736Seric # define FSHIFT 5 64357736Seric #endif 64457736Seric 64557977Seric #if defined(__alpha) 64657977Seric # define FSHIFT 10 64757977Seric #endif 64857977Seric 64951920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT) 65051920Seric # define FSHIFT 8 65157736Seric #endif 65257736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE) 65351920Seric # define FSCALE (1 << FSHIFT) 65451920Seric #endif 65540930Srick 65614872Seric getla() 65714872Seric { 65814872Seric static int kmem = -1; 65951920Seric #if LA_TYPE == LA_INT 66024943Seric long avenrun[3]; 66151920Seric #else 66251920Seric double avenrun[3]; 66351920Seric #endif 66425615Seric extern off_t lseek(); 66557736Seric extern char *errstring(); 66657736Seric extern int errno; 66714872Seric 66814872Seric if (kmem < 0) 66914872Seric { 67024945Seric kmem = open("/dev/kmem", 0, 0); 67114872Seric if (kmem < 0) 67257736Seric { 67357736Seric if (tTd(3, 1)) 67457736Seric printf("getla: open(/dev/kmem): %s\n", 67557736Seric errstring(errno)); 67614872Seric return (-1); 67757736Seric } 67851920Seric (void) fcntl(kmem, F_SETFD, 1); 67957736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68057736Seric { 68157736Seric if (tTd(3, 1)) 68257736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68357736Seric errstring(errno)); 68414872Seric return (-1); 68557736Seric } 68614872Seric } 68757736Seric if (tTd(3, 20)) 68857736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 68924945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 69023118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 69119967Seric { 69219967Seric /* thank you Ian */ 69357736Seric if (tTd(3, 1)) 69457736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 69519967Seric return (-1); 69619967Seric } 69751920Seric #if LA_TYPE == LA_INT 69857736Seric if (tTd(3, 5)) 69957736Seric { 70057736Seric printf("getla: avenrun = %d", avenrun[0]); 70157736Seric if (tTd(3, 15)) 70257736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 70357736Seric printf("\n"); 70457736Seric } 70557736Seric if (tTd(3, 1)) 70657736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 70724943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 70851920Seric #else 70957736Seric if (tTd(3, 5)) 71057736Seric { 71157736Seric printf("getla: avenrun = %g", avenrun[0]); 71257736Seric if (tTd(3, 15)) 71357736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 71457736Seric printf("\n"); 71557736Seric } 71657736Seric if (tTd(3, 1)) 71757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 71851920Seric return ((int) (avenrun[0] + 0.5)); 71951920Seric #endif 72014872Seric } 72114872Seric 72251773Seric #else 72351920Seric #if LA_TYPE == LA_SUBR 72451773Seric 72551773Seric getla() 72651773Seric { 72751920Seric double avenrun[3]; 72851920Seric 72951920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 73057736Seric { 73157736Seric if (tTd(3, 1)) 73257736Seric perror("getla: getloadavg failed:"); 73351920Seric return (-1); 73457736Seric } 73557736Seric if (tTd(3, 1)) 73657736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73751920Seric return ((int) (avenrun[0] + 0.5)); 73851773Seric } 73951773Seric 74051773Seric #else 74151773Seric 74251773Seric getla() 74351773Seric { 74457736Seric if (tTd(3, 1)) 74557736Seric printf("getla: ZERO\n"); 74651920Seric return (0); 74751773Seric } 74851773Seric 74951773Seric #endif 75051773Seric #endif 75124943Seric /* 75224943Seric ** SHOULDQUEUE -- should this message be queued or sent? 75324943Seric ** 75424943Seric ** Compares the message cost to the load average to decide. 75524943Seric ** 75624943Seric ** Parameters: 75724943Seric ** pri -- the priority of the message in question. 75857438Seric ** ctime -- the message creation time. 75924943Seric ** 76024943Seric ** Returns: 76124943Seric ** TRUE -- if this message should be queued up for the 76224943Seric ** time being. 76324943Seric ** FALSE -- if the load is low enough to send this message. 76424943Seric ** 76524943Seric ** Side Effects: 76624943Seric ** none. 76724943Seric */ 76824943Seric 76924943Seric bool 77057438Seric shouldqueue(pri, ctime) 77124943Seric long pri; 77257438Seric time_t ctime; 77324943Seric { 77451920Seric if (CurrentLA < QueueLA) 77524943Seric return (FALSE); 77658132Seric if (CurrentLA >= RefuseLA) 77758132Seric return (TRUE); 77851920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 77924943Seric } 78024943Seric /* 78153037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 78253037Seric ** 78353037Seric ** Parameters: 78453037Seric ** none. 78553037Seric ** 78653037Seric ** Returns: 78753037Seric ** TRUE if incoming SMTP connections should be refused 78853037Seric ** (for now). 78953037Seric ** FALSE if we should accept new work. 79053037Seric ** 79153037Seric ** Side Effects: 79253037Seric ** none. 79353037Seric */ 79453037Seric 79553037Seric bool 79653037Seric refuseconnections() 79753037Seric { 79853037Seric /* this is probably too simplistic */ 79958132Seric return (CurrentLA >= RefuseLA); 80053037Seric } 80153037Seric /* 80224943Seric ** SETPROCTITLE -- set process title for ps 80324943Seric ** 80424943Seric ** Parameters: 80558674Seric ** fmt -- a printf style format string. 80658674Seric ** a, b, c -- possible parameters to fmt. 80724943Seric ** 80824943Seric ** Returns: 80924943Seric ** none. 81024943Seric ** 81124943Seric ** Side Effects: 81224943Seric ** Clobbers argv of our main procedure so ps(1) will 81324943Seric ** display the title. 81424943Seric */ 81524943Seric 81658689Seric #ifdef SETPROCTITLE 81758689Seric # ifdef __hpux 81858689Seric # include <sys/pstat.h> 81958689Seric # endif 82058689Seric #endif 82158689Seric 82224943Seric /*VARARGS1*/ 82357642Seric #ifdef __STDC__ 82457642Seric setproctitle(char *fmt, ...) 82557642Seric #else 82657642Seric setproctitle(fmt, va_alist) 82724943Seric char *fmt; 82857642Seric va_dcl 82957642Seric #endif 83024943Seric { 83124943Seric # ifdef SETPROCTITLE 83224943Seric register char *p; 83325049Seric register int i; 83458674Seric char buf[MAXLINE]; 83556852Seric VA_LOCAL_DECL 83658689Seric # ifdef __hpux 83758689Seric union pstun pst; 83858689Seric # endif 83924943Seric extern char **Argv; 84024943Seric extern char *LastArgv; 84124943Seric 84258674Seric p = buf; 84324943Seric 84458674Seric /* print sendmail: heading for grep */ 84558674Seric (void) strcpy(p, "sendmail: "); 84658674Seric p += strlen(p); 84724943Seric 84858674Seric /* print the argument string */ 84958674Seric VA_START(fmt); 85058674Seric (void) vsprintf(p, fmt, ap); 85156852Seric VA_END; 85254996Seric 85358674Seric i = strlen(buf); 85458689Seric 85558689Seric # ifdef __hpux 85658689Seric pst.pst_command = buf; 85758689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 85858689Seric # else 85958689Seric 86054996Seric if (i > LastArgv - Argv[0] - 2) 86125049Seric { 86254996Seric i = LastArgv - Argv[0] - 2; 86358674Seric buf[i] = '\0'; 86425049Seric } 86558674Seric (void) strcpy(Argv[0], buf); 86654997Seric p = &Argv[0][i]; 86724943Seric while (p < LastArgv) 86824943Seric *p++ = ' '; 86958689Seric # endif 87056795Seric # endif /* SETPROCTITLE */ 87124943Seric } 87225698Seric /* 87325698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 87425698Seric ** 87525698Seric ** Parameters: 87625698Seric ** none. 87725698Seric ** 87825698Seric ** Returns: 87925698Seric ** none. 88025698Seric ** 88125698Seric ** Side Effects: 88225698Seric ** Picks up extant zombies. 88325698Seric */ 88425698Seric 88525698Seric # include <sys/wait.h> 88625698Seric 88746928Sbostic void 88825698Seric reapchild() 88925698Seric { 89025698Seric # ifdef WNOHANG 89125698Seric union wait status; 89225698Seric 89346928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 89425698Seric continue; 89556795Seric # else /* WNOHANG */ 89625698Seric auto int status; 89725698Seric 89846928Sbostic while (wait((int *)&status) > 0) 89925698Seric continue; 90056795Seric # endif /* WNOHANG */ 90158061Seric # ifdef SYSTEM5 90258061Seric (void) signal(SIGCHLD, reapchild); 90358061Seric # endif 90425698Seric } 90555418Seric /* 90655418Seric ** UNSETENV -- remove a variable from the environment 90755418Seric ** 90855418Seric ** Not needed on newer systems. 90955418Seric ** 91055418Seric ** Parameters: 91155418Seric ** name -- the string name of the environment variable to be 91255418Seric ** deleted from the current environment. 91355418Seric ** 91455418Seric ** Returns: 91555418Seric ** none. 91655418Seric ** 91755418Seric ** Globals: 91855418Seric ** environ -- a pointer to the current environment. 91955418Seric ** 92055418Seric ** Side Effects: 92155418Seric ** Modifies environ. 92255418Seric */ 92355418Seric 92455418Seric #ifdef UNSETENV 92555418Seric 92655418Seric void 92755418Seric unsetenv(name) 92855418Seric char *name; 92955418Seric { 93055418Seric extern char **environ; 93155418Seric register char **pp; 93255418Seric int len = strlen(name); 93355418Seric 93455418Seric for (pp = environ; *pp != NULL; pp++) 93555418Seric { 93655418Seric if (strncmp(name, *pp, len) == 0 && 93755418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 93855418Seric break; 93955418Seric } 94055418Seric 94155418Seric for (; *pp != NULL; pp++) 94255418Seric *pp = pp[1]; 94355418Seric } 94455418Seric 94555418Seric #endif /* UNSETENV */ 94656215Seric /* 94756215Seric ** GETDTABLESIZE -- return number of file descriptors 94856215Seric ** 94956215Seric ** Only on non-BSD systems 95056215Seric ** 95156215Seric ** Parameters: 95256215Seric ** none 95356215Seric ** 95456215Seric ** Returns: 95556215Seric ** size of file descriptor table 95656215Seric ** 95756215Seric ** Side Effects: 95856215Seric ** none 95956215Seric */ 96056215Seric 96156215Seric #ifdef SYSTEM5 96256215Seric 96356215Seric int 96456215Seric getdtablesize() 96556215Seric { 96658689Seric # ifdef _SC_OPEN_MAX 96758689Seric return sysconf(_SC_OPEN_MAX); 96858689Seric # else 96956215Seric return NOFILE; 97058689Seric # endif 97156215Seric } 97256215Seric 97356215Seric #endif 97457631Seric /* 97557631Seric ** UNAME -- get the UUCP name of this system. 97657631Seric */ 97757631Seric 97857943Seric #ifndef HASUNAME 97957631Seric 98057631Seric int 98157631Seric uname(name) 98257631Seric struct utsname *name; 98357631Seric { 98457631Seric FILE *file; 98557631Seric char *n; 98657631Seric 98757631Seric name->nodename[0] = '\0'; 98857631Seric 98957661Seric /* try /etc/whoami -- one line with the node name */ 99057631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 99157631Seric { 99257661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 99357631Seric (void) fclose(file); 99457661Seric n = strchr(name->nodename, '\n'); 99557631Seric if (n != NULL) 99657631Seric *n = '\0'; 99757631Seric if (name->nodename[0] != '\0') 99857631Seric return (0); 99957631Seric } 100057631Seric 100157661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 100257631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 100357631Seric { 100457631Seric char buf[MAXLINE]; 100557631Seric 100657631Seric while (fgets(buf, MAXLINE, file) != NULL) 100757631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 100857631Seric NODE_LENGTH, name->nodename) > 0) 100957631Seric break; 101057631Seric (void) fclose(file); 101157631Seric if (name->nodename[0] != '\0') 101257631Seric return (0); 101357631Seric } 101457631Seric 101557631Seric #ifdef TRUST_POPEN 101657631Seric /* 101757631Seric ** Popen is known to have security holes. 101857631Seric */ 101957631Seric 102057661Seric /* try uuname -l to return local name */ 102157631Seric if ((file = popen("uuname -l", "r")) != NULL) 102257631Seric { 102357661Seric (void) fgets(name, NODE_LENGTH + 1, file); 102457631Seric (void) pclose(file); 102557661Seric n = strchr(name, '\n'); 102657631Seric if (n != NULL) 102757631Seric *n = '\0'; 102857661Seric if (name->nodename[0] != '\0') 102957631Seric return (0); 103057631Seric } 103157631Seric #endif 103257631Seric 103357631Seric return (-1); 103457631Seric } 103557943Seric #endif /* HASUNAME */ 103658068Seric /* 103758068Seric ** INITGROUPS -- initialize groups 103858068Seric ** 103958068Seric ** Stub implementation for System V style systems 104058068Seric */ 104158068Seric 104258068Seric #ifndef HASINITGROUPS 104358068Seric # if !defined(SYSTEM5) || defined(hpux) 104458068Seric # define HASINITGROUPS 104558068Seric # endif 104658068Seric #endif 104758068Seric 104858068Seric #ifndef HASINITGROUPS 104958068Seric 105058068Seric initgroups(name, basegid) 105158068Seric char *name; 105258068Seric int basegid; 105358068Seric { 105458068Seric return 0; 105558068Seric } 105658068Seric 105758068Seric #endif 105858082Seric /* 105958082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 106058082Seric ** 106158082Seric ** Only implemented if you have statfs. 106258082Seric ** 106358082Seric ** Parameters: 106458333Seric ** msize -- the size to check against. If zero, we don't yet 106558333Seric ** know how big the message will be, so just check for 106658333Seric ** a "reasonable" amount. 106758082Seric ** 106858082Seric ** Returns: 106958082Seric ** TRUE if there is enough space. 107058082Seric ** FALSE otherwise. 107158082Seric */ 107258082Seric 107358082Seric #ifndef HASSTATFS 107458082Seric # if defined(BSD4_4) || defined(__osf__) 107558082Seric # define HASSTATFS 107658082Seric # endif 107758082Seric #endif 107858082Seric 107958082Seric #ifdef HASSTATFS 108058157Seric # undef HASUSTAT 108158157Seric #endif 108258157Seric 108358157Seric #if defined(HASUSTAT) 108458157Seric # include <sys/stat.h> 108558157Seric # include <ustat.h> 108658157Seric #endif 108758157Seric 108858157Seric #ifdef HASSTATFS 108958133Seric # if defined(sgi) || defined(apollo) 109058133Seric # include <sys/statfs.h> 109158133Seric # else 109258133Seric # if defined(sun) || defined(hpux) 109358133Seric # include <sys/vfs.h> 109458133Seric # else 109558157Seric # include <sys/mount.h> 109658133Seric # endif 109758133Seric # endif 109858082Seric #endif 109958082Seric 110058082Seric bool 110158333Seric enoughspace(msize) 110258333Seric long msize; 110358082Seric { 110458160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 110558157Seric # if defined(HASUSTAT) 110658153Seric struct ustat fs; 110758153Seric struct stat statbuf; 110858366Seric # define FSBLOCKSIZE DEV_BSIZE 110958157Seric # define f_bavail f_tfree 111058157Seric # else 111158157Seric # if defined(ultrix) 111258157Seric struct fs_data fs; 111358157Seric # define f_bavail fd_bfreen 111458366Seric # define FSBLOCKSIZE fs.fd_bsize 111558153Seric # else 111658082Seric struct statfs fs; 111758366Seric # define FSBLOCKSIZE fs.f_bsize 111858153Seric # endif 111958133Seric # endif 112058333Seric long blocksneeded; 112158082Seric extern int errno; 112258082Seric extern char *errstring(); 112358082Seric 112458333Seric if (MinBlocksFree <= 0 && msize <= 0) 112558082Seric { 112658082Seric if (tTd(4, 80)) 112758133Seric printf("enoughspace: no threshold\n"); 112858133Seric return TRUE; 112958133Seric } 113058333Seric 113158157Seric # if defined(HASUSTAT) 113258157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 113358157Seric # else 113458157Seric # if defined(sgi) || defined(apollo) 113558133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 113658157Seric # else 113758157Seric # if defined(ultrix) 113858133Seric if (statfs(QueueDir, &fs) > 0) 113958153Seric # else 114058133Seric if (statfs(QueueDir, &fs) == 0) 114158153Seric # endif 114258133Seric # endif 114358133Seric # endif 114458133Seric { 114558133Seric if (tTd(4, 80)) 114658333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 114758333Seric fs.f_bavail, msize); 114858333Seric 114958333Seric /* convert msize to block count */ 115058366Seric msize = msize / FSBLOCKSIZE + 1; 115158333Seric if (MinBlocksFree >= 0) 115258333Seric msize += MinBlocksFree; 115358333Seric 115458333Seric if (fs.f_bavail < msize) 115558090Seric { 115658090Seric #ifdef LOG 115758090Seric if (LogLevel > 0) 115858090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 115958333Seric QueueDir, fs.f_bavail, msize); 116058090Seric #endif 116158082Seric return FALSE; 116258090Seric } 116358082Seric } 116458082Seric else if (tTd(4, 80)) 116558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 116658333Seric MinBlocksFree, msize, errstring(errno)); 116758082Seric #endif 116858082Seric return TRUE; 116958082Seric } 117058542Seric /* 117158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 117258542Seric ** 117358542Seric ** This looks at an errno value and tells if this is likely to 117458542Seric ** go away if retried later. 117558542Seric ** 117658542Seric ** Parameters: 117758542Seric ** err -- the errno code to classify. 117858542Seric ** 117958542Seric ** Returns: 118058542Seric ** TRUE if this is probably transient. 118158542Seric ** FALSE otherwise. 118258542Seric */ 118358542Seric 118458542Seric bool 118558542Seric transienterror(err) 118658542Seric int err; 118758542Seric { 118858542Seric switch (err) 118958542Seric { 119058542Seric case EIO: /* I/O error */ 119158542Seric case ENXIO: /* Device not configured */ 119258542Seric case EAGAIN: /* Resource temporarily unavailable */ 119358542Seric case ENOMEM: /* Cannot allocate memory */ 119458542Seric case ENODEV: /* Operation not supported by device */ 119558542Seric case ENFILE: /* Too many open files in system */ 119658542Seric case EMFILE: /* Too many open files */ 119758542Seric case ENOSPC: /* No space left on device */ 119858542Seric #ifdef ETIMEDOUT 119958542Seric case ETIMEDOUT: /* Connection timed out */ 120058542Seric #endif 120158542Seric #ifdef ESTALE 120258542Seric case ESTALE: /* Stale NFS file handle */ 120358542Seric #endif 120458542Seric #ifdef ENETDOWN 120558542Seric case ENETDOWN: /* Network is down */ 120658542Seric #endif 120758542Seric #ifdef ENETUNREACH 120858542Seric case ENETUNREACH: /* Network is unreachable */ 120958542Seric #endif 121058542Seric #ifdef ENETRESET 121158542Seric case ENETRESET: /* Network dropped connection on reset */ 121258542Seric #endif 121358542Seric #ifdef ECONNABORTED 121458542Seric case ECONNABORTED: /* Software caused connection abort */ 121558542Seric #endif 121658542Seric #ifdef ECONNRESET 121758542Seric case ECONNRESET: /* Connection reset by peer */ 121858542Seric #endif 121958542Seric #ifdef ENOBUFS 122058542Seric case ENOBUFS: /* No buffer space available */ 122158542Seric #endif 122258542Seric #ifdef ESHUTDOWN 122358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 122458542Seric #endif 122558542Seric #ifdef ECONNREFUSED 122658542Seric case ECONNREFUSED: /* Connection refused */ 122758542Seric #endif 122858542Seric #ifdef EHOSTDOWN 122958542Seric case EHOSTDOWN: /* Host is down */ 123058542Seric #endif 123158542Seric #ifdef EHOSTUNREACH 123258542Seric case EHOSTUNREACH: /* No route to host */ 123358542Seric #endif 123458542Seric #ifdef EDQUOT 123558542Seric case EDQUOT: /* Disc quota exceeded */ 123658542Seric #endif 123758542Seric #ifdef EPROCLIM 123858542Seric case EPROCLIM: /* Too many processes */ 123958542Seric #endif 124058542Seric #ifdef EUSERS 124158542Seric case EUSERS: /* Too many users */ 124258542Seric #endif 124358542Seric #ifdef EDEADLK 124458542Seric case EDEADLK: /* Resource deadlock avoided */ 124558542Seric #endif 124658542Seric #ifdef EISCONN 124758542Seric case EISCONN: /* Socket already connected */ 124858542Seric #endif 124958542Seric #ifdef EINPROGRESS 125058542Seric case EINPROGRESS: /* Operation now in progress */ 125158542Seric #endif 125258542Seric #ifdef EALREADY 125358542Seric case EALREADY: /* Operation already in progress */ 125458542Seric #endif 125558542Seric #ifdef EADDRINUSE 125658542Seric case EADDRINUSE: /* Address already in use */ 125758542Seric #endif 125858542Seric #ifdef EADDRNOTAVAIL 125958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 126058542Seric #endif 126158542Seric #ifdef ENOSR 126258542Seric case ENOSR: /* Out of streams resources */ 126358542Seric #endif 126458542Seric return TRUE; 126558542Seric } 126658542Seric 126758542Seric /* nope, must be permanent */ 126858542Seric return FALSE; 126958542Seric } 127058689Seric /* 127158689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 127258689Seric ** 127358689Seric ** Parameters: 127458689Seric ** fd -- the file descriptor of the file. 127558689Seric ** filename -- the file name (for error messages). 127658689Seric ** type -- type of the lock. Bits can be: 127758689Seric ** LOCK_EX -- exclusive lock. 127858689Seric ** LOCK_NB -- non-blocking. 127958689Seric ** 128058689Seric ** Returns: 128158689Seric ** TRUE if the lock was acquired. 128258689Seric ** FALSE otherwise. 128358689Seric */ 128458689Seric 128558689Seric bool 128658689Seric lockfile(fd, filename, type) 128758689Seric int fd; 128858689Seric char *filename; 128958689Seric int type; 129058689Seric { 129158689Seric # ifdef LOCKF 129258689Seric int action; 129358689Seric struct flock lfd; 129458689Seric 129558701Seric if (bitset(LOCK_EX, type)) 129658689Seric lfd.l_type = F_WRLCK; 129758689Seric else 129858689Seric lfd.l_type = F_RDLCK; 129958689Seric 130058689Seric if (bitset(LOCK_NB, type)) 130158689Seric action = F_SETLK; 130258689Seric else 130358689Seric action = F_SETLKW; 130458689Seric 130558689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 130658689Seric 130758689Seric if (fcntl(fd, action, &lfd) >= 0) 130858689Seric return TRUE; 130958689Seric 131058689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 131158689Seric syserr("cannot lockf(%s)", filename); 131258689Seric # else 131358689Seric if (flock(fd, type) >= 0) 131458689Seric return TRUE; 131558689Seric 131658689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 131758689Seric syserr("cannot flock(%s)", filename); 131858689Seric # endif 131958689Seric return FALSE; 132058689Seric } 1321