Update ooo320-m1
[ooovba.git] / desktop / source / app / lockfile.cxx
blob7aef0d4717db751b02c379afc722d0327b7d27ac
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lockfile.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
33 #include <stdlib.h>
34 #include <time.h>
35 #ifdef WNT
36 #include <tools/prewin.h>
37 #include <windows.h>
38 #include <tools/postwin.h>
39 #else
40 #include <unistd.h>
41 #endif
42 #include <sal/types.h>
43 #include <osl/file.hxx>
44 #include <osl/socket.hxx>
45 #include <osl/security.hxx>
46 #include <unotools/bootstrap.hxx>
47 #include <tools/string.hxx>
48 #include <tools/config.hxx>
50 #include "lockfile.hxx"
53 using namespace ::osl;
54 using namespace ::rtl;
55 using namespace ::utl;
58 static rtl::OString impl_getHostname()
60 rtl::OString aHost;
61 #ifdef WNT
63 prevent windows from connecting to the net to get it's own
64 hostname by using the netbios name
66 sal_Int32 sz = MAX_COMPUTERNAME_LENGTH + 1;
67 char* szHost = new char[sz];
68 if (GetComputerName(szHost, (LPDWORD)&sz))
69 aHost = OString(szHost);
70 else
71 aHost = OString("UNKNOWN");
72 delete[] szHost;
73 #else
74 /* Don't do dns lookup on Linux either */
75 sal_Char pHostName[1024];
77 if ( gethostname( pHostName, sizeof( pHostName ) - 1 ) == 0 )
79 pHostName[sizeof( pHostName ) - 1] = '\0';
80 aHost = OString( pHostName );
82 else
83 aHost = OString("UNKNOWN");
84 #endif
86 return aHost;
89 namespace desktop {
91 Lockfile::Lockfile( bool bIPCserver )
92 :m_bIPCserver(bIPCserver)
93 ,m_bRemove(sal_False)
94 ,m_bIsLocked(sal_False)
96 // build the file-url to use for the lock
97 OUString aUserPath;
98 utl::Bootstrap::locateUserInstallation( aUserPath );
99 m_aLockname = aUserPath + LOCKFILE_SUFFIX;
101 // generate ID
102 const int nIdBytes = 16;
103 char tmpId[nIdBytes*2+1];
104 time_t t;
105 srand( (unsigned)(t = time( NULL )) );
106 int tmpByte = 0;
107 for (int i = 0; i<nIdBytes; i++) {
108 tmpByte = rand( ) % 0xFF;
109 sprintf( tmpId+i*2, "%02X", tmpByte ); // #100211# - checked
111 tmpId[nIdBytes*2]=0x00;
112 m_aId = OUString::createFromAscii( tmpId );
114 // generate date string
115 char *tmpTime = ctime( &t );
116 if (tmpTime != NULL) {
117 m_aDate = OUString::createFromAscii( tmpTime );
118 sal_Int32 i = m_aDate.indexOf('\n');
119 if (i > 0)
120 m_aDate = m_aDate.copy(0, i);
124 // try to create file
125 File aFile(m_aLockname);
126 if (aFile.open( OpenFlag_Create ) == File::E_EXIST) {
127 m_bIsLocked = sal_True;
128 } else {
129 // new lock created
130 aFile.close( );
131 syncToFile( );
132 m_bRemove = sal_True;
136 sal_Bool Lockfile::check( fpExecWarning execWarning )
139 if (m_bIsLocked) {
140 // lock existed, ask user what to do
141 if (isStale() ||
142 (execWarning != 0 && (*execWarning)( this ))) {
143 // remove file and create new
144 File::remove( m_aLockname );
145 File aFile(m_aLockname);
146 aFile.open( OpenFlag_Create );
147 aFile.close( );
148 syncToFile( );
149 m_bRemove = sal_True;
150 return sal_True;
151 } else {
152 //leave alone and return false
153 m_bRemove = sal_False;
154 return sal_False;
156 } else {
157 // lock was created by us
158 return sal_True;
162 sal_Bool Lockfile::isStale( void ) const
164 // this checks whether the lockfile was created on the same
165 // host by the same user. Should this be the case it is safe
166 // to assume that it is a stale lockfile which can be overwritten
167 String aLockname = m_aLockname;
168 Config aConfig(aLockname);
169 aConfig.SetGroup(LOCKFILE_GROUP);
170 ByteString aIPCserver = aConfig.ReadKey( LOCKFILE_IPCKEY );
171 if (! aIPCserver.EqualsIgnoreCaseAscii( "true" ))
172 return false;
174 ByteString aHost = aConfig.ReadKey( LOCKFILE_HOSTKEY );
175 ByteString aUser = aConfig.ReadKey( LOCKFILE_USERKEY );
177 // lockfile from same host?
178 ByteString myHost( impl_getHostname() );
179 if (aHost == myHost) {
180 // lockfile by same UID
181 OUString myUserName;
182 Security aSecurity;
183 aSecurity.getUserName( myUserName );
184 ByteString myUser = OUStringToOString( myUserName, RTL_TEXTENCODING_ASCII_US );
185 if (aUser == myUser)
186 return sal_True;
188 return sal_False;
191 void Lockfile::syncToFile( void ) const
193 String aLockname = m_aLockname;
194 Config aConfig(aLockname);
195 aConfig.SetGroup(LOCKFILE_GROUP);
197 // get information
198 ByteString aHost( impl_getHostname() );
199 OUString aUserName;
200 Security aSecurity;
201 aSecurity.getUserName( aUserName );
202 ByteString aUser = OUStringToOString( aUserName, RTL_TEXTENCODING_ASCII_US );
203 ByteString aTime = OUStringToOString( m_aDate, RTL_TEXTENCODING_ASCII_US );
204 ByteString aStamp = OUStringToOString( m_aId, RTL_TEXTENCODING_ASCII_US );
206 // write information
207 aConfig.WriteKey( LOCKFILE_USERKEY, aUser );
208 aConfig.WriteKey( LOCKFILE_HOSTKEY, aHost );
209 aConfig.WriteKey( LOCKFILE_STAMPKEY, aStamp );
210 aConfig.WriteKey( LOCKFILE_TIMEKEY, aTime );
211 aConfig.WriteKey(
212 LOCKFILE_IPCKEY,
213 m_bIPCserver ? ByteString("true") : ByteString("false") );
214 aConfig.Flush( );
217 void Lockfile::clean( void )
219 if ( m_bRemove )
221 File::remove( m_aLockname );
222 m_bRemove = sal_False;
226 Lockfile::~Lockfile( void )
228 // unlock userdata by removing file
229 if ( m_bRemove )
230 File::remove( m_aLockname );