From aaa3fe61c1d992ab23ae58194386f3b7dde44c2a Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 31 Jul 2014 23:13:03 +0100 Subject: [PATCH] Attempt to keep PID and logfile as netdisco user even when running as root --- Netdisco/Changes | 8 ++++++-- Netdisco/bin/netdisco-web | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Netdisco/Changes b/Netdisco/Changes index b017614e..ae262886 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -1,16 +1,20 @@ 2.029000 - [ENHANCEMENTS] + [NEW FEATURES] * [#108] Port Bounce port control feature (down and up in succession) * Support for stuffing other locations into @INC at runtime + + [ENHANCEMENTS] + * Reports config is now a list (see ReleaseNotes) + * Add "AP" as a hint for WAP support + * Attempt to keep PID and logfile as netdisco user even when running as root [BUG FIXES] * Missing action to netdisco-do should not throw Perl error * Sort CDP/LLDP data to be more consistent when multiple neighbors on a port - * Add "AP" as a hint for WAP support 2.028013 - 2014-07-31 diff --git a/Netdisco/bin/netdisco-web b/Netdisco/bin/netdisco-web index 98652245..6d0b0b2e 100755 --- a/Netdisco/bin/netdisco-web +++ b/Netdisco/bin/netdisco-web @@ -41,6 +41,7 @@ BEGIN { use Daemon::Control; use Filesys::Notify::Simple; +use IO::File; use App::Netdisco::Environment; my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml'; @@ -53,6 +54,19 @@ my $gid = (stat($netdisco->stringify))[5] || 0; my $log_dir = dir($home, 'logs'); mkdir $log_dir if ! -d $log_dir; +chown $uid, $gid, $log_dir; + +my $pid_file = file($home, 'netdisco-web.pid'); +my $log_file = file($log_dir, 'netdisco-web.log'); + +# change ownership of key files to be netdisco user +foreach my $file ($pid_file, $log_file) { + unless (-e $file) { + sysopen my $fh, $file, O_WRONLY|O_CREAT|O_NONBLOCK|O_NOCTTY; + close $fh; + } + chown $uid, $gid, $file; +} Daemon::Control->new({ name => 'Netdisco Web', @@ -62,9 +76,9 @@ Daemon::Control->new({ '--user', $uid, '--group', $gid, @args, $netdisco->stringify ], - pid_file => file($home, 'netdisco-web.pid'), - stderr_file => file($log_dir, 'netdisco-web.log'), - stdout_file => file($log_dir, 'netdisco-web.log'), + pid_file => $pid_file, + stderr_file => $log_file, + stdout_file => $log_file, redirect_before_fork => 0, ((scalar grep { $_ =~ m/port/ } @args) ? () : (uid => $uid, gid => $gid)),