Line data Source code
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 :
21 : #include "deployment.hrc"
22 : #include "unopkg_shared.h"
23 : #include "dp_identifier.hxx"
24 : #include "../../deployment/gui/dp_gui.hrc"
25 : #include "lockfile.hxx"
26 : #include "vcl/svapp.hxx"
27 : #include "vcl/msgbox.hxx"
28 : #include "rtl/bootstrap.hxx"
29 : #include "rtl/strbuf.hxx"
30 : #include "rtl/ustrbuf.hxx"
31 : #include "osl/process.h"
32 : #include "osl/file.hxx"
33 : #include "osl/thread.hxx"
34 : #include "tools/getprocessworkingdir.hxx"
35 : #include "comphelper/processfactory.hxx"
36 : #include "unotools/configmgr.hxx"
37 : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
38 : #include "com/sun/star/ucb/UniversalContentBroker.hpp"
39 : #include "cppuhelper/bootstrap.hxx"
40 : #include "comphelper/sequence.hxx"
41 : #include <stdio.h>
42 :
43 : using ::rtl::OUString;
44 : using ::rtl::OString;
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::ucb;
48 :
49 : namespace unopkg {
50 :
51 : bool getLockFilePath(OUString & out);
52 :
53 0 : ::rtl::OUString toString( OptionInfo const * info )
54 : {
55 : OSL_ASSERT( info != 0 );
56 0 : ::rtl::OUStringBuffer buf;
57 0 : buf.appendAscii("--");
58 0 : buf.appendAscii(info->m_name);
59 0 : if (info->m_short_option != '\0')
60 : {
61 0 : buf.appendAscii(" (short -" );
62 0 : buf.append(info->m_short_option );
63 0 : buf.appendAscii(")");
64 : }
65 0 : if (info->m_has_argument)
66 0 : buf.appendAscii(" <argument>" );
67 0 : return buf.makeStringAndClear();
68 : }
69 :
70 : //==============================================================================
71 0 : OptionInfo const * getOptionInfo(
72 : OptionInfo const * list,
73 : OUString const & opt, sal_Unicode copt )
74 : {
75 0 : for ( ; list->m_name != 0; ++list )
76 : {
77 0 : OptionInfo const & option_info = *list;
78 0 : if (!opt.isEmpty())
79 : {
80 0 : if (opt.equalsAsciiL(
81 0 : option_info.m_name, option_info.m_name_length ) &&
82 : (copt == '\0' || copt == option_info.m_short_option))
83 : {
84 0 : return &option_info;
85 : }
86 : }
87 : else
88 : {
89 : OSL_ASSERT( copt != '\0' );
90 0 : if (copt == option_info.m_short_option)
91 : {
92 0 : return &option_info;
93 : }
94 : }
95 : }
96 : OSL_FAIL( ::rtl::OUStringToOString(
97 : opt, osl_getThreadTextEncoding() ).getStr() );
98 0 : return 0;
99 : }
100 :
101 : //==============================================================================
102 0 : bool isOption( OptionInfo const * option_info, sal_uInt32 * pIndex )
103 : {
104 : OSL_ASSERT( option_info != 0 );
105 0 : if (osl_getCommandArgCount() <= *pIndex)
106 0 : return false;
107 :
108 0 : OUString arg;
109 0 : osl_getCommandArg( *pIndex, &arg.pData );
110 0 : sal_Int32 len = arg.getLength();
111 :
112 0 : if (len < 2 || arg[ 0 ] != '-')
113 0 : return false;
114 :
115 0 : if (len == 2 && arg[ 1 ] == option_info->m_short_option)
116 : {
117 0 : ++(*pIndex);
118 : dp_misc::TRACE(OUSTR(__FILE__": identified option \'")
119 0 : + OUSTR("\'") + OUString( option_info->m_short_option ) + OUSTR("\n"));
120 0 : return true;
121 : }
122 0 : if (arg[ 1 ] == '-' && rtl_ustr_ascii_compare(
123 0 : arg.pData->buffer + 2, option_info->m_name ) == 0)
124 : {
125 0 : ++(*pIndex);
126 : dp_misc::TRACE(OUSTR( __FILE__": identified option \'")
127 0 : + OUString::createFromAscii(option_info->m_name) + OUSTR("\'\n"));
128 0 : return true;
129 : }
130 0 : return false;
131 : }
132 : //==============================================================================
133 :
134 0 : bool isBootstrapVariable(sal_uInt32 * pIndex)
135 : {
136 : OSL_ASSERT(osl_getCommandArgCount() >= *pIndex);
137 :
138 0 : OUString arg;
139 0 : osl_getCommandArg(*pIndex, &arg.pData);
140 0 : if (arg.matchAsciiL("-env:", 5))
141 : {
142 0 : ++(*pIndex);
143 0 : return true;
144 : }
145 0 : return false;
146 : }
147 :
148 : //==============================================================================
149 0 : bool readArgument(
150 : OUString * pValue, OptionInfo const * option_info, sal_uInt32 * pIndex )
151 : {
152 0 : if (isOption( option_info, pIndex ))
153 : {
154 0 : if (*pIndex < osl_getCommandArgCount())
155 : {
156 : OSL_ASSERT( pValue != 0 );
157 0 : osl_getCommandArg( *pIndex, &pValue->pData );
158 : dp_misc::TRACE(OUSTR( __FILE__": argument value: ")
159 0 : + *pValue + OUSTR("\n"));
160 0 : ++(*pIndex);
161 0 : return true;
162 : }
163 0 : --(*pIndex);
164 : }
165 0 : return false;
166 : }
167 :
168 :
169 : namespace {
170 : struct ExecutableDir : public rtl::StaticWithInit<
171 : OUString, ExecutableDir> {
172 0 : const OUString operator () () {
173 0 : OUString path;
174 0 : if (osl_getExecutableFile( &path.pData ) != osl_Process_E_None) {
175 : throw RuntimeException(
176 0 : OUSTR("cannot locate executable directory!"),0 );
177 : }
178 0 : return path.copy( 0, path.lastIndexOf( '/' ) );
179 : }
180 : };
181 : struct ProcessWorkingDir : public rtl::StaticWithInit<
182 : OUString, ProcessWorkingDir> {
183 0 : const OUString operator () () {
184 0 : OUString workingDir;
185 0 : tools::getProcessWorkingDir(workingDir);
186 0 : return workingDir;
187 : }
188 : };
189 : } // anon namespace
190 :
191 : //==============================================================================
192 0 : OUString const & getExecutableDir()
193 : {
194 0 : return ExecutableDir::get();
195 : }
196 :
197 : //==============================================================================
198 0 : OUString const & getProcessWorkingDir()
199 : {
200 0 : return ProcessWorkingDir::get();
201 : }
202 :
203 : //==============================================================================
204 0 : OUString makeAbsoluteFileUrl(
205 : OUString const & sys_path, OUString const & base_url, bool throw_exc )
206 : {
207 : // system path to file url
208 0 : OUString file_url;
209 0 : oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData );
210 0 : if ( rc != osl_File_E_None) {
211 0 : OUString tempPath;
212 0 : if ( osl_getSystemPathFromFileURL( sys_path.pData, &tempPath.pData) == osl_File_E_None )
213 : {
214 0 : file_url = sys_path;
215 : }
216 0 : else if (throw_exc)
217 : {
218 : throw RuntimeException(
219 : OUSTR("cannot get file url from system path: ") +
220 0 : sys_path, Reference< XInterface >() );
221 0 : }
222 : }
223 :
224 0 : OUString abs;
225 0 : if (osl_getAbsoluteFileURL(
226 0 : base_url.pData, file_url.pData, &abs.pData ) != osl_File_E_None)
227 : {
228 0 : if (throw_exc) {
229 0 : ::rtl::OUStringBuffer buf;
230 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
231 0 : "making absolute file url failed: \"") );
232 0 : buf.append( base_url );
233 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
234 0 : "\" (base-url) and \"") );
235 0 : buf.append( file_url );
236 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" (file-url)!") );
237 : throw RuntimeException(
238 0 : buf.makeStringAndClear(), Reference< XInterface >() );
239 : }
240 0 : return OUString();
241 : }
242 0 : return abs[ abs.getLength() -1 ] == '/'
243 0 : ? abs.copy( 0, abs.getLength() -1 ) : abs;
244 : }
245 :
246 :
247 : namespace {
248 :
249 : //------------------------------------------------------------------------------
250 0 : inline void printf_space( sal_Int32 space )
251 : {
252 0 : while (space--)
253 0 : dp_misc::writeConsole(" ");
254 0 : }
255 :
256 : //------------------------------------------------------------------------------
257 0 : void printf_line(
258 : OUString const & name, OUString const & value, sal_Int32 level )
259 : {
260 0 : printf_space( level );
261 0 : dp_misc::writeConsole(name + OUSTR(": ") + value + OUSTR("\n"));
262 0 : }
263 :
264 : //------------------------------------------------------------------------------
265 0 : void printf_package(
266 : Reference<deployment::XPackage> const & xPackage,
267 : Reference<XCommandEnvironment> const & xCmdEnv, sal_Int32 level )
268 : {
269 : beans::Optional< OUString > id(
270 : level == 0
271 : ? beans::Optional< OUString >(
272 : true, dp_misc::getIdentifier( xPackage ) )
273 0 : : xPackage->getIdentifier() );
274 0 : if (id.IsPresent)
275 0 : printf_line( OUSTR("Identifier"), id.Value, level );
276 0 : OUString version(xPackage->getVersion());
277 0 : if (!version.isEmpty())
278 0 : printf_line( OUSTR("Version"), version, level + 1 );
279 0 : printf_line( OUSTR("URL"), xPackage->getURL(), level + 1 );
280 :
281 : beans::Optional< beans::Ambiguous<sal_Bool> > option(
282 0 : xPackage->isRegistered( Reference<task::XAbortChannel>(), xCmdEnv ) );
283 0 : OUString value;
284 0 : if (option.IsPresent) {
285 0 : beans::Ambiguous<sal_Bool> const & reg = option.Value;
286 0 : if (reg.IsAmbiguous)
287 0 : value = OUSTR("unknown");
288 : else
289 0 : value = reg.Value ? OUSTR("yes") : OUSTR("no");
290 : }
291 : else
292 0 : value = OUSTR("n/a");
293 0 : printf_line( OUSTR("is registered"), value, level + 1 );
294 :
295 : const Reference<deployment::XPackageTypeInfo> xPackageType(
296 0 : xPackage->getPackageType() );
297 : OSL_ASSERT( xPackageType.is() );
298 0 : if (xPackageType.is()) {
299 : printf_line( OUSTR("Media-Type"),
300 0 : xPackageType->getMediaType(), level + 1 );
301 : }
302 0 : printf_line( OUSTR("Description"), xPackage->getDescription(), level + 1 );
303 0 : if (xPackage->isBundle()) {
304 : Sequence< Reference<deployment::XPackage> > seq(
305 0 : xPackage->getBundle( Reference<task::XAbortChannel>(), xCmdEnv ) );
306 0 : printf_space( level + 1 );
307 0 : dp_misc::writeConsole("bundled Packages: {\n");
308 0 : ::std::vector<Reference<deployment::XPackage> >vec_bundle;
309 0 : ::comphelper::sequenceToContainer(vec_bundle, seq);
310 : printf_packages( vec_bundle, ::std::vector<bool>(vec_bundle.size()),
311 0 : xCmdEnv, level + 2 );
312 0 : printf_space( level + 1 );
313 0 : dp_misc::writeConsole("}\n");
314 0 : }
315 0 : }
316 :
317 : } // anon namespace
318 :
319 0 : void printf_unaccepted_licenses(
320 : Reference<deployment::XPackage> const & ext)
321 : {
322 : OUString id(
323 0 : dp_misc::getIdentifier(ext) );
324 0 : printf_line( OUSTR("Identifier"), id, 0 );
325 0 : printf_space(1);
326 0 : dp_misc::writeConsole(OUSTR("License not accepted\n\n"));
327 0 : }
328 :
329 : //==============================================================================
330 0 : void printf_packages(
331 : ::std::vector< Reference<deployment::XPackage> > const & allExtensions,
332 : ::std::vector<bool> const & vecUnaccepted,
333 : Reference<XCommandEnvironment> const & xCmdEnv, sal_Int32 level )
334 : {
335 : OSL_ASSERT(allExtensions.size() == vecUnaccepted.size());
336 :
337 0 : if (allExtensions.empty())
338 : {
339 0 : printf_space( level );
340 0 : dp_misc::writeConsole("<none>\n");
341 : }
342 : else
343 : {
344 : typedef ::std::vector< Reference<deployment::XPackage> >::const_iterator I_EXT;
345 0 : int index = 0;
346 0 : for (I_EXT i = allExtensions.begin(); i != allExtensions.end(); ++i, ++index)
347 : {
348 0 : if (vecUnaccepted[index])
349 0 : printf_unaccepted_licenses(*i);
350 : else
351 0 : printf_package( *i, xCmdEnv, level );
352 0 : dp_misc::writeConsole(OUSTR("\n"));
353 : }
354 : }
355 0 : }
356 :
357 :
358 :
359 : namespace {
360 :
361 : //------------------------------------------------------------------------------
362 0 : Reference<XComponentContext> bootstrapStandAlone()
363 : {
364 : Reference<XComponentContext> xContext =
365 0 : ::cppu::defaultBootstrap_InitialComponentContext();
366 :
367 : Reference<lang::XMultiServiceFactory> xServiceManager(
368 0 : xContext->getServiceManager(), UNO_QUERY_THROW );
369 : // set global process service factory used by unotools config helpers
370 0 : ::comphelper::setProcessServiceFactory( xServiceManager );
371 :
372 : // Initialize the UCB (for backwards compatibility, in case some code still
373 : // uses plain createInstance w/o args directly to obtain an instance):
374 0 : UniversalContentBroker::create( xContext );
375 :
376 0 : return xContext;
377 : }
378 :
379 : //------------------------------------------------------------------------------
380 0 : Reference<XComponentContext> connectToOffice(
381 : Reference<XComponentContext> const & xLocalComponentContext,
382 : bool verbose )
383 : {
384 0 : Sequence<OUString> args( 3 );
385 0 : args[ 0 ] = OUSTR("--nologo");
386 0 : args[ 1 ] = OUSTR("--nodefault");
387 :
388 0 : OUString pipeId( ::dp_misc::generateRandomPipeId() );
389 0 : ::rtl::OUStringBuffer buf;
390 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("--accept=pipe,name=") );
391 0 : buf.append( pipeId );
392 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(";urp;") );
393 0 : args[ 2 ] = buf.makeStringAndClear();
394 0 : OUString appURL( getExecutableDir() + OUSTR("/soffice") );
395 :
396 0 : if (verbose)
397 : {
398 : dp_misc::writeConsole(
399 : OUSTR("Raising process: ") +
400 0 : appURL +
401 0 : OUSTR("\nArguments: --nologo --nodefault ") +
402 0 : args[2] +
403 0 : OUSTR("\n"));
404 : }
405 :
406 0 : ::dp_misc::raiseProcess( appURL, args );
407 :
408 0 : if (verbose)
409 0 : dp_misc::writeConsole("OK. Connecting...");
410 :
411 : OSL_ASSERT( buf.getLength() == 0 );
412 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("uno:pipe,name=") );
413 0 : buf.append( pipeId );
414 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
415 0 : ";urp;StarOffice.ComponentContext") );
416 : Reference<XComponentContext> xRet(
417 : ::dp_misc::resolveUnoURL(
418 : buf.makeStringAndClear(), xLocalComponentContext ),
419 0 : UNO_QUERY_THROW );
420 0 : if (verbose)
421 0 : dp_misc::writeConsole("OK.\n");
422 :
423 0 : return xRet;
424 : }
425 :
426 : } // anon namespace
427 :
428 : /** returns the path to the lock file used by unopkg.
429 : @return the path. An empty string signifies an error.
430 : */
431 0 : OUString getLockFilePath()
432 : {
433 0 : OUString ret;
434 0 : OUString sBootstrap(RTL_CONSTASCII_USTRINGPARAM("${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}"));
435 0 : rtl::Bootstrap::expandMacros(sBootstrap);
436 0 : OUString sAbs;
437 0 : if (::osl::File::E_None == ::osl::File::getAbsoluteFileURL(
438 0 : sBootstrap, OUSTR(".lock"), sAbs))
439 : {
440 0 : if (::osl::File::E_None ==
441 0 : ::osl::File::getSystemPathFromFileURL(sAbs, sBootstrap))
442 : {
443 0 : ret = sBootstrap;
444 : }
445 : }
446 :
447 0 : return ret;
448 : }
449 : //==============================================================================
450 0 : Reference<XComponentContext> getUNO(
451 : bool verbose, bool shared, bool bGui,
452 : Reference<XComponentContext> & out_localContext)
453 : {
454 : // do not create any user data (for the root user) in --shared mode:
455 0 : if (shared) {
456 : rtl::Bootstrap::set(
457 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CFG_CacheUrl")),
458 0 : rtl::OUString());
459 : }
460 :
461 : // hold lock during process runtime:
462 0 : static ::desktop::Lockfile s_lockfile( false /* no IPC server */ );
463 0 : Reference<XComponentContext> xComponentContext( bootstrapStandAlone() );
464 0 : out_localContext = xComponentContext;
465 0 : if (::dp_misc::office_is_running()) {
466 : xComponentContext.set(
467 0 : connectToOffice( xComponentContext, verbose ) );
468 : }
469 : else
470 : {
471 0 : if (! s_lockfile.check( 0 ))
472 : {
473 0 : String sMsg(ResId(RID_STR_CONCURRENTINSTANCE, *DeploymentResMgr::get()));
474 : //Create this string before we call DeInitVCL, because this will kill
475 : //the ResMgr
476 0 : String sError(ResId(RID_STR_UNOPKG_ERROR, *DeploymentResMgr::get()));
477 :
478 0 : sMsg = sMsg + OUSTR("\n") + getLockFilePath();
479 :
480 0 : if (bGui)
481 : {
482 : //We show a message box or print to the console that there
483 : //is another instance already running
484 0 : if ( ! InitVCL() )
485 : throw RuntimeException( OUSTR("Cannot initialize VCL!"),
486 0 : NULL );
487 : {
488 0 : WarningBox warn(NULL, WB_OK | WB_DEF_OK, sMsg);
489 0 : warn.SetText(utl::ConfigManager::getProductName());
490 0 : warn.SetIcon(0);
491 0 : warn.Execute();
492 : }
493 0 : DeInitVCL();
494 : }
495 :
496 : throw LockFileException(
497 0 : OUSTR("\n") + sError + sMsg + OUSTR("\n"));
498 : }
499 : }
500 :
501 0 : return xComponentContext;
502 : }
503 :
504 : }
505 :
506 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|