Skip to content

Commit 00384ce

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 d43b823 commit 00384ce

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

lib/libspl/os/linux/gethostid.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,45 +42,39 @@ get_spl_hostid(void)
4242
env = getenv("ZFS_HOSTID");
4343
if (env) {
4444
hostid = strtoull(env, NULL, 0);
45-
return (hostid & HOSTID_MASK);
45+
return (hostid);
4646
}
4747

48-
f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
48+
f = fopen("/proc/sys/kernel/spl/hostid", "r");
4949
if (!f)
5050
return (0);
5151

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

5555
fclose(f);
5656

57-
return (hostid & HOSTID_MASK);
57+
return (hostid);
5858
}
5959

6060
unsigned long
6161
get_system_hostid(void)
6262
{
63-
unsigned long system_hostid = get_spl_hostid();
63+
unsigned long hostid = get_spl_hostid();
64+
6465
/*
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.
66+
* We do not use gethostid(3) because it can return a bogus ID,
67+
* depending on the libc and /etc/hostid presence,
68+
* and the kernel and userspace must agree.
7069
* See comments above hostid_read() in the SPL.
7170
*/
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);
71+
if (hostid == 0) {
72+
int fd = open("/etc/hostid", O_RDONLY);
7873
if (fd >= 0) {
79-
rc = read(fd, &hostid, hostid_size);
80-
if (rc > 0)
81-
system_hostid = (hostid & HOSTID_MASK);
82-
close(fd);
74+
int _ = read(fd, &hostid, 4);
75+
(void) _, close(fd);
8376
}
8477
}
85-
return (system_hostid);
78+
79+
return (hostid & HOSTID_MASK);
8680
}

0 commit comments

Comments
 (0)