Debian Codegraph @

script/deb-systemd-invoke

raw
1 #!/usr/bin/perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2013 Michael Stapelberg <[email protected]>
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # * Neither the name of Michael Stapelberg nor the
18 # names of contributors may be used to endorse or promote products
19 # derived from this software without specific prior written permission.
20 # .
21 # THIS SOFTWARE IS PROVIDED BY Michael Stapelberg ''AS IS'' AND ANY
22 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL Michael Stapelberg BE LIABLE FOR ANY
25 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 =head1 NAME
33
34 deb-systemd-invoke - wrapper around systemctl, respecting policy-rc.d
35
36 =head1 SYNOPSIS
37
38 B<deb-systemd-invoke> [B<--user>] start|stop|restart S<I<unit file> ...>
39 B<deb-systemd-invoke> [B<--user>] [B<--no-dbus>] daemon-reload|daemon-reexec
40
41 =head1 DESCRIPTION
42
43 B<deb-systemd-invoke> is a Debian-specific helper script which asks
44 /usr/sbin/policy-rc.d before performing a systemctl call.
45
46 B<deb-systemd-invoke> is intended to be used from maintscripts to manage
47 systemd unit files. It is specifically NOT intended to be used interactively by
48 users. Instead, users should run systemd and use systemctl, or not bother about
49 the systemd enabled state in case they are not running systemd.
50
51 =cut
52
53 use strict;
54 use warnings;
55 use Getopt::Long; # in core since Perl 5
56
57 if (@ARGV < 2) {
58 print STDERR "Syntax: $0 <action> [<unit file> [<unit file> ...]]\n";
59 exit 1;
60 }
61
62 my $is_system = 1;
63 my $use_dbus = 1;
64 my @instances = ();
65 my $result = GetOptions(
66 "user" => sub { $is_system = 0; },
67 "system" => sub { $is_system = 1; }, # default
68 "no-dbus" => sub { $use_dbus = 0; },
69 );
70
71 my $policyhelper = '/usr/sbin/policy-rc.d';
72 if (length $ENV{DPKG_ROOT}) {
73 $policyhelper = $ENV{DPKG_ROOT} . $policyhelper;
74 }
75 my @units = @ARGV;
76 my $action = shift @units;
77 if (-x $policyhelper) {
78 for my $unit (@units) {
79 system(qq|$policyhelper $unit "$action"|);
80
81 # 0 or 104 means run
82 # 101 means do not run
83 my $exitcode = ($? >> 8);
84 if ($exitcode == 101) {
85 print STDERR "$policyhelper returned 101, not running '" . join(' ', @ARGV) . "'\n";
86 exit 0;
87 } elsif ($exitcode != 104 && $exitcode != 0) {
88 print STDERR "deb-systemd-invoke only supports $policyhelper return codes 0, 101, and 104!\n";
89 print STDERR "Got return code $exitcode, ignoring.\n";
90 }
91 }
92 }
93
94 if (!$is_system) {
95 # '--machine <ID>@' was added in v250 and v249.10, before that we can't talk to arbitrary user instances
96 my $systemctl_version = `systemctl --version --quiet | sed -n -r "s/systemd ([0-9]+) \\(.*/\\1/p"`;
97 chomp ($systemctl_version);
98 if (system('dpkg', '--compare-versions', $systemctl_version, 'ge', '249') != 0) {
99 print STDERR "systemctl version $systemctl_version does not support acting on user instance, skipping\n";
100 exit 0;
101 }
102
103 # Each user instance of the manager has a corresponding user@<id<.service unit.
104 # Get the full list of IDs, so that we can talk to each user instance to start/stop
105 # user units.
106 @instances = `systemctl --no-legend --quiet list-units 'user@*' | sed -n -r 's/.*user@([0-9]+).service.*/\\1/p'`;
107 } else {
108 push @instances, 'system';
109 }
110
111 # If the job is disabled and is not currently running, the job is not started or restarted.
112 # However, if the job is disabled but has been forced into the running state, we *do* stop
113 # and restart it since this is expected behaviour for the admin who forced the start.
114 # We don't autostart static units either.
115 if ($action eq "start" || $action eq "restart") {
116 my $global_exit_code = 0;
117 my @start_units = ();
118
119 for my $instance (@instances) {
120 my @instance_args = ();
121
122 if ($instance eq 'system') {
123 push @instance_args, '--system';
124 } else {
125 chomp ($instance);
126 push @instance_args, '--user', '--machine', "$instance@";
127 }
128
129 for my $unit (@units) {
130 my $unit_installed = 0;
131 my $enabled_output = `systemctl @instance_args is-enabled -- '$unit'`;
132 # matching enabled and enabled-runtime as an installed non static unit
133 if ($enabled_output =~ /enabled/) {
134 $unit_installed = 1;
135 }
136 system('systemctl', @instance_args, '--quiet', 'is-active', '--', $unit);
137 my $unit_active = $?>>8 == 0 ? 1 : 0;
138 if (!$unit_installed && $action eq "start") {
139 print STDERR "$unit is a disabled or a static unit, not starting it.\n";
140 } elsif (!$unit_installed && !$unit_active && $action eq "restart") {
141 print STDERR "$unit is a disabled or a static unit not running, not starting it.\n";
142 }
143 else {
144 push @start_units, $unit;
145 }
146 }
147 if (@start_units) {
148 system('systemctl', '--quiet', @instance_args, $action, @start_units) == 0 or die("Could not execute systemctl: $!");
149 }
150 }
151 exit(0);
152 } elsif ($action eq "stop" && !$is_system) {
153 my $global_exit_code = 0;
154
155 for my $instance (@instances) {
156 chomp ($instance);
157 system('systemctl', '--quiet', '--user', '--machine', "$instance@", $action, @units);
158 }
159 exit(0);
160 } elsif (($action eq "daemon-reload" || $action eq "daemon-reexec") && !$is_system && $use_dbus) {
161 my $global_exit_code = 0;
162
163 for my $instance (@instances) {
164 chomp ($instance);
165 system('systemctl', '--quiet', '--user', '--machine', "$instance@", $action);
166 }
167 exit(0);
168 } elsif (($action eq "daemon-reload" || $action eq "daemon-reexec") && !$use_dbus) {
169 my $global_exit_code = 0;
170 my $signal;
171
172 if ($action eq "daemon-reload") {
173 $signal = 'SIGHUP';
174 } else {
175 $signal = 'SIGRTMIN+25';
176 }
177
178 if ($is_system) {
179 system('kill', '-s', $signal, '1');
180 } else {
181 system('systemctl', '--quiet', 'kill', '--kill-whom=main', '--signal', $signal, 'user@*.service');
182 }
183
184 exit(0);
185 } else {
186 exec('systemctl', @ARGV);
187 }