1 karl 1.59 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.13 //
|
3 karl 1.45 // 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.35 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.45 // 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.49 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.59 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
15 mike 1.14 // 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 mike 1.13 // 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 karl 1.45 //
|
21 mike 1.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
22 mike 1.13 // 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 mike 1.14 // 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 mike 1.13 // 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 #ifndef Pegasus_System_h
35 #define Pegasus_System_h
36
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/String.h>
|
39 kumpf 1.26 #include <Pegasus/Common/Linkage.h>
|
40 david 1.32 #include <Pegasus/Common/Logger.h>
|
41 dmitry.mikulin 1.76 #include <Pegasus/Common/Network.h>
|
42 kumpf 1.36 #include <sys/stat.h>
|
43 mike 1.13
|
44 kumpf 1.37
|
45 kumpf 1.38 #if defined(PEGASUS_OS_TYPE_WINDOWS)
|
46 kumpf 1.37 #ifndef mode_t
|
47 kumpf 1.38 typedef unsigned long mode_t;
|
48 kumpf 1.37 #endif
|
49 mreddy 1.73 #include <windows.h>
|
50 kumpf 1.37 #endif
51
|
52 carson.hovey 1.77 #if defined (PEGASUS_OS_TYPE_UNIX) || \
53 defined (PEGASUS_OS_VMS)
|
54 ouyang.jian 1.70 # include <unistd.h>
|
55 kumpf 1.63 # include <fcntl.h> // File locking
|
56 kumpf 1.60 # define PEGASUS_UID_T uid_t
57 # define PEGASUS_GID_T gid_t
58 #else
59 # define PEGASUS_UID_T Uint32
60 # define PEGASUS_GID_T Uint32
61 #endif
|
62 konrad.r 1.41
|
63 kumpf 1.17 //
64 // Protocal Type
65 //
66 #define TCP "tcp"
67
|
68 mike 1.13 PEGASUS_NAMESPACE_BEGIN
69
70 /** The System class defines wrappers for operating system related calls.
71 These are only placed here if they are extremely light. These are
72 usually just direct wrappers which map more or less one to one to the
73 underlying function.
74 */
75 class PEGASUS_COMMON_LINKAGE System
76 {
77 public:
78 /** getCurrentTime - Gets the current time as seconds and milliseconds
79 into the provided variables using system functions.
80 @param seconds Return for the seconds component of the time.
81 @param milliseconds Return for the milliseconds component of the time.
82 @return The value is returned in the parameters.
83 The time returned is as defined in number of seconds and milliseconds
84 since 00:00 Coordinated Universal Time (UTC), January 1, 1970,
|
85 mike 1.14
|
86 mike 1.13 */
87 static void getCurrentTime(Uint32& seconds, Uint32& milliseconds);
88
|
89 jim.wunderlich 1.58 /** Similar to getCurrentTime() above but get microseconds (rather than
|
90 kumpf 1.68 milliseconds).
|
91 jim.wunderlich 1.58 */
92 static void getCurrentTimeUsec(Uint32& seconds, Uint32& microseconds);
93
|
94 mike 1.13 /** getCurrentASCIITime Gets time/date in a fixed format. The format is
95 YY MM DD-HH:MM:SS
|
96 kumpf 1.68 @return Returns String with the ASCII time date.
|
97 mike 1.13 */
98 static String getCurrentASCIITime();
99
100 static void sleep(Uint32 seconds);
101
102 static Boolean exists(const char* path);
103
104 static Boolean canRead(const char* path);
105
106 static Boolean canWrite(const char* path);
107
108 static Boolean getCurrentDirectory(char* path, Uint32 size);
109
110 static Boolean isDirectory(const char* path);
111
112 static Boolean changeDirectory(const char* path);
113
114 static Boolean makeDirectory(const char* path);
115
116 static Boolean getFileSize(const char* path, Uint32& size);
117
118 mike 1.13 static Boolean removeDirectory(const char* path);
119
120 static Boolean removeFile(const char* path);
121
122 static Boolean renameFile(const char* oldPath, const char* newPath);
123
|
124 mike 1.21 static Boolean copyFile(const char* fromPath, const char* toPath);
125
|
126 mike 1.13 static String getHostName();
|
127 kumpf 1.22 static String getFullyQualifiedHostName ();
128 static String getSystemCreationClassName ();
|
129 kumpf 1.17
|
130 dmitry.mikulin 1.76 // The following 2 methods are wrappers around system functions
131 // gethostbyname/gethostbyaddr or gethostbyname_r/gethostbyaddr_r.
132 // In addition to calling corresponding system functions, these
133 // methods introduce re-tries when errno is set to TRY_AGAIN.
134 // Optional parameters are required to cover systems which use '_r'
135 // versions of the system functions.
136 static struct hostent* getHostByName(
137 const char* name,
138 struct hostent* he = 0,
139 char* buf = 0,
140 size_t len = 0);
141 static struct hostent* getHostByAddr(
142 const char *addr,
143 int len,
144 int type,
145 struct hostent* he = 0,
146 char* buf = 0,
147 size_t buflen = 0);
148
|
149 dmitry.mikulin 1.78 #if defined(PEGASUS_OS_ZOS) || \
150 defined(PEGASUS_OS_VMS) || \
151 defined(PEGASUS_ENABLE_IPV6)
152
|
153 dmitry.mikulin 1.76 // The following 2 methods are wrappers around system functions
154 // getaddrinfo/getnameinfo.
155 // In addition to calling corresponding system functions, these
156 // methods introduce re-tries on EAI_AGAIN error returns.
157 static int getAddrInfo(
158 const char *hostname,
159 const char *servname,
160 const struct addrinfo *hints,
161 struct addrinfo **res);
162 static int getNameInfo(
163 const struct sockaddr *sa,
164 size_t salen,
165 char *host,
166 size_t hostlen,
167 char *serv,
168 size_t servlen,
169 int flags);
170
|
171 dmitry.mikulin 1.78 #endif
172
|
173 dave.sudlik 1.71 // Gets IP address assosiated with hostName. af indicates the
174 // type of address (ipv4 or ipv6) returned.
175 static Boolean getHostIP(const String &hostName, int *af, String &hostIP);
176
177 // Gets IP address in binary form. af indicates the type of
178 // address (ipv4 or ipv6) returned. Address will be copied to dst.
179 static Boolean _acquireIP(const char* hostname, int *af, void *dst);
|
180 r.kieninger 1.44
|
181 venkat.puvvada 1.72 /**
182 Returns true if IPv6 stack is active by checking return code from
183 Socket::createSocket() and getSocketError() calls.
184
185 ATTN: We return true if some error other than
186 PEGASUS_INVALID_ADDRESS_FAMILY is returned while creating the socket
187 because we will not be sure whether the IPv6 stack is active or not
188 from the returned error code. Return value of "true" from this method
189 should not be trusted absolutely.
190 */
191 #ifdef PEGASUS_ENABLE_IPV6
192 static Boolean isIPv6StackActive();
193 #endif
194
|
195 kumpf 1.17 static Uint32 lookupPort(
196 const char * serviceName,
197 Uint32 defaultPort);
|
198 mike 1.14
|
199 carolann.graves 1.56 /**
|
200 kumpf 1.68 Attempts to find the given IP address(32bit) on any of the local defined
201 network interfaces
|
202 marek 1.66 */
203 static Boolean isIpOnNetworkInterface(Uint32 inIP);
|
204 kumpf 1.68
|
205 marek 1.66 /**
|
206 kumpf 1.68 Attempts to resolve a given hostname
207 this function possibly can take some as it can request information
208 from the DNS
209
210 @param resolvedNameIP On successful hostname resolution, this output
211 parameter contains the IP address that was determined.
212 @return true if successful, false if not successful.
|
213 marek 1.66 */
|
214 kumpf 1.68 static Boolean resolveHostNameAtDNS(
215 const char* hostname,
216 Uint32* resolvedNameIP);
|
217 marek 1.66
218 /**
|
219 kumpf 1.68 Attempts to resolve a given IP address
220 this function possibly can take some as it can request information
221 from the DNS
222 @param resolvedIP On successful hostname resolution, this output
223 parameter contains the IP address that was determined.
224 @return true if successful, false if not successful.
|
225 marek 1.66 */
226 static Boolean resolveIPAtDNS(Uint32 ip_addr, Uint32 * resolvedIP);
227
|
228 kumpf 1.68 /**
229 Bundling function used to determine if a given hostname or IP address
230 belongs to the local host
231 this function has the potential to take some time as it will possibly
232 use the DNS
|
233 marek 1.66 */
|
234 kumpf 1.68 static Boolean isLocalHost(const String& hostName);
|
235 marek 1.66
|
236 dave.sudlik 1.71 /**
237 Checks binIPAddress represented by address family and returns true
238 if binary representation matches with loopback ip address. binIPAddress
239 must be in host-byte order.
240 */
241 static Boolean isLoopBack(int af, void *binIPAddress);
242
|
243 kumpf 1.24 static String getEffectiveUserName();
|
244 mike 1.14
245 /**
|
246 kumpf 1.68 This function is used to input a password with echo disabled.
247 The function reads up to a newline and returns a password of at most
248 8 characters.
|
249 mike 1.14
|
250 kumpf 1.68 @param prompt String containing the message prompt to be displayed
251 @return password obtained from the user
|
252 mike 1.14 */
253 static String getPassword(const char* prompt);
254
255 /**
|
256 kumpf 1.68 This function is used to encrypt the user's password.
257 The encryption is compatible with Apache's password file (generated
258 using the htpasswd command)
|
259 mike 1.14
|
260 kumpf 1.68 @param password Password to be encrypted.
261 @param salt Two character string chosen from the set [a-zA-Z0-9./].
|
262 mike 1.14
|
263 kumpf 1.68 @return Encrypted password.
|
264 mike 1.14 */
265 static String encryptPassword(const char* password, const char* salt);
266
267 /**
|
268 kumpf 1.68 This function is used to verify whether specified user is a user
269 on the local system.
|
270 mike 1.14
|
271 kumpf 1.68 @param userName User name to be verified.
|
272 mike 1.14
|
273 kumpf 1.68 @return true if the username is valid, else false
|
274 mike 1.14 */
|
275 kumpf 1.28 static Boolean isSystemUser(const char* userName);
|
276 mike 1.14
277 /**
|
278 kumpf 1.68 Checks whether the given user is a privileged user.
|
279 mike 1.14
|
280 kumpf 1.68 @param userName User name to be checked.
281 @return true if the user is a privileged user, else false
|
282 mike 1.14 */
|
283 david.dillard 1.46 static Boolean isPrivilegedUser(const String& userName);
|
284 mike 1.14
|
285 kumpf 1.16 /**
|
286 kumpf 1.68 This function returns the privileged user name on the system.
287 @return the privileged user name
|
288 kumpf 1.19 */
289 static String getPrivilegedUserName();
290
291 /**
|
292 kumpf 1.68 This function is used to verify whether the specified user is a member
293 of the specified user group.
|
294 kumpf 1.39
|
295 kumpf 1.68 @param userName User name to be verified.
296 @param groupName User group name.
|
297 kumpf 1.39
|
298 kumpf 1.68 @return true if the user is a member of the user group, false otherwise.
299 @throw InternalSystemError - If there is an error accessing the
300 specified user or group information.
|
301 kumpf 1.39 */
302 static Boolean isGroupMember(const char* userName, const char* groupName);
303
304 /**
|
305 kumpf 1.60 Gets the user and group IDs associated with the specified user.
306 @param userName User name for which to look up user and group IDs.
307 @param uid User ID for the specified user name.
308 @param gid Group ID for the specified user name.
309 @return True if the user and group IDs were retrieved
310 successfully, false otherwise.
311 */
312 static Boolean lookupUserId(
313 const char* userName,
314 PEGASUS_UID_T& uid,
315 PEGASUS_GID_T& gid);
|
316 kumpf 1.48
|
317 kumpf 1.60 /**
|
318 kumpf 1.69 Changes the process user context to the specified user and group.
|
319 kumpf 1.78.2.1 IMPORTANT: This method is not reentrant and not async signal safe.
320 It should only be called in a single-threaded program.
|
321 kumpf 1.69 @param userName User name to set as the process user context.
|
322 kumpf 1.60 @param uid User ID to set as the process user context.
323 @param gid Group ID to set as the process group context.
324 @return True if the user context is successfully changed,
325 false otherwise.
|
326 kumpf 1.48 */
|
327 kumpf 1.69 static Boolean changeUserContext_SingleThreaded(
328 const char* userName,
|
329 kumpf 1.60 const PEGASUS_UID_T& uid,
330 const PEGASUS_GID_T& gid);
331
|
332 kumpf 1.48 /**
|
333 kumpf 1.68 This function is used to get the process ID of the calling process.
334 @return Process ID
|
335 kumpf 1.16 */
336 static Uint32 getPID();
337
|
338 mike 1.21 static Boolean truncateFile(const char* path, size_t newSize);
|
339 kumpf 1.23
|
340 kumpf 1.29 /** Compare two strings but ignore any case differences.
341 This method is provided only because some platforms lack a strcasecmp
342 function in the standard library.
343 */
344 static Sint32 strcasecmp(const char* s1, const char* s2);
|
345 r.kieninger 1.44
|
346 tony 1.30 /** Return just the file or directory name from the path into basename.
347 This method returns a file or directory name at the end of a path.
348 The path can be relative or absolute. If the path is the root,
|
349 r.kieninger 1.44 then empty string is returned.
|
350 tony 1.30 */
|
351 kumpf 1.31 static char *extract_file_name(const char *fullpath, char *basename);
|
352 tony 1.30
353 /** Return just the pathname into dirname. The fullpath can be relative
354 or absolute. This method returns a path minus the file or
355 directory name at the end of a supplied path (fullpath).
356 If the fullpath is the root, then fullpath is returned.
357 The resulting path will contain a trailing slash unless fullpath is
358 a file or directory name, in which case, just the file or directory
|
359 r.kieninger 1.44 name is returned.
|
360 tony 1.30 */
|
361 kumpf 1.31 static char *extract_file_path(const char *fullpath, char *dirname);
|
362 tony 1.30
363 // Is absolute path?
|
364 kumpf 1.31 static Boolean is_absolute_path(const char *path);
|
365 kumpf 1.36
366 /** Changes file permissions on the given file.
367 @param path path of the file.
|
368 r.kieninger 1.44 @param mode the bit-wise inclusive OR of the values for the desired
|
369 kumpf 1.36 permissions.
370 @return true on success, false on error and errno is set appropriately.
371 */
372 static Boolean changeFilePermissions(const char* path, mode_t mode);
|
373 kumpf 1.29
|
374 kumpf 1.57 /** Checks whether the specified file is a regular file owned by the
375 effective user for the current process.
|
376 kumpf 1.48 @param path Path of the file to check.
377 @return True if the file is owned by the effective user for the
378 current process, false otherwise.
379 */
380 static Boolean verifyFileOwnership(const char* path);
381
|
382 kumpf 1.40 /**
383 Flag indicating whether shared libraries are loaded with the
384 BIND_VERBOSE option.
385
386 THIS FLAG IS USED ON HP-UX ONLY.
387 */
|
388 kumpf 1.23 static Boolean bindVerbose;
|
389 david 1.32
|
390 kumpf 1.53 /**
391 Writes a message to the system log. This method encapsulates the
392 semantics of opening the system log, writing the specified message,
393 and closing the log.
394
395 @param ident An identifier to be prepended to the log messages
396 (typically a program name).
397 @param severity A severity value to be associated with the message.
398 Severity values are defined in Logger.h.
399 @param message A message to be written to the system log.
400 */
401 static void syslog(
402 const String& ident,
403 Uint32 severity,
404 const char* message);
|
405 david 1.32
|
406 marek 1.67 static void openlog(
|
407 kumpf 1.68 const char *ident,
408 int logopt,
|
409 marek 1.67 int facility);
410
411 static void closelog();
412
|
413 david 1.32 // System ID constants for Logger::put and Logger::trace
414 static const String CIMSERVER;
|
415 tony 1.34
416 // System ID constants for Logger::put and Logger::trace
417 static const String CIMLISTENER;
|
418 gs.keenan 1.50
|
419 mike 1.13 };
420
|
421 kumpf 1.63 /**
422 The AutoFileLock class uses an advisory file lock to allow access to a
423 resource to be controlled.
424 */
|
425 kumpf 1.64 class PEGASUS_COMMON_LINKAGE AutoFileLock
|
426 kumpf 1.63 {
427 public:
428
429 AutoFileLock(const char* fileName);
430 ~AutoFileLock();
431
432 private:
433
434 AutoFileLock();
435 AutoFileLock(const AutoFileLock&);
436 AutoFileLock& operator=(const AutoFileLock&);
437
438 #ifdef PEGASUS_OS_TYPE_UNIX
439 struct flock _fl;
440 int _fd;
441 #endif
|
442 mreddy 1.73 #ifdef PEGASUS_OS_TYPE_WINDOWS
443 HANDLE _hFile;
444 #endif
|
445 kumpf 1.63 };
446
|
447 mike 1.13 PEGASUS_NAMESPACE_END
448
449 #endif /* Pegasus_System_h */
|