xref: /titanic_41/usr/src/cmd/make/lib/vroot/lock.cc (revision 3625efb1376c3d31a0e742ed72b778e5b3add543)
1*3625efb1SRichard Lowe /*
2*3625efb1SRichard Lowe  * CDDL HEADER START
3*3625efb1SRichard Lowe  *
4*3625efb1SRichard Lowe  * The contents of this file are subject to the terms of the
5*3625efb1SRichard Lowe  * Common Development and Distribution License (the "License").
6*3625efb1SRichard Lowe  * You may not use this file except in compliance with the License.
7*3625efb1SRichard Lowe  *
8*3625efb1SRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3625efb1SRichard Lowe  * or http://www.opensolaris.org/os/licensing.
10*3625efb1SRichard Lowe  * See the License for the specific language governing permissions
11*3625efb1SRichard Lowe  * and limitations under the License.
12*3625efb1SRichard Lowe  *
13*3625efb1SRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
14*3625efb1SRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3625efb1SRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
16*3625efb1SRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
17*3625efb1SRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3625efb1SRichard Lowe  *
19*3625efb1SRichard Lowe  * CDDL HEADER END
20*3625efb1SRichard Lowe  */
21*3625efb1SRichard Lowe /*
22*3625efb1SRichard Lowe  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23*3625efb1SRichard Lowe  * Use is subject to license terms.
24*3625efb1SRichard Lowe  */
25*3625efb1SRichard Lowe 
26*3625efb1SRichard Lowe #include <stdio.h>
27*3625efb1SRichard Lowe #include <stdlib.h>
28*3625efb1SRichard Lowe #include <string.h>
29*3625efb1SRichard Lowe #include <sys/errno.h>
30*3625efb1SRichard Lowe #include <sys/param.h>
31*3625efb1SRichard Lowe #include <sys/stat.h>
32*3625efb1SRichard Lowe #include <sys/types.h>
33*3625efb1SRichard Lowe #include <unistd.h>
34*3625efb1SRichard Lowe #include <vroot/vroot.h>
35*3625efb1SRichard Lowe #include <signal.h>
36*3625efb1SRichard Lowe #include <errno.h>			/* errno */
37*3625efb1SRichard Lowe #include <libintl.h>
38*3625efb1SRichard Lowe 
39*3625efb1SRichard Lowe extern	char		*sys_errlist[];
40*3625efb1SRichard Lowe extern	int		sys_nerr;
41*3625efb1SRichard Lowe 
42*3625efb1SRichard Lowe static	void		file_lock_error(char *msg, char *file, char *str, int arg1, int arg2);
43*3625efb1SRichard Lowe 
44*3625efb1SRichard Lowe #define BLOCK_INTERUPTS sigfillset(&newset) ; \
45*3625efb1SRichard Lowe 	sigprocmask(SIG_SETMASK, &newset, &oldset)
46*3625efb1SRichard Lowe 
47*3625efb1SRichard Lowe #define UNBLOCK_INTERUPTS \
48*3625efb1SRichard Lowe 	sigprocmask(SIG_SETMASK, &oldset, &newset)
49*3625efb1SRichard Lowe 
50*3625efb1SRichard Lowe /*
51*3625efb1SRichard Lowe  * This code stolen from the NSE library and changed to not depend
52*3625efb1SRichard Lowe  * upon any NSE routines or header files.
53*3625efb1SRichard Lowe  *
54*3625efb1SRichard Lowe  * Simple file locking.
55*3625efb1SRichard Lowe  * Create a symlink to a file.  The "test and set" will be
56*3625efb1SRichard Lowe  * atomic as creating the symlink provides both functions.
57*3625efb1SRichard Lowe  *
58*3625efb1SRichard Lowe  * The timeout value specifies how long to wait for stale locks
59*3625efb1SRichard Lowe  * to disappear.  If the lock is more than 'timeout' seconds old
60*3625efb1SRichard Lowe  * then it is ok to blow it away.  This part has a small window
61*3625efb1SRichard Lowe  * of vunerability as the operations of testing the time,
62*3625efb1SRichard Lowe  * removing the lock and creating a new one are not atomic.
63*3625efb1SRichard Lowe  * It would be possible for two processes to both decide to blow
64*3625efb1SRichard Lowe  * away the lock and then have process A remove the lock and establish
65*3625efb1SRichard Lowe  * its own, and then then have process B remove the lock which accidentily
66*3625efb1SRichard Lowe  * removes A's lock rather than the stale one.
67*3625efb1SRichard Lowe  *
68*3625efb1SRichard Lowe  * A further complication is with the NFS.  If the file in question is
69*3625efb1SRichard Lowe  * being served by an NFS server, then its time is set by that server.
70*3625efb1SRichard Lowe  * We can not use the time on the client machine to check for a stale
71*3625efb1SRichard Lowe  * lock.  Therefore, a temp file on the server is created to get
72*3625efb1SRichard Lowe  * the servers current time.
73*3625efb1SRichard Lowe  *
74*3625efb1SRichard Lowe  * Returns an error message.  NULL return means the lock was obtained.
75*3625efb1SRichard Lowe  *
76*3625efb1SRichard Lowe  * 12/6/91 Added the parameter "file_locked".  Before this parameter
77*3625efb1SRichard Lowe  * was added, the calling procedure would have to wait for file_lock()
78*3625efb1SRichard Lowe  * to return before it sets the flag. If the user interrupted "make"
79*3625efb1SRichard Lowe  * between the time the lock was acquired and the time file_lock()
80*3625efb1SRichard Lowe  * returns, make wouldn't know that the file has been locked, and therefore
81*3625efb1SRichard Lowe  * it wouldn' remove the lock. Setting the flag right after locking the file
82*3625efb1SRichard Lowe  * makes this window much smaller.
83*3625efb1SRichard Lowe  */
84*3625efb1SRichard Lowe 
85*3625efb1SRichard Lowe int
file_lock(char * name,char * lockname,int * file_locked,int timeout)86*3625efb1SRichard Lowe file_lock(char *name, char *lockname, int *file_locked, int timeout)
87*3625efb1SRichard Lowe {
88*3625efb1SRichard Lowe 	int		counter = 0;
89*3625efb1SRichard Lowe 	static char	msg[MAXPATHLEN+1];
90*3625efb1SRichard Lowe 	int		printed_warning = 0;
91*3625efb1SRichard Lowe 	int		r;
92*3625efb1SRichard Lowe 	struct stat	statb;
93*3625efb1SRichard Lowe 	sigset_t newset;
94*3625efb1SRichard Lowe 	sigset_t oldset;
95*3625efb1SRichard Lowe 
96*3625efb1SRichard Lowe 	*file_locked = 0;
97*3625efb1SRichard Lowe 	if (timeout <= 0) {
98*3625efb1SRichard Lowe 		timeout = 120;
99*3625efb1SRichard Lowe 	}
100*3625efb1SRichard Lowe 	for (;;) {
101*3625efb1SRichard Lowe 		BLOCK_INTERUPTS;
102*3625efb1SRichard Lowe 		r = symlink(name, lockname);
103*3625efb1SRichard Lowe 		if (r == 0) {
104*3625efb1SRichard Lowe 			*file_locked = 1;
105*3625efb1SRichard Lowe 			UNBLOCK_INTERUPTS;
106*3625efb1SRichard Lowe 			return 0; /* success */
107*3625efb1SRichard Lowe 		}
108*3625efb1SRichard Lowe 		UNBLOCK_INTERUPTS;
109*3625efb1SRichard Lowe 
110*3625efb1SRichard Lowe 		if (errno != EEXIST) {
111*3625efb1SRichard Lowe 			file_lock_error(msg, name, (char *)"symlink(%s, %s)",
112*3625efb1SRichard Lowe 			    (int) name, (int) lockname);
113*3625efb1SRichard Lowe 			fprintf(stderr, "%s", msg);
114*3625efb1SRichard Lowe 			return errno;
115*3625efb1SRichard Lowe 		}
116*3625efb1SRichard Lowe 
117*3625efb1SRichard Lowe 		counter = 0;
118*3625efb1SRichard Lowe 		for (;;) {
119*3625efb1SRichard Lowe 			sleep(1);
120*3625efb1SRichard Lowe 			r = lstat(lockname, &statb);
121*3625efb1SRichard Lowe 			if (r == -1) {
122*3625efb1SRichard Lowe 				/*
123*3625efb1SRichard Lowe 				 * The lock must have just gone away - try
124*3625efb1SRichard Lowe 				 * again.
125*3625efb1SRichard Lowe 				 */
126*3625efb1SRichard Lowe 				break;
127*3625efb1SRichard Lowe 			}
128*3625efb1SRichard Lowe 
129*3625efb1SRichard Lowe 			if ((counter > 5) && (!printed_warning)) {
130*3625efb1SRichard Lowe 				/* Print waiting message after 5 secs */
131*3625efb1SRichard Lowe 				(void) getcwd(msg, MAXPATHLEN);
132*3625efb1SRichard Lowe 				fprintf(stderr,
133*3625efb1SRichard Lowe 					gettext("file_lock: file %s is already locked.\n"),
134*3625efb1SRichard Lowe 					name);
135*3625efb1SRichard Lowe 				fprintf(stderr,
136*3625efb1SRichard Lowe 					gettext("file_lock: will periodically check the lockfile %s for two minutes.\n"),
137*3625efb1SRichard Lowe 					lockname);
138*3625efb1SRichard Lowe 				fprintf(stderr,
139*3625efb1SRichard Lowe 					gettext("Current working directory %s\n"),
140*3625efb1SRichard Lowe 					msg);
141*3625efb1SRichard Lowe 
142*3625efb1SRichard Lowe 				printed_warning = 1;
143*3625efb1SRichard Lowe 			}
144*3625efb1SRichard Lowe 
145*3625efb1SRichard Lowe 			if (++counter > timeout ) {
146*3625efb1SRichard Lowe 				/*
147*3625efb1SRichard Lowe 				 * Waited enough - return an error..
148*3625efb1SRichard Lowe 				 */
149*3625efb1SRichard Lowe 				return EEXIST;
150*3625efb1SRichard Lowe 			}
151*3625efb1SRichard Lowe 		}
152*3625efb1SRichard Lowe 	}
153*3625efb1SRichard Lowe 	/* NOTREACHED */
154*3625efb1SRichard Lowe }
155*3625efb1SRichard Lowe 
156*3625efb1SRichard Lowe /*
157*3625efb1SRichard Lowe  * Format a message telling why the lock could not be created.
158*3625efb1SRichard Lowe  */
159*3625efb1SRichard Lowe static	void
file_lock_error(char * msg,char * file,char * str,int arg1,int arg2)160*3625efb1SRichard Lowe file_lock_error(char *msg, char *file, char *str, int arg1, int arg2)
161*3625efb1SRichard Lowe {
162*3625efb1SRichard Lowe 	int		len;
163*3625efb1SRichard Lowe 
164*3625efb1SRichard Lowe 	sprintf(msg, gettext("Could not lock file `%s'; "), file);
165*3625efb1SRichard Lowe 	len = strlen(msg);
166*3625efb1SRichard Lowe 	sprintf(&msg[len], str, arg1, arg2);
167*3625efb1SRichard Lowe 	strcat(msg, gettext(" failed - "));
168*3625efb1SRichard Lowe 	if (errno < sys_nerr) {
169*3625efb1SRichard Lowe 		strcat(msg, strerror(errno));
170*3625efb1SRichard Lowe 	} else {
171*3625efb1SRichard Lowe 		len = strlen(msg);
172*3625efb1SRichard Lowe 		sprintf(&msg[len], "errno %d", errno);
173*3625efb1SRichard Lowe 	}
174*3625efb1SRichard Lowe }
175*3625efb1SRichard Lowe 
176