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