httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrej van der Zee" <>
Subject apr_open_file and apr_write_file in post_read_request and log_transaction
Date Tue, 09 Sep 2008 14:10:11 GMT

I am new to Apache modules and I am trying to open a file in the hook
post_read_request and write to the file in log_transaction. The file
is supposed to be only valid for the duration of the request and is
named with a unique identifier (created by mod_unique_id). The
apr_file_t is carried, with other data, in the configuration vector
(request_config) between the different hooks. In post_read_reqeust I
open it like this (req_cfg is the configuration vector

    req_cfg->log_file = NULL;
    req_cfg->filepath = (char*) apr_pstrcat(r->pool, path_name,
unique_id, NULL);
    apr_int32_t flags = APR_WRITE | APR_CREATE | APR_EXCL | APR_APPEND;
    rv = apr_file_open(&req_cfg->log_file, req_cfg->filepath, flags,
APR_OS_DEFAULT, r->pool);
    if (rv != APR_SUCCESS || req_cfg->log_file == NULL) {
       ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to create
resource log file: %s",
       return DECLINED;

After this I am able to write to the file in this hook function. But,
when I try to write to the file in the hook log_transaction like this:

    char *msg = (char*) apr_psprintf(r->pool, "%s", "some msg");
    len = strlen(msg);
    apr_file_write(req_cfg->log_file, msg, &len);

Then I get a segfault because req_cfg->log_file is NULL. Here the
output from gdb:

Core was generated by `/usr/local/apache2/bin/httpd -k start'.
Program terminated with signal 11, Segmentation fault.
[New process 16759]
#0  apr_file_write (thefile=0x0, buf=0x81841d0, nbytes=0xbf8e5d94) at
151	    if (thefile->buffered) {
(gdb) print thefile
$1 = (apr_file_t *) 0x0
(gdb) Quit

Because the file is opened with r->pool (with r request_rec) in
post_read_request I expect it to be open in the hook log_transaction.
So I guess I am wrong? Is it closed by the cleanup handlers? And if
so, what is the way to make sure it is still open?


View raw message