Path: blob/main/sys/contrib/openzfs/cmd/zgenhostid.c
48259 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2020, Georgy Yakovlev. All rights reserved.24*/2526#include <errno.h>27#include <fcntl.h>28#include <getopt.h>29#include <inttypes.h>30#include <limits.h>31#include <stdint.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <sys/stat.h>36#include <time.h>37#include <unistd.h>3839static __attribute__((noreturn)) void40usage(void)41{42(void) fprintf(stderr,43"usage: zgenhostid [-fh] [-o path] [value]\n\n"44" -f\t\t force hostid file write\n"45" -h\t\t print this usage and exit\n"46" -o <filename>\t write hostid to this file\n\n"47"If hostid file is not present, store a hostid in it.\n"48"The optional value should be an 8-digit hex number between"49" 1 and 2^32-1.\n"50"If the value is 0 or no value is provided, a random one"51" will be generated.\n"52"The value must be unique among your systems.\n");53exit(EXIT_FAILURE);54}5556int57main(int argc, char **argv)58{59/* default file path, can be optionally set by user */60const char *path = "/etc/hostid";61/* holds converted user input or lrand48() generated value */62unsigned long input_i = 0;6364int opt;65int force_fwrite = 0;66while ((opt = getopt_long(argc, argv, "fo:h?", 0, 0)) != -1) {67switch (opt) {68case 'f':69force_fwrite = 1;70break;71case 'o':72path = optarg;73break;74case 'h':75case '?':76usage();77}78}7980char *in_s = argv[optind];81if (in_s != NULL) {82/* increment pointer by 2 if string is 0x prefixed */83if (strncasecmp("0x", in_s, 2) == 0) {84in_s += 2;85}8687/* need to be exactly 8 characters */88const char *hex = "0123456789abcdefABCDEF";89if (strlen(in_s) != 8 || strspn(in_s, hex) != 8) {90fprintf(stderr, "%s\n", strerror(ERANGE));91usage();92}9394input_i = strtoul(in_s, NULL, 16);95if (errno != 0) {96perror("strtoul");97exit(EXIT_FAILURE);98}99100if (input_i > UINT32_MAX) {101fprintf(stderr, "%s\n", strerror(ERANGE));102usage();103}104}105106struct stat fstat;107if (force_fwrite == 0 && stat(path, &fstat) == 0 &&108S_ISREG(fstat.st_mode)) {109fprintf(stderr, "%s: %s\n", path, strerror(EEXIST));110exit(EXIT_FAILURE);111}112113/*114* generate if not provided by user115* also handle unlikely zero return from lrand48()116*/117while (input_i == 0) {118srand48(getpid() ^ time(NULL));119input_i = lrand48();120}121122FILE *fp = fopen(path, "wb");123if (!fp) {124perror("fopen");125exit(EXIT_FAILURE);126}127128/*129* we need just 4 bytes in native endianness130* not using sethostid() because it may be missing or just a stub131*/132uint32_t hostid = input_i;133int written = fwrite(&hostid, 1, 4, fp);134if (written != 4) {135perror("fwrite");136exit(EXIT_FAILURE);137}138139fclose(fp);140exit(EXIT_SUCCESS);141}142143144