1 // SALOME Utils : general SALOME's definitions and tools
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Utils_Identity.cxx
25 // Author : Pascale NOYRET, EDF
30 # include "utilities.h"
31 # include "Utils_Identity.hxx"
37 #ifndef WNT /* unix functionality */
42 #ifndef WNT /* unix functionality */
44 # include <arpa/inet.h>
45 # include <netinet/in.h>
46 # include <sys/types.h>
49 const char* duplicate( const char *const str ) ;
51 const struct utsname get_uname( void )
53 struct utsname hostid;
54 const int retour=uname(&hostid);
59 const char* get_adip( void )
61 struct utsname hostid;
62 const int retour=uname(&hostid);
65 const hostent* pour_adip=gethostbyname(hostid.nodename);
66 ASSERT(pour_adip!=NULL);
67 const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
68 return duplicate(inet_ntoa(ip_addr));
70 const char* const get_pwname( void )
72 struct passwd *papa = getpwuid( getuid() ) ;
73 return papa->pw_name ;
76 #else /* Windows functionality */
82 const char* duplicate( const char *const str ) ;
84 const char* get_uname( void )
86 char* hostName = new char[256];
88 ASSERT(GetComputerName(hostName, &nSize));
92 const char* get_adip( void )
97 const char* const get_pwname( void )
99 DWORD dwSize = 256 + 1;
100 char* retVal = new char[256];
101 ASSERT(GetUserName ( retVal, &dwSize ));
107 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
108 PTOKEN_OWNER pTKowner = NULL;
109 LPVOID buffer = NULL;
112 if ( !OpenProcessToken ( GetCurrentProcess (), TOKEN_QUERY, &hProcessToken )) return 0;
113 if (!GetTokenInformation(hProcessToken, TokenOwner, buffer, dwsize, &dwsize)) return 0;
114 pTKowner = (PTOKEN_OWNER)buffer;
115 if ( pTKowner != NULL ) {
116 retVal = pTKowner->Owner;
118 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
123 #define getcwd _getcwd
124 #define getpid _getpid
129 Identity::Identity( const char *name ): _name(duplicate(name)),\
130 _hostid(get_uname()),\
133 _pwname(get_pwname()) ,\
134 _dir(getcwd(NULL,256)),\
137 _cstart(ctime(&_start))
140 ASSERT(strlen(_dir)<256);
144 Identity::~Identity(void)
146 delete [] (char*)_name ;
147 (char*&)_name = NULL ;
149 //delete [] (char*)_dir ;
150 //(char*&)_dir = NULL ;
153 delete [] (char*)_adip ;
154 (char*&)_adip = NULL ;
161 const char* const Identity::name (void) const
166 const pid_t& Identity::pid(void) const
168 const DWORD& Identity::pid(void) const
175 const struct utsname &Identity::hostid(void) const
177 const char* const Identity::hostid(void) const
184 const uid_t& Identity::uid(void) const
186 const PSID& Identity::uid(void) const
191 const time_t &Identity::start(void) const
195 const char* const Identity::rep (void) const
199 const char* const Identity::pwname (void) const
203 const char* const Identity::adip (void) const
208 /*------------------*/
210 /*------------------*/
212 const char* Identity::host_char( void ) const
215 return _hostid.nodename;
221 const char* Identity::start_char(void) const
223 return ctime(&_start) ;
226 std::ostream & operator<< ( std::ostream& os , const Identity& monid )
228 ASSERT(monid._name!=NULL) ;
229 os << "Identity :" << std::endl ;
230 os << '\t' << "Component name : " << monid._name << std::endl ;
231 os << '\t' << "Numero de PID : " << monid._pid << std::endl;
232 os << '\t' << "Uid utilisateur : " << monid._uid << std::endl;
233 os << '\t' << "nom utilisateur : " << monid._pwname << std::endl;
235 os << '\t' << "Nom de machine : " << monid._hostid.nodename << std::endl;
237 os << '\t' << "Nom de machine : " << monid._hostid << std::endl;
239 os << '\t' << "Adresse IP : " << monid._adip << std::endl;
240 os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
241 os << '\t' << "Dans le repertoire : " << monid._dir << std::endl;