uname()系统调用学习
uname()系统调用返回了一系列关于主机系统的标识信息。
#include <sys/utsname.h>
int uname(struct utsname *utsbuf);
Returns 0 on success, or -1 on error
#define _UTSNAME_LENGTH 65
struct utsname{
char sysname[_UTSNAME_LENGTH]; /*Implementation name*/
char nodename[_UTSNAME_LENGTH]; /*Node name on network*/
char release[_UTSNAME_LENGTH]; /*Implementation release level*/
char version[_UTSNAME_LENGTH]; /*Release version level*/
char machine[_UTSNAME_LENGTH]; /*Hardware on which system is running*/
char domainname[_UTSNAME_LENGTH]; /*NIS domain name of host*/
};
#include <stdio.h> #include <sys/utsname.h> int main() { struct utsname uts; if(uname(&uts) == -1) { fprintf(stderr, "uname.\n"); return -1; } printf("Node name: %s\n", uts.nodename); printf("system name: %s\n", uts.sysname); printf("release: %s\n", uts.release); printf("version: %s\n", uts.release); printf("machine: %s\n", uts.machine); printf("domainname: %s\n", uts.__domainname); return 0; }
输出:
Node name: hahaha
system name: Linux
release: 4.18.0-240.22.1.el8_3.x86_64
version: 4.18.0-240.22.1.el8_3.x86_64
machine: x86_64
domainname: (none)
查看了本机utsname.h头文件,发现里面有如下几行
_GNU_SOURCE
is the only one you should ever define yourself.
__USE_GNU
is defined internally through a mechanism in features.h
(which is included by all other glibc headers)
when _GNU_SOURCE
is defined, and possibly under other conditions.
Defining or undefining __USE_GNU
yourself will badly break the glibc headers.