1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_desktop.hxx" 30*cdf0e10cSrcweir #include <stdlib.h> 31*cdf0e10cSrcweir #include <time.h> 32*cdf0e10cSrcweir #ifdef WNT 33*cdf0e10cSrcweir #include <tools/prewin.h> 34*cdf0e10cSrcweir #include <windows.h> 35*cdf0e10cSrcweir #include <tools/postwin.h> 36*cdf0e10cSrcweir #endif 37*cdf0e10cSrcweir #include <sal/types.h> 38*cdf0e10cSrcweir #include <osl/file.hxx> 39*cdf0e10cSrcweir #include <osl/socket.hxx> 40*cdf0e10cSrcweir #include <osl/security.hxx> 41*cdf0e10cSrcweir #include <unotools/bootstrap.hxx> 42*cdf0e10cSrcweir #include <tools/string.hxx> 43*cdf0e10cSrcweir #include <tools/config.hxx> 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir #include "lockfile.hxx" 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using namespace ::osl; 49*cdf0e10cSrcweir using namespace ::rtl; 50*cdf0e10cSrcweir using namespace ::utl; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir namespace desktop { 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir // initialize static members... 56*cdf0e10cSrcweir // lock suffix 57*cdf0e10cSrcweir const OUString Lockfile::Suffix() 58*cdf0e10cSrcweir { return OUString::createFromAscii( "/.lock" ); } 59*cdf0e10cSrcweir // values for datafile 60*cdf0e10cSrcweir const ByteString Lockfile::Group() 61*cdf0e10cSrcweir { return ByteString( "Lockdata" ); } 62*cdf0e10cSrcweir const ByteString Lockfile::Userkey() 63*cdf0e10cSrcweir { return ByteString( "User" ); } 64*cdf0e10cSrcweir const ByteString Lockfile::Hostkey() 65*cdf0e10cSrcweir { return ByteString( "Host" ); } 66*cdf0e10cSrcweir const ByteString Lockfile::Stampkey() 67*cdf0e10cSrcweir { return ByteString( "Stamp" ); } 68*cdf0e10cSrcweir const ByteString Lockfile::Timekey() 69*cdf0e10cSrcweir { return ByteString( "Time" ); } 70*cdf0e10cSrcweir const ByteString Lockfile::IPCkey() 71*cdf0e10cSrcweir { return ByteString( "IPCServer" ); } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir Lockfile::Lockfile( bool bIPCserver ) 74*cdf0e10cSrcweir :m_bIPCserver(bIPCserver) 75*cdf0e10cSrcweir ,m_bRemove(sal_False) 76*cdf0e10cSrcweir ,m_bIsLocked(sal_False) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir // build the file-url to use for the lock 79*cdf0e10cSrcweir OUString aUserPath; 80*cdf0e10cSrcweir utl::Bootstrap::locateUserInstallation( aUserPath ); 81*cdf0e10cSrcweir m_aLockname = aUserPath + Suffix(); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // generate ID 84*cdf0e10cSrcweir const int nIdBytes = 16; 85*cdf0e10cSrcweir char tmpId[nIdBytes*2+1]; 86*cdf0e10cSrcweir time_t t; 87*cdf0e10cSrcweir srand( (unsigned)(t = time( NULL )) ); 88*cdf0e10cSrcweir int tmpByte = 0; 89*cdf0e10cSrcweir for (int i = 0; i<nIdBytes; i++) { 90*cdf0e10cSrcweir tmpByte = rand( ) % 0xFF; 91*cdf0e10cSrcweir sprintf( tmpId+i*2, "%02X", tmpByte ); // #100211# - checked 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir tmpId[nIdBytes*2]=0x00; 94*cdf0e10cSrcweir m_aId = OUString::createFromAscii( tmpId ); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir // generate date string 97*cdf0e10cSrcweir char *tmpTime = ctime( &t ); 98*cdf0e10cSrcweir if (tmpTime != NULL) { 99*cdf0e10cSrcweir m_aDate = OUString::createFromAscii( tmpTime ); 100*cdf0e10cSrcweir sal_Int32 i = m_aDate.indexOf('\n'); 101*cdf0e10cSrcweir if (i > 0) 102*cdf0e10cSrcweir m_aDate = m_aDate.copy(0, i); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir // try to create file 107*cdf0e10cSrcweir File aFile(m_aLockname); 108*cdf0e10cSrcweir if (aFile.open( OpenFlag_Create ) == File::E_EXIST) { 109*cdf0e10cSrcweir m_bIsLocked = sal_True; 110*cdf0e10cSrcweir } else { 111*cdf0e10cSrcweir // new lock created 112*cdf0e10cSrcweir aFile.close( ); 113*cdf0e10cSrcweir syncToFile( ); 114*cdf0e10cSrcweir m_bRemove = sal_True; 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir sal_Bool Lockfile::check( fpExecWarning execWarning ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir if (m_bIsLocked) { 122*cdf0e10cSrcweir // lock existed, ask user what to do 123*cdf0e10cSrcweir if (isStale() || 124*cdf0e10cSrcweir (execWarning != 0 && (*execWarning)( this ))) { 125*cdf0e10cSrcweir // remove file and create new 126*cdf0e10cSrcweir File::remove( m_aLockname ); 127*cdf0e10cSrcweir File aFile(m_aLockname); 128*cdf0e10cSrcweir aFile.open( OpenFlag_Create ); 129*cdf0e10cSrcweir aFile.close( ); 130*cdf0e10cSrcweir syncToFile( ); 131*cdf0e10cSrcweir m_bRemove = sal_True; 132*cdf0e10cSrcweir return sal_True; 133*cdf0e10cSrcweir } else { 134*cdf0e10cSrcweir //leave alone and return false 135*cdf0e10cSrcweir m_bRemove = sal_False; 136*cdf0e10cSrcweir return sal_False; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir } else { 139*cdf0e10cSrcweir // lock was created by us 140*cdf0e10cSrcweir return sal_True; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir sal_Bool Lockfile::isStale( void ) const 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir // this checks whether the lockfile was created on the same 147*cdf0e10cSrcweir // host by the same user. Should this be the case it is safe 148*cdf0e10cSrcweir // to assume that it is a stale lockfile which can be overwritten 149*cdf0e10cSrcweir String aLockname = m_aLockname; 150*cdf0e10cSrcweir Config aConfig(aLockname); 151*cdf0e10cSrcweir aConfig.SetGroup(Group()); 152*cdf0e10cSrcweir ByteString aIPCserver = aConfig.ReadKey( IPCkey() ); 153*cdf0e10cSrcweir if (! aIPCserver.EqualsIgnoreCaseAscii( "true" )) 154*cdf0e10cSrcweir return false; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir ByteString aHost = aConfig.ReadKey( Hostkey() ); 157*cdf0e10cSrcweir ByteString aUser = aConfig.ReadKey( Userkey() ); 158*cdf0e10cSrcweir // lockfile from same host? 159*cdf0e10cSrcweir ByteString myHost; 160*cdf0e10cSrcweir #ifdef WNT 161*cdf0e10cSrcweir /* 162*cdf0e10cSrcweir prevent windows from connecting to the net to get it's own 163*cdf0e10cSrcweir hostname by using the netbios name 164*cdf0e10cSrcweir */ 165*cdf0e10cSrcweir sal_Int32 sz = MAX_COMPUTERNAME_LENGTH + 1; 166*cdf0e10cSrcweir char* szHost = new char[sz]; 167*cdf0e10cSrcweir if (GetComputerName(szHost, (LPDWORD)&sz)) 168*cdf0e10cSrcweir myHost = OString(szHost); 169*cdf0e10cSrcweir else 170*cdf0e10cSrcweir myHost = OString("UNKNOWN"); 171*cdf0e10cSrcweir delete[] szHost; 172*cdf0e10cSrcweir #else 173*cdf0e10cSrcweir oslSocketResult sRes; 174*cdf0e10cSrcweir myHost = OUStringToOString( 175*cdf0e10cSrcweir SocketAddr::getLocalHostname( &sRes ), RTL_TEXTENCODING_ASCII_US ); 176*cdf0e10cSrcweir #endif 177*cdf0e10cSrcweir if (aHost == myHost) { 178*cdf0e10cSrcweir // lockfile by same UID 179*cdf0e10cSrcweir OUString myUserName; 180*cdf0e10cSrcweir Security aSecurity; 181*cdf0e10cSrcweir aSecurity.getUserName( myUserName ); 182*cdf0e10cSrcweir ByteString myUser = OUStringToOString( myUserName, RTL_TEXTENCODING_ASCII_US ); 183*cdf0e10cSrcweir if (aUser == myUser) 184*cdf0e10cSrcweir return sal_True; 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir return sal_False; 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir void Lockfile::syncToFile( void ) const 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir String aLockname = m_aLockname; 192*cdf0e10cSrcweir Config aConfig(aLockname); 193*cdf0e10cSrcweir aConfig.SetGroup(Group()); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir // get information 196*cdf0e10cSrcweir ByteString aHost; 197*cdf0e10cSrcweir #ifdef WNT 198*cdf0e10cSrcweir /* 199*cdf0e10cSrcweir prevent windows from connecting to the net to get it's own 200*cdf0e10cSrcweir hostname by using the netbios name 201*cdf0e10cSrcweir */ 202*cdf0e10cSrcweir sal_Int32 sz = MAX_COMPUTERNAME_LENGTH + 1; 203*cdf0e10cSrcweir char* szHost = new char[sz]; 204*cdf0e10cSrcweir if (GetComputerName(szHost, (LPDWORD)&sz)) 205*cdf0e10cSrcweir aHost = OString(szHost); 206*cdf0e10cSrcweir else 207*cdf0e10cSrcweir aHost = OString("UNKNOWN"); 208*cdf0e10cSrcweir delete[] szHost; 209*cdf0e10cSrcweir #else 210*cdf0e10cSrcweir oslSocketResult sRes; 211*cdf0e10cSrcweir aHost = OUStringToOString( 212*cdf0e10cSrcweir SocketAddr::getLocalHostname( &sRes ), RTL_TEXTENCODING_ASCII_US ); 213*cdf0e10cSrcweir #endif 214*cdf0e10cSrcweir OUString aUserName; 215*cdf0e10cSrcweir Security aSecurity; 216*cdf0e10cSrcweir aSecurity.getUserName( aUserName ); 217*cdf0e10cSrcweir ByteString aUser = OUStringToOString( aUserName, RTL_TEXTENCODING_ASCII_US ); 218*cdf0e10cSrcweir ByteString aTime = OUStringToOString( m_aDate, RTL_TEXTENCODING_ASCII_US ); 219*cdf0e10cSrcweir ByteString aStamp = OUStringToOString( m_aId, RTL_TEXTENCODING_ASCII_US ); 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir // write information 222*cdf0e10cSrcweir aConfig.WriteKey( Userkey(), aUser ); 223*cdf0e10cSrcweir aConfig.WriteKey( Hostkey(), aHost ); 224*cdf0e10cSrcweir aConfig.WriteKey( Stampkey(), aStamp ); 225*cdf0e10cSrcweir aConfig.WriteKey( Timekey(), aTime ); 226*cdf0e10cSrcweir aConfig.WriteKey( 227*cdf0e10cSrcweir IPCkey(), 228*cdf0e10cSrcweir m_bIPCserver ? ByteString("true") : ByteString("false") ); 229*cdf0e10cSrcweir aConfig.Flush( ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir void Lockfile::clean( void ) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir if ( m_bRemove ) 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir File::remove( m_aLockname ); 237*cdf0e10cSrcweir m_bRemove = sal_False; 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir Lockfile::~Lockfile( void ) 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir // unlock userdata by removing file 244*cdf0e10cSrcweir if ( m_bRemove ) 245*cdf0e10cSrcweir File::remove( m_aLockname ); 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir 257