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 <afs/param.h>
11 #include <afsconfig.h>
16 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
25 #include <afs/budb_client.h>
27 #include <afs/com_err.h>
28 #include <afs/bubasics.h>
30 #include "error_macros.h"
33 /* code to manage tape hosts
34 * specific to the ubik database implementation
37 afs_int32 bc_UpdateHosts();
38 extern struct bc_config *bc_globalConfig;
39 extern struct udbHandleS udbHandle;
42 /* ------------------------------------
43 * command level routines
44 * ------------------------------------
49 * Add a host to the tape hosts
52 afs_int32 bc_AddHostCmd(as, arock)
53 struct cmd_syndesc *as;
59 register afs_int32 code = 0;
61 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
62 code = bc_LockText(ctPtr);
66 code = bc_UpdateHosts();
68 com_err(whoami, code, "; Can't retrieve tape hosts");
72 /* add tape hosts first */
73 ti = as->parms[0].items;
75 if (as->parms[1].items) {
76 port = getPortOffset(as->parms[1].items->data);
77 if (port < 0) ERROR(BC_BADARG);
79 printf("Adding host %s offset %u to tape list...", ti->data,port);
81 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
84 printf("failed\n"); fflush(stdout);
86 com_err(whoami,0,"Port offset already in tape database");
90 code = bc_SaveHosts();
93 com_err(whoami, code, "Cannot save tape hosts");
94 com_err(whoami, 0,"Changes are temporary - for this session only");
103 if ( ctPtr->lockHandle != 0 )
104 bc_UnlockText(ctPtr);
108 afs_int32 bc_DeleteHostCmd(as, arock)
109 struct cmd_syndesc *as;
114 udbClientTextP ctPtr;
115 register afs_int32 code = 0;
117 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
118 code = bc_LockText(ctPtr);
122 code = bc_UpdateHosts();
124 com_err(whoami, code, "; Can't retrieve tape hosts");
128 /* delete tape hosts first */
129 ti = as->parms[0].items;
131 if (as->parms[1].items) {
132 port = bc_SafeATOI(as->parms[1].items->data);
133 if (port < 0) return(BC_BADARG);
136 printf("Deleting host %s offset %u to tape list...", ti->data,port);
138 code = bc_DeleteTapeHost(bc_globalConfig, ti->data,port);
142 printf("failed: no such host entry\n");
144 printf("failed with code %d\n", code);
148 code = bc_SaveHosts();
151 com_err(whoami, code, "Cannot save tape hosts");
152 com_err(whoami, 0,"Changes are temporary - for this session only");
162 if ( ctPtr->lockHandle != 0 )
163 bc_UnlockText(ctPtr);
169 * list all tape hosts (from internally built tables)
174 afs_int32 bc_ListHostsCmd(as, arock)
175 struct cmd_syndesc *as;
179 register struct bc_hostEntry *tentry;
181 code = bc_UpdateHosts();
183 com_err(whoami, code, "; Can't retrieve tape hosts");
187 printf("Tape hosts:\n");
188 for ( tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next )
190 printf(" Host %s, port offset %u\n",
191 tentry->name,tentry->portOffset);
199 /* ------------------------------------
201 * ------------------------------------
206 register struct bc_hostEntry *tentry, *temp;
208 tentry = bc_globalConfig->tapeHosts;
209 while ( tentry != 0 )
217 bc_globalConfig->tapeHosts = (struct bc_hostEntry *) 0;
222 * Open up the volume set configuration file as specified in our argument,
223 * then parse the file to set up our internal representation.
225 * 0 on successful processing,
234 struct bc_hostEntry *tfirst, *tlast, *the;
238 udbClientTextP ctPtr;
241 /* initialize locally used variables */
242 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
243 stream = ctPtr->textStream;
245 if ( ctPtr->textSize == 0 ) /* nothing defined yet */
248 if ( stream == NULL )
249 return(BC_INTERNALERROR);
253 /* now read the lines and build the structure list */
254 tfirst = tlast = (struct bc_hostEntry *) 0;
258 tp = fgets(tbuffer, sizeof(tbuffer), stream);
260 break; /* end of file */
262 sscanf(tbuffer, "%s %u", hostName, &port);
263 th = gethostbyname(hostName);
266 com_err(whoami,0,"can't get host info for %s from nameserver or /etc/hosts.",
269 the = (struct bc_hostEntry *) malloc(sizeof(struct bc_hostEntry));
270 if (the == (struct bc_hostEntry *)0)
272 bzero(the, sizeof(struct bc_hostEntry));
280 tfirst = tlast = the;
282 the->next = (struct bc_hostEntry *) 0;
283 the->name = (char *) malloc(strlen(hostName)+1);
284 strcpy(the->name, hostName);
285 the->portOffset = port;
287 bcopy(th->h_addr, &the->addr.sin_addr.s_addr, 4);
288 the->addr.sin_family = AF_INET;
289 the->addr.sin_port = 0;
293 bc_globalConfig->tapeHosts = tfirst;
299 * 1) save the current host information to disk
300 * 2) transmit to ubik server
305 register afs_int32 code = 0;
307 udbClientTextP ctPtr;
308 register FILE *stream;
309 struct bc_hostEntry *hePtr;
311 extern struct bc_config *bc_globalConfig;
313 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
314 stream = ctPtr->textStream;
317 if ( ctPtr->lockHandle == 0 )
318 return(BC_INTERNALERROR);
320 /* truncate the file */
321 code = ftruncate(fileno(stream), 0);
327 hePtr = bc_globalConfig->tapeHosts;
331 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
336 return(BC_INTERNALERROR);
339 code = bcdb_SaveTextFile(ctPtr);
343 /* do this on bcdb_SaveTextFile */
344 /* increment local version number */
345 ctPtr->textVersion++;
347 /* update locally stored file size */
348 ctPtr->textSize = filesize(ctPtr->textStream);
354 afs_int32 bc_UpdateHosts()
356 struct udbHandleS *uhptr = &udbHandle;
357 udbClientTextP ctPtr;
361 /* lock schedules and check validity */
362 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
364 code = bc_CheckTextVersion(ctPtr);
365 if ( code != BC_VERSIONMISMATCH )
367 ERROR(code); /* Version matches or some other error */
370 /* Must update the hosts */
371 /* If we are not already locked, then lock it now */
372 if ( ctPtr->lockHandle == 0 ) {
373 code = bc_LockText(ctPtr);
379 if (ctPtr->textVersion != -1)
381 com_err(whoami,0,"obsolete tapehosts - updating");
385 /* open a temp file to store the config text received from buserver *
386 * The open file stream is stored in ctPtr->textStream */
387 code = bc_openTextFile(ctPtr, &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
390 /* now get a fresh set of information from the database */
391 code = bcdb_GetTextFile(ctPtr);
395 /* fetch the version number */
396 code = ubik_Call(BUDB_GetTextVersion, uhptr->uh_client, 0,
397 ctPtr->textType, &ctPtr->textVersion);
402 code = bc_ParseHosts();
407 if ( lock && ctPtr->lockHandle )
408 bc_UnlockText(ctPtr);