(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 david 1.54 // Modified By: Dave Rosckes (rosckes@us.ibm.com)
 31            //
 32 mike  1.19 //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifdef PEGASUS_OS_HPUX
 35            # include <dl.h>
 36 mike  1.21 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 37            # include <dll.h>
 38 chuck 1.44 #elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
 39            #  include <fcntl.h> 
 40 david 1.70 #  include <qycmutilu2.H>
 41 chuck 1.44 #  include <unistd.cleinc>
 42 david 1.54 #  include "qycmmsgclsMessage.H" // ycmMessage class
 43 david 1.63 #  include "OS400SystemState.h"  // OS400LoadDynamicLibrary, etc
 44            #include "OS400ConvertChar.h"
 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 marek 1.60 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
 58            #include <arpa/inet.h>
 59            #endif
 60            
 61 david 1.54 #if defined(PEGASUS_USE_SYSLOGS) 
 62            #include <syslog.h>
 63            #endif
 64            
 65 mike  1.19 #include <sys/stat.h>
 66            #include <sys/types.h>
 67            #include <cstdio>
 68            #include <time.h>
 69 kumpf 1.41 #include <sys/time.h>
 70 kumpf 1.24 #include <netdb.h>
 71 kumpf 1.41 #include "System.h"
 72 mike  1.21 #include <Pegasus/Common/Tracer.h>
 73 kumpf 1.25 #include <Pegasus/Common/Destroyer.h>
 74 kumpf 1.42 #include <Pegasus/Common/InternalException.h>
 75 mike  1.21 
 76 mike  1.19 PEGASUS_NAMESPACE_BEGIN
 77            
 78 kumpf 1.35 #if defined(PEGASUS_OS_HPUX)
 79            Boolean System::bindVerbose = false;
 80            #endif
 81            
 82 david 1.63 #ifdef PEGASUS_OS_OS400
 83            typedef struct os400_pnstruct
 84            {
 85              Qlg_Path_Name_T qlg_struct;
 86              char * pn; 
 87            } OS400_PNSTRUCT;
 88            #endif
 89            
 90 mike  1.19 inline void sleep_wrapper(Uint32 seconds)
 91            {
 92                sleep(seconds);
 93            }
 94            
 95            void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 96            {
 97                timeval tv;
 98                gettimeofday(&tv, 0);
 99 kumpf 1.33     seconds = Uint32(tv.tv_sec);
100                milliseconds = Uint32(tv.tv_usec) / 1000;
101 mike  1.19 }
102            
103            String System::getCurrentASCIITime()
104            {
105                char    str[50];
106                time_t  rawTime;
107            
108                time(&rawTime);
109 kumpf 1.32     strftime(str, 40,"%m/%d/%Y-%T", localtime(&rawTime));
110 mike  1.19     String time = str;
111                return time;
112            }
113            
114            void System::sleep(Uint32 seconds)
115            {
116                sleep_wrapper(seconds);
117            }
118            
119            Boolean System::exists(const char* path)
120            {
121 david 1.63 #if defined(PEGASUS_OS_OS400)
122                OS400_PNSTRUCT pathname;
123                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
124                pathname.qlg_struct.CCSID = 1208;
125            #pragma convert(37)
126                memcpy(pathname.qlg_struct.Country_ID,"US",2);
127                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
128            #pragma convert(0)
129                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
130                pathname.qlg_struct.Path_Length = strlen(path);
131                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
132                pathname.pn = (char *)path;
133             
134                return QlgAccess((Qlg_Path_Name_T *)&pathname, F_OK) == 0;
135            #else
136 mike  1.19     return access(path, F_OK) == 0;
137 david 1.63 #endif
138 mike  1.19 }
139            
140            Boolean System::canRead(const char* path)
141            {
142 david 1.63 
143            #if defined(PEGASUS_OS_OS400)
144                OS400_PNSTRUCT pathname;
145                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
146                pathname.qlg_struct.CCSID = 1208;
147            #pragma convert(37)
148                memcpy(pathname.qlg_struct.Country_ID,"US",2);
149                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
150            #pragma convert(0)
151                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
152                pathname.qlg_struct.Path_Length = strlen(path);
153                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
154                pathname.pn = (char *)path;
155            
156                return QlgAccess((Qlg_Path_Name_T *)&pathname, R_OK) == 0;
157            #else
158 mike  1.19     return access(path, R_OK) == 0;
159 david 1.63 #endif
160 mike  1.19 }
161            
162            Boolean System::canWrite(const char* path)
163            {
164 david 1.63 #if defined(PEGASUS_OS_OS400)
165                OS400_PNSTRUCT pathname;
166                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
167                pathname.qlg_struct.CCSID = 1208;
168            #pragma convert(37)
169                memcpy(pathname.qlg_struct.Country_ID,"US",2);
170                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
171            #pragma convert(0)
172                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
173                pathname.qlg_struct.Path_Length = strlen(path);
174                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
175                pathname.pn = (char *)path;
176             
177                return QlgAccess((Qlg_Path_Name_T *)&pathname, W_OK) == 0;
178            #else
179 mike  1.19     return access(path, W_OK) == 0;
180 david 1.63 #endif
181 mike  1.19 }
182            
183            Boolean System::getCurrentDirectory(char* path, Uint32 size)
184            {
185 david 1.63 #if defined(PEGASUS_OS_OS400)
186                OS400_PNSTRUCT pathname;
187                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
188                pathname.qlg_struct.CCSID = 1208;
189            #pragma convert(37)
190                memcpy(pathname.qlg_struct.Country_ID,"US",2);
191                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
192            #pragma convert(0)
193                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
194                pathname.qlg_struct.Path_Length = strlen(path);
195                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
196                pathname.pn = (char *)path;
197             
198                return QlgGetcwd((Qlg_Path_Name_T *)&pathname, size) == 0;
199            #else
200 mike  1.19     return getcwd(path, size) != NULL;
201 david 1.63 #endif
202 mike  1.19 }
203            
204            Boolean System::isDirectory(const char* path)
205            {
206                struct stat st;
207            
208 david 1.63 #if defined(PEGASUS_OS_OS400)
209                OS400_PNSTRUCT pathname;
210                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
211                pathname.qlg_struct.CCSID = 1208;
212            #pragma convert(37)
213                memcpy(pathname.qlg_struct.Country_ID,"US",2);
214                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
215            #pragma convert(0)
216                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
217                pathname.qlg_struct.Path_Length = strlen(path);
218                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
219                pathname.pn = (char *)path;
220            
221                if (QlgStat((Qlg_Path_Name_T *)&pathname, &st) != 0)
222            	return false;
223            #else
224 mike  1.19     if (stat(path, &st) != 0)
225 mike  1.21         return false;
226 david 1.63 #endif
227 mike  1.19     return S_ISDIR(st.st_mode);
228            }
229            
230            Boolean System::changeDirectory(const char* path)
231            {
232 david 1.63 #if defined(PEGASUS_OS_OS400)
233                OS400_PNSTRUCT pathname;
234                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
235                pathname.qlg_struct.CCSID = 1208;
236            #pragma convert(37)
237                memcpy(pathname.qlg_struct.Country_ID,"US",2);
238                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
239            #pragma convert(0)
240                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
241                pathname.qlg_struct.Path_Length = strlen(path);
242                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
243                pathname.pn = (char *)path;
244             
245                return QlgChdir((Qlg_Path_Name_T *)&pathname) == 0;
246            #else
247 mike  1.19     return chdir(path) == 0;
248 david 1.63 #endif
249 mike  1.19 }
250            
251            Boolean System::makeDirectory(const char* path)
252            {
253 david 1.63 
254            #if defined(PEGASUS_OS_OS400)
255                OS400_PNSTRUCT pathname;
256                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
257                pathname.qlg_struct.CCSID = 1208;
258            #pragma convert(37)
259                memcpy(pathname.qlg_struct.Country_ID,"US",2);
260                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
261            #pragma convert(0)
262                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
263                pathname.qlg_struct.Path_Length = strlen(path);
264                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
265                pathname.pn = (char *)path;
266             
267                return QlgMkdir((Qlg_Path_Name_T *)&pathname, 0777) == 0;
268            #else
269 mike  1.19     return mkdir(path, 0777) == 0;
270 david 1.63 #endif
271            
272 mike  1.19 }
273            
274            Boolean System::getFileSize(const char* path, Uint32& size)
275            {
276                struct stat st;
277            
278 david 1.63 #if defined(PEGASUS_OS_OS400)
279                OS400_PNSTRUCT pathname;
280                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
281                pathname.qlg_struct.CCSID = 1208;
282            #pragma convert(37)
283                memcpy(pathname.qlg_struct.Country_ID,"US",2);
284                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
285            #pragma convert(0)
286                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
287                pathname.qlg_struct.Path_Length = strlen(path);
288                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
289                pathname.pn = (char *)path;
290            
291                if (QlgStat((Qlg_Path_Name_T *)&pathname, &st) != 0)
292            	return false;
293            #else
294 mike  1.19     if (stat(path, &st) != 0)
295 mike  1.21         return false;
296 david 1.63 #endif
297 mike  1.19 
298                size = st.st_size;
299                return true;
300            }
301            
302            Boolean System::removeDirectory(const char* path)
303            {
304 david 1.63 #if defined(PEGASUS_OS_OS400)
305                OS400_PNSTRUCT pathname;
306                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
307                pathname.qlg_struct.CCSID = 1208;
308            #pragma convert(37)
309                memcpy(pathname.qlg_struct.Country_ID,"US",2);
310                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
311            #pragma convert(0)
312                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
313                pathname.qlg_struct.Path_Length = strlen(path);
314                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
315                pathname.pn = (char *)path;
316             
317                return QlgRmdir((Qlg_Path_Name_T *)&pathname) == 0;
318            #else
319 mike  1.21     return rmdir(path) == 0;
320 david 1.63 #endif
321 mike  1.19 }
322            
323            Boolean System::removeFile(const char* path)
324            {
325 david 1.63 #if defined(PEGASUS_OS_OS400)
326                OS400_PNSTRUCT pathname;
327                memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
328                pathname.qlg_struct.CCSID = 1208;
329            #pragma convert(37)
330                memcpy(pathname.qlg_struct.Country_ID,"US",2);
331                memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
332            #pragma convert(0)
333                pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
334                pathname.qlg_struct.Path_Length = strlen(path);
335                pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
336                pathname.pn = (char *)path;
337             
338                return QlgUnlink((Qlg_Path_Name_T *)&pathname) == 0;
339            #else
340 mike  1.21     return unlink(path) == 0;
341 david 1.63 #endif
342 mike  1.19 }
343            
344            Boolean System::renameFile(const char* oldPath, const char* newPath)
345            {
346 david 1.63 #if defined(PEGASUS_OS_OS400)
347                OS400_PNSTRUCT oldpathname;
348                memset((void*)&oldpathname, 0x00, sizeof(OS400_PNSTRUCT));
349                oldpathname.qlg_struct.CCSID = 1208;
350            #pragma convert(37)
351                memcpy(oldpathname.qlg_struct.Country_ID,"US",2);
352                memcpy(oldpathname.qlg_struct.Language_ID,"ENU",3);
353            #pragma convert(0)
354                oldpathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
355                oldpathname.qlg_struct.Path_Length = strlen(oldPath);
356                oldpathname.qlg_struct.Path_Name_Delimiter[0] = '/';
357                oldpathname.pn = (char *)oldPath;
358            
359                OS400_PNSTRUCT newpathname;
360                memset((void*)&newpathname, 0x00, sizeof(OS400_PNSTRUCT));
361                newpathname.qlg_struct.CCSID = 1208;
362            #pragma convert(37)
363                memcpy(newpathname.qlg_struct.Country_ID,"US",2);
364                memcpy(newpathname.qlg_struct.Language_ID,"ENU",3);
365            #pragma convert(0)
366                newpathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
367 david 1.63     newpathname.qlg_struct.Path_Length = strlen(newPath);
368                newpathname.qlg_struct.Path_Name_Delimiter[0] = '/';
369                newpathname.pn = (char *)newPath;
370            
371                if (QlgLink((Qlg_Path_Name_T *)&oldpathname,
372            		(Qlg_Path_Name_T *)&newpathname) != 0)
373                {
374            	return false;
375                }
376            
377                return QlgUnlink((Qlg_Path_Name_T *)&oldpathname) == 0;
378            #else
379            
380 mike  1.19     if (link(oldPath, newPath) != 0)
381 mike  1.21         return false;
382 mike  1.19 
383                return unlink(oldPath) == 0;
384 david 1.63 #endif
385 mike  1.19 }
386            
387            DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
388            {
389 kumpf 1.40     PEG_METHOD_ENTER(TRC_OS_ABSTRACTION, "System::loadDynamicLibrary()");
390 mike  1.19 
391 mike  1.21     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
392                              "Attempting to load library %s", fileName);
393 mike  1.19 
394 mike  1.21 #if defined(PEGASUS_OS_HPUX)
395 kumpf 1.35     void* handle;
396                if (bindVerbose)
397                {
398                    handle = shl_load(fileName, 
399                                 BIND_IMMEDIATE | DYNAMIC_PATH | BIND_VERBOSE, 0L);
400                }
401                else 
402                {
403                    handle = shl_load(fileName, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);
404                }
405 mike  1.21     Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, 
406 kumpf 1.34                   "After loading lib %s, error code is %d", fileName, 
407                              (handle == (void *)0)?errno:0);
408 mike  1.21 
409 kumpf 1.40     PEG_METHOD_EXIT();
410 mike  1.19     return DynamicLibraryHandle(handle);
411 mike  1.21 #elif defined(PEGASUS_OS_TRU64)
412 kumpf 1.40     PEG_METHOD_EXIT();
413 mike  1.21     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW));
414            #elif defined(PEGASUS_OS_ZOS)
415 kumpf 1.40     PEG_METHOD_EXIT();
416 mike  1.21     return DynamicLibraryHandle(dllload(fileName));
417 chuck 1.44 #elif defined(PEGASUS_OS_OS400)
418                PEG_METHOD_EXIT();
419 chuck 1.57     return DynamicLibraryHandle(OS400_LoadDynamicLibrary(fileName));
420 mike  1.19 #else
421 kumpf 1.40     PEG_METHOD_EXIT();
422 david 1.63     return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL));
423 mike  1.21 #endif
424 mike  1.20 
425 mike  1.21 }
426 mike  1.20 
427 mike  1.21 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
428            {
429                // ATTN: Should this method indicate success/failure?
430 keith.petley 1.62 #if defined(PEGASUS_OS_LINUX) || defined(PEGASUS_OS_SOLARIS)
431 mike         1.21     dlclose(libraryHandle);
432                   #endif
433                   
434                   #ifdef PEGASUS_OS_HPUX
435                       // Note: shl_unload will unload the library even if it has been loaded
436                       // multiple times.  No reference count is kept.
437 kumpf        1.31     int ignored = shl_unload(reinterpret_cast<shl_t>(libraryHandle));
438 mike         1.19 #endif
439 chuck        1.57 
440                   #ifdef PEGASUS_OS_OS400
441                      OS400_UnloadDynamicLibrary((int)libraryHandle);
442                   #endif
443 kv.le        1.64 
444                   #ifdef PEGASUS_OS_AIX
445                       dlclose(libraryHandle);
446                   #endif
447                   
448 mike         1.19 }
449                   
450                   String System::dynamicLoadError() {
451 mike         1.21     // ATTN: Is this safe in a multi-threaded process?  Should this string
452                       // be returned from loadDynamicLibrary?
453 mike         1.19 #ifdef PEGASUS_OS_HPUX
454 mike         1.21     // ATTN: If shl_load() returns NULL, this value should be strerror(errno)
455                       return String();
456                   #elif defined(PEGASUS_OS_ZOS)
457 mike         1.19     return String();
458 chuck        1.44 #elif defined(PEGASUS_OS_OS400)
459 chuck        1.57     return String(OS400_DynamicLoadError());
460 mike         1.19 #else
461                       String dlerr = dlerror();
462                       return dlerr;
463                   #endif
464                   }
465                   
466                   
467                   DynamicSymbolHandle System::loadDynamicSymbol(
468                       DynamicLibraryHandle libraryHandle,
469                       const char* symbolName)
470                   {
471                   #ifdef PEGASUS_OS_HPUX
472                       char* p = (char*)symbolName;
473                       void* proc = 0;
474                   
475 kumpf        1.31     if (shl_findsym((shl_t*)&libraryHandle, symbolName, TYPE_UNDEFINED,
476                                       &proc) == 0)
477                       {
478 mike         1.21         return DynamicSymbolHandle(proc);
479 kumpf        1.31     }
480 mike         1.19 
481 kumpf        1.47     if (shl_findsym((shl_t*)libraryHandle,
482                                       (String("_") + symbolName).getCString(),
483                                       TYPE_UNDEFINED,
484 kumpf        1.31                     &proc) == 0)
485 mike         1.19     {
486 mike         1.21         return DynamicSymbolHandle(proc);
487 mike         1.19     }
488                   
489                       return 0;
490                   
491 mike         1.21 #elif defined(PEGASUS_OS_ZOS)
492                       return DynamicSymbolHandle(dllqueryfn((dllhandle *)libraryHandle,
493                                                  (char*)symbolName));
494 chuck        1.44 
495                   #elif defined(PEGASUS_OS_OS400)
496 chuck        1.57     return DynamicSymbolHandle(OS400_LoadDynamicSymbol((int)libraryHandle,
497                                                  symbolName));
498 mike         1.19 #else
499                   
500 chuck        1.57     return DynamicSymbolHandle(dlsym(libraryHandle,(char *) symbolName));
501 mike         1.19 
502                   #endif
503                   }
504                   
505                   String System::getHostName()
506                   {
507                       static char hostname[64];
508                   
509                       if (!*hostname)
510 david        1.63     {
511 mike         1.19         gethostname(hostname, sizeof(hostname));
512 david        1.63 #if defined(PEGASUS_OS_OS400)
513                   	EtoA(hostname);
514                   #endif
515                       }
516 mike         1.19 
517                       return hostname;
518 kumpf        1.24 }
519                   
520 kumpf        1.30 String System::getFullyQualifiedHostName ()
521                   {
522                   #ifdef PEGASUS_OS_HPUX
523                       char hostName [MAXHOSTNAMELEN];
524                       struct hostent *he;
525                       String fqName;
526                   
527                       if (gethostname (hostName, MAXHOSTNAMELEN) != 0)
528                       {
529                           return String::EMPTY;
530                       }
531                   
532                       if (he = gethostbyname (hostName))
533                       {
534                          strcpy (hostName, he->h_name);
535                       }
536                   
537                       fqName.assign (hostName);
538                   
539                       return fqName;
540                   #else
541 kumpf        1.30     //
542                       //  ATTN: Implement this method to return the fully qualified host name
543                       //
544                       return String::EMPTY;
545                   #endif
546                   }
547                   
548                   String System::getSystemCreationClassName ()
549                   {
550                   #ifdef PEGASUS_OS_HPUX
551                       return "CIM_ComputerSystem";
552                   #else
553                       //
554                       //  ATTN: Implement this method to return the system creation class name
555                       //
556                       return String::EMPTY;
557                   #endif
558                   }
559                   
560 kumpf        1.24 Uint32 System::lookupPort(
561                       const char * serviceName, 
562                       Uint32 defaultPort)
563                   {
564                       Uint32 localPort;
565                   
566                       struct servent *serv;
567                   
568                       //
569                       // Get wbem-local port from /etc/services
570                       //
571 chuck        1.44 #if !defined(PEGASUS_OS_OS400)
572 keith.petley 1.62 #ifdef PEGASUS_OS_SOLARIS
573                   #define SERV_BUFF_SIZE	1024
574                       struct servent	serv_result;
575                       char		buf[SERV_BUFF_SIZE];
576                   
577                       if ( (serv = getservbyname_r(serviceName, TCP, &serv_result,
578                   				buf, SERV_BUFF_SIZE)) != NULL )
579                   #else // PEGASUS_OS_SOLARIS
580 kumpf        1.37     if ( (serv = getservbyname(serviceName, TCP)) != NULL )
581 keith.petley 1.62 #endif // PEGASUS_OS_SOLARIS
582                   #else  // !PEGASUS_OS_OS400
583 chuck        1.65     // Note - serviceName came from Constants.h - no need to
584                       // convert to EBCDIC
585 chuck        1.44     // Need to cast on OS/400
586                       if ( (serv = getservbyname((char *)serviceName, TCP)) != NULL )
587 keith.petley 1.62 #endif  // !PEGASUS_OS_OS400
588 kumpf        1.24     {
589 sage         1.43         localPort = htons((uint16_t)serv->s_port);
590 kumpf        1.24     }
591                       else
592                       {
593                           localPort = defaultPort;
594                       }
595                   
596                       return localPort;
597 mike         1.19 }
598                   
599 mike         1.21 String System::getPassword(const char* prompt)
600                   {
601                   
602                       String password;
603                   
604 chuck        1.44 #if !defined(PEGASUS_OS_OS400)
605                       // Not supported on OS/400, and we don't need it.
606 mike         1.21     password = String(getpass( prompt ));
607 chuck        1.44 #endif
608 mike         1.21 
609                       return password;
610                   }
611                   
612 kumpf        1.38 String System::getEffectiveUserName()
613 mike         1.21 {
614                       String userName = String::EMPTY;
615                       struct passwd*   pwd = NULL;
616                   
617 kumpf        1.66 #if defined(PEGASUS_OS_SOLARIS) || \
618                       defined(PEGASUS_OS_HPUX) || \
619                       defined(PEGASUS_OS_LINUX)
620                   
621                       const unsigned int PWD_BUFF_SIZE = 1024;
622 keith.petley 1.62     struct passwd	local_pwd;
623                       char		buf[PWD_BUFF_SIZE];
624                       if(getpwuid_r(geteuid(), &local_pwd, buf, PWD_BUFF_SIZE, &pwd)) {
625                   	pwd = (struct passwd *)NULL;
626                       }
627                   #else
628 mike         1.21     //
629                       //  get the currently logged in user's UID.
630                       //
631 kumpf        1.38     pwd = getpwuid(geteuid());
632 keith.petley 1.62 #endif
633 mike         1.21     if ( pwd == NULL )
634                       {
635                           //ATTN: Log a message
636                           // "User might have been removed just after login"
637                       }
638                       else
639                       {
640 chuck        1.65 #if defined(PEGASUS_OS_OS400)
641                   	EtoA(pwd->pw_name);
642                   #endif
643 mike         1.21         //
644                           //  get the user name
645                           //
646                           userName.assign(pwd->pw_name);
647                       }
648                   
649                       return(userName);
650                   }
651                   
652                   String System::encryptPassword(const char* password, const char* salt)
653                   {
654 chuck        1.44 #if !defined(PEGASUS_OS_OS400)
655 mike         1.21     return ( String(crypt( password,salt)) );
656 chuck        1.44 #else
657                       // Not supported on OS400, and we don't need it.
658                       return ( String(password) );
659                   #endif
660 mike         1.21 }
661                   
662 kumpf        1.47 Boolean System::isSystemUser(const char* userName)
663 mike         1.21 {
664 chuck        1.65 #if defined(PEGASUS_OS_OS400)
665                       AtoE((char *)userName);
666                   #endif
667                   
668 kumpf        1.66 #if defined(PEGASUS_OS_SOLARIS) || \
669                       defined(PEGASUS_OS_HPUX) || \
670                       defined(PEGASUS_OS_LINUX)
671                   
672                       const unsigned int PWD_BUFF_SIZE = 1024;
673 keith.petley 1.62     struct passwd   pwd;
674                       struct passwd   *result;
675 kumpf        1.66     char            pwdBuffer[PWD_BUFF_SIZE];
676 keith.petley 1.62 
677 kumpf        1.66     if (getpwnam_r(userName, &pwd, pwdBuffer, PWD_BUFF_SIZE, &result) != 0)
678 keith.petley 1.62 #else
679 mike         1.21     //
680                       //  get the password entry for the user
681                       //
682                       if  ( getpwnam(userName) == NULL )
683 keith.petley 1.62 #endif
684 mike         1.21     {
685 chuck        1.65 #if defined(PEGASUS_OS_OS400)
686                   	EtoA((char *)userName);
687                   #endif
688 mike         1.21 	return false;
689                       }
690 chuck        1.65 #if defined(PEGASUS_OS_OS400)
691                       EtoA((char *)userName);
692                   #endif
693 mike         1.21     return true;
694                   }
695                   
696 kumpf        1.25 Boolean System::isPrivilegedUser(const String userName)
697 mike         1.21 {
698                       //
699 kumpf        1.38     // Check if the given user is a privileged user
700 mike         1.21     //
701 chuck        1.51 #if !defined(PEGASUS_OS_OS400)
702 kumpf        1.38     struct passwd   pwd;
703                       struct passwd   *result;
704                       char            pwdBuffer[1024];
705 kumpf        1.25 
706 kumpf        1.47     if (getpwnam_r(userName.getCString(), &pwd, pwdBuffer, 1024, &result) == 0)
707 kumpf        1.25     {
708 kumpf        1.38         if ( pwd.pw_uid == 0 )
709 kumpf        1.25         {
710 kumpf        1.38             return true;
711 kumpf        1.25         }
712 mike         1.21     }
713 kumpf        1.38     return false;
714 chuck        1.51 #else
715 chuck        1.65     CString user = userName.getCString();
716                       const char * tmp = (const char *)user;
717                       AtoE((char *)tmp);
718                       return ycmCheckUserCmdAuthorities(tmp);
719 chuck        1.51 #endif
720 kumpf        1.26 }
721                   
722                   String System::getPrivilegedUserName()
723                   {
724                       static String userName = String::EMPTY;
725                   
726                       if (userName == String::EMPTY)
727                       {
728                           struct passwd*   pwd = NULL;
729 kumpf        1.66 #if defined(PEGASUS_OS_SOLARIS) || \
730                       defined(PEGASUS_OS_HPUX) || \
731                       defined(PEGASUS_OS_LINUX)
732                           const unsigned int PWD_BUFF_SIZE = 1024;
733 keith.petley 1.62 	struct passwd	local_pwd;
734                   	char		buf[PWD_BUFF_SIZE];
735                   	if(getpwuid_r(0, &local_pwd, buf, PWD_BUFF_SIZE, &pwd)) {
736                   		pwd = (struct passwd *)NULL;
737                   	}
738                   #else
739 kumpf        1.26         //
740                           //  get the privileged user's UID.
741                           //
742 chuck        1.51 	//  (on OS/400, this is QSECOFR)
743 kumpf        1.26         pwd = getpwuid(0);
744 keith.petley 1.62 #endif
745 kumpf        1.26         if ( pwd != NULL )
746                           {
747 chuck        1.65 #if defined(PEGASUS_OS_OS400)
748                   	    EtoA(pwd->pw_name);
749                   #endif
750 kumpf        1.26             //
751                               //  get the user name
752                               //
753                               userName.assign(pwd->pw_name);
754                           }
755                           else
756                           {
757                               PEGASUS_ASSERT(0);
758                           }
759                       }
760                   
761                       return (userName);
762 mike         1.21 }
763 kumpf        1.22 
764                   Uint32 System::getPID()
765                   {
766                       //
767                       // Get the Process ID
768                       //
769                       Uint32 pid = getpid();
770                   
771                       return pid;
772                   }
773 mike         1.28 
774                   Boolean System::truncateFile(
775                       const char* path, 
776                       size_t newSize)
777                   {
778 chuck        1.44 #if !defined(PEGASUS_OS_OS400)
779 kumpf        1.29     return (truncate(path, newSize) == 0);
780 chuck        1.44 #else
781 chuck        1.65     OS400_PNSTRUCT pathname;
782                       memset((void*)&pathname, 0x00, sizeof(OS400_PNSTRUCT));
783                       pathname.qlg_struct.CCSID = 1208;
784                   #pragma convert(37)
785                       memcpy(pathname.qlg_struct.Country_ID,"US",2);
786                       memcpy(pathname.qlg_struct.Language_ID,"ENU",3);
787                   #pragma convert(0)
788                       pathname.qlg_struct.Path_Type = QLG_PTR_SINGLE;
789                       pathname.qlg_struct.Path_Length = strlen(path);
790                       pathname.qlg_struct.Path_Name_Delimiter[0] = '/';
791                       pathname.pn = (char *)path;
792                   
793                       int fd = QlgOpen((Qlg_Path_Name_T *)&pathname, O_WRONLY);
794 chuck        1.44     if (fd != -1)
795                       {
796                          int rc = ftruncate(fd, newSize);
797                          close(fd);
798                          return (rc == 0);
799                       }
800                       
801                       return false;
802                   #endif
803 mike         1.28 }
804 tony         1.52 
805                   // Is absolute path?
806                   Boolean System::is_absolute_path(const char *path)
807                   {
808                     if (path == NULL)
809                       return false;
810                     
811                     if (path[0] == '/')
812                       return true;
813                     
814                     return false;
815                   }
816 david        1.54 
817                   void System::openlog(const String ident)
818                   {
819                   #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
820                   
821 kumpf        1.56     // ATTN: Hard-code the "cimserver" identifier until the infrastructure
822                       // is set up to pass this as the "ident" string.
823                       //::openlog(ident.getCString(), LOG_PID|LOG_CONS, LOG_DAEMON);
824 kumpf        1.69     ::openlog("cimserver", LOG_PID, LOG_DAEMON);
825 david        1.54 
826                   #endif
827                   
828                       return;
829                   }
830                   
831                   void System::syslog(Uint32 severity, const char *data)
832                   {
833                   #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
834                   
835                       // FUTURE-SF-P3-20020517 : Use the Syslog on HP-UX. Eventually only 
836                       // certain messages will go to the Syslog and others to the 
837                       // Pegasus Logger.
838                       Uint32 syslogLevel = LOG_DEBUG;
839                   
840                       // Map the log levels.
841                       if (severity & Logger::TRACE) syslogLevel =       LOG_DEBUG;
842                       if (severity & Logger::INFORMATION) syslogLevel = LOG_INFO;
843                       if (severity & Logger::WARNING) syslogLevel =     LOG_WARNING;
844                       if (severity & Logger::SEVERE) syslogLevel =      LOG_ERR;
845                       if (severity & Logger::FATAL) syslogLevel =       LOG_CRIT;
846 david        1.54 
847 david        1.55     ::syslog(syslogLevel, "%s", data);
848 david        1.54 
849                   #elif defined(PEGASUS_OS_OS400)
850                   
851                       std::string replacementData = data;
852                       // All messages will go to the joblog. In the future
853                       // some messages may go to other message queues yet
854                       // to be determined.
855                       if ((severity & Logger::TRACE) ||
856                   	(severity & Logger::INFORMATION))
857                       {
858                   
859                   	// turn into ycmMessage so we can put it in the job log
860 chuck        1.65 #pragma convert(37)
861 david        1.67 	ycmMessage theMessage("CPIDF80",
862                   			          data,
863                                                 strlen(data),
864                   			          "Logger",
865                                                 ycmCTLCIMID,
866 david        1.68 			          TRUE);
867 chuck        1.65 #pragma convert(0)
868 david        1.54 
869                   	// put the message in the joblog
870                   	theMessage.joblogIt(UnitOfWorkError,
871                   			    ycmMessage::Informational);
872                       }
873                   
874                       if ((severity & Logger::WARNING) ||
875                   	(severity & Logger::SEVERE)  ||
876                   	(severity & Logger::FATAL))
877                       {
878                   	// turn into ycmMessage so we can put it in the job log
879 chuck        1.65 #pragma convert(37)
880 david        1.67 	ycmMessage theMessage("CPDDF82",
881                   			          data,
882                                                 strlen(data),
883                   			          "Logger",
884                                                 ycmCTLCIMID,
885 david        1.68 			          TRUE);
886 chuck        1.65 #pragma convert(0)
887 david        1.54 	// put the message in the joblog
888                   	theMessage.joblogIt(UnitOfWorkError,
889                   			    ycmMessage::Diagnostic);
890                       }
891                   
892                   #endif
893                   
894                       return;
895                   }
896                   
897                   void System::closelog()
898                   {
899                   #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
900                   
901 david        1.55     ::closelog();
902 david        1.54 
903                   #endif
904                   
905                       return;
906                   }
907                   
908                   // System ID constants for Logger::put and Logger::trace
909                   #if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
910                   const String System::CIMSERVER = "qycmcimom";  // Server system ID
911                   #else
912                   const String System::CIMSERVER = "cimserver";  // Server system ID
913                   #endif
914 mike         1.21     
915 mike         1.19 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2