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