home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_log_agent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  6.4 KB  |  189 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58.  
  59. #include "httpd.h"
  60. #include "http_config.h"
  61. #include "http_log.h"
  62.  
  63. module agent_log_module;
  64.  
  65. static int xfer_flags = (O_WRONLY | O_APPEND | O_CREAT);
  66. #ifdef OS2
  67. /* OS/2 dosen't support users and groups */
  68. static mode_t xfer_mode = (S_IREAD | S_IWRITE);
  69. #else
  70. static mode_t xfer_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  71. #endif
  72.  
  73. typedef struct {
  74.     char *fname;
  75.     int agent_fd;
  76. } agent_log_state;
  77.  
  78. static void *make_agent_log_state(pool *p, server_rec *s)
  79. {
  80.     agent_log_state *cls =
  81.     (agent_log_state *) ap_palloc(p, sizeof(agent_log_state));
  82.  
  83.     cls->fname = "";
  84.     cls->agent_fd = -1;
  85.  
  86.     return (void *) cls;
  87. }
  88.  
  89. static const char *set_agent_log(cmd_parms *parms, void *dummy, char *arg)
  90. {
  91.     agent_log_state *cls = ap_get_module_config(parms->server->module_config,
  92.                                              &agent_log_module);
  93.  
  94.     cls->fname = arg;
  95.     return NULL;
  96. }
  97.  
  98. static const command_rec agent_log_cmds[] =
  99. {
  100.     {"AgentLog", set_agent_log, NULL, RSRC_CONF, TAKE1,
  101.      "the filename of the agent log"},
  102.     {NULL}
  103. };
  104.  
  105. static void open_agent_log(server_rec *s, pool *p)
  106. {
  107.     agent_log_state *cls = ap_get_module_config(s->module_config,
  108.                                              &agent_log_module);
  109.  
  110.     char *fname = ap_server_root_relative(p, cls->fname);
  111.  
  112.     if (cls->agent_fd > 0)
  113.         return;                 /* virtual log shared w/main server */
  114.  
  115.     if (*cls->fname == '|') {
  116.         piped_log *pl;
  117.  
  118.         pl = ap_open_piped_log(p, cls->fname + 1);
  119.         if (pl == NULL) {
  120.         ap_log_error(APLOG_MARK, APLOG_ERR, s,
  121.              "couldn't spawn agent log pipe");
  122.             exit(1);
  123.         }
  124.         cls->agent_fd = ap_piped_log_write_fd(pl);
  125.     }
  126.     else if (*cls->fname != '\0') {
  127.         if ((cls->agent_fd = ap_popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  128.             ap_log_error(APLOG_MARK, APLOG_ERR, s,
  129.                          "could not open agent log file %s.", fname);
  130.             exit(1);
  131.         }
  132.     }
  133. }
  134.  
  135. static void init_agent_log(server_rec *s, pool *p)
  136. {
  137.     for (; s; s = s->next)
  138.         open_agent_log(s, p);
  139. }
  140.  
  141. static int agent_log_transaction(request_rec *orig)
  142. {
  143.     agent_log_state *cls = ap_get_module_config(orig->server->module_config,
  144.                                              &agent_log_module);
  145.  
  146.     char str[HUGE_STRING_LEN];
  147.     const char *agent;
  148.     request_rec *r;
  149.  
  150.     if (cls->agent_fd < 0)
  151.         return OK;
  152.  
  153.     for (r = orig; r->next; r = r->next)
  154.         continue;
  155.     if (*cls->fname == '\0')    /* Don't log agent */
  156.         return DECLINED;
  157.  
  158.     agent = ap_table_get(orig->headers_in, "User-Agent");
  159.     if (agent != NULL) {
  160.         ap_snprintf(str, sizeof(str), "%s\n", agent);
  161.         write(cls->agent_fd, str, strlen(str));
  162.     }
  163.  
  164.     return OK;
  165. }
  166.  
  167. module agent_log_module =
  168. {
  169.     STANDARD_MODULE_STUFF,
  170.     init_agent_log,             /* initializer */
  171.     NULL,                       /* create per-dir config */
  172.     NULL,                       /* merge per-dir config */
  173.     make_agent_log_state,       /* server config */
  174.     NULL,                       /* merge server config */
  175.     agent_log_cmds,             /* command table */
  176.     NULL,                       /* handlers */
  177.     NULL,                       /* filename translation */
  178.     NULL,                       /* check_user_id */
  179.     NULL,                       /* check auth */
  180.     NULL,                       /* check access */
  181.     NULL,                       /* type_checker */
  182.     NULL,                       /* fixups */
  183.     agent_log_transaction,      /* logger */
  184.     NULL,                       /* header parser */
  185.     NULL,                       /* child_init */
  186.     NULL,                       /* child_exit */
  187.     NULL                        /* post read-request */
  188. };
  189.