Daemons and flie locking

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


Tested on RedHat and Ubuntu

I am trying to create a daemon the tails and parses a "binary" (fixed
length record) log-file.  I have tried using Proc::Daemon and rolling
my own daemonization code and I really don't think that is the issue
though.  My problem is when I try to create a run-lock pid file in /

the basic flow is:

- start syslog
- become a daemon
- try to create/open & flock a pid file
- ... everything else

I know syslog is working even after the forks.  I know the forks are
working.  I know the pid file is being initially created but flock
bombs and tells me "Inappropriate ioctl for device".

the pid file open code is:

        die "Can not open pid file: $PIDFILEPATH\n";
    if (flock($PIDFILE, LOCK_EX | LOCK_NB) != 0) {
        die "Can not lock pid file: $PIDFILEPATH: $!: $?\n";
    truncate($PIDFILE, 0) ||
        die "Can not truncate pid file: $PIDFILEPATH\n";
    print $PIDFILE "$$\n";

The "$PIDFILEPATH" is /var/run/filename.pid


Re: Daemons and flie locking

Quoted text here. Click to load it

Perl's flock returns true on success and false on failure. All false
values compare numerically equal to 0, so this test is exactly
backwards. The value in $! is from some other error. Try

    if (!flock($PIDFILE, LOCK_EX|LOCK_NB)) {

or, in the same style as the other lines

    flock($PIDFILE, LOCK_EX|LOCK_NB) ||
        die ...;

Quoted text here. Click to load it

Why do you include $? here? What possible relevance does the exit status
of the last waited process have?


Re: Daemons and flie locking

Quoted text here. Click to load it

According to 'perodoc -f flock', flock "Returns true for success, false
on failure.", i.e much the same as sysopen and truncate.

Quoted text here. Click to load it

Brian Raven

Re: Daemons and flie locking

Ben, Brian,

You are both absolutely correct.  Bone-headed me.  Same thing with the
"$?".  I have been floundering for a while and forgot what all I had
tried.  I still have issues but it is not this.

thanks for the extra eyes

Re: Daemons and flie locking


Quoted text here. Click to load it

Even though this posting is about mod_perl, look at this:

But, if you search for "Inappropriate ioctl for device" on
the internet it seems to be default $! for the open function
via ENOTTY for it.

So even though open worked, the flock is or'd with
a non-blocking wait and returns right away.
You have to check the return value true/false.
But it looks like you die if the return value is true ( != 0 ).

The other thing might be the file permission, but it opened ok.

Try the flock without the non-blocking.
    if (!flock($PIDFILE, LOCK_EX)) {
        die "Can not lock pid file: $PIDFILEPATH: $!: $?\n";

Of course it could be anything. It depends on what your
OS does with locks.
I'm no expert, its just what I read about it.


Re: Daemons and flie locking

Dean Karres wrote:

Quoted text here. Click to load it

Others have already pointed out the wrong sense on your err check.  I
thought that that was the case before even seeing your code, because to
"Inappropriate ioctl" just screams 'You are inspecting $! when you ought
not be'.  I don't think I've ever seen that error reported under any
other situation.


Site Timeline