Daemons and flie locking

D

Dean Karres

Hi,

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 /
var/run.

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:

sysopen($PIDFILE, $PIDFILEPATH, O_CREAT | O_WRONLY) ||
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

ideas?
 
B

Brian Raven

Dean Karres said:
Hi,

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 /
var/run.

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:

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

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

Dean Karres

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
 
S

sln

Hi,

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 /
var/run.

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:

sysopen($PIDFILE, $PIDFILEPATH, O_CREAT | O_WRONLY) ||
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

ideas?

Even though this posting is about mod_perl, look at this:
http://www.perlmonks.org/?node_id=837196

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.

-sln
 
X

Xho Jingleheimerschmidt

Dean Karres wrote:
....
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".

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.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top