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 = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry));
259 if (the == (struct bc_hostEntry *)0)
261 memset(the, 0, sizeof(struct bc_hostEntry));
266 tfirst = tlast = the;
268 the->next = (struct bc_hostEntry *)0;
269 the->name = (char *)malloc(strlen(hostName) + 1);
270 strcpy(the->name, hostName);
271 the->portOffset = port;
273 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
274 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
275 the->addr.sin_len = sizeof(struct sockaddr_in);
277 the->addr.sin_family = AF_INET;
278 the->addr.sin_port = 0;
282 bc_globalConfig->tapeHosts = tfirst;
288 * 1) save the current host information to disk
289 * 2) transmit to ubik server
297 udbClientTextP ctPtr;
299 struct bc_hostEntry *hePtr;
301 extern struct bc_config *bc_globalConfig;
303 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
304 stream = ctPtr->textStream;
307 if (ctPtr->lockHandle == 0)
308 return (BC_INTERNALERROR);
310 /* truncate the file */
311 code = ftruncate(fileno(stream), 0);
317 hePtr = bc_globalConfig->tapeHosts;
320 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
325 return (BC_INTERNALERROR);
328 code = bcdb_SaveTextFile(ctPtr);
332 /* do this on bcdb_SaveTextFile */
333 /* increment local version number */
334 ctPtr->textVersion++;
336 /* update locally stored file size */
337 ctPtr->textSize = filesize(ctPtr->textStream);
346 struct udbHandleS *uhptr = &udbHandle;
347 udbClientTextP ctPtr;
351 /* lock schedules and check validity */
352 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
354 code = bc_CheckTextVersion(ctPtr);
355 if (code != BC_VERSIONMISMATCH) {
356 ERROR(code); /* Version matches or some other error */
359 /* Must update the hosts */
360 /* If we are not already locked, then lock it now */
361 if (ctPtr->lockHandle == 0) {
362 code = bc_LockText(ctPtr);
368 if (ctPtr->textVersion != -1) {
369 afs_com_err(whoami, 0, "obsolete tapehosts - updating");
373 /* open a temp file to store the config text received from buserver *
374 * The open file stream is stored in ctPtr->textStream */
376 bc_openTextFile(ctPtr,
377 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
380 /* now get a fresh set of information from the database */
381 code = bcdb_GetTextFile(ctPtr);
385 /* fetch the version number */
387 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
388 &ctPtr->textVersion);
393 code = bc_ParseHosts();
398 if (lock && ctPtr->lockHandle)
399 bc_UnlockText(ctPtr);