error.c revision ce6676db
1/* 2 * error message handling 3 */ 4/* 5Copyright 1994, 1998 The Open Group 6 7Permission to use, copy, modify, distribute, and sell this software and its 8documentation for any purpose is hereby granted without fee, provided that 9the above copyright notice appear in all copies and that both that 10copyright notice and this permission notice appear in supporting 11documentation. 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 23Except as contained in this notice, the name of The Open Group shall not be 24used in advertising or otherwise to promote the sale, use or other dealings 25in this Software without prior written authorization from The Open Group. 26 * Copyright 1991 Network Computing Devices; 27 * Portions Copyright 1987 by Digital Equipment Corporation 28 * 29 * Permission to use, copy, modify, distribute, and sell this software and 30 * its documentation for any purpose is hereby granted without fee, provided 31 * that the above copyright notice appear in all copies and that both that 32 * copyright notice and this permission notice appear in supporting 33 * documentation, and that the names of Network Computing Devices, or Digital 34 * not be used in advertising or publicity pertaining to distribution 35 * of the software without specific, written prior permission. 36 * 37 * NETWORK COMPUTING DEVICES, DIGITAL DISCLAIM ALL WARRANTIES WITH 38 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 39 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETWORK COMPUTING DEVICES, 40 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 41 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 42 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 43 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 44 * THIS SOFTWARE. 45 */ 46 47#include "xfs-config.h" 48 49#include <stdio.h> 50#include <stdlib.h> 51#include <stdarg.h> 52#include <X11/Xos.h> 53 54#ifdef USE_SYSLOG 55#include <syslog.h> 56#endif 57 58#include <errno.h> 59 60#include "misc.h" 61#include "globals.h" 62#include "osdep.h" 63 64Bool UseSyslog; 65Bool log_open = FALSE; 66char ErrorFile[PATH_MAX]; 67static char CurrentErrorFile[PATH_MAX]; 68 69static void 70abort_server(void) 71{ 72 fflush(stderr); 73 74#ifdef SABER 75 saber_stop(); 76#else 77 _exit(1); 78#endif 79} 80 81void 82InitErrors(void) 83{ 84 int i; 85 86#ifdef USE_SYSLOG 87 if (UseSyslog && !log_open) { 88 openlog("xfs", LOG_PID, LOG_DAEMON); 89 log_open = TRUE; 90 return; 91 } 92#endif 93 94 if (ErrorFile[0] && 95 (!log_open || (strcmp(CurrentErrorFile, ErrorFile) != 0)) ) { 96 i = open(ErrorFile, O_WRONLY | O_APPEND | O_CREAT, 0666); 97 if (i != -1) { 98 dup2(i, 2); 99 close(i); 100 log_open = TRUE; 101 } else { 102 ErrorF("can't open error file \"%s\"\n", ErrorFile); 103 } 104 strncpy(CurrentErrorFile, ErrorFile, sizeof CurrentErrorFile); 105 } 106} 107 108void 109CloseErrors(void) 110{ 111 int nullfd; 112 113 if (!log_open) 114 return; 115 116 log_open = FALSE; 117 118#ifdef USE_SYSLOG 119 if (UseSyslog) { 120 closelog(); 121 return; 122 } 123#endif 124 125 close (2); 126 nullfd = open ("/dev/null", O_RDWR); 127 if (nullfd != 2) { 128 dup2 (nullfd, 2); 129 close(nullfd); 130 } 131} 132 133void 134Error(char *str) 135{ 136#ifdef USE_SYSLOG 137 if (UseSyslog) { 138 syslog(LOG_ERR, "%s: %s", str, strerror(errno)); 139 return; 140 } 141#endif 142 perror(str); 143} 144 145/* 146 * used for informational messages 147 */ 148void 149NoticeF(char *f, ...) 150{ 151 /* XXX should Notices just be ignored if not using syslog? */ 152 va_list args; 153 va_start(args, f); 154#ifdef USE_SYSLOG 155 if (UseSyslog) { 156 vsyslog(LOG_NOTICE, f, args); 157 return; 158 } 159#else 160 fprintf(stderr, "%s notice: ", progname); 161 vfprintf(stderr, f, args); 162#endif /* USE_SYSLOG */ 163 va_end(args); 164} 165 166/* 167 * used for non-fatal error messages 168 */ 169void 170ErrorF(char * f, ...) 171{ 172 va_list args; 173 va_start(args, f); 174#ifdef USE_SYSLOG 175 if (UseSyslog) { 176 vsyslog(LOG_WARNING, f, args); 177 return; 178 } 179#else 180 fprintf(stderr, "%s error: ", progname); 181 vfprintf(stderr, f, args); 182#endif 183 va_end(args); 184} 185 186void 187FatalError(char * f, ...) 188{ 189 va_list args; 190 va_start(args, f); 191#ifdef USE_SYSLOG 192 if (UseSyslog) { 193 vsyslog(LOG_ERR, f, args); 194 return; 195 } 196#else 197 fprintf(stderr, "%s fatal error: ", progname); 198 vfprintf(stderr, f, args); 199#endif 200 va_end(args); 201 abort_server(); 202 /* NOTREACHED */ 203} 204