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>
12 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
21 #include <afs/budb_client.h>
23 #include <afs/com_err.h>
24 #include <afs/bubasics.h>
26 #include "error_macros.h"
29 /* code to manage tape hosts
30 * specific to the ubik database implementation
33 afs_int32 bc_UpdateHosts();
34 extern struct bc_config *bc_globalConfig;
35 extern struct udbHandleS udbHandle;
38 /* ------------------------------------
39 * command level routines
40 * ------------------------------------
45 * Add a host to the tape hosts
48 afs_int32 bc_AddHostCmd(as, arock)
49 struct cmd_syndesc *as;
55 register afs_int32 code = 0;
57 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
58 code = bc_LockText(ctPtr);
62 code = bc_UpdateHosts();
64 com_err(whoami, code, "; Can't retrieve tape hosts");
68 /* add tape hosts first */
69 ti = as->parms[0].items;
71 if (as->parms[1].items) {
72 port = getPortOffset(as->parms[1].items->data);
73 if (port < 0) ERROR(BC_BADARG);
75 printf("Adding host %s offset %u to tape list...", ti->data,port);
77 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
80 printf("failed\n"); fflush(stdout);
82 com_err(whoami,0,"Port offset already in tape database");
86 code = bc_SaveHosts();
89 com_err(whoami, code, "Cannot save tape hosts");
90 com_err(whoami, 0,"Changes are temporary - for this session only");
99 if ( ctPtr->lockHandle != 0 )
100 bc_UnlockText(ctPtr);
104 afs_int32 bc_DeleteHostCmd(as, arock)
105 struct cmd_syndesc *as;
110 udbClientTextP ctPtr;
111 register afs_int32 code = 0;
113 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
114 code = bc_LockText(ctPtr);
118 code = bc_UpdateHosts();
120 com_err(whoami, code, "; Can't retrieve tape hosts");
124 /* delete tape hosts first */
125 ti = as->parms[0].items;
127 if (as->parms[1].items) {
128 port = bc_SafeATOI(as->parms[1].items->data);
129 if (port < 0) return(BC_BADARG);
132 printf("Deleting host %s offset %u to tape list...", ti->data,port);
134 code = bc_DeleteTapeHost(bc_globalConfig, ti->data,port);
138 printf("failed: no such host entry\n");
140 printf("failed with code %d\n", code);
144 code = bc_SaveHosts();
147 com_err(whoami, code, "Cannot save tape hosts");
148 com_err(whoami, 0,"Changes are temporary - for this session only");
158 if ( ctPtr->lockHandle != 0 )
159 bc_UnlockText(ctPtr);
165 * list all tape hosts (from internally built tables)
170 afs_int32 bc_ListHostsCmd(as, arock)
171 struct cmd_syndesc *as;
175 register struct bc_hostEntry *tentry;
177 code = bc_UpdateHosts();
179 com_err(whoami, code, "; Can't retrieve tape hosts");
183 printf("Tape hosts:\n");
184 for ( tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next )
186 printf(" Host %s, port offset %u\n",
187 tentry->name,tentry->portOffset);
195 /* ------------------------------------
197 * ------------------------------------
202 register struct bc_hostEntry *tentry, *temp;
204 tentry = bc_globalConfig->tapeHosts;
205 while ( tentry != 0 )
213 bc_globalConfig->tapeHosts = (struct bc_hostEntry *) 0;
218 * Open up the volume set configuration file as specified in our argument,
219 * then parse the file to set up our internal representation.
221 * 0 on successful processing,
230 struct bc_hostEntry *tfirst, *tlast, *the;
234 udbClientTextP ctPtr;
237 /* initialize locally used variables */
238 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
239 stream = ctPtr->textStream;
241 if ( ctPtr->textSize == 0 ) /* nothing defined yet */
244 if ( stream == NULL )
245 return(BC_INTERNALERROR);
249 /* now read the lines and build the structure list */
250 tfirst = tlast = (struct bc_hostEntry *) 0;
254 tp = fgets(tbuffer, sizeof(tbuffer), stream);
256 break; /* end of file */
258 sscanf(tbuffer, "%s %u", hostName, &port);
259 th = gethostbyname(hostName);
262 com_err(whoami,0,"can't get host info for %s from nameserver or /etc/hosts.",
265 the = (struct bc_hostEntry *) malloc(sizeof(struct bc_hostEntry));
266 if (the == (struct bc_hostEntry *)0)
268 bzero(the, sizeof(struct bc_hostEntry));
276 tfirst = tlast = the;
278 the->next = (struct bc_hostEntry *) 0;
279 the->name = (char *) malloc(strlen(hostName)+1);
280 strcpy(the->name, hostName);
281 the->portOffset = port;
283 bcopy(th->h_addr, &the->addr.sin_addr.s_addr, 4);
284 the->addr.sin_family = AF_INET;
285 the->addr.sin_port = 0;
289 bc_globalConfig->tapeHosts = tfirst;
295 * 1) save the current host information to disk
296 * 2) transmit to ubik server
301 register afs_int32 code = 0;
303 udbClientTextP ctPtr;
304 register FILE *stream;
305 struct bc_hostEntry *hePtr;
307 extern struct bc_config *bc_globalConfig;
309 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
310 stream = ctPtr->textStream;
313 if ( ctPtr->lockHandle == 0 )
314 return(BC_INTERNALERROR);
316 /* truncate the file */
317 code = ftruncate(fileno(stream), 0);
323 hePtr = bc_globalConfig->tapeHosts;
327 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
332 return(BC_INTERNALERROR);
335 code = bcdb_SaveTextFile(ctPtr);
339 /* do this on bcdb_SaveTextFile */
340 /* increment local version number */
341 ctPtr->textVersion++;
343 /* update locally stored file size */
344 ctPtr->textSize = filesize(ctPtr->textStream);
350 afs_int32 bc_UpdateHosts()
352 struct udbHandleS *uhptr = &udbHandle;
353 udbClientTextP ctPtr;
357 /* lock schedules and check validity */
358 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
360 code = bc_CheckTextVersion(ctPtr);
361 if ( code != BC_VERSIONMISMATCH )
363 ERROR(code); /* Version matches or some other error */
366 /* Must update the hosts */
367 /* If we are not already locked, then lock it now */
368 if ( ctPtr->lockHandle == 0 ) {
369 code = bc_LockText(ctPtr);
375 if (ctPtr->textVersion != -1)
377 com_err(whoami,0,"obsolete tapehosts - updating");
381 /* open a temp file to store the config text received from buserver *
382 * The open file stream is stored in ctPtr->textStream */
383 code = bc_openTextFile(ctPtr, &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
386 /* now get a fresh set of information from the database */
387 code = bcdb_GetTextFile(ctPtr);
391 /* fetch the version number */
392 code = ubik_Call(BUDB_GetTextVersion, uhptr->uh_client, 0,
393 ctPtr->textType, &ctPtr->textVersion);
398 code = bc_ParseHosts();
403 if ( lock && ctPtr->lockHandle )
404 bc_UnlockText(ctPtr);