Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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/config.hxx>
33 :
34 : #include "lockfile.hxx"
35 :
36 :
37 : using namespace ::osl;
38 : using namespace ::rtl;
39 : using namespace ::utl;
40 :
41 :
42 0 : static OString impl_getHostname()
43 : {
44 0 : OString aHost;
45 : #ifdef WNT
46 : /*
47 : prevent windows from connecting to the net to get its own
48 : hostname by using the netbios name
49 : */
50 : sal_Int32 sz = MAX_COMPUTERNAME_LENGTH + 1;
51 : char* szHost = new char[sz];
52 : if (GetComputerName(szHost, (LPDWORD)&sz))
53 : aHost = OString(szHost);
54 : else
55 : aHost = OString("UNKNOWN");
56 : delete[] szHost;
57 : #else
58 : /* Don't do dns lookup on Linux either */
59 : sal_Char pHostName[1024];
60 :
61 0 : if ( gethostname( pHostName, sizeof( pHostName ) - 1 ) == 0 )
62 : {
63 0 : pHostName[sizeof( pHostName ) - 1] = '\0';
64 0 : aHost = OString( pHostName );
65 : }
66 : else
67 0 : aHost = OString("UNKNOWN");
68 : #endif
69 :
70 0 : return aHost;
71 : }
72 :
73 : namespace desktop {
74 :
75 0 : Lockfile::Lockfile( bool bIPCserver )
76 : :m_bIPCserver(bIPCserver)
77 : ,m_bRemove(sal_False)
78 0 : ,m_bIsLocked(sal_False)
79 : {
80 : // build the file-url to use for the lock
81 0 : OUString aUserPath;
82 0 : utl::Bootstrap::locateUserInstallation( aUserPath );
83 0 : m_aLockname = aUserPath + LOCKFILE_SUFFIX;
84 :
85 : // generate ID
86 0 : const int nIdBytes = 16;
87 : char tmpId[nIdBytes*2+1];
88 : time_t t;
89 0 : srand( (unsigned)(t = time( NULL )) );
90 0 : int tmpByte = 0;
91 0 : for (int i = 0; i<nIdBytes; i++) {
92 0 : tmpByte = rand( ) % 0xFF;
93 0 : sprintf( tmpId+i*2, "%02X", tmpByte );
94 : }
95 0 : tmpId[nIdBytes*2]=0x00;
96 0 : m_aId = OUString::createFromAscii( tmpId );
97 :
98 : // generate date string
99 0 : char *tmpTime = ctime( &t );
100 0 : if (tmpTime != NULL) {
101 0 : m_aDate = OUString::createFromAscii( tmpTime );
102 0 : sal_Int32 i = m_aDate.indexOf('\n');
103 0 : if (i > 0)
104 0 : m_aDate = m_aDate.copy(0, i);
105 : }
106 :
107 :
108 : // try to create file
109 0 : File aFile(m_aLockname);
110 0 : if (aFile.open( osl_File_OpenFlag_Create ) == File::E_EXIST) {
111 0 : m_bIsLocked = sal_True;
112 : } else {
113 : // new lock created
114 0 : aFile.close( );
115 0 : syncToFile( );
116 0 : m_bRemove = sal_True;
117 0 : }
118 0 : }
119 :
120 0 : sal_Bool Lockfile::check( fpExecWarning execWarning )
121 : {
122 :
123 0 : if (m_bIsLocked) {
124 : // lock existed, ask user what to do
125 0 : if (isStale() ||
126 0 : (execWarning != 0 && (*execWarning)( this ))) {
127 : // remove file and create new
128 0 : File::remove( m_aLockname );
129 0 : File aFile(m_aLockname);
130 0 : aFile.open( osl_File_OpenFlag_Create );
131 0 : aFile.close( );
132 0 : syncToFile( );
133 0 : m_bRemove = sal_True;
134 0 : return sal_True;
135 : } else {
136 : //leave alone and return false
137 0 : m_bRemove = sal_False;
138 0 : return sal_False;
139 : }
140 : } else {
141 : // lock was created by us
142 0 : return sal_True;
143 : }
144 : }
145 :
146 0 : sal_Bool Lockfile::isStale( void ) const
147 : {
148 : // this checks whether the lockfile was created on the same
149 : // host by the same user. Should this be the case it is safe
150 : // to assume that it is a stale lockfile which can be overwritten
151 0 : OUString aLockname = m_aLockname;
152 0 : Config aConfig(aLockname);
153 0 : aConfig.SetGroup(LOCKFILE_GROUP);
154 0 : OString aIPCserver = aConfig.ReadKey( LOCKFILE_IPCKEY );
155 0 : if (!aIPCserver.equalsIgnoreAsciiCase(OString("true")))
156 0 : return false;
157 :
158 0 : OString aHost = aConfig.ReadKey( LOCKFILE_HOSTKEY );
159 0 : OString aUser = aConfig.ReadKey( LOCKFILE_USERKEY );
160 :
161 : // lockfile from same host?
162 0 : OString myHost( impl_getHostname() );
163 0 : if (aHost == myHost) {
164 : // lockfile by same UID
165 0 : OUString myUserName;
166 0 : Security aSecurity;
167 0 : aSecurity.getUserName( myUserName );
168 0 : OString myUser(OUStringToOString(myUserName, RTL_TEXTENCODING_ASCII_US));
169 0 : if (aUser == myUser)
170 0 : return sal_True;
171 : }
172 0 : return sal_False;
173 : }
174 :
175 0 : void Lockfile::syncToFile( void ) const
176 : {
177 0 : OUString aLockname = m_aLockname;
178 0 : Config aConfig(aLockname);
179 0 : aConfig.SetGroup(LOCKFILE_GROUP);
180 :
181 : // get information
182 0 : OString aHost( impl_getHostname() );
183 0 : OUString aUserName;
184 0 : Security aSecurity;
185 0 : aSecurity.getUserName( aUserName );
186 0 : OString aUser = OUStringToOString( aUserName, RTL_TEXTENCODING_ASCII_US );
187 0 : OString aTime = OUStringToOString( m_aDate, RTL_TEXTENCODING_ASCII_US );
188 0 : OString aStamp = OUStringToOString( m_aId, RTL_TEXTENCODING_ASCII_US );
189 :
190 : // write information
191 0 : aConfig.WriteKey( LOCKFILE_USERKEY, aUser );
192 0 : aConfig.WriteKey( LOCKFILE_HOSTKEY, aHost );
193 0 : aConfig.WriteKey( LOCKFILE_STAMPKEY, aStamp );
194 0 : aConfig.WriteKey( LOCKFILE_TIMEKEY, aTime );
195 : aConfig.WriteKey(
196 : LOCKFILE_IPCKEY,
197 0 : m_bIPCserver ? OString("true") : OString("false") );
198 0 : aConfig.Flush( );
199 0 : }
200 :
201 0 : Lockfile::~Lockfile( void )
202 : {
203 : // unlock userdata by removing file
204 0 : if ( m_bRemove )
205 0 : File::remove( m_aLockname );
206 0 : }
207 : }
208 :
209 :
210 :
211 :
212 :
213 :
214 :
215 :
216 :
217 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|