Date: Fri, 23 Apr 1999 14:09:54 -0700 From: Claude Shannon <honig@sprynet.com> To: freebsd-newbies@freebsd.org Subject: a tutorial (donation to freebsd) on sysctl Message-ID: <3720E1A1.64266105@sprynet.com>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
I've attached a short html file which explains the sysctl interface
and kernel hacking to newbies (like myself, who just figured it out).
I have tried these instructions, they work.
I used SunOS for years and I love BSD.
David Honig
honig@sprynet.com
[-- Attachment #2 --]
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.5 [en] (X11; U; FreeBSD 3.0-RELEASE i386) [Netscape]">
</head>
<body text="#000000" bgcolor="#CCFFFF" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<center>
<h1>
How to Add <i>sysctl</i> Variables to Your FreeBSD 3. Kernel</h1></center>
<center><i>With BSD, you have the kernel source, and so can experiment
with the kernel. With BSD's </i>sysctl<i> tool, you can get and set
kernel variables on a running system.</i>
<br>
<hr WIDTH="100%">
<br><i>Adding a <a href="#Adding a Sysctl Variable">Variable</a>
Adding a <a href="#New Root Parent Node">Root Node </a>
<a href="#Sysctl">sysctl</a>
<a href="#KERNEL BUILDING">Kernel Building</a></i>
<hr WIDTH="100%"></center>
<h2>
<a NAME="Adding a Sysctl Variable"></a>Adding a Sysctl Variable to an Existing
Parent Node</h2>
<p><br>Find the kernel file you are interested in. I will choose
<i>/usr/src/sys/i386/isa/random_machdep.c.</i>
<p>In that file, add the following:
<blockquote><b>#include <sys/kernel.h></b>
<br><b>#include <sys/sysctrl.h></b></blockquote>
Now declare a variable you want to monitor and/or control, like so:
<blockquote>static int <b>my_var</b>=0;
<br>SYSCTL_INT( <b>_net</b>, OID_AUTO, my_var, CTLFLAG_RW, &my_var,
0, "");</blockquote>
Note that there are other "parent" nodes besides <b>_net</b>. You
can <a href="#New Root Parent Node">make your own</a>.
<p>Note that there are other types of variables: See <a href="/usr/src/sys/sys/sysctl.h">/usr/src/sys/sys/sysctl.h</a>
<br>(link is good only if this page is hosted on a BSD system!)
<p>Finally, <a href="#KERNEL BUILDING">rebuild the kernel</a> and reboot.
The new variable should be visible with the <i><a href="#Sysctl">sysctl</a></i>
utility.
<p>
<hr WIDTH="100%">
<h2>
How to Declare a <a NAME="New Root Parent Node"></a>New Root Parent
Node:</h2>
<p><br>In <i>/usr/src/sys/sys/sysctl.h</i> add a new CTL_node like so:
<p><tt><font size=-2>/*</font></tt>
<br><tt><font size=-2> * Top-level identifiers</font></tt>
<br><tt><font size=-2> */</font></tt>
<br><tt><font size=-2>#define CTL_UNSPEC
0
/* unused */</font></tt>
<br><tt><font size=-2>#define CTL_KERN
1
/* "high kernel": proc, limits */</font></tt>
<br><tt><font size=-2>#define CTL_VM
2
/* virtual memory */</font></tt>
<br><tt><font size=-2>#define CTL_VFS
3
/* file system, mount type is next */</font></tt>
<br><tt><font size=-2>#define CTL_NET
4
/* network, see socket.h */</font></tt>
<br><tt><font size=-2>#define CTL_DEBUG
5
/* debugging parameters */</font></tt>
<br><tt><font size=-2>#define CTL_HW
6
/* generic cpu/io */</font></tt>
<br><tt><font size=-2>#define CTL_MACHDEP 7
/* machine dependent */</font></tt>
<br><tt><font size=-2>#define CTL_USER
8
/* user-level */</font></tt>
<br><tt><font size=-2>#define CTL_P1003_1B 9
/* POSIX 1003.1B */</font></tt>
<br><b><tt><font size=-1>#define CTL_EXPER 10
/* DAH EXPERIMENTAL */</font></tt></b>
<br><tt><font size=-2>#define CTL_MAXID </font><b><font size=-1>
11 </font></b><font size=-2>
/* number of valid top-level ids */</font></tt>
<p><tt><font size=-2>/* DAH MODIFIED TO INCLUDE "EXPER" NODE *******/</font></tt>
<br><tt><font size=-2>#define CTL_NAMES { \</font></tt>
<br><tt><font size=-2> { 0, 0
}, \</font></tt>
<br><tt><font size=-2> { "kern",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "vm",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "vfs",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "net",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "debug",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "hw",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "machdep",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "user",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> { "p1003_1b",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2> </font><b><font size=-1>{
"exper", CTLTYPE_NODE }, \</font></b></tt>
<br><tt><font size=-2>}</font></tt>
<br>
<p>Then initialize that new node without a parent, like so:
<blockquote>SYSCTL_NODE(<b> , </b>CTL_EXPER, <b>exper</b>, CTLFLAG_RW,
0, "description");</blockquote>
(As seen in <i>/usr/src/sys/kern/kern_mib.c)</i>
<p>
<hr WIDTH="100%">
<h2>
<a NAME="KERNEL BUILDING"></a>KERNEL BUILDING</h2>
<p><br>To reconfigure and rebuild a kernel, do the following.
<br>To rebuild after a simple source-file change, start after the config
step.
<blockquote># cd /usr/src/sys/i386/conf
<br># cp CURRENT <i>NEWKERN</i>
<br># edit <i>NEWKERN</i>
<br># config <i>NEWKERN</i>
<p># cd ../../compile/<i>NEWKERN</i>
<br># make depend
<br># make
<br># make install
<br># reboot
<br>(old kernel is /kernel.old if you need it!)</blockquote>
<hr WIDTH="100%">
<h2>
<a NAME="Sysctl"></a>Sysctl</h2>
<i>Sysctl</i> is a command-line utility for inspecting and setting kernel
variables. There is also a sysctl API.
<p>sysctl <i>variable</i>
<br>sysctl -a
<br>sysctl -w <i>var=value
</i>note: you must be root to set a variable
<p>
<hr WIDTH="100%">
<br><a href="mailto:honig@sprynet.com">author</a>
</body>
</html>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3720E1A1.64266105>
