1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #if defined(PEGASUS_OS_VMS)
35 # include <fcntl.h>
36 #endif
37 #include <Pegasus/Common/Logger.h>
38 #include <Pegasus/Common/System.h>
39 #include <Pegasus/Common/TraceFileHandler.h>
40 #include <Pegasus/Common/Mutex.h>
41
42 PEGASUS_USING_STD;
43 mike 1.2
|
44 kumpf 1.5 PEGASUS_NAMESPACE_BEGIN
45
46 static Mutex writeMutex;
47 PEGASUS_FORK_SAFE_MUTEX(writeMutex)
|
48 mike 1.2
49 ///////////////////////////////////////////////////////////////////////////////
50 // Writes message to file. Locks the file before writing to it
51 // Implementation of this function is platform specific
52 ///////////////////////////////////////////////////////////////////////////////
53
54 #if defined(PEGASUS_OS_VMS)
55
|
56 marek 1.8 void TraceFileHandler::prepareFileHandle(void)
|
57 mike 1.2 {
58 Sint32 retCode;
|
59 thilo.boehm 1.13
60 if (_configHasChanged)
61 {
62 _reConfigure();
63 }
64
|
65 mike 1.2 // Check if the file has been deleted, if so re-open the file and
66 // continue
67 if (!System::exists(_fileName))
68 {
69 if (_fileHandle == 0)
70 {
71 _fileHandle = fopen(_fileName, "a+", "shr=get,put,upd");
72 // _fileHandle = fopen(_fileName,"a","shr=get");
73 }
74 else
75 {
76 _fileHandle =
77 freopen(_fileName, "a+", _fileHandle, "shr=get,put,upd");
78 // _fileHandle = freopen(_fileName,"a",_fileHandle,"shr=get");
79 }
80 if (!_fileHandle)
81 {
82 // Unable to re-open file, log a message
83
|
84 marek 1.10 Logger::put_l(
|
85 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
86 MessageLoaderParms(
87 "Common.TraceFileHandlerUnix.FAILED_TO_OPEN_FILE",
88 "Failed to open File $0",
89 _fileName));
|
90 mike 1.2 return;
91 }
92
93 // Set permissions on the trace file to 0400
94
|
95 kumpf 1.7 if (!FileSystem::changeFilePermissions(
96 String(_fileName), (S_IRUSR | S_IWUSR)))
|
97 mike 1.2 {
|
98 kumpf 1.7 Logger::put_l(
|
99 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
100 MessageLoaderParms(
101 "Common.TraceFileHandlerUnix."
102 "FAILED_TO_SET_FILE_PERMISSIONS",
103 "Failed to set permissions on file $0",
104 _fileName));
|
105 mike 1.2 return;
106 }
107 }
|
108 marek 1.8 // Seek to the end of File
109 retCode = fseek(_fileHandle, 0, SEEK_END);
110 }
|
111 mike 1.2
|
112 marek 1.8 void TraceFileHandler::handleMessage(
113 const char *message,
|
114 thilo.boehm 1.12 Uint32 msgLen,
|
115 marek 1.8 const char *fmt, va_list argList)
116 {
117 Sint32 retCode;
118 Sint32 fileDesc;
|
119 mike 1.2
|
120 marek 1.8 // Do not add Trace calls in the Critical section
121 // ---- BEGIN CRITICAL SECTION
|
122 mike 1.2
|
123 marek 1.8 prepareFileHandle();
124
|
125 mike 1.2 // Write the message to the file
126
127 retCode = fprintf(_fileHandle, "%s", message);
128 retCode = vfprintf(_fileHandle, fmt, argList);
129 retCode = fprintf(_fileHandle, "\n");
130 retCode = fflush(_fileHandle);
131 fileDesc = fileno(_fileHandle);
132 retCode = fsync(fileDesc);
133 _wroteToLog = false;
134 // retCode = fclose(_fileHandle);
135 // _fileHandle = 0;
136
137 // ---- END CRITICAL SECTION
138
139 return;
140 }
141
|
142 thilo.boehm 1.12 void TraceFileHandler::handleMessage(const char *message, Uint32 msgLen)
|
143 mike 1.2 {
|
144 marek 1.8 Sint32 retCode;
145 Sint32 fileDesc;
|
146 mike 1.2
147 // Do not add Trace calls in the Critical section
148 // ---- BEGIN CRITICAL SECTION
149
|
150 marek 1.8 prepareFileHandle();
151
152 // Write the message to the file
153
154 retCode = fprintf(_fileHandle, "%s\n", message);
155 retCode = fflush(_fileHandle);
156 fileDesc = fileno(_fileHandle);
157 retCode = fsync(fileDesc);
158 _wroteToLog = false;
159 // retCode = fclose(_fileHandle);
160 // _fileHandle = 0;
161
162 // ---- END CRITICAL SECTION
163
164 return;
165 }
166
167
168 #else /* PEGASUS_OS_VMS */
169
170 void TraceFileHandler::prepareFileHandle(void)
|
171 thilo.boehm 1.13 {
|
172 mike 1.2 // If the file has been deleted, re-open it and continue
173 if (!System::exists(_fileName))
174 {
175 fclose(_fileHandle);
176 _fileHandle = _openFile(_fileName);
177 if (!_fileHandle)
178 {
179 return;
180 }
181 }
182
183 // Got the Lock on the File. Seek to the end of File
184 fseek(_fileHandle, 0, SEEK_END);
185 # ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
186 long pos = ftell(_fileHandle);
187 // Check if the file size is approaching 2GB - which is the
188 // maximum size a file on 32 bit Linux can grow (ofcourse if
189 // not using large-files option). If this is not checked, the
190 // cimserver may get a SIGXFSZ signal and shutdown. See Bug#1527.
191 if (pos >= 0x7ff00000)
192 {
193 mike 1.2 // If the file size is almost 2 GB in size, close this trace
194 // file and open a new trace file which would have _fileCount
195 // as the suffix. So, if "cimserver.trc" is the trace file that
196 // approaches 2GB, the next file which gets created would be
197 // named "cimserver.trc.1" and so on ...
198 fclose(_fileHandle);
199 sprintf(_fileName, "%s.%u", _baseFileName, ++_fileCount);
200 _fileHandle = fopen(_fileName, "a+");
201 if (!_fileHandle)
202 {
203 // Unable to open file, log a message
204 if (!_wroteToLog)
205 {
|
206 kumpf 1.7 Logger::put_l(
|
207 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
208 MessageLoaderParms(
209 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
210 "Failed to open File $0",
211 _fileName));
|
212 mike 1.2 _wroteToLog = true;
213 }
214 return;
215 }
216 }
217 # endif
|
218 marek 1.8 }
219
220 void TraceFileHandler::handleMessage(
221 const char *message,
|
222 thilo.boehm 1.12 Uint32 msgLen,
|
223 marek 1.8 const char *fmt, va_list argList)
224 {
|
225 thilo.boehm 1.13 if (_configHasChanged)
226 {
227 _reConfigure();
228 }
229
|
230 marek 1.8 if (!_fileHandle)
231 {
232 // The trace file is not open, which means an earlier fopen() was
233 // unsuccessful. Stop now to avoid logging duplicate error messages.
234 return;
235 }
236
237 // Do not add Trace calls in the Critical section
238 // ---- BEGIN CRITICAL SECTION
239 AutoMutex writeLock(writeMutex);
|
240 mike 1.2
|
241 marek 1.8 prepareFileHandle();
|
242 mike 1.2 // Write the message to the file
243 fprintf(_fileHandle, "%s", message);
244 vfprintf(_fileHandle, fmt, argList);
245 fprintf(_fileHandle, "\n");
246 fflush(_fileHandle);
|
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 fflush(_fileHandle);
272 // ---- END CRITICAL SECTION
|
273 mike 1.2 }
274
|
275 marek 1.8
|
276 mike 1.2 #endif /* !PEGASUS_OS_VMS */
277
278 PEGASUS_NAMESPACE_END
|