]> git.ipfire.org Git - people/arne_f/ipfire-3.x.git/blob - pkgs/initscripts/functions
Change file layout of the makefiles.
[people/arne_f/ipfire-3.x.git] / pkgs / initscripts / functions
1 #!/bin/sh
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007, 2008 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21 #
22 # Partly based on scripts by DJ Lucas - dj@linuxfromscratch.org
23 #
24
25 # Distro Information
26 DISTRO="$(</etc/system-release)" # The distro name
27 DISTRO_CONTACT="http://bugtracker.ipfire.org" # Bug report address
28
29 # This sets default terminal options.
30 # stty sane - this has been removed as nobody recalls
31 # the reason for it in the first place - if no problems arize,
32 # then it will be removed completely at a later date.
33
34 # Setup default values for the environment
35 umask 022
36
37 # If we boot, we should only allow the
38 # use tools that are available in /bin:/sbin
39 if [ -n "${UPSTART_JOB}" ]; then
40 PATH="/bin:/sbin"
41 fi
42
43 # Find current screen size
44 if [ -z "${COLUMNS}" ]; then
45 COLUMNS=$(stty size)
46 COLUMNS=${COLUMNS##* }
47 fi
48
49 # When using remote connections, such as a serial port, stty size returns 0
50 if [ "${COLUMNS}" = "0" ]; then
51 COLUMNS=80
52 fi
53
54 ## Measurements for positioning result messages
55 COL=$((${COLUMNS} - 8))
56 WCOL=$((${COL} - 2))
57
58 # Set Cursur Position Commands, used via echo -e
59 SET_COL="\\033[${COL}G" # at the $COL char
60 SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
61 CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
62
63 # Normal colors
64 CLR_NORM_BLK="\\033[0;30m" # black
65 CLR_NORM_RED="\\033[0;31m" # red
66 CLR_NORM_GRN="\\033[0;32m" # green
67 CLR_NORM_YEL="\\033[0;33m" # yellow
68 CLR_NORM_BLU="\\033[0;34m" # blue
69 CLR_NORM_MAG="\\033[0;35m" # magenta
70 CLR_NORM_CYN="\\033[0;36m" # cyan
71 CLR_NORM_WHT="\\033[0;37m" # white
72 CLR_NORM_GRY="\\033[0;39m" # grey
73
74 # Emphased colors
75 CLR_BOLD_BLK="\\033[1;30m" # black
76 CLR_BOLD_RED="\\033[1;31m" # red
77 CLR_BOLD_GRN="\\033[1;32m" # green
78 CLR_BOLD_YEL="\\033[1;33m" # yellow
79 CLR_BOLD_BLU="\\033[1;34m" # blue
80 CLR_BOLD_MAG="\\033[1;35m" # magenta
81 CLR_BOLD_CYN="\\033[1;36m" # cyan
82 CLR_BOLD_WHT="\\033[1;37m" # white
83 CLR_BOLD_GRY="\\033[1;39m" # grey
84
85 # Background colors
86 CLR_BACK_BLK="\\033[40m" # black
87 CLR_BACK_RED="\\033[41m" # red
88 CLR_BACK_GRN="\\033[42m" # green
89 CLR_BACK_YEL="\\033[43m" # yellow
90 CLR_BACK_BLU="\\033[44m" # blue
91 CLR_BACK_MAG="\\033[45m" # magenta
92 CLR_BACK_CYN="\\033[46m" # cyan
93 CLR_BACK_WHT="\\033[47m" # white
94
95 # Action colors
96 BOLD=$CLR_BOLD_GRY
97 DONE=$CLR_BOLD_GRN
98 SKIP=$CLR_BOLD_BLU
99 WARN=$CLR_BOLD_MAG
100 FAIL=$CLR_BOLD_RED
101 NORMAL=$CLR_NORM_GRY
102
103 # Color hooks
104 BRACKET_L="${CLR_BOLD_BLU}[${NORMAL}"
105 BRACKET_R="${CLR_BOLD_BLU}]${NORMAL}"
106
107 # Define custom colors used in messages printed to the screen
108 BRACKET=${CLR_BOLD_BLU} # Blue
109 FAILURE=${CLR_BOLD_RED} # Red
110 INFO=${CLR_BOLD_CYN} # Cyan
111 NORMAL=${CLR_NORM_GRY} # Grey
112 SUCCESS=${CLR_BOLD_GRN} # Green
113 WARNING=${CLR_BOLD_YEL} # Yellow
114
115 # Prefix boot messages for easier reading on framebuffer consoles
116 PREFIX_SUCCESS=" ${SUCCESS}*${NORMAL} "
117 PREFIX_WARNING="${WARNING}**${NORMAL} "
118 PREFIX_FAILURE="${FAILURE}***${NORMAL}"
119
120 welcome_message="Welcome to ${INFO}${DISTRO}${NORMAL}"
121 welcome_message_length=$((${#DISTRO} + 11))
122
123 # Error message displayed when a script's exit value is not zero
124 print_error_msg() {
125 # ${link} and ${error_value} are defined by the rc script
126 echo -e "${FAILURE}FAILURE: You should not be reading this error message."
127 echo -e ""
128 echo -e -n "${FAILURE}It means that an unforseen error took place in"
129 echo -e -n "${INFO} ${link}"
130 echo -e "${FAILURE},"
131 echo -e "${FAILURE}which exited with a return value of ${error_value}."
132 echo -e ""
133 echo -e -n "${FAILURE}If you are able to track this error down to a bug"
134 echo -e "${FAILURE}in one of the files"
135 echo -e -n "provided by ${INFO}${DISTRO}${FAILURE}, "
136 echo -e -n "${FAILURE}please be so kind to inform us at "
137 echo -e "${INFO}${DISTRO_CONTACT}${FAILURE}.${NORMAL}"
138 echo -e ""
139 echo -e "${INFO}Press Enter to continue..."
140 echo -e "${NORMAL}"
141 read ENTER
142 }
143
144 ################################################################################
145 # log_success_msg() #
146 # Usage: log_success_msg [$MESSAGE | "message"] #
147 # #
148 # Purpose: Print a successful status message to the screen and optionally #
149 # a boot log file. #
150 # #
151 # Inputs: accepts one string value, either a quoted string or optionally #
152 # the value of $MESSAGE if set in the running environment. #
153 # #
154 # Return values: Not used #
155 ################################################################################
156 log_success_msg() {
157 echo -n -e "${PREFIX_SUCCESS}${INDENT}${@}"
158 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
159 }
160
161 ################################################################################
162 # log_failure_msg() #
163 # Usage: log_failure_msg [$MESSAGE | "message"] #
164 # #
165 # Purpose: Print a failure status message to the screen and optionally #
166 # a boot log file. #
167 # #
168 # Inputs: accepts one string value, either a quoted string or optionally #
169 # the value of $MESSAGE if set in the running environment. #
170 # #
171 # Return values: Not used #
172 ################################################################################
173 log_failure_msg() {
174 echo -n -e "${PREFIX_FAILURE}${INDENT}${@}"
175 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
176 }
177
178 ################################################################################
179 # log_warning_msg() #
180 # Usage: log_warning_msg [$MESSAGE | "message"] #
181 # #
182 # Purpose: Print a warning status message to the screen and optionally #
183 # a boot log file. #
184 # #
185 # Inputs: accepts one string value, either a quoted string or optionally #
186 # the value of $MESSAGE if set in the running environment. #
187 # #
188 # Return values: Not used #
189 ################################################################################
190 log_warning_msg() {
191 echo -n -e "${PREFIX_WARNING}${INDENT}${@}"
192 echo -e "${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
193 }
194
195 ############################## evaluate_retval() ###############################
196 # evaluate_retval requires that you pass exactly one evaluation parameter of #
197 # (start, stop, other) based on the previous action that is being evaluated. #
198 # This function is intended for use with start_daemon and killproc to #
199 # interpret the LSB exit codes properly, othewise the checks only for success #
200 # or failure. #
201 ################################################################################
202 evaluate_retval() {
203 local error_value="${?}"
204
205 # Handle LSB defined return values
206 case "${1}" in
207 start)
208 case "${error_value}" in
209 0)
210 log_success_msg "Starting ${MESSAGE} "
211 return "${error_value}"
212 ;;
213 2)
214 log_failure_msg "Starting ${MESSAGE} Error: Invalid argument!"
215 return "${error_value}"
216 ;;
217 5)
218 log_failure_msg "Starting ${MESSAGE} Error: Not available!"
219 return "${error_value}"
220 ;;
221 *)
222 log_failure_msg "Starting ${MESSAGE} Error: General failure!"
223 return "${error_value}"
224 ;;
225 esac
226 ;;
227
228 stop)
229 case "${error_value}" in
230 0)
231 log_success_msg "Stopping ${MESSAGE} "
232 return "${error_value}"
233 ;;
234 2)
235 log_failure_msg "Stopping ${MESSAGE} Error: Invalid argument!"
236 return "${error_value}"
237 ;;
238 5)
239 log_failure_msg "Stopping ${MESSAGE} Error: Not available!"
240 return "${error_value}"
241 ;;
242 7)
243 log_warning_msg "Stopping ${MESSAGE} Warning: Not running!"
244 return "${error_value}"
245 ;;
246 *)
247 log_failure_msg "Stopping ${MESSAGE} Error: General failure!"
248 return "${error_value}"
249 ;;
250 esac
251 ;;
252
253 force-reload)
254 message="Forcefully reloading "
255 ;;
256
257 reload)
258 message="Reloading "
259 ;;
260
261 restart)
262 message="Restarting "
263 ;;
264
265 try-restart)
266 message="Trying restart "
267 ;;
268
269 standard)
270 # $message or $MESSAGE must be set, but not both in order
271 # to use the 'standard' target.
272 ;;
273 esac
274
275 # Print messages for the generic force-reload, reload, restart,
276 # and try-restart targets
277 if [ "${error_value}" = "0" ]; then
278 log_success_msg "${message}${MESSAGE} "
279 return "${error_value}"
280 else
281 log_failure_msg "${message}${MESSAGE} "
282 return "${error_value}"
283 fi
284 }