xref: /illumos-gate/usr/src/cmd/ttymon/ulockf.c (revision 3bb2c156)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*3bb2c156SToomas Soome /*	  All Rights Reserved	*/
24ace1a5f1Sdp /*
25ace1a5f1Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26ace1a5f1Sdp  * Use is subject to license terms.
27ace1a5f1Sdp  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "uucp.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate /* #include <sys/types.h> */
337c478bd9Sstevel@tonic-gate /* #include <sys/stat.h> */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static struct stat _st_buf;
367c478bd9Sstevel@tonic-gate static char lockname[BUFSIZ];
377c478bd9Sstevel@tonic-gate 
38*3bb2c156SToomas Soome static void	stlock(char *);
39*3bb2c156SToomas Soome static int	onelock(char *, char *, char *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * make a lock file with given 'name'
437c478bd9Sstevel@tonic-gate  * If one already exists, send a signal 0 to the process--if
447c478bd9Sstevel@tonic-gate  * it fails, then unlink it and make a new one.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * input:
477c478bd9Sstevel@tonic-gate  *	name - name of the lock file to make
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * return:
507c478bd9Sstevel@tonic-gate  *	0	-> success
517c478bd9Sstevel@tonic-gate  *	FAIL	-> failure
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate GLOBAL int
mklock(char * name)55*3bb2c156SToomas Soome mklock(char *name)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	static	char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
587c478bd9Sstevel@tonic-gate 	static char tempfile[MAXNAMESIZE];
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #ifdef V8
617c478bd9Sstevel@tonic-gate 	char *cp;
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (pid[0] == '\0') {
65*3bb2c156SToomas Soome 		(void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
66*3bb2c156SToomas Soome 		(void) sprintf(tempfile, "%s/LTMP.%ld", X_LOCKDIR,
67*3bb2c156SToomas Soome 		    (long)getpid());
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifdef V8	/* this wouldn't be a problem if we used lock directories */
717c478bd9Sstevel@tonic-gate 		/* some day the truncation of system names will bite us */
727c478bd9Sstevel@tonic-gate 	cp = rindex(name, '/');
737c478bd9Sstevel@tonic-gate 	if (cp++ != CNULL)
74*3bb2c156SToomas Soome 		if (strlen(cp) > MAXBASENAME)
75*3bb2c156SToomas Soome 			*(cp+MAXBASENAME) = NULLCHAR;
767c478bd9Sstevel@tonic-gate #endif /* V8 */
777c478bd9Sstevel@tonic-gate 	if (onelock(pid, tempfile, name) == -1) {
787c478bd9Sstevel@tonic-gate 		(void) unlink(tempfile);
79*3bb2c156SToomas Soome 		if (cklock(name)) {
80*3bb2c156SToomas Soome 			return (FAIL);
81*3bb2c156SToomas Soome 		} else {
82*3bb2c156SToomas Soome 			if (onelock(pid, tempfile, name)) {
83*3bb2c156SToomas Soome 				(void) unlink(tempfile);
84*3bb2c156SToomas Soome 				DEBUG(4, "ulockf failed in onelock()\n%s", "");
85*3bb2c156SToomas Soome 				return (FAIL);
86*3bb2c156SToomas Soome 			}
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	stlock(name);
91*3bb2c156SToomas Soome 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * check to see if the lock file exists and is still active
967c478bd9Sstevel@tonic-gate  * - use kill(pid,0)
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * return:
997c478bd9Sstevel@tonic-gate  *	0	-> success (lock file removed - no longer active
1007c478bd9Sstevel@tonic-gate  *	FAIL	-> lock file still active
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate GLOBAL int
cklock(char * name)103*3bb2c156SToomas Soome cklock(char *name)
1047c478bd9Sstevel@tonic-gate {
105*3bb2c156SToomas Soome 	int ret;
1067c478bd9Sstevel@tonic-gate 	pid_t lpid = -1;
1077c478bd9Sstevel@tonic-gate 	char alpid[SIZEOFPID+2];	/* +2 for '\n' and NULL */
1087c478bd9Sstevel@tonic-gate 	int fd;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	fd = open(name, O_RDONLY);
1117c478bd9Sstevel@tonic-gate 	DEBUG(4, "ulockf name %s\n", name);
1127c478bd9Sstevel@tonic-gate 	if (fd == -1) {
113*3bb2c156SToomas Soome 		if (errno == ENOENT)  /* file does not exist -- OK */
114*3bb2c156SToomas Soome 			return (0);
115*3bb2c156SToomas Soome 		DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
116*3bb2c156SToomas Soome 		    errno);
117*3bb2c156SToomas Soome 		goto unlk;
1187c478bd9Sstevel@tonic-gate 	}
119*3bb2c156SToomas Soome 	ret = read(fd, (char *)alpid, SIZEOFPID + 1); /* +1 for '\n' */
1207c478bd9Sstevel@tonic-gate 	(void) close(fd);
1217c478bd9Sstevel@tonic-gate 	if (ret != (SIZEOFPID+1)) {
1227c478bd9Sstevel@tonic-gate 
123*3bb2c156SToomas Soome 		DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
124*3bb2c156SToomas Soome 		goto unlk;
1257c478bd9Sstevel@tonic-gate 	}
126*3bb2c156SToomas Soome 	lpid = (pid_t)strtol(alpid, NULL, 10);
127*3bb2c156SToomas Soome 	if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
128*3bb2c156SToomas Soome 		DEBUG(4, "Lock File--process still active--not removed\n%s",
129*3bb2c156SToomas Soome 		    "");
130*3bb2c156SToomas Soome 		return (FAIL);
131*3bb2c156SToomas Soome 	} else { /* process no longer active */
132*3bb2c156SToomas Soome 		DEBUG(4, "kill pid (%ld), ", (long)lpid);
133*3bb2c156SToomas Soome 		DEBUG(4, "returned %d", ret);
134*3bb2c156SToomas Soome 		DEBUG(4, "--ok to remove lock file (%s)\n", name);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate unlk:
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (unlink(name) != 0) {
139*3bb2c156SToomas Soome 		DEBUG(4, "ulockf failed in unlink()\n%s", "");
140*3bb2c156SToomas Soome 		return (FAIL);
1417c478bd9Sstevel@tonic-gate 	}
142*3bb2c156SToomas Soome 	return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
145*3bb2c156SToomas Soome #define	MAXLOCKS 10	/* maximum number of lock files */
1467c478bd9Sstevel@tonic-gate static char *Lockfile[MAXLOCKS];
1477c478bd9Sstevel@tonic-gate GLOBAL int Nlocks = 0;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * put name in list of lock files
1517c478bd9Sstevel@tonic-gate  * return:
1527c478bd9Sstevel@tonic-gate  *	none
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate static void
stlock(char * name)155*3bb2c156SToomas Soome stlock(char *name)
1567c478bd9Sstevel@tonic-gate {
157*3bb2c156SToomas Soome 	int i;
1587c478bd9Sstevel@tonic-gate 	char *p;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
1617c478bd9Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
1657c478bd9Sstevel@tonic-gate 	if (i >= Nlocks)
1667c478bd9Sstevel@tonic-gate 		i = Nlocks++;
167*3bb2c156SToomas Soome 	p = calloc((unsigned)strlen(name) + 1, sizeof (char));
1687c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
1697c478bd9Sstevel@tonic-gate 	(void) strcpy(p, name);
1707c478bd9Sstevel@tonic-gate 	Lockfile[i] = p;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * remove the named lock. If named lock is NULL,
1757c478bd9Sstevel@tonic-gate  *	then remove all locks currently in list.
1767c478bd9Sstevel@tonic-gate  * return:
1777c478bd9Sstevel@tonic-gate  *	none
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate GLOBAL void
rmlock(char * name)180*3bb2c156SToomas Soome rmlock(char *name)
1817c478bd9Sstevel@tonic-gate {
182*3bb2c156SToomas Soome 	int i;
1837c478bd9Sstevel@tonic-gate #ifdef V8
1847c478bd9Sstevel@tonic-gate 	char *cp;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	cp = rindex(name, '/');
1877c478bd9Sstevel@tonic-gate 	if (cp++ != CNULL)
188*3bb2c156SToomas Soome 		if (strlen(cp) > MAXBASENAME)
189*3bb2c156SToomas Soome 			*(cp+MAXBASENAME) = NULLCHAR;
1907c478bd9Sstevel@tonic-gate #endif /* V8 */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
1947c478bd9Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
1957c478bd9Sstevel@tonic-gate 			continue;
1967c478bd9Sstevel@tonic-gate 		if (name == NULL || EQUALS(name, Lockfile[i])) {
1977c478bd9Sstevel@tonic-gate 			(void) unlink(Lockfile[i]);
1987c478bd9Sstevel@tonic-gate 			free(Lockfile[i]);
1997c478bd9Sstevel@tonic-gate 			Lockfile[i] = NULL;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * remove a lock file
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  * Parameters:
2107c478bd9Sstevel@tonic-gate  *	pre -	Path and first part of file name of the lock file to be
2117c478bd9Sstevel@tonic-gate  *		removed.
2127c478bd9Sstevel@tonic-gate  *	s -	The suffix part of the lock file.  The name of the lock file
2137c478bd9Sstevel@tonic-gate  *		will be derrived by concatenating pre, a period, and s.
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  * return:
2167c478bd9Sstevel@tonic-gate  *	none
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate GLOBAL void
delock(char * pre,char * s)219*3bb2c156SToomas Soome delock(char *pre, char *s)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	char ln[MAXNAMESIZE];
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	(void) sprintf(ln, "%s.%s", pre, s);
2247c478bd9Sstevel@tonic-gate 	BASENAME(ln, '/')[MAXBASENAME] = '\0';
2257c478bd9Sstevel@tonic-gate 	rmlock(ln);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * create lock file
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * Parameters:
2337c478bd9Sstevel@tonic-gate  *	pre -	Path and first part of file name of the lock file to be
2347c478bd9Sstevel@tonic-gate  *		created.
2357c478bd9Sstevel@tonic-gate  *	name -	The suffix part of the lock file.  The name of the lock file
2367c478bd9Sstevel@tonic-gate  *		will be derrived by concatenating pre, a period, and name.
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  * return:
2397c478bd9Sstevel@tonic-gate  *	0	-> success
2407c478bd9Sstevel@tonic-gate  *	FAIL	-> failure
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate GLOBAL int
mlock(char * pre,char * name)243*3bb2c156SToomas Soome mlock(char *pre, char *name)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	char lname[MAXNAMESIZE];
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * if name has a '/' in it, then it's a device name and it's
2497c478bd9Sstevel@tonic-gate 	 * not in /dev (i.e., it's a remotely-mounted device or it's
250*3bb2c156SToomas Soome 	 * in a subdirectory of /dev).  in either case, creating our normal
2517c478bd9Sstevel@tonic-gate 	 * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if
252*3bb2c156SToomas Soome 	 * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never
253*3bb2c156SToomas Soome 	 * mind.  since we're using advisory filelocks on the devices
2547c478bd9Sstevel@tonic-gate 	 * themselves, it'll be safe.
2557c478bd9Sstevel@tonic-gate 	 *
2567c478bd9Sstevel@tonic-gate 	 * of course, programs and people who are used to looking at the
2577c478bd9Sstevel@tonic-gate 	 * lockfiles to find out what's going on are going to be a trifle
2587c478bd9Sstevel@tonic-gate 	 * misled.  we really need to re-consider the lockfile naming structure
2597c478bd9Sstevel@tonic-gate 	 * to accomodate devices in directories other than /dev ... maybe in
2607c478bd9Sstevel@tonic-gate 	 * the next release.
2617c478bd9Sstevel@tonic-gate 	 */
262*3bb2c156SToomas Soome 	if (strchr(name, '/') != NULL)
263*3bb2c156SToomas Soome 		return (0);
2647c478bd9Sstevel@tonic-gate 	(void) sprintf(lname, "%s.%s", pre, BASENAME(name, '/'));
2657c478bd9Sstevel@tonic-gate 	BASENAME(lname, '/')[MAXBASENAME] = '\0';
266*3bb2c156SToomas Soome 	return (mklock(lname));
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * makes a lock on behalf of pid.
2717c478bd9Sstevel@tonic-gate  * input:
2727c478bd9Sstevel@tonic-gate  *	pid - process id
2737c478bd9Sstevel@tonic-gate  *	tempfile - name of a temporary in the same file system
2747c478bd9Sstevel@tonic-gate  *	name - lock file name (full path name)
2757c478bd9Sstevel@tonic-gate  * return:
2767c478bd9Sstevel@tonic-gate  *	-1 - failed
2777c478bd9Sstevel@tonic-gate  *	0  - lock made successfully
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate static int
onelock(char * pid,char * tempfile,char * name)280*3bb2c156SToomas Soome onelock(char *pid, char *tempfile, char *name)
281*3bb2c156SToomas Soome {
282*3bb2c156SToomas Soome 	int fd;
2837c478bd9Sstevel@tonic-gate 	char	cb[100];
2847c478bd9Sstevel@tonic-gate 
285*3bb2c156SToomas Soome 	fd = creat(tempfile, (mode_t)0444);
286*3bb2c156SToomas Soome 	if (fd < 0) {
287*3bb2c156SToomas Soome 		(void) sprintf(cb, "%s %s %d", tempfile, name, errno);
2887c478bd9Sstevel@tonic-gate 		logent("ULOCKC", cb);
289*3bb2c156SToomas Soome 		if ((errno == EMFILE) || (errno == ENFILE))
2907c478bd9Sstevel@tonic-gate 			(void) unlink(tempfile);
291*3bb2c156SToomas Soome 		return (-1);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	/* +1 for '\n' */
294*3bb2c156SToomas Soome 	if (write(fd, pid, SIZEOFPID + 1) != (SIZEOFPID + 1)) {
295*3bb2c156SToomas Soome 		(void) sprintf(cb, "%s %s %d", tempfile, name, errno);
2967c478bd9Sstevel@tonic-gate 		logent("ULOCKW", cb);
2977c478bd9Sstevel@tonic-gate 		(void) unlink(tempfile);
2987c478bd9Sstevel@tonic-gate 		return (-1);
2997c478bd9Sstevel@tonic-gate 	}
300*3bb2c156SToomas Soome 	(void) chmod(tempfile, (mode_t)0444);
3017c478bd9Sstevel@tonic-gate 	(void) chown(tempfile, UUCPUID, UUCPGID);
3027c478bd9Sstevel@tonic-gate 	(void) close(fd);
303*3bb2c156SToomas Soome 	if (link(tempfile, name) < 0) {
304ace1a5f1Sdp 		DEBUG(4, "%s: ", strerror(errno));
3057c478bd9Sstevel@tonic-gate 		DEBUG(4, "link(%s, ", tempfile);
3067c478bd9Sstevel@tonic-gate 		DEBUG(4, "%s)\n", name);
307*3bb2c156SToomas Soome 		if (unlink(tempfile) < 0) {
3087c478bd9Sstevel@tonic-gate 			(void) sprintf(cb, "ULK err %s %d", tempfile,  errno);
3097c478bd9Sstevel@tonic-gate 			logent("ULOCKLNK", cb);
3107c478bd9Sstevel@tonic-gate 		}
311*3bb2c156SToomas Soome 		return (-1);
3127c478bd9Sstevel@tonic-gate 	}
313*3bb2c156SToomas Soome 	if (unlink(tempfile) < 0) {
314*3bb2c156SToomas Soome 		(void) sprintf(cb, "%s %d", tempfile, errno);
3157c478bd9Sstevel@tonic-gate 		logent("ULOCKF", cb);
3167c478bd9Sstevel@tonic-gate 	}
317*3bb2c156SToomas Soome 	return (0);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * fd_mklock(fd) - lock the device indicated by fd is possible
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  * return -
3247c478bd9Sstevel@tonic-gate  *	SUCCESS - this process now has the fd locked
3257c478bd9Sstevel@tonic-gate  *	FAIL - this process was not able to lock the fd
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate GLOBAL int
fd_mklock(int fd)329*3bb2c156SToomas Soome fd_mklock(int fd)
3307c478bd9Sstevel@tonic-gate {
331*3bb2c156SToomas Soome 	int tries = 0;
3327c478bd9Sstevel@tonic-gate 
333*3bb2c156SToomas Soome 	if (fstat(fd, &_st_buf) != 0)
334*3bb2c156SToomas Soome 		return (FAIL);
3357c478bd9Sstevel@tonic-gate 
336*3bb2c156SToomas Soome 	(void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
337*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_dev),
338*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_rdev),
339*3bb2c156SToomas Soome 	    (unsigned long) minor(_st_buf.st_rdev));
3407c478bd9Sstevel@tonic-gate 
341*3bb2c156SToomas Soome 	if (mklock(lockname) == FAIL)
342*3bb2c156SToomas Soome 		return (FAIL);
3437c478bd9Sstevel@tonic-gate 
344*3bb2c156SToomas Soome 	while (lockf(fd, F_TLOCK, 0L) != 0) {
345*3bb2c156SToomas Soome 		DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
346*3bb2c156SToomas Soome 		if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
347*3bb2c156SToomas Soome 			rmlock(lockname);
348*3bb2c156SToomas Soome 			logent("fd_mklock", "lockf failed");
349*3bb2c156SToomas Soome 			return (FAIL);
350*3bb2c156SToomas Soome 		}
351*3bb2c156SToomas Soome 		(void) sleep(2);
3527c478bd9Sstevel@tonic-gate 	}
353*3bb2c156SToomas Soome 	DEBUG(7, "fd_mklock: ok\n%s", "");
354*3bb2c156SToomas Soome 	return (SUCCESS);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * fn_cklock(name) - determine if the device indicated by name is locked
3597c478bd9Sstevel@tonic-gate  *
3607c478bd9Sstevel@tonic-gate  * return -
3617c478bd9Sstevel@tonic-gate  *	SUCCESS - the name is not locked
3627c478bd9Sstevel@tonic-gate  *	FAIL - the name is locked by another process
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate GLOBAL int
fn_cklock(char * name)366*3bb2c156SToomas Soome fn_cklock(char *name)
3677c478bd9Sstevel@tonic-gate {
368*3bb2c156SToomas Soome 	/* we temporarily use lockname to hold full path name */
369*3bb2c156SToomas Soome 	(void) sprintf(lockname, "%s%s", (*name == '/' ? "" : "/dev/"), name);
3707c478bd9Sstevel@tonic-gate 
371*3bb2c156SToomas Soome 	if (stat(lockname, &_st_buf) != 0)
372*3bb2c156SToomas Soome 		return (FAIL);
3737c478bd9Sstevel@tonic-gate 
374*3bb2c156SToomas Soome 	(void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
375*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_dev),
376*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_rdev),
377*3bb2c156SToomas Soome 	    (unsigned long) minor(_st_buf.st_rdev));
3787c478bd9Sstevel@tonic-gate 
379*3bb2c156SToomas Soome 	return (cklock(lockname));
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * fd_cklock(fd) - determine if the device indicated by fd is locked
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  * return -
3867c478bd9Sstevel@tonic-gate  *	SUCCESS - the fd is not locked
3877c478bd9Sstevel@tonic-gate  *	FAIL - the fd is locked by another process
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate GLOBAL int
fd_cklock(int fd)391*3bb2c156SToomas Soome fd_cklock(int fd)
3927c478bd9Sstevel@tonic-gate {
393*3bb2c156SToomas Soome 	if (fstat(fd, &_st_buf) != 0)
394*3bb2c156SToomas Soome 		return (FAIL);
395*3bb2c156SToomas Soome 
396*3bb2c156SToomas Soome 	(void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
397*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_dev),
398*3bb2c156SToomas Soome 	    (unsigned long) major(_st_buf.st_rdev),
399*3bb2c156SToomas Soome 	    (unsigned long) minor(_st_buf.st_rdev));
400*3bb2c156SToomas Soome 
401*3bb2c156SToomas Soome 	if (cklock(lockname) == FAIL)
402*3bb2c156SToomas Soome 		return (FAIL);
403*3bb2c156SToomas Soome 	else
404*3bb2c156SToomas Soome 		return (lockf(fd, F_TEST, 0L));
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * remove the locks associated with the device file descriptor
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  * return -
4117c478bd9Sstevel@tonic-gate  *	SUCCESS - both BNU lock file and advisory locks removed
412*3bb2c156SToomas Soome  *	FAIL -
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate GLOBAL void
fd_rmlock(int fd)416*3bb2c156SToomas Soome fd_rmlock(int fd)
4177c478bd9Sstevel@tonic-gate {
418*3bb2c156SToomas Soome 	if (fstat(fd, &_st_buf) == 0) {
419*3bb2c156SToomas Soome 		(void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
420*3bb2c156SToomas Soome 		    (unsigned long) major(_st_buf.st_dev),
421*3bb2c156SToomas Soome 		    (unsigned long) major(_st_buf.st_rdev),
422*3bb2c156SToomas Soome 		    (unsigned long) minor(_st_buf.st_rdev));
423*3bb2c156SToomas Soome 		rmlock(lockname);
424*3bb2c156SToomas Soome 	}
425*3bb2c156SToomas Soome 	(void) lockf(fd, F_ULOCK, 0L);
4267c478bd9Sstevel@tonic-gate }
427