LibreOffice Module cppuhelper (master)  1
bootstrap.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <chrono>
23 #include <cstring>
24 
25 #include <rtl/bootstrap.hxx>
26 #include <rtl/random.h>
27 #include <rtl/ustrbuf.hxx>
28 #include <rtl/uri.hxx>
29 #include <osl/file.hxx>
30 #include <osl/security.hxx>
31 #include <osl/thread.hxx>
33 #include <osl/process.h>
34 
35 #include <cppuhelper/bootstrap.hxx>
37 
38 #include <com/sun/star/bridge/UnoUrlResolver.hpp>
39 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
40 
41 #include "macro_expander.hxx"
42 
43 namespace com :: sun :: star :: uno { class XComponentContext; }
44 
45 using namespace ::osl;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 
49 using rtl::Bootstrap;
50 
51 namespace cppu
52 {
53 
55 {
56 }
57 
58 BootstrapException::BootstrapException( const OUString & rMessage )
59  :m_aMessage( rMessage )
60 {
61 }
62 
64 {
66 }
67 
69 {
70 }
71 
73 {
75  return *this;
76 }
77 
78 const OUString & BootstrapException::getMessage() const
79 {
80  return m_aMessage;
81 }
82 
84 {
85  Reference< XComponentContext > xRemoteContext;
86 
87  try
88  {
90  if (p1 == nullptr) {
91  throw BootstrapException(
92  "no soffice installation found!");
93  }
94  OUString p2;
95 #if defined(_WIN32)
96  p2 = o3tl::toU(p1);
97  free(p1);
98 #else
99  bool bOk = rtl_convertStringToUString(
100  &p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
101  (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
102  RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
103  RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR));
104  free(p1);
105  if (!bOk)
106  {
107  throw BootstrapException(
108  "bad characters in soffice installation path!");
109  }
110 #endif
111  OUString path;
112  if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
113  osl::FileBase::E_None)
114  {
115  throw BootstrapException(
116  "cannot convert soffice installation path to URL!");
117  }
118  if (!path.isEmpty() && !path.endsWith("/")) {
119  path += "/";
120  }
121 
122  OUString uri;
123  if (!Bootstrap::get("URE_BOOTSTRAP", uri)) {
124  Bootstrap::set(
125  "URE_BOOTSTRAP",
126  Bootstrap::encode(
127  path +
128 #if defined MACOSX
129  "../Resources/"
130 #endif
131  SAL_CONFIGFILE("fundamental")));
132  }
133 
134  // create default local component context
135  Reference< XComponentContext > xLocalContext(
137  if ( !xLocalContext.is() )
138  throw BootstrapException( "no local component context!" );
139 
140  // create a random pipe name
141  rtlRandomPool hPool = rtl_random_createPool();
142  if ( hPool == nullptr )
143  throw BootstrapException( "cannot create random pool!" );
144  sal_uInt8 bytes[ 16 ];
145  if ( rtl_random_getBytes( hPool, bytes, SAL_N_ELEMENTS( bytes ) )
146  != rtl_Random_E_None )
147  throw BootstrapException( "random pool error!" );
148  rtl_random_destroyPool( hPool );
149  OUStringBuffer buf("uno");
150  for (unsigned char byte : bytes)
151  buf.append( static_cast< sal_Int32 >( byte ) );
152  OUString sPipeName( buf.makeStringAndClear() );
153 
154  // arguments
155  OUString args [] = {
156  OUString("--nologo"),
157  OUString("--nodefault"),
158  OUString("--norestore"),
159  OUString("--nolockcheck"),
160  OUString("--accept=pipe,name=" + sPipeName + ";urp;")
161  };
162  rtl_uString * ar_args [] = {
163  args[ 0 ].pData,
164  args[ 1 ].pData,
165  args[ 2 ].pData,
166  args[ 3 ].pData,
167  args[ 4 ].pData
168  };
169  ::osl::Security sec;
170 
171  // start office process
172  oslProcess hProcess = nullptr;
173  oslProcessError rc = osl_executeProcess(
174  OUString(path + "soffice").pData, ar_args, SAL_N_ELEMENTS( ar_args ),
175  osl_Process_DETACHED,
176  sec.getHandle(),
177  nullptr, // => current working dir
178  nullptr, 0, // => no env vars
179  &hProcess );
180  switch ( rc )
181  {
182  case osl_Process_E_None:
183  osl_freeProcessHandle( hProcess );
184  break;
185  case osl_Process_E_NotFound:
186  throw BootstrapException( "image not found!" );
187  case osl_Process_E_TimedOut:
188  throw BootstrapException( "timeout occurred!" );
189  case osl_Process_E_NoPermission:
190  throw BootstrapException( "permission denied!" );
191  case osl_Process_E_Unknown:
192  throw BootstrapException( "unknown error!" );
193  case osl_Process_E_InvalidError:
194  default:
195  throw BootstrapException( "unmapped error!" );
196  }
197 
198  // create a URL resolver
199  Reference< bridge::XUnoUrlResolver > xUrlResolver(
200  bridge::UnoUrlResolver::create( xLocalContext ) );
201 
202  // connection string
203  OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
204 
205  // wait until office is started
206  for ( ; ; )
207  {
208  try
209  {
210  // try to connect to office
211  xRemoteContext.set(
212  xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
213  break;
214  }
215  catch ( connection::NoConnectException & )
216  {
217  // wait 500 ms, then try to connect again
218  ::osl::Thread::wait( std::chrono::milliseconds(500) );
219  }
220  }
221  }
222  catch ( Exception & e )
223  {
224  throw BootstrapException(
225  "unexpected UNO exception caught: " + e.Message );
226  }
227 
228  return xRemoteContext;
229 }
230 
231 OUString bootstrap_expandUri(OUString const & uri) {
232  OUString rest;
233  return uri.startsWith("vnd.sun.star.expand:", &rest)
235  rtl::Uri::decode(
236  rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
237  : uri;
238 }
239 
240 } // namespace cppu
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ::rtl::OUString & getMessage() const
Gets the message.
Definition: bootstrap.cxx:78
std::unique_ptr< ContentProperties > pData
std::vector< sal_uInt8 > bytes
tuple args
::rtl::OUString m_aMessage
Definition: bootstrap.hxx:127
OUString bootstrap_expandUri(OUString const &uri)
Definition: bootstrap.cxx:231
An exception indicating a bootstrap error.
Definition: bootstrap.hxx:87
BootstrapException()
Constructs a BootstrapException.
Definition: bootstrap.cxx:54
BootstrapException & operator=(const BootstrapException &e)
Assigns a BootstrapException.
Definition: bootstrap.cxx:72
virtual ~BootstrapException()
Destructs a BootstrapException.
Definition: bootstrap.cxx:68
#define SAL_N_ELEMENTS(arr)
#define SAL_CONFIGFILE(name)
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
Bootstraps an initial component context with service manager upon information from bootstrap variable...
Reference< XComponentContext > SAL_CALL bootstrap()
Bootstraps the component context from a UNO installation.
Definition: bootstrap.cxx:83
void * rtlRandomPool
unsigned char byte
char * cppuhelper_detail_findSofficePath()
unsigned char sal_uInt8
OUString expandMacros(OUString const &text)
Helper function to expand macros based on the unorc/uno.ini.
exports com.sun.star. uri