1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
// Service_Manager.cpp
// $Id$
#define ACE_BUILD_DLL
#include "ace/Get_Opt.h"
#include "ace/Service_Repository.h"
#include "ace/Service_Config.h"
#include "ace/Service_Manager.h"
#include "ace/Reactor.h"
#include "ace/WFMO_Reactor.h"
#if !defined (__ACE_INLINE__)
#include "ace/Service_Manager.i"
#endif /* __ACE_INLINE__ */
ACE_RCSID(ace, Service_Manager, "$Id$")
ACE_ALLOC_HOOK_DEFINE(ACE_Service_Manager)
void
ACE_Service_Manager::dump (void) const
{
ACE_TRACE ("ACE_Service_Manager::dump");
}
// Static variables.
u_short ACE_Service_Manager::DEFAULT_PORT_ = 10000;
ACE_Service_Manager::ACE_Service_Manager (void)
: debug_ (0),
signum_ (SIGHUP)
{
ACE_TRACE ("ACE_Service_Manager::ACE_Service_Manager");
}
int
ACE_Service_Manager::suspend (void)
{
ACE_TRACE ("ACE_Service_Manager::suspend");
return ACE_Reactor::instance ()->suspend_handler (this);
}
int
ACE_Service_Manager::resume (void)
{
ACE_TRACE ("ACE_Service_Manager::resume");
return ACE_Reactor::instance ()->resume_handler (this);
}
int
ACE_Service_Manager::open (const ACE_INET_Addr &sia)
{
ACE_TRACE ("ACE_Service_Manager::open");
// Reuse the listening address, even if it's already in use!
if (this->acceptor_.open (sia, 1) == -1)
return -1;
return 0;
}
int
ACE_Service_Manager::info (ASYS_TCHAR **strp, size_t length) const
{
ACE_TRACE ("ACE_Service_Manager::info");
ACE_INET_Addr sa;
ASYS_TCHAR buf[BUFSIZ];
if (this->acceptor_.get_local_addr (sa) == -1)
return -1;
ACE_OS::sprintf (buf,
ASYS_TEXT ("%d/%s %s"),
sa.get_port_number (),
ASYS_TEXT ("tcp"),
ASYS_TEXT ("# lists all services in the daemon\n"));
if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
return -1;
else
ACE_OS::strncpy (*strp, buf, length);
return ACE_OS::strlen (buf);
}
int
ACE_Service_Manager::init (int argc, ASYS_TCHAR *argv[])
{
ACE_TRACE ("ACE_Service_Manager::init");
ACE_INET_Addr local_addr (ACE_Service_Manager::DEFAULT_PORT_);
ACE_Get_Opt getopt (argc, argv, ASYS_TEXT ("dp:s:"), 0); // Start at argv[0]
for (int c; (c = getopt ()) != -1; )
switch (c)
{
case 'd':
this->debug_ = 1;
break;
case 'p':
local_addr.set ((u_short) ACE_OS::atoi (getopt.optarg));
break;
case 's':
this->signum_ = ACE_OS::atoi (getopt.optarg);
break;
default:
break;
}
if (this->open (local_addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("open")), -1);
else if (ACE_Reactor::instance ()->register_handler
(this,
ACE_Event_Handler::ACCEPT_MASK) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("registering service with ACE_Reactor\n")),
-1);
return 0;
}
int
ACE_Service_Manager::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
ACE_TRACE ("ACE_Service_Manager::handle_close");
return this->acceptor_.close ();
}
int
ACE_Service_Manager::fini (void)
{
ACE_TRACE ("ACE_Service_Manager::fini");
if (this->get_handle () != ACE_INVALID_HANDLE)
return ACE_Reactor::instance ()->remove_handler
(this,
ACE_Event_Handler::ACCEPT_MASK);
return 0;
}
ACE_HANDLE
ACE_Service_Manager::get_handle (void) const
{
ACE_TRACE ("ACE_Service_Manager::get_handle");
return this->acceptor_.get_handle ();
}
int
ACE_Service_Manager::handle_signal (int sig, siginfo_t *, ucontext_t *)
{
ACE_TRACE ("ACE_Service_Manager::handle_signal");
if (this->debug_)
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("got %S\n"),
sig));
return 0;
}
// Determine all the services offered by this daemon and return the
// information back to the client.
int
ACE_Service_Manager::list_services (void)
{
ACE_TRACE ("ACE_Service_Manager::list_services");
ACE_Service_Repository_Iterator sri (*ACE_Service_Repository::instance ());
for (const ACE_Service_Type *sr;
sri.next (sr) != 0;
sri.advance ())
{
int len = ACE_OS::strlen (sr->name ()) + 1;
ASYS_TCHAR buf[BUFSIZ];
ASYS_TCHAR *p = buf + len;
ACE_OS::strcpy (buf, sr->name ());
p[-1] = ' ';
p[0] = '\0';
len += sr->type ()->info (&p, sizeof buf - len);
if (this->debug_)
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("len = %d, info = %s%s"),
len,
buf,
buf[len - 1] == '\n' ? ASYS_TEXT ("") : ASYS_TEXT ("\n")));
if (len > 0)
{
ssize_t n = this->client_stream_.send_n (ASYS_MULTIBYTE_STRING (buf),
len);
if (n != len || (n == -1 && errno != EPIPE))
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("send_n")));
}
}
return 0;
}
// Trigger a remote reconfiguration of the Service Configurator.
int
ACE_Service_Manager::reconfigure_services (void)
{
ACE_TRACE ("ACE_Service_Manager::reconfigure_services");
#if 0
// Send ourselves a signal! ACE_OS::kill (ACE_OS::getpid (),
// this->signum_);
#endif /* 0 */
// Flag the main event loop that a reconfiguration should occur.
// The next trip through the <ACE_Reactor::run_event_loop> should
// pick this up and cause a reconfiguration. Note that we can't
// trigger the reconfiguration automatically since that might "pull
// the rug" out from underneath the existing services in a
// problematic way.
ACE_Service_Config::reconfig_occurred ((sig_atomic_t) 1);
return this->client_stream_.send_n ("done\n",
sizeof ("done\n"));
}
// Accept new connection from client and carry out the service they
// request.
int
ACE_Service_Manager::handle_input (ACE_HANDLE)
{
ACE_TRACE ("ACE_Service_Manager::handle_input");
// Try to find out if the implementation of the reactor that we are
// using requires us to reset the event association for the newly
// created handle. This is because the newly created handle will
// inherit the properties of the listen handle, including its event
// associations.
int reset_new_handle =
ACE_Reactor::instance ()->uses_event_associations ();
if (this->acceptor_.accept (this->client_stream_, // stream
0, // remote address
0, // timeout
1, // restart
reset_new_handle // reset new handler
) == -1)
return -1;
if (this->debug_)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("client_stream fd = %d\n"),
this->client_stream_.get_handle ()));
ACE_INET_Addr sa;
if (this->client_stream_.get_remote_addr (sa) == -1)
return -1;
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("accepted from host %s at port %d\n"),
sa.get_host_name (),
sa.get_port_number ()));
}
char request[BUFSIZ];
// Read service request from client.
switch (client_stream_.recv (request, sizeof request))
{
case -1:
if (this->debug_)
ACE_DEBUG ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("recv")));
break;
case 0:
return 0;
/* NOTREACHED */
default:
{
char *p;
// Kill trailing newlines.
for (p = request;
(*p != '\0') && (*p != '\r') && (*p != '\n');
p++)
continue;
*p = '\0';
ACE_Event_Handler *old_signal_handler = 0;
ACE_Reactor::instance ()->register_handler (SIGPIPE,
this,
0,
&old_signal_handler);
if (ACE_OS::strcmp (request, "help") == 0)
this->list_services ();
else if (ACE_OS::strcmp (request, "reconfigure") == 0)
this->reconfigure_services ();
// Additional management services may be handled here...
// Restore existing SIGPIPE handler
ACE_Reactor::instance ()->register_handler (SIGPIPE,
old_signal_handler);
}
}
if (this->client_stream_.close () == -1 && this->debug_)
ACE_DEBUG ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("close")));
return 0;
}
|