Add missing parenthesis in VERIFYF()
[zfs.git] / cmd / zgenhostid.c
blob04433b0d973c77f2ef96b2fb2ab065dedbd6f1c6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2020, Georgy Yakovlev. All rights reserved.
26 #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>
39 static __attribute__((noreturn)) void
40 usage(void)
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");
53 exit(EXIT_FAILURE);
56 int
57 main(int argc, char **argv)
59 /* default file path, can be optionally set by user */
60 const char *path = "/etc/hostid";
61 /* holds converted user input or lrand48() generated value */
62 unsigned long input_i = 0;
64 int opt;
65 int force_fwrite = 0;
66 while ((opt = getopt_long(argc, argv, "fo:h?", 0, 0)) != -1) {
67 switch (opt) {
68 case 'f':
69 force_fwrite = 1;
70 break;
71 case 'o':
72 path = optarg;
73 break;
74 case 'h':
75 case '?':
76 usage();
80 char *in_s = argv[optind];
81 if (in_s != NULL) {
82 /* increment pointer by 2 if string is 0x prefixed */
83 if (strncasecmp("0x", in_s, 2) == 0) {
84 in_s += 2;
87 /* need to be exactly 8 characters */
88 const char *hex = "0123456789abcdefABCDEF";
89 if (strlen(in_s) != 8 || strspn(in_s, hex) != 8) {
90 fprintf(stderr, "%s\n", strerror(ERANGE));
91 usage();
94 input_i = strtoul(in_s, NULL, 16);
95 if (errno != 0) {
96 perror("strtoul");
97 exit(EXIT_FAILURE);
100 if (input_i > UINT32_MAX) {
101 fprintf(stderr, "%s\n", strerror(ERANGE));
102 usage();
106 struct stat fstat;
107 if (force_fwrite == 0 && stat(path, &fstat) == 0 &&
108 S_ISREG(fstat.st_mode)) {
109 fprintf(stderr, "%s: %s\n", path, strerror(EEXIST));
110 exit(EXIT_FAILURE);
114 * generate if not provided by user
115 * also handle unlikely zero return from lrand48()
117 while (input_i == 0) {
118 srand48(getpid() ^ time(NULL));
119 input_i = lrand48();
122 FILE *fp = fopen(path, "wb");
123 if (!fp) {
124 perror("fopen");
125 exit(EXIT_FAILURE);
129 * we need just 4 bytes in native endianness
130 * not using sethostid() because it may be missing or just a stub
132 uint32_t hostid = input_i;
133 int written = fwrite(&hostid, 1, 4, fp);
134 if (written != 4) {
135 perror("fwrite");
136 exit(EXIT_FAILURE);
139 fclose(fp);
140 exit(EXIT_SUCCESS);