static char sccsid
[] = "@(#)ulockf.c 5.5 (Berkeley) 10/9/85";
#define LCKMODE 0444 /* File mode for lock files */
#define MAXLOCKS 16 /* Maximum number of lock files */
char *Lockfile
[MAXLOCKS
];
char *LockDirectory
= LOCKDIR
;
* This routine will attempt to create a lock file (file).
* It makes sure that the lock file is valid if it already exists.
* return codes: SUCCESS | FAIL
static char tempfile
[NAMESIZE
];
sprintf(tempfile
, "%s/LTMP.%d", LockDirectory
, pid
);
sprintf(file
, "%s/LCK..%s", LockDirectory
, hfile
);
while (onelock(pid
, tempfile
, file
) == -1) { /* lock file exists */
#if !defined(BSD4_2) && !defined(USG)
/* get status to check age of the lock file */
if (stat(file
, &stbuf
) == 0) {
if ((ptime
- stbuf
.st_ctime
) < atime
)
return FAIL
; /* file not old enough to delete */
ret
= read(fd
, &upid
, sizeof upid
);
if (ret
== sizeof upid
&& (kill(upid
, 0) == 0
return FAIL
; /* process is still running */
assert("DEAD LOCK", file
, errno
);
logent(file
, "DEAD LOCK");
sleep(5); /* avoid a possible race */
ASSERT(i
++ < 5, "CAN'T GET LOCKFILE", tempfile
, errno
);
for (i
= 0; i
< Nlocks
; i
++) {
ASSERT(i
< MAXLOCKS
, "TOO MANY LOCKS", CNULL
, i
);
p
= malloc((unsigned)(strlen(file
)+1));
ASSERT(p
!= NULL
, "CAN NOT ALLOCATE FOR", file
, 0);
* remove all lock files in list or name
sprintf(file
, "%s/LCK..%s", LockDirectory
, name
);
for (i
= 0; i
< Nlocks
; i
++) {
if (name
== NULL
|| strcmp(name
, Lockfile
[i
]) == SAME
) {
* makes lock a name on behalf of pid. Tempfile must be in the same
onelock(pid
, tempfile
, name
)
fd
= creat(name
, LCKMODE
, "1version");
fd
= creat(tempfile
, LCKMODE
);
DEBUG(1,"Can't creat temp file %s ", tempfile
);
DEBUG(1,"-- errno %d", errno
);
ret
= write(fd
, (char *)&pid
, sizeof(int));
if (ret
!= sizeof(int)) {
DEBUG(1,"Temp file write failed -- errno %d\n", errno
);
if (link(tempfile
, name
) < 0) {
#if !defined(BSD4_2) && !defined(USG)
* 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
= ut
.modtime
= Now
.time
;
/* 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
);