(file) Return to TraceFileHandler.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 martin 1.38 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.39 //
  3 martin 1.38 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.39 //
 10 martin 1.38 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.39 //
 17 martin 1.38 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.39 //
 20 martin 1.38 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.39 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.38 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.39 //
 28 martin 1.38 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32             #include <Pegasus/Common/FileSystem.h>
 33 thilo.boehm 1.36 #include <Pegasus/Common/Tracer.h>
 34 mike        1.2  #include <Pegasus/Common/TraceFileHandler.h>
 35 marek       1.41 #include <Pegasus/Common/Logger.h>
 36 mike        1.2  
 37                  #if defined(PEGASUS_OS_TYPE_WINDOWS)
 38                  # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
 39 kumpf       1.30 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
 40 mike        1.29 # include <Pegasus/Common/TraceFileHandlerPOSIX.cpp>
 41 mike        1.2  #else
 42                  # error "Unsupported platform"
 43                  #endif
 44                  
 45                  
 46                  PEGASUS_USING_STD;
 47                  
 48                  PEGASUS_NAMESPACE_BEGIN
 49                  
 50                  ////////////////////////////////////////////////////////////////////////////////
 51                  //  Constructs TraceFileHandler
 52                  ////////////////////////////////////////////////////////////////////////////////
 53                  
 54 dl.meetei   1.44 TraceFileHandler::TraceFileHandler(): 
 55                      _fileName(0), 
 56                      _fileHandle(0),
 57                      _logErrorBitField(0),
 58                      _configHasChanged(true),
 59 a.arora     1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
 60 dl.meetei   1.44     _baseFileName(0),
 61                      _fileCount(0)
 62 a.arora     1.16 #endif
 63 dl.meetei   1.44 {
 64 mike        1.2  }
 65                  
 66                  ////////////////////////////////////////////////////////////////////////////////
 67                  //  Destructs TraceFileHandler
 68                  ////////////////////////////////////////////////////////////////////////////////
 69                  
 70 kumpf       1.31 TraceFileHandler::~TraceFileHandler()
 71 mike        1.2  {
 72 kumpf       1.31     // Close the File
 73 mike        1.2      if (_fileHandle)
 74                      {
 75                          fclose(_fileHandle);
 76                      }
 77 thilo.boehm 1.36     free(_fileName);
 78 a.arora     1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
 79 thilo.boehm 1.36     free(_baseFileName);
 80 a.arora     1.16 #endif
 81 mike        1.2  }
 82                  
 83                  ////////////////////////////////////////////////////////////////////////////////
 84 kumpf       1.40 // The configuration of the trace has been updated.
 85 thilo.boehm 1.36 // At the next trace write, change to the new configuration.
 86 mike        1.2  ////////////////////////////////////////////////////////////////////////////////
 87 thilo.boehm 1.36 void TraceFileHandler::configurationUpdated()
 88                  {
 89                      _configHasChanged = true;
 90                  }
 91 mike        1.2  
 92 thilo.boehm 1.36 ////////////////////////////////////////////////////////////////////////////////
 93                  // If the trace configuration has been updated,
 94                  // close the old file and open the new file.
 95                  ////////////////////////////////////////////////////////////////////////////////
 96                  void TraceFileHandler::_reConfigure()
 97 mike        1.2  {
 98 thilo.boehm 1.36     AutoMutex writeLock(writeMutex);
 99                  
100                      if(!_configHasChanged)
101 kumpf       1.18     {
102 thilo.boehm 1.36         // An other thread does already the re-configuration.
103                          // do nothing.
104                          return;
105 kumpf       1.18     }
106                  
107 thilo.boehm 1.36     free(_fileName);
108 kumpf       1.18     _fileName = 0;
109                  #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
110 thilo.boehm 1.36     free(_baseFileName);
111 kumpf       1.18     _baseFileName = 0;
112                  #endif
113                  
114 thilo.boehm 1.37     if (Tracer::_getInstance() ->_traceFile.size() == 0)
115 thilo.boehm 1.36     {
116                          // if the file name is empty/NULL pointer do nothing
117 thilo.boehm 1.37         // wait for a new trace file.
118                          _configHasChanged=false;
119 thilo.boehm 1.36         return;
120                      }
121                  
122                      _fileName = strdup((const char*)Tracer::_getInstance()
123                                              ->_traceFile.getCString());
124                  
125                      // If a file is already open, close it.
126                      if (_fileHandle)
127 mike        1.2      {
128 thilo.boehm 1.36         fclose(_fileHandle);
129                          _fileHandle = 0;
130 mike        1.2      }
131 thilo.boehm 1.36 
132                      _fileHandle = _openFile(_fileName);
133 mike        1.2      if (!_fileHandle)
134                      {
135 thilo.boehm 1.36         // return with no message. _openFile() already wrote one.
136                          free(_fileName);
137                          _fileName = 0;
138 thilo.boehm 1.37         // wait for a new trace file
139                          _configHasChanged=false;
140 thilo.boehm 1.36         return;
141 mike        1.2      }
142 kumpf       1.18 
143 thilo.boehm 1.36     #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
144                      _baseFileName = strdup(_fileName);
145                      #endif
146                  
147                      _configHasChanged=false;
148 kumpf       1.18 
149 thilo.boehm 1.36     return;
150 kumpf       1.18 }
151                  
152                  FILE* TraceFileHandler::_openFile(const char* fileName)
153                  {
154 gs.keenan   1.25 #ifdef PEGASUS_OS_VMS
155                  //    FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
156                      FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
157                  #else
158 kumpf       1.18     FILE* fileHandle = fopen(fileName,"a+");
159 gs.keenan   1.25 #endif
160 kumpf       1.18     if (!fileHandle)
161                      {
162                          // Unable to open file, log a message
163 marek       1.41         MessageLoaderParms parm(
164                              "Common.TraceFileHandler.FAILED_TO_OPEN_FILE_SYSMSG",
165                              "Failed to open file $0: $1",
166                              fileName,PEGASUS_SYSTEM_ERRORMSG_NLS);
167                          _logError(TRCFH_FAILED_TO_OPEN_FILE_SYSMSG,parm);
168 kumpf       1.18         return 0;
169 mike        1.2      }
170 kumpf       1.9  
171                      //
172 kumpf       1.24     // Verify that the file has the correct owner
173                      //
174                      if (!System::verifyFileOwnership(fileName))
175                      {
176 marek       1.41         MessageLoaderParms parm(
177                              "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
178                              "File $0 is not owned by user $1.",
179                              fileName,
180                              System::getEffectiveUserName());
181                          _logError(TRCFH_UNEXPECTED_FILE_OWNER,parm);
182 kumpf       1.24         fclose(fileHandle);
183                          return 0;
184                      }
185                  
186                      //
187 kumpf       1.18     // Set the file permissions to 0600
188 marek       1.12     //
189 chuck       1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
190 kumpf       1.18     if (!FileSystem::changeFilePermissions(
191                              String(fileName), (S_IRUSR|S_IWUSR)) )
192 marek       1.12 #else
193 kumpf       1.18     if (!FileSystem::changeFilePermissions(
194                              String(fileName), (_S_IREAD|_S_IWRITE)) )
195 marek       1.12 #endif
196 kumpf       1.9      {
197 marek       1.41         MessageLoaderParms parm(
198                              "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
199                              "Failed to set permissions on file $0",
200                              fileName);
201                          _logError(TRCFH_FAILED_TO_SET_FILE_PERMISSIONS,parm);
202                          fclose(fileHandle);
203                          return 0;
204                      }
205                  
206                      return fileHandle;
207                  }
208                  
209                  void TraceFileHandler::_logError(
210                      ErrLogMessageIds msgID,
211                      const MessageLoaderParms & parms)
212                  {
213 ajay.rao    1.43     static Boolean isLogErrorProgress = false;
214 marek       1.41     // msgID has to be within range, else we have a severe coding error
215                      PEGASUS_ASSERT((msgID >= TRCFH_FAILED_TO_OPEN_FILE_SYSMSG) &&
216 ajay.rao    1.43         (msgID <= TRCFH_UNABLE_TO_WRITE_TRACE_TO_FILE));
217                      if (!isLogErrorProgress)
218 marek       1.41     {
219 ajay.rao    1.43         isLogErrorProgress = true;
220                          if ((_logErrorBitField & (1 << msgID)) == 0)
221                          {
222                             // log message not yet written, write log message
223                             Logger::put_l(
224                                 Logger::ERROR_LOG,
225                                 System::CIMSERVER,
226                                 Logger::WARNING,
227                                 parms);
228                             // mark bit in log error field to flag that specific log message
229                             // has been written
230                             _logErrorBitField |= (1 << msgID);
231                          }
232                          isLogErrorProgress = false;
233 kumpf       1.18     }
234 marek       1.41 }
235 kumpf       1.18 
236 mike        1.2  
237                  PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2