Re: [patch] kmail startup lock revisited

David Faure <[email protected]>
Newsgroups gmane.comp.kde.devel.kmail
Organization Klaralvdalens Datakonsult.
Message-ID <[email protected]>
On Tuesday 06 February 2007, David Faure wrote:
> On Tuesday 06 February 2007, Ingo Klöcker wrote:
> > On Tuesday 06 February 2007 00:03, David Faure wrote:
> > > Yes, but I'm really annoyed by this constant bogus warning box, when
> > > I know very well that I'm not running kmail on two displays...
> > 
> > I guess checking the name of the process with PID oldPid isn't a 
> > feasible (and portable) alternative?
> 
> It's not portable, but it's definitely feasible on at least linux :-)
> That sounds like a good plan, actually. Rather simple.

Works fine, too.

(It's quite fortunate that neither kmail nor kontact are kdeinit modules,
otherwise the solution would become much more complex.)

-- 
David Faure, [email protected], [email protected]
KDE/KOffice developer, Qt consultancy projects
Klarälvdalens Datakonsult AB, Platform-independent software solutions

_______________________________________________
KMail developers mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kmail-devel
kmstartup.diff (text/x-diff, 2.1 KB)
Index: kmstartup.cpp
===================================================================
--- kmstartup.cpp	(revision 643307)
+++ kmstartup.cpp	(working copy)
@@ -36,10 +36,12 @@
 
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/param.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <qfile.h>
 
 #undef Status // stupid X headers
 
@@ -154,12 +156,34 @@
   bool first_instance = false;
   if ( oldPid == -1 )
       first_instance = true;
-  // check if the lock file is stale by trying to see if
-  // the other pid is currently running.
-  // Not 100% correct but better safe than sorry
   else if (hostName == oldHostName && oldPid != getpid()) {
-      if ( kill(oldPid, 0) == -1 )
-          first_instance = ( errno == ESRCH );
+      // check if the lock file is stale
+#ifdef Q_OS_LINUX
+      if ( ::access("/proc", X_OK ) == 0 ) {
+          // On linux with /proc we can even check that it's really kmail and not something else
+          char path_buffer[MAXPATHLEN + 1];
+          path_buffer[MAXPATHLEN] = 0;
+          const QString procPath = QString("/proc/%1/exe").arg(oldPid);
+          const int length = readlink (procPath.latin1(), path_buffer, MAXPATHLEN);
+          if ( length == -1 ) // not such pid
+              first_instance = true;
+          else {
+              path_buffer[length] = '\0';
+              const QString path = QFile::decodeName(path_buffer);
+              kdDebug() << k_funcinfo << path << endl;
+              const int pos = path.findRev('/');
+              const QString fileName = path.mid(pos+1);
+              kdDebug() << "Found process " << oldPid << " running. It's: " << fileName << endl;
+              first_instance = fileName != "kmail" && fileName != "kontact";
+          }
+      } else
+#endif
+      {
+          // Otherwise we just check if the other pid is currently running.
+          // Not 100% correct but better safe than sorry.
+          if ( kill(oldPid, 0) == -1 )
+              first_instance = ( errno == ESRCH );
+      }
   }
 
   if ( !first_instance ) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.