2012-02-04 06:58:53 +01:00
|
|
|
/*
|
2016-07-31 10:18:22 +02:00
|
|
|
* Copyright (c) 2012 - 2016 William Pitcock <nenolod@dereferenced.org>.
|
2012-02-04 06:58:53 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-07-01 05:04:00 +02:00
|
|
|
#include <ircd/stdinc.h>
|
|
|
|
#include <ircd/capability.h>
|
|
|
|
#include <ircd/s_assert.h>
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-08-01 01:10:31 +02:00
|
|
|
#include <rb/format.h>
|
2016-07-31 10:18:22 +02:00
|
|
|
#include <list>
|
2012-02-05 04:16:40 +01:00
|
|
|
|
2016-08-01 01:10:31 +02:00
|
|
|
static std::list<CapabilityIndex *> capability_indexes;
|
2016-07-31 10:18:22 +02:00
|
|
|
|
|
|
|
CapabilityIndex::CapabilityIndex(std::string name_) : name(name_), highest_bit(1)
|
2016-02-27 08:41:36 +01:00
|
|
|
{
|
2016-08-01 01:10:31 +02:00
|
|
|
capability_indexes.push_back(this);
|
2016-07-31 10:18:22 +02:00
|
|
|
}
|
2016-02-27 08:41:36 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::~CapabilityIndex()
|
|
|
|
{
|
2016-08-01 01:10:31 +02:00
|
|
|
for (auto it = capability_indexes.begin(); it != capability_indexes.end(); it++)
|
|
|
|
{
|
|
|
|
if (*it == this)
|
|
|
|
{
|
|
|
|
capability_indexes.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 08:41:36 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
std::shared_ptr<CapabilityEntry>
|
|
|
|
CapabilityIndex::find(std::string cap)
|
2012-02-04 06:58:53 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
return cap_dict[cap];
|
|
|
|
}
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
unsigned int
|
|
|
|
CapabilityIndex::get(std::string cap, void **ownerdata)
|
|
|
|
{
|
|
|
|
std::shared_ptr<CapabilityEntry> entry;
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
entry = find(cap);
|
2012-02-04 08:55:11 +01:00
|
|
|
if (entry != NULL && !(entry->flags & CAP_ORPHANED))
|
2016-02-27 06:55:43 +01:00
|
|
|
{
|
|
|
|
if (ownerdata != NULL)
|
|
|
|
*ownerdata = entry->ownerdata;
|
2012-02-04 12:23:15 +01:00
|
|
|
return (1 << entry->value);
|
2016-02-27 06:55:43 +01:00
|
|
|
}
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2012-07-03 19:16:31 +02:00
|
|
|
return 0;
|
2012-02-04 06:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::put(std::string cap, void *ownerdata)
|
2012-02-04 06:58:53 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
std::shared_ptr<CapabilityEntry> entry;
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
if (highest_bit == 0)
|
2012-02-04 12:23:15 +01:00
|
|
|
return 0xFFFFFFFF;
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
if ((entry = find(cap)) != NULL)
|
2012-02-04 06:58:53 +01:00
|
|
|
{
|
2012-02-04 08:55:11 +01:00
|
|
|
entry->flags &= ~CAP_ORPHANED;
|
2012-02-04 12:23:15 +01:00
|
|
|
return (1 << entry->value);
|
2012-02-04 06:58:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
entry = cap_dict[cap] = std::make_shared<CapabilityEntry>(cap, highest_bit, ownerdata);
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
highest_bit++;
|
|
|
|
if (highest_bit % (sizeof(unsigned int) * 8) == 0)
|
|
|
|
highest_bit = 0;
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2012-02-04 12:23:15 +01:00
|
|
|
return (1 << entry->value);
|
2012-02-04 06:58:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:03:59 +01:00
|
|
|
unsigned int
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::put_anonymous()
|
2012-12-18 17:03:59 +01:00
|
|
|
{
|
|
|
|
unsigned int value;
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
if (!highest_bit)
|
2012-12-18 17:03:59 +01:00
|
|
|
return 0xFFFFFFFF;
|
2016-07-31 10:18:22 +02:00
|
|
|
|
|
|
|
value = 1 << highest_bit;
|
|
|
|
highest_bit++;
|
|
|
|
if (highest_bit % (sizeof(unsigned int) * 8) == 0)
|
|
|
|
highest_bit = 0;
|
|
|
|
|
2012-12-18 17:03:59 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-02-04 06:58:53 +01:00
|
|
|
void
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::orphan(std::string cap)
|
2012-02-04 06:58:53 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
std::shared_ptr<CapabilityEntry> entry;
|
2012-02-04 06:58:53 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
entry = cap_dict[cap];
|
2012-02-04 06:58:53 +01:00
|
|
|
if (entry != NULL)
|
2012-02-04 09:00:33 +01:00
|
|
|
{
|
|
|
|
entry->flags &= ~CAP_REQUIRED;
|
2012-02-04 08:55:11 +01:00
|
|
|
entry->flags |= CAP_ORPHANED;
|
2016-02-29 02:15:04 +01:00
|
|
|
entry->ownerdata = NULL;
|
2012-02-04 09:00:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::require(std::string cap)
|
2012-02-04 09:00:33 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
std::shared_ptr<CapabilityEntry> entry;
|
2012-02-04 09:00:33 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
entry = cap_dict[cap];
|
2012-02-04 09:00:33 +01:00
|
|
|
if (entry != NULL)
|
|
|
|
entry->flags |= CAP_REQUIRED;
|
2012-02-04 06:58:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 07:36:42 +01:00
|
|
|
const char *
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::list(unsigned int cap_mask)
|
2012-02-04 07:36:42 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
static std::string buf;
|
2012-02-04 07:36:42 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
buf.clear();
|
2012-02-04 07:36:42 +01:00
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
for (auto it = cap_dict.begin(); it != cap_dict.end(); it++)
|
2012-02-04 07:36:42 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
auto entry = it->second;
|
|
|
|
|
2012-07-03 19:15:25 +02:00
|
|
|
if ((1 << entry->value) & cap_mask)
|
2012-02-04 07:36:42 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
buf += entry->cap + " ";
|
2012-02-04 07:36:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
return buf.c_str();
|
2012-02-04 07:36:42 +01:00
|
|
|
}
|
2012-02-04 07:39:53 +01:00
|
|
|
|
|
|
|
unsigned int
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::mask()
|
2012-02-04 07:39:53 +01:00
|
|
|
{
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
for (auto it = cap_dict.begin(); it != cap_dict.end(); it++)
|
2012-02-04 07:39:53 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
auto entry = it->second;
|
2012-02-04 08:58:07 +01:00
|
|
|
if (!(entry->flags & CAP_ORPHANED))
|
2012-02-04 12:23:15 +01:00
|
|
|
mask |= (1 << entry->value);
|
2012-02-04 07:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2012-02-04 09:00:33 +01:00
|
|
|
|
|
|
|
unsigned int
|
2016-07-31 10:18:22 +02:00
|
|
|
CapabilityIndex::required()
|
2012-02-04 09:00:33 +01:00
|
|
|
{
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
2016-07-31 10:18:22 +02:00
|
|
|
for (auto it = cap_dict.begin(); it != cap_dict.end(); it++)
|
2012-02-04 09:00:33 +01:00
|
|
|
{
|
2016-07-31 10:18:22 +02:00
|
|
|
auto entry = it->second;
|
2012-02-04 09:00:33 +01:00
|
|
|
if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
|
2012-02-04 12:23:15 +01:00
|
|
|
mask |= (1 << entry->value);
|
2012-02-04 09:00:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2012-02-05 04:27:43 +01:00
|
|
|
|
|
|
|
void
|
2016-08-01 01:10:31 +02:00
|
|
|
CapabilityIndex::stats(void (*cb)(std::string &line, void *privdata), void *privdata)
|
2012-02-05 04:27:43 +01:00
|
|
|
{
|
2016-08-01 01:10:31 +02:00
|
|
|
std::string out = fmt::format("'{0}': {1} allocated:", name, highest_bit - 1);
|
2012-02-05 04:27:43 +01:00
|
|
|
|
2016-08-01 01:10:31 +02:00
|
|
|
for (auto it = cap_dict.begin(); it != cap_dict.end(); it++)
|
|
|
|
out += fmt::format(" {0}<{1}>", it->first, it->second->value);
|
2012-02-05 04:27:43 +01:00
|
|
|
|
2016-08-01 01:10:31 +02:00
|
|
|
cb(out, privdata);
|
|
|
|
}
|
2012-02-05 04:27:43 +01:00
|
|
|
|
2016-08-01 01:10:31 +02:00
|
|
|
void
|
|
|
|
capability_stats(void (*cb)(std::string &line, void *privdata), void *privdata)
|
|
|
|
{
|
|
|
|
for (auto it = capability_indexes.begin(); it != capability_indexes.end(); it++)
|
|
|
|
(*it)->stats(cb, privdata);
|
2012-02-05 04:27:43 +01:00
|
|
|
}
|