static char sccsid
[] = "@(#)ulockf.c 5.4 (Berkeley) %G%";
/* File mode for lock files */
* this routine will create a lock file (file).
* If one already exists, the create time is checked for
* older than the age time (atime).
* If it is older, an attempt will be made to unlink it
* return codes: SUCCESS | FAIL
static char tempfile
[NAMESIZE
];
sprintf(tempfile
, "%s/LTMP.%d", LOCKDIR
, pid
);
sprintf(file
, "%s/%s", LOCKDIR
, hfile
);
if (onelock(pid
, tempfile
, file
) == -1) {
/* get status to check age of the lock file */
ret
= stat(file
, &stbuf
);
if ((ptime
- stbuf
.st_ctime
) < atime
) {
/* file not old enough to delete */
logent(file
, "DEAD LOCK");
sleep(5); /* rti!trt: avoid a race */
ret
= onelock(pid
, tempfile
, file
);
#define MAXLOCKS 10 /* maximum number of lock files */
char *Lockfile
[MAXLOCKS
];
* stlock(name) put name in list of lock files
for (i
= 0; i
< Nlocks
; i
++) {
ASSERT(i
< MAXLOCKS
, "TOO MANY LOCKS", CNULL
, i
);
p
= calloc((unsigned)(strlen(name
)+1), sizeof (char));
ASSERT(p
!= NULL
, "CAN NOT ALLOCATE FOR", name
, 0);
* remove all lock files in list * or name
for (i
= 0; i
< Nlocks
; i
++) {
if (name
== NULL
|| strcmp(name
, Lockfile
[i
]) == SAME
) {
* isalock(name) returns 0 if the name is a lock.
* unlock(name) unlocks name if it is a lock.
* onelock(pid,tempfile,name) makes lock a name
* on behalf of pid. Tempfile must be in the same
* lock(pid,tempfile,names) either locks all the
if (stat(name
,&xstat
) < 0)
if (xstat
.st_size
!= sizeof(int))
onelock(pid
, tempfile
, name
)
fd
= creat(name
, LCKMODE
, "1version");
fd
= creat(tempfile
, LCKMODE
);
write(fd
, (char *)&pid
, sizeof(int));
if (link(tempfile
, name
) < 0) {
lock(pid
, tempfile
, names
)
for(i
=0; names
[i
] != 0; i
++) {
if (onelock(pid
, tempfile
, names
[i
]) == 0)
sprintf(ln
, "%s/%s.%s", LOCKDIR
, LOCKPRE
, s
);
* return codes: SUCCESS | FAIL
sprintf(lname
, "%s.%s", LOCKPRE
, sys
);
return ulockf(lname
, (time_t) SLCKTIME
) < 0 ? FAIL
: SUCCESS
;
* update 'change' time for lock files
* Only update ctime, not mtime or atime.
* The 'chmod' method permits cu(I)-like programs
* to determine how long uucp has been on the line.
* The old "change access, mod, and change time" method
* can be had by defining OLDTOUCH
static time_t lasttouch
= 0;
ut
.actime
= time(&ut
.modtime
);
/* Do not waste time touching locking files too often */
/* (But, defend against backward time changes) */
if (ut
.actime
>= lasttouch
&& ut
.actime
< lasttouch
+60)
DEBUG(4, "ultouch\n", 0);
for (i
= 0; i
< Nlocks
; i
++) {
chmod(Lockfile
[i
], LCKMODE
);
* set 'nologinflag' if the file /etc/nologin exists.
* This permits graceful shutdown of uucp.
if (nologinflag
== 0 && access(NOLOGIN
, 0) == 0)