| 1 | package Net::Connection::FreeBSD_sockstat; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Net::Connection; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | our @EXPORT = qw(sockstat_to_nc_objects); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | Net::Connection::FreeBSD_sockstat - Creates Net::Connection objects using sockstat on FreeBSD. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | Version 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | our $VERSION = '1.0.0'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | use Net::Connection::FreeBSD_sockstat; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | my @objects; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | eval{ @objects=&sockstat_to_nc_objects; }; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # this time don't resolve ports, ptrs, or usernames | ||||||||||||||||||||||||||||||||||||||||||
| 33 | my $args={ | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ports=>0, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ptrs=>0, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | eval{ @objects=&sockstat_to_nc_objects( $args )); }; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head1 REQUIREMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | This parses the JSON emitted by 'sockstat -46s --libxo json'. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | libxo support was added to sockstat in FreeBSD 15.0. Earlier releases ship a | ||||||||||||||||||||||||||||||||||||||||||
| 44 | sockstat that does not understand '--libxo' and will exit with a usage error. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | Process information is gathered by calling ps instead of reading the process | ||||||||||||||||||||||||||||||||||||||||||
| 47 | table directly, so nothing outside of the base system is needed for that. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | Worth noting for anyone coming from 0.0.x, which used Proc::ProcessTable, is | ||||||||||||||||||||||||||||||||||||||||||
| 50 | that ps appends the accounting name in parentheses when a proc has rewritten | ||||||||||||||||||||||||||||||||||||||||||
| 51 | its argv, so proc for those reads like the first of the two below instead of | ||||||||||||||||||||||||||||||||||||||||||
| 52 | the second. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | MUSHclient.exe (wine) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | MUSHclient.exe | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =head2 sockstat_to_nc_objects | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | This parses the output of 'sockstat -46s --libxo json'. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | Upon error, this will die. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =head3 args hash | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =head4 ports | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Attempt to resolve the port names. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | This value is a Perl boolean. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =head4 ptrs | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | Attempt to resolve the PTRs. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | This value is a Perl boolean. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head4 proc_info | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Call ps and use that to fill in additional info. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | This is incompatible with the string option. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | This defaults to true if no string is specified. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | This value is a Perl boolean. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =head4 string | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | If this is specified, it parses the string instead of calling sockstat. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | The string is expected to be the JSON produced by | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 'sockstat -46s --libxo json'. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | If running this on anything other than FreeBSD with out passing this, it will die. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =head4 zombie_skip | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | This skips sockets that are not associated with any process. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | These are the sockets that print as the line below with the standard text | ||||||||||||||||||||||||||||||||||||||||||
| 107 | output, the JSON for them lacking the user, command, pid, and fd keys. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS PATH STATE CONN STATE | ||||||||||||||||||||||||||||||||||||||||||
| 110 | ? ? ? ? tcp6 ::1:4045 *:* LISTEN | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | When these are not skipped, the resulting objects have a username of ? with | ||||||||||||||||||||||||||||||||||||||||||
| 113 | the PID and UID left undefined, as Net::Connection requires those two to be | ||||||||||||||||||||||||||||||||||||||||||
| 114 | numeric when they are defined. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | This defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | The value taken is a Perl boolean. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 29 | ▶ | sub sockstat_to_nc_objects { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sockstat_to_nc_objects: called CC=28 SCAR=34.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 29 | my %func_args; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 29 | ▶ | if ( defined( $_[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 29 | %func_args = %{ $_[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # set the defaults for the various args | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 29 | ▶ | if ( !defined( $func_args{proc_info} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # if a string is set, default to false | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 27 | ▶ | if ( defined( $func_args{string} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 26 | $func_args{proc_info} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | $func_args{proc_info} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 29 | ▶ | if ( !defined( $func_args{ptrs} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | $func_args{ptrs} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 29 | ▶ | if ( !defined( $func_args{ports} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | $func_args{ports} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 29 | ▶ | if ( !defined( $func_args{zombie_skip} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 26 | $func_args{zombie_skip} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 29 | my $sockstat_raw; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 29 | ▶ | if ( defined( $func_args{string} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 28 | $sockstat_raw = $func_args{string}; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 28 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | die('Function args string and proc_info are mutually exclusive'); | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | ▶ | if ( $^O !~ /freebsd/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | die('According to $^O, this is not FreeBSD and this is specifically written for FreeBSDs sockstat'); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | $sockstat_raw = `sockstat -46s --libxo json`; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | die( 'Calling "sockstat -46s --libxo json" failed with a exit code of ' . ( $? >> 8 ) ); | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } ## end else [ if ( defined( $func_args{string} ) ) ] | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 27 | my $sockstat_parsed; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 27 | eval { $sockstat_parsed = decode_json($sockstat_raw); }; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 27 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | die( 'Failed to decode the JSON from sockstat... ' . $@ ); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 23 | ▶ | if ( ( ref($sockstat_parsed) ne 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $sockstat_parsed ne "HASH" or ref $sockstat_parsed->{"sockstat"} ne "HASH" or ref $sockstat_parsed->{"sockstat"}{"socket"} ne "ARRAY"
ref $sockstat_parsed ne "HASH" or ref $sockstat_parsed->{"sockstat"} ne "HASH"
MC/DC100%3/3
ref $sockstat_parsed ne "HASH" or ref $sockstat_parsed->{"sockstat"} ne "HASH" or ref $sockstat_parsed->{"sockstat"}{"socket"} ne "ARRAY"
ref $sockstat_parsed ne "HASH"
ref $sockstat_parsed->{"sockstat"} ne "HASH"
ref $sockstat_parsed->{"sockstat"}{"socket"} ne "ARRAY"
Truth table100%4/4
A or B or C
A= ref $sockstat_parsed ne "HASH"B= ref $sockstat_parsed->{"sockstat"} ne "HASH"C= ref $sockstat_parsed->{"sockstat"}{"socket"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | || ( ref( $sockstat_parsed->{sockstat} ) ne 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||||
| 176 | || ( ref( $sockstat_parsed->{sockstat}{socket} ) ne 'ARRAY' ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | die('The JSON from sockstat does not contain the array .sockstat.socket'); | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # fetched once and then reused for each socket the PID in question owns | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 15 | my $proc_info; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 15 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $proc_info = &_ps_proc_info; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | # holds the Net::Connection objects | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 15 | my @nc_objects; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 15 | foreach my $socket ( @{ $sockstat_parsed->{sockstat}{socket} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | # sockets with no owning process lack the user, command, pid, and fd keys | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 31 | my $orphaned = 0; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 31 | ▶ | if ( !defined( $socket->{pid} ) || !defined( $socket->{user} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $socket->{"pid"} and defined $socket->{"user"}
MC/DC100%2/2
defined $socket->{"pid"} and defined $socket->{"user"}
defined $socket->{"pid"}
defined $socket->{"user"}
Truth table100%3/3
A and B
A= defined $socket->{"pid"}B= defined $socket->{"user"}
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 12 | $orphaned = 1; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 31 | ▶ | if ( $orphaned && $func_args{zombie_skip} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$orphaned and $func_args{"zombie_skip"}
MC/DC100%2/2
$orphaned and $func_args{"zombie_skip"}
$orphaned
$func_args{"zombie_skip"}
Truth table100%3/3
A and B
A= $orphanedB= $func_args{"zombie_skip"}
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # Net::Connection wants the PID and UID left undef rather than filled | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # in with something like a question mark as it requires them to be | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # numeric when they are defined | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 25 | my $uid; | |||||||||||||||||||||||||||||||||||||||||
| 206 | my $pid; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 25 | my $username = '?'; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 25 | ▶ | if ( !$orphaned ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 19 | $pid = $socket->{pid}; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 19 | $username = $socket->{user}; | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # sockstat prints the raw UID when it can not be resolved to a name | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 19 | $uid = getpwnam($username); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 19 | ▶ | if ( !defined($uid) && ( $username =~ /^[0-9]+$/ ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $uid and $username =~ /^[0-9]+$/
MC/DC100%2/2
not defined $uid and $username =~ /^[0-9]+$/
not defined $uid
$username =~ /^[0-9]+$/
Truth table100%3/3
not(A) and B
A= not defined $uidB= $username =~ /^[0-9]+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | $uid = $username; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } ## end if ( !$orphaned ) | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | # the basic args initially for Net::Connection | ||||||||||||||||||||||||||||||||||||||||||
| 220 | my $args = { | ||||||||||||||||||||||||||||||||||||||||||
| 221 | pid => $pid, | ||||||||||||||||||||||||||||||||||||||||||
| 222 | uid => $uid, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | username => $username, | ||||||||||||||||||||||||||||||||||||||||||
| 224 | state => '', | ||||||||||||||||||||||||||||||||||||||||||
| 225 | proto => $socket->{proto}, | ||||||||||||||||||||||||||||||||||||||||||
| 226 | ports => $func_args{ports}, | ||||||||||||||||||||||||||||||||||||||||||
| 227 | ptrs => $func_args{ptrs}, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | uid_resolve => $func_args{uid_resolve}, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 25 | }; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | # libxo breaks the addresses out into their own containers, meaning | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # there is no need to figure out where the address stops and the port | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # begins like there is with the text output | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 25 | $args->{local_host} = $socket->{local}{address}; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 25 | $args->{local_port} = &_port_stringify( $socket->{local}{port} ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 25 | $args->{foreign_host} = $socket->{foreign}{address}; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 25 | $args->{foreign_port} = &_port_stringify( $socket->{foreign}{port} ); | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # only emitted for the protocols that have a connection state, so TCP and SCTP | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 25 | ▶ | if ( defined( $socket->{'conn-state'} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 21 | $args->{state} = $socket->{'conn-state'}; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # put together process info if requested | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # skips adding it if the socket is orphaned as that means the proc no longer exists | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 25 | ▶ | if ( $func_args{proc_info} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$func_args{"proc_info"} and not $orphaned and defined $proc_info->{$pid}
$func_args{"proc_info"} and not $orphaned
MC/DC0%0/3
$func_args{"proc_info"} and not $orphaned and defined $proc_info->{$pid}
$func_args{"proc_info"}
not $orphaned
defined $proc_info->{$pid}
Truth table25%1/4
A and not(B) and C
A= $func_args{"proc_info"}B= not $orphanedC= defined $proc_info->{$pid}
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | && !$orphaned | ||||||||||||||||||||||||||||||||||||||||||
| 250 | && defined( $proc_info->{$pid} ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $args->{proc} = $proc_info->{$pid}{proc}; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $args->{wchan} = $proc_info->{$pid}{wchan}; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $args->{pctmem} = $proc_info->{$pid}{pctmem}; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | $args->{pctcpu} = $proc_info->{$pid}{pctcpu}; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $args->{pid_start} = $proc_info->{$pid}{pid_start}; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } ## end if ( $func_args{proc_info} && !$orphaned &&...) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 25 | push( @nc_objects, Net::Connection->new($args) ); | |||||||||||||||||||||||||||||||||||||||||
| 260 | } ## end foreach my $socket ( @{ $sockstat_parsed->{sockstat...}}) | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 15 | return @nc_objects; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } ## end sub sockstat_to_nc_objects | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # libxo emits the port as a integer, using zero where the text output uses a | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # star, so map it back so the returned objects match what sockstat displays | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 57 | ▶ | sub _port_stringify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_port_stringify: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 57 | my $port = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 57 | ▶ | if ( !defined($port) || ( $port eq '0' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $port or $port eq 0
MC/DC100%2/2
not defined $port or $port eq 0
not defined $port
$port eq 0
Truth table100%3/3
not(A) or B
A= not defined $portB= $port eq 0
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 20 | return '*'; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 37 | return $port; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | # | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # Calls ps and returns a hash ref, keyed on PID, of the bits that | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # Net::Connection wants filled in. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 8 | ▶ | sub _ps_proc_info { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ps_proc_info: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 8 | my $ps_raw = `ps -axww -o pid,wchan,%cpu,%mem,etimes,command --libxo json`; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 8 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | die( 'Calling ps failed with a exit code of ' . ( $? >> 8 ) ); | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 7 | my $ps_parsed; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 7 | eval { $ps_parsed = decode_json($ps_raw); }; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | die( 'Failed to decode the JSON from ps... ' . $@ ); | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 6 | ▶ | if ( ( ref($ps_parsed) ne 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $ps_parsed ne "HASH" or ref $ps_parsed->{"process-information"} ne "HASH" or ref $ps_parsed->{"process-information"}{"process"} ne "ARRAY"
ref $ps_parsed ne "HASH" or ref $ps_parsed->{"process-information"} ne "HASH"
MC/DC100%3/3
ref $ps_parsed ne "HASH" or ref $ps_parsed->{"process-information"} ne "HASH" or ref $ps_parsed->{"process-information"}{"process"} ne "ARRAY"
ref $ps_parsed ne "HASH"
ref $ps_parsed->{"process-information"} ne "HASH"
ref $ps_parsed->{"process-information"}{"process"} ne "ARRAY"
Truth table100%4/4
A or B or C
A= ref $ps_parsed ne "HASH"B= ref $ps_parsed->{"process-information"} ne "HASH"C= ref $ps_parsed->{"process-information"}{"process"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | || ( ref( $ps_parsed->{'process-information'} ) ne 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||||
| 297 | || ( ref( $ps_parsed->{'process-information'}{process} ) ne 'ARRAY' ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 4 | die('The JSON from ps does not contain the array .process-information.process'); | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | # ps reports how long the proc has been running instead of when it started | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 2 | my $proc_info = {}; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | foreach my $proc ( @{ $ps_parsed->{'process-information'}{process} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 307 | 4 | ▶ | if ( !defined( $proc->{pid} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # ps already wraps the names of kernel procs in square brackets | ||||||||||||||||||||||||||||||||||||||||||
| 312 | $proc_info->{ $proc->{pid} } = { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | proc => $proc->{command}, | ||||||||||||||||||||||||||||||||||||||||||
| 314 | wchan => $proc->{'wait-channel'}, | ||||||||||||||||||||||||||||||||||||||||||
| 315 | pctcpu => $proc->{'percent-cpu'} + 0, | ||||||||||||||||||||||||||||||||||||||||||
| 316 | pctmem => $proc->{'percent-memory'} + 0, | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | pid_start => $now - $proc->{'elapsed-times'}, | |||||||||||||||||||||||||||||||||||||||||
| 318 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | } ## end foreach my $proc ( @{ $ps_parsed->{'process-information'...}}) | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | return $proc_info; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } ## end sub _ps_proc_info | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | Please report any bugs or feature requests to C<bug-net-connection-freebsd_sockstat at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 331 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-FreeBSD_sockstat>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 332 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | perldoc Net::Connection::FreeBSD_sockstat | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-FreeBSD_sockstat> | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | L<https://cpanratings.perl.org/d/Net-Connection-FreeBSD_sockstat> | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | L<https://metacpan.org/release/Net-Connection-FreeBSD_sockstat> | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =item * Git Repo | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | L<https://gitea.eesdp.org/vvelox/Net-Connection-FreeBSD_sockstat> | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | This software is Copyright (c) 2021 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 9 | 1; # End of Net::Connection::FreeBSD_sockstat | |||||||||||||||||||||||||||||||||||||||||