xref: /trunk/main/desktop/source/app/lockfile.cxx (revision 0a407c2a85aa3a829e52a449aa13b6528fbe055e)
12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew 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
Suffix()53cdf0e10cSrcweir     const OUString Lockfile::Suffix()
54cdf0e10cSrcweir         { return OUString::createFromAscii( "/.lock" ); }
55cdf0e10cSrcweir     // values for datafile
Group()56cdf0e10cSrcweir     const ByteString Lockfile::Group()
57cdf0e10cSrcweir         { return ByteString( "Lockdata" ); }
Userkey()58cdf0e10cSrcweir     const ByteString Lockfile::Userkey()
59cdf0e10cSrcweir         { return ByteString( "User" ); }
Hostkey()60cdf0e10cSrcweir     const ByteString Lockfile::Hostkey()
61cdf0e10cSrcweir         { return ByteString( "Host" ); }
Stampkey()62cdf0e10cSrcweir     const ByteString Lockfile::Stampkey()
63cdf0e10cSrcweir         { return ByteString( "Stamp" ); }
Timekey()64cdf0e10cSrcweir     const ByteString Lockfile::Timekey()
65cdf0e10cSrcweir         { return ByteString( "Time" ); }
IPCkey()66cdf0e10cSrcweir     const ByteString Lockfile::IPCkey()
67cdf0e10cSrcweir         { return ByteString( "IPCServer" ); }
68cdf0e10cSrcweir 
Lockfile(bool bIPCserver)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 
check(fpExecWarning execWarning)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 
isStale(void) const140cdf0e10cSrcweir     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         /*
158*33608c82Smseidel           prevent Windows from connecting to the net to get its 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 
syncToFile(void) const185cdf0e10cSrcweir     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         /*
195*33608c82Smseidel           prevent Windows from connecting to the net to get its 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 
clean(void)228cdf0e10cSrcweir     void Lockfile::clean( void )
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         if ( m_bRemove )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             File::remove( m_aLockname );
233cdf0e10cSrcweir             m_bRemove = sal_False;
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
~Lockfile(void)237cdf0e10cSrcweir     Lockfile::~Lockfile( void )
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         // unlock userdata by removing file
240cdf0e10cSrcweir         if ( m_bRemove )
241cdf0e10cSrcweir             File::remove( m_aLockname );
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir }
244