/
lhg-service
executable file
·432 lines (314 loc) · 10.6 KB
/
lhg-service
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/perl
# regular check of hardware, if new hardware is found data is uploaded
# to Linux-Hardware-Guide database
# Currently only checking for new PCI and USB devices
$sid = $ARGV[0];
$upload_server="http://library.linux-hardware-guide.com";
$TMPDIR = "$ENV{HOME}/.tmp";
# Check for cached uploads - if found upload now
check_missed_uploads();
# Flag, if new hardware is found
$found_new_hardware = 0;
# check for first run
check_first_run();
check_root();
check_spool();
# ToDo: check if last upload failed -> $found_new_hardware = 1;
create_fingerprint_pci();
create_fingerprint_usb();
#print "PCI: \n";
#print "Known: @known_pci_array \n";
#print "Scan: @foundarray_pci";
#print "\nUSB: \n";
#print "Known: @known_usb_array \n";
#print "@foundarray_usb";
check_for_new_ids();
if ($found_new_hardware == 1) {
upload_data();
} else {
print "No new hardware found \n";
}
make_clean();
exit 0;
#
# create_fingerprint_pci
#
# read pci IDs of hardware
#
sub create_fingerprint_pci {
# modified by Charles K Barcza <blackPanther OS> because the
#/var/tmp or $ENV{HOME}/tmp better if system use tmpfs!
system ("mkdir -p $TMPDIR/.LHG_".$sid);
system ("lspci -nnk > $TMPDIR/.LHG_".$sid."/lspci.txt");
open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
open(FILEN, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
$nextline=<FILEN>;
$i=0;
#print "PCI Fingerprint: \n";
while ( <FILE> ) {
$nextline = <FILEN>;
#print "Line: $_";
$pciid = grab_pciid($_);
if ($pciid != "") {
#print "PCIID: $pciid \n";
$subsystemid = grab_subsystemid($nextline);
#print "Subsystem: $subsystemid \n";
}
if ($pciid != "") {
#print "$pciid,";
@foundarray_pci = (@foundarray_pci, $pciid);
}
#
#
}
#print "\n";
}
sub grab_subsystemid {
my $line = shift;
#print "-------\nLine: $line ";
if ( ($line =~ m/Subsystem/) and ($line =~ m/\w\w\w\w:\w\w\w\w/) ) {
my $pciid = substr($line,$-[0],9);
#print "PCIID: $pciid \n";
return $pciid;
} else {
#print " no match \n";
}
}
#
#
### USB subroutines
#
#
sub create_fingerprint_usb {
system ("lsusb > $TMPDIR/.LHG_".$sid."/lsusb.txt");
open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lsusb.txt");
$i=0;
#print "USB Fingerprint: \n";
while ( <FILE> ) {
#print "Line: $_ \n";
$usbid = grab_usbid($_);
#$usbname = grab_usbname($_);
if ($usbid ne "") {
#print "$usbid,";
@foundarray_usb = (@foundarray_usb, $usbid);
}
$i++;
#print "\n";
}
}
sub grab_usbid {
$line = shift;
#print "-------\nLine: $line ";
if ($line =~ m/\w\w\w\w:\w\w\w\w/) {
return substr($line,$-[0],9);
} else {
#print " no match \n";
}
}
sub grab_pciid {
$line = shift;
#print "-------\nLine: $line ";
if ( ($line =~ m/\w\w\w\w:\w\w\w\w/) and !($line =~ m/Subsystem/) ) {
$pciid = substr($line,$-[0],9);
#print "PCIID: $pciid \n";
return $pciid;
} else {
#print " no match \n";
}
}
sub check_root {
if ( $< != 0 ) {
#print "WARNING:\n";
#print "========\n";
print "Sorry! This script needs to be running with root privileges. ";
print "\n";
exit 1;
}
}
sub check_spool {
if ( -e "/var/spool/lhg-tools/pcidata" ) {
#print "pcidata exists";
# read in known data from spool file
@known_pci_array = lhg_readdata("/var/spool/lhg-tools/pcidata");
} else {
#print "pcidata does not exist";
$found_new_hardware = 1;
system ("mkdir -p /var/spool/lhg-tools/");
system ("touch /var/spool/lhg-tools/pcidata");
system ('echo "# List of PCI data \n" >> /var/spool/lhg-tools/pcidata');
}
if ( -e "/var/spool/lhg-tools/usbdata" ) {
#print "usbdata exists";
@known_usb_array = lhg_readdata("/var/spool/lhg-tools/usbdata");
} else {
#print "usbdata does not exist";
$found_new_hardware = 1;
system ("mkdir -p /var/spool/lhg-tools/");
system ("touch /var/spool/lhg-tools/usbdata");
system ('echo "# List of USB data \n" >> /var/spool/lhg-tools/usbdata');
# read in known data from spool file
}
}
sub make_clean {
# ToDo: cleanup files
}
sub lhg_readdata {
$FILE = shift;
@data_array = ();
open(FILE, $FILE);
while (<FILE>) {
if (substr($_,0,1) eq "#") {
# skip comment lines
} else {
$id = $_;
$id =~ s/(\r)|(\n)//g;
@data_array = (@data_array, $id);
}
}
# ToDo: read data from file
return @data_array;
}
sub check_for_new_ids {
#print "\n";
# compare PCI IDs
foreach (@foundarray_pci)
{
$searchfor = $_ ;
#print "checking $searchfor ...";
#if ($_ ~~ @known_pci_array) {
if ( grep( /^${searchfor}/, @known_pci_array ) ) {
#print "found \n";
}else {
#print "not found \n";
$found_new_hardware = 1;
# add to file
system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/pcidata');
}
}
# compare USB IDs
foreach (@foundarray_usb)
{
$searchfor = $_ ;
#print "checking $searchfor ...";
#if ($_ ~~ @known_usb_array) {
if ( grep( /^${searchfor}/, @known_usb_array ) ) {
#print "found \n";
}else {
#print "not found \n";
$found_new_hardware = 1;
# add to file
system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/usbdata');
}
}
}
sub upload_data {
my $exit_val;
my $pid;
print "found new HW -> starting upload \n";
if ( -e "./scan_hardware") {
print "executing HW scan \n";
$pid = fork;
die "Fork failed: $!" unless defined $pid;
unless ($pid) {
#exec "./long.pl"; # Some long running process.
exec ("./scan_hardware");
}
}elsif ( -e "/usr/bin/scan_hardware") {
print "executing HW scan \n";
$pid = fork;
die "Fork failed: $!" unless defined $pid;
unless ($pid) {
#exec "./long.pl"; # Some long running process.
#exec ("./scan_hardware");
exec ("/usr/bin/scan_hardware");
}
}
#wait for children to finish
waitpid $pid, 0;
#print "PID was: $pid \n";
$exit_val = $? >> 8;
#print "Exit Val: $exit_val \n";
if ($exit_val > 0) {
# upload did not work. store for later
my @chars = (0 .. 9, "A".."Z", "a".."z");
my $rndstring;
$rndstring .= $chars[rand @chars] for 1..8;
system ("mkdir -p /var/spool/lhg-tools/missed_uploads.$rndstring");
system ("cp $TMPDIR/.LHG_$pid/* /var/spool/lhg-tools/missed_uploads.$rndstring/");
} else {
# ToDo: upload done, also upload missed files
}
}
sub check_first_run {
$conf_filename = '/etc/lhg-tools.conf';
$pci_filename = '/var/spool/lhg-tools/pcidata';
if ( (-e $conf_filename) && (-e $pci_filename) ){
# files exists, program was run before
}else {
#enforcing hardware scan
$found_new_hardware = 1;
}
}
sub check_missed_uploads {
@files = glob("/var/spool/lhg-tools/missed_uploads.*");
foreach (@files) {
# do something
#print "uploading $_ ";
upload_cached_files($_);
}
}
sub upload_cached_files {
my $TMPDIR = shift;
my $fname = $TMPDIR;;
$fname =~ s/\/var\/spool\/lhg-tools\///;
my @allfiles = ("alsa_cards.txt","alsa_devices.txt","aplay.txt","cpuinfo.txt",
"dd.err.txt","dd.txt","dmesg.txt","lsb_release.txt","lspci.txt",
"lsusb.txt","lsusb_v.err.txt","lsusb_v.txt","scanimage.txt",
"version.txt","hdparm.txt","hdparm_direct.txt");
my $sid;
use LWP::UserAgent;
use HTTP::Request::Common;
#get session ID
$request = HTTP::Request->new(GET => $upload_server."/uploadrequest.php?v=0.2");
$ua = LWP::UserAgent->new;
$response = $ua->request($request);
$sid = $response->content;
#$ua = LWP::UserAgent->new;
#upload files
$response = $ua->request(POST $upload_server."/uploadfiles.php",
Content_Type => 'form-data',
Content => [ sid => $sid,
file1 => ["$TMPDIR/".$allfiles[0]],
file2 => ["$TMPDIR/".$allfiles[1]],
file3 => ["$TMPDIR/".$allfiles[2]],
file4 => ["$TMPDIR/".$allfiles[3]],
file5 => ["$TMPDIR/".$allfiles[4]],
file6 => ["$TMPDIR/".$allfiles[5]],
file7 => ["$TMPDIR/".$allfiles[6]],
file8 => ["$TMPDIR/".$allfiles[7]],
file9 => ["$TMPDIR/".$allfiles[8]],
file10 => ["$TMPDIR/".$allfiles[9]],
file11 => ["$TMPDIR/".$allfiles[10]],
file12 => ["$TMPDIR/".$allfiles[11]],
file13 => ["$TMPDIR/".$allfiles[12]],
file14 => ["$TMPDIR/".$allfiles[13]],
file15 => ["$TMPDIR/".$allfiles[14]],
file16 => ["$TMPDIR/".$allfiles[15]],
]
);
print " -> ";
print "(".$response->code.") :";
print $response->content;
print "\n";
#print "DEBUG: mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
if ($response->code == HTTP::Status::HTTP_REQUEST_TIMEOUT) {
print "Connection timeout. Upload failed \n";
}elsif ($response->code == 500 ) {
print "Bad Hostname, upload failed. Please check your internet connection. \n";
}else{
#print "upload successful";
#print "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
system "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
}
#exit 0;
}