Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / bucoord / tape_hosts.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include <afs/budb_client.h>
17 #include <afs/cmd.h>
18 #include <afs/com_err.h>
19 #include <afs/bubasics.h>
20
21 #include "bc.h"
22 #include "error_macros.h"
23 #include "bucoord_internal.h"
24 #include "bucoord_prototypes.h"
25
26 /* code to manage tape hosts
27 * specific to the ubik database implementation
28 */
29
30 extern struct bc_config *bc_globalConfig;
31 extern struct udbHandleS udbHandle;
32 extern char *whoami;
33
34 /* ------------------------------------
35 * command level routines
36 * ------------------------------------
37 */
38
39
40 /* bc_AddHostCmd
41 * Add a host to the tape hosts
42 */
43
44 int
45 bc_AddHostCmd(struct cmd_syndesc *as, void *arock)
46 {
47 struct cmd_item *ti;
48 udbClientTextP ctPtr;
49 afs_int32 port = 0;
50 afs_int32 code = 0;
51
52 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
53 code = bc_LockText(ctPtr);
54 if (code)
55 ERROR(code);
56
57 code = bc_UpdateHosts();
58 if (code) {
59 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
60 return (code);
61 }
62
63 /* add tape hosts first */
64 ti = as->parms[0].items;
65 if (ti) {
66 if (as->parms[1].items) {
67 port = getPortOffset(as->parms[1].items->data);
68 if (port < 0)
69 ERROR(BC_BADARG);
70 }
71 printf("Adding host %s offset %u to tape list...", ti->data, port);
72 fflush(stdout);
73 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
74 if (code) {
75 printf("failed\n");
76 fflush(stdout);
77 if (code == EEXIST)
78 afs_com_err(whoami, 0, "Port offset already in tape database");
79 ERROR(code);
80 }
81
82 code = bc_SaveHosts();
83 if (code) {
84 afs_com_err(whoami, code, "Cannot save tape hosts");
85 afs_com_err(whoami, 0,
86 "Changes are temporary - for this session only");
87 ERROR(code);
88 }
89 }
90
91 /* done */
92 printf("done\n");
93
94 error_exit:
95 if (ctPtr->lockHandle != 0)
96 bc_UnlockText(ctPtr);
97 return (code);
98 }
99
100 int
101 bc_DeleteHostCmd(struct cmd_syndesc *as, void *arock)
102 {
103 struct cmd_item *ti;
104 afs_int32 port = 0;
105 udbClientTextP ctPtr;
106 afs_int32 code = 0;
107
108 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
109 code = bc_LockText(ctPtr);
110 if (code)
111 ERROR(code);
112
113 code = bc_UpdateHosts();
114 if (code) {
115 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
116 return (code);
117 }
118
119 /* delete tape hosts first */
120 ti = as->parms[0].items;
121 if (ti) {
122 if (as->parms[1].items) {
123 port = bc_SafeATOI(as->parms[1].items->data);
124 if (port < 0)
125 return (BC_BADARG);
126 }
127
128 printf("Deleting host %s offset %u to tape list...", ti->data, port);
129 fflush(stdout);
130 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
131 if (code) {
132 if (code == ENOENT)
133 printf("failed: no such host entry\n");
134 else
135 printf("failed with code %d\n", code);
136 ERROR(code);
137 }
138
139 code = bc_SaveHosts();
140 if (code) {
141 afs_com_err(whoami, code, "Cannot save tape hosts");
142 afs_com_err(whoami, 0,
143 "Changes are temporary - for this session only");
144 ERROR(code);
145 }
146 }
147
148 /* done */
149 printf("done\n");
150 fflush(stdout);
151
152 error_exit:
153 if (ctPtr->lockHandle != 0)
154 bc_UnlockText(ctPtr);
155 return (code);
156 }
157
158
159 /* bc_ListHostsCmd
160 * list all tape hosts (from internally built tables)
161 * parameters:
162 * ignored
163 */
164
165 int
166 bc_ListHostsCmd(struct cmd_syndesc *as, void *arock)
167 {
168 afs_int32 code;
169 struct bc_hostEntry *tentry;
170
171 code = bc_UpdateHosts();
172 if (code) {
173 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
174 return (code);
175 }
176
177 printf("Tape hosts:\n");
178 for (tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next) {
179 printf(" Host %s, port offset %u\n", tentry->name,
180 tentry->portOffset);
181 }
182 fflush(stdout);
183 return 0;
184 }
185
186
187
188 /* ------------------------------------
189 * support routines
190 * ------------------------------------
191 */
192
193 int
194 bc_ClearHosts(void)
195 {
196 struct bc_hostEntry *tentry, *temp;
197
198 tentry = bc_globalConfig->tapeHosts;
199 while (tentry != 0) {
200 temp = tentry->next;
201 free(tentry->name);
202 free(tentry);
203 tentry = temp;
204 }
205
206 bc_globalConfig->tapeHosts = (struct bc_hostEntry *)0;
207 return (0);
208 }
209
210 /* bc_ParseHosts
211 * Open up the volume set configuration file as specified in our argument,
212 * then parse the file to set up our internal representation.
213 * exit:
214 * 0 on successful processing,
215 * -1 otherwise.
216 */
217
218 int
219 bc_ParseHosts(void)
220 {
221 char tbuffer[256];
222 char hostName[256];
223 afs_int32 port = 0;
224 struct bc_hostEntry *tfirst, *tlast, *the;
225 char *tp;
226 struct hostent *th;
227
228 udbClientTextP ctPtr;
229 FILE *stream;
230
231 /* initialize locally used variables */
232 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
233 stream = ctPtr->textStream;
234
235 if (ctPtr->textSize == 0) /* nothing defined yet */
236 return (0);
237
238 if (stream == NULL)
239 return (BC_INTERNALERROR);
240
241 rewind(stream);
242
243 /* now read the lines and build the structure list */
244 tfirst = tlast = (struct bc_hostEntry *)0;
245
246 while (1) {
247 tp = fgets(tbuffer, sizeof(tbuffer), stream);
248 if (!tp)
249 break; /* end of file */
250
251 sscanf(tbuffer, "%s %u", hostName, &port);
252 th = gethostbyname(hostName);
253 if (th == 0) {
254 afs_com_err(whoami, 0,
255 "can't get host info for %s from nameserver or /etc/hosts.",
256 hostName);
257 }
258 the = calloc(1, sizeof(struct bc_hostEntry));
259 if (the == (struct bc_hostEntry *)0)
260 return (BC_NOMEM);
261 if (tlast) {
262 tlast->next = the;
263 tlast = the;
264 } else {
265 tfirst = tlast = the;
266 }
267 the->next = (struct bc_hostEntry *)0;
268 the->name = strdup(hostName);
269 the->portOffset = port;
270 if (th) {
271 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
272 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
273 the->addr.sin_len = sizeof(struct sockaddr_in);
274 #endif
275 the->addr.sin_family = AF_INET;
276 the->addr.sin_port = 0;
277 }
278 }
279
280 bc_globalConfig->tapeHosts = tfirst;
281 return (0);
282 }
283
284 /* bc_SaveHosts
285 * really two parts
286 * 1) save the current host information to disk
287 * 2) transmit to ubik server
288 */
289
290 int
291 bc_SaveHosts(void)
292 {
293 afs_int32 code = 0;
294
295 udbClientTextP ctPtr;
296 FILE *stream;
297 struct bc_hostEntry *hePtr;
298
299 extern struct bc_config *bc_globalConfig;
300
301 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
302 stream = ctPtr->textStream;
303
304 /* must be locked */
305 if (ctPtr->lockHandle == 0)
306 return (BC_INTERNALERROR);
307
308 /* truncate the file */
309 code = ftruncate(fileno(stream), 0);
310 if (code)
311 ERROR(errno);
312
313 rewind(stream);
314
315 hePtr = bc_globalConfig->tapeHosts;
316
317 while (hePtr != 0) {
318 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
319 hePtr = hePtr->next;
320 }
321
322 if (ferror(stream))
323 return (BC_INTERNALERROR);
324
325 /* send to server */
326 code = bcdb_SaveTextFile(ctPtr);
327 if (code)
328 ERROR(code);
329
330 /* do this on bcdb_SaveTextFile */
331 /* increment local version number */
332 ctPtr->textVersion++;
333
334 /* update locally stored file size */
335 ctPtr->textSize = filesize(ctPtr->textStream);
336
337 error_exit:
338 return (code);
339 }
340
341 afs_int32
342 bc_UpdateHosts(void)
343 {
344 struct udbHandleS *uhptr = &udbHandle;
345 udbClientTextP ctPtr;
346 afs_int32 code;
347 int lock = 0;
348
349 /* lock schedules and check validity */
350 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
351
352 code = bc_CheckTextVersion(ctPtr);
353 if (code != BC_VERSIONMISMATCH) {
354 ERROR(code); /* Version matches or some other error */
355 }
356
357 /* Must update the hosts */
358 /* If we are not already locked, then lock it now */
359 if (ctPtr->lockHandle == 0) {
360 code = bc_LockText(ctPtr);
361 if (code)
362 ERROR(code);
363 lock = 1;
364 }
365
366 if (ctPtr->textVersion != -1) {
367 afs_com_err(whoami, 0, "obsolete tapehosts - updating");
368 bc_ClearHosts();
369 }
370
371 /* open a temp file to store the config text received from buserver *
372 * The open file stream is stored in ctPtr->textStream */
373 code =
374 bc_openTextFile(ctPtr,
375 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
376 if (code)
377 ERROR(code);
378 /* now get a fresh set of information from the database */
379 code = bcdb_GetTextFile(ctPtr);
380 if (code)
381 ERROR(code);
382
383 /* fetch the version number */
384 code =
385 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
386 &ctPtr->textVersion);
387 if (code)
388 ERROR(code);
389
390 /* parse the file */
391 code = bc_ParseHosts();
392 if (code)
393 ERROR(code);
394
395 error_exit:
396 if (lock && ctPtr->lockHandle)
397 bc_UnlockText(ctPtr);
398 return (code);
399 }