Bug 1798128

Summary: pidofproc uses pidof with -m that does not exist anymore with pidof from procps-ng
Product: Red Hat Enterprise Linux 8 Reporter: Welterlen Benoit <bwelterl>
Component: initscriptsAssignee: Jan Macku <jamacku>
Status: CLOSED ERRATA QA Contact: Daniel Rusek <drusek>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 8.2CC: initscripts-maint-list, lnykryn
Target Milestone: rc   
Target Release: 8.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: initscripts-10.00.7-1.el8 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-11-04 01:41:25 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Welterlen Benoit 2020-02-04 15:54:51 UTC
Description of problem:
When starting an initscript directly (without systemctl) and if it checks the pid with pidofproc, the output of pidofproc is always true as pidof in /etc/init.d/functions is called with -m that does not exist anymore, and thus the initial script is not omitted.

Version-Release number of selected component (if applicable):
RHEL 8 
procps-ng-3.3.15-1.el8.x86_64
initscripts-10.00.4-1.el8.x86_64

How reproducible:
Always

Steps to Reproduce:
1. Use an init scrips with pidofproc $progname
2. the output will always be true if the service is run through /etc/init.d/service start
3.

Actual results:
the service does not start if not called with systemctl

Expected results:
the service should start with /etc/init.d/service start

Additional info:
A workaround is to run "sh /etc/init.d/service start" as the script will be omitted by pidof.

- Script: 
--------------------------
#!/bin/bash
#
# chkconfig: 2345 50 50
#
# description: The dummy daemon
#
### BEGIN INIT INFO
# Provides: foo
# Short-Description: Dummy Daemon
# Description: The dummy daemon
### END INIT INFO

. /etc/init.d/functions

set -o functrace
set -x

PROGNAME=foo
PROGPATH=/usr/local/bin/foo
RETVAL=0

start() {
        echo -n $"Starting $PROGNAME: $$"
        echo pidof ` pidof $PROGNAME`
        if [ $UID -ne 0 ]; then
                # insufficient privilege
                RETVAL=1
                failure
        elif pidofproc $PROGPATH >/dev/null 3>&1 2>&1; then
                echo "Already runnning $PROGPATH" >> /tmp/foo
                #sleep 1000
                # already running
                RETVAL=1
                failure
        else
                #sleep 1000
                daemon $PROGPATH
                RETVAL=$?
                [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$PROGNAME
        fi
        echo
}

stop() {
        echo -n $"Stopping $PROGNAME: "
        if [ $UID -ne 0 ]; then
                # insufficient privilege
                RETVAL=1
                failure
        else
                killproc $PROGPATH
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROGNAME
        fi
        echo
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
esac

exit $RETVAL
------------------

- the daemon:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc, char* argv[])
{
        pid_t process_id = 0;
        pid_t sid = 0;

        // Create child process
        process_id = fork();
        // Indication of fork() failure
        if (process_id < 0) {
                printf("fork failed!\n");
                // Return failure in exit status
                exit(1);
        }
        // PARENT PROCESS. Need to kill it.
        if (process_id > 0) {
                printf("process_id of child process %d \n", process_id);
                // return success in exit status
                exit(0);
        }
        //unmask the file mode
        umask(0);
        //set new session
        sid = setsid();
        if(sid < 0) {
                // Return failure
                exit(1);
        }
        // Change the current working directory to root.
        chdir("/");
        // Close stdin. stdout and stderr
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
        while (1) {
                sleep(3600);
        }

        return (0);
}

Comment 3 Jan Macku 2020-03-11 11:45:57 UTC
Bug will be fixed in RHEL 8.3.0

Upstream PR: https://github.com/fedora-sysv/initscripts/pull/314

Comment 9 errata-xmlrpc 2020-11-04 01:41:25 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (initscripts bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2020:4463