File: crm_uuid.c

package info (click to toggle)
pacemaker 1.0.9.1%2Bhg15626-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,668 kB
  • ctags: 5,645
  • sloc: xml: 87,444; ansic: 57,853; python: 11,479; sh: 5,255; makefile: 663; perl: 387; sed: 262
file content (185 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

/* 
 * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <config.h>
#include <crm_internal.h>

#include <sys/param.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

#include <clplumbing/lsb_exitcodes.h>
#include <clplumbing/cl_log.h>
#include <clplumbing/cl_uuid.h>

#ifdef HAVE_GETOPT_H
#  include <getopt.h>
#endif

#define UUID_LEN 16

#define OPTARGS	"?rw:"

int read_local_hb_uuid(void);
int write_local_hb_uuid(const char *buffer);

static void usage(int rc) 
{
	FILE *stream = rc != 0 ? stderr : stdout;
	fprintf(stream, "crm_uuid [-r|-w new_ascii_value]\n");
	exit(rc);
}

int
main(int argc, char **argv)
{
	int flag;
	int rc = 0;	

	if(argc == 1) {
		/* no arguments specified, default to read */
		rc = read_local_hb_uuid();	
		return rc;
	}
	
	cl_log_args(argc, argv);

	while (1) {
		flag = getopt(argc, argv, OPTARGS);
		if (flag == -1) {
			break;
		}
		switch(flag) {
			case 'r':
				rc = read_local_hb_uuid();	
				break;
			case 'w':
				rc = write_local_hb_uuid(optarg);
				break;
			case '?':
				usage(LSB_EXIT_OK);
				break;
			default:
				usage(LSB_EXIT_GENERIC);
				break;
		}
	}
	return rc;
}

int
read_local_hb_uuid(void) 
{
	int rc = 0;
	cl_uuid_t uuid;
	char *buffer = NULL;
	long start = 0, read_len = 0;

	FILE *input = fopen(UUID_FILE, "r");
	
	if(input == NULL) {
		cl_perror("Could not open UUID file %s\n", UUID_FILE);
		return 1;
	}
	
	/* see how big the file is */
	start  = ftell(input);
	fseek(input, 0L, SEEK_END);
	if(UUID_LEN != ftell(input)) {
		fprintf(stderr, "%s must contain exactly %d bytes\n",
			UUID_FILE, UUID_LEN);
		abort();
	}
	
	fseek(input, 0L, start);
	
	if(start != ftell(input)) {
		fprintf(stderr, "fseek not behaving: %ld vs. %ld\n",
			start, ftell(input));
		rc = 2;
		goto bail;
	}

/* 	fprintf(stderr, "Reading %d bytes from: %s\n", UUID_LEN, UUID_FILE); */

	buffer = malloc(50);
	read_len = fread(uuid.uuid, 1, UUID_LEN, input);
	if(read_len != UUID_LEN) {
		fprintf(stderr, "Expected and read bytes differ: %d vs. %ld\n",
			UUID_LEN, read_len);
		rc = 3;
		goto bail;
		
	} else if(buffer != NULL) {
		cl_uuid_unparse(&uuid, buffer);
		fprintf(stdout, "%s\n", buffer);

	} else {
		fprintf(stderr, "No buffer to unparse\n");
		rc = 4;
	}

  bail:	
	free(buffer);
	fclose(input);

	return rc;
}

int
write_local_hb_uuid(const char *new_value) 
{
	int fd;
	int rc = 0;
	cl_uuid_t uuid;
	char *buffer = strdup(new_value);
	rc = cl_uuid_parse(buffer, &uuid);
	if(rc != 0) {
		fprintf(stderr, "Invalid ASCII UUID supplied: [%s]\n", new_value);
		fprintf(stderr, "ASCII UUIDs must be of the form"
		" XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
		" and contain only letters and digits\n");
		return 5;
	}
	
	if ((fd = open(UUID_FILE, O_WRONLY|O_SYNC|O_CREAT, 0644)) < 0) {
		cl_perror("Could not open %s", UUID_FILE);
		return 6;
	}
	
	if (write(fd, uuid.uuid, UUID_LEN) != UUID_LEN) {
		cl_perror("Could not write UUID to %s", UUID_FILE);
		rc=7;
	}
	
	if (close(fd) < 0) {
		cl_perror("Could not close %s", UUID_FILE);
		rc=8;
	}
	return rc;
}