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>
17 #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
53 bc_AddHostCmd(as, arock)
54 struct cmd_syndesc *as;
60 register afs_int32 code = 0;
62 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
63 code = bc_LockText(ctPtr);
67 code = bc_UpdateHosts();
69 com_err(whoami, code, "; Can't retrieve tape hosts");
73 /* add tape hosts first */
74 ti = as->parms[0].items;
76 if (as->parms[1].items) {
77 port = getPortOffset(as->parms[1].items->data);
81 printf("Adding host %s offset %u to tape list...", ti->data, port);
83 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
88 com_err(whoami, 0, "Port offset already in tape database");
92 code = bc_SaveHosts();
94 com_err(whoami, code, "Cannot save tape hosts");
96 "Changes are temporary - for this session only");
105 if (ctPtr->lockHandle != 0)
106 bc_UnlockText(ctPtr);
111 bc_DeleteHostCmd(as, arock)
112 struct cmd_syndesc *as;
117 udbClientTextP ctPtr;
118 register afs_int32 code = 0;
120 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
121 code = bc_LockText(ctPtr);
125 code = bc_UpdateHosts();
127 com_err(whoami, code, "; Can't retrieve tape hosts");
131 /* delete tape hosts first */
132 ti = as->parms[0].items;
134 if (as->parms[1].items) {
135 port = bc_SafeATOI(as->parms[1].items->data);
140 printf("Deleting host %s offset %u to tape list...", ti->data, port);
142 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
145 printf("failed: no such host entry\n");
147 printf("failed with code %d\n", code);
151 code = bc_SaveHosts();
153 com_err(whoami, code, "Cannot save tape hosts");
155 "Changes are temporary - for this session only");
165 if (ctPtr->lockHandle != 0)
166 bc_UnlockText(ctPtr);
172 * list all tape hosts (from internally built tables)
178 bc_ListHostsCmd(as, arock)
179 struct cmd_syndesc *as;
183 register struct bc_hostEntry *tentry;
185 code = bc_UpdateHosts();
187 com_err(whoami, code, "; Can't retrieve tape hosts");
191 printf("Tape hosts:\n");
192 for (tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next) {
193 printf(" Host %s, port offset %u\n", tentry->name,
202 /* ------------------------------------
204 * ------------------------------------
209 register struct bc_hostEntry *tentry, *temp;
211 tentry = bc_globalConfig->tapeHosts;
212 while (tentry != 0) {
219 bc_globalConfig->tapeHosts = (struct bc_hostEntry *)0;
224 * Open up the volume set configuration file as specified in our argument,
225 * then parse the file to set up our internal representation.
227 * 0 on successful processing,
237 struct bc_hostEntry *tfirst, *tlast, *the;
241 udbClientTextP ctPtr;
244 /* initialize locally used variables */
245 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
246 stream = ctPtr->textStream;
248 if (ctPtr->textSize == 0) /* nothing defined yet */
252 return (BC_INTERNALERROR);
256 /* now read the lines and build the structure list */
257 tfirst = tlast = (struct bc_hostEntry *)0;
260 tp = fgets(tbuffer, sizeof(tbuffer), stream);
262 break; /* end of file */
264 sscanf(tbuffer, "%s %u", hostName, &port);
265 th = gethostbyname(hostName);
268 "can't get host info for %s from nameserver or /etc/hosts.",
271 the = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry));
272 if (the == (struct bc_hostEntry *)0)
274 memset(the, 0, sizeof(struct bc_hostEntry));
279 tfirst = tlast = the;
281 the->next = (struct bc_hostEntry *)0;
282 the->name = (char *)malloc(strlen(hostName) + 1);
283 strcpy(the->name, hostName);
284 the->portOffset = port;
286 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
287 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
288 the->addr.sin_len = sizeof(struct sockaddr_in);
290 the->addr.sin_family = AF_INET;
291 the->addr.sin_port = 0;
295 bc_globalConfig->tapeHosts = tfirst;
301 * 1) save the current host information to disk
302 * 2) transmit to ubik server
307 register afs_int32 code = 0;
309 udbClientTextP ctPtr;
310 register FILE *stream;
311 struct bc_hostEntry *hePtr;
313 extern struct bc_config *bc_globalConfig;
315 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
316 stream = ctPtr->textStream;
319 if (ctPtr->lockHandle == 0)
320 return (BC_INTERNALERROR);
322 /* truncate the file */
323 code = ftruncate(fileno(stream), 0);
329 hePtr = bc_globalConfig->tapeHosts;
332 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
337 return (BC_INTERNALERROR);
340 code = bcdb_SaveTextFile(ctPtr);
344 /* do this on bcdb_SaveTextFile */
345 /* increment local version number */
346 ctPtr->textVersion++;
348 /* update locally stored file size */
349 ctPtr->textSize = filesize(ctPtr->textStream);
358 struct udbHandleS *uhptr = &udbHandle;
359 udbClientTextP ctPtr;
363 /* lock schedules and check validity */
364 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
366 code = bc_CheckTextVersion(ctPtr);
367 if (code != BC_VERSIONMISMATCH) {
368 ERROR(code); /* Version matches or some other error */
371 /* Must update the hosts */
372 /* If we are not already locked, then lock it now */
373 if (ctPtr->lockHandle == 0) {
374 code = bc_LockText(ctPtr);
380 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 */
388 bc_openTextFile(ctPtr,
389 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
392 /* now get a fresh set of information from the database */
393 code = bcdb_GetTextFile(ctPtr);
397 /* fetch the version number */
399 ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
400 &ctPtr->textVersion);
405 code = bc_ParseHosts();
410 if (lock && ctPtr->lockHandle)
411 bc_UnlockText(ctPtr);