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 9*271171e0SMartin 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 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 59eda14cbcSMatt Macy get_system_hostid(void) 60eda14cbcSMatt Macy { 6116038816SMartin Matuska unsigned long hostid = get_spl_hostid(); 6216038816SMartin Matuska 63eda14cbcSMatt Macy /* 6416038816SMartin Matuska * We do not use gethostid(3) because it can return a bogus ID, 6516038816SMartin Matuska * depending on the libc and /etc/hostid presence, 6616038816SMartin Matuska * and the kernel and userspace must agree. 67eda14cbcSMatt Macy * See comments above hostid_read() in the SPL. 68eda14cbcSMatt Macy */ 6916038816SMartin Matuska if (hostid == 0) { 7016038816SMartin Matuska int fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC); 71eda14cbcSMatt Macy if (fd >= 0) { 7216038816SMartin Matuska if (read(fd, &hostid, 4) < 0) 7316038816SMartin Matuska hostid = 0; 7416038816SMartin Matuska (void) close(fd); 75eda14cbcSMatt Macy } 76eda14cbcSMatt Macy } 7716038816SMartin Matuska 7816038816SMartin Matuska return (hostid & HOSTID_MASK); 79eda14cbcSMatt Macy } 80