From nobody@FreeBSD.org  Mon Oct  8 17:41:59 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id A25A116A419
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  8 Oct 2007 17:41:59 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 7FC7113C481
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  8 Oct 2007 17:41:59 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.1/8.14.1) with ESMTP id l98Hfxno018883
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 8 Oct 2007 17:41:59 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.1/8.14.1/Submit) id l98Hfx88018882;
	Mon, 8 Oct 2007 17:41:59 GMT
	(envelope-from nobody)
Message-Id: <200710081741.l98Hfx88018882@www.freebsd.org>
Date: Mon, 8 Oct 2007 17:41:59 GMT
From: Steven Hartland <steven.hartland@multiplay.co.uk>
To: freebsd-gnats-submit@FreeBSD.org
Subject: rc.subr doesnt deal with perl daemons
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         117027
>Category:       conf
>Synopsis:       rc.subr doesnt deal with perl daemons
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    smh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 08 17:50:01 GMT 2007
>Closed-Date:    Mon Jun 10 11:52:39 UTC 2013
>Last-Modified:  Mon Jun 10 11:52:39 UTC 2013
>Originator:     Steven Hartland
>Release:        FreeBSD 6.2-RELEASE-p7 i386
>Organization:
Multiplay
>Environment:
FreeBSD core2.multiplay.co.uk 6.2-RELEASE-p7 FreeBSD 6.2-RELEASE-p7 #1: Thu Aug 2 00:54:23 BST 2007 root@core2.multiplay.co.uk:/usr/src/sys/
i386/compile/MPUK_SMP i386
>Description:
Configuring a rc script to deal with perl daemons currently fails.

This is because the perl daemonization results in a procname of "[perl]" where as the currently the only special case tested is "the basename of the interpreter plus a colon"

Example output:
ps -o "pid,jid,command" -p 60082
  PID    JID COMMAND
60082      0 [perl]
>How-To-Repeat:
Try the mrtg daemon script:
#!/bin/sh
#
# $FreeBSD: ports/net-mgmt/mrtg/files/mrtg_daemon.sh.in,v 1.5 2006/04/23 17:39:52 oliver Exp $

#
# PROVIDE: mrtg_daemon
# REQUIRE: DAEMON bsnmpd snmpd

.  /etc/rc.subr


name="mrtg_daemon"
rcvar=`set_rcvar`

load_rc_config $name

: ${mrtg_daemon_enable="NO"}
: ${mrtg_daemon_pidfile="/var/run/mrtg/mrtg.pid"}
: ${mrtg_daemon_user="mrtg"}
: ${mrtg_daemon_group="mrtg"}
: ${mrtg_daemon_config="/usr/local/etc/mrtg/mrtg.cfg"}

: ${mrtg_daemon_flags="--pid-file $mrtg_daemon_pidfile --lock-file /var/run/mrtg/lockfile --confcache-file /var/run/mrtg/confcache --user $mrtg_daemon_user --group $mrtg_daemon_group --daemon $mrtg_daemon_config"}

start_precmd="mrtg_daemon_precmd"

command="/usr/local/bin/mrtg"
command_interpreter="/usr/local/bin/perl"
pidfile=${mrtg_daemon_pidfile}

mrtg_daemon_precmd()
{
       rm -f /var/run/mrtg/lockfile /var/run/mrtg/confcache $mrtg_daemon_pidfile
}

run_rc_command "$1"
>Fix:
The following patch fixes this behaviour by adding the test for "[perl]" to the valid options:-

--- /etc/rc.subr.orig   Mon Oct  8 18:31:34 2007
+++ /etc/rc.subr        Mon Oct  8 18:32:31 2007
@@ -271,5 +271,5 @@
                _fp_args='_argv'
                _fp_match='case "$_argv" in
-                   ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
+                   ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
        else                                    # a normal daemon
                _procnamebn=${_procname##*/}

Patch attached with submission follows:

--- /etc/rc.subr.orig	Mon Oct  8 18:31:34 2007
+++ /etc/rc.subr	Mon Oct  8 18:32:31 2007
@@ -271,5 +271,5 @@
 		_fp_args='_argv'
 		_fp_match='case "$_argv" in
-		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
+		    ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
 	else					# a normal daemon
 		_procnamebn=${_procname##*/}


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->yar 
Responsible-Changed-By: yar 
Responsible-Changed-When: Wed Oct 17 22:08:46 UTC 2007 
Responsible-Changed-Why:  
I've been doing some work around this area recently. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 
State-Changed-From-To: open->analyzed 
State-Changed-By: yar 
State-Changed-When: Fri Nov 2 12:33:14 UTC 2007 
State-Changed-Why:  
The problem with such daemons is that it's impossible to distinguish 
them when there are several of them, which can lead to matching 
wrong processes by _find_processes().  Even if they use pidfiles, 
it's still possible to get a stale pidfile and match a wrong process 
with the reused pid.  That can be even worse than no match at all. 

BTW, in my experiments perl daemons preserved their commands visible 
through ps(1) unless $0 was intentionally modified.  Is the daemon 
swapped out in your case?  What does `ps axl' tell about it? 

Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 

From: "Steven Hartland" <killing@multiplay.co.uk>
To: <bug-followup@freebsd.org>
Cc:  
Subject: Re: conf/117027: rc.subr doesnt deal with perl daemons
Date: Tue, 7 Feb 2012 09:14:40 -0000

 The output from ps axl | grep perl is:-
     0  1060     1   0  44  0 37520 14956 nanslp Ss    ??    2:56.85 /usr/local/bin/perl -w 
 /data/fileplay/scripts/fileplay_mirror.pl -daemonize
     0 34033     1   0  66 20 36504 12984 nanslp SNs   ??    0:36.93 servercheck (perl5.8.9)
 
 These as both daemons started using this patch.
 
 While I understand that its possible for a process to
 match even when using pid files, that is going to be
 a very very rare case as given atm it just fails every
 time that's an almost 100% improvement in behaviour.
 
 We've been running the mentioned patch for over 4 years
 now and have never had a problem.
 
     Regards
     Steve 
 
 
State-Changed-From-To: analyzed->open 
State-Changed-By: eadler 
State-Changed-When: Tue Jul 10 03:34:50 UTC 2012 
State-Changed-Why:  
over to the pool (with bugmeister approval). I have not verified this 
still a problem 


Responsible-Changed-From-To: yar->freebsd-bugs 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Tue Jul 10 03:34:50 UTC 2012 
Responsible-Changed-Why:  
over to the pool (with bugmeister approval). I have not verified this 
still a problem 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 

From: Steven Hartland <steven@multiplay.co.uk>
To: bug-followup@FreeBSD.org, steven.hartland@multiplay.co.uk
Cc:  
Subject: Re: conf/117027: rc.subr doesnt deal with perl daemons
Date: Tue, 10 Jul 2012 04:50:59 +0100

 Just to confirm this is still an issue on 8.3-RELEASE at least.
 
 ================================================
 This e.mail is private and confidential between Multiplay (UK) Ltd. and the person or entity to whom it is addressed. In the event of misdirection, the recipient is prohibited from using, copying, printing or otherwise disseminating it or any information contained in it. 
 
 In the event of misdirection, illegible or incomplete transmission please telephone +44 845 868 1337
 or return the E.mail to postmaster@multiplay.co.uk.
 
Responsible-Changed-From-To: freebsd-bugs->smh 
Responsible-Changed-By: smh 
Responsible-Changed-When: Thu Dec 13 22:10:21 UTC 2012 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 

From: Chris Rees <utisoft@gmail.com>
To: "bug-followup@freebsd.org" <bug-followup@freebsd.org>
Cc:  
Subject: Re: conf/117027: rc.subr doesnt deal with perl daemons
Date: Sun, 16 Dec 2012 17:18:24 +0000

 Hi Steven,
 
 I ran into this with java as well, which does the same thing.  The
 square brackets appear to be a "hiding $0" from unprivileged users.
 
 Do the rc scripts still fail when run as root (service mrtg status,
 for example)?
 
 Chris

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: conf/117027: commit references a PR
Date: Thu, 10 Jan 2013 11:08:30 +0000 (UTC)

 Author: smh
 Date: Thu Jan 10 11:08:22 2013
 New Revision: 245250
 URL: http://svnweb.freebsd.org/changeset/base/245250
 
 Log:
   Allow perl scripts to be used in rc.d scripts
   
   PR:		conf/117027
   Reviewed by:	pjd (mentor)
   Approved by:	hrs
   MFC after:	2 weeks
 
 Modified:
   head/etc/rc.subr
 
 Modified: head/etc/rc.subr
 ==============================================================================
 --- head/etc/rc.subr	Thu Jan 10 08:06:12 2013	(r245249)
 +++ head/etc/rc.subr	Thu Jan 10 11:08:22 2013	(r245250)
 @@ -290,7 +290,7 @@ _find_processes()
  		_interpbn=${1##*/}
  		_fp_args='_argv'
  		_fp_match='case "$_argv" in
 -		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
 +		    ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
  	else					# a normal daemon
  		_procnamebn=${_procname##*/}
  		_fp_args='_arg0 _argv'
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: smh 
State-Changed-When: Thu Jan 10 14:41:47 UTC 2013 
State-Changed-Why:  
Awaiting MFC 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: conf/117027: commit references a PR
Date: Wed,  5 Jun 2013 11:59:44 +0000 (UTC)

 Author: smh
 Date: Wed Jun  5 11:59:36 2013
 New Revision: 251418
 URL: http://svnweb.freebsd.org/changeset/base/251418
 
 Log:
   MFC r245250:
   Allow perl scripts to be used in rc.d scripts
   
   PR:		conf/117027
 
 Modified:
   stable/9/etc/rc.subr
 Directory Properties:
   stable/9/etc/   (props changed)
 
 Modified: stable/9/etc/rc.subr
 ==============================================================================
 --- stable/9/etc/rc.subr	Wed Jun  5 11:55:35 2013	(r251417)
 +++ stable/9/etc/rc.subr	Wed Jun  5 11:59:36 2013	(r251418)
 @@ -331,7 +331,7 @@ _find_processes()
  		_interpbn=${1##*/}
  		_fp_args='_argv'
  		_fp_match='case "$_argv" in
 -		    ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)'
 +		    ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
  	else					# a normal daemon
  		_procnamebn=${_procname##*/}
  		_fp_args='_arg0 _argv'
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: smh 
State-Changed-When: Mon Jun 10 11:52:38 UTC 2013 
State-Changed-Why:  
Committed. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117027 
>Unformatted:
