summaryrefslogtreecommitdiff
path: root/scripts/vyatta-reboot.pl
blob: eaef88514769d11a8f4f499a0ebc0a9e09800967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/perl
#
# Module: vyatta-reboot.pl
# 
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Stig Thormodsrud
# Date: May 2008
# Description: Script to reboot or schedule a reboot
# 
# **** End License ****
#

use lib "/opt/vyatta/share/perl5/";
use Getopt::Long;
use POSIX;
use IO::Prompt;
use Sys::Syslog qw(:standard :macros);

use strict;
use warnings;

my $reboot_job_file = '/var/run/reboot.job';


sub parse_at_output {
    my @lines = @_;
    
    foreach my $line (@lines) {
	if ($line =~ /error/) {
	    return (1, '', '');
	} elsif ($line =~ /job (\d+) (.*)$/) {
	    return (0, $1, $2);
	} 
    }
    return (1, '', '');
}

sub is_reboot_pending {

    if ( ! -f $reboot_job_file) {
	return (0, '');
    }
    my $job = `cat $reboot_job_file`;
    chomp $job;
    my $line = `atq $job`;
    if ($line =~ /\d+\s+(.*)\sa root$/) {
	return (1, $1);
    } else {
	return (0, '');
    }
}

sub do_reboot {
    my $login = shift;

    syslog("warning", "Reboot now requested by $login");
    exec("sudo /sbin/reboot");
}

sub cancel_reboot {
    my ($login, $time) = @_;

    my $job = `cat $reboot_job_file`;
    chomp $job;
    system("atrm $job");
    system("rm $reboot_job_file");
    syslog("warning", "Reboot scheduled for [$time] - CANCELED by $login");
}

#
# main
#
my ($action, $at_time, $now);
GetOptions("action=s"  => \$action,
	   "at_time=s" => \$at_time,
           "now!"      => \$now,
);

if (! defined $action) {
    die "no action specified";
} 

openlog($0, "", LOG_USER);
my $login = getlogin() || getpwuid($<) || "unknown";

#
# reboot
#
if ($action eq "reboot") {

    my ($rc, $time) = is_reboot_pending();
    if ($rc) {
        if (defined $now) {
            cancel_reboot($login, $time);
            do_reboot($login);
        } else {
            print "Reboot already scheduled for [$time]\n";
            exit 1;
        }
    }

    if (defined $now) {
        do_reboot($login);
    } else {
        if (defined($ENV{VYATTA_PROCESS_CLIENT} && $ENV{VYATTA_PROCESS_CLIENT} eq 'gui2_rest') || 
	    prompt("Proceed with reboot? [confirm]", -y1d=>"y")) {
            do_reboot($login);
	} else {
	    print "Reboot canceled\n";
	    exit 1;
        }
    }
}

#
# reboot_at
#
if ($action eq "reboot_at") {
    if (! -f '/usr/bin/at') {
	die "Package [at] not installed";
    }

    if (! defined $at_time) {
	die "no at_time specified";
    }

    my ($rc, $rtime) = is_reboot_pending();
    if ($rc) {
	print "Reboot already scheduled for [$rtime]\n";
	exit 1;
    }

    #
    # check if the time format is valid, then
    # remove that job
    #
    my @lines = `echo true | at $at_time 2>&1`;
    my ($err, $job, $time) = parse_at_output(@lines);
    if ($err) {
	print "Invalid time format [$at_time]\n";
	exit 1;
    }
    system("atrm $job");

    print "\nReload scheduled for $time\n\n";
    if (!defined($ENV{VYATTA_PROCESS_CLIENT}) || $ENV{VYATTA_PROCESS_CLIENT} ne 'gui2_rest') {
	if (! prompt("Proceed with reboot schedule? [confirm]", -y1d=>"y")) {
	    print "Reboot canceled\n";
	    exit 1;
	}
    }

    @lines = `echo sudo /sbin/reboot | at $at_time 2>&1`;
    ($err, $job, $time) = parse_at_output(@lines);
    if ($err) {
	print "Error: unable to schedule reboot\n";
	exit 1;
    }
    system("echo $job > $reboot_job_file");
    print "\nReboot scheduled for $time\n";
    syslog("warning", "Reboot scheduled for [$time] by $login");

    exit 0;
}

#
# reboot_cancel
#
if ($action eq "reboot_cancel") {

    my ($rc, $time) = is_reboot_pending();
    if (! $rc) {
	print "No reboot currently scheduled\n";
	exit 1;
    }
    cancel_reboot($login, $time);
    print "Reboot canceled\n";
    exit 0;
}

#
# show_reboot
#
if ($action eq "show_reboot") {

    my ($rc, $time) = is_reboot_pending();
    if ($rc) {
	print "Reboot scheduled for [$time]\n";
	exit 0;
    } else {
	print "No reboot currently scheduled\n";
    }
    exit 1;
}

exit 1;

# end of file