tangled
alpha
login
or
join now
pyrox.dev
/
nixpkgs
0
fork
atom
lol
0
fork
atom
overview
issues
pulls
pipelines
Merge branch 'dbus-switch-to-configuration'
Oliver Charles
12 years ago
ad805328
1c5d3c78
+24
-23
2 changed files
expand all
collapse all
unified
split
nixos
modules
system
activation
switch-to-configuration.pl
top-level.nix
+23
-22
nixos/modules/system/activation/switch-to-configuration.pl
···
4
use warnings;
5
use File::Basename;
6
use File::Slurp;
0
7
use Sys::Syslog qw(:standard :macros);
8
use Cwd 'abs_path';
9
···
62
# virtual console 1 and we restart the "tty1" unit.
63
$SIG{PIPE} = "IGNORE";
64
0
0
0
0
65
sub getActiveUnits {
66
-
# FIXME: use D-Bus or whatever to query this, since parsing the
67
-
# output of list-units is likely to break.
68
-
my $lines = `LANG= @systemd@/bin/systemctl list-units --full`;
69
my $res = {};
70
-
foreach my $line (split '\n', $lines) {
71
-
chomp $line;
72
-
last if $line eq "";
73
-
$line =~ /^\*?\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s/ or next;
74
-
next if $1 eq "UNIT";
75
-
$res->{$1} = { load => $2, state => $3, substate => $4 };
76
}
77
return $res;
78
}
···
297
if (scalar @unitsToStop > 0) {
298
@unitsToStop = unique(@unitsToStop);
299
print STDERR "stopping the following units: ", join(", ", sort(@unitsToStop)), "\n";
300
-
system("@systemd@/bin/systemctl", "stop", "--", @unitsToStop); # FIXME: ignore errors?
301
}
302
303
print STDERR "NOT restarting the following units: ", join(", ", sort(@unitsToSkip)), "\n"
···
312
# Restart systemd if necessary.
313
if (abs_path("/proc/1/exe") ne abs_path("@systemd@/lib/systemd/systemd")) {
314
print STDERR "restarting systemd...\n";
315
-
system("@systemd@/bin/systemctl", "daemon-reexec") == 0 or $res = 2;
0
316
}
317
318
# Forget about previously failed services.
319
-
system("@systemd@/bin/systemctl", "reset-failed");
320
321
-
# Make systemd reload its units.
322
-
system("@systemd@/bin/systemctl", "daemon-reload") == 0 or $res = 3;
323
324
# Restart changed services (those that have to be restarted rather
325
# than stopped and started).
326
my @restart = unique(split('\n', read_file($restartListFile, err_mode => 'quiet') // ""));
327
if (scalar @restart > 0) {
328
print STDERR "restarting the following units: ", join(", ", sort(@restart)), "\n";
329
-
system("@systemd@/bin/systemctl", "restart", "--", @restart) == 0 or $res = 4;
330
unlink($restartListFile);
331
}
332
···
338
# systemd.
339
my @start = unique("default.target", "timers.target", "sockets.target", split('\n', read_file($startListFile, err_mode => 'quiet') // ""));
340
print STDERR "starting the following units: ", join(", ", sort(@start)), "\n";
341
-
system("@systemd@/bin/systemctl", "start", "--", @start) == 0 or $res = 4;
342
unlink($startListFile);
343
344
# Reload units that need it. This includes remounting changed mount
···
346
my @reload = unique(split '\n', read_file($reloadListFile, err_mode => 'quiet') // "");
347
if (scalar @reload > 0) {
348
print STDERR "reloading the following units: ", join(", ", sort(@reload)), "\n";
349
-
system("@systemd@/bin/systemctl", "reload", "--", @reload) == 0 or $res = 4;
350
unlink($reloadListFile);
351
}
352
353
# Signal dbus to reload its configuration.
354
-
system("@systemd@/bin/systemctl", "reload", "dbus.service");
355
356
# Print failed and new units.
357
my (@failed, @new, @restarting);
···
362
}
363
elsif ($state->{state} eq "auto-restart") {
364
# A unit in auto-restart state is a failure *if* it previously failed to start
365
-
my $lines = `@systemd@/bin/systemctl show '$unit'`;
366
-
my $info = {};
367
-
parseKeyValues($info, split("\n", $lines));
368
369
-
if ($info->{ExecMainStatus} ne '0') {
370
push @failed, $unit;
371
}
372
}
···
4
use warnings;
5
use File::Basename;
6
use File::Slurp;
7
+
use Net::DBus;
8
use Sys::Syslog qw(:standard :macros);
9
use Cwd 'abs_path';
10
···
63
# virtual console 1 and we restart the "tty1" unit.
64
$SIG{PIPE} = "IGNORE";
65
66
+
my $dbus = Net::DBus->find;
67
+
my $systemdService = $dbus->get_service('org.freedesktop.systemd1');
68
+
my $systemdManager = $systemdService->get_object('/org/freedesktop/systemd1');
69
+
70
sub getActiveUnits {
0
0
0
71
my $res = {};
72
+
foreach my $unit (@{ $systemdManager->ListUnits() }) {
73
+
$res->{$unit->[0]} = {
74
+
load => $unit->[2],
75
+
state => $unit->[3],
76
+
substate => $unit->[4]
77
+
};
78
}
79
return $res;
80
}
···
299
if (scalar @unitsToStop > 0) {
300
@unitsToStop = unique(@unitsToStop);
301
print STDERR "stopping the following units: ", join(", ", sort(@unitsToStop)), "\n";
302
+
$systemdManager->StopUnit($_, "replace") for @unitsToStop;
303
}
304
305
print STDERR "NOT restarting the following units: ", join(", ", sort(@unitsToSkip)), "\n"
···
314
# Restart systemd if necessary.
315
if (abs_path("/proc/1/exe") ne abs_path("@systemd@/lib/systemd/systemd")) {
316
print STDERR "restarting systemd...\n";
317
+
318
+
$systemdManager->Reexecute();
319
}
320
321
# Forget about previously failed services.
322
+
$systemdManager->ResetFailed();
323
324
+
# Make systemd reload its units
325
+
$systemdManager->Reload();
326
327
# Restart changed services (those that have to be restarted rather
328
# than stopped and started).
329
my @restart = unique(split('\n', read_file($restartListFile, err_mode => 'quiet') // ""));
330
if (scalar @restart > 0) {
331
print STDERR "restarting the following units: ", join(", ", sort(@restart)), "\n";
332
+
$systemdManager->Restart($_, "replace") for @restart;
333
unlink($restartListFile);
334
}
335
···
341
# systemd.
342
my @start = unique("default.target", "timers.target", "sockets.target", split('\n', read_file($startListFile, err_mode => 'quiet') // ""));
343
print STDERR "starting the following units: ", join(", ", sort(@start)), "\n";
344
+
$systemdManager->StartUnit($_, "replace") for @start;
345
unlink($startListFile);
346
347
# Reload units that need it. This includes remounting changed mount
···
349
my @reload = unique(split '\n', read_file($reloadListFile, err_mode => 'quiet') // "");
350
if (scalar @reload > 0) {
351
print STDERR "reloading the following units: ", join(", ", sort(@reload)), "\n";
352
+
$systemdManager->ReloadUnit($_, "replace") for @reload;
353
unlink($reloadListFile);
354
}
355
356
# Signal dbus to reload its configuration.
357
+
$systemdManager->ReloadUnit("dbus.service", "replace");
358
359
# Print failed and new units.
360
my (@failed, @new, @restarting);
···
365
}
366
elsif ($state->{state} eq "auto-restart") {
367
# A unit in auto-restart state is a failure *if* it previously failed to start
368
+
my $unit = $systemdManager->GetUnit($unit);
0
0
369
370
+
if ($unit->ExecMainStatus ne '0') {
371
push @failed, $unit;
372
}
373
}
+1
-1
nixos/modules/system/activation/top-level.nix
···
109
configurationName = config.boot.loader.grub.configurationName;
110
111
# Needed by switch-to-configuration.
112
-
perl = "${pkgs.perl}/bin/perl -I${pkgs.perlPackages.FileSlurp}/lib/perl5/site_perl";
113
};
114
115
···
109
configurationName = config.boot.loader.grub.configurationName;
110
111
# Needed by switch-to-configuration.
112
+
perl = "${pkgs.perl}/bin/perl -I${pkgs.perlPackages.XMLTwig}/lib/perl5/site_perl -I${pkgs.perlPackages.XMLParser}/lib/perl5/site_perl -I${pkgs.perlPackages.NetDBus}/lib/perl5/site_perl -I${pkgs.perlPackages.FileSlurp}/lib/perl5/site_perl";
113
};
114
115