• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

eg/H05-Nov-2021-559438

t/H05-Nov-2021-295235

.gitignoreH A D05-Nov-2021212 2221

ChangesH A D05-Nov-20212.6 KiB6655

MANIFESTH A D05-Nov-2021307 2524

META.ymlH A D05-Nov-2021548 2322

Makefile.PLH A D03-May-20222.2 KiB9775

READMEH A D05-Nov-20216.1 KiB167126

Sysctl.pmH A D05-Nov-202111.1 KiB39967

Sysctl.xsH A D03-May-202238.6 KiB937810

TODOH A D05-Nov-2021643 2417

typemapH A D05-Nov-202118 21

README

1This file is the README for BSD::Sysctl
2
3INSTALLATION
4
5perl Makefile.PL
6make
7make test
8make install
9
10Building this module requires a FreeBSD system and a C compiler.
11Support for OpenBSD and NetBSD will appear in future releases. In
12theory, this module should be able to handle any system that uses
13a sysctl interface to the kernel.
14
15UNINSTALLATION
16
17This Perl module has components which will be found in the following
18directories:
19
20  .../perl5/site_perl/5.x.x/mach/auto/BSD/Sysctl/*
21  .../perl5/site_perl/5.x.x/mach/BSD/Sysctl.pm
22  .../perl5/5.x.x/man/man3/BSD::Sysctl.3
23
24TESTING
25
26This module requires the following module for thorough testing:
27
28  Test::More
29
30This module will use the following modules if they are available:
31(their absence is not a problem).
32
33  Test::Pod
34  Test::Pod::Coverage
35
36In order to test whether setting sysctl variables works, a number
37of preconditions must be met.
38
39Firstly, the test script t/02-set.t must be run with root privileges
40(either as root, or via sudo for example). If not, it will bail out
41gracefully.
42
43Secondly, you must choose the sysctl variable you wish to use.  The
44sysctl(8) man page lists a number of variables that can be modified.
45You must also choose a new value for the variable to store.
46
47BE CAREFUL: if you do not know what you are doing, you can render
48your system unstable, lose network connectivity, lose access to
49your filesystem and/or cause the system to lock up.
50
51If in doubt, a safe choice is a tweak for UDP blackholing:
52
53  net.inet.udp.blackhole
54
55This is usually set to 0, but you can (and often should) set it to
561. This controls how the system behaves when someone (usually a
57cracker), probes your machine.
58
59Finally, two environment variables must be set. The first variable,
60TEST_BSD_SYSCTL_NAME, holds the name of the variable, and the
61second variable, TEST_BSD_SYSCTL_VALUE, holds the new value to which
62the variable should be set.
63
64On a Bourne shell, the following commands will work:
65
66  % export TEST_BSD_SYSCTL_NAME=net.inet.udp.blackhole
67  % export TEST_BSD_SYSCTL_VALUE=1
68  % sudo make test
69
70When these three preconditions are met, the test script will then
71change the variable to the new value, and then, most importantly,
72set it back to the initial value. In the unlikely event that the
73test script crashes half way through, you will have to restore the
74variable manually to its initial value.
75
76HELPING
77
78Patches welcome!
79
80If you would like to see this module work on other operating systems,
81but don't particularly wish to hack on the source yourself, and you
82have a publically accessible machine on the net, and you are fine
83with the idea of giving me an account on said machine, then drop
84me a line.
85
86I will send you my public key for connecting via ssh. All I need
87is a shell and a C compiler, and I give you my word that I will not
88do anything evil. If required, I will connect only from a fixed
89address (which would allow you to set up an AllowUsers USER@HOST
90rule in your sshd_config file).
91
92IMPLEMENTATION
93
94A sysctl variable is usually specified symbolically, such as
95"net.inet.tcp.sendspace" (also referred to as a MIB). Internally,
96the kernel operates on an equivalent numeric list (also referred
97to as an OID). Furthermore, different variables contain different
98values. They may be numeric or character strings, they may be a
99single value, a list of values, or a set of key/value pairs.
100
101When a sysctl variable is fetched, the module first asks the kernel
102to convert the symbolic name to its numeric equivalent.  If this
103step fails, the module considers that the variable does not exist.
104This step is handled by _mib_info().
105
106If this step succeeds, the module now has the list of numeric values
107(the OID) that corresponds to the variable. It then performs a
108second call to ask the kernel how the result is to be formatted.
109
110It is a reasonable assumption to imagine that different BSD variants
111(perhaps even between major releases of the same operating system)
112will use different techniques for performing this step, and so the
113XS code will probably require a certain amount of conditional
114compilation.
115
116In FreeBSD, the formatting information is returned as a string. For
117instance, "I" is an integer, "IU" is an unsigned integer, "LU" is
118an unsigned long, "A" is an ASCII string.  More complex variables
119return the name of the C struct to use to decode the result, for
120instance the formatting code for "vm.loadavg" is "S,loadavg", which
121implies that the result should be cast to a "struct loadavg".
122
123Sometimes a number of auxilliary header files need to be included
124in order for the header file that defines the structure in question
125to be specified completely. The eg/sizeof.c is handy for figuring
126out what is required.
127
128A performance optimisation concerning the formatting information
129is to map the string names to integers (this means that the results
130can be dealt with in a switch statement, rather than a long if/else
131chain. The mapping table is stored in the mibfmt.map file, and is
132processed by Makefile.PL to produced the C header file needed by
133the XS file.
134
135Once the formatting key has been converted to an integer, the
136information is saved in the %MIB_CACHE hash table.  Each key
137points to a packed list of integers:
138
139  +-------------------------+
140  | format value            |
141  +-------------------------+
142  | count of remaining ints |
143  +-------------------------+
144  | oid element 1           |
145  +-------------------------+
146  | oid element 2           |
147  +-------------------------+
148  | ...                     |
149  +-------------------------+
150  | oid element n           |
151  +-------------------------+
152
153This information can be unpacked in Perl-space with the following
154unpack() template: 'i i/i'.
155
156When adding other operating systems, new format values may be
157appended to the end of the mapping file.
158
159After this step has been performed, _mib_lookup() may then
160proceed with fetching the value associated with the variable.
161The system call is performed, and, assuming no errors, the
162results are formatted (according to what %MIB_CACHE indicates, and
163a scalar, or a reference to an array or hash, is returned.
164
165Reading the source to the sysctl binary is very instructive.  (i.e.
166/usr/src/sbin/sysctl/sysctl.c on FreeBSD).
167