Files
libreoffice/desktop/source/deployment/misc/lockfile.cxx

214 lines
6.4 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patch contributed by: Jurgen Schmidt remove onlineregistration with dependencies http://svn.apache.org/viewvc?view=revision&revision=1240245 imported patch package_eventlistener.patch http://svn.apache.org/viewvc?view=revision&revision=1172103 Patch contributed by Pedro Giffuni Accept Google Chrome OS fonts as equivalent to MS fonts. http://svn.apache.org/viewvc?view=revision&revision=1233155 http://svn.apache.org/viewvc?view=revision&revision=1233408 Patch contributed by Andre Fischer Do not add targets for junit tests when junit is disabled. http://svn.apache.org/viewvc?view=revision&revision=1241508 Patches contributed by Mathias Bauer (and others) gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 cws mba34issues01: #i114600#: remove forbidden characters from list of unencoded characters http://svn.apache.org/viewvc?view=revision&revision=1172370 Patches contributed by Oliver Rainer-Wittman some clean up in JPEGReader due to memory constraints http://svn.apache.org/viewvc?view=revision&revision=1299729 119114 - method <UpdateDialog::addSpecificError(..)> - create entry with correct type http://svn.apache.org/viewvc?view=revision&revision=1305265 Patches contributed by Ariel Constenla-Haile i118707 - make toolbar control's popup window grab focus http://svn.apache.org/viewvc?view=revision&revision=1225846 Patches contributed by Herbert Duerr #i118662# remove usage of BerkeleyDB in desktop module http://svn.apache.org/viewvc?view=revision&revision=1213171 minor cleanups in dp_persmap.* http://svn.apache.org/viewvc?view=revision&revision=1215064 flush early to prevent problem with extension manager not cleaning up its objects http://svn.apache.org/viewvc?view=revision&revision=1228147 i118726 do not flush *pmap file while reading it http://svn.apache.org/viewvc?view=revision&revision=1230614 #i119048# migrate BDB extension entries using a simple heuristic http://svn.apache.org/viewvc?view=revision&revision=1300972 #i119048# handle edge cases when importing BDB hash files http://svn.apache.org/viewvc?view=revision&revision=1301428 #i119113# fix of-by-one when importing BDB files http://svn.apache.org/viewvc?view=revision&revision=1305420 restore our encryption settings, icon themes, and dictionaries. removed wrapper hacks, kill obsolete bundled extension blob / pre-registration handling, remove duplicated quickstart code. remove OS/2 conditionals.
2012-11-15 17:28:16 +00:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <memory>
2002-09-30 14:24:07 +00:00
#include <stdlib.h>
#include <time.h>
#ifndef _WIN32
#include <unistd.h>
2011-03-29 11:13:09 +02:00
#else
#if !defined WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
2011-03-29 11:13:09 +02:00
#include <windows.h>
#endif
#include <comphelper/random.hxx>
2002-09-30 14:24:07 +00:00
#include <sal/types.h>
#include <osl/file.hxx>
#include <osl/socket.hxx>
#include <osl/security.hxx>
#include <unotools/bootstrap.hxx>
#include <tools/config.hxx>
#include <lockfile.hxx>
2002-09-30 14:24:07 +00:00
using namespace ::osl;
using namespace ::utl;
static OString impl_getHostname()
{
OString aHost;
#ifdef _WIN32
/*
prevent windows from connecting to the net to get its own
hostname by using the netbios name
*/
DWORD sz = MAX_COMPUTERNAME_LENGTH + 1;
auto szHost = std::unique_ptr<char[]>(new char[sz]);
if (GetComputerNameA(szHost.get(), &sz))
aHost = OString(szHost.get());
else
aHost = OString("UNKNOWN");
#else
/* Don't do dns lookup on Linux either */
sal_Char pHostName[1024];
if ( gethostname( pHostName, sizeof( pHostName ) - 1 ) == 0 )
{
pHostName[sizeof( pHostName ) - 1] = '\0';
aHost = OString( pHostName );
}
else
aHost = OString("UNKNOWN");
#endif
return aHost;
}
2002-09-30 14:24:07 +00:00
namespace desktop {
Lockfile::Lockfile( bool bIPCserver )
:m_bIPCserver(bIPCserver)
,m_bRemove(false)
,m_bIsLocked(false)
2002-09-30 14:24:07 +00:00
{
// build the file-url to use for the lock
OUString aUserPath;
utl::Bootstrap::locateUserInstallation( aUserPath );
m_aLockname = aUserPath + "/.lock";
2002-09-30 14:24:07 +00:00
// generate ID
const int nIdBytes = 16;
char tmpId[nIdBytes*2+1];
time_t t = time(nullptr);
2002-09-30 14:24:07 +00:00
for (int i = 0; i<nIdBytes; i++) {
int tmpByte = comphelper::rng::uniform_int_distribution(0, 0xFF);
sprintf( tmpId+i*2, "%02X", tmpByte );
2002-09-30 14:24:07 +00:00
}
tmpId[nIdBytes*2]=0x00;
m_aId = OUString::createFromAscii( tmpId );
// generate date string
char *tmpTime = ctime( &t );
if (tmpTime != nullptr) {
m_aDate = OUString::createFromAscii( tmpTime );
sal_Int32 i = m_aDate.indexOf('\n');
if (i > 0)
m_aDate = m_aDate.copy(0, i);
}
2002-09-30 14:24:07 +00:00
2002-10-24 14:44:30 +00:00
// try to create file
File aFile(m_aLockname);
2010-12-11 18:02:43 +00:00
if (aFile.open( osl_File_OpenFlag_Create ) == File::E_EXIST) {
m_bIsLocked = true;
2002-10-24 14:44:30 +00:00
} else {
// new lock created
aFile.close( );
syncToFile( );
m_bRemove = true;
2002-10-24 14:44:30 +00:00
}
2002-09-30 14:24:07 +00:00
}
bool Lockfile::check( fpExecWarning execWarning )
2002-09-30 14:24:07 +00:00
{
2002-10-24 14:44:30 +00:00
if (m_bIsLocked) {
// lock existed, ask user what to do
if (isStale() ||
(execWarning != nullptr && (*execWarning)( this ))) {
2002-09-30 14:24:07 +00:00
// remove file and create new
File::remove( m_aLockname );
2002-10-24 14:44:30 +00:00
File aFile(m_aLockname);
2010-12-11 18:02:43 +00:00
aFile.open( osl_File_OpenFlag_Create );
2002-09-30 14:24:07 +00:00
aFile.close( );
syncToFile( );
m_bRemove = true;
return true;
2002-09-30 14:24:07 +00:00
} else {
//leave alone and return false
m_bRemove = false;
return false;
2002-09-30 14:24:07 +00:00
}
} else {
2002-10-24 14:44:30 +00:00
// lock was created by us
return true;
2002-09-30 14:24:07 +00:00
}
}
bool Lockfile::isStale() const
{
// this checks whether the lockfile was created on the same
// host by the same user. Should this be the case it is safe
// to assume that it is a stale lockfile which can be overwritten
OUString aLockname = m_aLockname;
Config aConfig(aLockname);
aConfig.SetGroup(LOCKFILE_GROUP);
OString aIPCserver = aConfig.ReadKey( LOCKFILE_IPCKEY );
if (!aIPCserver.equalsIgnoreAsciiCase(OString("true")))
return false;
OString aHost = aConfig.ReadKey( LOCKFILE_HOSTKEY );
OString aUser = aConfig.ReadKey( LOCKFILE_USERKEY );
// lockfile from same host?
OString myHost( impl_getHostname() );
if (aHost == myHost) {
// lockfile by same UID
OUString myUserName;
Security aSecurity;
aSecurity.getUserName( myUserName );
OString myUser(OUStringToOString(myUserName, RTL_TEXTENCODING_ASCII_US));
if (aUser == myUser)
return true;
}
return false;
}
void Lockfile::syncToFile() const
2002-09-30 14:24:07 +00:00
{
OUString aLockname = m_aLockname;
2002-09-30 14:24:07 +00:00
Config aConfig(aLockname);
aConfig.SetGroup(LOCKFILE_GROUP);
2002-09-30 14:24:07 +00:00
// get information
OString aHost( impl_getHostname() );
2002-09-30 14:24:07 +00:00
OUString aUserName;
Security aSecurity;
aSecurity.getUserName( aUserName );
OString aUser = OUStringToOString( aUserName, RTL_TEXTENCODING_ASCII_US );
OString aTime = OUStringToOString( m_aDate, RTL_TEXTENCODING_ASCII_US );
OString aStamp = OUStringToOString( m_aId, RTL_TEXTENCODING_ASCII_US );
2002-09-30 14:24:07 +00:00
// write information
aConfig.WriteKey( LOCKFILE_USERKEY, aUser );
aConfig.WriteKey( LOCKFILE_HOSTKEY, aHost );
aConfig.WriteKey( LOCKFILE_STAMPKEY, aStamp );
aConfig.WriteKey( LOCKFILE_TIMEKEY, aTime );
aConfig.WriteKey(
LOCKFILE_IPCKEY,
m_bIPCserver ? OString("true") : OString("false") );
2002-09-30 14:24:07 +00:00
aConfig.Flush( );
}
Lockfile::~Lockfile()
2002-09-30 14:24:07 +00:00
{
// unlock userdata by removing file
if ( m_bRemove )
File::remove( m_aLockname );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */