2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <afs/budb_client.h>
18 #include <afs/com_err.h>
19 #include <afs/bubasics.h>
22 #include "error_macros.h"
23 #include "bucoord_internal.h"
24 #include "bucoord_prototypes.h"
26 /* code to manage tape hosts
27 * specific to the ubik database implementation
30 extern struct bc_config *bc_globalConfig;
31 extern struct udbHandleS udbHandle;
34 /* ------------------------------------
35 * command level routines
36 * ------------------------------------
41 * Add a host to the tape hosts
45 bc_AddHostCmd(struct cmd_syndesc *as, void *arock)
52 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
53 code = bc_LockText(ctPtr);
57 code = bc_UpdateHosts();
59 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
63 /* add tape hosts first */
64 ti = as->parms[0].items;
66 if (as->parms[1].items) {
67 port = getPortOffset(as->parms[1].items->data);
71 printf("Adding host %s offset %u to tape list...", ti->data, port);
73 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
78 afs_com_err(whoami, 0, "Port offset already in tape database");
82 code = bc_SaveHosts();
84 afs_com_err(whoami, code, "Cannot save tape hosts");
85 afs_com_err(whoami, 0,
86 "Changes are temporary - for this session only");
95 if (ctPtr->lockHandle != 0)
101 bc_DeleteHostCmd(struct cmd_syndesc *as, void *arock)
105 udbClientTextP ctPtr;
108 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
109 code = bc_LockText(ctPtr);
113 code = bc_UpdateHosts();
115 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
119 /* delete tape hosts first */
120 ti = as->parms[0].items;
122 if (as->parms[1].items) {
123 port = bc_SafeATOI(as->parms[1].items->data);
128 printf("Deleting host %s offset %u to tape list...", ti->data, port);
130 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
133 printf("failed: no such host entry\n");
135 printf("failed with code %d\n", code);
139 code = bc_SaveHosts();
141 afs_com_err(whoami, code, "Cannot save tape hosts");
142 afs_com_err(whoami, 0,
143 "Changes are temporary - for this session only");
153 if (ctPtr->lockHandle != 0)
154 bc_UnlockText(ctPtr);
160 * list all tape hosts (from internally built tables)
166 bc_ListHostsCmd(struct cmd_syndesc *as, void *arock)
169 struct bc_hostEntry *tentry;
171 code = bc_UpdateHosts();
173 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
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,
188 /* ------------------------------------
190 * ------------------------------------
196 struct bc_hostEntry *tentry, *temp;
198 tentry = bc_globalConfig->tapeHosts;
199 while (tentry != 0) {
206 bc_globalConfig->tapeHosts = (struct bc_hostEntry *)0;
211 * Open up the volume set configuration file as specified in our argument,
212 * then parse the file to set up our internal representation.
214 * 0 on successful processing,
224 struct bc_hostEntry *tfirst, *tlast, *the;
228 udbClientTextP ctPtr;
231 /* initialize locally used variables */
232 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
233 stream = ctPtr->textStream;
235 if (ctPtr->textSize == 0) /* nothing defined yet */
239 return (BC_INTERNALERROR);
243 /* now read the lines and build the structure list */
244 tfirst = tlast = (struct bc_hostEntry *)0;
247 tp = fgets(tbuffer, sizeof(tbuffer), stream);
249 break; /* end of file */
251 sscanf(tbuffer, "%s %u", hostName, &port);
252 th = gethostbyname(hostName);
254 afs_com_err(whoami, 0,
255 "can't get host info for %s from nameserver or /etc/hosts.",
258 the = calloc(1, sizeof(struct bc_hostEntry));
259 if (the == (struct bc_hostEntry *)0)
265 tfirst = tlast = the;
267 the->next = (struct bc_hostEntry *)0;
268 the->name = strdup(hostName);
269 the->portOffset = port;
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);
275 the->addr.sin_family = AF_INET;
276 the->addr.sin_port = 0;
280 bc_globalConfig->tapeHosts = tfirst;
286 * 1) save the current host information to disk
287 * 2) transmit to ubik server
295 udbClientTextP ctPtr;
297 struct bc_hostEntry *hePtr;
299 extern struct bc_config *bc_globalConfig;
301 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
302 stream = ctPtr->textStream;
305 if (ctPtr->lockHandle == 0)
306 return (BC_INTERNALERROR);
308 /* truncate the file */
309 code = ftruncate(fileno(stream), 0);
315 hePtr = bc_globalConfig->tapeHosts;
318 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
323 return (BC_INTERNALERROR);
326 code = bcdb_SaveTextFile(ctPtr);
330 /* do this on bcdb_SaveTextFile */
331 /* increment local version number */
332 ctPtr->textVersion++;
334 /* update locally stored file size */
335 ctPtr->textSize = filesize(ctPtr->textStream);
344 struct udbHandleS *uhptr = &udbHandle;
345 udbClientTextP ctPtr;
349 /* lock schedules and check validity */
350 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
352 code = bc_CheckTextVersion(ctPtr);
353 if (code != BC_VERSIONMISMATCH) {
354 ERROR(code); /* Version matches or some other error */
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);
366 if (ctPtr->textVersion != -1) {
367 afs_com_err(whoami, 0, "obsolete tapehosts - updating");
371 /* open a temp file to store the config text received from buserver *
372 * The open file stream is stored in ctPtr->textStream */
374 bc_openTextFile(ctPtr,
375 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
378 /* now get a fresh set of information from the database */
379 code = bcdb_GetTextFile(ctPtr);
383 /* fetch the version number */
385 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
386 &ctPtr->textVersion);
391 code = bc_ParseHosts();
396 if (lock && ctPtr->lockHandle)
397 bc_UnlockText(ctPtr);