Skip to content

Commit

Permalink
Bring config engine to 2014 and don't polute namespace.
Browse files Browse the repository at this point in the history
Implement a TODO from June 2008 and get ride of a slack of global
variables used by the config engine. The code seems to be have been
refactored in 2008 to access most configfile parsing via the CONFIG
class and it already has most of the needed structures which also were
kept as global variables. From now on the config engine is accessed via
the CONFIG class and only res_all is left as global variable. As a side
effect the config CLASS is now available as my_config and is a global
variable.

Global variable introduced:

- CLASS CONFIG *my_config (Global configuration)

Global variables removed:

- res_all_size
- r_first
- r_last
- resources
- res_head

It should be possible also to get ride of res_all but that needs some
more testing. And is currently not very high on my urgency map.
  • Loading branch information
Marco van Wieringen committed Feb 17, 2015
1 parent 6d4e8c8 commit 3d09d78
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 92 deletions.
15 changes: 7 additions & 8 deletions src/console/console_conf.c
Expand Up @@ -44,14 +44,13 @@
#include "bareos.h"
#include "console_conf.h"

/* Define the first and last resource ID record
/*
* Define the first and last resource ID record
* types. Note, these should be unique for each
* daemon though not a requirement.
*/
int32_t r_first = R_FIRST;
int32_t r_last = R_LAST;
static RES *sres_head[R_LAST - R_FIRST + 1];
RES **res_head = sres_head;
static RES **res_head = sres_head;

/* Forward referenced subroutines */

Expand Down Expand Up @@ -121,7 +120,7 @@ static RES_ITEM dir_items[] = {
* This is the master resource definition.
* It must have one item for each of the resources.
*/
RES_TABLE resources[] = {
static RES_TABLE resources[] = {
{ "console", cons_items, R_CONSOLE, sizeof(CONRES) },
{ "director", dir_items, R_DIRECTOR, sizeof(DIRRES) },
{ NULL, NULL, 0 }
Expand Down Expand Up @@ -249,7 +248,7 @@ void free_resource(RES *sres, int type)
void save_resource(int type, RES_ITEM *items, int pass)
{
URES *res;
int rindex = type - r_first;
int rindex = type - R_FIRST;
int i;
int error = 0;

Expand Down Expand Up @@ -330,8 +329,8 @@ bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
exit_code,
(void *)&res_all,
res_all_size,
r_first,
r_last,
R_FIRST,
R_LAST,
resources,
res_head);
return config->parse_config();
Expand Down
9 changes: 5 additions & 4 deletions src/dird/dird.c
Expand Up @@ -429,15 +429,16 @@ static void init_reload(void)

static void free_saved_resources(int table)
{
int num = r_last - r_first + 1;
int num = my_config->m_r_last - my_config->m_r_first + 1;
RES **res_tab = reload_table[table].res_table;

if (!res_tab) {
Dmsg1(100, "res_tab for table %d already released.\n", table);
return;
}
Dmsg1(100, "Freeing resources for table %d\n", table);
for (int j=0; j<num; j++) {
free_resource(res_tab[j], r_first + j);
free_resource(res_tab[j], my_config->m_r_first + j);
}
free(res_tab);
reload_table[table].job_count = 0;
Expand Down Expand Up @@ -550,10 +551,10 @@ void reload_config(int sig)
}
reload_table[rtable].res_table = my_config->save_resources();
/* Now restore old resource values */
int num = r_last - r_first + 1;
int num = my_config->m_r_last - my_config->m_r_first + 1;
RES **res_tab = reload_table[table].res_table;
for (int i=0; i<num; i++) {
res_head[i] = res_tab[i];
my_config->m_res_head[i] = res_tab[i];
}
table = rtable; /* release new, bad, saved table below */
} else {
Expand Down
14 changes: 6 additions & 8 deletions src/dird/dird_conf.c
Expand Up @@ -51,10 +51,8 @@
* types. Note, these should be unique for each
* daemon though not a requirement.
*/
int32_t r_first = R_FIRST;
int32_t r_last = R_LAST;
static RES *sres_head[R_LAST - R_FIRST + 1];
RES **res_head = sres_head;
static RES **res_head = sres_head;

/*
* Set default indention e.g. 2 spaces.
Expand Down Expand Up @@ -476,7 +474,7 @@ extern RES_ITEM msgs_items[];
*
* name handler value code flags default_value
*/
RES_TABLE resources[] = {
static RES_TABLE resources[] = {
{ "director", dir_items, R_DIRECTOR, sizeof(DIRRES) },
{ "client", cli_items, R_CLIENT, sizeof(CLIENTRES) },
{ "jobdefs", job_items, R_JOBDEFS, sizeof(JOBRES) },
Expand Down Expand Up @@ -1899,7 +1897,7 @@ void free_resource(RES *sres, int type)
void save_resource(int type, RES_ITEM *items, int pass)
{
URES *res;
int rindex = type - r_first;
int rindex = type - R_FIRST;
int i;
bool error = false;

Expand Down Expand Up @@ -2172,7 +2170,7 @@ static void store_actiononpurge(LEX *lc, RES_ITEM *item, int index, int pass)
static void store_device(LEX *lc, RES_ITEM *item, int index, int pass)
{
URES *res;
int rindex = R_DEVICE - r_first;
int rindex = R_DEVICE - R_FIRST;
bool found = false;

if (pass == 1) {
Expand Down Expand Up @@ -3075,8 +3073,8 @@ bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code)
exit_code,
(void *)&res_all,
res_all_size,
r_first,
r_last,
R_FIRST,
R_LAST,
resources,
res_head);
return config->parse_config();
Expand Down
6 changes: 3 additions & 3 deletions src/dird/ua_output.c
Expand Up @@ -173,7 +173,7 @@ int show_cmd(UAContext *ua, const char *cmd)
if (bstrncasecmp(res_name, _(avail_resources[j].res_name), len)) {
type = avail_resources[j].type;
if (type > 0) {
res = res_head[type-r_first];
res = my_config->m_res_head[type - my_config->m_r_first];
} else {
res = NULL;
}
Expand All @@ -198,10 +198,10 @@ int show_cmd(UAContext *ua, const char *cmd)

switch (type) {
case -1: /* all */
for (j=r_first; j<=r_last; j++) {
for (j = my_config->m_r_first; j <= my_config->m_r_last; j++) {
/* Skip R_DEVICE since it is really not used or updated */
if (j != R_DEVICE) {
dump_resource(j, res_head[j-r_first], bsendmsg, ua);
dump_resource(j, my_config->m_res_head[j - my_config->m_r_first], bsendmsg, ua);
}
}
break;
Expand Down
12 changes: 5 additions & 7 deletions src/filed/filed_conf.c
Expand Up @@ -49,10 +49,8 @@
* types. Note, these should be unique for each
* daemon though not a requirement.
*/
int32_t r_first = R_FIRST;
int32_t r_last = R_LAST;
static RES *sres_head[R_LAST - R_FIRST + 1];
RES **res_head = sres_head;
static RES **res_head = sres_head;

/*
* Forward referenced subroutines
Expand Down Expand Up @@ -162,7 +160,7 @@ extern RES_ITEM msgs_items[];
* This is the master resource definition.
* It must have one item for each of the resources.
*/
RES_TABLE resources[] = {
static RES_TABLE resources[] = {
{ "director", dir_items, R_DIRECTOR, sizeof(DIRRES) },
{ "filedaemon", cli_items, R_CLIENT, sizeof(CLIENTRES) },
{ "client", cli_items, R_CLIENT, sizeof(CLIENTRES) }, /* alias for filedaemon */
Expand Down Expand Up @@ -387,7 +385,7 @@ void free_resource(RES *sres, int type)
void save_resource(int type, RES_ITEM *items, int pass)
{
URES *res;
int rindex = type - r_first;
int rindex = type - R_FIRST;
int i;
int error = 0;

Expand Down Expand Up @@ -571,8 +569,8 @@ bool parse_fd_config(CONFIG *config, const char *configfile, int exit_code)
exit_code,
(void *)&res_all,
res_all_size,
r_first,
r_last,
R_FIRST,
R_LAST,
resources,
res_head);
return config->parse_config();
Expand Down
13 changes: 6 additions & 7 deletions src/lib/parse_conf.c
Expand Up @@ -76,7 +76,6 @@ extern "C" URES res_all;
#else
extern URES res_all;
#endif
extern brwlock_t res_lock; /* resource lock */

/* Forward referenced subroutines */
static const char *get_default_configdir();
Expand Down Expand Up @@ -148,7 +147,7 @@ bool CONFIG::parse_config()
LEX_WARNING_HANDLER *scan_warning = m_scan_warning;
int err_type = m_err_type;

if (first && (errstat=rwl_init(&res_lock)) != 0) {
if (first && (errstat = rwl_init(&m_res_lock)) != 0) {
berrno be;
Jmsg1(NULL, M_ABORT, 0, _("Unable to initialize resource lock. ERR=%s\n"),
be.bstrerror(errstat));
Expand Down Expand Up @@ -216,14 +215,14 @@ bool CONFIG::parse_config()
scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str);
goto bail_out;
}
for (i = 0; resources[i].name; i++) {
if (bstrcasecmp(resources[i].name, lc->str)) {
items = resources[i].items;
for (i = 0; m_resources[i].name; i++) {
if (bstrcasecmp(m_resources[i].name, lc->str)) {
items = m_resources[i].items;
if (!items) {
break;
}
state = p_resource;
res_type = resources[i].rcode;
res_type = m_resources[i].rcode;
init_resource(res_type, items, pass);
break;
}
Expand Down Expand Up @@ -571,7 +570,7 @@ void CONFIG::init_resource(int type, RES_ITEM *items, int pass)
* If this triggers, take a look at lib/parse_conf.h
*/
if (i >= MAX_RES_ITEMS) {
Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[type - r_first]);
Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), m_resources[type - m_r_first]);
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/lib/parse_conf.h
Expand Up @@ -244,7 +244,6 @@ class CONFIG {
int32_t m_res_all_size; /* Length of buffer */
bool m_omit_defaults; /* Omit config variables with default values when dumping the config */

/* The below are not yet implemented */
int32_t m_r_first; /* First daemon resource type */
int32_t m_r_last; /* Last daemon resource type */
RES_TABLE *m_resources; /* Pointer to table of permitted resources */
Expand Down Expand Up @@ -297,10 +296,3 @@ const char *res_to_str(int rcode);
#define foreach_res(var, type) \
for(var=NULL; (*((void **)&(var))=(void *)GetNextRes((type), (RES *)var));)
#endif

/* ***FIXME*** eliminate these globals */
extern int32_t r_first;
extern int32_t r_last;
extern RES_TABLE resources[];
extern RES **res_head;
extern int32_t res_all_size;
41 changes: 16 additions & 25 deletions src/lib/res.c
Expand Up @@ -31,15 +31,7 @@
/* Forward referenced subroutines */
static void scan_types(LEX *lc, MSGSRES *msg, int dest, char *where, char *cmd);

/* Each daemon has a slightly different set of
* resources, so it will define the following
* global values.
*/
extern int32_t r_first;
extern int32_t r_last;
extern RES_TABLE resources[];
extern RES **res_head;
extern CONFIG *my_config;
extern CONFIG *my_config; /* Our Global config */

/*
* Set default indention e.g. 2 spaces.
Expand All @@ -62,7 +54,6 @@ extern "C" URES res_all;
extern URES res_all;
#endif

brwlock_t res_lock; /* resource lock */
static int res_locked = 0; /* resource chain lock count -- for debug */

/* #define TRACE_RES */
Expand All @@ -72,13 +63,13 @@ void b_LockRes(const char *file, int line)
int errstat;
#ifdef TRACE_RES
Pmsg4(000, "LockRes locked=%d w_active=%d at %s:%d\n",
res_locked, res_lock.w_active, file, line);
res_locked, my_config->m_res_lock.w_active, file, line);
if (res_locked) {
Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
Pmsg2(000, "LockRes writerid=%d myid=%d\n", my_config->m_res_lock.writer_id,
pthread_self());
}
#endif
if ((errstat=rwl_writelock(&res_lock)) != 0) {
if ((errstat = rwl_writelock(&my_config->m_res_lock)) != 0) {
Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d: ERR=%s\n"),
file, line, strerror(errstat));
}
Expand All @@ -88,14 +79,14 @@ void b_LockRes(const char *file, int line)
void b_UnlockRes(const char *file, int line)
{
int errstat;
if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
if ((errstat = rwl_writeunlock(&my_config->m_res_lock)) != 0) {
Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
file, line, strerror(errstat));
}
res_locked--;
#ifdef TRACE_RES
Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n",
res_locked, res_lock.w_active, file, line);
res_locked, my_config->m_res_lock.w_active, file, line);
#endif
}

Expand All @@ -105,10 +96,10 @@ void b_UnlockRes(const char *file, int line)
RES *GetResWithName(int rcode, const char *name)
{
RES *res;
int rindex = rcode - r_first;
int rindex = rcode - my_config->m_r_first;

LockRes();
res = res_head[rindex];
res = my_config->m_res_head[rindex];
while (res) {
if (bstrcmp(res->name, name)) {
break;
Expand All @@ -128,10 +119,10 @@ RES *GetResWithName(int rcode, const char *name)
RES *GetNextRes(int rcode, RES *res)
{
RES *nres;
int rindex = rcode - r_first;
int rindex = rcode - my_config->m_r_first;

if (res == NULL) {
nres = res_head[rindex];
nres = my_config->m_res_head[rindex];
} else {
nres = res->next;
}
Expand Down Expand Up @@ -164,10 +155,10 @@ RES_ITEM msgs_items[] = {

const char *res_to_str(int rcode)
{
if (rcode < r_first || rcode > r_last) {
if (rcode < my_config->m_r_first || rcode > my_config->m_r_last) {
return _("***UNKNOWN***");
} else {
return resources[rcode-r_first].name;
return my_config->m_resources[rcode - my_config->m_r_first].name;
}
}

Expand Down Expand Up @@ -1371,24 +1362,24 @@ bool MSGSRES::print_config(POOL_MEM &buff)
bool BRSRES::print_config(POOL_MEM &buff)
{
RES_ITEM *items;
int rindex = this->hdr.rcode - r_first;
int rindex = this->hdr.rcode - my_config->m_r_first;
int i = 0;
POOL_MEM cfg_str;
POOL_MEM temp;

/*
* Make sure the resource class has any items.
*/
if (!resources[rindex].items) {
if (!my_config->m_resources[rindex].items) {
return true;
}

memcpy(&res_all, this, resources[rindex].size);
memcpy(&res_all, this, my_config->m_resources[rindex].size);

pm_strcat(cfg_str, res_to_str(this->hdr.rcode));
pm_strcat(cfg_str, " {\n");

items = resources[rindex].items;
items = my_config->m_resources[rindex].items;

for (i = 0; items[i].name; i++) {
bool print_item = false;
Expand Down

0 comments on commit 3d09d78

Please sign in to comment.