help sysctl.h

Vlad Galu dudu at diaspar.rdsnet.ro
Thu Feb 12 05:56:23 PST 2004


Nate Grey <nate at 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 at freebsd.org mailing list
|http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
|To unsubscribe, send any mail to
|"freebsd-hackers-unsubscribe at 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20040212/0502c06f/attachment.bin


More information about the freebsd-hackers mailing list