1 martin 1.14 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.15 //
|
3 martin 1.14 // 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.15 //
|
10 martin 1.14 // 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.15 //
|
17 martin 1.14 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.15 //
|
20 martin 1.14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.14 // 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.15 //
|
28 martin 1.14 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #if defined(PEGASUS_OS_VMS)
33 # include <fcntl.h>
34 #endif
35 #include <Pegasus/Common/Logger.h>
36 #include <Pegasus/Common/System.h>
37 #include <Pegasus/Common/TraceFileHandler.h>
38 #include <Pegasus/Common/Mutex.h>
39
40 PEGASUS_USING_STD;
41
|
42 kumpf 1.5 PEGASUS_NAMESPACE_BEGIN
43
44 static Mutex writeMutex;
45 PEGASUS_FORK_SAFE_MUTEX(writeMutex)
|
46 mike 1.2
47 ///////////////////////////////////////////////////////////////////////////////
48 // Writes message to file. Locks the file before writing to it
49 // Implementation of this function is platform specific
50 ///////////////////////////////////////////////////////////////////////////////
51
52 #if defined(PEGASUS_OS_VMS)
53
|
54 marek 1.8 void TraceFileHandler::prepareFileHandle(void)
|
55 mike 1.2 {
56 Sint32 retCode;
|
57 thilo.boehm 1.13
58 if (_configHasChanged)
59 {
60 _reConfigure();
61 }
62
|
63 mike 1.2 // Check if the file has been deleted, if so re-open the file and
64 // continue
65 if (!System::exists(_fileName))
66 {
67 if (_fileHandle == 0)
68 {
69 _fileHandle = fopen(_fileName, "a+", "shr=get,put,upd");
70 // _fileHandle = fopen(_fileName,"a","shr=get");
71 }
72 else
73 {
74 _fileHandle =
75 freopen(_fileName, "a+", _fileHandle, "shr=get,put,upd");
76 // _fileHandle = freopen(_fileName,"a",_fileHandle,"shr=get");
77 }
78 if (!_fileHandle)
79 {
80 // Unable to re-open file, log a message
81
|
82 marek 1.10 Logger::put_l(
|
83 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
84 MessageLoaderParms(
85 "Common.TraceFileHandlerUnix.FAILED_TO_OPEN_FILE",
86 "Failed to open File $0",
87 _fileName));
|
88 mike 1.2 return;
89 }
90
91 // Set permissions on the trace file to 0400
92
|
93 kumpf 1.7 if (!FileSystem::changeFilePermissions(
94 String(_fileName), (S_IRUSR | S_IWUSR)))
|
95 mike 1.2 {
|
96 kumpf 1.7 Logger::put_l(
|
97 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
98 MessageLoaderParms(
99 "Common.TraceFileHandlerUnix."
100 "FAILED_TO_SET_FILE_PERMISSIONS",
101 "Failed to set permissions on file $0",
102 _fileName));
|
103 mike 1.2 return;
104 }
105 }
|
106 marek 1.8 // Seek to the end of File
107 retCode = fseek(_fileHandle, 0, SEEK_END);
108 }
|
109 mike 1.2
|
110 marek 1.8 void TraceFileHandler::handleMessage(
111 const char *message,
|
112 thilo.boehm 1.12 Uint32 msgLen,
|
113 marek 1.8 const char *fmt, va_list argList)
114 {
115 Sint32 retCode;
116 Sint32 fileDesc;
|
117 mike 1.2
|
118 marek 1.8 // Do not add Trace calls in the Critical section
119 // ---- BEGIN CRITICAL SECTION
|
120 mike 1.2
|
121 marek 1.8 prepareFileHandle();
122
|
123 mike 1.2 // Write the message to the file
124
125 retCode = fprintf(_fileHandle, "%s", message);
126 retCode = vfprintf(_fileHandle, fmt, argList);
127 retCode = fprintf(_fileHandle, "\n");
128 retCode = fflush(_fileHandle);
129 fileDesc = fileno(_fileHandle);
130 retCode = fsync(fileDesc);
131 _wroteToLog = false;
132 // retCode = fclose(_fileHandle);
133 // _fileHandle = 0;
134
135 // ---- END CRITICAL SECTION
136
137 return;
138 }
139
|
140 thilo.boehm 1.12 void TraceFileHandler::handleMessage(const char *message, Uint32 msgLen)
|
141 mike 1.2 {
|
142 marek 1.8 Sint32 retCode;
143 Sint32 fileDesc;
|
144 mike 1.2
145 // Do not add Trace calls in the Critical section
146 // ---- BEGIN CRITICAL SECTION
147
|
148 marek 1.8 prepareFileHandle();
149
150 // Write the message to the file
151
152 retCode = fprintf(_fileHandle, "%s\n", message);
153 retCode = fflush(_fileHandle);
154 fileDesc = fileno(_fileHandle);
155 retCode = fsync(fileDesc);
156 _wroteToLog = false;
157 // retCode = fclose(_fileHandle);
158 // _fileHandle = 0;
159
160 // ---- END CRITICAL SECTION
161
162 return;
163 }
164
165
166 #else /* PEGASUS_OS_VMS */
167
168 void TraceFileHandler::prepareFileHandle(void)
|
169 thilo.boehm 1.13 {
|
170 mike 1.2 // If the file has been deleted, re-open it and continue
171 if (!System::exists(_fileName))
172 {
173 fclose(_fileHandle);
174 _fileHandle = _openFile(_fileName);
175 if (!_fileHandle)
176 {
177 return;
178 }
179 }
180
181 // Got the Lock on the File. Seek to the end of File
182 fseek(_fileHandle, 0, SEEK_END);
183 # ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
184 long pos = ftell(_fileHandle);
185 // Check if the file size is approaching 2GB - which is the
186 // maximum size a file on 32 bit Linux can grow (ofcourse if
187 // not using large-files option). If this is not checked, the
188 // cimserver may get a SIGXFSZ signal and shutdown. See Bug#1527.
189 if (pos >= 0x7ff00000)
190 {
191 mike 1.2 // If the file size is almost 2 GB in size, close this trace
192 // file and open a new trace file which would have _fileCount
193 // as the suffix. So, if "cimserver.trc" is the trace file that
194 // approaches 2GB, the next file which gets created would be
195 // named "cimserver.trc.1" and so on ...
196 fclose(_fileHandle);
197 sprintf(_fileName, "%s.%u", _baseFileName, ++_fileCount);
198 _fileHandle = fopen(_fileName, "a+");
199 if (!_fileHandle)
200 {
201 // Unable to open file, log a message
202 if (!_wroteToLog)
203 {
|
204 kumpf 1.7 Logger::put_l(
|
205 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
206 MessageLoaderParms(
207 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
208 "Failed to open File $0",
209 _fileName));
|
210 mike 1.2 _wroteToLog = true;
211 }
212 return;
213 }
214 }
215 # endif
|
216 marek 1.8 }
217
218 void TraceFileHandler::handleMessage(
219 const char *message,
|
220 thilo.boehm 1.12 Uint32 msgLen,
|
221 marek 1.8 const char *fmt, va_list argList)
222 {
|
223 thilo.boehm 1.13 if (_configHasChanged)
224 {
225 _reConfigure();
226 }
227
|
228 marek 1.8 if (!_fileHandle)
229 {
230 // The trace file is not open, which means an earlier fopen() was
231 // unsuccessful. Stop now to avoid logging duplicate error messages.
232 return;
233 }
234
235 // Do not add Trace calls in the Critical section
236 // ---- BEGIN CRITICAL SECTION
237 AutoMutex writeLock(writeMutex);
|
238 mike 1.2
|
239 marek 1.8 prepareFileHandle();
|
240 mike 1.2 // Write the message to the file
241 fprintf(_fileHandle, "%s", message);
242 vfprintf(_fileHandle, fmt, argList);
243 fprintf(_fileHandle, "\n");
244 fflush(_fileHandle);
|
245 marek 1.8 // ---- END CRITICAL SECTION
246 }
247
|
248 thilo.boehm 1.12 void TraceFileHandler::handleMessage(const char *message, Uint32 msgLen)
|
249 marek 1.8 {
|
250 thilo.boehm 1.13 if (_configHasChanged)
251 {
252 _reConfigure();
253 }
254
|
255 marek 1.8 if (!_fileHandle)
256 {
257 // The trace file is not open, which means an earlier fopen() was
258 // unsuccessful. Stop now to avoid logging duplicate error messages.
259 return;
260 }
261
262 // Do not add Trace calls in the Critical section
263 // ---- BEGIN CRITICAL SECTION
264 AutoMutex writeLock(writeMutex);
265
266 prepareFileHandle();
267 // Write the message to the file
268 fprintf(_fileHandle, "%s\n", message);
269 fflush(_fileHandle);
270 // ---- END CRITICAL SECTION
|
271 mike 1.2 }
272
|
273 marek 1.8
|
274 mike 1.2 #endif /* !PEGASUS_OS_VMS */
275
276 PEGASUS_NAMESPACE_END
|