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