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