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