[Qt-interest] How to create single instance app?
Konrad Rosenbaum
konrad at silmor.de
Mon May 10 19:52:21 CEST 2010
On Monday 10 May 2010, Jeroen De Wachter wrote:
> Doesn't that act up if the application crashes and is unable to close
> the listen socket cleanly?
As far as my experience goes Qt sets the SO_REUSEADDR flag which prevents
this problem.
> I know I've spent far too much time waiting for crashed network-enabled
> applications' stuck sockets to get cleaned up by the OS (TIME_WAIT,
> anyone?) I'd hate to see that happen even with applications that don't
> use the network...
I think a socket is a bad idea for other reasons:
* it gives potential attackers another vector (even if bound to localhost)
* it squanders network and kernel ressources
* what do you do with incoming connections? even if you close them
immediately you have to handle them.
> I think the lock file mentioned before is a good way to go:
> - on application startup, check for the lock file
> - if it doesn't exist, create it and store the id of the process that
> creates the file inside that file
> - if it does exist, read the pid in the file and see if that process is
> still running
> - if not, replace the lock file and store your own process id inside
> - if the process is still running, return an error and quit
>
> I think start-stop-deamon on *nix system works like this as well, btw
> (though that may only cover invocation through start-stop-daemon)?
> And I'm sure Linux provides some API calls to work with lock files as
> well
I'm not entirely sure whether there is some high-level API for this. Or did
you mean flock(3)?
If you use low level POSIX stuff you can unify and secure the first two
steps by using open(...,O_WRONLY|O_EXCL);
With the normal Qt API you have a small race condition between the first two
steps. Probabilities of something going wrong are usually sufficiently low
for it to work in practice.
But the OP still did not answer what granularity of "single" he wanted, so
it is difficult to give a good recommendation.
The TCP solution is "one per computer", files can be used at any level as
long as there is a shared file system(*).
(*)even if it runs on tachion pulses. SCNR.
Konrad
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100510/5c5fa284/attachment.bin
More information about the Qt-interest-old
mailing list