Hello,

attached a patch to add a switch to have libreoffice store its pid in a file. I need that so that when libo runs in headless mode i can have a supervisor process checking if libo is running and in case just restart it. The error handling is a bit lame, do we have an helper to create a file with its content with one function call somewhere? that could help cleaning the pidfile creation code a bit i think.

I have a couple of issues though:
- i can't find where i should put the switch to show in the help output.
- have i placed the actual pid writing in the right place?

Stephan, i've copied the GETPID bits from sal/osl/all/log.cxx, is it ok for you to move them to sal/osl/process.h and include that file in log.cxx?

thanks,
riccardo
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index 3e03a57..08e65bde 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -1570,6 +1570,24 @@ int Desktop::Main()
         impl_checkRecoveryState(bCrashed, bExistsRecoveryData, bExistsSessionData);
         RTL_LOGFILE_CONTEXT_TRACE( aLog, "} impl_checkRecoveryState" );
 
+        OUString pidfileURL = rCmdLineArgs.GetPidfileName();
+        if ( !pidfileURL.isEmpty() )
+        {
+            osl::File pidfile( pidfileURL );
+            sal_Bool error = true;
+            if ( pidfile.open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) == osl::File::E_None )
+            {
+                OString pid( OString::valueOf( static_cast<sal_Int32>( OSL_GETPID() ) ) );
+                sal_uInt64 written = 0;
+                error = pidfile.write(pid.getStr(), pid.getLength(), written) != osl::File::E_None;
+                pidfile.close();
+            }
+            if ( error )
+            {
+                SAL_WARN("desktop", "cannot write pidfile " << pidfile.getURL());
+            }
+        }
+
         if ( rCmdLineArgs.IsHeadless() )
         {
             // Ensure that we use not the system file dialogs as
diff --git a/desktop/source/app/cmdlineargs.cxx b/desktop/source/app/cmdlineargs.cxx
index 83a251c..24de22a 100644
--- a/desktop/source/app/cmdlineargs.cxx
+++ b/desktop/source/app/cmdlineargs.cxx
@@ -525,6 +525,10 @@ bool CommandLineArgs::InterpretCommandLineParameter( const ::rtl::OUString& aArg
     {
         m_language = oArg.copy(RTL_CONSTASCII_LENGTH("language="));
     }
+    else if ( oArg.matchIgnoreAsciiCase("pidfile="))
+    {
+        m_pidfile = oArg.copy(RTL_CONSTASCII_LENGTH("pidfile="));
+    }
     else if ( oArg == "writer" )
     {
         m_writer = true;
@@ -849,6 +853,11 @@ bool CommandLineArgs::WantsToLoadDocument() const
     return m_bDocumentArgs;
 }
 
+OUString CommandLineArgs::GetPidfileName() const
+{
+    return m_pidfile;
+}
+
 } // namespace desktop
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/app/cmdlineargs.hxx b/desktop/source/app/cmdlineargs.hxx
index a9c2c48..4ea5e0d 100644
--- a/desktop/source/app/cmdlineargs.hxx
+++ b/desktop/source/app/cmdlineargs.hxx
@@ -105,6 +105,7 @@ class CommandLineArgs: private boost::noncopyable
         std::vector< rtl::OUString > GetConversionList() const;
         rtl::OUString       GetConversionParams() const;
         rtl::OUString       GetConversionOut() const;
+        OUString       GetPidfileName() const;
 
         // Special analyzed states (does not match directly to a command line parameter!)
         bool IsEmpty() const;
@@ -166,6 +167,7 @@ class CommandLineArgs: private boost::noncopyable
         rtl::OUString m_conversionout; // contains external URIs
         std::vector< rtl::OUString > m_infilter;
         rtl::OUString m_language;
+        OUString m_pidfile;
 };
 
 }
diff --git a/sal/inc/osl/process.h b/sal/inc/osl/process.h
index 241f9a8..1a4d713 100644
--- a/sal/inc/osl/process.h
+++ b/sal/inc/osl/process.h
@@ -37,6 +37,13 @@
 extern "C" {
 #endif
 
+#if defined WNT
+#include <process.h>
+#define OSL_GETPID _getpid
+#else
+#include <unistd.h>
+#define OSL_GETPID getpid
+#endif
 
 typedef sal_Int32 oslProcessOption;
 #define     osl_Process_WAIT       0x0001    /* wait for completion */
_______________________________________________
LibreOffice mailing list
LibreOffice@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice

Reply via email to