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
9 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // License: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #pragma implementation "snglinst.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_SNGLINST_CHECKER
35 #include "wx/string.h"
41 #include "wx/utils.h" // wxGetHomeDir()
43 #include "wx/snglinst.h"
46 #include <sys/types.h>
48 #include <signal.h> // for kill()
52 #elif defined(HAVE_FLOCK)
55 // normally, wxUSE_SNGLINST_CHECKER must have been reset by configure
56 #error "wxSingleInstanceChecker can't be compiled on this platform"
57 #endif // fcntl()/flock()
59 // ----------------------------------------------------------------------------
60 // private functions: (exclusively) lock/unlock the file
61 // ----------------------------------------------------------------------------
71 static int wxLockFile(int fd
, LockOperation lock
)
73 // init the flock parameter struct
75 fl
.l_type
= lock
== LOCK ? F_WRLCK
: F_UNLCK
;
77 // lock the entire file
85 return fcntl(fd
, F_SETLK
, &fl
);
90 static int wxLockFile(int fd
, LockOperation lock
)
92 return flock(fd
, lock
== LOCK ? LOCK_EX
| LOCK_NB
: LOCK_UN
);
95 #endif // fcntl()/flock()
97 // ----------------------------------------------------------------------------
98 // wxSingleInstanceCheckerImpl: the real implementation class
99 // ----------------------------------------------------------------------------
101 class wxSingleInstanceCheckerImpl
104 wxSingleInstanceCheckerImpl()
110 bool Create(const wxString
& name
);
112 pid_t
GetLockerPID() const { return m_pidLocker
; }
114 ~wxSingleInstanceCheckerImpl() { Unlock(); }
117 // try to create and lock the file
118 bool CreateLockFile();
120 // unlock and remove the lock file
123 // the descriptor of our lock file, -1 if none
126 // pid of the process owning the lock file
129 // the name of the lock file
133 // ============================================================================
134 // wxSingleInstanceCheckerImpl implementation
135 // ============================================================================
137 bool wxSingleInstanceCheckerImpl
::CreateLockFile()
139 // try to open the file
140 m_fdLock
= open(m_nameLock
,
141 O_WRONLY
| O_CREAT
| O_EXCL
,
144 if ( m_fdLock
!= -1 )
147 if ( wxLockFile(m_fdLock
, LOCK
) == 0 )
149 // fine, we have the exclusive lock to the file, write our PID
151 m_pidLocker
= getpid();
153 // use char here, not wxChar!
154 char buf
[256]; // enough for any PID size
155 int len
= sprintf(buf
, "%d", m_pidLocker
) + 1;
157 if ( write(m_fdLock
, buf
, len
) != len
)
159 wxLogSysError(_("Failed to write to lock file '%s'"),
172 // couldn't lock: this might have happened because of a race
173 // condition: maybe another instance opened and locked the file
174 // between our calls to open() and flock()
179 // we didn't create and lock the file
183 bool wxSingleInstanceCheckerImpl
::Create(const wxString
& name
)
187 if ( CreateLockFile() )
189 // nothing more to do
193 // try to open the file for reading and get the PID of the process
195 wxFile
file(name
, wxFile
::read
);
196 if ( !file
.IsOpened() )
198 // well, this is really weird - file doesn't exist and we can't
201 // normally, this just means that we don't have write access to
202 // the directory where we try to create it, so return failure,
203 // even it might also be a rare case of a race condition when
204 // another process managed to open and lock the file and terminate
205 // (erasing it) before we got here, but this should happen so
206 // rarely in practice that we don't care
207 wxLogError(_("Failed to access lock file."));
213 off_t count
= file
.Read(buf
, WXSIZEOF(buf
));
214 if ( count
== wxInvalidOffset
)
216 wxLogError(_("Failed to read PID from lock file."));
220 if ( sscanf(buf
, "%d", &m_pidLocker
) == 1 )
222 if ( kill(m_pidLocker
, 0) != 0 )
224 if ( unlink(name
) != 0 )
226 wxLogError(_("Failed to remove stale lock file '%s'."),
229 // return TRUE in this case for now...
233 wxLogMessage(_("Deleted stale lock file '%s'."),
237 (void)CreateLockFile();
240 //else: the other process is running
244 wxLogWarning(_("Invalid lock file '%s'."));
248 // return TRUE if we could get the PID of the process owning the lock file
249 // (whether it is still running or not), FALSE otherwise as it is
251 return m_pidLocker
!= 0;
254 void wxSingleInstanceCheckerImpl
::Unlock()
256 if ( m_fdLock
!= -1 )
258 if ( unlink(m_nameLock
) != 0 )
260 wxLogSysError(_("Failed to remove lock file '%s'"),
264 if ( wxLockFile(m_fdLock
, UNLOCK
) != 0 )
266 wxLogSysError(_("Failed to unlock lock file '%s'"),
270 if ( close(m_fdLock
) != 0 )
272 wxLogSysError(_("Failed to close lock file '%s'"),
280 // ============================================================================
281 // wxSingleInstanceChecker implementation
282 // ============================================================================
284 bool wxSingleInstanceChecker
::Create(const wxString
& name
,
285 const wxString
& path
)
287 wxASSERT_MSG( !m_impl
,
288 _T("calling wxSingleInstanceChecker::Create() twice?") );
290 // must have the file name to create a lock file
291 wxASSERT_MSG( !name
.empty(), _T("lock file name can't be empty") );
293 m_impl
= new wxSingleInstanceCheckerImpl
;
295 wxString fullname
= path
;
296 if ( fullname
.empty() )
298 fullname
<< wxGetHomeDir() << _T('/');
303 return m_impl
->Create(fullname
);
306 bool wxSingleInstanceChecker
::IsAnotherRunning() const
308 wxCHECK_MSG( m_impl
, FALSE
, _T("must call Create() first") );
310 // if another instance is running, it must own the lock file - otherwise
311 // we have it and the locker PID is ours one
312 return m_impl
->GetLockerPID() != getpid();
315 wxSingleInstanceChecker
::~wxSingleInstanceChecker()
320 #endif // wxUSE_SNGLINST_CHECKER