You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Dmitri Tikhonov <dm...@netilla.com> on 2003/05/08 19:14:28 UTC

flock() does not work in Apache2 module.

I've run into an interesting problem: my module needs to read
configuration from a file that is occasionally changed by another process.
I've created a test C program that reads configuration if it can obtain a
lock using flock(2) function:

    if (-1 == flock(fd, LOCK_SH|LOCK_NB)) {
        /* Could not lock... go with old config. */
        return -4;
    }

I tested it by locking the file using perl script:

    perl -we 'use Fcntl qw(:flock); \
	open(F, "/tmp/cnf"); \
	flock(F, LOCK_EX); \
	print "Locked.\n"; \
	sleep(20);'

However, when I put the function into my module, flock() always gets the
lock, no matter what.  Question: has anyone seen this before?  is there
a side-effect of Apache environment that I'm not aware of?

My set-up: Linux 2.4.21pre5, Apache 2.0.45, Perl 5.6.1, glibc 2.2.4.

  - Dmitri.



Re: flock() does not work in Apache2 module.

Posted by Jeff Trawick <tr...@attglobal.net>.
Dmitri Tikhonov wrote:
> I've run into an interesting problem: my module needs to read
> configuration from a file that is occasionally changed by another process.
> I've created a test C program that reads configuration if it can obtain a
> lock using flock(2) function:
> 
>     if (-1 == flock(fd, LOCK_SH|LOCK_NB)) {
>         /* Could not lock... go with old config. */
>         return -4;
>     }

Perhaps you mean that multiple threads in the same process calling into 
your module can the flock() mutex simultaneously?

I strongly suggest using APR locks, which do the right thing w.r.t. 
threads and processes.



Re: flock() does not work in Apache2 module.

Posted by Dmitri Tikhonov <dm...@netilla.com>.

On Thu, 8 May 2003, William A. Rowe, Jr. wrote:

> At 12:14 PM 5/8/2003, Dmitri Tikhonov wrote:
>
> >I've run into an interesting problem: my module needs to read
> >configuration from a file that is occasionally changed by another process.
> >I've created a test C program that reads configuration if it can obtain a
> >lock using flock(2) function:
> >
> >    if (-1 == flock(fd, LOCK_SH|LOCK_NB)) {
>
> Why not apr_file_lock?

Because on unix boxes, apr_file_lock will use fnctl, but I want just the
advisory lock.

> If not - why would you be asking here {dev@apr}?

I suspect that Apache libraries have something to do with flock not
working properly.

> >However, when I put the function into my module, flock() always gets the
> >lock, no matter what.  Question: has anyone seen this before?  is there
> >a side-effect of Apache environment that I'm not aware of?
>
> Ok, the answer (this applies to unix apr_file_lock too) ... unix flock is
> an advisory lock - it isn't enforced except between processes that choose
> to use it.
>
> So unless the other application that modifies that file chooses to use
> flock, you can't use flock to sync updates between two apps.

I know, and they both use flock.  The problem is that the other
application is written in Perl, and I don't want to use Apache::APR.

I could use some other locking scheme, such as a lock file (.lck), because
updates do not happen that often, but it wouldn't be as clean as I'd like.

Thanks,
  - Dmitri.



Re: flock() does not work in Apache2 module.

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
At 12:14 PM 5/8/2003, Dmitri Tikhonov wrote:

>I've run into an interesting problem: my module needs to read
>configuration from a file that is occasionally changed by another process.
>I've created a test C program that reads configuration if it can obtain a
>lock using flock(2) function:
>
>    if (-1 == flock(fd, LOCK_SH|LOCK_NB)) {

Why not apr_file_lock?  If not - why would you be asking here {dev@apr}?

>However, when I put the function into my module, flock() always gets the
>lock, no matter what.  Question: has anyone seen this before?  is there
>a side-effect of Apache environment that I'm not aware of?

Ok, the answer (this applies to unix apr_file_lock too) ... unix flock is
an advisory lock - it isn't enforced except between processes that choose
to use it.

So unless the other application that modifies that file chooses to use
flock, you can't use flock to sync updates between two apps.


>My set-up: Linux 2.4.21pre5, Apache 2.0.45, Perl 5.6.1, glibc 2.2.4.