Skip site navigation (1)Skip section navigation (2)
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
This is a multi-part message in MIME format.
--------------C7DF50165012C069796EFA4F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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

--------------C7DF50165012C069796EFA4F
Content-Type: text/html; charset=us-ascii;
 name="sysctl.html"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="sysctl.html"

<!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&nbsp; <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.&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Adding a <a href="#New Root Parent Node">Root Node&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#Sysctl">sysctl</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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.&nbsp; I will choose
<i>/usr/src/sys/i386/isa/random_machdep.c.</i>
<p>In that file, add the following:
<blockquote><b>#include &lt;sys/kernel.h></b>
<br><b>#include &lt;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, &amp;my_var,
0, "");</blockquote>
Note that there are other "parent" nodes besides <b>_net</b>.&nbsp; You
can <a href="#New Root Parent Node">make your own</a>.
<p>Note that there are other types of variables:&nbsp; 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.&nbsp;
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&nbsp;<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>&nbsp;* Top-level identifiers</font></tt>
<br><tt><font size=-2>&nbsp;*/</font></tt>
<br><tt><font size=-2>#define CTL_UNSPEC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* unused */</font></tt>
<br><tt><font size=-2>#define CTL_KERN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* "high kernel": proc, limits */</font></tt>
<br><tt><font size=-2>#define CTL_VM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* virtual memory */</font></tt>
<br><tt><font size=-2>#define CTL_VFS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* file system, mount type is next */</font></tt>
<br><tt><font size=-2>#define CTL_NET&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* network, see socket.h */</font></tt>
<br><tt><font size=-2>#define CTL_DEBUG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* debugging parameters */</font></tt>
<br><tt><font size=-2>#define CTL_HW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* generic cpu/io */</font></tt>
<br><tt><font size=-2>#define CTL_MACHDEP&nbsp;&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* machine dependent */</font></tt>
<br><tt><font size=-2>#define CTL_USER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* user-level */</font></tt>
<br><tt><font size=-2>#define CTL_P1003_1B&nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* POSIX 1003.1B */</font></tt>
<br><b><tt><font size=-1>#define CTL_EXPER&nbsp; 10&nbsp;&nbsp;&nbsp;&nbsp;
/* DAH EXPERIMENTAL */</font></tt></b>
<br><tt><font size=-2>#define CTL_MAXID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><b><font size=-1>
11&nbsp;</font></b><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { 0, 0
}, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "kern",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "vm",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "vfs",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "net",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "debug",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "hw",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "machdep",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "user",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { "p1003_1b",
CTLTYPE_NODE }, \</font></tt>
<br><tt><font size=-2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><b><font size=-1>{
"exper", CTLTYPE_NODE }, \</font></b></tt>
<br><tt><font size=-2>}</font></tt>
<br>&nbsp;
<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>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cp CURRENT <i>NEWKERN</i>
<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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.&nbsp; There is also a sysctl API.
<p>sysctl <i>variable</i>
<br>sysctl -a
<br>sysctl -w <i>var=value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

</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>

--------------C7DF50165012C069796EFA4F--



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-newbies" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3720E1A1.64266105>