应用层获取内核版本号
1. man 2 uname
$ man 2 uname
UNAME(2) Linux Programmer's Manual UNAME(2)
NAME
uname - get name and information about current kernel
SYNOPSIS
#include <sys/utsname.h>
int uname(struct utsname *buf);
DESCRIPTION
uname() returns system information in the structure pointed to by buf. The utsname struct is
defined in <sys/utsname.h>:
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
The length of the arrays in a struct utsname is unspecified (see NOTES); the fields are termi‐
nated by a null byte ('\0').
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
2. sample
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
int main(int argc, char **argv)
{
struct utsname kernelVersionBuffer;
//memset(&kernelVersionBuffer, 0, sizeof(kernelVersionBuffer));
int ret = uname(&kernelVersionBuffer);
if(ret < 0)
{
printf("errno:%d errMsg:%s\n", errno, strerror(errno));
return -1;
}
printf(" sysName:%s\n nodeName:%s\n release:%s\n version:%s\n machine:%s\n domainName:%s\n", \
kernelVersionBuffer.sysname,\
kernelVersionBuffer.nodename,\
kernelVersionBuffer.release,\
kernelVersionBuffer.version,\
kernelVersionBuffer.machine,\
kernelVersionBuffer.domainname);
return 0;
}
3. compile
$ gcc kernelVersion.c -o getKernelVersion
4. out
$ ./getKernelVersion
sysName:Linux
nodeName:ubuntu
release:4.15.0-142-generic
version:#146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021
machine:x86_64
domainName:(none)
$ uname -a
Linux ubuntu 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
标签:struct,版本号,char,uname,内核,release,include,kernelVersionBuffer,应用层
From: https://www.cnblogs.com/zhijun1996/p/17317775.html