home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / gopherd / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-21  |  5.6 KB  |  302 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1992/12/10 23:13:27 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/gopherd/RCS/error.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: error.c
  14.  * Error handling/logging routines.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: error.c,v $
  18.  * Revision 1.1  1992/12/10  23:13:27  lindner
  19.  * gopher 1.1 release
  20.  *
  21.  *
  22.  *********************************************************************/
  23.  
  24.  
  25. /*
  26.  * Error handling routines from Stevens: UNIX network programming
  27.  * pp 722-731
  28.  */
  29.  
  30. /*
  31.  * Error handling routines.
  32.  *
  33.  * The functions in this file are independent of any application
  34.  * variables, and may be used in any C program.  Either of the names
  35.  * CLIENT of SERVER may be defined when compiling this function.
  36.  * If neither are defined, we assume SERVER.
  37.  */
  38.  
  39. #include "gopherd.h"
  40. void my_perror();
  41.  
  42. #ifndef __stdc__
  43. #include <varargs.h>
  44. #endif
  45.  
  46. #ifndef NULL
  47. #define NULL ((void *) 0)
  48. #endif
  49.  
  50. extern char *pname;
  51.  
  52. #ifndef NOSYSLOG
  53.  /*
  54.   * Under useful OS's, these server routines use the syslog(3) facility.
  55.   * They don't append a newline for example.
  56.   */
  57.  
  58. #include <syslog.h>
  59.  
  60.  
  61. #else /* no syslog() */
  62. /*
  63.  * There really ought to be a better way to handle server logging
  64.  * under System V
  65.  */
  66. #define syslog(a,b)    fprintf(stderr, "%s\n", (b))
  67. #define openlog(a,b,c)    fprintf(stderr, "%s\n", (a))
  68.  
  69. #endif  /* NOSYSLOG */
  70.  
  71. char emesgstr[255] = {0};  /* used by all server routines */
  72.  
  73. /*
  74.  * Identify ourselves, for syslog() messages.
  75.  *
  76.  * LOG_PID is an option that says prepend each message with our pid.
  77.  * LOG_CONS is an option that says write to the console if unable to
  78.  * send the message to syslogd.
  79.  * LOG_DAEMON is our facility.
  80.  */
  81.  
  82. void
  83. err_init()
  84. {
  85.     /* have to be able to reopen, since daemon_start keeps closing us */
  86.     closelog();    /* in case we've been closed by daemon_start */
  87. #ifdef LOG_DAEMON
  88.     openlog("gopherd", (LOG_PID|LOG_CONS), LOG_DAEMON);
  89. #else
  90.     /* old old syslog - probably Ultrix */
  91.     openlog("gopherd", LOG_PID);
  92. #endif
  93. }
  94.  
  95. /*
  96.  * Fatal error.  Print a message and terminate.
  97.  * Don't print the system's errno value.
  98.  *
  99.  *    err_quit(str, arg1, arg2, ...)
  100.  */
  101.  
  102. #ifdef __stdc__
  103. err_quit(char *va_alist, ...)
  104. #else
  105. /*VARARGS1*/
  106. void
  107. err_quit(va_alist)
  108. va_dcl
  109. #endif
  110. {
  111.     va_list args;
  112.     char *fmt;
  113.  
  114. #ifdef __stdc__
  115.     fmt = va_alist;
  116.     va_start(args, va_alist);
  117. #else
  118.     va_start(args);
  119.     fmt = va_arg(args, char *);
  120. #endif
  121.  
  122.     (void) vsprintf(emesgstr, fmt, args);
  123.     va_end(args);
  124.  
  125.     syslog(LOG_ERR, emesgstr);
  126.     LOGGopher(-1, emesgstr);
  127.  
  128.     exit(1);
  129. }
  130.  
  131.  
  132. /*
  133.  * Fatal error related to a system call.  Print the message and terminate.
  134.  * Don't dump core, but do print the systems errno value and its associated
  135.  * message.
  136.  */
  137.  
  138. #ifdef __stdc__
  139. void err_sys(char *va_alist, ...)
  140. #else
  141. /*VARARGS*/
  142. void
  143. err_sys(va_alist)
  144. va_dcl
  145. #endif
  146. {
  147.     va_list args;
  148.     char *fmt;
  149.  
  150. #ifdef __stdc__
  151.     fmt = va_alist;
  152.     va_start(args,va_alist);
  153. #else
  154.     va_start(args);
  155.     fmt = va_arg(args, char *);
  156. #endif
  157.     (void) vsprintf(emesgstr, fmt, args);
  158.     va_end(args);
  159.  
  160.     my_perror();
  161.     syslog(LOG_ERR, emesgstr);
  162.     LOGGopher(-1, emesgstr);
  163.  
  164.     exit(1);
  165. }
  166.  
  167.  
  168. /*
  169.  * Recoverable error.  Print a message, and return to caller.
  170.  */
  171.  
  172. #ifdef __stdc__
  173. void err_ret(char *va_alist, ...)
  174. #else
  175. /*VARARGS1*/
  176. void
  177. err_ret(va_alist)
  178. va_dcl
  179. #endif
  180. {
  181.     va_list args;
  182.     char *fmt;
  183.  
  184. #ifdef __stdc__
  185.     fmt = va_alist;
  186.     va_start(args, va_alist);
  187. #else
  188.     va_start(args);
  189.     fmt = va_arg(args, char *);
  190. #endif
  191.     (void) vsprintf(emesgstr, fmt, args);
  192.     va_end(args);
  193.  
  194.     my_perror();
  195.     syslog(LOG_ERR, emesgstr);
  196.     LOGGopher(-1, emesgstr);
  197.  
  198.     return;
  199. }
  200.  
  201.  
  202. /*
  203.  * Fatal error.  Print a message, dump core and terminate.
  204.  */
  205.  
  206. #ifdef __stdc__
  207. void err_dump(char *va_alist, ...)
  208. #else
  209. /*VARARGS1*/
  210. void
  211. err_dump(va_alist)
  212. va_dcl
  213. #endif
  214. {
  215.     va_list args;
  216.     char *fmt;
  217.  
  218. #ifdef __stdc__
  219.     fmt = va_alist;
  220.     va_start(args, va_alist);
  221. #else
  222.     va_start(args);
  223.     fmt = va_arg(args, char *);
  224. #endif
  225.     (void) vsprintf(emesgstr, fmt, args);
  226.     va_end(args);
  227.  
  228.     my_perror();
  229.     syslog(LOG_ERR, emesgstr);
  230.     LOGGopher(-1, emesgstr);
  231.  
  232.     abort();
  233. }
  234.  
  235. /*
  236.  * Print the UNIX errno value.
  237.  * We just append it the the end of the emesgstr[] array.
  238.  */
  239.  
  240. void my_perror()
  241. {
  242.     int len;
  243.     char *sys_err_str();
  244.  
  245.     len = strlen(emesgstr);
  246.     sprintf(emesgstr + len, " %s", sys_err_str());
  247. }
  248.  
  249.  
  250. extern int errno;        /* UNIX error number */
  251. extern int sys_nerr;        /* # of error message strings in sys table */
  252. extern char *sys_errlist[];    /* the system error message table */
  253.  
  254. #ifdef SYS5
  255. int t_errno;
  256. int t_nerr;
  257. char *t_errlist[1];
  258. #endif
  259.  
  260. /*
  261.  * Return a string containing some additional operating system
  262.  * dependent information.
  263.  * Note that different versions of UNIX assign different meanings
  264.  * to the same value of "errno".  This means that if an error
  265.  * condition is being sent ot another UNIX system, we must interpret
  266.  * the errno value on the system that generated the error, and not
  267.  * just send the decimal value of errno to the other system.
  268.  */
  269.  
  270. char *
  271. sys_err_str()
  272. {
  273.     static char msgstr[200];
  274.  
  275.     if (errno != 0) {
  276.         if (errno >0 && errno < sys_nerr)
  277.             sprintf(msgstr, "(%s)", sys_errlist[errno]);
  278.         else
  279.             sprintf(msgstr, "(errno = %d)", errno);
  280.     } else {
  281.         msgstr[0] = '\0';
  282.     }
  283.  
  284. #ifdef SYS5
  285.  
  286.     if (t_errno != 0) {
  287.         char tmsgstr[100];
  288.  
  289.         if (t_errno > 0 && t_errno < sys_nerr)
  290.             sprintf(tmsgstr, " (%s)", t_errlist[t_errno]);
  291.         else
  292.             sprintf(tmsgstr, ", (t_errno = %d)", t_errno);
  293.  
  294.         strcat(msgstr, tmsgstr);
  295.     }
  296. #endif
  297.  
  298.     return(msgstr);
  299. }
  300.  
  301.  
  302.