Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Feb 2004 15:56:15 +0200
From:      Vlad Galu <dudu@diaspar.rdsnet.ro>
To:        freebsd-hackers@freebsd.org
Subject:   Re: help sysctl.h
Message-ID:  <20040212155615.6124eacb.dudu@diaspar.rdsnet.ro>
In-Reply-To: <20040124165621.GA19451@LapBSD.tin.it>
References:  <20040124165621.GA19451@LapBSD.tin.it>

next in thread | previous in thread | raw e-mail | index | archive | help
--Signature=_Thu__12_Feb_2004_15_56_15_+0200_2YDVQLeawO=xG1AD
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Nate Grey <nate@paranoici.org> writes:

|Hello,
|
|I'm trying to write a little program which retrieve the value 'sysctl
|hw.acpi.thermal.tz0.temperature', I want to write it in C, through
|sys/sysctl.h, but I'm a newbie C coder, so can anyone show me how to
|assign to a var the value stored in that sysctl using sysctl C call?
|I have read 'man 3 sysctl' but I didn't understand very well...
|
|P.S.
|1) Sorry for my English
|2) I'm not a list subscriber so please cc:
|
	This is simple. Let's try a read example first:

-- cut here --
#include <stdio.h>
#include <sys/types.h>
#include <sysctl.h>

int main() {
 int ret; /* here we will place our result */
 int size = sizeof(ret); /* this is the storage size of the variable we
put our result into */

 sysctlbyname("net.inet.ip.forwarding", (void *)&ret, &size, NULL,
NULL);
 
 return ret;
}
-- and here --
	As you can see, after the pointers to ret and size, we had two NULL
pointers. This means we didn't want to write anything to the sysctl.
	
	Let's see how to write the sysctl variable:

-- cut here --
#include <stdio.h>
#include <sys/types.h>
#include <sysctl.h>

int main() {
 int ret = 1;
 int size = sizeof(ret);

 sysctlbyname("net.inet.ip.forwarding", NULL, NULL, (void *)&ret, size);
 
 return ret;
}
-- and here --

	Now execute this proggie as root. After that, issue a 'sysctl
net.inet.ip.forwarding' from the shell. You'll see that the sysctl value
has changed.

	Hope this helps. I tried to give the simplest example possible.

|Thanks in advance. Bye
|
|_______________________________________________
|freebsd-hackers@freebsd.org mailing list
|http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
|To unsubscribe, send any mail to
|"freebsd-hackers-unsubscribe@freebsd.org"
|
|
|!DSPAM:402b8013604934185311595!
|
|
|


----
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

--Signature=_Thu__12_Feb_2004_15_56_15_+0200_2YDVQLeawO=xG1AD
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)

iD8DBQFAK4YBP5WtpVOrzpcRArcRAJ9f73t0klAdlhOT9kxloXoMaBLlsQCfU0+1
YGJGlkEoSwkxvpkzFe9cb7U=
=FJO1
-----END PGP SIGNATURE-----

--Signature=_Thu__12_Feb_2004_15_56_15_+0200_2YDVQLeawO=xG1AD--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040212155615.6124eacb.dudu>