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 amit99shah 1.46 #include <Pegasus/Common/String.h>
37 #include <Pegasus/Common/StringConversion.h>
38
|
39 mike 1.2
40 #if defined(PEGASUS_OS_TYPE_WINDOWS)
41 # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
|
42 kumpf 1.30 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
|
43 mike 1.29 # include <Pegasus/Common/TraceFileHandlerPOSIX.cpp>
|
44 mike 1.2 #else
45 # error "Unsupported platform"
46 #endif
47
48
49 PEGASUS_USING_STD;
50
51 PEGASUS_NAMESPACE_BEGIN
52
53 ////////////////////////////////////////////////////////////////////////////////
54 // Constructs TraceFileHandler
55 ////////////////////////////////////////////////////////////////////////////////
56
|
57 dl.meetei 1.44 TraceFileHandler::TraceFileHandler():
58 _fileName(0),
59 _fileHandle(0),
60 _logErrorBitField(0),
|
61 amit99shah 1.46 _configHasChanged(true),
62 _maxTraceFileSizeBytes(0),
63 _maxTraceFileNumber(0)
|
64 dl.meetei 1.44 {
|
65 mike 1.2 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // Destructs TraceFileHandler
69 ////////////////////////////////////////////////////////////////////////////////
70
|
71 kumpf 1.31 TraceFileHandler::~TraceFileHandler()
|
72 mike 1.2 {
|
73 kumpf 1.31 // Close the File
|
74 mike 1.2 if (_fileHandle)
75 {
76 fclose(_fileHandle);
77 }
|
78 thilo.boehm 1.36 free(_fileName);
|
79 mike 1.2 }
80
81 ////////////////////////////////////////////////////////////////////////////////
|
82 kumpf 1.40 // The configuration of the trace has been updated.
|
83 thilo.boehm 1.36 // At the next trace write, change to the new configuration.
|
84 mike 1.2 ////////////////////////////////////////////////////////////////////////////////
|
85 thilo.boehm 1.36 void TraceFileHandler::configurationUpdated()
86 {
87 _configHasChanged = true;
88 }
|
89 mike 1.2
|
90 thilo.boehm 1.36 ////////////////////////////////////////////////////////////////////////////////
91 // If the trace configuration has been updated,
92 // close the old file and open the new file.
93 ////////////////////////////////////////////////////////////////////////////////
94 void TraceFileHandler::_reConfigure()
|
95 mike 1.2 {
|
96 thilo.boehm 1.36 AutoMutex writeLock(writeMutex);
97
98 if(!_configHasChanged)
|
99 kumpf 1.18 {
|
100 thilo.boehm 1.36 // An other thread does already the re-configuration.
101 // do nothing.
102 return;
|
103 kumpf 1.18 }
104
|
105 thilo.boehm 1.36 free(_fileName);
|
106 kumpf 1.18 _fileName = 0;
107
|
108 thilo.boehm 1.37 if (Tracer::_getInstance() ->_traceFile.size() == 0)
|
109 thilo.boehm 1.36 {
110 // if the file name is empty/NULL pointer do nothing
|
111 thilo.boehm 1.37 // wait for a new trace file.
112 _configHasChanged=false;
|
113 thilo.boehm 1.36 return;
114 }
115
116 _fileName = strdup((const char*)Tracer::_getInstance()
117 ->_traceFile.getCString());
118
119 // If a file is already open, close it.
120 if (_fileHandle)
|
121 mike 1.2 {
|
122 thilo.boehm 1.36 fclose(_fileHandle);
123 _fileHandle = 0;
|
124 mike 1.2 }
|
125 thilo.boehm 1.36
126 _fileHandle = _openFile(_fileName);
|
127 mike 1.2 if (!_fileHandle)
128 {
|
129 thilo.boehm 1.36 // return with no message. _openFile() already wrote one.
130 free(_fileName);
131 _fileName = 0;
|
132 thilo.boehm 1.37 // wait for a new trace file
133 _configHasChanged=false;
|
134 thilo.boehm 1.36 return;
|
135 mike 1.2 }
|
136 kumpf 1.18
|
137 thilo.boehm 1.36 _configHasChanged=false;
|
138 kumpf 1.18
|
139 thilo.boehm 1.36 return;
|
140 kumpf 1.18 }
141
142 FILE* TraceFileHandler::_openFile(const char* fileName)
143 {
|
144 gs.keenan 1.25 #ifdef PEGASUS_OS_VMS
145 // FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
146 FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
147 #else
|
148 kumpf 1.18 FILE* fileHandle = fopen(fileName,"a+");
|
149 gs.keenan 1.25 #endif
|
150 kumpf 1.18 if (!fileHandle)
151 {
152 // Unable to open file, log a message
|
153 marek 1.41 MessageLoaderParms parm(
154 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE_SYSMSG",
155 "Failed to open file $0: $1",
156 fileName,PEGASUS_SYSTEM_ERRORMSG_NLS);
157 _logError(TRCFH_FAILED_TO_OPEN_FILE_SYSMSG,parm);
|
158 kumpf 1.18 return 0;
|
159 mike 1.2 }
|
160 kumpf 1.9
161 //
|
162 kumpf 1.24 // Verify that the file has the correct owner
163 //
164 if (!System::verifyFileOwnership(fileName))
165 {
|
166 marek 1.41 MessageLoaderParms parm(
167 "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
168 "File $0 is not owned by user $1.",
169 fileName,
170 System::getEffectiveUserName());
171 _logError(TRCFH_UNEXPECTED_FILE_OWNER,parm);
|
172 kumpf 1.24 fclose(fileHandle);
173 return 0;
174 }
175
176 //
|
177 kumpf 1.18 // Set the file permissions to 0600
|
178 marek 1.12 //
|
179 chuck 1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
|
180 kumpf 1.18 if (!FileSystem::changeFilePermissions(
181 String(fileName), (S_IRUSR|S_IWUSR)) )
|
182 marek 1.12 #else
|
183 kumpf 1.18 if (!FileSystem::changeFilePermissions(
184 String(fileName), (_S_IREAD|_S_IWRITE)) )
|
185 marek 1.12 #endif
|
186 kumpf 1.9 {
|
187 marek 1.41 MessageLoaderParms parm(
188 "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
189 "Failed to set permissions on file $0",
190 fileName);
191 _logError(TRCFH_FAILED_TO_SET_FILE_PERMISSIONS,parm);
192 fclose(fileHandle);
193 return 0;
194 }
195
196 return fileHandle;
197 }
198
199 void TraceFileHandler::_logError(
200 ErrLogMessageIds msgID,
201 const MessageLoaderParms & parms)
202 {
|
203 ajay.rao 1.43 static Boolean isLogErrorProgress = false;
|
204 marek 1.41 // msgID has to be within range, else we have a severe coding error
205 PEGASUS_ASSERT((msgID >= TRCFH_FAILED_TO_OPEN_FILE_SYSMSG) &&
|
206 ajay.rao 1.43 (msgID <= TRCFH_UNABLE_TO_WRITE_TRACE_TO_FILE));
207 if (!isLogErrorProgress)
|
208 marek 1.41 {
|
209 ajay.rao 1.43 isLogErrorProgress = true;
210 if ((_logErrorBitField & (1 << msgID)) == 0)
211 {
212 // log message not yet written, write log message
213 Logger::put_l(
214 Logger::ERROR_LOG,
215 System::CIMSERVER,
216 Logger::WARNING,
217 parms);
218 // mark bit in log error field to flag that specific log message
219 // has been written
220 _logErrorBitField |= (1 << msgID);
221 }
222 isLogErrorProgress = false;
|
223 kumpf 1.18 }
|
224 marek 1.41 }
|
225 kumpf 1.18
|
226 amit99shah 1.46 Boolean TraceFileHandler::_fileExists(char* fileName)
227 {
228 if (!System::exists(fileName))
229 {
230 _fileHandle = _openFile(fileName);
231 if(!_fileHandle)
232 {
233 return false;
234 }
235 }
236
237 Uint32 traceFileSize = 0;
238
239 if(!FileSystem::getFileSize(_fileName, traceFileSize))
240 {
241 return false;
242 }
243
244 /* Check if the size of the tracefile
245 is exceeding configured value.*/
246 if(traceFileSize > _maxTraceFileSizeBytes)
247 amit99shah 1.46 {
248 rollTraceFile(_fileName);
249 }
250
251 return true;
252 }
253
254 void TraceFileHandler::setMaxTraceFileSize(Uint32 maxTraceFileSizeBytes)
255 {
256 _maxTraceFileSizeBytes = maxTraceFileSizeBytes;
257 }
258
259 void TraceFileHandler::setMaxTraceFileNumber(Uint32 maxTraceFileNumber)
260 {
261 _maxTraceFileNumber = maxTraceFileNumber;
262 }
263
264 void TraceFileHandler::rollTraceFile(const char* fileName)
265 {
266 // Close the File
267 if (_fileHandle)
268 amit99shah 1.46 {
269 fclose(_fileHandle);
270 _fileHandle = 0;
271 }
272
273 String oldFileName;
274 oldFileName.append(fileName);
275 oldFileName.append(".");
276 char buffer[5];
277 Uint32 size = 0;
278 oldFileName.append(Uint32ToString(buffer, _maxTraceFileNumber, size));
279
280 String oldFileName_s(oldFileName);
281 FileSystem::removeFile(oldFileName_s);
282 Uint32 n = strlen(fileName) + 1;
283
284 for(Uint32 i = _maxTraceFileNumber ; i > 1 ; i--)
285 {
286 String newFileName_s = oldFileName_s;
287 oldFileName = oldFileName.subString(0, n);
288 oldFileName.append(Uint32ToString(buffer, (i-1), size));
289 amit99shah 1.46 oldFileName_s.assign(oldFileName);
290 FileSystem::renameFile(oldFileName_s, newFileName_s);
291 }
292
293 String fileName_s(fileName);
294 FileSystem::renameFile(fileName_s, oldFileName_s);
295
296 _fileHandle = TraceFileHandler::_openFile(fileName);
297 }
|
298 mike 1.2
299 PEGASUS_NAMESPACE_END
|