Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / apps / drwho / Rwho_DB_Manager.cpp
blobd36b1f646beabccc822c57e0db0415ace1715e8c
1 #include "global.h"
2 #include "Options.h"
3 #include "Rwho_DB_Manager.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_time.h"
7 #include "ace/OS_NS_fcntl.h"
9 // Change to the RWHO directory to speed up and simplify later
10 // processing. This requires opening the directory for reading with
11 // the directory iterator abstraction and then skipping the first two
12 // files in the directory, which are assumed to be "." and ".." (this
13 // function needs to be changed if this assumption does not hold!)
15 RWho_DB_Manager::RWho_DB_Manager ()
16 : number_of_users (0),
17 current_user (0),
18 WHOD_HEADER_SIZE (sizeof host_data - sizeof host_data.wd_we),
19 rwho_dir_name (RWHODIR)
21 if (ACE_OS::getcwd (this->original_pathname, MAXPATHLEN + 1) == 0)
22 ACE_ERROR ((LM_ERROR,
23 "%p\n%a",
24 Options::program_name,
25 1));
27 if (ACE_OS::chdir (this->rwho_dir_name) < 0)
28 ACE_ERROR ((LM_ERROR,
29 "%p\n%a",
30 this->rwho_dir_name,
31 1));
33 this->rwho_dir.open (this->rwho_dir_name);
35 #if 0
36 // Skip "." and ".."
37 this->rwho_dir.read ();
38 this->rwho_dir.read ();
39 #endif
42 // The destructor cleans up the RWHOD_DIR handle.
44 RWho_DB_Manager::~RWho_DB_Manager ()
46 if (ACE_OS::chdir (this->original_pathname) < 0)
47 ACE_ERROR ((LM_ERROR,
48 "%p\n%a",
49 Options::program_name,
50 1));
52 if (Options::get_opt (Options::DEBUGGING))
53 ACE_DEBUG ((LM_DEBUG,
54 "disposing the RWho_DB_Manager\n"));
57 // This procedure looks through the rwhod directory until it finds the next
58 // valid user file.
60 // The requirements for user files are:
61 // 1) The file is at least MIN_HOST_DATA_SIZE bytes long
62 // 2) It was received within the last MAX_HOST_TIMEOUT seconds
63 // Return:
64 // Are there any more hosts? */
66 int
67 RWho_DB_Manager::get_next_host ()
69 time_t current_time;
71 ACE_OS::time (&current_time);
73 // Go through each file in the directory looking for valid entries.
75 for (dirent *dir_ptr = this->rwho_dir.read ();
76 dir_ptr != 0;
77 dir_ptr = this->rwho_dir.read ())
79 ACE_HANDLE user_file =
80 ACE_OS::open (dir_ptr->d_name, O_RDONLY);
82 if (user_file < 0)
83 return -1;
85 int host_data_length =
86 ACE_OS::read (user_file,
87 (char *) &this->host_data,
88 sizeof this->host_data);
90 if (host_data_length > WHOD_HEADER_SIZE
91 && current_time - this->host_data.wd_recvtime < MAX_HOST_TIMEOUT)
93 this->current_user = 0;
94 this->number_of_users = (host_data_length - WHOD_HEADER_SIZE) / sizeof *this->host_data.wd_we;
95 ACE_OS::close (user_file);
96 return 1; // We found a good host, so return it.
98 else
99 ACE_OS::close (user_file);
102 // There are no more hosts, so return False.
103 return 0;
106 // Returns the next user's information. Note that for efficiency only
107 // pointers are copied, i.e., this info must be used before we call
108 // this function again.
111 RWho_DB_Manager::get_next_user (Protocol_Record &protocol_record)
113 // Get the next host file if necessary
114 if (this->current_user >= this->number_of_users
115 && this->get_next_host () == 0)
116 return 0;
118 protocol_record.set_login (this->host_data.wd_we[current_user].we_utmp.out_name);
119 Drwho_Node *current_node = protocol_record.get_drwho_list ();
120 current_node->set_host_name (this->host_data.wd_hostname);
121 current_node->set_idle_time (this->host_data.wd_we[current_user].we_idle);
122 this->current_user++;
124 return 1;