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