You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

112 lines
4.3 KiB

  1. #!/usr/bin/env perl
  2. # vim:ts=4:sw=4:expandtab
  3. # © 2013 Michael Stapelberg <stapelberg@debian.org>
  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. =head1 NAME
  32. deb-systemd-invoke - wrapper around systemctl, respecting policy-rc.d
  33. =head1 SYNOPSIS
  34. B<deb-systemd-invoke> start|stop|restart S<I<unit file> ...>
  35. =head1 DESCRIPTION
  36. B<deb-systemd-invoke> is a Debian-specific helper script which asks
  37. /usr/sbin/policy-rc.d before performing a systemctl call.
  38. B<deb-systemd-invoke> is intended to be used from maintscripts to start
  39. systemd unit files. It is specifically NOT intended to be used interactively by
  40. users. Instead, users should run systemd and use systemctl, or not bother about
  41. the systemd enabled state in case they are not running systemd.
  42. =cut
  43. use strict;
  44. use warnings;
  45. if (@ARGV < 2) {
  46. print STDERR "Syntax: $0 <action> <unit file> [<unit file> ...]\n";
  47. exit 1;
  48. }
  49. my $policyhelper = '/usr/sbin/policy-rc.d';
  50. my @units = @ARGV;
  51. my $action = shift @units;
  52. if (-x $policyhelper) {
  53. for my $unit (@units) {
  54. system(qq|$policyhelper $unit "$action"|);
  55. # 0 or 104 means run
  56. # 101 means do not run
  57. my $exitcode = ($? >> 8);
  58. if ($exitcode == 101) {
  59. print STDERR "$policyhelper returned 101, not running '" . join(' ', @ARGV) . "'\n";
  60. exit 0;
  61. } elsif ($exitcode != 104 && $exitcode != 0) {
  62. print STDERR "deb-systemd-invoke only supports $policyhelper return codes 0, 101, and 104!\n";
  63. print STDERR "Got return code $exitcode, ignoring.\n";
  64. }
  65. }
  66. }
  67. # If the job is disabled and is not currently running, the job is not started or restarted.
  68. # However, if the job is disabled but has been forced into the running state, we *do* stop
  69. # and restart it since this is expected behaviour for the admin who forced the start.
  70. # We don't autostart static units either.
  71. if ($action eq "start" || $action eq "restart") {
  72. my $global_exit_code = 0;
  73. my @start_units = ();
  74. for my $unit (@units) {
  75. my $unit_installed = 0;
  76. my $enabled_output = `/bin/systemctl is-enabled -- '$unit'`;
  77. # matching enabled and enabled-runtime as an installed non static unit
  78. if ($enabled_output =~ /enabled/) {
  79. $unit_installed = 1;
  80. }
  81. system('/bin/systemctl', '--quiet', 'is-active', '--', $unit);
  82. my $unit_active = $?>>8 == 0 ? 1 : 0;
  83. if (!$unit_installed && $action eq "start") {
  84. print STDERR "$unit is a disabled or a static unit, not starting it.\n";
  85. } elsif (!$unit_installed && !$unit_active && $action eq "restart") {
  86. print STDERR "$unit is a disabled or a static unit not running, not starting it.\n";
  87. }
  88. else {
  89. push @start_units, $unit;
  90. }
  91. }
  92. if (@start_units) {
  93. exec('/bin/systemctl', $action, @start_units) or die("Could not execute systemctl: $!");
  94. }
  95. exit(0);
  96. } else {
  97. exec '/bin/systemctl', @ARGV;
  98. }