This file is indexed.

/usr/share/perl5/Proc/Daemon.pod is in libproc-daemon-perl 0.14-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
=head1 NAME

Proc::Daemon - Run Perl program(s) as a daemon process.




=head1 SYNOPSIS

    use Proc::Daemon;

    $daemon = Proc::Daemon->new(
        work_dir => '/my/daemon/directory',
        .....
    );

    $Kid_1_PID = $daemon->Init;

    unless ( $Kid_1_PID ) {
        # code executed only by the child ...
    }

    $Kid_2_PID = $daemon->Init( { 
                    work_dir     => '/other/daemon/directory',
                    exec_command => 'perl /home/my_script.pl',
                 } );

    $pid = $daemon->Status( ... );

    $stopped = $daemon->Kill_Daemon( ... );




=head1 DESCRIPTION

This module can be used by a Perl program to initialize itself as a daemon
or to execute (C<exec>) a system command as daemon. You can also check the status
of the daemon (alive or dead) and you can kill the daemon.

A daemon is a process that runs in the background with no controlling
terminal. Generally servers (like FTP, HTTP and SIP servers) run as daemon
processes. Do not make the mistake to think that a daemon is a server. ;-)

Proc::Daemon does the following:


=over 4

=item 1

The script forks a child.


=item 2

The child changes the current working directory to
the value of 'work_dir'.


=item 3

The child clears the file creation mask.


=item 4

The child becomes a session leader, which detaches the program from the
controlling terminal.


=item 5

The child forks another child (the final daemon process). This prevents
the potential of acquiring a controlling terminal at all and detaches the
daemon completely from the first parent.


=item 6

The second child closes all open file descriptors (unless you define
C<dont_close_fh> and/or C<dont_close_fd>).


=item 7

The second child opens STDIN, STDOUT and STDERR to the location defined in the
constructor (C<new>).


=item 8

The second child returns to the calling script, or the program defined
in 'exec_command' is executed and the second child never returns.


=item 9

The first child transfers the PID of the second child (daemon) to the
parent. Additionally the PID of the daemon process can be written into a file
if 'pid_file' is defined. Then the first child exits.


=item 10

If the parent script is looking for a return value, then the PID(s) of the
child/ren will be returned. Otherwise the parent will exit.

=back


NOTE: Because of the second fork the daemon will not be a session-leader and
therefore Signals will not be send to other members of his process group. If
you need the functionality of a session-leader you may want to call
POSIX::setsid() manually at your daemon.


INFO: Since C<fork> is not performed the same way on Windows systems as on
Linux, this module does not work with Windows. Patches appreciated!




=head1 CONSTRUCTOR


=over 4

=item new ( %ARGS )

The constructor creates a new Proc::Daemon object based on the hash %ARGS. The
following keys from %ARGS are used:


=over 8

=item work_dir

Defines the path to the working directory of your daemon. Defaults to C</>.


=item setuid

Sets the real user identifier (C<< $< >>) and the effective user identifier
(C<< $> >>) for the daemon process using C<POSIX::setuid( ... )>, in case you
want to run your daemon under a different user from the parent. Obviously the
first user must have the rights to switch to the new user otherwise it will
stay the same. It is helpful to define the argument C<setuid> if you start your
script at boot time by init with the superuser, but wants the daemon to run
under a normal user account.


=item child_STDIN

Defines the path to STDIN for your daemon. Defaults to C</dev/null>. Default
Mode is '<' (read). You can define other Mode the same way as you do using
Perls C<open> in a two-argument form.


=item child_STDOUT

Defines the path where the output of your daemon will go. Defaults to
C</dev/null>. Default Mode is '+>' (write/read). You can define other Mode the
same way as you do using Perls C<open> in a two-argument form.


=item child_STDERR

Defines the path where the error output of your daemon will go. Defaults to
C</dev/null>. Default Mode is '+>' (write/read). You can define other Mode the
same way as you do using Perls C<open> in a two-argument form, see example
below.


=item dont_close_fh

If you define it, it must be an arrayref with file handles you want to preserve
from the parent into the child (daemon). This may be the case if you have code
below a C<__DATA__> token in your script or module called by C<use> or
C<require>.

    dont_close_fh => [ 'main::DATA', 'PackageName::DATA', $my_filehandle, ... ],

You can add any kind of file handle to the array (expression in single quotes or
a scalar variable), including 'STDIN', 'STDOUT' and 'STDERR'. Logically the path
settings from above (C<child_STDIN>, ...) will be ignored in this case.

DISCLAIMER: Using this argument may not detach your daemon fully from the
parent! Use it at your own risk.


=item dont_close_fd

Same function and disclaimer as C<dont_close_fh>, but instead of file handles
you write the numeric file descriptors inside the arrayref.


=item pid_file

Defines the path to a file (owned by the parent user) where the PID of the
daemon process will be stored. Defaults to C<undef> (= write no file).


=item exec_command

Scalar or arrayref with system command(s) that will be executed by the
daemon via Perls C<exec PROGRAM_LIST>. In this case the child will never
return to the parents process!

=back


Example:

    my $daemon = Proc::Daemon->new(
        work_dir     => '/working/daemon/directory',
        child_STDOUT => '/path/to/daemon/output.file',
        child_STDERR => '+>>debug.txt',
        pid_file     => 'pid.txt',
        exec_command => 'perl /home/my_script.pl',
      # or:
      # exec_command => [ 'perl /home/my_script.pl', 'perl /home/my_other_script.pl' ],
    );

In this example:

=over 8

=item *

the PID of the daemon will be returned to C<$daemon> in the parent process
and a pid-file will be created at C</working/daemon/directory/pid.txt>.

=item *

STDOUT will be open with Mode '+>' (write/read)
to C</path/to/daemon/output.file> and STDERR will be open
to C</working/daemon/directory/debug.txt> with Mode '+>>' (write/read opened for
appending).

=item *

the script C</home/my_script.pl> will be executed by C<perl> and run as
daemon. Therefore the child process will never return to this parent script.

=back

=back




=head1 METHODS

=over 4

=item Init( [ { %ARGS } ] )

Become a daemon.

If used for the first time after C<new>, you call C<Init> with the object
reference to start the daemon.

    $pid = $daemon->Init();

If you want to use the object reference created by C<new> for other daemons,
you write C<Init( { %ARGS } )>. %ARGS are the same as described in
C<new>. Notice that you shouldn't call C<Init()> without argument in this case,
or the next daemon will execute and/or write in the same files as the first
daemon. To prevent this use at least an empty anonymous hash here.

    $pid = $daemon->Init( {} );
    @pid = $daemon->Init( {
        work_dir     => '/other/daemon/directory',
        exec_command => [ 'perl /home/my_second_script.pl', 'perl /home/my_third_script.pl' ],
    } );

If you don't need the Proc::Daemon object reference in your script, you
can also use the method without object reference:

    $pid = Proc::Daemon::Init();
    # or
    $pid = Proc::Daemon::Init( { %ARGS } );

C<Init> returns the PID (scalar) of the daemon to the parent, or the PIDs
(array) of the daemons created if C<exec_command> has more then one program
to execute. See examples above.

C<Init> returns 0 to the child (daemon).

If you call the C<Init> method in the context without looking for a return value
(void context) the parent process will C<exit> here like in earlier versions:

    Proc::Daemon::Init();


=item Status( [ $ARG ] )

This function checks the status of the process (daemon). Returns the PID number
(alive) or 0 (dead).

$ARG can be a string with:

=over 8

=item *

C<undef>, in this case it tries to get the PID to check out of the object
reference settings.

=item *

a PID number to check.

=item *

the path to a file containing the PID to check.

=item *

the command line entry of the running program to check. This requires
L<Proc::ProcessTable> to be installed.

=back


=item Kill_Daemon( [ $ARG [, SIGNAL] ] )

This function kills the Daemon process. Returns the number of processes
successfully killed (which mostly is not the same as the PID number), or 0 if
the process wasn't found. 

$ARG is the same as of C<Status()>. SIGNAL is an optional signal name or number
as required by Perls C<kill> function and listed out by C<kill -l> on your
system. Default value is 9 ('KILL' = non-catchable, non-ignorable kill).


=item Fork

Is like the Perl built-in C<fork>, but it retries to fork over 30 seconds if
necessary and if possible to fork at all. It returns the child PID to the
parent process and 0 to the child process. If the fork is unsuccessful
it C<warn>s and returns C<undef>.

=back




=head1 OTHER METHODS

Proc::Daemon also defines some other functions. See source code for more
details:

=over 4

=item OpenMax( [ $NUMBER ] )

Returns the maximum file descriptor number. If undetermined $NUMBER will be
returned.


=item adjust_settings

Does some fixes/adjustments on the C<new> settings together with C<fix_filename>.


=item fix_filename( $KEYNAME )

Prevents double use of same filename in different processes.


=item get_pid( [ $STRING ] )

Returns the wanted PID if it can be found.


=item get_pid_by_proc_table_attr( $ATTR, $MATCH )

Returns the wanted PID by looking into the process table, or C<undef>. Requires
the C<Proc::ProcessTable> module to be installed.


=back




=head1 NOTES

C<Proc::Daemon::init> is still available for backwards capability.




=head1 AUTHORS

Primary-maintainer and code writer until version 0.03:

=over 4

=item *

Earl Hood, earl@earlhood.com, http://www.earlhood.com/

=back


Co-maintainer and code writer since version 0.04:

=over 4

=item *

Detlef Pilzecker, http://search.cpan.org/~deti/,
http://www.secure-sip-server.net/

=back




=head1 CREDITS

Initial implementation of C<Proc::Daemon> derived from the following sources:

=over 4

=item *

"Advanced Programming in the UNIX Environment" by W. Richard Stevens.
Addison-Wesley, Copyright 1992.

=item *

"UNIX Network Programming", Vol 1, by W. Richard Stevens.
Prentice-Hall PTR, Copyright 1998.

=back




=head1 PREREQUISITES

This module requires the C<POSIX> module to be installed.

The C<Proc::ProcessTable> module is not essentially required but it can be
useful if it is installed (see above).




=head1 SEE ALSO

L<perl(1)>, L<POSIX>, L<Proc::ProcessTable>




=head1 COPYRIGHT

This module is Copyright (C) 1997-2011 by Earl Hood and Detlef Pilzecker.

All Rights Reserved.

This module is free software. It may be used, redistributed and/or modified
under the same terms as Perl itself.