-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmythlock-rmtctrl.pl
More file actions
executable file
·188 lines (165 loc) · 5.41 KB
/
mythlock-rmtctrl.pl
File metadata and controls
executable file
·188 lines (165 loc) · 5.41 KB
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
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw/ $opt_d $opt_h $opt_l $opt_u /;
use POSIX qw(strftime);
###############################################################################################################################
# Prototype definitions
###############################################################################################################################
sub logmsg(@);
sub showmsg($);
sub process_opts();
sub lockmyth();
sub unlockmyth();
###############################################################################################################################
# Constant Definitions.
###############################################################################################################################
my ($TRUE) = 1;
my ($FALSE) = 0;
###############################################################################################################################
# Global vars, paths, commands to call.
###############################################################################################################################
my ($LOG) = "/home/myth/scripts/mythlock-rmtctrl.log";
my ($LOGSIZE) = 1024;
my ($DEBUG) = $FALSE;
my ($BASENAME) = $0;
my ($MYTHSTATUS)="/usr/bin/mythshutdown -s";
my ($UNLOCKMYTH)="/usr/bin/mythshutdown -u";
my ($LOCKMYTH)="/usr/bin/mythshutdown -l";
###############################################################################################################################
# The Mainline.
###############################################################################################################################
MAIN:
{
process_opts();
logmsg "$BASENAME started : PID($$)";
if (not exists($ENV{"HOME"})) { $ENV{"HOME"}="/home/myth"; }
my $mythstatus;
`$MYTHSTATUS`;
$mythstatus = $?>>8;
logmsg "initial status: $mythstatus.";
if (($mythstatus & 0x10) == 0) { lockmyth(); }
else { unlockmyth(); }
logmsg "$BASENAME Completed.";
}
sub lockmyth()
{
my $mythstatus;
for (my $i = 0; $i < 10; $i++) {
`$MYTHSTATUS`;
$mythstatus = $?>>8;
logmsg "lock: try $i, result $mythstatus.";
if (($mythstatus & 0x10) != 0) { last; }
`$LOCKMYTH`;
}
if (($mythstatus & 0x10) != 0) {
showmsg("Shutdown blocking is active.");
return;
}
showmsg("Unable to activate shutdown blocking.\n" .
"Please try again.");
}
sub unlockmyth()
{
my $mythstatus;
for (my $i = 0; $i < 10; $i++) {
`$MYTHSTATUS`;
$mythstatus = $?>>8;
logmsg "unlock: try $i, result $mythstatus.";
if (($mythstatus & 0x10) == 0) { last; }
`$UNLOCKMYTH`;
}
if (($mythstatus & 0x10) == 0) {
showmsg("Shutdown blocking deactivated.");
return;
}
showmsg("Unable to deactivate shutdown blocking.\n" .
"Please try again.");
}
###############################################################################################################################
# showmsg
# Fork and show a message on the TV to tell the user what the system will do
###############################################################################################################################
sub showmsg($)
{
my ($pid);
unless ($pid = fork())
{
# Child process
sleep(2);
my ($msg) = shift;
logmsg "Display message: " . $msg . "\n(showing for 6 sec..." if ($DEBUG == $TRUE);
`gxmessage -display :0 -timeout 6 -borderless -fn "URW Chancery L 48" -buttons '' -bg black -fg gray "$msg" &`;
logmsg "finished)\n" if ($DEBUG == $TRUE);
exit;
}
return $pid;
}
###############################################################################################################################
# logmsg
# Little routine to write to the log file.
# Rotates around $LOGSIZE bytes.
###############################################################################################################################
sub logmsg(@)
{
my ($string)=@_;
my $time=scalar localtime;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
my (@lines,$line);
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat("$LOG");
if (defined($size))
{
$size=$size/1024; #size in kbyte
if ($size >= $LOGSIZE)
{
unlink ("$LOG.old") if (-e("$LOG.old"));
rename ($LOG,"$LOG.old");
}
}
print "$time : $string\n" if ($DEBUG==$TRUE);
if (open (LOG,">>$LOG"))
{
if ($string =~ /\n/)
{
@lines=split(/\n/,$string);
foreach $line (@lines)
{
print LOG "$time : $line\n";
}
}
else
{
print LOG "$time : $string\n";
}
close (LOG);
}
else
{
print "Unable to open LOG $LOG : $!";
}
}
###############################################################################################################################
# process_opts()
# Set Global option flags dependant on command line input.
###############################################################################################################################
sub process_opts()
{
getopts('dluh');
$DEBUG=$TRUE if ($opt_d);
exit(usage(1)) if ($opt_h || ($opt_l && $opt_u));
}
###############################################################################################################################
# usage()
# Output Relevant Usage strings if incorrect opts are given.
###############################################################################################################################
sub usage()
{
my($ucode)=@_;
if ($ucode == 1)
{
print "Usage: $BASENAME [-dhlu]\n";
print "\t-d\t\tdebug\n";
print "\t-h\t\tprints this help message\n";
return(0);
}
}