home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  5.7 KB  |  305 lines

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