Skip to content

Commit

Permalink
bnet/tests: bugfixes for bsock connection tests
Browse files Browse the repository at this point in the history
- reviewed shutdown of BnetThreadServerTcp
- changed the value of INVALID_JCR to nullptr to avoid pthread destructor crash
- for director, tcp_server_ready flags that BnetThreadServerTcp is running
- added bconsole and director configs to src/tests/configs
  • Loading branch information
franku committed Nov 12, 2018
1 parent 404be80 commit 8f6a961
Show file tree
Hide file tree
Showing 39 changed files with 426 additions and 67 deletions.
6 changes: 3 additions & 3 deletions core/src/console/console_conf.cc
Expand Up @@ -316,13 +316,13 @@ static void ConfigReadyCallback(ConfigurationParser &my_config)

ConfigurationParser *InitConsConfig(const char *configfile, int exit_code)
{
my_config =
ConfigurationParser *config =
new ConfigurationParser(configfile, nullptr, nullptr, nullptr, nullptr, nullptr, exit_code,
(void *)&res_all, res_all_size, R_FIRST, R_LAST, resources, res_head,
default_config_filename.c_str(), "bconsole.d", ConfigReadyCallback,
SaveResource, DumpResource, FreeResource);
if (my_config) { my_config->r_own_ = R_CONSOLE; }
return my_config;
if (config) { config->r_own_ = R_CONSOLE; }
return config;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions core/src/dird/dird_conf.cc
Expand Up @@ -3882,13 +3882,13 @@ static void CreateAndAddUserAgentConsoleResource(ConfigurationParser &my_config)

ConfigurationParser *InitDirConfig(const char *configfile, int exit_code)
{
my_config =
ConfigurationParser *config =
new ConfigurationParser(configfile, nullptr, nullptr, InitResourceCb, ParseConfigCb, PrintConfigCb,
exit_code, (void *)&res_all, res_all_size, R_FIRST, R_LAST, resources, res_head,
default_config_filename.c_str(), "bareos-dir.d", ConfigReadyCallback,
SaveResource, DumpResource, FreeResource);
if (my_config) { my_config->r_own_ = R_DIRECTOR; }
return my_config;
if (config) { config->r_own_ = R_DIRECTOR; }
return config;
}


Expand Down
13 changes: 9 additions & 4 deletions core/src/dird/socket_server.cc
Expand Up @@ -47,6 +47,7 @@ static workq_t socket_workq;
static alist *sock_fds = NULL;
static pthread_t tcp_server_tid;
static ConnectionPool *client_connections = NULL;
static bool tcp_server_ready = false;

struct s_addr_port {
char *addr;
Expand Down Expand Up @@ -118,15 +119,14 @@ static void *HandleConnectionRequest(ConfigurationParser *config, void *arg)

extern "C" void *connect_thread(void *arg)
{
pthread_detach(pthread_self());
SetJcrInTsd(INVALID_JCR);

/*
* Permit MaxConnections connections.
*/
sock_fds = New(alist(10, not_owned_by_alist));
BnetThreadServerTcp((dlist *)arg, me->MaxConnections, sock_fds, &socket_workq, me->nokeepalive,
HandleConnectionRequest, my_config);
HandleConnectionRequest, my_config, &tcp_server_ready);

return NULL;
}
Expand All @@ -140,21 +140,26 @@ void StartSocketServer(dlist *addrs)
{
int status;
static dlist *myaddrs = addrs;
tcp_server_ready = false;

if (client_connections == NULL) { client_connections = New(ConnectionPool()); }
if ((status = pthread_create(&tcp_server_tid, NULL, connect_thread, (void *)myaddrs)) != 0) {
BErrNo be;
Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
}

int timeout_ctr = 1000; /* 1000 * 10000µs = 10 sec */
while (!tcp_server_ready && timeout_ctr--) {
Bmicrosleep(0, 10000);
}

return;
}

void StopSocketServer()
{
if (sock_fds) {
BnetStopThreadServerTcp(tcp_server_tid);
CleanupBnetThreadServerTcp(sock_fds, &socket_workq);
BnetStopAndWaitForThreadServerTcp(tcp_server_tid);
delete sock_fds;
sock_fds = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/filed/socket_server.cc
Expand Up @@ -129,7 +129,7 @@ void StopSocketServer(bool wait)
{
Dmsg0(100, "StopSocketServer\n");
if (sock_fds) {
BnetStopThreadServerTcp(tcp_server_tid);
BnetStopAndWaitForThreadServerTcp(tcp_server_tid);
/*
* before thread_servers terminates,
* it calls cleanup_bnet_thread_server_tcp
Expand Down
8 changes: 1 addition & 7 deletions core/src/include/jcr.h
Expand Up @@ -643,13 +643,7 @@ class JobControlRecord {
#endif /* STORAGE_DAEMON */
};

/*
* Setting a NULL in tsd doesn't clear the tsd but instead tells
* pthreads not to call the tsd destructor. Consequently, we
* define this *invalid* jcr address and stuff it in the tsd
* when the jcr is not valid.
*/
#define INVALID_JCR ((JobControlRecord *)(-1))
#define INVALID_JCR (nullptr)

/*
* Structure for all daemons that keeps some summary
Expand Down
17 changes: 14 additions & 3 deletions core/src/lib/bnet_server_tcp.cc
Expand Up @@ -70,19 +70,21 @@ struct s_sockfd {
* Stop the Threaded Network Server if its realy running in a separate thread.
* e.g. set the quit flag and wait for the other thread to exit cleanly.
*/
void BnetStopThreadServerTcp(pthread_t tid)
void BnetStopAndWaitForThreadServerTcp(pthread_t tid)
{
Dmsg0(100, "Stop BnetThreadServer and wait until finished\n");
quit = true;
if (!pthread_equal(tid, pthread_self())) {
pthread_kill(tid, TIMEOUT_SIGNAL);
pthread_join(tid, nullptr);
}
}

/**
* Perform a cleanup for the Threaded Network Server check if there is still
* something to do or that the cleanup already took place.
*/
void CleanupBnetThreadServerTcp(alist *sockfds, workq_t *client_wq)
static void CleanupBnetThreadServerTcp(alist *sockfds, workq_t *client_wq)
{
int status;
s_sockfd *fd_ptr = NULL;
Expand Down Expand Up @@ -131,7 +133,8 @@ void BnetThreadServerTcp(dlist *addr_list,
bool nokeepalive,
void *HandleConnectionRequest(ConfigurationParser *config,
void *bsock),
ConfigurationParser *config)
ConfigurationParser *config,
bool *const tcp_server_ready)
{
int newsockfd, status;
socklen_t clilen;
Expand Down Expand Up @@ -276,6 +279,10 @@ void BnetThreadServerTcp(dlist *addr_list,
}
#endif

if (tcp_server_ready) {
*tcp_server_ready = true;
}

/*
* Wait for a connection from the client process.
*/
Expand Down Expand Up @@ -389,4 +396,8 @@ void BnetThreadServerTcp(dlist *addr_list,
}

CleanupBnetThreadServerTcp(sockfds, client_wq);

if (tcp_server_ready) {
*tcp_server_ready = false;
}
}
6 changes: 3 additions & 3 deletions core/src/lib/bnet_server_tcp.h
Expand Up @@ -23,15 +23,15 @@

class ConfigurationParser;

void CleanupBnetThreadServerTcp(alist *sockfds, workq_t *client_wq);
void BnetThreadServerTcp(dlist *addr_list,
int max_clients,
alist *sockfds,
workq_t *client_wq,
bool nokeepalive,
void *HandleConnectionRequest(ConfigurationParser *config,
void *bsock),
ConfigurationParser *config);
void BnetStopThreadServerTcp(pthread_t tid);
ConfigurationParser *config,
bool * const tcp_server_ready = nullptr);
void BnetStopAndWaitForThreadServerTcp(pthread_t tid);

#endif // BAREOS_LIB_BNET_SEVER_TCP_H_
7 changes: 7 additions & 0 deletions core/src/lib/message.cc
Expand Up @@ -929,6 +929,11 @@ void DispatchMessage(JobControlRecord *jcr, int type, utime_t mtime, char *msg)
msgs = daemon_msgs;
}

if (!msgs) {
Dmsg1(100, "Could not dispatch message: %s", msg);
return;
}

/*
* If closing this message resource, print and send to syslog, then get out.
*/
Expand Down Expand Up @@ -1046,6 +1051,7 @@ void DispatchMessage(JobControlRecord *jcr, int type, utime_t mtime, char *msg)
break;
case MD_OPERATOR:
Dmsg1(850, "OPERATOR for following msg: %s\n", msg);
if (!jcr) { break; }
mcmd = GetPoolMemory(PM_MESSAGE);
if ((bpipe=open_mail_pipe(jcr, mcmd, d))) {
int status;
Expand Down Expand Up @@ -1105,6 +1111,7 @@ void DispatchMessage(JobControlRecord *jcr, int type, utime_t mtime, char *msg)
if (msgs->IsClosing()) {
break;
}
if (!jcr) { break; }
msgs->SetInUse();
if (!d->fd && !OpenDestFile(jcr, d, mode)) {
msgs->ClearInUse();
Expand Down
3 changes: 1 addition & 2 deletions core/src/stored/socket_server.cc
Expand Up @@ -136,8 +136,7 @@ void StartSocketServer(dlist *addrs)
void StopSocketServer()
{
if (sock_fds) {
BnetStopThreadServerTcp(tcp_server_tid);
CleanupBnetThreadServerTcp(sock_fds, &socket_workq);
BnetStopAndWaitForThreadServerTcp(tcp_server_tid);
delete sock_fds;
sock_fds = NULL;
}
Expand Down
41 changes: 23 additions & 18 deletions core/src/tests/CMakeLists.txt
Expand Up @@ -30,12 +30,33 @@ set (TEST_SRC
lib_tests.cc
)

set(LINK_LIBRARIES
stored_objects
dird_objects
console_objects
bareossd
bareos
bareoscats
bareossql
bareosfind
${LMDB_LIBS}
${NDMP_LIBS}
${JANSSON_LIBRARIES}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES})

IF(HAVE_PAM)
LIST(APPEND LINK_LIBRARIES ${PAM_LIBRARIES})
ENDIF()
add_definitions(-DTEST_SMALL_HTABLE)

# where to find the certificates
add_definitions(-DCERTDIR=\"${CMAKE_SOURCE_DIR}/../regress/configs/BASE/tls\")

# where to find the source dir
add_definitions(-DCMAKE_SOURCE_DIR=\"${CMAKE_SOURCE_DIR}\")

####### test_lib ###############################
add_executable(test_lib
${TEST_SRC}
)
Expand All @@ -59,31 +80,14 @@ set_property(TEST test_lib

add_dependencies(check test_lib)

####### test_bsock ###############################
add_executable(test_bsock
bsock_test.cc
bareos_test_sockets.cc
bsock_constructor_test.cc
bsock_cert_verify_common_names_test.cc
create_resource.cc
)
set(LINK_LIBRARIES
stored_objects
dird_objects
console_objects
bareossd
bareos
bareoscats
bareossql
bareosfind
${LMDB_LIBS}
${NDMP_LIBS}
${JANSSON_LIBRARIES}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES})

IF(HAVE_PAM)
LIST(APPEND LINK_LIBRARIES ${PAM_LIBRARIES})
ENDIF()

target_link_libraries(test_bsock ${LINK_LIBRARIES})

Expand All @@ -96,6 +100,7 @@ set_property(TEST test_bsock

add_dependencies(check test_bsock)

####### test_bsock ###############################
add_executable(test_connection_setup
bsock_test_connection_setup.cc
)
Expand Down

0 comments on commit 8f6a961

Please sign in to comment.