This source file includes following definitions.
- main
- read_local_hb_uuid
- write_local_hb_uuid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21 #include <crm_internal.h>
22
23 #include <sys/param.h>
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #ifdef HAVE_STRING_H
29 # include <string.h>
30 #endif
31
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <crm/crm.h>
37 #include <crm/common/util.h>
38 #include <clplumbing/cl_uuid.h>
39
40 #ifdef HAVE_GETOPT_H
41 # include <getopt.h>
42 #endif
43
44 #define UUID_LEN 16
45
46 #define OPTARGS "?rw:"
47
48 int read_local_hb_uuid(void);
49 int write_local_hb_uuid(const char *buffer);
50
51
52 static struct crm_option long_options[] = {
53
54 {"help", 0, 0, '?', "\tThis text"},
55 {"version", 0, 0, '$', "\t\tVersion information" },
56 {"read", 0, 0, 'r', "\tRead the machine's Heartbeat UUID" },
57 {"write", 1, 0, 'w', "\tChange the machine's Heartbeat UUID to a new value" },
58
59 {0, 0, 0, 0}
60 };
61
62
63 int
64 main(int argc, char **argv)
65 {
66 int flag;
67 int rc = 0;
68 int index = 0;
69
70 if (argc == 1) {
71
72 rc = read_local_hb_uuid();
73 return rc;
74 }
75
76 crm_log_cli_init("crm_uuid");
77 crm_set_options(NULL, " [-r|-w new_ascii_value]",
78 long_options, "A tool for manipulating Heartbeat's UUID file");
79
80 while (1) {
81 flag = crm_get_option(argc, argv, &index);
82 if (flag == -1)
83 break;
84
85 switch (flag) {
86 case '?':
87 case '$':
88 crm_help(flag, EX_OK);
89 break;
90 case 'r':
91 rc = read_local_hb_uuid();
92 break;
93 case 'w':
94 rc = write_local_hb_uuid(optarg);
95 break;
96 default:
97 crm_help('?', EX_USAGE);
98 break;
99 }
100 }
101 return rc;
102 }
103
104 int
105 read_local_hb_uuid(void)
106 {
107 int rc = 0;
108 cl_uuid_t uuid;
109 char *buffer = NULL;
110 long start = 0, read_len = 0;
111
112 FILE *input = fopen(UUID_FILE, "r");
113
114 if (input == NULL) {
115 crm_perror(LOG_ERR, "Could not open UUID file %s\n", UUID_FILE);
116 return 1;
117 }
118
119
120 start = ftell(input);
121 fseek(input, 0L, SEEK_END);
122 if (UUID_LEN != ftell(input)) {
123 fprintf(stderr, "%s must contain exactly %d bytes\n", UUID_FILE, UUID_LEN);
124 abort();
125 }
126
127 fseek(input, 0L, start);
128
129 if (start != ftell(input)) {
130 fprintf(stderr, "fseek not behaving: %ld vs. %ld\n", start, ftell(input));
131 rc = 2;
132 goto bail;
133 }
134
135
136
137 buffer = malloc(50);
138 read_len = fread(uuid.uuid, 1, UUID_LEN, input);
139 if (read_len != UUID_LEN) {
140 fprintf(stderr, "Expected and read bytes differ: %d vs. %ld\n", UUID_LEN, read_len);
141 rc = 3;
142 goto bail;
143
144 } else if (buffer != NULL) {
145 cl_uuid_unparse(&uuid, buffer);
146 fprintf(stdout, "%s\n", buffer);
147
148 } else {
149 fprintf(stderr, "No buffer to unparse\n");
150 rc = 4;
151 }
152
153 bail:
154 free(buffer);
155 fclose(input);
156
157 return rc;
158 }
159
160 int
161 write_local_hb_uuid(const char *new_value)
162 {
163 int fd;
164 int rc = 0;
165 cl_uuid_t uuid;
166 char *buffer = strdup(new_value);
167
168 rc = cl_uuid_parse(buffer, &uuid);
169 if (rc != 0) {
170 fprintf(stderr, "Invalid ASCII UUID supplied: [%s]\n", new_value);
171 fprintf(stderr, "ASCII UUIDs must be of the form"
172 " XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" " and contain only letters and digits\n");
173 return 5;
174 }
175
176 if ((fd = open(UUID_FILE, O_WRONLY | O_SYNC | O_CREAT, 0644)) < 0) {
177 crm_perror(LOG_ERR, "Could not open %s", UUID_FILE);
178 return 6;
179 }
180
181 if (write(fd, uuid.uuid, UUID_LEN) != UUID_LEN) {
182 crm_perror(LOG_ERR, "Could not write UUID to %s", UUID_FILE);
183 rc = 7;
184 }
185
186 if (close(fd) < 0) {
187 crm_perror(LOG_ERR, "Could not close %s", UUID_FILE);
188 rc = 8;
189 }
190 return rc;
191 }