2 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
22 #include "capability.h"
23 #include "rb_dictionary.h"
26 static rb_dlink_list capability_indexes
= { NULL
, NULL
, 0 };
28 struct CapabilityEntry
*
29 capability_find(struct CapabilityIndex
*idx
, const char *cap
)
31 s_assert(idx
!= NULL
);
35 return rb_dictionary_retrieve(idx
->cap_dict
, cap
);
39 capability_get(struct CapabilityIndex
*idx
, const char *cap
, void **ownerdata
)
41 struct CapabilityEntry
*entry
;
43 s_assert(idx
!= NULL
);
47 entry
= rb_dictionary_retrieve(idx
->cap_dict
, cap
);
48 if (entry
!= NULL
&& !(entry
->flags
& CAP_ORPHANED
))
50 if (ownerdata
!= NULL
)
51 *ownerdata
= entry
->ownerdata
;
52 return (1 << entry
->value
);
59 capability_put(struct CapabilityIndex
*idx
, const char *cap
, void *ownerdata
)
61 struct CapabilityEntry
*entry
;
63 s_assert(idx
!= NULL
);
64 if (!idx
->highest_bit
)
67 if ((entry
= rb_dictionary_retrieve(idx
->cap_dict
, cap
)) != NULL
)
69 entry
->flags
&= ~CAP_ORPHANED
;
70 return (1 << entry
->value
);
73 entry
= rb_malloc(sizeof(struct CapabilityEntry
));
76 entry
->value
= idx
->highest_bit
;
77 entry
->ownerdata
= ownerdata
;
79 rb_dictionary_add(idx
->cap_dict
, entry
->cap
, entry
);
82 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
85 return (1 << entry
->value
);
89 capability_put_anonymous(struct CapabilityIndex
*idx
)
93 s_assert(idx
!= NULL
);
94 if (!idx
->highest_bit
)
96 value
= 1 << idx
->highest_bit
;
98 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
104 capability_orphan(struct CapabilityIndex
*idx
, const char *cap
)
106 struct CapabilityEntry
*entry
;
108 s_assert(idx
!= NULL
);
110 entry
= rb_dictionary_retrieve(idx
->cap_dict
, cap
);
113 entry
->flags
&= ~CAP_REQUIRED
;
114 entry
->flags
|= CAP_ORPHANED
;
115 entry
->ownerdata
= NULL
;
120 capability_require(struct CapabilityIndex
*idx
, const char *cap
)
122 struct CapabilityEntry
*entry
;
124 s_assert(idx
!= NULL
);
126 entry
= rb_dictionary_retrieve(idx
->cap_dict
, cap
);
128 entry
->flags
|= CAP_REQUIRED
;
132 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
134 s_assert(delem
!= NULL
);
136 rb_free(delem
->data
);
139 struct CapabilityIndex
*
140 capability_index_create(const char *name
)
142 struct CapabilityIndex
*idx
;
144 idx
= rb_malloc(sizeof(struct CapabilityIndex
));
146 idx
->cap_dict
= rb_dictionary_create(name
, strcasecmp
);
147 idx
->highest_bit
= 1;
149 rb_dlinkAdd(idx
, &idx
->node
, &capability_indexes
);
155 capability_index_destroy(struct CapabilityIndex
*idx
)
157 s_assert(idx
!= NULL
);
159 rb_dlinkDelete(&idx
->node
, &capability_indexes
);
161 rb_dictionary_destroy(idx
->cap_dict
, capability_destroy
, NULL
);
166 capability_index_list(struct CapabilityIndex
*idx
, unsigned int cap_mask
)
168 struct DictionaryIter iter
;
169 struct CapabilityEntry
*entry
;
170 static char buf
[BUFSIZE
];
174 s_assert(idx
!= NULL
);
178 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
180 if ((1 << entry
->value
) & cap_mask
)
182 tl
= sprintf(t
, "%s ", entry
->cap
);
194 capability_index_mask(struct CapabilityIndex
*idx
)
196 struct DictionaryIter iter
;
197 struct CapabilityEntry
*entry
;
198 unsigned int mask
= 0;
200 s_assert(idx
!= NULL
);
202 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
204 if (!(entry
->flags
& CAP_ORPHANED
))
205 mask
|= (1 << entry
->value
);
212 capability_index_get_required(struct CapabilityIndex
*idx
)
214 struct DictionaryIter iter
;
215 struct CapabilityEntry
*entry
;
216 unsigned int mask
= 0;
218 s_assert(idx
!= NULL
);
220 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
222 if (!(entry
->flags
& CAP_ORPHANED
) && (entry
->flags
& CAP_REQUIRED
))
223 mask
|= (1 << entry
->value
);
230 capability_index_stats(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
235 RB_DLINK_FOREACH(node
, capability_indexes
.head
)
237 struct CapabilityIndex
*idx
= node
->data
;
238 struct DictionaryIter iter
;
239 struct CapabilityEntry
*entry
;
241 snprintf(buf
, sizeof buf
, "'%s': allocated bits - %d", idx
->name
, (idx
->highest_bit
- 1));
244 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
246 snprintf(buf
, sizeof buf
, "bit %d: '%s'", entry
->value
, entry
->cap
);
250 snprintf(buf
, sizeof buf
, "'%s': remaining bits - %u", idx
->name
,
251 (unsigned int)((sizeof(unsigned int) * 8) - (idx
->highest_bit
- 1)));
255 snprintf(buf
, sizeof buf
, "%ld capability indexes", rb_dlink_list_length(&capability_indexes
));