Salome HOME
Porting to Mandrake 10.1 and new products:
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Utils_Identity.cxx
25 //  Author : Pascale NOYRET, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 # include <iostream>
30 # include <arpa/inet.h>
31 # include <netinet/in.h>
32 # include <sys/types.h>
33 # include <netdb.h>
34 # include "utilities.h"
35 # include "Utils_Identity.hxx"
36 using namespace std;
37 extern "C"
38 {
39 # include <string.h>
40 # include <pwd.h>
41 }
42
43 const char* duplicate( const char *const str ) ;
44
45 const struct utsname get_uname( void )
46 {
47         struct utsname          hostid;
48         const int retour=uname(&hostid);
49         ASSERT(retour>=0);
50         return hostid ;
51 }
52
53 const char* get_adip( void )
54 {
55         struct utsname  hostid;
56         const int retour=uname(&hostid);
57         ASSERT(retour>=0);
58
59         const hostent* pour_adip=gethostbyname(hostid.nodename);
60         ASSERT(pour_adip!=NULL);
61         const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
62         return duplicate(inet_ntoa(ip_addr));
63 }
64 const char* const get_pwname( void )
65 {
66         struct passwd *papa = getpwuid( getuid() ) ;
67         return papa->pw_name ;
68 }
69
70 Identity::Identity( const char *name ): _name(duplicate(name)),\
71                                                         _hostid(get_uname()),\
72                                                         _adip(get_adip()),\
73                                                         _uid(getuid()) ,\
74                                                         _pwname(get_pwname()) ,\
75                                                         _dir(getcwd(NULL,256)),\
76                                                         _pid(getpid()) ,\
77                                                         _start(time(NULL)),\
78                                                         _cstart(ctime(&_start))
79 //CCRT
80 {
81         ASSERT(strlen(_dir)<256);
82 }
83
84
85 Identity::~Identity(void)
86 {
87         delete [] (char*)_name ;
88         (char*&)_name = NULL ;
89
90         //delete [] (char*)_dir ;
91         //(char*&)_dir = NULL ;
92         free((char*)_dir);
93         
94         delete [] (char*)_adip ;
95         (char*&)_adip = NULL ;
96 }
97
98 /*------------*/
99 /* Accessors  */
100 /*------------*/
101
102 const char* const Identity::name (void) const
103 {
104         return  _name ;
105 }
106 const pid_t &Identity::pid(void) const
107 {
108         return _pid ;
109 }
110 const struct utsname &Identity::hostid(void) const
111 {
112         return _hostid ;
113 }
114 const uid_t &Identity::uid(void) const
115 {
116         return _uid ;
117 }
118 const time_t &Identity::start(void) const
119 {
120         return _start ;
121 }
122 const char* const Identity::rep (void) const
123 {
124         return  _dir ;
125 }
126 const char* const Identity::pwname (void) const
127 {
128         return  _pwname ;
129 }
130 const char* const Identity::adip (void) const
131 {
132         return _adip ;
133 }
134
135 /*------------------*/
136 /* Other methods    */
137 /*------------------*/
138
139 const char* Identity::host_char( void ) const
140 {
141         return _hostid.nodename;
142 }
143
144 const char* Identity::start_char(void) const
145 {
146         return ctime(&_start) ;
147 }
148
149 ostream & operator<< ( ostream& os , const Identity& monid )
150 {
151         ASSERT(monid._name!=NULL) ;
152         os << "Identity :" << endl ;
153         os << '\t' << "Component name : " << monid._name << endl ;
154         os << '\t' << "Numero de PID :  " << monid._pid << endl;
155         os << '\t' << "Uid utilisateur  : "   << monid._uid << endl;
156         os << '\t' << "nom utilisateur  : "   << monid._pwname << endl;
157         os << '\t' << "Nom de machine : " << (monid._hostid).nodename << endl;
158         os << '\t' << "Adresse IP : " << monid._adip << endl;
159         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
160         os << '\t' << "Dans le repertoire : " << monid._dir << endl;
161
162         return os ;
163 }