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>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
26 #include <afs/budb_client.h>
28 #include <afs/com_err.h>
29 #include <afs/bubasics.h>
31 #include "error_macros.h"
34 /* code to manage tape hosts
35 * specific to the ubik database implementation
38 afs_int32 bc_UpdateHosts();
39 extern struct bc_config *bc_globalConfig;
40 extern struct udbHandleS udbHandle;
43 /* ------------------------------------
44 * command level routines
45 * ------------------------------------
50 * Add a host to the tape hosts
54 bc_AddHostCmd(as, arock)
55 struct cmd_syndesc *as;
61 register afs_int32 code = 0;
63 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
64 code = bc_LockText(ctPtr);
68 code = bc_UpdateHosts();
70 com_err(whoami, code, "; Can't retrieve tape hosts");
74 /* add tape hosts first */
75 ti = as->parms[0].items;
77 if (as->parms[1].items) {
78 port = getPortOffset(as->parms[1].items->data);
82 printf("Adding host %s offset %u to tape list...", ti->data, port);
84 code = bc_AddTapeHost(bc_globalConfig, ti->data, port);
89 com_err(whoami, 0, "Port offset already in tape database");
93 code = bc_SaveHosts();
95 com_err(whoami, code, "Cannot save tape hosts");
97 "Changes are temporary - for this session only");
106 if (ctPtr->lockHandle != 0)
107 bc_UnlockText(ctPtr);
112 bc_DeleteHostCmd(as, arock)
113 struct cmd_syndesc *as;
118 udbClientTextP ctPtr;
119 register afs_int32 code = 0;
121 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
122 code = bc_LockText(ctPtr);
126 code = bc_UpdateHosts();
128 com_err(whoami, code, "; Can't retrieve tape hosts");
132 /* delete tape hosts first */
133 ti = as->parms[0].items;
135 if (as->parms[1].items) {
136 port = bc_SafeATOI(as->parms[1].items->data);
141 printf("Deleting host %s offset %u to tape list...", ti->data, port);
143 code = bc_DeleteTapeHost(bc_globalConfig, ti->data, port);
146 printf("failed: no such host entry\n");
148 printf("failed with code %d\n", code);
152 code = bc_SaveHosts();
154 com_err(whoami, code, "Cannot save tape hosts");
156 "Changes are temporary - for this session only");
166 if (ctPtr->lockHandle != 0)
167 bc_UnlockText(ctPtr);
173 * list all tape hosts (from internally built tables)
179 bc_ListHostsCmd(as, arock)
180 struct cmd_syndesc *as;
184 register struct bc_hostEntry *tentry;
186 code = bc_UpdateHosts();
188 com_err(whoami, code, "; Can't retrieve tape hosts");
192 printf("Tape hosts:\n");
193 for (tentry = bc_globalConfig->tapeHosts; tentry; tentry = tentry->next) {
194 printf(" Host %s, port offset %u\n", tentry->name,
203 /* ------------------------------------
205 * ------------------------------------
210 register struct bc_hostEntry *tentry, *temp;
212 tentry = bc_globalConfig->tapeHosts;
213 while (tentry != 0) {
220 bc_globalConfig->tapeHosts = (struct bc_hostEntry *)0;
225 * Open up the volume set configuration file as specified in our argument,
226 * then parse the file to set up our internal representation.
228 * 0 on successful processing,
238 struct bc_hostEntry *tfirst, *tlast, *the;
242 udbClientTextP ctPtr;
245 /* initialize locally used variables */
246 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
247 stream = ctPtr->textStream;
249 if (ctPtr->textSize == 0) /* nothing defined yet */
253 return (BC_INTERNALERROR);
257 /* now read the lines and build the structure list */
258 tfirst = tlast = (struct bc_hostEntry *)0;
261 tp = fgets(tbuffer, sizeof(tbuffer), stream);
263 break; /* end of file */
265 sscanf(tbuffer, "%s %u", hostName, &port);
266 th = gethostbyname(hostName);
269 "can't get host info for %s from nameserver or /etc/hosts.",
272 the = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry));
273 if (the == (struct bc_hostEntry *)0)
275 memset(the, 0, 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 memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4);
288 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
289 the->addr.sin_len = sizeof(struct sockaddr_in);
291 the->addr.sin_family = AF_INET;
292 the->addr.sin_port = 0;
296 bc_globalConfig->tapeHosts = tfirst;
302 * 1) save the current host information to disk
303 * 2) transmit to ubik server
308 register afs_int32 code = 0;
310 udbClientTextP ctPtr;
311 register FILE *stream;
312 struct bc_hostEntry *hePtr;
314 extern struct bc_config *bc_globalConfig;
316 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
317 stream = ctPtr->textStream;
320 if (ctPtr->lockHandle == 0)
321 return (BC_INTERNALERROR);
323 /* truncate the file */
324 code = ftruncate(fileno(stream), 0);
330 hePtr = bc_globalConfig->tapeHosts;
333 fprintf(stream, "%s %u\n", hePtr->name, hePtr->portOffset);
338 return (BC_INTERNALERROR);
341 code = bcdb_SaveTextFile(ctPtr);
345 /* do this on bcdb_SaveTextFile */
346 /* increment local version number */
347 ctPtr->textVersion++;
349 /* update locally stored file size */
350 ctPtr->textSize = filesize(ctPtr->textStream);
359 struct udbHandleS *uhptr = &udbHandle;
360 udbClientTextP ctPtr;
364 /* lock schedules and check validity */
365 ctPtr = &bc_globalConfig->configText[TB_TAPEHOSTS];
367 code = bc_CheckTextVersion(ctPtr);
368 if (code != BC_VERSIONMISMATCH) {
369 ERROR(code); /* Version matches or some other error */
372 /* Must update the hosts */
373 /* If we are not already locked, then lock it now */
374 if (ctPtr->lockHandle == 0) {
375 code = bc_LockText(ctPtr);
381 if (ctPtr->textVersion != -1) {
382 com_err(whoami, 0, "obsolete tapehosts - updating");
386 /* open a temp file to store the config text received from buserver *
387 * The open file stream is stored in ctPtr->textStream */
389 bc_openTextFile(ctPtr,
390 &bc_globalConfig->tmpTextFileNames[TB_TAPEHOSTS][0]);
393 /* now get a fresh set of information from the database */
394 code = bcdb_GetTextFile(ctPtr);
398 /* fetch the version number */
400 ubik_Call(BUDB_GetTextVersion, uhptr->uh_client, 0, ctPtr->textType,
401 &ctPtr->textVersion);
406 code = bc_ParseHosts();
411 if (lock && ctPtr->lockHandle)
412 bc_UnlockText(ctPtr);