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>
15 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
23 #include <afs/budb_client.h>
25 #include <afs/com_err.h>
26 #include <afs/bubasics.h>
28 #include "error_macros.h"
30 #include "bucoord_internal.h"
32 /* code to manage tape hosts
33 * specific to the ubik database implementation
36 extern struct bc_config *bc_globalConfig;
37 extern struct udbHandleS udbHandle;
40 /* ------------------------------------
41 * command level routines
42 * ------------------------------------
47 * Add a host to the tape hosts
51 bc_AddHostCmd(struct cmd_syndesc *as, void *arock)
56 register afs_int32 code = 0;
58 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
59 code = bc_LockText(ctPtr);
63 code = bc_UpdateHosts();
65 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
69 /* add tape hosts first */
70 ti = as->parms[0].items;
72 if (as->parms[1].items) {
73 port = getPortOffset(as->parms[1].items->data);
77 printf("Adding host %s offset %u to tape list...", ti->data, port);
79 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
84 afs_com_err(whoami, 0, "Port offset already in tape database");
88 code = bc_SaveHosts();
90 afs_com_err(whoami, code, "Cannot save tape hosts");
91 afs_com_err(whoami, 0,
92 "Changes are temporary - for this session only");
101 if (ctPtr->lockHandle != 0)
102 bc_UnlockText(ctPtr);
107 bc_DeleteHostCmd(struct cmd_syndesc *as, void *arock)
111 udbClientTextP ctPtr;
112 register afs_int32 code = 0;
114 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
115 code = bc_LockText(ctPtr);
119 code = bc_UpdateHosts();
121 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
125 /* delete tape hosts first */
126 ti = as->parms[0].items;
128 if (as->parms[1].items) {
129 port = bc_SafeATOI(as->parms[1].items->data);
134 printf("Deleting host %s offset %u to tape list...", ti->data, port);
136 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
139 printf("failed: no such host entry\n");
141 printf("failed with code %d\n", code);
145 code = bc_SaveHosts();
147 afs_com_err(whoami, code, "Cannot save tape hosts");
148 afs_com_err(whoami, 0,
149 "Changes are temporary - for this session only");
159 if (ctPtr->lockHandle != 0)
160 bc_UnlockText(ctPtr);
166 * list all tape hosts (from internally built tables)
172 bc_ListHostsCmd(struct cmd_syndesc *as, void *arock)
175 register struct bc_hostEntry *tentry;
177 code = bc_UpdateHosts();
179 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
183 printf("Tape hosts:\n");
184 for (tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next) {
185 printf(" Host %s, port offset %u\n", tentry->name,
194 /* ------------------------------------
196 * ------------------------------------
202 register struct bc_hostEntry *tentry, *temp;
204 tentry = bc_globalConfig->tapeHosts;
205 while (tentry != 0) {
212 bc_globalConfig->tapeHosts = (struct bc_hostEntry *)0;
217 * Open up the volume set configuration file as specified in our argument,
218 * then parse the file to set up our internal representation.
220 * 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 */
245 return (BC_INTERNALERROR);
249 /* now read the lines and build the structure list */
250 tfirst = tlast = (struct bc_hostEntry *)0;
253 tp = fgets(tbuffer, sizeof(tbuffer), stream);
255 break; /* end of file */
257 sscanf(tbuffer, "%s %u", hostName, &port);
258 th = gethostbyname(hostName);
260 afs_com_err(whoami, 0,
261 "can't get host info for %s from nameserver or /etc/hosts.",
264 the = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry));
265 if (the == (struct bc_hostEntry *)0)
267 memset(the, 0, sizeof(struct bc_hostEntry));
272 tfirst = tlast = the;
274 the->next = (struct bc_hostEntry *)0;
275 the->name = (char *)malloc(strlen(hostName) + 1);
276 strcpy(the->name, hostName);
277 the->portOffset = port;
279 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
280 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
281 the->addr.sin_len = sizeof(struct sockaddr_in);
283 the->addr.sin_family = AF_INET;
284 the->addr.sin_port = 0;
288 bc_globalConfig->tapeHosts = tfirst;
294 * 1) save the current host information to disk
295 * 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;
326 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
331 return (BC_INTERNALERROR);
334 code = bcdb_SaveTextFile(ctPtr);
338 /* do this on bcdb_SaveTextFile */
339 /* increment local version number */
340 ctPtr->textVersion++;
342 /* update locally stored file size */
343 ctPtr->textSize = filesize(ctPtr->textStream);
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) {
362 ERROR(code); /* Version matches or some other error */
365 /* Must update the hosts */
366 /* If we are not already locked, then lock it now */
367 if (ctPtr->lockHandle == 0) {
368 code = bc_LockText(ctPtr);
374 if (ctPtr->textVersion != -1) {
375 afs_com_err(whoami, 0, "obsolete tapehosts - updating");
379 /* open a temp file to store the config text received from buserver *
380 * The open file stream is stored in ctPtr->textStream */
382 bc_openTextFile(ctPtr,
383 &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 */
393 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
394 &ctPtr->textVersion);
399 code = bc_ParseHosts();
404 if (lock && ctPtr->lockHandle)
405 bc_UnlockText(ctPtr);