| 1 |
package UML::Utils; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
require Exporter; |
| 7 |
our @ISA = qw(Exporter); |
| 8 |
|
| 9 |
our @EXPORT = |
| 10 |
qw( |
| 11 |
ask askCmd getDirList logAction get_sys_userid get_sys_username |
| 12 |
$mount |
| 13 |
$chmod |
| 14 |
$rm |
| 15 |
$route |
| 16 |
$arp |
| 17 |
$ifconfig |
| 18 |
$tunctl |
| 19 |
$bin_id |
| 20 |
$linux |
| 21 |
$sudo |
| 22 |
$ssh |
| 23 |
$kill |
| 24 |
$sh |
| 25 |
$sync |
| 26 |
$shutdown |
| 27 |
$poweroff |
| 28 |
$ps |
| 29 |
$uml_mconsole |
| 30 |
$screen |
| 31 |
$fuser |
| 32 |
$halt |
| 33 |
$chown |
| 34 |
); |
| 35 |
|
| 36 |
#use UML::Config; |
| 37 |
my $DEBUG_STEP = $UML::Config::DEBUG_STEP; |
| 38 |
|
| 39 |
use DirHandle; |
| 40 |
|
| 41 |
our $mount = "/bin/mount"; |
| 42 |
our $chmod = "/bin/chmod"; |
| 43 |
our $rm = "/bin/rm"; |
| 44 |
our $route = "/sbin/route"; |
| 45 |
our $arp = "/sbin/arp"; |
| 46 |
our $ifconfig = "/sbin/ifconfig"; |
| 47 |
our $tunctl = "/usr/bin/tunctl"; |
| 48 |
our $bin_id = "/usr/bin/id"; |
| 49 |
our $linux = "/usr/bin/linux"; |
| 50 |
our $sudo = "/usr/bin/sudo"; |
| 51 |
our $ssh = "/usr/bin/ssh"; |
| 52 |
our $kill = "/bin/kill"; |
| 53 |
our $sh = "/bin/sh"; |
| 54 |
our $sync = "/bin/sync"; |
| 55 |
our $shutdown = "/sbin/shutdown"; |
| 56 |
our $poweroff = "/sbin/poweroff"; |
| 57 |
our $ps = "/bin/ps"; |
| 58 |
our $uml_mconsole = "/usr/bin/uml_mconsole"; |
| 59 |
our $screen = "/usr/bin/screen"; |
| 60 |
our $fuser = "/bin/fuser"; |
| 61 |
our $halt = "/sbin/halt"; |
| 62 |
our $chown = "/bin/chown"; |
| 63 |
|
| 64 |
|
| 65 |
sub logMain { |
| 66 |
foreach (@_) { |
| 67 |
if ($_) { print $_; } |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
sub logResult { |
| 72 |
my $ls = shift; |
| 73 |
logMain(" ...", $ls); |
| 74 |
} |
| 75 |
|
| 76 |
sub logAction { |
| 77 |
my $ls = shift; |
| 78 |
logMain("\n", $ls); |
| 79 |
} |
| 80 |
|
| 81 |
sub doCmd { |
| 82 |
my $cmd = shift; |
| 83 |
my $res = system($cmd); |
| 84 |
if ($res == 0) { |
| 85 |
logResult("ok"); |
| 86 |
} else { |
| 87 |
logResult("failed ($res: $!)"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
sub ask { |
| 92 |
my $quest = shift; |
| 93 |
print "$quest (y|n)"; |
| 94 |
my $input; |
| 95 |
read STDIN, $input, 2; |
| 96 |
if ($input =~ m/y/i) { |
| 97 |
return 1; |
| 98 |
} else { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
sub askCmd { |
| 104 |
my $quest = shift; |
| 105 |
my $cmd = shift; |
| 106 |
my $description = shift; |
| 107 |
if ($DEBUG_STEP) { |
| 108 |
#print "cmd: $cmd", "\n"; |
| 109 |
print "cmd: $cmd", " "; |
| 110 |
if (ask($quest)) { |
| 111 |
#print `$cmd`; |
| 112 |
doCmd($cmd); |
| 113 |
} |
| 114 |
} else { |
| 115 |
$description && ($description .= ": "); |
| 116 |
logAction($description); |
| 117 |
#`$cmd`; |
| 118 |
doCmd($cmd); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
# "global" vars used in directory-recursive-parsing |
| 124 |
my $dirlist_buf; |
| 125 |
my @dirlist_path; |
| 126 |
my $dirlist_base; |
| 127 |
|
| 128 |
sub entry_callback { |
| 129 |
|
| 130 |
my $entry = shift; |
| 131 |
|
| 132 |
# CHECKS |
| 133 |
# dont't use this: |
| 134 |
if ($entry eq '.' || $entry eq '..') { return; } |
| 135 |
|
| 136 |
# PREPARE |
| 137 |
# prepare path to current entry |
| 138 |
my $cur_entry = join('/', @dirlist_path, $entry); |
| 139 |
# prepare path to current entry (absolute) |
| 140 |
my $cur_entry_abs = join('/', $dirlist_base, @dirlist_path, $entry); |
| 141 |
|
| 142 |
# ENTRY |
| 143 |
# add current entry to buffer |
| 144 |
$dirlist_buf .= $cur_entry . "\n"; |
| 145 |
|
| 146 |
# (SUB-)DIRECTORY |
| 147 |
# check if current entry is a (sub-)directory ... |
| 148 |
if (-d $cur_entry_abs) { |
| 149 |
push @dirlist_path, $cur_entry; |
| 150 |
# ... and parse this (recursion here!!!) |
| 151 |
iterate_path($cur_entry_abs); |
| 152 |
pop @dirlist_path; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
sub iterate_path { |
| 157 |
|
| 158 |
my $path = shift; |
| 159 |
|
| 160 |
# create new "DirHandle"-object |
| 161 |
my $d = new DirHandle $path; |
| 162 |
if (defined $d) { |
| 163 |
|
| 164 |
# iterate through all entries in $path ($d->read) and call out entry-handler on each entry |
| 165 |
while (defined(my $line = $d->read)) { |
| 166 |
entry_callback($line); |
| 167 |
} |
| 168 |
|
| 169 |
undef $d; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
sub getDirList { |
| 176 |
|
| 177 |
$dirlist_base = shift; |
| 178 |
|
| 179 |
# reset vars |
| 180 |
$dirlist_buf = ''; |
| 181 |
@dirlist_path = (); |
| 182 |
|
| 183 |
# start parsing file-structure |
| 184 |
iterate_path($dirlist_base); |
| 185 |
|
| 186 |
# return complete list of directory-content including files and subdirs |
| 187 |
# entries are newline (\n) - seperated |
| 188 |
return $dirlist_buf; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
sub get_sys_userid { |
| 193 |
my $username = shift; |
| 194 |
$username ||= ''; |
| 195 |
my $cmd = "$bin_id -u $username"; |
| 196 |
my $userid = `$cmd`; |
| 197 |
chomp($userid); |
| 198 |
return $userid; |
| 199 |
} |
| 200 |
|
| 201 |
sub get_sys_username { |
| 202 |
my $username = shift; |
| 203 |
$username ||= ''; |
| 204 |
my $cmd = "$bin_id -un $username"; |
| 205 |
my $userid = `$cmd`; |
| 206 |
chomp($userid); |
| 207 |
return $userid; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
1; |
| 212 |
|