"Inital commit to Gerrit"
[profile/ivi/dhcp.git] / common / memory.c
1 /* memory.c
2
3    Memory-resident database... */
4
5 /*
6  * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1995-2003 by Internet Software Consortium
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  *   Internet Systems Consortium, Inc.
22  *   950 Charter Street
23  *   Redwood City, CA 94063
24  *   <info@isc.org>
25  *   https://www.isc.org/
26  *
27  * This software has been written for Internet Systems Consortium
28  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29  * To learn more about Internet Systems Consortium, see
30  * ``https://www.isc.org/''.  To learn more about Vixie Enterprises,
31  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
32  * ``http://www.nominum.com''.
33  */
34
35 #include "dhcpd.h"
36
37 struct group *root_group;
38 group_hash_t *group_name_hash;
39 int (*group_write_hook) (struct group_object *);
40
41 isc_result_t delete_group (struct group_object *group, int writep)
42 {
43         struct group_object *d;
44
45         /* The group should exist and be hashed - if not, it's invalid. */
46         if (group_name_hash) {
47                 d = (struct group_object *)0;
48                 group_hash_lookup (&d, group_name_hash, group -> name,
49                                    strlen (group -> name), MDL);
50         } else
51                 return DHCP_R_INVALIDARG;
52         if (!d)
53                 return DHCP_R_INVALIDARG;
54
55         /* Also not okay to delete a group that's not the one in
56            the hash table. */
57         if (d != group)
58                 return DHCP_R_INVALIDARG;
59
60         /* If it's dynamic, and we're deleting it, we can just blow away the
61            hash table entry. */
62         if ((group -> flags & GROUP_OBJECT_DYNAMIC) &&
63             !(group -> flags & GROUP_OBJECT_STATIC)) {
64                 group_hash_delete (group_name_hash,
65                                    group -> name, strlen (group -> name), MDL);
66         } else {
67                 group -> flags |= GROUP_OBJECT_DELETED;
68                 if (group -> group)
69                         group_dereference (&group -> group, MDL);
70         }
71
72         /* Store the group declaration in the lease file. */
73         if (writep && group_write_hook) {
74                 if (!(*group_write_hook) (group))
75                         return ISC_R_IOERROR;
76         }
77         return ISC_R_SUCCESS;
78 }
79
80 isc_result_t supersede_group (struct group_object *group, int writep)
81 {
82         struct group_object *t;
83
84         /* Register the group in the group name hash table,
85            so we can look it up later. */
86         if (group_name_hash) {
87                 t = (struct group_object *)0;
88                 group_hash_lookup (&t, group_name_hash,
89                         group -> name,
90                              strlen (group -> name), MDL);
91                 if (t && t != group) {
92                         /* If this isn't a dynamic entry, then we need to flag
93                            the replacement as not dynamic either - otherwise,
94                            if the dynamic entry is deleted later, the static
95                            entry will come back next time the server is stopped
96                            and restarted. */
97                         if (!(t -> flags & GROUP_OBJECT_DYNAMIC))
98                                 group -> flags |= GROUP_OBJECT_STATIC;
99
100                         /* Delete the old object if it hasn't already been
101                            deleted.  If it has already been deleted, get rid of
102                            the hash table entry.  This is a legitimate
103                            situation - a deleted static object needs to be kept
104                            around so we remember it's deleted. */
105                         if (!(t -> flags & GROUP_OBJECT_DELETED))
106                                 delete_group (t, 0);
107                         else {
108                                 group_hash_delete (group_name_hash,
109                                                    group -> name,
110                                                    strlen (group -> name),
111                                                    MDL);
112                                 group_object_dereference (&t, MDL);
113                         }
114                 }
115         } else {
116                 group_new_hash(&group_name_hash, GROUP_HASH_SIZE, MDL);
117                 t = (struct group_object *)0;
118         }
119
120         /* Add the group to the group name hash if it's not
121            already there, and also thread it into the list of
122            dynamic groups if appropriate. */
123         if (!t) {
124                 group_hash_add (group_name_hash, group -> name,
125                                 strlen (group -> name), group, MDL);
126         }
127
128         /* Store the group declaration in the lease file. */
129         if (writep && group_write_hook) {
130                 if (!(*group_write_hook) (group))
131                         return ISC_R_IOERROR;
132         }
133         return ISC_R_SUCCESS;
134 }
135
136 int clone_group (struct group **gp, struct group *group,
137                  const char *file, int line)
138 {
139         struct group *g = (struct group *)0;
140
141         /* Normally gp should contain the null pointer, but for convenience
142            it's permissible to clone a group into itself. */
143         if (*gp && *gp != group)
144                 return 0;
145         if (!group_allocate (&g, file, line))
146                 return 0;
147         if (group == *gp)
148                 *gp = (struct group *)0;
149         group_reference (gp, g, file, line);
150         g -> authoritative = group -> authoritative;
151         group_reference (&g -> next, group, file, line);
152         group_dereference (&g, file, line);
153         return 1;
154 }