2 #include "ace/Get_Opt.h"
3 #include "ace/Handle_Set.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_fcntl.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_sys_resource.h"
8 #include "ace/streams.h"
10 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("server.ior");
12 class Test_i
: public virtual POA_Test
15 Test_i (CORBA::ORB_ptr orb
)
16 : orb_ (CORBA::ORB::_duplicate (orb
))
26 this->orb_
->shutdown ();
41 for (size_t i
= 0; i
< 0xffff; i
++)
43 this->openfds_
[i
] = ACE_INVALID_HANDLE
;
49 cout
<< "Server: closing " << (this->max_close_
- this->min_close_
) + 1
51 for (size_t i
= this->min_close_
; i
<= this->max_close_
; i
++)
53 ACE_OS::close (this->openfds_
[i
]);
58 void leak (const ACE_TCHAR
* file
)
60 for (size_t i
= 0; i
< 0xffff; i
++)
62 this->openfds_
[i
] = ACE_OS::open (file
, O_RDONLY
);
65 #if defined (ACE_WIN32)
66 // the test is not valid on windows so just wing this value
67 this->min_close_
= 1000;
69 this->min_close_
= (ACE_DEFAULT_SELECT_REACTOR_SIZE
- 2)
70 - (this->openfds_
[i
] - 1);
71 #endif /* ACE_WIN32 */
72 cout
<< "Server: first leaked handle is "
73 << this->openfds_
[i
] << " min_close is "
74 << this->min_close_
<< endl
;
77 if (this->openfds_
[i
] == ACE_INVALID_HANDLE
)
79 cout
<< "Server: last handle encounterd at i = " << i
<< endl
;
84 cout
<< "Server: Descriptors::leak did not saturate fdset" << endl
;
93 ACE_HANDLE openfds_
[0xffff];
100 parse_args (int argc
, ACE_TCHAR
*argv
[])
102 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:"));
105 while ((c
= get_opts ()) != -1)
109 ior_output_file
= get_opts
.opt_arg ();
114 ACE_ERROR_RETURN ((LM_ERROR
,
121 // Indicates successful parsing of the command line
126 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
128 #if defined (ACE_WIN32)
129 ACE_UNUSED_ARG (argc
);
130 ACE_UNUSED_ARG (argv
);
131 cout
<< "HandleExhaustion test not available on Windows" << endl
;
135 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
136 // We must make sure that our file descriptor limit does not exceed
137 // the size allowed (in the fd set) by the reactor. If it does, this
138 // test will fail (in a different way than expected) which is a
139 // completely different bug than this test is designed to address
142 // We must also make sure that this happens before creating the first
143 // ORB. Otherwise, the reactor will be created with a maximum size of
144 // the current rlimit for file desriptors (which will later on be
147 if (ACE_OS::getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
149 cout
<< "server evaluating rlimit, cur = "
150 << rlim
.rlim_cur
<< " max = " << rlim
.rlim_max
152 << ACE_DEFAULT_SELECT_REACTOR_SIZE
<< endl
;
153 if (rlim
.rlim_cur
< static_cast<rlim_t
> (ACE_DEFAULT_SELECT_REACTOR_SIZE
) &&
154 rlim
.rlim_max
> static_cast<rlim_t
> (ACE_DEFAULT_SELECT_REACTOR_SIZE
))
156 rlim
.rlim_cur
= ACE_DEFAULT_SELECT_REACTOR_SIZE
;
157 rlim
.rlim_max
= ACE_DEFAULT_SELECT_REACTOR_SIZE
;
158 ACE_OS::setrlimit(RLIMIT_NOFILE
, &rlim
);
159 cout
<< "server set rlimit_nofile" << endl
;
163 cout
<< "server does not support setting rlimit, reactor max = "
164 << ACE_DEFAULT_SELECT_REACTOR_SIZE
<< endl
;
165 #endif /* !ACE_LACKS_RLIMIT && RLIMIT_NOFILE */
168 CORBA::ORB_init (argc
, argv
);
170 CORBA::Object_var poa_object
=
171 orb
->resolve_initial_references("RootPOA");
173 PortableServer::POA_var root_poa
=
174 PortableServer::POA::_narrow (poa_object
.in ());
176 if (CORBA::is_nil (root_poa
.in ()))
177 ACE_ERROR_RETURN ((LM_ERROR
,
178 " (%P|%t) Panic: nil RootPOA\n"),
181 if (parse_args (argc
, argv
) != 0)
185 ACE_NEW_RETURN (test_i
,
188 PortableServer::ServantBase_var
owner_transfer(test_i
);
190 PortableServer::ObjectId_var id
= root_poa
->activate_object (test_i
);
192 CORBA::Object_var object
= root_poa
->id_to_reference (id
.in ());
194 Test_var test
= Test::_narrow (object
.in ());
196 CORBA::String_var ior
= orb
->object_to_string (test
.in ());
198 // Output the IOR to the <ior_output_file>
199 FILE *output_file
= ACE_OS::fopen (ior_output_file
, "w");
200 if (output_file
== 0)
201 ACE_ERROR_RETURN ((LM_ERROR
,
202 "Cannot open output file %s for writing IOR: %C\n",
206 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
207 ACE_OS::fclose (output_file
);
209 PortableServer::POAManager_var poa_manager
=
210 root_poa
->the_POAManager ();
211 poa_manager
->activate ();
213 Descriptors descriptors
;
221 ACE_Time_Value
tv (10);
224 cout
<< "Server: closing some fds" << endl
;
226 descriptors
.allow_accepts ();
230 if (!descriptors
.ok ())
232 cout
<< "Server: the accept error never occurred" << endl
;
233 ACE_ERROR_RETURN ((LM_ERROR
, "The accept error never occurred\n"), 1);
236 catch (const CORBA::Exception
& ex
)
238 ex
._tao_print_exception ("Exception caught:");
241 #endif /* ACE_WIN32 */