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 <avo/intl.h> /* for NOCATGETS */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/errno.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <vroot/vroot.h>
36 #include <mksdmsi18n/mksdmsi18n.h>
37 #include <signal.h>
38 #include <errno.h> /* errno */
39
40 extern char *sys_errlist[];
41 extern int sys_nerr;
42
43 static void file_lock_error(char *msg, char *file, char *str, int arg1, int arg2);
44
45 #define BLOCK_INTERUPTS sigfillset(&newset) ; \
46 sigprocmask(SIG_SETMASK, &newset, &oldset)
47
48 #define UNBLOCK_INTERUPTS \
49 sigprocmask(SIG_SETMASK, &oldset, &newset)
50
51 /*
52 * This code stolen from the NSE library and changed to not depend
53 * upon any NSE routines or header files.
54 *
55 * Simple file locking.
56 * Create a symlink to a file. The "test and set" will be
57 * atomic as creating the symlink provides both functions.
58 *
92 int r;
93 struct stat statb;
94 sigset_t newset;
95 sigset_t oldset;
96
97 *file_locked = 0;
98 if (timeout <= 0) {
99 timeout = 120;
100 }
101 for (;;) {
102 BLOCK_INTERUPTS;
103 r = symlink(name, lockname);
104 if (r == 0) {
105 *file_locked = 1;
106 UNBLOCK_INTERUPTS;
107 return 0; /* success */
108 }
109 UNBLOCK_INTERUPTS;
110
111 if (errno != EEXIST) {
112 file_lock_error(msg, name, (char *)NOCATGETS("symlink(%s, %s)"),
113 (int) name, (int) lockname);
114 fprintf(stderr, "%s", msg);
115 return errno;
116 }
117
118 counter = 0;
119 for (;;) {
120 sleep(1);
121 r = lstat(lockname, &statb);
122 if (r == -1) {
123 /*
124 * The lock must have just gone away - try
125 * again.
126 */
127 break;
128 }
129
130 if ((counter > 5) && (!printed_warning)) {
131 /* Print waiting message after 5 secs */
132 (void) getcwd(msg, MAXPATHLEN);
133 fprintf(stderr,
134 catgets(libmksdmsi18n_catd, 1, 162, "file_lock: file %s is already locked.\n"),
135 name);
136 fprintf(stderr,
137 catgets(libmksdmsi18n_catd, 1, 163, "file_lock: will periodically check the lockfile %s for two minutes.\n"),
138 lockname);
139 fprintf(stderr,
140 catgets(libmksdmsi18n_catd, 1, 144, "Current working directory %s\n"),
141 msg);
142
143 printed_warning = 1;
144 }
145
146 if (++counter > timeout ) {
147 /*
148 * Waited enough - return an error..
149 */
150 return EEXIST;
151 }
152 }
153 }
154 /* NOTREACHED */
155 }
156
157 /*
158 * Format a message telling why the lock could not be created.
159 */
160 static void
161 file_lock_error(char *msg, char *file, char *str, int arg1, int arg2)
162 {
163 int len;
164
165 sprintf(msg, catgets(libmksdmsi18n_catd, 1, 145, "Could not lock file `%s'; "), file);
166 len = strlen(msg);
167 sprintf(&msg[len], str, arg1, arg2);
168 strcat(msg, catgets(libmksdmsi18n_catd, 1, 146, " failed - "));
169 if (errno < sys_nerr) {
170 strcat(msg, strerror(errno));
171 } else {
172 len = strlen(msg);
173 sprintf(&msg[len], NOCATGETS("errno %d"), errno);
174 }
175 }
176
|
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 extern char *sys_errlist[];
40 extern int sys_nerr;
41
42 static void file_lock_error(char *msg, char *file, char *str, int arg1, int arg2);
43
44 #define BLOCK_INTERUPTS sigfillset(&newset) ; \
45 sigprocmask(SIG_SETMASK, &newset, &oldset)
46
47 #define UNBLOCK_INTERUPTS \
48 sigprocmask(SIG_SETMASK, &oldset, &newset)
49
50 /*
51 * This code stolen from the NSE library and changed to not depend
52 * upon any NSE routines or header files.
53 *
54 * Simple file locking.
55 * Create a symlink to a file. The "test and set" will be
56 * atomic as creating the symlink provides both functions.
57 *
91 int r;
92 struct stat statb;
93 sigset_t newset;
94 sigset_t oldset;
95
96 *file_locked = 0;
97 if (timeout <= 0) {
98 timeout = 120;
99 }
100 for (;;) {
101 BLOCK_INTERUPTS;
102 r = symlink(name, lockname);
103 if (r == 0) {
104 *file_locked = 1;
105 UNBLOCK_INTERUPTS;
106 return 0; /* success */
107 }
108 UNBLOCK_INTERUPTS;
109
110 if (errno != EEXIST) {
111 file_lock_error(msg, name, (char *)"symlink(%s, %s)",
112 (int) name, (int) lockname);
113 fprintf(stderr, "%s", msg);
114 return errno;
115 }
116
117 counter = 0;
118 for (;;) {
119 sleep(1);
120 r = lstat(lockname, &statb);
121 if (r == -1) {
122 /*
123 * The lock must have just gone away - try
124 * again.
125 */
126 break;
127 }
128
129 if ((counter > 5) && (!printed_warning)) {
130 /* Print waiting message after 5 secs */
131 (void) getcwd(msg, MAXPATHLEN);
132 fprintf(stderr,
133 gettext("file_lock: file %s is already locked.\n"),
134 name);
135 fprintf(stderr,
136 gettext("file_lock: will periodically check the lockfile %s for two minutes.\n"),
137 lockname);
138 fprintf(stderr,
139 gettext("Current working directory %s\n"),
140 msg);
141
142 printed_warning = 1;
143 }
144
145 if (++counter > timeout ) {
146 /*
147 * Waited enough - return an error..
148 */
149 return EEXIST;
150 }
151 }
152 }
153 /* NOTREACHED */
154 }
155
156 /*
157 * Format a message telling why the lock could not be created.
158 */
159 static void
160 file_lock_error(char *msg, char *file, char *str, int arg1, int arg2)
161 {
162 int len;
163
164 sprintf(msg, gettext("Could not lock file `%s'; "), file);
165 len = strlen(msg);
166 sprintf(&msg[len], str, arg1, arg2);
167 strcat(msg, gettext(" failed - "));
168 if (errno < sys_nerr) {
169 strcat(msg, strerror(errno));
170 } else {
171 len = strlen(msg);
172 sprintf(&msg[len], "errno %d", errno);
173 }
174 }
175
|