Skip to content

Commit 31d3437

Browse files
committed
sh/openrc-run: remove unnecessary loops
openrc-ruh.sh is only ever called from openrc-run.c, with a single command, so looping over "$@" is unnecessary
1 parent bdbe6e3 commit 31d3437

File tree

1 file changed

+79
-105
lines changed

1 file changed

+79
-105
lines changed

sh/openrc-run.sh.in

Lines changed: 79 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -277,31 +277,28 @@ if yesno "${rc_verbose:-$RC_VERBOSE}"; then
277277
export EINFO_VERBOSE
278278
fi
279279

280-
for _cmd; do
281-
if [ "$_cmd" != status -a "$_cmd" != describe ]; then
282-
# Apply any ulimit(s) defined
283-
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && \
284-
apply_ulimits ${rc_ulimit:-$RC_ULIMIT}
285-
# Apply cgroups settings if defined
286-
if [ "$(command -v cgroup_add_service)" = "cgroup_add_service" ]
280+
case $1 in
281+
status|describe) ;;
282+
*)
283+
# Apply any ulimit(s) defined
284+
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && apply_ulimits ${rc_ulimit:-$RC_ULIMIT}
285+
286+
# Apply cgroups settings if defined
287+
if [ "$(command -v cgroup_add_service)" = "cgroup_add_service" ]; then
288+
if grep -qs /sys/fs/cgroup /proc/1/mountinfo
287289
then
288-
if grep -qs /sys/fs/cgroup /proc/1/mountinfo
289-
then
290-
if [ -d /sys/fs/cgroup -a ! -w /sys/fs/cgroup ]; then
291-
eerror "No permission to apply cgroup settings"
292-
break
293-
fi
290+
if [ -d /sys/fs/cgroup -a ! -w /sys/fs/cgroup ]; then
291+
eerror "No permission to apply cgroup settings"
292+
break
294293
fi
295-
cgroup_add_service
296294
fi
297-
[ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] &&
298-
cgroup_set_limits
299-
[ "$(command -v cgroup2_set_limits)" = "cgroup2_set_limits" ] &&
300-
[ "$_cmd" = start ] &&
301-
cgroup2_set_limits
302-
break
295+
cgroup_add_service
303296
fi
304-
done
297+
298+
[ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] && cgroup_set_limits
299+
[ "$(command -v cgroup2_set_limits)" = "cgroup2_set_limits" ] && cgroup2_set_limits
300+
;;
301+
esac
305302

306303
eval "printf '%s\n' $required_dirs" | while read _d; do
307304
if [ -n "$_d" ] && [ ! -d "$_d" ]; then
@@ -327,95 +324,72 @@ if [ -n "$opts" ]; then
327324
ewarn "Please use extra_commands, extra_started_commands or extra_stopped_commands."
328325
fi
329326

330-
while [ -n "$1" ]; do
331-
# Special case depend
332-
if [ "$1" = depend ]; then
333-
shift
327+
# Special case depend
328+
if [ "$1" = depend ]; then
329+
shift
330+
331+
# Enter the dir of the init script to fix the globbing
332+
# bug 412677
333+
cd ${RC_SERVICE%/*}
334+
_depend
335+
cd /
336+
337+
return 0
338+
fi
334339

335-
# Enter the dir of the init script to fix the globbing
336-
# bug 412677
337-
cd ${RC_SERVICE%/*}
338-
_depend
339-
cd /
340-
continue
340+
# See if we have the required function and run it
341+
for _cmd in describe start stop status ${extra_commands:-$opts} $extra_started_commands $extra_stopped_commands; do
342+
[ "$_cmd" = "$1" ] || continue
343+
if [ "$(command -v "$1")" != "$1" ]; then
344+
case $_cmd in
345+
start|stop)
346+
exit 0;;
347+
*)
348+
eerror "$RC_SVCNAME: function '$1' defined but does not exist"
349+
exit 1;;
350+
esac
351+
break
341352
fi
342-
# See if we have the required function and run it
343-
for _cmd in describe start stop status ${extra_commands:-$opts} \
344-
$extra_started_commands $extra_stopped_commands
345-
do
353+
done
354+
355+
# If we're in the background, we may wish to fake some commands. We do this so we can
356+
# "start" ourselves from inactive which then triggers other services to start which
357+
# depend on us. A good example of this is openvpn.
358+
if yesno $IN_BACKGROUND; then
359+
for _cmd in $in_background_fake; do
346360
if [ "$_cmd" = "$1" ]; then
347-
if [ "$(command -v "$1")" = "$1" ]; then
348-
# If we're in the background, we may wish to
349-
# fake some commands. We do this so we can
350-
# "start" ourselves from inactive which then
351-
# triggers other services to start which
352-
# depend on us.
353-
# A good example of this is openvpn.
354-
if yesno $IN_BACKGROUND; then
355-
for _cmd in $in_background_fake; do
356-
if [ "$_cmd" = "$1" ]; then
357-
shift
358-
continue 3
359-
fi
360-
done
361-
fi
362-
# Check to see if we need to be started before
363-
# we can run this command
364-
for _cmd in $extra_started_commands; do
365-
if [ "$_cmd" = "$1" ]; then
366-
if verify_boot && ! service_started; then
367-
eerror "$RC_SVCNAME: cannot \`$1' as it has not been started"
368-
exit 1
369-
fi
370-
fi
371-
done
372-
# Check to see if we need to be stopped before
373-
# we can run this command
374-
for _cmd in $extra_stopped_commands; do
375-
if [ "$_cmd" = "$1" ]; then
376-
if verify_boot && ! service_stopped; then
377-
eerror "$RC_SVCNAME: cannot \`$1' as it has not been stopped"
378-
exit 1
379-
fi
380-
fi
381-
done
382-
unset _cmd
383-
case $1 in
384-
start|stop|status) verify_boot;;
385-
esac
386-
if [ "$(command -v "$1_pre")" = "$1_pre" ]
387-
then
388-
"$1"_pre || exit $?
389-
fi
390-
"$1" || exit $?
391-
if [ "$(command -v "$1_post")" = "$1_post" ]
392-
then
393-
"$1"_post || exit $?
394-
fi
395-
[ "$(command -v cgroup_cleanup)" = "cgroup_cleanup" ] &&
396-
[ "$1" = "stop" ] &&
397-
yesno "${rc_cgroup_cleanup}" && \
398-
cgroup_cleanup
399-
if [ "$(command -v cgroup2_remove)" = "cgroup2_remove" ]; then
400-
[ "$1" = stop ] || [ -z "${command}" ] &&
401-
cgroup2_remove
402-
fi
403-
shift
404-
continue 2
405-
else
406-
if [ "$_cmd" = "start" -o "$_cmd" = "stop" ]
407-
then
408-
shift
409-
continue 2
410-
else
411-
eerror "$RC_SVCNAME: function \`$1' defined but does not exist"
412-
exit 1
413-
fi
414-
fi
361+
exit 0
415362
fi
416363
done
417-
eerror "$RC_SVCNAME: unknown function \`$1'"
418-
exit 1
364+
fi
365+
366+
for _cmd in $extra_started_commands; do
367+
[ "$_cmd" = "$1" ] || continue
368+
if verify_boot && ! service_started; then
369+
eerror "$RC_SVCNAME: cannot '$1' as it has not been started"
370+
exit 1
371+
fi
372+
done
373+
374+
for _cmd in $extra_stopped_commands; do
375+
[ "$_cmd" = "$1" ] || continue
376+
if verify_boot && ! service_stopped; then
377+
eerror "$RC_SVCNAME: cannot '$1' as it has not been stopped"
378+
exit 1
379+
fi
419380
done
420381

382+
unset _cmd
383+
384+
case $1 in
385+
start|stop|status) verify_boot;;
386+
esac
387+
388+
[ "$(command -v "$1_pre")" = "$1_pre" ] && "$1"_pre || exit $?
389+
"$1" || exit $?
390+
[ "$(command -v "$1_post")" = "$1_post" ] && "$1"_post || exit $?
391+
392+
[ "$(command -v cgroup_cleanup)" = cgroup_cleanup ] && [ "$1" = stop ] && yesno "${rc_cgroup_cleanup}" && cgroup_cleanup
393+
[ "$(command -v cgroup2_remove)" = cgroup2_remove ] && { [ "$1" = stop ] || [ -z "${command}" ] } && cgroup2_remove
394+
421395
exit 0

0 commit comments

Comments
 (0)