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 $
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"
36 #include <tools/prewin.h>
38 #include <tools/postwin.h>
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()
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
);
71 aHost
= OString("UNKNOWN");
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
);
83 aHost
= OString("UNKNOWN");
91 Lockfile::Lockfile( bool bIPCserver
)
92 :m_bIPCserver(bIPCserver
)
94 ,m_bIsLocked(sal_False
)
96 // build the file-url to use for the lock
98 utl::Bootstrap::locateUserInstallation( aUserPath
);
99 m_aLockname
= aUserPath
+ LOCKFILE_SUFFIX
;
102 const int nIdBytes
= 16;
103 char tmpId
[nIdBytes
*2+1];
105 srand( (unsigned)(t
= time( NULL
)) );
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');
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
;
132 m_bRemove
= sal_True
;
136 sal_Bool
Lockfile::check( fpExecWarning execWarning
)
140 // lock existed, ask user what to do
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
);
149 m_bRemove
= sal_True
;
152 //leave alone and return false
153 m_bRemove
= sal_False
;
157 // lock was created by us
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" ))
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
183 aSecurity
.getUserName( myUserName
);
184 ByteString myUser
= OUStringToOString( myUserName
, RTL_TEXTENCODING_ASCII_US
);
191 void Lockfile::syncToFile( void ) const
193 String aLockname
= m_aLockname
;
194 Config
aConfig(aLockname
);
195 aConfig
.SetGroup(LOCKFILE_GROUP
);
198 ByteString
aHost( impl_getHostname() );
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
);
207 aConfig
.WriteKey( LOCKFILE_USERKEY
, aUser
);
208 aConfig
.WriteKey( LOCKFILE_HOSTKEY
, aHost
);
209 aConfig
.WriteKey( LOCKFILE_STAMPKEY
, aStamp
);
210 aConfig
.WriteKey( LOCKFILE_TIMEKEY
, aTime
);
213 m_bIPCserver
? ByteString("true") : ByteString("false") );
217 void Lockfile::clean( void )
221 File::remove( m_aLockname
);
222 m_bRemove
= sal_False
;
226 Lockfile::~Lockfile( void )
228 // unlock userdata by removing file
230 File::remove( m_aLockname
);