Salome HOME
Updated copyright comment
[modules/kernel.git] / src / Utils / Utils_Identity.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 //  SALOME Utils : general SALOME's definitions and tools
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 # include <cstring>
33
34 extern "C"
35 {
36 #ifndef WIN32 /* unix functionality */
37 # include <pwd.h>
38 #endif
39 }
40
41 #ifndef WIN32 /* unix functionality */
42
43 # include <arpa/inet.h>
44 # include <netinet/in.h>
45 # include <sys/types.h>
46 # include <netdb.h>
47
48 const char* duplicate( const char *const str ) ;
49
50 const struct utsname get_uname( void )
51 {
52         struct utsname          hostid;
53 #if defined(_DEBUG_) || defined(_DEBUG)
54         const int retour=uname(&hostid);
55         ASSERT(retour>=0);
56 #else
57         uname(&hostid);
58 #endif
59         return hostid ;
60 }
61
62 const char* get_adip( void )
63 {
64         struct utsname  hostid;
65 #if defined(_DEBUG_) || defined(_DEBUG)
66         const int retour=uname(&hostid);
67         ASSERT(retour>=0);
68 #else
69         uname(&hostid);
70 #endif
71
72         const hostent* pour_adip=gethostbyname(hostid.nodename);
73         if(pour_adip  == NULL)
74           pour_adip=gethostbyname("localhost");
75         ASSERT(pour_adip!=NULL);
76         const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
77         return duplicate(inet_ntoa(ip_addr));
78 }
79 const char* get_pwname( void )
80 {
81         struct passwd *papa = getpwuid(getuid());
82         return papa->pw_name ;
83 }
84
85 #else /* Windows functionality */
86
87 #include <windows.h>
88 #include <direct.h>
89 #include <process.h>
90
91 #include "Basics_Utils.hxx"
92
93 const char* duplicate( const char *const str ) ;
94
95 const char* get_uname( void )
96 {
97 #ifdef UNICODE
98             std::wstring hostName(4096, 0);
99 #else
100             static std::string hostName(4096, 0);
101 #endif
102         static DWORD nSize = (DWORD)hostName.length(); //!< TODO: conversion from size_t to DWORD
103         static int res = ::GetComputerNameEx(ComputerNameDnsFullyQualified, &hostName[0], &nSize);
104         ASSERT( res );
105 #ifdef UNICODE
106                 static std::string aRes = Kernel_Utils::utf8_encode_s(hostName);
107                 return aRes.c_str();
108 #else
109         return hostName.c_str();
110 #endif
111 }
112
113 const char* get_adip( void )
114 {
115         //#include <Nspapi.h>
116         //#include <Svcguid.h>
117         //static GUID sType = SVCID_HOSTNAME;
118         //static CSADDR_INFO* ips = new CSADDR_INFO[8]; // in case multiple IP addresses are returned
119         //static DWORD nSize = 1024;
120         //static std::string uname = get_uname();
121         //static int res = ::GetAddressByName( NS_DEFAULT, &sType, &uname[0], 0, 0, 0, ips, &nSize, 0, 0 );
122         //if ( res )
123         //  return ips[0].LocalAddr.lpSockaddr->sa_data;
124
125         static hostent* he = ::gethostbyname( get_uname() );
126         if ( he && he->h_addr_list && he->h_length >0 ) {
127           static char str[16];
128       unsigned i1 = (unsigned char)he->h_addr_list[0][0];
129       unsigned i2 = (unsigned char)he->h_addr_list[0][1];
130       unsigned i3 = (unsigned char)he->h_addr_list[0][2];
131       unsigned i4 = (unsigned char)he->h_addr_list[0][3];
132       sprintf ( str, "%03u.%03u.%03u.%03u", i1, i2, i3, i4 );
133                 return str;
134         }
135         return "<unknown>";
136 }
137
138 const char* const get_pwname( void )
139 {
140 #ifdef UNICODE
141         std::wstring retVal(4096, 0);
142 #else
143         static std::string retVal(4096, 0);
144 #endif
145   static DWORD  dwSize = (DWORD)(retVal.length() + 1); //!< TODO: conversion from size_t to DWORD
146   static int res = GetUserName( &retVal[0], &dwSize );
147   ASSERT( res );
148 #ifdef UNICODE
149   static std::string aRes = Kernel_Utils::utf8_encode_s(retVal);
150   return aRes.c_str();
151 #else
152   return retVal.c_str();
153 #endif
154 }
155
156 PSID getuid() {
157         PSID         retVal        = NULL;
158         HANDLE       hProcessToken = INVALID_HANDLE_VALUE;
159         PTOKEN_OWNER pTKowner      = NULL;
160         LPVOID buffer = NULL;
161         DWORD dwsize = 0;
162
163         if (  !OpenProcessToken ( GetCurrentProcess (), TOKEN_QUERY, &hProcessToken )) return 0;
164         if (!GetTokenInformation(hProcessToken, TokenOwner, buffer, dwsize, &dwsize)) return 0;
165         pTKowner = (PTOKEN_OWNER)buffer;
166         if ( pTKowner != NULL ) {
167                 retVal = pTKowner->Owner;
168         }
169         if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
170
171         return retVal;
172 }
173
174 #define getcwd _getcwd
175 #define getpid _getpid
176
177 #endif /* WIN32 */
178
179
180 Identity::Identity( const char *name ): _name(duplicate(name)),\
181                                                         _adip(get_adip()),\
182                                                         _hostid(get_uname()),\
183                                                         _pid(getpid()) ,\
184                                                         _uid(getuid()) ,\
185                                                         _pwname(get_pwname()) ,\
186                                                         _dir(getcwd(NULL,4096)),\
187                                                         _start(time(NULL)),\
188                                                         _cstart(ctime(&_start))
189 //CCRT
190 {
191         ASSERT(strlen(_dir)<4096);
192 }
193
194
195 Identity::~Identity(void)
196 {
197         delete [] (char*)_name ;
198         (char*&)_name = NULL ;
199
200         //delete [] (char*)_dir ;
201         //(char*&)_dir = NULL ;
202         free((char*)_dir);
203 #ifndef WIN32
204   // free the memory only on Unix
205   // because in Windows it is the same static variable
206   // (function get_adip() returns the same char* as get_uname() )
207         delete [] (char*)_adip ;
208 #endif
209         (char*&)_adip = NULL ;
210
211 }
212
213 /*------------*/
214 /* Accessors  */
215 /*------------*/
216
217 const char* Identity::name (void) const
218 {
219         return  _name ;
220 }
221 #ifndef WIN32
222 const pid_t& Identity::pid(void) const
223 #else
224 const DWORD& Identity::pid(void) const
225 #endif
226 {
227         return _pid ;
228 }
229
230 #ifndef WIN32
231 const struct utsname &Identity::hostid(void) const
232 #else
233 const char* Identity::hostid(void) const
234 #endif
235 {
236     return _hostid ;
237 }
238
239 #ifndef WIN32
240 const uid_t& Identity::uid(void) const
241 #else
242 const PSID& Identity::uid(void) const
243 #endif
244 {
245         return _uid ;
246 }
247 const time_t &Identity::start(void) const
248 {
249         return _start ;
250 }
251 const char* Identity::rep (void) const
252 {
253         return  _dir ;
254 }
255 const char* Identity::pwname (void) const
256 {
257         return  _pwname ;
258 }
259 const char* Identity::adip (void) const
260 {
261         return _adip ;
262 }
263
264 /*------------------*/
265 /* Other methods    */
266 /*------------------*/
267
268 const char* Identity::host_char( void ) const
269 {
270 #ifndef WIN32
271         return _hostid.nodename;
272 #else
273         return _hostid;
274 #endif
275 }
276
277 const char* Identity::start_char(void) const
278 {
279         return ctime(&_start) ;
280 }
281
282 std::ostream & operator<< ( std::ostream& os , const Identity& monid )
283 {
284         ASSERT(monid._name!=NULL) ;
285         os << "Identity :" << std::endl ;
286         os << '\t' << "Component name : " << monid._name << std::endl ;
287         os << '\t' << "Numero de PID :  " << monid._pid << std::endl;
288         os << '\t' << "Uid utilisateur  : "   << monid._uid << std::endl;
289         os << '\t' << "nom utilisateur  : "   << monid._pwname << std::endl;
290 #ifndef WIN32
291         os << '\t' << "Nom de machine : " << monid._hostid.nodename << std::endl;
292 #else
293         os << '\t' << "Nom de machine : " << monid._hostid << std::endl;
294 #endif
295         os << '\t' << "Adresse IP : " << monid._adip << std::endl;
296         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
297         os << '\t' << "Dans le repertoire : " << monid._dir << std::endl;
298
299         return os ;
300 }