Initial import from FreeBSD RELENG_4:
[games.git] / contrib / isc-dhcp / common / memory.c
1 /* memory.c
2
3    Memory-resident database... */
4
5 /*
6  * Copyright (c) 1995-2002 Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38  * To learn more about the Internet Software Consortium, see
39  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
40  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
41  * ``http://www.nominum.com''.
42  */
43
44 #ifndef lint
45 static char copyright[] =
46 "$Id: memory.c,v 1.66.2.4 2002/11/17 02:26:58 dhankins Exp $ Copyright (c) 1995-2002 The Internet Software Consortium.  All rights reserved.\n";
47 #endif /* not lint */
48
49 #include "dhcpd.h"
50
51 struct group *root_group;
52 group_hash_t *group_name_hash;
53 int (*group_write_hook) (struct group_object *);
54
55 isc_result_t delete_group (struct group_object *group, int writep)
56 {
57         struct group_object *d;
58
59         /* The group should exist and be hashed - if not, it's invalid. */
60         if (group_name_hash) {
61                 d = (struct group_object *)0;
62                 group_hash_lookup (&d, group_name_hash, group -> name,
63                                    strlen (group -> name), MDL);
64         } else
65                 return ISC_R_INVALIDARG;
66         if (!d)
67                 return ISC_R_INVALIDARG;
68
69         /* Also not okay to delete a group that's not the one in
70            the hash table. */
71         if (d != group)
72                 return ISC_R_INVALIDARG;
73
74         /* If it's dynamic, and we're deleting it, we can just blow away the
75            hash table entry. */
76         if ((group -> flags & GROUP_OBJECT_DYNAMIC) &&
77             !(group -> flags & GROUP_OBJECT_STATIC)) {
78                 group_hash_delete (group_name_hash,
79                                    group -> name, strlen (group -> name), MDL);
80         } else {
81                 group -> flags |= GROUP_OBJECT_DELETED;
82                 if (group -> group)
83                         group_dereference (&group -> group, MDL);
84         }
85
86         /* Store the group declaration in the lease file. */
87         if (writep && group_write_hook) {
88                 if (!(*group_write_hook) (group))
89                         return ISC_R_IOERROR;
90         }
91         return ISC_R_SUCCESS;
92 }
93
94 isc_result_t supersede_group (struct group_object *group, int writep)
95 {
96         struct group_object *t, *u;
97         isc_result_t status;
98
99         /* Register the group in the group name hash table,
100            so we can look it up later. */
101         if (group_name_hash) {
102                 t = (struct group_object *)0;
103                 group_hash_lookup (&t, group_name_hash,
104                         group -> name,
105                              strlen (group -> name), MDL);
106                 if (t && t != group) {
107                         /* If this isn't a dynamic entry, then we need to flag
108                            the replacement as not dynamic either - otherwise,
109                            if the dynamic entry is deleted later, the static
110                            entry will come back next time the server is stopped
111                            and restarted. */
112                         if (!(t -> flags & GROUP_OBJECT_DYNAMIC))
113                                 group -> flags |= GROUP_OBJECT_STATIC;
114
115                         /* Delete the old object if it hasn't already been
116                            deleted.  If it has already been deleted, get rid of
117                            the hash table entry.  This is a legitimate
118                            situation - a deleted static object needs to be kept
119                            around so we remember it's deleted. */
120                         if (!(t -> flags & GROUP_OBJECT_DELETED))
121                                 delete_group (t, 0);
122                         else {
123                                 group_hash_delete (group_name_hash,
124                                                    group -> name,
125                                                    strlen (group -> name),
126                                                    MDL);
127                                 group_object_dereference (&t, MDL);
128                         }
129                 }
130         } else {
131                 group_new_hash (&group_name_hash, 0, MDL);
132                 t = (struct group_object *)0;
133         }
134
135         /* Add the group to the group name hash if it's not
136            already there, and also thread it into the list of
137            dynamic groups if appropriate. */
138         if (!t) {
139                 group_hash_add (group_name_hash, group -> name,
140                                 strlen (group -> name), group, MDL);
141         }
142
143         /* Store the group declaration in the lease file. */
144         if (writep && group_write_hook) {
145                 if (!(*group_write_hook) (group))
146                         return ISC_R_IOERROR;
147         }
148         return ISC_R_SUCCESS;
149 }
150
151 int clone_group (struct group **gp, struct group *group,
152                  const char *file, int line)
153 {
154         isc_result_t status;
155         struct group *g = (struct group *)0;
156
157         /* Normally gp should contain the null pointer, but for convenience
158            it's permissible to clone a group into itself. */
159         if (*gp && *gp != group)
160                 return 0;
161         if (!group_allocate (&g, file, line))
162                 return 0;
163         if (group == *gp)
164                 *gp = (struct group *)0;
165         group_reference (gp, g, file, line);
166         g -> authoritative = group -> authoritative;
167         group_reference (&g -> next, group, file, line);
168         group_dereference (&g, file, line);
169         return 1;
170 }