| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
######################################################################################### |
|
2
|
|
|
|
|
|
|
# Package HiPi::Utils |
|
3
|
|
|
|
|
|
|
# Description: HiPi Utilities |
|
4
|
|
|
|
|
|
|
# Copyright: Copyright (c) 2013-2017 Mark Dootson |
|
5
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
|
6
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
|
7
|
|
|
|
|
|
|
######################################################################################### |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package HiPi::Utils; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# this package is retained to provide backwards compatibility with old module functions |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
######################################################################################### |
|
14
|
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
1651
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
49
|
|
|
16
|
1
|
|
|
1
|
|
10
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
17
|
1
|
|
|
1
|
|
10
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
58
|
|
|
18
|
|
|
|
|
|
|
require Exporter; |
|
19
|
1
|
|
|
1
|
|
8
|
use base qw( Exporter ); |
|
|
1
|
|
|
|
|
40
|
|
|
|
1
|
|
|
|
|
164
|
|
|
20
|
1
|
|
|
1
|
|
8
|
use XSLoader; |
|
|
1
|
|
|
|
|
8
|
|
|
|
1
|
|
|
|
|
9
|
|
|
21
|
1
|
|
|
1
|
|
23
|
use HiPi qw( :rpi ); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
338
|
|
|
22
|
1
|
|
|
1
|
|
10
|
use HiPi::RaspberryPi; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
11
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION ='0.81'; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $defaultuser = 'pi'; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
|
29
|
|
|
|
|
|
|
get_groups |
|
30
|
|
|
|
|
|
|
create_system_group |
|
31
|
|
|
|
|
|
|
create_user_group |
|
32
|
|
|
|
|
|
|
group_add_user |
|
33
|
|
|
|
|
|
|
group_remove_user |
|
34
|
|
|
|
|
|
|
cat_file |
|
35
|
|
|
|
|
|
|
echo_file |
|
36
|
|
|
|
|
|
|
home_directory |
|
37
|
|
|
|
|
|
|
is_windows |
|
38
|
|
|
|
|
|
|
is_unix |
|
39
|
|
|
|
|
|
|
is_raspberry |
|
40
|
|
|
|
|
|
|
is_mac |
|
41
|
|
|
|
|
|
|
is_raspberry_2 |
|
42
|
|
|
|
|
|
|
is_raspberry_3 |
|
43
|
|
|
|
|
|
|
uses_device_tree |
|
44
|
|
|
|
|
|
|
system_type |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => \@EXPORT_OK ); |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
0
|
0
|
|
sub is_raspberry { HiPi::RaspberryPi::is_raspberry; } |
|
50
|
0
|
|
|
0
|
0
|
|
sub is_raspberry_2 { HiPi::RaspberryPi::is_raspberry; } |
|
51
|
0
|
|
|
0
|
0
|
|
sub is_raspberry_3 { HiPi::RaspberryPi::is_raspberry_2; } |
|
52
|
0
|
|
|
0
|
0
|
|
sub uses_device_tree { HiPi::RaspberryPi::has_device_tree; } |
|
53
|
0
|
|
|
0
|
0
|
|
sub is_windows { HiPi::RaspberryPi::os_is_windows; } |
|
54
|
0
|
|
|
0
|
0
|
|
sub is_mac { HiPi::RaspberryPi::os_is_osx; } |
|
55
|
0
|
|
|
0
|
0
|
|
sub is_unix { HiPi::RaspberryPi::os_is_linux; } |
|
56
|
0
|
|
|
0
|
0
|
|
sub home_directory { HiPi::RaspberryPi::home_directory; } |
|
57
|
0
|
|
|
0
|
0
|
|
sub system_type { HiPi::RaspberryPi::has_device_tree; } |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
XSLoader::load('HiPi::Utils', $VERSION) if HiPi::is_raspberry_pi(); |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub get_groups { |
|
62
|
0
|
|
|
0
|
0
|
|
my $rhash = {}; |
|
63
|
0
|
0
|
|
|
|
|
return $rhash unless is_raspberry; |
|
64
|
0
|
|
|
|
|
|
setgrent(); |
|
65
|
0
|
|
|
|
|
|
while( my ($name,$passwd,$gid,$members) = getgrent() ){ |
|
66
|
0
|
|
|
|
|
|
$rhash->{$name} = { |
|
67
|
|
|
|
|
|
|
gid => $gid, |
|
68
|
|
|
|
|
|
|
members => [ split(/\s/, $members) ], |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
} |
|
71
|
0
|
|
|
|
|
|
endgrent(); |
|
72
|
0
|
|
|
|
|
|
return $rhash; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub create_system_group { |
|
76
|
0
|
|
|
0
|
0
|
|
my($gname, $gid) = @_; |
|
77
|
0
|
0
|
|
|
|
|
if( $gid ) { |
|
78
|
0
|
0
|
|
|
|
|
system(qq(groupadd -f -r -g $gid $gname)) and croak qq(Failed to create group $gname with gid $gid : $!); |
|
79
|
|
|
|
|
|
|
} else { |
|
80
|
0
|
0
|
|
|
|
|
system(qq(groupadd -f -r $gname)) and croak qq(Failed to create group $gname : $!); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub create_user_group { |
|
85
|
0
|
|
|
0
|
0
|
|
my($gname, $gid) = @_; |
|
86
|
0
|
0
|
|
|
|
|
if( $gid ) { |
|
87
|
0
|
0
|
|
|
|
|
system(qq(groupadd -f -g $gid $gname)) and croak qq(Failed to create group $gname with gid $gid : $!); |
|
88
|
|
|
|
|
|
|
} else { |
|
89
|
0
|
0
|
|
|
|
|
system(qq(groupadd -f $gname)) and croak qq(Failed to create group $gname : $!); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub group_add_user { |
|
94
|
0
|
|
|
0
|
0
|
|
my($gname, $uname) = @_; |
|
95
|
0
|
0
|
|
|
|
|
system(qq(gpasswd -a $uname $gname)) and croak qq(Failed to add user $uname to group $gname : $!); |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub group_remove_user { |
|
99
|
0
|
|
|
0
|
0
|
|
my($gname, $uname) = @_; |
|
100
|
0
|
0
|
|
|
|
|
system(qq(gpasswd -d $uname $gname)) and croak qq(Failed to remove user $uname from group $gname : $!); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub cat_file { |
|
104
|
0
|
|
|
0
|
0
|
|
my $filepath = shift; |
|
105
|
0
|
0
|
|
|
|
|
return '' unless HiPi::is_raspberry_pi(); |
|
106
|
0
|
|
|
|
|
|
my $rval = ''; |
|
107
|
|
|
|
|
|
|
{ |
|
108
|
0
|
|
|
|
|
|
local $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin'; |
|
|
0
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
$rval = qx(qq(/bin/cat $filepath)); |
|
110
|
0
|
0
|
|
|
|
|
if($?) { |
|
111
|
0
|
|
|
|
|
|
croak qq(reading file $filepath failed : $!); |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
0
|
|
|
|
|
|
return $rval; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub echo_file { |
|
118
|
0
|
|
|
0
|
0
|
|
my ($msg, $filepath, $append) = @_; |
|
119
|
0
|
0
|
|
|
|
|
return 0 unless HiPi::is_raspberry_pi(); |
|
120
|
0
|
0
|
|
|
|
|
my $redir = ( $append ) ? '>>' : '>'; |
|
121
|
0
|
|
|
|
|
|
my $canwrite = 0; |
|
122
|
|
|
|
|
|
|
# croak now if filepath is a directory |
|
123
|
0
|
0
|
|
|
|
|
croak qq($filepath is a directory) if -d $filepath; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# first check if file exists; |
|
126
|
0
|
0
|
|
|
|
|
if( -f $filepath ) { |
|
127
|
0
|
0
|
|
|
|
|
$canwrite = ( -w $filepath ) ? 1 : 0; |
|
128
|
|
|
|
|
|
|
} else { |
|
129
|
0
|
|
|
|
|
|
my $dir = $filepath; |
|
130
|
0
|
|
|
|
|
|
$dir =~ s/\/[^\/]+$//; |
|
131
|
0
|
0
|
|
|
|
|
unless( -d $dir ) { |
|
132
|
0
|
|
|
|
|
|
croak qq(Cannot write to $filepath. Directory does not exist); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
0
|
0
|
|
|
|
|
$canwrite = ( -w $dir ) ? 1 : 0; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $command = qq(/bin/echo \"$msg\" $append $filepath); |
|
138
|
|
|
|
|
|
|
{ |
|
139
|
0
|
|
|
|
|
|
local $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin'; |
|
|
0
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
if( $canwrite ) { |
|
141
|
0
|
0
|
|
|
|
|
system($command) and croak qq(Failed to echo to $filepath : $!); |
|
142
|
|
|
|
|
|
|
} else { |
|
143
|
0
|
|
|
|
|
|
croak qq(Failed to echo to $filepath : $!); |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub parse_udev_rule { |
|
150
|
|
|
|
|
|
|
# exists only for old version compatibility |
|
151
|
|
|
|
|
|
|
# return a default set |
|
152
|
0
|
|
|
0
|
0
|
|
return { gpio => { active => 1, group => 'gpio' }, spi => { active => 1, group => 'spi' }, }; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
0
|
0
|
|
sub set_udev_rules { |
|
156
|
|
|
|
|
|
|
# exists only for old version compatibility |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub parse_modprobe_conf { |
|
161
|
|
|
|
|
|
|
# exists only for old version compatibility |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# return a default set |
|
164
|
0
|
|
|
0
|
0
|
|
return { spidev => { active => 1, bufsiz => 4096 }, i2c_bcm2708 => { active => 1, baudrate => 100000 }, }; |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
0
|
0
|
|
sub set_modprobe_conf { |
|
169
|
|
|
|
|
|
|
# exists only for old version compatibility |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub drop_permissions_name { |
|
173
|
0
|
|
|
0
|
0
|
|
my($username, $groupname) = @_; |
|
174
|
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
return 0 unless HiPi::is_raspberry_pi(); |
|
176
|
|
|
|
|
|
|
|
|
177
|
0
|
|
0
|
|
|
|
$username ||= getlogin(); |
|
178
|
0
|
|
0
|
|
|
|
$username ||= $defaultuser; |
|
179
|
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
my($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwnam($username); |
|
181
|
0
|
|
|
|
|
|
my $targetuid = $uid; |
|
182
|
0
|
0
|
|
|
|
|
my $targetgid = ( $groupname ) ? (getgrnam($groupname))[2] : $gid; |
|
183
|
0
|
0
|
0
|
|
|
|
if( $targetuid > 0 && $targetgid > 0 ) { |
|
184
|
0
|
|
|
|
|
|
drop_permissions_id($targetuid, $targetgid); |
|
185
|
|
|
|
|
|
|
} else { |
|
186
|
0
|
|
|
|
|
|
croak qq(Could not drop permissions to uid $targetuid, gid $targetgid); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
0
|
0
|
0
|
|
|
|
unless( $> == $targetuid && $< == $targetuid && $) == $targetgid && $( == $targetgid) { |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
croak qq(Could not set Perl permissions to uid $targetuid, gid $targetgid); |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub drop_permissions_id { |
|
194
|
0
|
|
|
0
|
0
|
|
my($targetuid, $targetgid) = @_; |
|
195
|
0
|
|
|
|
|
|
_drop_permissions_id($targetuid, $targetgid); |
|
196
|
0
|
|
|
|
|
|
$> = $targetuid; |
|
197
|
0
|
|
|
|
|
|
$< = $targetuid; |
|
198
|
0
|
|
|
|
|
|
$) = $targetgid; |
|
199
|
0
|
|
|
|
|
|
$( = $targetgid; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub generate_mac_address { |
|
203
|
0
|
|
|
0
|
0
|
|
my @bytes = (); |
|
204
|
0
|
|
|
|
|
|
for (my $i = 0; $i < 6; $i ++) { |
|
205
|
0
|
|
|
|
|
|
push @bytes, int(rand(256)); |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# make sure bit 0 (broadcast) of first byte is not set, |
|
209
|
|
|
|
|
|
|
# and bit 1 (local) is set. |
|
210
|
|
|
|
|
|
|
# i.e. via bitwise AND with 254 and bitwise OR with 2. |
|
211
|
|
|
|
|
|
|
|
|
212
|
0
|
|
|
|
|
|
$bytes[0] &= 254; |
|
213
|
0
|
|
|
|
|
|
$bytes[0] |= 2; |
|
214
|
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
return sprintf('%02x:%02x:%02x:%02x:%02x:%02x', @bytes); |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
__END__ |