This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions
287 lines (242 loc) · 5.7 KB
/
functions
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
# initscripts functions
#
# width:
STAT_COL=80
if [ ! -t 1 ]; then
USECOLOR=""
else
# is /usr/share/terminfo already mounted, and TERM recognized?
/bin/tput cols &>/dev/null
if [ $? -eq 0 ]; then
STAT_COL=$(/bin/tput cols)
fi
fi
if [ "0$STAT_COL" -eq 0 ]; then
# if output was 0 (serial console), set default width to 80
STAT_COL=80
USECOLOR=""
fi
# we use 13 characters for our own stuff
STAT_COL=$(($STAT_COL - 13))
# disable colors on broken terminals
TERM_COLORS="$(/bin/tput colors 2>/dev/null)"
if [ $? = 3 ]; then
TERM_COLORS=8
elif [ -n "${TERM_COLORS}" ]; then
case "${TERM_COLORS}" in
*[!0-9]*)
USECOLOR=""
;;
*)
[ "${TERM_COLORS}" -lt 8 ] && USECOLOR=""
;;
esac
else
USECOLOR=""
fi
unset TERM_COLORS
# clear the TZ envvar, so daemons always respect /etc/localtime
unset TZ
# colors:
if [ "$USECOLOR" = "YES" -o "$USECOLOR" = "yes" ]; then
C_MAIN="\033[1;37;40m" # main text
C_OTHER="\033[1;34;40m" # prefix & brackets
C_SEPARATOR="\033[1;30;40m" # separator
C_BUSY="\033[0;36;40m" # busy
C_FAIL="\033[1;31;40m" # failed
C_DONE="\033[1;37;40m" # completed
C_BKGD="\033[1;35;40m" # backgrounded
C_H1="\033[1;37;40m" # highlight text 1
C_H2="\033[1;36;40m" # highlight text 2
C_CLEAR="\033[1;0m"
fi
if [ -t 1 -a $TERM != "mach-color" ]; then
SAVE_POSITION="\033[s"
RESTORE_POSITION="\033[u"
DEL_TEXT="\033[$(($STAT_COL+4))G"
else
SAVE_POSITION=""
RESTORE_POSITION=""
if [ $TERM="mach-color" ]; then
DEL_TEXT="\033[$(($STAT_COL+4))G"
else
DEL_TEXT=""
fi
fi
# prefixes:
PREFIX_REG="::"
PREFIX_HL=" >"
# functions:
deltext() {
printf "${DEL_TEXT}"
}
printhl() {
printf "${C_OTHER}${PREFIX_HL} ${C_H1}${1}${C_CLEAR} \n"
}
printsep() {
printf "\n${C_SEPARATOR} ------------------------------\n"
}
stat_bkgd() {
printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
deltext
printf " ${C_OTHER}[${C_BKGD}BKGD${C_OTHER}]${C_CLEAR} "
}
stat_busy() {
printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
printf "${SAVE_POSITION}"
deltext
printf " ${C_OTHER}[${C_BUSY}BUSY${C_OTHER}]${C_CLEAR} "
}
stat_append() {
printf "${RESTORE_POSITION}"
printf -- "${C_MAIN}${1}${C_CLEAR}"
printf "${SAVE_POSITION}"
}
stat_done() {
deltext
printf " ${C_OTHER}[${C_DONE}DONE${C_OTHER}]${C_CLEAR} \n"
}
stat_fail() {
deltext
printf " ${C_OTHER}[${C_FAIL}FAIL${C_OTHER}]${C_CLEAR} \n"
}
stat_die() {
retval=1
[ "$1" = "" ] || retval=$1
stat_fail
exit $retval
}
status() {
stat_busy "$1"
shift
$* >/dev/null 2>&1
if [ $? -eq 0 ]; then
stat_done
return 0
else
stat_fail
return 1
fi
}
# usage : in_array( $needle, $haystack )
# return : 0 - found
# 1 - not found
# Copied from makepkg
in_array() {
local needle=$1; shift
[ -z "$1" ] && return 1 # Not Found
local item
for item in "$@"; do
local c="${item:0:1}"
if [ "x$c" = "x@" ]; then
item="${item:1}"
fi
[ "$item" = "$needle" ] && return 0 # Found
done
return 1 # Not Found
}
# daemons:
add_daemon() {
[ -d /var/run/daemons ] || /bin/mkdir -p /var/run/daemons
/bin/touch /var/run/daemons/$1
}
rm_daemon() {
/bin/rm -f /var/run/daemons/$1
}
ck_daemon() {
[ -f /var/run/daemons/$1 ] && return 1
return 0
}
ck_depends() {
for daemon in $@; do
if ck_daemon $daemon; then
/etc/rc.d/$daemon start
fi
done
}
start_daemon() {
/etc/rc.d/$1 start
}
start_daemon_bkgd() {
stat_bkgd "Starting $1"
(/etc/rc.d/$1 start) &>/dev/null &
}
stop_daemon() {
/etc/rc.d/$1 stop
}
# Status functions
status_started() {
deltext
echo -ne "$C_OTHER[${C_STRT}STARTED$C_OTHER]$C_CLEAR "
}
status_stopped() {
deltext
echo -ne "$C_OTHER[${C_STRT}STOPPED$C_OTHER]$C_CLEAR "
}
ck_status() {
ck_daemon $1
if [ $? -eq 1 ]; then
status_started
else
status_stopped
fi
}
# Function for setting translators. Blatantly ripped from MAKEDEV
st() {
local NODE="$1"
local OWNER="$2"
local PERM="$3"
shift 3
if settrans -ck "$NODE" "$@"; then
chown "$OWNER" "$NODE"
chmod "$PERM" "$NODE"
fi
}
###############################
# Custom hooks in initscripts #
###############################
# Hooks can be used to include custom code in various places in the rc.* scripts
#
# Define a hook function in a functions.d file using:
# function_name() {
# ...
# }
# add_hook hook_name function_name
# It is allowed to register several hook functions for the same hook
# Is is also allowed to register the same hook function for several hooks
#
# Currently, the following hooks exist:
# sysinit_start: at the beginning of rc.sysinit
# multi_start: at the beginning of rc.multi
# single_start: at the beginning of single-user mode (in runsystem)
# shutdown_start: at the beginning of rc.shutdown
# sysinit_end: at the end of rc.sysinit
# multi_end: at the end of rc.multi
# single_end: at the end of single-user mode (in runsystem)
# sysinit_premount: before local filesystems are mounted, but after root is mounted read-write in rc.sysinit - NOT IMPLEMENTED YET
# shutdown_prekillall: before all processes are being killed in rc.shutdown - NOT IMPLEMENTED YET
# shutdown_postkillall: after all processes have been killed in rc.shutdown - NOT IMPLEMENTED YET
# shutdown_poweroff: directly before powering off in rc.shutdown - NOT IMPLEMENTED YET
#
# Make sure to never override the add_hook and run_hook functions via functions.d
declare -A hook_funcs
add_hook() {
[ -z "$1" -o -z "$2" ] && return 1
hook_funcs["$1"]="${hook_funcs["$1"]} $2"
}
run_hook() {
local func
[ -z "$1" ] && return 1
for func in ${hook_funcs["$1"]}; do
${func}
done
}
# Source additional functions at the end to allow overrides
for f in /etc/rc.d/functions.d/*; do
if [ -e $f ]; then
. $f
fi
done
# End of file
# vim: set ft=sh sw=2 ts=2 et: