diff options
-rw-r--r-- | inc/abrtlib.h | 5 | ||||
-rw-r--r-- | lib/Utils/DebugDump.cpp | 47 | ||||
-rw-r--r-- | lib/Utils/DebugDump.h | 8 | ||||
-rw-r--r-- | lib/Utils/Makefile.am | 8 | ||||
-rw-r--r-- | lib/Utils/dirsize.cpp | 100 | ||||
-rw-r--r-- | src/Daemon/CrashWatcher.cpp | 7 | ||||
-rw-r--r-- | src/Daemon/Daemon.cpp | 58 | ||||
-rw-r--r-- | src/Daemon/MiddleWare.cpp | 7 | ||||
-rw-r--r-- | src/Daemon/MiddleWare.h | 5 | ||||
-rw-r--r-- | src/Hooks/CCpp.cpp | 55 | ||||
-rw-r--r-- | src/Hooks/Makefile.am | 2 |
11 files changed, 212 insertions, 90 deletions
diff --git a/inc/abrtlib.h b/inc/abrtlib.h index 257f5571..0c1595a1 100644 --- a/inc/abrtlib.h +++ b/inc/abrtlib.h @@ -208,6 +208,11 @@ bool string_to_bool(const char *s); std::string ssprintf(const char *format, ...); std::string get_home_dir(int uid); std::string concat_path_file(const char *path, const char *filename); +double get_dirsize(const char *pPath); +double get_dirsize_find_largest_dir( + const char *pPath, + std::string *worst_dir = NULL, + const char *excluded = NULL); template <class T> std::string diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index fff46957..3e226f9a 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -46,7 +46,19 @@ static std::string RemoveBackSlashes(const char *pDir) return std::string(pDir, len); } -static bool ExistFileDir(const char* pPath); +static bool ExistFileDir(const char *pPath) +{ + struct stat buf; + if (stat(pPath, &buf) == 0) + { + if (S_ISDIR(buf.st_mode) || S_ISREG(buf.st_mode)) + { + return true; + } + } + return false; +} + static void LoadTextFile(const char *pPath, std::string& pData); CDebugDump::CDebugDump() : @@ -60,12 +72,12 @@ void CDebugDump::Open(const char *pDir) { if (m_bOpened) { - throw CABRTException(EXCEP_ERROR, "CDebugDump::CDebugDump(): DebugDump is already opened."); + throw CABRTException(EXCEP_ERROR, "CDebugDump is already opened"); } m_sDebugDumpDir = RemoveBackSlashes(pDir); if (!ExistFileDir(m_sDebugDumpDir.c_str())) { - throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): " + m_sDebugDumpDir + " does not exist."); + throw CABRTException(EXCEP_DD_OPEN, m_sDebugDumpDir + " does not exist"); } Lock(); m_bOpened = true; @@ -77,20 +89,6 @@ bool CDebugDump::Exist(const char* pPath) return ExistFileDir(fullPath.c_str()); } - -static bool ExistFileDir(const char* pPath) -{ - struct stat buf; - if (stat(pPath, &buf) == 0) - { - if (S_ISDIR(buf.st_mode) || S_ISREG(buf.st_mode)) - { - return true; - } - } - return false; -} - static bool GetAndSetLock(const char* pLockFile, const char* pPID) { while (symlink(pPID, pLockFile) != 0) @@ -429,3 +427,18 @@ bool CDebugDump::GetNextFile(std::string *short_name, std::string *full_name) m_pGetNextFileDir = NULL; return false; } + +/* Utility function */ +void delete_debug_dump_dir(const char *pDebugDumpDir) +{ + try + { + CDebugDump dd; + dd.Open(pDebugDumpDir); + dd.Delete(); + } + catch (CABRTException& e) + { + /* Ignoring "directory already deleted" and such */ + } +} diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index d7533534..7dfe61ea 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -76,4 +76,10 @@ class CDebugDump bool GetNextFile(std::string *short_name, std::string *full_name); }; -#endif /*DEBUGDUMP_H_*/ +/** + * Deletes particular debugdump directory. + * @param pDebugDumpDir A debugdump directory. + */ +void delete_debug_dump_dir(const char *pDebugDumpDir); + +#endif diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am index f0b44810..427e437c 100644 --- a/lib/Utils/Makefile.am +++ b/lib/Utils/Makefile.am @@ -1,5 +1,5 @@ # ABRTUtils has small set of deps. This reduces deps of smaller abrt binaries -# ABRTdUtils has more. It is used by daemon and plugins only +# ABRTdUtils has much more. It is used by daemon and plugins only lib_LTLIBRARIES = libABRTUtils.la libABRTdUtils.la # Not used just yet: @@ -8,7 +8,6 @@ lib_LTLIBRARIES = libABRTUtils.la libABRTdUtils.la # removed: CrashTypesSocket.cpp libABRTUtils_la_SOURCES = \ - stringops.cpp \ xfuncs.cpp \ encbase64.cpp \ read_write.cpp \ @@ -16,6 +15,8 @@ libABRTUtils_la_SOURCES = \ copyfd.cpp \ skip_whitespace.cpp \ popen_and_save_output.cpp \ + stringops.cpp \ + dirsize.cpp \ DebugDump.h DebugDump.cpp libABRTUtils_la_CPPFLAGS = \ -Wall -Werror \ @@ -35,10 +36,11 @@ libABRTUtils_la_LIBADD = libABRTdUtils_la_SOURCES = \ parse_release.cpp \ + make_descr.cpp \ CommLayerInner.h CommLayerInner.cpp \ abrt_dbus.h abrt_dbus.cpp \ abrt_xmlrpc.h abrt_xmlrpc.cpp \ - Plugin.h Plugin.cpp make_descr.cpp \ + Plugin.h Plugin.cpp \ Polkit.h Polkit.cpp \ Action.h Database.h Reporter.h Analyzer.h \ Observer.h \ diff --git a/lib/Utils/dirsize.cpp b/lib/Utils/dirsize.cpp new file mode 100644 index 00000000..739b6b73 --- /dev/null +++ b/lib/Utils/dirsize.cpp @@ -0,0 +1,100 @@ +/* + Copyright (C) 2009 Jiri Moskovcak (jmoskovc@redhat.com) + Copyright (C) 2009 RedHat inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include "abrtlib.h" + +using namespace std; + +double get_dirsize(const char *pPath) +{ + DIR *dp = opendir(pPath); + if (dp == NULL) + return 0; + + struct dirent *ep; + struct stat statbuf; + double size = 0; + while ((ep = readdir(dp)) != NULL) + { + if (dot_or_dotdot(ep->d_name)) + continue; + string dname = concat_path_file(pPath, ep->d_name); + if (lstat(dname.c_str(), &statbuf) != 0) + continue; + if (S_ISDIR(statbuf.st_mode)) + { + size += get_dirsize(dname.c_str()); + } + else if (S_ISREG(statbuf.st_mode)) + { + size += statbuf.st_size; + } + } + closedir(dp); + return size; +} + +double get_dirsize_find_largest_dir( + const char *pPath, + string *worst_dir, + const char *excluded) +{ + DIR *dp = opendir(pPath); + if (dp == NULL) + return 0; + + struct dirent *ep; + struct stat statbuf; + double size = 0; + double maxsz = 0; + while ((ep = readdir(dp)) != NULL) + { + if (dot_or_dotdot(ep->d_name)) + continue; + string dname = concat_path_file(pPath, ep->d_name); + if (lstat(dname.c_str(), &statbuf) != 0) + continue; + if (S_ISDIR(statbuf.st_mode)) + { + double sz = get_dirsize(dname.c_str()); + size += sz; + + if (worst_dir && (!excluded || strcmp(excluded, ep->d_name) != 0)) + { + /* Calculate "weighted" size and age + * w = sz_kbytes * age_mins */ + sz /= 1024; + long age = (time(NULL) - statbuf.st_mtime) / 60; + if (age > 0) + sz *= age; + + if (sz > maxsz) + { + maxsz = sz; + *worst_dir = ep->d_name; + } + } + } + else if (S_ISREG(statbuf.st_mode)) + { + size += statbuf.st_size; + } + } + closedir(dp); + return size; +} diff --git a/src/Daemon/CrashWatcher.cpp b/src/Daemon/CrashWatcher.cpp index f8595157..b82823c5 100644 --- a/src/Daemon/CrashWatcher.cpp +++ b/src/Daemon/CrashWatcher.cpp @@ -20,6 +20,7 @@ #include "abrtlib.h" #include "Daemon.h" #include "ABRTException.h" +#include "DebugDump.h" #include "CrashWatcher.h" void CCrashWatcher::Status(const char *pMessage, const char* peer, uint64_t pJobID) @@ -75,7 +76,7 @@ vector_crash_infos_t GetCrashInfos(const char *pUID) error_msg("Can't open file in dump directory for UUID %s, deleting", uuid); { std::string debugDumpDir = DeleteCrashInfo(uuid, uid); - DeleteDebugDumpDir(debugDumpDir.c_str()); + delete_debug_dump_dir(debugDumpDir.c_str()); } break; default: @@ -132,7 +133,7 @@ map_crash_report_t GetJobResult(const char* pUUID, const char* pUID, int force) default: error_msg("Corrupted crash with UUID %s, deleting", pUUID); std::string debugDumpDir = DeleteCrashInfo(pUUID, pUID); - DeleteDebugDumpDir(debugDumpDir.c_str()); + delete_debug_dump_dir(debugDumpDir.c_str()); break; } return crashReport; @@ -211,7 +212,7 @@ bool DeleteDebugDump(const char *pUUID, const char *pUID) try { std::string debugDumpDir = DeleteCrashInfo(pUUID, pUID); - DeleteDebugDumpDir(debugDumpDir.c_str()); + delete_debug_dump_dir(debugDumpDir.c_str()); } catch (CABRTException& e) { diff --git a/src/Daemon/Daemon.cpp b/src/Daemon/Daemon.cpp index 087d5373..504b4f04 100644 --- a/src/Daemon/Daemon.cpp +++ b/src/Daemon/Daemon.cpp @@ -41,6 +41,7 @@ #include "abrtlib.h" #include "ABRTException.h" #include "CrashWatcher.h" +#include "DebugDump.h" #include "Daemon.h" @@ -118,53 +119,6 @@ static bool s_exiting; CCommLayerServer* g_pCommLayer; -static double GetDirSize(const std::string &pPath, std::string *worst_dir = NULL, const char *excluded = NULL) -{ - DIR *dp = opendir(pPath.c_str()); - if (dp == NULL) - return 0; - - struct dirent *ep; - struct stat stats; - double size = 0; - double maxsz = 0; - while ((ep = readdir(dp)) != NULL) - { - if (dot_or_dotdot(ep->d_name)) - continue; - std::string dname = pPath + "/" + ep->d_name; - if (lstat(dname.c_str(), &stats) != 0) - continue; - if (S_ISDIR(stats.st_mode)) - { - double sz = GetDirSize(dname); - size += sz; - - if (worst_dir && strcmp(excluded, ep->d_name) != 0) - { - /* Calculate "weighted" size and age - * w = sz_kbytes * age_mins */ - sz /= 1024; - long age = (time(NULL) - stats.st_mtime) / 60; - if (age > 0) - sz *= age; - - if (sz > maxsz) - { - maxsz = sz; - *worst_dir = ep->d_name; - } - } - } - else if (S_ISREG(stats.st_mode)) - { - size += stats.st_size; - } - } - closedir(dp); - return size; -} - static void cron_delete_callback_data_cb(gpointer data) { cron_callback_data_t* cronDeleteCallbackData = static_cast<cron_callback_data_t*>(data); @@ -385,7 +339,7 @@ static void FindNewDumps(const char* pPath) case MW_REPORTED: case MW_OCCURED: VERB1 log("Already saved crash %s, deleting", itt->c_str()); - DeleteDebugDumpDir(itt->c_str()); + delete_debug_dump_dir(itt->c_str()); break; case MW_BLACKLISTED: case MW_CORRUPTED: @@ -394,7 +348,7 @@ static void FindNewDumps(const char* pPath) case MW_FILE_ERROR: default: log("Corrupted or bad crash %s (res:%d), deleting", itt->c_str(), (int)res); - DeleteDebugDumpDir(itt->c_str()); + delete_debug_dump_dir(itt->c_str()); break; } } @@ -513,12 +467,12 @@ static gboolean handle_inotify_cb(GIOChannel *gio, GIOCondition condition, gpoin std::string worst_dir; while (g_settings_nMaxCrashReportsSize > 0 - && GetDirSize(DEBUG_DUMPS_DIR, &worst_dir, name) / (1024*1024) >= g_settings_nMaxCrashReportsSize + && get_dirsize_find_largest_dir(DEBUG_DUMPS_DIR, &worst_dir, name) / (1024*1024) >= g_settings_nMaxCrashReportsSize && worst_dir != "" ) { log("Size of '%s' >= %u MB, deleting '%s'", DEBUG_DUMPS_DIR, g_settings_nMaxCrashReportsSize, worst_dir.c_str()); g_pCommLayer->QuotaExceed(_("Report size exceeded the quota. Please check system's MaxCrashReportsSize value in abrt.conf.")); - DeleteDebugDumpDir(concat_path_file(DEBUG_DUMPS_DIR, worst_dir.c_str()).c_str()); + delete_debug_dump_dir(concat_path_file(DEBUG_DUMPS_DIR, worst_dir.c_str()).c_str()); worst_dir = ""; } @@ -554,7 +508,7 @@ static gboolean handle_inotify_cb(GIOChannel *gio, GIOCondition condition, gpoin case MW_FILE_ERROR: default: log("Corrupted or bad crash, deleting"); - DeleteDebugDumpDir(fullname.c_str()); + delete_debug_dump_dir(fullname.c_str()); break; } } diff --git a/src/Daemon/MiddleWare.cpp b/src/Daemon/MiddleWare.cpp index f3c6bdf4..0a3fb9ad 100644 --- a/src/Daemon/MiddleWare.cpp +++ b/src/Daemon/MiddleWare.cpp @@ -520,13 +520,6 @@ report_status_t Report(const map_crash_report_t& pCrashReport, return ret; } -void DeleteDebugDumpDir(const char *pDebugDumpDir) -{ - CDebugDump dd; - dd.Open(pDebugDumpDir); - dd.Delete(); -} - std::string DeleteCrashInfo(const char *pUUID, const char *pUID) { diff --git a/src/Daemon/MiddleWare.h b/src/Daemon/MiddleWare.h index ac042def..b2d3ef28 100644 --- a/src/Daemon/MiddleWare.h +++ b/src/Daemon/MiddleWare.h @@ -105,11 +105,6 @@ report_status_t Report(const map_crash_report_t& pCrashReport, std::string getDebugDumpDir( const char *pUUID, const char *pUID); /** - * Deletes particular debugdump directory. - * @param pDebugDumpDir A debugdump directory. - */ -void DeleteDebugDumpDir(const char *pDebugDumpDir); -/** * Deletes a row from database. If a deleting is * successfull, it returns a debugdump directort, which is not * deleted. Otherwise, it returns empty string. diff --git a/src/Hooks/CCpp.cpp b/src/Hooks/CCpp.cpp index 138d0d66..de74bdf9 100644 --- a/src/Hooks/CCpp.cpp +++ b/src/Hooks/CCpp.cpp @@ -29,6 +29,8 @@ #define VAR_RUN_PID_FILE VAR_RUN"/abrt.pid" +using namespace std; + static char* get_executable(pid_t pid) { char buf[PATH_MAX + 1]; @@ -282,9 +284,58 @@ int main(int argc, char** argv) * but it does not log file name */ error_msg_and_die("error saving coredump to %s", path); } - /* free(executable); - why bother? */ - /* free(cmdline); */ + free(executable); + free(cmdline); log("saved core dump of pid %u to %s (%llu bytes)", (int)pid, path, (long long)size); + path[len] = '\0'; /* path now contains directory name */ + + /* We close dumpdir before we start catering for crash storm case. + * Otherwise, delete_debug_dump_dir's from other concurrent + * CCpp's won't be able to delete our dump (their delete_debug_dump_dir + * will wait for us), and we won't be able to delete their dumps. + * Classic deadlock. + */ + dd.Close(); + + /* Get it from abrt.conf */ + unsigned setting_MaxCrashReportsSize = 0; + FILE *fp = fopen(CONF_DIR"/abrt.conf", "r"); + if (fp) + { + char line[256]; + while (fgets(line, sizeof(line), fp) != NULL) + { + const char *p = skip_whitespace(line); + if (strncmp(p, "MaxCrashReportsSize", sizeof("MaxCrashReportsSize")-1) == 0) + { + p = skip_whitespace(p + sizeof("MaxCrashReportsSize")-1); + if (*p != '=') + continue; + p = skip_whitespace(p + 1); + if (isdigit(*p)) + setting_MaxCrashReportsSize = (unsigned long)atoi(p); + continue; + } + /* add more 'if (strncmp(p, "xx", sizeof("xx")-1) == 0)' here... */ + } + fclose(fp); + } + if (setting_MaxCrashReportsSize > 0) + { + string worst_dir; + while (1) + { + const char *base_dirname = strrchr(path, '/') + 1; /* never NULL */ + /* We exclude our own dump from candidates for deletion (3rd param): */ + double dirsize = get_dirsize_find_largest_dir(DEBUG_DUMPS_DIR, &worst_dir, base_dirname); + if (dirsize / (1024*1024) < setting_MaxCrashReportsSize || worst_dir == "") + break; + log("Size of '%s' >= %u MB, deleting '%s'", DEBUG_DUMPS_DIR, setting_MaxCrashReportsSize, worst_dir.c_str()); + delete_debug_dump_dir(concat_path_file(DEBUG_DUMPS_DIR, worst_dir.c_str()).c_str()); + worst_dir = ""; + } + } + return 0; } catch (CABRTException& e) diff --git a/src/Hooks/Makefile.am b/src/Hooks/Makefile.am index 026d4cf7..c643b248 100644 --- a/src/Hooks/Makefile.am +++ b/src/Hooks/Makefile.am @@ -8,6 +8,7 @@ hookCCpp_CPPFLAGS = \ -I$(srcdir)/../../inc \ -I$(srcdir)/../../lib/Utils \ -DDEBUG_DUMPS_DIR=\"$(DEBUG_DUMPS_DIR)\" \ + -DCONF_DIR=\"$(CONF_DIR)\" \ -DVAR_RUN=\"$(VAR_RUN)\" \ -D_GNU_SOURCE hookCCpp_LDADD = \ @@ -38,6 +39,7 @@ abrt_pyhook_helper_CPPFLAGS = \ -I$(srcdir)/../../inc \ -I$(srcdir)/../../lib/Utils \ -DDEBUG_DUMPS_DIR=\"$(DEBUG_DUMPS_DIR)\" \ + -DCONF_DIR=\"$(CONF_DIR)\" \ -DVAR_RUN=\"$(VAR_RUN)\" \ -D_GNU_SOURCE abrt_pyhook_helper_LDADD = \ |