From owner-svn-soc-all@FreeBSD.ORG Wed Jul 4 15:21:19 2012 Return-Path: Delivered-To: svn-soc-all@FreeBSD.org Received: from socsvn.FreeBSD.org (unknown [IPv6:2001:4f8:fff6::2f]) by hub.freebsd.org (Postfix) with SMTP id 772A7106566C for ; Wed, 4 Jul 2012 15:21:16 +0000 (UTC) (envelope-from tzabal@FreeBSD.org) Received: by socsvn.FreeBSD.org (sSMTP sendmail emulation); Wed, 04 Jul 2012 15:21:16 +0000 Date: Wed, 04 Jul 2012 15:21:16 +0000 From: tzabal@FreeBSD.org To: svn-soc-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-Id: <20120704152116.772A7106566C@hub.freebsd.org> Cc: Subject: socsvn commit: r238961 - soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jul 2012 15:21:19 -0000 Author: tzabal Date: Wed Jul 4 15:21:15 2012 New Revision: 238961 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=238961 Log: Updated version of crashreportd. Modified: soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py Modified: soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py ============================================================================== --- soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py Wed Jul 4 14:25:14 2012 (r238960) +++ soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py Wed Jul 4 15:21:15 2012 (r238961) @@ -1,47 +1,142 @@ -#!/usr/bin/python +#!/usr/local/bin/python import os +import re import time import tarfile -# Check if the given filename matches the pattern of a valid crash report -# Return 1 in failure and 0 in success -def check_filename(filename): - matchObj = re.match('^crashreport\.[A-Za-z0-9]{6}\.tar\.gz$', filename) + +# Check if the filename matches the pattern of a valid crash report name +# Return False in failure and True in success +def check_report_name(filename): + match_obj = re.match('^crashreport\.[A-Za-z0-9]{6}\.tar\.gz$', filename) + + if not match_obj: + print "debug1" + return False - if not matchObj: - return 1 + return True + + +# Check if the filename matches the pattern of a valid crash data name +# Return False in failure and True in success +def check_data_name(filename): + match_obj = re.match('^crashreport\.[A-Za-z0-9]{6}\.xml$', filename) + + if not match_obj: + print "debug2" + return False - return 0 + return True -def extract_file(filename): +# Check if the report is a tar.gz file and if it has only one file inside +# with a valid name. +# Return False in failure and True in success +def check_report_contents(filename): + report = crashreports_dir + "/" + filename + + if not tarfile.is_tarfile(report): + print "debug3" + return False + + try: + tarfile_obj = tarfile.open(report, 'r:gz') + contents_list = tarfile_obj.getnames() + except ReadError: + print "debug4" + return False + except CompressionError: + print "debug5" + return False + + if not len(contents_list) == 1: + print "debug6" + return False + + if not check_data_name(contents_list[0]): + print "debug7" + return False + + return True + + +def extract_report(filename): + if not os.path.isdir(extraction_dir): + print "debug11" + return False + try: report = crashreports_dir + "/" + filename - tarfileObj = tarfile.open(report) + tarfile_obj = tarfile.open(report, 'r:gz') + tarfile_obj.extractall(extraction_dir); except ReadError: - return 1 - - return 0 + print "debug12" + return False + except CompressionError: + print "debug13" + return False + + dirlist = os.listdir(extraction_dir) + if not len(dirlist) == 1: + print "debug14" + return False + + data_name = dirlist[0] + #print "data_name: ", data_name + + return True +# Container function that call all the check related functions +# Return False in failure and True in success def check_report(filename): - check_filename(filename) + if not check_report_name(filename): + print "debug8" + return False + + if not check_report_contents(filename): + print "debug9" + return False - return 0; + if not extract_report(filename): + print "debug10" + return False + + return True + + +# Create the Process ID file that contains the PID of crashreportd. +# It used from the rc.d script to stop the program normally. +def create_pid_file(): + pid = os.getpid() -# Obtain the Process ID -pid = os.getpid() + pid_file = '/home/tzabal/Desktop/crashreportd.pid' + #pid_file = '/var/run/crashreportd.pid' + try: + file_obj = open(pid_file, 'w') + file_obj.write(str(pid)) + file_obj.close() + except IOError: + return False + finally: + file_obj.close() + + return True -# Create the pid file and write the Process ID inside -#pid_file = '/var/run/crashreportd.pid' -pid_file = '/home/tzabal/crashreportd.pid' -f = open(pid_file, 'w') -f.write(str(pid)) -f.close() # The infinite loop of the daemon -crashreports_dir = '/home/tzabal/crashreports' -dirlist = os.listdir(crashreports_dir) -for filename in dirlist: - check_report(filename) +interval = 10 +crashreports_dir = '/home/tzabal/Desktop/crashreports' +#crashreports_dir = '/var/spool/crashreports' +extraction_dir = '/tmp/crashreports' +counter = 1 +create_pid_file() +while True: + print "== Pass:", counter, "==" + dirlist = os.listdir(crashreports_dir) + for filename in dirlist: + print "Report:", filename + check_report(filename) + counter += 1 + time.sleep(interval) \ No newline at end of file