[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [patch] KLockFile -- try harder
From:       Nick Shaforostoff <shaforostoff () gmail ! com>
Date:       2009-01-28 16:37:19
Message-ID: 758e8e2d0901280837h666e9755id713253eb33ed6a1 () mail ! gmail ! com
[Download RAW message or body]

This patch makes KLockFile handle pre-existing .lock files better.

Now, when existing lock file is detected, even if NoBlockFlag is specified,
LockStale or LockOK (depending on ForceFlag) is returned, instead of LockFile.

This is done now in only one while(true) cycle iteration, so it works
with NoBlockFlag.


Ok to commit?

["klockfile.patch" (application/octet-stream)]

Index: klockfile_unix.cpp
===================================================================
--- klockfile_unix.cpp	(revision 917477)
+++ klockfile_unix.cpp	(working copy)
@@ -270,54 +270,13 @@
            break;
         }
      }
-     else // KLockFile::Fail
+     else // KLockFile::Fail -- there is already such a file present (e.g. left by a crashed app)
      {
         if (!d->staleTimer.isNull() && d->statBuf != st_buf)
            d->staleTimer = QTime();
 
-        if (!d->staleTimer.isNull())
+        if (d->staleTimer.isNull())
         {
-           bool isStale = false;
-           if ((d->pid > 0) && !d->hostname.isEmpty())
-           {
-              // Check if hostname is us
-              char hostname[256];
-              hostname[0] = 0;
-              gethostname(hostname, 255);
-              hostname[255] = 0;
-
-              if (d->hostname == QLatin1String(hostname))
-              {
-                 // Check if pid still exists
-                 int res = ::kill(d->pid, 0);
-                 if ((res == -1) && (errno == ESRCH))
-                    isStale = true;
-              }
-           }
-           if (d->staleTimer.elapsed() > (d->staleTime*1000))
-              isStale = true;
-
-           if (isStale)
-           {
-              if ((options & ForceFlag) == 0)
-                 return KLockFile::LockStale;
-
-              result = deleteStaleLock(d->file, d->statBuf, d->linkCountSupport, d->componentData);
-
-              if (result == KLockFile::LockOK)
-              {
-                 // Lock deletion successful
-                 d->staleTimer = QTime();
-                 continue; // Now try to get the new lock
-              }
-              else if (result != KLockFile::LockFail)
-              {
-                 return result;
-              }
-           }
-        }
-        else
-        {
            memcpy(&(d->statBuf), &st_buf, sizeof(KDE_struct_stat));
            d->staleTimer.start();
 
@@ -337,9 +296,48 @@
                  d->hostname = ts.readLine();
            }
         }
+
+        bool isStale = false;
+        if ((d->pid > 0) && !d->hostname.isEmpty())
+        {
+           // Check if hostname is us
+           char hostname[256];
+           hostname[0] = 0;
+           gethostname(hostname, 255);
+           hostname[255] = 0;
+
+           if (d->hostname == QLatin1String(hostname))
+           {
+              // Check if pid still exists
+              int res = ::kill(d->pid, 0);
+              if ((res == -1) && (errno == ESRCH))
+                 isStale = true;
+           }
+        }
+        if (d->staleTimer.elapsed() > (d->staleTime*1000))
+           isStale = true;
+
+        if (isStale)
+        {
+           if ((options & ForceFlag) == 0)
+              return KLockFile::LockStale;
+
+           result = deleteStaleLock(d->file, d->statBuf, d->linkCountSupport, d->componentData);
+
+           if (result == KLockFile::LockOK)
+           {
+              // Lock deletion successful
+              d->staleTimer = QTime();
+              continue; // Now try to get the new lock
+           }
+           else if (result != KLockFile::LockFail)
+           {
+              return result;
+           }
+        }
      }
 
-     if ((options & NoBlockFlag) != 0)
+     if (options & NoBlockFlag)
         break;
 
      struct timeval tv;
Index: klockfile.h
===================================================================
--- klockfile.h	(revision 917477)
+++ klockfile.h	(working copy)
@@ -78,7 +78,8 @@
 
      /**
       * Automatically remove a lock when a lock is detected that is stale
-      * for more than staleTime() seconds.
+      * for more than staleTime() seconds, or if the process that created it
+      * is not running anymore.
       */
      ForceFlag = 2
    };


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic