File Coverage

blib/lib/Systemd/Daemon.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             # Generated by Makefile.PL from Daemon.pm.in #
2              
3             =encoding UTF-8
4              
5             =head1 NAME
6              
7             Systemd::Daemon — Write systemd-aware daemons in Perl.
8              
9             =head1 VERSION
10              
11             Version 0.06.
12              
13             =head1 SYNOPSIS
14              
15             B: The module is in experimental state, interface will be changed in the future.
16              
17             Functions:
18              
19             use Systemd::Daemon qw{ :funcs };
20              
21             notify( RELOADING => 1 );
22             while ( ... ) {
23             notify( STATUS => "Loading, $percent\% done" );
24             ...
25             };
26              
27             notify( READY => 1, STATUS => "Ready" );
28             ...
29              
30             notify( STOPPING => 1 );
31             ...
32              
33             Low-level bare C functions:
34              
35             use Systemd::Daemon qw{ :bare };
36              
37             sd_notify( 0, "READY=1\nSTATUS=Ready\n" );
38             sd_pid_notify( $pid, 0, "READ=1\nSTATUS=Ready\n" );
39             if ( sd_booted() > 0 ) { ... };
40              
41             =cut
42              
43             package Systemd::Daemon;
44              
45 1     1   14042 use 5.006;
  1         3  
  1         36  
46 1     1   5 use strict;
  1         1  
  1         31  
47 1     1   3 use warnings FATAL => 'all';
  1         4  
  1         56  
48              
49             our $VERSION = '0.06';
50              
51             use Systemd::Daemon::Inline
52 0           C => 'DATA',
53             filters => 'Strip_POD',
54             libs => '-lsystemd',
55             autowrap => 1,
56             clean_after_build => 1,
57             # TODO: Is this comment still valid?
58             # `clean_after_build => 0` causes `make install` to fail, see
59             # https://github.com/ingydotnet/inline-pm/issues/53
60             # `clean_after_build => 1` causes errors when building RPM: rpmbuild
61             # tries to extract debug information from files which were wiped out.
62 1     1   256 ;
  0            
63              
64             =head1 EXPORT
65              
66             No functions are exported by default. There are 3 import tags:
67              
68             =over
69              
70             =item :funcs
71              
72             Import higher-level Perl wrappers.
73              
74             =item :bare
75              
76             Import low-level bare C functions.
77              
78             =item :all
79              
80             Import all the functions.
81              
82             =back
83              
84             Also, each function can be imported individually, e. g.:
85              
86             use Systemd::Daemon qw{ notify };
87              
88             =cut
89              
90             use Exporter qw{ import };
91             BEGIN {
92             our @EXPORT_OK;
93             our %EXPORT_TAGS = (
94             funcs => [ qw{ notify } ],
95             bare => [ qw{ sd_notify sd_pid_notify sd_booted sd_watchdog_enabled } ],
96             );
97             Exporter::export_ok_tags( qw{ funcs bare } );
98             $EXPORT_TAGS{ all } = \@EXPORT_OK;
99             }; # BEGIN
100              
101             =head1 SUBROUTINES
102              
103             =head2 Perl subroutines
104              
105             =head3 notify( VAR => VALUE, ... )
106              
107             C is Perl wrapper for C functions C and C, so read
108             L first.
109              
110             C functions accept status as one string of newline-separated variable assignments, e. g.:
111              
112             sd_notify( 0, "RELOADING=1\nSTATUS=50% done\n" );
113              
114             Unlike to C functions, C accepts each variable separately as Perl "named arguments", e. g.:
115              
116             notify( RELOADING => 1, STATUS => '50% done' );
117              
118             C and C parameters can be specified as named arguments C and C
119             respectively, e. g.:
120              
121             notify( pid => $pid, unset => 1, ... );
122              
123             If C value is defined and not zero, C calls C, otherwise C
124             is called. C is defaulted to zero.
125              
126             L describes some
127             "well-known" variable assignments, for example, C. Systemd's reaction on assignment
128             C is not defined. In my experiments with systemd v217 any value but C<1> does not have
129             any effect. To make C more Perlish, C, C, C, and C
130             arguments are normalized: if its value is false (e. g. undef, zero or empty string), the respective
131             variable is not passed to underlying C function; if its value is true (not false), the respective
132             variable is set to C<1>.
133              
134             C returns result of underlying C (or C). It should be negative
135             integer in case of error, zero if C<$ENV{ NOTIFY_SOCKET }> is not set (and so, systemd cannot be
136             notified), and some positive value in case of success. However, L recommends to
137             ignore return value.
138              
139             =cut
140              
141             sub notify(@) {
142             my ( %args ) = @_;
143             my $pid = ( delete( $args{ pid } ) or 0 );
144             my $unset = ( delete( $args{ unset } ) or 0 );
145             foreach my $k ( qw{ READY RELOADING STOPPING WATCHDOG } ) {
146             delete( $args{ $k } ) and $args{ $k } = 1;
147             }; # foreach
148             my $state = join( '', map( "$_=$args{ $_ }\n", keys( %args ) ) );
149             my $rc;
150             if ( $pid != 0 ) {
151             $rc = sd_pid_notify( $pid, $unset, $state );
152             } else {
153             $rc = sd_notify( $unset, $state );
154             }; # if
155             return $rc;
156             }; # sub notify
157              
158             1;
159              
160             __DATA__