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 * @(#)lock.cc 1.17 06/12/12 27 */ 28 29 #pragma ident "@(#)lock.cc 1.17 06/12/12" 30 31 #include <avo/intl.h> /* for NOCATGETS */ 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <sys/errno.h> 36 #include <sys/param.h> 37 #include <sys/stat.h> 38 #include <sys/types.h> 39 #include <unistd.h> 40 #include <vroot/vroot.h> 41 #include <mksdmsi18n/mksdmsi18n.h> 42 #include <signal.h> 43 #include <errno.h> /* errno */ 44 45 #if !defined(linux) 46 extern char *sys_errlist[]; 47 extern int sys_nerr; 48 #endif 49 50 static void file_lock_error(char *msg, char *file, char *str, int arg1, int arg2); 51 52 #define BLOCK_INTERUPTS sigfillset(&newset) ; \ 53 sigprocmask(SIG_SETMASK, &newset, &oldset) 54 55 #define UNBLOCK_INTERUPTS \ 56 sigprocmask(SIG_SETMASK, &oldset, &newset) 57 58 /* 59 * This code stolen from the NSE library and changed to not depend 60 * upon any NSE routines or header files. 61 * 62 * Simple file locking. 63 * Create a symlink to a file. The "test and set" will be 64 * atomic as creating the symlink provides both functions. 65 * 66 * The timeout value specifies how long to wait for stale locks 67 * to disappear. If the lock is more than 'timeout' seconds old 68 * then it is ok to blow it away. This part has a small window 69 * of vunerability as the operations of testing the time, 70 * removing the lock and creating a new one are not atomic. 71 * It would be possible for two processes to both decide to blow 72 * away the lock and then have process A remove the lock and establish 73 * its own, and then then have process B remove the lock which accidentily 74 * removes A's lock rather than the stale one. 75 * 76 * A further complication is with the NFS. If the file in question is 77 * being served by an NFS server, then its time is set by that server. 78 * We can not use the time on the client machine to check for a stale 79 * lock. Therefore, a temp file on the server is created to get 80 * the servers current time. 81 * 82 * Returns an error message. NULL return means the lock was obtained. 83 * 84 * 12/6/91 Added the parameter "file_locked". Before this parameter 85 * was added, the calling procedure would have to wait for file_lock() 86 * to return before it sets the flag. If the user interrupted "make" 87 * between the time the lock was acquired and the time file_lock() 88 * returns, make wouldn't know that the file has been locked, and therefore 89 * it wouldn' remove the lock. Setting the flag right after locking the file 90 * makes this window much smaller. 91 */ 92 93 int 94 file_lock(char *name, char *lockname, int *file_locked, int timeout) 95 { 96 int counter = 0; 97 static char msg[MAXPATHLEN+1]; 98 int printed_warning = 0; 99 int r; 100 struct stat statb; 101 sigset_t newset; 102 sigset_t oldset; 103 104 *file_locked = 0; 105 if (timeout <= 0) { 106 timeout = 120; 107 } 108 for (;;) { 109 BLOCK_INTERUPTS; 110 r = symlink(name, lockname); 111 if (r == 0) { 112 *file_locked = 1; 113 UNBLOCK_INTERUPTS; 114 return 0; /* success */ 115 } 116 UNBLOCK_INTERUPTS; 117 118 if (errno != EEXIST) { 119 file_lock_error(msg, name, NOCATGETS("symlink(%s, %s)"), 120 (int) name, (int) lockname); 121 fprintf(stderr, "%s", msg); 122 return errno; 123 } 124 125 counter = 0; 126 for (;;) { 127 sleep(1); 128 r = lstat(lockname, &statb); 129 if (r == -1) { 130 /* 131 * The lock must have just gone away - try 132 * again. 133 */ 134 break; 135 } 136 137 if ((counter > 5) && (!printed_warning)) { 138 /* Print waiting message after 5 secs */ 139 #if defined(SUN5_0) || defined(HP_UX) || defined(linux) 140 (void) getcwd(msg, MAXPATHLEN); 141 #else 142 (void) getwd(msg); 143 #endif 144 fprintf(stderr, 145 catgets(libmksdmsi18n_catd, 1, 162, "file_lock: file %s is already locked.\n"), 146 name); 147 fprintf(stderr, 148 catgets(libmksdmsi18n_catd, 1, 163, "file_lock: will periodically check the lockfile %s for two minutes.\n"), 149 lockname); 150 fprintf(stderr, 151 catgets(libmksdmsi18n_catd, 1, 144, "Current working directory %s\n"), 152 msg); 153 154 printed_warning = 1; 155 } 156 157 if (++counter > timeout ) { 158 /* 159 * Waited enough - return an error.. 160 */ 161 return EEXIST; 162 } 163 } 164 } 165 /* NOTREACHED */ 166 } 167 168 /* 169 * Format a message telling why the lock could not be created. 170 */ 171 static void 172 file_lock_error(char *msg, char *file, char *str, int arg1, int arg2) 173 { 174 int len; 175 176 sprintf(msg, catgets(libmksdmsi18n_catd, 1, 145, "Could not lock file `%s'; "), file); 177 len = strlen(msg); 178 sprintf(&msg[len], str, arg1, arg2); 179 strcat(msg, catgets(libmksdmsi18n_catd, 1, 146, " failed - ")); 180 #if !defined(linux) 181 if (errno < sys_nerr) { 182 #ifdef SUN4_x 183 strcat(msg, sys_errlist[errno]); 184 #endif 185 #ifdef SUN5_0 186 strcat(msg, strerror(errno)); 187 #endif 188 } else { 189 len = strlen(msg); 190 sprintf(&msg[len], NOCATGETS("errno %d"), errno); 191 } 192 #else 193 strcat(msg, strerror(errno)); 194 #endif 195 } 196