Salome HOME
7d80073cd2bd6bd8ece452b9cac3acb8a371a1d6
[modules/kernel.git] / src / Utils / Utils_Identity.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : Utils_Identity.cxx
4 // Created   : Mon Nov  5 17:02:37 CET 2001
5 // Author    : Pascale NOYRET, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2001
8 // $Header$
9 //=============================================================================
10
11 # include <iostream.h>
12 # include <arpa/inet.h>
13 # include <netinet/in.h>
14 # include <sys/types.h>
15 # include <netdb.h>
16 # include "utilities.h"
17 # include "Utils_Identity.hxx"
18 extern "C"
19 {
20 # include <string.h>
21 # include <pwd.h>
22 }
23
24 const char* duplicate( const char *const str ) ;
25
26 const struct utsname get_uname( void )
27 {
28         struct utsname          hostid;
29         const int retour=uname(&hostid);
30         ASSERT(retour>=0);
31         return hostid ;
32 }
33
34 const char* get_adip( void )
35 {
36         struct utsname  hostid;
37         const int retour=uname(&hostid);
38         ASSERT(retour>=0);
39
40         const hostent* pour_adip=gethostbyname(hostid.nodename);
41         ASSERT(pour_adip!=NULL);
42         const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
43         return duplicate(inet_ntoa(ip_addr));
44 }
45 const char* const get_pwname( void )
46 {
47         struct passwd *papa = getpwuid( getuid() ) ;
48         return papa->pw_name ;
49 }
50
51 Identity::Identity( const char *name ): _name(duplicate(name)),\
52                                                         _hostid(get_uname()),\
53                                                         _adip(get_adip()),\
54                                                         _uid(getuid()) ,\
55                                                         _pwname(get_pwname()) ,\
56                                                         _dir(getcwd(NULL,0)),\
57                                                         _pid(getpid()) ,\
58                                                         _start(time(NULL)),\
59                                                         _cstart(ctime(&_start))
60 {
61         ;
62 }
63
64
65 Identity::~Identity(void)
66 {
67         delete [] (char*)_name ;
68         (char*&)_name = NULL ;
69
70         delete [] (char*)_dir ;
71         (char*&)_dir = NULL ;
72
73         delete [] (char*)_adip ;
74         (char*&)_adip = NULL ;
75 }
76
77 /*------------*/
78 /* Accessors  */
79 /*------------*/
80
81 const char* const Identity::name (void) const
82 {
83         return  _name ;
84 }
85 const pid_t &Identity::pid(void) const
86 {
87         return _pid ;
88 }
89 const struct utsname &Identity::hostid(void) const
90 {
91         return _hostid ;
92 }
93 const uid_t &Identity::uid(void) const
94 {
95         return _uid ;
96 }
97 const time_t &Identity::start(void) const
98 {
99         return _start ;
100 }
101 const char* const Identity::rep (void) const
102 {
103         return  _dir ;
104 }
105 const char* const Identity::pwname (void) const
106 {
107         return  _pwname ;
108 }
109 const char* const Identity::adip (void) const
110 {
111         return _adip ;
112 }
113
114 /*------------------*/
115 /* Other methods    */
116 /*------------------*/
117
118 const char* Identity::host_char( void ) const
119 {
120         return _hostid.nodename;
121 }
122
123 const char* Identity::start_char(void) const
124 {
125         return ctime(&_start) ;
126 }
127
128 ostream & operator<< ( ostream& os , const Identity& monid )
129 {
130         ASSERT(monid._name!=NULL) ;
131         os << "Identity :" << endl ;
132         os << '\t' << "Component name : " << monid._name << endl ;
133         os << '\t' << "Numero de PID :  " << monid._pid << endl;
134         os << '\t' << "Uid utilisateur  : "   << monid._uid << endl;
135         os << '\t' << "nom utilisateur  : "   << monid._pwname << endl;
136         os << '\t' << "Nom de machine : " << (monid._hostid).nodename << endl;
137         os << '\t' << "Adresse IP : " << monid._adip << endl;
138         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
139         os << '\t' << "Dans le repertoire : " << monid._dir << endl;
140
141         return os ;
142 }