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

  1 mike  1.19 //%/////////////////////////////////////////////////////////////////////////////
  2            //
  3 kumpf 1.39 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
  4            // The Open Group, Tivoli Systems
  5 mike  1.19 //
  6            // Permission is hereby granted, free of charge, to any person obtaining a copy
  7 mike  1.21 // of this software and associated documentation files (the "Software"), to
  8            // deal in the Software without restriction, including without limitation the
  9            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10 mike  1.19 // sell copies of the Software, and to permit persons to whom the Software is
 11            // furnished to do so, subject to the following conditions:
 12 kumpf 1.39 // 
 13 mike  1.21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14 mike  1.19 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16 mike  1.21 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19 mike  1.19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21            //
 22            //==============================================================================
 23            //
 24            // Author: Mike Brasher (mbrasher@bmc.com)
 25            //
 26 mike  1.21 // Modified By: Ben Heilbronn (ben_heilbronn@hp.com)
 27            //              Sushma Fernandes (sushma_fernandes@hp.com)
 28            //              Nag Boranna (nagaraja_boranna@hp.com)
 29 mike  1.19 //
 30            //%/////////////////////////////////////////////////////////////////////////////
 31            
 32            #ifdef PEGASUS_OS_HPUX
 33            # include <dl.h>
 34 mike  1.21 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 35            # include <dll.h>
 36 chuck 1.44 #elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
 37            #  include <fcntl.h> 
 38            #  include <mih/rslvsp.h>            /* rslvsp()                       */
 39            #  include <mih/micommon.h>          /* _AUTH_EXECUTE                  */
 40            #  include <mih/miobjtyp.h>          /* WLI_SRVPGM                     */
 41            #  include <pointer.h>               /* _SYSPTR                        */
 42            #  include <qusec.h>                 /* Qus_EC_t                       */
 43            #  include <qleawi.h>                /* QleActBndPgm(),QleGetExp()     */
 44            #  include <unistd.cleinc>
 45 mike  1.19 #else
 46            # include <dlfcn.h>
 47            #endif
 48            
 49 mike  1.21 #include <unistd.h>
 50 mike  1.19 #include <dirent.h>
 51 mike  1.21 #include <pwd.h>
 52 sage  1.23 
 53 chuck 1.44 #if !defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) && !defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) 
 54 kumpf 1.24 #include <crypt.h> 
 55 sage  1.23 #endif
 56            
 57 mike  1.19 #include <sys/stat.h>
 58            #include <sys/types.h>
 59            #include <cstdio>
 60            #include <time.h>
 61 kumpf 1.41 #include <sys/time.h>
 62 kumpf 1.24 #include <netdb.h>
 63 kumpf 1.41 #include "System.h"
 64 mike  1.21 #include <Pegasus/Common/Tracer.h>
 65 kumpf 1.25 #include <Pegasus/Common/Destroyer.h>
 66 kumpf 1.42 #include <Pegasus/Common/InternalException.h>
 67 mike  1.21 
 68 mike  1.19 PEGASUS_NAMESPACE_BEGIN
 69            
 70 kumpf 1.35 #if defined(PEGASUS_OS_HPUX)
 71            Boolean System::bindVerbose = false;
 72            #endif
 73            
 74 chuck 1.44 #if defined(PEGASUS_OS_OS400)
 75            char os400ExceptionID[8] = {0};
 76            #endif
 77            
 78 mike  1.19 inline void sleep_wrapper(Uint32 seconds)
 79            {
 80                sleep(seconds);
 81            }
 82            
 83            void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 84            {
 85                timeval tv;
 86                gettimeofday(&tv, 0);
 87 kumpf 1.33     seconds = Uint32(tv.tv_sec);
 88                milliseconds = Uint32(tv.tv_usec) / 1000;
 89 mike  1.19 }
 90            
 91            String System::getCurrentASCIITime()
 92            {
 93                char    str[50];
 94                time_t  rawTime;
 95            
 96                time(&rawTime);
 97 kumpf 1.32     strftime(str, 40,"%m/%d/%Y-%T", localtime(&rawTime));
 98 mike  1.19     String time = str;
 99                return time;
100            }
101            
102            void System::sleep(Uint32 seconds)
103            {
104                sleep_wrapper(seconds);
105            }
106            
107            Boolean System::exists(const char* path)
108            {
109                return access(path, F_OK) == 0;
110            }
111            
112            Boolean System::canRead(const char* path)
113            {
114                return access(path, R_OK) == 0;
115            }
116            
117            Boolean System::canWrite(const char* path)
118            {
119 mike  1.19     return access(path, W_OK) == 0;
120            }
121            
122            Boolean System::getCurrentDirectory(char* path, Uint32 size)
123            {
124                return getcwd(path, size) != NULL;
125            }
126            
127            Boolean System::isDirectory(const char* path)
128            {
129                struct stat st;
130            
131                if (stat(path, &st) != 0)
132 mike  1.21         return false;
133 mike  1.19 
134                return S_ISDIR(st.st_mode);
135            }
136            
137            Boolean System::changeDirectory(const char* path)
138            {
139                return chdir(path) == 0;
140            }
141            
142            Boolean System::makeDirectory(const char* path)
143            {
144                return mkdir(path, 0777) == 0;
145            }
146            
147            Boolean System::getFileSize(const char* path, Uint32& size)
148            {
149                struct stat st;
150            
151                if (stat(path, &st) != 0)
152 mike  1.21         return false;
153 mike  1.19 
154                size = st.st_size;
155                return true;
156            }
157            
158            Boolean System::removeDirectory(const char* path)
159            {
160 mike  1.21     return rmdir(path) == 0;
161 mike  1.19 }
162            
163            Boolean System::removeFile(const char* path)
164            {
165 mike  1.21     return unlink(path) == 0;
166 mike  1.19 }
167            
168            Boolean System::renameFile(const char* oldPath, const char* newPath)
169            {
170                if (link(oldPath, newPath) != 0)
171 mike  1.21         return false;
172 mike  1.19 
173                return unlink(oldPath) == 0;
174            }
175            
176            DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
177            {
178 kumpf 1.40     PEG_METHOD_ENTER(TRC_OS_ABSTRACTION, "System::loadDynamicLibrary()");
179 mike  1.19 
180 mike  1.21     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
181                              "Attempting to load library %s", fileName);
182 mike  1.19 
183 mike  1.21 #if defined(PEGASUS_OS_HPUX)
184 kumpf 1.35     void* handle;
185                if (bindVerbose)
186                {
187                    handle = shl_load(fileName, 
188                                 BIND_IMMEDIATE | DYNAMIC_PATH | BIND_VERBOSE, 0L);
189                }
190                else 
191                {
192                    handle = shl_load(fileName, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);
193                }
194 mike  1.21     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
195 kumpf 1.34                   "After loading lib %s, error code is %d", fileName, 
196                              (handle == (void *)0)?errno:0);
197 mike  1.21 
198 kumpf 1.40     PEG_METHOD_EXIT();
199 mike  1.19     return DynamicLibraryHandle(handle);
200 mike  1.21 #elif defined(PEGASUS_OS_TRU64)
201 kumpf 1.40     PEG_METHOD_EXIT();
202 mike  1.21     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW));
203            #elif defined(PEGASUS_OS_ZOS)
204 kumpf 1.40     PEG_METHOD_EXIT();
205 mike  1.21     return DynamicLibraryHandle(dllload(fileName));
206 chuck 1.44 #elif defined(PEGASUS_OS_OS400)
207                // Activate the service program.	
208            
209                // Parse out the library and srvpgm names.
210                // Note: the fileName passed in must be in OS/400 form - library/srvpgm
211                if (fileName == NULL || strlen(fileName) == 0 || strlen(fileName) >= 200)
212                   return 0;
213            
214 chuck 1.48     // More checking here!
215 chuck 1.44     char name[200];
216                strcpy(name, fileName);
217            
218                char* lib = strtok(name, "/");
219                if (lib == NULL || strlen(lib) == 0)
220                   return 0;
221            
222 chuck 1.48     char* srvpgm = strtok(NULL,"/");              
223 chuck 1.44     if (srvpgm == NULL || strlen(srvpgm) == 0)
224                   return 0;
225            
226                /*----------------------------------------------------------------*/
227                /* Resolve to the service program                                 */
228                /*----------------------------------------------------------------*/
229                _OBJ_TYPE_T objectType = WLI_SRVPGM;
230                _SYSPTR sysP = rslvsp(objectType, srvpgm, lib, _AUTH_NONE);
231            
232                /*----------------------------------------------------------------*/
233                /* Activate the service program                                   */
234                /*----------------------------------------------------------------*/
235                Qle_ABP_Info_t activationInfo;
236                int actInfoLen = sizeof(activationInfo);
237                int hdl;
238            
239                Qus_EC_t os400ErrorCode = {0};
240                os400ErrorCode.Bytes_Provided = sizeof(Qus_EC_t);
241                os400ErrorCode.Bytes_Available = 0;
242            
243                QleActBndPgm(&sysP,
244 chuck 1.44 		&hdl,
245            		&activationInfo,
246            		&actInfoLen,
247            		&os400ErrorCode);
248            
249                if (os400ErrorCode.Bytes_Available)
250                {
251                   // Got an error. 
252                   memset(os400ExceptionID, '\0', 8);
253                   strncpy(os400ExceptionID, os400ErrorCode.Exception_Id, 7);
254                   Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
255                              "Error activating service program. Exception Id = %s", os400ExceptionID);
256                   return 0;
257                }
258               
259                PEG_METHOD_EXIT();
260                return DynamicLibraryHandle(hdl);
261            
262 mike  1.19 #else
263 kumpf 1.40     PEG_METHOD_EXIT();
264 mike  1.21     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL));
265            #endif
266 mike  1.20 
267 mike  1.21 }
268 mike  1.20 
269 mike  1.21 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
270            {
271                // ATTN: Should this method indicate success/failure?
272            #ifdef PEGASUS_OS_LINUX
273                dlclose(libraryHandle);
274            #endif
275            
276            #ifdef PEGASUS_OS_HPUX
277                // Note: shl_unload will unload the library even if it has been loaded
278                // multiple times.  No reference count is kept.
279 kumpf 1.31     int ignored = shl_unload(reinterpret_cast<shl_t>(libraryHandle));
280 mike  1.19 #endif
281            }
282            
283            String System::dynamicLoadError() {
284 mike  1.21     // ATTN: Is this safe in a multi-threaded process?  Should this string
285                // be returned from loadDynamicLibrary?
286 mike  1.19 #ifdef PEGASUS_OS_HPUX
287 mike  1.21     // ATTN: If shl_load() returns NULL, this value should be strerror(errno)
288                return String();
289            #elif defined(PEGASUS_OS_ZOS)
290 mike  1.19     return String();
291 chuck 1.44 #elif defined(PEGASUS_OS_OS400)
292                return String(os400ExceptionID);
293 mike  1.19 #else
294                String dlerr = dlerror();
295                return dlerr;
296            #endif
297            }
298            
299            
300            DynamicSymbolHandle System::loadDynamicSymbol(
301                DynamicLibraryHandle libraryHandle,
302                const char* symbolName)
303            {
304            #ifdef PEGASUS_OS_HPUX
305                char* p = (char*)symbolName;
306                void* proc = 0;
307            
308 kumpf 1.31     if (shl_findsym((shl_t*)&libraryHandle, symbolName, TYPE_UNDEFINED,
309                                &proc) == 0)
310                {
311 mike  1.21         return DynamicSymbolHandle(proc);
312 kumpf 1.31     }
313 mike  1.19 
314 kumpf 1.47     if (shl_findsym((shl_t*)libraryHandle,
315                                (String("_") + symbolName).getCString(),
316                                TYPE_UNDEFINED,
317 kumpf 1.31                     &proc) == 0)
318 mike  1.19     {
319 mike  1.21         return DynamicSymbolHandle(proc);
320 mike  1.19     }
321            
322                return 0;
323            
324 mike  1.21 #elif defined(PEGASUS_OS_ZOS)
325                return DynamicSymbolHandle(dllqueryfn((dllhandle *)libraryHandle,
326                                           (char*)symbolName));
327 chuck 1.44 
328            #elif defined(PEGASUS_OS_OS400)
329               /*----------------------------------------------------------------*/
330               /* Get procedure pointer and return it to caller                  */
331               /*----------------------------------------------------------------*/
332            
333                Qus_EC_t os400ErrorCode = {0};
334                os400ErrorCode.Bytes_Provided = sizeof(Qus_EC_t);
335                os400ErrorCode.Bytes_Available = 0;
336            
337                int exportType;
338 chuck 1.48     int hdl = (int)libraryHandle; 
339 chuck 1.44     void * procAddress = NULL;
340            
341 chuck 1.48     QleGetExp(&hdl,
342 chuck 1.44 	     0,
343            	     0,
344            	     (char *)symbolName,
345            	     &procAddress,
346            	     &exportType,
347            	     &os400ErrorCode);
348            
349                if (os400ErrorCode.Bytes_Available)
350                {
351                  // Got an error. 
352                   memset(os400ExceptionID, '\0', 8);
353                   strncpy(os400ExceptionID, os400ErrorCode.Exception_Id, 7);
354                   Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
355                              "Error getting export. Exception Id = %s", os400ExceptionID);
356                   return 0;
357                }
358            
359                return DynamicSymbolHandle(procAddress);
360            
361 mike  1.19 #else
362            
363                return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName));
364            
365            #endif
366            }
367            
368            String System::getHostName()
369            {
370                static char hostname[64];
371            
372                if (!*hostname)
373                    gethostname(hostname, sizeof(hostname));
374            
375 kumpf 1.49     // (temporary?) fix for problem of object path creation with
376                // fully-qualified hostname
377 dan   1.46     char *dot = strchr(hostname, '.');
378                if (dot != NULL) *dot = '\0';
379            
380 mike  1.19     return hostname;
381 kumpf 1.24 }
382            
383 kumpf 1.30 String System::getFullyQualifiedHostName ()
384            {
385            #ifdef PEGASUS_OS_HPUX
386                char hostName [MAXHOSTNAMELEN];
387                struct hostent *he;
388                String fqName;
389            
390                if (gethostname (hostName, MAXHOSTNAMELEN) != 0)
391                {
392                    return String::EMPTY;
393                }
394            
395                if (he = gethostbyname (hostName))
396                {
397                   strcpy (hostName, he->h_name);
398                }
399            
400                fqName.assign (hostName);
401            
402                return fqName;
403            #else
404 kumpf 1.30     //
405                //  ATTN: Implement this method to return the fully qualified host name
406                //
407                return String::EMPTY;
408            #endif
409            }
410            
411            String System::getSystemCreationClassName ()
412            {
413            #ifdef PEGASUS_OS_HPUX
414                return "CIM_ComputerSystem";
415            #else
416                //
417                //  ATTN: Implement this method to return the system creation class name
418                //
419                return String::EMPTY;
420            #endif
421            }
422            
423 kumpf 1.24 Uint32 System::lookupPort(
424                const char * serviceName, 
425                Uint32 defaultPort)
426            {
427                Uint32 localPort;
428            
429                struct servent *serv;
430            
431                //
432                // Get wbem-local port from /etc/services
433                //
434 chuck 1.44 #if !defined(PEGASUS_OS_OS400)
435 kumpf 1.37     if ( (serv = getservbyname(serviceName, TCP)) != NULL )
436 chuck 1.44 #else
437                // Need to cast on OS/400
438                if ( (serv = getservbyname((char *)serviceName, TCP)) != NULL )
439            #endif
440 kumpf 1.24     {
441 sage  1.43 #ifndef PEGASUS_PLATFORM_LINUX_IX86_GNU
442 kumpf 1.24         localPort = serv->s_port;
443 sage  1.43 #else
444                    localPort = htons((uint16_t)serv->s_port);
445            #endif
446 kumpf 1.24     }
447                else
448                {
449                    localPort = defaultPort;
450                }
451            
452                return localPort;
453 mike  1.19 }
454            
455 mike  1.21 String System::getPassword(const char* prompt)
456            {
457            
458                String password;
459            
460 chuck 1.44 #if !defined(PEGASUS_OS_OS400)
461                // Not supported on OS/400, and we don't need it.
462 mike  1.21     password = String(getpass( prompt ));
463 chuck 1.44 #endif
464 mike  1.21 
465                return password;
466            }
467            
468 kumpf 1.38 String System::getEffectiveUserName()
469 mike  1.21 {
470                String userName = String::EMPTY;
471                struct passwd*   pwd = NULL;
472            
473                //
474                //  get the currently logged in user's UID.
475                //
476 kumpf 1.38     pwd = getpwuid(geteuid());
477 mike  1.21     if ( pwd == NULL )
478                {
479                    //ATTN: Log a message
480                    // "User might have been removed just after login"
481                }
482                else
483                {
484                    //
485                    //  get the user name
486                    //
487                    userName.assign(pwd->pw_name);
488                }
489            
490                return(userName);
491            }
492            
493            String System::encryptPassword(const char* password, const char* salt)
494            {
495 chuck 1.44 #if !defined(PEGASUS_OS_OS400)
496 mike  1.21     return ( String(crypt( password,salt)) );
497 chuck 1.44 #else
498                // Not supported on OS400, and we don't need it.
499                return ( String(password) );
500            #endif
501 mike  1.21 }
502            
503 kumpf 1.47 Boolean System::isSystemUser(const char* userName)
504 mike  1.21 {
505                //
506                //  get the password entry for the user
507                //
508                if  ( getpwnam(userName) == NULL )
509                {
510            	return false;
511                }
512                return true;
513            }
514            
515 kumpf 1.25 Boolean System::isPrivilegedUser(const String userName)
516 mike  1.21 {
517                //
518 kumpf 1.38     // Check if the given user is a privileged user
519 mike  1.21     //
520 kumpf 1.38     struct passwd   pwd;
521                struct passwd   *result;
522                char            pwdBuffer[1024];
523 kumpf 1.25 
524 kumpf 1.47     if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, 1024, &result) == 0)
525 kumpf 1.25     {
526 kumpf 1.38         if ( pwd.pw_uid == 0 )
527 kumpf 1.25         {
528 kumpf 1.38             return true;
529 kumpf 1.25         }
530 mike  1.21     }
531 kumpf 1.38     return false;
532 kumpf 1.26 }
533            
534            String System::getPrivilegedUserName()
535            {
536                static String userName = String::EMPTY;
537            
538                if (userName == String::EMPTY)
539                {
540                    struct passwd*   pwd = NULL;
541            
542                    //
543                    //  get the privileged user's UID.
544                    //
545                    pwd = getpwuid(0);
546                    if ( pwd != NULL )
547                    {
548                        //
549                        //  get the user name
550                        //
551                        userName.assign(pwd->pw_name);
552                    }
553 kumpf 1.26         else
554                    {
555                        PEGASUS_ASSERT(0);
556                    }
557                }
558            
559                return (userName);
560 mike  1.21 }
561 kumpf 1.22 
562            Uint32 System::getPID()
563            {
564                //
565                // Get the Process ID
566                //
567                Uint32 pid = getpid();
568            
569                return pid;
570            }
571 mike  1.28 
572            Boolean System::truncateFile(
573                const char* path, 
574                size_t newSize)
575            {
576 chuck 1.44 #if !defined(PEGASUS_OS_OS400)
577 kumpf 1.29     return (truncate(path, newSize) == 0);
578 chuck 1.44 #else
579                int fd = open(path, O_WRONLY);
580                if (fd != -1)
581                {
582                   int rc = ftruncate(fd, newSize);
583                   close(fd);
584                   return (rc == 0);
585                }
586                
587                return false;
588            #endif
589 mike  1.28 }
590 mike  1.21     
591 mike  1.19 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2