File: Daemon.pm

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (90 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download | duplicates (5)
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
package Munin::Common::Daemon;


use warnings;
use strict;

use English;
use IO::Socket;


sub emit_sd_notify_message {
    eval {
        my $socket_path = $ENV{NOTIFY_SOCKET};
        if (defined $socket_path) {
            # Prevent children from talking to the socket provided solely for us.
            delete $ENV{NOTIFY_SOCKET};
            # A socket path starting with "@" is interpreted as a Linux abstract namespace socket.
            # This can be indicated by the socket path starting with a null byte.
            # See "Address format: abstract" in "man 7 unix".
            $socket_path =~ s/^@/\0/;
            my $socket = IO::Socket::UNIX->new(Type => SOCK_DGRAM, Peer => $socket_path);
            if (defined $socket) {
                print($socket "READY=1\n");
                close($socket);
            }
        }
    }
}


1;


__END__

=head1 NAME

Munin::Common::Daemon - utilities for daemons.

=head1 SYNOPSIS

The following daemon-related features are supported:

=over

=item sd_notify: signal readiness of the daemon

=back


=head1 SUBROUTINES

=head2 emit_sd_notify_message

Example:

 emit_sd_notify_message();

Send a "ready" signal according to the C<sd_notify> interface:

=over

=item 1. check whether the environment variable "NOTIFY_SOCKET" is defined

=item 2. remove this variable from the environment (this interface is not propagated to children)

=item 3. send the string "READY=1" to the socket

=back

The function returns silently, if something fails.

The function should be called as soon as the service is ready to accept
requests.
Calling this function is always safe - independent of the caller supporting the
C<sd_notify> interface or not.

Examples for callers supporting the C<sd_notify> interface:


=over

=item systemd: see C<Type=Notify> in L<systemd.exec/5>

=item start-stop-daemon: see C<--notify-await> in L<start-stop-daemon/8>

=back

See the L<specification of "sd_notify"|https://www.freedesktop.org/software/systemd/man/sd_notify.html>
for further details of this interface.