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 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include <Pegasus/Common/FileSystem.h>
35 #include <Pegasus/Common/TraceFileHandler.h>
36
37 #if defined(PEGASUS_OS_TYPE_WINDOWS)
38 # include <Pegasus/Common/TraceFileHandlerWindows.cpp>
|
39 kumpf 1.30 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
|
40 mike 1.29 # include <Pegasus/Common/TraceFileHandlerPOSIX.cpp>
|
41 mike 1.2 #else
42 # error "Unsupported platform"
43 #endif
44
45
46 PEGASUS_USING_STD;
47
48 PEGASUS_NAMESPACE_BEGIN
49
50 ////////////////////////////////////////////////////////////////////////////////
51 // Constructs TraceFileHandler
52 ////////////////////////////////////////////////////////////////////////////////
53
|
54 kumpf 1.31 TraceFileHandler::TraceFileHandler()
|
55 mike 1.2 {
|
56 kumpf 1.3 _fileName = 0;
|
57 mike 1.2 _fileHandle = 0;
58 _wroteToLog = false;
|
59 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
60 _baseFileName = 0;
61 _fileCount = 0;
62 #endif
|
63 mike 1.2 }
64
65 ////////////////////////////////////////////////////////////////////////////////
66 // Destructs TraceFileHandler
67 ////////////////////////////////////////////////////////////////////////////////
68
|
69 kumpf 1.31 TraceFileHandler::~TraceFileHandler()
|
70 mike 1.2 {
|
71 kumpf 1.31 // Close the File
|
72 mike 1.2 if (_fileHandle)
73 {
74 fclose(_fileHandle);
75 }
|
76 kumpf 1.28 delete [] _fileName;
|
77 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
|
78 kumpf 1.28 delete [] _baseFileName;
|
79 a.arora 1.16 #endif
|
80 mike 1.2 }
81
82 ////////////////////////////////////////////////////////////////////////////////
83 // Sets the filename to the given filename and opens the file in append
84 // mode
85 ////////////////////////////////////////////////////////////////////////////////
86
87 Uint32 TraceFileHandler::setFileName(const char* fileName)
88 {
|
89 kumpf 1.18 // If a file is already open, close it
90 if (_fileHandle)
91 {
92 fclose(_fileHandle);
93 _fileHandle = 0;
94 }
95
96 delete [] _fileName;
97 _fileName = 0;
98 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
99 delete [] _baseFileName;
100 _baseFileName = 0;
101 #endif
102
|
103 mike 1.2 if (!isValidFilePath(fileName))
104 {
|
105 kumpf 1.31 return 1;
|
106 mike 1.2 }
|
107 kumpf 1.18 _fileHandle = _openFile(fileName);
|
108 mike 1.2 if (!_fileHandle)
109 {
|
110 kumpf 1.18 return 1;
|
111 mike 1.2 }
|
112 kumpf 1.18
113 _fileName = new char[strlen(fileName)+1];
114 strcpy(_fileName, fileName);
|
115 a.arora 1.16 #ifdef PEGASUS_PLATFORM_LINUX_GENERIC_GNU
|
116 kumpf 1.18 _baseFileName = new char[strlen(fileName)+1];
117 strcpy(_baseFileName, fileName);
|
118 a.arora 1.16 #endif
|
119 kumpf 1.18
120 return 0;
121 }
122
123 FILE* TraceFileHandler::_openFile(const char* fileName)
124 {
|
125 gs.keenan 1.25 #ifdef PEGASUS_OS_VMS
126 // FILE* fileHandle = fopen(fileName,"a+", "shr=get,put,upd");
127 FILE* fileHandle = fopen(fileName,"w", "shr=get,put,upd");
128 #else
|
129 kumpf 1.18 FILE* fileHandle = fopen(fileName,"a+");
|
130 gs.keenan 1.25 #endif
|
131 kumpf 1.18 if (!fileHandle)
132 {
133 // Unable to open file, log a message
|
134 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
135 kumpf 1.18 "Common.TraceFileHandler.FAILED_TO_OPEN_FILE",
136 "Failed to open file $0", fileName);
137 return 0;
|
138 mike 1.2 }
|
139 kumpf 1.9
140 //
|
141 kumpf 1.24 // Verify that the file has the correct owner
142 //
143 if (!System::verifyFileOwnership(fileName))
144 {
145 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::WARNING,
146 "Common.TraceFileHandler.UNEXPECTED_FILE_OWNER",
147 "File $0 is not owned by user $1.", fileName,
148 System::getEffectiveUserName());
149 fclose(fileHandle);
150 return 0;
151 }
152
153 //
|
154 kumpf 1.18 // Set the file permissions to 0600
|
155 marek 1.12 //
|
156 chuck 1.15 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
|
157 kumpf 1.18 if (!FileSystem::changeFilePermissions(
158 String(fileName), (S_IRUSR|S_IWUSR)) )
|
159 marek 1.12 #else
|
160 kumpf 1.18 if (!FileSystem::changeFilePermissions(
161 String(fileName), (_S_IREAD|_S_IWRITE)) )
|
162 marek 1.12 #endif
|
163 kumpf 1.9 {
|
164 kumpf 1.24 Logger::put_l(Logger::DEBUG_LOG, System::CIMSERVER, Logger::WARNING,
|
165 kumpf 1.9 "Common.TraceFileHandler.FAILED_TO_SET_FILE_PERMISSIONS",
|
166 kumpf 1.18 "Failed to set permissions on file $0", fileName);
167 fclose(fileHandle);
168 return 0;
169 }
170
171 return fileHandle;
|
172 mike 1.2 }
173
174 Boolean TraceFileHandler::isValidFilePath(const char* filePath)
175 {
176 String fileName = String(filePath);
177
178 // Check if the file path is a directory
179 FileSystem::translateSlashes(fileName);
180 if (FileSystem::isDirectory(fileName))
181 {
|
182 kumpf 1.31 return 0;
|
183 mike 1.2 }
184
185 // Check if the file exists and is writable
186 if (FileSystem::exists(fileName))
187 {
188 if (!FileSystem::canWrite(fileName))
189 {
|
190 kumpf 1.31 return 0;
|
191 mike 1.2 }
|
192 kumpf 1.31 else
193 {
194 return 1;
|
195 mike 1.2 }
196 }
197 else
198 {
199 // Check if directory is writable
|
200 kumpf 1.6 Uint32 index = fileName.reverseFind('/');
|
201 mike 1.2
|
202 kumpf 1.6 if (index != PEG_NOT_FOUND)
|
203 kumpf 1.31 {
|
204 kumpf 1.6 String dirName = fileName.subString(0,index);
|
205 mike 1.2 if (!FileSystem::isDirectory(dirName))
206 {
|
207 kumpf 1.31 return 0;
|
208 mike 1.2 }
209 if (!FileSystem::canWrite(dirName) )
210 {
|
211 kumpf 1.31 return 0;
|
212 mike 1.2 }
|
213 kumpf 1.31 else
214 {
215 return 1;
|
216 mike 1.2 }
217 }
|
218 kumpf 1.31 else
|
219 mike 1.2 {
220 String currentDir;
221
222 // Check if there is permission to write in the
223 // current working directory
224 FileSystem::getCurrentDirectory(currentDir);
225
226 if (!FileSystem::canWrite(currentDir))
227 {
|
228 kumpf 1.31 return 0;
|
229 mike 1.2 }
|
230 kumpf 1.31 else
231 {
232 return 1;
|
233 mike 1.2 }
234 }
235 }
|
236 carson.hovey 1.22 PEGASUS_UNREACHABLE(return 1;)
|
237 mike 1.2 }
238 PEGASUS_NAMESPACE_END
|