]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: preparatory change toward simplifying FileLock_Unlock
authorVMware, Inc <>
Mon, 26 Jul 2010 18:58:04 +0000 (11:58 -0700)
committerMarcelo Vanzin <mvanzin@vmware.com>
Mon, 26 Jul 2010 18:58:04 +0000 (11:58 -0700)
The FileLock_Lock routine returns an abstract file lock token
representing the lock on the file. There is no functional need
to base the name of the file to be unlocked to FileLock_Unlock
since the token should already contain the necessary information
for the unlock.

This change lays the groundwork. The next one will clean up all
of the FileLock_Unlock callers. In the end all of this is being
done because "it's the right thing to do" and because Kevin has
a need for it - it will make a mainMem cleanup easier.

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/lib/file/fileInt.h
open-vm-tools/lib/file/fileLockPosix.c
open-vm-tools/lib/file/fileLockPrimitive.c

index dfdfc10be22a5eefed42286cffd890b6bc9d61cf..bbdd471f8e80337c9d79cce10ab8549b26fbe26e 100644 (file)
@@ -195,6 +195,22 @@ typedef struct lock_values
    ActiveLock   *lockList;
 } LockValues;
 
+/*
+ * The lock token. This is returned by the lock operation and must be sent
+ * to the unlock operation.
+ */
+
+#define FILE_LOCK_TOKEN_SIGNATURE 0x4B434C46  // 'FLCK' in memory
+
+struct FileLockToken
+{
+   uint32  signature;
+   Unicode pathName;
+   Unicode lockFilePath;  // &implicitReadToken for implicit read locks
+};
+
+typedef struct FileLockToken FileLockToken;
+
 #include "file_extensions.h"
 
 #define FILELOCK_SUFFIX "." LOCK_FILE_EXTENSION
@@ -243,14 +259,13 @@ EXTERN int FileLockWriteFile(FILELOCK_FILE_HANDLE handle,
                              uint32 requestedBytes,
                              uint32 *resultantBytes);
 
-EXTERN void *FileLockIntrinsic(ConstUnicode filePathName,
-                               Bool exclusivity,
-                               uint32 msecMaxWaitTime,
-                               const char *payload,
-                               int *err);
+EXTERN FileLockToken *FileLockIntrinsic(ConstUnicode filePathName,
+                                        Bool exclusivity,
+                                        uint32 msecMaxWaitTime,
+                                        const char *payload,
+                                        int *err);
 
-EXTERN int FileUnlockIntrinsic(ConstUnicode filePathName,
-                               const void *lockToken);
+EXTERN int FileUnlockIntrinsic(FileLockToken *tokenPtr);
 
 EXTERN Bool FileLockIsLocked(ConstUnicode filePath,
                              int *err);
index 536b73fd9033f61dc831e37d8fa07e70c5229559..1f2e420e3fc1b052cd33532043f14f3b12cad35d 100644 (file)
@@ -1060,8 +1060,8 @@ FileLock_Lock(ConstUnicode filePath,         // IN:
               const uint32 msecMaxWaitTime,  // IN:
               int *err)                      // OUT:
 {
-   void *lockToken;
    Unicode normalizedPath;
+   FileLockToken *tokenPtr;
 
    ASSERT(filePath);
    ASSERT(err);
@@ -1070,20 +1070,20 @@ FileLock_Lock(ConstUnicode filePath,         // IN:
    if (normalizedPath == NULL) {
       *err = EINVAL;
 
-      lockToken = NULL;
+      tokenPtr = NULL;
    } else {
       char creationTimeString[32];
 
       Str_Sprintf(creationTimeString, sizeof creationTimeString, "%"FMT64"u",
                   ProcessCreationTime(getpid()));
 
-      lockToken = FileLockIntrinsic(normalizedPath, !readOnly, msecMaxWaitTime,
-                                    creationTimeString, err);
+      tokenPtr = FileLockIntrinsic(normalizedPath, !readOnly, msecMaxWaitTime,
+                                   creationTimeString, err);
 
       Unicode_Free(normalizedPath);
    }
 
-   return lockToken;
+   return (void *) tokenPtr;
 }
 
 
@@ -1151,22 +1151,10 @@ int
 FileLock_Unlock(ConstUnicode filePath,  // IN:
                 const void *lockToken)  // IN:
 {
-   int err;
-   Unicode normalizedPath;
-
    ASSERT(filePath);
    ASSERT(lockToken);
 
-   normalizedPath = FileLockNormalizePath(filePath);
-   if (normalizedPath == NULL) {
-      err = EINVAL;
-   } else {
-      err = FileUnlockIntrinsic(normalizedPath, lockToken);
-
-      Unicode_Free(normalizedPath);
-   }
-
-   return err;
+   return FileUnlockIntrinsic((FileLockToken *) lockToken);
 }
 
 
index 838644d707b7ca3b925e58d2c1d87fb3a1cee2f1..46c36acc0f23aa8184a8db50be4682dc22ee7008 100644 (file)
@@ -52,6 +52,7 @@
 #include "fileInt.h"
 #include "random.h"
 #include "vm_atomic.h"
+#include "util.h"
 
 #include "unicodeOperations.h"
 
@@ -919,55 +920,56 @@ Scanner(ConstUnicode lockDir,    // IN:
  */
 
 int
-FileUnlockIntrinsic(ConstUnicode pathName,  // IN:
-                    const void *lockToken)  // IN:
+FileUnlockIntrinsic(FileLockToken *tokenPtr)  // IN:
 {
    int err;
 
-   ASSERT(pathName);
-   ASSERT(lockToken);
+   ASSERT(tokenPtr && (tokenPtr->signature == FILE_LOCK_TOKEN_SIGNATURE));
 
-   LOG(1, ("Requesting unlock on %s\n", UTF8(pathName)));
+   LOG(1, ("Requesting unlock on %s\n", UTF8(tokenPtr->pathName)));
 
-   if (lockToken == &implicitReadToken) {
-      /*
-       * The lock token is the fixed-address implicit read lock token.
-       * Since no lock file was created no further action is required.
-       */
+   /*
+    * If the lockFilePath (a pointer) is the fixed-address token representing
+    * an implicit read lock, there is no lock file and the token can simply
+    * be discarded.
+    */
 
+   if (tokenPtr->lockFilePath == &implicitReadToken) {
       err = 0;
+
+      free(tokenPtr->pathName);
    } else {
       Unicode lockDir;
 
       /* The lock directory path */
-      lockDir = Unicode_Append(pathName, FILELOCK_SUFFIX);
+      lockDir = Unicode_Append(tokenPtr->pathName, FILELOCK_SUFFIX);
 
       /*
-       * The lock token is the (unicode) path of the lock file.
-       *
        * TODO: under vmx86_debug validate the contents of the lock file as
        *       matching the machineID and executionID.
        */
 
-      err = FileDeletionRobust((Unicode) lockToken, FALSE);
+      err = FileDeletionRobust(tokenPtr->lockFilePath, FALSE);
 
       if (err && vmx86_debug) {
          Log(LGPFX" %s failed for '%s': %s\n",
-             __FUNCTION__, (char *) lockToken, Err_Errno2String(err));
+             __FUNCTION__, tokenPtr->lockFilePath, Err_Errno2String(err));
       }
 
       /*
-       * The lockToken (a unicode path) was allocated in FileLockIntrinsic
-       * and returned to the caller hidden behind a "void *" pointer.
+       * Attempt to clean up the locking directory.
        */
 
-      Unicode_Free((Unicode) lockToken);
-
       FileRemoveDirectoryRobust(lockDir); // just in case we can clean up
 
       Unicode_Free(lockDir);
+      free(tokenPtr->lockFilePath);
+      free(tokenPtr->pathName);
    }
 
+   tokenPtr->signature = 0;  // Just in case...
+   free(tokenPtr);
+
    return err;
 }
 
@@ -1446,7 +1448,7 @@ CreateMemberFile(FILELOCK_FILE_HANDLE entryHandle,  // IN:
  *-----------------------------------------------------------------------------
  */
 
-void *
+FileLockToken *
 FileLockIntrinsic(ConstUnicode pathName,   // IN:
                   Bool exclusivity,        // IN:
                   uint32 msecMaxWaitTime,  // IN:
@@ -1456,6 +1458,7 @@ FileLockIntrinsic(ConstUnicode pathName,   // IN:
    FILELOCK_FILE_HANDLE handle;
    LockValues myValues;
    int createFlags;
+   FileLockToken *tokenPtr;
 
    Unicode lockDir = NULL;
    Unicode entryFilePath = NULL;
@@ -1593,16 +1596,22 @@ bail:
    free(myValues.locationChecksum);
    free(myValues.executionID);
 
-   if (*err != 0) {
+   if (*err == 0) {
+      tokenPtr = Util_SafeMalloc(sizeof(FileLockToken));
+
+      tokenPtr->signature = FILE_LOCK_TOKEN_SIGNATURE;
+      tokenPtr->pathName = Unicode_Duplicate(pathName);
+      tokenPtr->lockFilePath = memberFilePath;
+   } else {
       Unicode_Free(memberFilePath);
-      memberFilePath = NULL;
+      tokenPtr = NULL;
 
       if (*err == EAGAIN) {
          *err = 0; // lock not acquired
       }
    }
 
-   return (void *) memberFilePath;
+   return tokenPtr;
 }