update credits
[LibreOffice.git] / desktop / source / deployment / misc / lockfile.cxx
blob97202ecda67c032c8dcf0a1cfd18f35750ca8ed4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdlib.h>
21 #include <time.h>
22 #ifndef WNT
23 #include <unistd.h>
24 #else
25 #include <windows.h>
26 #endif
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()
45 OString aHost;
46 #ifdef WNT
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);
55 else
56 aHost = OString("UNKNOWN");
57 delete[] szHost;
58 #else
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 );
67 else
68 aHost = OString("UNKNOWN");
69 #endif
71 return aHost;
74 namespace desktop {
76 Lockfile::Lockfile( bool bIPCserver )
77 :m_bIPCserver(bIPCserver)
78 ,m_bRemove(sal_False)
79 ,m_bIsLocked(sal_False)
81 // build the file-url to use for the lock
82 OUString aUserPath;
83 utl::Bootstrap::locateUserInstallation( aUserPath );
84 m_aLockname = aUserPath + LOCKFILE_SUFFIX;
86 // generate ID
87 const int nIdBytes = 16;
88 char tmpId[nIdBytes*2+1];
89 time_t t;
90 srand( (unsigned)(t = time( NULL )) );
91 int tmpByte = 0;
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');
104 if (i > 0)
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;
113 } else {
114 // new lock created
115 aFile.close( );
116 syncToFile( );
117 m_bRemove = sal_True;
121 sal_Bool Lockfile::check( fpExecWarning execWarning )
124 if (m_bIsLocked) {
125 // lock existed, ask user what to do
126 if (isStale() ||
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 );
132 aFile.close( );
133 syncToFile( );
134 m_bRemove = sal_True;
135 return sal_True;
136 } else {
137 //leave alone and return false
138 m_bRemove = sal_False;
139 return sal_False;
141 } else {
142 // lock was created by us
143 return sal_True;
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")))
157 return false;
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
166 OUString myUserName;
167 Security aSecurity;
168 aSecurity.getUserName( myUserName );
169 OString myUser(OUStringToOString(myUserName, RTL_TEXTENCODING_ASCII_US));
170 if (aUser == myUser)
171 return sal_True;
173 return sal_False;
176 void Lockfile::syncToFile( void ) const
178 String aLockname = m_aLockname;
179 Config aConfig(aLockname);
180 aConfig.SetGroup(LOCKFILE_GROUP);
182 // get information
183 OString aHost( impl_getHostname() );
184 OUString aUserName;
185 Security aSecurity;
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 );
191 // write information
192 aConfig.WriteKey( LOCKFILE_USERKEY, aUser );
193 aConfig.WriteKey( LOCKFILE_HOSTKEY, aHost );
194 aConfig.WriteKey( LOCKFILE_STAMPKEY, aStamp );
195 aConfig.WriteKey( LOCKFILE_TIMEKEY, aTime );
196 aConfig.WriteKey(
197 LOCKFILE_IPCKEY,
198 m_bIPCserver ? OString("true") : OString("false") );
199 aConfig.Flush( );
202 Lockfile::~Lockfile( void )
204 // unlock userdata by removing file
205 if ( m_bRemove )
206 File::remove( m_aLockname );
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */