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 // Check if the file has been deleted, if so re-open the file and
60 // continue
61 if (!System::exists(_fileName))
62 {
63 if (_fileHandle == 0)
64 {
65 _fileHandle = fopen(_fileName, "a+", "shr=get,put,upd");
66 // _fileHandle = fopen(_fileName,"a","shr=get");
67 }
68 else
69 {
70 _fileHandle =
71 freopen(_fileName, "a+", _fileHandle, "shr=get,put,upd");
72 // _fileHandle = freopen(_fileName,"a",_fileHandle,"shr=get");
73 }
74 if (!_fileHandle)
75 {
76 // Unable to re-open file, log a message
77
|
78 marek 1.10 Logger::put_l(
|
79 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
80 MessageLoaderParms(
81 "Common.TraceFileHandlerUnix.FAILED_TO_OPEN_FILE",
82 "Failed to open File $0",
83 _fileName));
|
84 mike 1.2 return;
85 }
86
87 // Set permissions on the trace file to 0400
88
|
89 kumpf 1.7 if (!FileSystem::changeFilePermissions(
90 String(_fileName), (S_IRUSR | S_IWUSR)))
|
91 mike 1.2 {
|
92 kumpf 1.7 Logger::put_l(
|
93 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
94 MessageLoaderParms(
95 "Common.TraceFileHandlerUnix."
96 "FAILED_TO_SET_FILE_PERMISSIONS",
97 "Failed to set permissions on file $0",
98 _fileName));
|
99 mike 1.2 return;
100 }
101 }
|
102 marek 1.8 // Seek to the end of File
103 retCode = fseek(_fileHandle, 0, SEEK_END);
104 }
|
105 mike 1.2
|
106 marek 1.8 void TraceFileHandler::handleMessage(
107 const char *message,
108 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
|
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 _wroteToLog = false;
127 // retCode = fclose(_fileHandle);
128 // _fileHandle = 0;
129
130 // ---- END CRITICAL SECTION
131
132 return;
133 }
134
|
135 marek 1.8 void TraceFileHandler::handleMessage(const char *message)
|
136 mike 1.2 {
|
137 marek 1.8 Sint32 retCode;
138 Sint32 fileDesc;
|
139 mike 1.2
140 // Do not add Trace calls in the Critical section
141 // ---- BEGIN CRITICAL SECTION
142
|
143 marek 1.8 prepareFileHandle();
144
145 // Write the message to the file
146
147 retCode = fprintf(_fileHandle, "%s\n", message);
148 retCode = fflush(_fileHandle);
149 fileDesc = fileno(_fileHandle);
150 retCode = fsync(fileDesc);
151 _wroteToLog = false;
152 // retCode = fclose(_fileHandle);
153 // _fileHandle = 0;
154
155 // ---- END CRITICAL SECTION
156
157 return;
158 }
159
160
161 #else /* PEGASUS_OS_VMS */
162
163 void TraceFileHandler::prepareFileHandle(void)
164 marek 1.8 {
|
165 mike 1.2 // If the file has been deleted, re-open it and continue
166 if (!System::exists(_fileName))
167 {
168 fclose(_fileHandle);
169 _fileHandle = _openFile(_fileName);
170 if (!_fileHandle)
171 {
172 return;
173 }
174 }
175
176 // Got the Lock on the File. Seek to the end of File
177 fseek(_fileHandle, 0, SEEK_END);
178 # ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
179 long pos = ftell(_fileHandle);
180 // Check if the file size is approaching 2GB - which is the
181 // maximum size a file on 32 bit Linux can grow (ofcourse if
182 // not using large-files option). If this is not checked, the
183 // cimserver may get a SIGXFSZ signal and shutdown. See Bug#1527.
184 if (pos >= 0x7ff00000)
185 {
186 mike 1.2 // If the file size is almost 2 GB in size, close this trace
187 // file and open a new trace file which would have _fileCount
188 // as the suffix. So, if "cimserver.trc" is the trace file that
189 // approaches 2GB, the next file which gets created would be
190 // named "cimserver.trc.1" and so on ...
191 fclose(_fileHandle);
192 sprintf(_fileName, "%s.%u", _baseFileName, ++_fileCount);
193 _fileHandle = fopen(_fileName, "a+");
194 if (!_fileHandle)
195 {
196 // Unable to open file, log a message
197 if (!_wroteToLog)
198 {
|
199 kumpf 1.7 Logger::put_l(
|
200 kumpf 1.11 Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
201 MessageLoaderParms(
202 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
203 "Failed to open File $0",
204 _fileName));
|
205 mike 1.2 _wroteToLog = true;
206 }
207 return;
208 }
209 }
210 # endif
|
211 marek 1.8 }
212
213 void TraceFileHandler::handleMessage(
214 const char *message,
215 const char *fmt, va_list argList)
216 {
217 if (!_fileHandle)
218 {
219 // The trace file is not open, which means an earlier fopen() was
220 // unsuccessful. Stop now to avoid logging duplicate error messages.
221 return;
222 }
223
224 // Do not add Trace calls in the Critical section
225 // ---- BEGIN CRITICAL SECTION
226 AutoMutex writeLock(writeMutex);
|
227 mike 1.2
|
228 marek 1.8 prepareFileHandle();
|
229 mike 1.2 // Write the message to the file
230 fprintf(_fileHandle, "%s", message);
231 vfprintf(_fileHandle, fmt, argList);
232 fprintf(_fileHandle, "\n");
233 fflush(_fileHandle);
|
234 marek 1.8 // ---- END CRITICAL SECTION
235 }
236
237 void TraceFileHandler::handleMessage(const char *message)
238 {
239 if (!_fileHandle)
240 {
241 // The trace file is not open, which means an earlier fopen() was
242 // unsuccessful. Stop now to avoid logging duplicate error messages.
243 return;
244 }
245
246 // Do not add Trace calls in the Critical section
247 // ---- BEGIN CRITICAL SECTION
248 AutoMutex writeLock(writeMutex);
249
250 prepareFileHandle();
251 // Write the message to the file
252 fprintf(_fileHandle, "%s\n", message);
253 fflush(_fileHandle);
254 // ---- END CRITICAL SECTION
|
255 mike 1.2 }
256
|
257 marek 1.8
|
258 mike 1.2 #endif /* !PEGASUS_OS_VMS */
259
260 PEGASUS_NAMESPACE_END
|