1 karl 1.27 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
|
3 karl 1.17 // 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 karl 1.8 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.17 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.19 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.27 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.2 //
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 kumpf 1.4 //
|
21 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // 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 // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
33 //
34 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
35 // added nsk platform support
|
36 a.arora 1.16 // Amit K Arora, IBM (amita@in.ibm.com) for Bug#1527
|
37 kumpf 1.18 // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
|
38 gs.keenan 1.20 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
|
39 mike 1.2 //
40 //%/////////////////////////////////////////////////////////////////////////////
41
|
42 david.dillard 1.23 //#include <fstream>
43 //#include <iostream>
|
44 mike 1.2 #include <Pegasus/Common/FileSystem.h>
45 #include <Pegasus/Common/TraceFileHandler.h>
46
47 #if defined(PEGASUS_OS_TYPE_WINDOWS)
48 # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
49 #elif defined(PEGASUS_OS_TYPE_UNIX)
50 # include <Pegasus/Common/TraceFileHandlerUnix.cpp>
51 #elif defined(PEGASUS_OS_TYPE_NSK)
52 # include <Pegasus/Common/TraceFileHandlerNsk.cpp>
|
53 gs.keenan 1.20 #elif defined(PEGASUS_OS_VMS)
54 # include <Pegasus/Common/TraceFileHandlerVms.cpp>
|
55 mike 1.2 #else
56 # error "Unsupported platform"
57 #endif
58
59
60 PEGASUS_USING_STD;
61
62 PEGASUS_NAMESPACE_BEGIN
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // Constructs TraceFileHandler
66 ////////////////////////////////////////////////////////////////////////////////
67
68 TraceFileHandler::TraceFileHandler ()
69 {
|
70 kumpf 1.3 _fileName = 0;
|
71 mike 1.2 _fileHandle = 0;
72 _wroteToLog = false;
|
73 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
74 _baseFileName = 0;
75 _fileCount = 0;
76 #endif
|
77 mike 1.2 }
78
79 ////////////////////////////////////////////////////////////////////////////////
80 // Destructs TraceFileHandler
81 ////////////////////////////////////////////////////////////////////////////////
82
83 TraceFileHandler::~TraceFileHandler ()
84 {
85 // Close the File
86 if (_fileHandle)
87 {
88 fclose(_fileHandle);
89 }
|
90 kumpf 1.3 if (_fileName)
91 {
92 delete []_fileName;
93 }
|
94 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
95 if (_baseFileName)
96 {
97 delete []_baseFileName;
98 }
99 #endif
|
100 mike 1.2 }
101
102 ////////////////////////////////////////////////////////////////////////////////
103 // Sets the filename to the given filename and opens the file in append
104 // mode
105 ////////////////////////////////////////////////////////////////////////////////
106
107 Uint32 TraceFileHandler::setFileName(const char* fileName)
108 {
|
109 kumpf 1.18 // If a file is already open, close it
110 if (_fileHandle)
111 {
112 fclose(_fileHandle);
113 _fileHandle = 0;
114 }
115
116 delete [] _fileName;
117 _fileName = 0;
118 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
119 delete [] _baseFileName;
120 _baseFileName = 0;
121 #endif
122
|
123 mike 1.2 if (!isValidFilePath(fileName))
124 {
125 return 1;
126 }
|
127 kumpf 1.18 _fileHandle = _openFile(fileName);
|
128 mike 1.2 if (!_fileHandle)
129 {
|
130 kumpf 1.18 return 1;
|
131 mike 1.2 }
|
132 kumpf 1.18
133 _fileName = new char[strlen(fileName)+1];
134 strcpy(_fileName, fileName);
|
135 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
|
136 kumpf 1.18 _baseFileName = new char[strlen(fileName)+1];
137 strcpy(_baseFileName, fileName);
|
138 a.arora 1.16 #endif
|
139 kumpf 1.18
140 return 0;
141 }
142
143 FILE* TraceFileHandler::_openFile(const char* fileName)
144 {
|
145 gs.keenan 1.25 #ifdef PEGASUS_OS_VMS
146 // FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
147 FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
148 #else
|
149 kumpf 1.18 FILE* fileHandle = fopen(fileName,"a+");
|
150 gs.keenan 1.25 #endif
|
151 kumpf 1.18 if (!fileHandle)
152 {
153 // Unable to open file, log a message
|
154 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
155 kumpf 1.18 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
156 "Failed to open file $0", fileName);
157 return 0;
|
158 mike 1.2 }
|
159 kumpf 1.9
160 //
|
161 kumpf 1.24 // Verify that the file has the correct owner
162 //
163 if (!System::verifyFileOwnership(fileName))
164 {
165 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
166 "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
167 "File $0 is not owned by user $1.", fileName,
168 System::getEffectiveUserName());
169 fclose(fileHandle);
170 return 0;
171 }
172
173 //
|
174 kumpf 1.18 // Set the file permissions to 0600
|
175 marek 1.12 //
|
176 chuck 1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
|
177 kumpf 1.18 if (!FileSystem::changeFilePermissions(
178 String(fileName), (S_IRUSR|S_IWUSR)) )
|
179 marek 1.12 #else
|
180 kumpf 1.18 if (!FileSystem::changeFilePermissions(
181 String(fileName), (_S_IREAD|_S_IWRITE)) )
|
182 marek 1.12 #endif
|
183 kumpf 1.9 {
|
184 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
185 kumpf 1.9 "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
|
186 kumpf 1.18 "Failed to set permissions on file $0", fileName);
187 fclose(fileHandle);
188 return 0;
189 }
190
191 return fileHandle;
|
192 mike 1.2 }
193
194 Boolean TraceFileHandler::isValidFilePath(const char* filePath)
195 {
196 String fileName = String(filePath);
197
198 // Check if the file path is a directory
199 FileSystem::translateSlashes(fileName);
200 if (FileSystem::isDirectory(fileName))
201 {
202 return 0;
203 }
204
205 // Check if the file exists and is writable
206 if (FileSystem::exists(fileName))
207 {
208 if (!FileSystem::canWrite(fileName))
209 {
210 return 0;
211 }
212 else
213 mike 1.2 {
214 return 1;
215 }
216 }
217 else
218 {
219 // Check if directory is writable
|
220 kumpf 1.6 Uint32 index = fileName.reverseFind('/');
|
221 mike 1.2
|
222 kumpf 1.6 if (index != PEG_NOT_FOUND)
|
223 mike 1.2 {
|
224 kumpf 1.6 String dirName = fileName.subString(0,index);
|
225 mike 1.2 if (!FileSystem::isDirectory(dirName))
226 {
227 return 0;
228 }
229 if (!FileSystem::canWrite(dirName) )
230 {
231 return 0;
232 }
233 else
234 {
235 return 1;
236 }
237 }
238 else
239 {
240 String currentDir;
241
242 // Check if there is permission to write in the
243 // current working directory
244 FileSystem::getCurrentDirectory(currentDir);
245
246 mike 1.2 if (!FileSystem::canWrite(currentDir))
247 {
248 return 0;
249 }
250 else
251 {
252 return 1;
253 }
254 }
255 }
|
256 carson.hovey 1.22 PEGASUS_UNREACHABLE(return 1;)
|
257 mike 1.2 }
258 PEGASUS_NAMESPACE_END
|