best practice proposal: name locking and process exclusion

Lennart Poettering mzqohf at 0pointer.de
Thu Apr 7 10:52:02 PDT 2011


On Thu, 07.04.11 13:22, Colin Walters (walters at verbum.org) wrote:

> 
> On Thu, Apr 7, 2011 at 1:06 PM, Lennart Poettering <mzqohf at 0pointer.de> wrote:
> >
> > You want to claim it so early that you want to claim it actually before
> > the process is even spawned, which you can do by using bus activation.
> 
> I'm not parsing this.

You said you wanted this lock to be taken early in the process. I was suggesting that
actually you want this lock taken *before* the process is actually
started. Hence bus activation.

> > 1. If I am not bus activated send Ping() to my own bus name, thus
> >   activating a bus activated version of myself. Exit.
> > 2. Otherwise, run mainloop and stuff.
> 
> This has two undesirable aspects, namely:
> 
> * It forces apps to install a .service file; I was thinking of using
> this for GApplication, and I can't retroactively say "oh and yeah, you
> need to make a .service file".

It is my plan btw to get rid of this requirement, and make D-Bus
activate a service by mere presence of a systemd unit file (or XDG
.desktop file) but I figure that won't help you here much...

I think the easiest solution would be to use a lock file in
XDG_RUNTIME_DIR:

<snip>
fd = open($XDG_RUNTIME_DIR + $APPNAME, O_CREAT|O_RDWR, 0700);
if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
     g_error("Already running.");
     exit(1);
}
</snip>

And just leave the fd open. The lock will be released automatically when
the process exits. If you want to be nice you could even add an
unlink($XDG_RUNTIME_DIR + $APPNAME) as an atexit handler, which would be
one of the very few cases where atexit() would not be an
abomination. But there's not much of a need to clean the file up really,
as it would be cleaned up on session exit anyway, and its mere existance
doesn't block anything else from running. The BSD lock on it is what
matters.

And don't think of using POSIX instead of BSD locks: they are unusably
broken.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the dbus mailing list