syscall() example

This commit is contained in:
Kolan Sh 2011-05-04 19:10:53 +04:00
parent a8bb01e4b5
commit e74c9a80ae
1 changed files with 63 additions and 0 deletions

63
c/sysctl_ex/_sysctl_ex.c Normal file
View File

@ -0,0 +1,63 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/sysctl.h>
#include <errno.h>
int _sysctl(struct __sysctl_args *args );
#define OSNAMESZ 100
void test0_osname()
{
struct __sysctl_args args;
char osname[OSNAMESZ];
size_t osnamelth;
int name[] = { CTL_KERN, KERN_OSTYPE };
memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name;
args.nlen = sizeof(name)/sizeof(name[0]);
args.oldval = osname;
args.oldlenp = &osnamelth;
osnamelth = sizeof(osname);
if (syscall(SYS__sysctl, &args) == -1) {
perror("_sysctl");
exit(EXIT_FAILURE);
}
printf("This machine is running %*s\n", (int)osnamelth, osname);
}
void test1_tcp_max_syn_backlog()
{
struct __sysctl_args args;
int var;
size_t var_len;
int name[] = { CTL_NET, NET_IPV4, NET_TCP_MAX_SYN_BACKLOG };
memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name;
args.nlen = sizeof(name)/sizeof(name[0]);
args.oldval = &var;
args.oldlenp = &var_len;
if (syscall(SYS__sysctl, &args) == -1) {
perror("_sysctl");
exit(EXIT_FAILURE);
}
printf("NET_TCP_MAX_SYN_BACKLOG = %u\n", var);
}
int
main(void)
{
test0_osname();
test1_tcp_max_syn_backlog();
exit(EXIT_SUCCESS);
}