Salome HOME
Removed references to OCC
[modules/kernel.git] / src / Utils / Utils_Identity.cxx
1 //  SALOME Utils : general SALOME's definitions and tools
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : Utils_Identity.cxx
25 //  Author : Pascale NOYRET, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 # include <iostream>
30 # include "utilities.h"
31 # include "Utils_Identity.hxx"
32
33 extern "C"
34 {
35 # include <string.h>
36
37 #ifndef WNT /* unix functionality */
38 # include <pwd.h>
39 #endif
40 }
41
42 #ifndef WNT /* unix functionality */
43
44 # include <arpa/inet.h>
45 # include <netinet/in.h>
46 # include <sys/types.h>
47 # include <netdb.h>
48
49 const char* duplicate( const char *const str ) ;
50
51 const struct utsname get_uname( void )
52 {
53         struct utsname          hostid;
54         const int retour=uname(&hostid);
55         ASSERT(retour>=0);
56         return hostid ;
57 }
58
59 const char* get_adip( void )
60 {
61         struct utsname  hostid;
62         const int retour=uname(&hostid);
63         ASSERT(retour>=0);
64
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));
69 }
70 const char* const get_pwname( void )
71 {
72         struct passwd *papa = getpwuid( getuid() ) ;
73         return papa->pw_name ;
74 }
75
76 #else /* Windows functionality */
77
78 #include <windows.h>
79 #include <direct.h>
80 #include <process.h>
81
82 const char* duplicate( const char *const str ) ;
83
84 const char* get_uname( void )
85 {
86         static std::string hostName(256, 0);
87         static DWORD nSize = hostName.length();
88         static int res = ::GetComputerNameEx(ComputerNameDnsFullyQualified, &hostName[0], &nSize);
89         ASSERT( res );
90         return hostName.c_str();
91 }
92
93 const char* get_adip( void )
94 {
95         //#include <Nspapi.h>
96         //#include <Svcguid.h>
97         //static GUID sType = SVCID_HOSTNAME;
98         //static CSADDR_INFO* ips = new CSADDR_INFO[8]; // in case multiple IP addresses are returned
99         //static DWORD nSize = 1024;
100         //static std::string uname = get_uname();
101         //static int res = ::GetAddressByName( NS_DEFAULT, &sType, &uname[0], 0, 0, 0, ips, &nSize, 0, 0 );
102         //if ( res )
103         //  return ips[0].LocalAddr.lpSockaddr->sa_data;
104
105         static hostent* he = ::gethostbyname( get_uname() );
106         if ( he && he->h_addr_list && he->h_length >0 ) {
107           static char str[16];
108       unsigned i1 = (unsigned char)he->h_addr_list[0][0];
109       unsigned i2 = (unsigned char)he->h_addr_list[0][1];
110       unsigned i3 = (unsigned char)he->h_addr_list[0][2];
111       unsigned i4 = (unsigned char)he->h_addr_list[0][3];
112       sprintf ( str, "%03u.%03u.%03u.%03u", i1, i2, i3, i4 );
113                 return str;
114         }
115         return "<unknown>";
116 }
117
118 const char* const get_pwname( void )
119 {
120   static std::string retVal(256, 0);
121   static DWORD  dwSize = retVal.length() + 1;
122   static int res = GetUserName( &retVal[0], &dwSize );
123   ASSERT( res );
124   return retVal.c_str();
125 }
126
127 PSID getuid() {
128         PSID         retVal        = NULL;
129         HANDLE       hProcessToken = INVALID_HANDLE_VALUE;
130         PTOKEN_OWNER pTKowner      = NULL;
131         LPVOID buffer = NULL;
132         DWORD dwsize = 0;
133         
134         if (  !OpenProcessToken ( GetCurrentProcess (), TOKEN_QUERY, &hProcessToken )) return 0;
135         if (!GetTokenInformation(hProcessToken, TokenOwner, buffer, dwsize, &dwsize)) return 0;
136         pTKowner = (PTOKEN_OWNER)buffer;
137         if ( pTKowner != NULL ) {
138                 retVal = pTKowner->Owner;
139         }
140         if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
141         
142         return retVal;
143 }
144
145 #define getcwd _getcwd
146 #define getpid _getpid
147
148 #endif /* WNT */
149
150
151 Identity::Identity( const char *name ): _name(duplicate(name)),\
152                                                         _hostid(get_uname()),\
153                                                         _adip(get_adip()),\
154                                                         _uid(getuid()) ,\
155                                                         _pwname(get_pwname()) ,\
156                                                         _dir(getcwd(NULL,256)),\
157                                                         _pid(getpid()) ,\
158                                                         _start(time(NULL)),\
159                                                         _cstart(ctime(&_start))
160 //CCRT
161 {
162         ASSERT(strlen(_dir)<256);
163 }
164
165
166 Identity::~Identity(void)
167 {
168         delete [] (char*)_name ;
169         (char*&)_name = NULL ;
170
171         //delete [] (char*)_dir ;
172         //(char*&)_dir = NULL ;
173         free((char*)_dir);
174 #ifndef WIN32   
175   // free the memory only on Unix
176   // becasue at Windows it is the same static variable
177   // (function get_adip() returns the same char* as get_uname() )
178         delete [] (char*)_adip ;
179 #endif
180         (char*&)_adip = NULL ;
181
182 }
183
184 /*------------*/
185 /* Accessors  */
186 /*------------*/
187
188 const char* const Identity::name (void) const
189 {
190         return  _name ;
191 }
192 #ifndef WNT
193         const pid_t& Identity::pid(void) const
194 #else
195         const DWORD& Identity::pid(void) const
196 #endif
197 {
198         return _pid ;
199 }
200
201 #ifndef WNT
202         const struct utsname &Identity::hostid(void) const
203 #else
204         const char* const Identity::hostid(void) const
205 #endif
206 {
207     return _hostid ;
208 }
209
210 #ifndef WNT
211         const uid_t& Identity::uid(void) const
212 #else
213         const PSID& Identity::uid(void) const
214 #endif
215 {
216         return _uid ;
217 }
218 const time_t &Identity::start(void) const
219 {
220         return _start ;
221 }
222 const char* const Identity::rep (void) const
223 {
224         return  _dir ;
225 }
226 const char* const Identity::pwname (void) const
227 {
228         return  _pwname ;
229 }
230 const char* const Identity::adip (void) const
231 {
232         return _adip ;
233 }
234
235 /*------------------*/
236 /* Other methods    */
237 /*------------------*/
238
239 const char* Identity::host_char( void ) const
240 {
241 #ifndef WNT
242         return _hostid.nodename;
243 #else
244         return _hostid;
245 #endif
246 }
247
248 const char* Identity::start_char(void) const
249 {
250         return ctime(&_start) ;
251 }
252
253 std::ostream & operator<< ( std::ostream& os , const Identity& monid )
254 {
255         ASSERT(monid._name!=NULL) ;
256         os << "Identity :" << std::endl ;
257         os << '\t' << "Component name : " << monid._name << std::endl ;
258         os << '\t' << "Numero de PID :  " << monid._pid << std::endl;
259         os << '\t' << "Uid utilisateur  : "   << monid._uid << std::endl;
260         os << '\t' << "nom utilisateur  : "   << monid._pwname << std::endl;
261 #ifndef WNT
262         os << '\t' << "Nom de machine : " << monid._hostid.nodename << std::endl;
263 #else
264         os << '\t' << "Nom de machine : " << monid._hostid << std::endl;
265 #endif
266         os << '\t' << "Adresse IP : " << monid._adip << std::endl;
267         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
268         os << '\t' << "Dans le repertoire : " << monid._dir << std::endl;
269
270         return os ;
271 }