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*58796Seric static char sccsid[] = "@(#)conf.c 6.42 (Berkeley) 03/23/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, 66*58796Seric 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, 75*58796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 81*58796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 85*58796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 889055Seric "via", H_TRACE|H_FORCE, 899055Seric "mail-from", H_TRACE|H_FORCE, 908060Seric 91*58796Seric /* miscellaneous fields */ 92*58796Seric "comments", H_FORCE, 93*58796Seric "return-path", H_ACHECK, 94*58796Seric 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; 21156836Seric extern bool host_map_init(); 21253654Seric extern char *maphostname(); 21340973Sbostic 21453654Seric /* set up host name lookup map */ 21553654Seric s = stab("host", ST_MAPCLASS, ST_ENTER); 21656836Seric s->s_mapclass.map_init = host_map_init; 21753654Seric s->s_mapclass.map_lookup = maphostname; 21853654Seric 21953654Seric /* 22053654Seric ** Set up other map classes. 22153654Seric */ 22253654Seric 22353654Seric # ifdef DBM_MAP 22453654Seric /* dbm file access */ 22553654Seric { 22656836Seric extern bool dbm_map_init(); 22753654Seric extern char *dbm_map_lookup(); 22853654Seric 22953654Seric s = stab("dbm", ST_MAPCLASS, ST_ENTER); 23053654Seric s->s_mapclass.map_init = dbm_map_init; 23153654Seric s->s_mapclass.map_lookup = dbm_map_lookup; 23253654Seric } 23353654Seric # endif 23453654Seric 23553654Seric # ifdef BTREE_MAP 23653654Seric /* new database file access -- btree files */ 23753654Seric { 23856823Seric extern bool bt_map_init(); 23956823Seric extern char *db_map_lookup(); 24053654Seric 24153654Seric s = stab("btree", ST_MAPCLASS, ST_ENTER); 24253654Seric s->s_mapclass.map_init = bt_map_init; 24356823Seric s->s_mapclass.map_lookup = db_map_lookup; 24453654Seric } 24553654Seric # endif 24653654Seric 24753654Seric # ifdef HASH_MAP 24853654Seric /* new database file access -- hash files */ 24953654Seric { 25056823Seric extern bool hash_map_init(); 25156823Seric extern char *db_map_lookup(); 25253654Seric 25353654Seric s = stab("hash", ST_MAPCLASS, ST_ENTER); 25453654Seric s->s_mapclass.map_init = hash_map_init; 25556823Seric s->s_mapclass.map_lookup = db_map_lookup; 25653654Seric } 25753654Seric # endif 25853654Seric 25957208Seric # ifdef NIS_MAP 26057208Seric /* NIS map access */ 26157208Seric { 26257208Seric extern bool nis_map_init(); 26357208Seric extern char *nis_map_lookup(); 26457208Seric 26557208Seric s = stab("nis", ST_MAPCLASS, ST_ENTER); 26657208Seric s->s_mapclass.map_init = nis_map_init; 26757208Seric s->s_mapclass.map_lookup = nis_map_lookup; 26857208Seric } 26957208Seric # endif 27057208Seric 27153654Seric # ifdef USERDB_MAP 27253654Seric /* user database */ 27353654Seric { 27456836Seric extern bool udb_map_init(); 27553654Seric extern char *udb_map_lookup(); 27653654Seric 27753654Seric s = stab("udb", ST_MAPCLASS, ST_ENTER); 27853654Seric s->s_mapclass.map_init = udb_map_init; 27953654Seric s->s_mapclass.map_lookup = udb_map_lookup; 28053654Seric } 28153654Seric # endif 28253654Seric } 28353654Seric /* 28456836Seric ** HOST_MAP_INIT -- initialize host class structures 28556836Seric */ 28656836Seric 28756836Seric bool 28856836Seric host_map_init(map, mapname, args) 28956836Seric MAP *map; 29056836Seric char *mapname; 29156836Seric char *args; 29256836Seric { 29356836Seric register char *p = args; 29456836Seric 29556836Seric for (;;) 29656836Seric { 29758050Seric while (isascii(*p) && isspace(*p)) 29856836Seric p++; 29956836Seric if (*p != '-') 30056836Seric break; 30156836Seric switch (*++p) 30256836Seric { 30356836Seric case 'a': 30456836Seric map->map_app = ++p; 30556836Seric break; 30656836Seric } 30758050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 30856836Seric p++; 30956836Seric if (*p != '\0') 31056836Seric *p++ = '\0'; 31156836Seric } 31256836Seric if (map->map_app != NULL) 31356836Seric map->map_app = newstr(map->map_app); 31456836Seric return TRUE; 31556836Seric } 31657402Seric /* 31757402Seric ** SETUPMAILERS -- initialize default mailers 31857402Seric */ 31956836Seric 32057402Seric setupmailers() 32157402Seric { 32257402Seric char buf[100]; 32357402Seric 32457403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 32557403Seric makemailer(buf); 32657403Seric 32757402Seric strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE"); 32857402Seric makemailer(buf); 32957402Seric 33057402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 33157402Seric makemailer(buf); 33257402Seric } 33356836Seric /* 3344326Seric ** GETRUID -- get real user id (V7) 3354326Seric */ 3364326Seric 3374326Seric getruid() 3384326Seric { 3399274Seric if (OpMode == MD_DAEMON) 3404536Seric return (RealUid); 3414536Seric else 3424536Seric return (getuid()); 3434326Seric } 3444326Seric 3454326Seric 3464326Seric /* 3474326Seric ** GETRGID -- get real group id (V7). 3484326Seric */ 3494326Seric 3504326Seric getrgid() 3514326Seric { 3529274Seric if (OpMode == MD_DAEMON) 3534536Seric return (RealGid); 3544536Seric else 3554536Seric return (getgid()); 3564326Seric } 35753654Seric /* 3589369Seric ** USERNAME -- return the user id of the logged in user. 3599369Seric ** 3609369Seric ** Parameters: 3619369Seric ** none. 3629369Seric ** 3639369Seric ** Returns: 3649369Seric ** The login name of the logged in user. 3659369Seric ** 3669369Seric ** Side Effects: 3679369Seric ** none. 3689369Seric ** 3699369Seric ** Notes: 3709369Seric ** The return value is statically allocated. 3719369Seric */ 3729369Seric 3739369Seric char * 3749369Seric username() 3759369Seric { 37617469Seric static char *myname = NULL; 3779369Seric extern char *getlogin(); 37819904Smiriam register struct passwd *pw; 3799369Seric 38017469Seric /* cache the result */ 38117469Seric if (myname == NULL) 38217469Seric { 38317469Seric myname = getlogin(); 38417469Seric if (myname == NULL || myname[0] == '\0') 38517469Seric { 38617469Seric pw = getpwuid(getruid()); 38717469Seric if (pw != NULL) 38840993Sbostic myname = newstr(pw->pw_name); 38917469Seric } 39019904Smiriam else 39119904Smiriam { 39258736Seric uid_t uid = getuid(); 39319873Smiriam 39440993Sbostic myname = newstr(myname); 39540993Sbostic if ((pw = getpwnam(myname)) == NULL || 39658736Seric (uid != 0 && uid != pw->pw_uid)) 39719904Smiriam { 39858736Seric pw = getpwuid(uid); 39924945Seric if (pw != NULL) 40040993Sbostic myname = newstr(pw->pw_name); 40119873Smiriam } 40219873Smiriam } 40317469Seric if (myname == NULL || myname[0] == '\0') 40417469Seric { 40558151Seric syserr("554 Who are you?"); 40617469Seric myname = "postmaster"; 40717469Seric } 40817469Seric } 40917469Seric 41017469Seric return (myname); 4119369Seric } 4129369Seric /* 4134190Seric ** TTYPATH -- Get the path of the user's tty 414294Seric ** 415294Seric ** Returns the pathname of the user's tty. Returns NULL if 416294Seric ** the user is not logged in or if s/he has write permission 417294Seric ** denied. 418294Seric ** 419294Seric ** Parameters: 420294Seric ** none 421294Seric ** 422294Seric ** Returns: 423294Seric ** pathname of the user's tty. 424294Seric ** NULL if not logged in or write permission denied. 425294Seric ** 426294Seric ** Side Effects: 427294Seric ** none. 428294Seric ** 429294Seric ** WARNING: 430294Seric ** Return value is in a local buffer. 431294Seric ** 432294Seric ** Called By: 433294Seric ** savemail 434294Seric */ 435294Seric 436294Seric # include <sys/stat.h> 437294Seric 438294Seric char * 439294Seric ttypath() 440294Seric { 441294Seric struct stat stbuf; 442294Seric register char *pathn; 443294Seric extern char *ttyname(); 4444081Seric extern char *getlogin(); 445294Seric 446294Seric /* compute the pathname of the controlling tty */ 4479369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4489369Seric (pathn = ttyname(0)) == NULL) 449294Seric { 450294Seric errno = 0; 451294Seric return (NULL); 452294Seric } 453294Seric 454294Seric /* see if we have write permission */ 4552967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 456294Seric { 457294Seric errno = 0; 458294Seric return (NULL); 459294Seric } 460294Seric 461294Seric /* see if the user is logged in */ 462294Seric if (getlogin() == NULL) 463294Seric return (NULL); 464294Seric 465294Seric /* looks good */ 466294Seric return (pathn); 467294Seric } 4682967Seric /* 4692967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4702967Seric ** 4712967Seric ** This routine can be supplied on a per-installation basis 4722967Seric ** to determine whether a person is allowed to send a message. 4732967Seric ** This allows restriction of certain types of internet 4742967Seric ** forwarding or registration of users. 4752967Seric ** 4762967Seric ** If the hosts are found to be incompatible, an error 47757454Seric ** message should be given using "usrerr" and 0 should 4782967Seric ** be returned. 4792967Seric ** 4804288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4814288Seric ** function; this should be done on huge messages. 4824288Seric ** 4832967Seric ** Parameters: 4842967Seric ** to -- the person being sent to. 4852967Seric ** 4862967Seric ** Returns: 48757459Seric ** an exit status 4882967Seric ** 4892967Seric ** Side Effects: 4902967Seric ** none (unless you include the usrerr stuff) 4912967Seric */ 4922967Seric 49355012Seric checkcompat(to, e) 4942967Seric register ADDRESS *to; 49555012Seric register ENVELOPE *e; 4962967Seric { 49712133Seric # ifdef lint 49812133Seric if (to == NULL) 49912133Seric to++; 50012133Seric # endif lint 50110698Seric # ifdef EXAMPLE_CODE 50210698Seric /* this code is intended as an example only */ 5034437Seric register STAB *s; 5044437Seric 5054437Seric s = stab("arpa", ST_MAILER, ST_FIND); 50655012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 5079369Seric to->q_mailer == s->s_mailer) 5084437Seric { 50958151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 51010698Seric /* NoReturn = TRUE; to supress return copy */ 51157459Seric return (EX_UNAVAILABLE); 5124437Seric } 51356795Seric # endif /* EXAMPLE_CODE */ 51457459Seric return (EX_OK); 5152967Seric } 5169369Seric /* 5179369Seric ** HOLDSIGS -- arrange to hold all signals 5189369Seric ** 5199369Seric ** Parameters: 5209369Seric ** none. 5219369Seric ** 5229369Seric ** Returns: 5239369Seric ** none. 5249369Seric ** 5259369Seric ** Side Effects: 5269369Seric ** Arranges that signals are held. 5279369Seric */ 5289369Seric 5299369Seric holdsigs() 5309369Seric { 5319369Seric } 5329369Seric /* 5339369Seric ** RLSESIGS -- arrange to release all signals 5349369Seric ** 5359369Seric ** This undoes the effect of holdsigs. 5369369Seric ** 5379369Seric ** Parameters: 5389369Seric ** none. 5399369Seric ** 5409369Seric ** Returns: 5419369Seric ** none. 5429369Seric ** 5439369Seric ** Side Effects: 5449369Seric ** Arranges that signals are released. 5459369Seric */ 5469369Seric 5479369Seric rlsesigs() 5489369Seric { 5499369Seric } 55014872Seric /* 55114872Seric ** GETLA -- get the current load average 55214872Seric ** 55314881Seric ** This code stolen from la.c. 55414881Seric ** 55514872Seric ** Parameters: 55614872Seric ** none. 55714872Seric ** 55814872Seric ** Returns: 55914872Seric ** The current load average as an integer. 56014872Seric ** 56114872Seric ** Side Effects: 56214872Seric ** none. 56314872Seric */ 56414872Seric 56551920Seric /* try to guess what style of load average we have */ 56651920Seric #define LA_ZERO 1 /* always return load average as zero */ 56751920Seric #define LA_INT 2 /* read kmem for avenrun; interpret as int */ 56851920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 56951920Seric #define LA_SUBR 4 /* call getloadavg */ 57014872Seric 57151920Seric #ifndef LA_TYPE 57251920Seric # if defined(sun) 57351920Seric # define LA_TYPE LA_INT 57451920Seric # endif 57557977Seric # if defined(mips) || defined(__alpha) 57657977Seric /* Ultrix or OSF/1 or RISC/os */ 57751920Seric # define LA_TYPE LA_INT 57851920Seric # define LA_AVENRUN "avenrun" 57951920Seric # endif 58051920Seric # if defined(hpux) 58151920Seric # define LA_TYPE LA_FLOAT 58251920Seric # endif 58351920Seric 58451920Seric # ifndef LA_TYPE 58557736Seric # if defined(SYSTEM5) 58657736Seric # define LA_TYPE LA_INT 58757736Seric # define LA_AVENRUN "avenrun" 58857736Seric # else 58957736Seric # if defined(BSD) 59057736Seric # define LA_TYPE LA_SUBR 59157736Seric # else 59257736Seric # define LA_TYPE LA_ZERO 59357736Seric # endif 59457736Seric # endif 59551920Seric # endif 59651920Seric #endif 59751920Seric 59851920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) 59951920Seric 60014872Seric #include <nlist.h> 60114872Seric 60251920Seric #ifndef LA_AVENRUN 60351920Seric #define LA_AVENRUN "_avenrun" 60451920Seric #endif 60551920Seric 60651920Seric /* _PATH_UNIX should be defined in <paths.h> */ 60751920Seric #ifndef _PATH_UNIX 60851920Seric # if defined(hpux) 60951920Seric # define _PATH_UNIX "/hp-ux" 61051920Seric # endif 61151920Seric # if defined(mips) && !defined(ultrix) 61251920Seric /* powerful RISC/os */ 61351920Seric # define _PATH_UNIX "/unix" 61451920Seric # endif 61557736Seric # if defined(SYSTEM5) 61657977Seric # ifndef _PATH_UNIX 61757977Seric # define _PATH_UNIX "/unix" 61857977Seric # endif 61957736Seric # endif 62051920Seric # ifndef _PATH_UNIX 62151920Seric # define _PATH_UNIX "/vmunix" 62251920Seric # endif 62351920Seric #endif 62451920Seric 62514872Seric struct nlist Nl[] = 62614872Seric { 62751920Seric { LA_AVENRUN }, 62814872Seric #define X_AVENRUN 0 62914872Seric { 0 }, 63014872Seric }; 63114872Seric 63257736Seric #if defined(unixpc) 63357736Seric # define FSHIFT 5 63457736Seric #endif 63557736Seric 63657977Seric #if defined(__alpha) 63757977Seric # define FSHIFT 10 63857977Seric #endif 63957977Seric 64051920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT) 64151920Seric # define FSHIFT 8 64257736Seric #endif 64357736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE) 64451920Seric # define FSCALE (1 << FSHIFT) 64551920Seric #endif 64640930Srick 64714872Seric getla() 64814872Seric { 64914872Seric static int kmem = -1; 65051920Seric #if LA_TYPE == LA_INT 65124943Seric long avenrun[3]; 65251920Seric #else 65351920Seric double avenrun[3]; 65451920Seric #endif 65525615Seric extern off_t lseek(); 65657736Seric extern char *errstring(); 65757736Seric extern int errno; 65814872Seric 65914872Seric if (kmem < 0) 66014872Seric { 66124945Seric kmem = open("/dev/kmem", 0, 0); 66214872Seric if (kmem < 0) 66357736Seric { 66457736Seric if (tTd(3, 1)) 66557736Seric printf("getla: open(/dev/kmem): %s\n", 66657736Seric errstring(errno)); 66714872Seric return (-1); 66857736Seric } 66951920Seric (void) fcntl(kmem, F_SETFD, 1); 67057736Seric if (nlist(_PATH_UNIX, Nl) < 0) 67157736Seric { 67257736Seric if (tTd(3, 1)) 67357736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 67457736Seric errstring(errno)); 67514872Seric return (-1); 67657736Seric } 67714872Seric } 67857736Seric if (tTd(3, 20)) 67957736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 68024945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 68123118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 68219967Seric { 68319967Seric /* thank you Ian */ 68457736Seric if (tTd(3, 1)) 68557736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 68619967Seric return (-1); 68719967Seric } 68851920Seric #if LA_TYPE == LA_INT 68957736Seric if (tTd(3, 5)) 69057736Seric { 69157736Seric printf("getla: avenrun = %d", avenrun[0]); 69257736Seric if (tTd(3, 15)) 69357736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 69457736Seric printf("\n"); 69557736Seric } 69657736Seric if (tTd(3, 1)) 69757736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 69824943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 69951920Seric #else 70057736Seric if (tTd(3, 5)) 70157736Seric { 70257736Seric printf("getla: avenrun = %g", avenrun[0]); 70357736Seric if (tTd(3, 15)) 70457736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 70557736Seric printf("\n"); 70657736Seric } 70757736Seric if (tTd(3, 1)) 70857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 70951920Seric return ((int) (avenrun[0] + 0.5)); 71051920Seric #endif 71114872Seric } 71214872Seric 71351773Seric #else 71451920Seric #if LA_TYPE == LA_SUBR 71551773Seric 71651773Seric getla() 71751773Seric { 71851920Seric double avenrun[3]; 71951920Seric 72051920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 72157736Seric { 72257736Seric if (tTd(3, 1)) 72357736Seric perror("getla: getloadavg failed:"); 72451920Seric return (-1); 72557736Seric } 72657736Seric if (tTd(3, 1)) 72757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 72851920Seric return ((int) (avenrun[0] + 0.5)); 72951773Seric } 73051773Seric 73151773Seric #else 73251773Seric 73351773Seric getla() 73451773Seric { 73557736Seric if (tTd(3, 1)) 73657736Seric printf("getla: ZERO\n"); 73751920Seric return (0); 73851773Seric } 73951773Seric 74051773Seric #endif 74151773Seric #endif 74224943Seric /* 74324943Seric ** SHOULDQUEUE -- should this message be queued or sent? 74424943Seric ** 74524943Seric ** Compares the message cost to the load average to decide. 74624943Seric ** 74724943Seric ** Parameters: 74824943Seric ** pri -- the priority of the message in question. 74957438Seric ** ctime -- the message creation time. 75024943Seric ** 75124943Seric ** Returns: 75224943Seric ** TRUE -- if this message should be queued up for the 75324943Seric ** time being. 75424943Seric ** FALSE -- if the load is low enough to send this message. 75524943Seric ** 75624943Seric ** Side Effects: 75724943Seric ** none. 75824943Seric */ 75924943Seric 76024943Seric bool 76157438Seric shouldqueue(pri, ctime) 76224943Seric long pri; 76357438Seric time_t ctime; 76424943Seric { 76551920Seric if (CurrentLA < QueueLA) 76624943Seric return (FALSE); 76758132Seric if (CurrentLA >= RefuseLA) 76858132Seric return (TRUE); 76951920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 77024943Seric } 77124943Seric /* 77253037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 77353037Seric ** 77453037Seric ** Parameters: 77553037Seric ** none. 77653037Seric ** 77753037Seric ** Returns: 77853037Seric ** TRUE if incoming SMTP connections should be refused 77953037Seric ** (for now). 78053037Seric ** FALSE if we should accept new work. 78153037Seric ** 78253037Seric ** Side Effects: 78353037Seric ** none. 78453037Seric */ 78553037Seric 78653037Seric bool 78753037Seric refuseconnections() 78853037Seric { 78953037Seric /* this is probably too simplistic */ 79058132Seric return (CurrentLA >= RefuseLA); 79153037Seric } 79253037Seric /* 79324943Seric ** SETPROCTITLE -- set process title for ps 79424943Seric ** 79524943Seric ** Parameters: 79658674Seric ** fmt -- a printf style format string. 79758674Seric ** a, b, c -- possible parameters to fmt. 79824943Seric ** 79924943Seric ** Returns: 80024943Seric ** none. 80124943Seric ** 80224943Seric ** Side Effects: 80324943Seric ** Clobbers argv of our main procedure so ps(1) will 80424943Seric ** display the title. 80524943Seric */ 80624943Seric 80758689Seric #ifdef SETPROCTITLE 80858689Seric # ifdef __hpux 80958689Seric # include <sys/pstat.h> 81058689Seric # endif 81158689Seric #endif 81258689Seric 81324943Seric /*VARARGS1*/ 81457642Seric #ifdef __STDC__ 81557642Seric setproctitle(char *fmt, ...) 81657642Seric #else 81757642Seric setproctitle(fmt, va_alist) 81824943Seric char *fmt; 81957642Seric va_dcl 82057642Seric #endif 82124943Seric { 82224943Seric # ifdef SETPROCTITLE 82324943Seric register char *p; 82425049Seric register int i; 82558674Seric char buf[MAXLINE]; 82656852Seric VA_LOCAL_DECL 82758689Seric # ifdef __hpux 82858689Seric union pstun pst; 82958689Seric # endif 83024943Seric extern char **Argv; 83124943Seric extern char *LastArgv; 83224943Seric 83358674Seric p = buf; 83424943Seric 83558674Seric /* print sendmail: heading for grep */ 83658674Seric (void) strcpy(p, "sendmail: "); 83758674Seric p += strlen(p); 83824943Seric 83958674Seric /* print the argument string */ 84058674Seric VA_START(fmt); 84158674Seric (void) vsprintf(p, fmt, ap); 84256852Seric VA_END; 84354996Seric 84458674Seric i = strlen(buf); 84558689Seric 84658689Seric # ifdef __hpux 84758689Seric pst.pst_command = buf; 84858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 84958689Seric # else 85058689Seric 85154996Seric if (i > LastArgv - Argv[0] - 2) 85225049Seric { 85354996Seric i = LastArgv - Argv[0] - 2; 85458674Seric buf[i] = '\0'; 85525049Seric } 85658674Seric (void) strcpy(Argv[0], buf); 85754997Seric p = &Argv[0][i]; 85824943Seric while (p < LastArgv) 85924943Seric *p++ = ' '; 86058689Seric # endif 86156795Seric # endif /* SETPROCTITLE */ 86224943Seric } 86325698Seric /* 86425698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 86525698Seric ** 86625698Seric ** Parameters: 86725698Seric ** none. 86825698Seric ** 86925698Seric ** Returns: 87025698Seric ** none. 87125698Seric ** 87225698Seric ** Side Effects: 87325698Seric ** Picks up extant zombies. 87425698Seric */ 87525698Seric 87625698Seric # include <sys/wait.h> 87725698Seric 87846928Sbostic void 87925698Seric reapchild() 88025698Seric { 88125698Seric # ifdef WNOHANG 88225698Seric union wait status; 88325698Seric 88446928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 88525698Seric continue; 88656795Seric # else /* WNOHANG */ 88725698Seric auto int status; 88825698Seric 88946928Sbostic while (wait((int *)&status) > 0) 89025698Seric continue; 89156795Seric # endif /* WNOHANG */ 89258061Seric # ifdef SYSTEM5 89358061Seric (void) signal(SIGCHLD, reapchild); 89458061Seric # endif 89525698Seric } 89655418Seric /* 89755418Seric ** UNSETENV -- remove a variable from the environment 89855418Seric ** 89955418Seric ** Not needed on newer systems. 90055418Seric ** 90155418Seric ** Parameters: 90255418Seric ** name -- the string name of the environment variable to be 90355418Seric ** deleted from the current environment. 90455418Seric ** 90555418Seric ** Returns: 90655418Seric ** none. 90755418Seric ** 90855418Seric ** Globals: 90955418Seric ** environ -- a pointer to the current environment. 91055418Seric ** 91155418Seric ** Side Effects: 91255418Seric ** Modifies environ. 91355418Seric */ 91455418Seric 91555418Seric #ifdef UNSETENV 91655418Seric 91755418Seric void 91855418Seric unsetenv(name) 91955418Seric char *name; 92055418Seric { 92155418Seric extern char **environ; 92255418Seric register char **pp; 92355418Seric int len = strlen(name); 92455418Seric 92555418Seric for (pp = environ; *pp != NULL; pp++) 92655418Seric { 92755418Seric if (strncmp(name, *pp, len) == 0 && 92855418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 92955418Seric break; 93055418Seric } 93155418Seric 93255418Seric for (; *pp != NULL; pp++) 93355418Seric *pp = pp[1]; 93455418Seric } 93555418Seric 93655418Seric #endif /* UNSETENV */ 93756215Seric /* 93856215Seric ** GETDTABLESIZE -- return number of file descriptors 93956215Seric ** 94056215Seric ** Only on non-BSD systems 94156215Seric ** 94256215Seric ** Parameters: 94356215Seric ** none 94456215Seric ** 94556215Seric ** Returns: 94656215Seric ** size of file descriptor table 94756215Seric ** 94856215Seric ** Side Effects: 94956215Seric ** none 95056215Seric */ 95156215Seric 95256215Seric #ifdef SYSTEM5 95356215Seric 95456215Seric int 95556215Seric getdtablesize() 95656215Seric { 95758689Seric # ifdef _SC_OPEN_MAX 95858689Seric return sysconf(_SC_OPEN_MAX); 95958689Seric # else 96056215Seric return NOFILE; 96158689Seric # endif 96256215Seric } 96356215Seric 96456215Seric #endif 96557631Seric /* 96657631Seric ** UNAME -- get the UUCP name of this system. 96757631Seric */ 96857631Seric 96957943Seric #ifndef HASUNAME 97057631Seric 97157631Seric int 97257631Seric uname(name) 97357631Seric struct utsname *name; 97457631Seric { 97557631Seric FILE *file; 97657631Seric char *n; 97757631Seric 97857631Seric name->nodename[0] = '\0'; 97957631Seric 98057661Seric /* try /etc/whoami -- one line with the node name */ 98157631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 98257631Seric { 98357661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 98457631Seric (void) fclose(file); 98557661Seric n = strchr(name->nodename, '\n'); 98657631Seric if (n != NULL) 98757631Seric *n = '\0'; 98857631Seric if (name->nodename[0] != '\0') 98957631Seric return (0); 99057631Seric } 99157631Seric 99257661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 99357631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 99457631Seric { 99557631Seric char buf[MAXLINE]; 99657631Seric 99757631Seric while (fgets(buf, MAXLINE, file) != NULL) 99857631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 99957631Seric NODE_LENGTH, name->nodename) > 0) 100057631Seric break; 100157631Seric (void) fclose(file); 100257631Seric if (name->nodename[0] != '\0') 100357631Seric return (0); 100457631Seric } 100557631Seric 100657631Seric #ifdef TRUST_POPEN 100757631Seric /* 100857631Seric ** Popen is known to have security holes. 100957631Seric */ 101057631Seric 101157661Seric /* try uuname -l to return local name */ 101257631Seric if ((file = popen("uuname -l", "r")) != NULL) 101357631Seric { 101457661Seric (void) fgets(name, NODE_LENGTH + 1, file); 101557631Seric (void) pclose(file); 101657661Seric n = strchr(name, '\n'); 101757631Seric if (n != NULL) 101857631Seric *n = '\0'; 101957661Seric if (name->nodename[0] != '\0') 102057631Seric return (0); 102157631Seric } 102257631Seric #endif 102357631Seric 102457631Seric return (-1); 102557631Seric } 102657943Seric #endif /* HASUNAME */ 102758068Seric /* 102858068Seric ** INITGROUPS -- initialize groups 102958068Seric ** 103058068Seric ** Stub implementation for System V style systems 103158068Seric */ 103258068Seric 103358068Seric #ifndef HASINITGROUPS 103458068Seric # if !defined(SYSTEM5) || defined(hpux) 103558068Seric # define HASINITGROUPS 103658068Seric # endif 103758068Seric #endif 103858068Seric 103958068Seric #ifndef HASINITGROUPS 104058068Seric 104158068Seric initgroups(name, basegid) 104258068Seric char *name; 104358068Seric int basegid; 104458068Seric { 104558068Seric return 0; 104658068Seric } 104758068Seric 104858068Seric #endif 104958082Seric /* 105058082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 105158082Seric ** 105258082Seric ** Only implemented if you have statfs. 105358082Seric ** 105458082Seric ** Parameters: 105558333Seric ** msize -- the size to check against. If zero, we don't yet 105658333Seric ** know how big the message will be, so just check for 105758333Seric ** a "reasonable" amount. 105858082Seric ** 105958082Seric ** Returns: 106058082Seric ** TRUE if there is enough space. 106158082Seric ** FALSE otherwise. 106258082Seric */ 106358082Seric 106458082Seric #ifndef HASSTATFS 106558082Seric # if defined(BSD4_4) || defined(__osf__) 106658082Seric # define HASSTATFS 106758082Seric # endif 106858082Seric #endif 106958082Seric 107058082Seric #ifdef HASSTATFS 107158157Seric # undef HASUSTAT 107258157Seric #endif 107358157Seric 107458157Seric #if defined(HASUSTAT) 107558157Seric # include <sys/stat.h> 107658157Seric # include <ustat.h> 107758157Seric #endif 107858157Seric 107958157Seric #ifdef HASSTATFS 108058133Seric # if defined(sgi) || defined(apollo) 108158133Seric # include <sys/statfs.h> 108258133Seric # else 108358133Seric # if defined(sun) || defined(hpux) 108458133Seric # include <sys/vfs.h> 108558133Seric # else 108658157Seric # include <sys/mount.h> 108758133Seric # endif 108858133Seric # endif 108958082Seric #endif 109058082Seric 109158082Seric bool 109258333Seric enoughspace(msize) 109358333Seric long msize; 109458082Seric { 109558160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 109658157Seric # if defined(HASUSTAT) 109758153Seric struct ustat fs; 109858153Seric struct stat statbuf; 109958366Seric # define FSBLOCKSIZE DEV_BSIZE 110058157Seric # define f_bavail f_tfree 110158157Seric # else 110258157Seric # if defined(ultrix) 110358157Seric struct fs_data fs; 110458157Seric # define f_bavail fd_bfreen 110558366Seric # define FSBLOCKSIZE fs.fd_bsize 110658153Seric # else 110758082Seric struct statfs fs; 110858366Seric # define FSBLOCKSIZE fs.f_bsize 110958153Seric # endif 111058133Seric # endif 111158333Seric long blocksneeded; 111258082Seric extern int errno; 111358082Seric extern char *errstring(); 111458082Seric 111558333Seric if (MinBlocksFree <= 0 && msize <= 0) 111658082Seric { 111758082Seric if (tTd(4, 80)) 111858133Seric printf("enoughspace: no threshold\n"); 111958133Seric return TRUE; 112058133Seric } 112158333Seric 112258157Seric # if defined(HASUSTAT) 112358157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 112458157Seric # else 112558157Seric # if defined(sgi) || defined(apollo) 112658133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 112758157Seric # else 112858157Seric # if defined(ultrix) 112958133Seric if (statfs(QueueDir, &fs) > 0) 113058153Seric # else 113158133Seric if (statfs(QueueDir, &fs) == 0) 113258153Seric # endif 113358133Seric # endif 113458133Seric # endif 113558133Seric { 113658133Seric if (tTd(4, 80)) 113758333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 113858333Seric fs.f_bavail, msize); 113958333Seric 114058333Seric /* convert msize to block count */ 114158366Seric msize = msize / FSBLOCKSIZE + 1; 114258333Seric if (MinBlocksFree >= 0) 114358333Seric msize += MinBlocksFree; 114458333Seric 114558333Seric if (fs.f_bavail < msize) 114658090Seric { 114758090Seric #ifdef LOG 114858090Seric if (LogLevel > 0) 114958090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 115058333Seric QueueDir, fs.f_bavail, msize); 115158090Seric #endif 115258082Seric return FALSE; 115358090Seric } 115458082Seric } 115558082Seric else if (tTd(4, 80)) 115658333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 115758333Seric MinBlocksFree, msize, errstring(errno)); 115858082Seric #endif 115958082Seric return TRUE; 116058082Seric } 116158542Seric /* 116258542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 116358542Seric ** 116458542Seric ** This looks at an errno value and tells if this is likely to 116558542Seric ** go away if retried later. 116658542Seric ** 116758542Seric ** Parameters: 116858542Seric ** err -- the errno code to classify. 116958542Seric ** 117058542Seric ** Returns: 117158542Seric ** TRUE if this is probably transient. 117258542Seric ** FALSE otherwise. 117358542Seric */ 117458542Seric 117558542Seric bool 117658542Seric transienterror(err) 117758542Seric int err; 117858542Seric { 117958542Seric switch (err) 118058542Seric { 118158542Seric case EIO: /* I/O error */ 118258542Seric case ENXIO: /* Device not configured */ 118358542Seric case EAGAIN: /* Resource temporarily unavailable */ 118458542Seric case ENOMEM: /* Cannot allocate memory */ 118558542Seric case ENODEV: /* Operation not supported by device */ 118658542Seric case ENFILE: /* Too many open files in system */ 118758542Seric case EMFILE: /* Too many open files */ 118858542Seric case ENOSPC: /* No space left on device */ 118958542Seric #ifdef ETIMEDOUT 119058542Seric case ETIMEDOUT: /* Connection timed out */ 119158542Seric #endif 119258542Seric #ifdef ESTALE 119358542Seric case ESTALE: /* Stale NFS file handle */ 119458542Seric #endif 119558542Seric #ifdef ENETDOWN 119658542Seric case ENETDOWN: /* Network is down */ 119758542Seric #endif 119858542Seric #ifdef ENETUNREACH 119958542Seric case ENETUNREACH: /* Network is unreachable */ 120058542Seric #endif 120158542Seric #ifdef ENETRESET 120258542Seric case ENETRESET: /* Network dropped connection on reset */ 120358542Seric #endif 120458542Seric #ifdef ECONNABORTED 120558542Seric case ECONNABORTED: /* Software caused connection abort */ 120658542Seric #endif 120758542Seric #ifdef ECONNRESET 120858542Seric case ECONNRESET: /* Connection reset by peer */ 120958542Seric #endif 121058542Seric #ifdef ENOBUFS 121158542Seric case ENOBUFS: /* No buffer space available */ 121258542Seric #endif 121358542Seric #ifdef ESHUTDOWN 121458542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 121558542Seric #endif 121658542Seric #ifdef ECONNREFUSED 121758542Seric case ECONNREFUSED: /* Connection refused */ 121858542Seric #endif 121958542Seric #ifdef EHOSTDOWN 122058542Seric case EHOSTDOWN: /* Host is down */ 122158542Seric #endif 122258542Seric #ifdef EHOSTUNREACH 122358542Seric case EHOSTUNREACH: /* No route to host */ 122458542Seric #endif 122558542Seric #ifdef EDQUOT 122658542Seric case EDQUOT: /* Disc quota exceeded */ 122758542Seric #endif 122858542Seric #ifdef EPROCLIM 122958542Seric case EPROCLIM: /* Too many processes */ 123058542Seric #endif 123158542Seric #ifdef EUSERS 123258542Seric case EUSERS: /* Too many users */ 123358542Seric #endif 123458542Seric #ifdef EDEADLK 123558542Seric case EDEADLK: /* Resource deadlock avoided */ 123658542Seric #endif 123758542Seric #ifdef EISCONN 123858542Seric case EISCONN: /* Socket already connected */ 123958542Seric #endif 124058542Seric #ifdef EINPROGRESS 124158542Seric case EINPROGRESS: /* Operation now in progress */ 124258542Seric #endif 124358542Seric #ifdef EALREADY 124458542Seric case EALREADY: /* Operation already in progress */ 124558542Seric #endif 124658542Seric #ifdef EADDRINUSE 124758542Seric case EADDRINUSE: /* Address already in use */ 124858542Seric #endif 124958542Seric #ifdef EADDRNOTAVAIL 125058542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 125158542Seric #endif 125258542Seric #ifdef ENOSR 125358542Seric case ENOSR: /* Out of streams resources */ 125458542Seric #endif 125558542Seric return TRUE; 125658542Seric } 125758542Seric 125858542Seric /* nope, must be permanent */ 125958542Seric return FALSE; 126058542Seric } 126158689Seric /* 126258689Seric ** LOCKFILE -- lock a file using flock or (shudder) lockf 126358689Seric ** 126458689Seric ** Parameters: 126558689Seric ** fd -- the file descriptor of the file. 126658689Seric ** filename -- the file name (for error messages). 126758689Seric ** type -- type of the lock. Bits can be: 126858689Seric ** LOCK_EX -- exclusive lock. 126958689Seric ** LOCK_NB -- non-blocking. 127058689Seric ** 127158689Seric ** Returns: 127258689Seric ** TRUE if the lock was acquired. 127358689Seric ** FALSE otherwise. 127458689Seric */ 127558689Seric 127658689Seric bool 127758689Seric lockfile(fd, filename, type) 127858689Seric int fd; 127958689Seric char *filename; 128058689Seric int type; 128158689Seric { 128258689Seric # ifdef LOCKF 128358689Seric int action; 128458689Seric struct flock lfd; 128558689Seric 128658701Seric if (bitset(LOCK_EX, type)) 128758689Seric lfd.l_type = F_WRLCK; 128858689Seric else 128958689Seric lfd.l_type = F_RDLCK; 129058689Seric 129158689Seric if (bitset(LOCK_NB, type)) 129258689Seric action = F_SETLK; 129358689Seric else 129458689Seric action = F_SETLKW; 129558689Seric 129658689Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 129758689Seric 129858689Seric if (fcntl(fd, action, &lfd) >= 0) 129958689Seric return TRUE; 130058689Seric 130158689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 130258689Seric syserr("cannot lockf(%s)", filename); 130358689Seric # else 130458689Seric if (flock(fd, type) >= 0) 130558689Seric return TRUE; 130658689Seric 130758689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 130858689Seric syserr("cannot flock(%s)", filename); 130958689Seric # endif 131058689Seric return FALSE; 131158689Seric } 1312