Back to index

salome-kernel  6.5.0
Utils_Identity.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Utils : general SALOME's definitions and tools
00024 //  File   : Utils_Identity.cxx
00025 //  Author : Pascale NOYRET, EDF
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Utils/Utils_Identity.cxx,v 1.9.2.2.10.2.12.1 2012-04-12 14:05:32 vsr Exp $
00028 //
00029 # include <iostream>
00030 # include "utilities.h"
00031 # include "Utils_Identity.hxx"
00032 
00033 extern "C"
00034 {
00035 # include <string.h>
00036 
00037 #ifndef WIN32 /* unix functionality */
00038 # include <pwd.h>
00039 #endif
00040 }
00041 
00042 #ifndef WIN32 /* unix functionality */
00043 
00044 # include <arpa/inet.h>
00045 # include <netinet/in.h>
00046 # include <sys/types.h>
00047 # include <netdb.h>
00048 
00049 const char* duplicate( const char *const str ) ;
00050 
00051 const struct utsname get_uname( void )
00052 {
00053         struct utsname          hostid;
00054 #if defined(_DEBUG_) || defined(_DEBUG)
00055         const int retour=uname(&hostid);
00056         ASSERT(retour>=0);
00057 #else
00058         uname(&hostid);
00059 #endif
00060         return hostid ;
00061 }
00062 
00063 const char* get_adip( void )
00064 {
00065         struct utsname  hostid;
00066 #if defined(_DEBUG_) || defined(_DEBUG)
00067         const int retour=uname(&hostid);
00068         ASSERT(retour>=0);
00069 #else
00070         uname(&hostid);
00071 #endif
00072 
00073         const hostent* pour_adip=gethostbyname(hostid.nodename);
00074         ASSERT(pour_adip!=NULL);
00075         const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
00076         return duplicate(inet_ntoa(ip_addr));
00077 }
00078 const char* const get_pwname( void )
00079 {
00080         struct passwd *papa = getpwuid(getuid());
00081         return papa->pw_name ;
00082 }
00083 
00084 #else /* Windows functionality */
00085 
00086 #include <windows.h>
00087 #include <direct.h>
00088 #include <process.h>
00089 
00090 const char* duplicate( const char *const str ) ;
00091 
00092 const char* get_uname( void )
00093 {
00094         static std::string hostName(256, 0);
00095         static DWORD nSize = hostName.length();
00096         static int res = ::GetComputerNameEx(ComputerNameDnsFullyQualified, &hostName[0], &nSize);
00097         ASSERT( res );
00098         return hostName.c_str();
00099 }
00100 
00101 const char* get_adip( void )
00102 {
00103         //#include <Nspapi.h>
00104         //#include <Svcguid.h>
00105         //static GUID sType = SVCID_HOSTNAME;
00106         //static CSADDR_INFO* ips = new CSADDR_INFO[8]; // in case multiple IP addresses are returned
00107         //static DWORD nSize = 1024;
00108         //static std::string uname = get_uname();
00109         //static int res = ::GetAddressByName( NS_DEFAULT, &sType, &uname[0], 0, 0, 0, ips, &nSize, 0, 0 );
00110         //if ( res )
00111         //  return ips[0].LocalAddr.lpSockaddr->sa_data;
00112 
00113         static hostent* he = ::gethostbyname( get_uname() );
00114         if ( he && he->h_addr_list && he->h_length >0 ) {
00115           static char str[16];
00116       unsigned i1 = (unsigned char)he->h_addr_list[0][0];
00117       unsigned i2 = (unsigned char)he->h_addr_list[0][1];
00118       unsigned i3 = (unsigned char)he->h_addr_list[0][2];
00119       unsigned i4 = (unsigned char)he->h_addr_list[0][3];
00120       sprintf ( str, "%03u.%03u.%03u.%03u", i1, i2, i3, i4 );
00121                 return str;
00122         }
00123         return "<unknown>";
00124 }
00125 
00126 const char* const get_pwname( void )
00127 {
00128   static std::string retVal(256, 0);
00129   static DWORD  dwSize = retVal.length() + 1;
00130   static int res = GetUserName( &retVal[0], &dwSize );
00131   ASSERT( res );
00132   return retVal.c_str();
00133 }
00134 
00135 PSID getuid() {
00136         PSID         retVal        = NULL;
00137         HANDLE       hProcessToken = INVALID_HANDLE_VALUE;
00138         PTOKEN_OWNER pTKowner      = NULL;
00139         LPVOID buffer = NULL;
00140         DWORD dwsize = 0;
00141         
00142         if (  !OpenProcessToken ( GetCurrentProcess (), TOKEN_QUERY, &hProcessToken )) return 0;
00143         if (!GetTokenInformation(hProcessToken, TokenOwner, buffer, dwsize, &dwsize)) return 0;
00144         pTKowner = (PTOKEN_OWNER)buffer;
00145         if ( pTKowner != NULL ) {
00146                 retVal = pTKowner->Owner;
00147         }
00148         if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
00149         
00150         return retVal;
00151 }
00152 
00153 #define getcwd _getcwd
00154 #define getpid _getpid
00155 
00156 #endif /* WIN32 */
00157 
00158 
00159 Identity::Identity( const char *name ): _name(duplicate(name)),\
00160                                                         _hostid(get_uname()),\
00161                                                         _adip(get_adip()),\
00162                                                         _uid(getuid()) ,\
00163                                                         _pwname(get_pwname()) ,\
00164                                                         _dir(getcwd(NULL,256)),\
00165                                                         _pid(getpid()) ,\
00166                                                         _start(time(NULL)),\
00167                                                         _cstart(ctime(&_start))
00168 //CCRT
00169 {
00170         ASSERT(strlen(_dir)<256);
00171 }
00172 
00173 
00174 Identity::~Identity(void)
00175 {
00176         delete [] (char*)_name ;
00177         (char*&)_name = NULL ;
00178 
00179         //delete [] (char*)_dir ;
00180         //(char*&)_dir = NULL ;
00181         free((char*)_dir);
00182 #ifndef WIN32   
00183   // free the memory only on Unix
00184   // becasue at Windows it is the same static variable
00185   // (function get_adip() returns the same char* as get_uname() )
00186         delete [] (char*)_adip ;
00187 #endif
00188         (char*&)_adip = NULL ;
00189 
00190 }
00191 
00192 /*------------*/
00193 /* Accessors  */
00194 /*------------*/
00195 
00196 const char* const Identity::name (void) const
00197 {
00198         return  _name ;
00199 }
00200 #ifndef WIN32
00201         const pid_t& Identity::pid(void) const
00202 #else
00203         const DWORD& Identity::pid(void) const
00204 #endif
00205 {
00206         return _pid ;
00207 }
00208 
00209 #ifndef WIN32
00210         const struct utsname &Identity::hostid(void) const
00211 #else
00212         const char* const Identity::hostid(void) const
00213 #endif
00214 {
00215     return _hostid ;
00216 }
00217 
00218 #ifndef WIN32
00219         const uid_t& Identity::uid(void) const
00220 #else
00221         const PSID& Identity::uid(void) const
00222 #endif
00223 {
00224         return _uid ;
00225 }
00226 const time_t &Identity::start(void) const
00227 {
00228         return _start ;
00229 }
00230 const char* const Identity::rep (void) const
00231 {
00232         return  _dir ;
00233 }
00234 const char* const Identity::pwname (void) const
00235 {
00236         return  _pwname ;
00237 }
00238 const char* const Identity::adip (void) const
00239 {
00240         return _adip ;
00241 }
00242 
00243 /*------------------*/
00244 /* Other methods    */
00245 /*------------------*/
00246 
00247 const char* Identity::host_char( void ) const
00248 {
00249 #ifndef WIN32
00250         return _hostid.nodename;
00251 #else
00252         return _hostid;
00253 #endif
00254 }
00255 
00256 const char* Identity::start_char(void) const
00257 {
00258         return ctime(&_start) ;
00259 }
00260 
00261 std::ostream & operator<< ( std::ostream& os , const Identity& monid )
00262 {
00263         ASSERT(monid._name!=NULL) ;
00264         os << "Identity :" << std::endl ;
00265         os << '\t' << "Component name : " << monid._name << std::endl ;
00266         os << '\t' << "Numero de PID :  " << monid._pid << std::endl;
00267         os << '\t' << "Uid utilisateur  : "   << monid._uid << std::endl;
00268         os << '\t' << "nom utilisateur  : "   << monid._pwname << std::endl;
00269 #ifndef WIN32
00270         os << '\t' << "Nom de machine : " << monid._hostid.nodename << std::endl;
00271 #else
00272         os << '\t' << "Nom de machine : " << monid._hostid << std::endl;
00273 #endif
00274         os << '\t' << "Adresse IP : " << monid._adip << std::endl;
00275         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
00276         os << '\t' << "Dans le repertoire : " << monid._dir << std::endl;
00277 
00278         return os ;
00279 }