xref: /onnv-gate/usr/src/cmd/ttymon/ulockf.c (revision 634:263e46dc023d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*634Sdp /*
25*634Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*634Sdp  * Use is subject to license terms.
27*634Sdp  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
30*634Sdp #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "uucp.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate /* #include <sys/types.h> */
360Sstevel@tonic-gate /* #include <sys/stat.h> */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate static struct stat _st_buf;
390Sstevel@tonic-gate static char lockname[BUFSIZ];
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef	V7
420Sstevel@tonic-gate #define O_RDONLY	0
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static void	stlock();
460Sstevel@tonic-gate static int	onelock();
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * make a lock file with given 'name'
500Sstevel@tonic-gate  * If one already exists, send a signal 0 to the process--if
510Sstevel@tonic-gate  * it fails, then unlink it and make a new one.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * input:
540Sstevel@tonic-gate  *	name - name of the lock file to make
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * return:
570Sstevel@tonic-gate  *	0	-> success
580Sstevel@tonic-gate  *	FAIL	-> failure
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate GLOBAL int
mklock(name)620Sstevel@tonic-gate mklock(name)
630Sstevel@tonic-gate register char *name;
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	static	char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
660Sstevel@tonic-gate 	static char tempfile[MAXNAMESIZE];
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifdef V8
690Sstevel@tonic-gate 	char *cp;
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (pid[0] == '\0') {
730Sstevel@tonic-gate 		(void) sprintf(pid, "%*ld\n", SIZEOFPID, (long) getpid());
740Sstevel@tonic-gate 		(void) sprintf(tempfile, "%s/LTMP.%ld", X_LOCKDIR, (long) getpid());
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #ifdef V8	/* this wouldn't be a problem if we used lock directories */
780Sstevel@tonic-gate 		/* some day the truncation of system names will bite us */
790Sstevel@tonic-gate 	cp = rindex(name, '/');
800Sstevel@tonic-gate 	if (cp++ != CNULL)
810Sstevel@tonic-gate 	    if (strlen(cp) > MAXBASENAME)
820Sstevel@tonic-gate 		*(cp+MAXBASENAME) = NULLCHAR;
830Sstevel@tonic-gate #endif /* V8 */
840Sstevel@tonic-gate 	if (onelock(pid, tempfile, name) == -1) {
850Sstevel@tonic-gate 		(void) unlink(tempfile);
860Sstevel@tonic-gate 		if (cklock(name))
870Sstevel@tonic-gate 			return(FAIL);
880Sstevel@tonic-gate 		else {
890Sstevel@tonic-gate 		    if (onelock(pid, tempfile, name)) {
900Sstevel@tonic-gate 			(void) unlink(tempfile);
910Sstevel@tonic-gate 			DEBUG(4,"ulockf failed in onelock()\n%s", "");
920Sstevel@tonic-gate 			return(FAIL);
930Sstevel@tonic-gate 		    }
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	stlock(name);
980Sstevel@tonic-gate 	return(0);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * check to see if the lock file exists and is still active
1030Sstevel@tonic-gate  * - use kill(pid,0)
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * return:
1060Sstevel@tonic-gate  *	0	-> success (lock file removed - no longer active
1070Sstevel@tonic-gate  *	FAIL	-> lock file still active
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate GLOBAL int
cklock(name)1100Sstevel@tonic-gate cklock(name)
1110Sstevel@tonic-gate register char *name;
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	register int ret;
1140Sstevel@tonic-gate 	pid_t lpid = -1;
1150Sstevel@tonic-gate 	char alpid[SIZEOFPID+2];	/* +2 for '\n' and NULL */
1160Sstevel@tonic-gate 	int fd;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	fd = open(name, O_RDONLY);
1190Sstevel@tonic-gate 	DEBUG(4, "ulockf name %s\n", name);
1200Sstevel@tonic-gate 	if (fd == -1) {
1210Sstevel@tonic-gate 	    if (errno == ENOENT)  /* file does not exist -- OK */
1220Sstevel@tonic-gate 		return(0);
1230Sstevel@tonic-gate 	    DEBUG(4,"Lock File--can't read (errno %d) --remove it!\n", errno);
1240Sstevel@tonic-gate 	    goto unlk;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	ret = read(fd, (char *) alpid, SIZEOFPID+1); /* +1 for '\n' */
1270Sstevel@tonic-gate 	(void) close(fd);
1280Sstevel@tonic-gate 	if (ret != (SIZEOFPID+1)) {
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	    DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
1310Sstevel@tonic-gate 	    goto unlk;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	lpid = (pid_t) strtol(alpid, (char **) NULL, 10);
1340Sstevel@tonic-gate 	if ((ret=kill(lpid, 0)) == 0 || errno == EPERM) {
1350Sstevel@tonic-gate 	    DEBUG(4, "Lock File--process still active--not removed\n%s", "");
1360Sstevel@tonic-gate 	    return(FAIL);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	else { /* process no longer active */
1390Sstevel@tonic-gate 	    DEBUG(4, "kill pid (%ld), ", (long) lpid);
1400Sstevel@tonic-gate 	    DEBUG(4, "returned %d", ret);
1410Sstevel@tonic-gate 	    DEBUG(4, "--ok to remove lock file (%s)\n", name);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate unlk:
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (unlink(name) != 0) {
1460Sstevel@tonic-gate 		DEBUG(4,"ulockf failed in unlink()\n%s", "");
1470Sstevel@tonic-gate 		return(FAIL);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	return(0);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate #define MAXLOCKS 10	/* maximum number of lock files */
1530Sstevel@tonic-gate static char *Lockfile[MAXLOCKS];
1540Sstevel@tonic-gate GLOBAL int Nlocks = 0;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * put name in list of lock files
1580Sstevel@tonic-gate  * return:
1590Sstevel@tonic-gate  *	none
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate static void
stlock(name)1620Sstevel@tonic-gate stlock(name)
1630Sstevel@tonic-gate char *name;
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	register int i;
1660Sstevel@tonic-gate 	char *p;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
1690Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
1700Sstevel@tonic-gate 			break;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
1730Sstevel@tonic-gate 	if (i >= Nlocks)
1740Sstevel@tonic-gate 		i = Nlocks++;
1750Sstevel@tonic-gate 	p = (char*) calloc((unsigned) strlen(name) + 1, sizeof (char));
1760Sstevel@tonic-gate 	ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
1770Sstevel@tonic-gate 	(void) strcpy(p, name);
1780Sstevel@tonic-gate 	Lockfile[i] = p;
1790Sstevel@tonic-gate 	return;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * remove the named lock. If named lock is NULL,
1840Sstevel@tonic-gate  *	then remove all locks currently in list.
1850Sstevel@tonic-gate  * return:
1860Sstevel@tonic-gate  *	none
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate GLOBAL void
rmlock(name)1890Sstevel@tonic-gate rmlock(name)
1900Sstevel@tonic-gate register char *name;
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	register int i;
1930Sstevel@tonic-gate #ifdef V8
1940Sstevel@tonic-gate 	char *cp;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	cp = rindex(name, '/');
1970Sstevel@tonic-gate 	if (cp++ != CNULL)
1980Sstevel@tonic-gate 	    if (strlen(cp) > MAXBASENAME)
1990Sstevel@tonic-gate 		*(cp+MAXBASENAME) = NULLCHAR;
2000Sstevel@tonic-gate #endif /* V8 */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
2040Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
2050Sstevel@tonic-gate 			continue;
2060Sstevel@tonic-gate 		if (name == NULL || EQUALS(name, Lockfile[i])) {
2070Sstevel@tonic-gate 			(void) unlink(Lockfile[i]);
2080Sstevel@tonic-gate 			free(Lockfile[i]);
2090Sstevel@tonic-gate 			Lockfile[i] = NULL;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	return;
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * remove a lock file
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Parameters:
2210Sstevel@tonic-gate  *	pre -	Path and first part of file name of the lock file to be
2220Sstevel@tonic-gate  *		removed.
2230Sstevel@tonic-gate  *	s -	The suffix part of the lock file.  The name of the lock file
2240Sstevel@tonic-gate  *		will be derrived by concatenating pre, a period, and s.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * return:
2270Sstevel@tonic-gate  *	none
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate GLOBAL void
delock(pre,s)2300Sstevel@tonic-gate delock(pre, s)
2310Sstevel@tonic-gate char * pre;
2320Sstevel@tonic-gate char *s;
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	char ln[MAXNAMESIZE];
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	(void) sprintf(ln, "%s.%s", pre, s);
2370Sstevel@tonic-gate 	BASENAME(ln, '/')[MAXBASENAME] = '\0';
2380Sstevel@tonic-gate 	rmlock(ln);
2390Sstevel@tonic-gate 	return;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * create lock file
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  * Parameters:
2470Sstevel@tonic-gate  *	pre -	Path and first part of file name of the lock file to be
2480Sstevel@tonic-gate  *		created.
2490Sstevel@tonic-gate  *	name -	The suffix part of the lock file.  The name of the lock file
2500Sstevel@tonic-gate  *		will be derrived by concatenating pre, a period, and name.
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  * return:
2530Sstevel@tonic-gate  *	0	-> success
2540Sstevel@tonic-gate  *	FAIL	-> failure
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate GLOBAL int
mlock(pre,name)2570Sstevel@tonic-gate mlock(pre, name)
2580Sstevel@tonic-gate char * pre;
2590Sstevel@tonic-gate char *name;
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	char lname[MAXNAMESIZE];
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * if name has a '/' in it, then it's a device name and it's
2650Sstevel@tonic-gate 	 * not in /dev (i.e., it's a remotely-mounted device or it's
2660Sstevel@tonic-gate 	 * in a subdirectory of /dev).  in either case, creating our normal
2670Sstevel@tonic-gate 	 * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if
2680Sstevel@tonic-gate 	 * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never
2690Sstevel@tonic-gate 	 * mind.  since we're using advisory filelocks on the devices
2700Sstevel@tonic-gate 	 * themselves, it'll be safe.
2710Sstevel@tonic-gate 	 *
2720Sstevel@tonic-gate 	 * of course, programs and people who are used to looking at the
2730Sstevel@tonic-gate 	 * lockfiles to find out what's going on are going to be a trifle
2740Sstevel@tonic-gate 	 * misled.  we really need to re-consider the lockfile naming structure
2750Sstevel@tonic-gate 	 * to accomodate devices in directories other than /dev ... maybe in
2760Sstevel@tonic-gate 	 * the next release.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	if ( strchr(name, '/') != NULL )
2790Sstevel@tonic-gate 		return(0);
2800Sstevel@tonic-gate 	(void) sprintf(lname, "%s.%s", pre, BASENAME(name, '/'));
2810Sstevel@tonic-gate 	BASENAME(lname, '/')[MAXBASENAME] = '\0';
2820Sstevel@tonic-gate 	return(mklock(lname));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * makes a lock on behalf of pid.
2870Sstevel@tonic-gate  * input:
2880Sstevel@tonic-gate  *	pid - process id
2890Sstevel@tonic-gate  *	tempfile - name of a temporary in the same file system
2900Sstevel@tonic-gate  *	name - lock file name (full path name)
2910Sstevel@tonic-gate  * return:
2920Sstevel@tonic-gate  *	-1 - failed
2930Sstevel@tonic-gate  *	0  - lock made successfully
2940Sstevel@tonic-gate  */
2950Sstevel@tonic-gate static int
onelock(pid,tempfile,name)2960Sstevel@tonic-gate onelock(pid,tempfile,name)
2970Sstevel@tonic-gate char *pid;
2980Sstevel@tonic-gate char *tempfile, *name;
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	register int fd;
3010Sstevel@tonic-gate 	char	cb[100];
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	fd=creat(tempfile, (mode_t) 0444);
3040Sstevel@tonic-gate 	if(fd < 0){
3050Sstevel@tonic-gate 		(void) sprintf(cb, "%s %s %d",tempfile, name, errno);
3060Sstevel@tonic-gate 		logent("ULOCKC", cb);
3070Sstevel@tonic-gate 		if((errno == EMFILE) || (errno == ENFILE))
3080Sstevel@tonic-gate 			(void) unlink(tempfile);
3090Sstevel@tonic-gate 		return(-1);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	/* +1 for '\n' */
3120Sstevel@tonic-gate 	if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
3130Sstevel@tonic-gate 		(void) sprintf(cb, "%s %s %d",tempfile, name, errno);
3140Sstevel@tonic-gate 		logent("ULOCKW", cb);
3150Sstevel@tonic-gate 		(void) unlink(tempfile);
3160Sstevel@tonic-gate 		return (-1);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	(void) chmod(tempfile, (mode_t) 0444);
3190Sstevel@tonic-gate 	(void) chown(tempfile, UUCPUID, UUCPGID);
3200Sstevel@tonic-gate 	(void) close(fd);
3210Sstevel@tonic-gate 	if(link(tempfile,name)<0){
322*634Sdp 		DEBUG(4, "%s: ", strerror(errno));
3230Sstevel@tonic-gate 		DEBUG(4, "link(%s, ", tempfile);
3240Sstevel@tonic-gate 		DEBUG(4, "%s)\n", name);
3250Sstevel@tonic-gate 		if(unlink(tempfile)< 0){
3260Sstevel@tonic-gate 			(void) sprintf(cb, "ULK err %s %d", tempfile,  errno);
3270Sstevel@tonic-gate 			logent("ULOCKLNK", cb);
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		return(-1);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	if(unlink(tempfile)<0){
3320Sstevel@tonic-gate 		(void) sprintf(cb, "%s %d",tempfile,errno);
3330Sstevel@tonic-gate 		logent("ULOCKF", cb);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	return(0);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * fd_mklock(fd) - lock the device indicated by fd is possible
3400Sstevel@tonic-gate  *
3410Sstevel@tonic-gate  * return -
3420Sstevel@tonic-gate  *	SUCCESS - this process now has the fd locked
3430Sstevel@tonic-gate  *	FAIL - this process was not able to lock the fd
3440Sstevel@tonic-gate  */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate GLOBAL int
fd_mklock(fd)3470Sstevel@tonic-gate fd_mklock(fd)
3480Sstevel@tonic-gate int fd;
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate     int tries = 0;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate     if ( fstat(fd, &_st_buf) != 0 )
3530Sstevel@tonic-gate 	return(FAIL);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate     (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
3560Sstevel@tonic-gate         (unsigned long) major(_st_buf.st_dev),
3570Sstevel@tonic-gate 	(unsigned long) major(_st_buf.st_rdev),
3580Sstevel@tonic-gate 	(unsigned long) minor(_st_buf.st_rdev));
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate     if ( mklock(lockname) == FAIL )
3610Sstevel@tonic-gate 	return(FAIL);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate     while ( lockf(fd, F_TLOCK, 0L) != 0 ) {
3640Sstevel@tonic-gate 	DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
3650Sstevel@tonic-gate 	if ( (++tries >= MAX_LOCKTRY) || (errno != EAGAIN) ) {
3660Sstevel@tonic-gate 	    rmlock(lockname);
3670Sstevel@tonic-gate 	    logent("fd_mklock","lockf failed");
3680Sstevel@tonic-gate 	    return(FAIL);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	(void)sleep(2);
3710Sstevel@tonic-gate     }
3720Sstevel@tonic-gate     DEBUG(7, "fd_mklock: ok\n%s", "");
3730Sstevel@tonic-gate     return(SUCCESS);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * fn_cklock(name) - determine if the device indicated by name is locked
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * return -
3800Sstevel@tonic-gate  *	SUCCESS - the name is not locked
3810Sstevel@tonic-gate  *	FAIL - the name is locked by another process
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate GLOBAL int
fn_cklock(name)3850Sstevel@tonic-gate fn_cklock(name)
3860Sstevel@tonic-gate char *name;
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate     /* we temporarily use lockname to hold full path name */
3890Sstevel@tonic-gate     (void) sprintf(lockname, "%s%s", (*name == '/' ? "" : "/dev/"), name);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate     if ( stat(lockname, &_st_buf) != 0 )
3920Sstevel@tonic-gate 	return(FAIL);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate     (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
3950Sstevel@tonic-gate         (unsigned long) major(_st_buf.st_dev),
3960Sstevel@tonic-gate 	(unsigned long) major(_st_buf.st_rdev),
3970Sstevel@tonic-gate 	(unsigned long) minor(_st_buf.st_rdev));
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate     return(cklock(lockname));
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate  * fd_cklock(fd) - determine if the device indicated by fd is locked
4040Sstevel@tonic-gate  *
4050Sstevel@tonic-gate  * return -
4060Sstevel@tonic-gate  *	SUCCESS - the fd is not locked
4070Sstevel@tonic-gate  *	FAIL - the fd is locked by another process
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate GLOBAL int
fd_cklock(fd)4110Sstevel@tonic-gate fd_cklock(fd)
4120Sstevel@tonic-gate int fd;
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate     if ( fstat(fd, &_st_buf) != 0 )
4150Sstevel@tonic-gate 	return(FAIL);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate     (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
4180Sstevel@tonic-gate         (unsigned long) major(_st_buf.st_dev),
4190Sstevel@tonic-gate 	(unsigned long) major(_st_buf.st_rdev),
4200Sstevel@tonic-gate 	(unsigned long) minor(_st_buf.st_rdev));
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate     if ( cklock(lockname) == FAIL )
4230Sstevel@tonic-gate 	return(FAIL);
4240Sstevel@tonic-gate     else
4250Sstevel@tonic-gate 	return( lockf(fd, F_TEST, 0L) );
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate  * remove the locks associated with the device file descriptor
4300Sstevel@tonic-gate  *
4310Sstevel@tonic-gate  * return -
4320Sstevel@tonic-gate  *	SUCCESS - both BNU lock file and advisory locks removed
4330Sstevel@tonic-gate  *	FAIL -
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate GLOBAL void
fd_rmlock(fd)4370Sstevel@tonic-gate fd_rmlock(fd)
4380Sstevel@tonic-gate int fd;
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate     if ( fstat(fd, &_st_buf) == 0 ) {
4410Sstevel@tonic-gate         (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
4420Sstevel@tonic-gate             (unsigned long) major(_st_buf.st_dev),
4430Sstevel@tonic-gate 	    (unsigned long) major(_st_buf.st_rdev),
4440Sstevel@tonic-gate 	    (unsigned long) minor(_st_buf.st_rdev));
4450Sstevel@tonic-gate         rmlock(lockname);
4460Sstevel@tonic-gate     }
4470Sstevel@tonic-gate     (void) lockf(fd, F_ULOCK, 0L);
4480Sstevel@tonic-gate     return;
4490Sstevel@tonic-gate }
450