1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2017, Lawrence Livermore National Security, LLC.
23eda14cbcSMatt Macy  */
24eda14cbcSMatt Macy 
25eda14cbcSMatt Macy #include <fcntl.h>
26eda14cbcSMatt Macy #include <stdlib.h>
27eda14cbcSMatt Macy #include <stdio.h>
28eda14cbcSMatt Macy #include <sys/types.h>
29eda14cbcSMatt Macy #include <sys/stat.h>
30eda14cbcSMatt Macy #include <sys/systeminfo.h>
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy static unsigned long
get_spl_hostid(void)33eda14cbcSMatt Macy get_spl_hostid(void)
34eda14cbcSMatt Macy {
35eda14cbcSMatt Macy 	FILE *f;
36eda14cbcSMatt Macy 	unsigned long hostid;
37eda14cbcSMatt Macy 	char *env;
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy 	/*
40eda14cbcSMatt Macy 	 * Allow the hostid to be subverted for testing.
41eda14cbcSMatt Macy 	 */
42eda14cbcSMatt Macy 	env = getenv("ZFS_HOSTID");
4316038816SMartin Matuska 	if (env)
4416038816SMartin Matuska 		return (strtoull(env, NULL, 0));
45eda14cbcSMatt Macy 
4616038816SMartin Matuska 	f = fopen("/proc/sys/kernel/spl/hostid", "re");
47eda14cbcSMatt Macy 	if (!f)
48eda14cbcSMatt Macy 		return (0);
49eda14cbcSMatt Macy 
5016038816SMartin Matuska 	if (fscanf(f, "%lx", &hostid) != 1)
51eda14cbcSMatt Macy 		hostid = 0;
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy 	fclose(f);
54eda14cbcSMatt Macy 
5516038816SMartin Matuska 	return (hostid);
56eda14cbcSMatt Macy }
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy unsigned long
get_system_hostid(void)59eda14cbcSMatt Macy get_system_hostid(void)
60eda14cbcSMatt Macy {
6116038816SMartin Matuska 	unsigned long hostid = get_spl_hostid();
62*dbd5678dSMartin Matuska 	uint32_t system_hostid;
6316038816SMartin Matuska 
64eda14cbcSMatt Macy 	/*
6516038816SMartin Matuska 	 * We do not use gethostid(3) because it can return a bogus ID,
6616038816SMartin Matuska 	 * depending on the libc and /etc/hostid presence,
6716038816SMartin Matuska 	 * and the kernel and userspace must agree.
68eda14cbcSMatt Macy 	 * See comments above hostid_read() in the SPL.
69eda14cbcSMatt Macy 	 */
7016038816SMartin Matuska 	if (hostid == 0) {
7116038816SMartin Matuska 		int fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
72eda14cbcSMatt Macy 		if (fd >= 0) {
73*dbd5678dSMartin Matuska 			if (read(fd, &system_hostid, sizeof (system_hostid))
74*dbd5678dSMartin Matuska 			    != sizeof (system_hostid))
7516038816SMartin Matuska 				hostid = 0;
76*dbd5678dSMartin Matuska 			else
77*dbd5678dSMartin Matuska 				hostid = system_hostid;
7816038816SMartin Matuska 			(void) close(fd);
79eda14cbcSMatt Macy 		}
80eda14cbcSMatt Macy 	}
8116038816SMartin Matuska 
8216038816SMartin Matuska 	return (hostid & HOSTID_MASK);
83eda14cbcSMatt Macy }
84