Debian Codegraph @

script/update-rc.d

raw
1 #! /usr/bin/perl
2 # vim: ft=perl
3 #
4 # update-rc.d Update the links in /etc/rc[0-9S].d/
5 #
6
7 use strict;
8 use warnings;
9 # NB: All Perl modules used here must be in perl-base. Specifically, depending
10 # on modules in perl-modules is not okay! See bug #716923
11
12 my $initd = "/etc/init.d";
13 my $etcd = "/etc/rc";
14 my $dpkg_root = $ENV{DPKG_ROOT} // '';
15
16 # Print usage message and die.
17
18 sub usage {
19 print STDERR "update-rc.d: error: @_\n" if ($#_ >= 0);
20 print STDERR <<EOF;
21 usage: update-rc.d [-f] <basename> remove
22 update-rc.d [-f] <basename> defaults
23 update-rc.d [-f] <basename> defaults-disabled
24 update-rc.d <basename> disable|enable [S|2|3|4|5]
25 -f: force
26
27 The disable|enable API is not stable and might change in the future.
28 EOF
29 exit (1);
30 }
31
32 exit main(@ARGV);
33
34 sub info {
35 print STDOUT "update-rc.d: @_\n";
36 }
37
38 sub warning {
39 print STDERR "update-rc.d: warning: @_\n";
40 }
41
42 sub error {
43 print STDERR "update-rc.d: error: @_\n";
44 exit (1);
45 }
46
47 sub error_code {
48 my $rc = shift;
49 print STDERR "update-rc.d: error: @_\n";
50 exit ($rc);
51 }
52
53 sub make_path {
54 my ($path) = @_;
55 my @dirs = ();
56 my @path = split /\//, $path;
57 map { push @dirs, $_; mkdir join('/', @dirs), 0755; } @path;
58 }
59
60 # Given a script name, return any runlevels except 0 or 6 in which the
61 # script is enabled. If that gives nothing and the script is not
62 # explicitly disabled, return 6 if the script is disabled in runlevel
63 # 0 or 6.
64 sub script_runlevels {
65 my ($scriptname) = @_;
66 my @links=<"$dpkg_root/etc/rc[S12345].d/S[0-9][0-9]$scriptname">;
67 if (@links) {
68 return map(substr($_, 7, 1), @links);
69 } elsif (! <"$dpkg_root/etc/rc[S12345].d/K[0-9][0-9]$scriptname">) {
70 @links=<"$dpkg_root/etc/rc[06].d/K[0-9][0-9]$scriptname">;
71 return ("6") if (@links);
72 } else {
73 return ;
74 }
75 }
76
77 # Map the sysvinit runlevel to that of openrc.
78 sub openrc_rlconv {
79 my %rl_table = (
80 "S" => "sysinit",
81 "1" => "recovery",
82 "2" => "default",
83 "3" => "default",
84 "4" => "default",
85 "5" => "default",
86 "6" => "off" );
87
88 my %seen; # return unique runlevels
89 return grep !$seen{$_}++, map($rl_table{$_}, @_);
90 }
91
92 sub systemd_reload {
93 if (length $ENV{DPKG_ROOT}) {
94 # if we operate on a chroot from the outside, do not attempt to reload
95 return;
96 }
97 if (-d "/run/systemd/system") {
98 system("systemctl", "daemon-reload");
99 }
100 }
101
102 # Creates the necessary links to enable/disable a SysV init script (fallback if
103 # no insserv/rc-update exists)
104 sub make_sysv_links {
105 my ($scriptname, $action) = @_;
106
107 # for "remove" we cannot rely on the init script still being present, as
108 # this gets called in postrm for purging. Just remove all symlinks.
109 if ("remove" eq $action) { unlink($_) for
110 glob("$dpkg_root/etc/rc?.d/[SK][0-9][0-9]$scriptname"); return; }
111
112 # if the service already has any links, do not touch them
113 # numbers we don't care about, but enabled/disabled state we do
114 return if glob("$dpkg_root/etc/rc?.d/[SK][0-9][0-9]$scriptname");
115
116 # for "defaults", parse Default-{Start,Stop} and create these links
117 my ($lsb_start_ref, $lsb_stop_ref) = parse_def_start_stop("$dpkg_root/etc/init.d/$scriptname");
118 my $start = $action eq "defaults-disabled" ? "K" : "S";
119 foreach my $lvl (@$lsb_start_ref) {
120 make_path("$dpkg_root/etc/rc$lvl.d");
121 my $l = "$dpkg_root/etc/rc$lvl.d/${start}01$scriptname";
122 symlink("../init.d/$scriptname", $l);
123 }
124
125 foreach my $lvl (@$lsb_stop_ref) {
126 make_path("$dpkg_root/etc/rc$lvl.d");
127 my $l = "$dpkg_root/etc/rc$lvl.d/K01$scriptname";
128 symlink("../init.d/$scriptname", $l);
129 }
130 }
131
132 # Creates the necessary links to enable/disable the service (equivalent of an
133 # initscript) in systemd.
134 sub make_systemd_links {
135 my ($scriptname, $action) = @_;
136
137 # If called by systemctl (via systemd-sysv-install), do nothing to avoid
138 # an endless loop.
139 if (defined($ENV{_SKIP_SYSTEMD_NATIVE}) && $ENV{_SKIP_SYSTEMD_NATIVE} == 1) {
140 return;
141 }
142
143 # If systemctl is available, let's use that to create the symlinks.
144 if (-x "$dpkg_root/bin/systemctl" || -x "$dpkg_root/usr/bin/systemctl") {
145 my $systemd_root = '/';
146 if ($dpkg_root ne '') {
147 $systemd_root = $dpkg_root;
148 }
149 # Set this env var to avoid loop in systemd-sysv-install.
150 local $ENV{SYSTEMCTL_SKIP_SYSV} = 1;
151 # Use --quiet to mimic the old update-rc.d behaviour.
152 system("systemctl", "--root=$systemd_root", "--quiet", "$action", "$scriptname");
153 return;
154 }
155
156 # In addition to the insserv call we also enable/disable the service
157 # for systemd by creating the appropriate symlink in case there is a
158 # native systemd service. In case systemd is not installed we do this
159 # on our own instead of using systemctl.
160 my $service_path;
161 if (-f "/etc/systemd/system/$scriptname.service") {
162 $service_path = "/etc/systemd/system/$scriptname.service";
163 } elsif (-f "/lib/systemd/system/$scriptname.service") {
164 $service_path = "/lib/systemd/system/$scriptname.service";
165 } elsif (-f "/usr/lib/systemd/system/$scriptname.service") {
166 $service_path = "/usr/lib/systemd/system/$scriptname.service";
167 }
168 if (defined($service_path)) {
169 my $changed_sth;
170 open my $fh, '<', $service_path or error("unable to read $service_path");
171 while (<$fh>) {
172 chomp;
173 if (/^\s*WantedBy=(.+)$/i) {
174 my $wants_dir = "/etc/systemd/system/$1.wants";
175 my $service_link = "$wants_dir/$scriptname.service";
176 if ("enable" eq $action) {
177 make_path($wants_dir);
178 symlink($service_path, $service_link);
179 } else {
180 unlink($service_link) if -e $service_link;
181 }
182 }
183 }
184 close($fh);
185 }
186 }
187
188 sub create_sequence {
189 my $force = (@_);
190 my $insserv = "$dpkg_root/usr/lib/insserv/insserv";
191 # Fallback for older insserv package versions [2014-04-16]
192 $insserv = "/sbin/insserv" if ( -x "$dpkg_root/sbin/insserv");
193 # If insserv is not configured it is not fully installed
194 my $insserv_installed = -x "$dpkg_root$insserv" && -e "$dpkg_root/etc/insserv.conf";
195 my @opts;
196 push(@opts, '-f') if $force;
197 # Add force flag if initscripts is not installed
198 # This enables inistcripts-less systems to not fail when a facility is missing
199 unshift(@opts, '-f') unless is_initscripts_installed();
200 if ( $dpkg_root ne '' ) {
201 push( @opts,
202 '--path', "$dpkg_root/etc/init.d",
203 '--override', "$dpkg_root/etc/insserv/overrides/",
204 '--insserv-dir', "$dpkg_root/etc/init.d",
205 '--config', "$dpkg_root/etc/insserv.conf" );
206 }
207
208 my $openrc_installed = -x "$dpkg_root/sbin/openrc";
209
210 my $sysv_insserv ={};
211 $sysv_insserv->{remove} = sub {
212 my ($scriptname) = @_;
213 if ( -f "$dpkg_root/etc/init.d/$scriptname" ) {
214 return system($insserv, @opts, "-r", $scriptname) >> 8;
215 } else {
216 # insserv removes all dangling symlinks, no need to tell it
217 # what to look for.
218 my $rc = system($insserv, @opts) >> 8;
219 error_code($rc, "insserv rejected the script header") if $rc;
220 }
221 };
222 $sysv_insserv->{defaults} = sub {
223 my ($scriptname) = @_;
224 if ( -f "$dpkg_root/etc/init.d/$scriptname" ) {
225 my $rc = system($insserv, @opts, $scriptname) >> 8;
226 error_code($rc, "insserv rejected the script header") if $rc;
227 } else {
228 error("initscript does not exist: /etc/init.d/$scriptname");
229 }
230 };
231 $sysv_insserv->{defaults_disabled} = sub {
232 my ($scriptname) = @_;
233 return if glob("$dpkg_root/etc/rc?.d/[SK][0-9][0-9]$scriptname");
234 if ( -f "$dpkg_root/etc/init.d/$scriptname" ) {
235 my $rc = system($insserv, @opts, $scriptname) >> 8;
236 error_code($rc, "insserv rejected the script header") if $rc;
237 } else {
238 error("initscript does not exist: /etc/init.d/$scriptname");
239 }
240 sysv_toggle("disable", $scriptname);
241 };
242 $sysv_insserv->{toggle} = sub {
243 my ($action, $scriptname) = (shift, shift);
244 sysv_toggle($action, $scriptname, @_);
245
246 # Call insserv to resequence modified links
247 my $rc = system($insserv, @opts, $scriptname) >> 8;
248 error_code($rc, "insserv rejected the script header") if $rc;
249 };
250
251 my $sysv_plain = {};
252 $sysv_plain->{remove} = sub {
253 my ($scriptname) = @_;
254 make_sysv_links($scriptname, "remove");
255 };
256 $sysv_plain->{defaults} = sub {
257 my ($scriptname) = @_;
258 make_sysv_links($scriptname, "defaults");
259 };
260 $sysv_plain->{defaults_disabled} = sub {
261 my ($scriptname) = @_;
262 make_sysv_links($scriptname, "defaults-disabled");
263 };
264 $sysv_plain->{toggle} = sub {
265 my ($action, $scriptname) = (shift, shift);
266 sysv_toggle($action, $scriptname, @_);
267 };
268
269 my $systemd = {};
270 $systemd->{remove} = sub {
271 systemd_reload;
272 };
273 $systemd->{defaults} = sub {
274 systemd_reload;
275 };
276 $systemd->{defaults_disabled} = sub {
277 systemd_reload;
278 };
279 $systemd->{toggle} = sub {
280 my ($action, $scriptname) = (shift, shift);
281 make_systemd_links($scriptname, $action);
282 systemd_reload;
283 };
284
285 # Should we check exit codeS?
286 my $openrc = {};
287 $openrc->{remove} = sub {
288 my ($scriptname) = @_;
289 system("rc-update", "-qqa", "delete", $scriptname);
290
291 };
292 $openrc->{defaults} = sub {
293 my ($scriptname) = @_;
294 # OpenRC does not distinguish halt and reboot. They are handled
295 # by /etc/init.d/transit instead.
296 return if ("halt" eq $scriptname || "reboot" eq $scriptname);
297 # no need to consider default disabled runlevels
298 # because everything is disabled by openrc by default
299 my @rls=script_runlevels($scriptname);
300 if ( @rls ) {
301 system("rc-update", "add", $scriptname, openrc_rlconv(@rls));
302 }
303 };
304 $openrc->{defaults_disabled} = sub {
305 # In openrc everything is disabled by default
306 };
307 $openrc->{toggle} = sub {
308 my ($action, $scriptname) = (shift, shift);
309 my (@toggle_lvls, $start_lvls, $stop_lvls, @symlinks);
310 my $lsb_header = lsb_header_for_script($scriptname);
311
312 # Extra arguments to disable|enable action are runlevels. If none
313 # given parse LSB info for Default-Start value.
314 if ($#_ >= 0) {
315 @toggle_lvls = @_;
316 } else {
317 ($start_lvls, $stop_lvls) = parse_def_start_stop($lsb_header);
318 @toggle_lvls = @$start_lvls;
319 if ($#toggle_lvls < 0) {
320 error("$scriptname Default-Start contains no runlevels, aborting.");
321 }
322 }
323 my %openrc_act = ( "disable" => "del", "enable" => "add" );
324 system("rc-update", $openrc_act{$action}, $scriptname,
325 openrc_rlconv(@toggle_lvls))
326 };
327
328 my @sequence;
329 if ($insserv_installed) {
330 push @sequence, $sysv_insserv;
331 }
332 else {
333 push @sequence, $sysv_plain;
334 }
335 # OpenRC has to be after sysv_{insserv,plain} because it depends on them to synchronize
336 # states.
337 if ($openrc_installed) {
338 push @sequence, $openrc;
339 }
340 push @sequence, $systemd;
341
342 return @sequence;
343 }
344
345 ## Dependency based
346 sub main {
347 my @args = @_;
348 my $scriptname;
349 my $action;
350 my $force = 0;
351
352 while($#args >= 0 && ($_ = $args[0]) =~ /^-/) {
353 shift @args;
354 if (/^-f$/) { $force = 1; next }
355 if (/^-h|--help$/) { usage(); }
356 usage("unknown option");
357 }
358
359 usage("not enough arguments") if ($#args < 1);
360
361 my @sequence = create_sequence($force);
362
363 $scriptname = shift @args;
364 $action = shift @args;
365 if ("remove" eq $action) {
366 foreach my $init (@sequence) {
367 $init->{remove}->($scriptname);
368 }
369 } elsif ("defaults" eq $action || "start" eq $action ||
370 "stop" eq $action) {
371 # All start/stop/defaults arguments are discarded so emit a
372 # message if arguments have been given and are in conflict
373 # with Default-Start/Default-Stop values of LSB comment.
374 if ("start" eq $action || "stop" eq $action) {
375 cmp_args_with_defaults($scriptname, $action, @args);
376 }
377 foreach my $init (@sequence) {
378 $init->{defaults}->($scriptname);
379 }
380 } elsif ("defaults-disabled" eq $action) {
381 foreach my $init (@sequence) {
382 $init->{defaults_disabled}->($scriptname);
383 }
384 } elsif ("disable" eq $action || "enable" eq $action) {
385 foreach my $init (@sequence) {
386 $init->{toggle}->($action, $scriptname, @args);
387 }
388 } else {
389 usage();
390 }
391 }
392
393 sub parse_def_start_stop {
394 my $script = shift;
395 my (%lsb, @def_start_lvls, @def_stop_lvls);
396
397 open my $fh, '<', $script or error("unable to read $script");
398 while (<$fh>) {
399 chomp;
400 if (m/^### BEGIN INIT INFO\s*$/) {
401 $lsb{'begin'}++;
402 }
403 elsif (m/^### END INIT INFO\s*$/) {
404 $lsb{'end'}++;
405 last;
406 }
407 elsif ($lsb{'begin'} and not $lsb{'end'}) {
408 if (m/^# Default-Start:\s*(\S?.*)$/) {
409 @def_start_lvls = split(' ', $1);
410 }
411 if (m/^# Default-Stop:\s*(\S?.*)$/) {
412 @def_stop_lvls = split(' ', $1);
413 }
414 }
415 }
416 close($fh);
417
418 return (\@def_start_lvls, \@def_stop_lvls);
419 }
420
421 sub lsb_header_for_script {
422 my $name = shift;
423
424 foreach my $file ("/etc/insserv/overrides/$name", "/etc/init.d/$name",
425 "/usr/share/insserv/overrides/$name") {
426 return $file if -s $file;
427 }
428
429 error("cannot find a LSB script for $name");
430 }
431
432 sub cmp_args_with_defaults {
433 my ($name, $act) = (shift, shift);
434 my ($lsb_start_ref, $lsb_stop_ref, $arg_str, $lsb_str);
435 my (@arg_start_lvls, @arg_stop_lvls, @lsb_start_lvls, @lsb_stop_lvls);
436
437 ($lsb_start_ref, $lsb_stop_ref) = parse_def_start_stop("/etc/init.d/$name");
438 @lsb_start_lvls = @$lsb_start_ref;
439 @lsb_stop_lvls = @$lsb_stop_ref;
440 return if (!@lsb_start_lvls and !@lsb_stop_lvls);
441
442 warning "start and stop actions are no longer supported; falling back to defaults";
443 my $start = $act eq 'start' ? 1 : 0;
444 my $stop = $act eq 'stop' ? 1 : 0;
445
446 # The legacy part of this program passes arguments starting with
447 # "start|stop NN x y z ." but the insserv part gives argument list
448 # starting with sequence number (ie. strips off leading "start|stop")
449 # Start processing arguments immediately after the first seq number.
450 my $argi = $_[0] eq $act ? 2 : 1;
451
452 while (defined $_[$argi]) {
453 my $arg = $_[$argi];
454
455 # Runlevels 0 and 6 are always stop runlevels
456 if ($arg eq 0 or $arg eq 6) {
457 $start = 0; $stop = 1;
458 } elsif ($arg eq 'start') {
459 $start = 1; $stop = 0; $argi++; next;
460 } elsif ($arg eq 'stop') {
461 $start = 0; $stop = 1; $argi++; next;
462 } elsif ($arg eq '.') {
463 next;
464 }
465 push(@arg_start_lvls, $arg) if $start;
466 push(@arg_stop_lvls, $arg) if $stop;
467 } continue {
468 $argi++;
469 }
470
471 if ($#arg_start_lvls != $#lsb_start_lvls or
472 join("\0", sort @arg_start_lvls) ne join("\0", sort @lsb_start_lvls)) {
473 $arg_str = @arg_start_lvls ? "@arg_start_lvls" : "none";
474 $lsb_str = @lsb_start_lvls ? "@lsb_start_lvls" : "none";
475 warning "start runlevel arguments ($arg_str) do not match",
476 "$name Default-Start values ($lsb_str)";
477 }
478 if ($#arg_stop_lvls != $#lsb_stop_lvls or
479 join("\0", sort @arg_stop_lvls) ne join("\0", sort @lsb_stop_lvls)) {
480 $arg_str = @arg_stop_lvls ? "@arg_stop_lvls" : "none";
481 $lsb_str = @lsb_stop_lvls ? "@lsb_stop_lvls" : "none";
482 warning "stop runlevel arguments ($arg_str) do not match",
483 "$name Default-Stop values ($lsb_str)";
484 }
485 }
486
487 sub sysv_toggle {
488 my ($act, $name) = (shift, shift);
489 my (@toggle_lvls, $start_lvls, $stop_lvls, @symlinks);
490 my $lsb_header = lsb_header_for_script($name);
491
492 # Extra arguments to disable|enable action are runlevels. If none
493 # given parse LSB info for Default-Start value.
494 if ($#_ >= 0) {
495 @toggle_lvls = @_;
496 } else {
497 ($start_lvls, $stop_lvls) = parse_def_start_stop($lsb_header);
498 @toggle_lvls = @$start_lvls;
499 if ($#toggle_lvls < 0) {
500 error("$name Default-Start contains no runlevels, aborting.");
501 }
502 }
503
504 # Find symlinks in rc.d directories. Refuse to modify links in runlevels
505 # not used for normal system start sequence.
506 for my $lvl (@toggle_lvls) {
507 if ($lvl !~ /^[S2345]$/) {
508 warning("$act action will have no effect on runlevel $lvl");
509 next;
510 }
511 push(@symlinks, $_) for glob("$dpkg_root/etc/rc$lvl.d/[SK][0-9][0-9]$name");
512 }
513
514 if (!@symlinks) {
515 error("no runlevel symlinks to modify, aborting!");
516 }
517
518 # Toggle S/K bit of script symlink.
519 for my $cur_lnk (@symlinks) {
520 my $sk;
521 my @new_lnk = split(//, $cur_lnk);
522
523 if ("disable" eq $act) {
524 $sk = rindex($cur_lnk, '/S') + 1;
525 next if $sk < 1;
526 $new_lnk[$sk] = 'K';
527 } else {
528 $sk = rindex($cur_lnk, '/K') + 1;
529 next if $sk < 1;
530 $new_lnk[$sk] = 'S';
531 }
532
533 rename($cur_lnk, join('', @new_lnk)) or error($!);
534 }
535 }
536
537 # Try to determine if initscripts is installed
538 sub is_initscripts_installed {
539 # Check if mountkernfs is available. We cannot make inferences
540 # using the running init system because we may be running in a
541 # chroot
542 return glob("$dpkg_root/etc/rcS.d/S??mountkernfs.sh");
543 }