#!/usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # linux_pci # Linux PCI devices from lspci. # # Revision History: # 2026-10-18 Claude v1.0.0 new sensor, split from unix_pci (bug 210) package linux_pci; use strict; use warnings; use version; our $VERSION = version->declare('v1.0.0'); use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); use library qw(:all); my $CATEGORY = 'pci'; ####################################################### # # parseLspciVmm( $text ) # # Parses lspci -Dvmm output to old Linux pci key paths (v3.7.1 rules). # # Parameters: # $text - text from lspci -Dvmm # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseLspciVmm { my $text = shift; my @out; my $slotRegex = '^[0-9a-z]+[:.][0-9a-z]+'; foreach my $block (split(/\n\n+/, defined($text) ? $text : '')) { next unless $block =~ /\S/; my %row; # sorted, as v3.7.1 did, so duplicate keys get the same '0' suffixes foreach my $line (sort split(/\n/, $block)) { next unless $line =~ /([a-z]+):\s*(\S.*)/i; my ($k, $v) = (lc $1, $2); $k .= '0' while defined($row{$k}); # duplicate key: add 0's $row{$k} = $v; } unless (defined($row{'slot'})) { # no slot, so borrow the first value that looks like one $row{'slot'} = 'Unknown'; foreach my $k (keys %row) { if ($row{$k} =~ m/$slotRegex/i) { $row{'slot'} = $row{$k}; delete $row{$k}; last; } } } # a 'name' key breaks the xml package, so v3.7.1 renamed it if (defined($row{'name'})) { $row{'device name'} = $row{'name'}; delete $row{'name'}; } my $selector = 'Unknown'; foreach my $k ('slot', 'device', 'device0', 'sdevice', 'class', 'vendor', 'svendor') { if (defined($row{$k})) { $selector = $row{$k}; last; } } foreach my $k (sort keys %row) { push @out, "$CATEGORY\t$selector\t$k\t$row{$k}"; } } return \@out; } ####################################################### # # main() # # Emits pci keys. # # Parameters: # none # # Returns: # integer exit status (1 wrong OS, 2 not applicable) # # Side Effects: # runs lspci, writes STDOUT/STDERR # ####################################################### sub main { if (!checkOS({ 'linux' => undef })) { print STDERR "unsupported os\n"; return 1; } unless (validCommandOnSystem({ 'lspci' => undef })) { print STDERR "lspci not found\n"; return 2; } my @out = @{parseLspciVmm(runCommand('lspci', '-Dvmm'))}; print join("\n", @out), "\n" if @out; return @out ? 0 : 1; } exit main() unless caller; 1;