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 dl.meetei 1.93 #include <Pegasus/Common/Pegasus_inl.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 kumpf 1.86 /* 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 thilo.boehm 1.82 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 venkat.puvvada 1.90 /** Similar to getCurrentTime() above but get the full time in microseconds
136 */
137 static Uint64 getCurrentTimeUsec();
138
|
139 mike 1.13 /** getCurrentASCIITime Gets time/date in a fixed format. The format is
140 YY MM DD-HH:MM:SS
|
141 kumpf 1.68 @return Returns String with the ASCII time date.
|
142 mike 1.13 */
143 static String getCurrentASCIITime();
144
145 static void sleep(Uint32 seconds);
146
147 static Boolean exists(const char* path);
148
149 static Boolean canRead(const char* path);
150
151 static Boolean canWrite(const char* path);
152
153 static Boolean getCurrentDirectory(char* path, Uint32 size);
154
155 static Boolean isDirectory(const char* path);
156
157 static Boolean changeDirectory(const char* path);
158
159 static Boolean makeDirectory(const char* path);
160
161 static Boolean getFileSize(const char* path, Uint32& size);
162
163 mike 1.13 static Boolean removeDirectory(const char* path);
164
165 static Boolean removeFile(const char* path);
166
|
167 kumpf 1.83 /**
168 Renames a file. If the new name refers to an existing file, it is
169 removed and replaced with the renamed file. The rename operation is
170 performed atomically.
171 @param oldPath A character string containing the name of the file to
172 rename.
173 @param newPath A character string containing the name to which to
174 rename the file.
175 @return A Boolean indicating whether the rename operation was
176 successful.
177 */
|
178 mike 1.13 static Boolean renameFile(const char* oldPath, const char* newPath);
179
|
180 mike 1.21 static Boolean copyFile(const char* fromPath, const char* toPath);
181
|
182 mike 1.13 static String getHostName();
|
183 kumpf 1.22 static String getFullyQualifiedHostName ();
184 static String getSystemCreationClassName ();
|
185 kumpf 1.17
|
186 kumpf 1.86 // The following 2 methods are wrappers around system functions
|
187 dmitry.mikulin 1.76 // gethostbyname/gethostbyaddr or gethostbyname_r/gethostbyaddr_r.
188 // In addition to calling corresponding system functions, these
189 // methods introduce re-tries when errno is set to TRY_AGAIN.
190 // Optional parameters are required to cover systems which use '_r'
191 // versions of the system functions.
192 static struct hostent* getHostByName(
|
193 kumpf 1.86 const char* name,
194 struct hostent* he = 0,
195 char* buf = 0,
|
196 dmitry.mikulin 1.76 size_t len = 0);
197 static struct hostent* getHostByAddr(
|
198 kumpf 1.86 const char *addr,
199 int len,
|
200 dmitry.mikulin 1.76 int type,
|
201 kumpf 1.86 struct hostent* he = 0,
202 char* buf = 0,
|
203 dmitry.mikulin 1.76 size_t buflen = 0);
204
|
205 dmitry.mikulin 1.78
|
206 kumpf 1.86 // The following 2 methods are wrappers around system functions
207 // getaddrinfo/getnameinfo.
|
208 dmitry.mikulin 1.76 // In addition to calling corresponding system functions, these
209 // methods introduce re-tries on EAI_AGAIN error returns.
|
210 dev.meetei 1.92 //
211 // Callers are expected to call freeaddrinfo when using System::getAddrInfo
|
212 dmitry.mikulin 1.76 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 dave.sudlik 1.71 // Gets IP address assosiated with hostName. af indicates the
227 // type of address (ipv4 or ipv6) returned.
228 static Boolean getHostIP(const String &hostName, int *af, String &hostIP);
229
230 // Gets IP address in binary form. af indicates the type of
231 // address (ipv4 or ipv6) returned. Address will be copied to dst.
|
232 marek 1.79 static Boolean acquireIP(const char* hostname, int *af, void *dst);
|
233 r.kieninger 1.44
|
234 venkat.puvvada 1.72 /**
235 Returns true if IPv6 stack is active by checking return code from
236 Socket::createSocket() and getSocketError() calls.
237
|
238 kumpf 1.86 ATTN: We return true if some error other than
|
239 venkat.puvvada 1.72 PEGASUS_INVALID_ADDRESS_FAMILY is returned while creating the socket
240 because we will not be sure whether the IPv6 stack is active or not
241 from the returned error code. Return value of "true" from this method
242 should not be trusted absolutely.
243 */
244 #ifdef PEGASUS_ENABLE_IPV6
245 static Boolean isIPv6StackActive();
246 #endif
247
|
248 venkat.puvvada 1.80 /**
249 Returns all interface addresses. Both ip4 and ip6 interface addresses
250 will be returned.
251 */
252 static Array<String> getInterfaceAddrs();
253
|
254 kumpf 1.17 static Uint32 lookupPort(
255 const char * serviceName,
256 Uint32 defaultPort);
|
257 mike 1.14
|
258 carolann.graves 1.56 /**
|
259 kumpf 1.68 Attempts to find the given IP address(32bit) on any of the local defined
260 network interfaces
|
261 marek 1.66 */
262 static Boolean isIpOnNetworkInterface(Uint32 inIP);
|
263 kumpf 1.68
|
264 marek 1.66 /**
|
265 kumpf 1.68 Attempts to resolve a given hostname
266 this function possibly can take some as it can request information
267 from the DNS
268
269 @param resolvedNameIP On successful hostname resolution, this output
270 parameter contains the IP address that was determined.
271 @return true if successful, false if not successful.
|
272 marek 1.66 */
|
273 kumpf 1.68 static Boolean resolveHostNameAtDNS(
274 const char* hostname,
275 Uint32* resolvedNameIP);
|
276 marek 1.66
277 /**
|
278 kumpf 1.68 Attempts to resolve a given IP address
279 this function possibly can take some as it can request information
280 from the DNS
281 @param resolvedIP On successful hostname resolution, this output
282 parameter contains the IP address that was determined.
283 @return true if successful, false if not successful.
|
284 marek 1.66 */
285 static Boolean resolveIPAtDNS(Uint32 ip_addr, Uint32 * resolvedIP);
286
|
287 kumpf 1.68 /**
288 Bundling function used to determine if a given hostname or IP address
289 belongs to the local host
290 this function has the potential to take some time as it will possibly
291 use the DNS
|
292 marek 1.66 */
|
293 kumpf 1.68 static Boolean isLocalHost(const String& hostName);
|
294 marek 1.66
|
295 dave.sudlik 1.71 /**
296 Checks binIPAddress represented by address family and returns true
297 if binary representation matches with loopback ip address. binIPAddress
298 must be in host-byte order.
299 */
300 static Boolean isLoopBack(int af, void *binIPAddress);
301
|
302 kumpf 1.24 static String getEffectiveUserName();
|
303 mike 1.14
304 /**
|
305 kumpf 1.68 This function is used to input a password with echo disabled.
306 The function reads up to a newline and returns a password of at most
307 8 characters.
|
308 mike 1.14
|
309 kumpf 1.68 @param prompt String containing the message prompt to be displayed
310 @return password obtained from the user
|
311 mike 1.14 */
312 static String getPassword(const char* prompt);
313
314 /**
|
315 kumpf 1.68 This function is used to encrypt the user's password.
316 The encryption is compatible with Apache's password file (generated
317 using the htpasswd command)
|
318 mike 1.14
|
319 kumpf 1.68 @param password Password to be encrypted.
320 @param salt Two character string chosen from the set [a-zA-Z0-9./].
|
321 mike 1.14
|
322 kumpf 1.68 @return Encrypted password.
|
323 mike 1.14 */
324 static String encryptPassword(const char* password, const char* salt);
325
326 /**
|
327 kumpf 1.68 This function is used to verify whether specified user is a user
328 on the local system.
|
329 mike 1.14
|
330 kumpf 1.68 @param userName User name to be verified.
|
331 mike 1.14
|
332 kumpf 1.68 @return true if the username is valid, else false
|
333 mike 1.14 */
|
334 kumpf 1.28 static Boolean isSystemUser(const char* userName);
|
335 mike 1.14
336 /**
|
337 kumpf 1.68 Checks whether the given user is a privileged user.
|
338 mike 1.14
|
339 kumpf 1.68 @param userName User name to be checked.
340 @return true if the user is a privileged user, else false
|
341 mike 1.14 */
|
342 david.dillard 1.46 static Boolean isPrivilegedUser(const String& userName);
|
343 mike 1.14
|
344 kumpf 1.16 /**
|
345 kumpf 1.68 This function returns the privileged user name on the system.
346 @return the privileged user name
|
347 kumpf 1.19 */
348 static String getPrivilegedUserName();
349
350 /**
|
351 kumpf 1.68 This function is used to verify whether the specified user is a member
352 of the specified user group.
|
353 kumpf 1.39
|
354 kumpf 1.68 @param userName User name to be verified.
355 @param groupName User group name.
|
356 kumpf 1.39
|
357 kumpf 1.68 @return true if the user is a member of the user group, false otherwise.
358 @throw InternalSystemError - If there is an error accessing the
359 specified user or group information.
|
360 kumpf 1.39 */
361 static Boolean isGroupMember(const char* userName, const char* groupName);
362
363 /**
|
364 kumpf 1.60 Gets the user and group IDs associated with the specified user.
365 @param userName User name for which to look up user and group IDs.
366 @param uid User ID for the specified user name.
367 @param gid Group ID for the specified user name.
368 @return True if the user and group IDs were retrieved
369 successfully, false otherwise.
370 */
371 static Boolean lookupUserId(
372 const char* userName,
373 PEGASUS_UID_T& uid,
374 PEGASUS_GID_T& gid);
|
375 kumpf 1.48
|
376 kumpf 1.60 /**
|
377 kumpf 1.69 Changes the process user context to the specified user and group.
|
378 kumpf 1.81 IMPORTANT: This method is not reentrant and not async signal safe.
379 It should only be called in a single-threaded program.
|
380 kumpf 1.69 @param userName User name to set as the process user context.
|
381 kumpf 1.60 @param uid User ID to set as the process user context.
382 @param gid Group ID to set as the process group context.
383 @return True if the user context is successfully changed,
384 false otherwise.
|
385 kumpf 1.48 */
|
386 kumpf 1.69 static Boolean changeUserContext_SingleThreaded(
387 const char* userName,
|
388 kumpf 1.60 const PEGASUS_UID_T& uid,
389 const PEGASUS_GID_T& gid);
390
|
391 kumpf 1.48 /**
|
392 kumpf 1.68 This function is used to get the process ID of the calling process.
393 @return Process ID
|
394 kumpf 1.16 */
395 static Uint32 getPID();
396
|
397 mike 1.21 static Boolean truncateFile(const char* path, size_t newSize);
|
398 kumpf 1.23
|
399 kumpf 1.29 /** Compare two strings but ignore any case differences.
|
400 thilo.boehm 1.87 This method is provided only because some platforms lack a fast enough
401 strcasecmp function in the standard library.
|
402 kumpf 1.29 */
403 static Sint32 strcasecmp(const char* s1, const char* s2);
|
404 r.kieninger 1.44
|
405 thilo.boehm 1.87 /** Compare two strings for equality but ignore any case differences.
406 This method should only be used if length of both strings is
407 known already. If not, please use System::strcasecmp()
408 */
409 static bool strncasecmp(
410 const char* s1,
411 size_t s1_l,
412 const char* s2,
413 size_t s2_l);
414
|
415 tony 1.30 /** Return just the file or directory name from the path into basename.
416 This method returns a file or directory name at the end of a path.
417 The path can be relative or absolute. If the path is the root,
|
418 r.kieninger 1.44 then empty string is returned.
|
419 tony 1.30 */
|
420 kumpf 1.31 static char *extract_file_name(const char *fullpath, char *basename);
|
421 tony 1.30
422 /** Return just the pathname into dirname. The fullpath can be relative
423 or absolute. This method returns a path minus the file or
424 directory name at the end of a supplied path (fullpath).
425 If the fullpath is the root, then fullpath is returned.
426 The resulting path will contain a trailing slash unless fullpath is
427 a file or directory name, in which case, just the file or directory
|
428 r.kieninger 1.44 name is returned.
|
429 tony 1.30 */
|
430 kumpf 1.31 static char *extract_file_path(const char *fullpath, char *dirname);
|
431 tony 1.30
432 // Is absolute path?
|
433 kumpf 1.31 static Boolean is_absolute_path(const char *path);
|
434 kumpf 1.36
435 /** Changes file permissions on the given file.
436 @param path path of the file.
|
437 r.kieninger 1.44 @param mode the bit-wise inclusive OR of the values for the desired
|
438 kumpf 1.36 permissions.
439 @return true on success, false on error and errno is set appropriately.
440 */
441 static Boolean changeFilePermissions(const char* path, mode_t mode);
|
442 kumpf 1.29
|
443 kumpf 1.57 /** Checks whether the specified file is a regular file owned by the
444 effective user for the current process.
|
445 kumpf 1.48 @param path Path of the file to check.
446 @return True if the file is owned by the effective user for the
447 current process, false otherwise.
448 */
449 static Boolean verifyFileOwnership(const char* path);
450
|
451 kumpf 1.40 /**
452 Flag indicating whether shared libraries are loaded with the
453 BIND_VERBOSE option.
454
455 THIS FLAG IS USED ON HP-UX ONLY.
456 */
|
457 kumpf 1.23 static Boolean bindVerbose;
|
458 david 1.32
|
459 kumpf 1.53 /**
460 Writes a message to the system log. This method encapsulates the
461 semantics of opening the system log, writing the specified message,
462 and closing the log.
463
464 @param ident An identifier to be prepended to the log messages
465 (typically a program name).
466 @param severity A severity value to be associated with the message.
467 Severity values are defined in Logger.h.
468 @param message A message to be written to the system log.
469 */
470 static void syslog(
471 const String& ident,
472 Uint32 severity,
473 const char* message);
|
474 david 1.32
|
475 marek 1.67 static void openlog(
|
476 kumpf 1.68 const char *ident,
477 int logopt,
|
478 marek 1.67 int facility);
479
480 static void closelog();
481
|
482 marek 1.91 /** Function to set hostname and thus override system supplied value */
483 static void setHostName(const String & hostName);
484
485 /** Function to set fully qualified hostname and thus override system
486 supplied value */
487 static void setFullyQualifiedHostName(const String & fullHostName);
488
|
489 david 1.32 // System ID constants for Logger::put and Logger::trace
490 static const String CIMSERVER;
|
491 tony 1.34
492 // System ID constants for Logger::put and Logger::trace
493 static const String CIMLISTENER;
|
494 gs.keenan 1.50
|
495 sahana.prabhakar 1.88 // mutex used for synchronising threads calling getHostName.
|
496 marek 1.91 static Mutex _mutexForGetHostName;
|
497 sahana.prabhakar 1.88
498 // mutex used for synchronising threads calling getFullyQualifiedHostName
|
499 marek 1.91 static Mutex _mutexForGetFQHN;
|
500 sahana.prabhakar 1.88
|
501 marek 1.91 private:
502 // Strings caching hostnames to avoid repeated resolver lookups and to
503 // enable setting by configuration
504 static String _hostname;
505 static String _fullyQualifiedHostname;
|
506 mike 1.13 };
507
|
508 kumpf 1.63 /**
509 The AutoFileLock class uses an advisory file lock to allow access to a
510 resource to be controlled.
511 */
|
512 kumpf 1.64 class PEGASUS_COMMON_LINKAGE AutoFileLock
|
513 kumpf 1.63 {
514 public:
515
516 AutoFileLock(const char* fileName);
517 ~AutoFileLock();
518
519 private:
520
521 AutoFileLock();
522 AutoFileLock(const AutoFileLock&);
523 AutoFileLock& operator=(const AutoFileLock&);
524
525 #ifdef PEGASUS_OS_TYPE_UNIX
526 struct flock _fl;
527 int _fd;
528 #endif
|
529 mreddy 1.73 #ifdef PEGASUS_OS_TYPE_WINDOWS
530 HANDLE _hFile;
531 #endif
|
532 kumpf 1.63 };
533
|
534 mike 1.13 PEGASUS_NAMESPACE_END
535
536 #endif /* Pegasus_System_h */
|