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