1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

fixes some memcheck and helgrind errors: initialization of lws_log; setup of client and server

This commit is contained in:
Falk Werner 2019-05-01 22:17:40 +02:00
parent f5be771a2d
commit 315bd27dbd
15 changed files with 157 additions and 151 deletions

View File

@ -76,6 +76,7 @@ add_library(webfuse-core STATIC
lib/webfuse/core/status.c lib/webfuse/core/status.c
lib/webfuse/core/string.c lib/webfuse/core/string.c
lib/webfuse/core/path.c lib/webfuse/core/path.c
lib/webfuse/core/lws_log.c
) )
set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core)

View File

@ -10,8 +10,8 @@
#include "webfuse/adapter/impl/server_config.h" #include "webfuse/adapter/impl/server_config.h"
#include "webfuse/adapter/impl/server_protocol.h" #include "webfuse/adapter/impl/server_protocol.h"
#include "webfuse/core/lws_log.h"
#define WF_DISABLE_LWS_LOG 0
#define WF_SERVER_PROTOCOL_COUNT 3 #define WF_SERVER_PROTOCOL_COUNT 3
struct wf_server struct wf_server
@ -33,7 +33,7 @@ static bool wf_impl_server_tls_enabled(
static struct lws_context * wf_impl_server_context_create( static struct lws_context * wf_impl_server_context_create(
struct wf_server * server) struct wf_server * server)
{ {
lws_set_log_level(WF_DISABLE_LWS_LOG, NULL); wf_lwslog_disable();
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT); memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http"; server->ws_protocols[0].name = "http";
@ -125,6 +125,12 @@ void wf_impl_server_dispose(
free(server); free(server);
} }
bool wf_impl_server_is_operational(
struct wf_server * server)
{
return server->protocol.is_operational;
}
void wf_impl_server_service( void wf_impl_server_service(
struct wf_server * server, struct wf_server * server,
int timeout_ms) int timeout_ms)

View File

@ -1,6 +1,10 @@
#ifndef WF_ADAPTER_IMPL_SERVER_H #ifndef WF_ADAPTER_IMPL_SERVER_H
#define WF_ADAPTER_IMPL_SERVER_H #define WF_ADAPTER_IMPL_SERVER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -15,6 +19,9 @@ extern struct wf_server * wf_impl_server_create(
extern void wf_impl_server_dispose( extern void wf_impl_server_dispose(
struct wf_server * server); struct wf_server * server);
extern bool wf_impl_server_is_operational(
struct wf_server * server);
extern void wf_impl_server_service( extern void wf_impl_server_service(
struct wf_server * server, struct wf_server * server,
int timeout_ms); int timeout_ms);

View File

@ -30,6 +30,9 @@ static int wf_impl_server_protocol_callback(
switch (reason) switch (reason)
{ {
case LWS_CALLBACK_PROTOCOL_INIT:
protocol->is_operational = true;
break;
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
session = wf_impl_session_manager_add( session = wf_impl_session_manager_add(
&protocol->session_manager, &protocol->session_manager,
@ -206,6 +209,7 @@ void wf_impl_server_protocol_init(
char * mount_point) char * mount_point)
{ {
protocol->mount_point = strdup(mount_point); protocol->mount_point = strdup(mount_point);
protocol->is_operational = false;
wf_impl_timeout_manager_init(&protocol->timeout_manager); wf_impl_timeout_manager_init(&protocol->timeout_manager);
wf_impl_session_manager_init(&protocol->session_manager); wf_impl_session_manager_init(&protocol->session_manager);
@ -220,6 +224,8 @@ void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol) struct wf_server_protocol * protocol)
{ {
free(protocol->mount_point); free(protocol->mount_point);
protocol->is_operational = false;
wf_impl_jsonrpc_server_cleanup(&protocol->server); wf_impl_jsonrpc_server_cleanup(&protocol->server);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager); wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_authenticators_cleanup(&protocol->authenticators); wf_impl_authenticators_cleanup(&protocol->authenticators);

View File

@ -7,6 +7,10 @@
#include "webfuse/adapter/impl/session_manager.h" #include "webfuse/adapter/impl/session_manager.h"
#include "webfuse/adapter/impl/jsonrpc/server.h" #include "webfuse/adapter/impl/jsonrpc/server.h"
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -21,6 +25,7 @@ struct wf_server_protocol
struct wf_impl_authenticators authenticators; struct wf_impl_authenticators authenticators;
struct wf_impl_session_manager session_manager; struct wf_impl_session_manager session_manager;
struct wf_impl_jsonrpc_server server; struct wf_impl_jsonrpc_server server;
bool is_operational;
}; };
extern void wf_impl_server_protocol_init( extern void wf_impl_server_protocol_init(

View File

@ -0,0 +1,18 @@
#include "webfuse/core/lws_log.h"
#include <stdbool.h>
#include <libwebsockets.h>
#define WF_LWSLOG_DISABLE 0
static bool wf_lwslog_is_diabled = false;
void wf_lwslog_disable(void)
{
if (!wf_lwslog_is_diabled)
{
lws_set_log_level(WF_LWSLOG_DISABLE, NULL);
wf_lwslog_is_diabled = true;
}
}

View File

@ -0,0 +1,15 @@
#ifndef WF_LWS_LOG_H
#define WF_LWS_LOG_H
#ifdef __cplusplus
extern "C"
{
#endif
extern void wf_lwslog_disable(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -10,9 +10,9 @@
#include "webfuse/provider/impl/client_protocol.h" #include "webfuse/provider/impl/client_protocol.h"
#include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/url.h" #include "webfuse/provider/impl/url.h"
#include "webfuse/core/lws_log.h"
#define WFP_PROTOCOL ("fs") #define WFP_PROTOCOL ("fs")
#define WFP_DISABLE_LWS_LOG 0
#define WFP_CLIENT_PROTOCOL_COUNT 2 #define WFP_CLIENT_PROTOCOL_COUNT 2
struct wfp_client struct wfp_client
@ -29,7 +29,7 @@ struct wfp_client
struct wfp_client * wfp_impl_client_create( struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config) struct wfp_client_config * config)
{ {
lws_set_log_level(WFP_DISABLE_LWS_LOG, NULL); wf_lwslog_disable();
struct wfp_client * client = malloc(sizeof(struct wfp_client)); struct wfp_client * client = malloc(sizeof(struct wfp_client));
if (NULL != client) if (NULL != client)
@ -99,6 +99,12 @@ void wfp_impl_client_disconnect(
// ToDo: implement me // ToDo: implement me
} }
bool wfp_impl_client_is_connected(
struct wfp_client * client)
{
return client->protocol.is_connected;
}
void wfp_impl_client_service( void wfp_impl_client_service(
struct wfp_client * client, struct wfp_client * client,
int timeout_ms) int timeout_ms)

View File

@ -1,6 +1,10 @@
#ifndef WF_PROVIDER_IMPL_CLIENT_H #ifndef WF_PROVIDER_IMPL_CLIENT_H
#define WF_PROVIDER_IMPL_CLIENT_H #define WF_PROVIDER_IMPL_CLIENT_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -27,13 +31,12 @@ extern void wfp_impl_client_connect(
extern void wfp_impl_client_disconnect( extern void wfp_impl_client_disconnect(
struct wfp_client * client); struct wfp_client * client);
extern void wfp_impl_client_settimeout(
struct wfp_client * client,
unsigned int timepoint);
extern void wfp_impl_client_dispose( extern void wfp_impl_client_dispose(
struct wfp_client * client); struct wfp_client * client);
extern bool wfp_impl_client_is_connected(
struct wfp_client * client);
extern void wfp_impl_client_service( extern void wfp_impl_client_service(
struct wfp_client * client, struct wfp_client * client,
int timeout_ms); int timeout_ms);

View File

@ -84,13 +84,16 @@ static int wfp_impl_client_protocol_callback(
{ {
case LWS_CALLBACK_CLIENT_ESTABLISHED: case LWS_CALLBACK_CLIENT_ESTABLISHED:
wfp_impl_client_protocol_add_filesystem(protocol); wfp_impl_client_protocol_add_filesystem(protocol);
protocol->is_connected = true;
protocol->provider.connected(protocol->user_data); protocol->provider.connected(protocol->user_data);
break; break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data); protocol->provider.disconnected(protocol->user_data);
break; break;
case LWS_CALLBACK_CLIENT_CLOSED: case LWS_CALLBACK_CLIENT_CLOSED:
protocol->provider.connected(protocol->user_data); protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
break; break;
case LWS_CALLBACK_CLIENT_RECEIVE: case LWS_CALLBACK_CLIENT_RECEIVE:
wfp_impl_client_protocol_process_request(protocol, in, len); wfp_impl_client_protocol_process_request(protocol, in, len);
@ -126,6 +129,7 @@ void wfp_impl_client_protocol_init(
struct wfp_provider const * provider, struct wfp_provider const * provider,
void * user_data) void * user_data)
{ {
protocol->is_connected = false;
wf_slist_init(&protocol->messages); wf_slist_init(&protocol->messages);
protocol->wsi = NULL; protocol->wsi = NULL;

View File

@ -16,6 +16,7 @@ struct lws_protocols;
struct wfp_client_protocol struct wfp_client_protocol
{ {
bool is_connected;
struct wfp_request request; struct wfp_request request;
struct wfp_provider provider; struct wfp_provider provider;
void * user_data; void * user_data;

View File

@ -1,5 +1,6 @@
#include "integration/provider.hpp" #include "integration/provider.hpp"
#include "webfuse_provider.h" #include "webfuse_provider.h"
#include "webfuse/provider/impl/client.h"
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -11,40 +12,33 @@ namespace webfuse_test
class Provider::Private class Provider::Private
{ {
public: public:
Private(char const * url_) Private(char const * url)
: url(url_) : is_shutdown_requested(false)
, is_running(false)
, is_shutdown_requested(false)
{ {
config = wfp_client_config_create();
fs = wfp_static_filesystem_create(config);
wfp_static_filesystem_add_text(fs, "hello.txt", 0x444, "Hello, World");
client = wfp_client_create(config);
wfp_client_connect(client, url);
while (!wfp_impl_client_is_connected(client))
{
wfp_client_service(client, 100);
}
thread = std::thread(Run, this);
} }
~Private() ~Private()
{
Stop();
}
void Start()
{
std::lock_guard<std::mutex> lock(run_lock);
if (!is_running)
{
thread = std::thread(Run, this);
is_running = true;
msleep(200);
}
}
void Stop()
{
std::lock_guard<std::mutex> lock(run_lock);
if (is_running)
{ {
RequestShutdown(); RequestShutdown();
thread.join(); thread.join();
is_running = false;
} wfp_client_dispose(client);
wfp_static_filesystem_dispose(fs);
wfp_client_config_dispose(config);
} }
bool IsShutdownRequested() bool IsShutdownRequested()
@ -52,11 +46,6 @@ public:
std::lock_guard<std::mutex> lock(shutdown_lock); std::lock_guard<std::mutex> lock(shutdown_lock);
return is_shutdown_requested; return is_shutdown_requested;
} }
char const * GetUrl() const
{
return url.c_str();
}
private: private:
void RequestShutdown() void RequestShutdown()
{ {
@ -66,36 +55,20 @@ private:
static void Run(Provider::Private * context) static void Run(Provider::Private * context)
{ {
wfp_client_config * config = wfp_client_config_create();
wfp_static_filesystem * fs = wfp_static_filesystem_create(config);
wfp_static_filesystem_add_text(fs, "hello.txt", 0x444, "Hello, World");
wfp_client * client = wfp_client_create(config);
if (nullptr != client)
{
wfp_client_connect(client, context->GetUrl());
while (!context->IsShutdownRequested()) while (!context->IsShutdownRequested())
{ {
wfp_client_service(client, 100); wfp_client_service(context->client, 100);
}
} }
wfp_client_dispose(client);
}
wfp_static_filesystem_dispose(fs);
wfp_client_config_dispose(config);
}
std::string url;
bool is_running;
std::mutex run_lock;
bool is_shutdown_requested;
std::mutex shutdown_lock; std::mutex shutdown_lock;
std::thread thread; std::thread thread;
bool is_shutdown_requested;
wfp_client_config * config;
wfp_static_filesystem * fs;
public:
wfp_client * client;
}; };
Provider::Provider(char const * url) Provider::Provider(char const * url)
@ -108,14 +81,4 @@ Provider::~Provider()
delete d; delete d;
} }
void Provider::Start(void)
{
d->Start();
}
void Provider::Stop(void)
{
d->Stop();
}
} }

View File

@ -9,8 +9,6 @@ class Provider
public: public:
Provider(char const * url); Provider(char const * url);
~Provider(); ~Provider();
void Start(void);
void Stop(void);
private: private:
class Private; class Private;
Private * d; Private * d;

View File

@ -5,6 +5,7 @@
#include <cstring> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include "webfuse_adapter.h" #include "webfuse_adapter.h"
#include "webfuse/adapter/impl/server.h"
#include "msleep.hpp" #include "msleep.hpp"
#define WF_PATH_MAX (100) #define WF_PATH_MAX (100)
@ -16,41 +17,33 @@ class Server::Private
{ {
public: public:
Private() Private()
: is_running(false) : is_shutdown_requested(false)
, is_shutdown_requested(false)
{ {
snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX"); snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX");
mkdtemp(base_dir); mkdtemp(base_dir);
config = wf_server_config_create();
wf_server_config_set_port(config, 8080);
wf_server_config_set_mountpoint(config, base_dir);
server = wf_server_create(config);
while (!wf_impl_server_is_operational(server))
{
wf_server_service(server, 100);
}
thread = std::thread(Run, this);
} }
~Private() ~Private()
{
Stop();
rmdir(base_dir);
}
void Start()
{
std::lock_guard<std::mutex> lock(run_lock);
if (!is_running)
{
thread = std::thread(Run, this);
is_running = true;
msleep(200);
}
}
void Stop()
{
std::lock_guard<std::mutex> lock(run_lock);
if (is_running)
{ {
RequestShutdown(); RequestShutdown();
thread.join(); thread.join();
is_running = false; rmdir(base_dir);
} wf_server_dispose(server);
wf_server_config_dispose(config);
} }
bool IsShutdownRequested() bool IsShutdownRequested()
@ -59,11 +52,6 @@ public:
return is_shutdown_requested; return is_shutdown_requested;
} }
char const * GetBaseDir()
{
return base_dir;
}
private: private:
void RequestShutdown() void RequestShutdown()
{ {
@ -72,34 +60,23 @@ private:
} }
static void Run(Server::Private * context) static void Run(Server::Private * context)
{
wf_server_config * config = wf_server_config_create();
wf_server_config_set_port(config, 8080);
wf_server_config_set_mountpoint(config, context->GetBaseDir());
wf_server * server = wf_server_create(config);
if (nullptr != server)
{ {
while (!context->IsShutdownRequested()) while (!context->IsShutdownRequested())
{ {
wf_server_service(server, 100); wf_server_service(context->server, 100);
}
} }
wf_server_dispose(server);
}
wf_server_config_dispose(config);
}
char base_dir[WF_PATH_MAX];
bool is_running;
std::mutex run_lock;
bool is_shutdown_requested;
std::mutex shutdown_lock; std::mutex shutdown_lock;
std::thread thread; std::thread thread;
bool is_shutdown_requested;
public:
char base_dir[WF_PATH_MAX];
wf_server_config * config;
wf_server * server;
}; };
Server::Server() Server::Server()
@ -113,19 +90,9 @@ Server::~Server()
delete d; delete d;
} }
void Server::Start(void)
{
d->Start();
}
void Server::Stop(void)
{
d->Stop();
}
char const * Server::GetBaseDir(void) const char const * Server::GetBaseDir(void) const
{ {
return d->GetBaseDir(); return d->base_dir;
} }

View File

@ -2,12 +2,17 @@
#include "integration/server.hpp" #include "integration/server.hpp"
#include "integration/provider.hpp" #include "integration/provider.hpp"
#include <csignal>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <jansson.h>
#include "webfuse/core/lws_log.h"
using webfuse_test::Server; using webfuse_test::Server;
using webfuse_test::Provider; using webfuse_test::Provider;
@ -17,34 +22,35 @@ namespace
{ {
public: public:
IntegrationTest() IntegrationTest()
: provider("ws://localhost:8080/")
{ {
json_object_seed(0);
wf_lwslog_disable();
} }
protected: protected:
void SetUp() void SetUp()
{ {
server.Start(); server = new Server();
provider.Start(); provider = new Provider("ws://localhost:8080/");
} }
void TearDown() void TearDown()
{ {
provider.Stop(); delete provider;
server.Stop(); delete server;
} }
char const * GetBaseDir() const char const * GetBaseDir() const
{ {
return server.GetBaseDir(); return server->GetBaseDir();
} }
private: private:
Server server; Server * server;
Provider provider; Provider * provider;
}; };
} }
TEST_F(IntegrationTest, DISABLED_HasMountpoint) TEST_F(IntegrationTest, HasMountpoint)
{ {
struct stat buffer; struct stat buffer;
int rc = stat(GetBaseDir(), &buffer); int rc = stat(GetBaseDir(), &buffer);
@ -53,7 +59,7 @@ TEST_F(IntegrationTest, DISABLED_HasMountpoint)
ASSERT_TRUE(S_ISDIR(buffer.st_mode)); ASSERT_TRUE(S_ISDIR(buffer.st_mode));
} }
TEST_F(IntegrationTest, DISABLED_ProvidesTextFile) TEST_F(IntegrationTest, ProvidesTextFile)
{ {
std::string file_name = std::string(GetBaseDir()) + "/cprovider/default/hello.txt"; std::string file_name = std::string(GetBaseDir()) + "/cprovider/default/hello.txt";