Skip to content

Commit

Permalink
Merge pull request #415 from aschnell/master
Browse files Browse the repository at this point in the history
- fixed logging during shutdown
  • Loading branch information
aschnell authored Jul 24, 2018
2 parents 2fbeb7d + 7f293a9 commit ff8de73
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 113 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.5
0.5.6
7 changes: 7 additions & 0 deletions package/snapper.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon Jul 23 20:52:26 CEST 2018 - [email protected]

- fixed logging during shutdown of snapperd to avoid core dumps
(bsc#1096401 and others)
- version 0.5.6

-------------------------------------------------------------------
Mon May 28 10:44:49 CEST 2018 - [email protected]

Expand Down
11 changes: 5 additions & 6 deletions server/Client.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) [2012-2015] Novell, Inc.
* Copyright (c) 2016 SUSE LLC
* Copyright (c) [2016,2018] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -68,11 +68,9 @@ Client::~Client()
Snapshots& snapshots = snapper->getSnapshots();

Snapshots::iterator snap = snapshots.find(number);
if (snap == snapshots.end())
throw IllegalSnapshotException();

for (unsigned int i = 0; i < use_count; ++i)
snap->umountFilesystemSnapshot(false);
if (snap != snapshots.end())
for (unsigned int i = 0; i < use_count; ++i)
snap->umountFilesystemSnapshot(false);
}
}

Expand Down Expand Up @@ -115,6 +113,7 @@ Client::delete_comparison(list<Comparison*>::iterator it)
}

delete *it;
*it = nullptr;
}


Expand Down
12 changes: 11 additions & 1 deletion server/MetaSnapper.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) [2012-2015] Novell, Inc.
* Copyright (c) 2018 SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -103,6 +104,7 @@ MetaSnapper::MetaSnapper(ConfigInfo& config_info)
MetaSnapper::~MetaSnapper()
{
delete snapper;
snapper = nullptr;
}


Expand Down Expand Up @@ -172,7 +174,7 @@ void
MetaSnapper::unload()
{
delete snapper;
snapper = NULL;
snapper = nullptr;
}


Expand All @@ -198,6 +200,14 @@ MetaSnappers::init()
}


void
MetaSnappers::unload()
{
for (MetaSnapper& meta_snapper : entries)
meta_snapper.unload();
}


MetaSnappers::iterator
MetaSnappers::find(const string& config_name)
{
Expand Down
3 changes: 3 additions & 0 deletions server/MetaSnapper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) [2012-2015] Novell, Inc.
* Copyright (c) 2018 SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -123,6 +124,8 @@ class MetaSnappers

void init();

void unload();

typedef list<MetaSnapper>::iterator iterator;
typedef list<MetaSnapper>::const_iterator const_iterator;

Expand Down
3 changes: 3 additions & 0 deletions server/snapperd.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) [2012-2015] Novell, Inc.
* Copyright (c) 2018 SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -286,5 +287,7 @@ main(int argc, char** argv)

y2mil("Exiting");

meta_snappers.unload();

return 0;
}
1 change: 1 addition & 0 deletions snapper/Log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace snapper
{
callLogDo(level, *component, file, line, func, stream->str());
delete stream;
stream = nullptr;
}

}
39 changes: 27 additions & 12 deletions snapper/Logger.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) [2004-2013] Novell, Inc.
* Copyright (c) 2018 SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -32,15 +33,33 @@
#include "snapper/AppUtil.h"


#define LOG_FILENAME "/var/log/snapper.log"


namespace snapper
{
using namespace std;


// Use a pointer to avoid a global destructor. Otherwise the Snapper
// destructor in Factory.cc can be called after when logging does not work
// anymore. TODO: nicer solution.
string* filename = new string("/var/log/snapper.log");
namespace
{

struct LoggerData
{
LoggerData() : filename(LOG_FILENAME), mutex() {}

string filename;
boost::mutex mutex;
};


// Use pointer to avoid a global destructor. Otherwise other global
// destructors using logging can cause errors.

LoggerData* logger_data = new LoggerData();

}


LogDo log_do = NULL;
LogQuery log_query = NULL;
Expand Down Expand Up @@ -69,11 +88,9 @@ namespace snapper
string prefix = sformat("%s %s libsnapper(%d) %s(%s):%d", datetime(time(0), false, true).c_str(),
ln[level], getpid(), file, func, line);

static boost::mutex mutex;

boost::lock_guard<boost::mutex> lock(mutex);
boost::lock_guard<boost::mutex> lock(logger_data->mutex);

FILE* f = fopen(filename->c_str(), "ae");
FILE* f = fopen(logger_data->filename.c_str(), "ae");
if (f)
{
string tmp = text;
Expand Down Expand Up @@ -137,8 +154,7 @@ namespace snapper
void
initDefaultLogger()
{
delete filename;
filename = new string("/var/log/snapper.log");
logger_data->filename = LOG_FILENAME;

if (geteuid())
{
Expand All @@ -152,8 +168,7 @@ namespace snapper
{
memset(pwd.pw_passwd, 0, strlen(pwd.pw_passwd));

delete filename;
filename = new string(string(pwd.pw_dir) + "/.snapper.log");
logger_data->filename = string(pwd.pw_dir) + "/.snapper.log";
}
}

Expand Down
5 changes: 4 additions & 1 deletion snapper/Snapper.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) [2011-2015] Novell, Inc.
* Copyright (c) 2016 SUSE LLC
* Copyright (c) [2016,2018] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -147,7 +147,10 @@ namespace snapper
}

delete filesystem;
filesystem = nullptr;

delete config_info;
config_info = nullptr;
}


Expand Down
99 changes: 7 additions & 92 deletions snapper/SnapperTmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef SNAPPER_SNAPPER_TMPL_H
#define SNAPPER_SNAPPER_TMPL_H

#include <functional>

#include <ostream>
#include <fstream>
#include <sstream>
Expand All @@ -32,6 +32,7 @@

#include "snapper/AppUtil.h"


namespace snapper
{
using std::string;
Expand All @@ -47,6 +48,7 @@ namespace snapper
return num_str.str();
}


template<class Num> string hexString(Num number)
{
static_assert(std::is_integral<Num>::value, "not integral");
Expand All @@ -57,13 +59,15 @@ namespace snapper
return num_str.str();
}


template<class Value> void operator>>(const string& d, Value& v)
{
std::istringstream Data(d);
classic(Data);
Data >> v;
}


template<class Value> std::ostream& operator<<( std::ostream& s, const std::list<Value>& l )
{
s << "<";
Expand All @@ -77,12 +81,14 @@ namespace snapper
return( s );
}


template<class F, class S> std::ostream& operator<<( std::ostream& s, const std::pair<F,S>& p )
{
s << "[" << p.first << ":" << p.second << "]";
return( s );
}


template<class Key, class Value> std::ostream& operator<<( std::ostream& s, const std::map<Key,Value>& m )
{
s << "<";
Expand All @@ -97,76 +103,6 @@ namespace snapper
}


template <typename Type>
void logDiff(std::ostream& log, const char* text, const Type& lhs, const Type& rhs)
{
static_assert(!std::is_enum<Type>::value, "is enum");

if (lhs != rhs)
log << " " << text << ":" << lhs << "-->" << rhs;
}

template <typename Type>
void logDiffHex(std::ostream& log, const char* text, const Type& lhs, const Type& rhs)
{
static_assert(std::is_integral<Type>::value, "not integral");

if (lhs != rhs)
log << " " << text << ":" << std::hex << lhs << "-->" << rhs << std::dec;
}

template <typename Type>
void logDiffEnum(std::ostream& log, const char* text, const Type& lhs, const Type& rhs)
{
static_assert(std::is_enum<Type>::value, "not enum");

if (lhs != rhs)
log << " " << text << ":" << toString(lhs) << "-->" << toString(rhs);
}

inline
void logDiff(std::ostream& log, const char* text, bool lhs, bool rhs)
{
if (lhs != rhs)
{
if (rhs)
log << " -->" << text;
else
log << " " << text << "-->";
}
}


template<class Type>
struct deref_less : public std::binary_function<const Type*, const Type*, bool>
{
bool operator()(const Type* x, const Type* y) const { return *x < *y; }
};


template<class Type>
struct deref_equal_to : public std::binary_function<const Type*, const Type*, bool>
{
bool operator()(const Type* x, const Type* y) const { return *x == *y; }
};


template <class Type>
void pointerIntoSortedList(list<Type*>& l, Type* e)
{
l.insert(lower_bound(l.begin(), l.end(), e, deref_less<Type>()), e);
}


template <class Type>
void clearPointerList(list<Type*>& l)
{
for (typename list<Type*>::iterator i = l.begin(); i != l.end(); ++i)
delete *i;
l.clear();
}


template <typename ListType, typename Type>
bool contains(const ListType& l, const Type& value)
{
Expand All @@ -181,27 +117,6 @@ namespace snapper
}


template<typename Map, typename Key, typename Value>
typename Map::iterator mapInsertOrReplace(Map& m, const Key& k, const Value& v)
{
typename Map::iterator pos = m.lower_bound(k);
if (pos != m.end() && !typename Map::key_compare()(k, pos->first))
pos->second = v;
else
pos = m.insert(pos, typename Map::value_type(k, v));
return pos;
}

template<typename List, typename Value>
typename List::const_iterator addIfNotThere(List& l, const Value& v)
{
typename List::const_iterator pos = find( l.begin(), l.end(), v );
if (pos == l.end() )
pos = l.insert(l.end(), v);
return pos;
}


template <class T, unsigned int sz>
inline unsigned int lengthof(T (&)[sz]) { return sz; }

Expand Down

0 comments on commit ff8de73

Please sign in to comment.