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