aboutsummaryrefslogtreecommitdiffstats
path: root/bootscripts/contrib/lsb-v3/etc/init.d/lfs-functions
blob: b0fef4c299340bfe45f83d543029dec4939e1401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Begin /etc/init.d/lfs-functions
# Provides LFS specific functions for LSB style bootscripts

################################# chkstat() ###################################
# chk_stat checks the status of a script by checking for both a binary file   #
# to execute, and if set, a config file that may be needed for the program    #
# to run successfully.  The calling script will exit with a return value of 5 #
# if the binary does not exist, and a value of 6 if the needed config file is #
# unavailable as per LSB requirements.  This function accepts zero, one, or   #
# two string arguments.  If arguments are passed, the first must be a bin     #
# file.  If a second argument is passed, it is interpreted as the config      #
# file.  Optionally, zero arguments can be passed if BIN_FILE, and optinally  #
# CONFIG_FILE are set in the calling script.                                  #
###############################################################################
chk_stat()
{
    if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
        BIN_FILE="${1}"
        if [ -z "${2}" ]; then
            CONFIG_FILE=""
        else
            CONFIG_FILE="${2}"
        fi
    elif [ -z "${BIN_FILE}" ]; then
            echo "Usage: 'chk_stat BIN_FILE CONFIG_FILE'"
            exit 1 # Generic Error
    fi

    if [ ! -e "${BIN_FILE}" ]; then
        log_failure_msg "${BIN_FILE} not installed" &&
        exit 5
    fi

    if [ ! -z "${CONFIG_FILE}" ]; then
        if [ ! -e "${CONFIG_FILE}" ]; then
            log_failure_msg "${CONFIG_FILE} does not exist" &&
            exit 6
        fi
    fi
}

################################ loadproc() ###################################
# loadproc is just a wraper to start_daemon for simple scripts, which will    #
# require no arguments if $BIN_FILE is set.                                   #
###############################################################################
loadproc()
{
    start_daemon "${BIN_FILE}" "${@}"
}

################################ endproc() ####################################
# endproc, like loadproc, is just a wraper to killproc for simplicity and is  #
# dependent on $BIN_FILE being set.                                           #
###############################################################################
endproc()
{
    killproc "${BIN_FILE}" "${@}"
}

############################### statusproc() ##################################
# statusproc checks the status of a particular binary and displays the        #
# appropriate message (running or not running) and exits on the return value  #
# of pidofproc.  This function accepts two string arguments or zero arguments #
# if BIN_FILE and MESSAGE are set, else it requires the bin file as the first #
# argument, and the message as the second.  Both must be enclosed in quotes.  #
###############################################################################
statusproc()
{
    if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
        BIN_FILE="${1}"
        MESSAGE="${2}"
    elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
        echo "Usage: 'statusproc BIN_FILE MESSAGE'"
        exit 1 # Generic Error
    fi

    pidlist=`pidofproc "${BIN_FILE}"`
    STATUS=$?
    echo "Checking ${MESSAGE} status:"
    if [ "${STATUS}" -eq "0" ]; then
        log_success_msg "Running with PID(s) ${pidlist}"
    else
        log_warning_msg "Not running!"
    fi

    return "${STATUS}"
}

############################### reloadproc() ##################################
# reloadproc sends a HUP signal to the running program (relaod configuration) #
# It optionally, using the -force switch, checks the status of a particular   #
# program and starts it if it is not already running. This function accepts   #
# one optional switch (must be the first argument), and either two, or zero   #
# string arguments.  If BIN_FILE and MESSAGE are set in the script's          #
# environment, it will use those values,  else it requires the bin file as    #
# the first argument (following -force if used), and the message as the       #
# second. Both must be enclosed in quotes. If the force option is used, it    #
# follows the LSB definition of 'force-reload' - the program is started if    #
# not already running.                                                        #
###############################################################################
reloadproc()
{
    local force="0"
    if [ "${#}" -gt "0" -a "${1}" = "-force" ]; then
        force="1"
        shift 1
    fi

    if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
        BIN_FILE="${1}"
        MESSAGE="${2}"
    elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
        echo "Usage: 'reloadproc BIN_FILE MESSAGE'"
        exit 1 # Generic Error
    fi

    

}

############################## evaluate_retval() ###############################
# evaluate_retval requires that you pass exactly one evaluation parameter of   #
# (start, stop, other) based on the previous action that is being evaluated.   #
# This function is intended for use with start_daemon and killproc to          #
# interpret the LSB exit codes properly, othewise the checks only for success  #
# or failure.                                                                  #
################################################################################
evaluate_retval()
{
    local error_value="${?}"

    # Handle LSB defined return values
    case "${1}" in

      start)
        case "${error_value}" in
          0)
            log_success_msg "Starting ${MESSAGE} "
            return "${error_value}"
          ;;
          2)
            log_failure_msg "Starting ${MESSAGE} Error: Invalid argument!"
            return "${error_value}"
          ;;
          5)
            log_failure_msg "Starting ${MESSAGE} Error: Not available!"
            return "${error_value}"
          ;;
          *)
            log_failure_msg "Starting ${MESSAGE} Error: General failure!"
            return "${error_value}"
          ;;
        esac
      ;;

      stop)
        case "${error_value}" in
          0)
            log_success_msg "Stopping ${MESSAGE} "
            return "${error_value}"
            ;;
          2)
            log_failure_msg "Stopping ${MESSAGE} Error: Invalid argument!"
            return "${error_value}"
            ;;
          5)
            log_failure_msg "Stopping ${MESSAGE} Error: Not available!"
            return "${error_value}"
            ;;
          7)
            log_warning_msg "Stopping ${MESSAGE} Warning: Not running!"
            return "${error_value}"
            ;;
          *)
            log_failure_msg "Stopping ${MESSAGE} Error: General failure!"
            return "${error_value}"
            ;;
          esac
       ;;

       force-reload)
         message="Forcefully reloading "
       ;;

       reload)
         message="Reloading "
       ;;

       restart)
         message="Restarting "
       ;;

       try-restart)
         message="Trying restart "
       ;;

       standard)
         # $message or $MESSAGE must be set, but not both in order
         # to use the 'standard' target.
       ;;
    esac

    # Print messages for the generic force-reload, reload, restart, 
    # and try-restart targets
    if [ "${error_value}" -eq "0" ]
    then
        log_success_msg "${message}${MESSAGE} "
        return "${error_value}"
    else
        log_failure_msg "${message}${MESSAGE} "
        return "${error_value}"
    fi
}