Reputation: 453
I have to check Linux system information. I can execute system commands in C, but doing so I create a new process for every one, which is pretty expensive. I was wondering if there is a way to obtain system information without being forced to execute a shell command. I've been looking around for a while and I found nothing. Actually, I'm not even sure if it's more convenient to execute commands via Bash calling them from my C program or find a way to accomplish the tasks using only C.
Upvotes: 4
Views: 10945
Reputation: 531
You can also use the sys/utsname.h
header file to get the kernel version, hostname, operating system, machine hardware name, etc. More about sys/utsname.h
is here. This is an example of getting the current kernel release.
#include <stdio.h> // I/O
#include <sys/utsname.h>
int main(int argc, char const *argv[])
{
struct utsname buff;
printf("Kernel Release = %s\n", buff.release); // kernel release
return 0;
}
This is the same as using the uname
command. You can also use the -a
option which stands for all information.
uname -r # -r stands for kernel release
Upvotes: 0
Reputation: 9803
A presumably less known (and more complicated) way to do that, is that you can also use the api interface to sysctl
. To use it under Linux, you need to #include <unistd.h>
, #include <linux/sysctl.h>
. A code example of that is available in the man page:
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/sysctl.h>
int _sysctl(struct __sysctl_args *args );
#define OSNAMESZ 100
int
main(void)
{
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", osnamelth, osname);
exit(EXIT_SUCCESS);
}
However, the man page linked also notes:
Glibc does not provide a wrapper for this system call; call it using syscall(2). Or rather... don't call it: use of this system call has long been discouraged, and it is so unloved that it is likely to disappear in a future kernel version. Since Linux 2.6.24, uses of this system call result in warnings in the kernel log. Remove it from your programs now; use the /proc/sys interface instead.
This system call is available only if the kernel was configured with the CONFIG_SYSCTL_SYSCALL option.
Please keep in mind that anything you can do with sysctl()
, you can also just read()
from /proc/sys
. Also note that I do understand that the usefulness of that syscall is questionable, I just put it here for reference.
Upvotes: 3
Reputation: 29985
Linux exposes a lot of information under /proc
. You can read the data from there. For example, fopen
the file at /proc/cpuinfo
and read its contents.
Upvotes: 7