xref: /freebsd/sys/contrib/openzfs/cmd/zgenhostid.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1716fd348SMartin Matuska /*
2716fd348SMartin Matuska  * CDDL HEADER START
3716fd348SMartin Matuska  *
4716fd348SMartin Matuska  * The contents of this file are subject to the terms of the
5716fd348SMartin Matuska  * Common Development and Distribution License (the "License").
6716fd348SMartin Matuska  * You may not use this file except in compliance with the License.
7716fd348SMartin Matuska  *
8716fd348SMartin Matuska  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10716fd348SMartin Matuska  * See the License for the specific language governing permissions
11716fd348SMartin Matuska  * and limitations under the License.
12716fd348SMartin Matuska  *
13716fd348SMartin Matuska  * When distributing Covered Code, include this CDDL HEADER in each
14716fd348SMartin Matuska  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15716fd348SMartin Matuska  * If applicable, add the following below this CDDL HEADER, with the
16716fd348SMartin Matuska  * fields enclosed by brackets "[]" replaced with your own identifying
17716fd348SMartin Matuska  * information: Portions Copyright [yyyy] [name of copyright owner]
18716fd348SMartin Matuska  *
19716fd348SMartin Matuska  * CDDL HEADER END
20716fd348SMartin Matuska  */
21716fd348SMartin Matuska 
22716fd348SMartin Matuska /*
23716fd348SMartin Matuska  * Copyright (c) 2020, Georgy Yakovlev.  All rights reserved.
24716fd348SMartin Matuska  */
25716fd348SMartin Matuska 
26716fd348SMartin Matuska #include <errno.h>
27716fd348SMartin Matuska #include <fcntl.h>
28716fd348SMartin Matuska #include <getopt.h>
29716fd348SMartin Matuska #include <inttypes.h>
30716fd348SMartin Matuska #include <limits.h>
31716fd348SMartin Matuska #include <stdint.h>
32716fd348SMartin Matuska #include <stdio.h>
33716fd348SMartin Matuska #include <stdlib.h>
34716fd348SMartin Matuska #include <string.h>
35716fd348SMartin Matuska #include <sys/stat.h>
36716fd348SMartin Matuska #include <time.h>
37716fd348SMartin Matuska #include <unistd.h>
38716fd348SMartin Matuska 
39716fd348SMartin Matuska static __attribute__((noreturn)) void
usage(void)40716fd348SMartin Matuska usage(void)
41716fd348SMartin Matuska {
42716fd348SMartin Matuska 	(void) fprintf(stderr,
43716fd348SMartin Matuska 	    "usage: zgenhostid [-fh] [-o path] [value]\n\n"
44716fd348SMartin Matuska 	    "  -f\t\t force hostid file write\n"
45716fd348SMartin Matuska 	    "  -h\t\t print this usage and exit\n"
46716fd348SMartin Matuska 	    "  -o <filename>\t write hostid to this file\n\n"
47716fd348SMartin Matuska 	    "If hostid file is not present, store a hostid in it.\n"
48716fd348SMartin Matuska 	    "The optional value should be an 8-digit hex number between"
49716fd348SMartin Matuska 	    " 1 and 2^32-1.\n"
50716fd348SMartin Matuska 	    "If the value is 0 or no value is provided, a random one"
51716fd348SMartin Matuska 	    " will be generated.\n"
52716fd348SMartin Matuska 	    "The value must be unique among your systems.\n");
53716fd348SMartin Matuska 	exit(EXIT_FAILURE);
54716fd348SMartin Matuska }
55716fd348SMartin Matuska 
56716fd348SMartin Matuska int
main(int argc,char ** argv)57716fd348SMartin Matuska main(int argc, char **argv)
58716fd348SMartin Matuska {
59716fd348SMartin Matuska 	/* default file path, can be optionally set by user */
60716fd348SMartin Matuska 	const char *path = "/etc/hostid";
61716fd348SMartin Matuska 	/* holds converted user input or lrand48() generated value */
62716fd348SMartin Matuska 	unsigned long input_i = 0;
63716fd348SMartin Matuska 
64716fd348SMartin Matuska 	int opt;
65716fd348SMartin Matuska 	int force_fwrite = 0;
66716fd348SMartin Matuska 	while ((opt = getopt_long(argc, argv, "fo:h?", 0, 0)) != -1) {
67716fd348SMartin Matuska 		switch (opt) {
68716fd348SMartin Matuska 		case 'f':
69716fd348SMartin Matuska 			force_fwrite = 1;
70716fd348SMartin Matuska 			break;
71716fd348SMartin Matuska 		case 'o':
72716fd348SMartin Matuska 			path = optarg;
73716fd348SMartin Matuska 			break;
74716fd348SMartin Matuska 		case 'h':
75716fd348SMartin Matuska 		case '?':
76716fd348SMartin Matuska 			usage();
77716fd348SMartin Matuska 		}
78716fd348SMartin Matuska 	}
79716fd348SMartin Matuska 
80716fd348SMartin Matuska 	char *in_s = argv[optind];
81716fd348SMartin Matuska 	if (in_s != NULL) {
82716fd348SMartin Matuska 		/* increment pointer by 2 if string is 0x prefixed */
83716fd348SMartin Matuska 		if (strncasecmp("0x", in_s, 2) == 0) {
84716fd348SMartin Matuska 			in_s += 2;
85716fd348SMartin Matuska 		}
86716fd348SMartin Matuska 
87716fd348SMartin Matuska 		/* need to be exactly 8 characters */
88716fd348SMartin Matuska 		const char *hex = "0123456789abcdefABCDEF";
89716fd348SMartin Matuska 		if (strlen(in_s) != 8 || strspn(in_s, hex) != 8) {
90716fd348SMartin Matuska 			fprintf(stderr, "%s\n", strerror(ERANGE));
91716fd348SMartin Matuska 			usage();
92716fd348SMartin Matuska 		}
93716fd348SMartin Matuska 
94716fd348SMartin Matuska 		input_i = strtoul(in_s, NULL, 16);
95716fd348SMartin Matuska 		if (errno != 0) {
96716fd348SMartin Matuska 			perror("strtoul");
97716fd348SMartin Matuska 			exit(EXIT_FAILURE);
98716fd348SMartin Matuska 		}
99716fd348SMartin Matuska 
100716fd348SMartin Matuska 		if (input_i > UINT32_MAX) {
101716fd348SMartin Matuska 			fprintf(stderr, "%s\n", strerror(ERANGE));
102716fd348SMartin Matuska 			usage();
103716fd348SMartin Matuska 		}
104716fd348SMartin Matuska 	}
105716fd348SMartin Matuska 
106716fd348SMartin Matuska 	struct stat fstat;
107716fd348SMartin Matuska 	if (force_fwrite == 0 && stat(path, &fstat) == 0 &&
108716fd348SMartin Matuska 	    S_ISREG(fstat.st_mode)) {
109716fd348SMartin Matuska 		fprintf(stderr, "%s: %s\n", path, strerror(EEXIST));
110716fd348SMartin Matuska 		exit(EXIT_FAILURE);
111716fd348SMartin Matuska 	}
112716fd348SMartin Matuska 
113716fd348SMartin Matuska 	/*
114716fd348SMartin Matuska 	 * generate if not provided by user
115716fd348SMartin Matuska 	 * also handle unlikely zero return from lrand48()
116716fd348SMartin Matuska 	 */
117716fd348SMartin Matuska 	while (input_i == 0) {
118716fd348SMartin Matuska 		srand48(getpid() ^ time(NULL));
119716fd348SMartin Matuska 		input_i = lrand48();
120716fd348SMartin Matuska 	}
121716fd348SMartin Matuska 
122716fd348SMartin Matuska 	FILE *fp = fopen(path, "wb");
123716fd348SMartin Matuska 	if (!fp) {
124716fd348SMartin Matuska 		perror("fopen");
125716fd348SMartin Matuska 		exit(EXIT_FAILURE);
126716fd348SMartin Matuska 	}
127716fd348SMartin Matuska 
128716fd348SMartin Matuska 	/*
129716fd348SMartin Matuska 	 * we need just 4 bytes in native endianness
130716fd348SMartin Matuska 	 * not using sethostid() because it may be missing or just a stub
131716fd348SMartin Matuska 	 */
132716fd348SMartin Matuska 	uint32_t hostid = input_i;
133716fd348SMartin Matuska 	int written = fwrite(&hostid, 1, 4, fp);
134716fd348SMartin Matuska 	if (written != 4) {
135716fd348SMartin Matuska 		perror("fwrite");
136716fd348SMartin Matuska 		exit(EXIT_FAILURE);
137716fd348SMartin Matuska 	}
138716fd348SMartin Matuska 
139716fd348SMartin Matuska 	fclose(fp);
140716fd348SMartin Matuska 	exit(EXIT_SUCCESS);
141716fd348SMartin Matuska }
142