Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For ASCII string values do not leak NULL terminating character, which is a C thing, into perl. #3

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Makefile
Makefile.old
Build
Build.bat
META.*
MYMETA.*
.build/
_build/
cover_db/
blib/
inc/
.lwpcookies
.last_cover_stats
nytprof.out
pod2htm*.tmp
pm_to_blib
Sysctl.bs
Sysctl.c
Sysctl.o
bsd-sysctl.h
bsd-sysctl.ph
2 changes: 0 additions & 2 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Changes
LICENSE
Makefile.PL
MANIFEST
MANIFEST.SKIP
META.yml Module meta-data (added by MakeMaker)
README
Sysctl.pm
Expand Down
4 changes: 0 additions & 4 deletions MANIFEST.SKIP

This file was deleted.

108 changes: 38 additions & 70 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,33 @@ if ($Config{osname} ne 'freebsd') {

my %define;
my $maxlen = 0;
my $num = 16; # numbers below 16 are reserved for <sys/sysctl.h> CTLTYPE_*
while (<DATA>) {
chomp;
next unless my ($key, $num, $str) = ($_ =~ /^(\S+)\t(\S+)\t(\S+)/);
if ($str eq 'auto') {
if ($key =~ /^[ST],(.*)/) {
$str = 'FMT_' . uc($1);
}
else {
die "$0: cannot resolve auto name from $key\n";
}
}
next if /^#/ || /^\s*$/;
my $key = 'S,' . $_;
my $str = 'CTLTYPE_' . uc($_);
$maxlen = length($str) if $maxlen < length($str);
$define{$key} = [$str, $num];
$define{$key} = [$str, $num++];
}

open my $out_h, '>', 'bsd-sysctl.h' or die "Cannot open C header for output: $!\n";
open my $out_pl, '>', 'bsd-sysctl.pl' or die "Cannot open Perl header for output: $!\n";
open my $out_pl, '>', 'bsd-sysctl.ph' or die "Cannot open Perl header for output: $!\n";

my $years = (gmtime)[5]+1900;
$years = ($years == 2006) ? $years : "2006-$years";
open my $in_sys, '<', '/usr/include/sys/sysctl.h' or die "Cannot open sys/sysctl.h: $!\n";

print $out_h <<EOH;
/* bsd-sysctl.h -- defines for $module_name
*
* Copyright (C) $years, David Landgren, all rights reserved.
* This file genned by $0 at @{[scalar gmtime]}
*/

EOH

# bootinfo.h header file not available on the AMD64 platform
if (-r '/usr/include/machine/bootinfo.h') {
print $out_h "#include <machine/bootinfo.h>\n";
while (<$in_sys>) {
next unless (/^#define (CTLTYPE_[A-Z0-9]+)\s+(0x[0-9a-f]+|[0-9]+)/);
printf $out_pl "use constant %-${maxlen}s => %s;\n", $1, $2;
}

print $out_pl <<EOH;
# bsd-sysctl.pl -- constants for $module_name
#
# Copyright (C) $years, David Landgren, all rights reserved.
# This file genned by $0 at @{[scalar gmtime]}

EOH
close $in_sys;

for my $key (sort keys %define) {
printf $out_h "#define %-${maxlen}s %2d\n", @{$define{$key}};
printf $out_pl "use constant %-${maxlen}s => %2d;\n", @{$define{$key}};
}

print $out_pl "\n1;\n";

close $out_h;
close $out_pl;

Expand All @@ -74,49 +51,40 @@ WriteMakefile(
PREREQ_PM => {
'XSLoader' => 0
},
PM_FILTER => 'perl -pe "if (/^\#include (.+)$$/) { \
open FILE, \\$$1 or \
die \"open \\$$1\"; \
while (<FILE>) { print; }; \
close FILE; \
next; \
};"',
clean => {
FILES => 'bsd-sysctl.h bsd-sysctl.pl',
FILES => 'bsd-sysctl.h bsd-sysctl.ph',
},
MIN_PERL_VERSION => '5.8.0',
);

__DATA__
#
# List of constant definitions.
# List of structures we are able to parse.
#
# This produces
# -- bsd-sysctl.h (#defines for Sysctl.xs)
# -- bsd-sysctl.pl (use constants for Sysctl.pm)
#
# auto string definitions are derived from the first
# field, for instance, S,bootinfo => FMT_BOOTINFO
# (This eliminates a chance to introduce typos).
# _key numeric string

A 1 FMT_A
I 2 FMT_INT
IU 3 FMT_UINT
L 4 FMT_LONG
LU 5 FMT_ULONG
N 6 FMT_N
S,bootinfo 7 auto
S,clockinfo 8 auto
S,devstat 9 auto
S,icmpstat 10 auto
S,igmpstat 11 auto
S,ipstat 12 auto
S,loadavg 13 auto
S,mbstat 14 auto
S,nfsrvstats 15 auto
S,nfsstats 16 auto
S,ntptimeval 17 auto
S,rip6stat 18 auto
S,tcpstat 19 auto
S,timeval 20 auto
S,udpstat 21 auto
S,vmtotal 22 auto
S,xinpcb 23 auto
S,xvfsconf 24 auto
T,struct_cdev 25 auto
Q 26 FMT_64
QU 27 FMT_U64
# -- bsd-sysctl.ph (use constants for Sysctl.pm)

clockinfo
devstat
icmpstat
igmpstat
ipstat
loadavg
nfsrvstats
nfsstats
ntptimeval
rip6stat
tcpstat
timeval
udpstat
vmtotal
xinpcb
xvfsconf
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This file is the README for BSD::Sysctl version 0.11
This file is the README for BSD::Sysctl

INSTALLATION

Expand Down
56 changes: 16 additions & 40 deletions Sysctl.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# BSD::Sysctl.pm - Access BSD sysctl(8) information directly
#
# Copyright (C) 2021 Gleb Smirnoff, all rights reserved.
# Copyright (C) 2006-2014 David Landgren, all rights reserved.

package BSD::Sysctl;
Expand All @@ -12,36 +13,10 @@ use XSLoader;

use vars qw($VERSION @ISA %MIB_CACHE %MIB_SKIP @EXPORT_OK);

$VERSION = '0.11';
$VERSION = '0.12';
@ISA = qw(Exporter);

use constant FMT_A => 1;
use constant FMT_INT => 2;
use constant FMT_UINT => 3;
use constant FMT_LONG => 4;
use constant FMT_ULONG => 5;
use constant FMT_N => 6;
use constant FMT_BOOTINFO => 7;
use constant FMT_CLOCKINFO => 8;
use constant FMT_DEVSTAT => 9;
use constant FMT_ICMPSTAT => 10;
use constant FMT_IGMPSTAT => 11;
use constant FMT_IPSTAT => 12;
use constant FMT_LOADAVG => 13;
use constant FMT_MBSTAT => 14;
use constant FMT_NFSRVSTATS => 15;
use constant FMT_NFSSTATS => 16;
use constant FMT_NTPTIMEVAL => 17;
use constant FMT_RIP6STAT => 18;
use constant FMT_TCPSTAT => 19;
use constant FMT_TIMEVAL => 20;
use constant FMT_UDPSTAT => 21;
use constant FMT_VMTOTAL => 22;
use constant FMT_XINPCB => 23;
use constant FMT_XVFSCONF => 24;
use constant FMT_STRUCT_CDEV => 25;
use constant FMT_64 => 26;
use constant FMT_U64 => 27;
#include bsd-sysctl.ph

push @EXPORT_OK, 'sysctl';
sub sysctl {
Expand Down Expand Up @@ -85,10 +60,11 @@ sub set {
}

sub iterator {
my $class = shift;
my $name = shift;
my ($class, $name, %args) = @_;
my $self;

$self->{head} = $name || undef;
$self->{noskip} = 1 if (defined($args{noskip}));
return bless $self, $class;
}

Expand All @@ -105,7 +81,9 @@ sub value {

sub reset {
my $self = shift;
delete $self->{_ctx};
delete $self->{_next};
delete $self->{_name};
delete $self->{_len0};
return $self;
}

Expand All @@ -117,8 +95,7 @@ BSD::Sysctl - Manipulate kernel sysctl variables on BSD-like systems

=head1 VERSION

This document describes version 0.11 of BSD::Sysctl, released
2014-01-22.
This document describes version 0.12 of BSD::Sysctl

=head1 SYNOPSIS

Expand Down Expand Up @@ -260,6 +237,11 @@ fails, undef is returned.
print $k->name, '=', $k->value, "\n";
}

To force iteration through variables that are marked with CTLFLAG_SKIP
use 'noskip' argument:

my $k = BSD::Sysctl->iterator( 'kern', ( noskip => 1 ));

=item next

Moves the iterator to the next sysctl variable and loads the
Expand Down Expand Up @@ -349,7 +331,7 @@ at least for the time being. This is a bug that should be reported.

=head1 LIMITATIONS

At the current time, FreeBSD versions 4.x through 8.x are
At the current time only officially supported FreeBSD versions are
supported.

I am looking for volunteers to help port this module to NetBSD and
Expand All @@ -359,12 +341,6 @@ for more information.

=head1 BUGS

Some branches are not iterated on FreeBSD 4 (and perl 5.6.1). Most
notably, the C<vm.stats> branch. I am not sure of the reason, but
it's a failure in a C<sysctl> system call, so it could be related
to that release. As FreeBSD 4.x reached the end of its supported
life in 2007, I'm not particularly fussed.

Please report all bugs at
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=BSD-Sysctl|rt.cpan.org>.

Expand Down
Loading