侧边栏壁纸
博主头像
noerror

虚灵不寐,众理具而万事出。

  • 累计撰写 239 篇文章
  • 累计创建 9 个标签
  • 累计收到 2 条评论
标签搜索

目 录CONTENT

文章目录

gethostid函数用法详解

noerror
2022-11-09 / 0 评论 / 0 点赞 / 156 阅读 / 414 字 / 正在检测是否收录...

gethostid函数用法详解

gethostid函数简介

  • 头文件包含
#include <unistd.h>
  • 函数定义
long gethostid(void);
int sethostid(long  hostid );

gethostid函数常见使用错误

  • 编译错误
    warning: implicit declaration of function ‘gethostid’ [-Wimplicit-function-declaration]
    解决办法:包含头文件
#include <unistd.h>

gethostid函数详细描述

gethostid ()和sethostid ()分别获取或设置当前计算机的唯一32位标识符。32位标识符在现有的所有UNIX系统中是唯一的。这通常类似于由gethostbyname (3)返回的本地计算机的Internet地址,因此通常不需要设置。
sethostid ()调用仅限于超级用户。

gethostid函数返回值

gethostid ()返回由sethostid ()设置的当前主机的32位标识符
成功时,sethostid ()返回0;出错时,返回-1,并将errno设置为指示错误。

gethostid函数错误码

sethostid ()可能会失败,并出现以下错误:

  • EACCES 调用方没有权限写入用于存储主机ID的文件。
  • EPERM 调用进程的有效用户或组ID与其对应的真实ID不同。

gethostid函数其他说明

在glibc实现中,hostid存储在文件/etc/hostid 中(在glibc 2.2之前的版本中,使用的是文件/var/adm/hostid)
在glibc实现中,如果gethostid ()无法打开包含主机ID的文件,那么它将使用gethostname (2)获取主机名,并将该主机名传递给gethostbyname_r (3),以获取主机的IPv4地址,并返回通过比特旋转IPv4地址获得的值。(此值可能不唯一。)

0

评论区