xref: /illumos-gate/usr/src/cmd/make/lib/vroot/lock.cc (revision 356ba08c)
110d63b7dSRichard Lowe /*
210d63b7dSRichard Lowe  * CDDL HEADER START
310d63b7dSRichard Lowe  *
410d63b7dSRichard Lowe  * The contents of this file are subject to the terms of the
510d63b7dSRichard Lowe  * Common Development and Distribution License (the "License").
610d63b7dSRichard Lowe  * You may not use this file except in compliance with the License.
710d63b7dSRichard Lowe  *
810d63b7dSRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910d63b7dSRichard Lowe  * or http://www.opensolaris.org/os/licensing.
1010d63b7dSRichard Lowe  * See the License for the specific language governing permissions
1110d63b7dSRichard Lowe  * and limitations under the License.
1210d63b7dSRichard Lowe  *
1310d63b7dSRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
1410d63b7dSRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510d63b7dSRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
1610d63b7dSRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
1710d63b7dSRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
1810d63b7dSRichard Lowe  *
1910d63b7dSRichard Lowe  * CDDL HEADER END
2010d63b7dSRichard Lowe  */
2110d63b7dSRichard Lowe /*
2210d63b7dSRichard Lowe  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2310d63b7dSRichard Lowe  * Use is subject to license terms.
2410d63b7dSRichard Lowe  */
2510d63b7dSRichard Lowe 
2610d63b7dSRichard Lowe #include <stdio.h>
2710d63b7dSRichard Lowe #include <stdlib.h>
2810d63b7dSRichard Lowe #include <string.h>
2910d63b7dSRichard Lowe #include <sys/errno.h>
3010d63b7dSRichard Lowe #include <sys/param.h>
3110d63b7dSRichard Lowe #include <sys/stat.h>
3210d63b7dSRichard Lowe #include <sys/types.h>
3310d63b7dSRichard Lowe #include <unistd.h>
3410d63b7dSRichard Lowe #include <vroot/vroot.h>
3510d63b7dSRichard Lowe #include <signal.h>
3610d63b7dSRichard Lowe #include <errno.h>			/* errno */
3710d63b7dSRichard Lowe #include <libintl.h>
3810d63b7dSRichard Lowe 
39*356ba08cSToomas Soome static	void		file_lock_error(char *msg, char *file, const char *str,
40*356ba08cSToomas Soome     char *arg1, char * arg2);
4110d63b7dSRichard Lowe 
4210d63b7dSRichard Lowe #define BLOCK_INTERUPTS sigfillset(&newset) ; \
4310d63b7dSRichard Lowe 	sigprocmask(SIG_SETMASK, &newset, &oldset)
4410d63b7dSRichard Lowe 
4510d63b7dSRichard Lowe #define UNBLOCK_INTERUPTS \
4610d63b7dSRichard Lowe 	sigprocmask(SIG_SETMASK, &oldset, &newset)
4710d63b7dSRichard Lowe 
4810d63b7dSRichard Lowe /*
4910d63b7dSRichard Lowe  * This code stolen from the NSE library and changed to not depend
5010d63b7dSRichard Lowe  * upon any NSE routines or header files.
5110d63b7dSRichard Lowe  *
5210d63b7dSRichard Lowe  * Simple file locking.
5310d63b7dSRichard Lowe  * Create a symlink to a file.  The "test and set" will be
5410d63b7dSRichard Lowe  * atomic as creating the symlink provides both functions.
5510d63b7dSRichard Lowe  *
5610d63b7dSRichard Lowe  * The timeout value specifies how long to wait for stale locks
5710d63b7dSRichard Lowe  * to disappear.  If the lock is more than 'timeout' seconds old
5810d63b7dSRichard Lowe  * then it is ok to blow it away.  This part has a small window
5910d63b7dSRichard Lowe  * of vunerability as the operations of testing the time,
6010d63b7dSRichard Lowe  * removing the lock and creating a new one are not atomic.
6110d63b7dSRichard Lowe  * It would be possible for two processes to both decide to blow
6210d63b7dSRichard Lowe  * away the lock and then have process A remove the lock and establish
6310d63b7dSRichard Lowe  * its own, and then then have process B remove the lock which accidentily
6410d63b7dSRichard Lowe  * removes A's lock rather than the stale one.
6510d63b7dSRichard Lowe  *
6610d63b7dSRichard Lowe  * A further complication is with the NFS.  If the file in question is
6710d63b7dSRichard Lowe  * being served by an NFS server, then its time is set by that server.
6810d63b7dSRichard Lowe  * We can not use the time on the client machine to check for a stale
6910d63b7dSRichard Lowe  * lock.  Therefore, a temp file on the server is created to get
7010d63b7dSRichard Lowe  * the servers current time.
7110d63b7dSRichard Lowe  *
7210d63b7dSRichard Lowe  * Returns an error message.  NULL return means the lock was obtained.
7310d63b7dSRichard Lowe  *
7410d63b7dSRichard Lowe  * 12/6/91 Added the parameter "file_locked".  Before this parameter
7510d63b7dSRichard Lowe  * was added, the calling procedure would have to wait for file_lock()
7610d63b7dSRichard Lowe  * to return before it sets the flag. If the user interrupted "make"
7710d63b7dSRichard Lowe  * between the time the lock was acquired and the time file_lock()
7810d63b7dSRichard Lowe  * returns, make wouldn't know that the file has been locked, and therefore
7910d63b7dSRichard Lowe  * it wouldn' remove the lock. Setting the flag right after locking the file
8010d63b7dSRichard Lowe  * makes this window much smaller.
8110d63b7dSRichard Lowe  */
8210d63b7dSRichard Lowe 
8310d63b7dSRichard Lowe int
file_lock(char * name,char * lockname,int * file_locked,int timeout)8410d63b7dSRichard Lowe file_lock(char *name, char *lockname, int *file_locked, int timeout)
8510d63b7dSRichard Lowe {
8610d63b7dSRichard Lowe 	int		counter = 0;
8710d63b7dSRichard Lowe 	static char	msg[MAXPATHLEN+1];
8810d63b7dSRichard Lowe 	int		printed_warning = 0;
8910d63b7dSRichard Lowe 	int		r;
9010d63b7dSRichard Lowe 	struct stat	statb;
9110d63b7dSRichard Lowe 	sigset_t newset;
9210d63b7dSRichard Lowe 	sigset_t oldset;
9310d63b7dSRichard Lowe 
9410d63b7dSRichard Lowe 	*file_locked = 0;
9510d63b7dSRichard Lowe 	if (timeout <= 0) {
9610d63b7dSRichard Lowe 		timeout = 120;
9710d63b7dSRichard Lowe 	}
9810d63b7dSRichard Lowe 	for (;;) {
9910d63b7dSRichard Lowe 		BLOCK_INTERUPTS;
10010d63b7dSRichard Lowe 		r = symlink(name, lockname);
10110d63b7dSRichard Lowe 		if (r == 0) {
10210d63b7dSRichard Lowe 			*file_locked = 1;
10310d63b7dSRichard Lowe 			UNBLOCK_INTERUPTS;
10410d63b7dSRichard Lowe 			return 0; /* success */
10510d63b7dSRichard Lowe 		}
10610d63b7dSRichard Lowe 		UNBLOCK_INTERUPTS;
10710d63b7dSRichard Lowe 
10810d63b7dSRichard Lowe 		if (errno != EEXIST) {
109*356ba08cSToomas Soome 			file_lock_error(msg, name, "symlink(%s, %s)",
110*356ba08cSToomas Soome 			    name, lockname);
11110d63b7dSRichard Lowe 			fprintf(stderr, "%s", msg);
11210d63b7dSRichard Lowe 			return errno;
11310d63b7dSRichard Lowe 		}
11410d63b7dSRichard Lowe 
11510d63b7dSRichard Lowe 		counter = 0;
11610d63b7dSRichard Lowe 		for (;;) {
11710d63b7dSRichard Lowe 			sleep(1);
11810d63b7dSRichard Lowe 			r = lstat(lockname, &statb);
11910d63b7dSRichard Lowe 			if (r == -1) {
12010d63b7dSRichard Lowe 				/*
12110d63b7dSRichard Lowe 				 * The lock must have just gone away - try
12210d63b7dSRichard Lowe 				 * again.
12310d63b7dSRichard Lowe 				 */
12410d63b7dSRichard Lowe 				break;
12510d63b7dSRichard Lowe 			}
12610d63b7dSRichard Lowe 
12710d63b7dSRichard Lowe 			if ((counter > 5) && (!printed_warning)) {
12810d63b7dSRichard Lowe 				/* Print waiting message after 5 secs */
12910d63b7dSRichard Lowe 				(void) getcwd(msg, MAXPATHLEN);
130*356ba08cSToomas Soome 				fprintf(stderr, gettext(
131*356ba08cSToomas Soome 				    "file_lock: file %s is already locked.\n"),
13210d63b7dSRichard Lowe 				    name);
133*356ba08cSToomas Soome 				fprintf(stderr, gettext(
134*356ba08cSToomas Soome 				    "file_lock: will periodically check the "
135*356ba08cSToomas Soome 				    "lockfile %s for two minutes.\n"),
13610d63b7dSRichard Lowe 				    lockname);
137*356ba08cSToomas Soome 				fprintf(stderr, gettext(
138*356ba08cSToomas Soome 				    "Current working directory %s\n"), msg);
13910d63b7dSRichard Lowe 
14010d63b7dSRichard Lowe 				printed_warning = 1;
14110d63b7dSRichard Lowe 			}
14210d63b7dSRichard Lowe 
14310d63b7dSRichard Lowe 			if (++counter > timeout ) {
14410d63b7dSRichard Lowe 				/*
14510d63b7dSRichard Lowe 				 * Waited enough - return an error..
14610d63b7dSRichard Lowe 				 */
14710d63b7dSRichard Lowe 				return EEXIST;
14810d63b7dSRichard Lowe 			}
14910d63b7dSRichard Lowe 		}
15010d63b7dSRichard Lowe 	}
15110d63b7dSRichard Lowe 	/* NOTREACHED */
15210d63b7dSRichard Lowe }
15310d63b7dSRichard Lowe 
15410d63b7dSRichard Lowe /*
15510d63b7dSRichard Lowe  * Format a message telling why the lock could not be created.
15610d63b7dSRichard Lowe  */
15710d63b7dSRichard Lowe static	void
file_lock_error(char * msg,char * file,const char * str,char * arg1,char * arg2)158*356ba08cSToomas Soome file_lock_error(char *msg, char *file, const char *str, char *arg1, char *arg2)
15910d63b7dSRichard Lowe {
160*356ba08cSToomas Soome 	int		len, err;
161*356ba08cSToomas Soome 	char		*ptr;
16210d63b7dSRichard Lowe 
16310d63b7dSRichard Lowe 	sprintf(msg, gettext("Could not lock file `%s'; "), file);
16410d63b7dSRichard Lowe 	len = strlen(msg);
16510d63b7dSRichard Lowe 	sprintf(&msg[len], str, arg1, arg2);
16610d63b7dSRichard Lowe 	strcat(msg, gettext(" failed - "));
167*356ba08cSToomas Soome 
168*356ba08cSToomas Soome 	err = errno;
169*356ba08cSToomas Soome 	errno = 0;
170*356ba08cSToomas Soome 	ptr = strerror(err);
171*356ba08cSToomas Soome 	if (errno != EINVAL) {
172*356ba08cSToomas Soome 		strcat(msg, ptr);
17310d63b7dSRichard Lowe 	} else {
17410d63b7dSRichard Lowe 		len = strlen(msg);
175*356ba08cSToomas Soome 		sprintf(&msg[len], "errno %d", err);
17610d63b7dSRichard Lowe 	}
17710d63b7dSRichard Lowe }
178