2017-08-10 01:31:08 +03:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-12 00:16:13 +03:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2017-08-10 01:31:08 +03:00
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2017, Lawrence Livermore National Security, LLC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/systeminfo.h>
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
get_spl_hostid(void)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
unsigned long hostid;
|
|
|
|
char *env;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow the hostid to be subverted for testing.
|
|
|
|
*/
|
|
|
|
env = getenv("ZFS_HOSTID");
|
2021-04-11 02:51:04 +03:00
|
|
|
if (env)
|
|
|
|
return (strtoull(env, NULL, 0));
|
2017-08-10 01:31:08 +03:00
|
|
|
|
2021-04-11 02:51:04 +03:00
|
|
|
f = fopen("/proc/sys/kernel/spl/hostid", "re");
|
2017-08-10 01:31:08 +03:00
|
|
|
if (!f)
|
|
|
|
return (0);
|
|
|
|
|
2021-04-11 02:51:04 +03:00
|
|
|
if (fscanf(f, "%lx", &hostid) != 1)
|
2017-08-10 01:31:08 +03:00
|
|
|
hostid = 0;
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
2021-04-11 02:51:04 +03:00
|
|
|
return (hostid);
|
2017-08-10 01:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
get_system_hostid(void)
|
|
|
|
{
|
2021-04-11 02:51:04 +03:00
|
|
|
unsigned long hostid = get_spl_hostid();
|
2022-10-21 00:52:35 +03:00
|
|
|
uint32_t system_hostid;
|
2021-04-11 02:51:04 +03:00
|
|
|
|
2017-08-10 01:31:08 +03:00
|
|
|
/*
|
2021-04-11 02:51:04 +03:00
|
|
|
* We do not use gethostid(3) because it can return a bogus ID,
|
|
|
|
* depending on the libc and /etc/hostid presence,
|
|
|
|
* and the kernel and userspace must agree.
|
2017-08-10 01:31:08 +03:00
|
|
|
* See comments above hostid_read() in the SPL.
|
|
|
|
*/
|
2021-04-11 02:51:04 +03:00
|
|
|
if (hostid == 0) {
|
|
|
|
int fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
|
2017-08-10 01:31:08 +03:00
|
|
|
if (fd >= 0) {
|
2022-10-21 00:52:35 +03:00
|
|
|
if (read(fd, &system_hostid, sizeof (system_hostid))
|
|
|
|
!= sizeof (system_hostid))
|
2021-04-11 02:51:04 +03:00
|
|
|
hostid = 0;
|
2022-10-21 00:52:35 +03:00
|
|
|
else
|
|
|
|
hostid = system_hostid;
|
2021-04-11 02:51:04 +03:00
|
|
|
(void) close(fd);
|
2017-08-10 01:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 02:51:04 +03:00
|
|
|
|
|
|
|
return (hostid & HOSTID_MASK);
|
2017-08-10 01:31:08 +03:00
|
|
|
}
|