123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /* *
- * (c) Copyright 1993, 1994 Hewlett-Packard Company *
- * (c) Copyright 1993, 1994 International Business Machines Corp. *
- * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
- * (c) Copyright 1993, 1994 Novell, Inc. *
- */
- /*
- * xdm - display manager daemon
- *
- * $XConsortium: error.c /main/6 1996/10/21 12:50:31 mgreess $
- *
- * Copyright 1988 Massachusetts Institute of Technology
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of M.I.T. not be used in advertising or
- * publicity pertaining to distribution of the software without specific,
- * written prior permission. M.I.T. makes no representations about the
- * suitability of this software for any purpose. It is provided "as is"
- * without express or implied warranty.
- *
- * Author: Keith Packard, MIT X Consortium
- */
- /*
- * error.c
- *
- * Log display manager errors to a file as
- * we generally do not have a terminal to talk to
- */
- # include <sys/stat.h>
- # include <time.h>
- # include "dm.h"
- # include "vgmsg.h"
- # include <stdarg.h>
- # define Va_start(a,b) va_start(a,b)
- extern char DisplayName[];
- void
- InitErrorLog( void )
- {
- int i;
- static char tz[32];
-
-
- /*
- * add TimeZone to our environment so error message time stamps
- * have the correct value...
- */
-
- if (timeZone != NULL && strlen(timeZone) < 29) {
- strcpy(tz,"TZ=");
- strcat(tz,timeZone);
- putenv(tz);
- }
- if (errorLogFile && errorLogFile[0]) {
- i = creat (errorLogFile, 0666);
- if (i != -1) {
- if (i != 2) {
- dup2 (i, 2);
- close (i);
- }
- } else
- LogError(ReadCatalog(
- MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
- errorLogFile);
- }
- }
- /****************************************************************************
- *
- * CheckErrorFile
- *
- * Just a quick check to verify that we can open an error log.
- * Do this before we do BecomeDeamon.
- *
- ****************************************************************************/
- void
- CheckErrorFile( void )
- {
- int i;
- if (errorLogFile && errorLogFile[0]) {
- i = creat (errorLogFile, 0666);
- if (i != -1) {
- close (i);
- } else {
- fprintf(stderr, (char *)ReadCatalog(
- MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
- errorLogFile);
- }
- } else {
- fprintf(stderr, (char *)ReadCatalog(
- MC_LOG_SET,MC_LOG_NO_ERRLOG,MC_DEF_LOG_NO_ERRLOG),
- "\"\"");
- }
- }
- /****************************************************************************
- *
- * SyncErrorFile
- *
- * point the stderr stream to the most current Error Log File. This allows
- * the user to muck with the file while XDM is running and have everything
- * sync up later.
- *
- * optionally, write a time stamp to the file
- ****************************************************************************/
- int
- SyncErrorFile( int stamp )
- {
- time_t timer;
-
- TrimErrorFile();
- if (errorLogFile && errorLogFile[0] &&
- (freopen(errorLogFile, "a", stderr) != NULL)) {
- if (stamp) {
- timer = time(NULL);
- fprintf(stderr, "\n%s", ctime(&timer));
- }
- return(1);
- }
- else
- return(0);
- }
- /****************************************************************************
- *
- * TrimErrorFile
- *
- * Trim the length of the error log file until it is 75% of the maximum
- * specified by the resource "errorLogSize".
- *
- ****************************************************************************/
- void
- TrimErrorFile( void )
- {
- int f1 = -1;
- int f2 = -1;
- int deleteBytes;
-
- char buf[BUFSIZ];
- char *p;
- int n;
- off_t status;
- struct stat statb;
- /*
- * convert user-specified units of 1Kb to bytes...
- * put an upper cap of 200Kb on the file...
- */
- if (errorLogSize < 1024) errorLogSize *= 1024;
-
- if (errorLogSize > (200*1024) ) {
- errorLogSize = 200*1024;
- LogError(ReadCatalog(
- MC_LOG_SET,MC_LOG_MAX_LOGFILE,MC_DEF_LOG_MAX_LOGFILE));
- }
-
- if ( errorLogFile && errorLogFile[0] &&
- (stat(errorLogFile, &statb) == 0) &&
- (statb.st_size > errorLogSize) ) {
-
- deleteBytes = (statb.st_size - errorLogSize) + (errorLogSize / 4);
- Debug("TrimErrorLog(): discarding oldest %d bytes from logfile %s\n",
- deleteBytes, errorLogFile);
- /*
- * get two pointers to the file...
- */
-
- if ( (f1 = open(errorLogFile, O_RDWR)) < 0 ||
- (f2 = open(errorLogFile, O_RDWR)) < 0 ) {
- Debug("TrimErrorLog(): Cannot open file %s, error number = %d\n",
- errorLogFile, errno);
- if(f1 >= 0) {
- close(f1);
- }
- if(f2 >= 0) {
- close(f2);
- }
- return;
- }
-
- /*
- * position read pointer to the first full line after the trim
- * point...
- */
-
- if ( (status = lseek(f2, deleteBytes, SEEK_SET)) < 0 ) {
- Debug("TrimErrorLog(): Cannot lseek() in file %s, error number = %d\n",
- errorLogFile, errno);
- close(f1);
- close(f2);
- return;
- }
- memset(buf, 0, BUFSIZ);
- n = read(f2, buf, BUFSIZ - 1);
- if ( (p = strchr(buf,'\n')) != NULL ) {
- p++;
- n -= p - buf;
- deleteBytes += p - buf;
- }
- else {
- p = buf;
- }
-
- /*
- * shift bytes to be saved to the beginning of the file...
- */
-
- if(-1 == write (f1, p, n)) {
- perror(strerror(errno));
- }
-
- while ( (n = read(f2, buf, BUFSIZ)) > 0 ) {
- if(-1 == write(f1, buf, n)) {
- perror(strerror(errno));
- }
- }
- /*
- * truncate file to new length and close file pointers...
- */
-
- if(-1 == truncate(errorLogFile, statb.st_size - deleteBytes)) {
- perror(strerror(errno));
- }
- close(f1);
- close(f2);
- }
- }
- /****************************************************************************
- *
- * LogInfo
- *
- * Write an information message to the log file
- *
- ****************************************************************************/
- void
- LogInfo( unsigned char *fmt, ...)
- {
- va_list args;
- Va_start(args,fmt);
- if ( SyncErrorFile(1) ) {
- fprintf (stderr, "info (pid %ld): ", (long)getpid());
- vfprintf (stderr, (char *)fmt, args);
- fflush (stderr);
- }
- va_end(args);
- }
- /****************************************************************************
- *
- * LogError
- *
- * Write an error message to the log file.
- *
- ****************************************************************************/
- void
- LogError( unsigned char *fmt, ...)
- {
- va_list args;
- Va_start(args,fmt);
- if ( SyncErrorFile(1) ) {
- fprintf (stderr, "error (pid %ld): ", (long)getpid());
- vfprintf (stderr, (char *)fmt, args);
- fflush (stderr);
- }
- va_end(args);
- }
- /****************************************************************************
- *
- * LogOutOfMem
- *
- * Write an "out of memory" message to the log file.
- *
- ****************************************************************************/
- void
- LogOutOfMem( unsigned char *fmt, ...)
- {
- va_list args;
- Va_start(args,fmt);
- if ( SyncErrorFile(1) ) {
- fprintf(stderr, "%s", (char *)ReadCatalog(MC_ERROR_SET,MC_NO_MEMORY,MC_DEF_NO_MEMORY));
- vfprintf (stderr, (char *)fmt, args);
- fflush (stderr);
- }
- va_end(args);
- }
- /***************************************************************************
- *
- * CODE DISABLED!!! The following routines...
- *
- * getLocalAddress()
- * scanHostList()
- * indirectAlias()
- * ForEachMatchingIndirectHost()
- * UseChooser()
- * ForEachChooserHost()
- *
- * are disabled until indirect queries are supported...
- *
- ***************************************************************************/
- #if 0
- /****************************************************************************
- *
- * LogPanic
- *
- * Write a panic message to the log file.
- *
- ****************************************************************************/
- void
- LogPanic( char *fmt, ...)
- {
- va_list args;
- Va_start(args,fmt);
- if ( SyncErrorFile(1) ) {
- fprintf (stderr, "panic (pid %ld): ", (long)getpid());
- vfprintf (stderr, fmt, args);
- fflush (stderr);
- }
- va_end(args);
- }
- /****************************************************************************
- *
- * Panic
- *
- * Write a panic message to the console
- *
- ****************************************************************************/
- int
- Panic( char *mesg )
- {
- int i;
- i = creat ("/dev/console", 0666);
- write (i, "panic: ", 7);
- write (i, mesg, strlen (mesg));
- exit (1);
- }
- #endif /* DISABLED CODE */
- /****************************************************************************
- *
- * Debug
- *
- * Write a debug message to stdout
- *
- ****************************************************************************/
- static int DoName=True;
- void
- Debug( char *fmt, ...)
- {
- va_list args;
- Va_start(args,fmt);
- if (debugLevel > 0)
- {
- if ( strlen(DisplayName) > 0 && DoName)
- fprintf(stdout, "(%s) ", DisplayName);
- vprintf (fmt, args);
- fflush (stdout);
- /*
- * don't prepend the display name next time if this debug message
- * does not contain a "new line" character...
- */
- if ( strchr(fmt,'\n') == NULL )
- DoName=False;
- else
- DoName=True;
-
- }
- va_end(args);
- }
|