Skip to content

Commit

Permalink
Merge pull request #172 from imzhenyu/master
Browse files Browse the repository at this point in the history
enhancement in linux deployment
  • Loading branch information
imzhenyu authored Sep 7, 2016
2 parents 64f48f2 + 4cb4b19 commit ad1f026
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 38 deletions.
5 changes: 2 additions & 3 deletions bin/Linux/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ function clean_files(){
#$1 machine $2 app $3 sdir $4 tdir
function start_server(){
echo "starting $2 at $1"

ssh $1 'cd '${4}';nohup sh -c "(( ./start.sh >foo.out 2>foo.err </dev/null)&)"'
ssh $1 'cd '${4}'; export LD_LIBRARY_PATH=${4}:$LD_LIBRARY_PATH; nohup sh -c "(( ./start.sh >foo.out 2>foo.err </dev/null)&)"'
}

#$1 machine $2 app $3 sdir $4 tdir
function stop_server(){
echo "stopping $2 at $1"
ssh $1 'cd '${4}';nohup sh -c "(( ./stop.sh >foo.out 2>foo.err </dev/null)&)"'
ssh $1 'cd '${4}'; export LD_LIBRARY_PATH=${4}:$LD_LIBRARY_PATH; nohup sh -c "(( ./stop.sh >foo.out 2>foo.err </dev/null)&)"'
}

#$1 cmd $2 app $3 sdir $4 tdir
Expand Down
36 changes: 29 additions & 7 deletions src/core/src/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
# include <ifaddrs.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/ioctl.h>
# include <net/if.h>

# if defined(__FreeBSD__)
# include <netinet/in.h>
Expand Down Expand Up @@ -136,17 +138,37 @@ DSN_API uint32_t dsn_ipv4_local(const char* network_interface)
while (i != nullptr)
{
if (i->ifa_name != nullptr &&
i->ifa_addr != nullptr &&
i->ifa_addr->sa_family == AF_INET
i->ifa_addr != nullptr
)
{
if (strcmp(i->ifa_name, network_interface) == 0 ||
(network_interface[0] == '\0'
&& strncmp(i->ifa_name, "eth", 3) == 0
&& strncmp((const char*)&((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr, loopback, 4) != 0)
(network_interface[0] == '\0' && strncmp(i->ifa_name, "eth", 3) == 0)
)
ret = (uint32_t)ntohl(((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr);
break;
{
if (i->ifa_addr->sa_family == AF_INET &&
strncmp((const char*)&((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr, loopback, 4) != 0)
{
ret = (uint32_t)ntohl(((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr);
break;
}

// sometimes the sa_family is not AF_INET but we can still try
else
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;

ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, i->ifa_name, IFNAMSIZ - 1);

auto err = ioctl(fd, SIOCGIFADDR, &ifr);
if (err == 0 && strncmp((const char*)&((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr, loopback, 4) != 0)
{
ret = (uint32_t)ntohl(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
break;
}
}
}
}
i = i->ifa_next;
}
Expand Down
56 changes: 39 additions & 17 deletions src/core/src/library_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,70 @@ namespace dsn {
namespace utils {


dsn_handle_t load_dynamic_library(const char* module)
dsn_handle_t load_dynamic_library(const char* module, const std::vector<std::string>& search_dirs)
{
std::string module_name(module);
# if defined(_WIN32)
module_name += ".dll";
auto hmod = ::LoadLibraryA(module_name.c_str());
if (hmod == NULL)
{
derror("load dynamic library '%s' failed, err = %d", module_name.c_str(), ::GetLastError());
}
# elif defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
module_name = "lib" + module_name + ".so";
auto hmod = dlopen(module_name.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (nullptr == hmod)
# else
# error not implemented yet
# endif

// search given dirs with priority
std::vector<std::string> passes;
for (auto & dr : search_dirs)
{
derror("load dynamic library '%s' failed, err = %s", module_name.c_str(), dlerror());
passes.push_back(utils::filesystem::path_combine(dr, module_name));
}

// search OS given passes
passes.push_back(module_name);

// try
dsn_handle_t hmod = nullptr;
for (auto & m : passes)
{
# if defined(_WIN32)
hmod = (dsn_handle_t)::LoadLibraryA(m.c_str());
if (hmod == nullptr)
{
ddebug("load dynamic library '%s' failed, err = %d", m.c_str(), ::GetLastError());
}
# else
# error not implemented yet
hmod = (dsn_handle_t)dlopen(m.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (nullptr == hmod)
{
ddebug("load dynamic library '%s' failed, err = %s", m.c_str(), dlerror());
}
# endif
return (dsn_handle_t)(hmod);
else
break;
}

if (hmod == nullptr)
{
derror("load dynamic library '%s' failed, check logs for details", module_name.c_str());
}
return hmod;
}

dsn_handle_t load_symbol(dsn_handle_t hmodule, const char* symbol)
{
# if defined(_WIN32)
return (dsn_handle_t)::GetProcAddress((HMODULE)hmodule, (LPCSTR)symbol);
# elif defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
return (dsn_handle_t)dlsym((void*)hmodule, symbol);
# else
# error not implemented yet
return (dsn_handle_t)dlsym((void*)hmodule, symbol);
# endif
}

void unload_dynamic_library(dsn_handle_t hmodule)
{
# if defined(_WIN32)
::CloseHandle((HMODULE)hmodule);
# elif defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
dlclose((void*)hmodule);
# else
# error not implemented yet
dlclose((void*)hmodule);
# endif
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/library_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

# include <dsn/utility/ports.h>
# include <dsn/cpp/auto_codes.h>
# include <vector>

# ifdef __TITLE__
# undef __TITLE__
Expand All @@ -46,7 +47,7 @@
namespace dsn {
namespace utils {

extern dsn_handle_t load_dynamic_library(const char* module);
extern dsn_handle_t load_dynamic_library(const char* module, const std::vector<std::string>& search_dirs);

extern dsn_handle_t load_symbol(dsn_handle_t hmodule, const char* symbol);

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/library_utils.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ using namespace ::dsn;

TEST(core, load_dynamic_library)
{
ASSERT_FALSE(dsn::utils::load_dynamic_library("not_exist_module") != nullptr);
ASSERT_FALSE(dsn::utils::load_dynamic_library("not_exist_module", {}) != nullptr);
}
12 changes: 4 additions & 8 deletions src/core/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,18 @@ static void load_all_modules(::dsn::configuration_ptr config)
}
}

// do the real job
// prepare search dirs
std::string config_file_path = config->get_file_name();
std::string absolute_path;
::dsn::utils::filesystem::get_absolute_path(config_file_path, absolute_path);
std::string file_name = ::dsn::utils::filesystem::get_file_name(absolute_path);
absolute_path = absolute_path.substr(0, absolute_path.length() - file_name.length() - 1);
std::vector<std::string> search_dirs = { absolute_path };

// do the real jobs
for (auto m : modules)
{
auto hmod = ::dsn::utils::load_dynamic_library(m.first.c_str());
if (nullptr == hmod)
{
auto spath = ::dsn::utils::filesystem::path_combine(absolute_path, m.first);
hmod = ::dsn::utils::load_dynamic_library(spath.c_str());
}

auto hmod = ::dsn::utils::load_dynamic_library(m.first.c_str(), search_dirs);
if (nullptr == hmod)
{
dassert(false, "cannot load shared library %s specified in config file",
Expand Down

0 comments on commit ad1f026

Please sign in to comment.