File: a00148.html

package info (click to toggle)
omnievents 2.6.1-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,228 kB
  • ctags: 2,538
  • sloc: cpp: 7,346; sh: 2,568; python: 2,171; xml: 2,057; java: 1,407; makefile: 304; ansic: 9
file content (555 lines) | stat: -rw-r--r-- 33,161 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>daemon_windows.cc Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="main.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="namespacemembers.html">Namespace Members</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center>
<hr><h1>daemon_windows.cc</h1><a href="a00088.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">//                            Package   : omniEvents</font>
00002 <font class="comment">//  daemon_windows.cc         Created   : 2004/07/23</font>
00003 <font class="comment">//                            Author    : Alex Tingle</font>
00004 <font class="comment">//</font>
00005 <font class="comment">//    Copyright (C) 2004 Alex Tingle</font>
00006 <font class="comment">//</font>
00007 <font class="comment">//    This file is part of the omniEvents application.</font>
00008 <font class="comment">//</font>
00009 <font class="comment">//    omniEvents is free software; you can redistribute it and/or</font>
00010 <font class="comment">//    modify it under the terms of the GNU Lesser General Public</font>
00011 <font class="comment">//    License as published by the Free Software Foundation; either</font>
00012 <font class="comment">//    version 2.1 of the License, or (at your option) any later version.</font>
00013 <font class="comment">//</font>
00014 <font class="comment">//    omniEvents is distributed in the hope that it will be useful,</font>
00015 <font class="comment">//    but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00016 <font class="comment">//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</font>
00017 <font class="comment">//    Lesser General Public License for more details.</font>
00018 <font class="comment">//</font>
00019 <font class="comment">//    You should have received a copy of the GNU Lesser General Public</font>
00020 <font class="comment">//    License along with this library; if not, write to the Free Software</font>
00021 <font class="comment">//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</font>
00022 <font class="comment">//</font>
00023 
00024 <font class="preprocessor">#include "<a class="code" href="a00085.html">daemon.h</a>"</font>
00025 <font class="preprocessor">#include "<a class="code" href="a00089.html">daemon_windows.h</a>"</font>
00026 <font class="preprocessor">#include "<a class="code" href="a00106.html">main.h</a>"</font>
00027 
<a name="l00028"></a><a class="code" href="a00088.html#a0">00028</a> <font class="preprocessor">#define NEED_PACKAGE_INFO</font>
00029 <font class="preprocessor"></font><font class="preprocessor">#ifdef HAVE_CONFIG_H</font>
00030 <font class="preprocessor"></font><font class="preprocessor">#  include "<a class="code" href="a00082.html">config.h</a>"</font>
00031 <font class="preprocessor">#endif</font>
00032 <font class="preprocessor"></font>
00033 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00034 
00035 <font class="preprocessor">#include &lt;fstream&gt;</font>
00036 <font class="preprocessor">#include &lt;stdlib.h&gt;</font> <font class="comment">// exit, on_exit</font>
00037 <font class="preprocessor">#include &lt;errno.h&gt;</font> <font class="comment">// errno</font>
00038 <font class="preprocessor">#include &lt;string&gt;</font>
00039 <font class="preprocessor">#include &lt;vector&gt;</font>
00040 
<a name="l00041"></a><a class="code" href="a00088.html#a1">00041</a> <font class="preprocessor">#define AS_STR_2(x) #x</font>
<a name="l00042"></a><a class="code" href="a00088.html#a2">00042</a> <font class="preprocessor"></font><font class="preprocessor">#define AS_STR_1(x) AS_STR_2(x)</font>
00043 <font class="preprocessor"></font>
<a name="l00044"></a><a class="code" href="a00088.html#a3">00044</a> <font class="preprocessor">#define HERE __FILE__ ":" AS_STR_1(__LINE__)</font>
00045 <font class="preprocessor"></font>
00046 <font class="comment">// Forward declaration of omniORB::setLogFunction()</font>
00047 <font class="keyword">namespace </font>omniORB {
00048   <font class="keywordtype">void</font> <a class="code" href="a00139.html#a0">setLogFunction</a>(<font class="keywordtype">void</font> (*logFunction)(<font class="keyword">const</font> <font class="keywordtype">char</font>*));
00049 }
00050 
00051 <font class="keyword">namespace </font>OmniEvents {
00052 
<a name="l00054"></a><a class="code" href="a00079.html">00054</a> <font class="keyword">class </font><a class="code" href="a00079.html">Win</a>
00055 {
00056 <font class="keyword">public</font>:
<a name="l00057"></a><a class="code" href="a00079.html#d0">00057</a>   <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font>* <a class="code" href="a00079.html#d0">strerror</a>(DWORD e)
00058   {
00059     LPVOID buf;
00060     ::FormatMessage(
00061       FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00062       NULL,
00063       e,
00064       MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
00065       (LPTSTR)&amp;buf,
00066       0, NULL
00067     );
00068     <font class="keywordflow">return</font> (<font class="keyword">const</font> <font class="keywordtype">char</font>*)(buf);
00069   }
00070 
<a name="l00071"></a><a class="code" href="a00079.html#d1">00071</a>   <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="a00079.html#d1">perror</a>(<font class="keyword">const</font> <font class="keywordtype">char</font>* s=NULL)
00072   {
00073     <font class="keywordflow">if</font>(s)
00074     {
00075       Service::log(s);
00076       Service::log(<font class="stringliteral">": "</font>);
00077     }
00078     Service::log(Win::strerror(::GetLastError()));
00079   }
00080 };
00081 
<a name="l00086"></a><a class="code" href="a00072.html">00086</a> <font class="keyword">class </font><a class="code" href="a00072.html">RegistryKey</a>
00087 {
<a name="l00088"></a><a class="code" href="a00072.html#o0">00088</a>   HKEY <a class="code" href="a00072.html#o0">_hkey</a>;
<a name="l00089"></a><a class="code" href="a00072.html#o1">00089</a>   <font class="keywordtype">bool</font> <a class="code" href="a00072.html#o1">_open</a>;
00090 <font class="keyword">private</font>:
00091   <a class="code" href="a00072.html#c0">RegistryKey</a>(); 
<a name="l00092"></a><a class="code" href="a00072.html#c1">00092</a>   <a class="code" href="a00072.html#c0">RegistryKey</a>(HKEY hkey, <font class="keywordtype">bool</font> open=<font class="keyword">true</font>):<a class="code" href="a00072.html#o0">_hkey</a>(hkey),<a class="code" href="a00072.html#o1">_open</a>(open){}
00093 <font class="keyword">public</font>:
00094   <a class="code" href="a00072.html#c0">RegistryKey</a>(<a class="code" href="a00072.html#c0">RegistryKey</a>&amp; right);
00095   <a class="code" href="a00072.html#c0">RegistryKey</a>(HKEY hkey, <font class="keyword">const</font> <font class="keywordtype">char</font>* subkey, REGSAM samDesired=KEY_QUERY_VALUE);
00096   <a class="code" href="a00072.html#a2">~RegistryKey</a>();
<a name="l00097"></a><a class="code" href="a00072.html#a3">00097</a>   <a class="code" href="a00072.html#a3">operator bool</a>()<font class="keyword"> const </font>{ <font class="keywordflow">return</font> <a class="code" href="a00072.html#o1">_open</a>; }
00098   <font class="keywordtype">int</font> <a class="code" href="a00072.html#a4">setValueStr</a>(<font class="keyword">const</font> <font class="keywordtype">char</font>* name, <font class="keyword">const</font> <font class="keywordtype">char</font>* data);
00099   <font class="keywordtype">char</font>* <a class="code" href="a00072.html#a5">queryValueStr</a>(<font class="keyword">const</font> <font class="keywordtype">char</font>* name, <font class="keyword">const</font> <font class="keywordtype">int</font> maxlen=2048) <font class="keyword">const</font>;
00100 };
00101 
<a name="l00103"></a><a class="code" href="a00072.html#a0">00103</a> RegistryKey::RegistryKey(<a class="code" href="a00072.html">RegistryKey</a>&amp; right):
00104   _hkey(right._hkey),_open(right._open)
00105 {
00106   right._open=<font class="keyword">false</font>;
00107 }
00108 
<a name="l00110"></a><a class="code" href="a00072.html#a1">00110</a> RegistryKey::RegistryKey(
00111   HKEY hkey,
00112   <font class="keyword">const</font> <font class="keywordtype">char</font>* subkey,
00113   REGSAM samDesired
00114 ):_hkey(), _open(false)
00115 {
00116   <font class="keywordtype">long</font> ret=::RegOpenKeyEx(hkey,subkey,0,samDesired,&amp;<a class="code" href="a00072.html#o0">_hkey</a>);
00117   ::SetLastError(ret);
00118   <font class="keywordflow">if</font>(ret==ERROR_SUCCESS)
00119       <a class="code" href="a00072.html#o1">_open</a>=<font class="keyword">true</font>;
00120 }
00121 
<a name="l00123"></a><a class="code" href="a00072.html#a2">00123</a> RegistryKey::~RegistryKey()
00124 {
00125   <font class="comment">// Windows - why use two lines, when seven will do??</font>
00126   <font class="comment">// RegCloseKey() does not set last error, so complexity ensues...</font>
00127   <font class="keywordflow">if</font>(_open)
00128   {
00129     <font class="keywordtype">long</font> ret =::RegCloseKey(_hkey);
00130     ::SetLastError(ret);
00131     <font class="keywordflow">if</font>(ret!=ERROR_SUCCESS)
00132         Win::perror(<font class="stringliteral">"Warning at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00133   }
00134 }
00135 
<a name="l00136"></a><a class="code" href="a00072.html#a4">00136</a> <font class="keywordtype">int</font> RegistryKey::setValueStr(<font class="keyword">const</font> <font class="keywordtype">char</font>* name, <font class="keyword">const</font> <font class="keywordtype">char</font>* data)
00137 {
00138   <font class="keywordtype">long</font> ret=::RegSetValueEx(
00139       <a class="code" href="a00072.html#o0">_hkey</a>,name,0,REG_SZ,
00140       (<font class="keyword">const</font> BYTE*)(data),
00141       1+::strlen(data)
00142     );
00143   ::SetLastError(ret);
00144   <font class="keywordflow">if</font>(ret==ERROR_SUCCESS)
00145       <font class="keywordflow">return</font> 0;
00146   <font class="keywordflow">else</font>
00147       <font class="keywordflow">return</font> 1;
00148 }
00149 
<a name="l00150"></a><a class="code" href="a00072.html#a5">00150</a> <font class="keywordtype">char</font>* RegistryKey::queryValueStr(<font class="keyword">const</font> <font class="keywordtype">char</font>* name, <font class="keyword">const</font> <font class="keywordtype">int</font> maxlen)<font class="keyword"> const</font>
00151 <font class="keyword"></font>{
00152   <font class="keywordtype">char</font>* result =NULL;
00153   <font class="keywordtype">char</font>* buf =<font class="keyword">new</font> <font class="keywordtype">char</font>[maxlen];
00154   DWORD len =maxlen;
00155 
00156   <font class="keywordtype">long</font> ret=::RegQueryValueEx(<a class="code" href="a00072.html#o0">_hkey</a>,name,NULL,NULL,(LPBYTE)buf,&amp;len);
00157   ::SetLastError(ret);
00158   <font class="keywordflow">if</font>(ret==ERROR_SUCCESS &amp;&amp; len&lt;=maxlen)
00159       result=::strdup(buf); <font class="comment">// MSVC6 has no strndup()!!</font>
00160   <font class="keyword">delete</font>[] buf;
00161   <font class="keywordflow">return</font> result;
00162 }
00164 
<a name="l00166"></a><a class="code" href="a00138.html#a1">00166</a> <font class="keyword">static</font> <a class="code" href="a00074.html">Service</a> <a class="code" href="a00138.html#a1">service</a>;
00167 
00168 Daemon::Daemon(<font class="keywordtype">int</font>&amp; argc,<font class="keywordtype">char</font>**&amp; argv)  { service.<a class="code" href="a00074.html#a2">start</a>(argc,argv); }
00169 <font class="keywordtype">void</font> Daemon::tracefile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val) { service.<a class="code" href="a00074.html#a3">tracefile</a>(val); }
00170 <font class="keywordtype">void</font> Daemon::pidfile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)   { service.<a class="code" href="a00074.html#a4">pidfile</a>(val); }
00171 <font class="keywordtype">void</font> Daemon::foreground(<font class="keywordtype">bool</font> val)       { service.<a class="code" href="a00074.html#a5">foreground</a>(val); }
00172 <font class="keywordtype">void</font> Daemon::daemonize()                { service.<a class="code" href="a00074.html#a6">daemonize</a>(); }
00173 <font class="keywordtype">void</font> Daemon::runningOk()                { service.<a class="code" href="a00074.html#a7">runningOk</a>(); }
00174 Daemon::~Daemon()                       { service.<a class="code" href="a00074.html#a8">shutdown</a>(); }
00175 
00176 <font class="keywordtype">void</font> <a class="code" href="a00138.html#a3">shutdown0</a>(<font class="keywordtype">void</font>){ service.<a class="code" href="a00074.html#a8">shutdown</a>(); } 
00177 
00179 
<a name="l00180"></a><a class="code" href="a00074.html#a0">00180</a> Service::Service():
00181   _tracefile(NULL),
00182   _regSubKey("SYSTEM\\CurrentControlSet\\Services\\" <a class="code" href="a00082.html#a40">PACKAGE_NAME</a>),
00183   _serviceRunning(false),
00184   _callCount(0),
00185   _parameters(NULL),
00186   _argv(NULL),
00187   _logstream(&amp;cerr),
00188   _serviceStatusHandle()
00189 {}
00190 
00191 
<a name="l00192"></a><a class="code" href="a00074.html#a1">00192</a> Service::~Service()
00193 {
00194   <font class="keyword">delete</font>[] <a class="code" href="a00074.html#o0">_tracefile</a>;
00195   <font class="keyword">delete</font>[] <a class="code" href="a00074.html#o4">_parameters</a>;
00196   <font class="keyword">delete</font>[] <a class="code" href="a00074.html#o5">_argv</a>;
00197   <font class="keywordflow">if</font>(<a class="code" href="a00074.html#o6">_logstream</a>!=&amp;cerr)
00198       <font class="keyword">delete</font> <a class="code" href="a00074.html#o6">_logstream</a>;
00199 }
00200 
00201 
<a name="l00202"></a><a class="code" href="a00074.html#a3">00202</a> <font class="keywordtype">void</font> Service::tracefile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)
00203 {
00204   <font class="keyword">delete</font>[] <a class="code" href="a00074.html#o0">_tracefile</a>;
00205   <a class="code" href="a00074.html#o0">_tracefile</a>=::strdup(val);
00206 }
00207 
00208 
<a name="l00209"></a><a class="code" href="a00074.html#a4">00209</a> <font class="keywordtype">void</font> Service::pidfile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)
00210 {
00211   Service::log(<font class="stringliteral">"Option -P not supported on windows.\n"</font>);
00212   ::exit(1);
00213 }
00214 
00215 
<a name="l00216"></a><a class="code" href="a00074.html#a5">00216</a> <font class="keywordtype">void</font> Service::foreground(<font class="keywordtype">bool</font> val)
00217 {
00218   Service::log(<font class="stringliteral">"Option -f not supported on windows.\n"</font>);
00219   ::exit(1);
00220 }
00221 
00222 
<a name="l00223"></a><a class="code" href="a00074.html#a2">00223</a> <font class="keywordtype">void</font> Service::start(<font class="keywordtype">int</font>&amp; argc,<font class="keywordtype">char</font>**&amp; argv)
00224 {
00225   ++<a class="code" href="a00074.html#o3">_callCount</a>;
00226   <font class="keywordflow">if</font>(<a class="code" href="a00074.html#o3">_callCount</a>&gt;1)
00227   {
00228     <font class="comment">// This is a re-entrant call. We are inside 'ServiceMain()'.</font>
00229     setArgcArgv(argc,argv); <font class="comment">// Set argv &amp; argc from the registry.</font>
00230     <a class="code" href="a00074.html#o7">_serviceStatusHandle</a>=
00231       ::RegisterServiceCtrlHandler(
00232         <a class="code" href="a00082.html#a40">PACKAGE_NAME</a>,
00233         (LPHANDLER_FUNCTION)Service::ctrlHandler
00234       );
00235     <font class="keywordflow">if</font>(!<a class="code" href="a00074.html#o7">_serviceStatusHandle</a>)
00236         ::exit(1);
00237     <font class="keywordflow">if</font>(! setServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,1,3000) )
00238         ::exit(1);
00239     <a class="code" href="a00074.html#o2">_serviceRunning</a>=<font class="keyword">true</font>;
00240     <font class="comment">// ...and return to main().</font>
00241   }
00242   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"service"</font>))
00243   {
00244     <font class="comment">// Start service.</font>
00245     <font class="keywordtype">char</font>* name =::strdup(PACKAGE_NAME);
00246     SERVICE_TABLE_ENTRY servicetable[]=
00247     {
00248       {name,(LPSERVICE_MAIN_FUNCTION)::main},
00249       {NULL,NULL}
00250     };
00251     <font class="keywordflow">if</font>(! ::StartServiceCtrlDispatcher(servicetable) )
00252     {
00253       Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00254       ::exit(1);
00255     }
00256     ::exit(0);
00257   }
00258   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"install"</font>))
00259   {
00260     <a class="code" href="a00074.html#c1">install</a>(argc,argv);
00261     cout&lt;&lt;<font class="stringliteral">"Service '"</font> <a class="code" href="a00082.html#a40">PACKAGE_NAME</a> <font class="stringliteral">"' installed OK."</font>&lt;&lt;endl;
00262     ::exit(0);
00263   }
00264   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"uninstall"</font>))
00265   {
00266     <a class="code" href="a00074.html#c2">uninstall</a>();
00267     cout&lt;&lt;<font class="stringliteral">"Service '"</font> <a class="code" href="a00082.html#a40">PACKAGE_NAME</a> <font class="stringliteral">"' removed."</font>&lt;&lt;endl;
00268     ::exit(0);
00269   }
00270   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"getoptions"</font>))
00271   {
00272     <a class="code" href="a00074.html#c3">readParameters</a>();
00273     cout&lt;&lt;<a class="code" href="a00074.html#o4">_parameters</a>&lt;&lt;endl;
00274     ::exit(0);
00275   }
00276   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"setoptions"</font>))
00277   {
00278     <a class="code" href="a00074.html#c4">writeParameters</a>(argc,argv);
00279     ::exit(0);
00280   }
00281   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(argc&gt;=2 &amp;&amp; 0==::strcmp(argv[1],<font class="stringliteral">"run"</font>))
00282   {
00283     setArgcArgv(argc,argv); <font class="comment">// Set argv &amp; argc from the registry.</font>
00284   }
00285   <font class="keywordflow">else</font>
00286   {
00287     ; <font class="comment">// Just run the program in the foreground.</font>
00288   }
00289 }
00290 
00291 
<a name="l00292"></a><a class="code" href="a00074.html#a6">00292</a> <font class="keywordtype">void</font> Service::daemonize()
00293 {
00294   <font class="keywordflow">if</font>(<a class="code" href="a00074.html#o0">_tracefile</a> &amp;&amp; <a class="code" href="a00074.html#o0">_tracefile</a>[0]!=<font class="charliteral">'\0'</font>)
00295   {
00296     <a class="code" href="a00074.html#o6">_logstream</a>=<font class="keyword">new</font> ofstream(<a class="code" href="a00074.html#o0">_tracefile</a>,ios::out|ios::app);
00297     <a class="code" href="a00139.html#a0">omniORB::setLogFunction</a>(Service::log);
00298   }
00299 
00300   <font class="comment">// Register the shutdown function.</font>
00301   <font class="keywordflow">if</font>( ::atexit(shutdown0) &lt;0) <font class="comment">// Windows has atexit()</font>
00302   {
00303     Service::log(<font class="stringliteral">"Failed to set exit handler."</font>);
00304     ::exit(-1);
00305   }
00306 }
00307 
00308 
<a name="l00309"></a><a class="code" href="a00074.html#a7">00309</a> <font class="keywordtype">void</font> Service::runningOk()
00310 {
00311   <font class="keywordflow">if</font>(_serviceRunning)
00312   {
00313     <font class="keywordflow">if</font>(! setServiceStatus(SERVICE_RUNNING,NO_ERROR,0,0,0) )
00314         ::exit(1);
00315   }
00316 }
00317 
00318 
<a name="l00319"></a><a class="code" href="a00074.html#a8">00319</a> <font class="keywordtype">void</font> Service::shutdown()
00320 {
00321   <font class="keywordflow">if</font>(<a class="code" href="a00074.html#o6">_logstream</a>!=&amp;cerr)
00322   {
00323     <font class="keyword">delete</font> <a class="code" href="a00074.html#o6">_logstream</a>;
00324     <a class="code" href="a00074.html#o6">_logstream</a>=&amp;cerr;
00325   }
00326   <font class="keywordflow">if</font>(_serviceRunning)
00327   {
00328     setServiceStatus(SERVICE_STOPPED,NO_ERROR,0,0,0);
00329     <a class="code" href="a00074.html#o2">_serviceRunning</a>=<font class="keyword">false</font>;
00330   }
00331 }
00332 
00333 <font class="comment">// static callback</font>
<a name="l00334"></a><a class="code" href="a00074.html#d0">00334</a> <font class="keywordtype">void</font> Service::log(<font class="keyword">const</font> <font class="keywordtype">char</font>* message)
00335 {
00336   (*service.<a class="code" href="a00074.html#o6">_logstream</a>)&lt;&lt;message&lt;&lt;flush;
00337 }
00338 
00339 <font class="comment">// static callback</font>
<a name="l00340"></a><a class="code" href="a00074.html#d1">00340</a> <font class="keywordtype">void</font> Service::ctrlHandler(DWORD controlCode)
00341 {
00342   <font class="keywordflow">switch</font>(controlCode)
00343   {  
00344   <font class="keywordflow">case</font> SERVICE_CONTROL_SHUTDOWN:
00345   <font class="keywordflow">case</font> SERVICE_CONTROL_STOP:
00346       ::OmniEvents_Orb_shutdown(controlCode);
00347       service.setServiceStatus(SERVICE_STOP_PENDING,NO_ERROR,0,1,6000);
00348       <font class="keywordflow">break</font>;
00349   <font class="keywordflow">case</font> 128: <font class="comment">// User defined code.</font>
00350       ::OmniEvents_Orb_bumpTraceLevel(controlCode);
00351       <font class="keywordflow">break</font>;
00352   <font class="keywordflow">default</font>:
00353       <font class="keywordflow">break</font>;
00354   }
00355 }
00356 
00357 
00358 <font class="keywordtype">void</font> Service::setArgcArgv(<font class="keywordtype">int</font>&amp; argc,<font class="keywordtype">char</font>**&amp; argv)
00359 {
00360   <a class="code" href="a00074.html#c3">readParameters</a>();
00361   vector&lt;char*&gt; args;
00362   <font class="keywordtype">char</font>* param =::strtok(<a class="code" href="a00074.html#o4">_parameters</a>,<font class="stringliteral">"\t "</font>);
00363   <font class="keywordflow">while</font>(param)
00364   {
00365     args.push_back(param);
00366     param=::strtok(NULL,<font class="stringliteral">"\t "</font>);
00367   }
00368   <font class="keywordflow">if</font>(!args.empty())
00369   {
00370     <a class="code" href="a00074.html#o5">_argv</a>=<font class="keyword">new</font> <font class="keywordtype">char</font>*[argc+args.size()]; <font class="comment">// deleted by ~Service()</font>
00371     <font class="keywordtype">int</font> i=0;
00372     <a class="code" href="a00074.html#o5">_argv</a>[i++]=argv[0];
00373     <font class="keywordflow">for</font>(<font class="keywordtype">int</font> j=0; j&lt;args.size(); ++j)
00374         <a class="code" href="a00074.html#o5">_argv</a>[i++]=args[j];
00375     <font class="keywordflow">for</font>(<font class="keywordtype">int</font> k=1; k&lt;argc; ++k)
00376         <a class="code" href="a00074.html#o5">_argv</a>[i++]=argv[k];
00377     argv=<a class="code" href="a00074.html#o5">_argv</a>;
00378     argc=i;
00379   }
00380 }
00381 
00382 
<a name="l00383"></a><a class="code" href="a00074.html#c1">00383</a> <font class="keywordtype">void</font> Service::install(<font class="keywordtype">int</font> argc,<font class="keywordtype">char</font>** argv)<font class="keyword"> const</font>
00384 <font class="keyword"></font>{
00385   <font class="comment">//</font>
00386   <font class="comment">// Install service</font>
00387   <font class="keywordtype">char</font> exe_file_name[MAX_PATH];
00388   <font class="keywordflow">if</font>(0== ::GetModuleFileName(0, exe_file_name, MAX_PATH) )
00389   {
00390     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00391     ::exit(1);
00392   }
00393 
00394   string command =string(exe_file_name)+<font class="stringliteral">" service"</font>;
00395 
00396   SC_HANDLE scmanager =::OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
00397   <font class="keywordflow">if</font>(!scmanager)
00398   {
00399     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00400     ::exit(1);
00401   }
00402   SC_HANDLE service =
00403     ::CreateService(
00404       scmanager,
00405       <a class="code" href="a00082.html#a40">PACKAGE_NAME</a>,
00406       <font class="stringliteral">"CORBA Event Daemon"</font>,
00407       SERVICE_ALL_ACCESS,
00408       SERVICE_WIN32_OWN_PROCESS,
00409       SERVICE_AUTO_START,
00410       SERVICE_ERROR_NORMAL,
00411       command.c_str(),
00412       0,0,0,0,0
00413     );
00414   <font class="keywordflow">if</font>(!service)
00415   {
00416     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00417     ::exit(1);
00418   }
00419   <font class="keywordflow">if</font>(0== ::CloseServiceHandle(service) )
00420   {
00421     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00422     ::exit(1);
00423   }
00424   <font class="keywordflow">if</font>(0== ::CloseServiceHandle(scmanager) )
00425   {
00426     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00427     ::exit(1);
00428   }
00429 
00430   <font class="comment">//</font>
00431   <font class="comment">// Set the service's parameters &amp; description.</font>
00432   <a class="code" href="a00074.html#c4">writeParameters</a>(argc,argv);
00433   <a class="code" href="a00072.html">RegistryKey</a> rkey(HKEY_LOCAL_MACHINE,<a class="code" href="a00074.html#o1">_regSubKey</a>,KEY_SET_VALUE);
00434   <font class="keywordflow">if</font>(!rkey)
00435   {
00436     Win::perror(<font class="stringliteral">"Can't open registry key at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00437     ::exit(1);
00438   }
00439   <font class="keywordflow">if</font>(0!= rkey.<a class="code" href="a00072.html#a4">setValueStr</a>(<font class="stringliteral">"Description"</font>,
00440            <font class="stringliteral">"Asynchronous broadcast channels for CORBA applications."</font>) )
00441   {
00442     Win::perror(<font class="stringliteral">"Can't set registry value 'Description' at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00443     ::exit(1);
00444   }
00445 }
00446 
00447 
<a name="l00448"></a><a class="code" href="a00074.html#c2">00448</a> <font class="keywordtype">void</font> Service::uninstall()<font class="keyword"> const</font>
00449 <font class="keyword"></font>{
00450   SC_HANDLE scmanager =::OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
00451   <font class="keywordflow">if</font>(!scmanager)
00452   {
00453     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00454     ::exit(1);
00455   }
00456   SC_HANDLE service =
00457     ::OpenService(
00458       scmanager,
00459       <a class="code" href="a00082.html#a40">PACKAGE_NAME</a>,
00460       SC_MANAGER_ALL_ACCESS
00461     );
00462   <font class="keywordflow">if</font>(!service)
00463   {
00464     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00465     ::exit(1);
00466   }
00467   <font class="keywordflow">if</font>(0== ::DeleteService(service) )
00468   {
00469     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00470     ::exit(1);
00471   }
00472   <font class="keywordflow">if</font>(0== ::CloseServiceHandle(service) )
00473   {
00474     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00475     ::exit(1);
00476   }
00477   <font class="keywordflow">if</font>(0== ::CloseServiceHandle(scmanager) )
00478   {
00479     Win::perror(<a class="code" href="a00088.html#a3">HERE</a>);
00480     ::exit(1);
00481   }
00482 }
00483 
00484 
<a name="l00485"></a><a class="code" href="a00074.html#c3">00485</a> <font class="keywordtype">void</font> Service::readParameters()
00486 {
00487   <a class="code" href="a00072.html">RegistryKey</a> rkey(HKEY_LOCAL_MACHINE,<a class="code" href="a00074.html#o1">_regSubKey</a>);
00488   <font class="keywordflow">if</font>(!rkey)
00489   {
00490     Win::perror(<font class="stringliteral">"Can't open registry key at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00491     ::exit(1);
00492   }
00493   <a class="code" href="a00074.html#o4">_parameters</a>=rkey.<a class="code" href="a00072.html#a5">queryValueStr</a>(<font class="stringliteral">"Parameters"</font>); <font class="comment">// deleted by ~Service()</font>
00494   <font class="keywordflow">if</font>(<a class="code" href="a00074.html#o4">_parameters</a>==NULL)
00495   {
00496     Win::perror(<font class="stringliteral">"Can't get Parameters at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00497     ::exit(1);
00498   }
00499 }
00500 
00501 
<a name="l00502"></a><a class="code" href="a00074.html#c4">00502</a> <font class="keywordtype">void</font> Service::writeParameters(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font>** argv)<font class="keyword"> const</font>
00503 <font class="keyword"></font>{
00504   <a class="code" href="a00072.html">RegistryKey</a> rkey(HKEY_LOCAL_MACHINE,<a class="code" href="a00074.html#o1">_regSubKey</a>,KEY_SET_VALUE);
00505   <font class="keywordflow">if</font>(!rkey)
00506   {
00507     Win::perror(<font class="stringliteral">"Can't open registry key at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00508     ::exit(1);
00509   }
00510   string parameters =<font class="stringliteral">""</font>;
00511   <font class="keywordflow">for</font>(<font class="keywordtype">int</font> i=2; i&lt;argc; ++i)
00512   {
00513     <font class="keywordflow">if</font>(!parameters.empty())
00514         parameters+=<font class="stringliteral">" "</font>;
00515     parameters+=argv[i];
00516   }
00517   <font class="keywordflow">if</font>(0!= rkey.<a class="code" href="a00072.html#a4">setValueStr</a>(<font class="stringliteral">"Parameters"</font>,parameters.c_str()) )
00518   {
00519     Win::perror(<font class="stringliteral">"Can't set registry value 'Parameters' at "</font> <a class="code" href="a00088.html#a3">HERE</a>);
00520     ::exit(1);
00521   }
00522 }
00523 
00524 
00525 <font class="keywordtype">bool</font> Service::setServiceStatus(
00526   DWORD currentState,
00527   DWORD win32ExitCode,
00528   DWORD serviceSpecificExitCode,
00529   DWORD checkPoint,
00530   DWORD waitHint)
00531 {
00532   SERVICE_STATUS s;
00533   s.dwServiceType            =SERVICE_WIN32_OWN_PROCESS;
00534   s.dwCurrentState           =currentState;
00535   s.dwServiceSpecificExitCode=serviceSpecificExitCode;
00536   s.dwCheckPoint             =checkPoint;
00537   s.dwWaitHint               =waitHint;
00538 
00539   <font class="keywordflow">if</font>(currentState==SERVICE_START_PENDING)
00540       s.dwControlsAccepted=0;
00541   <font class="keywordflow">else</font>
00542       s.dwControlsAccepted=SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
00543 
00544   <font class="keywordflow">if</font>(serviceSpecificExitCode==0)
00545       s.dwWin32ExitCode=win32ExitCode;
00546   <font class="keywordflow">else</font>
00547       s.dwWin32ExitCode=ERROR_SERVICE_SPECIFIC_ERROR;
00548 
00549   <font class="keywordflow">return</font> (0!= ::SetServiceStatus(<a class="code" href="a00074.html#o7">_serviceStatusHandle</a>,&amp;s) );
00550 }
00551 
00552 } <font class="comment">// end namespace OmniEvents</font>
</pre></div><hr><address align="right"><small>Generated on Fri Nov 19 17:42:20 2004 for OmniEvents by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>