Sysctl function __ Function

Source: Internet
Author: User

Regarding the function of Sysctl, the data on the net is relatively few, I tidy up some everybody study together.

1.sysctl related functions

Sysctl,sysctlname,sysctlnametomib, the function of these three functions is to get or set the function of the system information.

Note: For the use of the SYSCTL command and configuration file methods on the Internet is easier to find, we solve.

2. The Library

Standard C library functions

3. Summary

#include <sys/types.h>

#include <sys/sysctl.h>

int sysctl (const int *name,u_int namelen,void *oldp,size_t *oldlenp,const void *newp,size_t);

int sysctlbyname (const char *name, void *OLDP, size_t *oldlenp,
const void *NEWP, size_t newlen);

Int
Sysctlnametomib (const char *name, int *mibp, size_t *sizep);

4. Describe

The SYSCTL function retrieves system information and allows the appropriate process to set system information. sysctl function can get integer, string and table information (this sentence is not in line with the Chinese taste ah, this is the landlord brewing a half-day translation out of the feeling should be similar to the sysctl-a information). The information can be reset by sysctl the command line excuse.

The SYSCTL function returns a consistent snapshot based on the needs of the data. Consistency includes being locked into the destination buffer memory so that the data is copied out without the need for a lock, and the call function Sysctl to avoid deadlock.

This rule is described in the form of a MIB name, listing name, and the length of this name is the parameter Namelen.

The Sysctlbyname function accepts ASCII as a form of name and number name. Moreover, it is strict with the standard sysctl function.

The information is copied into the buffer via the parameter OLDP. The length of the buffer is specified by the previous position, and the location gives a large number of copies of the data after a successful call and after the call returns the error code. If that large amount of available data is greater than the size provided by that buffer, this call provides the most appropriate data and returns the error code, and OLDP and OLDLENP are set to null if the old data is not desired.

Call a function by setting the parameter OLDP to null sysctl the size of the data can be absolutely available. The available data will be returned to the location pointed to by the OLDLENP. For some operations, a lot of space will change frequently. For these operations, the system is pointing around so much that the size returned is large enough to be the returned data.

In order to set a new value, NEWP, which is set to point to a buffernewlen length, is set by the desired value. If a new value is not set, the NEWP should be set to null and Newlen set to 0.

The Sysctlnametomib () function accepts an ASCII representation of the name, the name looks for the integer vector, and returns the value of the FIN definition MIBP the MIB array indicated. The number of elements of the MIB array is called by the Sizep at the specified location, and this position gives the number of copies of the item cessful call. This produces the name of the ASCII that can be used for subsequent MIB and size sysctl () calls to get data associated with the request. The purpose of this interface is to reuse the application ed request the same variable (sysctl () function run in a third time with the same request via the Sysctlbyname () function). The Sysctlnametomib () function also applies to crawling the MIB prefix and then adding the last component. Limited--for example, get process and PID less than 100

Example:

int i, mib[4];
size_t Len;
struct KINFO_PROC kp;

/* Fill out the three components of the MIB * *
len = 4;
Sysctlnametomib ("Kern.proc.pid", MiB, &len);

/* Fetch and print entries for PID ' s < 100 * *
for (i = 0; i < i++) {
MIB[3] = i;
len = sizeof (KP);
if (Sysctl (MIB, 4, &AMP;KP, &len, NULL, 0) = = 1)
Perror ("Sysctl");
else if (len > 0)
Printkproc (&AMP;KP);
}

So much for the time being, the rest of the debug stuff.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.