linux/libspl: gethostid: read from /proc/sys/kernel/spl/hostid, simplify

Fixes get_system_hostid() if it was set via the aforementioned sysctl
and simplifies the code a bit.  The kernel and user-space must agree,
after all.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11879
This commit is contained in:
наб 2021-04-11 01:51:04 +02:00 committed by Brian Behlendorf
parent e5c4f86e7a
commit 7be4669a65
1 changed files with 17 additions and 24 deletions

View File

@ -40,47 +40,40 @@ get_spl_hostid(void)
* Allow the hostid to be subverted for testing. * Allow the hostid to be subverted for testing.
*/ */
env = getenv("ZFS_HOSTID"); env = getenv("ZFS_HOSTID");
if (env) { if (env)
hostid = strtoull(env, NULL, 0); return (strtoull(env, NULL, 0));
return (hostid & HOSTID_MASK);
}
f = fopen("/sys/module/spl/parameters/spl_hostid", "re"); f = fopen("/proc/sys/kernel/spl/hostid", "re");
if (!f) if (!f)
return (0); return (0);
if (fscanf(f, "%lu", &hostid) != 1) if (fscanf(f, "%lx", &hostid) != 1)
hostid = 0; hostid = 0;
fclose(f); fclose(f);
return (hostid & HOSTID_MASK); return (hostid);
} }
unsigned long unsigned long
get_system_hostid(void) get_system_hostid(void)
{ {
unsigned long system_hostid = get_spl_hostid(); unsigned long hostid = get_spl_hostid();
/* /*
* We do not use the library call gethostid() because * We do not use gethostid(3) because it can return a bogus ID,
* it generates a hostid value that the kernel is * depending on the libc and /etc/hostid presence,
* unaware of, if the spl_hostid module parameter has not * and the kernel and userspace must agree.
* been set and there is no system hostid file (e.g.
* /etc/hostid). The kernel and userspace must agree.
* See comments above hostid_read() in the SPL. * See comments above hostid_read() in the SPL.
*/ */
if (system_hostid == 0) { if (hostid == 0) {
int fd, rc; int fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
unsigned long hostid;
int hostid_size = 4; /* 4 bytes regardless of arch */
fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
if (fd >= 0) { if (fd >= 0) {
rc = read(fd, &hostid, hostid_size); if (read(fd, &hostid, 4) < 0)
if (rc > 0) hostid = 0;
system_hostid = (hostid & HOSTID_MASK); (void) close(fd);
close(fd);
} }
} }
return (system_hostid);
return (hostid & HOSTID_MASK);
} }