home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / apache_1.0.5 / src / mod_log_common.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  6.7 KB  |  212 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55.  
  56. #include "httpd.h"
  57. #include "http_config.h"
  58.  
  59. module common_log_module;
  60.  
  61. static int xfer_flags = ( O_WRONLY | O_APPEND | O_CREAT );
  62. static mode_t xfer_mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
  63.  
  64. typedef struct {
  65.     char *fname;
  66.     int log_fd;
  67. } common_log_state;
  68.  
  69. void *make_common_log_state (pool *p, server_rec *s)
  70. {
  71.     common_log_state *cls =
  72.       (common_log_state *)palloc (p, sizeof (common_log_state));
  73.  
  74.     cls->fname = DEFAULT_XFERLOG;
  75.     cls->log_fd = -1;
  76.  
  77.     return (void *)cls;
  78. }
  79.  
  80. char *set_common_log (cmd_parms *parms, void *dummy, char *arg)
  81. {
  82.     common_log_state *cls = get_module_config (parms->server->module_config,
  83.                            &common_log_module);
  84.   
  85.     cls->fname = arg;
  86.     return NULL;
  87. }
  88.  
  89. command_rec common_log_cmds[] = {
  90. { "TransferLog", set_common_log, NULL, RSRC_CONF, TAKE1,
  91.     "the filename of the access log" },
  92. { NULL }
  93. };
  94.  
  95. void common_log_child (void *cmd)
  96. {
  97.     /* Child process code for 'TransferLog "|..."';
  98.      * may want a common framework for this, since I expect it will
  99.      * be common for other foo-loggers to want this sort of thing...
  100.      */
  101.     
  102.     cleanup_for_exec();
  103.     signal (SIGHUP, SIG_IGN);
  104.     execl (SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
  105.     fprintf (stderr, "Exec of shell for logging failed!!!\n");
  106.     exit (1);
  107. }
  108.  
  109. void open_common_log (server_rec *s, pool *p)
  110. {
  111.     common_log_state *cls = get_module_config (s->module_config,
  112.                            &common_log_module);
  113.   
  114.     char *fname = server_root_relative (p, cls->fname);
  115.     
  116.     if (cls->log_fd > 0) return; /* virtual log shared w/main server */
  117.     
  118.     if (*cls->fname == '|') {
  119.     FILE *dummy;
  120.     
  121.     spawn_child(p, common_log_child, (void *)(cls->fname+1),
  122.             kill_after_timeout, &dummy, NULL);
  123.  
  124.     if (dummy == NULL) {
  125.         fprintf (stderr, "Couldn't fork child for TransferLog process\n");
  126.         exit (1);
  127.     }
  128.  
  129.     cls->log_fd = fileno (dummy);
  130.     }
  131.     else if((cls->log_fd = popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  132.         fprintf(stderr,"httpd: could not open transfer log file %s.\n", fname);
  133.         perror("open");
  134.         exit(1);
  135.     }
  136. }
  137.  
  138. void init_common_log (server_rec *s, pool *p)
  139. {
  140.     for (; s; s = s->next) open_common_log (s, p);
  141. }
  142.  
  143. int common_log_transaction(request_rec *orig)
  144. {
  145.     common_log_state *cls = get_module_config (orig->server->module_config,
  146.                            &common_log_module);
  147.   
  148.     char *str;
  149.     long timz;
  150.     struct tm *t;
  151.     char tstr[MAX_STRING_LEN], status[MAX_STRING_LEN], sign;
  152.     conn_rec *c = orig->connection;
  153.     request_rec *r;
  154.  
  155.     /* Common log format records an unholy melange of the original request
  156.      * and whatever it was that we actually served.  Just stay compatible
  157.      * here; the whole point of the module scheme is to allow people to
  158.      * create better alternatives, but screwing up is an option we wish
  159.      * to preserve...
  160.      */
  161.     
  162.     for (r = orig; r->next; r = r->next)
  163.         continue;
  164.  
  165.     t = get_gmtoff(&timz);
  166.     sign = (timz < 0 ? '-' : '+');
  167.     if(timz < 0) 
  168.         timz = -timz;
  169.  
  170.     strftime(tstr,MAX_STRING_LEN," [%d/%b/%Y:%H:%M:%S",t);
  171.     sprintf(&tstr[strlen(tstr)], " %c%02ld%02ld] \"", sign, timz/3600,
  172.         timz%3600);
  173.  
  174.     if (r->status != -1) sprintf(status,"%d ", r->status);
  175.     else                 strcpy(status, "- ");
  176.  
  177.     if(r->bytes_sent != -1)
  178.     sprintf(&status[strlen(status)], "%d\n", r->bytes_sent);
  179.     else
  180.         strcat(status, "-\n");
  181.  
  182.     str = pstrcat(orig->pool, c->remote_name, " ",
  183.           (c->remote_logname != NULL ? c->remote_logname : "-"),
  184.           " ",
  185.           (c->user != NULL ? c->user : "-"),
  186.           tstr, 
  187.           (orig->the_request != NULL ? orig->the_request : "NULL"), 
  188.           "\" ", status, NULL);
  189.     
  190.     write(cls->log_fd, str, strlen(str));
  191.  
  192.     return OK;
  193. }
  194.  
  195. module common_log_module = {
  196.    STANDARD_MODULE_STUFF,
  197.    init_common_log,        /* initializer */
  198.    NULL,            /* create per-dir config */
  199.    NULL,            /* merge per-dir config */
  200.    make_common_log_state,    /* server config */
  201.    NULL,            /* merge server config */
  202.    common_log_cmds,        /* command table */
  203.    NULL,            /* handlers */
  204.    NULL,            /* filename translation */
  205.    NULL,            /* check_user_id */
  206.    NULL,            /* check auth */
  207.    NULL,            /* check access */
  208.    NULL,            /* type_checker */
  209.    NULL,            /* fixups */
  210.    common_log_transaction    /* logger */
  211. };
  212.