LCOV - code coverage report
Current view: top level - libreoffice/desktop/source/deployment/misc - lockfile.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 88 0.0 %
Date: 2012-12-27 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          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             : #include <stdlib.h>
      21             : #include <time.h>
      22             : #ifndef WNT
      23             : #include <unistd.h>
      24             : #else
      25             : #include <windows.h>
      26             : #endif
      27             : #include <sal/types.h>
      28             : #include <osl/file.hxx>
      29             : #include <osl/socket.hxx>
      30             : #include <osl/security.hxx>
      31             : #include <unotools/bootstrap.hxx>
      32             : #include <tools/string.hxx>
      33             : #include <tools/config.hxx>
      34             : 
      35             : #include "lockfile.hxx"
      36             : 
      37             : 
      38             : using namespace ::osl;
      39             : using namespace ::rtl;
      40             : using namespace ::utl;
      41             : 
      42             : 
      43           0 : static rtl::OString impl_getHostname()
      44             : {
      45           0 :     rtl::OString aHost;
      46             : #ifdef WNT
      47             :     /*
      48             :        prevent windows from connecting to the net to get it's own
      49             :        hostname by using the netbios name
      50             :        */
      51             :     sal_Int32 sz = MAX_COMPUTERNAME_LENGTH + 1;
      52             :     char* szHost = new char[sz];
      53             :     if (GetComputerName(szHost, (LPDWORD)&sz))
      54             :         aHost = OString(szHost);
      55             :     else
      56             :         aHost = OString("UNKNOWN");
      57             :     delete[] szHost;
      58             : #else
      59             :     /* Don't do dns lookup on Linux either */
      60             :     sal_Char pHostName[1024];
      61             : 
      62           0 :     if ( gethostname( pHostName, sizeof( pHostName ) - 1 ) == 0 )
      63             :     {
      64           0 :         pHostName[sizeof( pHostName ) - 1] = '\0';
      65           0 :         aHost = OString( pHostName );
      66             :     }
      67             :     else
      68           0 :         aHost = OString("UNKNOWN");
      69             : #endif
      70             : 
      71           0 :     return aHost;
      72             : }
      73             : 
      74             : namespace desktop {
      75             : 
      76           0 :     Lockfile::Lockfile( bool bIPCserver )
      77             :     :m_bIPCserver(bIPCserver)
      78             :     ,m_bRemove(sal_False)
      79           0 :     ,m_bIsLocked(sal_False)
      80             :     {
      81             :         // build the file-url to use for the lock
      82           0 :         OUString aUserPath;
      83           0 :         utl::Bootstrap::locateUserInstallation( aUserPath );
      84           0 :         m_aLockname = aUserPath + LOCKFILE_SUFFIX;
      85             : 
      86             :         // generate ID
      87           0 :         const int nIdBytes = 16;
      88             :         char tmpId[nIdBytes*2+1];
      89             :         time_t t;
      90           0 :         srand( (unsigned)(t = time( NULL )) );
      91           0 :         int tmpByte = 0;
      92           0 :         for (int i = 0; i<nIdBytes; i++) {
      93           0 :             tmpByte = rand( ) % 0xFF;
      94           0 :             sprintf( tmpId+i*2, "%02X", tmpByte );
      95             :         }
      96           0 :         tmpId[nIdBytes*2]=0x00;
      97           0 :         m_aId = OUString::createFromAscii( tmpId );
      98             : 
      99             :         // generate date string
     100           0 :         char *tmpTime = ctime( &t );
     101           0 :         if (tmpTime != NULL) {
     102           0 :             m_aDate = OUString::createFromAscii( tmpTime );
     103           0 :             sal_Int32 i = m_aDate.indexOf('\n');
     104           0 :             if (i > 0)
     105           0 :                 m_aDate = m_aDate.copy(0, i);
     106             :         }
     107             : 
     108             : 
     109             :         // try to create file
     110           0 :         File aFile(m_aLockname);
     111           0 :         if (aFile.open( osl_File_OpenFlag_Create ) == File::E_EXIST) {
     112           0 :             m_bIsLocked = sal_True;
     113             :         } else {
     114             :             // new lock created
     115           0 :             aFile.close( );
     116           0 :             syncToFile( );
     117           0 :             m_bRemove = sal_True;
     118           0 :         }
     119           0 :     }
     120             : 
     121           0 :     sal_Bool Lockfile::check( fpExecWarning execWarning )
     122             :     {
     123             : 
     124           0 :         if (m_bIsLocked) {
     125             :             // lock existed, ask user what to do
     126           0 :             if (isStale() ||
     127           0 :                 (execWarning != 0 && (*execWarning)( this ))) {
     128             :                 // remove file and create new
     129           0 :                 File::remove( m_aLockname );
     130           0 :                 File aFile(m_aLockname);
     131           0 :                 aFile.open( osl_File_OpenFlag_Create );
     132           0 :                 aFile.close( );
     133           0 :                 syncToFile( );
     134           0 :                 m_bRemove = sal_True;
     135           0 :                 return sal_True;
     136             :             } else {
     137             :                 //leave alone and return false
     138           0 :                 m_bRemove = sal_False;
     139           0 :                 return sal_False;
     140             :             }
     141             :         } else {
     142             :             // lock was created by us
     143           0 :             return sal_True;
     144             :         }
     145             :     }
     146             : 
     147           0 :     sal_Bool Lockfile::isStale( void ) const
     148             :     {
     149             :         // this checks whether the lockfile was created on the same
     150             :         // host by the same user. Should this be the case it is safe
     151             :         // to assume that it is a stale lockfile which can be overwritten
     152           0 :         String aLockname = m_aLockname;
     153           0 :         Config aConfig(aLockname);
     154           0 :         aConfig.SetGroup(LOCKFILE_GROUP);
     155           0 :         rtl::OString aIPCserver  = aConfig.ReadKey( LOCKFILE_IPCKEY );
     156           0 :         if (!aIPCserver.equalsIgnoreAsciiCase(rtl::OString("true")))
     157           0 :             return false;
     158             : 
     159           0 :         rtl::OString aHost = aConfig.ReadKey( LOCKFILE_HOSTKEY );
     160           0 :         rtl::OString aUser = aConfig.ReadKey( LOCKFILE_USERKEY );
     161             : 
     162             :         // lockfile from same host?
     163           0 :         rtl::OString myHost( impl_getHostname() );
     164           0 :         if (aHost == myHost) {
     165             :             // lockfile by same UID
     166           0 :             OUString myUserName;
     167           0 :             Security aSecurity;
     168           0 :             aSecurity.getUserName( myUserName );
     169           0 :             rtl::OString myUser(rtl::OUStringToOString(myUserName, RTL_TEXTENCODING_ASCII_US));
     170           0 :             if (aUser == myUser)
     171           0 :                 return sal_True;
     172             :         }
     173           0 :         return sal_False;
     174             :     }
     175             : 
     176           0 :     void Lockfile::syncToFile( void ) const
     177             :     {
     178           0 :         String aLockname = m_aLockname;
     179           0 :         Config aConfig(aLockname);
     180           0 :         aConfig.SetGroup(LOCKFILE_GROUP);
     181             : 
     182             :         // get information
     183           0 :         rtl::OString aHost( impl_getHostname() );
     184           0 :         OUString aUserName;
     185           0 :         Security aSecurity;
     186           0 :         aSecurity.getUserName( aUserName );
     187           0 :         rtl::OString aUser  = OUStringToOString( aUserName, RTL_TEXTENCODING_ASCII_US );
     188           0 :         rtl::OString aTime  = OUStringToOString( m_aDate, RTL_TEXTENCODING_ASCII_US );
     189           0 :         rtl::OString aStamp = OUStringToOString( m_aId, RTL_TEXTENCODING_ASCII_US );
     190             : 
     191             :         // write information
     192           0 :         aConfig.WriteKey( LOCKFILE_USERKEY,  aUser );
     193           0 :         aConfig.WriteKey( LOCKFILE_HOSTKEY,  aHost );
     194           0 :         aConfig.WriteKey( LOCKFILE_STAMPKEY, aStamp );
     195           0 :         aConfig.WriteKey( LOCKFILE_TIMEKEY,  aTime );
     196             :         aConfig.WriteKey(
     197             :             LOCKFILE_IPCKEY,
     198           0 :             m_bIPCserver ? rtl::OString("true") : rtl::OString("false") );
     199           0 :         aConfig.Flush( );
     200           0 :     }
     201             : 
     202           0 :     Lockfile::~Lockfile( void )
     203             :     {
     204             :         // unlock userdata by removing file
     205           0 :         if ( m_bRemove )
     206           0 :             File::remove( m_aLockname );
     207           0 :     }
     208             : }
     209             : 
     210             : 
     211             : 
     212             : 
     213             : 
     214             : 
     215             : 
     216             : 
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10