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