1# -*-Shell-script-*-
2#
3# functions     This file contains functions to be used by most or all
4#               shell scripts in the /etc/init.d directory.
5#
6#
7# SPDX-License-Identifier: GPL-2.0-only
8#
9
10NORMAL="\\033[0;39m"         # Standard console grey
11SUCCESS="\\033[1;32m"        # Success is green
12WARNING="\\033[1;33m"        # Warnings are yellow
13FAILURE="\\033[1;31m"        # Failures are red
14INFO="\\033[1;36m"           # Information is light cyan
15BRACKET="\\033[1;34m"        # Brackets are blue
16
17# NOTE: The pidofproc () doesn't support the process which is a script unless
18#       the pidof supports "-x" option. If you want to use it for such a
19#       process:
20#       1) If there is no "pidof -x", replace the "pidof $1" with another
21#          command like(for core-image-minimal):
22#            ps | awk '/'"$1"'/ {print $1}'
23#       Or
24#       2) If there is "pidof -x", replace "pidof" with "pidof -x".
25#
26# pidofproc - print the pid of a process
27# $1: the name of the process
28pidofproc () {
29
30	# pidof output null when no program is running, so no "2>/dev/null".
31	pid=`pidof $1`
32	status=$?
33	case $status in
34	0)
35		echo $pid
36		return 0
37		;;
38	127)
39		echo "ERROR: command pidof not found" >&2
40		exit 127
41		;;
42	*)
43		return $status
44		;;
45	esac
46}
47
48machine_id() { # return the machine ID
49	awk 'BEGIN { FS=": " } /Hardware/ \
50		{ gsub(" ", "_", $2); print tolower($2) } ' </proc/cpuinfo
51}
52
53killproc() { # kill the named process(es)
54	pid=`pidofproc $1` && kill $pid
55}
56
57status() {
58    local pid
59    if [ "$#" = 0 ]; then
60        echo "Usage: status {program}"
61        return 1
62    fi
63    pid=`pidofproc $1`
64    if [ -n "$pid" ]; then
65        echo "$1 (pid $pid) is running..."
66        return 0
67    else
68        echo "$1 is stopped"
69    fi
70    return 3
71}
72
73success() {
74    echo -n -e "${BRACKET}[${SUCCESS}  OK  ${BRACKET}]${NORMAL}"
75    return 0
76}
77
78failure() {
79    local rc=$*
80    echo -n -e "${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
81    return $rc
82}
83
84warning() {
85    local rc=$*
86    echo -n -e "${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
87    return $rc
88}
89
90passed() {
91    local rc=$*
92    echo -n -e "${BRACKET}[${SUCCESS} PASS ${BRACKET}]${NORMAL}"
93    return $rc
94}
95