0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-28 00:14:07 +01:00

Merge pull request #197 from jevolk/mapi3

Module API Version 3 + ratbox package
This commit is contained in:
William Pitcock 2016-06-28 22:19:57 -05:00 committed by GitHub
commit 7f6e873ec7
16 changed files with 1193 additions and 706 deletions

View file

@ -61,7 +61,7 @@ extern const char *creation;
extern const char *generation;
extern const char *infotext[];
extern const char *serno;
extern const unsigned long int datecode;
extern const time_t datecode;
extern const char *ircd_version;
extern const char *logFileName;
extern const char *pidFileName;

View file

@ -63,4 +63,7 @@ extern void report_operspy(struct Client *, const char *, const char *);
extern const char *smalldate(time_t);
extern void ilog_error(const char *);
void vslog(ilogfile dest, unsigned int snomask, const char *fmt, va_list ap);
void slog(ilogfile dest, unsigned int snomask, const char *fmt, ...) AFP(3, 4);
#endif

View file

@ -38,7 +38,8 @@
struct module
{
char *name;
const char *name;
const char *path;
const char *version;
const char *description;
lt_dlhandle address;
@ -53,6 +54,7 @@ struct module
#define MAPI_V1 (MAPI_MAGIC_HDR | 0x1)
#define MAPI_V2 (MAPI_MAGIC_HDR | 0x2)
#define MAPI_V3 (MAPI_MAGIC_HDR | 0x3)
#define MAPI_MAGIC(x) ((x) & 0xffff0000)
#define MAPI_VERSION(x) ((x) & 0x0000ffff)
@ -109,7 +111,7 @@ struct mapi_mheader_av2
mapi_cap_list_av2 *mapi_cap_list; /* List of CAPs to add */
const char *mapi_module_version; /* Module's version (freeform), replaced with ircd version if NULL */
const char *mapi_module_description; /* Module's description (freeform) */
unsigned long int mapi_datecode; /* Unix timestamp of module's build */
time_t mapi_datecode; /* Unix timestamp of module's build */
};
#define DECLARE_MODULE_AV1(name, reg, unreg, cl, hl, hfnlist, v) \
@ -118,6 +120,62 @@ struct mapi_mheader_av2
#define DECLARE_MODULE_AV2(name, reg, unreg, cl, hl, hfnlist, caplist, v, desc) \
struct mapi_mheader_av2 _mheader = { MAPI_V2, reg, unreg, cl, hl, hfnlist, caplist, v, desc, DATECODE}
/***
Version 3 modules utilize a flexible key/value vector.
Example:
DECLARE_MODULE_AV3
(
MOD_ATTR { "name", "mymodule" },
MOD_ATTR { "mtab", &message_foo },
MOD_ATTR { "mtab", &message_unfoo },
MOD_ATTR { "init", modinitfunc },
MOD_ATTR { "hook", MOD_HOOK { "myhook", &id } },
MOD_ATTR { "hookfn", MOD_HOOKFN { "myhook", hookfun } },
)
Notes:
- Multiple keys with the same name will have different behavior depending on the logic for that key.
- On load, the order in which keys are specified is the order they will be evaluated (top to bottom).
- On unload, the evaluation is the REVERSE order (bottom to top).
- If an init function returns false, or other error occurs, no further keys are evaluated and the
unload rolls back from that point.
***/
struct mapi_av3_attr
{
#define MAPI_V3_KEY_MAXLEN 16 /* Maximum length for a key string */
const char *key;
union { const void *cvalue; void *value; int (*init)(void); void (*fini)(void); };
};
struct mapi_mheader_av3
{
int mapi_version; // Module API version
struct mapi_av3_attr **attrs; // A vector of attributes, NULL terminated
};
#define MOD_ATTR &(struct mapi_av3_attr)
#define MOD_HOOK &(mapi_hlist_av1)
#define MOD_HOOKFN &(mapi_hfn_list_av1)
#define DECLARE_MODULE_AV3(...) \
struct mapi_mheader_av3 _mheader = \
{ \
MAPI_V3, (struct mapi_av3_attr *[]) \
{ \
MOD_ATTR { "time", DATECODE }, \
__VA_ARGS__, \
NULL \
} \
};
// Prefixes your slog() message with module info
void module_log(struct module *mod, const char *fmt, ...) AFP(2, 3);
/* add a path */
void mod_add_path(const char *path);
void mod_clear_paths(void);

View file

@ -70,23 +70,6 @@ char *alloca ();
#endif
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# ifndef HAVE__BOOL
# ifdef __cplusplus
typedef bool _Bool;
# else
# define _Bool signed char
# endif
# endif
# define bool _Bool
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
#endif
#include <stdio.h>
#include <assert.h>
#include <stdio.h>

View file

@ -163,34 +163,39 @@ close_logfiles(void)
}
}
void
ilog(ilogfile dest, const char *format, ...)
{
FILE *logfile = *log_table[dest].logfile;
char buf[BUFSIZE];
char buf2[BUFSIZE];
va_list args;
if(logfile == NULL)
static void
log_va(const ilogfile dest, const unsigned int snomask, const char *const fmt, va_list ap)
{
FILE *const logfile = *log_table[dest].logfile;
if(!logfile && !snomask)
return;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
char buf[BUFSIZE];
vsnprintf(buf, sizeof(buf), fmt, ap);
snprintf(buf2, sizeof(buf2), "%s %s\n",
smalldate(rb_current_time()), buf);
if(snomask)
sendto_realops_snomask(snomask, L_ALL, "%s", buf);
if(fputs(buf2, logfile) < 0)
if(fprintf(logfile, "%s %s\n", smalldate(rb_current_time()), buf) < 0)
{
fclose(logfile);
*log_table[dest].logfile = NULL;
return;
} else {
fflush(logfile);
}
fflush(logfile);
}
void
ilog(ilogfile dest, const char *format, ...)
{
va_list args;
va_start(args, format);
log_va(dest, 0, format, args);
va_end(args);
}
static void
_iprint(const char *domain, const char *buf)
{
@ -309,3 +314,20 @@ ilog_error(const char *error)
sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)",
error, e, errstr);
}
void
vslog(const ilogfile dest, const unsigned int snomask, const char *const fmt, va_list ap)
{
log_va(dest, snomask, fmt, ap);
}
void
slog(const ilogfile dest, const unsigned int snomask, const char *const fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vslog(dest, snomask, fmt, ap);
va_end(ap);
}

View file

@ -276,8 +276,564 @@ load_one_module(const char *path, int origin, bool coremodule)
return false;
}
static char unknown_ver[] = "<unknown>";
static char unknown_description[] = "<none>";
void module_log(struct module *const mod,
const char *const fmt,
...)
{
va_list ap;
va_start(ap, fmt);
char buf[BUFSIZE];
vsnprintf(buf, sizeof(buf), fmt, ap),
slog(L_MAIN, SNO_GENERAL, "Module %s: %s", mod->name, buf);
va_end(ap);
}
// ******************************************************************************
// INTERNAL API STACK
// driven by load_a_module() / unload_one_module() (bottom)
static
bool init_module_v1(struct module *const mod)
{
struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)mod->mapi_header;
if(mheader->mapi_register && (mheader->mapi_register() == -1))
return false;
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_add_cmd(*m);
}
if(mheader->mapi_hook_list)
{
mapi_hlist_av1 *m;
for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
*m->hapi_id = register_hook(m->hapi_name);
}
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
add_hook(m->hapi_name, m->fn);
}
mod->version = mheader->mapi_module_version;
return true;
}
static
void fini_module_v1(struct module *const mod)
{
struct mapi_mheader_av1 *mheader = mod->mapi_header;
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_del_cmd(*m);
}
/* hook events are never removed, we simply lose the
* ability to call them --fl
*/
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
remove_hook(m->hapi_name, m->fn);
}
if(mheader->mapi_unregister)
mheader->mapi_unregister();
}
static
bool init_module__cap(struct module *const mod,
mapi_cap_list_av2 *const m)
{
struct CapabilityIndex *idx;
switch(m->cap_index)
{
case MAPI_CAP_CLIENT: idx = cli_capindex; break;
case MAPI_CAP_SERVER: idx = serv_capindex; break;
default:
slog(L_MAIN, SNO_GENERAL,
"Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
m->cap_index,
m->cap_name,
mod->name);
return false;
}
if(m->cap_id)
{
*(m->cap_id) = capability_put(idx, m->cap_name, m->cap_ownerdata);
sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * ADD :%s", me.name, m->cap_name);
}
return true;
}
static
void fini_module__cap(struct module *const mod,
mapi_cap_list_av2 *const m)
{
struct CapabilityIndex *idx;
switch(m->cap_index)
{
case MAPI_CAP_CLIENT: idx = cli_capindex; break;
case MAPI_CAP_SERVER: idx = serv_capindex; break;
default:
slog(L_MAIN, SNO_GENERAL,
"Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
m->cap_index,
m->cap_name,
mod->name);
return;
}
if(m->cap_id)
{
capability_orphan(idx, m->cap_name);
sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", me.name, m->cap_name);
}
}
static
bool init_module_v2(struct module *const mod)
{
struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)mod->mapi_header;
if(mheader->mapi_register && (mheader->mapi_register() == -1))
return false;
/* Basic date code checks
*
* Don't make them fatal, but do complain about differences within a certain time frame.
* Later on if there are major API changes we can add fatal checks.
* -- Elizafox
*/
if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0)
{
long int delta = datecode - mheader->mapi_datecode;
if (delta > MOD_WARN_DELTA)
{
delta /= 86400;
iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
mod->name, delta);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s build date is out of sync with ircd build date by %ld days, expect problems",
mod->name, delta);
}
}
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_add_cmd(*m);
}
if(mheader->mapi_hook_list)
{
mapi_hlist_av1 *m;
for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
*m->hapi_id = register_hook(m->hapi_name);
}
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
add_hook(m->hapi_name, m->fn);
}
/* New in MAPI v2 - version replacement */
mod->version = mheader->mapi_module_version? mheader->mapi_module_version : ircd_version;
mod->description = mheader->mapi_module_description;
if(mheader->mapi_cap_list)
{
mapi_cap_list_av2 *m;
for (m = mheader->mapi_cap_list; m->cap_name; ++m)
if(!init_module__cap(mod, m))
return false;
}
return true;
}
static
void fini_module_v2(struct module *const mod)
{
struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)mod->mapi_header;
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_del_cmd(*m);
}
/* hook events are never removed, we simply lose the
* ability to call them --fl
*/
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
remove_hook(m->hapi_name, m->fn);
}
if(mheader->mapi_unregister)
mheader->mapi_unregister();
if(mheader->mapi_cap_list)
{
mapi_cap_list_av2 *m;
for (m = mheader->mapi_cap_list; m->cap_name; ++m)
fini_module__cap(mod, m);
}
}
static
bool require_value(struct module *const mod,
struct mapi_av3_attr *const attr)
{
if(!attr->value)
{
module_log(mod, "key[%s] requires non-null value", attr->key);
return false;
}
return true;
}
static
bool init_v3_module_attr(struct module *const mod,
struct mapi_mheader_av3 *const h,
struct mapi_av3_attr *const attr)
{
if(EmptyString(attr->key))
{
module_log(mod, "Skipping a NULL or empty key (ignoring)");
return true;
}
if(strncmp(attr->key, "time", MAPI_V3_KEY_MAXLEN) == 0)
{
//TODO: elizafox's warning
return true;
}
if(strncmp(attr->key, "name", MAPI_V3_KEY_MAXLEN) == 0)
{
module_log(mod, "Changing the display unsupported (ignoring)");
return true;
}
if(strncmp(attr->key, "mtab", MAPI_V3_KEY_MAXLEN) == 0)
{
if(!require_value(mod, attr))
return false;
struct Message *const v = attr->value;
mod_add_cmd(v);
return true;
}
if(strncmp(attr->key, "hook", MAPI_V3_KEY_MAXLEN) == 0)
{
if(!require_value(mod, attr))
return false;
mapi_hlist_av1 *const v = attr->value;
*v->hapi_id = register_hook(v->hapi_name);
return true;
}
if(strncmp(attr->key, "hookfn", MAPI_V3_KEY_MAXLEN) == 0)
{
if(!require_value(mod, attr))
return false;
mapi_hfn_list_av1 *const v = attr->value;
add_hook(v->hapi_name, v->fn);
return true;
}
if(strncmp(attr->key, "cap", MAPI_V3_KEY_MAXLEN) == 0)
{
if(!require_value(mod, attr))
return false;
mapi_cap_list_av2 *const v = attr->value;
return init_module__cap(mod, v);
}
if(strncmp(attr->key, "description", MAPI_V3_KEY_MAXLEN) == 0)
{
mod->description = (const char *)attr->value;
return true;
}
if(strncmp(attr->key, "version", MAPI_V3_KEY_MAXLEN) == 0)
{
mod->version = (const char *)attr->value;
return true;
}
if(strncmp(attr->key, "init", MAPI_V3_KEY_MAXLEN) == 0)
return attr->init();
// Ignore fini on load
if(strncmp(attr->key, "fini", MAPI_V3_KEY_MAXLEN) == 0)
return true;
// TODO: analysis.
module_log(mod, "Unknown key [%s]. Host version does not yet support unknown keys.", attr->key);
return false;
}
static
void fini_v3_module_attr(struct module *const mod,
struct mapi_mheader_av3 *const h,
struct mapi_av3_attr *const attr)
{
if(EmptyString(attr->key))
{
module_log(mod, "Skipping a NULL or empty key (ignoring)");
return;
}
if(strncmp(attr->key, "mtab", MAPI_V3_KEY_MAXLEN) == 0)
{
if(attr->value)
mod_del_cmd(attr->value);
return;
}
if(strncmp(attr->key, "hook", MAPI_V3_KEY_MAXLEN) == 0)
{
// ???
return;
}
if(strncmp(attr->key, "hookfn", MAPI_V3_KEY_MAXLEN) == 0)
{
if(!attr->value)
return;
mapi_hfn_list_av1 *const v = attr->value;
remove_hook(v->hapi_name, v->fn);
return;
}
if(strncmp(attr->key, "cap", MAPI_V3_KEY_MAXLEN) == 0)
{
if(attr->value)
fini_module__cap(mod, attr->value);
return;
}
if(strncmp(attr->key, "fini", MAPI_V3_KEY_MAXLEN) == 0)
{
if(attr->value)
attr->fini();
return;
}
}
static
void fini_module_v3(struct module *const mod)
{
struct mapi_mheader_av3 *const h = (struct mapi_mheader_av3 *)mod->mapi_header;
if(!h->attrs)
{
module_log(mod, "(unload) has no attribute vector!");
return;
}
ssize_t i = -1;
struct mapi_av3_attr *attr;
for(attr = h->attrs[++i]; attr; attr = h->attrs[++i]);
for(attr = h->attrs[--i]; i > -1; attr = h->attrs[--i])
fini_v3_module_attr(mod, h, attr);
}
static
bool init_module_v3(struct module *const mod)
{
struct mapi_mheader_av3 *const h = (struct mapi_mheader_av3 *)mod->mapi_header;
if(!h->attrs)
{
module_log(mod, "has no attribute vector!");
return false;
}
size_t i = 0;
for(struct mapi_av3_attr *attr = h->attrs[i]; attr; attr = h->attrs[++i])
{
if(!init_v3_module_attr(mod, h, attr))
{
h->attrs[i] = NULL;
fini_module_v3(mod);
return false;
}
}
return true;
}
static
bool init_module(struct module *const mod)
{
mod->mapi_header = lt_dlsym(mod->address, "_mheader");
if(!mod->mapi_header)
{
module_log(mod, "has no MAPI header. (%s)", lt_dlerror());
return false;
}
const int version_magic = *(const int *)mod->mapi_header;
if(MAPI_MAGIC(version_magic) != MAPI_MAGIC_HDR)
{
module_log(mod, "has an invalid header (magic is [%x] mismatches [%x]).",
MAPI_MAGIC(version_magic),
MAPI_MAGIC_HDR);
return false;
}
mod->mapi_version = MAPI_VERSION(version_magic);
switch(mod->mapi_version)
{
case 1: return init_module_v1(mod);
case 2: return init_module_v2(mod);
case 3: return init_module_v3(mod);
default:
module_log(mod, "has unknown/unsupported MAPI version %d.", mod->mapi_version);
return false;
}
}
static
const char *reflect_origin(const int origin)
{
switch(origin)
{
case MAPI_ORIGIN_EXTENSION: return "extension";
case MAPI_ORIGIN_CORE: return "core";
default: return "unknown";
}
}
static
void free_module(struct module **ptr)
{
if(!ptr || !*ptr)
return;
struct module *mod = *ptr;
if(mod->name)
rb_free(mod->name);
if(mod->path)
rb_free(mod->path);
rb_free(mod);
}
static
void close_handle(lt_dlhandle *handle)
{
if(handle && *handle)
lt_dlclose(*handle);
}
/*
* load_a_module()
*
* inputs - path name of module, bool to notice, int of origin, bool if core
* output - false if error true if success
* side effects - loads a module if successful
*/
bool
load_a_module(const char *path, bool warn, int origin, bool core)
{
char *const name RB_AUTO_PTR = rb_basename(path);
/* Trim off the ending for the display name if we have to */
char *c;
if((c = rb_strcasestr(name, LT_MODULE_EXT)) != NULL)
*c = '\0';
lt_dlhandle handle RB_UNIQUE_PTR(close_handle) = lt_dlopenext(path);
if(handle == NULL)
{
slog(L_MAIN, SNO_GENERAL, "Error loading module %s: %s", name, lt_dlerror());
return false;
}
struct module *mod RB_UNIQUE_PTR(free_module) = rb_malloc(sizeof(struct module));
mod->name = rb_strdup(name);
mod->path = rb_strdup(path);
mod->address = handle;
mod->origin = origin;
mod->core = core;
if(!init_module(mod))
{
slog(L_MAIN, SNO_GENERAL, "Loading module %s aborted.", name);
return false;
}
if(!mod->version)
mod->version = "<unknown>";
if(!mod->description)
mod->description = "<no description>";
if(warn)
slog(L_MAIN, SNO_GENERAL,
"Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded from [%s] to %p",
name,
mod->version,
mod->mapi_version,
reflect_origin(mod->origin),
mod->description,
mod->path,
(const void *)mod->address);
// NULL the acquired resources after commitment to list ownership
rb_dlinkAdd(mod, &mod->node, &module_list);
mod = NULL;
handle = NULL;
return true;
}
/* unload_one_module()
*
@ -290,7 +846,6 @@ bool
unload_one_module(const char *name, bool warn)
{
struct module *mod;
if((mod = findmodule_byname(name)) == NULL)
return false;
@ -310,361 +865,24 @@ unload_one_module(const char *name, bool warn)
/* Left the comment in but the code isn't here any more -larne */
switch (mod->mapi_version)
{
case 1:
{
struct mapi_mheader_av1 *mheader = mod->mapi_header;
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_del_cmd(*m);
}
/* hook events are never removed, we simply lose the
* ability to call them --fl
*/
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
remove_hook(m->hapi_name, m->fn);
}
if(mheader->mapi_unregister)
mheader->mapi_unregister();
break;
}
case 2:
{
struct mapi_mheader_av2 *mheader = mod->mapi_header;
/* XXX duplicate code :( */
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_del_cmd(*m);
}
/* hook events are never removed, we simply lose the
* ability to call them --fl
*/
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
remove_hook(m->hapi_name, m->fn);
}
if(mheader->mapi_unregister)
mheader->mapi_unregister();
if(mheader->mapi_cap_list)
{
mapi_cap_list_av2 *m;
for (m = mheader->mapi_cap_list; m->cap_name; ++m)
{
struct CapabilityIndex *idx;
switch (m->cap_index)
{
case MAPI_CAP_CLIENT:
idx = cli_capindex;
break;
case MAPI_CAP_SERVER:
idx = serv_capindex;
break;
default:
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
m->cap_index, m->cap_name, mod->name);
ilog(L_MAIN,
"Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
m->cap_index, m->cap_name, mod->name);
continue;
}
if (m->cap_id != NULL)
{
capability_orphan(idx, m->cap_name);
sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", me.name, m->cap_name);
}
}
}
break;
}
default:
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unknown/unsupported MAPI version %d when unloading %s!",
mod->mapi_version, mod->name);
ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
mod->mapi_version, mod->name);
case 1: fini_module_v1(mod); break;
case 2: fini_module_v2(mod); break;
case 3: fini_module_v3(mod); break;
default:
slog(L_MAIN, SNO_GENERAL,
"Unknown/unsupported MAPI version %d when unloading %s!",
mod->mapi_version,
mod->name);
break;
}
lt_dlclose(mod->address);
rb_dlinkDelete(&mod->node, &module_list);
rb_free(mod->name);
rb_free(mod);
close_handle(&mod->address);
if(warn)
{
ilog(L_MAIN, "Module %s unloaded", name);
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name);
}
slog(L_MAIN, SNO_GENERAL, "Module %s unloaded", name);
return true;
}
/*
* load_a_module()
*
* inputs - path name of module, bool to notice, int of origin, bool if core
* output - false if error true if success
* side effects - loads a module if successful
*/
bool
load_a_module(const char *path, bool warn, int origin, bool core)
{
struct module *mod;
lt_dlhandle tmpptr;
char *mod_displayname, *c;
const char *ver, *description = NULL;
size_t module_ext_len = strlen(LT_MODULE_EXT);
int *mapi_version;
mod_displayname = rb_basename(path);
/* Trim off the ending for the display name if we have to */
if((c = rb_strcasestr(mod_displayname, LT_MODULE_EXT)) != NULL)
*c = '\0';
tmpptr = lt_dlopenext(path);
if(tmpptr == NULL)
{
const char *err = lt_dlerror();
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Error loading module %s: %s", mod_displayname, err);
ilog(L_MAIN, "Error loading module %s: %s", mod_displayname, err);
rb_free(mod_displayname);
return false;
}
/*
* _mheader is actually a struct mapi_mheader_*, but mapi_version
* is always the first member of this structure, so we treate it
* as a single int in order to determine the API version.
* -larne.
*/
mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader");
if((mapi_version == NULL
&& (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL)
|| MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR)
{
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Data format error: module %s has no MAPI header.",
mod_displayname);
ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_displayname);
(void) lt_dlclose(tmpptr);
rb_free(mod_displayname);
return false;
}
switch (MAPI_VERSION(*mapi_version))
{
case 1:
{
struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */
if(mheader->mapi_register && (mheader->mapi_register() == -1))
{
ilog(L_MAIN, "Module %s indicated failure during load.",
mod_displayname);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s indicated failure during load.",
mod_displayname);
lt_dlclose(tmpptr);
rb_free(mod_displayname);
return false;
}
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_add_cmd(*m);
}
if(mheader->mapi_hook_list)
{
mapi_hlist_av1 *m;
for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
*m->hapi_id = register_hook(m->hapi_name);
}
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
add_hook(m->hapi_name, m->fn);
}
ver = mheader->mapi_module_version;
break;
}
case 2:
{
struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version; /* see above */
/* XXX duplicated code :( */
if(mheader->mapi_register && (mheader->mapi_register() == -1))
{
ilog(L_MAIN, "Module %s indicated failure during load.",
mod_displayname);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s indicated failure during load.",
mod_displayname);
lt_dlclose(tmpptr);
rb_free(mod_displayname);
return false;
}
/* Basic date code checks
*
* Don't make them fatal, but do complain about differences within a certain time frame.
* Later on if there are major API changes we can add fatal checks.
* -- Elizafox
*/
if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0)
{
long int delta = datecode - mheader->mapi_datecode;
if (delta > MOD_WARN_DELTA)
{
delta /= 86400;
iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
mod_displayname, delta);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s build date is out of sync with ircd build date by %ld days, expect problems",
mod_displayname, delta);
}
}
if(mheader->mapi_command_list)
{
struct Message **m;
for (m = mheader->mapi_command_list; *m; ++m)
mod_add_cmd(*m);
}
if(mheader->mapi_hook_list)
{
mapi_hlist_av1 *m;
for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
*m->hapi_id = register_hook(m->hapi_name);
}
if(mheader->mapi_hfn_list)
{
mapi_hfn_list_av1 *m;
for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
add_hook(m->hapi_name, m->fn);
}
/* New in MAPI v2 - version replacement */
ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version;
description = mheader->mapi_module_description;
if(mheader->mapi_cap_list)
{
mapi_cap_list_av2 *m;
for (m = mheader->mapi_cap_list; m->cap_name; ++m)
{
struct CapabilityIndex *idx;
int result;
switch (m->cap_index)
{
case MAPI_CAP_CLIENT:
idx = cli_capindex;
break;
case MAPI_CAP_SERVER:
idx = serv_capindex;
break;
default:
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
m->cap_index, m->cap_name, mod_displayname);
ilog(L_MAIN,
"Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
m->cap_index, m->cap_name, mod_displayname);
continue;
}
result = capability_put(idx, m->cap_name, m->cap_ownerdata);
if (m->cap_id != NULL)
{
*(m->cap_id) = result;
sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * ADD :%s", me.name, m->cap_name);
}
}
}
}
break;
default:
ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.",
mod_displayname, MAPI_VERSION(*mapi_version));
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s has unknown/unsupported MAPI version %d.",
mod_displayname, *mapi_version);
lt_dlclose(tmpptr);
rb_free(mod_displayname);
return false;
}
if(ver == NULL)
ver = unknown_ver;
if(description == NULL)
description = unknown_description;
mod = rb_malloc(sizeof(struct module));
mod->address = tmpptr;
mod->version = ver;
mod->description = description;
mod->core = core;
mod->name = rb_strdup(mod_displayname);
mod->mapi_header = mapi_version;
mod->mapi_version = MAPI_VERSION(*mapi_version);
mod->origin = origin;
rb_dlinkAdd(mod, &mod->node, &module_list);
if(warn)
{
const char *o;
switch (origin)
{
case MAPI_ORIGIN_EXTENSION:
o = "extension";
break;
case MAPI_ORIGIN_CORE:
o = "core";
break;
default:
o = "unknown";
break;
}
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description,
(void *) tmpptr);
ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, (void *) tmpptr);
}
rb_free(mod_displayname);
// free after the unload message in case *name came from the mod struct.
free_module(&mod);
return true;
}

View file

@ -57,7 +57,7 @@ const char *generation = "$generation";
const char *creation = "$creation";
const char *ircd_version = PATCHLEVEL;
const char *serno = SERNO;
const unsigned long int datecode = DATECODE;
const time_t datecode = DATECODE;
const char *infotext[] =
{

View file

@ -104,7 +104,7 @@ AC_TYPE_UID_T
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h netinet/tcp.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h])
AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h netinet/tcp.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h execinfo.h])
AC_HEADER_TIME
dnl Networking Functions

View file

@ -33,7 +33,12 @@ typedef struct rb_dictionary_iter rb_dictionary_iter;
struct rb_dictionary;
// This comparator could be based on a union of function types emulating a
// quasi-template, shutting up a lot of warnings. For now it gets The Treatment.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
typedef int (*DCF)(/* const void *a, const void *b */);
#pragma GCC diagnostic pop
struct rb_dictionary_element
{

340
librb/include/rb_dlink.h Normal file
View file

@ -0,0 +1,340 @@
/*
* ircd-ratbox: A slightly useful ircd.
* tools.h: Header for the various tool functions.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/
#ifndef RB_LIB_H
#error "Do not use rb_dlink.h directly"
#endif
#ifndef __DLINK_H__
#define __DLINK_H__
typedef struct rb_dlink_node
{
void *data;
struct rb_dlink_node *prev;
struct rb_dlink_node *next;
}
rb_dlink_node;
typedef struct rb_dlink_list
{
rb_dlink_node *head;
rb_dlink_node *tail;
unsigned long length;
}
rb_dlink_list;
// Utils
#define rb_dlink_list_length(list) (list)->length
// XXX I'd like to remove this interface, it's not safe.
rb_dlink_node *rb_make_rb_dlink_node(void);
void rb_free_rb_dlink_node(rb_dlink_node *lp);
void rb_init_rb_dlink_nodes(size_t dh_size);
#define rb_dlinkAddAlloc(data, list) rb_dlinkAdd(data, rb_make_rb_dlink_node(), list)
#define rb_dlinkAddTailAlloc(data, list) rb_dlinkAddTail(data, rb_make_rb_dlink_node(), list)
#define rb_dlinkDestroy(node, list) do { rb_dlinkDelete(node, list); rb_free_rb_dlink_node(node); } while(0)
// Vintage
static void rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list);
static void rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list);
static void rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list);
static rb_dlink_node *rb_dlinkFindDelete(void *data, rb_dlink_list *list);
static int rb_dlinkFindDestroy(void *data, rb_dlink_list *list);
static rb_dlink_node *rb_dlinkFind(void *data, rb_dlink_list *list);
static void rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist);
static void rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list);
static void rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list);
static void rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to);
/* This macros are basically swiped from the linux kernel
* they are simple yet effective
*/
// Usage: rb_dlink_node *n; RB_DLINK_FOREACH(n, list.head) { ... }
#define RB_DLINK_FOREACH(node, head) for(node = (head); node != NULL; node = node->next)
#define RB_DLINK_FOREACH_PREV(node, head) for(node = (head); node != NULL; node = node->prev)
// Allows for modifying the list during iteration.
// Usage: rb_dlink_node *cur, *nxt; RB_DLINK_FOREACH_SAFE(cur, nxt, list.head) { ... }
#define RB_DLINK_FOREACH_SAFE(cur, nxt, head) \
for(cur = (head), nxt = cur? cur->next : NULL; cur != NULL; cur = nxt, nxt = cur? cur->next : NULL)
#define RB_DLINK_FOREACH_PREV_SAFE(cur, pre, head) \
for(cur = (head), pre = cur? cur->prev : NULL; cur != NULL; cur = pre, pre = cur? cur->prev : NULL)
/*
* dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
* which is mine.
* -- adrian
*/
static inline void
rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist)
{
/* Assumption: If m->next == NULL, then list->tail == m
* and: If m->prev == NULL, then list->head == m
*/
assert(m != NULL);
assert(oldlist != NULL);
assert(newlist != NULL);
if(m->next)
m->next->prev = m->prev;
else
oldlist->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
oldlist->head = m->next;
m->prev = NULL;
m->next = newlist->head;
if(newlist->head != NULL)
newlist->head->prev = m;
else if(newlist->tail == NULL)
newlist->tail = m;
newlist->head = m;
oldlist->length--;
newlist->length++;
}
static inline void
rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(data != NULL);
assert(m != NULL);
assert(list != NULL);
m->data = data;
m->prev = NULL;
m->next = list->head;
/* Assumption: If list->tail != NULL, list->head != NULL */
if(list->head != NULL)
list->head->prev = m;
else if(list->tail == NULL)
list->tail = m;
list->head = m;
list->length++;
}
static inline void
rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(b != NULL);
assert(data != NULL);
assert(m != NULL);
assert(list != NULL);
/* Shortcut - if its the first one, call rb_dlinkAdd only */
if(b == list->head)
{
rb_dlinkAdd(data, m, list);
}
else
{
m->data = data;
b->prev->next = m;
m->prev = b->prev;
b->prev = m;
m->next = b;
list->length++;
}
}
static inline void
rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list)
{
if(list->tail == m)
return;
/* From here assume that m->next != NULL as that can only
* be at the tail and assume that the node is on the list
*/
m->next->prev = m->prev;
if(m->prev != NULL)
m->prev->next = m->next;
else
list->head = m->next;
list->tail->next = m;
m->prev = list->tail;
m->next = NULL;
list->tail = m;
}
static inline void
rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(m != NULL);
assert(list != NULL);
assert(data != NULL);
m->data = data;
m->next = NULL;
m->prev = list->tail;
/* Assumption: If list->tail != NULL, list->head != NULL */
if(list->tail != NULL)
list->tail->next = m;
else if(list->head == NULL)
list->head = m;
list->tail = m;
list->length++;
}
/* Execution profiles show that this function is called the most
* often of all non-spontaneous functions. So it had better be
* efficient. */
static inline void
rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list)
{
assert(m != NULL);
assert(list != NULL);
/* Assumption: If m->next == NULL, then list->tail == m
* and: If m->prev == NULL, then list->head == m
*/
if(m->next)
m->next->prev = m->prev;
else
list->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
list->head = m->next;
m->next = m->prev = NULL;
list->length--;
}
static inline rb_dlink_node *
rb_dlinkFindDelete(void *data, rb_dlink_list *list)
{
rb_dlink_node *m;
assert(list != NULL);
assert(data != NULL);
RB_DLINK_FOREACH(m, list->head)
{
if(m->data != data)
continue;
if(m->next)
m->next->prev = m->prev;
else
list->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
list->head = m->next;
m->next = m->prev = NULL;
list->length--;
return m;
}
return NULL;
}
static inline int
rb_dlinkFindDestroy(void *data, rb_dlink_list *list)
{
void *ptr;
assert(list != NULL);
assert(data != NULL);
ptr = rb_dlinkFindDelete(data, list);
if(ptr != NULL)
{
rb_free_rb_dlink_node(ptr);
return 1;
}
return 0;
}
/*
* rb_dlinkFind
* inputs - list to search
* - data
* output - pointer to link or NULL if not found
* side effects - Look for ptr in the linked listed pointed to by link.
*/
static inline rb_dlink_node *
rb_dlinkFind(void *data, rb_dlink_list *list)
{
rb_dlink_node *ptr;
assert(list != NULL);
assert(data != NULL);
RB_DLINK_FOREACH(ptr, list->head)
{
if(ptr->data == data)
return (ptr);
}
return (NULL);
}
static inline void
rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to)
{
assert(from != NULL);
assert(to != NULL);
/* There are three cases */
/* case one, nothing in from list */
if(from->head == NULL)
return;
/* case two, nothing in to list */
if(to->head == NULL)
{
to->head = from->head;
to->tail = from->tail;
from->head = from->tail = NULL;
to->length = from->length;
from->length = 0;
return;
}
/* third case play with the links */
from->tail->next = to->head;
to->head->prev = from->tail;
to->head = from->head;
from->head = from->tail = NULL;
to->length += from->length;
from->length = 0;
}
#endif /* __DLINK_H__ */

View file

@ -10,6 +10,24 @@
#include <signal.h>
#include <ctype.h>
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else
#ifndef HAVE__BOOL
#ifdef __cplusplus
typedef bool _Bool;
#else
#define _Bool signed char
#endif
#endif
#define bool _Bool
#define false 0
#define true 1
#define __bool_true_false_are_defined 1
#endif
#ifdef __GNUC__
#undef alloca
#define alloca __builtin_alloca
@ -128,13 +146,16 @@ char *rb_strerror(int error);
#endif
#ifdef __GNUC__
#define slrb_assert(expr) do \
if(rb_unlikely(!(expr))) { \
rb_lib_log( \
"file: %s line: %d (%s): Assertion failed: (%s)", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
} \
while(0)
#define slrb_assert(expr) do \
{ \
if(rb_unlikely(!(expr))) \
{ \
rb_lib_log("file: %s line: %d (%s): Assertion failed: (%s)", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
rb_backtrace_log_symbols(); \
} \
} \
while(0)
#else
#define slrb_assert(expr) do \
if(rb_unlikely(!(expr))) { \
@ -246,8 +267,13 @@ pid_t rb_waitpid(pid_t pid, int *status, int options);
pid_t rb_getpid(void);
//unsigned int rb_geteuid(void);
void *const *rb_backtrace(int *len); // writes to and returns static vector (*len indicates element count)
const char *const *rb_backtrace_symbols(int *len); // translates rb_backtrace(), all static
void rb_backtrace_log_symbols(void); // rb_backtrace_symbols piped to rb_lib_log()
#include <rb_tools.h>
#include <rb_dlink.h>
#include <rb_memory.h>
#include <rb_commio.h>
#include <rb_balloc.h>
@ -256,5 +282,6 @@ pid_t rb_getpid(void);
#include <rb_helper.h>
#include <rb_rawbuf.h>
#include <rb_patricia.h>
#include <rb_dictionary.h>
#endif

View file

@ -32,9 +32,16 @@
#include <stdlib.h>
#define RB_UNIQUE_PTR(deleter) __attribute__((cleanup(deleter)))
#define RB_AUTO_PTR RB_UNIQUE_PTR(rb_raii_free)
void rb_outofmemory(void) __attribute__((noreturn));
#ifdef __clang__
__attribute__((malloc, returns_nonnull))
#else
__attribute__((malloc, alloc_size(1), returns_nonnull))
#endif
static inline void *
rb_malloc(size_t size)
{
@ -44,6 +51,11 @@ rb_malloc(size_t size)
return (ret);
}
#ifdef __clang__
__attribute__((returns_nonnull))
#else
__attribute__((alloc_size(2), returns_nonnull))
#endif
static inline void *
rb_realloc(void *x, size_t y)
{
@ -54,6 +66,7 @@ rb_realloc(void *x, size_t y)
return (ret);
}
__attribute__((returns_nonnull))
static inline char *
rb_strndup(const char *x, size_t y)
{
@ -64,6 +77,7 @@ rb_strndup(const char *x, size_t y)
return (ret);
}
__attribute__((returns_nonnull))
static inline char *
rb_strdup(const char *x)
{
@ -74,12 +88,29 @@ rb_strdup(const char *x)
return (ret);
}
// overrule libc's prototype and ignore the cast warning, allowing proper const
// propagation without casting everywhere in the real code.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
static inline void
rb_free(void *ptr)
rb_free(const void *const ptr)
{
if(rb_likely(ptr != NULL))
free(ptr);
free((void *)ptr);
}
#pragma GCC diagnostic pop
static inline void
rb_raii_free(const void *const ptr)
{
if(!ptr)
return;
const void *const _ptr = *(const void *const *)ptr;
if(!_ptr)
return;
rb_free(_ptr);
}
#endif /* _I_MEMORY_H */

View file

@ -48,311 +48,7 @@ char *rb_basename(const char *);
char *rb_dirname(const char *);
int rb_string_to_array(char *string, char **parv, int maxpara);
/*
* double-linked-list stuff
*/
typedef struct _rb_dlink_node rb_dlink_node;
typedef struct _rb_dlink_list rb_dlink_list;
struct _rb_dlink_node
{
void *data;
rb_dlink_node *prev;
rb_dlink_node *next;
};
struct _rb_dlink_list
{
rb_dlink_node *head;
rb_dlink_node *tail;
unsigned long length;
};
rb_dlink_node *rb_make_rb_dlink_node(void);
void rb_free_rb_dlink_node(rb_dlink_node *lp);
void rb_init_rb_dlink_nodes(size_t dh_size);
/* This macros are basically swiped from the linux kernel
* they are simple yet effective
*/
/*
* Walks forward of a list.
* pos is your node
* head is your list head
*/
#define RB_DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
/*
* Walks forward of a list safely while removing nodes
* pos is your node
* n is another list head for temporary storage
* head is your list head
*/
#define RB_DLINK_FOREACH_SAFE(pos, n, head) for (pos = (head), n = pos ? pos->next : NULL; pos != NULL; pos = n, n = pos ? pos->next : NULL)
#define RB_DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev)
/* Returns the list length */
#define rb_dlink_list_length(list) (list)->length
#define rb_dlinkAddAlloc(data, list) rb_dlinkAdd(data, rb_make_rb_dlink_node(), list)
#define rb_dlinkAddTailAlloc(data, list) rb_dlinkAddTail(data, rb_make_rb_dlink_node(), list)
#define rb_dlinkDestroy(node, list) do { rb_dlinkDelete(node, list); rb_free_rb_dlink_node(node); } while(0)
/*
* dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
* which is mine.
* -- adrian
*/
static inline void
rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist)
{
/* Assumption: If m->next == NULL, then list->tail == m
* and: If m->prev == NULL, then list->head == m
*/
assert(m != NULL);
assert(oldlist != NULL);
assert(newlist != NULL);
if(m->next)
m->next->prev = m->prev;
else
oldlist->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
oldlist->head = m->next;
m->prev = NULL;
m->next = newlist->head;
if(newlist->head != NULL)
newlist->head->prev = m;
else if(newlist->tail == NULL)
newlist->tail = m;
newlist->head = m;
oldlist->length--;
newlist->length++;
}
static inline void
rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(data != NULL);
assert(m != NULL);
assert(list != NULL);
m->data = data;
m->prev = NULL;
m->next = list->head;
/* Assumption: If list->tail != NULL, list->head != NULL */
if(list->head != NULL)
list->head->prev = m;
else if(list->tail == NULL)
list->tail = m;
list->head = m;
list->length++;
}
static inline void
rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(b != NULL);
assert(data != NULL);
assert(m != NULL);
assert(list != NULL);
/* Shortcut - if its the first one, call rb_dlinkAdd only */
if(b == list->head)
{
rb_dlinkAdd(data, m, list);
}
else
{
m->data = data;
b->prev->next = m;
m->prev = b->prev;
b->prev = m;
m->next = b;
list->length++;
}
}
static inline void
rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list)
{
if(list->tail == m)
return;
/* From here assume that m->next != NULL as that can only
* be at the tail and assume that the node is on the list
*/
m->next->prev = m->prev;
if(m->prev != NULL)
m->prev->next = m->next;
else
list->head = m->next;
list->tail->next = m;
m->prev = list->tail;
m->next = NULL;
list->tail = m;
}
static inline void
rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list)
{
assert(m != NULL);
assert(list != NULL);
assert(data != NULL);
m->data = data;
m->next = NULL;
m->prev = list->tail;
/* Assumption: If list->tail != NULL, list->head != NULL */
if(list->tail != NULL)
list->tail->next = m;
else if(list->head == NULL)
list->head = m;
list->tail = m;
list->length++;
}
/* Execution profiles show that this function is called the most
* often of all non-spontaneous functions. So it had better be
* efficient. */
static inline void
rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list)
{
assert(m != NULL);
assert(list != NULL);
/* Assumption: If m->next == NULL, then list->tail == m
* and: If m->prev == NULL, then list->head == m
*/
if(m->next)
m->next->prev = m->prev;
else
list->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
list->head = m->next;
m->next = m->prev = NULL;
list->length--;
}
static inline rb_dlink_node *
rb_dlinkFindDelete(void *data, rb_dlink_list *list)
{
rb_dlink_node *m;
assert(list != NULL);
assert(data != NULL);
RB_DLINK_FOREACH(m, list->head)
{
if(m->data != data)
continue;
if(m->next)
m->next->prev = m->prev;
else
list->tail = m->prev;
if(m->prev)
m->prev->next = m->next;
else
list->head = m->next;
m->next = m->prev = NULL;
list->length--;
return m;
}
return NULL;
}
static inline int
rb_dlinkFindDestroy(void *data, rb_dlink_list *list)
{
void *ptr;
assert(list != NULL);
assert(data != NULL);
ptr = rb_dlinkFindDelete(data, list);
if(ptr != NULL)
{
rb_free_rb_dlink_node(ptr);
return 1;
}
return 0;
}
/*
* rb_dlinkFind
* inputs - list to search
* - data
* output - pointer to link or NULL if not found
* side effects - Look for ptr in the linked listed pointed to by link.
*/
static inline rb_dlink_node *
rb_dlinkFind(void *data, rb_dlink_list *list)
{
rb_dlink_node *ptr;
assert(list != NULL);
assert(data != NULL);
RB_DLINK_FOREACH(ptr, list->head)
{
if(ptr->data == data)
return (ptr);
}
return (NULL);
}
static inline void
rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to)
{
assert(from != NULL);
assert(to != NULL);
/* There are three cases */
/* case one, nothing in from list */
if(from->head == NULL)
return;
/* case two, nothing in to list */
if(to->head == NULL)
{
to->head = from->head;
to->tail = from->tail;
from->head = from->tail = NULL;
to->length = from->length;
from->length = 0;
return;
}
/* third case play with the links */
from->tail->next = to->head;
to->head->prev = from->tail;
to->head = from->head;
from->head = from->tail = NULL;
to->length += from->length;
from->length = 0;
}
size_t rb_array_to_string(int parc, const char **parv, char *buf, size_t max);
typedef struct _rb_zstring
{

View file

@ -1,6 +1,9 @@
make_and_lookup
make_and_lookup_ip
rb_accept_tcp
rb_backtrace
rb_backtrace_symbols
rb_backtrace_log_symbols
rb_base64_decode
rb_base64_encode
rb_basename
@ -174,6 +177,7 @@ rb_strcasecmp
rb_strcasestr
rb_strerror
rb_string_to_array
rb_array_to_string
rb_strlcat
rb_strlcpy
rb_strncasecmp

View file

@ -23,6 +23,11 @@
*/
#include <librb_config.h>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#include <rb_lib.h>
#include <commio-int.h>
#include <commio-ssl.h>
@ -423,3 +428,68 @@ rb_base64_decode(const unsigned char *str, int length, int *ret)
*ret = j;
return result;
}
void *bt_stack[64];
char bt_symbuf[64][256];
const char *bt_symbol[64];
#ifdef HAVE_EXECINFO_H
void *const *rb_backtrace(int *const usrlen)
{
int len = backtrace(bt_stack, 64);
if(usrlen) *usrlen = len;
return bt_stack;
}
#else
void *const *rb_backtrace(int *const usrlen)
{
if(usrlen) *usrlen = 0;
return bt_stack;
}
#endif
#ifdef HAVE_EXECINFO_H
const char *const *rb_backtrace_symbols(int *const usrlen)
{
int len;
void *const *const stack = rb_backtrace(&len);
char *const *const symbol = backtrace_symbols(stack, len);
if(!symbol)
len = 0;
if(usrlen)
*usrlen = len;
for(int i = 0; i < len; i++)
{
rb_strlcpy(bt_symbuf[i], symbol[i], sizeof(bt_symbuf[i]));
bt_symbol[i] = bt_symbuf[i];
}
free((char **)symbol);
return bt_symbol;
}
#else
const char *const *rb_backtrace_symbols(int *const usrlen)
{
if(usrlen) *usrlen = 0;
return bt_symbol;
}
#endif
#ifdef HAVE_EXECINFO_H
void rb_backtrace_log_symbols(void)
{
int len;
const char *const *const symbol = rb_backtrace_symbols(&len);
for(int i = 0; i < len; i++)
rb_lib_log("%2d: %s", i, symbol[i]);
}
#else
void rb_backtrace_log_symbols(void)
{
}
#endif

View file

@ -138,6 +138,36 @@ rb_string_to_array(char *string, char **parv, int maxpara)
return x;
}
/* rb_array_to_string()
* Inverse of rb_string_to_array()
* - space for delim
* - ':' prefixed at last parameter
*/
size_t
rb_array_to_string(int parc, const char **parv, char *buf, size_t max)
{
size_t ret = 0;
if(rb_unlikely(!max))
return ret;
buf[0] = '\0';
for(int i = 0; i < parc - 1; ++i)
{
ret = rb_strlcat(buf, parv[i], max);
ret = rb_strlcat(buf, " ", max);
}
if(parc)
{
ret = rb_strlcat(buf, ":", max);
ret = rb_strlcat(buf, parv[parc - 1], max);
}
return ret;
}
#ifndef HAVE_STRCASECMP
#ifndef _WIN32
/* Fallback taken from FreeBSD. --Elizafox */