1 #include "ace/Svc_Conf.h"
2 #include "ace/Get_Opt.h"
4 #include "ace/Malloc.h"
5 #include "ace/Service_Manager.h"
6 #include "ace/Service_Types.h"
7 #include "ace/Containers.h"
8 #include "ace/Reactor.h"
9 #include "ace/Thread_Manager.h"
11 #include "ace/XML_Svc_Conf.h"
12 #include "ace/SString.h"
14 #ifndef ACE_LACKS_UNIX_SIGNALS
15 # include "ace/Signal.h"
16 #endif /* !ACE_LACKS_UNIX_SIGNALS */
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_string.h"
20 #include "ace/OS_NS_time.h"
21 #include "ace/OS_NS_unistd.h"
22 #include "ace/OS_NS_sys_stat.h"
23 #include "ace/TSS_T.h"
24 #include "ace/Service_Gestalt.h"
25 #include "ace/Svc_Conf_Param.h"
28 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 ACE_Service_Type_Dynamic_Guard::ACE_Service_Type_Dynamic_Guard
31 (ACE_Service_Repository
&r
, const ACE_TCHAR
*name
)
33 // Relocation starts where the next service will be inserted (if any)
34 , repo_begin_ (r
.current_size ())
36 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
37 // On this thread (for the duration of the initialize() method),
38 // we're about to do two things that require locking: (1) fiddle
39 // with the repository and (2) load a DLL and hence lock the
42 // Now if we don't lock the repo here, it is possible that two
43 // threads may deadlock on initialization because they can acquire
44 // locks (1) and (2) in different order, for instance:
46 // T1: loads a DLL (2) and registers a service (1);
48 // T2: may be relocating a service (1), which could lead to a
49 // (re)opening or uping the ref count on a DLL (2);
51 // To prevent this, we lock the repo here, using the repo_monitor_
53 , repo_monitor_ (r
.lock_
)
57 ACELIB_DEBUG ((LM_DEBUG
,
58 ACE_TEXT ("ACE (%P|%t) STDG::<ctor>, repo=%@")
59 ACE_TEXT(", name=%s - beginning at [%d]\n"),
64 ACE_ASSERT (this->name_
!= 0); // No name?
69 ACE_Service_Type_Dynamic_Guard::~ACE_Service_Type_Dynamic_Guard ()
71 const ACE_Service_Type
*tmp
= 0;
73 // Lookup without ignoring suspended services. Making sure
74 // not to ignore any inactive services, since those may be forward
77 int const ret
= this->repo_
.find_i (this->name_
, slot
, &tmp
, false);
79 // We inserted it (as inactive), so we expect to find it, right?
80 if ((ret
< 0 && ret
!= -2) || tmp
== 0)
83 ACELIB_ERROR ((LM_WARNING
,
84 ACE_TEXT ("ACE (%P|%t) STDG::<dtor> - Failed (%d) to find %s -> %@\n"),
85 ret
, this->name_
, tmp
));
89 if (tmp
->type () != 0)
91 // Something has registered a proper (non-forward-decl) service with
92 // the same name as our dummy.
95 ACELIB_DEBUG ((LM_DEBUG
,
96 ACE_TEXT ("ACE (%P|%t) STDG::<dtor>, repo=%@ [%d], ")
97 ACE_TEXT ("name=%s - updating dependents [%d - %d)\n"),
102 this->repo_
.current_size ()));
104 // Relocate any services inserted since we were created.
105 // Any (static, i.e. DLL = 0) services registered in
106 // the context of this guard aren't really static because
107 // their code belongs in the DLL's code segment
108 this->repo_
.relocate_i (this->repo_begin_
, this->repo_
.current_size (), tmp
->dll());
111 ACELIB_DEBUG ((LM_DEBUG
,
112 ACE_TEXT ("ACE (%P|%t) STDG::<dtor>, repo=%@ [%d], ")
113 ACE_TEXT ("name=%s - loaded (type=%@, impl=%@, object=%@, active=%d)\n"),
119 tmp
->type ()->object (),
125 // ----------------------------------------
127 ACE_Service_Gestalt::Processed_Static_Svc::
128 Processed_Static_Svc (const ACE_Static_Svc_Descriptor
*assd
)
132 #if defined (ACE_HAS_ALLOC_HOOKS)
133 ACE_ALLOCATOR_NORETURN (name_
, static_cast<ACE_TCHAR
*>(ACE_Allocator::instance()->malloc (sizeof(ACE_TCHAR
) * (ACE_OS::strlen(assd
->name_
)+1))));
135 ACE_NEW_NORETURN (name_
, ACE_TCHAR
[ACE_OS::strlen(assd
->name_
)+1]);
136 #endif /* ACE_HAS_ALLOC_HOOKS */
137 ACE_OS::strcpy(name_
,assd
->name_
);
140 ACE_Service_Gestalt::Processed_Static_Svc::~Processed_Static_Svc ()
142 #if defined (ACE_HAS_ALLOC_HOOKS)
143 ACE_Allocator::instance()->free(name_
);
146 #endif /* ACE_HAS_ALLOC_HOOKS */
149 ACE_ALLOC_HOOK_DEFINE(ACE_Service_Gestalt::Processed_Static_Svc
)
152 ACE_Service_Gestalt::intrusive_add_ref (ACE_Service_Gestalt
* g
)
157 ACE_ASSERT (g
->refcnt_
> 0);
162 ACE_Service_Gestalt::intrusive_remove_ref (ACE_Service_Gestalt
* g
)
166 long tmp
= --g
->refcnt_
;
167 if (tmp
<= 0) delete g
;
168 ACE_ASSERT (tmp
>= 0);
173 ACE_Service_Gestalt::~ACE_Service_Gestalt ()
175 if (this->svc_repo_is_owned_
)
180 delete this->static_svcs_
;
181 this->static_svcs_
= 0;
183 // Delete the dynamically allocated static_svcs instance.
186 ACELIB_DEBUG ((LM_DEBUG
,
187 ACE_TEXT ("ACE (%P|%t) SG::~SG - this=%@, pss = %@\n"),
188 this, this->processed_static_svcs_
));
191 if (this->processed_static_svcs_
&&
192 !this->processed_static_svcs_
->is_empty())
194 Processed_Static_Svc
**pss
= 0;
195 for (ACE_PROCESSED_STATIC_SVCS_ITERATOR
iter (*this->processed_static_svcs_
);
196 iter
.next (pss
) != 0;
203 delete this->processed_static_svcs_
;
204 this->processed_static_svcs_
= 0;
206 delete this->svc_conf_file_queue_
;
207 this->svc_conf_file_queue_
= 0;
209 delete this->svc_queue_
;
210 this->svc_queue_
= 0;
213 ACE_Service_Gestalt::ACE_Service_Gestalt (size_t size
,
214 bool svc_repo_is_owned
,
216 : svc_repo_is_owned_ (svc_repo_is_owned
)
217 , svc_repo_size_ (size
)
219 , logger_key_ (ACE_DEFAULT_LOGGER_KEY
)
220 , no_static_svcs_ (no_static_svcs
)
222 , svc_conf_file_queue_ (0)
225 , processed_static_svcs_ (0)
228 (void)this->init_i ();
232 ACELIB_DEBUG ((LM_DEBUG
,
233 ACE_TEXT ("ACE (%P|%t) SG::ctor - this = %@, pss = %@\n"),
234 this, this->processed_static_svcs_
));
238 /// Performs the common initialization tasks for a new or previously
239 /// closed instance. Must not be virtual, as it is also called from
242 ACE_Service_Gestalt::init_i ()
244 // Only initialize the repo_ if (a) we are being constructed, or;
245 // (b) we're being open()-ed, perhaps after previously having been
246 // close()-ed. In both cases: repo_ == 0 and we need a repository.
247 if (this->repo_
== 0)
249 if (this->svc_repo_is_owned_
)
251 ACE_NEW_RETURN (this->repo_
,
252 ACE_Service_Repository (this->svc_repo_size_
),
258 ACE_Service_Repository::instance (this->svc_repo_size_
);
262 if (init_svc_conf_file_queue () == -1)
269 /// Add the default statically-linked services to the Service
272 ACE_Service_Gestalt::load_static_svcs ()
274 ACE_TRACE ("ACE_Service_Gestalt::load_static_svcs");
276 if (this->static_svcs_
== 0)
277 return 0; // Nothing to do
279 ACE_Static_Svc_Descriptor
**ssdp
= 0;
280 for (ACE_STATIC_SVCS_ITERATOR
iter (*this->static_svcs_
);
281 iter
.next (ssdp
) != 0;
284 ACE_Static_Svc_Descriptor
*ssd
= *ssdp
;
286 if (this->process_directive (*ssd
, 1) == -1)
290 } /* load_static_svcs () */
293 /// Find a static service descriptor by name
295 ACE_Service_Gestalt::find_static_svc_descriptor (const ACE_TCHAR
* name
,
296 ACE_Static_Svc_Descriptor
**ssd
) const
298 ACE_TRACE ("ACE_Service_Gestalt::find_static_svc_descriptor");
300 if (this->static_svcs_
== 0)
303 ACE_Static_Svc_Descriptor
**ssdp
= 0;
304 for (ACE_STATIC_SVCS_ITERATOR
iter ( *this->static_svcs_
);
305 iter
.next (ssdp
) != 0;
308 if (ACE_OS::strcmp ((*ssdp
)->name_
, name
) == 0)
321 const ACE_Static_Svc_Descriptor
*
322 ACE_Service_Gestalt::find_processed_static_svc (const ACE_TCHAR
* name
)
324 if (this->processed_static_svcs_
== 0 || name
== 0)
327 Processed_Static_Svc
**pss
= 0;
328 for (ACE_PROCESSED_STATIC_SVCS_ITERATOR
iter (*this->processed_static_svcs_
);
329 iter
.next (pss
) != 0;
332 if (ACE_OS::strcmp ((*pss
)->name_
, name
) == 0)
333 return (*pss
)->assd_
;
339 /// @brief Captures a list of the direcives processed (explicitely) for this
340 /// Gestalt so that services can be replicated in other repositories
341 /// upon their first initialization.
343 /// This is part of the mechanism ensuring distinct local instances
344 /// for static service objects, loaded in another repository.
346 ACE_Service_Gestalt::add_processed_static_svc
347 (const ACE_Static_Svc_Descriptor
*assd
)
349 /// When process_directive(Static_Svc_Descriptor&) is called, it
350 /// associates a service object with the Gestalt and makes the
351 /// resource (a Service Object) local to the repository. This is but
352 /// the first step in using such SO. The next is the
353 /// "initialization" step. It is typically done through a "static"
354 /// service configuration directive.
356 /// In contrast a "dynamic" directive, when processed through the
357 /// overloaded process_directives(string) both creates the SO
358 /// locally and initializes it, where the statics directive must
359 /// first locate the SO and then calls the init() method. This means
360 /// that durig the "static" initialization there's no specific
361 /// information about the hosting repository and the gestalt must
362 /// employ some lookup strategy to find it elsewhere.
364 if (this->processed_static_svcs_
== 0)
365 ACE_NEW (this->processed_static_svcs_
,
366 ACE_PROCESSED_STATIC_SVCS
);
368 Processed_Static_Svc
**pss
= 0;
369 for (ACE_PROCESSED_STATIC_SVCS_ITERATOR
iter (*this->processed_static_svcs_
);
370 iter
.next (pss
) != 0;
373 if (ACE_OS::strcmp ((*pss
)->name_
, assd
->name_
) == 0)
375 (*pss
)->assd_
= assd
;
379 Processed_Static_Svc
*tmp
= 0;
380 ACE_NEW (tmp
,Processed_Static_Svc(assd
));
381 this->processed_static_svcs_
->insert(tmp
);
384 ACELIB_DEBUG ((LM_DEBUG
,
385 ACE_TEXT ("ACE (%P|%t) SG::add_processed_static_svc, ")
386 ACE_TEXT ("repo=%@ - %s\n"),
392 /// Queues a static service object descriptor which, during open()
393 /// will be given to process_directive() to create the Service
394 /// Object. Normally, only called from static initializers, prior to
395 /// calling open() but loading a service from a DLL can cause it too.
398 ACE_Service_Gestalt::insert (ACE_Static_Svc_Descriptor
*stsd
)
400 if (this->static_svcs_
== 0)
401 ACE_NEW_RETURN (this->static_svcs_
,
405 return this->static_svcs_
->insert (stsd
);
409 ACE_ALLOC_HOOK_DEFINE (ACE_Service_Gestalt
)
413 ACE_Service_Gestalt::dump () const
415 #if defined (ACE_HAS_DUMP)
416 ACE_TRACE ("ACE_Service_Gestalt::dump");
417 #endif /* ACE_HAS_DUMP */
421 ACE_Service_Gestalt::initialize (const ACE_TCHAR
*svc_name
,
422 const ACE_TCHAR
*parameters
)
424 ACE_TRACE ("ACE_Service_Gestalt_Base::initialize (repo)");
425 ACE_ARGV
args (parameters
);
430 ACELIB_DEBUG ((LM_DEBUG
,
431 ACE_TEXT ("ACE (%P|%t) SG::initialize - () repo=%@, ")
432 ACE_TEXT ("looking up static ")
433 ACE_TEXT ("service \'%s\' to initialize\n"),
439 const ACE_Service_Type
*srp
= 0;
440 for (int i
= 0; this->find (svc_name
, &srp
) == -1 && i
< 2; i
++)
441 // if (this->repo_->find (svc_name, &srp) == -1)
443 const ACE_Static_Svc_Descriptor
*assd
=
444 ACE_Service_Config::global()->find_processed_static_svc(svc_name
);
447 this->process_directive_i(*assd
, 0);
451 ACELIB_ERROR_RETURN ((LM_ERROR
,
452 ACE_TEXT ("ACE (%P|%t) ERROR: SG::initialize - service \'%s\'")
453 ACE_TEXT (" was not located.\n"),
459 ACELIB_ERROR_RETURN ((LM_ERROR
,
460 ACE_TEXT ("ACE (%P|%t) ERROR: SG::initialize - service \'%s\'")
461 ACE_TEXT (" was not located.\n"),
465 /// If initialization fails ...
466 if (srp
->type ()->init (args
.argc (),
469 // ... report and remove this entry.
470 ACELIB_ERROR ((LM_ERROR
,
471 ACE_TEXT ("ACE (%P|%t) ERROR: SG::initialize - static init of \'%s\'")
472 ACE_TEXT (" failed (%p)\n"),
473 svc_name
, ACE_TEXT ("error")));
474 this->repo_
->remove (svc_name
);
478 // If everything is ok, activate it
479 const_cast<ACE_Service_Type
*>(srp
)->active (1);
484 #if (ACE_USES_CLASSIC_SVC_CONF == 1)
486 ACE_Service_Gestalt::initialize (const ACE_Service_Type_Factory
*stf
,
487 const ACE_TCHAR
*parameters
)
489 ACE_TRACE ("ACE_Service_Gestalt::initialize");
493 ACELIB_DEBUG ((LM_DEBUG
,
494 ACE_TEXT ("ACE (%P|%t) SG::initialize - repo=%@, name=%s")
495 ACE_TEXT (" - looking up in the repo\n"),
500 ACE_Service_Type
*srp
= 0;
501 int const retv
= this->repo_
->find (stf
->name (),
502 (const ACE_Service_Type
**) &srp
);
504 // If there is an active service already, remove it first
505 // before it can be re-installed.
510 ACELIB_DEBUG ((LM_WARNING
,
511 ACE_TEXT ("ACE (%P|%t) SG::initialize - repo=%@,")
512 ACE_TEXT (" name=%s - removing a pre-existing namesake.\n"),
516 this->repo_
->remove (stf
->name ());
519 // If there is an inactive service by that name it may have been
520 // either inactivated, or just a forward declaration for a service,
521 // that is in the process of being initialized. If it is the latter,
522 // then we have detected an attempt to initialize the same dynamic
523 // service while still processing previous attempt. This can lock up
524 // the process, because the ACE_DLL_Manager::open () is not
525 // re-entrant - it uses a Singleton lock to serialize concurent
526 // invocations. This use case must be handled here, because if the
527 // DLL_Manager was re-entrant we would have entered an infinite
529 if (retv
== -2 && srp
->type () == 0)
530 ACELIB_ERROR_RETURN ((LM_WARNING
,
531 ACE_TEXT ("ACE (%P|%t) SG::initialize - repo=%@,")
532 ACE_TEXT (" name=%s - forward-declared; ")
533 ACE_TEXT (" recursive initialization requests are")
534 ACE_TEXT (" ignored.\n"),
539 // Reserve a spot for the dynamic service by inserting an incomplete
540 // service declaration, i.e. one that can not produce a service
541 // object if asked (a forward declaration). This declaration
542 // ensures maintaining the proper partial ordering of the services
543 // with respect to their finalization. For example, dependent static
544 // services must be registered *after* the dynamic service that
545 // loads them, so that their finalization is complete *before*
546 // finalizing the dynamic service.
547 ACE_Service_Type_Dynamic_Guard
dummy (*this->repo_
,
550 // make_service_type() is doing the dynamic loading and also runs
551 // any static initializers
552 std::unique_ptr
<ACE_Service_Type
> tmp (stf
->make_service_type (this));
554 if (tmp
.get () != 0 &&
555 this->initialize_i (tmp
.get (), parameters
) == 0)
557 // All good. Tthe ACE_Service_Type instance is now owned by the
558 // repository and we should make sure it is not destroyed upon
559 // exit from this method.
566 #endif /* (ACE_USES_CLASSIC_SVC_CONF == 1) */
569 /// Dynamically link the shared object file and retrieve a pointer to
570 /// the designated shared object in this file.
571 /// @note This is obsolete (and error-prone) in the presense of dynamic
572 /// services with their own static services. This method will allow those
573 /// static services to register *before* the dynamic service that owns them.
574 /// Upon finalization of the static services the process may crash, because
575 /// the dynamic service's DLL may have been already released, together with
576 /// the memory in which the static services reside.
577 /// It may not crash, for instance, when the first static service to register
578 /// is the same as the dynamic service being loaded. You should be so lucky! ..
580 ACE_Service_Gestalt::initialize (const ACE_Service_Type
*sr
,
581 const ACE_TCHAR
*parameters
)
583 ACE_TRACE ("ACE_Service_Gestalt::initialize");
586 ACELIB_DEBUG ((LM_DEBUG
,
587 ACE_TEXT ("ACE (%P|%t) SG::initialize - repo=%@, name=%s")
588 ACE_TEXT (" - looking up in the repo\n"),
592 ACE_Service_Type
*srp
= 0;
593 if (this->repo_
->find (sr
->name (),
594 (const ACE_Service_Type
**) &srp
) >= 0)
597 ACELIB_DEBUG ((LM_WARNING
,
598 ACE_TEXT ("ACE (%P|%t) SG::initialize - repo=%@, name=%s")
599 ACE_TEXT (" - removing a pre-existing namesake.\n"),
603 this->repo_
->remove (sr
->name ());
606 return this->initialize_i (sr
, parameters
);
609 /// Dynamically link the shared object file and retrieve a pointer to
610 /// the designated shared object in this file.
612 ACE_Service_Gestalt::initialize_i (const ACE_Service_Type
*sr
,
613 const ACE_TCHAR
*parameters
)
615 ACE_TRACE ("ACE_Service_Gestalt::initialize_i");
616 ACE_ARGV
args (parameters
);
617 if (sr
->type ()->init (args
.argc (),
620 // We just get ps to avoid having remove() delete it.
621 ACE_Service_Type
*ps
= 0;
622 this->repo_
->remove (sr
->name (), &ps
);
625 // Not using LM_ERROR here to avoid confusing the test harness
627 ACELIB_ERROR_RETURN ((LM_WARNING
,
628 ACE_TEXT ("ACE (%P|%t) SG::initialize_i -")
629 ACE_TEXT (" repo=%@, name=%s - remove failed: %m\n"),
637 if (this->repo_
->insert (sr
) == -1)
640 // Not using LM_ERROR here to avoid confusing the test harness
642 ACELIB_ERROR_RETURN ((LM_WARNING
,
643 ACE_TEXT ("ACE (%P|%t) SG::initialize_i -")
644 ACE_TEXT (" repo=%@, name=%s - insert failed: %m\n"),
655 // Totally remove <svc_name> from the daemon by removing it from the
656 // ACE_Reactor, and unlinking it if necessary.
659 ACE_Service_Gestalt::remove (const ACE_TCHAR svc_name
[])
661 ACE_TRACE ("ACE_Service_Gestalt::remove");
662 if (this->repo_
== 0)
665 return this->repo_
->remove (svc_name
);
668 /// Suspend @a svc_name. Note that this will not unlink the service
669 /// from the daemon if it was dynamically linked, it will mark it as
670 /// being suspended in the Service Repository and call the <suspend>
671 /// member function on the appropriate <ACE_Service_Object>. A service
672 /// can be resumed later on by calling the <resume> method...
674 ACE_Service_Gestalt::suspend (const ACE_TCHAR svc_name
[])
676 ACE_TRACE ("ACE_Service_Gestalt::suspend");
677 if (this->repo_
== 0)
680 return this->repo_
->suspend (svc_name
);
683 // Resume a SVC_NAME that was previously suspended or has not yet
684 // been resumed (e.g., a static service).
687 ACE_Service_Gestalt::resume (const ACE_TCHAR svc_name
[])
689 ACE_TRACE ("ACE_Service_Gestalt::resume");
690 if (this->repo_
== 0)
693 return this->repo_
->resume (svc_name
);
698 ACE_Service_Gestalt::process_directive (const ACE_Static_Svc_Descriptor
&ssd
,
701 int const result
= process_directive_i (ssd
, force_replace
);
704 this->add_processed_static_svc(&ssd
);
710 ACE_Service_Gestalt::process_directive_i (const ACE_Static_Svc_Descriptor
&ssd
,
713 if (this->repo_
== 0)
718 if (this->repo_
->find (ssd
.name_
, 0, 0) >= 0)
720 // The service is already there, just return
726 ACE_Service_Object_Exterminator gobbler
;
727 void *sym
= (ssd
.alloc_
)(&gobbler
);
729 ACE_Service_Type_Impl
*stp
=
730 ACE_Service_Config::create_service_type_impl (ssd
.name_
,
738 ACE_Service_Type
*service_type
= 0;
740 // This is just a temporary to force the compiler to use the right
741 // constructor in ACE_Service_Type. Note that, in cases where we are
742 // called from a static initializer which is part of a DLL, there is
743 // not enough information about the actuall DLL in this context.
746 ACE_NEW_RETURN (service_type
,
747 ACE_Service_Type (ssd
.name_
,
755 ACELIB_DEBUG ((LM_DEBUG
,
756 ACE_TEXT ("ACE (%P|%t) SG::process_directive_i, ")
757 ACE_TEXT ("repo=%@ - %s, dll=%s, force=%d\n"),
760 (tmp_dll
.dll_name_
== 0) ? ACE_TEXT ("<null>") : tmp_dll
.dll_name_
,
764 return this->repo_
->insert (service_type
);
767 #if (ACE_USES_CLASSIC_SVC_CONF == 1)
770 ACE_Service_Gestalt::process_directives_i (ACE_Svc_Conf_Param
*param
)
774 ACELIB_DEBUG ((LM_DEBUG
,
775 ACE_TEXT ("ACE (%P|%t) SG::process_directives_i, ")
776 ACE_TEXT ("repo=%@ - %s\n"),
778 (param
->type
== ACE_Svc_Conf_Param::SVC_CONF_FILE
)
779 ? ACE_TEXT ("<from file>")
780 : param
->source
.directive
));
783 // Were we called in the context of the current instance?
784 ACE_ASSERT (this == param
->config
);
786 // Temporarily (for the duration of this call) make sure that *any*
787 // static service registrations will happen with this instance. Such
788 // registrations are possible as a side-effect of dynamically
789 // loading a DLL, which has other static services registered. Thus
790 // this instance will own both the DLL and those static services,
791 // which implies that their finalization will be performed in the
792 // correct order, i.e. prior to finalizing the DLL
793 ACE_Service_Config_Guard
guard (this);
795 ::ace_yyparse (param
);
797 // This is a hack, better errors should be provided...
798 if (param
->yyerrno
> 0)
800 // Always set the last error if ace_yyparse() fails.
801 // Other code may use errno to determine the type
802 // of problem that occurred from processing directives.
803 ACE_OS::last_error (EINVAL
);
804 return param
->yyerrno
;
813 ACE_Service_Gestalt::get_xml_svc_conf (ACE_DLL
&xmldll
)
815 if (xmldll
.open (ACE_TEXT ("ACEXML_XML_Svc_Conf_Parser")) == -1)
816 ACELIB_ERROR_RETURN ((LM_ERROR
,
817 ACE_TEXT ("ACE (%P|%t) Failure to open ACEXML_XML_Svc_Conf_Parser: %p\n"),
818 ACE_TEXT("ACE_Service_Config::get_xml_svc_conf")),
821 void * foo
= xmldll
.symbol (ACE_TEXT ("_ACEXML_create_XML_Svc_Conf_Object"));
822 intptr_t const temp_p
= reinterpret_cast<intptr_t> (foo
);
824 ACE_XML_Svc_Conf::Factory factory
= reinterpret_cast<ACE_XML_Svc_Conf::Factory
> (temp_p
);
827 ACELIB_ERROR_RETURN ((LM_ERROR
,
828 ACE_TEXT ("ACE (%P|%t) Unable to resolve factory: %p\n"),
834 #endif /* ACE_USES_CLASSIC_SVC_CONF == 1 */
837 ACE_Service_Gestalt::process_file (const ACE_TCHAR file
[])
839 ACE_TRACE ("ACE_Service_Gestalt::process_file");
841 // To avoid recursive processing of the same file and the same repository
842 // we maintain an implicit stack of dummy "services" named after the file
843 // being processed. Anytime we have to open a new file, we then can check
844 // to see if it is not already being processed by searching for a dummy
845 // service with a matching name.
846 if (this->repo_
->find (file
, 0, 0) >=0)
848 ACELIB_DEBUG ((LM_WARNING
,
849 ACE_TEXT ("ACE (%P|%t) Configuration file %s is currently")
850 ACE_TEXT (" being processed. Ignoring recursive process_file().\n"),
855 // Register a dummy service as a forward decl, using the file name as name.
856 // The entry will be automaticaly removed once the thread exits this block.
857 ACE_Service_Type_Dynamic_Guard
recursion_guard (*this->repo_
,
861 * @TODO: Test with ACE_USES_CLASSIC_SVC_CONF turned off!
863 #if (ACE_USES_CLASSIC_SVC_CONF == 1)
866 FILE *fp
= ACE_OS::fopen (file
,
871 // Invalid svc.conf file. We'll report it here and break out of
874 ACELIB_DEBUG ((LM_ERROR
,
875 ACE_TEXT ("ACE (%P|%t): %p\n"),
878 // Use stat to find out if the file exists. I didn't use access()
879 // because stat is better supported on most non-unix platforms.
881 if (ACE_OS::stat (file
, &exists
) == 0)
882 // If it exists, but we couldn't open it for reading then we
883 // must not have permission to read it.
891 ACE_Svc_Conf_Param
f (this, fp
);
893 // Keep track of the number of errors.
894 result
= this->process_directives_i (&f
);
896 (void) ACE_OS::fclose (fp
);
902 std::unique_ptr
<ACE_XML_Svc_Conf
> xml_svc_conf (this->get_xml_svc_conf (dll
));
904 if (xml_svc_conf
.get () == 0)
907 return xml_svc_conf
->parse_file (file
);
908 #endif /* ACE_USES_CLASSIC_SVC_CONF == 1 */
912 ACE_Service_Gestalt::process_directive (const ACE_TCHAR directive
[])
914 ACE_TRACE ("ACE_Service_Gestalt::process_directive");
918 ACELIB_DEBUG ((LM_DEBUG
,
919 ACE_TEXT ("ACE (%P|%t) SG::process_directive, repo=%@ - %s\n"),
924 #if (ACE_USES_CLASSIC_SVC_CONF == 1)
925 ACE_UNUSED_ARG (directive
);
927 ACE_Svc_Conf_Param
d (this, directive
);
929 return this->process_directives_i (&d
);
933 std::unique_ptr
<ACE_XML_Svc_Conf
> xml_svc_conf (this->get_xml_svc_conf (dll
));
935 if (xml_svc_conf
.get () == 0)
938 // Temporarily (for the duration of this call) make sure that *any* static
939 // service registrations will happen with this instance. Such registrations
940 // are possible as a side-effect of dynamically loading a DLL, which has
941 // other static services registered. Thus this instance will own both the
942 // DLL and those static services, which implies that their finalization
943 // will be performed in the correct order, i.e. prior to finalizing the DLL
944 ACE_Service_Config_Guard
guard (this);
946 return xml_svc_conf
->parse_string (directive
);
947 #endif /* ACE_USES_CLASSIC_SVC_CONF == 1 */
949 } /* process_directive () */
953 ACE_Service_Gestalt::init_svc_conf_file_queue ()
955 if (this->svc_conf_file_queue_
== 0)
957 ACE_SVC_QUEUE
*tmp
= 0;
961 this->svc_conf_file_queue_
= tmp
;
966 ACELIB_DEBUG ((LM_DEBUG
,
967 ACE_TEXT ("ACE (%P|%t) SG::init_svc_conf_file_queue ")
968 ACE_TEXT ("- this=%@, repo=%@\n"),
973 } /* init_svc_conf_file_queue () */
977 ACE_Service_Gestalt::open_i (const ACE_TCHAR program_name
[],
978 const ACE_TCHAR
* logger_key
,
979 bool ignore_static_svcs
,
980 bool ignore_default_svc_conf_file
,
981 bool ignore_debug_flag
)
983 ACE_TRACE ("ACE_Service_Gestalt::open_i");
985 ACE_Log_Msg
*log_msg
= ACE_LOG_MSG
;
987 this->no_static_svcs_
= ignore_static_svcs
;
989 // Record the current log setting upon entering this thread.
990 u_long old_process_mask
= log_msg
->priority_mask
991 (ACE_Log_Msg::PROCESS
);
993 u_long old_thread_mask
= log_msg
->priority_mask
994 (ACE_Log_Msg::THREAD
);
998 ACELIB_DEBUG ((LM_DEBUG
,
999 ACE_TEXT ("ACE (%P|%t) SG::open_i - this=%@, ")
1000 ACE_TEXT ("opened=%d, loadstatics=%d\n"),
1001 this, this->is_opened_
, this->no_static_svcs_
));
1004 // Guard against reentrant processing. For example,
1005 // if the singleton gestalt (ubergestalt) was already open,
1006 // do not open it again...
1007 if (this->is_opened_
++ != 0)
1010 if (this->init_i () != 0)
1013 u_long flags
= log_msg
->flags ();
1015 // Only use STDERR if the caller hasn't already set the flags.
1017 flags
= (u_long
) ACE_Log_Msg::STDERR
;
1019 const ACE_TCHAR
*key
= logger_key
;
1021 if (key
== 0 || ACE_OS::strcmp (key
, ACE_DEFAULT_LOGGER_KEY
) == 0)
1023 // Only use the static <logger_key_> if the caller doesn't
1024 // override it in the parameter list or if the key supplied is
1025 // equal to the default static logger key.
1026 key
= this->logger_key_
;
1030 ACE_SET_BITS (flags
, ACE_Log_Msg::LOGGER
);
1033 if (log_msg
->open (program_name
,
1038 if (!ignore_debug_flag
)
1040 // If -d was included as a startup parameter, the user wants debug
1041 // information printed during service initialization.
1043 ACE_Log_Msg::enable_debug_messages ();
1045 // The user has requested no debugging info.
1046 ACE_Log_Msg::disable_debug_messages ();
1049 if (!ignore_default_svc_conf_file
)
1051 bool add_default
= true;
1052 bool has_files
= this->svc_conf_file_queue_
&&
1053 !this->svc_conf_file_queue_
->is_empty ();
1054 bool has_cmdline
= this->svc_queue_
&& !this->svc_queue_
->is_empty ();
1055 if (has_files
|| has_cmdline
)
1057 // check if default file is already listed
1058 ACE_TString
*sptr
= 0;
1059 ACE_TString
default_svc_conf (ACE_DEFAULT_SVC_CONF
);
1061 for (ACE_SVC_QUEUE_ITERATOR
iter (*this->svc_conf_file_queue_
);
1062 iter
.next (sptr
) != 0 && add_default
;
1065 add_default
= (*sptr
!= default_svc_conf
);
1070 FILE *fp
= ACE_OS::fopen (ACE_DEFAULT_SVC_CONF
, ACE_TEXT ("r"));
1074 add_default
= false;
1078 // Load the default "svc.conf" entry. here if there weren't
1079 // overriding -f arguments in <parse_args>.
1080 if (add_default
&& svc_conf_file_queue_
->enqueue_head
1081 (ACE_TString (ACE_DEFAULT_SVC_CONF
)) == -1)
1084 ACELIB_ERROR_RETURN ((LM_ERROR
,
1086 ACE_TEXT ("enqueuing ")
1087 ACE_DEFAULT_SVC_CONF
1093 // See if we need to load the static services.
1094 if (this->no_static_svcs_
== 0
1095 && this->load_static_svcs () == -1)
1099 result
= this->process_directives ();
1102 int temp
= this->process_commandline_directives ();
1105 else result
+= temp
;
1109 // Reset debugging back to the way it was when we came into
1112 // Make sure to save/restore errno properly.
1113 ACE_Errno_Guard
error (errno
);
1115 if (!ignore_debug_flag
)
1117 log_msg
->priority_mask (old_process_mask
, ACE_Log_Msg::PROCESS
);
1118 log_msg
->priority_mask (old_thread_mask
, ACE_Log_Msg::THREAD
);
1127 ACE_Service_Gestalt::is_opened ()
1129 return this->is_opened_
;
1133 ACE_Service_Gestalt::process_commandline_directives ()
1136 if (this->svc_queue_
!= 0)
1138 ACE_TString
*sptr
= 0;
1139 for (ACE_SVC_QUEUE_ITERATOR
iter (*this->svc_queue_
);
1140 iter
.next (sptr
) != 0;
1143 // Process just a single directive.
1144 if (this->process_directive ((sptr
->fast_rep ())) != 0)
1146 ACELIB_ERROR ((LM_ERROR
,
1147 ACE_TEXT ("ACE (%P|%t) %p\n"),
1148 ACE_TEXT ("process_directive")));
1153 delete this->svc_queue_
;
1154 this->svc_queue_
= 0;
1158 } /* process_commandline_directives () */
1162 ACE_Service_Gestalt::parse_args (int argc
, ACE_TCHAR
*argv
[])
1164 ACE_TRACE ("ACE_Service_Gestalt::parse_args");
1165 bool unused_ignore_default_svc_conf
= true;
1166 return parse_args_i (argc
, argv
, unused_ignore_default_svc_conf
);
1170 ACE_Service_Gestalt::parse_args_i (int argc
,
1172 bool &ignore_default_svc_conf_file
)
1174 ACE_TRACE ("ACE_Service_Gestalt::parse_args_i");
1175 ACE_Get_Opt
get_opt (argc
,
1177 ACE_TEXT ("df:k:nyS:"),
1178 1); // Start at argv[1].
1180 if (this->init_svc_conf_file_queue () == -1)
1183 for (int c
; (argc
!= 0) && ((c
= get_opt ()) != -1); )
1190 if (this->svc_conf_file_queue_
->enqueue_tail (ACE_TString (get_opt
.opt_arg ())) == -1)
1191 ACELIB_ERROR_RETURN ((LM_ERROR
,
1193 ACE_TEXT ("enqueue_tail")),
1195 ignore_default_svc_conf_file
= true;
1199 * @TODO: Is this always a static storage? Shouldn't we copy
1200 * & gain ownership of the value?
1202 this->logger_key_
= get_opt
.opt_arg ();
1205 this->no_static_svcs_
= 1;
1208 this->no_static_svcs_
= 0;
1211 if (this->svc_queue_
== 0)
1213 ACE_NEW_RETURN (this->svc_queue_
,
1218 if (this->svc_queue_
->enqueue_tail (ACE_TString (get_opt
.opt_arg ())) == -1)
1219 ACELIB_ERROR_RETURN ((LM_ERROR
,
1221 ACE_TEXT ("enqueue_tail")),
1226 ACELIB_DEBUG ((LM_DEBUG
,
1227 ACE_TEXT ("ACE (%P|%t) %c is not a ACE_Service_Config option\n"),
1232 } /* parse_args_i () */
1235 // Process service configuration directives from the files queued for
1238 ACE_Service_Gestalt::process_directives (bool )
1240 ACE_TRACE ("ACE_Service_Gestalt::process_directives");
1241 if (this->svc_conf_file_queue_
== 0
1242 || this->svc_conf_file_queue_
->is_empty ())
1247 ACE_TString
*sptr
= 0;
1250 // Iterate through all the svc.conf files.
1251 for (ACE_SVC_QUEUE_ITERATOR
iter (*this->svc_conf_file_queue_
);
1252 iter
.next (sptr
) != 0;
1255 int result
= this->process_file (sptr
->fast_rep ());
1262 } /* process_directives () */
1264 // Tidy up and perform last rites on a terminating ACE_Service_Gestalt.
1266 ACE_Service_Gestalt::close ()
1268 ACE_TRACE ("ACE_Service_Gestalt::close");
1270 if (!this->is_opened_
|| --this->is_opened_
!= 0)
1273 // Delete the list fo svc.conf files
1274 delete this->svc_conf_file_queue_
;
1275 this->svc_conf_file_queue_
= 0;
1277 if (this->processed_static_svcs_
&&
1278 !this->processed_static_svcs_
->is_empty())
1280 Processed_Static_Svc
**pss
= 0;
1281 for (ACE_PROCESSED_STATIC_SVCS_ITERATOR
iter (*this->processed_static_svcs_
);
1282 iter
.next (pss
) != 0;
1288 delete this->processed_static_svcs_
;
1289 this->processed_static_svcs_
= 0;
1291 #ifndef ACE_NLOGGING
1293 ACELIB_DEBUG ((LM_DEBUG
,
1294 ACE_TEXT ("ACE (%P|%t) SG::close - complete this=%@, repo=%@, owned=%d\n"),
1295 this, this->repo_
, this->svc_repo_is_owned_
));
1298 if (this->svc_repo_is_owned_
)
1307 ACE_END_VERSIONED_NAMESPACE_DECL
1309 #if !defined (__ACE_INLINE__)
1310 #include "ace/Service_Gestalt.inl"
1311 #endif /* __ACE_INLINE__ */
1313 // Allocate a Service Manager.
1314 ACE_FACTORY_DEFINE (ACE
, ACE_Service_Manager
)