1 karl 1.19 //%2005////////////////////////////////////////////////////////////////////////
|
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 mike 1.2 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
|
18 kumpf 1.4 //
|
19 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
20 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //==============================================================================
29 //
30 // Author: Sushma Fernandes, Hewlett-Packard Company (sushma_fernandes@hp.com)
31 //
32 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
33 // added nsk platform support
|
34 a.arora 1.16 // Amit K Arora, IBM (amita@in.ibm.com) for Bug#1527
|
35 kumpf 1.18 // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
|
36 gs.keenan 1.20 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
|
37 mike 1.2 //
38 //%/////////////////////////////////////////////////////////////////////////////
39
|
40 david.dillard 1.23 //#include <fstream>
41 //#include <iostream>
|
42 mike 1.2 #include <Pegasus/Common/FileSystem.h>
43 #include <Pegasus/Common/TraceFileHandler.h>
44
45 #if defined(PEGASUS_OS_TYPE_WINDOWS)
46 # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
47 #elif defined(PEGASUS_OS_TYPE_UNIX)
48 # include <Pegasus/Common/TraceFileHandlerUnix.cpp>
49 #elif defined(PEGASUS_OS_TYPE_NSK)
50 # include <Pegasus/Common/TraceFileHandlerNsk.cpp>
|
51 gs.keenan 1.20 #elif defined(PEGASUS_OS_VMS)
52 # include <Pegasus/Common/TraceFileHandlerVms.cpp>
|
53 mike 1.2 #else
54 # error "Unsupported platform"
55 #endif
56
57
58 PEGASUS_USING_STD;
59
60 PEGASUS_NAMESPACE_BEGIN
61
62 ////////////////////////////////////////////////////////////////////////////////
63 // Constructs TraceFileHandler
64 ////////////////////////////////////////////////////////////////////////////////
65
66 TraceFileHandler::TraceFileHandler ()
67 {
|
68 kumpf 1.3 _fileName = 0;
|
69 mike 1.2 _fileHandle = 0;
70 _wroteToLog = false;
|
71 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
72 _baseFileName = 0;
73 _fileCount = 0;
74 #endif
|
75 mike 1.2 }
76
77 ////////////////////////////////////////////////////////////////////////////////
78 // Destructs TraceFileHandler
79 ////////////////////////////////////////////////////////////////////////////////
80
81 TraceFileHandler::~TraceFileHandler ()
82 {
83 // Close the File
84 if (_fileHandle)
85 {
86 fclose(_fileHandle);
87 }
|
88 kumpf 1.3 if (_fileName)
89 {
90 delete []_fileName;
91 }
|
92 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
93 if (_baseFileName)
94 {
95 delete []_baseFileName;
96 }
97 #endif
|
98 mike 1.2 }
99
100 ////////////////////////////////////////////////////////////////////////////////
101 // Sets the filename to the given filename and opens the file in append
102 // mode
103 ////////////////////////////////////////////////////////////////////////////////
104
105 Uint32 TraceFileHandler::setFileName(const char* fileName)
106 {
|
107 kumpf 1.18 // If a file is already open, close it
108 if (_fileHandle)
109 {
110 fclose(_fileHandle);
111 _fileHandle = 0;
112 }
113
114 delete [] _fileName;
115 _fileName = 0;
116 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
117 delete [] _baseFileName;
118 _baseFileName = 0;
119 #endif
120
|
121 mike 1.2 if (!isValidFilePath(fileName))
122 {
123 return 1;
124 }
|
125 kumpf 1.18 _fileHandle = _openFile(fileName);
|
126 mike 1.2 if (!_fileHandle)
127 {
|
128 kumpf 1.18 return 1;
|
129 mike 1.2 }
|
130 kumpf 1.18
131 _fileName = new char[strlen(fileName)+1];
132 strcpy(_fileName, fileName);
|
133 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
|
134 kumpf 1.18 _baseFileName = new char[strlen(fileName)+1];
135 strcpy(_baseFileName, fileName);
|
136 a.arora 1.16 #endif
|
137 kumpf 1.18
138 return 0;
139 }
140
141 FILE* TraceFileHandler::_openFile(const char* fileName)
142 {
|
143 gs.keenan 1.25 #ifdef PEGASUS_OS_VMS
144 // FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
145 FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
146 #else
|
147 kumpf 1.18 FILE* fileHandle = fopen(fileName,"a+");
|
148 gs.keenan 1.25 #endif
|
149 kumpf 1.18 if (!fileHandle)
150 {
151 // Unable to open file, log a message
|
152 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
153 kumpf 1.18 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
154 "Failed to open file $0", fileName);
155 return 0;
|
156 mike 1.2 }
|
157 kumpf 1.9
158 //
|
159 kumpf 1.24 // Verify that the file has the correct owner
160 //
161 if (!System::verifyFileOwnership(fileName))
162 {
163 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
164 "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
165 "File $0 is not owned by user $1.", fileName,
166 System::getEffectiveUserName());
167 fclose(fileHandle);
168 return 0;
169 }
170
171 //
|
172 kumpf 1.18 // Set the file permissions to 0600
|
173 marek 1.12 //
|
174 chuck 1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
|
175 kumpf 1.18 if (!FileSystem::changeFilePermissions(
176 String(fileName), (S_IRUSR|S_IWUSR)) )
|
177 marek 1.12 #else
|
178 kumpf 1.18 if (!FileSystem::changeFilePermissions(
179 String(fileName), (_S_IREAD|_S_IWRITE)) )
|
180 marek 1.12 #endif
|
181 kumpf 1.9 {
|
182 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
183 kumpf 1.9 "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
|
184 kumpf 1.18 "Failed to set permissions on file $0", fileName);
185 fclose(fileHandle);
186 return 0;
187 }
188
189 return fileHandle;
|
190 mike 1.2 }
191
192 Boolean TraceFileHandler::isValidFilePath(const char* filePath)
193 {
194 String fileName = String(filePath);
195
196 // Check if the file path is a directory
197 FileSystem::translateSlashes(fileName);
198 if (FileSystem::isDirectory(fileName))
199 {
200 return 0;
201 }
202
203 // Check if the file exists and is writable
204 if (FileSystem::exists(fileName))
205 {
206 if (!FileSystem::canWrite(fileName))
207 {
208 return 0;
209 }
210 else
211 mike 1.2 {
212 return 1;
213 }
214 }
215 else
216 {
217 // Check if directory is writable
|
218 kumpf 1.6 Uint32 index = fileName.reverseFind('/');
|
219 mike 1.2
|
220 kumpf 1.6 if (index != PEG_NOT_FOUND)
|
221 mike 1.2 {
|
222 kumpf 1.6 String dirName = fileName.subString(0,index);
|
223 mike 1.2 if (!FileSystem::isDirectory(dirName))
224 {
225 return 0;
226 }
227 if (!FileSystem::canWrite(dirName) )
228 {
229 return 0;
230 }
231 else
232 {
233 return 1;
234 }
235 }
236 else
237 {
238 String currentDir;
239
240 // Check if there is permission to write in the
241 // current working directory
242 FileSystem::getCurrentDirectory(currentDir);
243
244 mike 1.2 if (!FileSystem::canWrite(currentDir))
245 {
246 return 0;
247 }
248 else
249 {
250 return 1;
251 }
252 }
253 }
|
254 carson.hovey 1.22 PEGASUS_UNREACHABLE(return 1;)
|
255 mike 1.2 }
256 PEGASUS_NAMESPACE_END
|