man-pages/man2/sysctl.2.html
2021-03-31 01:06:50 +01:00

243 lines
7.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SYSCTL</TITLE>
</HEAD><BODY>
<H1>SYSCTL</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sysctl - read/write system parameters
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/linux/sysctl.h">linux/sysctl.h</A>&gt;</B>
<B>int _sysctl(struct __sysctl_args *</B><I>args</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>This system call no longer exists on current kernels!</B>
See NOTES.
<P>
The
<B>_sysctl</B>()
call reads and/or writes kernel parameters.
For example, the hostname,
or the maximum number of open files.
The argument has the form
<P>
struct __sysctl_args {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;*name;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;integer&nbsp;vector&nbsp;describing&nbsp;variable&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nlen;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;length&nbsp;of&nbsp;this&nbsp;vector&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;&nbsp;&nbsp;*oldval;&nbsp;&nbsp;/*&nbsp;0&nbsp;or&nbsp;address&nbsp;where&nbsp;to&nbsp;store&nbsp;old&nbsp;value&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;*oldlenp;&nbsp;/*&nbsp;available&nbsp;room&nbsp;for&nbsp;old&nbsp;value,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overwritten&nbsp;by&nbsp;actual&nbsp;size&nbsp;of&nbsp;old&nbsp;value&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;&nbsp;&nbsp;*newval;&nbsp;&nbsp;/*&nbsp;0&nbsp;or&nbsp;address&nbsp;of&nbsp;new&nbsp;value&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;&nbsp;newlen;&nbsp;&nbsp;/*&nbsp;size&nbsp;of&nbsp;new&nbsp;value&nbsp;*/
};
<P>
This call does a search in a tree structure, possibly resembling
a directory tree under
<I>/proc/sys</I>,
and if the requested item is found calls some appropriate routine
to read or modify the value.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
Upon successful completion,
<B>_sysctl</B>()
returns 0.
Otherwise, a value of -1 is returned and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EACCES</B>, <B>EPERM</B>
<DD>
No search permission for one of the encountered &quot;directories&quot;,
or no read permission where
<I>oldval</I>
was nonzero, or no write permission where
<I>newval</I>
was nonzero.
<DT id="2"><B>EFAULT</B>
<DD>
The invocation asked for the previous value by setting
<I>oldval</I>
non-NULL, but allowed zero room in
<I>oldlenp</I>.
<DT id="3"><B>ENOTDIR</B>
<DD>
<I>name</I>
was not found.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
This system call first appeared in Linux 1.3.57.
It was removed in Linux 5.5.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
This call is Linux-specific, and should not be used in programs
intended to be portable.
It originated in
4.4BSD.
Only Linux has the
<I>/proc/sys</I>
mirror, and the object naming schemes differ between Linux and 4.4BSD,
but the declaration of the
<B>sysctl</B>()
function is the same in both.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Use of this system call was long discouraged:
since Linux 2.6.24,
uses of this system call result in warnings in the kernel log,
and in Linux 5.5, the system call was finally removed.
Use the
<I>/proc/sys</I>
interface instead.
<P>
Note that on older kernels where this system call still exists,
it is available only if the kernel was configured with the
<B>CONFIG_SYSCTL_SYSCALL</B>
option.
Furthermore,
glibc does not provide a wrapper for this system call,
necessitating the use of
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
<P>
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
The object names vary between kernel versions,
making this system call worthless for applications.
<P>
Not all available objects are properly documented.
<P>
It is not yet possible to change operating system by writing to
<I>/proc/sys/kernel/ostype</I>.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/linux/sysctl.h">linux/sysctl.h</A>&gt;
<P>
int _sysctl(struct __sysctl_args *args );
<P>
#define OSNAMESZ 100
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;__sysctl_args&nbsp;args;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;osname[OSNAMESZ];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;osnamelth;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;name[]&nbsp;=&nbsp;{&nbsp;CTL_KERN,&nbsp;KERN_OSTYPE&nbsp;};
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(&amp;args,&nbsp;0,&nbsp;sizeof(struct&nbsp;__sysctl_args));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;args.name&nbsp;=&nbsp;name;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;args.nlen&nbsp;=&nbsp;sizeof(name)/sizeof(name[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;args.oldval&nbsp;=&nbsp;osname;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;args.oldlenp&nbsp;=&nbsp;&amp;osnamelth;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;osnamelth&nbsp;=&nbsp;sizeof(osname);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(syscall(SYS__sysctl,&nbsp;&amp;args)&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;_sysctl&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;This&nbsp;machine&nbsp;is&nbsp;running&nbsp;%*s\n&quot;,&nbsp;osnamelth,&nbsp;osname);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="8"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="9"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="10"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAI">NOTES</A><DD>
<DT id="12"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="13"><A HREF="#lbAK">EXAMPLE</A><DD>
<DT id="14"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:35 GMT, March 31, 2021
</BODY>
</HTML>