1 #include "ace/Malloc_Base.h"
2 #include "ace/Read_Buffer.h"
3 #include "ace/Thread_Manager.h"
5 // FUZZ: disable check_for_streams_include
6 #include "ace/streams.h" /* Because dump () uses ofstream. */
8 #include "Dump_Restore.h"
9 #include "ace/OS_NS_signal.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_string.h"
12 #include "ace/OS_NS_unistd.h"
15 Dump_Restore::Dump_Restore (int argc
, ACE_TCHAR
*argv
[])
18 ACE_NEW (this->ns_context_
,
21 // Cache the name options
22 this->name_options_
= this->ns_context_
->name_options ();
23 this->name_options_
->parse_args (argc
, argv
);
25 //determine name context
26 if (ACE_OS::strcmp (this->name_options_
->nameserver_host (),
27 ACE_TEXT ("localhost")) == 0)
29 if (ns_context_
->open (ACE_Naming_Context::PROC_LOCAL
) == -1)
32 ACE_TEXT ("ns_context_->open")));
36 // Don't really need to do this but it's a hack to fix the
37 // problme of Display () not printing the right hostname
38 ACE_OS::strcpy (this->hostname_
,
39 this->name_options_
->nameserver_host ());
41 this->name_options_
->nameserver_port ();
43 if (this->ns_context_
->open (ACE_Naming_Context::NET_LOCAL
) == -1)
46 ACE_TEXT ("ns_context_->open")));
49 this->display_menu ();
51 if (ACE_Event_Handler::register_stdin_handler (this,
52 ACE_Reactor::instance (),
53 ACE_Thread_Manager::instance ()) == -1)
56 ACE_TEXT ("register_stdin_handler")));
59 Dump_Restore::~Dump_Restore ()
61 // Deregister this handler with the ACE_Reactor.
62 ACE_Reactor::instance ()->remove_handler
64 ACE_Event_Handler::DONT_CALL
| ACE_Event_Handler::READ_MASK
);
66 ACE_OS::fclose (this->infile_
);
70 Dump_Restore::handle_input (ACE_HANDLE
)
76 if (::scanf ("%s", option
) <= 0)
79 ACE_TEXT ("try again\n")));
95 if (::scanf ("%s %hu", buf1
, &port
) <= 0)
97 set_host (ACE_TEXT_CHAR_TO_TCHAR (buf1
), port
);
101 if (::scanf ("%s", filename_
) <= 0)
104 ACE_OS::fclose (this->infile_
);
105 this->infile_
= ACE_OS::fopen(filename_
, ACE_TEXT("r"));
109 populate (Dump_Restore::BIND
);
113 populate (Dump_Restore::UNBIND
);
117 populate (Dump_Restore::REBIND
);
121 if (::scanf ("%s", dump_filename_
) <= 0)
130 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Unrecognized command.\n")));
138 Dump_Restore::display_menu ()
140 ACE_DEBUG ((LM_DEBUG
,
142 ACE_DEBUG ((LM_DEBUG
,
143 ACE_TEXT ("Name Service Main Menu\n")));
144 ACE_DEBUG ((LM_DEBUG
,
145 ACE_TEXT ("----------------------\n")));
147 // Check if using local name space or remote name space
148 if (ACE_OS::strcmp (this->name_options_
->nameserver_host (),
149 ACE_TEXT ("localhost")) == 0)
151 if (this->ns_scope_
== ACE_Naming_Context::PROC_LOCAL
)
152 ACE_DEBUG ((LM_DEBUG
,
153 ACE_TEXT (" *** Using Process Local Database ***\n\n")));
155 ACE_DEBUG ((LM_DEBUG
,
156 ACE_TEXT (" *** Using Node Local Database ***\n\n")));
160 ACE_DEBUG ((LM_DEBUG
,
161 ACE_TEXT (" Hostname: %s\n"),
163 ACE_DEBUG ((LM_DEBUG
,
164 ACE_TEXT (" Port Number: %d\n"),
169 ACE_DEBUG ((LM_DEBUG
,
170 ACE_TEXT ("Input File: %C\n"),
173 ACE_DEBUG ((LM_DEBUG
,
174 ACE_TEXT ("** No Input File Specified **\n")));
176 ACE_DEBUG ((LM_DEBUG
,
177 ACE_TEXT ("<P> Use Process Local Database\n")));
178 ACE_DEBUG ((LM_DEBUG
,
179 ACE_TEXT ("<N> Use Node Local Database\n")));
180 ACE_DEBUG ((LM_DEBUG
,
181 ACE_TEXT ("<H> Set Remote Name server <host> and <port>\n")));
182 ACE_DEBUG ((LM_DEBUG
,
183 ACE_TEXT ("<F> Set Input File <file name>\n")));
184 ACE_DEBUG ((LM_DEBUG
,
185 ACE_TEXT ("<B> Bind\n")));
186 ACE_DEBUG ((LM_DEBUG
,
187 ACE_TEXT ("<U> Unbind\n")));
188 ACE_DEBUG ((LM_DEBUG
,
189 ACE_TEXT ("<R> Rebind\n")));
190 ACE_DEBUG ((LM_DEBUG
,
191 ACE_TEXT ("<D> Dump <file name>\n")));
192 ACE_DEBUG ((LM_DEBUG
,
193 ACE_TEXT ("<Q> or ^C (exit)\n")));
198 Dump_Restore::set_proc_local ()
201 this->name_options_
->nameserver_host (ACE_TEXT ("localhost"));
202 this->name_options_
->nameserver_port (0);
204 // Set Naming Context scope
206 ACE_Naming_Context::PROC_LOCAL
;
208 // Remove old naming context
209 delete this->ns_context_
;
211 // Create new Naming Context
212 ACE_NEW_RETURN (this->ns_context_
,
216 if (this->ns_context_
->open (ACE_Naming_Context::PROC_LOCAL
) == -1)
217 ACE_ERROR_RETURN ((LM_ERROR
,
219 ACE_TEXT ("ns_context_->open")),
226 Dump_Restore::set_node_local ()
229 this->name_options_
->nameserver_host (ACE_TEXT ("localhost"));
230 this->name_options_
->nameserver_port (0);
232 // Set Naming Context scope
233 this->ns_scope_
= ACE_Naming_Context::NODE_LOCAL
;
235 // Remove old naming context
236 delete this->ns_context_
;
238 // Create new Naming Context
239 ACE_NEW_RETURN (this->ns_context_
,
243 if (ns_context_
->open (ACE_Naming_Context::NODE_LOCAL
) == -1)
244 ACE_ERROR_RETURN ((LM_ERROR
,
246 ACE_TEXT ("ns_context_->open")),
252 Dump_Restore::set_host (const ACE_TCHAR
*hostname
,
256 this->name_options_
->nameserver_host (hostname
);
257 this->name_options_
->nameserver_port (port
);
259 // Don't really need to do this but it's a hack to fix the problme
260 // of Display () not printing the right hostname
261 ACE_OS::strcpy (this->hostname_
, hostname
);
263 this->ns_scope_
= ACE_Naming_Context::NET_LOCAL
;
265 // remove old naming context
266 delete this->ns_context_
;
268 // Create new Naming Context
269 ACE_NEW_RETURN (this->ns_context_
,
273 // assume net_local context
274 if (ns_context_
->open (ACE_Naming_Context::NET_LOCAL
) == -1)
275 ACE_ERROR_RETURN ((LM_ERROR
,
277 ACE_TEXT ("ns_context_->open")),
283 Dump_Restore::doit (Dump_Restore::Operation_Type op
,
292 case Dump_Restore::BIND
:
294 result
= this->bind (name
, value
, type
);
297 case Dump_Restore::UNBIND
:
299 result
= this->unbind (name
);
302 case Dump_Restore::REBIND
:
304 result
= this->rebind (name
, value
, type
);
313 Dump_Restore::populate (Dump_Restore::Operation_Type op
)
318 enum State
{ NAME
, VALUE
, TYPE
};
321 // reset file pointer
322 ACE_OS::rewind (this->infile_
);
324 ACE_Allocator
*allocator
=
325 ACE_Allocator::instance ();
326 ACE_Read_Buffer
read_buffer (this->infile_
,
331 (temp
= read_buffer
.read ('\n')) != 0;
335 const char *actual_name
= 0;
337 const char *actual_value
= 0;
339 const char *actual_type
= 0;
345 ACE_OS::strtok (name
, "=");
346 actual_name
= ACE_OS::strtok (0, "=");
351 ACE_OS::strtok (value
, "=");
352 actual_value
= ACE_OS::strtok (0, "=");
357 ACE_OS::strtok (type
, "=");
358 actual_type
= ACE_OS::strtok (0, "=");
361 result
= this->doit (op
,
366 result
= this->doit (op
,
370 allocator
->free(name
);
372 allocator
->free(value
);
374 allocator
->free(type
);
378 ACE_ERROR_RETURN ((LM_ERROR
,
380 ACE_TEXT ("populate")),
393 Dump_Restore::bind (const char *key
,
397 int result
= ns_context_
->bind (key
,
401 ACE_ERROR_RETURN ((LM_ERROR
,
403 ACE_TEXT ("ns_context_->bind")),
405 else if (result
== 1)
406 ACE_ERROR_RETURN ((LM_ERROR
,
407 ACE_TEXT ("%s%s%s\n"),
410 ACE_TEXT ("> already bound")),
416 Dump_Restore::unbind (const char *key
)
418 int result
= ns_context_
->unbind (key
);
421 ACE_ERROR_RETURN ((LM_ERROR
,
423 ACE_TEXT ("ns_context_->unbind")),
429 Dump_Restore::rebind (const char *key
,
433 if (ns_context_
->rebind (key
,
436 ACE_ERROR_RETURN ((LM_ERROR
,
438 ACE_TEXT ("ns_context_->rebind")),
444 Dump_Restore::quit ()
446 return ACE_OS::kill (ACE_OS::getpid (), SIGINT
);
450 Dump_Restore::dump ()
452 ofstream
output_file (dump_filename_
);
454 ostream
*orig_stream
= ACE_Log_Msg::instance ()->msg_ostream ();
455 ACE_Log_Msg::instance ()->msg_ostream (&output_file
);
456 ACE_Log_Msg::instance ()->clr_flags (ACE_Log_Msg::STDERR
| ACE_Log_Msg::LOGGER
);
457 ACE_Log_Msg::instance ()->set_flags (ACE_Log_Msg::OSTREAM
);
459 ns_context_
->dump ();
461 ACE_Log_Msg::instance ()->msg_ostream (orig_stream
);
462 ACE_Log_Msg::instance ()->clr_flags (ACE_Log_Msg::STDERR
);