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