1 /*
2 * error message handling
3 */
4 /*
5 Copyright 1994, 1998 The Open Group
6
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
11 documentation.
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of The Open Group shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in 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 "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
64 Bool UseSyslog;
65 Bool log_open = FALSE;
66 char ErrorFile[PATH_MAX];
67 static char CurrentErrorFile[PATH_MAX];
68
69 static void _X_NORETURN
abort_server(void)70 abort_server(void)
71 {
72 fflush(stderr);
73
74 _exit(1);
75 }
76
77 void
InitErrors(void)78 InitErrors(void)
79 {
80 int i;
81
82 #ifdef USE_SYSLOG
83 if (UseSyslog && !log_open) {
84 openlog("xfs", LOG_PID, LOG_DAEMON);
85 log_open = TRUE;
86 return;
87 }
88 #endif
89
90 if (ErrorFile[0] &&
91 (!log_open || (strcmp(CurrentErrorFile, ErrorFile) != 0)) ) {
92 i = open(ErrorFile, O_WRONLY | O_APPEND | O_CREAT, 0666);
93 if (i != -1) {
94 dup2(i, 2);
95 close(i);
96 log_open = TRUE;
97 } else {
98 ErrorF("can't open error file \"%s\"\n", ErrorFile);
99 }
100 strncpy(CurrentErrorFile, ErrorFile, sizeof CurrentErrorFile);
101 }
102 }
103
104 void
CloseErrors(void)105 CloseErrors(void)
106 {
107 int nullfd;
108
109 if (!log_open)
110 return;
111
112 log_open = FALSE;
113
114 #ifdef USE_SYSLOG
115 if (UseSyslog) {
116 closelog();
117 return;
118 }
119 #endif
120
121 close (2);
122 nullfd = open ("/dev/null", O_RDWR);
123 if (nullfd != 2) {
124 dup2 (nullfd, 2);
125 close(nullfd);
126 }
127 }
128
129 void
Error(const char * str)130 Error(const char *str)
131 {
132 #ifdef USE_SYSLOG
133 if (UseSyslog) {
134 syslog(LOG_ERR, "%s: %s", str, strerror(errno));
135 return;
136 }
137 #endif
138 perror(str);
139 }
140
141 /*
142 * used for informational messages
143 */
144 void
NoticeF(const char * f,...)145 NoticeF(const char *f, ...)
146 {
147 /* XXX should Notices just be ignored if not using syslog? */
148 va_list args;
149 va_start(args, f);
150 #ifdef USE_SYSLOG
151 if (UseSyslog) {
152 vsyslog(LOG_NOTICE, f, args);
153 return;
154 }
155 #else
156 fprintf(stderr, "%s notice: ", progname);
157 vfprintf(stderr, f, args);
158 #endif /* USE_SYSLOG */
159 va_end(args);
160 }
161
162 /*
163 * used for non-fatal error messages
164 */
165 void
ErrorF(const char * f,...)166 ErrorF(const char * f, ...)
167 {
168 va_list args;
169 va_start(args, f);
170 #ifdef USE_SYSLOG
171 if (UseSyslog) {
172 vsyslog(LOG_WARNING, f, args);
173 return;
174 }
175 #else
176 fprintf(stderr, "%s error: ", progname);
177 vfprintf(stderr, f, args);
178 #endif
179 va_end(args);
180 }
181
182 void
FatalError(const char * f,...)183 FatalError(const char * f, ...)
184 {
185 va_list args;
186 va_start(args, f);
187 #ifdef USE_SYSLOG
188 if (UseSyslog) {
189 vsyslog(LOG_ERR, f, args);
190 return;
191 }
192 #else
193 fprintf(stderr, "%s fatal error: ", progname);
194 vfprintf(stderr, f, args);
195 #endif
196 va_end(args);
197 abort_server();
198 /* NOTREACHED */
199 }
200