Linux 编程中可以用API 获取主机的唯一标识,许可证管理者也会用此来保证相关程序只在拥有合法许可证的机器上运行。
#include <unistd.h>
long gethostid(void);
AME
gethostid, sethostid - get or set the unique identifier of the current host
SYNOPSIS
#include <unistd.h>
long gethostid(void);
int sethostid(long hostid);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sethostid(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
DESCRIPTION
gethostid() and sethostid() respectively get or set a unique 32-bit identifier for the current machine. The 32-bit identifier is intended to be
unique among all Unix systems in existence. This normally resembles the Internet address for the local machine, as returned by gethostby-
name(3), and thus usually never needs to be set.
The sethostid() call is restricted to the superuser.
RETURN VALUE
gethostid() returns the 32-bit identifier for the current host as set by sethostid().
On success, sethostid() returns 0; on error, -1 is returned, and errno is set to indicate the error.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int result;
if(-1==(result=gethostid()))
{
printf("gethostid err\n");
exit(0);
}
printf("hosid is :%d\n",result);
return 0;
}
运行结果:
dg@localhost My_project]$
[zdg@localhost My_project]$ ./getusid
hosid is :8323328
[zdg@localhost My_project]$