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