File: a00146.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 (503 lines) | stat: -rw-r--r-- 33,586 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
<!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_unix.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_unix.cc</h1><a href="a00086.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_unix.h              Created   : 2004/06/29</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="a00106.html">main.h</a>"</font>
00026 <font class="preprocessor">#include "<a class="code" href="a00087.html">daemon_unix.h</a>"</font>
00027 
<a name="l00028"></a><a class="code" href="a00086.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="preprocessor">#ifdef HAVE_IOSTREAM</font>
00034 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;iostream&gt;</font>
00035 <font class="preprocessor">#  include &lt;fstream&gt;</font>
00036 <font class="preprocessor">#else</font>
00037 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;iostream.h&gt;</font>
00038 <font class="preprocessor">#  include &lt;fstream.h&gt;</font>
00039 <font class="preprocessor">#endif</font>
00040 <font class="preprocessor"></font>
00041 <font class="preprocessor">#ifdef HAVE_STD_IOSTREAM</font>
00042 <font class="preprocessor"></font><font class="keyword">using</font> <font class="keyword">namespace </font>std;
00043 <font class="preprocessor">#endif</font>
00044 <font class="preprocessor"></font>
00045 <font class="preprocessor">#include &lt;stdlib.h&gt;</font> <font class="comment">// exit, on_exit</font>
00046 <font class="preprocessor">#include &lt;errno.h&gt;</font> <font class="comment">// errno</font>
00047 
00048 <font class="preprocessor">#ifdef HAVE_UNISTD_H</font>
00049 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;unistd.h&gt;</font> <font class="comment">// fork, umask, setsid, dup2, chdir, close</font>
00050 <font class="preprocessor">#endif</font>
00051 <font class="preprocessor"></font>
00052 <font class="preprocessor">#ifdef HAVE_SYS_TYPES_H</font>
00053 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;sys/types.h&gt;</font> <font class="comment">// fork, umask, open</font>
00054 <font class="preprocessor">#endif</font>
00055 <font class="preprocessor"></font>
00056 <font class="preprocessor">#ifdef HAVE_SYS_STAT_H</font>
00057 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;sys/stat.h&gt;</font> <font class="comment">//open</font>
00058 <font class="preprocessor">#endif</font>
00059 <font class="preprocessor"></font>
00060 <font class="preprocessor">#ifdef HAVE_FCNTL_H</font>
00061 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;fcntl.h&gt;</font> <font class="comment">// open</font>
00062 <font class="preprocessor">#endif</font>
00063 <font class="preprocessor"></font>
00064 <font class="preprocessor">#ifdef HAVE_SYSLOG_H</font>
00065 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;syslog.h&gt;</font> <font class="comment">// openlog, syslog</font>
00066 <font class="preprocessor">#endif</font>
00067 <font class="preprocessor"></font>
00068 <font class="preprocessor">#ifdef HAVE_STRING_H</font>
00069 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;string.h&gt;</font> <font class="comment">// strerror</font>
00070 <font class="preprocessor">#endif</font>
00071 <font class="preprocessor"></font>
00072 <font class="preprocessor">#ifdef HAVE_SIGNAL_H</font>
00073 <font class="preprocessor"></font><font class="preprocessor">#include &lt;signal.h&gt;</font> <font class="comment">// kill</font>
00074 <font class="preprocessor">#endif</font>
00075 <font class="preprocessor"></font>
00076 <font class="preprocessor">#include &lt;string&gt;</font>
00077 
00078 <font class="comment">// Forward declaration of omniORB::setLogFunction()</font>
<a name="l00079"></a><a class="code" href="a00139.html">00079</a> <font class="keyword">namespace </font>omniORB {
00080   <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>*));
00081 }
00082 
00083 <font class="keyword">namespace </font>OmniEvents {
00084 
<a name="l00085"></a><a class="code" href="a00086.html#a1">00085</a> <font class="preprocessor">#define STRERR_FILE_LINE strerror(errno)&lt;&lt;" "&lt;&lt;__FILE__&lt;&lt;":"&lt;&lt;__LINE__</font>
00086 <font class="preprocessor"></font>
<a name="l00087"></a><a class="code" href="a00086.html#a2">00087</a> <font class="preprocessor">#define PIPE_READ  0</font>
<a name="l00088"></a><a class="code" href="a00086.html#a3">00088</a> <font class="preprocessor"></font><font class="preprocessor">#define PIPE_WRITE 1</font>
00089 <font class="preprocessor"></font>
00090 
<a name="l00094"></a><a class="code" href="a00138.html#a0">00094</a> <a class="code" href="a00045.html">DaemonImpl</a> <a class="code" href="a00138.html#a0">daemon</a>;
00095 
<a name="l00096"></a><a class="code" href="a00044.html#a0">00096</a> Daemon::Daemon(<font class="keywordtype">int</font>&amp;,<font class="keywordtype">char</font>**&amp;)
00097 {
00098   <font class="comment">// Initialise the DaemonImpl singleton.</font>
00099   daemon.<a class="code" href="a00045.html#m0">_tracefile</a>=NULL;
00100   daemon.<a class="code" href="a00045.html#m1">_foreground</a>=<font class="keyword">false</font>;
00101   daemon.<a class="code" href="a00045.html#m2">_pidfile</a>=NULL;
00102   daemon.<a class="code" href="a00045.html#m3">_pipe</a>[0]=daemon.<a class="code" href="a00045.html#m3">_pipe</a>[1]=-1;
00103   daemon.<a class="code" href="a00045.html#m4">_havePidfile</a>=<font class="keyword">false</font>;
00104   daemon.<a class="code" href="a00045.html#m5">_haveParent</a>=<font class="keyword">false</font>;
00105   daemon.<a class="code" href="a00045.html#m6">_haveSyslog</a>=<font class="keyword">false</font>;
00106 }
<a name="l00107"></a><a class="code" href="a00044.html#a2">00107</a> <font class="keywordtype">void</font> Daemon::tracefile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val) { daemon.<a class="code" href="a00045.html#a2">tracefile</a>(val); }
<a name="l00108"></a><a class="code" href="a00044.html#a3">00108</a> <font class="keywordtype">void</font> Daemon::pidfile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)   { daemon.<a class="code" href="a00045.html#a3">pidfile</a>(val); }
<a name="l00109"></a><a class="code" href="a00044.html#a4">00109</a> <font class="keywordtype">void</font> Daemon::foreground(<font class="keywordtype">bool</font> val)       { daemon.<a class="code" href="a00045.html#a4">foreground</a>(val); }
<a name="l00110"></a><a class="code" href="a00044.html#a5">00110</a> <font class="keywordtype">void</font> Daemon::daemonize()                { daemon.<a class="code" href="a00045.html#a6">daemonize</a>(); }
<a name="l00111"></a><a class="code" href="a00044.html#a6">00111</a> <font class="keywordtype">void</font> Daemon::runningOk()                { daemon.<a class="code" href="a00045.html#a7">runningOk</a>(); }
<a name="l00112"></a><a class="code" href="a00044.html#a1">00112</a> Daemon::~Daemon()                       { daemon.<a class="code" href="a00045.html#a8">shutdown</a>(0); }
00113 
<a name="l00114"></a><a class="code" href="a00138.html#a3">00114</a> <font class="keywordtype">void</font> <a class="code" href="a00138.html#a3">shutdown0</a>(<font class="keywordtype">void</font>)       { daemon.<a class="code" href="a00045.html#a8">shutdown</a>(0); } 
<a name="l00115"></a><a class="code" href="a00138.html#a4">00115</a> <font class="keywordtype">void</font> <a class="code" href="a00138.html#a4">shutdown2</a>(<font class="keywordtype">int</font> s,<font class="keywordtype">void</font>*){ daemon.<a class="code" href="a00045.html#a8">shutdown</a>(s); } 
00116 
00118 
<a name="l00119"></a><a class="code" href="a00045.html#a0">00119</a> DaemonImpl::DaemonImpl(){}
00120 
00121 
<a name="l00122"></a><a class="code" href="a00045.html#a1">00122</a> DaemonImpl::~DaemonImpl()
00123 {
00124   <font class="keyword">delete</font>[] <a class="code" href="a00045.html#m2">_pidfile</a>;
00125   <font class="keyword">delete</font>[] <a class="code" href="a00045.html#m0">_tracefile</a>;
00126   <a class="code" href="a00045.html#m2">_pidfile</a>=NULL;
00127   <a class="code" href="a00045.html#m0">_tracefile</a>=NULL;
00128 }
00129 
00130 
<a name="l00131"></a><a class="code" href="a00045.html#a2">00131</a> <font class="keywordtype">void</font> DaemonImpl::tracefile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)
00132 {
00133   <a class="code" href="a00045.html#m0">_tracefile</a>=::strdup(val);
00134 }
00135 
00136 
<a name="l00137"></a><a class="code" href="a00045.html#a4">00137</a> <font class="keywordtype">void</font> DaemonImpl::foreground(<font class="keywordtype">bool</font> val)
00138 {
00139   <a class="code" href="a00045.html#m1">_foreground</a>=val;
00140 }
00141 
00142 
<a name="l00143"></a><a class="code" href="a00045.html#a3">00143</a> <font class="keywordtype">void</font> DaemonImpl::pidfile(<font class="keyword">const</font> <font class="keywordtype">char</font>* val)
00144 {
00145   string pidfileStr =val;
00146   <font class="keywordflow">if</font>(pidfileStr[0]!=<font class="charliteral">'/'</font>)
00147       pidfileStr=string(<font class="stringliteral">"/var/run/"</font>)+pidfileStr;
00148   DaemonImpl::_pidfile=::strdup(pidfileStr.c_str());
00149 }
00150 
00151 
<a name="l00152"></a><a class="code" href="a00045.html#a5">00152</a> <font class="keywordtype">void</font> DaemonImpl::initialize(<font class="keywordtype">int</font>&amp;,<font class="keywordtype">char</font>**&amp;)
00153 {
00154   <font class="comment">// Does nothing on Unix</font>
00155 }
00156 
00157 
<a name="l00158"></a><a class="code" href="a00045.html#a6">00158</a> <font class="keywordtype">void</font> DaemonImpl::daemonize()
00159 {
00160   <font class="comment">// Register the shutdown function.</font>
00161 <font class="preprocessor">#ifdef HAVE_ON_EXIT</font>
00162 <font class="preprocessor"></font>  <font class="keywordflow">if</font>( ::on_exit(<a class="code" href="a00138.html#a4">shutdown2</a>,NULL) &lt;0)
00163 <font class="preprocessor">#else</font>
00164 <font class="preprocessor"></font>  <font class="keywordflow">if</font>( ::atexit(shutdown0) &lt;0)
00165 <font class="preprocessor">#endif</font>
00166 <font class="preprocessor"></font>  {
00167     cerr&lt;&lt;<font class="stringliteral">"Failed to set exit handler."</font>&lt;&lt;endl;
00168     ::exit(-1);
00169   }
00170 
00171   <font class="keywordflow">if</font>(!<a class="code" href="a00045.html#m1">_foreground</a>)
00172   {
00173     this-&gt;<a class="code" href="a00045.html#c0">fork</a>();
00174     <font class="comment">// ...now in the CHILD.</font>
00175   }
00176 
00177   <font class="comment">// Check &amp; write the pidfile (if _pidfile is set).</font>
00178   <a class="code" href="a00045.html#c3">checkPidfileOrShutdown</a>();
00179   <a class="code" href="a00045.html#c4">writePidfile</a>();
00180 
00181   <font class="comment">// Change the file mode mask</font>
00182   ::umask(0);
00183   
00184   <font class="comment">// Change the current working directory</font>
00185   <font class="keywordflow">if</font>(::chdir(<font class="stringliteral">"/"</font>)!=0)
00186   {
00187     cerr&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00188     ::exit(-1);
00189   }
00190 
00191   <font class="comment">// If _tracefile is not set, then use syslog.</font>
00192   <font class="keywordflow">if</font>(<a class="code" href="a00045.html#m0">_tracefile</a> &amp;&amp; <a class="code" href="a00045.html#m0">_tracefile</a>[0]!=<font class="charliteral">'\0'</font>)
00193   {
00194     <a class="code" href="a00045.html#c1">redirectStreamsTo</a>(<a class="code" href="a00045.html#m0">_tracefile</a>);
00195   }
00196   <font class="keywordflow">else</font>
00197   {
00198 <font class="preprocessor">#ifndef HAVE_OMNIORB3</font>
00199 <font class="preprocessor"></font><font class="preprocessor">#  ifdef LOG_PERROR</font>
00200 <font class="preprocessor"></font>    ::openlog(<a class="code" href="a00082.html#a40">PACKAGE_NAME</a> <font class="stringliteral">": "</font>,LOG_PID|LOG_PERROR,LOG_DAEMON);
00201 <font class="preprocessor">#  else</font>
00202 <font class="preprocessor"></font>    ::openlog(<a class="code" href="a00082.html#a40">PACKAGE_NAME</a> <font class="stringliteral">": "</font>,LOG_PID,LOG_DAEMON);
00203 <font class="preprocessor">#  endif</font>
00204 <font class="preprocessor"></font>    <a class="code" href="a00045.html#m6">_haveSyslog</a>=<font class="keyword">true</font>;
00205     <a class="code" href="a00139.html#a0">omniORB::setLogFunction</a>(DaemonImpl::log);
00206 <font class="preprocessor">#else</font>
00207 <font class="preprocessor"></font>    cerr&lt;&lt;<font class="stringliteral">"You must use option -t to set the file for trace messages."</font>
00208       <font class="stringliteral">"\n(This is because omniORB3 cannot redirect messages to syslog.)"</font>&lt;&lt;endl;
00209     ::exit(-1);
00210 <font class="preprocessor">#endif</font>
00211 <font class="preprocessor"></font>  }
00212 } <font class="comment">// end daemonize()</font>
00213 
00214 
<a name="l00215"></a><a class="code" href="a00045.html#a7">00215</a> <font class="keywordtype">void</font> DaemonImpl::runningOk()
00216 {
00217   <font class="keywordflow">if</font>(_haveParent)
00218   {
00219     <a class="code" href="a00045.html#m5">_haveParent</a>=<font class="keyword">false</font>;
00220     <a class="code" href="a00045.html#c6">notifyParent</a>(0);
00221   }
00222 
00223   <font class="comment">// No longer send syslog messages to stderr.</font>
00224   <font class="keywordflow">if</font>(_haveSyslog)
00225   {
00226 <font class="preprocessor">#ifdef LOG_PERROR</font>
00227 <font class="preprocessor"></font>    ::closelog();
00228     <font class="comment">// FIXME: Possible race here? If a log message is sent right now.</font>
00229     ::openlog(<a class="code" href="a00082.html#a40">PACKAGE_NAME</a> <font class="stringliteral">": "</font>,LOG_PID,LOG_DAEMON);
00230 <font class="preprocessor">#endif</font>
00231 <font class="preprocessor"></font>    <a class="code" href="a00045.html#c1">redirectStreamsTo</a>(<font class="stringliteral">"/dev/null"</font>);
00232   }
00233 }
00234 
00235 
<a name="l00236"></a><a class="code" href="a00045.html#a8">00236</a> <font class="keywordtype">void</font> DaemonImpl::shutdown(<font class="keywordtype">int</font> status)
00237 {
00238   <font class="comment">// Remove the pidfile.</font>
00239   <font class="keywordflow">if</font>(<a class="code" href="a00045.html#m4">_havePidfile</a> &amp;&amp; <a class="code" href="a00045.html#m2">_pidfile</a> &amp;&amp; 0!=::unlink(_pidfile))
00240   {
00241     cerr&lt;&lt;<font class="stringliteral">"Failed to remove pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"': "</font>
00242         &lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00243     status=-1;
00244   }
00245   <a class="code" href="a00045.html#m4">_havePidfile</a>=<font class="keyword">false</font>;
00246 
00247   <font class="comment">// Close syslog.</font>
00248   <font class="keywordflow">if</font>(_haveSyslog)
00249   {
00250     <a class="code" href="a00045.html#m6">_haveSyslog</a>=<font class="keyword">false</font>;
00251     ::closelog();
00252   }
00253   
00254   <font class="comment">// Notify the parent.</font>
00255   <font class="keywordflow">if</font>(_haveParent)
00256   {
00257     <a class="code" href="a00045.html#m5">_haveParent</a>=<font class="keyword">false</font>;
00258     <a class="code" href="a00045.html#c6">notifyParent</a>(status);
00259   }
00260 
00261   <font class="comment">// outtahere...</font>
00262 }
00263 
00264 
<a name="l00265"></a><a class="code" href="a00045.html#d0">00265</a> <font class="keywordtype">void</font> DaemonImpl::log(<font class="keyword">const</font> <font class="keywordtype">char</font>* message)
00266 {
00267   <font class="keywordtype">int</font> priority =LOG_INFO;
00268   <font class="comment">// Cut off the redundant package name prefix.</font>
00269   <font class="comment">//  "omniEvents: " --&gt; stripped off</font>
00270   <font class="comment">//  "omniEvents! " --&gt; stripped off and sets priority to LOG_ERR</font>
00271   <font class="keyword">const</font> <font class="keywordtype">char</font>* mPos( message );
00272   <font class="keyword">const</font> <font class="keywordtype">char</font>* pPos( <font class="stringliteral">"omniEvents: "</font> );
00273   <font class="keywordflow">while</font>(*mPos &amp;&amp; (*mPos==*pPos || *pPos==<font class="charliteral">':'</font>))
00274   {
00275     ++mPos;
00276     ++pPos;
00277     <font class="keywordflow">if</font>(!*pPos)
00278     {
00279       <font class="keywordflow">switch</font>(message[10])
00280       {
00281         <font class="keywordflow">case</font> <font class="charliteral">'!'</font>:  priority=LOG_ERR; <font class="comment">// ...AND DROPS THROUGH...</font>
00282         <font class="keywordflow">case</font> <font class="charliteral">':'</font>:  message=mPos;
00283       }
00284       <font class="keywordflow">break</font>; <font class="comment">// loop exit</font>
00285     }
00286   }
00287   <font class="comment">// Send the message.</font>
00288   ::syslog(priority,message);
00289 <font class="preprocessor">#ifndef LOG_PERROR</font>
00290 <font class="preprocessor"></font>  <font class="comment">// If we don't have LOG_PERROR, then we'll have to manually send</font>
00291   <font class="comment">// log messages to stderr.</font>
00292   <font class="keywordflow">if</font>(daemon.<a class="code" href="a00045.html#m5">_haveParent</a>)
00293       cerr&lt;&lt;message&lt;&lt;flush;
00294 <font class="preprocessor">#endif</font>
00295 <font class="preprocessor"></font>}
00296 
00297 
<a name="l00298"></a><a class="code" href="a00045.html#c3">00298</a> <font class="keywordtype">void</font> DaemonImpl::checkPidfileOrShutdown()
00299 {
00300   <font class="keywordflow">if</font>(!<a class="code" href="a00045.html#m2">_pidfile</a>)
00301       <font class="keywordflow">return</font>;
00302 
00303   <font class="comment">// Try to read pidfile.</font>
00304   pid_t pidFromFile =0;
00305   <font class="keyword">struct </font>stat buf;
00306   <font class="keywordflow">if</font>(0==::stat(<a class="code" href="a00045.html#m2">_pidfile</a>,&amp;buf))
00307   {
00308     <font class="keywordflow">if</font>(!S_ISREG(buf.st_mode))
00309     {
00310       cerr&lt;&lt;<font class="stringliteral">"Pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"' is not a regular file."</font>&lt;&lt;endl;
00311       ::exit(-1);
00312     }
00313     <font class="keywordflow">try</font>
00314     {
00315       ifstream infile(<a class="code" href="a00045.html#m2">_pidfile</a>);
00316       infile&gt;&gt;pidFromFile;
00317       infile.close();
00318     }
00319     <font class="keywordflow">catch</font>(...)
00320     {
00321       cerr&lt;&lt;<font class="stringliteral">"Failed to read pidfile'"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"'."</font>&lt;&lt;endl;
00322       ::exit(-1);
00323     }
00324   }
00325   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(errno!=ENOENT)
00326   {
00327     cerr&lt;&lt;<font class="stringliteral">"Failed to stat pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"': "</font>
00328         &lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00329     ::exit(-1);
00330   }
00331 
00332   <font class="comment">// If process 'pidFromFile' is running then exit, else remove pidfile.</font>
00333   <font class="keywordflow">if</font>(pidFromFile&gt;0)
00334   {
00335     <font class="keywordflow">if</font>(0==::kill(pidFromFile,0)) <font class="comment">// tests for running 'pidFromFile'.</font>
00336     {
00337       cerr&lt;&lt;<font class="stringliteral">"Quitting because process "</font>&lt;&lt;pidFromFile
00338           &lt;&lt;<font class="stringliteral">" defined in pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"'"</font>
00339           &lt;&lt;<font class="stringliteral">" is already running."</font>&lt;&lt;endl;
00340       ::exit(-1);
00341     }
00342     <font class="keywordflow">else</font> <font class="keywordflow">if</font>(errno!=ESRCH)
00343     {
00344       cerr&lt;&lt;<font class="stringliteral">"Failed to test for process "</font>&lt;&lt;pidFromFile
00345           &lt;&lt;<font class="stringliteral">" defined in pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"': "</font>
00346           &lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00347       ::exit(-1);
00348     }
00349   }
00350 }
00351 
00352 
<a name="l00353"></a><a class="code" href="a00045.html#c4">00353</a> <font class="keywordtype">void</font> DaemonImpl::writePidfile()
00354 {
00355   <font class="keywordflow">if</font>(_pidfile)
00356   {
00357     <font class="keywordflow">try</font>
00358     {
00359 <font class="preprocessor">#ifdef FSTREAM_OPEN_PROT</font>
00360 <font class="preprocessor"></font>      ofstream outfile(<a class="code" href="a00045.html#m2">_pidfile</a>,ios::out|ios::trunc,0644);
00361 <font class="preprocessor">#else</font>
00362 <font class="preprocessor"></font>      ofstream outfile(<a class="code" href="a00045.html#m2">_pidfile</a>,ios::out|ios::trunc);
00363 <font class="preprocessor">#endif</font>
00364 <font class="preprocessor"></font>      outfile&lt;&lt;::getpid()&lt;&lt;endl;
00365       outfile.close();
00366       <font class="comment">// Tell shutdown() that the pidfile needs to be cleared away.</font>
00367       <a class="code" href="a00045.html#m4">_havePidfile</a>=<font class="keyword">true</font>;
00368     }
00369     <font class="keywordflow">catch</font>(...)
00370     {
00371       cerr&lt;&lt;<font class="stringliteral">"Failed to write pidfile '"</font>&lt;&lt;<a class="code" href="a00045.html#m2">_pidfile</a>&lt;&lt;<font class="stringliteral">"'."</font>&lt;&lt;endl;
00372       ::exit(-1);
00373     }
00374   }
00375 }
00376 
00377 
<a name="l00378"></a><a class="code" href="a00045.html#c0">00378</a> <font class="keywordtype">void</font> DaemonImpl::fork()
00379 {
00380   <font class="keywordflow">if</font>( ::pipe(_pipe) &lt;0)
00381   {
00382     cerr&lt;&lt;<font class="stringliteral">"Failed to open pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00383     ::exit(-1);
00384   }
00385 
00386   <font class="comment">// Fork off from the parent process</font>
00387   pid_t pid =::fork();
00388   <font class="keywordflow">if</font>(pid&lt;0)
00389   {
00390     cerr&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00391     ::exit(-1);
00392   }
00393   <font class="keywordflow">else</font> <font class="keywordflow">if</font>(pid&gt;0)
00394   {
00395     <font class="comment">//</font>
00396     <font class="comment">// Now in the PARENT</font>
00397     <font class="comment">//</font>
00398 
00399     <font class="comment">// Close the write end of the pipe.</font>
00400     <font class="keywordflow">if</font>( ::close(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a3">PIPE_WRITE</a>]) &lt;0)
00401         cerr&lt;&lt;<font class="stringliteral">"Failed to close pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00402 
00403     ::_exit(<a class="code" href="a00045.html#c5">waitForChild</a>()); <font class="comment">// Exit without flushing buffers</font>
00404   }
00405 
00406   <font class="comment">//</font>
00407   <font class="comment">// ...now in the CHILD.</font>
00408   <font class="comment">//</font>
00409 
00410   <a class="code" href="a00045.html#m5">_haveParent</a>=<font class="keyword">true</font>;
00411 
00412   <font class="comment">// Close the read end of the pipe</font>
00413   <font class="keywordflow">if</font>( ::close(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a2">PIPE_READ</a>]) &lt;0)
00414       cerr&lt;&lt;<font class="stringliteral">"Failed to close pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00415 
00416   <font class="comment">// Create a new SID for the child process</font>
00417   pid_t sid =::setsid();
00418   <font class="keywordflow">if</font>(sid&lt;0)
00419   {
00420     cerr&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00421     ::exit(-1);
00422   }
00423 }
00424 
00425 
<a name="l00426"></a><a class="code" href="a00045.html#c1">00426</a> <font class="keywordtype">void</font> DaemonImpl::redirectStreamsTo(<font class="keyword">const</font> <font class="keywordtype">char</font>* filename)
00427 {
00428   <font class="keywordflow">if</font>(<a class="code" href="a00045.html#c2">openFileFor</a>(<a class="code" href="a00099.html#a0">STDIN_FILENO</a>,<font class="stringliteral">"/dev/null"</font>,O_RDONLY)&lt;0)
00429   {
00430     cerr&lt;&lt;<font class="stringliteral">"Failed to open /dev/null for STDIN: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00431     ::exit(-1);
00432   }
00433   <font class="keywordflow">if</font>(<a class="code" href="a00045.html#c2">openFileFor</a>(<a class="code" href="a00099.html#a1">STDOUT_FILENO</a>,filename,O_WRONLY|O_CREAT|O_APPEND)&lt;0)
00434   {
00435     cerr&lt;&lt;<font class="stringliteral">"Failed to open "</font>&lt;&lt;filename&lt;&lt;<font class="stringliteral">" for STDOUT: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00436     ::exit(-1);
00437   }
00438   <font class="keywordflow">if</font>(<a class="code" href="a00045.html#c2">openFileFor</a>(STDERR_FILENO,filename,O_WRONLY|O_CREAT|O_APPEND)&lt;0)
00439   {
00440     cerr&lt;&lt;<font class="stringliteral">"Failed to open "</font>&lt;&lt;filename&lt;&lt;<font class="stringliteral">" for STDERR: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00441     ::exit(-1);
00442   }
00443 }
00444 
00445 
<a name="l00446"></a><a class="code" href="a00045.html#c2">00446</a> <font class="keywordtype">int</font> DaemonImpl::openFileFor(<font class="keywordtype">int</font> fd, <font class="keyword">const</font> <font class="keywordtype">char</font>* filename, <font class="keywordtype">int</font> flags)
00447 {
00448   <font class="keywordtype">int</font> newfd =::open(filename,flags,0644);
00449   <font class="keywordflow">if</font>(newfd&lt;0)
00450       <font class="keywordflow">return</font> -1;
00451   <font class="keywordflow">if</font>(newfd==fd)
00452       <font class="keywordflow">return</font> fd;
00453   <font class="keywordflow">if</font>(::dup2(newfd,fd)&lt;0) <font class="comment">// replace fd with a copy of newfd</font>
00454       <font class="keywordflow">return</font> -1;
00455   ::close(newfd);
00456   <font class="keywordflow">return</font> fd;
00457 }
00458 
00459 
<a name="l00460"></a><a class="code" href="a00045.html#c5">00460</a> <font class="keywordtype">int</font> DaemonImpl::waitForChild()
00461 {
00462   <font class="keywordtype">int</font> status =-1;
00463   ssize_t bytes =::read(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a2">PIPE_READ</a>],&amp;status,<font class="keyword">sizeof</font>(status));
00464   <font class="keywordflow">if</font>(bytes&lt;<font class="keyword">sizeof</font>(status))
00465   {
00466     status=-1;
00467     <font class="keywordflow">if</font>(bytes&lt;0)
00468        cerr&lt;&lt;<font class="stringliteral">"Parent failed to read result from pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00469   }
00470   <font class="keywordflow">if</font>( ::close(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a2">PIPE_READ</a>]) !=0)
00471       cerr&lt;&lt;<font class="stringliteral">"Failed to close pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00472 
00473   <font class="keywordflow">return</font> status;
00474 }
00475 
00476 
<a name="l00477"></a><a class="code" href="a00045.html#c6">00477</a> <font class="keywordtype">void</font> DaemonImpl::notifyParent(<font class="keywordtype">int</font> status)
00478 {
00479   ssize_t r =::write(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a3">PIPE_WRITE</a>],&amp;status,<font class="keyword">sizeof</font>(status));
00480   <font class="keywordflow">if</font>(r&lt;<font class="keyword">sizeof</font>(status))
00481   {
00482     <font class="keywordflow">if</font>(r&lt;0)
00483         cerr&lt;&lt;<font class="stringliteral">"read() failed while writing return value to pipe: "</font>
00484             &lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00485     <font class="keywordflow">else</font>
00486         cerr&lt;&lt;<font class="stringliteral">"write() too short while writing return value from pipe: "</font>
00487             &lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00488   }
00489   <font class="keywordflow">if</font>( ::close(<a class="code" href="a00045.html#m3">_pipe</a>[<a class="code" href="a00086.html#a3">PIPE_WRITE</a>]) !=0)
00490       cerr&lt;&lt;<font class="stringliteral">"Failed to close pipe: "</font>&lt;&lt;<a class="code" href="a00086.html#a1">STRERR_FILE_LINE</a>&lt;&lt;endl;
00491 }
00492 
00493 } <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>