Skip to content

Commit 41fa7c3

Browse files
linux/libspl: gethostid: read from /proc/sys/kernel/spl/hostid, simplify
The kernel and user-space must agree, after all Signed-off-by: Ahelenia Ziemiańska <[email protected]>
1 parent 45c2446 commit 41fa7c3

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

lib/libspl/os/linux/gethostid.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,40 @@ get_spl_hostid(void)
4040
* Allow the hostid to be subverted for testing.
4141
*/
4242
env = getenv("ZFS_HOSTID");
43-
if (env) {
44-
hostid = strtoull(env, NULL, 0);
45-
return (hostid & HOSTID_MASK);
46-
}
43+
if (env)
44+
return (strtoull(env, NULL, 0));
4745

48-
f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
46+
f = fopen("/proc/sys/kernel/spl/hostid", "r");
4947
if (!f)
5048
return (0);
5149

52-
if (fscanf(f, "%lu", &hostid) != 1)
50+
if (fscanf(f, "%lx", &hostid) != 1)
5351
hostid = 0;
5452

5553
fclose(f);
5654

57-
return (hostid & HOSTID_MASK);
55+
return (hostid);
5856
}
5957

6058
unsigned long
6159
get_system_hostid(void)
6260
{
63-
unsigned long system_hostid = get_spl_hostid();
61+
unsigned long hostid = get_spl_hostid();
62+
6463
/*
65-
* We do not use the library call gethostid() because
66-
* it generates a hostid value that the kernel is
67-
* unaware of, if the spl_hostid module parameter has not
68-
* been set and there is no system hostid file (e.g.
69-
* /etc/hostid). The kernel and userspace must agree.
64+
* We do not use gethostid(3) because it can return a bogus ID,
65+
* depending on the libc and /etc/hostid presence,
66+
* and the kernel and userspace must agree.
7067
* See comments above hostid_read() in the SPL.
7168
*/
72-
if (system_hostid == 0) {
73-
int fd, rc;
74-
unsigned long hostid;
75-
int hostid_size = 4; /* 4 bytes regardless of arch */
76-
77-
fd = open("/etc/hostid", O_RDONLY);
69+
if (hostid == 0) {
70+
int fd = open("/etc/hostid", O_RDONLY);
7871
if (fd >= 0) {
79-
rc = read(fd, &hostid, hostid_size);
80-
if (rc > 0)
81-
system_hostid = (hostid & HOSTID_MASK);
82-
close(fd);
72+
if (read(fd, &hostid, 4) < 0)
73+
hostid = 0;
74+
(void) close(fd);
8375
}
8476
}
85-
return (system_hostid);
77+
78+
return (hostid & HOSTID_MASK);
8679
}

0 commit comments

Comments
 (0)