apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Graham Leggett <minf...@sharp.fm>
Subject Re: opening named pipes
Date Tue, 18 Dec 2012 21:50:07 GMT
On 18 Dec 2012, at 11:41 PM, Chris Darroch <chrisd@pearsoncmg.com> wrote:

>  I recently needed to wire up httpd's rotatelogs to a named pipe (a FIFO)
> instead of to stdin.  For context, we have a non-httpd server process
> configured to write to the pipe instead of a log file; further, this
> server likes to intermittently open, write to, and close its various
> log files.
> 
>  I wanted/needed to open the pipe in non-blocking mode, so rotatelogs
> would start right away (we're using rotatelogs -cf) instead of blocking
> until a writer process happened to open the pipe to write some log data.
> 
>  My first attempt was to open the pipe as a regular file using
> apr_file_open(), then extract the fd with apr_os_file_get(), use it
> to create an APR pipe with apr_os_pipe_put(), and then set the
> non-blocking status with apr_file_pipe_timeout_set().  That doesn't
> work with APR 1.4.x, though, because the initial file open lacks
> O_NONBLOCK.
> 
>  With APR trunk I could use APR_FOPEN_NONBLOCK, but even so it's
> a bit of a workaround for something which I thought would be simple.

I added APR_FOPEN_NONBLOCK for this exact reason - non blocking writes to named pipes, for
the same use you're trying (writing log files to named pipes).

In theory APR_FOPEN_NONBLOCK could be backported to apr v1.5 and then released. One outstanding
thing to do is ensure APR_FOPEN_NONBLOCK returns APR_ENOTIMPL on platforms that don't support
it (yet).

>  At any rate, here's what I ended up with, an apr_file_namedpipe_open()
> call which tries to align with the usage of the 1.4.x APR_*_BLOCK flags
> in apr_file_pipe_create_ex(), although all that's really needed is
> a simple Boolean -- but we don't have APR_FOPEN_NONBLOCK in 1.4.x.
> 
>  Unlike the apr_file_open() trick I tried first, it won't let you
> open a non-pipe by accident, and it will set the timeout and blocking
> flags right away.

The trouble is that to use this, every app that wants to use pipes needs to use a special
call to do so, which isn't ideal.

In our case we wanted the named pipes to be interchangeable with files, and created an application
that summarised log file data on the fly before writing it to the named pipe (for example,
100 hits to "/foo" became "100: /foo").

Regards,
Graham
--


Mime
View raw message