Skip to content

Commit

Permalink
Merge pull request #1279
Browse files Browse the repository at this point in the history
lib: make foreach_res() reload-safe
  • Loading branch information
pstorz committed Nov 4, 2022
2 parents 8a47780 + 200d369 commit cda7bdc
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 158 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
- build: enable compiling on ARM [PR #1270]
- core and webui: adapt binary info messages to new wording [PR #1298]
- build: enable -Wextra warning level and apply required changes [PR #1261]
- lib: make foreach_res() reload-safe [PR #1279]

### Deprecated
- make_catalog_backup.pl is now a shell wrapper script which will be removed in version 23.
Expand Down
7 changes: 4 additions & 3 deletions core/src/dird/dird_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class ProfileResource : public BareosResource {
struct UserAcl {
BareosResource* corresponding_resource = nullptr;
alist<const char*>* ACL_lists[Num_ACL] = {0}; /**< Pointers to ACLs */
alist<const char*>* profiles = nullptr; /**< Pointers to profile resources */
alist<ProfileResource*>* profiles
= nullptr; /**< Pointers to profile resources */
};

// Console Resource
Expand Down Expand Up @@ -340,7 +341,7 @@ class StorageResource
char* media_type = nullptr; /**< Media Type provided by this Storage */
char* ndmp_changer_device = nullptr; /**< If DIR controls storage directly
(NDMP_NATIVE) changer device used */
alist<const char*>* device
alist<DeviceResource*>* device
= nullptr; /**< Alternate devices for this Storage */
int32_t MaxConcurrentJobs = 0; /**< Maximum concurrent jobs */
int32_t MaxConcurrentReadJobs = 0; /**< Maximum concurrent jobs reading */
Expand Down Expand Up @@ -475,7 +476,7 @@ class JobResource : public BareosResource {
alist<const char*>* FdPluginOptions = nullptr; /**< Generic FD plugin options used by this Job */
alist<const char*>* SdPluginOptions = nullptr; /**< Generic SD plugin options used by this Job */
alist<const char*>* DirPluginOptions = nullptr; /**< Generic DIR plugin options used by this Job */
alist<const char*>* base = nullptr; /**< Base jobs */
alist<JobResource*>* base = nullptr; /**< Base jobs */

bool allow_mixed_priority = false; /**< Allow jobs with higher priority concurrently with this */
bool where_use_regexp = false; /**< true if RestoreWhere is a BareosRegex */
Expand Down
2 changes: 1 addition & 1 deletion core/src/dird/job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ void CreateClones(JobControlRecord* jcr)
Dmsg2(900, "cloned=%d run_cmds=%p\n", jcr->impl->cloned,
jcr->impl->res.job->run_cmds);
if (!jcr->impl->cloned && jcr->impl->res.job->run_cmds) {
char* runcmd = nullptr;
const char* runcmd = nullptr;
JobId_t jobid;
JobResource* job = jcr->impl->res.job;
POOLMEM* cmd = GetPoolMemory(PM_FNAME);
Expand Down
2 changes: 1 addition & 1 deletion core/src/filed/dir_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static bool ValidateCommand(JobControlRecord* jcr,
const char* cmd,
alist<const char*>* allowed_job_cmds)
{
char* allowed_job_cmd = nullptr;
const char* allowed_job_cmd = nullptr;
bool allowed = false;

// If there is no explicit list of allowed cmds allow all cmds.
Expand Down
2 changes: 1 addition & 1 deletion core/src/filed/filed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ static bool CheckResources()

/* If everything is well, attempt to initialize our public/private keys */
if (OK && (me->pki_encrypt || me->pki_sign)) {
char* filepath = nullptr;
const char* filepath = nullptr;
/* Load our keypair */
me->pki_keypair = crypto_keypair_new();
if (!me->pki_keypair) {
Expand Down
10 changes: 4 additions & 6 deletions core/src/findlib/attribs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,10 @@ int encode_attribsEx(JobControlRecord* jcr,
}

// Do casting according to unknown type to keep compiler happy
# ifdef HAVE_TYPEOF
# define plug(st, val) st = (typeof st)val
# else
// Use templates to do the casting
template <class T> void plug(T& st, uint64_t val) { st = static_cast<T>(val); }
# endif
template <typename T> static void plug(T& st, uint64_t val)
{
st = static_cast<T>(val);
}

/**
* Set Extended File Attributes for Win32
Expand Down
3 changes: 0 additions & 3 deletions core/src/include/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,6 @@ extern char win_os[];
// Define to 1 if TLS support should be enabled
#cmakedefine HAVE_TLS @HAVE_TLS@

// Define to 1 if compiler has typeof
#cmakedefine HAVE_TYPEOF @HAVE_TYPEOF@

// Define to 1 if you have the <ucontext.h> header file
#cmakedefine HAVE_UCONTEXT_H @HAVE_UCONTEXT_H@

Expand Down
56 changes: 13 additions & 43 deletions core/src/lib/alist.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2012 Free Software Foundation Europe e.V.
Copyright (C) 2016-2021 Bareos GmbH & Co. KG
Copyright (C) 2016-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -35,48 +35,18 @@
* Loop var through each member of list using an increasing index.
* Loop var through each member of list using an decreasing index.
*/
#ifdef HAVE_TYPEOF
# define foreach_alist(var, list) \
for ((var) = list ? (typeof((var)))(list)->first() : 0; (var); \
(var) = (typeof(var))(list)->next())

# define foreach_alist_null(var, list) \
for ((var) = list ? (typeof((var)))(list)->first() : nullptr; (var); \
(var) = (typeof(var))(list)->next())

# define foreach_alist_index(inx, var, list) \
for ((inx) = 0; \
(list != nullptr) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
(inx)++)

# define foreach_alist_rindex(inx, var, list) \
for ((list != nullptr) ? (inx) = ((list)->size() - 1) : 0; \
(list != nullptr) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
(inx)--)

#else
# define foreach_alist(var, list) \
for ((void)(list ? (*((void**)&(var)) = (void*)((list)->first())) : 0); \
(var); (*((void**)&(var)) = (void*)((list)->next())))

# define foreach_alist_null(var, list) \
for ((void)(list ? (*((void**)&(var)) = (void*)((list)->first())) \
: nullptr); \
(var); (*((void**)&(var)) = (void*)((list)->next())))

# define foreach_alist_index(inx, var, list) \
for ((inx) = 0; (list != nullptr) \
? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
(inx)++)

# define foreach_alist_rindex(inx, var, list) \
for ((list != nullptr) ? (inx) = ((list)->size() - 1) : 0; \
(list != nullptr) \
? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
(inx)--)
#endif
#define foreach_alist(var, list) \
for ((var) = list ? (list)->first() : 0; (var); (var) = (list)->next())

#define foreach_alist_null(var, list) \
for ((var) = list ? (list)->first() : nullptr; (var); (var) = (list)->next())

#define foreach_alist_index(inx, var, list) \
for ((inx) = 0; (list != nullptr) ? ((var) = (list)->get((inx))) : 0; (inx)++)

#define foreach_alist_rindex(inx, var, list) \
for ((list != nullptr) ? (inx) = ((list)->size() - 1) : 0; \
(list != nullptr) ? ((var) = (list)->get((inx))) : 0; (inx)--)


#include <string>
Expand Down
17 changes: 2 additions & 15 deletions core/src/lib/attribs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2018 Bareos GmbH & Co. KG
Copyright (C) 2016-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -107,23 +107,10 @@ void EncodeStat(char* buf,
}

/* Do casting according to unknown type to keep compiler happy */
#ifdef HAVE_TYPEOF
# define plug(st, val) st = (typeof st)val
#else
# if !HAVE_GCC & HAVE_SUN_OS
/* Sun compiler does not handle templates correctly */
# define plug(st, val) st = val
# elif __sgi
# define plug(st, val) st = val
# else
/* Use templates to do the casting */
template <class T>
void plug(T& st, uint64_t val)
template <typename T> static void plug(T& st, uint64_t val)
{
st = static_cast<T>(val);
}
# endif
#endif

// Decode a stat packet from base64 characters
int DecodeStat(char* buf, struct stat* statp, int stat_size, int32_t* LinkFI)
Expand Down
6 changes: 3 additions & 3 deletions core/src/lib/bareos_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -23,9 +23,9 @@

#include "lib/bareos_resource.h"

const char* GetResourceName(void* resource)
const char* GetResourceName(const void* resource)
{
return ((BareosResource*)resource)->resource_name_;
return static_cast<const BareosResource*>(resource)->resource_name_;
}

BareosResource::BareosResource()
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/bareos_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -64,6 +64,6 @@ class BareosResource {
bool verbose);
};

const char* GetResourceName(void* resource);
const char* GetResourceName(const void* resource);

#endif // BAREOS_LIB_BAREOS_RESOURCE_H_
21 changes: 3 additions & 18 deletions core/src/lib/dlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,9 @@
#include "lib/message.h"
#include "lib/message_severity.h"

/**
* There is a lot of extra casting here to work around the fact
* that some compilers (Sun and Visual C++) do not accept
* (void *) as an lvalue on the left side of an equal.
*
* Loop var through each member of list
*/
#ifdef HAVE_TYPEOF
# define foreach_dlist(var, list) \
for ((var) = nullptr; \
(list ? ((var) = (typeof(var))(list)->next(var)) : nullptr) \
!= nullptr;)
#else
# define foreach_dlist(var, list) \
for ((var) = nullptr; \
(list ? (*((void**)&(var)) = (void*)((list)->next(var))) : nullptr) \
!= nullptr;)
#endif
#define foreach_dlist(var, list) \
for ((var) = nullptr; \
(list ? ((var) = (list)->next(var)) : nullptr) != nullptr;)

template <typename T> class dlist {
T* head{nullptr};
Expand Down
13 changes: 2 additions & 11 deletions core/src/lib/htable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,8 @@
#ifndef BAREOS_LIB_HTABLE_H_
#define BAREOS_LIB_HTABLE_H_

// Loop var through each member of table
#ifdef HAVE_TYPEOF
# define foreach_htable(var, tbl) \
for ((var) = (typeof(var))((tbl)->first()); (var); \
(var) = (typeof(var))((tbl)->next()))
#else
# define foreach_htable(var, tbl) \
for ((*((void**)&(var)) = (void*)((tbl)->first())); (var); \
(*((void**)&(var)) = (void*)((tbl)->next())))
#endif

#define foreach_htable(var, tbl) \
for ((var) = (tbl)->first(); (var); (var) = (tbl)->next())

#include "include/config.h"
#include "monotonic_buffer.h"
Expand Down
10 changes: 5 additions & 5 deletions core/src/lib/output_formatter_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "lib/util.h"
#include "lib/output_formatter_resource.h"

const char* GetAsCString(void* item) { return (const char*)item; }
const char* GetAsCString(const void* item) { return (const char*)item; }

OutputFormatterResource::OutputFormatterResource(OutputFormatter* send,
int indent_level)
Expand Down Expand Up @@ -199,13 +199,13 @@ void OutputFormatterResource::KeyUnquotedString(const char* name,
void OutputFormatterResource::KeyMultipleStringsInOneLine(
const char* key,
alist<const char*>* list,
std::function<const char*(void* item)> GetValue,
std::function<const char*(const void* item)> GetValue,
bool as_comment,
bool quoted_strings)
{
// Each member of the list is comma-separated
int cnt = 0;
char* item = nullptr;
const char* item = nullptr;
std::string format = "%s";
if (quoted_strings) { format = "\"%s\""; }

Expand Down Expand Up @@ -255,13 +255,13 @@ void OutputFormatterResource::KeyMultipleStringsOnePerLineAddItem(
void OutputFormatterResource::KeyMultipleStringsOnePerLine(
const char* key,
alist<const char*>* list,
std::function<const char*(void* item)> GetValue,
std::function<const char*(const void* item)> GetValue,
bool as_comment,
bool quoted_strings,
bool escape_strings)
{
// One line for each member of the list
char* item = nullptr;
const char* item = nullptr;

if ((list == NULL) or (list->empty())) {
if (as_comment) {
Expand Down
17 changes: 9 additions & 8 deletions core/src/lib/output_formatter_resource.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2020-2021 Bareos GmbH & Co. KG
Copyright (C) 2020-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -107,7 +107,7 @@ class OutputFormatterResource {
void KeyMultipleStringsInOneLine(
const char* key,
alist<const char*>* list,
std::function<const char*(void* item)> GetValue,
std::function<const char*(const void* item)> GetValue,
bool as_comment = false,
bool quoted_strings = true);

Expand All @@ -117,12 +117,13 @@ class OutputFormatterResource {
bool quoted_strings = true,
bool escape_strings = false);

void KeyMultipleStringsOnePerLine(const char* key,
alist<const char*>* list,
std::function<const char*(void*)> GetValue,
bool as_comment = false,
bool quoted_strings = true,
bool escape_strings = false);
void KeyMultipleStringsOnePerLine(
const char* key,
alist<const char*>* list,
std::function<const char*(const void*)> GetValue,
bool as_comment = false,
bool quoted_strings = true,
bool escape_strings = false);

void KeyMultipleStringsOnePerLine(const char* key,
const std::vector<std::string>&,
Expand Down
24 changes: 15 additions & 9 deletions core/src/lib/parse_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,22 @@ void IndentConfigItem(PoolMem& cfg_str,
const char* config_item,
bool inherited = false);

/* this function is used as an initializer in foreach_res, so we can null
* the pointer passed into and also get a reference to the configuration that
* we then keep for the lifetime of the loop.
*/
inline std::shared_ptr<ConfigResourcesContainer> _init_foreach_res_(
ConfigurationParser* my_config,
void* var)
{
memset(var, 0, sizeof(void*));
return my_config->GetResourcesContainer();
}
// Loop through each resource of type, returning in var
#ifdef HAVE_TYPEOF
# define foreach_res(var, type) \
for ((var) = NULL; ((var) = (typeof(var))my_config->GetNextRes( \
(type), (BareosResource*)var));)
#else
# define foreach_res(var, type) \
for (var = NULL; (*((void**)&(var)) = (void*)my_config->GetNextRes( \
(type), (BareosResource*)var));)
#endif
#define foreach_res(var, type) \
for (auto _config_table_ = _init_foreach_res_(my_config, &var); \
((var) \
= static_cast<decltype(var)>(my_config->GetNextRes((type), var)));)

#define LockRes(x) (x)->b_LockRes(__FILE__, __LINE__)
#define UnlockRes(x) (x)->b_UnlockRes(__FILE__, __LINE__)
Expand Down

0 comments on commit cda7bdc

Please sign in to comment.