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