]>
Commit | Line | Data |
---|---|---|
4f3c5f06 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: unix/snglinst.cpp | |
3 | // Purpose: implements wxSingleInstanceChecker class for Unix using | |
4 | // lock files with fcntl(2) or flock(2) | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 09.06.01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
10 | // License: wxWindows license | |
11 | /////////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | #ifdef __GNUG__ | |
22 | #pragma implementation "snglinst.h" | |
23 | #endif | |
24 | ||
25 | // For compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #if wxUSE_SNGLINST_CHECKER | |
33 | ||
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/string.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/file.h" | |
39 | #endif //WX_PRECOMP | |
40 | ||
41 | #include "wx/utils.h" // wxGetHomeDir() | |
42 | ||
43 | #include "wx/snglinst.h" | |
44 | ||
45 | #include <unistd.h> | |
46 | #include <sys/types.h> | |
47 | #include <sys/stat.h> | |
48 | #include <signal.h> // for kill() | |
b5299791 | 49 | #include <errno.h> |
4f3c5f06 VZ |
50 | |
51 | #ifdef HAVE_FCNTL | |
52 | #include <fcntl.h> | |
53 | #elif defined(HAVE_FLOCK) | |
54 | #include <sys/file.h> | |
55 | #else | |
56 | // normally, wxUSE_SNGLINST_CHECKER must have been reset by configure | |
57 | #error "wxSingleInstanceChecker can't be compiled on this platform" | |
58 | #endif // fcntl()/flock() | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
b5299791 | 61 | // constants |
4f3c5f06 VZ |
62 | // ---------------------------------------------------------------------------- |
63 | ||
b5299791 | 64 | // argument of wxLockFile() |
4f3c5f06 VZ |
65 | enum LockOperation |
66 | { | |
67 | LOCK, | |
68 | UNLOCK | |
69 | }; | |
70 | ||
b5299791 VZ |
71 | // return value of CreateLockFile() |
72 | enum LockResult | |
73 | { | |
74 | LOCK_ERROR = -1, | |
75 | LOCK_EXISTS, | |
76 | LOCK_CREATED | |
77 | }; | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // private functions: (exclusively) lock/unlock the file | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
4f3c5f06 VZ |
83 | #ifdef HAVE_FCNTL |
84 | ||
85 | static int wxLockFile(int fd, LockOperation lock) | |
86 | { | |
87 | // init the flock parameter struct | |
88 | struct flock fl; | |
89 | fl.l_type = lock == LOCK ? F_WRLCK : F_UNLCK; | |
90 | ||
91 | // lock the entire file | |
92 | fl.l_whence = | |
93 | fl.l_start = | |
94 | fl.l_len = 0; | |
95 | ||
96 | // is this needed? | |
97 | fl.l_pid = getpid(); | |
98 | ||
99 | return fcntl(fd, F_SETLK, &fl); | |
100 | } | |
101 | ||
102 | #else // HAVE_FLOCK | |
103 | ||
104 | static int wxLockFile(int fd, LockOperation lock) | |
105 | { | |
106 | return flock(fd, lock == LOCK ? LOCK_EX | LOCK_NB : LOCK_UN); | |
107 | } | |
108 | ||
109 | #endif // fcntl()/flock() | |
110 | ||
111 | // ---------------------------------------------------------------------------- | |
112 | // wxSingleInstanceCheckerImpl: the real implementation class | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | class wxSingleInstanceCheckerImpl | |
116 | { | |
117 | public: | |
118 | wxSingleInstanceCheckerImpl() | |
119 | { | |
120 | m_fdLock = -1; | |
121 | m_pidLocker = 0; | |
122 | } | |
123 | ||
124 | bool Create(const wxString& name); | |
125 | ||
126 | pid_t GetLockerPID() const { return m_pidLocker; } | |
127 | ||
128 | ~wxSingleInstanceCheckerImpl() { Unlock(); } | |
129 | ||
130 | private: | |
131 | // try to create and lock the file | |
b5299791 | 132 | LockResult CreateLockFile(); |
4f3c5f06 VZ |
133 | |
134 | // unlock and remove the lock file | |
135 | void Unlock(); | |
136 | ||
137 | // the descriptor of our lock file, -1 if none | |
138 | int m_fdLock; | |
139 | ||
140 | // pid of the process owning the lock file | |
141 | pid_t m_pidLocker; | |
142 | ||
143 | // the name of the lock file | |
144 | wxString m_nameLock; | |
145 | }; | |
146 | ||
147 | // ============================================================================ | |
148 | // wxSingleInstanceCheckerImpl implementation | |
149 | // ============================================================================ | |
150 | ||
b5299791 | 151 | LockResult wxSingleInstanceCheckerImpl::CreateLockFile() |
4f3c5f06 VZ |
152 | { |
153 | // try to open the file | |
154 | m_fdLock = open(m_nameLock, | |
155 | O_WRONLY | O_CREAT | O_EXCL, | |
156 | S_IREAD | S_IWRITE); | |
157 | ||
158 | if ( m_fdLock != -1 ) | |
159 | { | |
160 | // try to lock it | |
b5299791 VZ |
161 | int rc = wxLockFile(m_fdLock, LOCK); |
162 | if ( rc == 0 ) | |
4f3c5f06 VZ |
163 | { |
164 | // fine, we have the exclusive lock to the file, write our PID | |
165 | // into it | |
166 | m_pidLocker = getpid(); | |
167 | ||
168 | // use char here, not wxChar! | |
169 | char buf[256]; // enough for any PID size | |
170 | int len = sprintf(buf, "%d", m_pidLocker) + 1; | |
171 | ||
172 | if ( write(m_fdLock, buf, len) != len ) | |
173 | { | |
174 | wxLogSysError(_("Failed to write to lock file '%s'"), | |
175 | m_nameLock.c_str()); | |
176 | ||
177 | Unlock(); | |
178 | ||
b5299791 | 179 | return LOCK_ERROR; |
4f3c5f06 VZ |
180 | } |
181 | ||
182 | fsync(m_fdLock); | |
183 | ||
b5299791 | 184 | return LOCK_CREATED; |
4f3c5f06 | 185 | } |
b5299791 VZ |
186 | else // failure: see what exactly happened |
187 | { | |
188 | close(m_fdLock); | |
189 | m_fdLock = -1; | |
4f3c5f06 | 190 | |
b5299791 VZ |
191 | if ( rc != EACCES && rc != EAGAIN ) |
192 | { | |
193 | wxLogSysError(_("Failed to lock the lock file '%s'"), | |
194 | m_nameLock.c_str()); | |
195 | ||
196 | unlink(m_nameLock); | |
197 | ||
198 | return LOCK_ERROR; | |
199 | } | |
200 | //else: couldn't lock because the lock is held by another process: | |
201 | // this might have happened because of a race condition: | |
202 | // maybe another instance opened and locked the file between | |
203 | // our calls to open() and flock(), so don't give an error | |
204 | } | |
4f3c5f06 VZ |
205 | } |
206 | ||
207 | // we didn't create and lock the file | |
b5299791 | 208 | return LOCK_EXISTS; |
4f3c5f06 VZ |
209 | } |
210 | ||
211 | bool wxSingleInstanceCheckerImpl::Create(const wxString& name) | |
212 | { | |
213 | m_nameLock = name; | |
214 | ||
b5299791 | 215 | switch ( CreateLockFile() ) |
4f3c5f06 | 216 | { |
b5299791 VZ |
217 | case LOCK_EXISTS: |
218 | // there is a lock file, check below if it is still valid | |
219 | break; | |
220 | ||
221 | case LOCK_CREATED: | |
222 | // nothing more to do | |
223 | return TRUE; | |
224 | ||
225 | case LOCK_ERROR: | |
226 | // oops... | |
227 | return FALSE; | |
4f3c5f06 VZ |
228 | } |
229 | ||
230 | // try to open the file for reading and get the PID of the process | |
231 | // which has it | |
232 | wxFile file(name, wxFile::read); | |
233 | if ( !file.IsOpened() ) | |
234 | { | |
235 | // well, this is really weird - file doesn't exist and we can't | |
236 | // create it | |
237 | // | |
238 | // normally, this just means that we don't have write access to | |
239 | // the directory where we try to create it, so return failure, | |
240 | // even it might also be a rare case of a race condition when | |
241 | // another process managed to open and lock the file and terminate | |
242 | // (erasing it) before we got here, but this should happen so | |
243 | // rarely in practice that we don't care | |
244 | wxLogError(_("Failed to access lock file.")); | |
245 | ||
246 | return FALSE; | |
247 | } | |
248 | ||
249 | char buf[256]; | |
250 | off_t count = file.Read(buf, WXSIZEOF(buf)); | |
251 | if ( count == wxInvalidOffset ) | |
252 | { | |
253 | wxLogError(_("Failed to read PID from lock file.")); | |
254 | } | |
255 | else | |
256 | { | |
257 | if ( sscanf(buf, "%d", &m_pidLocker) == 1 ) | |
258 | { | |
259 | if ( kill(m_pidLocker, 0) != 0 ) | |
260 | { | |
261 | if ( unlink(name) != 0 ) | |
262 | { | |
263 | wxLogError(_("Failed to remove stale lock file '%s'."), | |
264 | name.c_str()); | |
265 | ||
266 | // return TRUE in this case for now... | |
267 | } | |
268 | else | |
269 | { | |
270 | wxLogMessage(_("Deleted stale lock file '%s'."), | |
271 | name.c_str()); | |
272 | ||
273 | // retry now | |
274 | (void)CreateLockFile(); | |
275 | } | |
276 | } | |
277 | //else: the other process is running | |
278 | } | |
279 | else | |
280 | { | |
b5299791 | 281 | wxLogWarning(_("Invalid lock file '%s'."), name.c_str()); |
4f3c5f06 VZ |
282 | } |
283 | } | |
284 | ||
285 | // return TRUE if we could get the PID of the process owning the lock file | |
286 | // (whether it is still running or not), FALSE otherwise as it is | |
287 | // unexpected | |
288 | return m_pidLocker != 0; | |
289 | } | |
290 | ||
291 | void wxSingleInstanceCheckerImpl::Unlock() | |
292 | { | |
293 | if ( m_fdLock != -1 ) | |
294 | { | |
295 | if ( unlink(m_nameLock) != 0 ) | |
296 | { | |
297 | wxLogSysError(_("Failed to remove lock file '%s'"), | |
298 | m_nameLock.c_str()); | |
299 | } | |
300 | ||
301 | if ( wxLockFile(m_fdLock, UNLOCK) != 0 ) | |
302 | { | |
303 | wxLogSysError(_("Failed to unlock lock file '%s'"), | |
304 | m_nameLock.c_str()); | |
305 | } | |
306 | ||
307 | if ( close(m_fdLock) != 0 ) | |
308 | { | |
309 | wxLogSysError(_("Failed to close lock file '%s'"), | |
310 | m_nameLock.c_str()); | |
311 | } | |
312 | } | |
313 | ||
314 | m_pidLocker = 0; | |
315 | } | |
316 | ||
317 | // ============================================================================ | |
318 | // wxSingleInstanceChecker implementation | |
319 | // ============================================================================ | |
320 | ||
321 | bool wxSingleInstanceChecker::Create(const wxString& name, | |
322 | const wxString& path) | |
323 | { | |
324 | wxASSERT_MSG( !m_impl, | |
325 | _T("calling wxSingleInstanceChecker::Create() twice?") ); | |
326 | ||
327 | // must have the file name to create a lock file | |
328 | wxASSERT_MSG( !name.empty(), _T("lock file name can't be empty") ); | |
329 | ||
330 | m_impl = new wxSingleInstanceCheckerImpl; | |
331 | ||
332 | wxString fullname = path; | |
333 | if ( fullname.empty() ) | |
334 | { | |
335 | fullname << wxGetHomeDir() << _T('/'); | |
336 | } | |
337 | ||
338 | fullname << name; | |
339 | ||
340 | return m_impl->Create(fullname); | |
341 | } | |
342 | ||
343 | bool wxSingleInstanceChecker::IsAnotherRunning() const | |
344 | { | |
345 | wxCHECK_MSG( m_impl, FALSE, _T("must call Create() first") ); | |
346 | ||
347 | // if another instance is running, it must own the lock file - otherwise | |
348 | // we have it and the locker PID is ours one | |
349 | return m_impl->GetLockerPID() != getpid(); | |
350 | } | |
351 | ||
352 | wxSingleInstanceChecker::~wxSingleInstanceChecker() | |
353 | { | |
354 | delete m_impl; | |
355 | } | |
356 | ||
357 | #endif // wxUSE_SNGLINST_CHECKER | |
358 |