Skip to content

Commit

Permalink
[Server/Logrotate] Make sure XRootD logrotate does not interfire with…
Browse files Browse the repository at this point in the history
… system logrotate, fixes #490
  • Loading branch information
simonmichal committed Mar 30, 2017
1 parent 7f5a2d6 commit 609d9f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packaging/common/xrootd.logrotate
@@ -1,5 +1,17 @@
/var/log/xrootd/*/*.log /var/log/xrootd/*.log
{
# if the xrootd log rotate is enabled don't do anything
# (by convention if xrootd log rotate is enabled the
# .lock file exists)
prerotate
LOCK=`dirname $1`/.lock
if [ -f $LOCK ]; then
exit 1
else
exit 0
fi
endscript
dateext
missingok
nomail
nocreate
Expand Down
48 changes: 48 additions & 0 deletions src/XrdSys/XrdSysLogger.cc
Expand Up @@ -198,6 +198,12 @@ int XrdSysLogger::Bind(const char *path, int lfh)
doLFR = (lfh > 0);
if ((rc = ReBind(0))) return rc;

// Lock the logs if XRootD is suppose to handle log rotation itself
//
rc = HandleLogRotateLock( doLFR );
if( rc )
return -rc;

// Handle specifics of lofile rotation
//
if (eInt == onFifo) {if ((rc = FifoMake())) return -rc;}
Expand Down Expand Up @@ -441,6 +447,48 @@ int XrdSysLogger::FifoMake()
return 0;
}

/******************************************************************************/
/* H a n d l e L o g R o t a t e L o c k */
/******************************************************************************/
int XrdSysLogger::HandleLogRotateLock( bool dorotate )
{
if( !ePath ) return 0;

char *end = rindex(ePath, '/') + 1;
const std::string lckPath = std::string( ePath, end ) + ".lock";
int rc = unlink( lckPath.c_str() );
if( rc && errno != ENOENT )
{
BLAB( "The logfile lock (" << lckPath.c_str() << ") exists and cannot be removed: " << strerror( errno ) );
return EEXIST;
}

if( dorotate )
{
rc = open( lckPath.c_str(), O_CREAT, 0644 );
if( rc < 0 )
{
BLAB( "Failed to create the logfile lock (" << lckPath.c_str() << "): " << strerror( errno ) );
return errno;
}
close( rc );
}

return 0;
}

/******************************************************************************/
/* R m L o g R o t a t e L o c k */
/******************************************************************************/
void XrdSysLogger::RmLogRotateLock()
{
if( !ePath ) return;

char *end = rindex(ePath, '/') + 1;
const std::string lckPath = std::string( ePath, end ) + ".lock";
unlink( lckPath.c_str() );
}

/******************************************************************************/
/* F i f o W a i t */
/******************************************************************************/
Expand Down
9 changes: 8 additions & 1 deletion src/XrdSys/XrdSysLogger.hh
Expand Up @@ -66,7 +66,12 @@ public:
//! Destructor
//-----------------------------------------------------------------------------

~XrdSysLogger() {if (ePath) free(ePath);}
~XrdSysLogger()
{
RmLogRotateLock();
if (ePath)
free(ePath);
}

//-----------------------------------------------------------------------------
//! Add a message to be printed at midnight.
Expand Down Expand Up @@ -235,6 +240,8 @@ void FifoWait();
int Time(char *tbuff);
static int TimeStamp(struct timeval &tVal, unsigned long tID,
char *tbuff, int tbsz, bool hires);
int HandleLogRotateLock( bool dorotate );
void RmLogRotateLock();

struct mmMsg
{mmMsg *next;
Expand Down

0 comments on commit 609d9f4

Please sign in to comment.