xref: /illumos-gate/usr/src/lib/libnsl/dial/ulockf.c (revision 1da57d55)
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  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2561961e0fSrobinson  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27*e8031f0aSraf 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*e8031f0aSraf #include "mt.h"
327c478bd9Sstevel@tonic-gate #include "uucp.h"
337c478bd9Sstevel@tonic-gate 
34*e8031f0aSraf static void	stlock(char *);
35*e8031f0aSraf static int	onelock(char *, char *, char *);
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * make a lock file with given 'name'
397c478bd9Sstevel@tonic-gate  * If one already exists, send a signal 0 to the process--if
407c478bd9Sstevel@tonic-gate  * it fails, then unlink it and make a new one.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * input:
437c478bd9Sstevel@tonic-gate  *	name - name of the lock file to make
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * return:
467c478bd9Sstevel@tonic-gate  *	0	-> success
477c478bd9Sstevel@tonic-gate  *	FAIL	-> failure
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
5061961e0fSrobinson static int
mklock(char * name)5161961e0fSrobinson mklock(char *name)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	static	char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
547c478bd9Sstevel@tonic-gate 	static char *tempfile;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (pid[0] == '\0') {
5761961e0fSrobinson 		tempfile = malloc(MAXNAMESIZE);
587c478bd9Sstevel@tonic-gate 		if (tempfile == NULL)
597c478bd9Sstevel@tonic-gate 			return (FAIL);
6061961e0fSrobinson 		(void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
6161961e0fSrobinson 		(void) snprintf(tempfile, MAXNAMESIZE, "%s/LTMP.%ld", X_LOCKDIR,
6261961e0fSrobinson 							(long)getpid());
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (onelock(pid, tempfile, name) == -1) {
667c478bd9Sstevel@tonic-gate 		(void) unlink(tempfile);
6761961e0fSrobinson 		if (cklock(name))
687c478bd9Sstevel@tonic-gate 			return (FAIL);
697c478bd9Sstevel@tonic-gate 		else {
707c478bd9Sstevel@tonic-gate 			if (onelock(pid, tempfile, name)) {
717c478bd9Sstevel@tonic-gate 			(void) unlink(tempfile);
7261961e0fSrobinson 			DEBUG(4, "ulockf failed in onelock()\n%s", "");
737c478bd9Sstevel@tonic-gate 			return (FAIL);
747c478bd9Sstevel@tonic-gate 			}
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	stlock(name);
787c478bd9Sstevel@tonic-gate 	return (0);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * check to see if the lock file exists and is still active
8361961e0fSrobinson  * - use kill(pid, 0)
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * return:
867c478bd9Sstevel@tonic-gate  *	0	-> success (lock file removed - no longer active
877c478bd9Sstevel@tonic-gate  *	FAIL	-> lock file still active
887c478bd9Sstevel@tonic-gate  */
8961961e0fSrobinson static int
cklock(char * name)9061961e0fSrobinson cklock(char *name)
917c478bd9Sstevel@tonic-gate {
9261961e0fSrobinson 	int ret;
937c478bd9Sstevel@tonic-gate 	pid_t lpid = -1;
947c478bd9Sstevel@tonic-gate 	char alpid[SIZEOFPID+2];	/* +2 for '\n' and NULL */
957c478bd9Sstevel@tonic-gate 	int fd;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	fd = open(name, O_RDONLY);
987c478bd9Sstevel@tonic-gate 	DEBUG(4, "ulockf name %s\n", name);
997c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1007c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {	/* file does not exist -- OK */
10161961e0fSrobinson 			return (0);
1027c478bd9Sstevel@tonic-gate 		}
10361961e0fSrobinson 		DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
10461961e0fSrobinson 									errno);
1057c478bd9Sstevel@tonic-gate 		goto unlk;
1067c478bd9Sstevel@tonic-gate 	}
10761961e0fSrobinson 	ret = read(fd, (char *)alpid, SIZEOFPID + 1); /* +1 for '\n' */
1087c478bd9Sstevel@tonic-gate 	(void) close(fd);
1097c478bd9Sstevel@tonic-gate 	if (ret != (SIZEOFPID+1)) {
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
1127c478bd9Sstevel@tonic-gate 		goto unlk;
1137c478bd9Sstevel@tonic-gate 	}
11461961e0fSrobinson 	lpid = (pid_t)strtol(alpid, NULL, 10);
11561961e0fSrobinson 	if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
11661961e0fSrobinson 		DEBUG(4, "Lock File--process still active--not removed\n%s",
11761961e0fSrobinson 									"");
1187c478bd9Sstevel@tonic-gate 		return (FAIL);
1197c478bd9Sstevel@tonic-gate 	}
12061961e0fSrobinson 	/* process no longer active */
12161961e0fSrobinson 	DEBUG(4, "kill pid (%ld), ", (long)lpid);
12261961e0fSrobinson 	DEBUG(4, "returned %d", ret);
12361961e0fSrobinson 	DEBUG(4, "--ok to remove lock file (%s)\n", name);
1247c478bd9Sstevel@tonic-gate unlk:
12561961e0fSrobinson 
1267c478bd9Sstevel@tonic-gate 	if (unlink(name) != 0) {
12761961e0fSrobinson 		DEBUG(4, "ulockf failed in unlink()\n%s", "");
1287c478bd9Sstevel@tonic-gate 		return (FAIL);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
13361961e0fSrobinson #define	MAXLOCKS	10	/* maximum number of lock files */
1347c478bd9Sstevel@tonic-gate static char *Lockfile[MAXLOCKS];
13561961e0fSrobinson static int Nlocks = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * put name in list of lock files
1397c478bd9Sstevel@tonic-gate  * return:
1407c478bd9Sstevel@tonic-gate  *	none
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate static void
stlock(char * name)14361961e0fSrobinson stlock(char *name)
1447c478bd9Sstevel@tonic-gate {
14561961e0fSrobinson 	int i;
1467c478bd9Sstevel@tonic-gate 	char *p;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
1497c478bd9Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
1537c478bd9Sstevel@tonic-gate 	if (i >= Nlocks)
1547c478bd9Sstevel@tonic-gate 		i = Nlocks++;
15561961e0fSrobinson 	p = calloc((unsigned)strlen(name) + 1, sizeof (char));
1567c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
1577c478bd9Sstevel@tonic-gate 	(void) strcpy(p, name);
1587c478bd9Sstevel@tonic-gate 	Lockfile[i] = p;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * remove the named lock. If named lock is NULL,
1637c478bd9Sstevel@tonic-gate  *	then remove all locks currently in list.
1647c478bd9Sstevel@tonic-gate  * return:
1657c478bd9Sstevel@tonic-gate  *	none
1667c478bd9Sstevel@tonic-gate  */
16761961e0fSrobinson static void
rmlock(char * name)16861961e0fSrobinson rmlock(char *name)
1697c478bd9Sstevel@tonic-gate {
17061961e0fSrobinson 	int i;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	for (i = 0; i < Nlocks; i++) {
1737c478bd9Sstevel@tonic-gate 		if (Lockfile[i] == NULL)
1747c478bd9Sstevel@tonic-gate 			continue;
1757c478bd9Sstevel@tonic-gate 		if (name == NULL || EQUALS(name, Lockfile[i])) {
1767c478bd9Sstevel@tonic-gate 			(void) unlink(Lockfile[i]);
1777c478bd9Sstevel@tonic-gate 			free(Lockfile[i]);
1787c478bd9Sstevel@tonic-gate 			Lockfile[i] = NULL;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * makes a lock on behalf of pid.
1857c478bd9Sstevel@tonic-gate  * input:
1867c478bd9Sstevel@tonic-gate  *	pid - process id
1877c478bd9Sstevel@tonic-gate  *	tempfile - name of a temporary in the same file system
1887c478bd9Sstevel@tonic-gate  *	name - lock file name (full path name)
1897c478bd9Sstevel@tonic-gate  * return:
1907c478bd9Sstevel@tonic-gate  *	-1 - failed
1917c478bd9Sstevel@tonic-gate  *	0  - lock made successfully
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static int
onelock(char * pid,char * tempfile,char * name)19461961e0fSrobinson onelock(char *pid, char *tempfile, char *name)
19561961e0fSrobinson {
19661961e0fSrobinson 	int fd;
1977c478bd9Sstevel@tonic-gate 	char	cb[100];
1987c478bd9Sstevel@tonic-gate 
19961961e0fSrobinson 	fd = creat(tempfile, (mode_t)0444);
20061961e0fSrobinson 	if (fd < 0) {
20161961e0fSrobinson 		(void) snprintf(cb, sizeof (cb),
20261961e0fSrobinson 					"%s %s %d", tempfile, name, errno);
2037c478bd9Sstevel@tonic-gate 		logent("ULOCKC", cb);
2047c478bd9Sstevel@tonic-gate 		if ((errno == EMFILE) || (errno == ENFILE))
2057c478bd9Sstevel@tonic-gate 			(void) unlink(tempfile);
2067c478bd9Sstevel@tonic-gate 		return (-1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	/* +1 for '\n' */
2097c478bd9Sstevel@tonic-gate 	if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
21061961e0fSrobinson 		(void) snprintf(cb, sizeof (cb),
21161961e0fSrobinson 					"%s %s %d", tempfile, name, errno);
2127c478bd9Sstevel@tonic-gate 		logent("ULOCKW", cb);
2137c478bd9Sstevel@tonic-gate 		(void) unlink(tempfile);
2147c478bd9Sstevel@tonic-gate 		return (-1);
2157c478bd9Sstevel@tonic-gate 	}
21661961e0fSrobinson 	(void) chmod(tempfile, (mode_t)0444);
2177c478bd9Sstevel@tonic-gate 	(void) chown(tempfile, UUCPUID, UUCPGID);
2187c478bd9Sstevel@tonic-gate 	(void) close(fd);
21961961e0fSrobinson 	if (link(tempfile, name) < 0) {
2207c478bd9Sstevel@tonic-gate 		DEBUG(4, "%s: ", strerror(errno));
2217c478bd9Sstevel@tonic-gate 		DEBUG(4, "link(%s, ", tempfile);
2227c478bd9Sstevel@tonic-gate 		DEBUG(4, "%s)\n", name);
22361961e0fSrobinson 		if (unlink(tempfile) <  0) {
22461961e0fSrobinson 			(void) snprintf(cb, sizeof (cb),
22561961e0fSrobinson 					"ULK err %s %d", tempfile,  errno);
2267c478bd9Sstevel@tonic-gate 			logent("ULOCKLNK", cb);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		return (-1);
2297c478bd9Sstevel@tonic-gate 	}
23061961e0fSrobinson 	if (unlink(tempfile) < 0) {
23161961e0fSrobinson 		(void) snprintf(cb, sizeof (cb), "%s %d", tempfile, errno);
2327c478bd9Sstevel@tonic-gate 		logent("ULOCKF", cb);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * fd_mklock(fd) - lock the device indicated by fd is possible
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * return -
2417c478bd9Sstevel@tonic-gate  *	SUCCESS - this process now has the fd locked
2427c478bd9Sstevel@tonic-gate  *	FAIL - this process was not able to lock the fd
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate 
24561961e0fSrobinson static int
fd_mklock(int fd)24661961e0fSrobinson fd_mklock(int fd)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	int tries = 0;
2497c478bd9Sstevel@tonic-gate 	struct stat64 _st_buf;
2507c478bd9Sstevel@tonic-gate 	char lockname[BUFSIZ];
2517c478bd9Sstevel@tonic-gate 
252*e8031f0aSraf 	if (fstat64(fd, &_st_buf) != 0)
2537c478bd9Sstevel@tonic-gate 		return (FAIL);
2547c478bd9Sstevel@tonic-gate 
25561961e0fSrobinson 	(void) snprintf(lockname, sizeof (lockname),
25661961e0fSrobinson 		"%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
25761961e0fSrobinson 		(unsigned long)major(_st_buf.st_dev),
25861961e0fSrobinson 		(unsigned long)major(_st_buf.st_rdev),
25961961e0fSrobinson 		(unsigned long)minor(_st_buf.st_rdev));
2607c478bd9Sstevel@tonic-gate 
26161961e0fSrobinson 	if (mklock(lockname) == FAIL)
2627c478bd9Sstevel@tonic-gate 		return (FAIL);
26361961e0fSrobinson 
26461961e0fSrobinson 	while (lockf(fd, F_TLOCK, 0L) != 0) {
2657c478bd9Sstevel@tonic-gate 		DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
2667c478bd9Sstevel@tonic-gate 		if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
2677c478bd9Sstevel@tonic-gate 			rmlock(lockname);
26861961e0fSrobinson 			logent("fd_mklock", "lockf failed");
2697c478bd9Sstevel@tonic-gate 			return (FAIL);
2707c478bd9Sstevel@tonic-gate 		}
27161961e0fSrobinson 		(void) sleep(2);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	DEBUG(7, "fd_mklock: ok\n%s", "");
2747c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * remove the locks associated with the device file descriptor
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  * return -
2817c478bd9Sstevel@tonic-gate  *	SUCCESS - both BNU lock file and advisory locks removed
28261961e0fSrobinson  *	FAIL -
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate 
28561961e0fSrobinson static void
fd_rmlock(int fd)28661961e0fSrobinson fd_rmlock(int fd)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	struct stat64 _st_buf;
2897c478bd9Sstevel@tonic-gate 	char lockname[BUFSIZ];
2907c478bd9Sstevel@tonic-gate 
291*e8031f0aSraf 	if (fstat64(fd, &_st_buf) == 0) {
29261961e0fSrobinson 		(void) snprintf(lockname, sizeof (lockname),
29361961e0fSrobinson 			"%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
29461961e0fSrobinson 			(unsigned long)major(_st_buf.st_dev),
29561961e0fSrobinson 			(unsigned long)major(_st_buf.st_rdev),
29661961e0fSrobinson 			(unsigned long)minor(_st_buf.st_rdev));
2977c478bd9Sstevel@tonic-gate 		rmlock(lockname);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	(void) lockf(fd, F_ULOCK, 0L);
3007c478bd9Sstevel@tonic-gate }
301