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
13 * (C) COPYRIGHT IBM CORPORATION 1987, 1998
14 * LICENSED MATERIALS - PROPERTY OF IBM
18 #include <afsconfig.h>
19 #include <afs/param.h>
24 #include <afs/com_err.h>
25 #include <afs/bubasics.h>
28 #include "bucoord_internal.h"
30 struct ubik_client *cstructp; /*Ptr to Ubik client structure */
32 static int FreeVolumeEntryList(struct bc_volumeEntry *aentry);
33 static int FreeVolumeEntry(struct bc_volumeEntry *aentry);
35 /* Code to maintain dump schedule and volume set abstractions.
36 * A volume set looks like this:
37 * vsname: servername partition-name <volume list>*
38 * A dump schedule looks like this:
39 * dsname: vsname period parent-ds
42 /* get partition id from a name */
44 bc_GetPartitionID(char *aname, afs_int32 *aval)
47 /*bc_GetPartitionID */
52 /* special-case "anything" */
53 if (strcmp(aname, ".*") == 0) {
59 return -1; /* unknown */
60 /* numbers go straight through */
61 if (tc >= '0' && tc <= '9') {
62 *aval = bc_SafeATOI(aname);
65 /* otherwise check for vicepa or /vicepa, or just plain "a" */
67 if (strlen(aname) <= 2) {
69 } else if (!strncmp(aname, "/vicep", 6)) {
70 strncpy(ascii, aname + 6, 2);
71 } else if (!strncmp(aname, "vicep", 5)) {
72 strncpy(ascii, aname + 5, 2);
74 return (BC_NOPARTITION); /* bad partition name */
75 /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz, and are numbered
76 * from 0. Do the appropriate conversion */
78 /* one char name, 0..25 */
79 if (ascii[0] < 'a' || ascii[0] > 'z')
80 return -1; /* wrongo */
81 *aval = ascii[0] - 'a';
84 /* two char name, 26 .. <whatever> */
85 if (ascii[0] < 'a' || ascii[0] > 'z')
86 return -1; /* wrongo */
87 if (ascii[1] < 'a' || ascii[1] > 'z')
88 return -1; /* just as bad */
89 *aval = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
92 } /*bc_GetPartitionID */
94 /*----------------------------------------------------------------------------
98 * Given a string containing a host name or its IP address in dot notation, fill in
99 * the given sockaddr with all the corresponding info.
102 * aname : Host name or dotted IP address.
103 * asockaddr: Ptr to sockaddr to fill in for the above host.
106 * 0 if everything went well,
107 * -1 if it couldn't be translated.
110 * Nothing interesting.
114 *----------------------------------------------------------------------------
118 bc_ParseHost(char *aname, struct sockaddr_in *asockaddr)
122 struct hostent *th; /*Host entry */
123 afs_uint32 addr; /*Converted address */
124 afs_int32 b1, b2, b3, b4; /*Byte-sized address chunks */
125 afs_int32 code; /*Return code from sscanf() */
126 afs_int32 tmp1, tmp2;
129 * Try to parse the given name as a dot-notation IP address first.
131 code = sscanf(aname, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
134 * Four chunks were read, so we assume success. Construct the socket.
136 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
137 asockaddr->sin_len = sizeof(struct sockaddr_in);
139 asockaddr->sin_family = AF_INET;
140 asockaddr->sin_port = 0;
141 addr = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
142 memcpy(&tmp1, &addr, sizeof(afs_int32));
144 memcpy(&asockaddr->sin_addr.s_addr, &tmp2, sizeof(afs_int32));
149 * The given string isn't a dotted IP address. Try to map it as a host
150 * name, or leave it as a wild-card.
153 if (strcmp(aname, ".*") == 0) {
154 memset(asockaddr, 0, sizeof(struct sockaddr_in));
158 th = gethostbyname(aname);
161 * No such luck, return failure.
166 * We found a mapping; construct the socket.
168 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
169 asockaddr->sin_len = sizeof(struct sockaddr_in);
171 asockaddr->sin_family = AF_INET;
172 asockaddr->sin_port = 0;
173 memcpy(&tmp1, th->h_addr, sizeof(afs_int32));
175 memcpy(&(asockaddr->sin_addr.s_addr), &tmp2,
176 sizeof(asockaddr->sin_addr.s_addr));
182 /* create an empty volume set, new items are added via bc_AddVolumeItem */
184 bc_CreateVolumeSet(struct bc_config *aconfig, char *avolName,
187 struct bc_volumeSet **tlast, *tset, *nset;
189 if (bc_FindVolumeSet(aconfig, avolName))
190 return -1; /* already exists */
191 /* move to end of the list */
193 nset = (struct bc_volumeSet *)malloc(sizeof(struct bc_volumeSet));
194 memset(nset, 0, sizeof(*nset));
195 nset->flags = aflags;
196 nset->name = (char *)malloc(strlen(avolName) + 1);
197 strcpy(nset->name, avolName);
198 if (aflags & VSFLAG_TEMPORARY) {
199 /* Add to beginning of list */
200 nset->next = aconfig->vset;
201 aconfig->vset = nset;
203 /* Add to end of list */
204 for (tlast = &aconfig->vset, tset = *tlast; tset;
205 tlast = &tset->next, tset = *tlast);
212 FreeVolumeEntry(struct bc_volumeEntry *aentry)
215 free(aentry->serverName);
216 free(aentry->partname);
222 FreeVolumeEntryList(struct bc_volumeEntry *aentry)
224 struct bc_volumeEntry *tnext;
227 tnext = aentry->next;
228 FreeVolumeEntry(aentry);
237 FreeVolumeSet(struct bc_volumeSet *avset)
239 FreeVolumeEntryList(avset->ventries);
245 bc_DeleteVolumeSet(struct bc_config *aconfig, char *avolName,
248 struct bc_volumeSet **tlast, *tset;
251 tlast = &aconfig->vset;
252 for (tset = *tlast; tset; tlast = &tset->next, tset = *tlast) {
253 if (strcmp(avolName, tset->name) == 0) {
254 *flags = tset->flags; /* Remember flags */
255 *tlast = tset->next; /* Remove from chain */
256 FreeVolumeSet(tset); /* Free the volume set */
261 /* if we get here, we didn't find the item */
266 bc_DeleteVolumeItem(struct bc_config *aconfig, char *avolName,
270 struct bc_volumeSet *tset;
271 struct bc_volumeEntry *tentry, **tlast;
273 tset = bc_FindVolumeSet(aconfig, avolName);
277 tlast = &tset->ventries;
278 for (i = 1, tentry = *tlast; tentry;
279 tlast = &tentry->next, tentry = *tlast, i++) {
281 /* found entry we want */
282 *tlast = tentry->next;
283 FreeVolumeEntry(tentry);
287 return -2; /* not found */
291 bc_AddVolumeItem(struct bc_config *aconfig, char *avolName, char *ahost,
292 char *apart, char *avol)
294 struct bc_volumeSet *tset;
295 struct bc_volumeEntry **tlast, *tentry;
298 tset = bc_FindVolumeSet(aconfig, avolName);
300 return (BC_NOVOLSET);
302 /* otherwise append this item to the end of the real list */
303 tlast = &tset->ventries;
305 /* move to end of the list */
306 for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast);
307 tentry = (struct bc_volumeEntry *)malloc(sizeof(struct bc_volumeEntry));
308 memset(tentry, 0, sizeof(*tentry));
309 tentry->serverName = (char *)malloc(strlen(ahost) + 1);
310 strcpy(tentry->serverName, ahost);
311 tentry->partname = (char *)malloc(strlen(apart) + 1);
312 strcpy(tentry->partname, apart);
313 tentry->name = (char *)malloc(strlen(avol) + 1);
314 strcpy(tentry->name, avol);
316 code = bc_ParseHost(tentry->serverName, &tentry->server);
320 code = bc_GetPartitionID(tentry->partname, &tentry->partition);
324 *tlast = tentry; /* thread on the list */
328 struct bc_volumeSet *
329 bc_FindVolumeSet(struct bc_config *aconfig, char *aname)
330 { /*bc_FindVolumeSet */
332 struct bc_volumeSet *tvs;
334 for (tvs = aconfig->vset; tvs; tvs = tvs->next) {
335 if (!strcmp(tvs->name, aname))
338 return (struct bc_volumeSet *)0;
340 } /*bc_FindVolumeSet */
342 /* ------------------------------------
343 * dumpschedule management code
344 * ------------------------------------
347 /* bc_CreateDumpSchedule
348 * Add another node to the dump schedule.
350 * aconfig - in core configuration structures
351 * adumpName - name of new dump node
352 * expDate - expiration date
353 * expType - absolute or relative
357 bc_CreateDumpSchedule(struct bc_config *aconfig, char *adumpName,
358 afs_int32 expDate, afs_int32 expType)
360 struct bc_dumpSchedule *tdump;
361 struct bc_dumpSchedule *parent, *node;
364 if (strcmp(adumpName, "none") == 0)
365 return -2; /* invalid name */
367 code = FindDump(aconfig, adumpName, &parent, &node);
369 return -1; /* node already exists */
371 return -2; /* name specification error */
373 tdump = (struct bc_dumpSchedule *)malloc(sizeof(struct bc_dumpSchedule));
374 memset(tdump, 0, sizeof(*tdump));
376 /* prepend this node to the dump schedule list */
377 tdump->next = aconfig->dsched;
378 aconfig->dsched = tdump;
380 /* save the name of this dump node */
381 tdump->name = (char *)malloc(strlen(adumpName) + 1);
382 strcpy(tdump->name, adumpName);
384 /* expiration information */
385 tdump->expDate = expDate;
386 tdump->expType = expType;
388 bc_ProcessDumpSchedule(aconfig); /* redo tree */
393 /* Recursively remove this node and all of its children from aconfig's
394 * list of dumps. Note that this leaves the sibling pointers damaged (pointing
395 * to strange places), so we must call bc_ProcessDumpSchedule when we're done.
398 bc_DeleteDumpScheduleAddr(struct bc_config *aconfig,
399 struct bc_dumpSchedule *adumpAddr)
401 struct bc_dumpSchedule **tlast, *tdump;
402 struct bc_dumpSchedule *tnext;
404 /* knock off all children first */
405 for (tdump = adumpAddr->firstChild; tdump; tdump = tnext) {
406 /* extract next ptr now, since will be freed by recursive call below */
407 tnext = tdump->nextSibling;
408 bc_DeleteDumpScheduleAddr(aconfig, tdump);
411 /* finally, remove us from the list of good dudes */
412 for (tlast = &aconfig->dsched, tdump = *tlast; tdump;
413 tlast = &tdump->next, tdump = *tlast) {
414 if (tdump == adumpAddr) {
415 /* found the one we're looking for */
416 *tlast = tdump->next; /* remove us from basic list */
425 /* bc_FindDumpSchedule
426 * Finds dump schedule aname by doing a linear search
428 * aconfig - handle for incore configuration tables
429 * aname - (path)name to match on
431 * 0 for failure, ptr to dumpschedule for success
434 struct bc_dumpSchedule *
435 bc_FindDumpSchedule(struct bc_config *aconfig, char *aname)
437 struct bc_dumpSchedule *tds;
438 for (tds = aconfig->dsched; tds; tds = tds->next) {
439 if (!strcmp(tds->name, aname))
442 return (struct bc_dumpSchedule *)0;
445 /* bc_DeleteDumpSchedule
446 * Delete dump node adumpName from the dump schedule
450 bc_DeleteDumpSchedule(struct bc_config *aconfig, char *adumpName)
452 struct bc_dumpSchedule *tdump;
454 /* does a linear search of the dump schedules in order to find
457 for (tdump = aconfig->dsched; tdump; tdump = tdump->next) {
458 if (strcmp(tdump->name, adumpName) == 0) {
459 /* found it, we can zap recursively */
460 bc_DeleteDumpScheduleAddr(aconfig, tdump);
461 /* tree's been pruned, but we have to recompute the internal pointers
462 * from first principles, since we didn't bother to maintain
463 * the sibling and children pointers during the call to delete
465 bc_ProcessDumpSchedule(aconfig);
469 /* if we make it here, there's no such dump schedule entry */
474 /* bc_ProcessDumpSchedule
475 * Walk over the dump schedule list, building it into a tree. This
476 * algorithm is simple, but takes O(N*2) operations to run, with N=number
477 * of dump schedule nodes. It probably will never matter
481 bc_ProcessDumpSchedule(struct bc_config *aconfig)
483 struct bc_dumpSchedule *tds;
484 struct bc_dumpSchedule *parentptr, *nodeptr;
487 /* first, clear all the links on all entries so that this function
488 * may be called any number of times with no ill effects
490 for (tds = aconfig->dsched; tds; tds = tds->next) {
491 tds->parent = (struct bc_dumpSchedule *)0;
492 tds->nextSibling = (struct bc_dumpSchedule *)0;
493 tds->firstChild = (struct bc_dumpSchedule *)0;
496 for (tds = aconfig->dsched; tds; tds = tds->next) {
497 retval = FindDump(aconfig, tds->name, &parentptr, &nodeptr);
499 printf("bc_processdumpschedule: finddump returns %d\n", retval);
503 /* only need to do work if it is not a root node */
504 if (parentptr != 0) {
505 nodeptr->parent = parentptr;
506 nodeptr->nextSibling = parentptr->firstChild;
507 parentptr->firstChild = nodeptr;
517 * parentptr - set to parent node, if one exists
518 * nodeptr - set to node requested
521 * 0 - success, parentptr and nodeptr set appropriately
522 * -1 - node not found, parent exists if reqd. Will be 0 for root nodes.
523 * -2 - path search error. Some node on the path does not exist.
524 * -3 - name specification error
526 * pathname checking should be done externally. In particular, trailing
527 * / symbols may return confusing error codes. (e.g on missing last
528 * node returns -2 rather than -1)
532 FindDump(struct bc_config *aconfig, char *nodeString,
533 struct bc_dumpSchedule **parentptr,
534 struct bc_dumpSchedule **nodeptr)
536 struct bc_dumpSchedule *dsptr;
544 /* ensure first char is correct separator */
545 if ((nodeString[0] != '/')
546 || (strlen(&nodeString[0]) <= 1)
548 printf("FindDump: %s, error in dump name specification\n",
554 curptr = &nodeString[1]; /* past first / */
555 separator = strchr(curptr, '/');
557 matchLength = strlen(curptr) + 1; /* +1 for leading / */
559 matchLength = (separator - &nodeString[0]);
561 /* printf("matchLength = %d\n", matchLength); */
563 /* now search all the nodes for this name */
564 for (dsptr = aconfig->dsched; dsptr != 0; dsptr = dsptr->next) {
565 /* printf("compare %s with %s for %d\n",
566 * dsptr->name, nodeString, matchLength); */
567 if (strlen(dsptr->name) != matchLength)
570 if (strncmp(dsptr->name, nodeString, matchLength) == 0) {
576 if (nodeString[matchLength] == 0) {
577 /* last node in the path */
579 return (0); /* all ok */
581 /* node not found; parent exists for non root nodes */
586 /* failed to find a node in the path */
589 curptr = separator + 1;
591 printf("FindDump: trailing / in %s\n", nodeString);
595 separator = strchr(curptr, '/');
597 matchLength = strlen(&nodeString[0]);
599 matchLength = separator - &nodeString[0];
601 *parentptr = *nodeptr;