Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / app / officeipcthread.cxx
blob9b77da1a9037201d2950bae75c807919907de607
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <config_dbus.h>
23 #include <config_features.h>
24 #include <config_feature_desktop.h>
26 #include <app.hxx>
27 #include "officeipcthread.hxx"
28 #include "cmdlineargs.hxx"
29 #include "dispatchwatcher.hxx"
30 #include <com/sun/star/frame/TerminationVetoException.hpp>
31 #include <salhelper/thread.hxx>
32 #include <sal/log.hxx>
33 #include <unotools/bootstrap.hxx>
34 #include <utility>
35 #include <vcl/svapp.hxx>
36 #include <unotools/configmgr.hxx>
37 #include <osl/pipe.hxx>
38 #include <rtl/digest.h>
39 #include <rtl/ustrbuf.hxx>
40 #include <osl/conditn.hxx>
41 #include <unotools/moduleoptions.hxx>
42 #include <rtl/strbuf.hxx>
43 #include <cppuhelper/supportsservice.hxx>
44 #include <osl/file.hxx>
45 #include <rtl/process.h>
46 #include <o3tl/string_view.hxx>
48 #include <cassert>
49 #include <cstdlib>
50 #include <memory>
51 #include <thread>
53 #if ENABLE_DBUS
54 #include <dbus/dbus.h>
55 #include <sys/socket.h>
56 #endif
58 using namespace desktop;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::frame;
63 namespace {
65 char const ARGUMENT_PREFIX[] = "InternalIPC::Arguments";
66 char const SEND_ARGUMENTS[] = "InternalIPC::SendArguments";
67 char const PROCESSING_DONE[] = "InternalIPC::ProcessingDone";
69 // Receives packets from the pipe until a packet ends in a NUL character (that
70 // will not be included in the returned string) or it cannot read anything (due
71 // to error or closed pipe, in which case an empty string will be returned to
72 // signal failure):
73 OString readStringFromPipe(osl::StreamPipe const & pipe) {
74 for (OStringBuffer str;;) {
75 char buf[1024];
76 sal_Int32 n = pipe.recv(buf, std::size(buf));
77 if (n <= 0) {
78 SAL_INFO("desktop.app", "read empty string");
79 return "";
81 bool end = false;
82 if (buf[n - 1] == '\0') {
83 end = true;
84 --n;
86 str.append(buf, n);
87 //TODO: how does OStringBuffer.append handle overflow?
88 if (end) {
89 auto s = str.makeStringAndClear();
90 SAL_INFO("desktop.app", "read <" << s << ">");
91 return s;
98 namespace desktop
101 namespace {
103 class Parser: public CommandLineArgs::Supplier {
104 public:
105 explicit Parser(OString input): m_input(std::move(input)) {
106 if (!m_input.match(ARGUMENT_PREFIX) ||
107 m_input.getLength() == RTL_CONSTASCII_LENGTH(ARGUMENT_PREFIX))
109 throw CommandLineArgs::Supplier::Exception();
111 m_index = RTL_CONSTASCII_LENGTH(ARGUMENT_PREFIX);
112 switch (m_input[m_index++]) {
113 case '0':
114 break;
115 case '1':
117 OUString url;
118 if (!next(&url, false)) {
119 throw CommandLineArgs::Supplier::Exception();
121 m_cwdUrl = url;
122 break;
124 case '2':
126 OUString path;
127 if (!next(&path, false)) {
128 throw CommandLineArgs::Supplier::Exception();
130 OUString url;
131 if (osl::FileBase::getFileURLFromSystemPath(path, url) ==
132 osl::FileBase::E_None)
134 m_cwdUrl = url;
136 break;
138 default:
139 throw CommandLineArgs::Supplier::Exception();
143 virtual std::optional< OUString > getCwdUrl() override { return m_cwdUrl; }
145 virtual bool next(OUString * argument) override { return next(argument, true); }
147 private:
148 bool next(OUString * argument, bool prefix) {
149 OSL_ASSERT(argument != nullptr);
150 if (m_index < m_input.getLength()) {
151 if (prefix) {
152 if (m_input[m_index] != ',') {
153 throw CommandLineArgs::Supplier::Exception();
155 ++m_index;
157 OStringBuffer b;
158 while (m_index < m_input.getLength()) {
159 char c = m_input[m_index];
160 if (c == ',') {
161 break;
163 ++m_index;
164 if (c == '\\') {
165 if (m_index >= m_input.getLength())
166 throw CommandLineArgs::Supplier::Exception();
167 c = m_input[m_index++];
168 switch (c) {
169 case '0':
170 c = '\0';
171 break;
172 case ',':
173 case '\\':
174 break;
175 default:
176 throw CommandLineArgs::Supplier::Exception();
179 b.append(c);
181 OString b2(b.makeStringAndClear());
182 if (!rtl_convertStringToUString(
183 &argument->pData, b2.getStr(), b2.getLength(),
184 RTL_TEXTENCODING_UTF8,
185 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
186 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
187 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
189 throw CommandLineArgs::Supplier::Exception();
191 return true;
192 } else {
193 return false;
197 std::optional< OUString > m_cwdUrl;
198 OString m_input;
199 sal_Int32 m_index;
202 bool addArgument(OStringBuffer &rArguments, char prefix,
203 const OUString &rArgument)
205 OString utf8;
206 if (!rArgument.convertToString(
207 &utf8, RTL_TEXTENCODING_UTF8,
208 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
209 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
211 return false;
213 rArguments.append(prefix);
214 for (sal_Int32 i = 0; i < utf8.getLength(); ++i) {
215 char c = utf8[i];
216 switch (c) {
217 case '\0':
218 rArguments.append("\\0");
219 break;
220 case ',':
221 rArguments.append("\\,");
222 break;
223 case '\\':
224 rArguments.append("\\\\");
225 break;
226 default:
227 rArguments.append(c);
228 break;
231 return true;
236 rtl::Reference< RequestHandler > RequestHandler::pGlobal;
238 // Turns a string in aMsg such as file:///home/foo/.libreoffice/3
239 // Into a hex string of well known length ff132a86...
240 static OUString CreateMD5FromString( const OUString& aMsg )
242 SAL_INFO("desktop.app", "create md5 from '" << aMsg << "'");
244 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
245 if ( handle )
247 const sal_uInt8* pData = reinterpret_cast<const sal_uInt8*>(aMsg.getStr());
248 sal_uInt32 nSize = aMsg.getLength() * sizeof( sal_Unicode );
249 sal_uInt32 nMD5KeyLen = rtl_digest_queryLength( handle );
250 std::unique_ptr<sal_uInt8[]> pMD5KeyBuffer(new sal_uInt8[ nMD5KeyLen ]);
252 rtl_digest_init( handle, pData, nSize );
253 rtl_digest_update( handle, pData, nSize );
254 rtl_digest_get( handle, pMD5KeyBuffer.get(), nMD5KeyLen );
255 rtl_digest_destroy( handle );
257 // Create hex-value string from the MD5 value to keep the string size minimal
258 OUStringBuffer aBuffer( nMD5KeyLen * 2 + 1 );
259 for ( sal_uInt32 i = 0; i < nMD5KeyLen; i++ )
260 aBuffer.append( static_cast<sal_Int32>(pMD5KeyBuffer[i]), 16 );
262 return aBuffer.makeStringAndClear();
265 return OUString();
268 namespace {
270 class ProcessEventsClass_Impl
272 public:
273 DECL_STATIC_LINK( ProcessEventsClass_Impl, CallEvent, void*, void );
274 DECL_STATIC_LINK( ProcessEventsClass_Impl, ProcessDocumentsEvent, void*, void );
279 IMPL_STATIC_LINK( ProcessEventsClass_Impl, CallEvent, void*, pEvent, void )
281 // Application events are processed by the Desktop::HandleAppEvent implementation.
282 Desktop::HandleAppEvent( *static_cast<ApplicationEvent*>(pEvent) );
283 delete static_cast<ApplicationEvent*>(pEvent);
286 IMPL_STATIC_LINK( ProcessEventsClass_Impl, ProcessDocumentsEvent, void*, pEvent, void )
288 // Documents requests are processed by the RequestHandler implementation
289 ProcessDocumentsRequest* pDocsRequest = static_cast<ProcessDocumentsRequest*>(pEvent);
290 RequestHandler::ExecuteCmdLineRequests(*pDocsRequest, false);
291 delete pDocsRequest;
294 static void ImplPostForeignAppEvent( ApplicationEvent* pEvent )
296 Application::PostUserEvent( LINK( nullptr, ProcessEventsClass_Impl, CallEvent ), pEvent );
299 static void ImplPostProcessDocumentsEvent( std::unique_ptr<ProcessDocumentsRequest> pEvent )
301 Application::PostUserEvent( LINK( nullptr, ProcessEventsClass_Impl, ProcessDocumentsEvent ), pEvent.release() );
304 oslSignalAction SalMainPipeExchangeSignal_impl(SAL_UNUSED_PARAMETER void* /*pData*/, oslSignalInfo* pInfo)
306 if( pInfo->Signal == osl_Signal_Terminate )
307 RequestHandler::Disable();
308 return osl_Signal_ActCallNextHdl;
312 // The RequestHandlerController implementation is a bookkeeper for all pending requests
313 // that were created by the RequestHandler. The requests are waiting to be processed by
314 // our framework loadComponentFromURL function (e.g. open/print request).
315 // During shutdown the framework is asking RequestHandlerController about pending requests.
316 // If there are pending requests framework has to stop the shutdown process. It is waiting
317 // for these requests because framework is not able to handle shutdown and open a document
318 // concurrently.
321 // XServiceInfo
322 OUString SAL_CALL RequestHandlerController::getImplementationName()
324 return "com.sun.star.comp.RequestHandlerController";
327 sal_Bool RequestHandlerController::supportsService(
328 OUString const & ServiceName)
330 return cppu::supportsService(this, ServiceName);
333 Sequence< OUString > SAL_CALL RequestHandlerController::getSupportedServiceNames()
335 return { };
338 // XEventListener
339 void SAL_CALL RequestHandlerController::disposing( const EventObject& )
343 // XTerminateListener
344 void SAL_CALL RequestHandlerController::queryTermination( const EventObject& )
346 // Desktop ask about pending request through our office ipc pipe. We have to
347 // be sure that no pending request is waiting because framework is not able to
348 // handle shutdown and open a document concurrently.
350 if ( RequestHandler::AreRequestsPending() )
351 throw TerminationVetoException();
352 RequestHandler::SetDowning();
355 void SAL_CALL RequestHandlerController::notifyTermination( const EventObject& )
359 class IpcThread: public salhelper::Thread {
360 public:
361 void start(RequestHandler * handler) {
362 m_handler = handler;
363 launch();
366 virtual void close() = 0;
368 protected:
369 explicit IpcThread(char const * name): Thread(name), m_handler(nullptr) {}
371 virtual ~IpcThread() override {}
373 bool process(OString const & arguments, bool * waitProcessed);
375 RequestHandler * m_handler;
378 class PipeIpcThread: public IpcThread {
379 public:
380 static RequestHandler::Status enable(rtl::Reference<IpcThread> * thread);
382 private:
383 explicit PipeIpcThread(osl::Pipe pipe):
384 IpcThread("PipeIPC"), pipe_(std::move(pipe))
387 virtual ~PipeIpcThread() override {}
389 void execute() override;
391 void close() override { pipe_.close(); }
393 osl::Pipe pipe_;
396 #if ENABLE_DBUS
398 namespace {
400 struct DbusConnectionHolder {
401 explicit DbusConnectionHolder(DBusConnection * theConnection):
402 connection(theConnection)
405 DbusConnectionHolder(DbusConnectionHolder && other): connection(nullptr)
406 { std::swap(connection, other.connection); }
408 ~DbusConnectionHolder() {
409 if (connection != nullptr) {
410 dbus_connection_close(connection);
411 dbus_connection_unref(connection);
415 DBusConnection * connection;
418 struct DbusMessageHolder {
419 explicit DbusMessageHolder(DBusMessage * theMessage): message(theMessage) {}
421 ~DbusMessageHolder() { clear(); }
423 void clear() {
424 if (message != nullptr) {
425 dbus_message_unref(message);
427 message = nullptr;
430 DBusMessage * message;
432 private:
433 DbusMessageHolder(DbusMessageHolder const &) = delete;
434 DbusMessageHolder& operator =(DbusMessageHolder const &) = delete;
439 class DbusIpcThread: public IpcThread {
440 public:
441 static RequestHandler::Status enable(rtl::Reference<IpcThread> * thread);
443 private:
444 explicit DbusIpcThread(DbusConnectionHolder && connection):
445 IpcThread("DbusIPC"), connection_(std::move(connection))
448 virtual ~DbusIpcThread() override {}
450 void execute() override;
452 void close() override;
454 DbusConnectionHolder connection_;
457 RequestHandler::Status DbusIpcThread::enable(rtl::Reference<IpcThread> * thread)
459 assert(thread != nullptr);
460 if (!dbus_threads_init_default()) {
461 SAL_WARN("desktop.app", "dbus_threads_init_default failed");
462 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
464 DBusError e;
465 dbus_error_init(&e);
466 DbusConnectionHolder con(dbus_bus_get_private(DBUS_BUS_SESSION, &e));
467 assert((con.connection == nullptr) == bool(dbus_error_is_set(&e)));
468 if (con.connection == nullptr) {
469 SAL_WARN(
470 "desktop.app",
471 "dbus_bus_get_private failed with: " << e.name << ": "
472 << e.message);
473 dbus_error_free(&e);
474 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
476 for (;;) {
477 int n = dbus_bus_request_name(
478 con.connection, "org.libreoffice.LibreOfficeIpc0",
479 DBUS_NAME_FLAG_DO_NOT_QUEUE, &e);
480 assert((n == -1) == bool(dbus_error_is_set(&e)));
481 switch (n) {
482 case -1:
483 SAL_WARN(
484 "desktop.app",
485 "dbus_bus_request_name failed with: " << e.name << ": "
486 << e.message);
487 dbus_error_free(&e);
488 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
489 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
490 *thread = new DbusIpcThread(std::move(con));
491 return RequestHandler::IPC_STATUS_OK;
492 case DBUS_REQUEST_NAME_REPLY_EXISTS:
494 OStringBuffer buf(ARGUMENT_PREFIX);
495 OUString arg;
496 if (!(utl::Bootstrap::getProcessWorkingDir(arg)
497 && addArgument(buf, '1', arg)))
499 buf.append('0');
501 sal_uInt32 narg = rtl_getAppCommandArgCount();
502 for (sal_uInt32 i = 0; i != narg; ++i) {
503 rtl_getAppCommandArg(i, &arg.pData);
504 if (!addArgument(buf, ',', arg)) {
505 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
508 char const * argstr = buf.getStr();
509 DbusMessageHolder msg(
510 dbus_message_new_method_call(
511 "org.libreoffice.LibreOfficeIpc0",
512 "/org/libreoffice/LibreOfficeIpc0",
513 "org.libreoffice.LibreOfficeIpcIfc0", "Execute"));
514 if (msg.message == nullptr) {
515 SAL_WARN(
516 "desktop.app", "dbus_message_new_method_call failed");
517 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
519 DBusMessageIter it;
520 dbus_message_iter_init_append(msg.message, &it);
521 if (!dbus_message_iter_append_basic(
522 &it, DBUS_TYPE_STRING, &argstr))
524 SAL_WARN(
525 "desktop.app", "dbus_message_iter_append_basic failed");
526 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
528 DbusMessageHolder repl(
529 dbus_connection_send_with_reply_and_block(
530 con.connection, msg.message, 0x7FFFFFFF, &e));
531 assert(
532 (repl.message == nullptr) == bool(dbus_error_is_set(&e)));
533 if (repl.message == nullptr) {
534 SAL_INFO(
535 "desktop.app",
536 "dbus_connection_send_with_reply_and_block failed"
537 " with: " << e.name << ": " << e.message);
538 dbus_error_free(&e);
539 break;
541 return RequestHandler::IPC_STATUS_2ND_OFFICE;
543 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
544 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
545 SAL_WARN(
546 "desktop.app",
547 "dbus_bus_request_name failed with unexpected " << +n);
548 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
549 default:
550 for (;;) std::abort();
555 void DbusIpcThread::execute()
557 assert(m_handler != nullptr);
558 m_handler->cReady.wait();
559 for (;;) {
561 osl::MutexGuard g(RequestHandler::GetMutex());
562 if (m_handler->mState == RequestHandler::State::Downing) {
563 break;
566 if (!dbus_connection_read_write(connection_.connection, -1)) {
567 break;
569 for (;;) {
570 DbusMessageHolder msg(
571 dbus_connection_pop_message(connection_.connection));
572 if (msg.message == nullptr) {
573 break;
575 if (!dbus_message_is_method_call(
576 msg.message, "org.libreoffice.LibreOfficeIpcIfc0",
577 "Execute"))
579 SAL_INFO("desktop.app", "unknown DBus message ignored");
580 continue;
582 DBusMessageIter it;
583 if (!dbus_message_iter_init(msg.message, &it)) {
584 SAL_WARN(
585 "desktop.app", "DBus message without argument ignored");
586 continue;
588 if (dbus_message_iter_get_arg_type(&it) != DBUS_TYPE_STRING) {
589 SAL_WARN(
590 "desktop.app",
591 "DBus message with non-string argument ignored");
592 continue;
594 char const * argstr;
595 dbus_message_iter_get_basic(&it, &argstr);
596 bool waitProcessed = false;
598 osl::MutexGuard g(RequestHandler::GetMutex());
599 if (!process(argstr, &waitProcessed)) {
600 continue;
603 if (waitProcessed) {
604 m_handler->cProcessed.wait();
606 DbusMessageHolder repl(dbus_message_new_method_return(msg.message));
607 if (repl.message == nullptr) {
608 SAL_WARN(
609 "desktop.app", "dbus_message_new_method_return failed");
610 continue;
612 dbus_uint32_t serial = 0;
613 if (!dbus_connection_send(
614 connection_.connection, repl.message, &serial)) {
615 SAL_WARN("desktop.app", "dbus_connection_send failed");
616 continue;
618 dbus_connection_flush(connection_.connection);
623 void DbusIpcThread::close() {
624 assert(connection_.connection != nullptr);
625 // Make dbus_connection_read_write fall out of internal poll call blocking
626 // on POLLIN:
627 int fd;
628 if (!dbus_connection_get_socket(connection_.connection, &fd)) {
629 SAL_WARN("desktop.app", "dbus_connection_get_socket failed");
630 return;
632 if (shutdown(fd, SHUT_RD) == -1) {
633 auto const e = errno;
634 SAL_WARN("desktop.app", "shutdown failed with errno " << e);
638 #endif
640 ::osl::Mutex& RequestHandler::GetMutex()
642 static ::osl::Mutex theRequestHandlerMutex;
643 return theRequestHandlerMutex;
646 void RequestHandler::SetDowning()
648 // We have the order to block all incoming requests. Framework
649 // wants to shutdown and we have to make sure that no loading/printing
650 // requests are executed anymore.
651 ::osl::MutexGuard aGuard( GetMutex() );
653 if ( pGlobal.is() )
654 pGlobal->mState = State::Downing;
657 void RequestHandler::EnableRequests()
659 // switch between just queueing the requests and executing them
660 ::osl::MutexGuard aGuard( GetMutex() );
662 if ( pGlobal.is() )
664 if (pGlobal->mState != State::Downing) {
665 pGlobal->mState = State::RequestsEnabled;
667 ProcessDocumentsRequest aEmptyReq(std::nullopt);
668 // trigger already queued requests
669 RequestHandler::ExecuteCmdLineRequests(aEmptyReq, true);
673 bool RequestHandler::AreRequestsPending()
675 // Give info about pending requests
676 ::osl::MutexGuard aGuard( GetMutex() );
677 if ( pGlobal.is() )
678 return ( pGlobal->mnPendingRequests > 0 );
679 else
680 return false;
683 void RequestHandler::RequestsCompleted()
685 // Remove nCount pending requests from our internal counter
686 ::osl::MutexGuard aGuard( GetMutex() );
687 if ( pGlobal.is() )
689 if ( pGlobal->mnPendingRequests > 0 )
690 pGlobal->mnPendingRequests --;
694 RequestHandler::Status RequestHandler::Enable(bool ipc)
696 ::osl::MutexGuard aGuard( GetMutex() );
698 if( pGlobal.is() )
699 return IPC_STATUS_OK;
701 #if !HAVE_FEATURE_DESKTOP || HAVE_FEATURE_MACOSX_SANDBOX || defined(EMSCRIPTEN)
702 ipc = false;
703 #endif
705 if (!ipc) {
706 pGlobal = new RequestHandler;
707 return IPC_STATUS_OK;
710 enum class Kind { Pipe, Dbus };
711 Kind kind;
712 #if ENABLE_DBUS
713 kind = std::getenv("LIBO_FLATPAK") != nullptr ? Kind::Dbus : Kind::Pipe;
714 #else
715 kind = Kind::Pipe;
716 #endif
717 rtl::Reference<IpcThread> thread;
718 Status stat = Status(); // silence bogus potentially-uninitialized warnings
719 switch (kind) {
720 case Kind::Pipe:
721 stat = PipeIpcThread::enable(&thread);
722 break;
723 case Kind::Dbus:
724 #if ENABLE_DBUS
725 stat = DbusIpcThread::enable(&thread);
726 break;
727 #endif
728 default:
729 assert(false);
731 assert(thread.is() == (stat == IPC_STATUS_OK));
732 if (stat == IPC_STATUS_OK) {
733 pGlobal = new RequestHandler;
734 pGlobal->mIpcThread = thread;
735 pGlobal->mIpcThread->start(pGlobal.get());
737 return stat;
740 RequestHandler::Status PipeIpcThread::enable(rtl::Reference<IpcThread> * thread)
742 assert(thread != nullptr);
744 // The name of the named pipe is created with the hashcode of the user installation directory (without /user). We have to retrieve
745 // this information from a unotools implementation.
746 OUString aUserInstallPath;
747 ::utl::Bootstrap::PathStatus aLocateResult = ::utl::Bootstrap::locateUserInstallation( aUserInstallPath );
748 if (aLocateResult != utl::Bootstrap::PATH_EXISTS
749 && aLocateResult != utl::Bootstrap::PATH_VALID)
751 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
754 // Try to determine if we are the first office or not! This should prevent multiple
755 // access to the user directory !
756 // First we try to create our pipe if this fails we try to connect. We have to do this
757 // in a loop because the other office can crash or shutdown between createPipe
758 // and connectPipe!!
759 auto aUserInstallPathHashCode = CreateMD5FromString(aUserInstallPath);
761 // Check result to create a hash code from the user install path
762 if ( aUserInstallPathHashCode.isEmpty() )
763 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR; // Something completely broken, we cannot create a valid hash code!
765 osl::Pipe pipe;
766 enum PipeMode
768 PIPEMODE_DONTKNOW,
769 PIPEMODE_CREATED,
770 PIPEMODE_CONNECTED
772 PipeMode nPipeMode = PIPEMODE_DONTKNOW;
774 OUString aPipeIdent( "SingleOfficeIPC_" + aUserInstallPathHashCode );
777 osl::Security security;
779 // Try to create pipe
780 if ( pipe.create( aPipeIdent, osl_Pipe_CREATE, security ))
782 // Pipe created
783 nPipeMode = PIPEMODE_CREATED;
785 else if( pipe.create( aPipeIdent, osl_Pipe_OPEN, security )) // Creation not successful, now we try to connect
787 osl::StreamPipe aStreamPipe(pipe.getHandle());
788 if (readStringFromPipe(aStreamPipe) == SEND_ARGUMENTS)
790 // Pipe connected to first office
791 nPipeMode = PIPEMODE_CONNECTED;
793 else
795 // Pipe connection failed (other office exited or crashed)
796 std::this_thread::sleep_for( std::chrono::milliseconds(500) );
799 else
801 oslPipeError eReason = pipe.getError();
802 if ((eReason == osl_Pipe_E_ConnectionRefused) || (eReason == osl_Pipe_E_invalidError))
803 return RequestHandler::IPC_STATUS_PIPE_ERROR;
805 // Wait for second office to be ready
806 std::this_thread::sleep_for( std::chrono::milliseconds(10) );
809 } while ( nPipeMode == PIPEMODE_DONTKNOW );
811 if ( nPipeMode == PIPEMODE_CREATED )
813 // Seems we are the one and only, so create listening thread
814 *thread = new PipeIpcThread(pipe);
815 return RequestHandler::IPC_STATUS_OK;
817 else
819 // Seems another office is running. Pipe arguments to it and self terminate
820 osl::StreamPipe aStreamPipe(pipe.getHandle());
822 OStringBuffer aArguments(ARGUMENT_PREFIX);
823 OUString cwdUrl;
824 if (!(utl::Bootstrap::getProcessWorkingDir(cwdUrl) &&
825 addArgument(aArguments, '1', cwdUrl)))
827 aArguments.append('0');
829 sal_uInt32 nCount = rtl_getAppCommandArgCount();
830 for( sal_uInt32 i=0; i < nCount; i++ )
832 rtl_getAppCommandArg( i, &aUserInstallPath.pData );
833 if (!addArgument(aArguments, ',', aUserInstallPath)) {
834 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
837 aArguments.append('\0');
838 // finally, write the string onto the pipe
839 SAL_INFO("desktop.app", "writing <" << aArguments.getStr() << ">");
840 sal_Int32 n = aStreamPipe.write(
841 aArguments.getStr(), aArguments.getLength());
842 if (n != aArguments.getLength()) {
843 SAL_INFO("desktop.app", "short write: " << n);
844 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
847 if (readStringFromPipe(aStreamPipe) != PROCESSING_DONE)
849 // something went wrong
850 return RequestHandler::IPC_STATUS_BOOTSTRAP_ERROR;
853 return RequestHandler::IPC_STATUS_2ND_OFFICE;
857 void RequestHandler::Disable()
859 osl::ClearableMutexGuard aMutex( GetMutex() );
861 if( !pGlobal.is() )
862 return;
864 rtl::Reference< RequestHandler > handler(pGlobal);
865 pGlobal.clear();
867 handler->mState = State::Downing;
868 if (handler->mIpcThread.is()) {
869 handler->mIpcThread->close();
872 // release mutex to avoid deadlocks
873 aMutex.clear();
875 handler->cReady.set();
877 // exit gracefully and join
878 if (handler->mIpcThread.is())
880 handler->mIpcThread->join();
881 handler->mIpcThread.clear();
884 handler->cReady.reset();
887 RequestHandler::RequestHandler() :
888 mState( State::Starting ),
889 mnPendingRequests( 0 )
893 RequestHandler::~RequestHandler()
895 assert(!mIpcThread.is());
898 void RequestHandler::SetReady(bool bIsReady)
900 osl::MutexGuard g(GetMutex());
901 if (pGlobal.is())
903 if (bIsReady)
904 pGlobal->cReady.set();
905 else
906 pGlobal->cReady.reset();
910 void RequestHandler::WaitForReady()
912 rtl::Reference<RequestHandler> t;
914 osl::MutexGuard g(GetMutex());
915 t = pGlobal;
917 if (t.is())
919 t->cReady.wait();
923 bool IpcThread::process(OString const & arguments, bool * waitProcessed) {
924 assert(waitProcessed != nullptr);
926 std::unique_ptr< CommandLineArgs > aCmdLineArgs;
929 Parser p(arguments);
930 aCmdLineArgs.reset( new CommandLineArgs( p ) );
932 catch ( const CommandLineArgs::Supplier::Exception & )
934 SAL_WARN("desktop.app", "Error in received command line arguments");
935 return false;
938 bool bDocRequestSent = false;
940 OUString aUnknown( aCmdLineArgs->GetUnknown() );
941 if (aUnknown.isEmpty() && !aCmdLineArgs->IsHelp() && !aCmdLineArgs->IsVersion())
943 const CommandLineArgs &rCurrentCmdLineArgs = Desktop::GetCommandLineArgs();
945 if ( aCmdLineArgs->IsQuickstart() )
947 // we have to use application event, because we have to start quickstart service in main thread!!
948 ApplicationEvent* pAppEvent =
949 new ApplicationEvent(ApplicationEvent::Type::QuickStart);
950 ImplPostForeignAppEvent( pAppEvent );
953 // handle request for acceptor
954 std::vector< OUString > const & accept = aCmdLineArgs->GetAccept();
955 for (auto const& elem : accept)
957 ApplicationEvent* pAppEvent = new ApplicationEvent(
958 ApplicationEvent::Type::Accept, elem);
959 ImplPostForeignAppEvent( pAppEvent );
961 // handle acceptor removal
962 std::vector< OUString > const & unaccept = aCmdLineArgs->GetUnaccept();
963 for (auto const& elem : unaccept)
965 ApplicationEvent* pAppEvent = new ApplicationEvent(
966 ApplicationEvent::Type::Unaccept, elem);
967 ImplPostForeignAppEvent( pAppEvent );
970 std::unique_ptr<ProcessDocumentsRequest> pRequest(new ProcessDocumentsRequest(
971 aCmdLineArgs->getCwdUrl()));
972 m_handler->cProcessed.reset();
973 pRequest->pcProcessed = &m_handler->cProcessed;
974 m_handler->mbSuccess = false;
975 pRequest->mpbSuccess = &m_handler->mbSuccess;
977 // Print requests are not dependent on the --invisible cmdline argument as they are
978 // loaded with the "hidden" flag! So they are always checked.
979 pRequest->aPrintList = aCmdLineArgs->GetPrintList();
980 bDocRequestSent |= !pRequest->aPrintList.empty();
981 pRequest->aPrintToList = aCmdLineArgs->GetPrintToList();
982 pRequest->aPrinterName = aCmdLineArgs->GetPrinterName();
983 bDocRequestSent |= !( pRequest->aPrintToList.empty() || pRequest->aPrinterName.isEmpty() );
984 pRequest->aConversionList = aCmdLineArgs->GetConversionList();
985 pRequest->aConversionParams = aCmdLineArgs->GetConversionParams();
986 pRequest->aConversionOut = aCmdLineArgs->GetConversionOut();
987 pRequest->aImageConversionType = aCmdLineArgs->GetImageConversionType();
988 pRequest->aInFilter = aCmdLineArgs->GetInFilter();
989 pRequest->bTextCat = aCmdLineArgs->IsTextCat();
990 pRequest->bScriptCat = aCmdLineArgs->IsScriptCat();
991 bDocRequestSent |= !pRequest->aConversionList.empty();
993 if ( !rCurrentCmdLineArgs.IsInvisible() )
995 // Read cmdline args that can open/create documents. As they would open a window
996 // they are only allowed if the "--invisible" is currently not used!
997 pRequest->aOpenList = aCmdLineArgs->GetOpenList();
998 bDocRequestSent |= !pRequest->aOpenList.empty();
999 pRequest->aViewList = aCmdLineArgs->GetViewList();
1000 bDocRequestSent |= !pRequest->aViewList.empty();
1001 pRequest->aStartList = aCmdLineArgs->GetStartList();
1002 bDocRequestSent |= !pRequest->aStartList.empty();
1003 pRequest->aForceOpenList = aCmdLineArgs->GetForceOpenList();
1004 bDocRequestSent |= !pRequest->aForceOpenList.empty();
1005 pRequest->aForceNewList = aCmdLineArgs->GetForceNewList();
1006 bDocRequestSent |= !pRequest->aForceNewList.empty();
1008 // Special command line args to create an empty document for a given module
1010 // #i18338# (lo)
1011 // we only do this if no document was specified on the command line,
1012 // since this would be inconsistent with the behaviour of
1013 // the first process, see OpenClients() (call to OpenDefault()) in app.cxx
1014 if ( aCmdLineArgs->HasModuleParam() && !bDocRequestSent )
1016 SvtModuleOptions aOpt;
1017 SvtModuleOptions::EFactory eFactory = SvtModuleOptions::EFactory::WRITER;
1018 if ( aCmdLineArgs->IsWriter() )
1019 eFactory = SvtModuleOptions::EFactory::WRITER;
1020 else if ( aCmdLineArgs->IsCalc() )
1021 eFactory = SvtModuleOptions::EFactory::CALC;
1022 else if ( aCmdLineArgs->IsDraw() )
1023 eFactory = SvtModuleOptions::EFactory::DRAW;
1024 else if ( aCmdLineArgs->IsImpress() )
1025 eFactory = SvtModuleOptions::EFactory::IMPRESS;
1026 else if ( aCmdLineArgs->IsBase() )
1027 eFactory = SvtModuleOptions::EFactory::DATABASE;
1028 else if ( aCmdLineArgs->IsMath() )
1029 eFactory = SvtModuleOptions::EFactory::MATH;
1030 else if ( aCmdLineArgs->IsGlobal() )
1031 eFactory = SvtModuleOptions::EFactory::WRITERGLOBAL;
1032 else if ( aCmdLineArgs->IsWeb() )
1033 eFactory = SvtModuleOptions::EFactory::WRITERWEB;
1035 if ( !pRequest->aOpenList.empty() )
1036 pRequest->aModule = aOpt.GetFactoryName( eFactory );
1037 else
1038 pRequest->aOpenList.push_back( aOpt.GetFactoryEmptyDocumentURL( eFactory ) );
1039 bDocRequestSent = true;
1043 if ( !aCmdLineArgs->IsQuickstart() ) {
1044 bool bShowHelp = false;
1045 OUStringBuffer aHelpURLBuffer;
1046 if (aCmdLineArgs->IsHelpWriter()) {
1047 bShowHelp = true;
1048 aHelpURLBuffer.append("vnd.sun.star.help://swriter/start");
1049 } else if (aCmdLineArgs->IsHelpCalc()) {
1050 bShowHelp = true;
1051 aHelpURLBuffer.append("vnd.sun.star.help://scalc/start");
1052 } else if (aCmdLineArgs->IsHelpDraw()) {
1053 bShowHelp = true;
1054 aHelpURLBuffer.append("vnd.sun.star.help://sdraw/start");
1055 } else if (aCmdLineArgs->IsHelpImpress()) {
1056 bShowHelp = true;
1057 aHelpURLBuffer.append("vnd.sun.star.help://simpress/start");
1058 } else if (aCmdLineArgs->IsHelpBase()) {
1059 bShowHelp = true;
1060 aHelpURLBuffer.append("vnd.sun.star.help://sdatabase/start");
1061 } else if (aCmdLineArgs->IsHelpBasic()) {
1062 bShowHelp = true;
1063 aHelpURLBuffer.append("vnd.sun.star.help://sbasic/start");
1064 } else if (aCmdLineArgs->IsHelpMath()) {
1065 bShowHelp = true;
1066 aHelpURLBuffer.append("vnd.sun.star.help://smath/start");
1068 if (bShowHelp) {
1069 aHelpURLBuffer.append("?Language="
1070 + utl::ConfigManager::getUILocale()
1071 #if defined UNX
1072 + "&System=UNX");
1073 #elif defined _WIN32
1074 + "&System=WIN");
1075 #endif
1076 ApplicationEvent* pAppEvent = new ApplicationEvent(
1077 ApplicationEvent::Type::OpenHelpUrl,
1078 aHelpURLBuffer.makeStringAndClear());
1079 ImplPostForeignAppEvent( pAppEvent );
1083 if ( bDocRequestSent )
1085 // Send requests to dispatch watcher if we have at least one. The receiver
1086 // is responsible to delete the request after processing it.
1087 if ( aCmdLineArgs->HasModuleParam() )
1089 SvtModuleOptions aOpt;
1091 // Support command line parameters to start a module (as preselection)
1092 if ( aCmdLineArgs->IsWriter() && aOpt.IsModuleInstalled( SvtModuleOptions::EModule::WRITER ) )
1093 pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::EFactory::WRITER );
1094 else if ( aCmdLineArgs->IsCalc() && aOpt.IsModuleInstalled( SvtModuleOptions::EModule::CALC ) )
1095 pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::EFactory::CALC );
1096 else if ( aCmdLineArgs->IsImpress() && aOpt.IsModuleInstalled( SvtModuleOptions::EModule::IMPRESS ) )
1097 pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::EFactory::IMPRESS );
1098 else if ( aCmdLineArgs->IsDraw() && aOpt.IsModuleInstalled( SvtModuleOptions::EModule::DRAW ) )
1099 pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::EFactory::DRAW );
1102 ImplPostProcessDocumentsEvent( std::move(pRequest) );
1104 else
1106 // delete not used request again
1107 pRequest.reset();
1109 if (aCmdLineArgs->IsEmpty())
1111 // no document was sent, just bring Office to front
1112 ApplicationEvent* pAppEvent =
1113 new ApplicationEvent(ApplicationEvent::Type::Appear);
1114 ImplPostForeignAppEvent( pAppEvent );
1117 *waitProcessed = bDocRequestSent;
1118 return true;
1121 void PipeIpcThread::execute()
1123 assert(m_handler != nullptr);
1126 osl::StreamPipe aStreamPipe;
1127 oslPipeError nError = pipe_.accept( aStreamPipe );
1130 if( nError == osl_Pipe_E_None )
1132 // if we receive a request while the office is displaying some dialog or error during
1133 // bootstrap, that dialogs event loop might get events that are dispatched by this thread
1134 // we have to wait for cReady to be set by the real main loop.
1135 // only requests that don't dispatch events may be processed before cReady is set.
1136 m_handler->cReady.wait();
1138 // we might have decided to shutdown while we were sleeping
1139 if (!RequestHandler::pGlobal.is()) return;
1141 // only lock the mutex when processing starts, otherwise we deadlock when the office goes
1142 // down during wait
1143 osl::ClearableMutexGuard aGuard( RequestHandler::GetMutex() );
1145 if (m_handler->mState == RequestHandler::State::Downing)
1147 break;
1150 // notify client we're ready to process its args:
1151 SAL_INFO("desktop.app", "writing <" << SEND_ARGUMENTS << ">");
1152 std::size_t n = aStreamPipe.write(
1153 SEND_ARGUMENTS, std::size(SEND_ARGUMENTS));
1154 // incl. terminating NUL
1155 if (n != std::size(SEND_ARGUMENTS)) {
1156 SAL_WARN("desktop.app", "short write: " << n);
1157 continue;
1160 OString aArguments = readStringFromPipe(aStreamPipe);
1162 // Is this a lookup message from another application? if so, ignore
1163 if (aArguments.isEmpty())
1164 continue;
1166 bool waitProcessed = false;
1167 if (!process(aArguments, &waitProcessed)) {
1168 continue;
1171 // we don't need the mutex any longer...
1172 aGuard.clear();
1173 bool bSuccess = true;
1174 // wait for processing to finish
1175 if (waitProcessed)
1177 m_handler->cProcessed.wait();
1178 bSuccess = m_handler->mbSuccess;
1180 if (bSuccess)
1182 // processing finished, inform the requesting end:
1183 SAL_INFO("desktop.app", "writing <" << PROCESSING_DONE << ">");
1184 n = aStreamPipe.write(PROCESSING_DONE, std::size(PROCESSING_DONE));
1185 // incl. terminating NUL
1186 if (n != std::size(PROCESSING_DONE))
1188 SAL_WARN("desktop.app", "short write: " << n);
1189 continue;
1193 else
1196 osl::MutexGuard aGuard( RequestHandler::GetMutex() );
1197 if (m_handler->mState == RequestHandler::State::Downing)
1199 break;
1203 SAL_WARN( "desktop.app", "Error on accept: " << static_cast<int>(nError));
1204 std::this_thread::sleep_for( std::chrono::seconds(1) );
1206 } while( schedule() );
1209 static void AddToDispatchList(
1210 std::vector<DispatchWatcher::DispatchRequest>& rDispatchList,
1211 std::optional< OUString > const & cwdUrl,
1212 std::vector< OUString > const & aRequestList,
1213 DispatchWatcher::RequestType nType,
1214 const OUString& aParam,
1215 const OUString& aFactory )
1217 for (auto const& request : aRequestList)
1219 rDispatchList.push_back({nType, request, cwdUrl, aParam, aFactory});
1223 static void AddConversionsToDispatchList(
1224 std::vector<DispatchWatcher::DispatchRequest>& rDispatchList,
1225 std::optional< OUString > const & cwdUrl,
1226 std::vector< OUString > const & rRequestList,
1227 const OUString& rParam,
1228 const OUString& rPrinterName,
1229 const OUString& rFactory,
1230 const OUString& rParamOut,
1231 std::u16string_view rImgOut,
1232 const bool isTextCat,
1233 const bool isScriptCat )
1235 DispatchWatcher::RequestType nType;
1236 OUString aParam( rParam );
1238 if( !rParam.isEmpty() )
1240 if ( isTextCat )
1241 nType = DispatchWatcher::REQUEST_CAT;
1242 else
1243 nType = DispatchWatcher::REQUEST_CONVERSION;
1245 else
1247 if ( isScriptCat )
1248 nType = DispatchWatcher::REQUEST_SCRIPT_CAT;
1249 else
1251 nType = DispatchWatcher::REQUEST_BATCHPRINT;
1252 aParam = rPrinterName;
1256 OUString aPWD;
1257 if (cwdUrl)
1259 aPWD = *cwdUrl;
1261 else
1263 utl::Bootstrap::getProcessWorkingDir( aPWD );
1266 if (OUString aOutDir(rParamOut.trim()); !aOutDir.isEmpty())
1268 if (osl::FileBase::getAbsoluteFileURL(aPWD, rParamOut, aOutDir) == osl::FileBase::E_None)
1269 osl::FileBase::getSystemPathFromFileURL(aOutDir, aOutDir);
1270 aParam += ";" + aOutDir;
1272 else
1274 ::osl::FileBase::getSystemPathFromFileURL( aPWD, aPWD );
1275 aParam += ";" + aPWD;
1278 if( !rImgOut.empty() )
1279 aParam += OUString::Concat("|") + o3tl::trim(rImgOut);
1281 for (auto const& request : rRequestList)
1283 rDispatchList.push_back({nType, request, cwdUrl, aParam, rFactory});
1287 namespace {
1289 struct ConditionSetGuard
1291 osl::Condition* m_pCondition;
1292 ConditionSetGuard(osl::Condition* pCondition) : m_pCondition(pCondition) {}
1293 ~ConditionSetGuard() { if (m_pCondition) m_pCondition->set(); }
1298 bool RequestHandler::ExecuteCmdLineRequests(
1299 ProcessDocumentsRequest& aRequest, bool noTerminate)
1301 // protect the dispatch list
1302 osl::ClearableMutexGuard aGuard( GetMutex() );
1304 // ensure that Processed flag (if exists) is signaled in any outcome
1305 ConditionSetGuard aSetGuard(aRequest.pcProcessed);
1307 static std::vector<DispatchWatcher::DispatchRequest> aDispatchList;
1309 // Create dispatch list for dispatch watcher
1310 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aInFilter, DispatchWatcher::REQUEST_INFILTER, "", aRequest.aModule );
1311 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aOpenList, DispatchWatcher::REQUEST_OPEN, "", aRequest.aModule );
1312 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aViewList, DispatchWatcher::REQUEST_VIEW, "", aRequest.aModule );
1313 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aStartList, DispatchWatcher::REQUEST_START, "", aRequest.aModule );
1314 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintList, DispatchWatcher::REQUEST_PRINT, "", aRequest.aModule );
1315 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintToList, DispatchWatcher::REQUEST_PRINTTO, aRequest.aPrinterName, aRequest.aModule );
1316 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceOpenList, DispatchWatcher::REQUEST_FORCEOPEN, "", aRequest.aModule );
1317 AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceNewList, DispatchWatcher::REQUEST_FORCENEW, "", aRequest.aModule );
1318 AddConversionsToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aConversionList, aRequest.aConversionParams, aRequest.aPrinterName, aRequest.aModule, aRequest.aConversionOut, aRequest.aImageConversionType, aRequest.bTextCat, aRequest.bScriptCat );
1319 bool bShutdown( false );
1321 if ( pGlobal.is() )
1323 if( ! pGlobal->AreRequestsEnabled() )
1325 // Either starting, or downing - do not process the request, just try to bring Office to front
1326 ApplicationEvent* pAppEvent =
1327 new ApplicationEvent(ApplicationEvent::Type::Appear);
1328 ImplPostForeignAppEvent(pAppEvent);
1329 return bShutdown;
1332 pGlobal->mnPendingRequests += aDispatchList.size();
1333 if ( !pGlobal->mpDispatchWatcher.is() )
1335 pGlobal->mpDispatchWatcher = new DispatchWatcher;
1337 rtl::Reference<DispatchWatcher> dispatchWatcher(
1338 pGlobal->mpDispatchWatcher);
1340 // copy for execute
1341 std::vector<DispatchWatcher::DispatchRequest> aTempList;
1342 aTempList.swap( aDispatchList );
1344 aGuard.clear();
1346 // Execute dispatch requests
1347 bShutdown = dispatchWatcher->executeDispatchRequests( aTempList, noTerminate);
1348 if (aRequest.mpbSuccess)
1349 *aRequest.mpbSuccess = true; // signal that we have actually succeeded
1352 return bShutdown;
1357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */