home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / http_log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-30  |  5.8 KB  |  194 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.  * http_log.c: Dealing with the logs and errors
  57.  * 
  58.  * Rob McCool
  59.  * 
  60.  */
  61.  
  62.  
  63. #include "httpd.h"
  64. #include "http_config.h"
  65. #include "http_core.h"
  66. #include "http_log.h"
  67.  
  68. #include <stdarg.h>
  69.  
  70. void error_log_child (void *cmd)
  71. {
  72.     /* Child process code for 'ErrorLog "|..."';
  73.      * may want a common framework for this, since I expect it will
  74.      * be common for other foo-loggers to want this sort of thing...
  75.      */
  76.     
  77.     cleanup_for_exec();
  78.     signal (SIGHUP, SIG_IGN);
  79.     execl (SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
  80.     exit (1);
  81. }
  82.  
  83. void open_error_log(server_rec *s, pool *p)
  84. {
  85.     char *fname;
  86.   
  87.     fname = server_root_relative (p, s->error_fname);
  88.  
  89.     if (*fname == '|') {
  90.       FILE *dummy;
  91.  
  92.       spawn_child(p, error_log_child, (void *)(fname+1),
  93.                     kill_after_timeout, &dummy, NULL);
  94.  
  95.         if (dummy == NULL) {
  96.             fprintf (stderr, "Couldn't fork child for ErrorLog process\n");
  97.             exit (1);
  98.       }
  99.     } else {
  100.         if(!(s->error_log = pfopen(p, fname, "a"))) {
  101.             fprintf(stderr,"httpd: could not open error log file %s.\n", fname);
  102.             perror("fopen");
  103.             exit(1);
  104.       }
  105.     }
  106. }
  107.  
  108. void open_logs (server_rec *s_main, pool *p)
  109. {
  110.     server_rec *virt, *q;
  111.  
  112.     open_error_log (s_main, p);
  113.  
  114.     for (virt = s_main->next; virt; virt = virt->next) {
  115.     if (virt->error_fname)
  116.     {
  117.         for (q=s_main; q != virt; q = q->next)
  118.         if (q->error_fname != NULL &&
  119.             strcmp(q->error_fname, virt->error_fname) == 0)
  120.             break;
  121.         if (q == virt) open_error_log (virt, p);
  122.         else virt->error_log = q->error_log;
  123.     }
  124.     else
  125.         virt->error_log = s_main->error_log;
  126.     }
  127. }
  128.  
  129. void error_log2stderr(server_rec *s) {
  130.     if(fileno(s->error_log) != STDERR_FILENO)
  131.         dup2(fileno(s->error_log),STDERR_FILENO);
  132. }
  133.  
  134. void log_pid(pool *p, char *pid_fname) {
  135.     FILE *pid_file;
  136.  
  137.     if (!pid_fname) return;
  138.     pid_fname = server_root_relative (p, pid_fname);
  139.     if(!(pid_file = fopen(pid_fname,"w"))) {
  140.     perror("fopen");
  141.         fprintf(stderr,"httpd: could not log pid to file %s\n", pid_fname);
  142.         exit(1);
  143.     }
  144.     fprintf(pid_file,"%ld\n",(long)getpid());
  145.     fclose(pid_file);
  146. }
  147.  
  148. void log_error(char *err, server_rec *s) {
  149.     fprintf(s->error_log, "[%s] %s\n",get_time(),err);
  150.     fflush(s->error_log);
  151. }
  152.  
  153. void
  154. log_unixerr(const char *routine, const char *file, const char *msg,
  155.         server_rec *s)
  156. {
  157.     const char *p, *q;
  158.  
  159.     p = strerror(errno);
  160.     q = get_time();
  161.  
  162.     if (file != NULL)
  163.     fprintf(s->error_log, "[%s] %s: %s: %s\n", q, routine, file, p);
  164.     else
  165.     fprintf(s->error_log, "[%s] %s: %s\n", q, routine, p);
  166.     if (msg != NULL) fprintf(s->error_log, "[%s] - %s\n", q, msg);
  167.  
  168.     fflush(s->error_log);
  169. }
  170.  
  171. void
  172. log_printf(const server_rec *s, const char *fmt, ...)
  173. {
  174.     va_list args;
  175.     
  176.     fprintf(s->error_log, "[%s] ", get_time());
  177.     va_start (args, fmt);
  178.     vfprintf (s->error_log, fmt, args);
  179.     va_end (args);
  180.  
  181.     fputc('\n', s->error_log);
  182.     fflush(s->error_log);
  183. }
  184.  
  185. void log_reason(char *reason, char *file, request_rec *r) {
  186.     fprintf (r->server->error_log,
  187.          "[%s] access to %s failed for %s, reason: %s\n",
  188.          get_time(), file,
  189.          get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME),
  190.          reason);
  191.     fflush (r->server->error_log);
  192. }
  193.  
  194.