1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
35 #include "lockfile.hxx"
38 using namespace ::osl
;
39 using namespace ::rtl
;
40 using namespace ::utl
;
43 static OString
impl_getHostname()
48 prevent windows from connecting to the net to get it's own
49 hostname by using the netbios name
51 sal_Int32 sz
= MAX_COMPUTERNAME_LENGTH
+ 1;
52 char* szHost
= new char[sz
];
53 if (GetComputerName(szHost
, (LPDWORD
)&sz
))
54 aHost
= OString(szHost
);
56 aHost
= OString("UNKNOWN");
59 /* Don't do dns lookup on Linux either */
60 sal_Char pHostName
[1024];
62 if ( gethostname( pHostName
, sizeof( pHostName
) - 1 ) == 0 )
64 pHostName
[sizeof( pHostName
) - 1] = '\0';
65 aHost
= OString( pHostName
);
68 aHost
= OString("UNKNOWN");
76 Lockfile::Lockfile( bool bIPCserver
)
77 :m_bIPCserver(bIPCserver
)
79 ,m_bIsLocked(sal_False
)
81 // build the file-url to use for the lock
83 utl::Bootstrap::locateUserInstallation( aUserPath
);
84 m_aLockname
= aUserPath
+ LOCKFILE_SUFFIX
;
87 const int nIdBytes
= 16;
88 char tmpId
[nIdBytes
*2+1];
90 srand( (unsigned)(t
= time( NULL
)) );
92 for (int i
= 0; i
<nIdBytes
; i
++) {
93 tmpByte
= rand( ) % 0xFF;
94 sprintf( tmpId
+i
*2, "%02X", tmpByte
);
96 tmpId
[nIdBytes
*2]=0x00;
97 m_aId
= OUString::createFromAscii( tmpId
);
99 // generate date string
100 char *tmpTime
= ctime( &t
);
101 if (tmpTime
!= NULL
) {
102 m_aDate
= OUString::createFromAscii( tmpTime
);
103 sal_Int32 i
= m_aDate
.indexOf('\n');
105 m_aDate
= m_aDate
.copy(0, i
);
109 // try to create file
110 File
aFile(m_aLockname
);
111 if (aFile
.open( osl_File_OpenFlag_Create
) == File::E_EXIST
) {
112 m_bIsLocked
= sal_True
;
117 m_bRemove
= sal_True
;
121 sal_Bool
Lockfile::check( fpExecWarning execWarning
)
125 // lock existed, ask user what to do
127 (execWarning
!= 0 && (*execWarning
)( this ))) {
128 // remove file and create new
129 File::remove( m_aLockname
);
130 File
aFile(m_aLockname
);
131 aFile
.open( osl_File_OpenFlag_Create
);
134 m_bRemove
= sal_True
;
137 //leave alone and return false
138 m_bRemove
= sal_False
;
142 // lock was created by us
147 sal_Bool
Lockfile::isStale( void ) const
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 String aLockname
= m_aLockname
;
153 Config
aConfig(aLockname
);
154 aConfig
.SetGroup(LOCKFILE_GROUP
);
155 OString aIPCserver
= aConfig
.ReadKey( LOCKFILE_IPCKEY
);
156 if (!aIPCserver
.equalsIgnoreAsciiCase(OString("true")))
159 OString aHost
= aConfig
.ReadKey( LOCKFILE_HOSTKEY
);
160 OString aUser
= aConfig
.ReadKey( LOCKFILE_USERKEY
);
162 // lockfile from same host?
163 OString
myHost( impl_getHostname() );
164 if (aHost
== myHost
) {
165 // lockfile by same UID
168 aSecurity
.getUserName( myUserName
);
169 OString
myUser(OUStringToOString(myUserName
, RTL_TEXTENCODING_ASCII_US
));
176 void Lockfile::syncToFile( void ) const
178 String aLockname
= m_aLockname
;
179 Config
aConfig(aLockname
);
180 aConfig
.SetGroup(LOCKFILE_GROUP
);
183 OString
aHost( impl_getHostname() );
186 aSecurity
.getUserName( aUserName
);
187 OString aUser
= OUStringToOString( aUserName
, RTL_TEXTENCODING_ASCII_US
);
188 OString aTime
= OUStringToOString( m_aDate
, RTL_TEXTENCODING_ASCII_US
);
189 OString aStamp
= OUStringToOString( m_aId
, RTL_TEXTENCODING_ASCII_US
);
192 aConfig
.WriteKey( LOCKFILE_USERKEY
, aUser
);
193 aConfig
.WriteKey( LOCKFILE_HOSTKEY
, aHost
);
194 aConfig
.WriteKey( LOCKFILE_STAMPKEY
, aStamp
);
195 aConfig
.WriteKey( LOCKFILE_TIMEKEY
, aTime
);
198 m_bIPCserver
? OString("true") : OString("false") );
202 Lockfile::~Lockfile( void )
204 // unlock userdata by removing file
206 File::remove( m_aLockname
);
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */