(file) Return to System.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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 s.kodali 1.78.2.3 #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.78.2.2 #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.78.2.2 #  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.78.2.2 
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                             static Boolean renameFile(const char* oldPath, const char* newPath);
164                         
165 mike           1.21         static Boolean copyFile(const char* fromPath, const char* toPath);
166                         
167 mike           1.13         static String getHostName();
168 kumpf          1.22         static String getFullyQualifiedHostName ();
169                             static String getSystemCreationClassName ();
170 kumpf          1.17     
171 dmitry.mikulin 1.76         // The following 2 methods are wrappers around system functions 
172                             // gethostbyname/gethostbyaddr or gethostbyname_r/gethostbyaddr_r.
173                             // In addition to calling corresponding system functions, these
174                             // methods introduce re-tries when errno is set to TRY_AGAIN.
175                             // Optional parameters are required to cover systems which use '_r'
176                             // versions of the system functions.
177                             static struct hostent* getHostByName(
178                                 const char* name, 
179                                 struct hostent* he = 0, 
180                                 char* buf = 0, 
181                                 size_t len = 0);
182                             static struct hostent* getHostByAddr(
183                                 const char *addr, 
184                                 int len, 
185                                 int type,
186                                 struct hostent* he = 0, 
187                                 char* buf = 0, 
188                                 size_t buflen = 0);
189                         
190 dmitry.mikulin 1.78     #if defined(PEGASUS_OS_ZOS) || \
191                             defined(PEGASUS_OS_VMS) || \
192                             defined(PEGASUS_ENABLE_IPV6)
193                         
194 dmitry.mikulin 1.76         // The following 2 methods are wrappers around system functions 
195                             // getaddrinfo/getnameinfo. 
196                             // In addition to calling corresponding system functions, these
197                             // methods introduce re-tries on EAI_AGAIN error returns.
198                             static int getAddrInfo(
199                                 const char *hostname, 
200                                 const char *servname,
201                                 const struct addrinfo *hints, 
202                                 struct addrinfo **res);
203                             static int getNameInfo(
204                                 const struct sockaddr *sa, 
205                                 size_t salen,
206                                 char *host, 
207                                 size_t hostlen, 
208                                 char *serv, 
209                                 size_t servlen, 
210                                 int flags);
211                         
212 dmitry.mikulin 1.78     #endif
213                         
214 dave.sudlik    1.71         // Gets IP address assosiated with hostName. af indicates the
215                             // type of address (ipv4 or ipv6) returned.
216                             static Boolean getHostIP(const String &hostName, int *af, String &hostIP);
217                         
218                             // Gets IP address in binary form. af indicates the type of
219                             // address (ipv4 or ipv6) returned. Address will be copied to dst.
220                             static Boolean _acquireIP(const char* hostname, int *af, void *dst);
221 r.kieninger    1.44     
222 venkat.puvvada 1.72         /**
223                                 Returns true if IPv6 stack is active by checking return code from
224                                 Socket::createSocket() and getSocketError() calls.
225                         
226                                 ATTN: We return true if some error other than 
227                                 PEGASUS_INVALID_ADDRESS_FAMILY is returned while creating the socket
228                                 because we will not be sure whether the IPv6 stack is active or not
229                                 from the returned error code. Return value of "true" from this method
230                                 should not be trusted absolutely.
231                             */
232                         #ifdef PEGASUS_ENABLE_IPV6
233                             static Boolean isIPv6StackActive();
234                         #endif
235                         
236 s.kodali       1.78.2.3     /**
237                                 Returns all interface addresses. Both ip4 and ip6 interface addresses
238                                 will be returned.
239                             */
240                             static Array<String> getInterfaceAddrs();
241                         
242 kumpf          1.17         static Uint32 lookupPort(
243                                 const char * serviceName,
244                                 Uint32 defaultPort);
245 mike           1.14     
246 carolann.graves 1.56         /**
247 kumpf           1.68             Attempts to find the given IP address(32bit) on any of the local defined
248                                  network interfaces
249 marek           1.66          */
250                              static Boolean isIpOnNetworkInterface(Uint32 inIP);
251 kumpf           1.68     
252 marek           1.66         /**
253 kumpf           1.68             Attempts to resolve a given hostname
254                                  this function possibly can take some as it can request information
255                                  from the DNS
256                          
257                                  @param resolvedNameIP On successful hostname resolution, this output
258                                  parameter contains the IP address that was determined.
259                                  @return true if successful, false if not successful.
260 marek           1.66          */
261 kumpf           1.68         static Boolean resolveHostNameAtDNS(
262                                  const char* hostname,
263                                  Uint32* resolvedNameIP);
264 marek           1.66     
265                              /**
266 kumpf           1.68             Attempts to resolve a given IP address
267                                  this function possibly can take some as it can request information
268                                  from the DNS
269                                  @param resolvedIP 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                              static Boolean resolveIPAtDNS(Uint32 ip_addr, Uint32 * resolvedIP);
274                          
275 kumpf           1.68         /**
276                                  Bundling function used to determine if a given hostname or IP address
277                                  belongs to the local host
278                                  this function has the potential to take some time as it will possibly
279                                  use the DNS
280 marek           1.66          */
281 kumpf           1.68         static Boolean isLocalHost(const String& hostName);
282 marek           1.66     
283 dave.sudlik     1.71         /**
284                                  Checks binIPAddress represented by address family and returns true
285                                  if binary representation matches with loopback ip address. binIPAddress
286                                  must be in host-byte order.
287                              */
288                              static Boolean isLoopBack(int af, void *binIPAddress);
289                          
290 kumpf           1.24         static String getEffectiveUserName();
291 mike            1.14     
292                              /**
293 kumpf           1.68             This function is used to input a password with echo disabled.
294                                  The function reads up to a newline and returns a password of at most
295                                  8 characters.
296 mike            1.14     
297 kumpf           1.68             @param prompt String containing the message prompt to be displayed
298                                  @return password obtained from the user
299 mike            1.14         */
300                              static String getPassword(const char* prompt);
301                          
302                              /**
303 kumpf           1.68             This function is used to encrypt the user's password.
304                                  The encryption is compatible with Apache's password file (generated
305                                  using the htpasswd command)
306 mike            1.14     
307 kumpf           1.68             @param password Password to be encrypted.
308                                  @param salt Two character string chosen from the set [a-zA-Z0-9./].
309 mike            1.14     
310 kumpf           1.68             @return Encrypted password.
311 mike            1.14         */
312                              static String encryptPassword(const char* password, const char* salt);
313                          
314                              /**
315 kumpf           1.68             This function is used to verify whether specified user is a user
316                                  on the local system.
317 mike            1.14     
318 kumpf           1.68             @param userName User name to be verified.
319 mike            1.14     
320 kumpf           1.68             @return true if the username is valid, else false
321 mike            1.14         */
322 kumpf           1.28         static Boolean isSystemUser(const char* userName);
323 mike            1.14     
324                              /**
325 kumpf           1.68             Checks whether the given user is a privileged user.
326 mike            1.14     
327 kumpf           1.68             @param userName User name to be checked.
328                                  @return true if the user is a privileged user, else false
329 mike            1.14         */
330 david.dillard   1.46         static Boolean isPrivilegedUser(const String& userName);
331 mike            1.14     
332 kumpf           1.16         /**
333 kumpf           1.68             This function returns the privileged user name on the system.
334                                  @return the privileged user name
335 kumpf           1.19         */
336                              static String getPrivilegedUserName();
337                          
338                              /**
339 kumpf           1.68             This function is used to verify whether the specified user is a member
340                                  of the specified user group.
341 kumpf           1.39     
342 kumpf           1.68             @param userName User name to be verified.
343                                  @param groupName User group name.
344 kumpf           1.39     
345 kumpf           1.68             @return true if the user is a member of the user group, false otherwise.
346                                  @throw InternalSystemError - If there is an error accessing the
347                                  specified user or group information.
348 kumpf           1.39         */
349                              static Boolean isGroupMember(const char* userName, const char* groupName);
350                          
351                              /**
352 kumpf           1.60             Gets the user and group IDs associated with the specified user.
353                                  @param userName  User name for which to look up user and group IDs.
354                                  @param uid       User ID for the specified user name.
355                                  @param gid       Group ID for the specified user name.
356                                  @return          True if the user and group IDs were retrieved
357                                                   successfully, false otherwise.
358                              */
359                              static Boolean lookupUserId(
360                                  const char* userName,
361                                  PEGASUS_UID_T& uid,
362                                  PEGASUS_GID_T& gid);
363 kumpf           1.48     
364 kumpf           1.60         /**
365 kumpf           1.69             Changes the process user context to the specified user and group.
366 kumpf           1.78.2.1         IMPORTANT:  This method is not reentrant and not async signal safe.
367                                  It should only be called in a single-threaded program.
368 kumpf           1.69             @param userName  User name to set as the process user context.
369 kumpf           1.60             @param uid       User ID to set as the process user context.
370                                  @param gid       Group ID to set as the process group context.
371                                  @return          True if the user context is successfully changed,
372                                                   false otherwise.
373 kumpf           1.48         */
374 kumpf           1.69         static Boolean changeUserContext_SingleThreaded(
375                                  const char* userName,
376 kumpf           1.60             const PEGASUS_UID_T& uid,
377                                  const PEGASUS_GID_T& gid);
378                          
379 kumpf           1.48         /**
380 kumpf           1.68             This function is used to get the process ID of the calling process.
381                                  @return Process ID
382 kumpf           1.16         */
383                              static Uint32 getPID();
384                          
385 mike            1.21         static Boolean truncateFile(const char* path, size_t newSize);
386 kumpf           1.23     
387 kumpf           1.29         /** Compare two strings but ignore any case differences.
388                                  This method is provided only because some platforms lack a strcasecmp
389                                  function in the standard library.
390                              */
391                              static Sint32 strcasecmp(const char* s1, const char* s2);
392 r.kieninger     1.44     
393 tony            1.30         /** Return just the file or directory name from the path into basename.
394                                  This method returns a file or directory name at the end of a path.
395                                  The path can be relative or absolute. If the path is the root,
396 r.kieninger     1.44             then empty string is returned.
397 tony            1.30         */
398 kumpf           1.31         static char *extract_file_name(const char *fullpath, char *basename);
399 tony            1.30     
400                              /** Return just the pathname into dirname. The fullpath can be relative
401                                  or absolute. This method returns a path minus the file or
402                                  directory name at the end of a supplied path (fullpath).
403                                  If the fullpath is the root, then fullpath is returned.
404                                  The resulting path will contain a trailing slash unless fullpath is
405                                  a file or directory name, in which case, just the file or directory
406 r.kieninger     1.44             name is returned.
407 tony            1.30         */
408 kumpf           1.31         static char *extract_file_path(const char *fullpath, char *dirname);
409 tony            1.30     
410                              // Is absolute path?
411 kumpf           1.31         static Boolean is_absolute_path(const char *path);
412 kumpf           1.36     
413                              /** Changes file permissions on the given file.
414                                  @param path path of the file.
415 r.kieninger     1.44             @param mode the bit-wise inclusive OR of the values for the desired
416 kumpf           1.36             permissions.
417                                  @return true on success, false on error and errno is set appropriately.
418                              */
419                              static Boolean changeFilePermissions(const char* path, mode_t mode);
420 kumpf           1.29     
421 kumpf           1.57         /** Checks whether the specified file is a regular file owned by the
422                                  effective user for the current process.
423 kumpf           1.48             @param path Path of the file to check.
424                                  @return True if the file is owned by the effective user for the
425                                  current process, false otherwise.
426                              */
427                              static Boolean verifyFileOwnership(const char* path);
428                          
429 kumpf           1.40         /**
430                                  Flag indicating whether shared libraries are loaded with the
431                                  BIND_VERBOSE option.
432                          
433                                  THIS FLAG IS USED ON HP-UX ONLY.
434                               */
435 kumpf           1.23         static Boolean bindVerbose;
436 david           1.32     
437 kumpf           1.53         /**
438                                  Writes a message to the system log.  This method encapsulates the
439                                  semantics of opening the system log, writing the specified message,
440                                  and closing the log.
441                          
442                                  @param ident An identifier to be prepended to the log messages
443                                  (typically a program name).
444                                  @param severity A severity value to be associated with the message.
445                                  Severity values are defined in Logger.h.
446                                  @param message A message to be written to the system log.
447                              */
448                              static void syslog(
449                                  const String& ident,
450                                  Uint32 severity,
451                                  const char* message);
452 david           1.32     
453 marek           1.67         static void openlog(
454 kumpf           1.68             const char *ident,
455                                  int logopt,
456 marek           1.67             int facility);
457                          
458                              static void closelog();
459                          
460 david           1.32         // System ID constants for Logger::put and Logger::trace
461                              static const String CIMSERVER;
462 tony            1.34     
463                              // System ID constants for Logger::put and Logger::trace
464                              static const String CIMLISTENER;
465 gs.keenan       1.50     
466 mike            1.13     };
467                          
468 kumpf           1.63     /**
469                              The AutoFileLock class uses an advisory file lock to allow access to a
470                              resource to be controlled.
471                          */
472 kumpf           1.64     class PEGASUS_COMMON_LINKAGE AutoFileLock
473 kumpf           1.63     {
474                          public:
475                          
476                              AutoFileLock(const char* fileName);
477                              ~AutoFileLock();
478                          
479                          private:
480                          
481                              AutoFileLock();
482                              AutoFileLock(const AutoFileLock&);
483                              AutoFileLock& operator=(const AutoFileLock&);
484                          
485                          #ifdef PEGASUS_OS_TYPE_UNIX
486                              struct flock _fl;
487                              int _fd;
488                          #endif
489 mreddy          1.73     #ifdef PEGASUS_OS_TYPE_WINDOWS
490                              HANDLE _hFile;
491                          #endif
492 kumpf           1.63     };
493                          
494 mike            1.13     PEGASUS_NAMESPACE_END
495                          
496                          #endif /* Pegasus_System_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2