Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-18 Thread Scott Andrews
On Fri, 16 Apr 2021 20:12:16 -0500
DJ Lucas  wrote:

> On 4/9/2021 5:16 AM, Scott Andrews wrote:
> > This script is not intended to run with ash, the she bang line is
> > as follows
> >
> > #!/bin/bash --posix  
> 
> 
> My apologies for the duplicate reply Scott, I didn't reply to list.
> 
> 
> Unfortunately, init-functions is a special case. You cannot
> guarantee that the calling script will use /bin/bash. 

You can on a lfs/blfs system and the script all the init
scripts use /bin/bash.

>That script
> will be run under whatever interpreter is used by the calling
> script (the schebang in init-functions is basically meaningless).
> When making any changes in init-functions, you have to test at
> least under dash (it's not a 1:1 for AST/AT ash, but close
> enough, and building AST ash is likely a non-starter now days). You
> don't get a choice here, it has to be sh compliant because we have
> no control over what a vendor supplied script will use.
> 
> --DJ
> 



Well on a lfs system which shell is the default shell?

Second the various init scripts that lfs currently use are
mixed /bin/bash and /bin/sh.

What vendor supplied script?  We are talking lfs/blfs here.
Why would a vendor script source init-functions? It most likely
wouldn't work anyway if it did.

sh compliant in what way? POSIX? UNIX?
The sh used by lfs (gnu bash) is what I call sh++ as it uses and
accept most bash builtins.

If you test my changes you will see that they run the same sourced
by a script using /bin/sh or /bin/bash (I tested for that).  And yes
I do know that LSB requires the scripts to be /bin/sh, but we are
again talking lfs here.

BTW is dash even in the BOOK?

I have no use for dash or ash.

If I was to use dash and posted here I would simply be told your
distro your rules, so after all, why would that not apply here for
some one wanting to use dash or a vendor script?
-- 
http://lists.linuxfromscratch.org/listinfo/lfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Do not top post on this list.

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

http://en.wikipedia.org/wiki/Posting_style


Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-16 Thread DJ Lucas


On 4/9/2021 5:16 AM, Scott Andrews wrote:
This script is not intended to run with ash, the she bang line is as 
follows


#!/bin/bash --posix



My apologies for the duplicate reply Scott, I didn't reply to list.


Unfortunately, init-functions is a special case. You cannot guarantee 
that the calling script will use /bin/bash. That script will be run 
under whatever interpreter is used by the calling script (the schebang 
in init-functions is basically meaningless). When making any changes in 
init-functions, you have to test at least under dash (it's not a 1:1 for 
AST/AT ash, but close enough, and building AST ash is likely a 
non-starter now days). You don't get a choice here, it has to be sh 
compliant because we have no control over what a vendor supplied script 
will use.


--DJ

--
http://lists.linuxfromscratch.org/listinfo/lfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Do not top post on this list.

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

http://en.wikipedia.org/wiki/Posting_style


Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-09 Thread Bruce Dubbs

On 4/9/21 12:50 PM, Tim Tassonis wrote:

Hi Scott

On 4/9/21 12:16 PM, Scott Andrews wrote:


On 4/9/21 2:10 AM, Tim Tassonis wrote:

Hi Scott

On 4/9/21 2:40 AM, Scott Andrews wrote:

# Function:    pidofproc [-p pidfile] pathname
#   The pidofproc function shall return one or more process
#   identifiers for a particular daemon using the algorithm
#   given above. Only process identifiers of running
#   processes should be returned. Multiple process
#   identifiers shall be separated by a single space.
#   The pidofproc function shall return the LSB defined exit
#   status codes for "status". It shall return 0 if the
#   program is running and not 0 otherwise.
#
#   If the status action is requested, the init script will return
#   the following exit status codes.
#   0   program is running or service is OK
#   1   program is dead and /var/run pid file exists
#   2   program is dead and /var/lock lock file exists
#   3   program is not running
#   4   program or service status is unknown
#   5-99    reserved for future LSB use
#   100-149 reserved for distribution use
#   150-199 reserved for application use
#   200-254 reserved
function pidofproc {
 local pidfile=""
 local program=""
 local pid=""
 local pidlist=""
 local list=""
 local exitstatus=0
 # Process arguments
 while [[ $# -gt 0 ]]; do
     case "${1}" in
         -p) shift; pidfile="${1}" ;;
         *)    program="${1##*/}" ;;
     esac
     shift
 done
 # If a PID file is not specified, make one
 test -z "${pidfile}" && pidfile="/var/run/${program}.pid"
 # If a pid file exists and is readable, use it.
 if [[ -r "${pidfile}" ]]; then
     list=$(head -n1 "${pidfile}")
 else
     list=$(pidof "${program}")
     exitstatus=$?
     if [[ ${exitstatus} -gt 0 ]]; then exitstatus=4; fi
 fi
 if [[ exitstatus -eq 0 ]]; then
     # Figure out if all listed PIDs are running.
     for pid in ${list}; do
         if kill -0 "${pid}" 2> /dev/null; then
             pidlist+="${pid} "
         else
             exitstatus=1
         fi
     done
 fi
 if [[ -z "${pidlist}" ]]; then
     if [[ -f "${pidfile}" ]]; then
         exitstatus=1
     else
         exitstatus=3
     fi
 fi
 printf "%s" "${pidlist% }"
 return ${exitstatus}
}


Testing Function: pidofproc: Start
Testing pidofproc: valid program, no pidfile: running
 Returns pid and exit status of 0
 pidofproc /usr/sbin/dhcpcd: >:pid:[472] exit status:[0]
 pidofproc dhcpcd: >:pid:[472] exit status:[0]

Testing pidofproc: valid program, valid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p /var/run/dhcpcd.pid /usr/sbin/dhcpcd: 
>:pid:[472] exit status:[0]
 pidofproc /usr/sbin/dhcpcd -p /var/run/dhcpcd.pid: 
>:pid:[472] exit status:[0]
 pidofproc -p /var/run/dhcpcd.pid dhcpcd: >:pid:[472] exit 
status:[0]
 pidofproc dhcpcd -p /var/run/dhcpcd.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: invalid program, no pidfile
 Returns no pid and exit status of 3
 pidofproc /usr/sbin/barf: >: pid:[] exit status:[3]

Testing pidofproc: valid program, invalid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p barf.pid /usr/sbin/dhcpcd: >: pid:[472] exit 
status:[0]
 pidofproc /usr/sbin/dhcpcd -p barf.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: valid program, no pidfile: not running
 Returns no pid and exit status of 3
 pidofproc /bin/ls: >: pid:[] exit status:[3]

Testing pidofproc: valid program: valid pidfile: not running
 Returns no pid and exit status of 1

touch /var/run/ls.pid

     pidofproc /bin/ls: >: pid:[] exit status:[1]
 pidofproc -p /var/run/ls.pid /bin/ls: >: pid:[] exit 
status:[1]

rm /var/run/ls.pid



Looks great, tested it successfully with bash. Testing with busybox 
ash does not work, but naturally, this is not needed on an LFS system.



This script is not intended to run with ash, the she bang line is as 
follows


#!/bin/bash --posix



Fully agree.






I do have one question however:

Testing with invalid program and valid pidfile results in printing 
the pid and returning 0:


Test with vaild program and valid pidfile:


root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcd
3144root@keegan:~# echo $?
0

root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcf
3144root@keeganecho $?
0

In the second case, /sbin/dhcpcf does not exist, but the pidfile 
does, and belongs to /sbin/dhcpcd. Is this intended behaviour? Not 
that this really matters a lot in real-world, as the function 
certainly will not be called that way often.




It reads the pidfile and if the process is running with then

     for pid in ${list}; do
         if kill -0 "${pid}" 2> /dev/null;then
             pidlist+="${pid} "
 

Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-09 Thread Tim Tassonis

Hi Scott

On 4/9/21 12:16 PM, Scott Andrews wrote:


On 4/9/21 2:10 AM, Tim Tassonis wrote:

Hi Scott

On 4/9/21 2:40 AM, Scott Andrews wrote:

# Function:    pidofproc [-p pidfile] pathname
#   The pidofproc function shall return one or more process
#   identifiers for a particular daemon using the algorithm
#   given above. Only process identifiers of running
#   processes should be returned. Multiple process
#   identifiers shall be separated by a single space.
#   The pidofproc function shall return the LSB defined exit
#   status codes for "status". It shall return 0 if the
#   program is running and not 0 otherwise.
#
#   If the status action is requested, the init script will return
#   the following exit status codes.
#   0   program is running or service is OK
#   1   program is dead and /var/run pid file exists
#   2   program is dead and /var/lock lock file exists
#   3   program is not running
#   4   program or service status is unknown
#   5-99    reserved for future LSB use
#   100-149 reserved for distribution use
#   150-199 reserved for application use
#   200-254 reserved
function pidofproc {
 local pidfile=""
 local program=""
 local pid=""
 local pidlist=""
 local list=""
 local exitstatus=0
 # Process arguments
 while [[ $# -gt 0 ]]; do
     case "${1}" in
         -p) shift; pidfile="${1}" ;;
         *)    program="${1##*/}" ;;
     esac
     shift
 done
 # If a PID file is not specified, make one
 test -z "${pidfile}" && pidfile="/var/run/${program}.pid"
 # If a pid file exists and is readable, use it.
 if [[ -r "${pidfile}" ]]; then
     list=$(head -n1 "${pidfile}")
 else
     list=$(pidof "${program}")
     exitstatus=$?
     if [[ ${exitstatus} -gt 0 ]]; then exitstatus=4; fi
 fi
 if [[ exitstatus -eq 0 ]]; then
     # Figure out if all listed PIDs are running.
     for pid in ${list}; do
         if kill -0 "${pid}" 2> /dev/null; then
             pidlist+="${pid} "
         else
             exitstatus=1
         fi
     done
 fi
 if [[ -z "${pidlist}" ]]; then
     if [[ -f "${pidfile}" ]]; then
         exitstatus=1
     else
         exitstatus=3
     fi
 fi
 printf "%s" "${pidlist% }"
 return ${exitstatus}
}


Testing Function: pidofproc: Start
Testing pidofproc: valid program, no pidfile: running
 Returns pid and exit status of 0
 pidofproc /usr/sbin/dhcpcd: >:pid:[472] exit status:[0]
 pidofproc dhcpcd: >:pid:[472] exit status:[0]

Testing pidofproc: valid program, valid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p /var/run/dhcpcd.pid /usr/sbin/dhcpcd: 
>:pid:[472] exit status:[0]
 pidofproc /usr/sbin/dhcpcd -p /var/run/dhcpcd.pid: 
>:pid:[472] exit status:[0]
 pidofproc -p /var/run/dhcpcd.pid dhcpcd: >:pid:[472] exit 
status:[0]
 pidofproc dhcpcd -p /var/run/dhcpcd.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: invalid program, no pidfile
 Returns no pid and exit status of 3
 pidofproc /usr/sbin/barf: >: pid:[] exit status:[3]

Testing pidofproc: valid program, invalid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p barf.pid /usr/sbin/dhcpcd: >: pid:[472] exit 
status:[0]
 pidofproc /usr/sbin/dhcpcd -p barf.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: valid program, no pidfile: not running
 Returns no pid and exit status of 3
 pidofproc /bin/ls: >: pid:[] exit status:[3]

Testing pidofproc: valid program: valid pidfile: not running
 Returns no pid and exit status of 1

touch /var/run/ls.pid

     pidofproc /bin/ls: >: pid:[] exit status:[1]
 pidofproc -p /var/run/ls.pid /bin/ls: >: pid:[] exit status:[1]
rm /var/run/ls.pid



Looks great, tested it successfully with bash. Testing with busybox 
ash does not work, but naturally, this is not needed on an LFS system.



This script is not intended to run with ash, the she bang line is as 
follows


#!/bin/bash --posix



Fully agree.






I do have one question however:

Testing with invalid program and valid pidfile results in printing the 
pid and returning 0:


Test with vaild program and valid pidfile:


root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcd
3144root@keegan:~# echo $?
0

root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcf
3144root@keeganecho $?
0

In the second case, /sbin/dhcpcf does not exist, but the pidfile does, 
and belongs to /sbin/dhcpcd. Is this intended behaviour? Not that this 
really matters a lot in real-world, as the function certainly will not 
be called that way often.




It reads the pidfile and if the process is running with then

         for pid in ${list}; do
             if kill -0 "${pid}" 2> /dev/null;then
                 pidlist+="${pid} "
             else
                 exitstatus=1

Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-09 Thread Scott Andrews


On 4/9/21 2:10 AM, Tim Tassonis wrote:

Hi Scott

On 4/9/21 2:40 AM, Scott Andrews wrote:

# Function:    pidofproc [-p pidfile] pathname
#   The pidofproc function shall return one or more process
#   identifiers for a particular daemon using the algorithm
#   given above. Only process identifiers of running
#   processes should be returned. Multiple process
#   identifiers shall be separated by a single space.
#   The pidofproc function shall return the LSB defined exit
#   status codes for "status". It shall return 0 if the
#   program is running and not 0 otherwise.
#
#   If the status action is requested, the init script will return
#   the following exit status codes.
#   0   program is running or service is OK
#   1   program is dead and /var/run pid file exists
#   2   program is dead and /var/lock lock file exists
#   3   program is not running
#   4   program or service status is unknown
#   5-99    reserved for future LSB use
#   100-149 reserved for distribution use
#   150-199 reserved for application use
#   200-254 reserved
function pidofproc {
 local pidfile=""
 local program=""
 local pid=""
 local pidlist=""
 local list=""
 local exitstatus=0
 # Process arguments
 while [[ $# -gt 0 ]]; do
     case "${1}" in
         -p) shift; pidfile="${1}" ;;
         *)    program="${1##*/}" ;;
     esac
     shift
 done
 # If a PID file is not specified, make one
 test -z "${pidfile}" && pidfile="/var/run/${program}.pid"
 # If a pid file exists and is readable, use it.
 if [[ -r "${pidfile}" ]]; then
     list=$(head -n1 "${pidfile}")
 else
     list=$(pidof "${program}")
     exitstatus=$?
     if [[ ${exitstatus} -gt 0 ]]; then exitstatus=4; fi
 fi
 if [[ exitstatus -eq 0 ]]; then
     # Figure out if all listed PIDs are running.
     for pid in ${list}; do
         if kill -0 "${pid}" 2> /dev/null; then
             pidlist+="${pid} "
         else
             exitstatus=1
         fi
     done
 fi
 if [[ -z "${pidlist}" ]]; then
     if [[ -f "${pidfile}" ]]; then
         exitstatus=1
     else
         exitstatus=3
     fi
 fi
 printf "%s" "${pidlist% }"
 return ${exitstatus}
}


Testing Function: pidofproc: Start
Testing pidofproc: valid program, no pidfile: running
 Returns pid and exit status of 0
 pidofproc /usr/sbin/dhcpcd: >:pid:[472] exit status:[0]
 pidofproc dhcpcd: >:pid:[472] exit status:[0]

Testing pidofproc: valid program, valid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p /var/run/dhcpcd.pid /usr/sbin/dhcpcd: 
>:pid:[472] exit status:[0]
 pidofproc /usr/sbin/dhcpcd -p /var/run/dhcpcd.pid: 
>:pid:[472] exit status:[0]
 pidofproc -p /var/run/dhcpcd.pid dhcpcd: >:pid:[472] exit 
status:[0]
 pidofproc dhcpcd -p /var/run/dhcpcd.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: invalid program, no pidfile
 Returns no pid and exit status of 3
 pidofproc /usr/sbin/barf: >: pid:[] exit status:[3]

Testing pidofproc: valid program, invalid pidfile: running
 Returns pid and exit status of 0
 pidofproc -p barf.pid /usr/sbin/dhcpcd: >: pid:[472] exit 
status:[0]
 pidofproc /usr/sbin/dhcpcd -p barf.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: valid program, no pidfile: not running
 Returns no pid and exit status of 3
 pidofproc /bin/ls: >: pid:[] exit status:[3]

Testing pidofproc: valid program: valid pidfile: not running
 Returns no pid and exit status of 1

touch /var/run/ls.pid

     pidofproc /bin/ls: >: pid:[] exit status:[1]
 pidofproc -p /var/run/ls.pid /bin/ls: >: pid:[] exit status:[1]
rm /var/run/ls.pid



Looks great, tested it successfully with bash. Testing with busybox 
ash does not work, but naturally, this is not needed on an LFS system.



This script is not intended to run with ash, the she bang line is as follows

#!/bin/bash --posix




I do have one question however:

Testing with invalid program and valid pidfile results in printing the 
pid and returning 0:


Test with vaild program and valid pidfile:


root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcd
3144root@keegan:~# echo $?
0

root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcf
3144root@keeganecho $?
0

In the second case, /sbin/dhcpcf does not exist, but the pidfile does, 
and belongs to /sbin/dhcpcd. Is this intended behaviour? Not that this 
really matters a lot in real-world, as the function certainly will not 
be called that way often.




It reads the pidfile and if the process is running with then

        for pid in ${list}; do
            if kill -0 "${pid}" 2> /dev/null;then
                pidlist+="${pid} "
            else
                exitstatus=1
            fi
        done

will return with an 0 exit status so yes it did 

Re: [lfs-support] init-functions: new pidofproc function: posix compliant: no sub shells: tested

2021-04-09 Thread Tim Tassonis

Hi Scott

On 4/9/21 2:40 AM, Scott Andrews wrote:

# Function:    pidofproc [-p pidfile] pathname
#   The pidofproc function shall return one or more process
#   identifiers for a particular daemon using the algorithm
#   given above. Only process identifiers of running
#   processes should be returned. Multiple process
#   identifiers shall be separated by a single space.
#   The pidofproc function shall return the LSB defined exit
#   status codes for "status". It shall return 0 if the
#   program is running and not 0 otherwise.
#
#   If the status action is requested, the init script will return
#   the following exit status codes.
#   0   program is running or service is OK
#   1   program is dead and /var/run pid file exists
#   2   program is dead and /var/lock lock file exists
#   3   program is not running
#   4   program or service status is unknown
#   5-99    reserved for future LSB use
#   100-149 reserved for distribution use
#   150-199 reserved for application use
#   200-254 reserved
function pidofproc {
     local pidfile=""
     local program=""
     local pid=""
     local pidlist=""
     local list=""
     local exitstatus=0
     # Process arguments
     while [[ $# -gt 0 ]]; do
         case "${1}" in
             -p) shift; pidfile="${1}" ;;
             *)    program="${1##*/}" ;;
         esac
         shift
     done
     # If a PID file is not specified, make one
     test -z "${pidfile}" && pidfile="/var/run/${program}.pid"
     # If a pid file exists and is readable, use it.
     if [[ -r "${pidfile}" ]]; then
         list=$(head -n1 "${pidfile}")
     else
         list=$(pidof "${program}")
         exitstatus=$?
         if [[ ${exitstatus} -gt 0 ]]; then exitstatus=4; fi
     fi
     if [[ exitstatus -eq 0 ]]; then
         # Figure out if all listed PIDs are running.
         for pid in ${list}; do
             if kill -0 "${pid}" 2> /dev/null; then
                 pidlist+="${pid} "
             else
                 exitstatus=1
             fi
         done
     fi
     if [[ -z "${pidlist}" ]]; then
         if [[ -f "${pidfile}" ]]; then
             exitstatus=1
         else
             exitstatus=3
         fi
     fi
     printf "%s" "${pidlist% }"
     return ${exitstatus}
}


Testing Function: pidofproc: Start
Testing pidofproc: valid program, no pidfile: running
     Returns pid and exit status of 0
     pidofproc /usr/sbin/dhcpcd: >:pid:[472] exit status:[0]
     pidofproc dhcpcd: >:pid:[472] exit status:[0]

Testing pidofproc: valid program, valid pidfile: running
     Returns pid and exit status of 0
     pidofproc -p /var/run/dhcpcd.pid /usr/sbin/dhcpcd: >:pid:[472] 
exit status:[0]
     pidofproc /usr/sbin/dhcpcd -p /var/run/dhcpcd.pid: >:pid:[472] 
exit status:[0]
     pidofproc -p /var/run/dhcpcd.pid dhcpcd: >:pid:[472] exit 
status:[0]
     pidofproc dhcpcd -p /var/run/dhcpcd.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: invalid program, no pidfile
     Returns no pid and exit status of 3
     pidofproc /usr/sbin/barf: >: pid:[] exit status:[3]

Testing pidofproc: valid program, invalid pidfile: running
     Returns pid and exit status of 0
     pidofproc -p barf.pid /usr/sbin/dhcpcd: >: pid:[472] exit 
status:[0]
     pidofproc /usr/sbin/dhcpcd -p barf.pid: >: pid:[472] exit 
status:[0]


Testing pidofproc: valid program, no pidfile: not running
     Returns no pid and exit status of 3
     pidofproc /bin/ls: >: pid:[] exit status:[3]

Testing pidofproc: valid program: valid pidfile: not running
     Returns no pid and exit status of 1

touch /var/run/ls.pid

     pidofproc /bin/ls: >: pid:[] exit status:[1]
     pidofproc -p /var/run/ls.pid /bin/ls: >: pid:[] exit status:[1]
rm /var/run/ls.pid



Looks great, tested it successfully with bash. Testing with busybox ash 
does not work, but naturally, this is not needed on an LFS system.


I do have one question however:

Testing with invalid program and valid pidfile results in printing the 
pid and returning 0:


Test with vaild program and valid pidfile:


root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcd
3144root@keegan:~# echo $?
0

root@keegan:~# ./pidofproc -p /var/run/dhcpcd-eno1.pid /sbin/dhcpcf
3144root@keeganecho $?
0

In the second case, /sbin/dhcpcf does not exist, but the pidfile does, 
and belongs to /sbin/dhcpcd. Is this intended behaviour? Not that this 
really matters a lot in real-world, as the function certainly will not 
be called that way often.



Another minor question: What is the difference between


if [[ -r "${pidfile}" ]]; then


and

if [ -r "${pidfile}" ]; then


I always use the latter, you seem to prefer the first, is there a 
difference I'm unaware of?


Bye
Tim

--
http://lists.linuxfromscratch.org/listinfo/lfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Do not top post on this list.

A: Because it messes up the