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