简体   繁体   English

在 C 中获取 Linux 系统信息

[英]Get Linux system information in C

I have to check Linux system information.我必须检查Linux系统信息。 I can execute system commands in C, but doing so I create a new process for every one, which is pretty expensive.我可以在 C 中执行系统命令,但是这样做我为每个人创建了一个新进程,这非常昂贵。 I was wondering if there is a way to obtain system information without being forced to execute a shell command.我想知道是否有一种方法可以在不强制执行 shell 命令的情况下获取系统信息。 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.实际上,我什至不确定通过 Bash 从我的 C 程序中调用它们来执行命令还是找到一种仅使用 C 来完成任务的方法更方便。

Linux exposes a lot of information under /proc . Linux 在/proc下暴露了很多信息。 You can read the data from there.您可以从那里读取数据。 For example, fopen the file at /proc/cpuinfo and read its contents.例如, fopen /proc/cpuinfo的文件并读取其内容。

A presumably less known ( and more complicated ) way to do that, is that you can also use the api interface to sysctl .一种可能鲜为人知(且更复杂)的方法是,您还可以使用sysctl的 api 接口。 To use it under Linux, you need to #include <unistd.h> , #include <linux/sysctl.h> .要在 Linux 下使用它,您需要#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; Glibc 没有为这个系统调用提供包装器; call it using syscall(2).使用 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.自 Linux 2.6.24 起,使用此系统调用会导致内核日志中出现警告。 Remove it from your programs now;立即将其从您的程序中删除; use the /proc/sys interface instead.改用 /proc/sys 接口。

This system call is available only if the kernel was configured with the CONFIG_SYSCTL_SYSCALL option.仅当内核配置了 CONFIG_SYSCTL_SYSCALL 选项时,此系统调用才可用。

Please keep in mind that anything you can do with sysctl() , you can also just read() from /proc/sys .请记住,你可以用sysctl()做任何事情,你也可以从/proc/sys read() Also note that I do understand that the usefulness of that syscall is questionable , I just put it here for reference .另请注意,我确实理解该系统调用的用处值得怀疑我只是将其放在这里以供参考

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 .您还可以使用sys/utsname.h头文件来获取内核版本、主机名、操作系统、机器硬件名称等。有关sys/utsname.h更多信息在这里 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.这与使用uname命令相同。 You can also use the -a option which stands for all information.您还可以使用代表所有信息的-a选项。

uname -r # -r stands for kernel release

no working!!!!!!!!!不工作!!!!!!!!! Test before give a answer !!!!!!!!!!!!!!!在给出答案之前测试!!!!!!!!!!!!!!! it's a very good practice!!!!!!!!!!这是一个非常好的做法!!!!!!!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM