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"
31 #include "bucoord_prototypes.h"
33 /* code to manage tape hosts
34 * specific to the ubik database implementation
37 extern struct bc_config *bc_globalConfig;
38 extern struct udbHandleS udbHandle;
41 /* ------------------------------------
42 * command level routines
43 * ------------------------------------
48 * Add a host to the tape hosts
52 bc_AddHostCmd(struct cmd_syndesc *as, void *arock)
59 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
60 code = bc_LockText(ctPtr);
64 code = bc_UpdateHosts();
66 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
70 /* add tape hosts first */
71 ti = as->parms[0].items;
73 if (as->parms[1].items) {
74 port = getPortOffset(as->parms[1].items->data);
78 printf("Adding host %s offset %u to tape list...", ti->data, port);
80 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
85 afs_com_err(whoami, 0, "Port offset already in tape database");
89 code = bc_SaveHosts();
91 afs_com_err(whoami, code, "Cannot save tape hosts");
92 afs_com_err(whoami, 0,
93 "Changes are temporary - for this session only");
102 if (ctPtr->lockHandle != 0)
103 bc_UnlockText(ctPtr);
108 bc_DeleteHostCmd(struct cmd_syndesc *as, void *arock)
112 udbClientTextP ctPtr;
115 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
116 code = bc_LockText(ctPtr);
120 code = bc_UpdateHosts();
122 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
126 /* delete tape hosts first */
127 ti = as->parms[0].items;
129 if (as->parms[1].items) {
130 port = bc_SafeATOI(as->parms[1].items->data);
135 printf("Deleting host %s offset %u to tape list...", ti->data, port);
137 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
140 printf("failed: no such host entry\n");
142 printf("failed with code %d\n", code);
146 code = bc_SaveHosts();
148 afs_com_err(whoami, code, "Cannot save tape hosts");
149 afs_com_err(whoami, 0,
150 "Changes are temporary - for this session only");
160 if (ctPtr->lockHandle != 0)
161 bc_UnlockText(ctPtr);
167 * list all tape hosts (from internally built tables)
173 bc_ListHostsCmd(struct cmd_syndesc *as, void *arock)
176 struct bc_hostEntry *tentry;
178 code = bc_UpdateHosts();
180 afs_com_err(whoami, code, "; Can't retrieve tape hosts");
184 printf("Tape hosts:\n");
185 for (tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next) {
186 printf(" Host %s, port offset %u\n", tentry->name,
195 /* ------------------------------------
197 * ------------------------------------
203 struct bc_hostEntry *tentry, *temp;
205 tentry = bc_globalConfig->tapeHosts;
206 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,
231 struct bc_hostEntry *tfirst, *tlast, *the;
235 udbClientTextP ctPtr;
238 /* initialize locally used variables */
239 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
240 stream = ctPtr->textStream;
242 if (ctPtr->textSize == 0) /* nothing defined yet */
246 return (BC_INTERNALERROR);
250 /* now read the lines and build the structure list */
251 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);
261 afs_com_err(whoami, 0,
262 "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 memset(the, 0, sizeof(struct bc_hostEntry));
273 tfirst = tlast = the;
275 the->next = (struct bc_hostEntry *)0;
276 the->name = (char *)malloc(strlen(hostName) + 1);
277 strcpy(the->name, hostName);
278 the->portOffset = port;
280 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
281 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
282 the->addr.sin_len = sizeof(struct sockaddr_in);
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
304 udbClientTextP ctPtr;
306 struct bc_hostEntry *hePtr;
308 extern struct bc_config *bc_globalConfig;
310 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
311 stream = ctPtr->textStream;
314 if (ctPtr->lockHandle == 0)
315 return (BC_INTERNALERROR);
317 /* truncate the file */
318 code = ftruncate(fileno(stream), 0);
324 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);
353 struct udbHandleS *uhptr = &udbHandle;
354 udbClientTextP ctPtr;
358 /* lock schedules and check validity */
359 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
361 code = bc_CheckTextVersion(ctPtr);
362 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) {
376 afs_com_err(whoami, 0, "obsolete tapehosts - updating");
380 /* open a temp file to store the config text received from buserver *
381 * The open file stream is stored in ctPtr->textStream */
383 bc_openTextFile(ctPtr,
384 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
387 /* now get a fresh set of information from the database */
388 code = bcdb_GetTextFile(ctPtr);
392 /* fetch the version number */
394 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
395 &ctPtr->textVersion);
400 code = bc_ParseHosts();
405 if (lock && ctPtr->lockHandle)
406 bc_UnlockText(ctPtr);