2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
25 * (c) Copyright 1993, 1994 International Business Machines Corp. *
26 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
27 * (c) Copyright 1993, 1994 Novell, Inc. *
30 * xdm - display manager daemon
32 * $XConsortium: error.c /main/6 1996/10/21 12:50:31 mgreess $
34 * Copyright 1988 Massachusetts Institute of Technology
36 * Permission to use, copy, modify, and distribute this software and its
37 * documentation for any purpose and without fee is hereby granted, provided
38 * that the above copyright notice appear in all copies and that both that
39 * copyright notice and this permission notice appear in supporting
40 * documentation, and that the name of M.I.T. not be used in advertising or
41 * publicity pertaining to distribution of the software without specific,
42 * written prior permission. M.I.T. makes no representations about the
43 * suitability of this software for any purpose. It is provided "as is"
44 * without express or implied warranty.
46 * Author: Keith Packard, MIT X Consortium
52 * Log display manager errors to a file as
53 * we generally do not have a terminal to talk to
56 # include <sys/stat.h>
62 # define Va_start(a,b) va_start(a,b)
64 extern char DisplayName[];
75 * add TimeZone to our environment so error message time stamps
76 * have the correct value...
79 if (timeZone != NULL && strlen(timeZone) < 29) {
86 if (errorLogFile && errorLogFile[0]) {
87 i = creat (errorLogFile, 0666);
95 MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
100 /****************************************************************************
104 * Just a quick check to verify that we can open an error log.
105 * Do this before we do BecomeDeamon.
107 ****************************************************************************/
109 CheckErrorFile( void )
113 if (errorLogFile && errorLogFile[0]) {
114 i = creat (errorLogFile, 0666);
118 fprintf(stderr, (char *)ReadCatalog(
119 MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
123 fprintf(stderr, (char *)ReadCatalog(
124 MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
130 /****************************************************************************
134 * point the stderr stream to the most current Error Log File. This allows
135 * the user to muck with the file while XDM is running and have everything
138 * optionally, write a time stamp to the file
139 ****************************************************************************/
142 SyncErrorFile( int stamp )
147 if (errorLogFile && errorLogFile[0] &&
148 (freopen(errorLogFile, "a", stderr) != NULL)) {
152 fprintf(stderr, "\n%s", ctime(&timer));
161 /****************************************************************************
165 * Trim the length of the error log file until it is 75% of the maximum
166 * specified by the resource "errorLogSize".
168 ****************************************************************************/
171 TrimErrorFile( void )
186 * convert user-specified units of 1Kb to bytes...
187 * put an upper cap of 200Kb on the file...
190 if (errorLogSize < 1024) errorLogSize *= 1024;
192 if (errorLogSize > (200*1024) ) {
193 errorLogSize = 200*1024;
194 LogError(ReadCatalog(
195 MC_LOG_SET,MC_LOG_MAX_LOGFILE,MC_DEF_LOG_MAX_LOGFILE));
198 if ( errorLogFile && errorLogFile[0] &&
199 (stat(errorLogFile, &statb) == 0) &&
200 (statb.st_size > errorLogSize) ) {
202 deleteBytes = (statb.st_size - errorLogSize) + (errorLogSize / 4);
204 Debug("TrimErrorLog(): discarding oldest %d bytes from logfile %s\n",
205 deleteBytes, errorLogFile);
209 * get two pointers to the file...
212 if ( (f1 = open(errorLogFile, O_RDWR)) < 0 ||
213 (f2 = open(errorLogFile, O_RDWR)) < 0 ) {
214 Debug("TrimErrorLog(): Cannot open file %s, error number = %d\n",
215 errorLogFile, errno);
221 * position read pointer to the first full line after the trim
225 if ( (status = lseek(f2, deleteBytes, SEEK_SET)) < 0 ) {
226 Debug("TrimErrorLog(): Cannot lseek() in file %s, error number = %d\n",
227 errorLogFile, errno);
231 n = read(f2, buf, BUFSIZ);
233 if ( (p = strchr(buf,'\n')) != NULL ) {
236 deleteBytes += p - buf;
244 * shift bytes to be saved to the beginning of the file...
249 while ( (n = read(f2, buf, BUFSIZ)) > 0 )
253 * truncate file to new length and close file pointers...
256 truncate(errorLogFile, statb.st_size - deleteBytes);
265 /****************************************************************************
269 * Write an information message to the log file
271 ****************************************************************************/
274 LogInfo( unsigned char *fmt, ...)
280 if ( SyncErrorFile(1) ) {
281 fprintf (stderr, "info (pid %ld): ", (long)getpid());
282 vfprintf (stderr, (char *)fmt, args);
291 /****************************************************************************
295 * Write an error message to the log file.
297 ****************************************************************************/
300 LogError( unsigned char *fmt, ...)
306 if ( SyncErrorFile(1) ) {
307 fprintf (stderr, "error (pid %ld): ", (long)getpid());
308 vfprintf (stderr, (char *)fmt, args);
317 /****************************************************************************
321 * Write an "out of memory" message to the log file.
323 ****************************************************************************/
326 LogOutOfMem( unsigned char *fmt, ...)
332 if ( SyncErrorFile(1) ) {
333 fprintf(stderr, "%s", (char *)ReadCatalog(MC_ERROR_SET,MC_NO_MEMORY,MC_DEF_NO_MEMORY));
334 vfprintf (stderr, (char *)fmt, args);
343 /***************************************************************************
345 * CODE DISABLED!!! The following routines...
350 * ForEachMatchingIndirectHost()
352 * ForEachChooserHost()
354 * are disabled until indirect queries are supported...
356 ***************************************************************************/
360 /****************************************************************************
364 * Write a panic message to the log file.
366 ****************************************************************************/
368 LogPanic( char *fmt, ...)
374 if ( SyncErrorFile(1) ) {
375 fprintf (stderr, "panic (pid %ld): ", (long)getpid());
376 vfprintf (stderr, fmt, args);
385 /****************************************************************************
389 * Write a panic message to the console
391 ****************************************************************************/
398 i = creat ("/dev/console", 0666);
399 write (i, "panic: ", 7);
400 write (i, mesg, strlen (mesg));
403 #endif /* DISABLED CODE */
407 /****************************************************************************
411 * Write a debug message to stdout
413 ****************************************************************************/
415 static int DoName=True;
418 Debug( char *fmt, ...)
426 if ( strlen(DisplayName) > 0 && DoName)
427 fprintf(stdout, "(%s) ", DisplayName);
433 * don't prepend the display name next time if this debug message
434 * does not contain a "new line" character...
437 if ( strchr(fmt,'\n') == NULL )