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