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